Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3632 lines
98 KiB

  1. /* pngrutil.c - utilities to read a PNG file
  2. *
  3. * Last changed in libpng 1.5.2 [March 31, 2011]
  4. * Copyright (c) 1998-2011 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. * This file contains routines that are only called from within
  13. * libpng itself during the course of reading an image.
  14. */
  15. #include "pngpriv.h"
  16. #ifdef PNG_READ_SUPPORTED
  17. #define png_strtod(p,a,b) strtod(a,b)
  18. png_uint_32 PNGAPI
  19. png_get_uint_31(png_structp png_ptr, png_const_bytep buf)
  20. {
  21. png_uint_32 uval = png_get_uint_32(buf);
  22. if (uval > PNG_UINT_31_MAX)
  23. png_error(png_ptr, "PNG unsigned integer out of range");
  24. return (uval);
  25. }
  26. #if defined(PNG_READ_gAMA_SUPPORTED) || defined(PNG_READ_cHRM_SUPPORTED)
  27. /* The following is a variation on the above for use with the fixed
  28. * point values used for gAMA and cHRM. Instead of png_error it
  29. * issues a warning and returns (-1) - an invalid value because both
  30. * gAMA and cHRM use *unsigned* integers for fixed point values.
  31. */
  32. #define PNG_FIXED_ERROR (-1)
  33. static png_fixed_point /* PRIVATE */
  34. png_get_fixed_point(png_structp png_ptr, png_const_bytep buf)
  35. {
  36. png_uint_32 uval = png_get_uint_32(buf);
  37. if (uval <= PNG_UINT_31_MAX)
  38. return (png_fixed_point)uval; /* known to be in range */
  39. /* The caller can turn off the warning by passing NULL. */
  40. if (png_ptr != NULL)
  41. png_warning(png_ptr, "PNG fixed point integer out of range");
  42. return PNG_FIXED_ERROR;
  43. }
  44. #endif
  45. #ifdef PNG_READ_INT_FUNCTIONS_SUPPORTED
  46. /* NOTE: the read macros will obscure these definitions, so that if
  47. * PNG_USE_READ_MACROS is set the library will not use them internally,
  48. * but the APIs will still be available externally.
  49. *
  50. * The parentheses around "PNGAPI function_name" in the following three
  51. * functions are necessary because they allow the macros to co-exist with
  52. * these (unused but exported) functions.
  53. */
  54. /* Grab an unsigned 32-bit integer from a buffer in big-endian format. */
  55. png_uint_32 (PNGAPI
  56. png_get_uint_32)(png_const_bytep buf)
  57. {
  58. png_uint_32 uval =
  59. ((png_uint_32)(*(buf )) << 24) +
  60. ((png_uint_32)(*(buf + 1)) << 16) +
  61. ((png_uint_32)(*(buf + 2)) << 8) +
  62. ((png_uint_32)(*(buf + 3)) ) ;
  63. return uval;
  64. }
  65. /* Grab a signed 32-bit integer from a buffer in big-endian format. The
  66. * data is stored in the PNG file in two's complement format and there
  67. * is no guarantee that a 'png_int_32' is exactly 32 bits, therefore
  68. * the following code does a two's complement to native conversion.
  69. */
  70. png_int_32 (PNGAPI
  71. png_get_int_32)(png_const_bytep buf)
  72. {
  73. png_uint_32 uval = png_get_uint_32(buf);
  74. if ((uval & 0x80000000L) == 0) /* non-negative */
  75. return uval;
  76. uval = (uval ^ 0xffffffffL) + 1; /* 2's complement: -x = ~x+1 */
  77. return -(png_int_32)uval;
  78. }
  79. /* Grab an unsigned 16-bit integer from a buffer in big-endian format. */
  80. png_uint_16 (PNGAPI
  81. png_get_uint_16)(png_const_bytep buf)
  82. {
  83. /* ANSI-C requires an int value to accomodate at least 16 bits so this
  84. * works and allows the compiler not to worry about possible narrowing
  85. * on 32 bit systems. (Pre-ANSI systems did not make integers smaller
  86. * than 16 bits either.)
  87. */
  88. unsigned int val =
  89. ((unsigned int)(*buf) << 8) +
  90. ((unsigned int)(*(buf + 1)));
  91. return (png_uint_16)val;
  92. }
  93. #endif /* PNG_READ_INT_FUNCTIONS_SUPPORTED */
  94. /* Read and check the PNG file signature */
  95. void /* PRIVATE */
  96. png_read_sig(png_structp png_ptr, png_infop info_ptr)
  97. {
  98. png_size_t num_checked, num_to_check;
  99. /* Exit if the user application does not expect a signature. */
  100. if (png_ptr->sig_bytes >= 8)
  101. return;
  102. num_checked = png_ptr->sig_bytes;
  103. num_to_check = 8 - num_checked;
  104. #ifdef PNG_IO_STATE_SUPPORTED
  105. png_ptr->io_state = PNG_IO_READING | PNG_IO_SIGNATURE;
  106. #endif
  107. /* The signature must be serialized in a single I/O call. */
  108. png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
  109. png_ptr->sig_bytes = 8;
  110. if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
  111. {
  112. if (num_checked < 4 &&
  113. png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
  114. png_error(png_ptr, "Not a PNG file");
  115. else
  116. png_error(png_ptr, "PNG file corrupted by ASCII conversion");
  117. }
  118. if (num_checked < 3)
  119. png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
  120. }
  121. /* Read the chunk header (length + type name).
  122. * Put the type name into png_ptr->chunk_name, and return the length.
  123. */
  124. png_uint_32 /* PRIVATE */
  125. png_read_chunk_header(png_structp png_ptr)
  126. {
  127. png_byte buf[8];
  128. png_uint_32 length;
  129. #ifdef PNG_IO_STATE_SUPPORTED
  130. png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_HDR;
  131. #endif
  132. /* Read the length and the chunk name.
  133. * This must be performed in a single I/O call.
  134. */
  135. png_read_data(png_ptr, buf, 8);
  136. length = png_get_uint_31(png_ptr, buf);
  137. /* Put the chunk name into png_ptr->chunk_name. */
  138. png_memcpy(png_ptr->chunk_name, buf + 4, 4);
  139. png_debug2(0, "Reading %s chunk, length = %u",
  140. png_ptr->chunk_name, length);
  141. /* Reset the crc and run it over the chunk name. */
  142. png_reset_crc(png_ptr);
  143. png_calculate_crc(png_ptr, png_ptr->chunk_name, 4);
  144. /* Check to see if chunk name is valid. */
  145. png_check_chunk_name(png_ptr, png_ptr->chunk_name);
  146. #ifdef PNG_IO_STATE_SUPPORTED
  147. png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_DATA;
  148. #endif
  149. return length;
  150. }
  151. /* Read data, and (optionally) run it through the CRC. */
  152. void /* PRIVATE */
  153. png_crc_read(png_structp png_ptr, png_bytep buf, png_size_t length)
  154. {
  155. if (png_ptr == NULL)
  156. return;
  157. png_read_data(png_ptr, buf, length);
  158. png_calculate_crc(png_ptr, buf, length);
  159. }
  160. /* Optionally skip data and then check the CRC. Depending on whether we
  161. * are reading a ancillary or critical chunk, and how the program has set
  162. * things up, we may calculate the CRC on the data and print a message.
  163. * Returns '1' if there was a CRC error, '0' otherwise.
  164. */
  165. int /* PRIVATE */
  166. png_crc_finish(png_structp png_ptr, png_uint_32 skip)
  167. {
  168. png_size_t i;
  169. png_size_t istop = png_ptr->zbuf_size;
  170. for (i = (png_size_t)skip; i > istop; i -= istop)
  171. {
  172. png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
  173. }
  174. if (i)
  175. {
  176. png_crc_read(png_ptr, png_ptr->zbuf, i);
  177. }
  178. if (png_crc_error(png_ptr))
  179. {
  180. if (((png_ptr->chunk_name[0] & 0x20) && /* Ancillary */
  181. !(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)) ||
  182. (!(png_ptr->chunk_name[0] & 0x20) && /* Critical */
  183. (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE)))
  184. {
  185. png_chunk_warning(png_ptr, "CRC error");
  186. }
  187. else
  188. {
  189. png_chunk_benign_error(png_ptr, "CRC error");
  190. return (0);
  191. }
  192. return (1);
  193. }
  194. return (0);
  195. }
  196. /* Compare the CRC stored in the PNG file with that calculated by libpng from
  197. * the data it has read thus far.
  198. */
  199. int /* PRIVATE */
  200. png_crc_error(png_structp png_ptr)
  201. {
  202. png_byte crc_bytes[4];
  203. png_uint_32 crc;
  204. int need_crc = 1;
  205. if (png_ptr->chunk_name[0] & 0x20) /* ancillary */
  206. {
  207. if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
  208. (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
  209. need_crc = 0;
  210. }
  211. else /* critical */
  212. {
  213. if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
  214. need_crc = 0;
  215. }
  216. #ifdef PNG_IO_STATE_SUPPORTED
  217. png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_CRC;
  218. #endif
  219. /* The chunk CRC must be serialized in a single I/O call. */
  220. png_read_data(png_ptr, crc_bytes, 4);
  221. if (need_crc)
  222. {
  223. crc = png_get_uint_32(crc_bytes);
  224. return ((int)(crc != png_ptr->crc));
  225. }
  226. else
  227. return (0);
  228. }
  229. #if defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_READ_iTXt_SUPPORTED) || \
  230. defined(PNG_READ_iCCP_SUPPORTED)
  231. static png_size_t
  232. png_inflate(png_structp png_ptr, png_bytep data, png_size_t size,
  233. png_bytep output, png_size_t output_size)
  234. {
  235. png_size_t count = 0;
  236. /* zlib can't necessarily handle more than 65535 bytes at once (i.e. it can't
  237. * even necessarily handle 65536 bytes) because the type uInt is "16 bits or
  238. * more". Consequently it is necessary to chunk the input to zlib. This
  239. * code uses ZLIB_IO_MAX, from pngpriv.h, as the maximum (the maximum value
  240. * that can be stored in a uInt.) It is possible to set ZLIB_IO_MAX to a
  241. * lower value in pngpriv.h and this may sometimes have a performance
  242. * advantage, because it forces access of the input data to be separated from
  243. * at least some of the use by some period of time.
  244. */
  245. png_ptr->zstream.next_in = data;
  246. /* avail_in is set below from 'size' */
  247. png_ptr->zstream.avail_in = 0;
  248. while (1)
  249. {
  250. int ret, avail;
  251. /* The setting of 'avail_in' used to be outside the loop, by setting it
  252. * inside it is possible to chunk the input to zlib and simply rely on
  253. * zlib to advance the 'next_in' pointer. This allows arbitrary amounts o
  254. * data to be passed through zlib at the unavoidable cost of requiring a
  255. * window save (memcpy of up to 32768 output bytes) every ZLIB_IO_MAX
  256. * input bytes.
  257. */
  258. if (png_ptr->zstream.avail_in == 0 && size > 0)
  259. {
  260. if (size <= ZLIB_IO_MAX)
  261. {
  262. /* The value is less than ZLIB_IO_MAX so the cast is safe: */
  263. png_ptr->zstream.avail_in = (uInt)size;
  264. size = 0;
  265. }
  266. else
  267. {
  268. png_ptr->zstream.avail_in = ZLIB_IO_MAX;
  269. size -= ZLIB_IO_MAX;
  270. }
  271. }
  272. /* Reset the output buffer each time round - we empty it
  273. * after every inflate call.
  274. */
  275. png_ptr->zstream.next_out = png_ptr->zbuf;
  276. png_ptr->zstream.avail_out = png_ptr->zbuf_size;
  277. ret = inflate(&png_ptr->zstream, Z_NO_FLUSH);
  278. avail = png_ptr->zbuf_size - png_ptr->zstream.avail_out;
  279. /* First copy/count any new output - but only if we didn't
  280. * get an error code.
  281. */
  282. if ((ret == Z_OK || ret == Z_STREAM_END) && avail > 0)
  283. {
  284. png_size_t space = avail; /* > 0, see above */
  285. if (output != 0 && output_size > count)
  286. {
  287. png_size_t copy = output_size - count;
  288. if (space < copy)
  289. copy = space;
  290. png_memcpy(output + count, png_ptr->zbuf, copy);
  291. }
  292. count += space;
  293. }
  294. if (ret == Z_OK)
  295. continue;
  296. /* Termination conditions - always reset the zstream, it
  297. * must be left in inflateInit state.
  298. */
  299. png_ptr->zstream.avail_in = 0;
  300. inflateReset(&png_ptr->zstream);
  301. if (ret == Z_STREAM_END)
  302. return count; /* NOTE: may be zero. */
  303. /* Now handle the error codes - the API always returns 0
  304. * and the error message is dumped into the uncompressed
  305. * buffer if available.
  306. */
  307. {
  308. PNG_CONST char *msg;
  309. #ifdef PNG_CONSOLE_IO_SUPPORTED
  310. char umsg[52];
  311. #endif
  312. if (png_ptr->zstream.msg != 0)
  313. msg = png_ptr->zstream.msg;
  314. else
  315. {
  316. #ifdef PNG_CONSOLE_IO_SUPPORTED
  317. switch (ret)
  318. {
  319. case Z_BUF_ERROR:
  320. msg = "Buffer error in compressed datastream in %s chunk";
  321. break;
  322. case Z_DATA_ERROR:
  323. msg = "Data error in compressed datastream in %s chunk";
  324. break;
  325. default:
  326. msg = "Incomplete compressed datastream in %s chunk";
  327. break;
  328. }
  329. png_snprintf(umsg, sizeof umsg, msg, png_ptr->chunk_name);
  330. msg = umsg;
  331. #else
  332. msg = "Damaged compressed datastream in chunk other than IDAT";
  333. #endif
  334. }
  335. png_warning(png_ptr, msg);
  336. }
  337. /* 0 means an error - notice that this code simply ignores
  338. * zero length compressed chunks as a result.
  339. */
  340. return 0;
  341. }
  342. }
  343. /*
  344. * Decompress trailing data in a chunk. The assumption is that chunkdata
  345. * points at an allocated area holding the contents of a chunk with a
  346. * trailing compressed part. What we get back is an allocated area
  347. * holding the original prefix part and an uncompressed version of the
  348. * trailing part (the malloc area passed in is freed).
  349. */
  350. void /* PRIVATE */
  351. png_decompress_chunk(png_structp png_ptr, int comp_type,
  352. png_size_t chunklength,
  353. png_size_t prefix_size, png_size_t *newlength)
  354. {
  355. /* The caller should guarantee this */
  356. if (prefix_size > chunklength)
  357. {
  358. /* The recovery is to delete the chunk. */
  359. png_warning(png_ptr, "invalid chunklength");
  360. prefix_size = 0; /* To delete everything */
  361. }
  362. else if (comp_type == PNG_COMPRESSION_TYPE_BASE)
  363. {
  364. png_size_t expanded_size = png_inflate(png_ptr,
  365. (png_bytep)(png_ptr->chunkdata + prefix_size),
  366. chunklength - prefix_size,
  367. 0, /*output*/
  368. 0); /*output size*/
  369. /* Now check the limits on this chunk - if the limit fails the
  370. * compressed data will be removed, the prefix will remain.
  371. */
  372. #ifdef PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED
  373. if (png_ptr->user_chunk_malloc_max &&
  374. (prefix_size + expanded_size >= png_ptr->user_chunk_malloc_max - 1))
  375. #else
  376. # ifdef PNG_USER_CHUNK_MALLOC_MAX
  377. if ((PNG_USER_CHUNK_MALLOC_MAX > 0) &&
  378. prefix_size + expanded_size >= PNG_USER_CHUNK_MALLOC_MAX - 1)
  379. # endif
  380. #endif
  381. png_warning(png_ptr, "Exceeded size limit while expanding chunk");
  382. /* If the size is zero either there was an error and a message
  383. * has already been output (warning) or the size really is zero
  384. * and we have nothing to do - the code will exit through the
  385. * error case below.
  386. */
  387. #if defined(PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED) || \
  388. defined(PNG_USER_CHUNK_MALLOC_MAX)
  389. else if (expanded_size > 0)
  390. #else
  391. if (expanded_size > 0)
  392. #endif
  393. {
  394. /* Success (maybe) - really uncompress the chunk. */
  395. png_size_t new_size = 0;
  396. png_charp text = png_malloc_warn(png_ptr,
  397. prefix_size + expanded_size + 1);
  398. if (text != NULL)
  399. {
  400. png_memcpy(text, png_ptr->chunkdata, prefix_size);
  401. new_size = png_inflate(png_ptr,
  402. (png_bytep)(png_ptr->chunkdata + prefix_size),
  403. chunklength - prefix_size,
  404. (png_bytep)(text + prefix_size), expanded_size);
  405. text[prefix_size + expanded_size] = 0; /* just in case */
  406. if (new_size == expanded_size)
  407. {
  408. png_free(png_ptr, png_ptr->chunkdata);
  409. png_ptr->chunkdata = text;
  410. *newlength = prefix_size + expanded_size;
  411. return; /* The success return! */
  412. }
  413. png_warning(png_ptr, "png_inflate logic error");
  414. png_free(png_ptr, text);
  415. }
  416. else
  417. png_warning(png_ptr, "Not enough memory to decompress chunk");
  418. }
  419. }
  420. else /* if (comp_type != PNG_COMPRESSION_TYPE_BASE) */
  421. {
  422. #ifdef PNG_STDIO_SUPPORTED
  423. char umsg[50];
  424. png_snprintf(umsg, sizeof umsg,
  425. "Unknown zTXt compression type %d", comp_type);
  426. png_warning(png_ptr, umsg);
  427. #else
  428. png_warning(png_ptr, "Unknown zTXt compression type");
  429. #endif
  430. /* The recovery is to simply drop the data. */
  431. }
  432. /* Generic error return - leave the prefix, delete the compressed
  433. * data, reallocate the chunkdata to remove the potentially large
  434. * amount of compressed data.
  435. */
  436. {
  437. png_charp text = png_malloc_warn(png_ptr, prefix_size + 1);
  438. if (text != NULL)
  439. {
  440. if (prefix_size > 0)
  441. png_memcpy(text, png_ptr->chunkdata, prefix_size);
  442. png_free(png_ptr, png_ptr->chunkdata);
  443. png_ptr->chunkdata = text;
  444. /* This is an extra zero in the 'uncompressed' part. */
  445. *(png_ptr->chunkdata + prefix_size) = 0x00;
  446. }
  447. /* Ignore a malloc error here - it is safe. */
  448. }
  449. *newlength = prefix_size;
  450. }
  451. #endif
  452. /* Read and check the IDHR chunk */
  453. void /* PRIVATE */
  454. png_handle_IHDR(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  455. {
  456. png_byte buf[13];
  457. png_uint_32 width, height;
  458. int bit_depth, color_type, compression_type, filter_type;
  459. int interlace_type;
  460. png_debug(1, "in png_handle_IHDR");
  461. if (png_ptr->mode & PNG_HAVE_IHDR)
  462. png_error(png_ptr, "Out of place IHDR");
  463. /* Check the length */
  464. if (length != 13)
  465. png_error(png_ptr, "Invalid IHDR chunk");
  466. png_ptr->mode |= PNG_HAVE_IHDR;
  467. png_crc_read(png_ptr, buf, 13);
  468. png_crc_finish(png_ptr, 0);
  469. width = png_get_uint_31(png_ptr, buf);
  470. height = png_get_uint_31(png_ptr, buf + 4);
  471. bit_depth = buf[8];
  472. color_type = buf[9];
  473. compression_type = buf[10];
  474. filter_type = buf[11];
  475. interlace_type = buf[12];
  476. /* Set internal variables */
  477. png_ptr->width = width;
  478. png_ptr->height = height;
  479. png_ptr->bit_depth = (png_byte)bit_depth;
  480. png_ptr->interlaced = (png_byte)interlace_type;
  481. png_ptr->color_type = (png_byte)color_type;
  482. #ifdef PNG_MNG_FEATURES_SUPPORTED
  483. png_ptr->filter_type = (png_byte)filter_type;
  484. #endif
  485. png_ptr->compression_type = (png_byte)compression_type;
  486. /* Find number of channels */
  487. switch (png_ptr->color_type)
  488. {
  489. default: /* invalid, png_set_IHDR calls png_error */
  490. case PNG_COLOR_TYPE_GRAY:
  491. case PNG_COLOR_TYPE_PALETTE:
  492. png_ptr->channels = 1;
  493. break;
  494. case PNG_COLOR_TYPE_RGB:
  495. png_ptr->channels = 3;
  496. break;
  497. case PNG_COLOR_TYPE_GRAY_ALPHA:
  498. png_ptr->channels = 2;
  499. break;
  500. case PNG_COLOR_TYPE_RGB_ALPHA:
  501. png_ptr->channels = 4;
  502. break;
  503. }
  504. /* Set up other useful info */
  505. png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth *
  506. png_ptr->channels);
  507. png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width);
  508. png_debug1(3, "bit_depth = %d", png_ptr->bit_depth);
  509. png_debug1(3, "channels = %d", png_ptr->channels);
  510. png_debug1(3, "rowbytes = %lu", (unsigned long)png_ptr->rowbytes);
  511. png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
  512. color_type, interlace_type, compression_type, filter_type);
  513. }
  514. /* Read and check the palette */
  515. void /* PRIVATE */
  516. png_handle_PLTE(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  517. {
  518. png_color palette[PNG_MAX_PALETTE_LENGTH];
  519. int num, i;
  520. #ifdef PNG_POINTER_INDEXING_SUPPORTED
  521. png_colorp pal_ptr;
  522. #endif
  523. png_debug(1, "in png_handle_PLTE");
  524. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  525. png_error(png_ptr, "Missing IHDR before PLTE");
  526. else if (png_ptr->mode & PNG_HAVE_IDAT)
  527. {
  528. png_warning(png_ptr, "Invalid PLTE after IDAT");
  529. png_crc_finish(png_ptr, length);
  530. return;
  531. }
  532. else if (png_ptr->mode & PNG_HAVE_PLTE)
  533. png_error(png_ptr, "Duplicate PLTE chunk");
  534. png_ptr->mode |= PNG_HAVE_PLTE;
  535. if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
  536. {
  537. png_warning(png_ptr,
  538. "Ignoring PLTE chunk in grayscale PNG");
  539. png_crc_finish(png_ptr, length);
  540. return;
  541. }
  542. #ifndef PNG_READ_OPT_PLTE_SUPPORTED
  543. if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
  544. {
  545. png_crc_finish(png_ptr, length);
  546. return;
  547. }
  548. #endif
  549. if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3)
  550. {
  551. if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
  552. {
  553. png_warning(png_ptr, "Invalid palette chunk");
  554. png_crc_finish(png_ptr, length);
  555. return;
  556. }
  557. else
  558. {
  559. png_error(png_ptr, "Invalid palette chunk");
  560. }
  561. }
  562. num = (int)length / 3;
  563. #ifdef PNG_POINTER_INDEXING_SUPPORTED
  564. for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++)
  565. {
  566. png_byte buf[3];
  567. png_crc_read(png_ptr, buf, 3);
  568. pal_ptr->red = buf[0];
  569. pal_ptr->green = buf[1];
  570. pal_ptr->blue = buf[2];
  571. }
  572. #else
  573. for (i = 0; i < num; i++)
  574. {
  575. png_byte buf[3];
  576. png_crc_read(png_ptr, buf, 3);
  577. /* Don't depend upon png_color being any order */
  578. palette[i].red = buf[0];
  579. palette[i].green = buf[1];
  580. palette[i].blue = buf[2];
  581. }
  582. #endif
  583. /* If we actually need the PLTE chunk (ie for a paletted image), we do
  584. * whatever the normal CRC configuration tells us. However, if we
  585. * have an RGB image, the PLTE can be considered ancillary, so
  586. * we will act as though it is.
  587. */
  588. #ifndef PNG_READ_OPT_PLTE_SUPPORTED
  589. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  590. #endif
  591. {
  592. png_crc_finish(png_ptr, 0);
  593. }
  594. #ifndef PNG_READ_OPT_PLTE_SUPPORTED
  595. else if (png_crc_error(png_ptr)) /* Only if we have a CRC error */
  596. {
  597. /* If we don't want to use the data from an ancillary chunk,
  598. * we have two options: an error abort, or a warning and we
  599. * ignore the data in this chunk (which should be OK, since
  600. * it's considered ancillary for a RGB or RGBA image).
  601. */
  602. if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE))
  603. {
  604. if (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)
  605. {
  606. png_chunk_benign_error(png_ptr, "CRC error");
  607. }
  608. else
  609. {
  610. png_chunk_warning(png_ptr, "CRC error");
  611. return;
  612. }
  613. }
  614. /* Otherwise, we (optionally) emit a warning and use the chunk. */
  615. else if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN))
  616. {
  617. png_chunk_warning(png_ptr, "CRC error");
  618. }
  619. }
  620. #endif
  621. png_set_PLTE(png_ptr, info_ptr, palette, num);
  622. #ifdef PNG_READ_tRNS_SUPPORTED
  623. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  624. {
  625. if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
  626. {
  627. if (png_ptr->num_trans > (png_uint_16)num)
  628. {
  629. png_warning(png_ptr, "Truncating incorrect tRNS chunk length");
  630. png_ptr->num_trans = (png_uint_16)num;
  631. }
  632. if (info_ptr->num_trans > (png_uint_16)num)
  633. {
  634. png_warning(png_ptr, "Truncating incorrect info tRNS chunk length");
  635. info_ptr->num_trans = (png_uint_16)num;
  636. }
  637. }
  638. }
  639. #endif
  640. }
  641. void /* PRIVATE */
  642. png_handle_IEND(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  643. {
  644. png_debug(1, "in png_handle_IEND");
  645. if (!(png_ptr->mode & PNG_HAVE_IHDR) || !(png_ptr->mode & PNG_HAVE_IDAT))
  646. {
  647. png_error(png_ptr, "No image in file");
  648. }
  649. png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND);
  650. if (length != 0)
  651. {
  652. png_warning(png_ptr, "Incorrect IEND chunk length");
  653. }
  654. png_crc_finish(png_ptr, length);
  655. PNG_UNUSED(info_ptr) /* Quiet compiler warnings about unused info_ptr */
  656. }
  657. #ifdef PNG_READ_gAMA_SUPPORTED
  658. void /* PRIVATE */
  659. png_handle_gAMA(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  660. {
  661. png_fixed_point igamma;
  662. png_byte buf[4];
  663. png_debug(1, "in png_handle_gAMA");
  664. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  665. png_error(png_ptr, "Missing IHDR before gAMA");
  666. else if (png_ptr->mode & PNG_HAVE_IDAT)
  667. {
  668. png_warning(png_ptr, "Invalid gAMA after IDAT");
  669. png_crc_finish(png_ptr, length);
  670. return;
  671. }
  672. else if (png_ptr->mode & PNG_HAVE_PLTE)
  673. /* Should be an error, but we can cope with it */
  674. png_warning(png_ptr, "Out of place gAMA chunk");
  675. if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA)
  676. #ifdef PNG_READ_sRGB_SUPPORTED
  677. && !(info_ptr->valid & PNG_INFO_sRGB)
  678. #endif
  679. )
  680. {
  681. png_warning(png_ptr, "Duplicate gAMA chunk");
  682. png_crc_finish(png_ptr, length);
  683. return;
  684. }
  685. if (length != 4)
  686. {
  687. png_warning(png_ptr, "Incorrect gAMA chunk length");
  688. png_crc_finish(png_ptr, length);
  689. return;
  690. }
  691. png_crc_read(png_ptr, buf, 4);
  692. if (png_crc_finish(png_ptr, 0))
  693. return;
  694. igamma = png_get_fixed_point(NULL, buf);
  695. /* Check for zero gamma or an error. */
  696. if (igamma <= 0)
  697. {
  698. png_warning(png_ptr,
  699. "Ignoring gAMA chunk with out of range gamma");
  700. return;
  701. }
  702. # ifdef PNG_READ_sRGB_SUPPORTED
  703. if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB))
  704. {
  705. if (PNG_OUT_OF_RANGE(igamma, 45500L, 500))
  706. {
  707. png_warning(png_ptr,
  708. "Ignoring incorrect gAMA value when sRGB is also present");
  709. # ifdef PNG_CONSOLE_IO_SUPPORTED
  710. fprintf(stderr, "gamma = (%d/100000)", (int)igamma);
  711. # endif
  712. return;
  713. }
  714. }
  715. # endif /* PNG_READ_sRGB_SUPPORTED */
  716. # ifdef PNG_READ_GAMMA_SUPPORTED
  717. /* Gamma correction on read is supported. */
  718. png_ptr->gamma = igamma;
  719. # endif
  720. /* And set the 'info' structure members. */
  721. png_set_gAMA_fixed(png_ptr, info_ptr, igamma);
  722. }
  723. #endif
  724. #ifdef PNG_READ_sBIT_SUPPORTED
  725. void /* PRIVATE */
  726. png_handle_sBIT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  727. {
  728. png_size_t truelen;
  729. png_byte buf[4];
  730. png_debug(1, "in png_handle_sBIT");
  731. buf[0] = buf[1] = buf[2] = buf[3] = 0;
  732. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  733. png_error(png_ptr, "Missing IHDR before sBIT");
  734. else if (png_ptr->mode & PNG_HAVE_IDAT)
  735. {
  736. png_warning(png_ptr, "Invalid sBIT after IDAT");
  737. png_crc_finish(png_ptr, length);
  738. return;
  739. }
  740. else if (png_ptr->mode & PNG_HAVE_PLTE)
  741. {
  742. /* Should be an error, but we can cope with it */
  743. png_warning(png_ptr, "Out of place sBIT chunk");
  744. }
  745. if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT))
  746. {
  747. png_warning(png_ptr, "Duplicate sBIT chunk");
  748. png_crc_finish(png_ptr, length);
  749. return;
  750. }
  751. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  752. truelen = 3;
  753. else
  754. truelen = (png_size_t)png_ptr->channels;
  755. if (length != truelen || length > 4)
  756. {
  757. png_warning(png_ptr, "Incorrect sBIT chunk length");
  758. png_crc_finish(png_ptr, length);
  759. return;
  760. }
  761. png_crc_read(png_ptr, buf, truelen);
  762. if (png_crc_finish(png_ptr, 0))
  763. return;
  764. if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
  765. {
  766. png_ptr->sig_bit.red = buf[0];
  767. png_ptr->sig_bit.green = buf[1];
  768. png_ptr->sig_bit.blue = buf[2];
  769. png_ptr->sig_bit.alpha = buf[3];
  770. }
  771. else
  772. {
  773. png_ptr->sig_bit.gray = buf[0];
  774. png_ptr->sig_bit.red = buf[0];
  775. png_ptr->sig_bit.green = buf[0];
  776. png_ptr->sig_bit.blue = buf[0];
  777. png_ptr->sig_bit.alpha = buf[1];
  778. }
  779. png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit));
  780. }
  781. #endif
  782. #ifdef PNG_READ_cHRM_SUPPORTED
  783. void /* PRIVATE */
  784. png_handle_cHRM(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  785. {
  786. png_byte buf[32];
  787. png_fixed_point x_white, y_white, x_red, y_red, x_green, y_green, x_blue,
  788. y_blue;
  789. png_debug(1, "in png_handle_cHRM");
  790. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  791. png_error(png_ptr, "Missing IHDR before cHRM");
  792. else if (png_ptr->mode & PNG_HAVE_IDAT)
  793. {
  794. png_warning(png_ptr, "Invalid cHRM after IDAT");
  795. png_crc_finish(png_ptr, length);
  796. return;
  797. }
  798. else if (png_ptr->mode & PNG_HAVE_PLTE)
  799. /* Should be an error, but we can cope with it */
  800. png_warning(png_ptr, "Missing PLTE before cHRM");
  801. if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM)
  802. # ifdef PNG_READ_sRGB_SUPPORTED
  803. && !(info_ptr->valid & PNG_INFO_sRGB)
  804. # endif
  805. )
  806. {
  807. png_warning(png_ptr, "Duplicate cHRM chunk");
  808. png_crc_finish(png_ptr, length);
  809. return;
  810. }
  811. if (length != 32)
  812. {
  813. png_warning(png_ptr, "Incorrect cHRM chunk length");
  814. png_crc_finish(png_ptr, length);
  815. return;
  816. }
  817. png_crc_read(png_ptr, buf, 32);
  818. if (png_crc_finish(png_ptr, 0))
  819. return;
  820. x_white = png_get_fixed_point(NULL, buf);
  821. y_white = png_get_fixed_point(NULL, buf + 4);
  822. x_red = png_get_fixed_point(NULL, buf + 8);
  823. y_red = png_get_fixed_point(NULL, buf + 12);
  824. x_green = png_get_fixed_point(NULL, buf + 16);
  825. y_green = png_get_fixed_point(NULL, buf + 20);
  826. x_blue = png_get_fixed_point(NULL, buf + 24);
  827. y_blue = png_get_fixed_point(NULL, buf + 28);
  828. if (x_white == PNG_FIXED_ERROR ||
  829. y_white == PNG_FIXED_ERROR ||
  830. x_red == PNG_FIXED_ERROR ||
  831. y_red == PNG_FIXED_ERROR ||
  832. x_green == PNG_FIXED_ERROR ||
  833. y_green == PNG_FIXED_ERROR ||
  834. x_blue == PNG_FIXED_ERROR ||
  835. y_blue == PNG_FIXED_ERROR)
  836. {
  837. png_warning(png_ptr, "Ignoring cHRM chunk with negative chromaticities");
  838. return;
  839. }
  840. #ifdef PNG_READ_sRGB_SUPPORTED
  841. if ((info_ptr != NULL) && (info_ptr->valid & PNG_INFO_sRGB))
  842. {
  843. if (PNG_OUT_OF_RANGE(x_white, 31270, 1000) ||
  844. PNG_OUT_OF_RANGE(y_white, 32900, 1000) ||
  845. PNG_OUT_OF_RANGE(x_red, 64000L, 1000) ||
  846. PNG_OUT_OF_RANGE(y_red, 33000, 1000) ||
  847. PNG_OUT_OF_RANGE(x_green, 30000, 1000) ||
  848. PNG_OUT_OF_RANGE(y_green, 60000L, 1000) ||
  849. PNG_OUT_OF_RANGE(x_blue, 15000, 1000) ||
  850. PNG_OUT_OF_RANGE(y_blue, 6000, 1000))
  851. {
  852. png_warning(png_ptr,
  853. "Ignoring incorrect cHRM value when sRGB is also present");
  854. #ifdef PNG_CONSOLE_IO_SUPPORTED
  855. fprintf(stderr, "wx=%d, wy=%d, rx=%d, ry=%d\n",
  856. x_white, y_white, x_red, y_red);
  857. fprintf(stderr, "gx=%d, gy=%d, bx=%d, by=%d\n",
  858. x_green, y_green, x_blue, y_blue);
  859. #endif /* PNG_CONSOLE_IO_SUPPORTED */
  860. }
  861. return;
  862. }
  863. #endif /* PNG_READ_sRGB_SUPPORTED */
  864. png_set_cHRM_fixed(png_ptr, info_ptr, x_white, y_white, x_red, y_red,
  865. x_green, y_green, x_blue, y_blue);
  866. }
  867. #endif
  868. #ifdef PNG_READ_sRGB_SUPPORTED
  869. void /* PRIVATE */
  870. png_handle_sRGB(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  871. {
  872. int intent;
  873. png_byte buf[1];
  874. png_debug(1, "in png_handle_sRGB");
  875. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  876. png_error(png_ptr, "Missing IHDR before sRGB");
  877. else if (png_ptr->mode & PNG_HAVE_IDAT)
  878. {
  879. png_warning(png_ptr, "Invalid sRGB after IDAT");
  880. png_crc_finish(png_ptr, length);
  881. return;
  882. }
  883. else if (png_ptr->mode & PNG_HAVE_PLTE)
  884. /* Should be an error, but we can cope with it */
  885. png_warning(png_ptr, "Out of place sRGB chunk");
  886. if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB))
  887. {
  888. png_warning(png_ptr, "Duplicate sRGB chunk");
  889. png_crc_finish(png_ptr, length);
  890. return;
  891. }
  892. if (length != 1)
  893. {
  894. png_warning(png_ptr, "Incorrect sRGB chunk length");
  895. png_crc_finish(png_ptr, length);
  896. return;
  897. }
  898. png_crc_read(png_ptr, buf, 1);
  899. if (png_crc_finish(png_ptr, 0))
  900. return;
  901. intent = buf[0];
  902. /* Check for bad intent */
  903. if (intent >= PNG_sRGB_INTENT_LAST)
  904. {
  905. png_warning(png_ptr, "Unknown sRGB intent");
  906. return;
  907. }
  908. #if defined(PNG_READ_gAMA_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED)
  909. if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA))
  910. {
  911. if (PNG_OUT_OF_RANGE(info_ptr->gamma, 45500L, 500))
  912. {
  913. png_warning(png_ptr,
  914. "Ignoring incorrect gAMA value when sRGB is also present");
  915. #ifdef PNG_CONSOLE_IO_SUPPORTED
  916. fprintf(stderr, "incorrect gamma=(%d/100000)\n", info_ptr->gamma);
  917. #endif
  918. }
  919. }
  920. #endif /* PNG_READ_gAMA_SUPPORTED */
  921. #ifdef PNG_READ_cHRM_SUPPORTED
  922. if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM))
  923. if (PNG_OUT_OF_RANGE(info_ptr->x_white, 31270, 1000) ||
  924. PNG_OUT_OF_RANGE(info_ptr->y_white, 32900, 1000) ||
  925. PNG_OUT_OF_RANGE(info_ptr->x_red, 64000L, 1000) ||
  926. PNG_OUT_OF_RANGE(info_ptr->y_red, 33000, 1000) ||
  927. PNG_OUT_OF_RANGE(info_ptr->x_green, 30000, 1000) ||
  928. PNG_OUT_OF_RANGE(info_ptr->y_green, 60000L, 1000) ||
  929. PNG_OUT_OF_RANGE(info_ptr->x_blue, 15000, 1000) ||
  930. PNG_OUT_OF_RANGE(info_ptr->y_blue, 6000, 1000))
  931. {
  932. png_warning(png_ptr,
  933. "Ignoring incorrect cHRM value when sRGB is also present");
  934. }
  935. #endif /* PNG_READ_cHRM_SUPPORTED */
  936. png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr, intent);
  937. }
  938. #endif /* PNG_READ_sRGB_SUPPORTED */
  939. #ifdef PNG_READ_iCCP_SUPPORTED
  940. void /* PRIVATE */
  941. png_handle_iCCP(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  942. /* Note: this does not properly handle chunks that are > 64K under DOS */
  943. {
  944. png_byte compression_type;
  945. png_bytep pC;
  946. png_charp profile;
  947. png_uint_32 skip = 0;
  948. png_uint_32 profile_size;
  949. png_alloc_size_t profile_length;
  950. png_size_t slength, prefix_length, data_length;
  951. png_debug(1, "in png_handle_iCCP");
  952. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  953. png_error(png_ptr, "Missing IHDR before iCCP");
  954. else if (png_ptr->mode & PNG_HAVE_IDAT)
  955. {
  956. png_warning(png_ptr, "Invalid iCCP after IDAT");
  957. png_crc_finish(png_ptr, length);
  958. return;
  959. }
  960. else if (png_ptr->mode & PNG_HAVE_PLTE)
  961. /* Should be an error, but we can cope with it */
  962. png_warning(png_ptr, "Out of place iCCP chunk");
  963. if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_iCCP))
  964. {
  965. png_warning(png_ptr, "Duplicate iCCP chunk");
  966. png_crc_finish(png_ptr, length);
  967. return;
  968. }
  969. #ifdef PNG_MAX_MALLOC_64K
  970. if (length > (png_uint_32)65535L)
  971. {
  972. png_warning(png_ptr, "iCCP chunk too large to fit in memory");
  973. skip = length - (png_uint_32)65535L;
  974. length = (png_uint_32)65535L;
  975. }
  976. #endif
  977. png_free(png_ptr, png_ptr->chunkdata);
  978. png_ptr->chunkdata = (png_charp)png_malloc(png_ptr, length + 1);
  979. slength = (png_size_t)length;
  980. png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
  981. if (png_crc_finish(png_ptr, skip))
  982. {
  983. png_free(png_ptr, png_ptr->chunkdata);
  984. png_ptr->chunkdata = NULL;
  985. return;
  986. }
  987. png_ptr->chunkdata[slength] = 0x00;
  988. for (profile = png_ptr->chunkdata; *profile; profile++)
  989. /* Empty loop to find end of name */ ;
  990. ++profile;
  991. /* There should be at least one zero (the compression type byte)
  992. * following the separator, and we should be on it
  993. */
  994. if (profile >= png_ptr->chunkdata + slength - 1)
  995. {
  996. png_free(png_ptr, png_ptr->chunkdata);
  997. png_ptr->chunkdata = NULL;
  998. png_warning(png_ptr, "Malformed iCCP chunk");
  999. return;
  1000. }
  1001. /* Compression_type should always be zero */
  1002. compression_type = *profile++;
  1003. if (compression_type)
  1004. {
  1005. png_warning(png_ptr, "Ignoring nonzero compression type in iCCP chunk");
  1006. compression_type = 0x00; /* Reset it to zero (libpng-1.0.6 through 1.0.8
  1007. wrote nonzero) */
  1008. }
  1009. prefix_length = profile - png_ptr->chunkdata;
  1010. png_decompress_chunk(png_ptr, compression_type,
  1011. slength, prefix_length, &data_length);
  1012. profile_length = data_length - prefix_length;
  1013. if (prefix_length > data_length || profile_length < 4)
  1014. {
  1015. png_free(png_ptr, png_ptr->chunkdata);
  1016. png_ptr->chunkdata = NULL;
  1017. png_warning(png_ptr, "Profile size field missing from iCCP chunk");
  1018. return;
  1019. }
  1020. /* Check the profile_size recorded in the first 32 bits of the ICC profile */
  1021. pC = (png_bytep)(png_ptr->chunkdata + prefix_length);
  1022. profile_size = ((*(pC )) << 24) |
  1023. ((*(pC + 1)) << 16) |
  1024. ((*(pC + 2)) << 8) |
  1025. ((*(pC + 3)) );
  1026. /* NOTE: the following guarantees that 'profile_length' fits into 32 bits,
  1027. * because profile_size is a 32 bit value.
  1028. */
  1029. if (profile_size < profile_length)
  1030. profile_length = profile_size;
  1031. /* And the following guarantees that profile_size == profile_length. */
  1032. if (profile_size > profile_length)
  1033. {
  1034. png_free(png_ptr, png_ptr->chunkdata);
  1035. png_ptr->chunkdata = NULL;
  1036. #ifdef PNG_STDIO_SUPPORTED
  1037. {
  1038. char umsg[80];
  1039. png_snprintf2(umsg, 80,
  1040. "Ignoring iCCP chunk with declared size = %u "
  1041. "and actual length = %u",
  1042. (unsigned int) profile_size,
  1043. (unsigned int) profile_length);
  1044. png_warning(png_ptr, umsg);
  1045. }
  1046. #else
  1047. png_warning(png_ptr,
  1048. "Ignoring iCCP chunk with uncompressed size mismatch");
  1049. #endif
  1050. return;
  1051. }
  1052. png_set_iCCP(png_ptr, info_ptr, png_ptr->chunkdata,
  1053. compression_type, (png_bytep)png_ptr->chunkdata + prefix_length,
  1054. profile_size);
  1055. png_free(png_ptr, png_ptr->chunkdata);
  1056. png_ptr->chunkdata = NULL;
  1057. }
  1058. #endif /* PNG_READ_iCCP_SUPPORTED */
  1059. #ifdef PNG_READ_sPLT_SUPPORTED
  1060. void /* PRIVATE */
  1061. png_handle_sPLT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1062. /* Note: this does not properly handle chunks that are > 64K under DOS */
  1063. {
  1064. png_bytep entry_start;
  1065. png_sPLT_t new_palette;
  1066. png_sPLT_entryp pp;
  1067. png_uint_32 data_length;
  1068. int entry_size, i;
  1069. png_uint_32 skip = 0;
  1070. png_size_t slength;
  1071. png_uint_32 dl;
  1072. png_size_t max_dl;
  1073. png_debug(1, "in png_handle_sPLT");
  1074. #ifdef PNG_USER_LIMITS_SUPPORTED
  1075. if (png_ptr->user_chunk_cache_max != 0)
  1076. {
  1077. if (png_ptr->user_chunk_cache_max == 1)
  1078. {
  1079. png_crc_finish(png_ptr, length);
  1080. return;
  1081. }
  1082. if (--png_ptr->user_chunk_cache_max == 1)
  1083. {
  1084. png_warning(png_ptr, "No space in chunk cache for sPLT");
  1085. png_crc_finish(png_ptr, length);
  1086. return;
  1087. }
  1088. }
  1089. #endif
  1090. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1091. png_error(png_ptr, "Missing IHDR before sPLT");
  1092. else if (png_ptr->mode & PNG_HAVE_IDAT)
  1093. {
  1094. png_warning(png_ptr, "Invalid sPLT after IDAT");
  1095. png_crc_finish(png_ptr, length);
  1096. return;
  1097. }
  1098. #ifdef PNG_MAX_MALLOC_64K
  1099. if (length > (png_uint_32)65535L)
  1100. {
  1101. png_warning(png_ptr, "sPLT chunk too large to fit in memory");
  1102. skip = length - (png_uint_32)65535L;
  1103. length = (png_uint_32)65535L;
  1104. }
  1105. #endif
  1106. png_free(png_ptr, png_ptr->chunkdata);
  1107. png_ptr->chunkdata = (png_charp)png_malloc(png_ptr, length + 1);
  1108. /* WARNING: this may break if size_t is less than 32 bits; it is assumed
  1109. * that the PNG_MAX_MALLOC_64K test is enabled in this case, but this is a
  1110. * potential breakage point if the types in pngconf.h aren't exactly right.
  1111. */
  1112. slength = (png_size_t)length;
  1113. png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
  1114. if (png_crc_finish(png_ptr, skip))
  1115. {
  1116. png_free(png_ptr, png_ptr->chunkdata);
  1117. png_ptr->chunkdata = NULL;
  1118. return;
  1119. }
  1120. png_ptr->chunkdata[slength] = 0x00;
  1121. for (entry_start = (png_bytep)png_ptr->chunkdata; *entry_start;
  1122. entry_start++)
  1123. /* Empty loop to find end of name */ ;
  1124. ++entry_start;
  1125. /* A sample depth should follow the separator, and we should be on it */
  1126. if (entry_start > (png_bytep)png_ptr->chunkdata + slength - 2)
  1127. {
  1128. png_free(png_ptr, png_ptr->chunkdata);
  1129. png_ptr->chunkdata = NULL;
  1130. png_warning(png_ptr, "malformed sPLT chunk");
  1131. return;
  1132. }
  1133. new_palette.depth = *entry_start++;
  1134. entry_size = (new_palette.depth == 8 ? 6 : 10);
  1135. /* This must fit in a png_uint_32 because it is derived from the original
  1136. * chunk data length (and use 'length', not 'slength' here for clarity -
  1137. * they are guaranteed to be the same, see the tests above.)
  1138. */
  1139. data_length = length - (png_uint_32)(entry_start -
  1140. (png_bytep)png_ptr->chunkdata);
  1141. /* Integrity-check the data length */
  1142. if (data_length % entry_size)
  1143. {
  1144. png_free(png_ptr, png_ptr->chunkdata);
  1145. png_ptr->chunkdata = NULL;
  1146. png_warning(png_ptr, "sPLT chunk has bad length");
  1147. return;
  1148. }
  1149. dl = (png_int_32)(data_length / entry_size);
  1150. max_dl = PNG_SIZE_MAX / png_sizeof(png_sPLT_entry);
  1151. if (dl > max_dl)
  1152. {
  1153. png_warning(png_ptr, "sPLT chunk too long");
  1154. return;
  1155. }
  1156. new_palette.nentries = (png_int_32)(data_length / entry_size);
  1157. new_palette.entries = (png_sPLT_entryp)png_malloc_warn(
  1158. png_ptr, new_palette.nentries * png_sizeof(png_sPLT_entry));
  1159. if (new_palette.entries == NULL)
  1160. {
  1161. png_warning(png_ptr, "sPLT chunk requires too much memory");
  1162. return;
  1163. }
  1164. #ifdef PNG_POINTER_INDEXING_SUPPORTED
  1165. for (i = 0; i < new_palette.nentries; i++)
  1166. {
  1167. pp = new_palette.entries + i;
  1168. if (new_palette.depth == 8)
  1169. {
  1170. pp->red = *entry_start++;
  1171. pp->green = *entry_start++;
  1172. pp->blue = *entry_start++;
  1173. pp->alpha = *entry_start++;
  1174. }
  1175. else
  1176. {
  1177. pp->red = png_get_uint_16(entry_start); entry_start += 2;
  1178. pp->green = png_get_uint_16(entry_start); entry_start += 2;
  1179. pp->blue = png_get_uint_16(entry_start); entry_start += 2;
  1180. pp->alpha = png_get_uint_16(entry_start); entry_start += 2;
  1181. }
  1182. pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
  1183. }
  1184. #else
  1185. pp = new_palette.entries;
  1186. for (i = 0; i < new_palette.nentries; i++)
  1187. {
  1188. if (new_palette.depth == 8)
  1189. {
  1190. pp[i].red = *entry_start++;
  1191. pp[i].green = *entry_start++;
  1192. pp[i].blue = *entry_start++;
  1193. pp[i].alpha = *entry_start++;
  1194. }
  1195. else
  1196. {
  1197. pp[i].red = png_get_uint_16(entry_start); entry_start += 2;
  1198. pp[i].green = png_get_uint_16(entry_start); entry_start += 2;
  1199. pp[i].blue = png_get_uint_16(entry_start); entry_start += 2;
  1200. pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2;
  1201. }
  1202. pp[i].frequency = png_get_uint_16(entry_start); entry_start += 2;
  1203. }
  1204. #endif
  1205. /* Discard all chunk data except the name and stash that */
  1206. new_palette.name = png_ptr->chunkdata;
  1207. png_set_sPLT(png_ptr, info_ptr, &new_palette, 1);
  1208. png_free(png_ptr, png_ptr->chunkdata);
  1209. png_ptr->chunkdata = NULL;
  1210. png_free(png_ptr, new_palette.entries);
  1211. }
  1212. #endif /* PNG_READ_sPLT_SUPPORTED */
  1213. #ifdef PNG_READ_tRNS_SUPPORTED
  1214. void /* PRIVATE */
  1215. png_handle_tRNS(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1216. {
  1217. png_byte readbuf[PNG_MAX_PALETTE_LENGTH];
  1218. png_debug(1, "in png_handle_tRNS");
  1219. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1220. png_error(png_ptr, "Missing IHDR before tRNS");
  1221. else if (png_ptr->mode & PNG_HAVE_IDAT)
  1222. {
  1223. png_warning(png_ptr, "Invalid tRNS after IDAT");
  1224. png_crc_finish(png_ptr, length);
  1225. return;
  1226. }
  1227. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
  1228. {
  1229. png_warning(png_ptr, "Duplicate tRNS chunk");
  1230. png_crc_finish(png_ptr, length);
  1231. return;
  1232. }
  1233. if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
  1234. {
  1235. png_byte buf[2];
  1236. if (length != 2)
  1237. {
  1238. png_warning(png_ptr, "Incorrect tRNS chunk length");
  1239. png_crc_finish(png_ptr, length);
  1240. return;
  1241. }
  1242. png_crc_read(png_ptr, buf, 2);
  1243. png_ptr->num_trans = 1;
  1244. png_ptr->trans_color.gray = png_get_uint_16(buf);
  1245. }
  1246. else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
  1247. {
  1248. png_byte buf[6];
  1249. if (length != 6)
  1250. {
  1251. png_warning(png_ptr, "Incorrect tRNS chunk length");
  1252. png_crc_finish(png_ptr, length);
  1253. return;
  1254. }
  1255. png_crc_read(png_ptr, buf, (png_size_t)length);
  1256. png_ptr->num_trans = 1;
  1257. png_ptr->trans_color.red = png_get_uint_16(buf);
  1258. png_ptr->trans_color.green = png_get_uint_16(buf + 2);
  1259. png_ptr->trans_color.blue = png_get_uint_16(buf + 4);
  1260. }
  1261. else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  1262. {
  1263. if (!(png_ptr->mode & PNG_HAVE_PLTE))
  1264. {
  1265. /* Should be an error, but we can cope with it. */
  1266. png_warning(png_ptr, "Missing PLTE before tRNS");
  1267. }
  1268. if (length > (png_uint_32)png_ptr->num_palette ||
  1269. length > PNG_MAX_PALETTE_LENGTH)
  1270. {
  1271. png_warning(png_ptr, "Incorrect tRNS chunk length");
  1272. png_crc_finish(png_ptr, length);
  1273. return;
  1274. }
  1275. if (length == 0)
  1276. {
  1277. png_warning(png_ptr, "Zero length tRNS chunk");
  1278. png_crc_finish(png_ptr, length);
  1279. return;
  1280. }
  1281. png_crc_read(png_ptr, readbuf, (png_size_t)length);
  1282. png_ptr->num_trans = (png_uint_16)length;
  1283. }
  1284. else
  1285. {
  1286. png_warning(png_ptr, "tRNS chunk not allowed with alpha channel");
  1287. png_crc_finish(png_ptr, length);
  1288. return;
  1289. }
  1290. if (png_crc_finish(png_ptr, 0))
  1291. {
  1292. png_ptr->num_trans = 0;
  1293. return;
  1294. }
  1295. png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans,
  1296. &(png_ptr->trans_color));
  1297. }
  1298. #endif
  1299. #ifdef PNG_READ_bKGD_SUPPORTED
  1300. void /* PRIVATE */
  1301. png_handle_bKGD(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1302. {
  1303. png_size_t truelen;
  1304. png_byte buf[6];
  1305. png_debug(1, "in png_handle_bKGD");
  1306. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1307. png_error(png_ptr, "Missing IHDR before bKGD");
  1308. else if (png_ptr->mode & PNG_HAVE_IDAT)
  1309. {
  1310. png_warning(png_ptr, "Invalid bKGD after IDAT");
  1311. png_crc_finish(png_ptr, length);
  1312. return;
  1313. }
  1314. else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
  1315. !(png_ptr->mode & PNG_HAVE_PLTE))
  1316. {
  1317. png_warning(png_ptr, "Missing PLTE before bKGD");
  1318. png_crc_finish(png_ptr, length);
  1319. return;
  1320. }
  1321. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD))
  1322. {
  1323. png_warning(png_ptr, "Duplicate bKGD chunk");
  1324. png_crc_finish(png_ptr, length);
  1325. return;
  1326. }
  1327. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  1328. truelen = 1;
  1329. else if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
  1330. truelen = 6;
  1331. else
  1332. truelen = 2;
  1333. if (length != truelen)
  1334. {
  1335. png_warning(png_ptr, "Incorrect bKGD chunk length");
  1336. png_crc_finish(png_ptr, length);
  1337. return;
  1338. }
  1339. png_crc_read(png_ptr, buf, truelen);
  1340. if (png_crc_finish(png_ptr, 0))
  1341. return;
  1342. /* We convert the index value into RGB components so that we can allow
  1343. * arbitrary RGB values for background when we have transparency, and
  1344. * so it is easy to determine the RGB values of the background color
  1345. * from the info_ptr struct.
  1346. */
  1347. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  1348. {
  1349. png_ptr->background.index = buf[0];
  1350. if (info_ptr && info_ptr->num_palette)
  1351. {
  1352. if (buf[0] >= info_ptr->num_palette)
  1353. {
  1354. png_warning(png_ptr, "Incorrect bKGD chunk index value");
  1355. return;
  1356. }
  1357. png_ptr->background.red =
  1358. (png_uint_16)png_ptr->palette[buf[0]].red;
  1359. png_ptr->background.green =
  1360. (png_uint_16)png_ptr->palette[buf[0]].green;
  1361. png_ptr->background.blue =
  1362. (png_uint_16)png_ptr->palette[buf[0]].blue;
  1363. }
  1364. }
  1365. else if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) /* GRAY */
  1366. {
  1367. png_ptr->background.red =
  1368. png_ptr->background.green =
  1369. png_ptr->background.blue =
  1370. png_ptr->background.gray = png_get_uint_16(buf);
  1371. }
  1372. else
  1373. {
  1374. png_ptr->background.red = png_get_uint_16(buf);
  1375. png_ptr->background.green = png_get_uint_16(buf + 2);
  1376. png_ptr->background.blue = png_get_uint_16(buf + 4);
  1377. }
  1378. png_set_bKGD(png_ptr, info_ptr, &(png_ptr->background));
  1379. }
  1380. #endif
  1381. #ifdef PNG_READ_hIST_SUPPORTED
  1382. void /* PRIVATE */
  1383. png_handle_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1384. {
  1385. unsigned int num, i;
  1386. png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH];
  1387. png_debug(1, "in png_handle_hIST");
  1388. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1389. png_error(png_ptr, "Missing IHDR before hIST");
  1390. else if (png_ptr->mode & PNG_HAVE_IDAT)
  1391. {
  1392. png_warning(png_ptr, "Invalid hIST after IDAT");
  1393. png_crc_finish(png_ptr, length);
  1394. return;
  1395. }
  1396. else if (!(png_ptr->mode & PNG_HAVE_PLTE))
  1397. {
  1398. png_warning(png_ptr, "Missing PLTE before hIST");
  1399. png_crc_finish(png_ptr, length);
  1400. return;
  1401. }
  1402. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST))
  1403. {
  1404. png_warning(png_ptr, "Duplicate hIST chunk");
  1405. png_crc_finish(png_ptr, length);
  1406. return;
  1407. }
  1408. num = length / 2 ;
  1409. if (num != (unsigned int)png_ptr->num_palette || num >
  1410. (unsigned int)PNG_MAX_PALETTE_LENGTH)
  1411. {
  1412. png_warning(png_ptr, "Incorrect hIST chunk length");
  1413. png_crc_finish(png_ptr, length);
  1414. return;
  1415. }
  1416. for (i = 0; i < num; i++)
  1417. {
  1418. png_byte buf[2];
  1419. png_crc_read(png_ptr, buf, 2);
  1420. readbuf[i] = png_get_uint_16(buf);
  1421. }
  1422. if (png_crc_finish(png_ptr, 0))
  1423. return;
  1424. png_set_hIST(png_ptr, info_ptr, readbuf);
  1425. }
  1426. #endif
  1427. #ifdef PNG_READ_pHYs_SUPPORTED
  1428. void /* PRIVATE */
  1429. png_handle_pHYs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1430. {
  1431. png_byte buf[9];
  1432. png_uint_32 res_x, res_y;
  1433. int unit_type;
  1434. png_debug(1, "in png_handle_pHYs");
  1435. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1436. png_error(png_ptr, "Missing IHDR before pHYs");
  1437. else if (png_ptr->mode & PNG_HAVE_IDAT)
  1438. {
  1439. png_warning(png_ptr, "Invalid pHYs after IDAT");
  1440. png_crc_finish(png_ptr, length);
  1441. return;
  1442. }
  1443. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
  1444. {
  1445. png_warning(png_ptr, "Duplicate pHYs chunk");
  1446. png_crc_finish(png_ptr, length);
  1447. return;
  1448. }
  1449. if (length != 9)
  1450. {
  1451. png_warning(png_ptr, "Incorrect pHYs chunk length");
  1452. png_crc_finish(png_ptr, length);
  1453. return;
  1454. }
  1455. png_crc_read(png_ptr, buf, 9);
  1456. if (png_crc_finish(png_ptr, 0))
  1457. return;
  1458. res_x = png_get_uint_32(buf);
  1459. res_y = png_get_uint_32(buf + 4);
  1460. unit_type = buf[8];
  1461. png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type);
  1462. }
  1463. #endif
  1464. #ifdef PNG_READ_oFFs_SUPPORTED
  1465. void /* PRIVATE */
  1466. png_handle_oFFs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1467. {
  1468. png_byte buf[9];
  1469. png_int_32 offset_x, offset_y;
  1470. int unit_type;
  1471. png_debug(1, "in png_handle_oFFs");
  1472. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1473. png_error(png_ptr, "Missing IHDR before oFFs");
  1474. else if (png_ptr->mode & PNG_HAVE_IDAT)
  1475. {
  1476. png_warning(png_ptr, "Invalid oFFs after IDAT");
  1477. png_crc_finish(png_ptr, length);
  1478. return;
  1479. }
  1480. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs))
  1481. {
  1482. png_warning(png_ptr, "Duplicate oFFs chunk");
  1483. png_crc_finish(png_ptr, length);
  1484. return;
  1485. }
  1486. if (length != 9)
  1487. {
  1488. png_warning(png_ptr, "Incorrect oFFs chunk length");
  1489. png_crc_finish(png_ptr, length);
  1490. return;
  1491. }
  1492. png_crc_read(png_ptr, buf, 9);
  1493. if (png_crc_finish(png_ptr, 0))
  1494. return;
  1495. offset_x = png_get_int_32(buf);
  1496. offset_y = png_get_int_32(buf + 4);
  1497. unit_type = buf[8];
  1498. png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);
  1499. }
  1500. #endif
  1501. #ifdef PNG_READ_pCAL_SUPPORTED
  1502. /* Read the pCAL chunk (described in the PNG Extensions document) */
  1503. void /* PRIVATE */
  1504. png_handle_pCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1505. {
  1506. png_int_32 X0, X1;
  1507. png_byte type, nparams;
  1508. png_charp buf, units, endptr;
  1509. png_charpp params;
  1510. png_size_t slength;
  1511. int i;
  1512. png_debug(1, "in png_handle_pCAL");
  1513. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1514. png_error(png_ptr, "Missing IHDR before pCAL");
  1515. else if (png_ptr->mode & PNG_HAVE_IDAT)
  1516. {
  1517. png_warning(png_ptr, "Invalid pCAL after IDAT");
  1518. png_crc_finish(png_ptr, length);
  1519. return;
  1520. }
  1521. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL))
  1522. {
  1523. png_warning(png_ptr, "Duplicate pCAL chunk");
  1524. png_crc_finish(png_ptr, length);
  1525. return;
  1526. }
  1527. png_debug1(2, "Allocating and reading pCAL chunk data (%u bytes)",
  1528. length + 1);
  1529. png_free(png_ptr, png_ptr->chunkdata);
  1530. png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
  1531. if (png_ptr->chunkdata == NULL)
  1532. {
  1533. png_warning(png_ptr, "No memory for pCAL purpose");
  1534. return;
  1535. }
  1536. slength = (png_size_t)length;
  1537. png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
  1538. if (png_crc_finish(png_ptr, 0))
  1539. {
  1540. png_free(png_ptr, png_ptr->chunkdata);
  1541. png_ptr->chunkdata = NULL;
  1542. return;
  1543. }
  1544. png_ptr->chunkdata[slength] = 0x00; /* Null terminate the last string */
  1545. png_debug(3, "Finding end of pCAL purpose string");
  1546. for (buf = png_ptr->chunkdata; *buf; buf++)
  1547. /* Empty loop */ ;
  1548. endptr = png_ptr->chunkdata + slength;
  1549. /* We need to have at least 12 bytes after the purpose string
  1550. * in order to get the parameter information.
  1551. */
  1552. if (endptr <= buf + 12)
  1553. {
  1554. png_warning(png_ptr, "Invalid pCAL data");
  1555. png_free(png_ptr, png_ptr->chunkdata);
  1556. png_ptr->chunkdata = NULL;
  1557. return;
  1558. }
  1559. png_debug(3, "Reading pCAL X0, X1, type, nparams, and units");
  1560. X0 = png_get_int_32((png_bytep)buf+1);
  1561. X1 = png_get_int_32((png_bytep)buf+5);
  1562. type = buf[9];
  1563. nparams = buf[10];
  1564. units = buf + 11;
  1565. png_debug(3, "Checking pCAL equation type and number of parameters");
  1566. /* Check that we have the right number of parameters for known
  1567. * equation types.
  1568. */
  1569. if ((type == PNG_EQUATION_LINEAR && nparams != 2) ||
  1570. (type == PNG_EQUATION_BASE_E && nparams != 3) ||
  1571. (type == PNG_EQUATION_ARBITRARY && nparams != 3) ||
  1572. (type == PNG_EQUATION_HYPERBOLIC && nparams != 4))
  1573. {
  1574. png_warning(png_ptr, "Invalid pCAL parameters for equation type");
  1575. png_free(png_ptr, png_ptr->chunkdata);
  1576. png_ptr->chunkdata = NULL;
  1577. return;
  1578. }
  1579. else if (type >= PNG_EQUATION_LAST)
  1580. {
  1581. png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
  1582. }
  1583. for (buf = units; *buf; buf++)
  1584. /* Empty loop to move past the units string. */ ;
  1585. png_debug(3, "Allocating pCAL parameters array");
  1586. params = (png_charpp)png_malloc_warn(png_ptr,
  1587. (png_size_t)(nparams * png_sizeof(png_charp)));
  1588. if (params == NULL)
  1589. {
  1590. png_free(png_ptr, png_ptr->chunkdata);
  1591. png_ptr->chunkdata = NULL;
  1592. png_warning(png_ptr, "No memory for pCAL params");
  1593. return;
  1594. }
  1595. /* Get pointers to the start of each parameter string. */
  1596. for (i = 0; i < (int)nparams; i++)
  1597. {
  1598. buf++; /* Skip the null string terminator from previous parameter. */
  1599. png_debug1(3, "Reading pCAL parameter %d", i);
  1600. for (params[i] = buf; buf <= endptr && *buf != 0x00; buf++)
  1601. /* Empty loop to move past each parameter string */ ;
  1602. /* Make sure we haven't run out of data yet */
  1603. if (buf > endptr)
  1604. {
  1605. png_warning(png_ptr, "Invalid pCAL data");
  1606. png_free(png_ptr, png_ptr->chunkdata);
  1607. png_ptr->chunkdata = NULL;
  1608. png_free(png_ptr, params);
  1609. return;
  1610. }
  1611. }
  1612. png_set_pCAL(png_ptr, info_ptr, png_ptr->chunkdata, X0, X1, type, nparams,
  1613. units, params);
  1614. png_free(png_ptr, png_ptr->chunkdata);
  1615. png_ptr->chunkdata = NULL;
  1616. png_free(png_ptr, params);
  1617. }
  1618. #endif
  1619. #ifdef PNG_READ_sCAL_SUPPORTED
  1620. /* Read the sCAL chunk */
  1621. void /* PRIVATE */
  1622. png_handle_sCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1623. {
  1624. png_size_t slength, i;
  1625. int state;
  1626. png_debug(1, "in png_handle_sCAL");
  1627. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1628. png_error(png_ptr, "Missing IHDR before sCAL");
  1629. else if (png_ptr->mode & PNG_HAVE_IDAT)
  1630. {
  1631. png_warning(png_ptr, "Invalid sCAL after IDAT");
  1632. png_crc_finish(png_ptr, length);
  1633. return;
  1634. }
  1635. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL))
  1636. {
  1637. png_warning(png_ptr, "Duplicate sCAL chunk");
  1638. png_crc_finish(png_ptr, length);
  1639. return;
  1640. }
  1641. png_debug1(2, "Allocating and reading sCAL chunk data (%u bytes)",
  1642. length + 1);
  1643. png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
  1644. if (png_ptr->chunkdata == NULL)
  1645. {
  1646. png_warning(png_ptr, "Out of memory while processing sCAL chunk");
  1647. png_crc_finish(png_ptr, length);
  1648. return;
  1649. }
  1650. slength = (png_size_t)length;
  1651. png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
  1652. png_ptr->chunkdata[slength] = 0x00; /* Null terminate the last string */
  1653. if (png_crc_finish(png_ptr, 0))
  1654. {
  1655. png_free(png_ptr, png_ptr->chunkdata);
  1656. png_ptr->chunkdata = NULL;
  1657. return;
  1658. }
  1659. /* Validate the unit. */
  1660. if (png_ptr->chunkdata[0] != 1 && png_ptr->chunkdata[0] != 2)
  1661. {
  1662. png_warning(png_ptr, "Invalid sCAL ignored: invalid unit");
  1663. png_free(png_ptr, png_ptr->chunkdata);
  1664. png_ptr->chunkdata = NULL;
  1665. return;
  1666. }
  1667. /* Validate the ASCII numbers, need two ASCII numbers separated by
  1668. * a '\0' and they need to fit exactly in the chunk data.
  1669. */
  1670. i = 0;
  1671. state = 0;
  1672. if (png_ptr->chunkdata[1] == 45 /* negative width */ ||
  1673. !png_check_fp_number(png_ptr->chunkdata, slength, &state, &i) ||
  1674. i >= slength || png_ptr->chunkdata[i++] != 0)
  1675. png_warning(png_ptr, "Invalid sCAL chunk ignored: bad width format");
  1676. else
  1677. {
  1678. png_size_t heighti = i;
  1679. if (png_ptr->chunkdata[i] == 45 /* negative height */ ||
  1680. !png_check_fp_number(png_ptr->chunkdata, slength, &state, &i) ||
  1681. i != slength)
  1682. png_warning(png_ptr, "Invalid sCAL chunk ignored: bad height format");
  1683. else
  1684. /* This is the (only) success case. */
  1685. png_set_sCAL_s(png_ptr, info_ptr, png_ptr->chunkdata[0],
  1686. png_ptr->chunkdata+1, png_ptr->chunkdata+heighti);
  1687. }
  1688. /* Clean up - just free the temporarily allocated buffer. */
  1689. png_free(png_ptr, png_ptr->chunkdata);
  1690. png_ptr->chunkdata = NULL;
  1691. }
  1692. #endif
  1693. #ifdef PNG_READ_tIME_SUPPORTED
  1694. void /* PRIVATE */
  1695. png_handle_tIME(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1696. {
  1697. png_byte buf[7];
  1698. png_time mod_time;
  1699. png_debug(1, "in png_handle_tIME");
  1700. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1701. png_error(png_ptr, "Out of place tIME chunk");
  1702. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME))
  1703. {
  1704. png_warning(png_ptr, "Duplicate tIME chunk");
  1705. png_crc_finish(png_ptr, length);
  1706. return;
  1707. }
  1708. if (png_ptr->mode & PNG_HAVE_IDAT)
  1709. png_ptr->mode |= PNG_AFTER_IDAT;
  1710. if (length != 7)
  1711. {
  1712. png_warning(png_ptr, "Incorrect tIME chunk length");
  1713. png_crc_finish(png_ptr, length);
  1714. return;
  1715. }
  1716. png_crc_read(png_ptr, buf, 7);
  1717. if (png_crc_finish(png_ptr, 0))
  1718. return;
  1719. mod_time.second = buf[6];
  1720. mod_time.minute = buf[5];
  1721. mod_time.hour = buf[4];
  1722. mod_time.day = buf[3];
  1723. mod_time.month = buf[2];
  1724. mod_time.year = png_get_uint_16(buf);
  1725. png_set_tIME(png_ptr, info_ptr, &mod_time);
  1726. }
  1727. #endif
  1728. #ifdef PNG_READ_tEXt_SUPPORTED
  1729. /* Note: this does not properly handle chunks that are > 64K under DOS */
  1730. void /* PRIVATE */
  1731. png_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1732. {
  1733. png_textp text_ptr;
  1734. png_charp key;
  1735. png_charp text;
  1736. png_uint_32 skip = 0;
  1737. png_size_t slength;
  1738. int ret;
  1739. png_debug(1, "in png_handle_tEXt");
  1740. #ifdef PNG_USER_LIMITS_SUPPORTED
  1741. if (png_ptr->user_chunk_cache_max != 0)
  1742. {
  1743. if (png_ptr->user_chunk_cache_max == 1)
  1744. {
  1745. png_crc_finish(png_ptr, length);
  1746. return;
  1747. }
  1748. if (--png_ptr->user_chunk_cache_max == 1)
  1749. {
  1750. png_warning(png_ptr, "No space in chunk cache for tEXt");
  1751. png_crc_finish(png_ptr, length);
  1752. return;
  1753. }
  1754. }
  1755. #endif
  1756. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1757. png_error(png_ptr, "Missing IHDR before tEXt");
  1758. if (png_ptr->mode & PNG_HAVE_IDAT)
  1759. png_ptr->mode |= PNG_AFTER_IDAT;
  1760. #ifdef PNG_MAX_MALLOC_64K
  1761. if (length > (png_uint_32)65535L)
  1762. {
  1763. png_warning(png_ptr, "tEXt chunk too large to fit in memory");
  1764. skip = length - (png_uint_32)65535L;
  1765. length = (png_uint_32)65535L;
  1766. }
  1767. #endif
  1768. png_free(png_ptr, png_ptr->chunkdata);
  1769. png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
  1770. if (png_ptr->chunkdata == NULL)
  1771. {
  1772. png_warning(png_ptr, "No memory to process text chunk");
  1773. return;
  1774. }
  1775. slength = (png_size_t)length;
  1776. png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
  1777. if (png_crc_finish(png_ptr, skip))
  1778. {
  1779. png_free(png_ptr, png_ptr->chunkdata);
  1780. png_ptr->chunkdata = NULL;
  1781. return;
  1782. }
  1783. key = png_ptr->chunkdata;
  1784. key[slength] = 0x00;
  1785. for (text = key; *text; text++)
  1786. /* Empty loop to find end of key */ ;
  1787. if (text != key + slength)
  1788. text++;
  1789. text_ptr = (png_textp)png_malloc_warn(png_ptr,
  1790. png_sizeof(png_text));
  1791. if (text_ptr == NULL)
  1792. {
  1793. png_warning(png_ptr, "Not enough memory to process text chunk");
  1794. png_free(png_ptr, png_ptr->chunkdata);
  1795. png_ptr->chunkdata = NULL;
  1796. return;
  1797. }
  1798. text_ptr->compression = PNG_TEXT_COMPRESSION_NONE;
  1799. text_ptr->key = key;
  1800. text_ptr->lang = NULL;
  1801. text_ptr->lang_key = NULL;
  1802. text_ptr->itxt_length = 0;
  1803. text_ptr->text = text;
  1804. text_ptr->text_length = png_strlen(text);
  1805. ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
  1806. png_free(png_ptr, png_ptr->chunkdata);
  1807. png_ptr->chunkdata = NULL;
  1808. png_free(png_ptr, text_ptr);
  1809. if (ret)
  1810. png_warning(png_ptr, "Insufficient memory to process text chunk");
  1811. }
  1812. #endif
  1813. #ifdef PNG_READ_zTXt_SUPPORTED
  1814. /* Note: this does not correctly handle chunks that are > 64K under DOS */
  1815. void /* PRIVATE */
  1816. png_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1817. {
  1818. png_textp text_ptr;
  1819. png_charp text;
  1820. int comp_type;
  1821. int ret;
  1822. png_size_t slength, prefix_len, data_len;
  1823. png_debug(1, "in png_handle_zTXt");
  1824. #ifdef PNG_USER_LIMITS_SUPPORTED
  1825. if (png_ptr->user_chunk_cache_max != 0)
  1826. {
  1827. if (png_ptr->user_chunk_cache_max == 1)
  1828. {
  1829. png_crc_finish(png_ptr, length);
  1830. return;
  1831. }
  1832. if (--png_ptr->user_chunk_cache_max == 1)
  1833. {
  1834. png_warning(png_ptr, "No space in chunk cache for zTXt");
  1835. png_crc_finish(png_ptr, length);
  1836. return;
  1837. }
  1838. }
  1839. #endif
  1840. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1841. png_error(png_ptr, "Missing IHDR before zTXt");
  1842. if (png_ptr->mode & PNG_HAVE_IDAT)
  1843. png_ptr->mode |= PNG_AFTER_IDAT;
  1844. #ifdef PNG_MAX_MALLOC_64K
  1845. /* We will no doubt have problems with chunks even half this size, but
  1846. * there is no hard and fast rule to tell us where to stop.
  1847. */
  1848. if (length > (png_uint_32)65535L)
  1849. {
  1850. png_warning(png_ptr, "zTXt chunk too large to fit in memory");
  1851. png_crc_finish(png_ptr, length);
  1852. return;
  1853. }
  1854. #endif
  1855. png_free(png_ptr, png_ptr->chunkdata);
  1856. png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
  1857. if (png_ptr->chunkdata == NULL)
  1858. {
  1859. png_warning(png_ptr, "Out of memory processing zTXt chunk");
  1860. return;
  1861. }
  1862. slength = (png_size_t)length;
  1863. png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
  1864. if (png_crc_finish(png_ptr, 0))
  1865. {
  1866. png_free(png_ptr, png_ptr->chunkdata);
  1867. png_ptr->chunkdata = NULL;
  1868. return;
  1869. }
  1870. png_ptr->chunkdata[slength] = 0x00;
  1871. for (text = png_ptr->chunkdata; *text; text++)
  1872. /* Empty loop */ ;
  1873. /* zTXt must have some text after the chunkdataword */
  1874. if (text >= png_ptr->chunkdata + slength - 2)
  1875. {
  1876. png_warning(png_ptr, "Truncated zTXt chunk");
  1877. png_free(png_ptr, png_ptr->chunkdata);
  1878. png_ptr->chunkdata = NULL;
  1879. return;
  1880. }
  1881. else
  1882. {
  1883. comp_type = *(++text);
  1884. if (comp_type != PNG_TEXT_COMPRESSION_zTXt)
  1885. {
  1886. png_warning(png_ptr, "Unknown compression type in zTXt chunk");
  1887. comp_type = PNG_TEXT_COMPRESSION_zTXt;
  1888. }
  1889. text++; /* Skip the compression_method byte */
  1890. }
  1891. prefix_len = text - png_ptr->chunkdata;
  1892. png_decompress_chunk(png_ptr, comp_type,
  1893. (png_size_t)length, prefix_len, &data_len);
  1894. text_ptr = (png_textp)png_malloc_warn(png_ptr,
  1895. png_sizeof(png_text));
  1896. if (text_ptr == NULL)
  1897. {
  1898. png_warning(png_ptr, "Not enough memory to process zTXt chunk");
  1899. png_free(png_ptr, png_ptr->chunkdata);
  1900. png_ptr->chunkdata = NULL;
  1901. return;
  1902. }
  1903. text_ptr->compression = comp_type;
  1904. text_ptr->key = png_ptr->chunkdata;
  1905. text_ptr->lang = NULL;
  1906. text_ptr->lang_key = NULL;
  1907. text_ptr->itxt_length = 0;
  1908. text_ptr->text = png_ptr->chunkdata + prefix_len;
  1909. text_ptr->text_length = data_len;
  1910. ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
  1911. png_free(png_ptr, text_ptr);
  1912. png_free(png_ptr, png_ptr->chunkdata);
  1913. png_ptr->chunkdata = NULL;
  1914. if (ret)
  1915. png_error(png_ptr, "Insufficient memory to store zTXt chunk");
  1916. }
  1917. #endif
  1918. #ifdef PNG_READ_iTXt_SUPPORTED
  1919. /* Note: this does not correctly handle chunks that are > 64K under DOS */
  1920. void /* PRIVATE */
  1921. png_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1922. {
  1923. png_textp text_ptr;
  1924. png_charp key, lang, text, lang_key;
  1925. int comp_flag;
  1926. int comp_type = 0;
  1927. int ret;
  1928. png_size_t slength, prefix_len, data_len;
  1929. png_debug(1, "in png_handle_iTXt");
  1930. #ifdef PNG_USER_LIMITS_SUPPORTED
  1931. if (png_ptr->user_chunk_cache_max != 0)
  1932. {
  1933. if (png_ptr->user_chunk_cache_max == 1)
  1934. {
  1935. png_crc_finish(png_ptr, length);
  1936. return;
  1937. }
  1938. if (--png_ptr->user_chunk_cache_max == 1)
  1939. {
  1940. png_warning(png_ptr, "No space in chunk cache for iTXt");
  1941. png_crc_finish(png_ptr, length);
  1942. return;
  1943. }
  1944. }
  1945. #endif
  1946. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1947. png_error(png_ptr, "Missing IHDR before iTXt");
  1948. if (png_ptr->mode & PNG_HAVE_IDAT)
  1949. png_ptr->mode |= PNG_AFTER_IDAT;
  1950. #ifdef PNG_MAX_MALLOC_64K
  1951. /* We will no doubt have problems with chunks even half this size, but
  1952. * there is no hard and fast rule to tell us where to stop.
  1953. */
  1954. if (length > (png_uint_32)65535L)
  1955. {
  1956. png_warning(png_ptr, "iTXt chunk too large to fit in memory");
  1957. png_crc_finish(png_ptr, length);
  1958. return;
  1959. }
  1960. #endif
  1961. png_free(png_ptr, png_ptr->chunkdata);
  1962. png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
  1963. if (png_ptr->chunkdata == NULL)
  1964. {
  1965. png_warning(png_ptr, "No memory to process iTXt chunk");
  1966. return;
  1967. }
  1968. slength = (png_size_t)length;
  1969. png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
  1970. if (png_crc_finish(png_ptr, 0))
  1971. {
  1972. png_free(png_ptr, png_ptr->chunkdata);
  1973. png_ptr->chunkdata = NULL;
  1974. return;
  1975. }
  1976. png_ptr->chunkdata[slength] = 0x00;
  1977. for (lang = png_ptr->chunkdata; *lang; lang++)
  1978. /* Empty loop */ ;
  1979. lang++; /* Skip NUL separator */
  1980. /* iTXt must have a language tag (possibly empty), two compression bytes,
  1981. * translated keyword (possibly empty), and possibly some text after the
  1982. * keyword
  1983. */
  1984. if (lang >= png_ptr->chunkdata + slength - 3)
  1985. {
  1986. png_warning(png_ptr, "Truncated iTXt chunk");
  1987. png_free(png_ptr, png_ptr->chunkdata);
  1988. png_ptr->chunkdata = NULL;
  1989. return;
  1990. }
  1991. else
  1992. {
  1993. comp_flag = *lang++;
  1994. comp_type = *lang++;
  1995. }
  1996. for (lang_key = lang; *lang_key; lang_key++)
  1997. /* Empty loop */ ;
  1998. lang_key++; /* Skip NUL separator */
  1999. if (lang_key >= png_ptr->chunkdata + slength)
  2000. {
  2001. png_warning(png_ptr, "Truncated iTXt chunk");
  2002. png_free(png_ptr, png_ptr->chunkdata);
  2003. png_ptr->chunkdata = NULL;
  2004. return;
  2005. }
  2006. for (text = lang_key; *text; text++)
  2007. /* Empty loop */ ;
  2008. text++; /* Skip NUL separator */
  2009. if (text >= png_ptr->chunkdata + slength)
  2010. {
  2011. png_warning(png_ptr, "Malformed iTXt chunk");
  2012. png_free(png_ptr, png_ptr->chunkdata);
  2013. png_ptr->chunkdata = NULL;
  2014. return;
  2015. }
  2016. prefix_len = text - png_ptr->chunkdata;
  2017. key=png_ptr->chunkdata;
  2018. if (comp_flag)
  2019. png_decompress_chunk(png_ptr, comp_type,
  2020. (size_t)length, prefix_len, &data_len);
  2021. else
  2022. data_len = png_strlen(png_ptr->chunkdata + prefix_len);
  2023. text_ptr = (png_textp)png_malloc_warn(png_ptr,
  2024. png_sizeof(png_text));
  2025. if (text_ptr == NULL)
  2026. {
  2027. png_warning(png_ptr, "Not enough memory to process iTXt chunk");
  2028. png_free(png_ptr, png_ptr->chunkdata);
  2029. png_ptr->chunkdata = NULL;
  2030. return;
  2031. }
  2032. text_ptr->compression = (int)comp_flag + 1;
  2033. text_ptr->lang_key = png_ptr->chunkdata + (lang_key - key);
  2034. text_ptr->lang = png_ptr->chunkdata + (lang - key);
  2035. text_ptr->itxt_length = data_len;
  2036. text_ptr->text_length = 0;
  2037. text_ptr->key = png_ptr->chunkdata;
  2038. text_ptr->text = png_ptr->chunkdata + prefix_len;
  2039. ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
  2040. png_free(png_ptr, text_ptr);
  2041. png_free(png_ptr, png_ptr->chunkdata);
  2042. png_ptr->chunkdata = NULL;
  2043. if (ret)
  2044. png_error(png_ptr, "Insufficient memory to store iTXt chunk");
  2045. }
  2046. #endif
  2047. /* This function is called when we haven't found a handler for a
  2048. * chunk. If there isn't a problem with the chunk itself (ie bad
  2049. * chunk name, CRC, or a critical chunk), the chunk is silently ignored
  2050. * -- unless the PNG_FLAG_UNKNOWN_CHUNKS_SUPPORTED flag is on in which
  2051. * case it will be saved away to be written out later.
  2052. */
  2053. void /* PRIVATE */
  2054. png_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  2055. {
  2056. png_uint_32 skip = 0;
  2057. png_debug(1, "in png_handle_unknown");
  2058. #ifdef PNG_USER_LIMITS_SUPPORTED
  2059. if (png_ptr->user_chunk_cache_max != 0)
  2060. {
  2061. if (png_ptr->user_chunk_cache_max == 1)
  2062. {
  2063. png_crc_finish(png_ptr, length);
  2064. return;
  2065. }
  2066. if (--png_ptr->user_chunk_cache_max == 1)
  2067. {
  2068. png_warning(png_ptr, "No space in chunk cache for unknown chunk");
  2069. png_crc_finish(png_ptr, length);
  2070. return;
  2071. }
  2072. }
  2073. #endif
  2074. if (png_ptr->mode & PNG_HAVE_IDAT)
  2075. {
  2076. PNG_IDAT;
  2077. if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) /* Not an IDAT */
  2078. png_ptr->mode |= PNG_AFTER_IDAT;
  2079. }
  2080. if (!(png_ptr->chunk_name[0] & 0x20))
  2081. {
  2082. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  2083. if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
  2084. PNG_HANDLE_CHUNK_ALWAYS
  2085. #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
  2086. && png_ptr->read_user_chunk_fn == NULL
  2087. #endif
  2088. )
  2089. #endif
  2090. png_chunk_error(png_ptr, "unknown critical chunk");
  2091. }
  2092. #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
  2093. if ((png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS)
  2094. #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
  2095. || (png_ptr->read_user_chunk_fn != NULL)
  2096. #endif
  2097. )
  2098. {
  2099. #ifdef PNG_MAX_MALLOC_64K
  2100. if (length > (png_uint_32)65535L)
  2101. {
  2102. png_warning(png_ptr, "unknown chunk too large to fit in memory");
  2103. skip = length - (png_uint_32)65535L;
  2104. length = (png_uint_32)65535L;
  2105. }
  2106. #endif
  2107. png_memcpy((png_charp)png_ptr->unknown_chunk.name,
  2108. (png_charp)png_ptr->chunk_name,
  2109. png_sizeof(png_ptr->unknown_chunk.name));
  2110. png_ptr->unknown_chunk.name[png_sizeof(png_ptr->unknown_chunk.name)-1]
  2111. = '\0';
  2112. png_ptr->unknown_chunk.size = (png_size_t)length;
  2113. if (length == 0)
  2114. png_ptr->unknown_chunk.data = NULL;
  2115. else
  2116. {
  2117. png_ptr->unknown_chunk.data = (png_bytep)png_malloc(png_ptr, length);
  2118. png_crc_read(png_ptr, (png_bytep)png_ptr->unknown_chunk.data, length);
  2119. }
  2120. #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
  2121. if (png_ptr->read_user_chunk_fn != NULL)
  2122. {
  2123. /* Callback to user unknown chunk handler */
  2124. int ret;
  2125. ret = (*(png_ptr->read_user_chunk_fn))
  2126. (png_ptr, &png_ptr->unknown_chunk);
  2127. if (ret < 0)
  2128. png_chunk_error(png_ptr, "error in user chunk");
  2129. if (ret == 0)
  2130. {
  2131. if (!(png_ptr->chunk_name[0] & 0x20))
  2132. {
  2133. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  2134. if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
  2135. PNG_HANDLE_CHUNK_ALWAYS)
  2136. #endif
  2137. png_chunk_error(png_ptr, "unknown critical chunk");
  2138. }
  2139. png_set_unknown_chunks(png_ptr, info_ptr,
  2140. &png_ptr->unknown_chunk, 1);
  2141. }
  2142. }
  2143. else
  2144. #endif
  2145. png_set_unknown_chunks(png_ptr, info_ptr, &png_ptr->unknown_chunk, 1);
  2146. png_free(png_ptr, png_ptr->unknown_chunk.data);
  2147. png_ptr->unknown_chunk.data = NULL;
  2148. }
  2149. else
  2150. #endif
  2151. skip = length;
  2152. png_crc_finish(png_ptr, skip);
  2153. #ifndef PNG_READ_USER_CHUNKS_SUPPORTED
  2154. PNG_UNUSED(info_ptr) /* Quiet compiler warnings about unused info_ptr */
  2155. #endif
  2156. }
  2157. /* This function is called to verify that a chunk name is valid.
  2158. * This function can't have the "critical chunk check" incorporated
  2159. * into it, since in the future we will need to be able to call user
  2160. * functions to handle unknown critical chunks after we check that
  2161. * the chunk name itself is valid.
  2162. */
  2163. #define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
  2164. void /* PRIVATE */
  2165. png_check_chunk_name(png_structp png_ptr, png_const_bytep chunk_name)
  2166. {
  2167. png_debug(1, "in png_check_chunk_name");
  2168. if (isnonalpha(chunk_name[0]) || isnonalpha(chunk_name[1]) ||
  2169. isnonalpha(chunk_name[2]) || isnonalpha(chunk_name[3]))
  2170. {
  2171. png_chunk_error(png_ptr, "invalid chunk type");
  2172. }
  2173. }
  2174. /* Combines the row recently read in with the existing pixels in the
  2175. * row. This routine takes care of alpha and transparency if requested.
  2176. * This routine also handles the two methods of progressive display
  2177. * of interlaced images, depending on the mask value.
  2178. * The mask value describes which pixels are to be combined with
  2179. * the row. The pattern always repeats every 8 pixels, so just 8
  2180. * bits are needed. A one indicates the pixel is to be combined,
  2181. * a zero indicates the pixel is to be skipped. This is in addition
  2182. * to any alpha or transparency value associated with the pixel. If
  2183. * you want all pixels to be combined, pass 0xff (255) in mask.
  2184. */
  2185. void /* PRIVATE */
  2186. png_combine_row(png_structp png_ptr, png_bytep row, int mask)
  2187. {
  2188. png_debug(1, "in png_combine_row");
  2189. if (mask == 0xff)
  2190. {
  2191. png_memcpy(row, png_ptr->row_buf + 1,
  2192. PNG_ROWBYTES(png_ptr->row_info.pixel_depth, png_ptr->width));
  2193. }
  2194. else
  2195. {
  2196. switch (png_ptr->row_info.pixel_depth)
  2197. {
  2198. case 1:
  2199. {
  2200. png_bytep sp = png_ptr->row_buf + 1;
  2201. png_bytep dp = row;
  2202. int s_inc, s_start, s_end;
  2203. int m = 0x80;
  2204. int shift;
  2205. png_uint_32 i;
  2206. png_uint_32 row_width = png_ptr->width;
  2207. #ifdef PNG_READ_PACKSWAP_SUPPORTED
  2208. if (png_ptr->transformations & PNG_PACKSWAP)
  2209. {
  2210. s_start = 0;
  2211. s_end = 7;
  2212. s_inc = 1;
  2213. }
  2214. else
  2215. #endif
  2216. {
  2217. s_start = 7;
  2218. s_end = 0;
  2219. s_inc = -1;
  2220. }
  2221. shift = s_start;
  2222. for (i = 0; i < row_width; i++)
  2223. {
  2224. if (m & mask)
  2225. {
  2226. int value;
  2227. value = (*sp >> shift) & 0x01;
  2228. *dp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);
  2229. *dp |= (png_byte)(value << shift);
  2230. }
  2231. if (shift == s_end)
  2232. {
  2233. shift = s_start;
  2234. sp++;
  2235. dp++;
  2236. }
  2237. else
  2238. shift += s_inc;
  2239. if (m == 1)
  2240. m = 0x80;
  2241. else
  2242. m >>= 1;
  2243. }
  2244. break;
  2245. }
  2246. case 2:
  2247. {
  2248. png_bytep sp = png_ptr->row_buf + 1;
  2249. png_bytep dp = row;
  2250. int s_start, s_end, s_inc;
  2251. int m = 0x80;
  2252. int shift;
  2253. png_uint_32 i;
  2254. png_uint_32 row_width = png_ptr->width;
  2255. int value;
  2256. #ifdef PNG_READ_PACKSWAP_SUPPORTED
  2257. if (png_ptr->transformations & PNG_PACKSWAP)
  2258. {
  2259. s_start = 0;
  2260. s_end = 6;
  2261. s_inc = 2;
  2262. }
  2263. else
  2264. #endif
  2265. {
  2266. s_start = 6;
  2267. s_end = 0;
  2268. s_inc = -2;
  2269. }
  2270. shift = s_start;
  2271. for (i = 0; i < row_width; i++)
  2272. {
  2273. if (m & mask)
  2274. {
  2275. value = (*sp >> shift) & 0x03;
  2276. *dp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
  2277. *dp |= (png_byte)(value << shift);
  2278. }
  2279. if (shift == s_end)
  2280. {
  2281. shift = s_start;
  2282. sp++;
  2283. dp++;
  2284. }
  2285. else
  2286. shift += s_inc;
  2287. if (m == 1)
  2288. m = 0x80;
  2289. else
  2290. m >>= 1;
  2291. }
  2292. break;
  2293. }
  2294. case 4:
  2295. {
  2296. png_bytep sp = png_ptr->row_buf + 1;
  2297. png_bytep dp = row;
  2298. int s_start, s_end, s_inc;
  2299. int m = 0x80;
  2300. int shift;
  2301. png_uint_32 i;
  2302. png_uint_32 row_width = png_ptr->width;
  2303. int value;
  2304. #ifdef PNG_READ_PACKSWAP_SUPPORTED
  2305. if (png_ptr->transformations & PNG_PACKSWAP)
  2306. {
  2307. s_start = 0;
  2308. s_end = 4;
  2309. s_inc = 4;
  2310. }
  2311. else
  2312. #endif
  2313. {
  2314. s_start = 4;
  2315. s_end = 0;
  2316. s_inc = -4;
  2317. }
  2318. shift = s_start;
  2319. for (i = 0; i < row_width; i++)
  2320. {
  2321. if (m & mask)
  2322. {
  2323. value = (*sp >> shift) & 0xf;
  2324. *dp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
  2325. *dp |= (png_byte)(value << shift);
  2326. }
  2327. if (shift == s_end)
  2328. {
  2329. shift = s_start;
  2330. sp++;
  2331. dp++;
  2332. }
  2333. else
  2334. shift += s_inc;
  2335. if (m == 1)
  2336. m = 0x80;
  2337. else
  2338. m >>= 1;
  2339. }
  2340. break;
  2341. }
  2342. default:
  2343. {
  2344. png_bytep sp = png_ptr->row_buf + 1;
  2345. png_bytep dp = row;
  2346. png_size_t pixel_bytes = (png_ptr->row_info.pixel_depth >> 3);
  2347. png_uint_32 i;
  2348. png_uint_32 row_width = png_ptr->width;
  2349. png_byte m = 0x80;
  2350. for (i = 0; i < row_width; i++)
  2351. {
  2352. if (m & mask)
  2353. {
  2354. png_memcpy(dp, sp, pixel_bytes);
  2355. }
  2356. sp += pixel_bytes;
  2357. dp += pixel_bytes;
  2358. if (m == 1)
  2359. m = 0x80;
  2360. else
  2361. m >>= 1;
  2362. }
  2363. break;
  2364. }
  2365. }
  2366. }
  2367. }
  2368. #ifdef PNG_READ_INTERLACING_SUPPORTED
  2369. void /* PRIVATE */
  2370. png_do_read_interlace(png_structp png_ptr)
  2371. {
  2372. png_row_infop row_info = &(png_ptr->row_info);
  2373. png_bytep row = png_ptr->row_buf + 1;
  2374. int pass = png_ptr->pass;
  2375. png_uint_32 transformations = png_ptr->transformations;
  2376. /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  2377. /* Offset to next interlace block */
  2378. PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
  2379. png_debug(1, "in png_do_read_interlace");
  2380. if (row != NULL && row_info != NULL)
  2381. {
  2382. png_uint_32 final_width;
  2383. final_width = row_info->width * png_pass_inc[pass];
  2384. switch (row_info->pixel_depth)
  2385. {
  2386. case 1:
  2387. {
  2388. png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 3);
  2389. png_bytep dp = row + (png_size_t)((final_width - 1) >> 3);
  2390. int sshift, dshift;
  2391. int s_start, s_end, s_inc;
  2392. int jstop = png_pass_inc[pass];
  2393. png_byte v;
  2394. png_uint_32 i;
  2395. int j;
  2396. #ifdef PNG_READ_PACKSWAP_SUPPORTED
  2397. if (transformations & PNG_PACKSWAP)
  2398. {
  2399. sshift = (int)((row_info->width + 7) & 0x07);
  2400. dshift = (int)((final_width + 7) & 0x07);
  2401. s_start = 7;
  2402. s_end = 0;
  2403. s_inc = -1;
  2404. }
  2405. else
  2406. #endif
  2407. {
  2408. sshift = 7 - (int)((row_info->width + 7) & 0x07);
  2409. dshift = 7 - (int)((final_width + 7) & 0x07);
  2410. s_start = 0;
  2411. s_end = 7;
  2412. s_inc = 1;
  2413. }
  2414. for (i = 0; i < row_info->width; i++)
  2415. {
  2416. v = (png_byte)((*sp >> sshift) & 0x01);
  2417. for (j = 0; j < jstop; j++)
  2418. {
  2419. *dp &= (png_byte)((0x7f7f >> (7 - dshift)) & 0xff);
  2420. *dp |= (png_byte)(v << dshift);
  2421. if (dshift == s_end)
  2422. {
  2423. dshift = s_start;
  2424. dp--;
  2425. }
  2426. else
  2427. dshift += s_inc;
  2428. }
  2429. if (sshift == s_end)
  2430. {
  2431. sshift = s_start;
  2432. sp--;
  2433. }
  2434. else
  2435. sshift += s_inc;
  2436. }
  2437. break;
  2438. }
  2439. case 2:
  2440. {
  2441. png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2);
  2442. png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2);
  2443. int sshift, dshift;
  2444. int s_start, s_end, s_inc;
  2445. int jstop = png_pass_inc[pass];
  2446. png_uint_32 i;
  2447. #ifdef PNG_READ_PACKSWAP_SUPPORTED
  2448. if (transformations & PNG_PACKSWAP)
  2449. {
  2450. sshift = (int)(((row_info->width + 3) & 0x03) << 1);
  2451. dshift = (int)(((final_width + 3) & 0x03) << 1);
  2452. s_start = 6;
  2453. s_end = 0;
  2454. s_inc = -2;
  2455. }
  2456. else
  2457. #endif
  2458. {
  2459. sshift = (int)((3 - ((row_info->width + 3) & 0x03)) << 1);
  2460. dshift = (int)((3 - ((final_width + 3) & 0x03)) << 1);
  2461. s_start = 0;
  2462. s_end = 6;
  2463. s_inc = 2;
  2464. }
  2465. for (i = 0; i < row_info->width; i++)
  2466. {
  2467. png_byte v;
  2468. int j;
  2469. v = (png_byte)((*sp >> sshift) & 0x03);
  2470. for (j = 0; j < jstop; j++)
  2471. {
  2472. *dp &= (png_byte)((0x3f3f >> (6 - dshift)) & 0xff);
  2473. *dp |= (png_byte)(v << dshift);
  2474. if (dshift == s_end)
  2475. {
  2476. dshift = s_start;
  2477. dp--;
  2478. }
  2479. else
  2480. dshift += s_inc;
  2481. }
  2482. if (sshift == s_end)
  2483. {
  2484. sshift = s_start;
  2485. sp--;
  2486. }
  2487. else
  2488. sshift += s_inc;
  2489. }
  2490. break;
  2491. }
  2492. case 4:
  2493. {
  2494. png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 1);
  2495. png_bytep dp = row + (png_size_t)((final_width - 1) >> 1);
  2496. int sshift, dshift;
  2497. int s_start, s_end, s_inc;
  2498. png_uint_32 i;
  2499. int jstop = png_pass_inc[pass];
  2500. #ifdef PNG_READ_PACKSWAP_SUPPORTED
  2501. if (transformations & PNG_PACKSWAP)
  2502. {
  2503. sshift = (int)(((row_info->width + 1) & 0x01) << 2);
  2504. dshift = (int)(((final_width + 1) & 0x01) << 2);
  2505. s_start = 4;
  2506. s_end = 0;
  2507. s_inc = -4;
  2508. }
  2509. else
  2510. #endif
  2511. {
  2512. sshift = (int)((1 - ((row_info->width + 1) & 0x01)) << 2);
  2513. dshift = (int)((1 - ((final_width + 1) & 0x01)) << 2);
  2514. s_start = 0;
  2515. s_end = 4;
  2516. s_inc = 4;
  2517. }
  2518. for (i = 0; i < row_info->width; i++)
  2519. {
  2520. png_byte v = (png_byte)((*sp >> sshift) & 0xf);
  2521. int j;
  2522. for (j = 0; j < jstop; j++)
  2523. {
  2524. *dp &= (png_byte)((0xf0f >> (4 - dshift)) & 0xff);
  2525. *dp |= (png_byte)(v << dshift);
  2526. if (dshift == s_end)
  2527. {
  2528. dshift = s_start;
  2529. dp--;
  2530. }
  2531. else
  2532. dshift += s_inc;
  2533. }
  2534. if (sshift == s_end)
  2535. {
  2536. sshift = s_start;
  2537. sp--;
  2538. }
  2539. else
  2540. sshift += s_inc;
  2541. }
  2542. break;
  2543. }
  2544. default:
  2545. {
  2546. png_size_t pixel_bytes = (row_info->pixel_depth >> 3);
  2547. png_bytep sp = row + (png_size_t)(row_info->width - 1)
  2548. * pixel_bytes;
  2549. png_bytep dp = row + (png_size_t)(final_width - 1) * pixel_bytes;
  2550. int jstop = png_pass_inc[pass];
  2551. png_uint_32 i;
  2552. for (i = 0; i < row_info->width; i++)
  2553. {
  2554. png_byte v[8];
  2555. int j;
  2556. png_memcpy(v, sp, pixel_bytes);
  2557. for (j = 0; j < jstop; j++)
  2558. {
  2559. png_memcpy(dp, v, pixel_bytes);
  2560. dp -= pixel_bytes;
  2561. }
  2562. sp -= pixel_bytes;
  2563. }
  2564. break;
  2565. }
  2566. }
  2567. row_info->width = final_width;
  2568. row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width);
  2569. }
  2570. #ifndef PNG_READ_PACKSWAP_SUPPORTED
  2571. PNG_UNUSED(transformations) /* Silence compiler warning */
  2572. #endif
  2573. }
  2574. #endif /* PNG_READ_INTERLACING_SUPPORTED */
  2575. void /* PRIVATE */
  2576. png_read_filter_row(png_structp png_ptr, png_row_infop row_info, png_bytep row,
  2577. png_const_bytep prev_row, int filter)
  2578. {
  2579. png_debug(1, "in png_read_filter_row");
  2580. png_debug2(2, "row = %u, filter = %d", png_ptr->row_number, filter);
  2581. switch (filter)
  2582. {
  2583. case PNG_FILTER_VALUE_NONE:
  2584. break;
  2585. case PNG_FILTER_VALUE_SUB:
  2586. {
  2587. png_size_t i;
  2588. png_size_t istop = row_info->rowbytes;
  2589. unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
  2590. png_bytep rp = row + bpp;
  2591. png_bytep lp = row;
  2592. for (i = bpp; i < istop; i++)
  2593. {
  2594. *rp = (png_byte)(((int)(*rp) + (int)(*lp++)) & 0xff);
  2595. rp++;
  2596. }
  2597. break;
  2598. }
  2599. case PNG_FILTER_VALUE_UP:
  2600. {
  2601. png_size_t i;
  2602. png_size_t istop = row_info->rowbytes;
  2603. png_bytep rp = row;
  2604. png_const_bytep pp = prev_row;
  2605. for (i = 0; i < istop; i++)
  2606. {
  2607. *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
  2608. rp++;
  2609. }
  2610. break;
  2611. }
  2612. case PNG_FILTER_VALUE_AVG:
  2613. {
  2614. png_size_t i;
  2615. png_bytep rp = row;
  2616. png_const_bytep pp = prev_row;
  2617. png_bytep lp = row;
  2618. unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
  2619. png_size_t istop = row_info->rowbytes - bpp;
  2620. for (i = 0; i < bpp; i++)
  2621. {
  2622. *rp = (png_byte)(((int)(*rp) +
  2623. ((int)(*pp++) / 2 )) & 0xff);
  2624. rp++;
  2625. }
  2626. for (i = 0; i < istop; i++)
  2627. {
  2628. *rp = (png_byte)(((int)(*rp) +
  2629. (int)(*pp++ + *lp++) / 2 ) & 0xff);
  2630. rp++;
  2631. }
  2632. break;
  2633. }
  2634. case PNG_FILTER_VALUE_PAETH:
  2635. {
  2636. png_size_t i;
  2637. png_bytep rp = row;
  2638. png_const_bytep pp = prev_row;
  2639. png_bytep lp = row;
  2640. png_const_bytep cp = prev_row;
  2641. unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
  2642. png_size_t istop=row_info->rowbytes - bpp;
  2643. for (i = 0; i < bpp; i++)
  2644. {
  2645. *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
  2646. rp++;
  2647. }
  2648. for (i = 0; i < istop; i++) /* Use leftover rp,pp */
  2649. {
  2650. int a, b, c, pa, pb, pc, p;
  2651. a = *lp++;
  2652. b = *pp++;
  2653. c = *cp++;
  2654. p = b - c;
  2655. pc = a - c;
  2656. #ifdef PNG_USE_ABS
  2657. pa = abs(p);
  2658. pb = abs(pc);
  2659. pc = abs(p + pc);
  2660. #else
  2661. pa = p < 0 ? -p : p;
  2662. pb = pc < 0 ? -pc : pc;
  2663. pc = (p + pc) < 0 ? -(p + pc) : p + pc;
  2664. #endif
  2665. /*
  2666. if (pa <= pb && pa <= pc)
  2667. p = a;
  2668. else if (pb <= pc)
  2669. p = b;
  2670. else
  2671. p = c;
  2672. */
  2673. p = (pa <= pb && pa <= pc) ? a : (pb <= pc) ? b : c;
  2674. *rp = (png_byte)(((int)(*rp) + p) & 0xff);
  2675. rp++;
  2676. }
  2677. break;
  2678. }
  2679. default:
  2680. png_error(png_ptr, "Ignoring bad adaptive filter type");
  2681. /*NOT REACHED */
  2682. break;
  2683. }
  2684. }
  2685. #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  2686. void /* PRIVATE */
  2687. png_read_finish_row(png_structp png_ptr)
  2688. {
  2689. #ifdef PNG_READ_INTERLACING_SUPPORTED
  2690. /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  2691. /* Start of interlace block */
  2692. PNG_CONST int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
  2693. /* Offset to next interlace block */
  2694. PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
  2695. /* Start of interlace block in the y direction */
  2696. PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
  2697. /* Offset to next interlace block in the y direction */
  2698. PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
  2699. #endif /* PNG_READ_INTERLACING_SUPPORTED */
  2700. png_debug(1, "in png_read_finish_row");
  2701. png_ptr->row_number++;
  2702. if (png_ptr->row_number < png_ptr->num_rows)
  2703. return;
  2704. #ifdef PNG_READ_INTERLACING_SUPPORTED
  2705. if (png_ptr->interlaced)
  2706. {
  2707. png_ptr->row_number = 0;
  2708. png_memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
  2709. do
  2710. {
  2711. png_ptr->pass++;
  2712. if (png_ptr->pass >= 7)
  2713. break;
  2714. png_ptr->iwidth = (png_ptr->width +
  2715. png_pass_inc[png_ptr->pass] - 1 -
  2716. png_pass_start[png_ptr->pass]) /
  2717. png_pass_inc[png_ptr->pass];
  2718. if (!(png_ptr->transformations & PNG_INTERLACE))
  2719. {
  2720. png_ptr->num_rows = (png_ptr->height +
  2721. png_pass_yinc[png_ptr->pass] - 1 -
  2722. png_pass_ystart[png_ptr->pass]) /
  2723. png_pass_yinc[png_ptr->pass];
  2724. }
  2725. else /* if (png_ptr->transformations & PNG_INTERLACE) */
  2726. break; /* libpng deinterlacing sees every row */
  2727. } while (png_ptr->num_rows == 0 || png_ptr->iwidth == 0);
  2728. if (png_ptr->pass < 7)
  2729. return;
  2730. }
  2731. #endif /* PNG_READ_INTERLACING_SUPPORTED */
  2732. if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
  2733. {
  2734. PNG_IDAT;
  2735. char extra;
  2736. int ret;
  2737. png_ptr->zstream.next_out = (Byte *)&extra;
  2738. png_ptr->zstream.avail_out = (uInt)1;
  2739. for (;;)
  2740. {
  2741. if (!(png_ptr->zstream.avail_in))
  2742. {
  2743. while (!png_ptr->idat_size)
  2744. {
  2745. png_crc_finish(png_ptr, 0);
  2746. png_ptr->idat_size = png_read_chunk_header(png_ptr);
  2747. if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
  2748. png_error(png_ptr, "Not enough image data");
  2749. }
  2750. png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
  2751. png_ptr->zstream.next_in = png_ptr->zbuf;
  2752. if (png_ptr->zbuf_size > png_ptr->idat_size)
  2753. png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
  2754. png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zstream.avail_in);
  2755. png_ptr->idat_size -= png_ptr->zstream.avail_in;
  2756. }
  2757. ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
  2758. if (ret == Z_STREAM_END)
  2759. {
  2760. if (!(png_ptr->zstream.avail_out) || png_ptr->zstream.avail_in ||
  2761. png_ptr->idat_size)
  2762. png_warning(png_ptr, "Extra compressed data");
  2763. png_ptr->mode |= PNG_AFTER_IDAT;
  2764. png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
  2765. break;
  2766. }
  2767. if (ret != Z_OK)
  2768. png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
  2769. "Decompression Error");
  2770. if (!(png_ptr->zstream.avail_out))
  2771. {
  2772. png_warning(png_ptr, "Extra compressed data");
  2773. png_ptr->mode |= PNG_AFTER_IDAT;
  2774. png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
  2775. break;
  2776. }
  2777. }
  2778. png_ptr->zstream.avail_out = 0;
  2779. }
  2780. if (png_ptr->idat_size || png_ptr->zstream.avail_in)
  2781. png_warning(png_ptr, "Extra compression data");
  2782. inflateReset(&png_ptr->zstream);
  2783. png_ptr->mode |= PNG_AFTER_IDAT;
  2784. }
  2785. #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
  2786. void /* PRIVATE */
  2787. png_read_start_row(png_structp png_ptr)
  2788. {
  2789. #ifdef PNG_READ_INTERLACING_SUPPORTED
  2790. /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  2791. /* Start of interlace block */
  2792. PNG_CONST int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
  2793. /* Offset to next interlace block */
  2794. PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
  2795. /* Start of interlace block in the y direction */
  2796. PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
  2797. /* Offset to next interlace block in the y direction */
  2798. PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
  2799. #endif
  2800. int max_pixel_depth;
  2801. png_size_t row_bytes;
  2802. png_debug(1, "in png_read_start_row");
  2803. png_ptr->zstream.avail_in = 0;
  2804. png_init_read_transformations(png_ptr);
  2805. #ifdef PNG_READ_INTERLACING_SUPPORTED
  2806. if (png_ptr->interlaced)
  2807. {
  2808. if (!(png_ptr->transformations & PNG_INTERLACE))
  2809. png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
  2810. png_pass_ystart[0]) / png_pass_yinc[0];
  2811. else
  2812. png_ptr->num_rows = png_ptr->height;
  2813. png_ptr->iwidth = (png_ptr->width +
  2814. png_pass_inc[png_ptr->pass] - 1 -
  2815. png_pass_start[png_ptr->pass]) /
  2816. png_pass_inc[png_ptr->pass];
  2817. }
  2818. else
  2819. #endif /* PNG_READ_INTERLACING_SUPPORTED */
  2820. {
  2821. png_ptr->num_rows = png_ptr->height;
  2822. png_ptr->iwidth = png_ptr->width;
  2823. }
  2824. max_pixel_depth = png_ptr->pixel_depth;
  2825. #ifdef PNG_READ_PACK_SUPPORTED
  2826. if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8)
  2827. max_pixel_depth = 8;
  2828. #endif
  2829. #ifdef PNG_READ_EXPAND_SUPPORTED
  2830. if (png_ptr->transformations & PNG_EXPAND)
  2831. {
  2832. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  2833. {
  2834. if (png_ptr->num_trans)
  2835. max_pixel_depth = 32;
  2836. else
  2837. max_pixel_depth = 24;
  2838. }
  2839. else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
  2840. {
  2841. if (max_pixel_depth < 8)
  2842. max_pixel_depth = 8;
  2843. if (png_ptr->num_trans)
  2844. max_pixel_depth *= 2;
  2845. }
  2846. else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
  2847. {
  2848. if (png_ptr->num_trans)
  2849. {
  2850. max_pixel_depth *= 4;
  2851. max_pixel_depth /= 3;
  2852. }
  2853. }
  2854. }
  2855. #endif
  2856. #ifdef PNG_READ_EXPAND_16_SUPPORTED
  2857. if (png_ptr->transformations & PNG_EXPAND_16)
  2858. {
  2859. # ifdef PNG_READ_EXPAND_SUPPORTED
  2860. /* In fact it is an error if it isn't supported, but checking is
  2861. * the safe way.
  2862. */
  2863. if (png_ptr->transformations & PNG_EXPAND)
  2864. {
  2865. if (png_ptr->bit_depth < 16)
  2866. max_pixel_depth *= 2;
  2867. }
  2868. else
  2869. # endif
  2870. png_ptr->transformations &= ~PNG_EXPAND_16;
  2871. }
  2872. #endif
  2873. #ifdef PNG_READ_FILLER_SUPPORTED
  2874. if (png_ptr->transformations & (PNG_FILLER))
  2875. {
  2876. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  2877. max_pixel_depth = 32;
  2878. else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
  2879. {
  2880. if (max_pixel_depth <= 8)
  2881. max_pixel_depth = 16;
  2882. else
  2883. max_pixel_depth = 32;
  2884. }
  2885. else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
  2886. {
  2887. if (max_pixel_depth <= 32)
  2888. max_pixel_depth = 32;
  2889. else
  2890. max_pixel_depth = 64;
  2891. }
  2892. }
  2893. #endif
  2894. #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
  2895. if (png_ptr->transformations & PNG_GRAY_TO_RGB)
  2896. {
  2897. if (
  2898. #ifdef PNG_READ_EXPAND_SUPPORTED
  2899. (png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) ||
  2900. #endif
  2901. #ifdef PNG_READ_FILLER_SUPPORTED
  2902. (png_ptr->transformations & (PNG_FILLER)) ||
  2903. #endif
  2904. png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  2905. {
  2906. if (max_pixel_depth <= 16)
  2907. max_pixel_depth = 32;
  2908. else
  2909. max_pixel_depth = 64;
  2910. }
  2911. else
  2912. {
  2913. if (max_pixel_depth <= 8)
  2914. {
  2915. if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  2916. max_pixel_depth = 32;
  2917. else
  2918. max_pixel_depth = 24;
  2919. }
  2920. else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  2921. max_pixel_depth = 64;
  2922. else
  2923. max_pixel_depth = 48;
  2924. }
  2925. }
  2926. #endif
  2927. #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
  2928. defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
  2929. if (png_ptr->transformations & PNG_USER_TRANSFORM)
  2930. {
  2931. int user_pixel_depth = png_ptr->user_transform_depth*
  2932. png_ptr->user_transform_channels;
  2933. if (user_pixel_depth > max_pixel_depth)
  2934. max_pixel_depth=user_pixel_depth;
  2935. }
  2936. #endif
  2937. /* Align the width on the next larger 8 pixels. Mainly used
  2938. * for interlacing
  2939. */
  2940. row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7));
  2941. /* Calculate the maximum bytes needed, adding a byte and a pixel
  2942. * for safety's sake
  2943. */
  2944. row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) +
  2945. 1 + ((max_pixel_depth + 7) >> 3);
  2946. #ifdef PNG_MAX_MALLOC_64K
  2947. if (row_bytes > (png_uint_32)65536L)
  2948. png_error(png_ptr, "This image requires a row greater than 64KB");
  2949. #endif
  2950. if (row_bytes + 48 > png_ptr->old_big_row_buf_size)
  2951. {
  2952. png_free(png_ptr, png_ptr->big_row_buf);
  2953. if (png_ptr->interlaced)
  2954. png_ptr->big_row_buf = (png_bytep)png_calloc(png_ptr,
  2955. row_bytes + 48);
  2956. else
  2957. png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr,
  2958. row_bytes + 48);
  2959. png_ptr->old_big_row_buf_size = row_bytes + 48;
  2960. #ifdef PNG_ALIGNED_MEMORY_SUPPORTED
  2961. /* Use 16-byte aligned memory for row_buf with at least 16 bytes
  2962. * of padding before and after row_buf.
  2963. */
  2964. png_ptr->row_buf = png_ptr->big_row_buf + 32 -
  2965. (((png_alloc_size_t)png_ptr->big_row_buf + 15) & 0x0F);
  2966. png_ptr->old_big_row_buf_size = row_bytes + 48;
  2967. #else
  2968. /* Use 32 bytes of padding before and 16 bytes after row_buf. */
  2969. png_ptr->row_buf = png_ptr->big_row_buf + 32;
  2970. #endif
  2971. png_ptr->old_big_row_buf_size = row_bytes + 48;
  2972. }
  2973. #ifdef PNG_MAX_MALLOC_64K
  2974. if (png_ptr->rowbytes > 65535)
  2975. png_error(png_ptr, "This image requires a row greater than 64KB");
  2976. #endif
  2977. if (png_ptr->rowbytes > (PNG_SIZE_MAX - 1))
  2978. png_error(png_ptr, "Row has too many bytes to allocate in memory");
  2979. if (png_ptr->rowbytes + 1 > png_ptr->old_prev_row_size)
  2980. {
  2981. png_free(png_ptr, png_ptr->prev_row);
  2982. png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, png_ptr->rowbytes + 1);
  2983. png_ptr->old_prev_row_size = png_ptr->rowbytes + 1;
  2984. }
  2985. png_memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
  2986. png_debug1(3, "width = %u,", png_ptr->width);
  2987. png_debug1(3, "height = %u,", png_ptr->height);
  2988. png_debug1(3, "iwidth = %u,", png_ptr->iwidth);
  2989. png_debug1(3, "num_rows = %u,", png_ptr->num_rows);
  2990. png_debug1(3, "rowbytes = %lu,", (unsigned long)png_ptr->rowbytes);
  2991. png_debug1(3, "irowbytes = %lu",
  2992. (unsigned long)PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1);
  2993. png_ptr->flags |= PNG_FLAG_ROW_INIT;
  2994. }
  2995. #endif /* PNG_READ_SUPPORTED */