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