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