pngpread.c 35 KB

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