pngpread.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. /* pngpread.c - read a png file in push mode
  2. *
  3. * Copyright (c) 2018-2025 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_PUSH_SAVE_BUFFER_IF_LT(8)
  171. png_ptr->push_length = png_read_chunk_header(png_ptr);
  172. png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
  173. }
  174. chunk_name = png_ptr->chunk_name;
  175. if (chunk_name == png_IDAT)
  176. {
  177. if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
  178. png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
  179. /* If we reach an IDAT chunk, this means we have read all of the
  180. * header chunks, and we can start reading the image (or if this
  181. * is called after the image has been read - we have an error).
  182. */
  183. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  184. png_error(png_ptr, "Missing IHDR before IDAT");
  185. else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
  186. (png_ptr->mode & PNG_HAVE_PLTE) == 0)
  187. png_error(png_ptr, "Missing PLTE before IDAT");
  188. png_ptr->process_mode = PNG_READ_IDAT_MODE;
  189. if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
  190. if ((png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) == 0)
  191. if (png_ptr->push_length == 0)
  192. return;
  193. png_ptr->mode |= PNG_HAVE_IDAT;
  194. if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
  195. png_benign_error(png_ptr, "Too many IDATs found");
  196. }
  197. if (chunk_name == png_IHDR)
  198. {
  199. if (png_ptr->push_length != 13)
  200. png_error(png_ptr, "Invalid IHDR length");
  201. PNG_PUSH_SAVE_BUFFER_IF_FULL
  202. png_handle_chunk(png_ptr, info_ptr, png_ptr->push_length);
  203. }
  204. else if (chunk_name == png_IEND)
  205. {
  206. PNG_PUSH_SAVE_BUFFER_IF_FULL
  207. png_handle_chunk(png_ptr, info_ptr, png_ptr->push_length);
  208. png_ptr->process_mode = PNG_READ_DONE_MODE;
  209. png_push_have_end(png_ptr, info_ptr);
  210. }
  211. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  212. else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0)
  213. {
  214. PNG_PUSH_SAVE_BUFFER_IF_FULL
  215. png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length, keep);
  216. if (chunk_name == png_PLTE)
  217. png_ptr->mode |= PNG_HAVE_PLTE;
  218. }
  219. #endif
  220. else if (chunk_name == png_IDAT)
  221. {
  222. png_ptr->idat_size = png_ptr->push_length;
  223. png_ptr->process_mode = PNG_READ_IDAT_MODE;
  224. png_push_have_info(png_ptr, info_ptr);
  225. png_ptr->zstream.avail_out =
  226. (uInt) PNG_ROWBYTES(png_ptr->pixel_depth,
  227. png_ptr->iwidth) + 1;
  228. png_ptr->zstream.next_out = png_ptr->row_buf;
  229. return;
  230. }
  231. else
  232. {
  233. PNG_PUSH_SAVE_BUFFER_IF_FULL
  234. png_handle_chunk(png_ptr, info_ptr, png_ptr->push_length);
  235. }
  236. png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
  237. }
  238. void PNGCBAPI
  239. png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, size_t length)
  240. {
  241. png_bytep ptr;
  242. if (png_ptr == NULL)
  243. return;
  244. ptr = buffer;
  245. if (png_ptr->save_buffer_size != 0)
  246. {
  247. size_t save_size;
  248. if (length < png_ptr->save_buffer_size)
  249. save_size = length;
  250. else
  251. save_size = png_ptr->save_buffer_size;
  252. memcpy(ptr, png_ptr->save_buffer_ptr, save_size);
  253. length -= save_size;
  254. ptr += save_size;
  255. png_ptr->buffer_size -= save_size;
  256. png_ptr->save_buffer_size -= save_size;
  257. png_ptr->save_buffer_ptr += save_size;
  258. }
  259. if (length != 0 && png_ptr->current_buffer_size != 0)
  260. {
  261. size_t save_size;
  262. if (length < png_ptr->current_buffer_size)
  263. save_size = length;
  264. else
  265. save_size = png_ptr->current_buffer_size;
  266. memcpy(ptr, png_ptr->current_buffer_ptr, save_size);
  267. png_ptr->buffer_size -= save_size;
  268. png_ptr->current_buffer_size -= save_size;
  269. png_ptr->current_buffer_ptr += save_size;
  270. }
  271. }
  272. void /* PRIVATE */
  273. png_push_save_buffer(png_structrp png_ptr)
  274. {
  275. if (png_ptr->save_buffer_size != 0)
  276. {
  277. if (png_ptr->save_buffer_ptr != png_ptr->save_buffer)
  278. {
  279. size_t i, istop;
  280. png_bytep sp;
  281. png_bytep dp;
  282. istop = png_ptr->save_buffer_size;
  283. for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer;
  284. i < istop; i++, sp++, dp++)
  285. {
  286. *dp = *sp;
  287. }
  288. }
  289. }
  290. if (png_ptr->save_buffer_size + png_ptr->current_buffer_size >
  291. png_ptr->save_buffer_max)
  292. {
  293. size_t new_max;
  294. png_bytep old_buffer;
  295. if (png_ptr->save_buffer_size > PNG_SIZE_MAX -
  296. (png_ptr->current_buffer_size + 256))
  297. {
  298. png_error(png_ptr, "Potential overflow of save_buffer");
  299. }
  300. new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256;
  301. old_buffer = png_ptr->save_buffer;
  302. png_ptr->save_buffer = (png_bytep)png_malloc_warn(png_ptr,
  303. (size_t)new_max);
  304. if (png_ptr->save_buffer == NULL)
  305. {
  306. png_free(png_ptr, old_buffer);
  307. png_error(png_ptr, "Insufficient memory for save_buffer");
  308. }
  309. if (old_buffer)
  310. memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);
  311. else if (png_ptr->save_buffer_size)
  312. png_error(png_ptr, "save_buffer error");
  313. png_free(png_ptr, old_buffer);
  314. png_ptr->save_buffer_max = new_max;
  315. }
  316. if (png_ptr->current_buffer_size)
  317. {
  318. memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size,
  319. png_ptr->current_buffer_ptr, png_ptr->current_buffer_size);
  320. png_ptr->save_buffer_size += png_ptr->current_buffer_size;
  321. png_ptr->current_buffer_size = 0;
  322. }
  323. png_ptr->save_buffer_ptr = png_ptr->save_buffer;
  324. png_ptr->buffer_size = 0;
  325. }
  326. void /* PRIVATE */
  327. png_push_restore_buffer(png_structrp png_ptr, png_bytep buffer,
  328. size_t buffer_length)
  329. {
  330. png_ptr->current_buffer = buffer;
  331. png_ptr->current_buffer_size = buffer_length;
  332. png_ptr->buffer_size = buffer_length + png_ptr->save_buffer_size;
  333. png_ptr->current_buffer_ptr = png_ptr->current_buffer;
  334. }
  335. void /* PRIVATE */
  336. png_push_read_IDAT(png_structrp png_ptr)
  337. {
  338. if ((png_ptr->mode & PNG_HAVE_CHUNK_HEADER) == 0)
  339. {
  340. png_byte chunk_length[4];
  341. png_byte chunk_tag[4];
  342. /* TODO: this code can be commoned up with the same code in push_read */
  343. PNG_PUSH_SAVE_BUFFER_IF_LT(8)
  344. png_push_fill_buffer(png_ptr, chunk_length, 4);
  345. png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
  346. png_reset_crc(png_ptr);
  347. png_crc_read(png_ptr, chunk_tag, 4);
  348. png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(chunk_tag);
  349. png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
  350. if (png_ptr->chunk_name != png_IDAT)
  351. {
  352. png_ptr->process_mode = PNG_READ_CHUNK_MODE;
  353. if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
  354. png_error(png_ptr, "Not enough compressed data");
  355. return;
  356. }
  357. png_ptr->idat_size = png_ptr->push_length;
  358. }
  359. if (png_ptr->idat_size != 0 && png_ptr->save_buffer_size != 0)
  360. {
  361. size_t save_size = png_ptr->save_buffer_size;
  362. png_uint_32 idat_size = png_ptr->idat_size;
  363. /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
  364. * are of different types and we don't know which variable has the fewest
  365. * bits. Carefully select the smaller and cast it to the type of the
  366. * larger - this cannot overflow. Do not cast in the following test - it
  367. * will break on either 16-bit or 64-bit platforms.
  368. */
  369. if (idat_size < save_size)
  370. save_size = (size_t)idat_size;
  371. else
  372. idat_size = (png_uint_32)save_size;
  373. png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
  374. png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size);
  375. png_ptr->idat_size -= idat_size;
  376. png_ptr->buffer_size -= save_size;
  377. png_ptr->save_buffer_size -= save_size;
  378. png_ptr->save_buffer_ptr += save_size;
  379. }
  380. if (png_ptr->idat_size != 0 && png_ptr->current_buffer_size != 0)
  381. {
  382. size_t save_size = png_ptr->current_buffer_size;
  383. png_uint_32 idat_size = png_ptr->idat_size;
  384. /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
  385. * are of different types and we don't know which variable has the fewest
  386. * bits. Carefully select the smaller and cast it to the type of the
  387. * larger - this cannot overflow.
  388. */
  389. if (idat_size < save_size)
  390. save_size = (size_t)idat_size;
  391. else
  392. idat_size = (png_uint_32)save_size;
  393. png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
  394. png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size);
  395. png_ptr->idat_size -= idat_size;
  396. png_ptr->buffer_size -= save_size;
  397. png_ptr->current_buffer_size -= save_size;
  398. png_ptr->current_buffer_ptr += save_size;
  399. }
  400. if (png_ptr->idat_size == 0)
  401. {
  402. PNG_PUSH_SAVE_BUFFER_IF_LT(4)
  403. png_crc_finish(png_ptr, 0);
  404. png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
  405. png_ptr->mode |= PNG_AFTER_IDAT;
  406. png_ptr->zowner = 0;
  407. }
  408. }
  409. void /* PRIVATE */
  410. png_process_IDAT_data(png_structrp png_ptr, png_bytep buffer,
  411. size_t buffer_length)
  412. {
  413. /* The caller checks for a non-zero buffer length. */
  414. if (!(buffer_length > 0) || buffer == NULL)
  415. png_error(png_ptr, "No IDAT data (internal error)");
  416. /* This routine must process all the data it has been given
  417. * before returning, calling the row callback as required to
  418. * handle the uncompressed results.
  419. */
  420. png_ptr->zstream.next_in = buffer;
  421. /* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */
  422. png_ptr->zstream.avail_in = (uInt)buffer_length;
  423. /* Keep going until the decompressed data is all processed
  424. * or the stream marked as finished.
  425. */
  426. while (png_ptr->zstream.avail_in > 0 &&
  427. (png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
  428. {
  429. int ret;
  430. /* We have data for zlib, but we must check that zlib
  431. * has someplace to put the results. It doesn't matter
  432. * if we don't expect any results -- it may be the input
  433. * data is just the LZ end code.
  434. */
  435. if (!(png_ptr->zstream.avail_out > 0))
  436. {
  437. /* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */
  438. png_ptr->zstream.avail_out = (uInt)(PNG_ROWBYTES(png_ptr->pixel_depth,
  439. png_ptr->iwidth) + 1);
  440. png_ptr->zstream.next_out = png_ptr->row_buf;
  441. }
  442. /* Using Z_SYNC_FLUSH here means that an unterminated
  443. * LZ stream (a stream with a missing end code) can still
  444. * be handled, otherwise (Z_NO_FLUSH) a future zlib
  445. * implementation might defer output and therefore
  446. * change the current behavior (see comments in inflate.c
  447. * for why this doesn't happen at present with zlib 1.2.5).
  448. */
  449. ret = PNG_INFLATE(png_ptr, Z_SYNC_FLUSH);
  450. /* Check for any failure before proceeding. */
  451. if (ret != Z_OK && ret != Z_STREAM_END)
  452. {
  453. /* Terminate the decompression. */
  454. png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
  455. png_ptr->zowner = 0;
  456. /* This may be a truncated stream (missing or
  457. * damaged end code). Treat that as a warning.
  458. */
  459. if (png_ptr->row_number >= png_ptr->num_rows ||
  460. png_ptr->pass > 6)
  461. png_warning(png_ptr, "Truncated compressed data in IDAT");
  462. else
  463. {
  464. if (ret == Z_DATA_ERROR)
  465. png_benign_error(png_ptr, "IDAT: ADLER32 checksum mismatch");
  466. else
  467. png_error(png_ptr, "Decompression error in IDAT");
  468. }
  469. /* Skip the check on unprocessed input */
  470. return;
  471. }
  472. /* Did inflate output any data? */
  473. if (png_ptr->zstream.next_out != png_ptr->row_buf)
  474. {
  475. /* Is this unexpected data after the last row?
  476. * If it is, artificially terminate the LZ output
  477. * here.
  478. */
  479. if (png_ptr->row_number >= png_ptr->num_rows ||
  480. png_ptr->pass > 6)
  481. {
  482. /* Extra data. */
  483. png_warning(png_ptr, "Extra compressed data in IDAT");
  484. png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
  485. png_ptr->zowner = 0;
  486. /* Do no more processing; skip the unprocessed
  487. * input check below.
  488. */
  489. return;
  490. }
  491. /* Do we have a complete row? */
  492. if (png_ptr->zstream.avail_out == 0)
  493. png_push_process_row(png_ptr);
  494. }
  495. /* And check for the end of the stream. */
  496. if (ret == Z_STREAM_END)
  497. png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
  498. }
  499. /* All the data should have been processed, if anything
  500. * is left at this point we have bytes of IDAT data
  501. * after the zlib end code.
  502. */
  503. if (png_ptr->zstream.avail_in > 0)
  504. png_warning(png_ptr, "Extra compression data in IDAT");
  505. }
  506. void /* PRIVATE */
  507. png_push_process_row(png_structrp png_ptr)
  508. {
  509. /* 1.5.6: row_info moved out of png_struct to a local here. */
  510. png_row_info row_info;
  511. row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */
  512. row_info.color_type = png_ptr->color_type;
  513. row_info.bit_depth = png_ptr->bit_depth;
  514. row_info.channels = png_ptr->channels;
  515. row_info.pixel_depth = png_ptr->pixel_depth;
  516. row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
  517. if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
  518. {
  519. if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST)
  520. png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1,
  521. png_ptr->prev_row + 1, png_ptr->row_buf[0]);
  522. else
  523. png_error(png_ptr, "bad adaptive filter value");
  524. }
  525. /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
  526. * 1.5.6, while the buffer really is this big in current versions of libpng
  527. * it may not be in the future, so this was changed just to copy the
  528. * interlaced row count:
  529. */
  530. memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
  531. #ifdef PNG_READ_TRANSFORMS_SUPPORTED
  532. if (png_ptr->transformations != 0)
  533. png_do_read_transformations(png_ptr, &row_info);
  534. #endif
  535. /* The transformed pixel depth should match the depth now in row_info. */
  536. if (png_ptr->transformed_pixel_depth == 0)
  537. {
  538. png_ptr->transformed_pixel_depth = row_info.pixel_depth;
  539. if (row_info.pixel_depth > png_ptr->maximum_pixel_depth)
  540. png_error(png_ptr, "progressive row overflow");
  541. }
  542. else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth)
  543. png_error(png_ptr, "internal progressive row size calculation error");
  544. #ifdef PNG_READ_INTERLACING_SUPPORTED
  545. /* Expand interlaced rows to full size */
  546. if (png_ptr->interlaced != 0 &&
  547. (png_ptr->transformations & PNG_INTERLACE) != 0)
  548. {
  549. if (png_ptr->pass < 6)
  550. png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass,
  551. png_ptr->transformations);
  552. switch (png_ptr->pass)
  553. {
  554. case 0:
  555. {
  556. int i;
  557. for (i = 0; i < 8 && png_ptr->pass == 0; i++)
  558. {
  559. png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  560. png_read_push_finish_row(png_ptr); /* Updates png_ptr->pass */
  561. }
  562. if (png_ptr->pass == 2) /* Pass 1 might be empty */
  563. {
  564. for (i = 0; i < 4 && png_ptr->pass == 2; i++)
  565. {
  566. png_push_have_row(png_ptr, NULL);
  567. png_read_push_finish_row(png_ptr);
  568. }
  569. }
  570. if (png_ptr->pass == 4 && png_ptr->height <= 4)
  571. {
  572. for (i = 0; i < 2 && png_ptr->pass == 4; i++)
  573. {
  574. png_push_have_row(png_ptr, NULL);
  575. png_read_push_finish_row(png_ptr);
  576. }
  577. }
  578. if (png_ptr->pass == 6 && png_ptr->height <= 4)
  579. {
  580. png_push_have_row(png_ptr, NULL);
  581. png_read_push_finish_row(png_ptr);
  582. }
  583. break;
  584. }
  585. case 1:
  586. {
  587. int i;
  588. for (i = 0; i < 8 && png_ptr->pass == 1; i++)
  589. {
  590. png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  591. png_read_push_finish_row(png_ptr);
  592. }
  593. if (png_ptr->pass == 2) /* Skip top 4 generated rows */
  594. {
  595. for (i = 0; i < 4 && png_ptr->pass == 2; i++)
  596. {
  597. png_push_have_row(png_ptr, NULL);
  598. png_read_push_finish_row(png_ptr);
  599. }
  600. }
  601. break;
  602. }
  603. case 2:
  604. {
  605. int i;
  606. for (i = 0; i < 4 && png_ptr->pass == 2; i++)
  607. {
  608. png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  609. png_read_push_finish_row(png_ptr);
  610. }
  611. for (i = 0; i < 4 && png_ptr->pass == 2; i++)
  612. {
  613. png_push_have_row(png_ptr, NULL);
  614. png_read_push_finish_row(png_ptr);
  615. }
  616. if (png_ptr->pass == 4) /* Pass 3 might be empty */
  617. {
  618. for (i = 0; i < 2 && png_ptr->pass == 4; i++)
  619. {
  620. png_push_have_row(png_ptr, NULL);
  621. png_read_push_finish_row(png_ptr);
  622. }
  623. }
  624. break;
  625. }
  626. case 3:
  627. {
  628. int i;
  629. for (i = 0; i < 4 && png_ptr->pass == 3; i++)
  630. {
  631. png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  632. png_read_push_finish_row(png_ptr);
  633. }
  634. if (png_ptr->pass == 4) /* Skip top two generated rows */
  635. {
  636. for (i = 0; i < 2 && png_ptr->pass == 4; i++)
  637. {
  638. png_push_have_row(png_ptr, NULL);
  639. png_read_push_finish_row(png_ptr);
  640. }
  641. }
  642. break;
  643. }
  644. case 4:
  645. {
  646. int i;
  647. for (i = 0; i < 2 && png_ptr->pass == 4; i++)
  648. {
  649. png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  650. png_read_push_finish_row(png_ptr);
  651. }
  652. for (i = 0; i < 2 && png_ptr->pass == 4; i++)
  653. {
  654. png_push_have_row(png_ptr, NULL);
  655. png_read_push_finish_row(png_ptr);
  656. }
  657. if (png_ptr->pass == 6) /* Pass 5 might be empty */
  658. {
  659. png_push_have_row(png_ptr, NULL);
  660. png_read_push_finish_row(png_ptr);
  661. }
  662. break;
  663. }
  664. case 5:
  665. {
  666. int i;
  667. for (i = 0; i < 2 && png_ptr->pass == 5; i++)
  668. {
  669. png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  670. png_read_push_finish_row(png_ptr);
  671. }
  672. if (png_ptr->pass == 6) /* Skip top generated row */
  673. {
  674. png_push_have_row(png_ptr, NULL);
  675. png_read_push_finish_row(png_ptr);
  676. }
  677. break;
  678. }
  679. default:
  680. case 6:
  681. {
  682. png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  683. png_read_push_finish_row(png_ptr);
  684. if (png_ptr->pass != 6)
  685. break;
  686. png_push_have_row(png_ptr, NULL);
  687. png_read_push_finish_row(png_ptr);
  688. }
  689. }
  690. }
  691. else
  692. #endif
  693. {
  694. png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  695. png_read_push_finish_row(png_ptr);
  696. }
  697. }
  698. void /* PRIVATE */
  699. png_read_push_finish_row(png_structrp png_ptr)
  700. {
  701. png_ptr->row_number++;
  702. if (png_ptr->row_number < png_ptr->num_rows)
  703. return;
  704. #ifdef PNG_READ_INTERLACING_SUPPORTED
  705. if (png_ptr->interlaced != 0)
  706. {
  707. png_ptr->row_number = 0;
  708. memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
  709. do
  710. {
  711. png_ptr->pass++;
  712. if ((png_ptr->pass == 1 && png_ptr->width < 5) ||
  713. (png_ptr->pass == 3 && png_ptr->width < 3) ||
  714. (png_ptr->pass == 5 && png_ptr->width < 2))
  715. png_ptr->pass++;
  716. if (png_ptr->pass > 7)
  717. png_ptr->pass--;
  718. if (png_ptr->pass >= 7)
  719. break;
  720. png_ptr->iwidth = (png_ptr->width +
  721. png_pass_inc[png_ptr->pass] - 1 -
  722. png_pass_start[png_ptr->pass]) /
  723. png_pass_inc[png_ptr->pass];
  724. if ((png_ptr->transformations & PNG_INTERLACE) != 0)
  725. break;
  726. png_ptr->num_rows = (png_ptr->height +
  727. png_pass_yinc[png_ptr->pass] - 1 -
  728. png_pass_ystart[png_ptr->pass]) /
  729. png_pass_yinc[png_ptr->pass];
  730. } while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0);
  731. }
  732. #endif /* READ_INTERLACING */
  733. }
  734. void /* PRIVATE */
  735. png_push_have_info(png_structrp png_ptr, png_inforp info_ptr)
  736. {
  737. if (png_ptr->info_fn != NULL)
  738. (*(png_ptr->info_fn))(png_ptr, info_ptr);
  739. }
  740. void /* PRIVATE */
  741. png_push_have_end(png_structrp png_ptr, png_inforp info_ptr)
  742. {
  743. if (png_ptr->end_fn != NULL)
  744. (*(png_ptr->end_fn))(png_ptr, info_ptr);
  745. }
  746. void /* PRIVATE */
  747. png_push_have_row(png_structrp png_ptr, png_bytep row)
  748. {
  749. if (png_ptr->row_fn != NULL)
  750. (*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number,
  751. (int)png_ptr->pass);
  752. }
  753. #ifdef PNG_READ_INTERLACING_SUPPORTED
  754. void PNGAPI
  755. png_progressive_combine_row(png_const_structrp png_ptr, png_bytep old_row,
  756. png_const_bytep new_row)
  757. {
  758. if (png_ptr == NULL)
  759. return;
  760. /* new_row is a flag here - if it is NULL then the app callback was called
  761. * from an empty row (see the calls to png_struct::row_fn below), otherwise
  762. * it must be png_ptr->row_buf+1
  763. */
  764. if (new_row != NULL)
  765. png_combine_row(png_ptr, old_row, 1/*blocky display*/);
  766. }
  767. #endif /* READ_INTERLACING */
  768. void PNGAPI
  769. png_set_progressive_read_fn(png_structrp png_ptr, png_voidp progressive_ptr,
  770. png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
  771. png_progressive_end_ptr end_fn)
  772. {
  773. if (png_ptr == NULL)
  774. return;
  775. png_ptr->info_fn = info_fn;
  776. png_ptr->row_fn = row_fn;
  777. png_ptr->end_fn = end_fn;
  778. png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer);
  779. }
  780. png_voidp PNGAPI
  781. png_get_progressive_ptr(png_const_structrp png_ptr)
  782. {
  783. if (png_ptr == NULL)
  784. return NULL;
  785. return png_ptr->io_ptr;
  786. }
  787. #endif /* PROGRESSIVE_READ */