pngpread.c 36 KB

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