pngpread.c 36 KB

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