pngrutil.c 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /* pngrutil.c - utilities to read a PNG file
  10. *
  11. * libpng version 1.2.8 - December 3, 2004
  12. * For conditions of distribution and use, see copyright notice in png.h
  13. * Copyright (c) 1998-2004 Glenn Randers-Pehrson
  14. * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  15. * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
  16. *
  17. * This file contains routines that are only called from within
  18. * libpng itself during the course of reading an image.
  19. */
  20. #define PNG_INTERNAL
  21. #include "png.h"
  22. #if defined(_WIN32_WCE)
  23. /* strtod() function is not supported on WindowsCE */
  24. # ifdef PNG_FLOATING_POINT_SUPPORTED
  25. __inline double strtod(const char *nptr, char **endptr)
  26. {
  27. double result = 0;
  28. int len;
  29. wchar_t *str, *end;
  30. len = MultiByteToWideChar(CP_ACP, 0, nptr, -1, NULL, 0);
  31. str = (wchar_t *)malloc(len * sizeof(wchar_t));
  32. if ( NULL != str )
  33. {
  34. MultiByteToWideChar(CP_ACP, 0, nptr, -1, str, len);
  35. result = wcstod(str, &end);
  36. len = WideCharToMultiByte(CP_ACP, 0, end, -1, NULL, 0, NULL, NULL);
  37. *endptr = (char *)nptr + (png_strlen(nptr) - len + 1);
  38. free(str);
  39. }
  40. return result;
  41. }
  42. # endif
  43. #endif
  44. png_uint_32 /* PRIVATE */
  45. png_get_uint_31(png_structp png_ptr, png_bytep buf)
  46. {
  47. png_uint_32 i = png_get_uint_32(buf);
  48. if (i > PNG_UINT_31_MAX)
  49. png_error(png_ptr, "PNG unsigned integer out of range.\n");
  50. return (i);
  51. }
  52. #ifndef PNG_READ_BIG_ENDIAN_SUPPORTED
  53. /* Grab an unsigned 32-bit integer from a buffer in big-endian format. */
  54. png_uint_32 /* PRIVATE */
  55. png_get_uint_32(png_bytep buf)
  56. {
  57. png_uint_32 i = ((png_uint_32)(*buf) << 24) +
  58. ((png_uint_32)(*(buf + 1)) << 16) +
  59. ((png_uint_32)(*(buf + 2)) << 8) +
  60. (png_uint_32)(*(buf + 3));
  61. return (i);
  62. }
  63. #if defined(PNG_READ_pCAL_SUPPORTED) || defined(PNG_READ_oFFs_SUPPORTED)
  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 it is
  66. * assumed that the machine format for signed integers is the same. */
  67. png_int_32 /* PRIVATE */
  68. png_get_int_32(png_bytep buf)
  69. {
  70. png_int_32 i = ((png_int_32)(*buf) << 24) +
  71. ((png_int_32)(*(buf + 1)) << 16) +
  72. ((png_int_32)(*(buf + 2)) << 8) +
  73. (png_int_32)(*(buf + 3));
  74. return (i);
  75. }
  76. #endif /* PNG_READ_pCAL_SUPPORTED */
  77. /* Grab an unsigned 16-bit integer from a buffer in big-endian format. */
  78. png_uint_16 /* PRIVATE */
  79. png_get_uint_16(png_bytep buf)
  80. {
  81. png_uint_16 i = (png_uint_16)(((png_uint_16)(*buf) << 8) +
  82. (png_uint_16)(*(buf + 1)));
  83. return (i);
  84. }
  85. #endif /* PNG_READ_BIG_ENDIAN_SUPPORTED */
  86. /* Read data, and (optionally) run it through the CRC. */
  87. void /* PRIVATE */
  88. png_crc_read(png_structp png_ptr, png_bytep buf, png_size_t length)
  89. {
  90. png_read_data(png_ptr, buf, length);
  91. png_calculate_crc(png_ptr, buf, length);
  92. }
  93. /* Optionally skip data and then check the CRC. Depending on whether we
  94. are reading a ancillary or critical chunk, and how the program has set
  95. things up, we may calculate the CRC on the data and print a message.
  96. Returns '1' if there was a CRC error, '0' otherwise. */
  97. int /* PRIVATE */
  98. png_crc_finish(png_structp png_ptr, png_uint_32 skip)
  99. {
  100. png_size_t i;
  101. png_size_t istop = png_ptr->zbuf_size;
  102. for (i = (png_size_t)skip; i > istop; i -= istop)
  103. {
  104. png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
  105. }
  106. if (i)
  107. {
  108. png_crc_read(png_ptr, png_ptr->zbuf, i);
  109. }
  110. if (png_crc_error(png_ptr))
  111. {
  112. if (((png_ptr->chunk_name[0] & 0x20) && /* Ancillary */
  113. !(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)) ||
  114. (!(png_ptr->chunk_name[0] & 0x20) && /* Critical */
  115. (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE)))
  116. {
  117. png_chunk_warning(png_ptr, "CRC error");
  118. }
  119. else
  120. {
  121. png_chunk_error(png_ptr, "CRC error");
  122. }
  123. return (1);
  124. }
  125. return (0);
  126. }
  127. /* Compare the CRC stored in the PNG file with that calculated by libpng from
  128. the data it has read thus far. */
  129. int /* PRIVATE */
  130. png_crc_error(png_structp png_ptr)
  131. {
  132. png_byte crc_bytes[4];
  133. png_uint_32 crc;
  134. int need_crc = 1;
  135. if (png_ptr->chunk_name[0] & 0x20) /* ancillary */
  136. {
  137. if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
  138. (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
  139. need_crc = 0;
  140. }
  141. else /* critical */
  142. {
  143. if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
  144. need_crc = 0;
  145. }
  146. png_read_data(png_ptr, crc_bytes, 4);
  147. if (need_crc)
  148. {
  149. crc = png_get_uint_32(crc_bytes);
  150. return ((int)(crc != png_ptr->crc));
  151. }
  152. else
  153. return (0);
  154. }
  155. #if defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_READ_iTXt_SUPPORTED) || \
  156. defined(PNG_READ_iCCP_SUPPORTED)
  157. /*
  158. * Decompress trailing data in a chunk. The assumption is that chunkdata
  159. * points at an allocated area holding the contents of a chunk with a
  160. * trailing compressed part. What we get back is an allocated area
  161. * holding the original prefix part and an uncompressed version of the
  162. * trailing part (the malloc area passed in is freed).
  163. */
  164. png_charp /* PRIVATE */
  165. png_decompress_chunk(png_structp png_ptr, int comp_type,
  166. png_charp chunkdata, png_size_t chunklength,
  167. png_size_t prefix_size, png_size_t *newlength)
  168. {
  169. static char msg[] = "Error decoding compressed text";
  170. png_charp text;
  171. png_size_t text_size;
  172. if (comp_type == PNG_COMPRESSION_TYPE_BASE)
  173. {
  174. int ret = Z_OK;
  175. png_ptr->zstream.next_in = (png_bytep)(chunkdata + prefix_size);
  176. png_ptr->zstream.avail_in = (uInt)(chunklength - prefix_size);
  177. png_ptr->zstream.next_out = png_ptr->zbuf;
  178. png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  179. text_size = 0;
  180. text = NULL;
  181. while (png_ptr->zstream.avail_in)
  182. {
  183. ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
  184. if (ret != Z_OK && ret != Z_STREAM_END)
  185. {
  186. if (png_ptr->zstream.msg != NULL)
  187. png_warning(png_ptr, png_ptr->zstream.msg);
  188. else
  189. png_warning(png_ptr, msg);
  190. inflateReset(&png_ptr->zstream);
  191. png_ptr->zstream.avail_in = 0;
  192. if (text == NULL)
  193. {
  194. text_size = prefix_size + png_sizeof(msg) + 1;
  195. text = (png_charp)png_malloc_warn(png_ptr, text_size);
  196. if (text == NULL)
  197. {
  198. png_free(png_ptr,chunkdata);
  199. png_error(png_ptr,"Not enough memory to decompress chunk");
  200. }
  201. png_memcpy(text, chunkdata, prefix_size);
  202. }
  203. text[text_size - 1] = 0x00;
  204. /* Copy what we can of the error message into the text chunk */
  205. text_size = (png_size_t)(chunklength - (text - chunkdata) - 1);
  206. text_size = png_sizeof(msg) > text_size ? text_size :
  207. png_sizeof(msg);
  208. png_memcpy(text + prefix_size, msg, text_size + 1);
  209. break;
  210. }
  211. if (!png_ptr->zstream.avail_out || ret == Z_STREAM_END)
  212. {
  213. if (text == NULL)
  214. {
  215. text_size = prefix_size +
  216. png_ptr->zbuf_size - png_ptr->zstream.avail_out;
  217. text = (png_charp)png_malloc_warn(png_ptr, text_size + 1);
  218. if (text == NULL)
  219. {
  220. png_free(png_ptr,chunkdata);
  221. png_error(png_ptr,"Not enough memory to decompress chunk.");
  222. }
  223. png_memcpy(text + prefix_size, png_ptr->zbuf,
  224. text_size - prefix_size);
  225. png_memcpy(text, chunkdata, prefix_size);
  226. *(text + text_size) = 0x00;
  227. }
  228. else
  229. {
  230. png_charp tmp;
  231. tmp = text;
  232. text = (png_charp)png_malloc_warn(png_ptr,
  233. (png_uint_32)(text_size +
  234. png_ptr->zbuf_size - png_ptr->zstream.avail_out + 1));
  235. if (text == NULL)
  236. {
  237. png_free(png_ptr, tmp);
  238. png_free(png_ptr, chunkdata);
  239. png_error(png_ptr,"Not enough memory to decompress chunk..");
  240. }
  241. png_memcpy(text, tmp, text_size);
  242. png_free(png_ptr, tmp);
  243. png_memcpy(text + text_size, png_ptr->zbuf,
  244. (png_ptr->zbuf_size - png_ptr->zstream.avail_out));
  245. text_size += png_ptr->zbuf_size - png_ptr->zstream.avail_out;
  246. *(text + text_size) = 0x00;
  247. }
  248. if (ret == Z_STREAM_END)
  249. break;
  250. else
  251. {
  252. png_ptr->zstream.next_out = png_ptr->zbuf;
  253. png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  254. }
  255. }
  256. }
  257. if (ret != Z_STREAM_END)
  258. {
  259. #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
  260. char umsg[50];
  261. if (ret == Z_BUF_ERROR)
  262. sprintf(umsg,"Buffer error in compressed datastream in %s chunk",
  263. png_ptr->chunk_name);
  264. else if (ret == Z_DATA_ERROR)
  265. sprintf(umsg,"Data error in compressed datastream in %s chunk",
  266. png_ptr->chunk_name);
  267. else
  268. sprintf(umsg,"Incomplete compressed datastream in %s chunk",
  269. png_ptr->chunk_name);
  270. png_warning(png_ptr, umsg);
  271. #else
  272. png_warning(png_ptr,
  273. "Incomplete compressed datastream in chunk other than IDAT");
  274. #endif
  275. text_size=prefix_size;
  276. if (text == NULL)
  277. {
  278. text = (png_charp)png_malloc_warn(png_ptr, text_size+1);
  279. if (text == NULL)
  280. {
  281. png_free(png_ptr, chunkdata);
  282. png_error(png_ptr,"Not enough memory for text.");
  283. }
  284. png_memcpy(text, chunkdata, prefix_size);
  285. }
  286. *(text + text_size) = 0x00;
  287. }
  288. inflateReset(&png_ptr->zstream);
  289. png_ptr->zstream.avail_in = 0;
  290. png_free(png_ptr, chunkdata);
  291. chunkdata = text;
  292. *newlength=text_size;
  293. }
  294. else /* if (comp_type != PNG_COMPRESSION_TYPE_BASE) */
  295. {
  296. #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
  297. char umsg[50];
  298. sprintf(umsg, "Unknown zTXt compression type %d", comp_type);
  299. png_warning(png_ptr, umsg);
  300. #else
  301. png_warning(png_ptr, "Unknown zTXt compression type");
  302. #endif
  303. *(chunkdata + prefix_size) = 0x00;
  304. *newlength=prefix_size;
  305. }
  306. return chunkdata;
  307. }
  308. #endif
  309. /* read and check the IDHR chunk */
  310. void /* PRIVATE */
  311. png_handle_IHDR(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  312. {
  313. png_byte buf[13];
  314. png_uint_32 width, height;
  315. int bit_depth, color_type, compression_type, filter_type;
  316. int interlace_type;
  317. png_debug(1, "in png_handle_IHDR\n");
  318. if (png_ptr->mode & PNG_HAVE_IHDR)
  319. png_error(png_ptr, "Out of place IHDR");
  320. /* check the length */
  321. if (length != 13)
  322. png_error(png_ptr, "Invalid IHDR chunk");
  323. png_ptr->mode |= PNG_HAVE_IHDR;
  324. png_crc_read(png_ptr, buf, 13);
  325. png_crc_finish(png_ptr, 0);
  326. width = png_get_uint_31(png_ptr, buf);
  327. height = png_get_uint_31(png_ptr, buf + 4);
  328. bit_depth = buf[8];
  329. color_type = buf[9];
  330. compression_type = buf[10];
  331. filter_type = buf[11];
  332. interlace_type = buf[12];
  333. /* set internal variables */
  334. png_ptr->width = width;
  335. png_ptr->height = height;
  336. png_ptr->bit_depth = (png_byte)bit_depth;
  337. png_ptr->interlaced = (png_byte)interlace_type;
  338. png_ptr->color_type = (png_byte)color_type;
  339. #if defined(PNG_MNG_FEATURES_SUPPORTED)
  340. png_ptr->filter_type = (png_byte)filter_type;
  341. #endif
  342. png_ptr->compression_type = (png_byte)compression_type;
  343. /* find number of channels */
  344. switch (png_ptr->color_type)
  345. {
  346. case PNG_COLOR_TYPE_GRAY:
  347. case PNG_COLOR_TYPE_PALETTE:
  348. png_ptr->channels = 1;
  349. break;
  350. case PNG_COLOR_TYPE_RGB:
  351. png_ptr->channels = 3;
  352. break;
  353. case PNG_COLOR_TYPE_GRAY_ALPHA:
  354. png_ptr->channels = 2;
  355. break;
  356. case PNG_COLOR_TYPE_RGB_ALPHA:
  357. png_ptr->channels = 4;
  358. break;
  359. }
  360. /* set up other useful info */
  361. png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth *
  362. png_ptr->channels);
  363. png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth,png_ptr->width);
  364. png_debug1(3,"bit_depth = %d\n", png_ptr->bit_depth);
  365. png_debug1(3,"channels = %d\n", png_ptr->channels);
  366. png_debug1(3,"rowbytes = %lu\n", png_ptr->rowbytes);
  367. png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
  368. color_type, interlace_type, compression_type, filter_type);
  369. }
  370. /* read and check the palette */
  371. void /* PRIVATE */
  372. png_handle_PLTE(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  373. {
  374. png_color palette[PNG_MAX_PALETTE_LENGTH];
  375. int num, i;
  376. #ifndef PNG_NO_POINTER_INDEXING
  377. png_colorp pal_ptr;
  378. #endif
  379. png_debug(1, "in png_handle_PLTE\n");
  380. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  381. png_error(png_ptr, "Missing IHDR before PLTE");
  382. else if (png_ptr->mode & PNG_HAVE_IDAT)
  383. {
  384. png_warning(png_ptr, "Invalid PLTE after IDAT");
  385. png_crc_finish(png_ptr, length);
  386. return;
  387. }
  388. else if (png_ptr->mode & PNG_HAVE_PLTE)
  389. png_error(png_ptr, "Duplicate PLTE chunk");
  390. png_ptr->mode |= PNG_HAVE_PLTE;
  391. if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
  392. {
  393. png_warning(png_ptr,
  394. "Ignoring PLTE chunk in grayscale PNG");
  395. png_crc_finish(png_ptr, length);
  396. return;
  397. }
  398. #if !defined(PNG_READ_OPT_PLTE_SUPPORTED)
  399. if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
  400. {
  401. png_crc_finish(png_ptr, length);
  402. return;
  403. }
  404. #endif
  405. if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3)
  406. {
  407. if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
  408. {
  409. png_warning(png_ptr, "Invalid palette chunk");
  410. png_crc_finish(png_ptr, length);
  411. return;
  412. }
  413. else
  414. {
  415. png_error(png_ptr, "Invalid palette chunk");
  416. }
  417. }
  418. num = (int)length / 3;
  419. #ifndef PNG_NO_POINTER_INDEXING
  420. for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++)
  421. {
  422. png_byte buf[3];
  423. png_crc_read(png_ptr, buf, 3);
  424. pal_ptr->red = buf[0];
  425. pal_ptr->green = buf[1];
  426. pal_ptr->blue = buf[2];
  427. }
  428. #else
  429. for (i = 0; i < num; i++)
  430. {
  431. png_byte buf[3];
  432. png_crc_read(png_ptr, buf, 3);
  433. /* don't depend upon png_color being any order */
  434. palette[i].red = buf[0];
  435. palette[i].green = buf[1];
  436. palette[i].blue = buf[2];
  437. }
  438. #endif
  439. /* If we actually NEED the PLTE chunk (ie for a paletted image), we do
  440. whatever the normal CRC configuration tells us. However, if we
  441. have an RGB image, the PLTE can be considered ancillary, so
  442. we will act as though it is. */
  443. #if !defined(PNG_READ_OPT_PLTE_SUPPORTED)
  444. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  445. #endif
  446. {
  447. png_crc_finish(png_ptr, 0);
  448. }
  449. #if !defined(PNG_READ_OPT_PLTE_SUPPORTED)
  450. else if (png_crc_error(png_ptr)) /* Only if we have a CRC error */
  451. {
  452. /* If we don't want to use the data from an ancillary chunk,
  453. we have two options: an error abort, or a warning and we
  454. ignore the data in this chunk (which should be OK, since
  455. it's considered ancillary for a RGB or RGBA image). */
  456. if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE))
  457. {
  458. if (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)
  459. {
  460. png_chunk_error(png_ptr, "CRC error");
  461. }
  462. else
  463. {
  464. png_chunk_warning(png_ptr, "CRC error");
  465. return;
  466. }
  467. }
  468. /* Otherwise, we (optionally) emit a warning and use the chunk. */
  469. else if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN))
  470. {
  471. png_chunk_warning(png_ptr, "CRC error");
  472. }
  473. }
  474. #endif
  475. png_set_PLTE(png_ptr, info_ptr, palette, num);
  476. #if defined(PNG_READ_tRNS_SUPPORTED)
  477. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  478. {
  479. if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
  480. {
  481. if (png_ptr->num_trans > (png_uint_16)num)
  482. {
  483. png_warning(png_ptr, "Truncating incorrect tRNS chunk length");
  484. png_ptr->num_trans = (png_uint_16)num;
  485. }
  486. if (info_ptr->num_trans > (png_uint_16)num)
  487. {
  488. png_warning(png_ptr, "Truncating incorrect info tRNS chunk length");
  489. info_ptr->num_trans = (png_uint_16)num;
  490. }
  491. }
  492. }
  493. #endif
  494. }
  495. void /* PRIVATE */
  496. png_handle_IEND(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  497. {
  498. png_debug(1, "in png_handle_IEND\n");
  499. if (!(png_ptr->mode & PNG_HAVE_IHDR) || !(png_ptr->mode & PNG_HAVE_IDAT))
  500. {
  501. png_error(png_ptr, "No image in file");
  502. }
  503. png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND);
  504. if (length != 0)
  505. {
  506. png_warning(png_ptr, "Incorrect IEND chunk length");
  507. }
  508. png_crc_finish(png_ptr, length);
  509. if (&info_ptr == NULL) /* quiet compiler warnings about unused info_ptr */
  510. return;
  511. }
  512. #if defined(PNG_READ_gAMA_SUPPORTED)
  513. void /* PRIVATE */
  514. png_handle_gAMA(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  515. {
  516. png_fixed_point igamma;
  517. #ifdef PNG_FLOATING_POINT_SUPPORTED
  518. float file_gamma;
  519. #endif
  520. png_byte buf[4];
  521. png_debug(1, "in png_handle_gAMA\n");
  522. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  523. png_error(png_ptr, "Missing IHDR before gAMA");
  524. else if (png_ptr->mode & PNG_HAVE_IDAT)
  525. {
  526. png_warning(png_ptr, "Invalid gAMA after IDAT");
  527. png_crc_finish(png_ptr, length);
  528. return;
  529. }
  530. else if (png_ptr->mode & PNG_HAVE_PLTE)
  531. /* Should be an error, but we can cope with it */
  532. png_warning(png_ptr, "Out of place gAMA chunk");
  533. if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA)
  534. #if defined(PNG_READ_sRGB_SUPPORTED)
  535. && !(info_ptr->valid & PNG_INFO_sRGB)
  536. #endif
  537. )
  538. {
  539. png_warning(png_ptr, "Duplicate gAMA chunk");
  540. png_crc_finish(png_ptr, length);
  541. return;
  542. }
  543. if (length != 4)
  544. {
  545. png_warning(png_ptr, "Incorrect gAMA chunk length");
  546. png_crc_finish(png_ptr, length);
  547. return;
  548. }
  549. png_crc_read(png_ptr, buf, 4);
  550. if (png_crc_finish(png_ptr, 0))
  551. return;
  552. igamma = (png_fixed_point)png_get_uint_32(buf);
  553. /* check for zero gamma */
  554. if (igamma == 0)
  555. {
  556. png_warning(png_ptr,
  557. "Ignoring gAMA chunk with gamma=0");
  558. return;
  559. }
  560. #if defined(PNG_READ_sRGB_SUPPORTED)
  561. if (info_ptr->valid & PNG_INFO_sRGB)
  562. if (PNG_OUT_OF_RANGE(igamma, 45500L, 500))
  563. {
  564. png_warning(png_ptr,
  565. "Ignoring incorrect gAMA value when sRGB is also present");
  566. #ifndef PNG_NO_CONSOLE_IO
  567. fprintf(stderr, "gamma = (%d/100000)\n", (int)igamma);
  568. #endif
  569. return;
  570. }
  571. #endif /* PNG_READ_sRGB_SUPPORTED */
  572. #ifdef PNG_FLOATING_POINT_SUPPORTED
  573. file_gamma = (float)igamma / (float)100000.0;
  574. # ifdef PNG_READ_GAMMA_SUPPORTED
  575. png_ptr->gamma = file_gamma;
  576. # endif
  577. png_set_gAMA(png_ptr, info_ptr, file_gamma);
  578. #endif
  579. #ifdef PNG_FIXED_POINT_SUPPORTED
  580. png_set_gAMA_fixed(png_ptr, info_ptr, igamma);
  581. #endif
  582. }
  583. #endif
  584. #if defined(PNG_READ_sBIT_SUPPORTED)
  585. void /* PRIVATE */
  586. png_handle_sBIT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  587. {
  588. png_size_t truelen;
  589. png_byte buf[4];
  590. png_debug(1, "in png_handle_sBIT\n");
  591. buf[0] = buf[1] = buf[2] = buf[3] = 0;
  592. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  593. png_error(png_ptr, "Missing IHDR before sBIT");
  594. else if (png_ptr->mode & PNG_HAVE_IDAT)
  595. {
  596. png_warning(png_ptr, "Invalid sBIT after IDAT");
  597. png_crc_finish(png_ptr, length);
  598. return;
  599. }
  600. else if (png_ptr->mode & PNG_HAVE_PLTE)
  601. {
  602. /* Should be an error, but we can cope with it */
  603. png_warning(png_ptr, "Out of place sBIT chunk");
  604. }
  605. if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT))
  606. {
  607. png_warning(png_ptr, "Duplicate sBIT chunk");
  608. png_crc_finish(png_ptr, length);
  609. return;
  610. }
  611. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  612. truelen = 3;
  613. else
  614. truelen = (png_size_t)png_ptr->channels;
  615. if (length != truelen || length > 4)
  616. {
  617. png_warning(png_ptr, "Incorrect sBIT chunk length");
  618. png_crc_finish(png_ptr, length);
  619. return;
  620. }
  621. png_crc_read(png_ptr, buf, truelen);
  622. if (png_crc_finish(png_ptr, 0))
  623. return;
  624. if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
  625. {
  626. png_ptr->sig_bit.red = buf[0];
  627. png_ptr->sig_bit.green = buf[1];
  628. png_ptr->sig_bit.blue = buf[2];
  629. png_ptr->sig_bit.alpha = buf[3];
  630. }
  631. else
  632. {
  633. png_ptr->sig_bit.gray = buf[0];
  634. png_ptr->sig_bit.red = buf[0];
  635. png_ptr->sig_bit.green = buf[0];
  636. png_ptr->sig_bit.blue = buf[0];
  637. png_ptr->sig_bit.alpha = buf[1];
  638. }
  639. png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit));
  640. }
  641. #endif
  642. #if defined(PNG_READ_cHRM_SUPPORTED)
  643. void /* PRIVATE */
  644. png_handle_cHRM(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  645. {
  646. png_byte buf[4];
  647. #ifdef PNG_FLOATING_POINT_SUPPORTED
  648. float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y;
  649. #endif
  650. png_fixed_point int_x_white, int_y_white, int_x_red, int_y_red, int_x_green,
  651. int_y_green, int_x_blue, int_y_blue;
  652. png_uint_32 uint_x, uint_y;
  653. png_debug(1, "in png_handle_cHRM\n");
  654. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  655. png_error(png_ptr, "Missing IHDR before cHRM");
  656. else if (png_ptr->mode & PNG_HAVE_IDAT)
  657. {
  658. png_warning(png_ptr, "Invalid cHRM after IDAT");
  659. png_crc_finish(png_ptr, length);
  660. return;
  661. }
  662. else if (png_ptr->mode & PNG_HAVE_PLTE)
  663. /* Should be an error, but we can cope with it */
  664. png_warning(png_ptr, "Missing PLTE before cHRM");
  665. if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM)
  666. #if defined(PNG_READ_sRGB_SUPPORTED)
  667. && !(info_ptr->valid & PNG_INFO_sRGB)
  668. #endif
  669. )
  670. {
  671. png_warning(png_ptr, "Duplicate cHRM chunk");
  672. png_crc_finish(png_ptr, length);
  673. return;
  674. }
  675. if (length != 32)
  676. {
  677. png_warning(png_ptr, "Incorrect cHRM chunk length");
  678. png_crc_finish(png_ptr, length);
  679. return;
  680. }
  681. png_crc_read(png_ptr, buf, 4);
  682. uint_x = png_get_uint_32(buf);
  683. png_crc_read(png_ptr, buf, 4);
  684. uint_y = png_get_uint_32(buf);
  685. if (uint_x > 80000L || uint_y > 80000L ||
  686. uint_x + uint_y > 100000L)
  687. {
  688. png_warning(png_ptr, "Invalid cHRM white point");
  689. png_crc_finish(png_ptr, 24);
  690. return;
  691. }
  692. int_x_white = (png_fixed_point)uint_x;
  693. int_y_white = (png_fixed_point)uint_y;
  694. png_crc_read(png_ptr, buf, 4);
  695. uint_x = png_get_uint_32(buf);
  696. png_crc_read(png_ptr, buf, 4);
  697. uint_y = png_get_uint_32(buf);
  698. if (uint_x > 80000L || uint_y > 80000L ||
  699. uint_x + uint_y > 100000L)
  700. {
  701. png_warning(png_ptr, "Invalid cHRM red point");
  702. png_crc_finish(png_ptr, 16);
  703. return;
  704. }
  705. int_x_red = (png_fixed_point)uint_x;
  706. int_y_red = (png_fixed_point)uint_y;
  707. png_crc_read(png_ptr, buf, 4);
  708. uint_x = png_get_uint_32(buf);
  709. png_crc_read(png_ptr, buf, 4);
  710. uint_y = png_get_uint_32(buf);
  711. if (uint_x > 80000L || uint_y > 80000L ||
  712. uint_x + uint_y > 100000L)
  713. {
  714. png_warning(png_ptr, "Invalid cHRM green point");
  715. png_crc_finish(png_ptr, 8);
  716. return;
  717. }
  718. int_x_green = (png_fixed_point)uint_x;
  719. int_y_green = (png_fixed_point)uint_y;
  720. png_crc_read(png_ptr, buf, 4);
  721. uint_x = png_get_uint_32(buf);
  722. png_crc_read(png_ptr, buf, 4);
  723. uint_y = png_get_uint_32(buf);
  724. if (uint_x > 80000L || uint_y > 80000L ||
  725. uint_x + uint_y > 100000L)
  726. {
  727. png_warning(png_ptr, "Invalid cHRM blue point");
  728. png_crc_finish(png_ptr, 0);
  729. return;
  730. }
  731. int_x_blue = (png_fixed_point)uint_x;
  732. int_y_blue = (png_fixed_point)uint_y;
  733. #ifdef PNG_FLOATING_POINT_SUPPORTED
  734. white_x = (float)int_x_white / (float)100000.0;
  735. white_y = (float)int_y_white / (float)100000.0;
  736. red_x = (float)int_x_red / (float)100000.0;
  737. red_y = (float)int_y_red / (float)100000.0;
  738. green_x = (float)int_x_green / (float)100000.0;
  739. green_y = (float)int_y_green / (float)100000.0;
  740. blue_x = (float)int_x_blue / (float)100000.0;
  741. blue_y = (float)int_y_blue / (float)100000.0;
  742. #endif
  743. #if defined(PNG_READ_sRGB_SUPPORTED)
  744. if (info_ptr->valid & PNG_INFO_sRGB)
  745. {
  746. if (PNG_OUT_OF_RANGE(int_x_white, 31270, 1000) ||
  747. PNG_OUT_OF_RANGE(int_y_white, 32900, 1000) ||
  748. PNG_OUT_OF_RANGE(int_x_red, 64000L, 1000) ||
  749. PNG_OUT_OF_RANGE(int_y_red, 33000, 1000) ||
  750. PNG_OUT_OF_RANGE(int_x_green, 30000, 1000) ||
  751. PNG_OUT_OF_RANGE(int_y_green, 60000L, 1000) ||
  752. PNG_OUT_OF_RANGE(int_x_blue, 15000, 1000) ||
  753. PNG_OUT_OF_RANGE(int_y_blue, 6000, 1000))
  754. {
  755. png_warning(png_ptr,
  756. "Ignoring incorrect cHRM value when sRGB is also present");
  757. #ifndef PNG_NO_CONSOLE_IO
  758. #ifdef PNG_FLOATING_POINT_SUPPORTED
  759. fprintf(stderr,"wx=%f, wy=%f, rx=%f, ry=%f\n",
  760. white_x, white_y, red_x, red_y);
  761. fprintf(stderr,"gx=%f, gy=%f, bx=%f, by=%f\n",
  762. green_x, green_y, blue_x, blue_y);
  763. #else
  764. fprintf(stderr,"wx=%ld, wy=%ld, rx=%ld, ry=%ld\n",
  765. int_x_white, int_y_white, int_x_red, int_y_red);
  766. fprintf(stderr,"gx=%ld, gy=%ld, bx=%ld, by=%ld\n",
  767. int_x_green, int_y_green, int_x_blue, int_y_blue);
  768. #endif
  769. #endif /* PNG_NO_CONSOLE_IO */
  770. }
  771. png_crc_finish(png_ptr, 0);
  772. return;
  773. }
  774. #endif /* PNG_READ_sRGB_SUPPORTED */
  775. #ifdef PNG_FLOATING_POINT_SUPPORTED
  776. png_set_cHRM(png_ptr, info_ptr,
  777. white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y);
  778. #endif
  779. #ifdef PNG_FIXED_POINT_SUPPORTED
  780. png_set_cHRM_fixed(png_ptr, info_ptr,
  781. int_x_white, int_y_white, int_x_red, int_y_red, int_x_green,
  782. int_y_green, int_x_blue, int_y_blue);
  783. #endif
  784. if (png_crc_finish(png_ptr, 0))
  785. return;
  786. }
  787. #endif
  788. #if defined(PNG_READ_sRGB_SUPPORTED)
  789. void /* PRIVATE */
  790. png_handle_sRGB(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  791. {
  792. int intent;
  793. png_byte buf[1];
  794. png_debug(1, "in png_handle_sRGB\n");
  795. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  796. png_error(png_ptr, "Missing IHDR before sRGB");
  797. else if (png_ptr->mode & PNG_HAVE_IDAT)
  798. {
  799. png_warning(png_ptr, "Invalid sRGB after IDAT");
  800. png_crc_finish(png_ptr, length);
  801. return;
  802. }
  803. else if (png_ptr->mode & PNG_HAVE_PLTE)
  804. /* Should be an error, but we can cope with it */
  805. png_warning(png_ptr, "Out of place sRGB chunk");
  806. if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB))
  807. {
  808. png_warning(png_ptr, "Duplicate sRGB chunk");
  809. png_crc_finish(png_ptr, length);
  810. return;
  811. }
  812. if (length != 1)
  813. {
  814. png_warning(png_ptr, "Incorrect sRGB chunk length");
  815. png_crc_finish(png_ptr, length);
  816. return;
  817. }
  818. png_crc_read(png_ptr, buf, 1);
  819. if (png_crc_finish(png_ptr, 0))
  820. return;
  821. intent = buf[0];
  822. /* check for bad intent */
  823. if (intent >= PNG_sRGB_INTENT_LAST)
  824. {
  825. png_warning(png_ptr, "Unknown sRGB intent");
  826. return;
  827. }
  828. #if defined(PNG_READ_gAMA_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED)
  829. if ((info_ptr->valid & PNG_INFO_gAMA))
  830. {
  831. png_fixed_point igamma;
  832. #ifdef PNG_FIXED_POINT_SUPPORTED
  833. igamma=info_ptr->int_gamma;
  834. #else
  835. # ifdef PNG_FLOATING_POINT_SUPPORTED
  836. igamma=(png_fixed_point)(info_ptr->gamma * 100000.);
  837. # endif
  838. #endif
  839. if (PNG_OUT_OF_RANGE(igamma, 45500L, 500))
  840. {
  841. png_warning(png_ptr,
  842. "Ignoring incorrect gAMA value when sRGB is also present");
  843. #ifndef PNG_NO_CONSOLE_IO
  844. # ifdef PNG_FIXED_POINT_SUPPORTED
  845. fprintf(stderr,"incorrect gamma=(%d/100000)\n",(int)png_ptr->int_gamma);
  846. # else
  847. # ifdef PNG_FLOATING_POINT_SUPPORTED
  848. fprintf(stderr,"incorrect gamma=%f\n",png_ptr->gamma);
  849. # endif
  850. # endif
  851. #endif
  852. }
  853. }
  854. #endif /* PNG_READ_gAMA_SUPPORTED */
  855. #ifdef PNG_READ_cHRM_SUPPORTED
  856. #ifdef PNG_FIXED_POINT_SUPPORTED
  857. if (info_ptr->valid & PNG_INFO_cHRM)
  858. if (PNG_OUT_OF_RANGE(info_ptr->int_x_white, 31270, 1000) ||
  859. PNG_OUT_OF_RANGE(info_ptr->int_y_white, 32900, 1000) ||
  860. PNG_OUT_OF_RANGE(info_ptr->int_x_red, 64000L, 1000) ||
  861. PNG_OUT_OF_RANGE(info_ptr->int_y_red, 33000, 1000) ||
  862. PNG_OUT_OF_RANGE(info_ptr->int_x_green, 30000, 1000) ||
  863. PNG_OUT_OF_RANGE(info_ptr->int_y_green, 60000L, 1000) ||
  864. PNG_OUT_OF_RANGE(info_ptr->int_x_blue, 15000, 1000) ||
  865. PNG_OUT_OF_RANGE(info_ptr->int_y_blue, 6000, 1000))
  866. {
  867. png_warning(png_ptr,
  868. "Ignoring incorrect cHRM value when sRGB is also present");
  869. }
  870. #endif /* PNG_FIXED_POINT_SUPPORTED */
  871. #endif /* PNG_READ_cHRM_SUPPORTED */
  872. png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr, intent);
  873. }
  874. #endif /* PNG_READ_sRGB_SUPPORTED */
  875. #if defined(PNG_READ_iCCP_SUPPORTED)
  876. void /* PRIVATE */
  877. png_handle_iCCP(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  878. /* Note: this does not properly handle chunks that are > 64K under DOS */
  879. {
  880. png_charp chunkdata;
  881. png_byte compression_type;
  882. png_bytep pC;
  883. png_charp profile;
  884. png_uint_32 skip = 0;
  885. png_uint_32 profile_size, profile_length;
  886. png_size_t slength, prefix_length, data_length;
  887. png_debug(1, "in png_handle_iCCP\n");
  888. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  889. png_error(png_ptr, "Missing IHDR before iCCP");
  890. else if (png_ptr->mode & PNG_HAVE_IDAT)
  891. {
  892. png_warning(png_ptr, "Invalid iCCP after IDAT");
  893. png_crc_finish(png_ptr, length);
  894. return;
  895. }
  896. else if (png_ptr->mode & PNG_HAVE_PLTE)
  897. /* Should be an error, but we can cope with it */
  898. png_warning(png_ptr, "Out of place iCCP chunk");
  899. if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_iCCP))
  900. {
  901. png_warning(png_ptr, "Duplicate iCCP chunk");
  902. png_crc_finish(png_ptr, length);
  903. return;
  904. }
  905. #ifdef PNG_MAX_MALLOC_64K
  906. if (length > (png_uint_32)65535L)
  907. {
  908. png_warning(png_ptr, "iCCP chunk too large to fit in memory");
  909. skip = length - (png_uint_32)65535L;
  910. length = (png_uint_32)65535L;
  911. }
  912. #endif
  913. chunkdata = (png_charp)png_malloc(png_ptr, length + 1);
  914. slength = (png_size_t)length;
  915. png_crc_read(png_ptr, (png_bytep)chunkdata, slength);
  916. if (png_crc_finish(png_ptr, skip))
  917. {
  918. png_free(png_ptr, chunkdata);
  919. return;
  920. }
  921. chunkdata[slength] = 0x00;
  922. for (profile = chunkdata; *profile; profile++)
  923. /* empty loop to find end of name */ ;
  924. ++profile;
  925. /* there should be at least one zero (the compression type byte)
  926. following the separator, and we should be on it */
  927. if ( profile >= chunkdata + slength)
  928. {
  929. png_free(png_ptr, chunkdata);
  930. png_warning(png_ptr, "Malformed iCCP chunk");
  931. return;
  932. }
  933. /* compression_type should always be zero */
  934. compression_type = *profile++;
  935. if (compression_type)
  936. {
  937. png_warning(png_ptr, "Ignoring nonzero compression type in iCCP chunk");
  938. compression_type=0x00; /* Reset it to zero (libpng-1.0.6 through 1.0.8
  939. wrote nonzero) */
  940. }
  941. prefix_length = profile - chunkdata;
  942. chunkdata = png_decompress_chunk(png_ptr, compression_type, chunkdata,
  943. slength, prefix_length, &data_length);
  944. profile_length = data_length - prefix_length;
  945. if ( prefix_length > data_length || profile_length < 4)
  946. {
  947. png_free(png_ptr, chunkdata);
  948. png_warning(png_ptr, "Profile size field missing from iCCP chunk");
  949. return;
  950. }
  951. /* Check the profile_size recorded in the first 32 bits of the ICC profile */
  952. pC = (png_bytep)(chunkdata+prefix_length);
  953. profile_size = ((*(pC ))<<24) |
  954. ((*(pC+1))<<16) |
  955. ((*(pC+2))<< 8) |
  956. ((*(pC+3)) );
  957. if(profile_size < profile_length)
  958. profile_length = profile_size;
  959. if(profile_size > profile_length)
  960. {
  961. png_free(png_ptr, chunkdata);
  962. png_warning(png_ptr, "Ignoring truncated iCCP profile.\n");
  963. return;
  964. }
  965. png_set_iCCP(png_ptr, info_ptr, chunkdata, compression_type,
  966. chunkdata + prefix_length, profile_length);
  967. png_free(png_ptr, chunkdata);
  968. }
  969. #endif /* PNG_READ_iCCP_SUPPORTED */
  970. #if defined(PNG_READ_sPLT_SUPPORTED)
  971. void /* PRIVATE */
  972. png_handle_sPLT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  973. /* Note: this does not properly handle chunks that are > 64K under DOS */
  974. {
  975. png_bytep chunkdata;
  976. png_bytep entry_start;
  977. png_sPLT_t new_palette;
  978. #ifdef PNG_NO_POINTER_INDEXING
  979. png_sPLT_entryp pp;
  980. #endif
  981. int data_length, entry_size, i;
  982. png_uint_32 skip = 0;
  983. png_size_t slength;
  984. png_debug(1, "in png_handle_sPLT\n");
  985. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  986. png_error(png_ptr, "Missing IHDR before sPLT");
  987. else if (png_ptr->mode & PNG_HAVE_IDAT)
  988. {
  989. png_warning(png_ptr, "Invalid sPLT after IDAT");
  990. png_crc_finish(png_ptr, length);
  991. return;
  992. }
  993. #ifdef PNG_MAX_MALLOC_64K
  994. if (length > (png_uint_32)65535L)
  995. {
  996. png_warning(png_ptr, "sPLT chunk too large to fit in memory");
  997. skip = length - (png_uint_32)65535L;
  998. length = (png_uint_32)65535L;
  999. }
  1000. #endif
  1001. chunkdata = (png_bytep)png_malloc(png_ptr, length + 1);
  1002. slength = (png_size_t)length;
  1003. png_crc_read(png_ptr, (png_bytep)chunkdata, slength);
  1004. if (png_crc_finish(png_ptr, skip))
  1005. {
  1006. png_free(png_ptr, chunkdata);
  1007. return;
  1008. }
  1009. chunkdata[slength] = 0x00;
  1010. for (entry_start = chunkdata; *entry_start; entry_start++)
  1011. /* empty loop to find end of name */ ;
  1012. ++entry_start;
  1013. /* a sample depth should follow the separator, and we should be on it */
  1014. if (entry_start > chunkdata + slength)
  1015. {
  1016. png_free(png_ptr, chunkdata);
  1017. png_warning(png_ptr, "malformed sPLT chunk");
  1018. return;
  1019. }
  1020. new_palette.depth = *entry_start++;
  1021. entry_size = (new_palette.depth == 8 ? 6 : 10);
  1022. data_length = (slength - (entry_start - chunkdata));
  1023. /* integrity-check the data length */
  1024. if (data_length % entry_size)
  1025. {
  1026. png_free(png_ptr, chunkdata);
  1027. png_warning(png_ptr, "sPLT chunk has bad length");
  1028. return;
  1029. }
  1030. new_palette.nentries = (png_uint_32) (data_length / entry_size);
  1031. if ((png_uint_32) new_palette.nentries > (png_uint_32) (PNG_SIZE_MAX /
  1032. png_sizeof(png_sPLT_entry)))
  1033. {
  1034. png_warning(png_ptr, "sPLT chunk too long");
  1035. return;
  1036. }
  1037. new_palette.entries = (png_sPLT_entryp)png_malloc_warn(
  1038. png_ptr, new_palette.nentries * png_sizeof(png_sPLT_entry));
  1039. if (new_palette.entries == NULL)
  1040. {
  1041. png_warning(png_ptr, "sPLT chunk requires too much memory");
  1042. return;
  1043. }
  1044. #ifndef PNG_NO_POINTER_INDEXING
  1045. for (i = 0; i < new_palette.nentries; i++)
  1046. {
  1047. png_sPLT_entryp pp = new_palette.entries + i;
  1048. if (new_palette.depth == 8)
  1049. {
  1050. pp->red = *entry_start++;
  1051. pp->green = *entry_start++;
  1052. pp->blue = *entry_start++;
  1053. pp->alpha = *entry_start++;
  1054. }
  1055. else
  1056. {
  1057. pp->red = png_get_uint_16(entry_start); entry_start += 2;
  1058. pp->green = png_get_uint_16(entry_start); entry_start += 2;
  1059. pp->blue = png_get_uint_16(entry_start); entry_start += 2;
  1060. pp->alpha = png_get_uint_16(entry_start); entry_start += 2;
  1061. }
  1062. pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
  1063. }
  1064. #else
  1065. pp = new_palette.entries;
  1066. for (i = 0; i < new_palette.nentries; i++)
  1067. {
  1068. if (new_palette.depth == 8)
  1069. {
  1070. pp[i].red = *entry_start++;
  1071. pp[i].green = *entry_start++;
  1072. pp[i].blue = *entry_start++;
  1073. pp[i].alpha = *entry_start++;
  1074. }
  1075. else
  1076. {
  1077. pp[i].red = png_get_uint_16(entry_start); entry_start += 2;
  1078. pp[i].green = png_get_uint_16(entry_start); entry_start += 2;
  1079. pp[i].blue = png_get_uint_16(entry_start); entry_start += 2;
  1080. pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2;
  1081. }
  1082. pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
  1083. }
  1084. #endif
  1085. /* discard all chunk data except the name and stash that */
  1086. new_palette.name = (png_charp)chunkdata;
  1087. png_set_sPLT(png_ptr, info_ptr, &new_palette, 1);
  1088. png_free(png_ptr, chunkdata);
  1089. png_free(png_ptr, new_palette.entries);
  1090. }
  1091. #endif /* PNG_READ_sPLT_SUPPORTED */
  1092. #if defined(PNG_READ_tRNS_SUPPORTED)
  1093. void /* PRIVATE */
  1094. png_handle_tRNS(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1095. {
  1096. png_byte readbuf[PNG_MAX_PALETTE_LENGTH];
  1097. png_debug(1, "in png_handle_tRNS\n");
  1098. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1099. png_error(png_ptr, "Missing IHDR before tRNS");
  1100. else if (png_ptr->mode & PNG_HAVE_IDAT)
  1101. {
  1102. png_warning(png_ptr, "Invalid tRNS after IDAT");
  1103. png_crc_finish(png_ptr, length);
  1104. return;
  1105. }
  1106. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
  1107. {
  1108. png_warning(png_ptr, "Duplicate tRNS chunk");
  1109. png_crc_finish(png_ptr, length);
  1110. return;
  1111. }
  1112. if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
  1113. {
  1114. png_byte buf[2];
  1115. if (length != 2)
  1116. {
  1117. png_warning(png_ptr, "Incorrect tRNS chunk length");
  1118. png_crc_finish(png_ptr, length);
  1119. return;
  1120. }
  1121. png_crc_read(png_ptr, buf, 2);
  1122. png_ptr->num_trans = 1;
  1123. png_ptr->trans_values.gray = png_get_uint_16(buf);
  1124. }
  1125. else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
  1126. {
  1127. png_byte buf[6];
  1128. if (length != 6)
  1129. {
  1130. png_warning(png_ptr, "Incorrect tRNS chunk length");
  1131. png_crc_finish(png_ptr, length);
  1132. return;
  1133. }
  1134. png_crc_read(png_ptr, buf, (png_size_t)length);
  1135. png_ptr->num_trans = 1;
  1136. png_ptr->trans_values.red = png_get_uint_16(buf);
  1137. png_ptr->trans_values.green = png_get_uint_16(buf + 2);
  1138. png_ptr->trans_values.blue = png_get_uint_16(buf + 4);
  1139. }
  1140. else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  1141. {
  1142. if (!(png_ptr->mode & PNG_HAVE_PLTE))
  1143. {
  1144. /* Should be an error, but we can cope with it. */
  1145. png_warning(png_ptr, "Missing PLTE before tRNS");
  1146. }
  1147. if (length > (png_uint_32)png_ptr->num_palette ||
  1148. length > PNG_MAX_PALETTE_LENGTH)
  1149. {
  1150. png_warning(png_ptr, "Incorrect tRNS chunk length");
  1151. png_crc_finish(png_ptr, length);
  1152. return;
  1153. }
  1154. if (length == 0)
  1155. {
  1156. png_warning(png_ptr, "Zero length tRNS chunk");
  1157. png_crc_finish(png_ptr, length);
  1158. return;
  1159. }
  1160. png_crc_read(png_ptr, readbuf, (png_size_t)length);
  1161. png_ptr->num_trans = (png_uint_16)length;
  1162. }
  1163. else
  1164. {
  1165. png_warning(png_ptr, "tRNS chunk not allowed with alpha channel");
  1166. png_crc_finish(png_ptr, length);
  1167. return;
  1168. }
  1169. if (png_crc_finish(png_ptr, 0))
  1170. return;
  1171. png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans,
  1172. &(png_ptr->trans_values));
  1173. }
  1174. #endif
  1175. #if defined(PNG_READ_bKGD_SUPPORTED)
  1176. void /* PRIVATE */
  1177. png_handle_bKGD(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1178. {
  1179. png_size_t truelen;
  1180. png_byte buf[6];
  1181. png_debug(1, "in png_handle_bKGD\n");
  1182. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1183. png_error(png_ptr, "Missing IHDR before bKGD");
  1184. else if (png_ptr->mode & PNG_HAVE_IDAT)
  1185. {
  1186. png_warning(png_ptr, "Invalid bKGD after IDAT");
  1187. png_crc_finish(png_ptr, length);
  1188. return;
  1189. }
  1190. else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
  1191. !(png_ptr->mode & PNG_HAVE_PLTE))
  1192. {
  1193. png_warning(png_ptr, "Missing PLTE before bKGD");
  1194. png_crc_finish(png_ptr, length);
  1195. return;
  1196. }
  1197. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD))
  1198. {
  1199. png_warning(png_ptr, "Duplicate bKGD chunk");
  1200. png_crc_finish(png_ptr, length);
  1201. return;
  1202. }
  1203. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  1204. truelen = 1;
  1205. else if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
  1206. truelen = 6;
  1207. else
  1208. truelen = 2;
  1209. if (length != truelen)
  1210. {
  1211. png_warning(png_ptr, "Incorrect bKGD chunk length");
  1212. png_crc_finish(png_ptr, length);
  1213. return;
  1214. }
  1215. png_crc_read(png_ptr, buf, truelen);
  1216. if (png_crc_finish(png_ptr, 0))
  1217. return;
  1218. /* We convert the index value into RGB components so that we can allow
  1219. * arbitrary RGB values for background when we have transparency, and
  1220. * so it is easy to determine the RGB values of the background color
  1221. * from the info_ptr struct. */
  1222. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  1223. {
  1224. png_ptr->background.index = buf[0];
  1225. if(info_ptr->num_palette)
  1226. {
  1227. if(buf[0] > info_ptr->num_palette)
  1228. {
  1229. png_warning(png_ptr, "Incorrect bKGD chunk index value");
  1230. return;
  1231. }
  1232. png_ptr->background.red =
  1233. (png_uint_16)png_ptr->palette[buf[0]].red;
  1234. png_ptr->background.green =
  1235. (png_uint_16)png_ptr->palette[buf[0]].green;
  1236. png_ptr->background.blue =
  1237. (png_uint_16)png_ptr->palette[buf[0]].blue;
  1238. }
  1239. }
  1240. else if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) /* GRAY */
  1241. {
  1242. png_ptr->background.red =
  1243. png_ptr->background.green =
  1244. png_ptr->background.blue =
  1245. png_ptr->background.gray = png_get_uint_16(buf);
  1246. }
  1247. else
  1248. {
  1249. png_ptr->background.red = png_get_uint_16(buf);
  1250. png_ptr->background.green = png_get_uint_16(buf + 2);
  1251. png_ptr->background.blue = png_get_uint_16(buf + 4);
  1252. }
  1253. png_set_bKGD(png_ptr, info_ptr, &(png_ptr->background));
  1254. }
  1255. #endif
  1256. #if defined(PNG_READ_hIST_SUPPORTED)
  1257. void /* PRIVATE */
  1258. png_handle_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1259. {
  1260. unsigned int num, i;
  1261. png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH];
  1262. png_debug(1, "in png_handle_hIST\n");
  1263. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1264. png_error(png_ptr, "Missing IHDR before hIST");
  1265. else if (png_ptr->mode & PNG_HAVE_IDAT)
  1266. {
  1267. png_warning(png_ptr, "Invalid hIST after IDAT");
  1268. png_crc_finish(png_ptr, length);
  1269. return;
  1270. }
  1271. else if (!(png_ptr->mode & PNG_HAVE_PLTE))
  1272. {
  1273. png_warning(png_ptr, "Missing PLTE before hIST");
  1274. png_crc_finish(png_ptr, length);
  1275. return;
  1276. }
  1277. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST))
  1278. {
  1279. png_warning(png_ptr, "Duplicate hIST chunk");
  1280. png_crc_finish(png_ptr, length);
  1281. return;
  1282. }
  1283. num = length / 2 ;
  1284. if (num != (unsigned int) png_ptr->num_palette || num >
  1285. (unsigned int) PNG_MAX_PALETTE_LENGTH)
  1286. {
  1287. png_warning(png_ptr, "Incorrect hIST chunk length");
  1288. png_crc_finish(png_ptr, length);
  1289. return;
  1290. }
  1291. for (i = 0; i < num; i++)
  1292. {
  1293. png_byte buf[2];
  1294. png_crc_read(png_ptr, buf, 2);
  1295. readbuf[i] = png_get_uint_16(buf);
  1296. }
  1297. if (png_crc_finish(png_ptr, 0))
  1298. return;
  1299. png_set_hIST(png_ptr, info_ptr, readbuf);
  1300. }
  1301. #endif
  1302. #if defined(PNG_READ_pHYs_SUPPORTED)
  1303. void /* PRIVATE */
  1304. png_handle_pHYs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1305. {
  1306. png_byte buf[9];
  1307. png_uint_32 res_x, res_y;
  1308. int unit_type;
  1309. png_debug(1, "in png_handle_pHYs\n");
  1310. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1311. png_error(png_ptr, "Missing IHDR before pHYs");
  1312. else if (png_ptr->mode & PNG_HAVE_IDAT)
  1313. {
  1314. png_warning(png_ptr, "Invalid pHYs after IDAT");
  1315. png_crc_finish(png_ptr, length);
  1316. return;
  1317. }
  1318. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
  1319. {
  1320. png_warning(png_ptr, "Duplicate pHYs chunk");
  1321. png_crc_finish(png_ptr, length);
  1322. return;
  1323. }
  1324. if (length != 9)
  1325. {
  1326. png_warning(png_ptr, "Incorrect pHYs chunk length");
  1327. png_crc_finish(png_ptr, length);
  1328. return;
  1329. }
  1330. png_crc_read(png_ptr, buf, 9);
  1331. if (png_crc_finish(png_ptr, 0))
  1332. return;
  1333. res_x = png_get_uint_32(buf);
  1334. res_y = png_get_uint_32(buf + 4);
  1335. unit_type = buf[8];
  1336. png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type);
  1337. }
  1338. #endif
  1339. #if defined(PNG_READ_oFFs_SUPPORTED)
  1340. void /* PRIVATE */
  1341. png_handle_oFFs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1342. {
  1343. png_byte buf[9];
  1344. png_int_32 offset_x, offset_y;
  1345. int unit_type;
  1346. png_debug(1, "in png_handle_oFFs\n");
  1347. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1348. png_error(png_ptr, "Missing IHDR before oFFs");
  1349. else if (png_ptr->mode & PNG_HAVE_IDAT)
  1350. {
  1351. png_warning(png_ptr, "Invalid oFFs after IDAT");
  1352. png_crc_finish(png_ptr, length);
  1353. return;
  1354. }
  1355. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs))
  1356. {
  1357. png_warning(png_ptr, "Duplicate oFFs chunk");
  1358. png_crc_finish(png_ptr, length);
  1359. return;
  1360. }
  1361. if (length != 9)
  1362. {
  1363. png_warning(png_ptr, "Incorrect oFFs chunk length");
  1364. png_crc_finish(png_ptr, length);
  1365. return;
  1366. }
  1367. png_crc_read(png_ptr, buf, 9);
  1368. if (png_crc_finish(png_ptr, 0))
  1369. return;
  1370. offset_x = png_get_int_32(buf);
  1371. offset_y = png_get_int_32(buf + 4);
  1372. unit_type = buf[8];
  1373. png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);
  1374. }
  1375. #endif
  1376. #if defined(PNG_READ_pCAL_SUPPORTED)
  1377. /* read the pCAL chunk (described in the PNG Extensions document) */
  1378. void /* PRIVATE */
  1379. png_handle_pCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1380. {
  1381. png_charp purpose;
  1382. png_int_32 X0, X1;
  1383. png_byte type, nparams;
  1384. png_charp buf, units, endptr;
  1385. png_charpp params;
  1386. png_size_t slength;
  1387. int i;
  1388. png_debug(1, "in png_handle_pCAL\n");
  1389. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1390. png_error(png_ptr, "Missing IHDR before pCAL");
  1391. else if (png_ptr->mode & PNG_HAVE_IDAT)
  1392. {
  1393. png_warning(png_ptr, "Invalid pCAL after IDAT");
  1394. png_crc_finish(png_ptr, length);
  1395. return;
  1396. }
  1397. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL))
  1398. {
  1399. png_warning(png_ptr, "Duplicate pCAL chunk");
  1400. png_crc_finish(png_ptr, length);
  1401. return;
  1402. }
  1403. png_debug1(2, "Allocating and reading pCAL chunk data (%lu bytes)\n",
  1404. length + 1);
  1405. purpose = (png_charp)png_malloc_warn(png_ptr, length + 1);
  1406. if (purpose == NULL)
  1407. {
  1408. png_warning(png_ptr, "No memory for pCAL purpose.");
  1409. return;
  1410. }
  1411. slength = (png_size_t)length;
  1412. png_crc_read(png_ptr, (png_bytep)purpose, slength);
  1413. if (png_crc_finish(png_ptr, 0))
  1414. {
  1415. png_free(png_ptr, purpose);
  1416. return;
  1417. }
  1418. purpose[slength] = 0x00; /* null terminate the last string */
  1419. png_debug(3, "Finding end of pCAL purpose string\n");
  1420. for (buf = purpose; *buf; buf++)
  1421. /* empty loop */ ;
  1422. endptr = purpose + slength;
  1423. /* We need to have at least 12 bytes after the purpose string
  1424. in order to get the parameter information. */
  1425. if (endptr <= buf + 12)
  1426. {
  1427. png_warning(png_ptr, "Invalid pCAL data");
  1428. png_free(png_ptr, purpose);
  1429. return;
  1430. }
  1431. png_debug(3, "Reading pCAL X0, X1, type, nparams, and units\n");
  1432. X0 = png_get_int_32((png_bytep)buf+1);
  1433. X1 = png_get_int_32((png_bytep)buf+5);
  1434. type = buf[9];
  1435. nparams = buf[10];
  1436. units = buf + 11;
  1437. png_debug(3, "Checking pCAL equation type and number of parameters\n");
  1438. /* Check that we have the right number of parameters for known
  1439. equation types. */
  1440. if ((type == PNG_EQUATION_LINEAR && nparams != 2) ||
  1441. (type == PNG_EQUATION_BASE_E && nparams != 3) ||
  1442. (type == PNG_EQUATION_ARBITRARY && nparams != 3) ||
  1443. (type == PNG_EQUATION_HYPERBOLIC && nparams != 4))
  1444. {
  1445. png_warning(png_ptr, "Invalid pCAL parameters for equation type");
  1446. png_free(png_ptr, purpose);
  1447. return;
  1448. }
  1449. else if (type >= PNG_EQUATION_LAST)
  1450. {
  1451. png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
  1452. }
  1453. for (buf = units; *buf; buf++)
  1454. /* Empty loop to move past the units string. */ ;
  1455. png_debug(3, "Allocating pCAL parameters array\n");
  1456. params = (png_charpp)png_malloc_warn(png_ptr, (png_uint_32)(nparams
  1457. *png_sizeof(png_charp))) ;
  1458. if (params == NULL)
  1459. {
  1460. png_free(png_ptr, purpose);
  1461. png_warning(png_ptr, "No memory for pCAL params.");
  1462. return;
  1463. }
  1464. /* Get pointers to the start of each parameter string. */
  1465. for (i = 0; i < (int)nparams; i++)
  1466. {
  1467. buf++; /* Skip the null string terminator from previous parameter. */
  1468. png_debug1(3, "Reading pCAL parameter %d\n", i);
  1469. for (params[i] = buf; *buf != 0x00 && buf <= endptr; buf++)
  1470. /* Empty loop to move past each parameter string */ ;
  1471. /* Make sure we haven't run out of data yet */
  1472. if (buf > endptr)
  1473. {
  1474. png_warning(png_ptr, "Invalid pCAL data");
  1475. png_free(png_ptr, purpose);
  1476. png_free(png_ptr, params);
  1477. return;
  1478. }
  1479. }
  1480. png_set_pCAL(png_ptr, info_ptr, purpose, X0, X1, type, nparams,
  1481. units, params);
  1482. png_free(png_ptr, purpose);
  1483. png_free(png_ptr, params);
  1484. }
  1485. #endif
  1486. #if defined(PNG_READ_sCAL_SUPPORTED)
  1487. /* read the sCAL chunk */
  1488. void /* PRIVATE */
  1489. png_handle_sCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1490. {
  1491. png_charp buffer, ep;
  1492. #ifdef PNG_FLOATING_POINT_SUPPORTED
  1493. double width, height;
  1494. png_charp vp;
  1495. #else
  1496. #ifdef PNG_FIXED_POINT_SUPPORTED
  1497. png_charp swidth, sheight;
  1498. #endif
  1499. #endif
  1500. png_size_t slength;
  1501. png_debug(1, "in png_handle_sCAL\n");
  1502. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1503. png_error(png_ptr, "Missing IHDR before sCAL");
  1504. else if (png_ptr->mode & PNG_HAVE_IDAT)
  1505. {
  1506. png_warning(png_ptr, "Invalid sCAL after IDAT");
  1507. png_crc_finish(png_ptr, length);
  1508. return;
  1509. }
  1510. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL))
  1511. {
  1512. png_warning(png_ptr, "Duplicate sCAL chunk");
  1513. png_crc_finish(png_ptr, length);
  1514. return;
  1515. }
  1516. png_debug1(2, "Allocating and reading sCAL chunk data (%lu bytes)\n",
  1517. length + 1);
  1518. buffer = (png_charp)png_malloc_warn(png_ptr, length + 1);
  1519. if (buffer == NULL)
  1520. {
  1521. png_warning(png_ptr, "Out of memory while processing sCAL chunk");
  1522. return;
  1523. }
  1524. slength = (png_size_t)length;
  1525. png_crc_read(png_ptr, (png_bytep)buffer, slength);
  1526. if (png_crc_finish(png_ptr, 0))
  1527. {
  1528. png_free(png_ptr, buffer);
  1529. return;
  1530. }
  1531. buffer[slength] = 0x00; /* null terminate the last string */
  1532. ep = buffer + 1; /* skip unit byte */
  1533. #ifdef PNG_FLOATING_POINT_SUPPORTED
  1534. width = strtod(ep, &vp);
  1535. if (*vp)
  1536. {
  1537. png_warning(png_ptr, "malformed width string in sCAL chunk");
  1538. return;
  1539. }
  1540. #else
  1541. #ifdef PNG_FIXED_POINT_SUPPORTED
  1542. swidth = (png_charp)png_malloc_warn(png_ptr, png_strlen(ep) + 1);
  1543. if (swidth == NULL)
  1544. {
  1545. png_warning(png_ptr, "Out of memory while processing sCAL chunk width");
  1546. return;
  1547. }
  1548. png_memcpy(swidth, ep, (png_size_t)png_strlen(ep));
  1549. #endif
  1550. #endif
  1551. for (ep = buffer; *ep; ep++)
  1552. /* empty loop */ ;
  1553. ep++;
  1554. #ifdef PNG_FLOATING_POINT_SUPPORTED
  1555. height = strtod(ep, &vp);
  1556. if (*vp)
  1557. {
  1558. png_warning(png_ptr, "malformed height string in sCAL chunk");
  1559. return;
  1560. }
  1561. #else
  1562. #ifdef PNG_FIXED_POINT_SUPPORTED
  1563. sheight = (png_charp)png_malloc_warn(png_ptr, png_strlen(ep) + 1);
  1564. if (swidth == NULL)
  1565. {
  1566. png_warning(png_ptr, "Out of memory while processing sCAL chunk height");
  1567. return;
  1568. }
  1569. png_memcpy(sheight, ep, (png_size_t)png_strlen(ep));
  1570. #endif
  1571. #endif
  1572. if (buffer + slength < ep
  1573. #ifdef PNG_FLOATING_POINT_SUPPORTED
  1574. || width <= 0. || height <= 0.
  1575. #endif
  1576. )
  1577. {
  1578. png_warning(png_ptr, "Invalid sCAL data");
  1579. png_free(png_ptr, buffer);
  1580. #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
  1581. png_free(png_ptr, swidth);
  1582. png_free(png_ptr, sheight);
  1583. #endif
  1584. return;
  1585. }
  1586. #ifdef PNG_FLOATING_POINT_SUPPORTED
  1587. png_set_sCAL(png_ptr, info_ptr, buffer[0], width, height);
  1588. #else
  1589. #ifdef PNG_FIXED_POINT_SUPPORTED
  1590. png_set_sCAL_s(png_ptr, info_ptr, buffer[0], swidth, sheight);
  1591. #endif
  1592. #endif
  1593. png_free(png_ptr, buffer);
  1594. #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
  1595. png_free(png_ptr, swidth);
  1596. png_free(png_ptr, sheight);
  1597. #endif
  1598. }
  1599. #endif
  1600. #if defined(PNG_READ_tIME_SUPPORTED)
  1601. void /* PRIVATE */
  1602. png_handle_tIME(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1603. {
  1604. png_byte buf[7];
  1605. png_time mod_time;
  1606. png_debug(1, "in png_handle_tIME\n");
  1607. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1608. png_error(png_ptr, "Out of place tIME chunk");
  1609. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME))
  1610. {
  1611. png_warning(png_ptr, "Duplicate tIME chunk");
  1612. png_crc_finish(png_ptr, length);
  1613. return;
  1614. }
  1615. if (png_ptr->mode & PNG_HAVE_IDAT)
  1616. png_ptr->mode |= PNG_AFTER_IDAT;
  1617. if (length != 7)
  1618. {
  1619. png_warning(png_ptr, "Incorrect tIME chunk length");
  1620. png_crc_finish(png_ptr, length);
  1621. return;
  1622. }
  1623. png_crc_read(png_ptr, buf, 7);
  1624. if (png_crc_finish(png_ptr, 0))
  1625. return;
  1626. mod_time.second = buf[6];
  1627. mod_time.minute = buf[5];
  1628. mod_time.hour = buf[4];
  1629. mod_time.day = buf[3];
  1630. mod_time.month = buf[2];
  1631. mod_time.year = png_get_uint_16(buf);
  1632. png_set_tIME(png_ptr, info_ptr, &mod_time);
  1633. }
  1634. #endif
  1635. #if defined(PNG_READ_tEXt_SUPPORTED)
  1636. /* Note: this does not properly handle chunks that are > 64K under DOS */
  1637. void /* PRIVATE */
  1638. png_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1639. {
  1640. png_textp text_ptr;
  1641. png_charp key;
  1642. png_charp text;
  1643. png_uint_32 skip = 0;
  1644. png_size_t slength;
  1645. int ret;
  1646. png_debug(1, "in png_handle_tEXt\n");
  1647. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1648. png_error(png_ptr, "Missing IHDR before tEXt");
  1649. if (png_ptr->mode & PNG_HAVE_IDAT)
  1650. png_ptr->mode |= PNG_AFTER_IDAT;
  1651. #ifdef PNG_MAX_MALLOC_64K
  1652. if (length > (png_uint_32)65535L)
  1653. {
  1654. png_warning(png_ptr, "tEXt chunk too large to fit in memory");
  1655. skip = length - (png_uint_32)65535L;
  1656. length = (png_uint_32)65535L;
  1657. }
  1658. #endif
  1659. key = (png_charp)png_malloc_warn(png_ptr, length + 1);
  1660. if (key == NULL)
  1661. {
  1662. png_warning(png_ptr, "No memory to process text chunk.");
  1663. return;
  1664. }
  1665. slength = (png_size_t)length;
  1666. png_crc_read(png_ptr, (png_bytep)key, slength);
  1667. if (png_crc_finish(png_ptr, skip))
  1668. {
  1669. png_free(png_ptr, key);
  1670. return;
  1671. }
  1672. key[slength] = 0x00;
  1673. for (text = key; *text; text++)
  1674. /* empty loop to find end of key */ ;
  1675. if (text != key + slength)
  1676. text++;
  1677. text_ptr = (png_textp)png_malloc_warn(png_ptr,
  1678. (png_uint_32)png_sizeof(png_text));
  1679. if (text_ptr == NULL)
  1680. {
  1681. png_warning(png_ptr, "Not enough memory to process text chunk.");
  1682. png_free(png_ptr, key);
  1683. return;
  1684. }
  1685. text_ptr->compression = PNG_TEXT_COMPRESSION_NONE;
  1686. text_ptr->key = key;
  1687. #ifdef PNG_iTXt_SUPPORTED
  1688. text_ptr->lang = NULL;
  1689. text_ptr->lang_key = NULL;
  1690. text_ptr->itxt_length = 0;
  1691. #endif
  1692. text_ptr->text = text;
  1693. text_ptr->text_length = png_strlen(text);
  1694. ret=png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
  1695. png_free(png_ptr, key);
  1696. png_free(png_ptr, text_ptr);
  1697. if (ret)
  1698. png_warning(png_ptr, "Insufficient memory to process text chunk.");
  1699. }
  1700. #endif
  1701. #if defined(PNG_READ_zTXt_SUPPORTED)
  1702. /* note: this does not correctly handle chunks that are > 64K under DOS */
  1703. void /* PRIVATE */
  1704. png_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1705. {
  1706. png_textp text_ptr;
  1707. png_charp chunkdata;
  1708. png_charp text;
  1709. int comp_type;
  1710. int ret;
  1711. png_size_t slength, prefix_len, data_len;
  1712. png_debug(1, "in png_handle_zTXt\n");
  1713. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1714. png_error(png_ptr, "Missing IHDR before zTXt");
  1715. if (png_ptr->mode & PNG_HAVE_IDAT)
  1716. png_ptr->mode |= PNG_AFTER_IDAT;
  1717. #ifdef PNG_MAX_MALLOC_64K
  1718. /* We will no doubt have problems with chunks even half this size, but
  1719. there is no hard and fast rule to tell us where to stop. */
  1720. if (length > (png_uint_32)65535L)
  1721. {
  1722. png_warning(png_ptr,"zTXt chunk too large to fit in memory");
  1723. png_crc_finish(png_ptr, length);
  1724. return;
  1725. }
  1726. #endif
  1727. chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
  1728. if (chunkdata == NULL)
  1729. {
  1730. png_warning(png_ptr,"Out of memory processing zTXt chunk.");
  1731. return;
  1732. }
  1733. slength = (png_size_t)length;
  1734. png_crc_read(png_ptr, (png_bytep)chunkdata, slength);
  1735. if (png_crc_finish(png_ptr, 0))
  1736. {
  1737. png_free(png_ptr, chunkdata);
  1738. return;
  1739. }
  1740. chunkdata[slength] = 0x00;
  1741. for (text = chunkdata; *text; text++)
  1742. /* empty loop */ ;
  1743. /* zTXt must have some text after the chunkdataword */
  1744. if (text == chunkdata + slength)
  1745. {
  1746. comp_type = PNG_TEXT_COMPRESSION_NONE;
  1747. png_warning(png_ptr, "Zero length zTXt chunk");
  1748. }
  1749. else
  1750. {
  1751. comp_type = *(++text);
  1752. if (comp_type != PNG_TEXT_COMPRESSION_zTXt)
  1753. {
  1754. png_warning(png_ptr, "Unknown compression type in zTXt chunk");
  1755. comp_type = PNG_TEXT_COMPRESSION_zTXt;
  1756. }
  1757. text++; /* skip the compression_method byte */
  1758. }
  1759. prefix_len = text - chunkdata;
  1760. chunkdata = (png_charp)png_decompress_chunk(png_ptr, comp_type, chunkdata,
  1761. (png_size_t)length, prefix_len, &data_len);
  1762. text_ptr = (png_textp)png_malloc_warn(png_ptr,
  1763. (png_uint_32)png_sizeof(png_text));
  1764. if (text_ptr == NULL)
  1765. {
  1766. png_warning(png_ptr,"Not enough memory to process zTXt chunk.");
  1767. png_free(png_ptr, chunkdata);
  1768. return;
  1769. }
  1770. text_ptr->compression = comp_type;
  1771. text_ptr->key = chunkdata;
  1772. #ifdef PNG_iTXt_SUPPORTED
  1773. text_ptr->lang = NULL;
  1774. text_ptr->lang_key = NULL;
  1775. text_ptr->itxt_length = 0;
  1776. #endif
  1777. text_ptr->text = chunkdata + prefix_len;
  1778. text_ptr->text_length = data_len;
  1779. ret=png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
  1780. png_free(png_ptr, text_ptr);
  1781. png_free(png_ptr, chunkdata);
  1782. if (ret)
  1783. png_error(png_ptr, "Insufficient memory to store zTXt chunk.");
  1784. }
  1785. #endif
  1786. #if defined(PNG_READ_iTXt_SUPPORTED)
  1787. /* note: this does not correctly handle chunks that are > 64K under DOS */
  1788. void /* PRIVATE */
  1789. png_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1790. {
  1791. png_textp text_ptr;
  1792. png_charp chunkdata;
  1793. png_charp key, lang, text, lang_key;
  1794. int comp_flag;
  1795. int comp_type = 0;
  1796. int ret;
  1797. png_size_t slength, prefix_len, data_len;
  1798. png_debug(1, "in png_handle_iTXt\n");
  1799. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1800. png_error(png_ptr, "Missing IHDR before iTXt");
  1801. if (png_ptr->mode & PNG_HAVE_IDAT)
  1802. png_ptr->mode |= PNG_AFTER_IDAT;
  1803. #ifdef PNG_MAX_MALLOC_64K
  1804. /* We will no doubt have problems with chunks even half this size, but
  1805. there is no hard and fast rule to tell us where to stop. */
  1806. if (length > (png_uint_32)65535L)
  1807. {
  1808. png_warning(png_ptr,"iTXt chunk too large to fit in memory");
  1809. png_crc_finish(png_ptr, length);
  1810. return;
  1811. }
  1812. #endif
  1813. chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
  1814. if (chunkdata == NULL)
  1815. {
  1816. png_warning(png_ptr, "No memory to process iTXt chunk.");
  1817. return;
  1818. }
  1819. slength = (png_size_t)length;
  1820. png_crc_read(png_ptr, (png_bytep)chunkdata, slength);
  1821. if (png_crc_finish(png_ptr, 0))
  1822. {
  1823. png_free(png_ptr, chunkdata);
  1824. return;
  1825. }
  1826. chunkdata[slength] = 0x00;
  1827. for (lang = chunkdata; *lang; lang++)
  1828. /* empty loop */ ;
  1829. lang++; /* skip NUL separator */
  1830. /* iTXt must have a language tag (possibly empty), two compression bytes,
  1831. translated keyword (possibly empty), and possibly some text after the
  1832. keyword */
  1833. if (lang >= chunkdata + slength)
  1834. {
  1835. comp_flag = PNG_TEXT_COMPRESSION_NONE;
  1836. png_warning(png_ptr, "Zero length iTXt chunk");
  1837. }
  1838. else
  1839. {
  1840. comp_flag = *lang++;
  1841. comp_type = *lang++;
  1842. }
  1843. for (lang_key = lang; *lang_key; lang_key++)
  1844. /* empty loop */ ;
  1845. lang_key++; /* skip NUL separator */
  1846. for (text = lang_key; *text; text++)
  1847. /* empty loop */ ;
  1848. text++; /* skip NUL separator */
  1849. prefix_len = text - chunkdata;
  1850. key=chunkdata;
  1851. if (comp_flag)
  1852. chunkdata = png_decompress_chunk(png_ptr, comp_type, chunkdata,
  1853. (size_t)length, prefix_len, &data_len);
  1854. else
  1855. data_len=png_strlen(chunkdata + prefix_len);
  1856. text_ptr = (png_textp)png_malloc_warn(png_ptr,
  1857. (png_uint_32)png_sizeof(png_text));
  1858. if (text_ptr == NULL)
  1859. {
  1860. png_warning(png_ptr,"Not enough memory to process iTXt chunk.");
  1861. png_free(png_ptr, chunkdata);
  1862. return;
  1863. }
  1864. text_ptr->compression = (int)comp_flag + 1;
  1865. text_ptr->lang_key = chunkdata+(lang_key-key);
  1866. text_ptr->lang = chunkdata+(lang-key);
  1867. text_ptr->itxt_length = data_len;
  1868. text_ptr->text_length = 0;
  1869. text_ptr->key = chunkdata;
  1870. text_ptr->text = chunkdata + prefix_len;
  1871. ret=png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
  1872. png_free(png_ptr, text_ptr);
  1873. png_free(png_ptr, chunkdata);
  1874. if (ret)
  1875. png_error(png_ptr, "Insufficient memory to store iTXt chunk.");
  1876. }
  1877. #endif
  1878. /* This function is called when we haven't found a handler for a
  1879. chunk. If there isn't a problem with the chunk itself (ie bad
  1880. chunk name, CRC, or a critical chunk), the chunk is silently ignored
  1881. -- unless the PNG_FLAG_UNKNOWN_CHUNKS_SUPPORTED flag is on in which
  1882. case it will be saved away to be written out later. */
  1883. void /* PRIVATE */
  1884. png_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1885. {
  1886. png_uint_32 skip = 0;
  1887. png_debug(1, "in png_handle_unknown\n");
  1888. if (png_ptr->mode & PNG_HAVE_IDAT)
  1889. {
  1890. #ifdef PNG_USE_LOCAL_ARRAYS
  1891. PNG_IDAT;
  1892. #endif
  1893. if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) /* not an IDAT */
  1894. png_ptr->mode |= PNG_AFTER_IDAT;
  1895. }
  1896. png_check_chunk_name(png_ptr, png_ptr->chunk_name);
  1897. if (!(png_ptr->chunk_name[0] & 0x20))
  1898. {
  1899. #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
  1900. if(png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
  1901. PNG_HANDLE_CHUNK_ALWAYS
  1902. #if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
  1903. && png_ptr->read_user_chunk_fn == NULL
  1904. #endif
  1905. )
  1906. #endif
  1907. png_chunk_error(png_ptr, "unknown critical chunk");
  1908. }
  1909. #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
  1910. if (png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS)
  1911. {
  1912. png_unknown_chunk chunk;
  1913. #ifdef PNG_MAX_MALLOC_64K
  1914. if (length > (png_uint_32)65535L)
  1915. {
  1916. png_warning(png_ptr, "unknown chunk too large to fit in memory");
  1917. skip = length - (png_uint_32)65535L;
  1918. length = (png_uint_32)65535L;
  1919. }
  1920. #endif
  1921. png_strcpy((png_charp)chunk.name, (png_charp)png_ptr->chunk_name);
  1922. chunk.data = (png_bytep)png_malloc(png_ptr, length);
  1923. chunk.size = (png_size_t)length;
  1924. png_crc_read(png_ptr, (png_bytep)chunk.data, length);
  1925. #if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
  1926. if(png_ptr->read_user_chunk_fn != NULL)
  1927. {
  1928. /* callback to user unknown chunk handler */
  1929. if ((*(png_ptr->read_user_chunk_fn)) (png_ptr, &chunk) <= 0)
  1930. {
  1931. if (!(png_ptr->chunk_name[0] & 0x20))
  1932. if(png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
  1933. PNG_HANDLE_CHUNK_ALWAYS)
  1934. {
  1935. png_free(png_ptr, chunk.data);
  1936. png_chunk_error(png_ptr, "unknown critical chunk");
  1937. }
  1938. png_set_unknown_chunks(png_ptr, info_ptr, &chunk, 1);
  1939. }
  1940. }
  1941. else
  1942. #endif
  1943. png_set_unknown_chunks(png_ptr, info_ptr, &chunk, 1);
  1944. png_free(png_ptr, chunk.data);
  1945. }
  1946. else
  1947. #endif
  1948. skip = length;
  1949. png_crc_finish(png_ptr, skip);
  1950. #if !defined(PNG_READ_USER_CHUNKS_SUPPORTED)
  1951. if (&info_ptr == NULL) /* quiet compiler warnings about unused info_ptr */
  1952. return;
  1953. #endif
  1954. }
  1955. /* This function is called to verify that a chunk name is valid.
  1956. This function can't have the "critical chunk check" incorporated
  1957. into it, since in the future we will need to be able to call user
  1958. functions to handle unknown critical chunks after we check that
  1959. the chunk name itself is valid. */
  1960. #define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
  1961. void /* PRIVATE */
  1962. png_check_chunk_name(png_structp png_ptr, png_bytep chunk_name)
  1963. {
  1964. png_debug(1, "in png_check_chunk_name\n");
  1965. if (isnonalpha(chunk_name[0]) || isnonalpha(chunk_name[1]) ||
  1966. isnonalpha(chunk_name[2]) || isnonalpha(chunk_name[3]))
  1967. {
  1968. png_chunk_error(png_ptr, "invalid chunk type");
  1969. }
  1970. }
  1971. /* Combines the row recently read in with the existing pixels in the
  1972. row. This routine takes care of alpha and transparency if requested.
  1973. This routine also handles the two methods of progressive display
  1974. of interlaced images, depending on the mask value.
  1975. The mask value describes which pixels are to be combined with
  1976. the row. The pattern always repeats every 8 pixels, so just 8
  1977. bits are needed. A one indicates the pixel is to be combined,
  1978. a zero indicates the pixel is to be skipped. This is in addition
  1979. to any alpha or transparency value associated with the pixel. If
  1980. you want all pixels to be combined, pass 0xff (255) in mask. */
  1981. #ifndef PNG_HAVE_ASSEMBLER_COMBINE_ROW
  1982. void /* PRIVATE */
  1983. png_combine_row(png_structp png_ptr, png_bytep row, int mask)
  1984. {
  1985. png_debug(1,"in png_combine_row\n");
  1986. if (mask == 0xff)
  1987. {
  1988. png_memcpy(row, png_ptr->row_buf + 1,
  1989. PNG_ROWBYTES(png_ptr->row_info.pixel_depth, png_ptr->width));
  1990. }
  1991. else
  1992. {
  1993. switch (png_ptr->row_info.pixel_depth)
  1994. {
  1995. case 1:
  1996. {
  1997. png_bytep sp = png_ptr->row_buf + 1;
  1998. png_bytep dp = row;
  1999. int s_inc, s_start, s_end;
  2000. int m = 0x80;
  2001. int shift;
  2002. png_uint_32 i;
  2003. png_uint_32 row_width = png_ptr->width;
  2004. #if defined(PNG_READ_PACKSWAP_SUPPORTED)
  2005. if (png_ptr->transformations & PNG_PACKSWAP)
  2006. {
  2007. s_start = 0;
  2008. s_end = 7;
  2009. s_inc = 1;
  2010. }
  2011. else
  2012. #endif
  2013. {
  2014. s_start = 7;
  2015. s_end = 0;
  2016. s_inc = -1;
  2017. }
  2018. shift = s_start;
  2019. for (i = 0; i < row_width; i++)
  2020. {
  2021. if (m & mask)
  2022. {
  2023. int value;
  2024. value = (*sp >> shift) & 0x01;
  2025. *dp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);
  2026. *dp |= (png_byte)(value << shift);
  2027. }
  2028. if (shift == s_end)
  2029. {
  2030. shift = s_start;
  2031. sp++;
  2032. dp++;
  2033. }
  2034. else
  2035. shift += s_inc;
  2036. if (m == 1)
  2037. m = 0x80;
  2038. else
  2039. m >>= 1;
  2040. }
  2041. break;
  2042. }
  2043. case 2:
  2044. {
  2045. png_bytep sp = png_ptr->row_buf + 1;
  2046. png_bytep dp = row;
  2047. int s_start, s_end, s_inc;
  2048. int m = 0x80;
  2049. int shift;
  2050. png_uint_32 i;
  2051. png_uint_32 row_width = png_ptr->width;
  2052. int value;
  2053. #if defined(PNG_READ_PACKSWAP_SUPPORTED)
  2054. if (png_ptr->transformations & PNG_PACKSWAP)
  2055. {
  2056. s_start = 0;
  2057. s_end = 6;
  2058. s_inc = 2;
  2059. }
  2060. else
  2061. #endif
  2062. {
  2063. s_start = 6;
  2064. s_end = 0;
  2065. s_inc = -2;
  2066. }
  2067. shift = s_start;
  2068. for (i = 0; i < row_width; i++)
  2069. {
  2070. if (m & mask)
  2071. {
  2072. value = (*sp >> shift) & 0x03;
  2073. *dp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
  2074. *dp |= (png_byte)(value << shift);
  2075. }
  2076. if (shift == s_end)
  2077. {
  2078. shift = s_start;
  2079. sp++;
  2080. dp++;
  2081. }
  2082. else
  2083. shift += s_inc;
  2084. if (m == 1)
  2085. m = 0x80;
  2086. else
  2087. m >>= 1;
  2088. }
  2089. break;
  2090. }
  2091. case 4:
  2092. {
  2093. png_bytep sp = png_ptr->row_buf + 1;
  2094. png_bytep dp = row;
  2095. int s_start, s_end, s_inc;
  2096. int m = 0x80;
  2097. int shift;
  2098. png_uint_32 i;
  2099. png_uint_32 row_width = png_ptr->width;
  2100. int value;
  2101. #if defined(PNG_READ_PACKSWAP_SUPPORTED)
  2102. if (png_ptr->transformations & PNG_PACKSWAP)
  2103. {
  2104. s_start = 0;
  2105. s_end = 4;
  2106. s_inc = 4;
  2107. }
  2108. else
  2109. #endif
  2110. {
  2111. s_start = 4;
  2112. s_end = 0;
  2113. s_inc = -4;
  2114. }
  2115. shift = s_start;
  2116. for (i = 0; i < row_width; i++)
  2117. {
  2118. if (m & mask)
  2119. {
  2120. value = (*sp >> shift) & 0xf;
  2121. *dp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
  2122. *dp |= (png_byte)(value << shift);
  2123. }
  2124. if (shift == s_end)
  2125. {
  2126. shift = s_start;
  2127. sp++;
  2128. dp++;
  2129. }
  2130. else
  2131. shift += s_inc;
  2132. if (m == 1)
  2133. m = 0x80;
  2134. else
  2135. m >>= 1;
  2136. }
  2137. break;
  2138. }
  2139. default:
  2140. {
  2141. png_bytep sp = png_ptr->row_buf + 1;
  2142. png_bytep dp = row;
  2143. png_size_t pixel_bytes = (png_ptr->row_info.pixel_depth >> 3);
  2144. png_uint_32 i;
  2145. png_uint_32 row_width = png_ptr->width;
  2146. png_byte m = 0x80;
  2147. for (i = 0; i < row_width; i++)
  2148. {
  2149. if (m & mask)
  2150. {
  2151. png_memcpy(dp, sp, pixel_bytes);
  2152. }
  2153. sp += pixel_bytes;
  2154. dp += pixel_bytes;
  2155. if (m == 1)
  2156. m = 0x80;
  2157. else
  2158. m >>= 1;
  2159. }
  2160. break;
  2161. }
  2162. }
  2163. }
  2164. }
  2165. #endif /* !PNG_HAVE_ASSEMBLER_COMBINE_ROW */
  2166. #ifdef PNG_READ_INTERLACING_SUPPORTED
  2167. #ifndef PNG_HAVE_ASSEMBLER_READ_INTERLACE /* else in pngvcrd.c, pnggccrd.c */
  2168. /* OLD pre-1.0.9 interface:
  2169. void png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
  2170. png_uint_32 transformations)
  2171. */
  2172. void /* PRIVATE */
  2173. png_do_read_interlace(png_structp png_ptr)
  2174. {
  2175. png_row_infop row_info = &(png_ptr->row_info);
  2176. png_bytep row = png_ptr->row_buf + 1;
  2177. int pass = png_ptr->pass;
  2178. png_uint_32 transformations = png_ptr->transformations;
  2179. #ifdef PNG_USE_LOCAL_ARRAYS
  2180. /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  2181. /* offset to next interlace block */
  2182. const int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
  2183. #endif
  2184. png_debug(1,"in png_do_read_interlace (stock C version)\n");
  2185. if (row != NULL && row_info != NULL)
  2186. {
  2187. png_uint_32 final_width;
  2188. final_width = row_info->width * png_pass_inc[pass];
  2189. switch (row_info->pixel_depth)
  2190. {
  2191. case 1:
  2192. {
  2193. png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 3);
  2194. png_bytep dp = row + (png_size_t)((final_width - 1) >> 3);
  2195. int sshift, dshift;
  2196. int s_start, s_end, s_inc;
  2197. int jstop = png_pass_inc[pass];
  2198. png_byte v;
  2199. png_uint_32 i;
  2200. int j;
  2201. #if defined(PNG_READ_PACKSWAP_SUPPORTED)
  2202. if (transformations & PNG_PACKSWAP)
  2203. {
  2204. sshift = (int)((row_info->width + 7) & 0x07);
  2205. dshift = (int)((final_width + 7) & 0x07);
  2206. s_start = 7;
  2207. s_end = 0;
  2208. s_inc = -1;
  2209. }
  2210. else
  2211. #endif
  2212. {
  2213. sshift = 7 - (int)((row_info->width + 7) & 0x07);
  2214. dshift = 7 - (int)((final_width + 7) & 0x07);
  2215. s_start = 0;
  2216. s_end = 7;
  2217. s_inc = 1;
  2218. }
  2219. for (i = 0; i < row_info->width; i++)
  2220. {
  2221. v = (png_byte)((*sp >> sshift) & 0x01);
  2222. for (j = 0; j < jstop; j++)
  2223. {
  2224. *dp &= (png_byte)((0x7f7f >> (7 - dshift)) & 0xff);
  2225. *dp |= (png_byte)(v << dshift);
  2226. if (dshift == s_end)
  2227. {
  2228. dshift = s_start;
  2229. dp--;
  2230. }
  2231. else
  2232. dshift += s_inc;
  2233. }
  2234. if (sshift == s_end)
  2235. {
  2236. sshift = s_start;
  2237. sp--;
  2238. }
  2239. else
  2240. sshift += s_inc;
  2241. }
  2242. break;
  2243. }
  2244. case 2:
  2245. {
  2246. png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2);
  2247. png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2);
  2248. int sshift, dshift;
  2249. int s_start, s_end, s_inc;
  2250. int jstop = png_pass_inc[pass];
  2251. png_uint_32 i;
  2252. #if defined(PNG_READ_PACKSWAP_SUPPORTED)
  2253. if (transformations & PNG_PACKSWAP)
  2254. {
  2255. sshift = (int)(((row_info->width + 3) & 0x03) << 1);
  2256. dshift = (int)(((final_width + 3) & 0x03) << 1);
  2257. s_start = 6;
  2258. s_end = 0;
  2259. s_inc = -2;
  2260. }
  2261. else
  2262. #endif
  2263. {
  2264. sshift = (int)((3 - ((row_info->width + 3) & 0x03)) << 1);
  2265. dshift = (int)((3 - ((final_width + 3) & 0x03)) << 1);
  2266. s_start = 0;
  2267. s_end = 6;
  2268. s_inc = 2;
  2269. }
  2270. for (i = 0; i < row_info->width; i++)
  2271. {
  2272. png_byte v;
  2273. int j;
  2274. v = (png_byte)((*sp >> sshift) & 0x03);
  2275. for (j = 0; j < jstop; j++)
  2276. {
  2277. *dp &= (png_byte)((0x3f3f >> (6 - dshift)) & 0xff);
  2278. *dp |= (png_byte)(v << dshift);
  2279. if (dshift == s_end)
  2280. {
  2281. dshift = s_start;
  2282. dp--;
  2283. }
  2284. else
  2285. dshift += s_inc;
  2286. }
  2287. if (sshift == s_end)
  2288. {
  2289. sshift = s_start;
  2290. sp--;
  2291. }
  2292. else
  2293. sshift += s_inc;
  2294. }
  2295. break;
  2296. }
  2297. case 4:
  2298. {
  2299. png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 1);
  2300. png_bytep dp = row + (png_size_t)((final_width - 1) >> 1);
  2301. int sshift, dshift;
  2302. int s_start, s_end, s_inc;
  2303. png_uint_32 i;
  2304. int jstop = png_pass_inc[pass];
  2305. #if defined(PNG_READ_PACKSWAP_SUPPORTED)
  2306. if (transformations & PNG_PACKSWAP)
  2307. {
  2308. sshift = (int)(((row_info->width + 1) & 0x01) << 2);
  2309. dshift = (int)(((final_width + 1) & 0x01) << 2);
  2310. s_start = 4;
  2311. s_end = 0;
  2312. s_inc = -4;
  2313. }
  2314. else
  2315. #endif
  2316. {
  2317. sshift = (int)((1 - ((row_info->width + 1) & 0x01)) << 2);
  2318. dshift = (int)((1 - ((final_width + 1) & 0x01)) << 2);
  2319. s_start = 0;
  2320. s_end = 4;
  2321. s_inc = 4;
  2322. }
  2323. for (i = 0; i < row_info->width; i++)
  2324. {
  2325. png_byte v = (png_byte)((*sp >> sshift) & 0xf);
  2326. int j;
  2327. for (j = 0; j < jstop; j++)
  2328. {
  2329. *dp &= (png_byte)((0xf0f >> (4 - dshift)) & 0xff);
  2330. *dp |= (png_byte)(v << dshift);
  2331. if (dshift == s_end)
  2332. {
  2333. dshift = s_start;
  2334. dp--;
  2335. }
  2336. else
  2337. dshift += s_inc;
  2338. }
  2339. if (sshift == s_end)
  2340. {
  2341. sshift = s_start;
  2342. sp--;
  2343. }
  2344. else
  2345. sshift += s_inc;
  2346. }
  2347. break;
  2348. }
  2349. default:
  2350. {
  2351. png_size_t pixel_bytes = (row_info->pixel_depth >> 3);
  2352. png_bytep sp = row + (png_size_t)(row_info->width - 1) * pixel_bytes;
  2353. png_bytep dp = row + (png_size_t)(final_width - 1) * pixel_bytes;
  2354. int jstop = png_pass_inc[pass];
  2355. png_uint_32 i;
  2356. for (i = 0; i < row_info->width; i++)
  2357. {
  2358. png_byte v[8];
  2359. int j;
  2360. png_memcpy(v, sp, pixel_bytes);
  2361. for (j = 0; j < jstop; j++)
  2362. {
  2363. png_memcpy(dp, v, pixel_bytes);
  2364. dp -= pixel_bytes;
  2365. }
  2366. sp -= pixel_bytes;
  2367. }
  2368. break;
  2369. }
  2370. }
  2371. row_info->width = final_width;
  2372. row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,final_width);
  2373. }
  2374. #if !defined(PNG_READ_PACKSWAP_SUPPORTED)
  2375. if (&transformations == NULL) /* silence compiler warning */
  2376. return;
  2377. #endif
  2378. }
  2379. #endif /* !PNG_HAVE_ASSEMBLER_READ_INTERLACE */
  2380. #endif /* PNG_READ_INTERLACING_SUPPORTED */
  2381. #ifndef PNG_HAVE_ASSEMBLER_READ_FILTER_ROW
  2382. void /* PRIVATE */
  2383. png_read_filter_row(png_structp png_ptr, png_row_infop row_info, png_bytep row,
  2384. png_bytep prev_row, int filter)
  2385. {
  2386. png_debug(1, "in png_read_filter_row\n");
  2387. png_debug2(2,"row = %lu, filter = %d\n", png_ptr->row_number, filter);
  2388. switch (filter)
  2389. {
  2390. case PNG_FILTER_VALUE_NONE:
  2391. break;
  2392. case PNG_FILTER_VALUE_SUB:
  2393. {
  2394. png_uint_32 i;
  2395. png_uint_32 istop = row_info->rowbytes;
  2396. png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3;
  2397. png_bytep rp = row + bpp;
  2398. png_bytep lp = row;
  2399. for (i = bpp; i < istop; i++)
  2400. {
  2401. *rp = (png_byte)(((int)(*rp) + (int)(*lp++)) & 0xff);
  2402. rp++;
  2403. }
  2404. break;
  2405. }
  2406. case PNG_FILTER_VALUE_UP:
  2407. {
  2408. png_uint_32 i;
  2409. png_uint_32 istop = row_info->rowbytes;
  2410. png_bytep rp = row;
  2411. png_bytep pp = prev_row;
  2412. for (i = 0; i < istop; i++)
  2413. {
  2414. *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
  2415. rp++;
  2416. }
  2417. break;
  2418. }
  2419. case PNG_FILTER_VALUE_AVG:
  2420. {
  2421. png_uint_32 i;
  2422. png_bytep rp = row;
  2423. png_bytep pp = prev_row;
  2424. png_bytep lp = row;
  2425. png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3;
  2426. png_uint_32 istop = row_info->rowbytes - bpp;
  2427. for (i = 0; i < bpp; i++)
  2428. {
  2429. *rp = (png_byte)(((int)(*rp) +
  2430. ((int)(*pp++) / 2 )) & 0xff);
  2431. rp++;
  2432. }
  2433. for (i = 0; i < istop; i++)
  2434. {
  2435. *rp = (png_byte)(((int)(*rp) +
  2436. (int)(*pp++ + *lp++) / 2 ) & 0xff);
  2437. rp++;
  2438. }
  2439. break;
  2440. }
  2441. case PNG_FILTER_VALUE_PAETH:
  2442. {
  2443. png_uint_32 i;
  2444. png_bytep rp = row;
  2445. png_bytep pp = prev_row;
  2446. png_bytep lp = row;
  2447. png_bytep cp = prev_row;
  2448. png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3;
  2449. png_uint_32 istop=row_info->rowbytes - bpp;
  2450. for (i = 0; i < bpp; i++)
  2451. {
  2452. *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
  2453. rp++;
  2454. }
  2455. for (i = 0; i < istop; i++) /* use leftover rp,pp */
  2456. {
  2457. int a, b, c, pa, pb, pc, p;
  2458. a = *lp++;
  2459. b = *pp++;
  2460. c = *cp++;
  2461. p = b - c;
  2462. pc = a - c;
  2463. #ifdef PNG_USE_ABS
  2464. pa = abs(p);
  2465. pb = abs(pc);
  2466. pc = abs(p + pc);
  2467. #else
  2468. pa = p < 0 ? -p : p;
  2469. pb = pc < 0 ? -pc : pc;
  2470. pc = (p + pc) < 0 ? -(p + pc) : p + pc;
  2471. #endif
  2472. /*
  2473. if (pa <= pb && pa <= pc)
  2474. p = a;
  2475. else if (pb <= pc)
  2476. p = b;
  2477. else
  2478. p = c;
  2479. */
  2480. p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
  2481. *rp = (png_byte)(((int)(*rp) + p) & 0xff);
  2482. rp++;
  2483. }
  2484. break;
  2485. }
  2486. default:
  2487. png_warning(png_ptr, "Ignoring bad adaptive filter type");
  2488. *row=0;
  2489. break;
  2490. }
  2491. }
  2492. #endif /* !PNG_HAVE_ASSEMBLER_READ_FILTER_ROW */
  2493. void /* PRIVATE */
  2494. png_read_finish_row(png_structp png_ptr)
  2495. {
  2496. #ifdef PNG_USE_LOCAL_ARRAYS
  2497. /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  2498. /* start of interlace block */
  2499. const int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
  2500. /* offset to next interlace block */
  2501. const int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
  2502. /* start of interlace block in the y direction */
  2503. const int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
  2504. /* offset to next interlace block in the y direction */
  2505. const int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
  2506. #endif
  2507. png_debug(1, "in png_read_finish_row\n");
  2508. png_ptr->row_number++;
  2509. if (png_ptr->row_number < png_ptr->num_rows)
  2510. return;
  2511. if (png_ptr->interlaced)
  2512. {
  2513. png_ptr->row_number = 0;
  2514. png_memset_check(png_ptr, png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
  2515. do
  2516. {
  2517. png_ptr->pass++;
  2518. if (png_ptr->pass >= 7)
  2519. break;
  2520. png_ptr->iwidth = (png_ptr->width +
  2521. png_pass_inc[png_ptr->pass] - 1 -
  2522. png_pass_start[png_ptr->pass]) /
  2523. png_pass_inc[png_ptr->pass];
  2524. png_ptr->irowbytes = PNG_ROWBYTES(png_ptr->pixel_depth,
  2525. png_ptr->iwidth) + 1;
  2526. if (!(png_ptr->transformations & PNG_INTERLACE))
  2527. {
  2528. png_ptr->num_rows = (png_ptr->height +
  2529. png_pass_yinc[png_ptr->pass] - 1 -
  2530. png_pass_ystart[png_ptr->pass]) /
  2531. png_pass_yinc[png_ptr->pass];
  2532. if (!(png_ptr->num_rows))
  2533. continue;
  2534. }
  2535. else /* if (png_ptr->transformations & PNG_INTERLACE) */
  2536. break;
  2537. } while (png_ptr->iwidth == 0);
  2538. if (png_ptr->pass < 7)
  2539. return;
  2540. }
  2541. if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
  2542. {
  2543. #ifdef PNG_USE_LOCAL_ARRAYS
  2544. PNG_IDAT;
  2545. #endif
  2546. char extra;
  2547. int ret;
  2548. png_ptr->zstream.next_out = (Byte *)&extra;
  2549. png_ptr->zstream.avail_out = (uInt)1;
  2550. for(;;)
  2551. {
  2552. if (!(png_ptr->zstream.avail_in))
  2553. {
  2554. while (!png_ptr->idat_size)
  2555. {
  2556. png_byte chunk_length[4];
  2557. png_crc_finish(png_ptr, 0);
  2558. png_read_data(png_ptr, chunk_length, 4);
  2559. png_ptr->idat_size = png_get_uint_31(png_ptr, chunk_length);
  2560. png_reset_crc(png_ptr);
  2561. png_crc_read(png_ptr, png_ptr->chunk_name, 4);
  2562. if (png_memcmp(png_ptr->chunk_name, (png_bytep)png_IDAT, 4))
  2563. png_error(png_ptr, "Not enough image data");
  2564. }
  2565. png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
  2566. png_ptr->zstream.next_in = png_ptr->zbuf;
  2567. if (png_ptr->zbuf_size > png_ptr->idat_size)
  2568. png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
  2569. png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zstream.avail_in);
  2570. png_ptr->idat_size -= png_ptr->zstream.avail_in;
  2571. }
  2572. ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
  2573. if (ret == Z_STREAM_END)
  2574. {
  2575. if (!(png_ptr->zstream.avail_out) || png_ptr->zstream.avail_in ||
  2576. png_ptr->idat_size)
  2577. png_warning(png_ptr, "Extra compressed data");
  2578. png_ptr->mode |= PNG_AFTER_IDAT;
  2579. png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
  2580. break;
  2581. }
  2582. if (ret != Z_OK)
  2583. png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
  2584. "Decompression Error");
  2585. if (!(png_ptr->zstream.avail_out))
  2586. {
  2587. png_warning(png_ptr, "Extra compressed data.");
  2588. png_ptr->mode |= PNG_AFTER_IDAT;
  2589. png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
  2590. break;
  2591. }
  2592. }
  2593. png_ptr->zstream.avail_out = 0;
  2594. }
  2595. if (png_ptr->idat_size || png_ptr->zstream.avail_in)
  2596. png_warning(png_ptr, "Extra compression data");
  2597. inflateReset(&png_ptr->zstream);
  2598. png_ptr->mode |= PNG_AFTER_IDAT;
  2599. }
  2600. void /* PRIVATE */
  2601. png_read_start_row(png_structp png_ptr)
  2602. {
  2603. #ifdef PNG_USE_LOCAL_ARRAYS
  2604. /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  2605. /* start of interlace block */
  2606. const int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
  2607. /* offset to next interlace block */
  2608. const int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
  2609. /* start of interlace block in the y direction */
  2610. const int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
  2611. /* offset to next interlace block in the y direction */
  2612. const int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
  2613. #endif
  2614. int max_pixel_depth;
  2615. png_uint_32 row_bytes;
  2616. png_debug(1, "in png_read_start_row\n");
  2617. png_ptr->zstream.avail_in = 0;
  2618. png_init_read_transformations(png_ptr);
  2619. if (png_ptr->interlaced)
  2620. {
  2621. if (!(png_ptr->transformations & PNG_INTERLACE))
  2622. png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
  2623. png_pass_ystart[0]) / png_pass_yinc[0];
  2624. else
  2625. png_ptr->num_rows = png_ptr->height;
  2626. png_ptr->iwidth = (png_ptr->width +
  2627. png_pass_inc[png_ptr->pass] - 1 -
  2628. png_pass_start[png_ptr->pass]) /
  2629. png_pass_inc[png_ptr->pass];
  2630. row_bytes = PNG_ROWBYTES(png_ptr->pixel_depth,png_ptr->iwidth) + 1;
  2631. png_ptr->irowbytes = (png_size_t)row_bytes;
  2632. if((png_uint_32)png_ptr->irowbytes != row_bytes)
  2633. png_error(png_ptr, "Rowbytes overflow in png_read_start_row");
  2634. }
  2635. else
  2636. {
  2637. png_ptr->num_rows = png_ptr->height;
  2638. png_ptr->iwidth = png_ptr->width;
  2639. png_ptr->irowbytes = png_ptr->rowbytes + 1;
  2640. }
  2641. max_pixel_depth = png_ptr->pixel_depth;
  2642. #if defined(PNG_READ_PACK_SUPPORTED)
  2643. if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8)
  2644. max_pixel_depth = 8;
  2645. #endif
  2646. #if defined(PNG_READ_EXPAND_SUPPORTED)
  2647. if (png_ptr->transformations & PNG_EXPAND)
  2648. {
  2649. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  2650. {
  2651. if (png_ptr->num_trans)
  2652. max_pixel_depth = 32;
  2653. else
  2654. max_pixel_depth = 24;
  2655. }
  2656. else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
  2657. {
  2658. if (max_pixel_depth < 8)
  2659. max_pixel_depth = 8;
  2660. if (png_ptr->num_trans)
  2661. max_pixel_depth *= 2;
  2662. }
  2663. else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
  2664. {
  2665. if (png_ptr->num_trans)
  2666. {
  2667. max_pixel_depth *= 4;
  2668. max_pixel_depth /= 3;
  2669. }
  2670. }
  2671. }
  2672. #endif
  2673. #if defined(PNG_READ_FILLER_SUPPORTED)
  2674. if (png_ptr->transformations & (PNG_FILLER))
  2675. {
  2676. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  2677. max_pixel_depth = 32;
  2678. else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
  2679. {
  2680. if (max_pixel_depth <= 8)
  2681. max_pixel_depth = 16;
  2682. else
  2683. max_pixel_depth = 32;
  2684. }
  2685. else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
  2686. {
  2687. if (max_pixel_depth <= 32)
  2688. max_pixel_depth = 32;
  2689. else
  2690. max_pixel_depth = 64;
  2691. }
  2692. }
  2693. #endif
  2694. #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
  2695. if (png_ptr->transformations & PNG_GRAY_TO_RGB)
  2696. {
  2697. if (
  2698. #if defined(PNG_READ_EXPAND_SUPPORTED)
  2699. (png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) ||
  2700. #endif
  2701. #if defined(PNG_READ_FILLER_SUPPORTED)
  2702. (png_ptr->transformations & (PNG_FILLER)) ||
  2703. #endif
  2704. png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  2705. {
  2706. if (max_pixel_depth <= 16)
  2707. max_pixel_depth = 32;
  2708. else
  2709. max_pixel_depth = 64;
  2710. }
  2711. else
  2712. {
  2713. if (max_pixel_depth <= 8)
  2714. {
  2715. if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  2716. max_pixel_depth = 32;
  2717. else
  2718. max_pixel_depth = 24;
  2719. }
  2720. else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  2721. max_pixel_depth = 64;
  2722. else
  2723. max_pixel_depth = 48;
  2724. }
  2725. }
  2726. #endif
  2727. #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
  2728. defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
  2729. if(png_ptr->transformations & PNG_USER_TRANSFORM)
  2730. {
  2731. int user_pixel_depth=png_ptr->user_transform_depth*
  2732. png_ptr->user_transform_channels;
  2733. if(user_pixel_depth > max_pixel_depth)
  2734. max_pixel_depth=user_pixel_depth;
  2735. }
  2736. #endif
  2737. /* align the width on the next larger 8 pixels. Mainly used
  2738. for interlacing */
  2739. row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7));
  2740. /* calculate the maximum bytes needed, adding a byte and a pixel
  2741. for safety's sake */
  2742. row_bytes = PNG_ROWBYTES(max_pixel_depth,row_bytes) +
  2743. 1 + ((max_pixel_depth + 7) >> 3);
  2744. #ifdef PNG_MAX_MALLOC_64K
  2745. if (row_bytes > (png_uint_32)65536L)
  2746. png_error(png_ptr, "This image requires a row greater than 64KB");
  2747. #endif
  2748. png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes+64);
  2749. png_ptr->row_buf = png_ptr->big_row_buf+32;
  2750. #if defined(PNG_DEBUG) && defined(PNG_USE_PNGGCCRD)
  2751. png_ptr->row_buf_size = row_bytes;
  2752. #endif
  2753. #ifdef PNG_MAX_MALLOC_64K
  2754. if ((png_uint_32)png_ptr->rowbytes + 1 > (png_uint_32)65536L)
  2755. png_error(png_ptr, "This image requires a row greater than 64KB");
  2756. #endif
  2757. if ((png_uint_32)png_ptr->rowbytes > PNG_SIZE_MAX - 1)
  2758. png_error(png_ptr, "Row has too many bytes to allocate in memory.");
  2759. png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)(
  2760. png_ptr->rowbytes + 1));
  2761. png_memset_check(png_ptr, png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
  2762. png_debug1(3, "width = %lu,\n", png_ptr->width);
  2763. png_debug1(3, "height = %lu,\n", png_ptr->height);
  2764. png_debug1(3, "iwidth = %lu,\n", png_ptr->iwidth);
  2765. png_debug1(3, "num_rows = %lu\n", png_ptr->num_rows);
  2766. png_debug1(3, "rowbytes = %lu,\n", png_ptr->rowbytes);
  2767. png_debug1(3, "irowbytes = %lu,\n", png_ptr->irowbytes);
  2768. png_ptr->flags |= PNG_FLAG_ROW_INIT;
  2769. }