pngpread.c 31 KB

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