pngpread.c 36 KB

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