pngrutil.c 89 KB

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