pngrutil.c 124 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132
  1. /* pngrutil.c - utilities to read a PNG file
  2. *
  3. * Last changed in libpng 1.5.22 [March 26, 2015]
  4. * Copyright (c) 1998-2015 Glenn Randers-Pehrson
  5. * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  6. * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
  7. *
  8. * This code is released under the libpng license.
  9. * For conditions of distribution and use, see the disclaimer
  10. * and license in png.h
  11. *
  12. * This file contains routines that are only called from within
  13. * libpng itself during the course of reading an image.
  14. */
  15. #include "pngpriv.h"
  16. #ifdef PNG_READ_SUPPORTED
  17. png_uint_32 PNGAPI
  18. png_get_uint_31(png_structp png_ptr, png_const_bytep buf)
  19. {
  20. png_uint_32 uval = png_get_uint_32(buf);
  21. if (uval > PNG_UINT_31_MAX)
  22. png_error(png_ptr, "PNG unsigned integer out of range");
  23. return (uval);
  24. }
  25. #if defined(PNG_READ_gAMA_SUPPORTED) || defined(PNG_READ_cHRM_SUPPORTED)
  26. /* The following is a variation on the above for use with the fixed
  27. * point values used for gAMA and cHRM. Instead of png_error it
  28. * issues a warning and returns (-1) - an invalid value because both
  29. * gAMA and cHRM use *unsigned* integers for fixed point values.
  30. */
  31. #define PNG_FIXED_ERROR (-1)
  32. static png_fixed_point /* PRIVATE */
  33. png_get_fixed_point(png_structp png_ptr, png_const_bytep buf)
  34. {
  35. png_uint_32 uval = png_get_uint_32(buf);
  36. if (uval <= PNG_UINT_31_MAX)
  37. return (png_fixed_point)uval; /* known to be in range */
  38. /* The caller can turn off the warning by passing NULL. */
  39. if (png_ptr != NULL)
  40. png_warning(png_ptr, "PNG fixed point integer out of range");
  41. return PNG_FIXED_ERROR;
  42. }
  43. #endif
  44. #ifdef PNG_READ_INT_FUNCTIONS_SUPPORTED
  45. /* NOTE: the read macros will obscure these definitions, so that if
  46. * PNG_USE_READ_MACROS is set the library will not use them internally,
  47. * but the APIs will still be available externally.
  48. *
  49. * The parentheses around "PNGAPI function_name" in the following three
  50. * functions are necessary because they allow the macros to co-exist with
  51. * these (unused but exported) functions.
  52. */
  53. /* Grab an unsigned 32-bit integer from a buffer in big-endian format. */
  54. png_uint_32 (PNGAPI
  55. png_get_uint_32)(png_const_bytep buf)
  56. {
  57. png_uint_32 uval =
  58. ((png_uint_32)(*(buf )) << 24) +
  59. ((png_uint_32)(*(buf + 1)) << 16) +
  60. ((png_uint_32)(*(buf + 2)) << 8) +
  61. ((png_uint_32)(*(buf + 3)) ) ;
  62. return uval;
  63. }
  64. /* Grab a signed 32-bit integer from a buffer in big-endian format. The
  65. * data is stored in the PNG file in two's complement format and there
  66. * is no guarantee that a 'png_int_32' is exactly 32 bits, therefore
  67. * the following code does a two's complement to native conversion.
  68. */
  69. png_int_32 (PNGAPI
  70. png_get_int_32)(png_const_bytep buf)
  71. {
  72. png_uint_32 uval = png_get_uint_32(buf);
  73. if ((uval & 0x80000000) == 0) /* non-negative */
  74. return uval;
  75. uval = (uval ^ 0xffffffff) + 1; /* 2's complement: -x = ~x+1 */
  76. return -(png_int_32)uval;
  77. }
  78. /* Grab an unsigned 16-bit integer from a buffer in big-endian format. */
  79. png_uint_16 (PNGAPI
  80. png_get_uint_16)(png_const_bytep buf)
  81. {
  82. /* ANSI-C requires an int value to accomodate at least 16 bits so this
  83. * works and allows the compiler not to worry about possible narrowing
  84. * on 32 bit systems. (Pre-ANSI systems did not make integers smaller
  85. * than 16 bits either.)
  86. */
  87. unsigned int val =
  88. ((unsigned int)(*buf) << 8) +
  89. ((unsigned int)(*(buf + 1)));
  90. return (png_uint_16)val;
  91. }
  92. #endif /* PNG_READ_INT_FUNCTIONS_SUPPORTED */
  93. /* Read and check the PNG file signature */
  94. void /* PRIVATE */
  95. png_read_sig(png_structp png_ptr, png_infop info_ptr)
  96. {
  97. png_size_t num_checked, num_to_check;
  98. /* Exit if the user application does not expect a signature. */
  99. if (png_ptr->sig_bytes >= 8)
  100. return;
  101. num_checked = png_ptr->sig_bytes;
  102. num_to_check = 8 - num_checked;
  103. #ifdef PNG_IO_STATE_SUPPORTED
  104. png_ptr->io_state = PNG_IO_READING | PNG_IO_SIGNATURE;
  105. #endif
  106. /* The signature must be serialized in a single I/O call. */
  107. png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
  108. png_ptr->sig_bytes = 8;
  109. if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
  110. {
  111. if (num_checked < 4 &&
  112. png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
  113. png_error(png_ptr, "Not a PNG file");
  114. else
  115. png_error(png_ptr, "PNG file corrupted by ASCII conversion");
  116. }
  117. if (num_checked < 3)
  118. png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
  119. }
  120. /* Read the chunk header (length + type name).
  121. * Put the type name into png_ptr->chunk_name, and return the length.
  122. */
  123. png_uint_32 /* PRIVATE */
  124. png_read_chunk_header(png_structp png_ptr)
  125. {
  126. png_byte buf[8];
  127. png_uint_32 length;
  128. #ifdef PNG_IO_STATE_SUPPORTED
  129. png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_HDR;
  130. #endif
  131. /* Read the length and the chunk name.
  132. * This must be performed in a single I/O call.
  133. */
  134. png_read_data(png_ptr, buf, 8);
  135. length = png_get_uint_31(png_ptr, buf);
  136. /* Put the chunk name into png_ptr->chunk_name. */
  137. png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(buf+4);
  138. png_debug2(0, "Reading %lx chunk, length = %lu",
  139. (unsigned long)png_ptr->chunk_name, (unsigned long)length);
  140. /* Reset the crc and run it over the chunk name. */
  141. png_reset_crc(png_ptr);
  142. png_calculate_crc(png_ptr, buf + 4, 4);
  143. /* Check to see if chunk name is valid. */
  144. png_check_chunk_name(png_ptr, png_ptr->chunk_name);
  145. #ifdef PNG_IO_STATE_SUPPORTED
  146. png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_DATA;
  147. #endif
  148. return length;
  149. }
  150. /* Read data, and (optionally) run it through the CRC. */
  151. void /* PRIVATE */
  152. png_crc_read(png_structp png_ptr, png_bytep buf, png_size_t length)
  153. {
  154. if (png_ptr == NULL)
  155. return;
  156. png_read_data(png_ptr, buf, length);
  157. png_calculate_crc(png_ptr, buf, length);
  158. }
  159. /* Optionally skip data and then check the CRC. Depending on whether we
  160. * are reading a ancillary or critical chunk, and how the program has set
  161. * things up, we may calculate the CRC on the data and print a message.
  162. * Returns '1' if there was a CRC error, '0' otherwise.
  163. */
  164. int /* PRIVATE */
  165. png_crc_finish(png_structp png_ptr, png_uint_32 skip)
  166. {
  167. png_size_t i;
  168. png_size_t istop = png_ptr->zbuf_size;
  169. for (i = (png_size_t)skip; i > istop; i -= istop)
  170. {
  171. png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
  172. }
  173. if (i != 0)
  174. {
  175. png_crc_read(png_ptr, png_ptr->zbuf, i);
  176. }
  177. if (png_crc_error(png_ptr))
  178. {
  179. if (PNG_CHUNK_ANCILLIARY(png_ptr->chunk_name) ?
  180. !(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) :
  181. (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE))
  182. {
  183. png_chunk_warning(png_ptr, "CRC error");
  184. }
  185. else
  186. {
  187. png_chunk_benign_error(png_ptr, "CRC error");
  188. return (0);
  189. }
  190. return (1);
  191. }
  192. return (0);
  193. }
  194. /* Compare the CRC stored in the PNG file with that calculated by libpng from
  195. * the data it has read thus far.
  196. */
  197. int /* PRIVATE */
  198. png_crc_error(png_structp png_ptr)
  199. {
  200. png_byte crc_bytes[4];
  201. png_uint_32 crc;
  202. int need_crc = 1;
  203. if (PNG_CHUNK_ANCILLIARY(png_ptr->chunk_name))
  204. {
  205. if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
  206. (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
  207. need_crc = 0;
  208. }
  209. else /* critical */
  210. {
  211. if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
  212. need_crc = 0;
  213. }
  214. #ifdef PNG_IO_STATE_SUPPORTED
  215. png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_CRC;
  216. #endif
  217. /* The chunk CRC must be serialized in a single I/O call. */
  218. png_read_data(png_ptr, crc_bytes, 4);
  219. if (need_crc != 0)
  220. {
  221. crc = png_get_uint_32(crc_bytes);
  222. return ((int)(crc != png_ptr->crc));
  223. }
  224. else
  225. return (0);
  226. }
  227. #ifdef PNG_READ_COMPRESSED_TEXT_SUPPORTED
  228. static png_size_t
  229. png_inflate(png_structp png_ptr, png_bytep data, png_size_t size,
  230. png_bytep output, png_size_t output_size)
  231. {
  232. png_size_t count = 0;
  233. /* HACK: added in libpng 1.5.18: the progressive reader always leaves
  234. * png_ptr->zstream in a non-reset state. This causes a reset if it needs to
  235. * be used again. This only copes with that one specific error; see libpng
  236. * 1.6 for a better solution.
  237. */
  238. if ((png_ptr->flags & PNG_FLAG_ZSTREAM_PROGRESSIVE) != 0)
  239. {
  240. (void)inflateReset(&png_ptr->zstream);
  241. png_ptr->flags &= ~PNG_FLAG_ZSTREAM_PROGRESSIVE;
  242. }
  243. /* zlib can't necessarily handle more than 65535 bytes at once (i.e. it can't
  244. * even necessarily handle 65536 bytes) because the type uInt is "16 bits or
  245. * more". Consequently it is necessary to chunk the input to zlib. This
  246. * code uses ZLIB_IO_MAX, from pngpriv.h, as the maximum (the maximum value
  247. * that can be stored in a uInt.) It is possible to set ZLIB_IO_MAX to a
  248. * lower value in pngpriv.h and this may sometimes have a performance
  249. * advantage, because it forces access of the input data to be separated from
  250. * at least some of the use by some period of time.
  251. */
  252. png_ptr->zstream.next_in = data;
  253. /* avail_in is set below from 'size' */
  254. png_ptr->zstream.avail_in = 0;
  255. while (1)
  256. {
  257. int ret, avail;
  258. /* The setting of 'avail_in' used to be outside the loop; by setting it
  259. * inside it is possible to chunk the input to zlib and simply rely on
  260. * zlib to advance the 'next_in' pointer. This allows arbitrary amounts o
  261. * data to be passed through zlib at the unavoidable cost of requiring a
  262. * window save (memcpy of up to 32768 output bytes) every ZLIB_IO_MAX
  263. * input bytes.
  264. */
  265. if (png_ptr->zstream.avail_in == 0 && size > 0)
  266. {
  267. if (size <= ZLIB_IO_MAX)
  268. {
  269. /* The value is less than ZLIB_IO_MAX so the cast is safe: */
  270. png_ptr->zstream.avail_in = (uInt)size;
  271. size = 0;
  272. }
  273. else
  274. {
  275. png_ptr->zstream.avail_in = ZLIB_IO_MAX;
  276. size -= ZLIB_IO_MAX;
  277. }
  278. }
  279. /* Reset the output buffer each time round - we empty it
  280. * after every inflate call.
  281. */
  282. png_ptr->zstream.next_out = png_ptr->zbuf;
  283. png_ptr->zstream.avail_out = png_ptr->zbuf_size;
  284. ret = inflate(&png_ptr->zstream, Z_NO_FLUSH);
  285. avail = png_ptr->zbuf_size - png_ptr->zstream.avail_out;
  286. /* First copy/count any new output - but only if we didn't
  287. * get an error code.
  288. */
  289. if ((ret == Z_OK || ret == Z_STREAM_END) && avail > 0)
  290. {
  291. png_size_t space = avail; /* > 0, see above */
  292. if (output != 0 && output_size > count)
  293. {
  294. png_size_t copy = output_size - count;
  295. if (space < copy)
  296. copy = space;
  297. png_memcpy(output + count, png_ptr->zbuf, copy);
  298. }
  299. count += space;
  300. }
  301. if (ret == Z_OK)
  302. continue;
  303. /* Termination conditions - always reset the zstream, it
  304. * must be left in inflateInit state.
  305. */
  306. png_ptr->zstream.avail_in = 0;
  307. inflateReset(&png_ptr->zstream);
  308. if (ret == Z_STREAM_END)
  309. return count; /* NOTE: may be zero. */
  310. /* Now handle the error codes - the API always returns 0
  311. * and the error message is dumped into the uncompressed
  312. * buffer if available.
  313. */
  314. # ifdef PNG_WARNINGS_SUPPORTED
  315. {
  316. png_const_charp msg;
  317. if (png_ptr->zstream.msg != 0)
  318. msg = png_ptr->zstream.msg;
  319. else switch (ret)
  320. {
  321. case Z_BUF_ERROR:
  322. msg = "Buffer error in compressed datastream";
  323. break;
  324. case Z_DATA_ERROR:
  325. msg = "Data error in compressed datastream";
  326. break;
  327. default:
  328. msg = "Incomplete compressed datastream";
  329. break;
  330. }
  331. png_chunk_warning(png_ptr, msg);
  332. }
  333. # endif
  334. /* 0 means an error - notice that this code simply ignores
  335. * zero length compressed chunks as a result.
  336. */
  337. return 0;
  338. }
  339. }
  340. /*
  341. * Decompress trailing data in a chunk. The assumption is that chunkdata
  342. * points at an allocated area holding the contents of a chunk with a
  343. * trailing compressed part. What we get back is an allocated area
  344. * holding the original prefix part and an uncompressed version of the
  345. * trailing part (the malloc area passed in is freed).
  346. */
  347. void /* PRIVATE */
  348. png_decompress_chunk(png_structp png_ptr, int comp_type,
  349. png_size_t chunklength,
  350. png_size_t prefix_size, png_size_t *newlength)
  351. {
  352. /* The caller should guarantee this */
  353. if (prefix_size > chunklength)
  354. {
  355. /* The recovery is to delete the chunk. */
  356. png_warning(png_ptr, "invalid chunklength");
  357. prefix_size = 0; /* To delete everything */
  358. }
  359. else if (comp_type == PNG_COMPRESSION_TYPE_BASE)
  360. {
  361. png_size_t expanded_size = png_inflate(png_ptr,
  362. (png_bytep)(png_ptr->chunkdata + prefix_size),
  363. chunklength - prefix_size,
  364. 0, /* output */
  365. 0); /* output size */
  366. /* Now check the limits on this chunk - if the limit fails the
  367. * compressed data will be removed, the prefix will remain.
  368. */
  369. if (prefix_size >= (~(png_size_t)0) - 1 ||
  370. expanded_size >= (~(png_size_t)0) - 1 - prefix_size
  371. #ifdef PNG_USER_LIMITS_SUPPORTED
  372. || (png_ptr->user_chunk_malloc_max &&
  373. (prefix_size + expanded_size >= png_ptr->user_chunk_malloc_max - 1))
  374. #else
  375. || ((PNG_USER_CHUNK_MALLOC_MAX > 0) &&
  376. prefix_size + expanded_size >= PNG_USER_CHUNK_MALLOC_MAX - 1)
  377. #endif
  378. )
  379. png_warning(png_ptr, "Exceeded size limit while expanding chunk");
  380. /* If the size is zero either there was an error and a message
  381. * has already been output (warning) or the size really is zero
  382. * and we have nothing to do - the code will exit through the
  383. * error case below.
  384. */
  385. else if (expanded_size > 0)
  386. {
  387. /* Success (maybe) - really uncompress the chunk. */
  388. png_size_t new_size = 0;
  389. png_charp text = (png_charp)png_malloc_warn(png_ptr,
  390. prefix_size + expanded_size + 1);
  391. if (text != NULL)
  392. {
  393. png_memcpy(text, png_ptr->chunkdata, prefix_size);
  394. new_size = png_inflate(png_ptr,
  395. (png_bytep)(png_ptr->chunkdata + prefix_size),
  396. chunklength - prefix_size,
  397. (png_bytep)(text + prefix_size), expanded_size);
  398. text[prefix_size + expanded_size] = 0; /* just in case */
  399. if (new_size == expanded_size)
  400. {
  401. png_free(png_ptr, png_ptr->chunkdata);
  402. png_ptr->chunkdata = text;
  403. *newlength = prefix_size + expanded_size;
  404. return; /* The success return! */
  405. }
  406. png_warning(png_ptr, "png_inflate logic error");
  407. png_free(png_ptr, text);
  408. }
  409. else
  410. png_warning(png_ptr, "Not enough memory to decompress chunk");
  411. }
  412. }
  413. else /* if (comp_type != PNG_COMPRESSION_TYPE_BASE) */
  414. {
  415. PNG_WARNING_PARAMETERS(p)
  416. png_warning_parameter_signed(p, 1, PNG_NUMBER_FORMAT_d, comp_type);
  417. png_formatted_warning(png_ptr, p, "Unknown compression type @1");
  418. /* The recovery is to simply drop the data. */
  419. }
  420. /* Generic error return - leave the prefix, delete the compressed
  421. * data, reallocate the chunkdata to remove the potentially large
  422. * amount of compressed data.
  423. */
  424. {
  425. png_charp text = (png_charp)png_malloc_warn(png_ptr, prefix_size + 1);
  426. if (text != NULL)
  427. {
  428. if (prefix_size > 0)
  429. png_memcpy(text, png_ptr->chunkdata, prefix_size);
  430. png_free(png_ptr, png_ptr->chunkdata);
  431. png_ptr->chunkdata = text;
  432. /* This is an extra zero in the 'uncompressed' part. */
  433. *(png_ptr->chunkdata + prefix_size) = 0x00;
  434. }
  435. /* Ignore a malloc error here - it is safe. */
  436. }
  437. *newlength = prefix_size;
  438. }
  439. #endif /* PNG_READ_COMPRESSED_TEXT_SUPPORTED */
  440. /* Read and check the IDHR chunk */
  441. void /* PRIVATE */
  442. png_handle_IHDR(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  443. {
  444. png_byte buf[13];
  445. png_uint_32 width, height;
  446. int bit_depth, color_type, compression_type, filter_type;
  447. int interlace_type;
  448. png_debug(1, "in png_handle_IHDR");
  449. if (png_ptr->mode & PNG_HAVE_IHDR)
  450. png_error(png_ptr, "Out of place IHDR");
  451. /* Check the length */
  452. if (length != 13)
  453. png_error(png_ptr, "Invalid IHDR chunk");
  454. png_ptr->mode |= PNG_HAVE_IHDR;
  455. png_crc_read(png_ptr, buf, 13);
  456. png_crc_finish(png_ptr, 0);
  457. width = png_get_uint_31(png_ptr, buf);
  458. height = png_get_uint_31(png_ptr, buf + 4);
  459. bit_depth = buf[8];
  460. color_type = buf[9];
  461. compression_type = buf[10];
  462. filter_type = buf[11];
  463. interlace_type = buf[12];
  464. /* Set internal variables */
  465. png_ptr->width = width;
  466. png_ptr->height = height;
  467. png_ptr->bit_depth = (png_byte)bit_depth;
  468. png_ptr->interlaced = (png_byte)interlace_type;
  469. png_ptr->color_type = (png_byte)color_type;
  470. #ifdef PNG_MNG_FEATURES_SUPPORTED
  471. png_ptr->filter_type = (png_byte)filter_type;
  472. #endif
  473. png_ptr->compression_type = (png_byte)compression_type;
  474. /* Find number of channels */
  475. switch (png_ptr->color_type)
  476. {
  477. default: /* invalid, png_set_IHDR calls png_error */
  478. case PNG_COLOR_TYPE_GRAY:
  479. case PNG_COLOR_TYPE_PALETTE:
  480. png_ptr->channels = 1;
  481. break;
  482. case PNG_COLOR_TYPE_RGB:
  483. png_ptr->channels = 3;
  484. break;
  485. case PNG_COLOR_TYPE_GRAY_ALPHA:
  486. png_ptr->channels = 2;
  487. break;
  488. case PNG_COLOR_TYPE_RGB_ALPHA:
  489. png_ptr->channels = 4;
  490. break;
  491. }
  492. /* Set up other useful info */
  493. png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth *
  494. png_ptr->channels);
  495. png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width);
  496. png_debug1(3, "bit_depth = %d", png_ptr->bit_depth);
  497. png_debug1(3, "channels = %d", png_ptr->channels);
  498. png_debug1(3, "rowbytes = %lu", (unsigned long)png_ptr->rowbytes);
  499. png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
  500. color_type, interlace_type, compression_type, filter_type);
  501. }
  502. /* Read and check the palette */
  503. void /* PRIVATE */
  504. png_handle_PLTE(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  505. {
  506. png_color palette[PNG_MAX_PALETTE_LENGTH];
  507. int num, i;
  508. #ifdef PNG_POINTER_INDEXING_SUPPORTED
  509. png_colorp pal_ptr;
  510. #endif
  511. png_debug(1, "in png_handle_PLTE");
  512. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  513. png_error(png_ptr, "Missing IHDR before PLTE");
  514. else if (png_ptr->mode & PNG_HAVE_IDAT)
  515. {
  516. png_warning(png_ptr, "Invalid PLTE after IDAT");
  517. png_crc_finish(png_ptr, length);
  518. return;
  519. }
  520. else if (png_ptr->mode & PNG_HAVE_PLTE)
  521. png_error(png_ptr, "Duplicate PLTE chunk");
  522. png_ptr->mode |= PNG_HAVE_PLTE;
  523. if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
  524. {
  525. png_warning(png_ptr,
  526. "Ignoring PLTE chunk in grayscale PNG");
  527. png_crc_finish(png_ptr, length);
  528. return;
  529. }
  530. #ifndef PNG_READ_OPT_PLTE_SUPPORTED
  531. if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
  532. {
  533. png_crc_finish(png_ptr, length);
  534. return;
  535. }
  536. #endif
  537. if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3)
  538. {
  539. if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
  540. {
  541. png_warning(png_ptr, "Invalid palette chunk");
  542. png_crc_finish(png_ptr, length);
  543. return;
  544. }
  545. else
  546. {
  547. png_error(png_ptr, "Invalid palette chunk");
  548. }
  549. }
  550. num = (int)length / 3;
  551. #ifdef PNG_POINTER_INDEXING_SUPPORTED
  552. for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++)
  553. {
  554. png_byte buf[3];
  555. png_crc_read(png_ptr, buf, 3);
  556. pal_ptr->red = buf[0];
  557. pal_ptr->green = buf[1];
  558. pal_ptr->blue = buf[2];
  559. }
  560. #else
  561. for (i = 0; i < num; i++)
  562. {
  563. png_byte buf[3];
  564. png_crc_read(png_ptr, buf, 3);
  565. /* Don't depend upon png_color being any order */
  566. palette[i].red = buf[0];
  567. palette[i].green = buf[1];
  568. palette[i].blue = buf[2];
  569. }
  570. #endif
  571. /* If we actually need the PLTE chunk (ie for a paletted image), we do
  572. * whatever the normal CRC configuration tells us. However, if we
  573. * have an RGB image, the PLTE can be considered ancillary, so
  574. * we will act as though it is.
  575. */
  576. #ifndef PNG_READ_OPT_PLTE_SUPPORTED
  577. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  578. #endif
  579. {
  580. png_crc_finish(png_ptr, 0);
  581. }
  582. #ifndef PNG_READ_OPT_PLTE_SUPPORTED
  583. else if (png_crc_error(png_ptr)) /* Only if we have a CRC error */
  584. {
  585. /* If we don't want to use the data from an ancillary chunk,
  586. * we have two options: an error abort, or a warning and we
  587. * ignore the data in this chunk (which should be OK, since
  588. * it's considered ancillary for a RGB or RGBA image).
  589. */
  590. if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE))
  591. {
  592. if (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)
  593. {
  594. png_chunk_benign_error(png_ptr, "CRC error");
  595. }
  596. else
  597. {
  598. png_chunk_warning(png_ptr, "CRC error");
  599. return;
  600. }
  601. }
  602. /* Otherwise, we (optionally) emit a warning and use the chunk. */
  603. else if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN))
  604. {
  605. png_chunk_warning(png_ptr, "CRC error");
  606. }
  607. }
  608. #endif
  609. png_set_PLTE(png_ptr, info_ptr, palette, num);
  610. #ifdef PNG_READ_tRNS_SUPPORTED
  611. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  612. {
  613. if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
  614. {
  615. if (png_ptr->num_trans > (png_uint_16)num)
  616. {
  617. png_warning(png_ptr, "Truncating incorrect tRNS chunk length");
  618. png_ptr->num_trans = (png_uint_16)num;
  619. }
  620. if (info_ptr->num_trans > (png_uint_16)num)
  621. {
  622. png_warning(png_ptr, "Truncating incorrect info tRNS chunk length");
  623. info_ptr->num_trans = (png_uint_16)num;
  624. }
  625. }
  626. }
  627. #endif
  628. }
  629. void /* PRIVATE */
  630. png_handle_IEND(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  631. {
  632. png_debug(1, "in png_handle_IEND");
  633. if (!(png_ptr->mode & PNG_HAVE_IHDR) || !(png_ptr->mode & PNG_HAVE_IDAT))
  634. {
  635. png_error(png_ptr, "No image in file");
  636. }
  637. png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND);
  638. if (length != 0)
  639. {
  640. png_warning(png_ptr, "Incorrect IEND chunk length");
  641. }
  642. png_crc_finish(png_ptr, length);
  643. PNG_UNUSED(info_ptr) /* Quiet compiler warnings about unused info_ptr */
  644. }
  645. #ifdef PNG_READ_gAMA_SUPPORTED
  646. void /* PRIVATE */
  647. png_handle_gAMA(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  648. {
  649. png_fixed_point igamma;
  650. png_byte buf[4];
  651. png_debug(1, "in png_handle_gAMA");
  652. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  653. png_error(png_ptr, "Missing IHDR before gAMA");
  654. else if (png_ptr->mode & PNG_HAVE_IDAT)
  655. {
  656. png_warning(png_ptr, "Invalid gAMA after IDAT");
  657. png_crc_finish(png_ptr, length);
  658. return;
  659. }
  660. else if (png_ptr->mode & PNG_HAVE_PLTE)
  661. /* Should be an error, but we can cope with it */
  662. png_warning(png_ptr, "Out of place gAMA chunk");
  663. if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA)
  664. #ifdef PNG_READ_sRGB_SUPPORTED
  665. && !(info_ptr->valid & PNG_INFO_sRGB)
  666. #endif
  667. )
  668. {
  669. png_warning(png_ptr, "Duplicate gAMA chunk");
  670. png_crc_finish(png_ptr, length);
  671. return;
  672. }
  673. if (length != 4)
  674. {
  675. png_warning(png_ptr, "Incorrect gAMA chunk length");
  676. png_crc_finish(png_ptr, length);
  677. return;
  678. }
  679. png_crc_read(png_ptr, buf, 4);
  680. if (png_crc_finish(png_ptr, 0))
  681. return;
  682. igamma = png_get_fixed_point(NULL, buf);
  683. /* Check for zero gamma or an error. */
  684. if (igamma <= 0)
  685. {
  686. png_warning(png_ptr,
  687. "Ignoring gAMA chunk with out of range gamma");
  688. return;
  689. }
  690. # ifdef PNG_READ_sRGB_SUPPORTED
  691. if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB))
  692. {
  693. if (PNG_OUT_OF_RANGE(igamma, 45500, 500))
  694. {
  695. PNG_WARNING_PARAMETERS(p)
  696. png_warning_parameter_signed(p, 1, PNG_NUMBER_FORMAT_fixed, igamma);
  697. png_formatted_warning(png_ptr, p,
  698. "Ignoring incorrect gAMA value @1 when sRGB is also present");
  699. return;
  700. }
  701. }
  702. # endif /* PNG_READ_sRGB_SUPPORTED */
  703. # ifdef PNG_READ_GAMMA_SUPPORTED
  704. /* Gamma correction on read is supported. */
  705. png_ptr->gamma = igamma;
  706. # endif
  707. /* And set the 'info' structure members. */
  708. png_set_gAMA_fixed(png_ptr, info_ptr, igamma);
  709. }
  710. #endif
  711. #ifdef PNG_READ_sBIT_SUPPORTED
  712. void /* PRIVATE */
  713. png_handle_sBIT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  714. {
  715. png_size_t truelen;
  716. png_byte buf[4];
  717. png_debug(1, "in png_handle_sBIT");
  718. buf[0] = buf[1] = buf[2] = buf[3] = 0;
  719. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  720. png_error(png_ptr, "Missing IHDR before sBIT");
  721. else if (png_ptr->mode & PNG_HAVE_IDAT)
  722. {
  723. png_warning(png_ptr, "Invalid sBIT after IDAT");
  724. png_crc_finish(png_ptr, length);
  725. return;
  726. }
  727. else if (png_ptr->mode & PNG_HAVE_PLTE)
  728. {
  729. /* Should be an error, but we can cope with it */
  730. png_warning(png_ptr, "Out of place sBIT chunk");
  731. }
  732. if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT))
  733. {
  734. png_warning(png_ptr, "Duplicate sBIT chunk");
  735. png_crc_finish(png_ptr, length);
  736. return;
  737. }
  738. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  739. truelen = 3;
  740. else
  741. truelen = (png_size_t)png_ptr->channels;
  742. if (length != truelen || length > 4)
  743. {
  744. png_warning(png_ptr, "Incorrect sBIT chunk length");
  745. png_crc_finish(png_ptr, length);
  746. return;
  747. }
  748. png_crc_read(png_ptr, buf, truelen);
  749. if (png_crc_finish(png_ptr, 0))
  750. return;
  751. if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
  752. {
  753. png_ptr->sig_bit.red = buf[0];
  754. png_ptr->sig_bit.green = buf[1];
  755. png_ptr->sig_bit.blue = buf[2];
  756. png_ptr->sig_bit.alpha = buf[3];
  757. }
  758. else
  759. {
  760. png_ptr->sig_bit.gray = buf[0];
  761. png_ptr->sig_bit.red = buf[0];
  762. png_ptr->sig_bit.green = buf[0];
  763. png_ptr->sig_bit.blue = buf[0];
  764. png_ptr->sig_bit.alpha = buf[1];
  765. }
  766. png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit));
  767. }
  768. #endif
  769. #ifdef PNG_READ_cHRM_SUPPORTED
  770. void /* PRIVATE */
  771. png_handle_cHRM(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  772. {
  773. png_byte buf[32];
  774. png_fixed_point x_white, y_white, x_red, y_red, x_green, y_green, x_blue,
  775. y_blue;
  776. png_debug(1, "in png_handle_cHRM");
  777. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  778. png_error(png_ptr, "Missing IHDR before cHRM");
  779. else if (png_ptr->mode & PNG_HAVE_IDAT)
  780. {
  781. png_warning(png_ptr, "Invalid cHRM after IDAT");
  782. png_crc_finish(png_ptr, length);
  783. return;
  784. }
  785. else if (png_ptr->mode & PNG_HAVE_PLTE)
  786. /* Should be an error, but we can cope with it */
  787. png_warning(png_ptr, "Out of place cHRM chunk");
  788. if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM)
  789. # ifdef PNG_READ_sRGB_SUPPORTED
  790. && !(info_ptr->valid & PNG_INFO_sRGB)
  791. # endif
  792. )
  793. {
  794. png_warning(png_ptr, "Duplicate cHRM chunk");
  795. png_crc_finish(png_ptr, length);
  796. return;
  797. }
  798. if (length != 32)
  799. {
  800. png_warning(png_ptr, "Incorrect cHRM chunk length");
  801. png_crc_finish(png_ptr, length);
  802. return;
  803. }
  804. png_crc_read(png_ptr, buf, 32);
  805. if (png_crc_finish(png_ptr, 0))
  806. return;
  807. x_white = png_get_fixed_point(NULL, buf);
  808. y_white = png_get_fixed_point(NULL, buf + 4);
  809. x_red = png_get_fixed_point(NULL, buf + 8);
  810. y_red = png_get_fixed_point(NULL, buf + 12);
  811. x_green = png_get_fixed_point(NULL, buf + 16);
  812. y_green = png_get_fixed_point(NULL, buf + 20);
  813. x_blue = png_get_fixed_point(NULL, buf + 24);
  814. y_blue = png_get_fixed_point(NULL, buf + 28);
  815. if (x_white == PNG_FIXED_ERROR ||
  816. y_white == PNG_FIXED_ERROR ||
  817. x_red == PNG_FIXED_ERROR ||
  818. y_red == PNG_FIXED_ERROR ||
  819. x_green == PNG_FIXED_ERROR ||
  820. y_green == PNG_FIXED_ERROR ||
  821. x_blue == PNG_FIXED_ERROR ||
  822. y_blue == PNG_FIXED_ERROR)
  823. {
  824. png_warning(png_ptr, "Ignoring cHRM chunk with negative chromaticities");
  825. return;
  826. }
  827. #ifdef PNG_READ_sRGB_SUPPORTED
  828. if ((info_ptr != NULL) && (info_ptr->valid & PNG_INFO_sRGB))
  829. {
  830. if (PNG_OUT_OF_RANGE(x_white, 31270, 1000) ||
  831. PNG_OUT_OF_RANGE(y_white, 32900, 1000) ||
  832. PNG_OUT_OF_RANGE(x_red, 64000, 1000) ||
  833. PNG_OUT_OF_RANGE(y_red, 33000, 1000) ||
  834. PNG_OUT_OF_RANGE(x_green, 30000, 1000) ||
  835. PNG_OUT_OF_RANGE(y_green, 60000, 1000) ||
  836. PNG_OUT_OF_RANGE(x_blue, 15000, 1000) ||
  837. PNG_OUT_OF_RANGE(y_blue, 6000, 1000))
  838. {
  839. PNG_WARNING_PARAMETERS(p)
  840. png_warning_parameter_signed(p, 1, PNG_NUMBER_FORMAT_fixed, x_white);
  841. png_warning_parameter_signed(p, 2, PNG_NUMBER_FORMAT_fixed, y_white);
  842. png_warning_parameter_signed(p, 3, PNG_NUMBER_FORMAT_fixed, x_red);
  843. png_warning_parameter_signed(p, 4, PNG_NUMBER_FORMAT_fixed, y_red);
  844. png_warning_parameter_signed(p, 5, PNG_NUMBER_FORMAT_fixed, x_green);
  845. png_warning_parameter_signed(p, 6, PNG_NUMBER_FORMAT_fixed, y_green);
  846. png_warning_parameter_signed(p, 7, PNG_NUMBER_FORMAT_fixed, x_blue);
  847. png_warning_parameter_signed(p, 8, PNG_NUMBER_FORMAT_fixed, y_blue);
  848. png_formatted_warning(png_ptr, p,
  849. "Ignoring incorrect cHRM white(@1,@2) r(@3,@4)g(@5,@6)b(@7,@8) "
  850. "when sRGB is also present");
  851. }
  852. return;
  853. }
  854. #endif /* PNG_READ_sRGB_SUPPORTED */
  855. #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
  856. /* Store the _white values as default coefficients for the rgb to gray
  857. * operation if it is supported. Check if the transform is already set to
  858. * avoid destroying the transform values.
  859. */
  860. if (!png_ptr->rgb_to_gray_coefficients_set)
  861. {
  862. /* png_set_background has not been called and we haven't seen an sRGB
  863. * chunk yet. Find the XYZ of the three end points.
  864. */
  865. png_XYZ XYZ;
  866. png_xy xy;
  867. xy.redx = x_red;
  868. xy.redy = y_red;
  869. xy.greenx = x_green;
  870. xy.greeny = y_green;
  871. xy.bluex = x_blue;
  872. xy.bluey = y_blue;
  873. xy.whitex = x_white;
  874. xy.whitey = y_white;
  875. if (png_XYZ_from_xy_checked(png_ptr, &XYZ, xy))
  876. {
  877. /* The success case, because XYZ_from_xy normalises to a reference
  878. * white Y of 1.0 we just need to scale the numbers. This should
  879. * always work just fine. It is an internal error if this overflows.
  880. */
  881. {
  882. png_fixed_point r, g, b;
  883. if (png_muldiv(&r, XYZ.redY, 32768, PNG_FP_1) &&
  884. r >= 0 && r <= 32768 &&
  885. png_muldiv(&g, XYZ.greenY, 32768, PNG_FP_1) &&
  886. g >= 0 && g <= 32768 &&
  887. png_muldiv(&b, XYZ.blueY, 32768, PNG_FP_1) &&
  888. b >= 0 && b <= 32768 &&
  889. r+g+b <= 32769)
  890. {
  891. /* We allow 0 coefficients here. r+g+b may be 32769 if two or
  892. * all of the coefficients were rounded up. Handle this by
  893. * reducing the *largest* coefficient by 1; this matches the
  894. * approach used for the default coefficients in pngrtran.c
  895. */
  896. int add = 0;
  897. if (r+g+b > 32768)
  898. add = -1;
  899. else if (r+g+b < 32768)
  900. add = 1;
  901. if (add != 0)
  902. {
  903. if (g >= r && g >= b)
  904. g += add;
  905. else if (r >= g && r >= b)
  906. r += add;
  907. else
  908. b += add;
  909. }
  910. /* Check for an internal error. */
  911. if (r+g+b != 32768)
  912. png_error(png_ptr,
  913. "internal error handling cHRM coefficients");
  914. png_ptr->rgb_to_gray_red_coeff = (png_uint_16)r;
  915. png_ptr->rgb_to_gray_green_coeff = (png_uint_16)g;
  916. }
  917. /* This is a png_error at present even though it could be ignored -
  918. * it should never happen, but it is important that if it does, the
  919. * bug is fixed.
  920. */
  921. else
  922. png_error(png_ptr, "internal error handling cHRM->XYZ");
  923. }
  924. }
  925. }
  926. #endif
  927. png_set_cHRM_fixed(png_ptr, info_ptr, x_white, y_white, x_red, y_red,
  928. x_green, y_green, x_blue, y_blue);
  929. }
  930. #endif
  931. #ifdef PNG_READ_sRGB_SUPPORTED
  932. void /* PRIVATE */
  933. png_handle_sRGB(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  934. {
  935. int intent;
  936. png_byte buf[1];
  937. png_debug(1, "in png_handle_sRGB");
  938. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  939. png_error(png_ptr, "Missing IHDR before sRGB");
  940. else if (png_ptr->mode & PNG_HAVE_IDAT)
  941. {
  942. png_warning(png_ptr, "Invalid sRGB after IDAT");
  943. png_crc_finish(png_ptr, length);
  944. return;
  945. }
  946. else if (png_ptr->mode & PNG_HAVE_PLTE)
  947. /* Should be an error, but we can cope with it */
  948. png_warning(png_ptr, "Out of place sRGB chunk");
  949. if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB))
  950. {
  951. png_warning(png_ptr, "Duplicate sRGB chunk");
  952. png_crc_finish(png_ptr, length);
  953. return;
  954. }
  955. if (length != 1)
  956. {
  957. png_warning(png_ptr, "Incorrect sRGB chunk length");
  958. png_crc_finish(png_ptr, length);
  959. return;
  960. }
  961. png_crc_read(png_ptr, buf, 1);
  962. if (png_crc_finish(png_ptr, 0))
  963. return;
  964. intent = buf[0];
  965. /* Check for bad intent */
  966. if (intent >= PNG_sRGB_INTENT_LAST)
  967. {
  968. png_warning(png_ptr, "Unknown sRGB intent");
  969. return;
  970. }
  971. #if defined(PNG_READ_gAMA_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED)
  972. if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA))
  973. {
  974. if (PNG_OUT_OF_RANGE(info_ptr->gamma, 45500, 500))
  975. {
  976. PNG_WARNING_PARAMETERS(p)
  977. png_warning_parameter_signed(p, 1, PNG_NUMBER_FORMAT_fixed,
  978. info_ptr->gamma);
  979. png_formatted_warning(png_ptr, p,
  980. "Ignoring incorrect gAMA value @1 when sRGB is also present");
  981. }
  982. }
  983. #endif /* PNG_READ_gAMA_SUPPORTED */
  984. #ifdef PNG_READ_cHRM_SUPPORTED
  985. if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM))
  986. if (PNG_OUT_OF_RANGE(info_ptr->x_white, 31270, 1000) ||
  987. PNG_OUT_OF_RANGE(info_ptr->y_white, 32900, 1000) ||
  988. PNG_OUT_OF_RANGE(info_ptr->x_red, 64000, 1000) ||
  989. PNG_OUT_OF_RANGE(info_ptr->y_red, 33000, 1000) ||
  990. PNG_OUT_OF_RANGE(info_ptr->x_green, 30000, 1000) ||
  991. PNG_OUT_OF_RANGE(info_ptr->y_green, 60000, 1000) ||
  992. PNG_OUT_OF_RANGE(info_ptr->x_blue, 15000, 1000) ||
  993. PNG_OUT_OF_RANGE(info_ptr->y_blue, 6000, 1000))
  994. {
  995. png_warning(png_ptr,
  996. "Ignoring incorrect cHRM value when sRGB is also present");
  997. }
  998. #endif /* PNG_READ_cHRM_SUPPORTED */
  999. /* This is recorded for use when handling the cHRM chunk above. An sRGB
  1000. * chunk unconditionally overwrites the coefficients for grayscale conversion
  1001. * too.
  1002. */
  1003. png_ptr->is_sRGB = 1;
  1004. # ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
  1005. /* Don't overwrite user supplied values: */
  1006. if (!png_ptr->rgb_to_gray_coefficients_set)
  1007. {
  1008. /* These numbers come from the sRGB specification (or, since one has to
  1009. * pay much money to get a copy, the wikipedia sRGB page) the
  1010. * chromaticity values quoted have been inverted to get the reverse
  1011. * transformation from RGB to XYZ and the 'Y' coefficients scaled by
  1012. * 32768 (then rounded).
  1013. *
  1014. * sRGB and ITU Rec-709 both truncate the values for the D65 white
  1015. * point to four digits and, even though it actually stores five
  1016. * digits, the PNG spec gives the truncated value.
  1017. *
  1018. * This means that when the chromaticities are converted back to XYZ
  1019. * end points we end up with (6968,23435,2366), which, as described in
  1020. * pngrtran.c, would overflow. If the five digit precision and up is
  1021. * used we get, instead:
  1022. *
  1023. * 6968*R + 23435*G + 2365*B
  1024. *
  1025. * (Notice that this rounds the blue coefficient down, rather than the
  1026. * choice used in pngrtran.c which is to round the green one down.)
  1027. */
  1028. png_ptr->rgb_to_gray_red_coeff = 6968; /* 0.212639005871510 */
  1029. png_ptr->rgb_to_gray_green_coeff = 23434; /* 0.715168678767756 */
  1030. /* png_ptr->rgb_to_gray_blue_coeff = 2366; 0.072192315360734 */
  1031. /* The following keeps the cHRM chunk from destroying the
  1032. * coefficients again in the event that it follows the sRGB chunk.
  1033. */
  1034. png_ptr->rgb_to_gray_coefficients_set = 1;
  1035. }
  1036. # endif
  1037. png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr, intent);
  1038. }
  1039. #endif /* PNG_READ_sRGB_SUPPORTED */
  1040. #ifdef PNG_READ_iCCP_SUPPORTED
  1041. void /* PRIVATE */
  1042. png_handle_iCCP(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1043. /* Note: this does not properly handle chunks that are > 64K under DOS */
  1044. {
  1045. png_byte compression_type;
  1046. png_bytep pC;
  1047. png_charp profile;
  1048. png_uint_32 skip = 0;
  1049. png_uint_32 profile_size;
  1050. png_alloc_size_t profile_length;
  1051. png_size_t slength, prefix_length, data_length;
  1052. png_debug(1, "in png_handle_iCCP");
  1053. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1054. png_error(png_ptr, "Missing IHDR before iCCP");
  1055. else if (png_ptr->mode & PNG_HAVE_IDAT)
  1056. {
  1057. png_warning(png_ptr, "Invalid iCCP after IDAT");
  1058. png_crc_finish(png_ptr, length);
  1059. return;
  1060. }
  1061. else if (png_ptr->mode & PNG_HAVE_PLTE)
  1062. /* Should be an error, but we can cope with it */
  1063. png_warning(png_ptr, "Out of place iCCP chunk");
  1064. if ((png_ptr->mode & PNG_HAVE_iCCP) || (info_ptr != NULL &&
  1065. (info_ptr->valid & (PNG_INFO_iCCP|PNG_INFO_sRGB))))
  1066. {
  1067. png_warning(png_ptr, "Duplicate iCCP chunk");
  1068. png_crc_finish(png_ptr, length);
  1069. return;
  1070. }
  1071. png_ptr->mode |= PNG_HAVE_iCCP;
  1072. #ifdef PNG_MAX_MALLOC_64K
  1073. if (length > (png_uint_32)65535L)
  1074. {
  1075. png_warning(png_ptr, "iCCP chunk too large to fit in memory");
  1076. skip = length - (png_uint_32)65535L;
  1077. length = (png_uint_32)65535L;
  1078. }
  1079. #endif
  1080. png_free(png_ptr, png_ptr->chunkdata);
  1081. png_ptr->chunkdata = (png_charp)png_malloc(png_ptr, length + 1);
  1082. slength = length;
  1083. png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
  1084. if (png_crc_finish(png_ptr, skip))
  1085. {
  1086. png_free(png_ptr, png_ptr->chunkdata);
  1087. png_ptr->chunkdata = NULL;
  1088. return;
  1089. }
  1090. png_ptr->chunkdata[slength] = 0x00;
  1091. for (profile = png_ptr->chunkdata; *profile; profile++)
  1092. /* Empty loop to find end of name */ ;
  1093. ++profile;
  1094. /* There should be at least one zero (the compression type byte)
  1095. * following the separator, and we should be on it
  1096. */
  1097. if (profile >= png_ptr->chunkdata + slength - 1)
  1098. {
  1099. png_free(png_ptr, png_ptr->chunkdata);
  1100. png_ptr->chunkdata = NULL;
  1101. png_warning(png_ptr, "Malformed iCCP chunk");
  1102. return;
  1103. }
  1104. /* Compression_type should always be zero */
  1105. compression_type = *profile++;
  1106. if (compression_type != 0)
  1107. {
  1108. png_warning(png_ptr, "Ignoring nonzero compression type in iCCP chunk");
  1109. compression_type = 0x00; /* Reset it to zero (libpng-1.0.6 through 1.0.8
  1110. wrote nonzero) */
  1111. }
  1112. prefix_length = profile - png_ptr->chunkdata;
  1113. png_decompress_chunk(png_ptr, compression_type,
  1114. slength, prefix_length, &data_length);
  1115. profile_length = data_length - prefix_length;
  1116. if (prefix_length > data_length || profile_length < 4)
  1117. {
  1118. png_free(png_ptr, png_ptr->chunkdata);
  1119. png_ptr->chunkdata = NULL;
  1120. png_warning(png_ptr, "Profile size field missing from iCCP chunk");
  1121. return;
  1122. }
  1123. /* Check the profile_size recorded in the first 32 bits of the ICC profile */
  1124. pC = (png_bytep)(png_ptr->chunkdata + prefix_length);
  1125. profile_size = ((*(pC )) << 24) |
  1126. ((*(pC + 1)) << 16) |
  1127. ((*(pC + 2)) << 8) |
  1128. ((*(pC + 3)) );
  1129. /* NOTE: the following guarantees that 'profile_length' fits into 32 bits,
  1130. * because profile_size is a 32 bit value.
  1131. */
  1132. if (profile_size < profile_length)
  1133. profile_length = profile_size;
  1134. /* And the following guarantees that profile_size == profile_length. */
  1135. if (profile_size > profile_length)
  1136. {
  1137. PNG_WARNING_PARAMETERS(p)
  1138. png_free(png_ptr, png_ptr->chunkdata);
  1139. png_ptr->chunkdata = NULL;
  1140. png_warning_parameter_unsigned(p, 1, PNG_NUMBER_FORMAT_u, profile_size);
  1141. png_warning_parameter_unsigned(p, 2, PNG_NUMBER_FORMAT_u, profile_length);
  1142. png_formatted_warning(png_ptr, p,
  1143. "Ignoring iCCP chunk with declared size = @1 and actual length = @2");
  1144. return;
  1145. }
  1146. png_set_iCCP(png_ptr, info_ptr, png_ptr->chunkdata,
  1147. compression_type, (png_bytep)png_ptr->chunkdata + prefix_length,
  1148. profile_size);
  1149. png_free(png_ptr, png_ptr->chunkdata);
  1150. png_ptr->chunkdata = NULL;
  1151. }
  1152. #endif /* PNG_READ_iCCP_SUPPORTED */
  1153. #ifdef PNG_READ_sPLT_SUPPORTED
  1154. void /* PRIVATE */
  1155. png_handle_sPLT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1156. /* Note: this does not properly handle chunks that are > 64K under DOS */
  1157. {
  1158. png_bytep entry_start;
  1159. png_sPLT_t new_palette;
  1160. png_sPLT_entryp pp;
  1161. png_uint_32 data_length;
  1162. int entry_size, i;
  1163. png_uint_32 skip = 0;
  1164. png_size_t slength;
  1165. png_uint_32 dl;
  1166. png_size_t max_dl;
  1167. png_debug(1, "in png_handle_sPLT");
  1168. #ifdef PNG_USER_LIMITS_SUPPORTED
  1169. if (png_ptr->user_chunk_cache_max != 0)
  1170. {
  1171. if (png_ptr->user_chunk_cache_max == 1)
  1172. {
  1173. png_crc_finish(png_ptr, length);
  1174. return;
  1175. }
  1176. if (--png_ptr->user_chunk_cache_max == 1)
  1177. {
  1178. png_warning(png_ptr, "No space in chunk cache for sPLT");
  1179. png_crc_finish(png_ptr, length);
  1180. return;
  1181. }
  1182. }
  1183. #endif
  1184. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1185. png_error(png_ptr, "Missing IHDR before sPLT");
  1186. else if (png_ptr->mode & PNG_HAVE_IDAT)
  1187. {
  1188. png_warning(png_ptr, "Invalid sPLT after IDAT");
  1189. png_crc_finish(png_ptr, length);
  1190. return;
  1191. }
  1192. #ifdef PNG_MAX_MALLOC_64K
  1193. if (length > (png_uint_32)65535L)
  1194. {
  1195. png_warning(png_ptr, "sPLT chunk too large to fit in memory");
  1196. skip = length - (png_uint_32)65535L;
  1197. length = (png_uint_32)65535L;
  1198. }
  1199. #endif
  1200. png_free(png_ptr, png_ptr->chunkdata);
  1201. png_ptr->chunkdata = (png_charp)png_malloc(png_ptr, length + 1);
  1202. /* WARNING: this may break if size_t is less than 32 bits; it is assumed
  1203. * that the PNG_MAX_MALLOC_64K test is enabled in this case, but this is a
  1204. * potential breakage point if the types in pngconf.h aren't exactly right.
  1205. */
  1206. slength = length;
  1207. png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
  1208. if (png_crc_finish(png_ptr, skip))
  1209. {
  1210. png_free(png_ptr, png_ptr->chunkdata);
  1211. png_ptr->chunkdata = NULL;
  1212. return;
  1213. }
  1214. png_ptr->chunkdata[slength] = 0x00;
  1215. for (entry_start = (png_bytep)png_ptr->chunkdata; *entry_start;
  1216. entry_start++)
  1217. /* Empty loop to find end of name */ ;
  1218. ++entry_start;
  1219. /* A sample depth should follow the separator, and we should be on it */
  1220. if (entry_start > (png_bytep)png_ptr->chunkdata + slength - 2)
  1221. {
  1222. png_free(png_ptr, png_ptr->chunkdata);
  1223. png_ptr->chunkdata = NULL;
  1224. png_warning(png_ptr, "malformed sPLT chunk");
  1225. return;
  1226. }
  1227. new_palette.depth = *entry_start++;
  1228. entry_size = (new_palette.depth == 8 ? 6 : 10);
  1229. /* This must fit in a png_uint_32 because it is derived from the original
  1230. * chunk data length (and use 'length', not 'slength' here for clarity -
  1231. * they are guaranteed to be the same, see the tests above.)
  1232. */
  1233. data_length = length - (png_uint_32)(entry_start -
  1234. (png_bytep)png_ptr->chunkdata);
  1235. /* Integrity-check the data length */
  1236. if (data_length % entry_size)
  1237. {
  1238. png_free(png_ptr, png_ptr->chunkdata);
  1239. png_ptr->chunkdata = NULL;
  1240. png_warning(png_ptr, "sPLT chunk has bad length");
  1241. return;
  1242. }
  1243. dl = (png_int_32)(data_length / entry_size);
  1244. max_dl = PNG_SIZE_MAX / png_sizeof(png_sPLT_entry);
  1245. if (dl > max_dl)
  1246. {
  1247. png_warning(png_ptr, "sPLT chunk too long");
  1248. return;
  1249. }
  1250. new_palette.nentries = (png_int_32)(data_length / entry_size);
  1251. new_palette.entries = (png_sPLT_entryp)png_malloc_warn(
  1252. png_ptr, new_palette.nentries * png_sizeof(png_sPLT_entry));
  1253. if (new_palette.entries == NULL)
  1254. {
  1255. png_warning(png_ptr, "sPLT chunk requires too much memory");
  1256. return;
  1257. }
  1258. #ifdef PNG_POINTER_INDEXING_SUPPORTED
  1259. for (i = 0; i < new_palette.nentries; i++)
  1260. {
  1261. pp = new_palette.entries + i;
  1262. if (new_palette.depth == 8)
  1263. {
  1264. pp->red = *entry_start++;
  1265. pp->green = *entry_start++;
  1266. pp->blue = *entry_start++;
  1267. pp->alpha = *entry_start++;
  1268. }
  1269. else
  1270. {
  1271. pp->red = png_get_uint_16(entry_start); entry_start += 2;
  1272. pp->green = png_get_uint_16(entry_start); entry_start += 2;
  1273. pp->blue = png_get_uint_16(entry_start); entry_start += 2;
  1274. pp->alpha = png_get_uint_16(entry_start); entry_start += 2;
  1275. }
  1276. pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
  1277. }
  1278. #else
  1279. pp = new_palette.entries;
  1280. for (i = 0; i < new_palette.nentries; i++)
  1281. {
  1282. if (new_palette.depth == 8)
  1283. {
  1284. pp[i].red = *entry_start++;
  1285. pp[i].green = *entry_start++;
  1286. pp[i].blue = *entry_start++;
  1287. pp[i].alpha = *entry_start++;
  1288. }
  1289. else
  1290. {
  1291. pp[i].red = png_get_uint_16(entry_start); entry_start += 2;
  1292. pp[i].green = png_get_uint_16(entry_start); entry_start += 2;
  1293. pp[i].blue = png_get_uint_16(entry_start); entry_start += 2;
  1294. pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2;
  1295. }
  1296. pp[i].frequency = png_get_uint_16(entry_start); entry_start += 2;
  1297. }
  1298. #endif
  1299. /* Discard all chunk data except the name and stash that */
  1300. new_palette.name = png_ptr->chunkdata;
  1301. png_set_sPLT(png_ptr, info_ptr, &new_palette, 1);
  1302. png_free(png_ptr, png_ptr->chunkdata);
  1303. png_ptr->chunkdata = NULL;
  1304. png_free(png_ptr, new_palette.entries);
  1305. }
  1306. #endif /* PNG_READ_sPLT_SUPPORTED */
  1307. #ifdef PNG_READ_tRNS_SUPPORTED
  1308. void /* PRIVATE */
  1309. png_handle_tRNS(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1310. {
  1311. png_byte readbuf[PNG_MAX_PALETTE_LENGTH];
  1312. png_debug(1, "in png_handle_tRNS");
  1313. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1314. png_error(png_ptr, "Missing IHDR before tRNS");
  1315. else if (png_ptr->mode & PNG_HAVE_IDAT)
  1316. {
  1317. png_warning(png_ptr, "Invalid tRNS after IDAT");
  1318. png_crc_finish(png_ptr, length);
  1319. return;
  1320. }
  1321. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
  1322. {
  1323. png_warning(png_ptr, "Duplicate tRNS chunk");
  1324. png_crc_finish(png_ptr, length);
  1325. return;
  1326. }
  1327. if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
  1328. {
  1329. png_byte buf[2];
  1330. if (length != 2)
  1331. {
  1332. png_warning(png_ptr, "Incorrect tRNS chunk length");
  1333. png_crc_finish(png_ptr, length);
  1334. return;
  1335. }
  1336. png_crc_read(png_ptr, buf, 2);
  1337. png_ptr->num_trans = 1;
  1338. png_ptr->trans_color.gray = png_get_uint_16(buf);
  1339. }
  1340. else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
  1341. {
  1342. png_byte buf[6];
  1343. if (length != 6)
  1344. {
  1345. png_warning(png_ptr, "Incorrect tRNS chunk length");
  1346. png_crc_finish(png_ptr, length);
  1347. return;
  1348. }
  1349. png_crc_read(png_ptr, buf, (png_size_t)length);
  1350. png_ptr->num_trans = 1;
  1351. png_ptr->trans_color.red = png_get_uint_16(buf);
  1352. png_ptr->trans_color.green = png_get_uint_16(buf + 2);
  1353. png_ptr->trans_color.blue = png_get_uint_16(buf + 4);
  1354. }
  1355. else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  1356. {
  1357. if (!(png_ptr->mode & PNG_HAVE_PLTE))
  1358. {
  1359. /* Should be an error, but we can cope with it. */
  1360. png_warning(png_ptr, "Missing PLTE before tRNS");
  1361. }
  1362. if (length > (png_uint_32)png_ptr->num_palette ||
  1363. length > PNG_MAX_PALETTE_LENGTH)
  1364. {
  1365. png_warning(png_ptr, "Incorrect tRNS chunk length");
  1366. png_crc_finish(png_ptr, length);
  1367. return;
  1368. }
  1369. if (length == 0)
  1370. {
  1371. png_warning(png_ptr, "Zero length tRNS chunk");
  1372. png_crc_finish(png_ptr, length);
  1373. return;
  1374. }
  1375. png_crc_read(png_ptr, readbuf, (png_size_t)length);
  1376. png_ptr->num_trans = (png_uint_16)length;
  1377. }
  1378. else
  1379. {
  1380. png_warning(png_ptr, "tRNS chunk not allowed with alpha channel");
  1381. png_crc_finish(png_ptr, length);
  1382. return;
  1383. }
  1384. if (png_crc_finish(png_ptr, 0))
  1385. {
  1386. png_ptr->num_trans = 0;
  1387. return;
  1388. }
  1389. png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans,
  1390. &(png_ptr->trans_color));
  1391. }
  1392. #endif
  1393. #ifdef PNG_READ_bKGD_SUPPORTED
  1394. void /* PRIVATE */
  1395. png_handle_bKGD(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1396. {
  1397. png_size_t truelen;
  1398. png_byte buf[6];
  1399. png_color_16 background;
  1400. png_debug(1, "in png_handle_bKGD");
  1401. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1402. png_error(png_ptr, "Missing IHDR before bKGD");
  1403. else if (png_ptr->mode & PNG_HAVE_IDAT)
  1404. {
  1405. png_warning(png_ptr, "Invalid bKGD after IDAT");
  1406. png_crc_finish(png_ptr, length);
  1407. return;
  1408. }
  1409. else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
  1410. !(png_ptr->mode & PNG_HAVE_PLTE))
  1411. {
  1412. png_warning(png_ptr, "Missing PLTE before bKGD");
  1413. png_crc_finish(png_ptr, length);
  1414. return;
  1415. }
  1416. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD))
  1417. {
  1418. png_warning(png_ptr, "Duplicate bKGD chunk");
  1419. png_crc_finish(png_ptr, length);
  1420. return;
  1421. }
  1422. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  1423. truelen = 1;
  1424. else if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
  1425. truelen = 6;
  1426. else
  1427. truelen = 2;
  1428. if (length != truelen)
  1429. {
  1430. png_warning(png_ptr, "Incorrect bKGD chunk length");
  1431. png_crc_finish(png_ptr, length);
  1432. return;
  1433. }
  1434. png_crc_read(png_ptr, buf, truelen);
  1435. if (png_crc_finish(png_ptr, 0))
  1436. return;
  1437. /* We convert the index value into RGB components so that we can allow
  1438. * arbitrary RGB values for background when we have transparency, and
  1439. * so it is easy to determine the RGB values of the background color
  1440. * from the info_ptr struct.
  1441. */
  1442. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  1443. {
  1444. background.index = buf[0];
  1445. if (info_ptr && info_ptr->num_palette)
  1446. {
  1447. if (buf[0] >= info_ptr->num_palette)
  1448. {
  1449. png_warning(png_ptr, "Incorrect bKGD chunk index value");
  1450. return;
  1451. }
  1452. background.red = (png_uint_16)png_ptr->palette[buf[0]].red;
  1453. background.green = (png_uint_16)png_ptr->palette[buf[0]].green;
  1454. background.blue = (png_uint_16)png_ptr->palette[buf[0]].blue;
  1455. }
  1456. else
  1457. background.red = background.green = background.blue = 0;
  1458. background.gray = 0;
  1459. }
  1460. else if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) /* GRAY */
  1461. {
  1462. background.index = 0;
  1463. background.red =
  1464. background.green =
  1465. background.blue =
  1466. background.gray = png_get_uint_16(buf);
  1467. }
  1468. else
  1469. {
  1470. background.index = 0;
  1471. background.red = png_get_uint_16(buf);
  1472. background.green = png_get_uint_16(buf + 2);
  1473. background.blue = png_get_uint_16(buf + 4);
  1474. background.gray = 0;
  1475. }
  1476. png_set_bKGD(png_ptr, info_ptr, &background);
  1477. }
  1478. #endif
  1479. #ifdef PNG_READ_hIST_SUPPORTED
  1480. void /* PRIVATE */
  1481. png_handle_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1482. {
  1483. unsigned int num, i;
  1484. png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH];
  1485. png_debug(1, "in png_handle_hIST");
  1486. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1487. png_error(png_ptr, "Missing IHDR before hIST");
  1488. else if (png_ptr->mode & PNG_HAVE_IDAT)
  1489. {
  1490. png_warning(png_ptr, "Invalid hIST after IDAT");
  1491. png_crc_finish(png_ptr, length);
  1492. return;
  1493. }
  1494. else if (!(png_ptr->mode & PNG_HAVE_PLTE))
  1495. {
  1496. png_warning(png_ptr, "Missing PLTE before hIST");
  1497. png_crc_finish(png_ptr, length);
  1498. return;
  1499. }
  1500. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST))
  1501. {
  1502. png_warning(png_ptr, "Duplicate hIST chunk");
  1503. png_crc_finish(png_ptr, length);
  1504. return;
  1505. }
  1506. if (length > 2*PNG_MAX_PALETTE_LENGTH ||
  1507. length != (unsigned int) (2*png_ptr->num_palette))
  1508. {
  1509. png_warning(png_ptr, "Incorrect hIST chunk length");
  1510. png_crc_finish(png_ptr, length);
  1511. return;
  1512. }
  1513. num = length / 2 ;
  1514. for (i = 0; i < num; i++)
  1515. {
  1516. png_byte buf[2];
  1517. png_crc_read(png_ptr, buf, 2);
  1518. readbuf[i] = png_get_uint_16(buf);
  1519. }
  1520. if (png_crc_finish(png_ptr, 0))
  1521. return;
  1522. png_set_hIST(png_ptr, info_ptr, readbuf);
  1523. }
  1524. #endif
  1525. #ifdef PNG_READ_pHYs_SUPPORTED
  1526. void /* PRIVATE */
  1527. png_handle_pHYs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1528. {
  1529. png_byte buf[9];
  1530. png_uint_32 res_x, res_y;
  1531. int unit_type;
  1532. png_debug(1, "in png_handle_pHYs");
  1533. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1534. png_error(png_ptr, "Missing IHDR before pHYs");
  1535. else if (png_ptr->mode & PNG_HAVE_IDAT)
  1536. {
  1537. png_warning(png_ptr, "Invalid pHYs after IDAT");
  1538. png_crc_finish(png_ptr, length);
  1539. return;
  1540. }
  1541. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
  1542. {
  1543. png_warning(png_ptr, "Duplicate pHYs chunk");
  1544. png_crc_finish(png_ptr, length);
  1545. return;
  1546. }
  1547. if (length != 9)
  1548. {
  1549. png_warning(png_ptr, "Incorrect pHYs chunk length");
  1550. png_crc_finish(png_ptr, length);
  1551. return;
  1552. }
  1553. png_crc_read(png_ptr, buf, 9);
  1554. if (png_crc_finish(png_ptr, 0))
  1555. return;
  1556. res_x = png_get_uint_32(buf);
  1557. res_y = png_get_uint_32(buf + 4);
  1558. unit_type = buf[8];
  1559. png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type);
  1560. }
  1561. #endif
  1562. #ifdef PNG_READ_oFFs_SUPPORTED
  1563. void /* PRIVATE */
  1564. png_handle_oFFs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1565. {
  1566. png_byte buf[9];
  1567. png_int_32 offset_x, offset_y;
  1568. int unit_type;
  1569. png_debug(1, "in png_handle_oFFs");
  1570. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1571. png_error(png_ptr, "Missing IHDR before oFFs");
  1572. else if (png_ptr->mode & PNG_HAVE_IDAT)
  1573. {
  1574. png_warning(png_ptr, "Invalid oFFs after IDAT");
  1575. png_crc_finish(png_ptr, length);
  1576. return;
  1577. }
  1578. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs))
  1579. {
  1580. png_warning(png_ptr, "Duplicate oFFs chunk");
  1581. png_crc_finish(png_ptr, length);
  1582. return;
  1583. }
  1584. if (length != 9)
  1585. {
  1586. png_warning(png_ptr, "Incorrect oFFs chunk length");
  1587. png_crc_finish(png_ptr, length);
  1588. return;
  1589. }
  1590. png_crc_read(png_ptr, buf, 9);
  1591. if (png_crc_finish(png_ptr, 0))
  1592. return;
  1593. offset_x = png_get_int_32(buf);
  1594. offset_y = png_get_int_32(buf + 4);
  1595. unit_type = buf[8];
  1596. png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);
  1597. }
  1598. #endif
  1599. #ifdef PNG_READ_pCAL_SUPPORTED
  1600. /* Read the pCAL chunk (described in the PNG Extensions document) */
  1601. void /* PRIVATE */
  1602. png_handle_pCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1603. {
  1604. png_int_32 X0, X1;
  1605. png_byte type, nparams;
  1606. png_charp buf, units, endptr;
  1607. png_charpp params;
  1608. png_size_t slength;
  1609. int i;
  1610. png_debug(1, "in png_handle_pCAL");
  1611. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1612. png_error(png_ptr, "Missing IHDR before pCAL");
  1613. else if (png_ptr->mode & PNG_HAVE_IDAT)
  1614. {
  1615. png_warning(png_ptr, "Invalid pCAL after IDAT");
  1616. png_crc_finish(png_ptr, length);
  1617. return;
  1618. }
  1619. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL))
  1620. {
  1621. png_warning(png_ptr, "Duplicate pCAL chunk");
  1622. png_crc_finish(png_ptr, length);
  1623. return;
  1624. }
  1625. png_debug1(2, "Allocating and reading pCAL chunk data (%u bytes)",
  1626. length + 1);
  1627. png_free(png_ptr, png_ptr->chunkdata);
  1628. png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
  1629. if (png_ptr->chunkdata == NULL)
  1630. {
  1631. png_warning(png_ptr, "No memory for pCAL purpose");
  1632. return;
  1633. }
  1634. slength = length;
  1635. png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
  1636. if (png_crc_finish(png_ptr, 0))
  1637. {
  1638. png_free(png_ptr, png_ptr->chunkdata);
  1639. png_ptr->chunkdata = NULL;
  1640. return;
  1641. }
  1642. png_ptr->chunkdata[slength] = 0x00; /* Null terminate the last string */
  1643. png_debug(3, "Finding end of pCAL purpose string");
  1644. for (buf = png_ptr->chunkdata; *buf; buf++)
  1645. /* Empty loop */ ;
  1646. endptr = png_ptr->chunkdata + slength;
  1647. /* We need to have at least 12 bytes after the purpose string
  1648. * in order to get the parameter information.
  1649. */
  1650. if (endptr <= buf + 12)
  1651. {
  1652. png_warning(png_ptr, "Invalid pCAL data");
  1653. png_free(png_ptr, png_ptr->chunkdata);
  1654. png_ptr->chunkdata = NULL;
  1655. return;
  1656. }
  1657. png_debug(3, "Reading pCAL X0, X1, type, nparams, and units");
  1658. X0 = png_get_int_32((png_bytep)buf+1);
  1659. X1 = png_get_int_32((png_bytep)buf+5);
  1660. type = buf[9];
  1661. nparams = buf[10];
  1662. units = buf + 11;
  1663. png_debug(3, "Checking pCAL equation type and number of parameters");
  1664. /* Check that we have the right number of parameters for known
  1665. * equation types.
  1666. */
  1667. if ((type == PNG_EQUATION_LINEAR && nparams != 2) ||
  1668. (type == PNG_EQUATION_BASE_E && nparams != 3) ||
  1669. (type == PNG_EQUATION_ARBITRARY && nparams != 3) ||
  1670. (type == PNG_EQUATION_HYPERBOLIC && nparams != 4))
  1671. {
  1672. png_warning(png_ptr, "Invalid pCAL parameters for equation type");
  1673. png_free(png_ptr, png_ptr->chunkdata);
  1674. png_ptr->chunkdata = NULL;
  1675. return;
  1676. }
  1677. else if (type >= PNG_EQUATION_LAST)
  1678. {
  1679. png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
  1680. }
  1681. for (buf = units; *buf; buf++)
  1682. /* Empty loop to move past the units string. */ ;
  1683. png_debug(3, "Allocating pCAL parameters array");
  1684. params = (png_charpp)png_malloc_warn(png_ptr,
  1685. (png_size_t)(nparams * png_sizeof(png_charp)));
  1686. if (params == NULL)
  1687. {
  1688. png_free(png_ptr, png_ptr->chunkdata);
  1689. png_ptr->chunkdata = NULL;
  1690. png_warning(png_ptr, "No memory for pCAL params");
  1691. return;
  1692. }
  1693. /* Get pointers to the start of each parameter string. */
  1694. for (i = 0; i < (int)nparams; i++)
  1695. {
  1696. buf++; /* Skip the null string terminator from previous parameter. */
  1697. png_debug1(3, "Reading pCAL parameter %d", i);
  1698. for (params[i] = buf; buf <= endptr && *buf != 0x00; buf++)
  1699. /* Empty loop to move past each parameter string */ ;
  1700. /* Make sure we haven't run out of data yet */
  1701. if (buf > endptr)
  1702. {
  1703. png_warning(png_ptr, "Invalid pCAL data");
  1704. png_free(png_ptr, png_ptr->chunkdata);
  1705. png_ptr->chunkdata = NULL;
  1706. png_free(png_ptr, params);
  1707. return;
  1708. }
  1709. }
  1710. png_set_pCAL(png_ptr, info_ptr, png_ptr->chunkdata, X0, X1, type, nparams,
  1711. units, params);
  1712. png_free(png_ptr, png_ptr->chunkdata);
  1713. png_ptr->chunkdata = NULL;
  1714. png_free(png_ptr, params);
  1715. }
  1716. #endif
  1717. #ifdef PNG_READ_sCAL_SUPPORTED
  1718. /* Read the sCAL chunk */
  1719. void /* PRIVATE */
  1720. png_handle_sCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1721. {
  1722. png_size_t slength, i;
  1723. int state;
  1724. png_debug(1, "in png_handle_sCAL");
  1725. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1726. png_error(png_ptr, "Missing IHDR before sCAL");
  1727. else if (png_ptr->mode & PNG_HAVE_IDAT)
  1728. {
  1729. png_warning(png_ptr, "Invalid sCAL after IDAT");
  1730. png_crc_finish(png_ptr, length);
  1731. return;
  1732. }
  1733. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL))
  1734. {
  1735. png_warning(png_ptr, "Duplicate sCAL chunk");
  1736. png_crc_finish(png_ptr, length);
  1737. return;
  1738. }
  1739. /* Need unit type, width, \0, height: minimum 4 bytes */
  1740. else if (length < 4)
  1741. {
  1742. png_warning(png_ptr, "sCAL chunk too short");
  1743. png_crc_finish(png_ptr, length);
  1744. return;
  1745. }
  1746. png_debug1(2, "Allocating and reading sCAL chunk data (%u bytes)",
  1747. length + 1);
  1748. png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
  1749. if (png_ptr->chunkdata == NULL)
  1750. {
  1751. png_warning(png_ptr, "Out of memory while processing sCAL chunk");
  1752. png_crc_finish(png_ptr, length);
  1753. return;
  1754. }
  1755. slength = length;
  1756. png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
  1757. png_ptr->chunkdata[slength] = 0x00; /* Null terminate the last string */
  1758. if (png_crc_finish(png_ptr, 0))
  1759. {
  1760. png_free(png_ptr, png_ptr->chunkdata);
  1761. png_ptr->chunkdata = NULL;
  1762. return;
  1763. }
  1764. /* Validate the unit. */
  1765. if (png_ptr->chunkdata[0] != 1 && png_ptr->chunkdata[0] != 2)
  1766. {
  1767. png_warning(png_ptr, "Invalid sCAL ignored: invalid unit");
  1768. png_free(png_ptr, png_ptr->chunkdata);
  1769. png_ptr->chunkdata = NULL;
  1770. return;
  1771. }
  1772. /* Validate the ASCII numbers, need two ASCII numbers separated by
  1773. * a '\0' and they need to fit exactly in the chunk data.
  1774. */
  1775. i = 1;
  1776. state = 0;
  1777. if (!png_check_fp_number(png_ptr->chunkdata, slength, &state, &i) ||
  1778. i >= slength || png_ptr->chunkdata[i++] != 0)
  1779. png_warning(png_ptr, "Invalid sCAL chunk ignored: bad width format");
  1780. else if (!PNG_FP_IS_POSITIVE(state))
  1781. png_warning(png_ptr, "Invalid sCAL chunk ignored: non-positive width");
  1782. else
  1783. {
  1784. png_size_t heighti = i;
  1785. state = 0;
  1786. if (!png_check_fp_number(png_ptr->chunkdata, slength, &state, &i) ||
  1787. i != slength)
  1788. png_warning(png_ptr, "Invalid sCAL chunk ignored: bad height format");
  1789. else if (!PNG_FP_IS_POSITIVE(state))
  1790. png_warning(png_ptr,
  1791. "Invalid sCAL chunk ignored: non-positive height");
  1792. else
  1793. /* This is the (only) success case. */
  1794. png_set_sCAL_s(png_ptr, info_ptr, png_ptr->chunkdata[0],
  1795. png_ptr->chunkdata+1, png_ptr->chunkdata+heighti);
  1796. }
  1797. /* Clean up - just free the temporarily allocated buffer. */
  1798. png_free(png_ptr, png_ptr->chunkdata);
  1799. png_ptr->chunkdata = NULL;
  1800. }
  1801. #endif
  1802. #ifdef PNG_READ_tIME_SUPPORTED
  1803. void /* PRIVATE */
  1804. png_handle_tIME(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1805. {
  1806. png_byte buf[7];
  1807. png_time mod_time;
  1808. png_debug(1, "in png_handle_tIME");
  1809. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1810. png_error(png_ptr, "Out of place tIME chunk");
  1811. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME))
  1812. {
  1813. png_warning(png_ptr, "Duplicate tIME chunk");
  1814. png_crc_finish(png_ptr, length);
  1815. return;
  1816. }
  1817. if (png_ptr->mode & PNG_HAVE_IDAT)
  1818. png_ptr->mode |= PNG_AFTER_IDAT;
  1819. if (length != 7)
  1820. {
  1821. png_warning(png_ptr, "Incorrect tIME chunk length");
  1822. png_crc_finish(png_ptr, length);
  1823. return;
  1824. }
  1825. png_crc_read(png_ptr, buf, 7);
  1826. if (png_crc_finish(png_ptr, 0))
  1827. return;
  1828. mod_time.second = buf[6];
  1829. mod_time.minute = buf[5];
  1830. mod_time.hour = buf[4];
  1831. mod_time.day = buf[3];
  1832. mod_time.month = buf[2];
  1833. mod_time.year = png_get_uint_16(buf);
  1834. png_set_tIME(png_ptr, info_ptr, &mod_time);
  1835. }
  1836. #endif
  1837. #ifdef PNG_READ_tEXt_SUPPORTED
  1838. /* Note: this does not properly handle chunks that are > 64K under DOS */
  1839. void /* PRIVATE */
  1840. png_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1841. {
  1842. png_textp text_ptr;
  1843. png_charp key;
  1844. png_charp text;
  1845. png_uint_32 skip = 0;
  1846. png_size_t slength;
  1847. int ret;
  1848. png_debug(1, "in png_handle_tEXt");
  1849. #ifdef PNG_USER_LIMITS_SUPPORTED
  1850. if (png_ptr->user_chunk_cache_max != 0)
  1851. {
  1852. if (png_ptr->user_chunk_cache_max == 1)
  1853. {
  1854. png_crc_finish(png_ptr, length);
  1855. return;
  1856. }
  1857. if (--png_ptr->user_chunk_cache_max == 1)
  1858. {
  1859. png_warning(png_ptr, "No space in chunk cache for tEXt");
  1860. png_crc_finish(png_ptr, length);
  1861. return;
  1862. }
  1863. }
  1864. #endif
  1865. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1866. png_error(png_ptr, "Missing IHDR before tEXt");
  1867. if (png_ptr->mode & PNG_HAVE_IDAT)
  1868. png_ptr->mode |= PNG_AFTER_IDAT;
  1869. #ifdef PNG_MAX_MALLOC_64K
  1870. if (length > (png_uint_32)65535L)
  1871. {
  1872. png_warning(png_ptr, "tEXt chunk too large to fit in memory");
  1873. skip = length - (png_uint_32)65535L;
  1874. length = (png_uint_32)65535L;
  1875. }
  1876. #endif
  1877. png_free(png_ptr, png_ptr->chunkdata);
  1878. png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
  1879. if (png_ptr->chunkdata == NULL)
  1880. {
  1881. png_warning(png_ptr, "No memory to process text chunk");
  1882. return;
  1883. }
  1884. slength = length;
  1885. png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
  1886. if (png_crc_finish(png_ptr, skip))
  1887. {
  1888. png_free(png_ptr, png_ptr->chunkdata);
  1889. png_ptr->chunkdata = NULL;
  1890. return;
  1891. }
  1892. key = png_ptr->chunkdata;
  1893. key[slength] = 0x00;
  1894. for (text = key; *text; text++)
  1895. /* Empty loop to find end of key */ ;
  1896. if (text != key + slength)
  1897. text++;
  1898. text_ptr = (png_textp)png_malloc_warn(png_ptr,
  1899. png_sizeof(png_text));
  1900. if (text_ptr == NULL)
  1901. {
  1902. png_warning(png_ptr, "Not enough memory to process text chunk");
  1903. png_free(png_ptr, png_ptr->chunkdata);
  1904. png_ptr->chunkdata = NULL;
  1905. return;
  1906. }
  1907. text_ptr->compression = PNG_TEXT_COMPRESSION_NONE;
  1908. text_ptr->key = key;
  1909. text_ptr->lang = NULL;
  1910. text_ptr->lang_key = NULL;
  1911. text_ptr->itxt_length = 0;
  1912. text_ptr->text = text;
  1913. text_ptr->text_length = png_strlen(text);
  1914. ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
  1915. png_free(png_ptr, png_ptr->chunkdata);
  1916. png_ptr->chunkdata = NULL;
  1917. png_free(png_ptr, text_ptr);
  1918. if (ret != 0)
  1919. png_warning(png_ptr, "Insufficient memory to process text chunk");
  1920. }
  1921. #endif
  1922. #ifdef PNG_READ_zTXt_SUPPORTED
  1923. /* Note: this does not correctly handle chunks that are > 64K under DOS */
  1924. void /* PRIVATE */
  1925. png_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1926. {
  1927. png_textp text_ptr;
  1928. png_charp text;
  1929. int comp_type;
  1930. int ret;
  1931. png_size_t slength, prefix_len, data_len;
  1932. png_debug(1, "in png_handle_zTXt");
  1933. #ifdef PNG_USER_LIMITS_SUPPORTED
  1934. if (png_ptr->user_chunk_cache_max != 0)
  1935. {
  1936. if (png_ptr->user_chunk_cache_max == 1)
  1937. {
  1938. png_crc_finish(png_ptr, length);
  1939. return;
  1940. }
  1941. if (--png_ptr->user_chunk_cache_max == 1)
  1942. {
  1943. png_warning(png_ptr, "No space in chunk cache for zTXt");
  1944. png_crc_finish(png_ptr, length);
  1945. return;
  1946. }
  1947. }
  1948. #endif
  1949. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1950. png_error(png_ptr, "Missing IHDR before zTXt");
  1951. if (png_ptr->mode & PNG_HAVE_IDAT)
  1952. png_ptr->mode |= PNG_AFTER_IDAT;
  1953. #ifdef PNG_MAX_MALLOC_64K
  1954. /* We will no doubt have problems with chunks even half this size, but
  1955. * there is no hard and fast rule to tell us where to stop.
  1956. */
  1957. if (length > (png_uint_32)65535L)
  1958. {
  1959. png_warning(png_ptr, "zTXt chunk too large to fit in memory");
  1960. png_crc_finish(png_ptr, length);
  1961. return;
  1962. }
  1963. #endif
  1964. png_free(png_ptr, png_ptr->chunkdata);
  1965. png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
  1966. if (png_ptr->chunkdata == NULL)
  1967. {
  1968. png_warning(png_ptr, "Out of memory processing zTXt chunk");
  1969. return;
  1970. }
  1971. slength = length;
  1972. png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
  1973. if (png_crc_finish(png_ptr, 0))
  1974. {
  1975. png_free(png_ptr, png_ptr->chunkdata);
  1976. png_ptr->chunkdata = NULL;
  1977. return;
  1978. }
  1979. png_ptr->chunkdata[slength] = 0x00;
  1980. for (text = png_ptr->chunkdata; *text; text++)
  1981. /* Empty loop */ ;
  1982. /* zTXt must have some text after the chunkdataword */
  1983. if (text >= png_ptr->chunkdata + slength - 2)
  1984. {
  1985. png_warning(png_ptr, "Truncated zTXt chunk");
  1986. png_free(png_ptr, png_ptr->chunkdata);
  1987. png_ptr->chunkdata = NULL;
  1988. return;
  1989. }
  1990. else
  1991. {
  1992. comp_type = *(++text);
  1993. if (comp_type != PNG_TEXT_COMPRESSION_zTXt)
  1994. {
  1995. png_warning(png_ptr, "Unknown compression type in zTXt chunk");
  1996. comp_type = PNG_TEXT_COMPRESSION_zTXt;
  1997. }
  1998. text++; /* Skip the compression_method byte */
  1999. }
  2000. prefix_len = text - png_ptr->chunkdata;
  2001. png_decompress_chunk(png_ptr, comp_type,
  2002. (png_size_t)length, prefix_len, &data_len);
  2003. text_ptr = (png_textp)png_malloc_warn(png_ptr,
  2004. png_sizeof(png_text));
  2005. if (text_ptr == NULL)
  2006. {
  2007. png_warning(png_ptr, "Not enough memory to process zTXt chunk");
  2008. png_free(png_ptr, png_ptr->chunkdata);
  2009. png_ptr->chunkdata = NULL;
  2010. return;
  2011. }
  2012. text_ptr->compression = comp_type;
  2013. text_ptr->key = png_ptr->chunkdata;
  2014. text_ptr->lang = NULL;
  2015. text_ptr->lang_key = NULL;
  2016. text_ptr->itxt_length = 0;
  2017. text_ptr->text = png_ptr->chunkdata + prefix_len;
  2018. text_ptr->text_length = data_len;
  2019. ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
  2020. png_free(png_ptr, text_ptr);
  2021. png_free(png_ptr, png_ptr->chunkdata);
  2022. png_ptr->chunkdata = NULL;
  2023. if (ret != 0)
  2024. png_error(png_ptr, "Insufficient memory to store zTXt chunk");
  2025. }
  2026. #endif
  2027. #ifdef PNG_READ_iTXt_SUPPORTED
  2028. /* Note: this does not correctly handle chunks that are > 64K under DOS */
  2029. void /* PRIVATE */
  2030. png_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  2031. {
  2032. png_textp text_ptr;
  2033. png_charp key, lang, text, lang_key;
  2034. int comp_flag;
  2035. int comp_type;
  2036. int ret;
  2037. png_size_t slength, prefix_len, data_len;
  2038. png_debug(1, "in png_handle_iTXt");
  2039. #ifdef PNG_USER_LIMITS_SUPPORTED
  2040. if (png_ptr->user_chunk_cache_max != 0)
  2041. {
  2042. if (png_ptr->user_chunk_cache_max == 1)
  2043. {
  2044. png_crc_finish(png_ptr, length);
  2045. return;
  2046. }
  2047. if (--png_ptr->user_chunk_cache_max == 1)
  2048. {
  2049. png_warning(png_ptr, "No space in chunk cache for iTXt");
  2050. png_crc_finish(png_ptr, length);
  2051. return;
  2052. }
  2053. }
  2054. #endif
  2055. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  2056. png_error(png_ptr, "Missing IHDR before iTXt");
  2057. if (png_ptr->mode & PNG_HAVE_IDAT)
  2058. png_ptr->mode |= PNG_AFTER_IDAT;
  2059. #ifdef PNG_MAX_MALLOC_64K
  2060. /* We will no doubt have problems with chunks even half this size, but
  2061. * there is no hard and fast rule to tell us where to stop.
  2062. */
  2063. if (length > (png_uint_32)65535L)
  2064. {
  2065. png_warning(png_ptr, "iTXt chunk too large to fit in memory");
  2066. png_crc_finish(png_ptr, length);
  2067. return;
  2068. }
  2069. #endif
  2070. png_free(png_ptr, png_ptr->chunkdata);
  2071. png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
  2072. if (png_ptr->chunkdata == NULL)
  2073. {
  2074. png_warning(png_ptr, "No memory to process iTXt chunk");
  2075. return;
  2076. }
  2077. slength = length;
  2078. png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
  2079. if (png_crc_finish(png_ptr, 0))
  2080. {
  2081. png_free(png_ptr, png_ptr->chunkdata);
  2082. png_ptr->chunkdata = NULL;
  2083. return;
  2084. }
  2085. png_ptr->chunkdata[slength] = 0x00;
  2086. for (lang = png_ptr->chunkdata; *lang; lang++)
  2087. /* Empty loop */ ;
  2088. lang++; /* Skip NUL separator */
  2089. /* iTXt must have a language tag (possibly empty), two compression bytes,
  2090. * translated keyword (possibly empty), and possibly some text after the
  2091. * keyword
  2092. */
  2093. if (lang >= png_ptr->chunkdata + slength - 3)
  2094. {
  2095. png_warning(png_ptr, "Truncated iTXt chunk");
  2096. png_free(png_ptr, png_ptr->chunkdata);
  2097. png_ptr->chunkdata = NULL;
  2098. return;
  2099. }
  2100. comp_flag = *lang++;
  2101. comp_type = *lang++;
  2102. /* 1.5.14: The spec says "for uncompressed text decoders shall ignore [the
  2103. * compression type]". The compression flag shall be 0 (no compression) or
  2104. * 1 (compressed with method 0 - deflate.)
  2105. */
  2106. if (comp_flag/*compressed*/ != 0)
  2107. {
  2108. if (comp_flag != 1)
  2109. {
  2110. png_warning(png_ptr, "invalid iTXt compression flag");
  2111. png_free(png_ptr, png_ptr->chunkdata);
  2112. png_ptr->chunkdata = NULL;
  2113. return;
  2114. }
  2115. if (comp_type != 0)
  2116. {
  2117. png_warning(png_ptr, "unknown iTXt compression type");
  2118. png_free(png_ptr, png_ptr->chunkdata);
  2119. png_ptr->chunkdata = NULL;
  2120. return;
  2121. }
  2122. }
  2123. for (lang_key = lang; *lang_key; lang_key++)
  2124. /* Empty loop */ ;
  2125. lang_key++; /* Skip NUL separator */
  2126. if (lang_key >= png_ptr->chunkdata + slength)
  2127. {
  2128. png_warning(png_ptr, "Truncated iTXt chunk");
  2129. png_free(png_ptr, png_ptr->chunkdata);
  2130. png_ptr->chunkdata = NULL;
  2131. return;
  2132. }
  2133. for (text = lang_key; *text; text++)
  2134. /* Empty loop */ ;
  2135. text++; /* Skip NUL separator */
  2136. if (text >= png_ptr->chunkdata + slength)
  2137. {
  2138. png_warning(png_ptr, "Malformed iTXt chunk");
  2139. png_free(png_ptr, png_ptr->chunkdata);
  2140. png_ptr->chunkdata = NULL;
  2141. return;
  2142. }
  2143. prefix_len = text - png_ptr->chunkdata;
  2144. key=png_ptr->chunkdata;
  2145. if (comp_flag/*compressed*/)
  2146. png_decompress_chunk(png_ptr, comp_type,
  2147. (size_t)length, prefix_len, &data_len);
  2148. else
  2149. data_len = png_strlen(png_ptr->chunkdata + prefix_len);
  2150. text_ptr = (png_textp)png_malloc_warn(png_ptr,
  2151. png_sizeof(png_text));
  2152. if (text_ptr == NULL)
  2153. {
  2154. png_warning(png_ptr, "Not enough memory to process iTXt chunk");
  2155. png_free(png_ptr, png_ptr->chunkdata);
  2156. png_ptr->chunkdata = NULL;
  2157. return;
  2158. }
  2159. text_ptr->compression =
  2160. (comp_flag ? PNG_ITXT_COMPRESSION_zTXt : PNG_ITXT_COMPRESSION_NONE);
  2161. text_ptr->lang_key = png_ptr->chunkdata + (lang_key - key);
  2162. text_ptr->lang = png_ptr->chunkdata + (lang - key);
  2163. text_ptr->itxt_length = data_len;
  2164. text_ptr->text_length = 0;
  2165. text_ptr->key = png_ptr->chunkdata;
  2166. text_ptr->text = png_ptr->chunkdata + prefix_len;
  2167. ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
  2168. png_free(png_ptr, text_ptr);
  2169. png_free(png_ptr, png_ptr->chunkdata);
  2170. png_ptr->chunkdata = NULL;
  2171. if (ret != 0)
  2172. png_error(png_ptr, "Insufficient memory to store iTXt chunk");
  2173. }
  2174. #endif
  2175. /* This function is called when we haven't found a handler for a
  2176. * chunk. If there isn't a problem with the chunk itself (ie bad
  2177. * chunk name, CRC, or a critical chunk), the chunk is silently ignored
  2178. * -- unless the PNG_FLAG_UNKNOWN_CHUNKS_SUPPORTED flag is on in which
  2179. * case it will be saved away to be written out later.
  2180. */
  2181. void /* PRIVATE */
  2182. png_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  2183. {
  2184. png_uint_32 skip = 0;
  2185. png_debug(1, "in png_handle_unknown");
  2186. #ifdef PNG_USER_LIMITS_SUPPORTED
  2187. if (png_ptr->user_chunk_cache_max != 0)
  2188. {
  2189. if (png_ptr->user_chunk_cache_max == 1)
  2190. {
  2191. png_crc_finish(png_ptr, length);
  2192. return;
  2193. }
  2194. if (--png_ptr->user_chunk_cache_max == 1)
  2195. {
  2196. png_warning(png_ptr, "No space in chunk cache for unknown chunk");
  2197. png_crc_finish(png_ptr, length);
  2198. return;
  2199. }
  2200. }
  2201. #endif
  2202. if (png_ptr->mode & PNG_HAVE_IDAT)
  2203. {
  2204. if (png_ptr->chunk_name != png_IDAT)
  2205. png_ptr->mode |= PNG_AFTER_IDAT;
  2206. }
  2207. if (PNG_CHUNK_CRITICAL(png_ptr->chunk_name))
  2208. {
  2209. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  2210. if (png_chunk_unknown_handling(png_ptr, png_ptr->chunk_name) !=
  2211. PNG_HANDLE_CHUNK_ALWAYS
  2212. #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
  2213. && png_ptr->read_user_chunk_fn == NULL
  2214. #endif
  2215. )
  2216. #endif
  2217. png_chunk_error(png_ptr, "unknown critical chunk");
  2218. }
  2219. #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
  2220. if ((png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS)
  2221. #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
  2222. || (png_ptr->read_user_chunk_fn != NULL)
  2223. #endif
  2224. )
  2225. {
  2226. #ifdef PNG_MAX_MALLOC_64K
  2227. if (length > 65535)
  2228. {
  2229. png_warning(png_ptr, "unknown chunk too large to fit in memory");
  2230. skip = length - 65535;
  2231. length = 65535;
  2232. }
  2233. #endif
  2234. /* TODO: this code is very close to the unknown handling in pngpread.c,
  2235. * maybe it can be put into a common utility routine?
  2236. * png_struct::unknown_chunk is just used as a temporary variable, along
  2237. * with the data into which the chunk is read. These can be eliminated.
  2238. */
  2239. PNG_CSTRING_FROM_CHUNK(png_ptr->unknown_chunk.name, png_ptr->chunk_name);
  2240. png_ptr->unknown_chunk.size = (png_size_t)length;
  2241. if (length == 0)
  2242. png_ptr->unknown_chunk.data = NULL;
  2243. else
  2244. {
  2245. png_ptr->unknown_chunk.data = (png_bytep)png_malloc(png_ptr, length);
  2246. png_crc_read(png_ptr, png_ptr->unknown_chunk.data, length);
  2247. }
  2248. #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
  2249. if (png_ptr->read_user_chunk_fn != NULL)
  2250. {
  2251. /* Callback to user unknown chunk handler */
  2252. int ret;
  2253. ret = (*(png_ptr->read_user_chunk_fn))
  2254. (png_ptr, &png_ptr->unknown_chunk);
  2255. if (ret < 0)
  2256. png_chunk_error(png_ptr, "error in user chunk");
  2257. if (ret == 0)
  2258. {
  2259. if (PNG_CHUNK_CRITICAL(png_ptr->chunk_name))
  2260. {
  2261. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  2262. if (png_chunk_unknown_handling(png_ptr, png_ptr->chunk_name) !=
  2263. PNG_HANDLE_CHUNK_ALWAYS)
  2264. #endif
  2265. png_chunk_error(png_ptr, "unknown critical chunk");
  2266. }
  2267. png_set_unknown_chunks(png_ptr, info_ptr,
  2268. &png_ptr->unknown_chunk, 1);
  2269. }
  2270. }
  2271. else
  2272. #endif
  2273. png_set_unknown_chunks(png_ptr, info_ptr, &png_ptr->unknown_chunk, 1);
  2274. png_free(png_ptr, png_ptr->unknown_chunk.data);
  2275. png_ptr->unknown_chunk.data = NULL;
  2276. }
  2277. else
  2278. #endif
  2279. skip = length;
  2280. png_crc_finish(png_ptr, skip);
  2281. #ifndef PNG_READ_USER_CHUNKS_SUPPORTED
  2282. PNG_UNUSED(info_ptr) /* Quiet compiler warnings about unused info_ptr */
  2283. #endif
  2284. }
  2285. /* This function is called to verify that a chunk name is valid.
  2286. * This function can't have the "critical chunk check" incorporated
  2287. * into it, since in the future we will need to be able to call user
  2288. * functions to handle unknown critical chunks after we check that
  2289. * the chunk name itself is valid.
  2290. */
  2291. /* Bit hacking: the test for an invalid byte in the 4 byte chunk name is:
  2292. *
  2293. * ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
  2294. */
  2295. void /* PRIVATE */
  2296. png_check_chunk_name(png_structp png_ptr, png_uint_32 chunk_name)
  2297. {
  2298. int i;
  2299. png_debug(1, "in png_check_chunk_name");
  2300. for (i=1; i<=4; ++i)
  2301. {
  2302. int c = chunk_name & 0xff;
  2303. if (c < 65 || c > 122 || (c > 90 && c < 97))
  2304. png_chunk_error(png_ptr, "invalid chunk type");
  2305. chunk_name >>= 8;
  2306. }
  2307. }
  2308. /* Combines the row recently read in with the existing pixels in the row. This
  2309. * routine takes care of alpha and transparency if requested. This routine also
  2310. * handles the two methods of progressive display of interlaced images,
  2311. * depending on the 'display' value; if 'display' is true then the whole row
  2312. * (dp) is filled from the start by replicating the available pixels. If
  2313. * 'display' is false only those pixels present in the pass are filled in.
  2314. */
  2315. void /* PRIVATE */
  2316. png_combine_row(png_structp png_ptr, png_bytep dp, int display)
  2317. {
  2318. unsigned int pixel_depth = png_ptr->transformed_pixel_depth;
  2319. png_const_bytep sp = png_ptr->row_buf + 1;
  2320. png_alloc_size_t row_width = png_ptr->width;
  2321. unsigned int pass = png_ptr->pass;
  2322. png_bytep end_ptr = 0;
  2323. png_byte end_byte = 0;
  2324. unsigned int end_mask;
  2325. png_debug(1, "in png_combine_row");
  2326. /* Added in 1.5.6: it should not be possible to enter this routine until at
  2327. * least one row has been read from the PNG data and transformed.
  2328. */
  2329. if (pixel_depth == 0)
  2330. png_error(png_ptr, "internal row logic error");
  2331. /* Added in 1.5.4: the pixel depth should match the information returned by
  2332. * any call to png_read_update_info at this point. Do not continue if we got
  2333. * this wrong.
  2334. */
  2335. if (png_ptr->info_rowbytes != 0 && png_ptr->info_rowbytes !=
  2336. PNG_ROWBYTES(pixel_depth, row_width))
  2337. png_error(png_ptr, "internal row size calculation error");
  2338. /* Don't expect this to ever happen: */
  2339. if (row_width == 0)
  2340. png_error(png_ptr, "internal row width error");
  2341. /* Preserve the last byte in cases where only part of it will be overwritten,
  2342. * the multiply below may overflow, we don't care because ANSI-C guarantees
  2343. * we get the low bits.
  2344. */
  2345. end_mask = (pixel_depth * row_width) & 7;
  2346. if (end_mask != 0)
  2347. {
  2348. /* end_ptr == NULL is a flag to say do nothing */
  2349. end_ptr = dp + PNG_ROWBYTES(pixel_depth, row_width) - 1;
  2350. end_byte = *end_ptr;
  2351. # ifdef PNG_READ_PACKSWAP_SUPPORTED
  2352. if (png_ptr->transformations & PNG_PACKSWAP) /* little-endian byte */
  2353. end_mask = 0xff << end_mask;
  2354. else /* big-endian byte */
  2355. # endif
  2356. end_mask = 0xff >> end_mask;
  2357. /* end_mask is now the bits to *keep* from the destination row */
  2358. }
  2359. /* For non-interlaced images this reduces to a png_memcpy(). A png_memcpy()
  2360. * will also happen if interlacing isn't supported or if the application
  2361. * does not call png_set_interlace_handling(). In the latter cases the
  2362. * caller just gets a sequence of the unexpanded rows from each interlace
  2363. * pass.
  2364. */
  2365. #ifdef PNG_READ_INTERLACING_SUPPORTED
  2366. if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE) &&
  2367. pass < 6 && (display == 0 ||
  2368. /* The following copies everything for 'display' on passes 0, 2 and 4. */
  2369. (display == 1 && (pass & 1) != 0)))
  2370. {
  2371. /* Narrow images may have no bits in a pass; the caller should handle
  2372. * this, but this test is cheap:
  2373. */
  2374. if (row_width <= PNG_PASS_START_COL(pass))
  2375. return;
  2376. if (pixel_depth < 8)
  2377. {
  2378. /* For pixel depths up to 4 bpp the 8-pixel mask can be expanded to fit
  2379. * into 32 bits, then a single loop over the bytes using the four byte
  2380. * values in the 32-bit mask can be used. For the 'display' option the
  2381. * expanded mask may also not require any masking within a byte. To
  2382. * make this work the PACKSWAP option must be taken into account - it
  2383. * simply requires the pixels to be reversed in each byte.
  2384. *
  2385. * The 'regular' case requires a mask for each of the first 6 passes,
  2386. * the 'display' case does a copy for the even passes in the range
  2387. * 0..6. This has already been handled in the test above.
  2388. *
  2389. * The masks are arranged as four bytes with the first byte to use in
  2390. * the lowest bits (little-endian) regardless of the order (PACKSWAP or
  2391. * not) of the pixels in each byte.
  2392. *
  2393. * NOTE: the whole of this logic depends on the caller of this function
  2394. * only calling it on rows appropriate to the pass. This function only
  2395. * understands the 'x' logic; the 'y' logic is handled by the caller.
  2396. *
  2397. * The following defines allow generation of compile time constant bit
  2398. * masks for each pixel depth and each possibility of swapped or not
  2399. * swapped bytes. Pass 'p' is in the range 0..6; 'x', a pixel index,
  2400. * is in the range 0..7; and the result is 1 if the pixel is to be
  2401. * copied in the pass, 0 if not. 'S' is for the sparkle method, 'B'
  2402. * for the block method.
  2403. *
  2404. * With some compilers a compile time expression of the general form:
  2405. *
  2406. * (shift >= 32) ? (a >> (shift-32)) : (b >> shift)
  2407. *
  2408. * Produces warnings with values of 'shift' in the range 33 to 63
  2409. * because the right hand side of the ?: expression is evaluated by
  2410. * the compiler even though it isn't used. Microsoft Visual C (various
  2411. * versions) and the Intel C compiler are known to do this. To avoid
  2412. * this the following macros are used in 1.5.6. This is a temporary
  2413. * solution to avoid destabilizing the code during the release process.
  2414. */
  2415. # if PNG_USE_COMPILE_TIME_MASKS
  2416. # define PNG_LSR(x,s) ((x)>>((s) & 0x1f))
  2417. # define PNG_LSL(x,s) ((x)<<((s) & 0x1f))
  2418. # else
  2419. # define PNG_LSR(x,s) ((x)>>(s))
  2420. # define PNG_LSL(x,s) ((x)<<(s))
  2421. # endif
  2422. # define S_COPY(p,x) (((p)<4 ? PNG_LSR(0x80088822,(3-(p))*8+(7-(x))) :\
  2423. PNG_LSR(0xaa55ff00,(7-(p))*8+(7-(x)))) & 1)
  2424. # define B_COPY(p,x) (((p)<4 ? PNG_LSR(0xff0fff33,(3-(p))*8+(7-(x))) :\
  2425. PNG_LSR(0xff55ff00,(7-(p))*8+(7-(x)))) & 1)
  2426. /* Return a mask for pass 'p' pixel 'x' at depth 'd'. The mask is
  2427. * little endian - the first pixel is at bit 0 - however the extra
  2428. * parameter 's' can be set to cause the mask position to be swapped
  2429. * within each byte, to match the PNG format. This is done by XOR of
  2430. * the shift with 7, 6 or 4 for bit depths 1, 2 and 4.
  2431. */
  2432. # define PIXEL_MASK(p,x,d,s) \
  2433. (PNG_LSL(((PNG_LSL(1U,(d)))-1),(((x)*(d))^((s)?8-(d):0))))
  2434. /* Hence generate the appropriate 'block' or 'sparkle' pixel copy mask.
  2435. */
  2436. # define S_MASKx(p,x,d,s) (S_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
  2437. # define B_MASKx(p,x,d,s) (B_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
  2438. /* Combine 8 of these to get the full mask. For the 1-bpp and 2-bpp
  2439. * cases the result needs replicating, for the 4-bpp case the above
  2440. * generates a full 32 bits.
  2441. */
  2442. # define MASK_EXPAND(m,d) ((m)*((d)==1?0x01010101:((d)==2?0x00010001:1)))
  2443. # define S_MASK(p,d,s) MASK_EXPAND(S_MASKx(p,0,d,s) + S_MASKx(p,1,d,s) +\
  2444. S_MASKx(p,2,d,s) + S_MASKx(p,3,d,s) + S_MASKx(p,4,d,s) +\
  2445. S_MASKx(p,5,d,s) + S_MASKx(p,6,d,s) + S_MASKx(p,7,d,s), d)
  2446. # define B_MASK(p,d,s) MASK_EXPAND(B_MASKx(p,0,d,s) + B_MASKx(p,1,d,s) +\
  2447. B_MASKx(p,2,d,s) + B_MASKx(p,3,d,s) + B_MASKx(p,4,d,s) +\
  2448. B_MASKx(p,5,d,s) + B_MASKx(p,6,d,s) + B_MASKx(p,7,d,s), d)
  2449. #if PNG_USE_COMPILE_TIME_MASKS
  2450. /* Utility macros to construct all the masks for a depth/swap
  2451. * combination. The 's' parameter says whether the format is PNG
  2452. * (big endian bytes) or not. Only the three odd-numbered passes are
  2453. * required for the display/block algorithm.
  2454. */
  2455. # define S_MASKS(d,s) { S_MASK(0,d,s), S_MASK(1,d,s), S_MASK(2,d,s),\
  2456. S_MASK(3,d,s), S_MASK(4,d,s), S_MASK(5,d,s) }
  2457. # define B_MASKS(d,s) { B_MASK(1,d,s), B_MASK(3,d,s), B_MASK(5,d,s) }
  2458. # define DEPTH_INDEX(d) ((d)==1?0:((d)==2?1:2))
  2459. /* Hence the pre-compiled masks indexed by PACKSWAP (or not), depth and
  2460. * then pass:
  2461. */
  2462. static PNG_CONST png_uint_32 row_mask[2/*PACKSWAP*/][3/*depth*/][6] =
  2463. {
  2464. /* Little-endian byte masks for PACKSWAP */
  2465. { S_MASKS(1,0), S_MASKS(2,0), S_MASKS(4,0) },
  2466. /* Normal (big-endian byte) masks - PNG format */
  2467. { S_MASKS(1,1), S_MASKS(2,1), S_MASKS(4,1) }
  2468. };
  2469. /* display_mask has only three entries for the odd passes, so index by
  2470. * pass>>1.
  2471. */
  2472. static PNG_CONST png_uint_32 display_mask[2][3][3] =
  2473. {
  2474. /* Little-endian byte masks for PACKSWAP */
  2475. { B_MASKS(1,0), B_MASKS(2,0), B_MASKS(4,0) },
  2476. /* Normal (big-endian byte) masks - PNG format */
  2477. { B_MASKS(1,1), B_MASKS(2,1), B_MASKS(4,1) }
  2478. };
  2479. # define MASK(pass,depth,display,png)\
  2480. ((display)?display_mask[png][DEPTH_INDEX(depth)][pass>>1]:\
  2481. row_mask[png][DEPTH_INDEX(depth)][pass])
  2482. #else /* !PNG_USE_COMPILE_TIME_MASKS */
  2483. /* This is the runtime alternative: it seems unlikely that this will
  2484. * ever be either smaller or faster than the compile time approach.
  2485. */
  2486. # define MASK(pass,depth,display,png)\
  2487. ((display)?B_MASK(pass,depth,png):S_MASK(pass,depth,png))
  2488. #endif /* !PNG_USE_COMPILE_TIME_MASKS */
  2489. /* Use the appropriate mask to copy the required bits. In some cases
  2490. * the byte mask will be 0 or 0xff, optimize these cases. row_width is
  2491. * the number of pixels, but the code copies bytes, so it is necessary
  2492. * to special case the end.
  2493. */
  2494. png_uint_32 pixels_per_byte = 8 / pixel_depth;
  2495. png_uint_32 mask;
  2496. # ifdef PNG_READ_PACKSWAP_SUPPORTED
  2497. if (png_ptr->transformations & PNG_PACKSWAP)
  2498. mask = MASK(pass, pixel_depth, display, 0);
  2499. else
  2500. # endif
  2501. mask = MASK(pass, pixel_depth, display, 1);
  2502. for (;;)
  2503. {
  2504. png_uint_32 m;
  2505. /* It doesn't matter in the following if png_uint_32 has more than
  2506. * 32 bits because the high bits always match those in m<<24; it is,
  2507. * however, essential to use OR here, not +, because of this.
  2508. */
  2509. m = mask;
  2510. mask = (m >> 8) | (m << 24); /* rotate right to good compilers */
  2511. m &= 0xff;
  2512. if (m != 0) /* something to copy */
  2513. {
  2514. if (m != 0xff)
  2515. *dp = (png_byte)((*dp & ~m) | (*sp & m));
  2516. else
  2517. *dp = *sp;
  2518. }
  2519. /* NOTE: this may overwrite the last byte with garbage if the image
  2520. * is not an exact number of bytes wide; libpng has always done
  2521. * this.
  2522. */
  2523. if (row_width <= pixels_per_byte)
  2524. break; /* May need to restore part of the last byte */
  2525. row_width -= pixels_per_byte;
  2526. ++dp;
  2527. ++sp;
  2528. }
  2529. }
  2530. else /* pixel_depth >= 8 */
  2531. {
  2532. unsigned int bytes_to_copy, bytes_to_jump;
  2533. /* Validate the depth - it must be a multiple of 8 */
  2534. if (pixel_depth & 7)
  2535. png_error(png_ptr, "invalid user transform pixel depth");
  2536. pixel_depth >>= 3; /* now in bytes */
  2537. row_width *= pixel_depth;
  2538. /* Regardless of pass number the Adam 7 interlace always results in a
  2539. * fixed number of pixels to copy then to skip. There may be a
  2540. * different number of pixels to skip at the start though.
  2541. */
  2542. {
  2543. unsigned int offset = PNG_PASS_START_COL(pass) * pixel_depth;
  2544. row_width -= offset;
  2545. dp += offset;
  2546. sp += offset;
  2547. }
  2548. /* Work out the bytes to copy. */
  2549. if (display != 0)
  2550. {
  2551. /* When doing the 'block' algorithm the pixel in the pass gets
  2552. * replicated to adjacent pixels. This is why the even (0,2,4,6)
  2553. * passes are skipped above - the entire expanded row is copied.
  2554. */
  2555. bytes_to_copy = (1<<((6-pass)>>1)) * pixel_depth;
  2556. /* But don't allow this number to exceed the actual row width. */
  2557. if (bytes_to_copy > row_width)
  2558. bytes_to_copy = (unsigned int)/*SAFE*/row_width;
  2559. }
  2560. else /* normal row; Adam7 only ever gives us one pixel to copy. */
  2561. bytes_to_copy = pixel_depth;
  2562. /* In Adam7 there is a constant offset between where the pixels go. */
  2563. bytes_to_jump = PNG_PASS_COL_OFFSET(pass) * pixel_depth;
  2564. /* And simply copy these bytes. Some optimization is possible here,
  2565. * depending on the value of 'bytes_to_copy'. Special case the low
  2566. * byte counts, which we know to be frequent.
  2567. *
  2568. * Notice that these cases all 'return' rather than 'break' - this
  2569. * avoids an unnecessary test on whether to restore the last byte
  2570. * below.
  2571. */
  2572. switch (bytes_to_copy)
  2573. {
  2574. case 1:
  2575. for (;;)
  2576. {
  2577. *dp = *sp;
  2578. if (row_width <= bytes_to_jump)
  2579. return;
  2580. dp += bytes_to_jump;
  2581. sp += bytes_to_jump;
  2582. row_width -= bytes_to_jump;
  2583. }
  2584. case 2:
  2585. /* There is a possibility of a partial copy at the end here; this
  2586. * slows the code down somewhat.
  2587. */
  2588. do
  2589. {
  2590. dp[0] = sp[0], dp[1] = sp[1];
  2591. if (row_width <= bytes_to_jump)
  2592. return;
  2593. sp += bytes_to_jump;
  2594. dp += bytes_to_jump;
  2595. row_width -= bytes_to_jump;
  2596. }
  2597. while (row_width > 1);
  2598. /* And there can only be one byte left at this point: */
  2599. *dp = *sp;
  2600. return;
  2601. case 3:
  2602. /* This can only be the RGB case, so each copy is exactly one
  2603. * pixel and it is not necessary to check for a partial copy.
  2604. */
  2605. for(;;)
  2606. {
  2607. dp[0] = sp[0], dp[1] = sp[1], dp[2] = sp[2];
  2608. if (row_width <= bytes_to_jump)
  2609. return;
  2610. sp += bytes_to_jump;
  2611. dp += bytes_to_jump;
  2612. row_width -= bytes_to_jump;
  2613. }
  2614. default:
  2615. #if PNG_ALIGN_TYPE != PNG_ALIGN_NONE
  2616. /* Check for double byte alignment and, if possible, use a
  2617. * 16-bit copy. Don't attempt this for narrow images - ones that
  2618. * are less than an interlace panel wide. Don't attempt it for
  2619. * wide bytes_to_copy either - use the png_memcpy there.
  2620. */
  2621. if (bytes_to_copy < 16 /*else use png_memcpy*/ &&
  2622. png_isaligned(dp, png_uint_16) &&
  2623. png_isaligned(sp, png_uint_16) &&
  2624. bytes_to_copy % sizeof (png_uint_16) == 0 &&
  2625. bytes_to_jump % sizeof (png_uint_16) == 0)
  2626. {
  2627. /* Everything is aligned for png_uint_16 copies, but try for
  2628. * png_uint_32 first.
  2629. */
  2630. if (png_isaligned(dp, png_uint_32) &&
  2631. png_isaligned(sp, png_uint_32) &&
  2632. bytes_to_copy % sizeof (png_uint_32) == 0 &&
  2633. bytes_to_jump % sizeof (png_uint_32) == 0)
  2634. {
  2635. png_uint_32p dp32 = (png_uint_32p)dp;
  2636. png_const_uint_32p sp32 = (png_const_uint_32p)sp;
  2637. size_t skip = (bytes_to_jump-bytes_to_copy) /
  2638. sizeof (png_uint_32);
  2639. do
  2640. {
  2641. size_t c = bytes_to_copy;
  2642. do
  2643. {
  2644. *dp32++ = *sp32++;
  2645. c -= sizeof (png_uint_32);
  2646. }
  2647. while (c > 0);
  2648. if (row_width <= bytes_to_jump)
  2649. return;
  2650. dp32 += skip;
  2651. sp32 += skip;
  2652. row_width -= bytes_to_jump;
  2653. }
  2654. while (bytes_to_copy <= row_width);
  2655. /* Get to here when the row_width truncates the final copy.
  2656. * There will be 1-3 bytes left to copy, so don't try the
  2657. * 16-bit loop below.
  2658. */
  2659. dp = (png_bytep)dp32;
  2660. sp = (png_const_bytep)sp32;
  2661. do
  2662. *dp++ = *sp++;
  2663. while (--row_width > 0);
  2664. return;
  2665. }
  2666. /* Else do it in 16-bit quantities, but only if the size is
  2667. * not too large.
  2668. */
  2669. else
  2670. {
  2671. png_uint_16p dp16 = (png_uint_16p)dp;
  2672. png_const_uint_16p sp16 = (png_const_uint_16p)sp;
  2673. size_t skip = (bytes_to_jump-bytes_to_copy) /
  2674. sizeof (png_uint_16);
  2675. do
  2676. {
  2677. size_t c = bytes_to_copy;
  2678. do
  2679. {
  2680. *dp16++ = *sp16++;
  2681. c -= sizeof (png_uint_16);
  2682. }
  2683. while (c > 0);
  2684. if (row_width <= bytes_to_jump)
  2685. return;
  2686. dp16 += skip;
  2687. sp16 += skip;
  2688. row_width -= bytes_to_jump;
  2689. }
  2690. while (bytes_to_copy <= row_width);
  2691. /* End of row - 1 byte left, bytes_to_copy > row_width: */
  2692. dp = (png_bytep)dp16;
  2693. sp = (png_const_bytep)sp16;
  2694. do
  2695. *dp++ = *sp++;
  2696. while (--row_width > 0);
  2697. return;
  2698. }
  2699. }
  2700. #endif /* PNG_ALIGN_ code */
  2701. /* The true default - use a png_memcpy: */
  2702. for (;;)
  2703. {
  2704. png_memcpy(dp, sp, bytes_to_copy);
  2705. if (row_width <= bytes_to_jump)
  2706. return;
  2707. sp += bytes_to_jump;
  2708. dp += bytes_to_jump;
  2709. row_width -= bytes_to_jump;
  2710. if (bytes_to_copy > row_width)
  2711. bytes_to_copy = (unsigned int)/*SAFE*/row_width;
  2712. }
  2713. }
  2714. /* NOT REACHED*/
  2715. } /* pixel_depth >= 8 */
  2716. /* Here if pixel_depth < 8 to check 'end_ptr' below. */
  2717. }
  2718. else
  2719. #endif
  2720. /* If here then the switch above wasn't used so just png_memcpy the whole row
  2721. * from the temporary row buffer (notice that this overwrites the end of the
  2722. * destination row if it is a partial byte.)
  2723. */
  2724. png_memcpy(dp, sp, PNG_ROWBYTES(pixel_depth, row_width));
  2725. /* Restore the overwritten bits from the last byte if necessary. */
  2726. if (end_ptr != NULL)
  2727. *end_ptr = (png_byte)((end_byte & end_mask) | (*end_ptr & ~end_mask));
  2728. }
  2729. #ifdef PNG_READ_INTERLACING_SUPPORTED
  2730. void /* PRIVATE */
  2731. png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
  2732. png_uint_32 transformations /* Because these may affect the byte layout */)
  2733. {
  2734. /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  2735. /* Offset to next interlace block */
  2736. static PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
  2737. png_debug(1, "in png_do_read_interlace");
  2738. if (row != NULL && row_info != NULL)
  2739. {
  2740. png_uint_32 final_width;
  2741. final_width = row_info->width * png_pass_inc[pass];
  2742. switch (row_info->pixel_depth)
  2743. {
  2744. case 1:
  2745. {
  2746. png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 3);
  2747. png_bytep dp = row + (png_size_t)((final_width - 1) >> 3);
  2748. int sshift, dshift;
  2749. int s_start, s_end, s_inc;
  2750. int jstop = png_pass_inc[pass];
  2751. png_byte v;
  2752. png_uint_32 i;
  2753. int j;
  2754. #ifdef PNG_READ_PACKSWAP_SUPPORTED
  2755. if (transformations & PNG_PACKSWAP)
  2756. {
  2757. sshift = (int)((row_info->width + 7) & 0x07);
  2758. dshift = (int)((final_width + 7) & 0x07);
  2759. s_start = 7;
  2760. s_end = 0;
  2761. s_inc = -1;
  2762. }
  2763. else
  2764. #endif
  2765. {
  2766. sshift = 7 - (int)((row_info->width + 7) & 0x07);
  2767. dshift = 7 - (int)((final_width + 7) & 0x07);
  2768. s_start = 0;
  2769. s_end = 7;
  2770. s_inc = 1;
  2771. }
  2772. for (i = 0; i < row_info->width; i++)
  2773. {
  2774. v = (png_byte)((*sp >> sshift) & 0x01);
  2775. for (j = 0; j < jstop; j++)
  2776. {
  2777. *dp &= (png_byte)((0x7f7f >> (7 - dshift)) & 0xff);
  2778. *dp |= (png_byte)(v << dshift);
  2779. if (dshift == s_end)
  2780. {
  2781. dshift = s_start;
  2782. dp--;
  2783. }
  2784. else
  2785. dshift += s_inc;
  2786. }
  2787. if (sshift == s_end)
  2788. {
  2789. sshift = s_start;
  2790. sp--;
  2791. }
  2792. else
  2793. sshift += s_inc;
  2794. }
  2795. break;
  2796. }
  2797. case 2:
  2798. {
  2799. png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2);
  2800. png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2);
  2801. int sshift, dshift;
  2802. int s_start, s_end, s_inc;
  2803. int jstop = png_pass_inc[pass];
  2804. png_uint_32 i;
  2805. #ifdef PNG_READ_PACKSWAP_SUPPORTED
  2806. if (transformations & PNG_PACKSWAP)
  2807. {
  2808. sshift = (int)(((row_info->width + 3) & 0x03) << 1);
  2809. dshift = (int)(((final_width + 3) & 0x03) << 1);
  2810. s_start = 6;
  2811. s_end = 0;
  2812. s_inc = -2;
  2813. }
  2814. else
  2815. #endif
  2816. {
  2817. sshift = (int)((3 - ((row_info->width + 3) & 0x03)) << 1);
  2818. dshift = (int)((3 - ((final_width + 3) & 0x03)) << 1);
  2819. s_start = 0;
  2820. s_end = 6;
  2821. s_inc = 2;
  2822. }
  2823. for (i = 0; i < row_info->width; i++)
  2824. {
  2825. png_byte v;
  2826. int j;
  2827. v = (png_byte)((*sp >> sshift) & 0x03);
  2828. for (j = 0; j < jstop; j++)
  2829. {
  2830. *dp &= (png_byte)((0x3f3f >> (6 - dshift)) & 0xff);
  2831. *dp |= (png_byte)(v << dshift);
  2832. if (dshift == s_end)
  2833. {
  2834. dshift = s_start;
  2835. dp--;
  2836. }
  2837. else
  2838. dshift += s_inc;
  2839. }
  2840. if (sshift == s_end)
  2841. {
  2842. sshift = s_start;
  2843. sp--;
  2844. }
  2845. else
  2846. sshift += s_inc;
  2847. }
  2848. break;
  2849. }
  2850. case 4:
  2851. {
  2852. png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 1);
  2853. png_bytep dp = row + (png_size_t)((final_width - 1) >> 1);
  2854. int sshift, dshift;
  2855. int s_start, s_end, s_inc;
  2856. png_uint_32 i;
  2857. int jstop = png_pass_inc[pass];
  2858. #ifdef PNG_READ_PACKSWAP_SUPPORTED
  2859. if (transformations & PNG_PACKSWAP)
  2860. {
  2861. sshift = (int)(((row_info->width + 1) & 0x01) << 2);
  2862. dshift = (int)(((final_width + 1) & 0x01) << 2);
  2863. s_start = 4;
  2864. s_end = 0;
  2865. s_inc = -4;
  2866. }
  2867. else
  2868. #endif
  2869. {
  2870. sshift = (int)((1 - ((row_info->width + 1) & 0x01)) << 2);
  2871. dshift = (int)((1 - ((final_width + 1) & 0x01)) << 2);
  2872. s_start = 0;
  2873. s_end = 4;
  2874. s_inc = 4;
  2875. }
  2876. for (i = 0; i < row_info->width; i++)
  2877. {
  2878. png_byte v = (png_byte)((*sp >> sshift) & 0x0f);
  2879. int j;
  2880. for (j = 0; j < jstop; j++)
  2881. {
  2882. *dp &= (png_byte)((0xf0f >> (4 - dshift)) & 0xff);
  2883. *dp |= (png_byte)(v << dshift);
  2884. if (dshift == s_end)
  2885. {
  2886. dshift = s_start;
  2887. dp--;
  2888. }
  2889. else
  2890. dshift += s_inc;
  2891. }
  2892. if (sshift == s_end)
  2893. {
  2894. sshift = s_start;
  2895. sp--;
  2896. }
  2897. else
  2898. sshift += s_inc;
  2899. }
  2900. break;
  2901. }
  2902. default:
  2903. {
  2904. png_size_t pixel_bytes = (row_info->pixel_depth >> 3);
  2905. png_bytep sp = row + (png_size_t)(row_info->width - 1)
  2906. * pixel_bytes;
  2907. png_bytep dp = row + (png_size_t)(final_width - 1) * pixel_bytes;
  2908. int jstop = png_pass_inc[pass];
  2909. png_uint_32 i;
  2910. for (i = 0; i < row_info->width; i++)
  2911. {
  2912. png_byte v[8]; /* SAFE; pixel_depth does not exceed 64 */
  2913. int j;
  2914. png_memcpy(v, sp, pixel_bytes);
  2915. for (j = 0; j < jstop; j++)
  2916. {
  2917. png_memcpy(dp, v, pixel_bytes);
  2918. dp -= pixel_bytes;
  2919. }
  2920. sp -= pixel_bytes;
  2921. }
  2922. break;
  2923. }
  2924. }
  2925. row_info->width = final_width;
  2926. row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width);
  2927. }
  2928. #ifndef PNG_READ_PACKSWAP_SUPPORTED
  2929. PNG_UNUSED(transformations) /* Silence compiler warning */
  2930. #endif
  2931. }
  2932. #endif /* PNG_READ_INTERLACING_SUPPORTED */
  2933. static void
  2934. png_read_filter_row_sub(png_row_infop row_info, png_bytep row,
  2935. png_const_bytep prev_row)
  2936. {
  2937. png_size_t i;
  2938. png_size_t istop = row_info->rowbytes;
  2939. unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
  2940. png_bytep rp = row + bpp;
  2941. PNG_UNUSED(prev_row)
  2942. for (i = bpp; i < istop; i++)
  2943. {
  2944. *rp = (png_byte)(((int)(*rp) + (int)(*(rp-bpp))) & 0xff);
  2945. rp++;
  2946. }
  2947. }
  2948. static void
  2949. png_read_filter_row_up(png_row_infop row_info, png_bytep row,
  2950. png_const_bytep prev_row)
  2951. {
  2952. png_size_t i;
  2953. png_size_t istop = row_info->rowbytes;
  2954. png_bytep rp = row;
  2955. png_const_bytep pp = prev_row;
  2956. for (i = 0; i < istop; i++)
  2957. {
  2958. *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
  2959. rp++;
  2960. }
  2961. }
  2962. static void
  2963. png_read_filter_row_avg(png_row_infop row_info, png_bytep row,
  2964. png_const_bytep prev_row)
  2965. {
  2966. png_size_t i;
  2967. png_bytep rp = row;
  2968. png_const_bytep pp = prev_row;
  2969. unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
  2970. png_size_t istop = row_info->rowbytes - bpp;
  2971. for (i = 0; i < bpp; i++)
  2972. {
  2973. *rp = (png_byte)(((int)(*rp) +
  2974. ((int)(*pp++) / 2 )) & 0xff);
  2975. rp++;
  2976. }
  2977. for (i = 0; i < istop; i++)
  2978. {
  2979. *rp = (png_byte)(((int)(*rp) +
  2980. (int)(*pp++ + *(rp-bpp)) / 2 ) & 0xff);
  2981. rp++;
  2982. }
  2983. }
  2984. static void
  2985. png_read_filter_row_paeth_1byte_pixel(png_row_infop row_info, png_bytep row,
  2986. png_const_bytep prev_row)
  2987. {
  2988. png_bytep rp_end = row + row_info->rowbytes;
  2989. int a, c;
  2990. /* First pixel/byte */
  2991. c = *prev_row++;
  2992. a = *row + c;
  2993. *row++ = (png_byte)a;
  2994. /* Remainder */
  2995. while (row < rp_end)
  2996. {
  2997. int b, pa, pb, pc, p;
  2998. a &= 0xff; /* From previous iteration or start */
  2999. b = *prev_row++;
  3000. p = b - c;
  3001. pc = a - c;
  3002. # ifdef PNG_USE_ABS
  3003. pa = abs(p);
  3004. pb = abs(pc);
  3005. pc = abs(p + pc);
  3006. # else
  3007. pa = p < 0 ? -p : p;
  3008. pb = pc < 0 ? -pc : pc;
  3009. pc = (p + pc) < 0 ? -(p + pc) : p + pc;
  3010. # endif
  3011. /* Find the best predictor, the least of pa, pb, pc favoring the earlier
  3012. * ones in the case of a tie.
  3013. */
  3014. if (pb < pa) pa = pb, a = b;
  3015. if (pc < pa) a = c;
  3016. /* Calculate the current pixel in a, and move the previous row pixel to c
  3017. * for the next time round the loop
  3018. */
  3019. c = b;
  3020. a += *row;
  3021. *row++ = (png_byte)a;
  3022. }
  3023. }
  3024. static void
  3025. png_read_filter_row_paeth_multibyte_pixel(png_row_infop row_info, png_bytep row,
  3026. png_const_bytep prev_row)
  3027. {
  3028. int bpp = (row_info->pixel_depth + 7) >> 3;
  3029. png_bytep rp_end = row + bpp;
  3030. /* Process the first pixel in the row completely (this is the same as 'up'
  3031. * because there is only one candidate predictor for the first row).
  3032. */
  3033. while (row < rp_end)
  3034. {
  3035. int a = *row + *prev_row++;
  3036. *row++ = (png_byte)a;
  3037. }
  3038. /* Remainder */
  3039. rp_end += row_info->rowbytes - bpp;
  3040. while (row < rp_end)
  3041. {
  3042. int a, b, c, pa, pb, pc, p;
  3043. c = *(prev_row - bpp);
  3044. a = *(row - bpp);
  3045. b = *prev_row++;
  3046. p = b - c;
  3047. pc = a - c;
  3048. # ifdef PNG_USE_ABS
  3049. pa = abs(p);
  3050. pb = abs(pc);
  3051. pc = abs(p + pc);
  3052. # else
  3053. pa = p < 0 ? -p : p;
  3054. pb = pc < 0 ? -pc : pc;
  3055. pc = (p + pc) < 0 ? -(p + pc) : p + pc;
  3056. # endif
  3057. if (pb < pa) pa = pb, a = b;
  3058. if (pc < pa) a = c;
  3059. c = b;
  3060. a += *row;
  3061. *row++ = (png_byte)a;
  3062. }
  3063. }
  3064. static void
  3065. png_init_filter_functions(png_structp pp)
  3066. {
  3067. unsigned int bpp = (pp->pixel_depth + 7) >> 3;
  3068. pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub;
  3069. pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up;
  3070. pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg;
  3071. if (bpp == 1)
  3072. pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
  3073. png_read_filter_row_paeth_1byte_pixel;
  3074. else
  3075. pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
  3076. png_read_filter_row_paeth_multibyte_pixel;
  3077. #ifdef PNG_FILTER_OPTIMIZATIONS
  3078. /* To use this define PNG_FILTER_OPTIMIZATIONS as the name of a function to
  3079. * call to install hardware optimizations for the above functions; simply
  3080. * replace whatever elements of the pp->read_filter[] array with a hardware
  3081. * specific (or, for that matter, generic) optimization.
  3082. *
  3083. * To see an example of this examine what configure.ac does when
  3084. * --enable-arm-neon is specified on the command line.
  3085. */
  3086. PNG_FILTER_OPTIMIZATIONS(pp, bpp);
  3087. #endif
  3088. }
  3089. void /* PRIVATE */
  3090. png_read_filter_row(png_structp pp, png_row_infop row_info, png_bytep row,
  3091. png_const_bytep prev_row, int filter)
  3092. {
  3093. if (filter > PNG_FILTER_VALUE_NONE && filter < PNG_FILTER_VALUE_LAST)
  3094. {
  3095. if (pp->read_filter[0] == NULL)
  3096. png_init_filter_functions(pp);
  3097. pp->read_filter[filter-1](row_info, row, prev_row);
  3098. }
  3099. }
  3100. #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  3101. void /* PRIVATE */
  3102. png_read_finish_row(png_structp png_ptr)
  3103. {
  3104. #ifdef PNG_READ_INTERLACING_SUPPORTED
  3105. /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  3106. /* Start of interlace block */
  3107. static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
  3108. /* Offset to next interlace block */
  3109. static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
  3110. /* Start of interlace block in the y direction */
  3111. static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
  3112. /* Offset to next interlace block in the y direction */
  3113. static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
  3114. #endif /* PNG_READ_INTERLACING_SUPPORTED */
  3115. png_debug(1, "in png_read_finish_row");
  3116. png_ptr->row_number++;
  3117. if (png_ptr->row_number < png_ptr->num_rows)
  3118. return;
  3119. #ifdef PNG_READ_INTERLACING_SUPPORTED
  3120. if (png_ptr->interlaced)
  3121. {
  3122. png_ptr->row_number = 0;
  3123. /* TO DO: don't do this if prev_row isn't needed (requires
  3124. * read-ahead of the next row's filter byte.
  3125. */
  3126. png_memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
  3127. do
  3128. {
  3129. png_ptr->pass++;
  3130. if (png_ptr->pass >= 7)
  3131. break;
  3132. png_ptr->iwidth = (png_ptr->width +
  3133. png_pass_inc[png_ptr->pass] - 1 -
  3134. png_pass_start[png_ptr->pass]) /
  3135. png_pass_inc[png_ptr->pass];
  3136. if (!(png_ptr->transformations & PNG_INTERLACE))
  3137. {
  3138. png_ptr->num_rows = (png_ptr->height +
  3139. png_pass_yinc[png_ptr->pass] - 1 -
  3140. png_pass_ystart[png_ptr->pass]) /
  3141. png_pass_yinc[png_ptr->pass];
  3142. }
  3143. else /* if (png_ptr->transformations & PNG_INTERLACE) */
  3144. break; /* libpng deinterlacing sees every row */
  3145. } while (png_ptr->num_rows == 0 || png_ptr->iwidth == 0);
  3146. if (png_ptr->pass < 7)
  3147. return;
  3148. }
  3149. #endif /* PNG_READ_INTERLACING_SUPPORTED */
  3150. if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
  3151. {
  3152. char extra;
  3153. int ret;
  3154. png_ptr->zstream.next_out = (Byte *)&extra;
  3155. png_ptr->zstream.avail_out = (uInt)1;
  3156. for (;;)
  3157. {
  3158. if (!(png_ptr->zstream.avail_in))
  3159. {
  3160. while (!png_ptr->idat_size)
  3161. {
  3162. png_crc_finish(png_ptr, 0);
  3163. png_ptr->idat_size = png_read_chunk_header(png_ptr);
  3164. if (png_ptr->chunk_name != png_IDAT)
  3165. png_error(png_ptr, "Not enough image data");
  3166. }
  3167. png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
  3168. png_ptr->zstream.next_in = png_ptr->zbuf;
  3169. if (png_ptr->zbuf_size > png_ptr->idat_size)
  3170. png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
  3171. png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zstream.avail_in);
  3172. png_ptr->idat_size -= png_ptr->zstream.avail_in;
  3173. }
  3174. ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
  3175. if (ret == Z_STREAM_END)
  3176. {
  3177. if (!(png_ptr->zstream.avail_out) || png_ptr->zstream.avail_in ||
  3178. png_ptr->idat_size)
  3179. png_warning(png_ptr, "Extra compressed data");
  3180. png_ptr->mode |= PNG_AFTER_IDAT;
  3181. png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
  3182. break;
  3183. }
  3184. if (ret != Z_OK)
  3185. png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
  3186. "Decompression Error");
  3187. if (!(png_ptr->zstream.avail_out))
  3188. {
  3189. png_warning(png_ptr, "Extra compressed data");
  3190. png_ptr->mode |= PNG_AFTER_IDAT;
  3191. png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
  3192. break;
  3193. }
  3194. }
  3195. png_ptr->zstream.avail_out = 0;
  3196. }
  3197. if (png_ptr->idat_size || png_ptr->zstream.avail_in)
  3198. png_warning(png_ptr, "Extra compression data");
  3199. inflateReset(&png_ptr->zstream);
  3200. png_ptr->mode |= PNG_AFTER_IDAT;
  3201. }
  3202. #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
  3203. void /* PRIVATE */
  3204. png_read_start_row(png_structp png_ptr)
  3205. {
  3206. #ifdef PNG_READ_INTERLACING_SUPPORTED
  3207. /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  3208. /* Start of interlace block */
  3209. static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
  3210. /* Offset to next interlace block */
  3211. static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
  3212. /* Start of interlace block in the y direction */
  3213. static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
  3214. /* Offset to next interlace block in the y direction */
  3215. static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
  3216. #endif
  3217. int max_pixel_depth;
  3218. png_size_t row_bytes;
  3219. png_debug(1, "in png_read_start_row");
  3220. png_ptr->zstream.avail_in = 0;
  3221. #ifdef PNG_READ_TRANSFORMS_SUPPORTED
  3222. png_init_read_transformations(png_ptr);
  3223. #endif
  3224. #ifdef PNG_READ_INTERLACING_SUPPORTED
  3225. if (png_ptr->interlaced)
  3226. {
  3227. if (!(png_ptr->transformations & PNG_INTERLACE))
  3228. png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
  3229. png_pass_ystart[0]) / png_pass_yinc[0];
  3230. else
  3231. png_ptr->num_rows = png_ptr->height;
  3232. png_ptr->iwidth = (png_ptr->width +
  3233. png_pass_inc[png_ptr->pass] - 1 -
  3234. png_pass_start[png_ptr->pass]) /
  3235. png_pass_inc[png_ptr->pass];
  3236. }
  3237. else
  3238. #endif /* PNG_READ_INTERLACING_SUPPORTED */
  3239. {
  3240. png_ptr->num_rows = png_ptr->height;
  3241. png_ptr->iwidth = png_ptr->width;
  3242. }
  3243. max_pixel_depth = png_ptr->pixel_depth;
  3244. /* WARNING: * png_read_transform_info (pngrtran.c) performs a simpliar set of
  3245. * calculations to calculate the final pixel depth, then
  3246. * png_do_read_transforms actually does the transforms. This means that the
  3247. * code which effectively calculates this value is actually repeated in three
  3248. * separate places. They must all match. Innocent changes to the order of
  3249. * transformations can and will break libpng in a way that causes memory
  3250. * overwrites.
  3251. *
  3252. * TODO: fix this.
  3253. */
  3254. #ifdef PNG_READ_PACK_SUPPORTED
  3255. if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8)
  3256. max_pixel_depth = 8;
  3257. #endif
  3258. #ifdef PNG_READ_EXPAND_SUPPORTED
  3259. if (png_ptr->transformations & PNG_EXPAND)
  3260. {
  3261. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  3262. {
  3263. if (png_ptr->num_trans)
  3264. max_pixel_depth = 32;
  3265. else
  3266. max_pixel_depth = 24;
  3267. }
  3268. else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
  3269. {
  3270. if (max_pixel_depth < 8)
  3271. max_pixel_depth = 8;
  3272. if (png_ptr->num_trans)
  3273. max_pixel_depth *= 2;
  3274. }
  3275. else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
  3276. {
  3277. if (png_ptr->num_trans)
  3278. {
  3279. max_pixel_depth *= 4;
  3280. max_pixel_depth /= 3;
  3281. }
  3282. }
  3283. }
  3284. #endif
  3285. #ifdef PNG_READ_EXPAND_16_SUPPORTED
  3286. if (png_ptr->transformations & PNG_EXPAND_16)
  3287. {
  3288. # ifdef PNG_READ_EXPAND_SUPPORTED
  3289. /* In fact it is an error if it isn't supported, but checking is
  3290. * the safe way.
  3291. */
  3292. if (png_ptr->transformations & PNG_EXPAND)
  3293. {
  3294. if (png_ptr->bit_depth < 16)
  3295. max_pixel_depth *= 2;
  3296. }
  3297. else
  3298. # endif
  3299. png_ptr->transformations &= ~PNG_EXPAND_16;
  3300. }
  3301. #endif
  3302. #ifdef PNG_READ_FILLER_SUPPORTED
  3303. if (png_ptr->transformations & (PNG_FILLER))
  3304. {
  3305. if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
  3306. {
  3307. if (max_pixel_depth <= 8)
  3308. max_pixel_depth = 16;
  3309. else
  3310. max_pixel_depth = 32;
  3311. }
  3312. else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB ||
  3313. png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  3314. {
  3315. if (max_pixel_depth <= 32)
  3316. max_pixel_depth = 32;
  3317. else
  3318. max_pixel_depth = 64;
  3319. }
  3320. }
  3321. #endif
  3322. #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
  3323. if (png_ptr->transformations & PNG_GRAY_TO_RGB)
  3324. {
  3325. if (
  3326. #ifdef PNG_READ_EXPAND_SUPPORTED
  3327. (png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) ||
  3328. #endif
  3329. #ifdef PNG_READ_FILLER_SUPPORTED
  3330. (png_ptr->transformations & (PNG_FILLER)) ||
  3331. #endif
  3332. png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  3333. {
  3334. if (max_pixel_depth <= 16)
  3335. max_pixel_depth = 32;
  3336. else
  3337. max_pixel_depth = 64;
  3338. }
  3339. else
  3340. {
  3341. if (max_pixel_depth <= 8)
  3342. {
  3343. if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  3344. max_pixel_depth = 32;
  3345. else
  3346. max_pixel_depth = 24;
  3347. }
  3348. else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  3349. max_pixel_depth = 64;
  3350. else
  3351. max_pixel_depth = 48;
  3352. }
  3353. }
  3354. #endif
  3355. #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
  3356. defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
  3357. if (png_ptr->transformations & PNG_USER_TRANSFORM)
  3358. {
  3359. int user_pixel_depth = png_ptr->user_transform_depth *
  3360. png_ptr->user_transform_channels;
  3361. if (user_pixel_depth > max_pixel_depth)
  3362. max_pixel_depth = user_pixel_depth;
  3363. }
  3364. #endif
  3365. /* This value is stored in png_struct and double checked in the row read
  3366. * code.
  3367. */
  3368. png_ptr->maximum_pixel_depth = (png_byte)max_pixel_depth;
  3369. png_ptr->transformed_pixel_depth = 0; /* calculated on demand */
  3370. /* Align the width on the next larger 8 pixels. Mainly used
  3371. * for interlacing
  3372. */
  3373. row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7));
  3374. /* Calculate the maximum bytes needed, adding a byte and a pixel
  3375. * for safety's sake
  3376. */
  3377. row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) +
  3378. 1 + ((max_pixel_depth + 7) >> 3);
  3379. #ifdef PNG_MAX_MALLOC_64K
  3380. if (row_bytes > (png_uint_32)65536L)
  3381. png_error(png_ptr, "This image requires a row greater than 64KB");
  3382. #endif
  3383. if (row_bytes + 48 > png_ptr->old_big_row_buf_size)
  3384. {
  3385. png_free(png_ptr, png_ptr->big_row_buf);
  3386. png_free(png_ptr, png_ptr->big_prev_row);
  3387. if (png_ptr->interlaced)
  3388. png_ptr->big_row_buf = (png_bytep)png_calloc(png_ptr,
  3389. row_bytes + 48);
  3390. else
  3391. png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
  3392. png_ptr->big_prev_row = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
  3393. #ifdef PNG_ALIGNED_MEMORY_SUPPORTED
  3394. /* Use 16-byte aligned memory for row_buf with at least 16 bytes
  3395. * of padding before and after row_buf; treat prev_row similarly.
  3396. * NOTE: the alignment is to the start of the pixels, one beyond the start
  3397. * of the buffer, because of the filter byte. Prior to libpng 1.5.6 this
  3398. * was incorrect; the filter byte was aligned, which had the exact
  3399. * opposite effect of that intended.
  3400. */
  3401. {
  3402. png_bytep temp = png_ptr->big_row_buf + 32;
  3403. int extra = (int)((temp - (png_bytep)0) & 0x0f);
  3404. png_ptr->row_buf = temp - extra - 1/*filter byte*/;
  3405. temp = png_ptr->big_prev_row + 32;
  3406. extra = (int)((temp - (png_bytep)0) & 0x0f);
  3407. png_ptr->prev_row = temp - extra - 1/*filter byte*/;
  3408. }
  3409. #else
  3410. /* Use 31 bytes of padding before and 17 bytes after row_buf. */
  3411. png_ptr->row_buf = png_ptr->big_row_buf + 31;
  3412. png_ptr->prev_row = png_ptr->big_prev_row + 31;
  3413. #endif
  3414. png_ptr->old_big_row_buf_size = row_bytes + 48;
  3415. }
  3416. #ifdef PNG_MAX_MALLOC_64K
  3417. if (png_ptr->rowbytes > 65535)
  3418. png_error(png_ptr, "This image requires a row greater than 64KB");
  3419. #endif
  3420. if (png_ptr->rowbytes > (PNG_SIZE_MAX - 1))
  3421. png_error(png_ptr, "Row has too many bytes to allocate in memory");
  3422. png_memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
  3423. png_debug1(3, "width = %u,", png_ptr->width);
  3424. png_debug1(3, "height = %u,", png_ptr->height);
  3425. png_debug1(3, "iwidth = %u,", png_ptr->iwidth);
  3426. png_debug1(3, "num_rows = %u,", png_ptr->num_rows);
  3427. png_debug1(3, "rowbytes = %lu,", (unsigned long)png_ptr->rowbytes);
  3428. png_debug1(3, "irowbytes = %lu",
  3429. (unsigned long)PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1);
  3430. png_ptr->flags |= PNG_FLAG_ROW_INIT;
  3431. }
  3432. #endif /* PNG_READ_SUPPORTED */