pngrutil.c 89 KB

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