pngpread.c 31 KB

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