pngpread.c 27 KB

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