pngpread.c 33 KB

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