pngpread.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  1. /* pngpread.c - read a png file in push mode
  2. *
  3. * Last changed in libpng 1.5.11 [June 14, 2012]
  4. * Copyright (c) 1998-2012 Glenn Randers-Pehrson
  5. * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  6. * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
  7. *
  8. * This code is released under the libpng license.
  9. * For conditions of distribution and use, see the disclaimer
  10. * and license in png.h
  11. */
  12. #include "pngpriv.h"
  13. #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
  14. /* Push model modes */
  15. #define PNG_READ_SIG_MODE 0
  16. #define PNG_READ_CHUNK_MODE 1
  17. #define PNG_READ_IDAT_MODE 2
  18. #define PNG_SKIP_MODE 3
  19. #define PNG_READ_tEXt_MODE 4
  20. #define PNG_READ_zTXt_MODE 5
  21. #define PNG_READ_DONE_MODE 6
  22. #define PNG_READ_iTXt_MODE 7
  23. #define PNG_ERROR_MODE 8
  24. void PNGAPI
  25. png_process_data(png_structp png_ptr, png_infop info_ptr,
  26. png_bytep buffer, png_size_t buffer_size)
  27. {
  28. if (png_ptr == NULL || info_ptr == NULL)
  29. return;
  30. png_push_restore_buffer(png_ptr, buffer, buffer_size);
  31. while (png_ptr->buffer_size)
  32. {
  33. png_process_some_data(png_ptr, info_ptr);
  34. }
  35. }
  36. png_size_t PNGAPI
  37. png_process_data_pause(png_structp png_ptr, int save)
  38. {
  39. if (png_ptr != NULL)
  40. {
  41. /* It's easiest for the caller if we do the save, then the caller doesn't
  42. * have to supply the same data again:
  43. */
  44. if (save)
  45. png_push_save_buffer(png_ptr);
  46. else
  47. {
  48. /* This includes any pending saved bytes: */
  49. png_size_t remaining = png_ptr->buffer_size;
  50. png_ptr->buffer_size = 0;
  51. /* So subtract the saved buffer size, unless all the data
  52. * is actually 'saved', in which case we just return 0
  53. */
  54. if (png_ptr->save_buffer_size < remaining)
  55. return remaining - png_ptr->save_buffer_size;
  56. }
  57. }
  58. return 0;
  59. }
  60. png_uint_32 PNGAPI
  61. png_process_data_skip(png_structp png_ptr)
  62. {
  63. png_uint_32 remaining = 0;
  64. if (png_ptr != NULL && png_ptr->process_mode == PNG_SKIP_MODE &&
  65. png_ptr->skip_length > 0)
  66. {
  67. /* At the end of png_process_data the buffer size must be 0 (see the loop
  68. * above) so we can detect a broken call here:
  69. */
  70. if (png_ptr->buffer_size != 0)
  71. png_error(png_ptr,
  72. "png_process_data_skip called inside png_process_data");
  73. /* If is impossible for there to be a saved buffer at this point -
  74. * otherwise we could not be in SKIP mode. This will also happen if
  75. * png_process_skip is called inside png_process_data (but only very
  76. * rarely.)
  77. */
  78. if (png_ptr->save_buffer_size != 0)
  79. png_error(png_ptr, "png_process_data_skip called with saved data");
  80. remaining = png_ptr->skip_length;
  81. png_ptr->skip_length = 0;
  82. png_ptr->process_mode = PNG_READ_CHUNK_MODE;
  83. }
  84. return remaining;
  85. }
  86. /* What we do with the incoming data depends on what we were previously
  87. * doing before we ran out of data...
  88. */
  89. void /* PRIVATE */
  90. png_process_some_data(png_structp png_ptr, png_infop info_ptr)
  91. {
  92. if (png_ptr == NULL)
  93. return;
  94. switch (png_ptr->process_mode)
  95. {
  96. case PNG_READ_SIG_MODE:
  97. {
  98. png_push_read_sig(png_ptr, info_ptr);
  99. break;
  100. }
  101. case PNG_READ_CHUNK_MODE:
  102. {
  103. png_push_read_chunk(png_ptr, info_ptr);
  104. break;
  105. }
  106. case PNG_READ_IDAT_MODE:
  107. {
  108. png_push_read_IDAT(png_ptr);
  109. break;
  110. }
  111. case PNG_SKIP_MODE:
  112. {
  113. png_push_crc_finish(png_ptr);
  114. break;
  115. }
  116. default:
  117. {
  118. png_ptr->buffer_size = 0;
  119. break;
  120. }
  121. }
  122. }
  123. /* Read any remaining signature bytes from the stream and compare them with
  124. * the correct PNG signature. It is possible that this routine is called
  125. * with bytes already read from the signature, either because they have been
  126. * checked by the calling application, or because of multiple calls to this
  127. * routine.
  128. */
  129. void /* PRIVATE */
  130. png_push_read_sig(png_structp png_ptr, png_infop info_ptr)
  131. {
  132. png_size_t num_checked = png_ptr->sig_bytes,
  133. num_to_check = 8 - num_checked;
  134. if (png_ptr->buffer_size < num_to_check)
  135. {
  136. num_to_check = png_ptr->buffer_size;
  137. }
  138. png_push_fill_buffer(png_ptr, &(info_ptr->signature[num_checked]),
  139. num_to_check);
  140. png_ptr->sig_bytes = (png_byte)(png_ptr->sig_bytes + num_to_check);
  141. if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
  142. {
  143. if (num_checked < 4 &&
  144. png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
  145. png_error(png_ptr, "Not a PNG file");
  146. else
  147. png_error(png_ptr, "PNG file corrupted by ASCII conversion");
  148. }
  149. else
  150. {
  151. if (png_ptr->sig_bytes >= 8)
  152. {
  153. png_ptr->process_mode = PNG_READ_CHUNK_MODE;
  154. }
  155. }
  156. }
  157. void /* PRIVATE */
  158. png_push_read_chunk(png_structp png_ptr, png_infop info_ptr)
  159. {
  160. png_uint_32 chunk_name;
  161. /* First we make sure we have enough data for the 4 byte chunk name
  162. * and the 4 byte chunk length before proceeding with decoding the
  163. * chunk data. To fully decode each of these chunks, we also make
  164. * sure we have enough data in the buffer for the 4 byte CRC at the
  165. * end of every chunk (except IDAT, which is handled separately).
  166. */
  167. if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))
  168. {
  169. png_byte chunk_length[4];
  170. png_byte chunk_tag[4];
  171. if (png_ptr->buffer_size < 8)
  172. {
  173. png_push_save_buffer(png_ptr);
  174. return;
  175. }
  176. png_push_fill_buffer(png_ptr, chunk_length, 4);
  177. png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
  178. png_reset_crc(png_ptr);
  179. png_crc_read(png_ptr, chunk_tag, 4);
  180. png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(chunk_tag);
  181. png_check_chunk_name(png_ptr, png_ptr->chunk_name);
  182. png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
  183. }
  184. chunk_name = png_ptr->chunk_name;
  185. if (chunk_name == png_IDAT)
  186. {
  187. /* This is here above the if/else case statement below because if the
  188. * unknown handling marks 'IDAT' as unknown then the IDAT handling case is
  189. * completely skipped.
  190. *
  191. * TODO: there must be a better way of doing this.
  192. */
  193. if (png_ptr->mode & PNG_AFTER_IDAT)
  194. png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
  195. }
  196. if (chunk_name == png_IHDR)
  197. {
  198. if (png_ptr->push_length != 13)
  199. png_error(png_ptr, "Invalid IHDR length");
  200. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  201. {
  202. png_push_save_buffer(png_ptr);
  203. return;
  204. }
  205. png_handle_IHDR(png_ptr, info_ptr, png_ptr->push_length);
  206. }
  207. else if (chunk_name == png_IEND)
  208. {
  209. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  210. {
  211. png_push_save_buffer(png_ptr);
  212. return;
  213. }
  214. png_handle_IEND(png_ptr, info_ptr, png_ptr->push_length);
  215. png_ptr->process_mode = PNG_READ_DONE_MODE;
  216. png_push_have_end(png_ptr, info_ptr);
  217. }
  218. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  219. else if (png_chunk_unknown_handling(png_ptr, chunk_name))
  220. {
  221. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  222. {
  223. png_push_save_buffer(png_ptr);
  224. return;
  225. }
  226. if (chunk_name == png_IDAT)
  227. png_ptr->mode |= PNG_HAVE_IDAT;
  228. png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length);
  229. if (chunk_name == png_PLTE)
  230. png_ptr->mode |= PNG_HAVE_PLTE;
  231. else if (chunk_name == png_IDAT)
  232. {
  233. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  234. png_error(png_ptr, "Missing IHDR before IDAT");
  235. else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
  236. !(png_ptr->mode & PNG_HAVE_PLTE))
  237. png_error(png_ptr, "Missing PLTE before IDAT");
  238. }
  239. }
  240. #endif
  241. else if (chunk_name == png_PLTE)
  242. {
  243. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  244. {
  245. png_push_save_buffer(png_ptr);
  246. return;
  247. }
  248. png_handle_PLTE(png_ptr, info_ptr, png_ptr->push_length);
  249. }
  250. else if (chunk_name == png_IDAT)
  251. {
  252. /* If we reach an IDAT chunk, this means we have read all of the
  253. * header chunks, and we can start reading the image (or if this
  254. * is called after the image has been read - we have an error).
  255. */
  256. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  257. png_error(png_ptr, "Missing IHDR before IDAT");
  258. else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
  259. !(png_ptr->mode & PNG_HAVE_PLTE))
  260. png_error(png_ptr, "Missing PLTE before IDAT");
  261. if (png_ptr->mode & PNG_HAVE_IDAT)
  262. {
  263. if (!(png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
  264. if (png_ptr->push_length == 0)
  265. return;
  266. if (png_ptr->mode & PNG_AFTER_IDAT)
  267. png_benign_error(png_ptr, "Too many IDATs found");
  268. }
  269. png_ptr->idat_size = png_ptr->push_length;
  270. png_ptr->mode |= PNG_HAVE_IDAT;
  271. png_ptr->process_mode = PNG_READ_IDAT_MODE;
  272. png_push_have_info(png_ptr, info_ptr);
  273. png_ptr->zstream.avail_out =
  274. (uInt) PNG_ROWBYTES(png_ptr->pixel_depth,
  275. png_ptr->iwidth) + 1;
  276. png_ptr->zstream.next_out = png_ptr->row_buf;
  277. return;
  278. }
  279. #ifdef PNG_READ_gAMA_SUPPORTED
  280. else if (png_ptr->chunk_name == png_gAMA)
  281. {
  282. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  283. {
  284. png_push_save_buffer(png_ptr);
  285. return;
  286. }
  287. png_handle_gAMA(png_ptr, info_ptr, png_ptr->push_length);
  288. }
  289. #endif
  290. #ifdef PNG_READ_sBIT_SUPPORTED
  291. else if (png_ptr->chunk_name == png_sBIT)
  292. {
  293. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  294. {
  295. png_push_save_buffer(png_ptr);
  296. return;
  297. }
  298. png_handle_sBIT(png_ptr, info_ptr, png_ptr->push_length);
  299. }
  300. #endif
  301. #ifdef PNG_READ_cHRM_SUPPORTED
  302. else if (png_ptr->chunk_name == png_cHRM)
  303. {
  304. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  305. {
  306. png_push_save_buffer(png_ptr);
  307. return;
  308. }
  309. png_handle_cHRM(png_ptr, info_ptr, png_ptr->push_length);
  310. }
  311. #endif
  312. #ifdef PNG_READ_sRGB_SUPPORTED
  313. else if (chunk_name == png_sRGB)
  314. {
  315. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  316. {
  317. png_push_save_buffer(png_ptr);
  318. return;
  319. }
  320. png_handle_sRGB(png_ptr, info_ptr, png_ptr->push_length);
  321. }
  322. #endif
  323. #ifdef PNG_READ_iCCP_SUPPORTED
  324. else if (png_ptr->chunk_name == png_iCCP)
  325. {
  326. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  327. {
  328. png_push_save_buffer(png_ptr);
  329. return;
  330. }
  331. png_handle_iCCP(png_ptr, info_ptr, png_ptr->push_length);
  332. }
  333. #endif
  334. #ifdef PNG_READ_sPLT_SUPPORTED
  335. else if (chunk_name == png_sPLT)
  336. {
  337. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  338. {
  339. png_push_save_buffer(png_ptr);
  340. return;
  341. }
  342. png_handle_sPLT(png_ptr, info_ptr, png_ptr->push_length);
  343. }
  344. #endif
  345. #ifdef PNG_READ_tRNS_SUPPORTED
  346. else if (chunk_name == png_tRNS)
  347. {
  348. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  349. {
  350. png_push_save_buffer(png_ptr);
  351. return;
  352. }
  353. png_handle_tRNS(png_ptr, info_ptr, png_ptr->push_length);
  354. }
  355. #endif
  356. #ifdef PNG_READ_bKGD_SUPPORTED
  357. else if (chunk_name == png_bKGD)
  358. {
  359. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  360. {
  361. png_push_save_buffer(png_ptr);
  362. return;
  363. }
  364. png_handle_bKGD(png_ptr, info_ptr, png_ptr->push_length);
  365. }
  366. #endif
  367. #ifdef PNG_READ_hIST_SUPPORTED
  368. else if (chunk_name == png_hIST)
  369. {
  370. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  371. {
  372. png_push_save_buffer(png_ptr);
  373. return;
  374. }
  375. png_handle_hIST(png_ptr, info_ptr, png_ptr->push_length);
  376. }
  377. #endif
  378. #ifdef PNG_READ_pHYs_SUPPORTED
  379. else if (chunk_name == png_pHYs)
  380. {
  381. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  382. {
  383. png_push_save_buffer(png_ptr);
  384. return;
  385. }
  386. png_handle_pHYs(png_ptr, info_ptr, png_ptr->push_length);
  387. }
  388. #endif
  389. #ifdef PNG_READ_oFFs_SUPPORTED
  390. else if (chunk_name == png_oFFs)
  391. {
  392. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  393. {
  394. png_push_save_buffer(png_ptr);
  395. return;
  396. }
  397. png_handle_oFFs(png_ptr, info_ptr, png_ptr->push_length);
  398. }
  399. #endif
  400. #ifdef PNG_READ_pCAL_SUPPORTED
  401. else if (chunk_name == png_pCAL)
  402. {
  403. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  404. {
  405. png_push_save_buffer(png_ptr);
  406. return;
  407. }
  408. png_handle_pCAL(png_ptr, info_ptr, png_ptr->push_length);
  409. }
  410. #endif
  411. #ifdef PNG_READ_sCAL_SUPPORTED
  412. else if (chunk_name == png_sCAL)
  413. {
  414. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  415. {
  416. png_push_save_buffer(png_ptr);
  417. return;
  418. }
  419. png_handle_sCAL(png_ptr, info_ptr, png_ptr->push_length);
  420. }
  421. #endif
  422. #ifdef PNG_READ_tIME_SUPPORTED
  423. else if (chunk_name == png_tIME)
  424. {
  425. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  426. {
  427. png_push_save_buffer(png_ptr);
  428. return;
  429. }
  430. png_handle_tIME(png_ptr, info_ptr, png_ptr->push_length);
  431. }
  432. #endif
  433. #ifdef PNG_READ_tEXt_SUPPORTED
  434. else if (chunk_name == png_tEXt)
  435. {
  436. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  437. {
  438. png_push_save_buffer(png_ptr);
  439. return;
  440. }
  441. png_handle_tEXt(png_ptr, info_ptr, png_ptr->push_length);
  442. }
  443. #endif
  444. #ifdef PNG_READ_zTXt_SUPPORTED
  445. else if (chunk_name == png_zTXt)
  446. {
  447. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  448. {
  449. png_push_save_buffer(png_ptr);
  450. return;
  451. }
  452. png_handle_zTXt(png_ptr, info_ptr, png_ptr->push_length);
  453. }
  454. #endif
  455. #ifdef PNG_READ_iTXt_SUPPORTED
  456. else if (chunk_name == png_iTXt)
  457. {
  458. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  459. {
  460. png_push_save_buffer(png_ptr);
  461. return;
  462. }
  463. png_handle_iTXt(png_ptr, info_ptr, png_ptr->push_length);
  464. }
  465. #endif
  466. else
  467. {
  468. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  469. {
  470. png_push_save_buffer(png_ptr);
  471. return;
  472. }
  473. png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length);
  474. }
  475. png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
  476. }
  477. void /* PRIVATE */
  478. png_push_crc_skip(png_structp png_ptr, png_uint_32 skip)
  479. {
  480. png_ptr->process_mode = PNG_SKIP_MODE;
  481. png_ptr->skip_length = skip;
  482. }
  483. void /* PRIVATE */
  484. png_push_crc_finish(png_structp png_ptr)
  485. {
  486. if (png_ptr->skip_length && png_ptr->save_buffer_size)
  487. {
  488. png_size_t save_size = png_ptr->save_buffer_size;
  489. png_uint_32 skip_length = png_ptr->skip_length;
  490. /* We want the smaller of 'skip_length' and 'save_buffer_size', but
  491. * they are of different types and we don't know which variable has the
  492. * fewest bits. Carefully select the smaller and cast it to the type of
  493. * the larger - this cannot overflow. Do not cast in the following test
  494. * - it will break on either 16 or 64 bit platforms.
  495. */
  496. if (skip_length < save_size)
  497. save_size = (png_size_t)skip_length;
  498. else
  499. skip_length = (png_uint_32)save_size;
  500. png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
  501. png_ptr->skip_length -= skip_length;
  502. png_ptr->buffer_size -= save_size;
  503. png_ptr->save_buffer_size -= save_size;
  504. png_ptr->save_buffer_ptr += save_size;
  505. }
  506. if (png_ptr->skip_length && png_ptr->current_buffer_size)
  507. {
  508. png_size_t save_size = png_ptr->current_buffer_size;
  509. png_uint_32 skip_length = png_ptr->skip_length;
  510. /* We want the smaller of 'skip_length' and 'current_buffer_size', here,
  511. * the same problem exists as above and the same solution.
  512. */
  513. if (skip_length < save_size)
  514. save_size = (png_size_t)skip_length;
  515. else
  516. skip_length = (png_uint_32)save_size;
  517. png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
  518. png_ptr->skip_length -= skip_length;
  519. png_ptr->buffer_size -= save_size;
  520. png_ptr->current_buffer_size -= save_size;
  521. png_ptr->current_buffer_ptr += save_size;
  522. }
  523. if (!png_ptr->skip_length)
  524. {
  525. if (png_ptr->buffer_size < 4)
  526. {
  527. png_push_save_buffer(png_ptr);
  528. return;
  529. }
  530. png_crc_finish(png_ptr, 0);
  531. png_ptr->process_mode = PNG_READ_CHUNK_MODE;
  532. }
  533. }
  534. void PNGCBAPI
  535. png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, png_size_t length)
  536. {
  537. png_bytep ptr;
  538. if (png_ptr == NULL)
  539. return;
  540. ptr = buffer;
  541. if (png_ptr->save_buffer_size)
  542. {
  543. png_size_t save_size;
  544. if (length < png_ptr->save_buffer_size)
  545. save_size = length;
  546. else
  547. save_size = png_ptr->save_buffer_size;
  548. png_memcpy(ptr, png_ptr->save_buffer_ptr, save_size);
  549. length -= save_size;
  550. ptr += save_size;
  551. png_ptr->buffer_size -= save_size;
  552. png_ptr->save_buffer_size -= save_size;
  553. png_ptr->save_buffer_ptr += save_size;
  554. }
  555. if (length && png_ptr->current_buffer_size)
  556. {
  557. png_size_t save_size;
  558. if (length < png_ptr->current_buffer_size)
  559. save_size = length;
  560. else
  561. save_size = png_ptr->current_buffer_size;
  562. png_memcpy(ptr, png_ptr->current_buffer_ptr, save_size);
  563. png_ptr->buffer_size -= save_size;
  564. png_ptr->current_buffer_size -= save_size;
  565. png_ptr->current_buffer_ptr += save_size;
  566. }
  567. }
  568. void /* PRIVATE */
  569. png_push_save_buffer(png_structp png_ptr)
  570. {
  571. if (png_ptr->save_buffer_size)
  572. {
  573. if (png_ptr->save_buffer_ptr != png_ptr->save_buffer)
  574. {
  575. png_size_t i, istop;
  576. png_bytep sp;
  577. png_bytep dp;
  578. istop = png_ptr->save_buffer_size;
  579. for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer;
  580. i < istop; i++, sp++, dp++)
  581. {
  582. *dp = *sp;
  583. }
  584. }
  585. }
  586. if (png_ptr->save_buffer_size + png_ptr->current_buffer_size >
  587. png_ptr->save_buffer_max)
  588. {
  589. png_size_t new_max;
  590. png_bytep old_buffer;
  591. if (png_ptr->save_buffer_size > PNG_SIZE_MAX -
  592. (png_ptr->current_buffer_size + 256))
  593. {
  594. png_error(png_ptr, "Potential overflow of save_buffer");
  595. }
  596. new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256;
  597. old_buffer = png_ptr->save_buffer;
  598. png_ptr->save_buffer = (png_bytep)png_malloc_warn(png_ptr, new_max);
  599. if (png_ptr->save_buffer == NULL)
  600. {
  601. png_free(png_ptr, old_buffer);
  602. png_error(png_ptr, "Insufficient memory for save_buffer");
  603. }
  604. png_memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);
  605. png_free(png_ptr, old_buffer);
  606. png_ptr->save_buffer_max = new_max;
  607. }
  608. if (png_ptr->current_buffer_size)
  609. {
  610. png_memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size,
  611. png_ptr->current_buffer_ptr, png_ptr->current_buffer_size);
  612. png_ptr->save_buffer_size += png_ptr->current_buffer_size;
  613. png_ptr->current_buffer_size = 0;
  614. }
  615. png_ptr->save_buffer_ptr = png_ptr->save_buffer;
  616. png_ptr->buffer_size = 0;
  617. }
  618. void /* PRIVATE */
  619. png_push_restore_buffer(png_structp png_ptr, png_bytep buffer,
  620. png_size_t buffer_length)
  621. {
  622. png_ptr->current_buffer = buffer;
  623. png_ptr->current_buffer_size = buffer_length;
  624. png_ptr->buffer_size = buffer_length + png_ptr->save_buffer_size;
  625. png_ptr->current_buffer_ptr = png_ptr->current_buffer;
  626. }
  627. void /* PRIVATE */
  628. png_push_read_IDAT(png_structp png_ptr)
  629. {
  630. if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))
  631. {
  632. png_byte chunk_length[4];
  633. png_byte chunk_tag[4];
  634. /* TODO: this code can be commoned up with the same code in push_read */
  635. if (png_ptr->buffer_size < 8)
  636. {
  637. png_push_save_buffer(png_ptr);
  638. return;
  639. }
  640. png_push_fill_buffer(png_ptr, chunk_length, 4);
  641. png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
  642. png_reset_crc(png_ptr);
  643. png_crc_read(png_ptr, chunk_tag, 4);
  644. png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(chunk_tag);
  645. png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
  646. if (png_ptr->chunk_name != png_IDAT)
  647. {
  648. png_ptr->process_mode = PNG_READ_CHUNK_MODE;
  649. if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
  650. png_error(png_ptr, "Not enough compressed data");
  651. return;
  652. }
  653. png_ptr->idat_size = png_ptr->push_length;
  654. }
  655. if (png_ptr->idat_size && png_ptr->save_buffer_size)
  656. {
  657. png_size_t save_size = png_ptr->save_buffer_size;
  658. png_uint_32 idat_size = png_ptr->idat_size;
  659. /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
  660. * are of different types and we don't know which variable has the fewest
  661. * bits. Carefully select the smaller and cast it to the type of the
  662. * larger - this cannot overflow. Do not cast in the following test - it
  663. * will break on either 16 or 64 bit platforms.
  664. */
  665. if (idat_size < save_size)
  666. save_size = (png_size_t)idat_size;
  667. else
  668. idat_size = (png_uint_32)save_size;
  669. png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
  670. png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size);
  671. png_ptr->idat_size -= idat_size;
  672. png_ptr->buffer_size -= save_size;
  673. png_ptr->save_buffer_size -= save_size;
  674. png_ptr->save_buffer_ptr += save_size;
  675. }
  676. if (png_ptr->idat_size && png_ptr->current_buffer_size)
  677. {
  678. png_size_t save_size = png_ptr->current_buffer_size;
  679. png_uint_32 idat_size = png_ptr->idat_size;
  680. /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
  681. * are of different types and we don't know which variable has the fewest
  682. * bits. Carefully select the smaller and cast it to the type of the
  683. * larger - this cannot overflow.
  684. */
  685. if (idat_size < save_size)
  686. save_size = (png_size_t)idat_size;
  687. else
  688. idat_size = (png_uint_32)save_size;
  689. png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
  690. png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size);
  691. png_ptr->idat_size -= idat_size;
  692. png_ptr->buffer_size -= save_size;
  693. png_ptr->current_buffer_size -= save_size;
  694. png_ptr->current_buffer_ptr += save_size;
  695. }
  696. if (!png_ptr->idat_size)
  697. {
  698. if (png_ptr->buffer_size < 4)
  699. {
  700. png_push_save_buffer(png_ptr);
  701. return;
  702. }
  703. png_crc_finish(png_ptr, 0);
  704. png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
  705. png_ptr->mode |= PNG_AFTER_IDAT;
  706. }
  707. }
  708. void /* PRIVATE */
  709. png_process_IDAT_data(png_structp png_ptr, png_bytep buffer,
  710. png_size_t buffer_length)
  711. {
  712. /* The caller checks for a non-zero buffer length. */
  713. if (!(buffer_length > 0) || buffer == NULL)
  714. png_error(png_ptr, "No IDAT data (internal error)");
  715. /* This routine must process all the data it has been given
  716. * before returning, calling the row callback as required to
  717. * handle the uncompressed results.
  718. */
  719. png_ptr->zstream.next_in = buffer;
  720. png_ptr->zstream.avail_in = (uInt)buffer_length;
  721. /* Keep going until the decompressed data is all processed
  722. * or the stream marked as finished.
  723. */
  724. while (png_ptr->zstream.avail_in > 0 &&
  725. !(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
  726. {
  727. int ret;
  728. /* We have data for zlib, but we must check that zlib
  729. * has someplace to put the results. It doesn't matter
  730. * if we don't expect any results -- it may be the input
  731. * data is just the LZ end code.
  732. */
  733. if (!(png_ptr->zstream.avail_out > 0))
  734. {
  735. png_ptr->zstream.avail_out =
  736. (uInt) PNG_ROWBYTES(png_ptr->pixel_depth,
  737. png_ptr->iwidth) + 1;
  738. png_ptr->zstream.next_out = png_ptr->row_buf;
  739. }
  740. /* Using Z_SYNC_FLUSH here means that an unterminated
  741. * LZ stream (a stream with a missing end code) can still
  742. * be handled, otherwise (Z_NO_FLUSH) a future zlib
  743. * implementation might defer output and therefore
  744. * change the current behavior (see comments in inflate.c
  745. * for why this doesn't happen at present with zlib 1.2.5).
  746. */
  747. ret = inflate(&png_ptr->zstream, Z_SYNC_FLUSH);
  748. /* Check for any failure before proceeding. */
  749. if (ret != Z_OK && ret != Z_STREAM_END)
  750. {
  751. /* Terminate the decompression. */
  752. png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
  753. /* This may be a truncated stream (missing or
  754. * damaged end code). Treat that as a warning.
  755. */
  756. if (png_ptr->row_number >= png_ptr->num_rows ||
  757. png_ptr->pass > 6)
  758. png_warning(png_ptr, "Truncated compressed data in IDAT");
  759. else
  760. png_error(png_ptr, "Decompression error in IDAT");
  761. /* Skip the check on unprocessed input */
  762. return;
  763. }
  764. /* Did inflate output any data? */
  765. if (png_ptr->zstream.next_out != png_ptr->row_buf)
  766. {
  767. /* Is this unexpected data after the last row?
  768. * If it is, artificially terminate the LZ output
  769. * here.
  770. */
  771. if (png_ptr->row_number >= png_ptr->num_rows ||
  772. png_ptr->pass > 6)
  773. {
  774. /* Extra data. */
  775. png_warning(png_ptr, "Extra compressed data in IDAT");
  776. png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
  777. /* Do no more processing; skip the unprocessed
  778. * input check below.
  779. */
  780. return;
  781. }
  782. /* Do we have a complete row? */
  783. if (png_ptr->zstream.avail_out == 0)
  784. png_push_process_row(png_ptr);
  785. }
  786. /* And check for the end of the stream. */
  787. if (ret == Z_STREAM_END)
  788. png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
  789. }
  790. /* All the data should have been processed, if anything
  791. * is left at this point we have bytes of IDAT data
  792. * after the zlib end code.
  793. */
  794. if (png_ptr->zstream.avail_in > 0)
  795. png_warning(png_ptr, "Extra compression data in IDAT");
  796. }
  797. void /* PRIVATE */
  798. png_push_process_row(png_structp png_ptr)
  799. {
  800. /* 1.5.6: row_info moved out of png_struct to a local here. */
  801. png_row_info row_info;
  802. row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */
  803. row_info.color_type = png_ptr->color_type;
  804. row_info.bit_depth = png_ptr->bit_depth;
  805. row_info.channels = png_ptr->channels;
  806. row_info.pixel_depth = png_ptr->pixel_depth;
  807. row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
  808. if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
  809. {
  810. if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST)
  811. png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1,
  812. png_ptr->prev_row + 1, png_ptr->row_buf[0]);
  813. else
  814. png_error(png_ptr, "bad adaptive filter value");
  815. }
  816. /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
  817. * 1.5.6, while the buffer really is this big in current versions of libpng
  818. * it may not be in the future, so this was changed just to copy the
  819. * interlaced row count:
  820. */
  821. png_memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
  822. #ifdef PNG_READ_TRANSFORMS_SUPPORTED
  823. if (png_ptr->transformations)
  824. png_do_read_transformations(png_ptr, &row_info);
  825. #endif
  826. /* The transformed pixel depth should match the depth now in row_info. */
  827. if (png_ptr->transformed_pixel_depth == 0)
  828. {
  829. png_ptr->transformed_pixel_depth = row_info.pixel_depth;
  830. if (row_info.pixel_depth > png_ptr->maximum_pixel_depth)
  831. png_error(png_ptr, "progressive row overflow");
  832. }
  833. else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth)
  834. png_error(png_ptr, "internal progressive row size calculation error");
  835. #ifdef PNG_READ_INTERLACING_SUPPORTED
  836. /* Blow up interlaced rows to full size */
  837. if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
  838. {
  839. if (png_ptr->pass < 6)
  840. png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass,
  841. png_ptr->transformations);
  842. switch (png_ptr->pass)
  843. {
  844. case 0:
  845. {
  846. int i;
  847. for (i = 0; i < 8 && png_ptr->pass == 0; i++)
  848. {
  849. png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  850. png_read_push_finish_row(png_ptr); /* Updates png_ptr->pass */
  851. }
  852. if (png_ptr->pass == 2) /* Pass 1 might be empty */
  853. {
  854. for (i = 0; i < 4 && png_ptr->pass == 2; i++)
  855. {
  856. png_push_have_row(png_ptr, NULL);
  857. png_read_push_finish_row(png_ptr);
  858. }
  859. }
  860. if (png_ptr->pass == 4 && png_ptr->height <= 4)
  861. {
  862. for (i = 0; i < 2 && png_ptr->pass == 4; i++)
  863. {
  864. png_push_have_row(png_ptr, NULL);
  865. png_read_push_finish_row(png_ptr);
  866. }
  867. }
  868. if (png_ptr->pass == 6 && png_ptr->height <= 4)
  869. {
  870. png_push_have_row(png_ptr, NULL);
  871. png_read_push_finish_row(png_ptr);
  872. }
  873. break;
  874. }
  875. case 1:
  876. {
  877. int i;
  878. for (i = 0; i < 8 && png_ptr->pass == 1; i++)
  879. {
  880. png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  881. png_read_push_finish_row(png_ptr);
  882. }
  883. if (png_ptr->pass == 2) /* Skip top 4 generated rows */
  884. {
  885. for (i = 0; i < 4 && png_ptr->pass == 2; i++)
  886. {
  887. png_push_have_row(png_ptr, NULL);
  888. png_read_push_finish_row(png_ptr);
  889. }
  890. }
  891. break;
  892. }
  893. case 2:
  894. {
  895. int i;
  896. for (i = 0; i < 4 && png_ptr->pass == 2; i++)
  897. {
  898. png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  899. png_read_push_finish_row(png_ptr);
  900. }
  901. for (i = 0; i < 4 && png_ptr->pass == 2; i++)
  902. {
  903. png_push_have_row(png_ptr, NULL);
  904. png_read_push_finish_row(png_ptr);
  905. }
  906. if (png_ptr->pass == 4) /* Pass 3 might be empty */
  907. {
  908. for (i = 0; i < 2 && png_ptr->pass == 4; i++)
  909. {
  910. png_push_have_row(png_ptr, NULL);
  911. png_read_push_finish_row(png_ptr);
  912. }
  913. }
  914. break;
  915. }
  916. case 3:
  917. {
  918. int i;
  919. for (i = 0; i < 4 && png_ptr->pass == 3; i++)
  920. {
  921. png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  922. png_read_push_finish_row(png_ptr);
  923. }
  924. if (png_ptr->pass == 4) /* Skip top two generated rows */
  925. {
  926. for (i = 0; i < 2 && png_ptr->pass == 4; i++)
  927. {
  928. png_push_have_row(png_ptr, NULL);
  929. png_read_push_finish_row(png_ptr);
  930. }
  931. }
  932. break;
  933. }
  934. case 4:
  935. {
  936. int i;
  937. for (i = 0; i < 2 && png_ptr->pass == 4; i++)
  938. {
  939. png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  940. png_read_push_finish_row(png_ptr);
  941. }
  942. for (i = 0; i < 2 && png_ptr->pass == 4; i++)
  943. {
  944. png_push_have_row(png_ptr, NULL);
  945. png_read_push_finish_row(png_ptr);
  946. }
  947. if (png_ptr->pass == 6) /* Pass 5 might be empty */
  948. {
  949. png_push_have_row(png_ptr, NULL);
  950. png_read_push_finish_row(png_ptr);
  951. }
  952. break;
  953. }
  954. case 5:
  955. {
  956. int i;
  957. for (i = 0; i < 2 && png_ptr->pass == 5; i++)
  958. {
  959. png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  960. png_read_push_finish_row(png_ptr);
  961. }
  962. if (png_ptr->pass == 6) /* Skip top generated row */
  963. {
  964. png_push_have_row(png_ptr, NULL);
  965. png_read_push_finish_row(png_ptr);
  966. }
  967. break;
  968. }
  969. default:
  970. case 6:
  971. {
  972. png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  973. png_read_push_finish_row(png_ptr);
  974. if (png_ptr->pass != 6)
  975. break;
  976. png_push_have_row(png_ptr, NULL);
  977. png_read_push_finish_row(png_ptr);
  978. }
  979. }
  980. }
  981. else
  982. #endif
  983. {
  984. png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  985. png_read_push_finish_row(png_ptr);
  986. }
  987. }
  988. void /* PRIVATE */
  989. png_read_push_finish_row(png_structp png_ptr)
  990. {
  991. #ifdef PNG_READ_INTERLACING_SUPPORTED
  992. /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  993. /* Start of interlace block */
  994. static PNG_CONST png_byte FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
  995. /* Offset to next interlace block */
  996. static PNG_CONST png_byte FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
  997. /* Start of interlace block in the y direction */
  998. static PNG_CONST png_byte FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
  999. /* Offset to next interlace block in the y direction */
  1000. static PNG_CONST png_byte FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
  1001. /* Height of interlace block. This is not currently used - if you need
  1002. * it, uncomment it here and in png.h
  1003. static PNG_CONST png_byte FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
  1004. */
  1005. #endif
  1006. png_ptr->row_number++;
  1007. if (png_ptr->row_number < png_ptr->num_rows)
  1008. return;
  1009. #ifdef PNG_READ_INTERLACING_SUPPORTED
  1010. if (png_ptr->interlaced)
  1011. {
  1012. png_ptr->row_number = 0;
  1013. png_memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
  1014. do
  1015. {
  1016. png_ptr->pass++;
  1017. if ((png_ptr->pass == 1 && png_ptr->width < 5) ||
  1018. (png_ptr->pass == 3 && png_ptr->width < 3) ||
  1019. (png_ptr->pass == 5 && png_ptr->width < 2))
  1020. png_ptr->pass++;
  1021. if (png_ptr->pass > 7)
  1022. png_ptr->pass--;
  1023. if (png_ptr->pass >= 7)
  1024. break;
  1025. png_ptr->iwidth = (png_ptr->width +
  1026. png_pass_inc[png_ptr->pass] - 1 -
  1027. png_pass_start[png_ptr->pass]) /
  1028. png_pass_inc[png_ptr->pass];
  1029. if (png_ptr->transformations & PNG_INTERLACE)
  1030. break;
  1031. png_ptr->num_rows = (png_ptr->height +
  1032. png_pass_yinc[png_ptr->pass] - 1 -
  1033. png_pass_ystart[png_ptr->pass]) /
  1034. png_pass_yinc[png_ptr->pass];
  1035. } while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0);
  1036. }
  1037. #endif /* PNG_READ_INTERLACING_SUPPORTED */
  1038. }
  1039. void /* PRIVATE */
  1040. png_push_have_info(png_structp png_ptr, png_infop info_ptr)
  1041. {
  1042. if (png_ptr->info_fn != NULL)
  1043. (*(png_ptr->info_fn))(png_ptr, info_ptr);
  1044. }
  1045. void /* PRIVATE */
  1046. png_push_have_end(png_structp png_ptr, png_infop info_ptr)
  1047. {
  1048. if (png_ptr->end_fn != NULL)
  1049. (*(png_ptr->end_fn))(png_ptr, info_ptr);
  1050. }
  1051. void /* PRIVATE */
  1052. png_push_have_row(png_structp png_ptr, png_bytep row)
  1053. {
  1054. if (png_ptr->row_fn != NULL)
  1055. (*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number,
  1056. (int)png_ptr->pass);
  1057. }
  1058. #ifdef PNG_READ_INTERLACING_SUPPORTED
  1059. void PNGAPI
  1060. png_progressive_combine_row (png_structp png_ptr, png_bytep old_row,
  1061. png_const_bytep new_row)
  1062. {
  1063. if (png_ptr == NULL)
  1064. return;
  1065. /* new_row is a flag here - if it is NULL then the app callback was called
  1066. * from an empty row (see the calls to png_struct::row_fn below), otherwise
  1067. * it must be png_ptr->row_buf+1
  1068. */
  1069. if (new_row != NULL)
  1070. png_combine_row(png_ptr, old_row, 1/*display*/);
  1071. }
  1072. #endif /* PNG_READ_INTERLACING_SUPPORTED */
  1073. void PNGAPI
  1074. png_set_progressive_read_fn(png_structp png_ptr, png_voidp progressive_ptr,
  1075. png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
  1076. png_progressive_end_ptr end_fn)
  1077. {
  1078. if (png_ptr == NULL)
  1079. return;
  1080. png_ptr->info_fn = info_fn;
  1081. png_ptr->row_fn = row_fn;
  1082. png_ptr->end_fn = end_fn;
  1083. png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer);
  1084. }
  1085. png_voidp PNGAPI
  1086. png_get_progressive_ptr(png_const_structp png_ptr)
  1087. {
  1088. if (png_ptr == NULL)
  1089. return (NULL);
  1090. return png_ptr->io_ptr;
  1091. }
  1092. #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */