pngpread.c 33 KB

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