The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1 | |
| 2 | /* pngrutil.c - utilities to read a PNG file |
| 3 | * |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4 | * Last changed in libpng 1.6.3 [July 18, 2013] |
| 5 | * Copyright (c) 1998-2013 Glenn Randers-Pehrson |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [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.) |
| 8 | * |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 9 | * This code is released under the libpng license. |
| 10 | * For conditions of distribution and use, see the disclaimer |
| 11 | * and license in png.h |
| 12 | * |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 13 | * This file contains routines that are only called from within |
| 14 | * libpng itself during the course of reading an image. |
| 15 | */ |
| 16 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 17 | #include "pngpriv.h" |
| 18 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 19 | #ifdef PNG_READ_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 20 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 21 | png_uint_32 PNGAPI |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 22 | png_get_uint_31(png_const_structrp png_ptr, png_const_bytep buf) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 23 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 24 | png_uint_32 uval = png_get_uint_32(buf); |
| 25 | |
| 26 | if (uval > PNG_UINT_31_MAX) |
| 27 | png_error(png_ptr, "PNG unsigned integer out of range"); |
| 28 | |
| 29 | return (uval); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 30 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 31 | |
| 32 | #if defined(PNG_READ_gAMA_SUPPORTED) || defined(PNG_READ_cHRM_SUPPORTED) |
| 33 | /* The following is a variation on the above for use with the fixed |
| 34 | * point values used for gAMA and cHRM. Instead of png_error it |
| 35 | * issues a warning and returns (-1) - an invalid value because both |
| 36 | * gAMA and cHRM use *unsigned* integers for fixed point values. |
| 37 | */ |
| 38 | #define PNG_FIXED_ERROR (-1) |
| 39 | |
| 40 | static png_fixed_point /* PRIVATE */ |
| 41 | png_get_fixed_point(png_structrp png_ptr, png_const_bytep buf) |
| 42 | { |
| 43 | png_uint_32 uval = png_get_uint_32(buf); |
| 44 | |
| 45 | if (uval <= PNG_UINT_31_MAX) |
| 46 | return (png_fixed_point)uval; /* known to be in range */ |
| 47 | |
| 48 | /* The caller can turn off the warning by passing NULL. */ |
| 49 | if (png_ptr != NULL) |
| 50 | png_warning(png_ptr, "PNG fixed point integer out of range"); |
| 51 | |
| 52 | return PNG_FIXED_ERROR; |
| 53 | } |
| 54 | #endif |
| 55 | |
| 56 | #ifdef PNG_READ_INT_FUNCTIONS_SUPPORTED |
| 57 | /* NOTE: the read macros will obscure these definitions, so that if |
| 58 | * PNG_USE_READ_MACROS is set the library will not use them internally, |
| 59 | * but the APIs will still be available externally. |
| 60 | * |
| 61 | * The parentheses around "PNGAPI function_name" in the following three |
| 62 | * functions are necessary because they allow the macros to co-exist with |
| 63 | * these (unused but exported) functions. |
| 64 | */ |
| 65 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 66 | /* Grab an unsigned 32-bit integer from a buffer in big-endian format. */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 67 | png_uint_32 (PNGAPI |
| 68 | png_get_uint_32)(png_const_bytep buf) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 69 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 70 | png_uint_32 uval = |
| 71 | ((png_uint_32)(*(buf )) << 24) + |
| 72 | ((png_uint_32)(*(buf + 1)) << 16) + |
| 73 | ((png_uint_32)(*(buf + 2)) << 8) + |
| 74 | ((png_uint_32)(*(buf + 3)) ) ; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 75 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 76 | return uval; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /* Grab a signed 32-bit integer from a buffer in big-endian format. The |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 80 | * data is stored in the PNG file in two's complement format and there |
| 81 | * is no guarantee that a 'png_int_32' is exactly 32 bits, therefore |
| 82 | * the following code does a two's complement to native conversion. |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 83 | */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 84 | png_int_32 (PNGAPI |
| 85 | png_get_int_32)(png_const_bytep buf) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 86 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 87 | png_uint_32 uval = png_get_uint_32(buf); |
| 88 | if ((uval & 0x80000000) == 0) /* non-negative */ |
| 89 | return uval; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 90 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 91 | uval = (uval ^ 0xffffffff) + 1; /* 2's complement: -x = ~x+1 */ |
| 92 | return -(png_int_32)uval; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /* Grab an unsigned 16-bit integer from a buffer in big-endian format. */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 96 | png_uint_16 (PNGAPI |
| 97 | png_get_uint_16)(png_const_bytep buf) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 98 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 99 | /* ANSI-C requires an int value to accomodate at least 16 bits so this |
| 100 | * works and allows the compiler not to worry about possible narrowing |
| 101 | * on 32 bit systems. (Pre-ANSI systems did not make integers smaller |
| 102 | * than 16 bits either.) |
| 103 | */ |
| 104 | unsigned int val = |
| 105 | ((unsigned int)(*buf) << 8) + |
| 106 | ((unsigned int)(*(buf + 1))); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 107 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 108 | return (png_uint_16)val; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 109 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 110 | |
| 111 | #endif /* PNG_READ_INT_FUNCTIONS_SUPPORTED */ |
| 112 | |
| 113 | /* Read and check the PNG file signature */ |
| 114 | void /* PRIVATE */ |
| 115 | png_read_sig(png_structrp png_ptr, png_inforp info_ptr) |
| 116 | { |
| 117 | png_size_t num_checked, num_to_check; |
| 118 | |
| 119 | /* Exit if the user application does not expect a signature. */ |
| 120 | if (png_ptr->sig_bytes >= 8) |
| 121 | return; |
| 122 | |
| 123 | num_checked = png_ptr->sig_bytes; |
| 124 | num_to_check = 8 - num_checked; |
| 125 | |
| 126 | #ifdef PNG_IO_STATE_SUPPORTED |
| 127 | png_ptr->io_state = PNG_IO_READING | PNG_IO_SIGNATURE; |
| 128 | #endif |
| 129 | |
| 130 | /* The signature must be serialized in a single I/O call. */ |
| 131 | png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check); |
| 132 | png_ptr->sig_bytes = 8; |
| 133 | |
| 134 | if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check)) |
| 135 | { |
| 136 | if (num_checked < 4 && |
| 137 | png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4)) |
| 138 | png_error(png_ptr, "Not a PNG file"); |
| 139 | else |
| 140 | png_error(png_ptr, "PNG file corrupted by ASCII conversion"); |
| 141 | } |
| 142 | if (num_checked < 3) |
| 143 | png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE; |
| 144 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 145 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 146 | /* Read the chunk header (length + type name). |
| 147 | * Put the type name into png_ptr->chunk_name, and return the length. |
| 148 | */ |
| 149 | png_uint_32 /* PRIVATE */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 150 | png_read_chunk_header(png_structrp png_ptr) |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 151 | { |
| 152 | png_byte buf[8]; |
| 153 | png_uint_32 length; |
| 154 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 155 | #ifdef PNG_IO_STATE_SUPPORTED |
| 156 | png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_HDR; |
| 157 | #endif |
| 158 | |
| 159 | /* Read the length and the chunk name. |
| 160 | * This must be performed in a single I/O call. |
| 161 | */ |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 162 | png_read_data(png_ptr, buf, 8); |
| 163 | length = png_get_uint_31(png_ptr, buf); |
| 164 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 165 | /* Put the chunk name into png_ptr->chunk_name. */ |
| 166 | png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(buf+4); |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 167 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 168 | png_debug2(0, "Reading %lx chunk, length = %lu", |
| 169 | (unsigned long)png_ptr->chunk_name, (unsigned long)length); |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 170 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 171 | /* Reset the crc and run it over the chunk name. */ |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 172 | png_reset_crc(png_ptr); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 173 | png_calculate_crc(png_ptr, buf + 4, 4); |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 174 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 175 | /* Check to see if chunk name is valid. */ |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 176 | png_check_chunk_name(png_ptr, png_ptr->chunk_name); |
| 177 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 178 | #ifdef PNG_IO_STATE_SUPPORTED |
| 179 | png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_DATA; |
| 180 | #endif |
| 181 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 182 | return length; |
| 183 | } |
| 184 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 185 | /* Read data, and (optionally) run it through the CRC. */ |
| 186 | void /* PRIVATE */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 187 | png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 188 | { |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 189 | if (png_ptr == NULL) |
| 190 | return; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 191 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 192 | png_read_data(png_ptr, buf, length); |
| 193 | png_calculate_crc(png_ptr, buf, length); |
| 194 | } |
| 195 | |
| 196 | /* Optionally skip data and then check the CRC. Depending on whether we |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 197 | * are reading an ancillary or critical chunk, and how the program has set |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 198 | * things up, we may calculate the CRC on the data and print a message. |
| 199 | * Returns '1' if there was a CRC error, '0' otherwise. |
| 200 | */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 201 | int /* PRIVATE */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 202 | png_crc_finish(png_structrp png_ptr, png_uint_32 skip) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 203 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 204 | /* The size of the local buffer for inflate is a good guess as to a |
| 205 | * reasonable size to use for buffering reads from the application. |
| 206 | */ |
| 207 | while (skip > 0) |
| 208 | { |
| 209 | png_uint_32 len; |
| 210 | png_byte tmpbuf[PNG_INFLATE_BUF_SIZE]; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 211 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 212 | len = (sizeof tmpbuf); |
| 213 | if (len > skip) |
| 214 | len = skip; |
| 215 | skip -= len; |
| 216 | |
| 217 | png_crc_read(png_ptr, tmpbuf, len); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | if (png_crc_error(png_ptr)) |
| 221 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 222 | if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name) ? |
| 223 | !(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) : |
| 224 | (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE)) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 225 | { |
| 226 | png_chunk_warning(png_ptr, "CRC error"); |
| 227 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 228 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 229 | else |
| 230 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 231 | png_chunk_benign_error(png_ptr, "CRC error"); |
| 232 | return (0); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 233 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 234 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 235 | return (1); |
| 236 | } |
| 237 | |
| 238 | return (0); |
| 239 | } |
| 240 | |
John Reck | 90bf0d2 | 2013-08-14 10:37:42 -0700 | [diff] [blame] | 241 | #ifdef PNG_INDEX_SUPPORTED |
| 242 | int /* PRIVATE */ |
| 243 | png_opt_crc_finish(png_structrp png_ptr, png_uint_32 skip) |
| 244 | { |
| 245 | while (skip > 0) |
| 246 | { |
| 247 | png_uint_32 len; |
| 248 | png_byte tmpbuf[PNG_INFLATE_BUF_SIZE]; |
| 249 | |
| 250 | len = (sizeof tmpbuf); |
| 251 | if (len > skip) |
| 252 | len = skip; |
| 253 | skip -= len; |
| 254 | |
| 255 | png_crc_read(png_ptr, tmpbuf, len); |
| 256 | } |
| 257 | |
| 258 | if (png_crc_error(png_ptr)) |
| 259 | { |
| 260 | png_chunk_warning(png_ptr, "CRC error"); |
| 261 | return (1); |
| 262 | } |
| 263 | |
| 264 | return (0); |
| 265 | } |
| 266 | #endif |
| 267 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 268 | /* Compare the CRC stored in the PNG file with that calculated by libpng from |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 269 | * the data it has read thus far. |
| 270 | */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 271 | int /* PRIVATE */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 272 | png_crc_error(png_structrp png_ptr) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 273 | { |
| 274 | png_byte crc_bytes[4]; |
| 275 | png_uint_32 crc; |
| 276 | int need_crc = 1; |
| 277 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 278 | if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name)) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 279 | { |
| 280 | if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) == |
| 281 | (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN)) |
| 282 | need_crc = 0; |
| 283 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 284 | |
| 285 | else /* critical */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 286 | { |
| 287 | if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) |
| 288 | need_crc = 0; |
| 289 | } |
| 290 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 291 | #ifdef PNG_IO_STATE_SUPPORTED |
| 292 | png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_CRC; |
| 293 | #endif |
| 294 | |
| 295 | /* The chunk CRC must be serialized in a single I/O call. */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 296 | png_read_data(png_ptr, crc_bytes, 4); |
| 297 | |
| 298 | if (need_crc) |
| 299 | { |
| 300 | crc = png_get_uint_32(crc_bytes); |
| 301 | return ((int)(crc != png_ptr->crc)); |
| 302 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 303 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 304 | else |
| 305 | return (0); |
| 306 | } |
| 307 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 308 | /* Manage the read buffer; this simply reallocates the buffer if it is not small |
| 309 | * enough (or if it is not allocated). The routine returns a pointer to the |
| 310 | * buffer; if an error occurs and 'warn' is set the routine returns NULL, else |
| 311 | * it will call png_error (via png_malloc) on failure. (warn == 2 means |
| 312 | * 'silent'). |
| 313 | */ |
| 314 | static png_bytep |
| 315 | png_read_buffer(png_structrp png_ptr, png_alloc_size_t new_size, int warn) |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 316 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 317 | png_bytep buffer = png_ptr->read_buffer; |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 318 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 319 | if (buffer != NULL && new_size > png_ptr->read_buffer_size) |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 320 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 321 | png_ptr->read_buffer = NULL; |
| 322 | png_ptr->read_buffer = NULL; |
| 323 | png_ptr->read_buffer_size = 0; |
| 324 | png_free(png_ptr, buffer); |
| 325 | buffer = NULL; |
| 326 | } |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 327 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 328 | if (buffer == NULL) |
| 329 | { |
| 330 | buffer = png_voidcast(png_bytep, png_malloc_base(png_ptr, new_size)); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 331 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 332 | if (buffer != NULL) |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 333 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 334 | png_ptr->read_buffer = buffer; |
| 335 | png_ptr->read_buffer_size = new_size; |
| 336 | } |
| 337 | |
| 338 | else if (warn < 2) /* else silent */ |
| 339 | { |
| 340 | #ifdef PNG_WARNINGS_SUPPORTED |
| 341 | if (warn) |
| 342 | png_chunk_warning(png_ptr, "insufficient memory to read chunk"); |
| 343 | else |
| 344 | #endif |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 345 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 346 | #ifdef PNG_ERROR_TEXT_SUPPORTED |
| 347 | png_chunk_error(png_ptr, "insufficient memory to read chunk"); |
| 348 | #endif |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 349 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | |
| 353 | return buffer; |
| 354 | } |
| 355 | |
| 356 | /* png_inflate_claim: claim the zstream for some nefarious purpose that involves |
| 357 | * decompression. Returns Z_OK on success, else a zlib error code. It checks |
| 358 | * the owner but, in final release builds, just issues a warning if some other |
| 359 | * chunk apparently owns the stream. Prior to release it does a png_error. |
| 360 | */ |
| 361 | static int |
| 362 | png_inflate_claim(png_structrp png_ptr, png_uint_32 owner) |
| 363 | { |
| 364 | if (png_ptr->zowner != 0) |
| 365 | { |
| 366 | char msg[64]; |
| 367 | |
| 368 | PNG_STRING_FROM_CHUNK(msg, png_ptr->zowner); |
| 369 | /* So the message that results is "<chunk> using zstream"; this is an |
| 370 | * internal error, but is very useful for debugging. i18n requirements |
| 371 | * are minimal. |
| 372 | */ |
| 373 | (void)png_safecat(msg, (sizeof msg), 4, " using zstream"); |
| 374 | # if PNG_LIBPNG_BUILD_BASE_TYPE >= PNG_LIBPNG_BUILD_RC |
| 375 | png_chunk_warning(png_ptr, msg); |
| 376 | png_ptr->zowner = 0; |
| 377 | # else |
| 378 | png_chunk_error(png_ptr, msg); |
| 379 | # endif |
| 380 | } |
| 381 | |
| 382 | /* Implementation note: unlike 'png_deflate_claim' this internal function |
| 383 | * does not take the size of the data as an argument. Some efficiency could |
| 384 | * be gained by using this when it is known *if* the zlib stream itself does |
| 385 | * not record the number; however, this is an illusion: the original writer |
| 386 | * of the PNG may have selected a lower window size, and we really must |
| 387 | * follow that because, for systems with with limited capabilities, we |
| 388 | * would otherwise reject the application's attempts to use a smaller window |
| 389 | * size (zlib doesn't have an interface to say "this or lower"!). |
| 390 | * |
| 391 | * inflateReset2 was added to zlib 1.2.4; before this the window could not be |
| 392 | * reset, therefore it is necessary to always allocate the maximum window |
| 393 | * size with earlier zlibs just in case later compressed chunks need it. |
| 394 | */ |
| 395 | { |
| 396 | int ret; /* zlib return code */ |
| 397 | # if PNG_ZLIB_VERNUM >= 0x1240 |
| 398 | |
| 399 | # if defined(PNG_SET_OPTION_SUPPORTED) && \ |
| 400 | defined(PNG_MAXIMUM_INFLATE_WINDOW) |
| 401 | int window_bits; |
| 402 | |
| 403 | if (((png_ptr->options >> PNG_MAXIMUM_INFLATE_WINDOW) & 3) == |
| 404 | PNG_OPTION_ON) |
| 405 | window_bits = 15; |
| 406 | |
| 407 | else |
| 408 | window_bits = 0; |
| 409 | # else |
| 410 | # define window_bits 0 |
| 411 | # endif |
| 412 | # endif |
| 413 | |
| 414 | /* Set this for safety, just in case the previous owner left pointers to |
| 415 | * memory allocations. |
| 416 | */ |
| 417 | png_ptr->zstream.next_in = NULL; |
| 418 | png_ptr->zstream.avail_in = 0; |
| 419 | png_ptr->zstream.next_out = NULL; |
| 420 | png_ptr->zstream.avail_out = 0; |
| 421 | |
| 422 | if (png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) |
| 423 | { |
| 424 | # if PNG_ZLIB_VERNUM < 0x1240 |
| 425 | ret = inflateReset(&png_ptr->zstream); |
| 426 | # else |
| 427 | ret = inflateReset2(&png_ptr->zstream, window_bits); |
| 428 | # endif |
| 429 | } |
| 430 | |
| 431 | else |
| 432 | { |
| 433 | # if PNG_ZLIB_VERNUM < 0x1240 |
| 434 | ret = inflateInit(&png_ptr->zstream); |
| 435 | # else |
| 436 | ret = inflateInit2(&png_ptr->zstream, window_bits); |
| 437 | # endif |
| 438 | |
| 439 | if (ret == Z_OK) |
| 440 | png_ptr->flags |= PNG_FLAG_ZSTREAM_INITIALIZED; |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | if (ret == Z_OK) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 444 | png_ptr->zowner = owner; |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 445 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 446 | else |
| 447 | png_zstream_error(png_ptr, ret); |
| 448 | |
| 449 | return ret; |
| 450 | } |
| 451 | |
| 452 | # ifdef window_bits |
| 453 | # undef window_bits |
| 454 | # endif |
| 455 | } |
| 456 | |
| 457 | #ifdef PNG_READ_COMPRESSED_TEXT_SUPPORTED |
| 458 | /* png_inflate now returns zlib error codes including Z_OK and Z_STREAM_END to |
| 459 | * allow the caller to do multiple calls if required. If the 'finish' flag is |
| 460 | * set Z_FINISH will be passed to the final inflate() call and Z_STREAM_END must |
| 461 | * be returned or there has been a problem, otherwise Z_SYNC_FLUSH is used and |
| 462 | * Z_OK or Z_STREAM_END will be returned on success. |
| 463 | * |
| 464 | * The input and output sizes are updated to the actual amounts of data consumed |
| 465 | * or written, not the amount available (as in a z_stream). The data pointers |
| 466 | * are not changed, so the next input is (data+input_size) and the next |
| 467 | * available output is (output+output_size). |
| 468 | */ |
| 469 | static int |
| 470 | png_inflate(png_structrp png_ptr, png_uint_32 owner, int finish, |
| 471 | /* INPUT: */ png_const_bytep input, png_uint_32p input_size_ptr, |
| 472 | /* OUTPUT: */ png_bytep output, png_alloc_size_t *output_size_ptr) |
| 473 | { |
| 474 | if (png_ptr->zowner == owner) /* Else not claimed */ |
| 475 | { |
| 476 | int ret; |
| 477 | png_alloc_size_t avail_out = *output_size_ptr; |
| 478 | png_uint_32 avail_in = *input_size_ptr; |
| 479 | |
| 480 | /* zlib can't necessarily handle more than 65535 bytes at once (i.e. it |
| 481 | * can't even necessarily handle 65536 bytes) because the type uInt is |
| 482 | * "16 bits or more". Consequently it is necessary to chunk the input to |
| 483 | * zlib. This code uses ZLIB_IO_MAX, from pngpriv.h, as the maximum (the |
| 484 | * maximum value that can be stored in a uInt.) It is possible to set |
| 485 | * ZLIB_IO_MAX to a lower value in pngpriv.h and this may sometimes have |
| 486 | * a performance advantage, because it reduces the amount of data accessed |
| 487 | * at each step and that may give the OS more time to page it in. |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 488 | */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 489 | png_ptr->zstream.next_in = PNGZ_INPUT_CAST(input); |
| 490 | /* avail_in and avail_out are set below from 'size' */ |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 491 | png_ptr->zstream.avail_in = 0; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 492 | png_ptr->zstream.avail_out = 0; |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 493 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 494 | /* Read directly into the output if it is available (this is set to |
| 495 | * a local buffer below if output is NULL). |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 496 | */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 497 | if (output != NULL) |
| 498 | png_ptr->zstream.next_out = output; |
| 499 | |
| 500 | do |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 501 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 502 | uInt avail; |
| 503 | Byte local_buffer[PNG_INFLATE_BUF_SIZE]; |
| 504 | |
| 505 | /* zlib INPUT BUFFER */ |
| 506 | /* The setting of 'avail_in' used to be outside the loop; by setting it |
| 507 | * inside it is possible to chunk the input to zlib and simply rely on |
| 508 | * zlib to advance the 'next_in' pointer. This allows arbitrary |
| 509 | * amounts of data to be passed through zlib at the unavoidable cost of |
| 510 | * requiring a window save (memcpy of up to 32768 output bytes) |
| 511 | * every ZLIB_IO_MAX input bytes. |
| 512 | */ |
| 513 | avail_in += png_ptr->zstream.avail_in; /* not consumed last time */ |
| 514 | |
| 515 | avail = ZLIB_IO_MAX; |
| 516 | |
| 517 | if (avail_in < avail) |
| 518 | avail = (uInt)avail_in; /* safe: < than ZLIB_IO_MAX */ |
| 519 | |
| 520 | avail_in -= avail; |
| 521 | png_ptr->zstream.avail_in = avail; |
| 522 | |
| 523 | /* zlib OUTPUT BUFFER */ |
| 524 | avail_out += png_ptr->zstream.avail_out; /* not written last time */ |
| 525 | |
| 526 | avail = ZLIB_IO_MAX; /* maximum zlib can process */ |
| 527 | |
| 528 | if (output == NULL) |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 529 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 530 | /* Reset the output buffer each time round if output is NULL and |
| 531 | * make available the full buffer, up to 'remaining_space' |
| 532 | */ |
| 533 | png_ptr->zstream.next_out = local_buffer; |
| 534 | if ((sizeof local_buffer) < avail) |
| 535 | avail = (sizeof local_buffer); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 536 | } |
| 537 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 538 | if (avail_out < avail) |
| 539 | avail = (uInt)avail_out; /* safe: < ZLIB_IO_MAX */ |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 540 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 541 | png_ptr->zstream.avail_out = avail; |
| 542 | avail_out -= avail; |
| 543 | |
| 544 | /* zlib inflate call */ |
| 545 | /* In fact 'avail_out' may be 0 at this point, that happens at the end |
| 546 | * of the read when the final LZ end code was not passed at the end of |
| 547 | * the previous chunk of input data. Tell zlib if we have reached the |
| 548 | * end of the output buffer. |
| 549 | */ |
| 550 | ret = inflate(&png_ptr->zstream, avail_out > 0 ? Z_NO_FLUSH : |
| 551 | (finish ? Z_FINISH : Z_SYNC_FLUSH)); |
| 552 | } while (ret == Z_OK); |
| 553 | |
| 554 | /* For safety kill the local buffer pointer now */ |
| 555 | if (output == NULL) |
| 556 | png_ptr->zstream.next_out = NULL; |
| 557 | |
| 558 | /* Claw back the 'size' and 'remaining_space' byte counts. */ |
| 559 | avail_in += png_ptr->zstream.avail_in; |
| 560 | avail_out += png_ptr->zstream.avail_out; |
| 561 | |
| 562 | /* Update the input and output sizes; the updated values are the amount |
| 563 | * consumed or written, effectively the inverse of what zlib uses. |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 564 | */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 565 | if (avail_out > 0) |
| 566 | *output_size_ptr -= avail_out; |
| 567 | |
| 568 | if (avail_in > 0) |
| 569 | *input_size_ptr -= avail_in; |
| 570 | |
| 571 | /* Ensure png_ptr->zstream.msg is set (even in the success case!) */ |
| 572 | png_zstream_error(png_ptr, ret); |
| 573 | return ret; |
| 574 | } |
| 575 | |
| 576 | else |
| 577 | { |
| 578 | /* This is a bad internal error. The recovery assigns to the zstream msg |
| 579 | * pointer, which is not owned by the caller, but this is safe; it's only |
| 580 | * used on errors! |
| 581 | */ |
| 582 | png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed"); |
| 583 | return Z_STREAM_ERROR; |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 584 | } |
| 585 | } |
| 586 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 587 | /* |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 588 | * Decompress trailing data in a chunk. The assumption is that read_buffer |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 589 | * points at an allocated area holding the contents of a chunk with a |
| 590 | * trailing compressed part. What we get back is an allocated area |
| 591 | * holding the original prefix part and an uncompressed version of the |
| 592 | * trailing part (the malloc area passed in is freed). |
| 593 | */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 594 | static int |
| 595 | png_decompress_chunk(png_structrp png_ptr, |
| 596 | png_uint_32 chunklength, png_uint_32 prefix_size, |
| 597 | png_alloc_size_t *newlength /* must be initialized to the maximum! */, |
| 598 | int terminate /*add a '\0' to the end of the uncompressed data*/) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 599 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 600 | /* TODO: implement different limits for different types of chunk. |
| 601 | * |
| 602 | * The caller supplies *newlength set to the maximum length of the |
| 603 | * uncompressed data, but this routine allocates space for the prefix and |
| 604 | * maybe a '\0' terminator too. We have to assume that 'prefix_size' is |
| 605 | * limited only by the maximum chunk size. |
| 606 | */ |
| 607 | png_alloc_size_t limit = PNG_SIZE_MAX; |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 608 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 609 | # ifdef PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED |
| 610 | if (png_ptr->user_chunk_malloc_max > 0 && |
| 611 | png_ptr->user_chunk_malloc_max < limit) |
| 612 | limit = png_ptr->user_chunk_malloc_max; |
| 613 | # elif PNG_USER_CHUNK_MALLOC_MAX > 0 |
| 614 | if (PNG_USER_CHUNK_MALLOC_MAX < limit) |
| 615 | limit = PNG_USER_CHUNK_MALLOC_MAX; |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 616 | # endif |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 617 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 618 | if (limit >= prefix_size + (terminate != 0)) |
| 619 | { |
| 620 | int ret; |
| 621 | |
| 622 | limit -= prefix_size + (terminate != 0); |
| 623 | |
| 624 | if (limit < *newlength) |
| 625 | *newlength = limit; |
| 626 | |
| 627 | /* Now try to claim the stream. */ |
| 628 | ret = png_inflate_claim(png_ptr, png_ptr->chunk_name); |
| 629 | |
| 630 | if (ret == Z_OK) |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 631 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 632 | png_uint_32 lzsize = chunklength - prefix_size; |
Dave Burke | ccee121 | 2012-03-05 22:36:13 -0800 | [diff] [blame] | 633 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 634 | ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/, |
| 635 | /* input: */ png_ptr->read_buffer + prefix_size, &lzsize, |
| 636 | /* output: */ NULL, newlength); |
| 637 | |
| 638 | if (ret == Z_STREAM_END) |
Dave Burke | ccee121 | 2012-03-05 22:36:13 -0800 | [diff] [blame] | 639 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 640 | /* Use 'inflateReset' here, not 'inflateReset2' because this |
| 641 | * preserves the previously decided window size (otherwise it would |
| 642 | * be necessary to store the previous window size.) In practice |
| 643 | * this doesn't matter anyway, because png_inflate will call inflate |
| 644 | * with Z_FINISH in almost all cases, so the window will not be |
| 645 | * maintained. |
| 646 | */ |
| 647 | if (inflateReset(&png_ptr->zstream) == Z_OK) |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 648 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 649 | /* Because of the limit checks above we know that the new, |
| 650 | * expanded, size will fit in a size_t (let alone an |
| 651 | * png_alloc_size_t). Use png_malloc_base here to avoid an |
| 652 | * extra OOM message. |
| 653 | */ |
| 654 | png_alloc_size_t new_size = *newlength; |
| 655 | png_alloc_size_t buffer_size = prefix_size + new_size + |
| 656 | (terminate != 0); |
| 657 | png_bytep text = png_voidcast(png_bytep, png_malloc_base(png_ptr, |
| 658 | buffer_size)); |
| 659 | |
| 660 | if (text != NULL) |
| 661 | { |
| 662 | ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/, |
| 663 | png_ptr->read_buffer + prefix_size, &lzsize, |
| 664 | text + prefix_size, newlength); |
| 665 | |
| 666 | if (ret == Z_STREAM_END) |
| 667 | { |
| 668 | if (new_size == *newlength) |
| 669 | { |
| 670 | if (terminate) |
| 671 | text[prefix_size + *newlength] = 0; |
| 672 | |
| 673 | if (prefix_size > 0) |
| 674 | memcpy(text, png_ptr->read_buffer, prefix_size); |
| 675 | |
| 676 | { |
| 677 | png_bytep old_ptr = png_ptr->read_buffer; |
| 678 | |
| 679 | png_ptr->read_buffer = text; |
| 680 | png_ptr->read_buffer_size = buffer_size; |
| 681 | text = old_ptr; /* freed below */ |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | else |
| 686 | { |
| 687 | /* The size changed on the second read, there can be no |
| 688 | * guarantee that anything is correct at this point. |
| 689 | * The 'msg' pointer has been set to "unexpected end of |
| 690 | * LZ stream", which is fine, but return an error code |
| 691 | * that the caller won't accept. |
| 692 | */ |
| 693 | ret = PNG_UNEXPECTED_ZLIB_RETURN; |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | else if (ret == Z_OK) |
| 698 | ret = PNG_UNEXPECTED_ZLIB_RETURN; /* for safety */ |
| 699 | |
| 700 | /* Free the text pointer (this is the old read_buffer on |
| 701 | * success) |
| 702 | */ |
| 703 | png_free(png_ptr, text); |
| 704 | |
| 705 | /* This really is very benign, but it's still an error because |
| 706 | * the extra space may otherwise be used as a Trojan Horse. |
| 707 | */ |
| 708 | if (ret == Z_STREAM_END && |
| 709 | chunklength - prefix_size != lzsize) |
| 710 | png_chunk_benign_error(png_ptr, "extra compressed data"); |
| 711 | } |
| 712 | |
| 713 | else |
| 714 | { |
| 715 | /* Out of memory allocating the buffer */ |
| 716 | ret = Z_MEM_ERROR; |
| 717 | png_zstream_error(png_ptr, Z_MEM_ERROR); |
| 718 | } |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 719 | } |
| 720 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 721 | else |
| 722 | { |
| 723 | /* inflateReset failed, store the error message */ |
| 724 | png_zstream_error(png_ptr, ret); |
| 725 | |
| 726 | if (ret == Z_STREAM_END) |
| 727 | ret = PNG_UNEXPECTED_ZLIB_RETURN; |
| 728 | } |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 729 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 730 | |
| 731 | else if (ret == Z_OK) |
| 732 | ret = PNG_UNEXPECTED_ZLIB_RETURN; |
| 733 | |
| 734 | /* Release the claimed stream */ |
| 735 | png_ptr->zowner = 0; |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 736 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 737 | |
| 738 | else /* the claim failed */ if (ret == Z_STREAM_END) /* impossible! */ |
| 739 | ret = PNG_UNEXPECTED_ZLIB_RETURN; |
| 740 | |
| 741 | return ret; |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 742 | } |
| 743 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 744 | else |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 745 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 746 | /* Application/configuration limits exceeded */ |
| 747 | png_zstream_error(png_ptr, Z_MEM_ERROR); |
| 748 | return Z_MEM_ERROR; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 749 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 750 | } |
| 751 | #endif /* PNG_READ_COMPRESSED_TEXT_SUPPORTED */ |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 752 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 753 | #ifdef PNG_READ_iCCP_SUPPORTED |
| 754 | /* Perform a partial read and decompress, producing 'avail_out' bytes and |
| 755 | * reading from the current chunk as required. |
| 756 | */ |
| 757 | static int |
| 758 | png_inflate_read(png_structrp png_ptr, png_bytep read_buffer, uInt read_size, |
| 759 | png_uint_32p chunk_bytes, png_bytep next_out, png_alloc_size_t *out_size, |
| 760 | int finish) |
| 761 | { |
| 762 | if (png_ptr->zowner == png_ptr->chunk_name) |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 763 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 764 | int ret; |
| 765 | |
| 766 | /* next_in and avail_in must have been initialized by the caller. */ |
| 767 | png_ptr->zstream.next_out = next_out; |
| 768 | png_ptr->zstream.avail_out = 0; /* set in the loop */ |
| 769 | |
| 770 | do |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 771 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 772 | if (png_ptr->zstream.avail_in == 0) |
| 773 | { |
| 774 | if (read_size > *chunk_bytes) |
| 775 | read_size = (uInt)*chunk_bytes; |
| 776 | *chunk_bytes -= read_size; |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 777 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 778 | if (read_size > 0) |
| 779 | png_crc_read(png_ptr, read_buffer, read_size); |
| 780 | |
| 781 | png_ptr->zstream.next_in = read_buffer; |
| 782 | png_ptr->zstream.avail_in = read_size; |
| 783 | } |
| 784 | |
| 785 | if (png_ptr->zstream.avail_out == 0) |
| 786 | { |
| 787 | uInt avail = ZLIB_IO_MAX; |
| 788 | if (avail > *out_size) |
| 789 | avail = (uInt)*out_size; |
| 790 | *out_size -= avail; |
| 791 | |
| 792 | png_ptr->zstream.avail_out = avail; |
| 793 | } |
| 794 | |
| 795 | /* Use Z_SYNC_FLUSH when there is no more chunk data to ensure that all |
| 796 | * the available output is produced; this allows reading of truncated |
| 797 | * streams. |
| 798 | */ |
| 799 | ret = inflate(&png_ptr->zstream, |
| 800 | *chunk_bytes > 0 ? Z_NO_FLUSH : (finish ? Z_FINISH : Z_SYNC_FLUSH)); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 801 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 802 | while (ret == Z_OK && (*out_size > 0 || png_ptr->zstream.avail_out > 0)); |
| 803 | |
| 804 | *out_size += png_ptr->zstream.avail_out; |
| 805 | png_ptr->zstream.avail_out = 0; /* Should not be required, but is safe */ |
| 806 | |
| 807 | /* Ensure the error message pointer is always set: */ |
| 808 | png_zstream_error(png_ptr, ret); |
| 809 | return ret; |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 810 | } |
| 811 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 812 | else |
| 813 | { |
| 814 | png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed"); |
| 815 | return Z_STREAM_ERROR; |
| 816 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 817 | } |
| 818 | #endif |
| 819 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 820 | /* Read and check the IDHR chunk */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 821 | void /* PRIVATE */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 822 | png_handle_IHDR(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 823 | { |
| 824 | png_byte buf[13]; |
| 825 | png_uint_32 width, height; |
| 826 | int bit_depth, color_type, compression_type, filter_type; |
| 827 | int interlace_type; |
| 828 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 829 | png_debug(1, "in png_handle_IHDR"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 830 | |
| 831 | if (png_ptr->mode & PNG_HAVE_IHDR) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 832 | png_chunk_error(png_ptr, "out of place"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 833 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 834 | /* Check the length */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 835 | if (length != 13) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 836 | png_chunk_error(png_ptr, "invalid"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 837 | |
| 838 | png_ptr->mode |= PNG_HAVE_IHDR; |
| 839 | |
| 840 | png_crc_read(png_ptr, buf, 13); |
| 841 | png_crc_finish(png_ptr, 0); |
| 842 | |
| 843 | width = png_get_uint_31(png_ptr, buf); |
| 844 | height = png_get_uint_31(png_ptr, buf + 4); |
| 845 | bit_depth = buf[8]; |
| 846 | color_type = buf[9]; |
| 847 | compression_type = buf[10]; |
| 848 | filter_type = buf[11]; |
| 849 | interlace_type = buf[12]; |
| 850 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 851 | /* Set internal variables */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 852 | png_ptr->width = width; |
| 853 | png_ptr->height = height; |
| 854 | png_ptr->bit_depth = (png_byte)bit_depth; |
| 855 | png_ptr->interlaced = (png_byte)interlace_type; |
| 856 | png_ptr->color_type = (png_byte)color_type; |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 857 | #ifdef PNG_MNG_FEATURES_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 858 | png_ptr->filter_type = (png_byte)filter_type; |
| 859 | #endif |
| 860 | png_ptr->compression_type = (png_byte)compression_type; |
| 861 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 862 | /* Find number of channels */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 863 | switch (png_ptr->color_type) |
| 864 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 865 | default: /* invalid, png_set_IHDR calls png_error */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 866 | case PNG_COLOR_TYPE_GRAY: |
| 867 | case PNG_COLOR_TYPE_PALETTE: |
| 868 | png_ptr->channels = 1; |
| 869 | break; |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 870 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 871 | case PNG_COLOR_TYPE_RGB: |
| 872 | png_ptr->channels = 3; |
| 873 | break; |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 874 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 875 | case PNG_COLOR_TYPE_GRAY_ALPHA: |
| 876 | png_ptr->channels = 2; |
| 877 | break; |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 878 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 879 | case PNG_COLOR_TYPE_RGB_ALPHA: |
| 880 | png_ptr->channels = 4; |
| 881 | break; |
| 882 | } |
| 883 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 884 | /* Set up other useful info */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 885 | png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth * |
| 886 | png_ptr->channels); |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 887 | png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width); |
| 888 | png_debug1(3, "bit_depth = %d", png_ptr->bit_depth); |
| 889 | png_debug1(3, "channels = %d", png_ptr->channels); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 890 | png_debug1(3, "rowbytes = %lu", (unsigned long)png_ptr->rowbytes); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 891 | png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 892 | color_type, interlace_type, compression_type, filter_type); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 893 | } |
| 894 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 895 | /* Read and check the palette */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 896 | void /* PRIVATE */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 897 | png_handle_PLTE(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 898 | { |
| 899 | png_color palette[PNG_MAX_PALETTE_LENGTH]; |
| 900 | int num, i; |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 901 | #ifdef PNG_POINTER_INDEXING_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 902 | png_colorp pal_ptr; |
| 903 | #endif |
| 904 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 905 | png_debug(1, "in png_handle_PLTE"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 906 | |
| 907 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 908 | png_chunk_error(png_ptr, "missing IHDR"); |
| 909 | |
| 910 | /* Moved to before the 'after IDAT' check below because otherwise duplicate |
| 911 | * PLTE chunks are potentially ignored (the spec says there shall not be more |
| 912 | * than one PLTE, the error is not treated as benign, so this check trumps |
| 913 | * the requirement that PLTE appears before IDAT.) |
| 914 | */ |
| 915 | else if (png_ptr->mode & PNG_HAVE_PLTE) |
| 916 | png_chunk_error(png_ptr, "duplicate"); |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 917 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 918 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 919 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 920 | /* This is benign because the non-benign error happened before, when an |
| 921 | * IDAT was encountered in a color-mapped image with no PLTE. |
| 922 | */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 923 | png_crc_finish(png_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 924 | png_chunk_benign_error(png_ptr, "out of place"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 925 | return; |
| 926 | } |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 927 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 928 | png_ptr->mode |= PNG_HAVE_PLTE; |
| 929 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 930 | if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 931 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 932 | png_crc_finish(png_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 933 | png_chunk_benign_error(png_ptr, "ignored in grayscale PNG"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 934 | return; |
| 935 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 936 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 937 | #ifndef PNG_READ_OPT_PLTE_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 938 | if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE) |
| 939 | { |
| 940 | png_crc_finish(png_ptr, length); |
| 941 | return; |
| 942 | } |
| 943 | #endif |
| 944 | |
| 945 | if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3) |
| 946 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 947 | png_crc_finish(png_ptr, length); |
| 948 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 949 | if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 950 | png_chunk_benign_error(png_ptr, "invalid"); |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 951 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 952 | else |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 953 | png_chunk_error(png_ptr, "invalid"); |
| 954 | |
| 955 | return; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 956 | } |
| 957 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 958 | /* The cast is safe because 'length' is less than 3*PNG_MAX_PALETTE_LENGTH */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 959 | num = (int)length / 3; |
| 960 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 961 | #ifdef PNG_POINTER_INDEXING_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 962 | for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++) |
| 963 | { |
| 964 | png_byte buf[3]; |
| 965 | |
| 966 | png_crc_read(png_ptr, buf, 3); |
| 967 | pal_ptr->red = buf[0]; |
| 968 | pal_ptr->green = buf[1]; |
| 969 | pal_ptr->blue = buf[2]; |
| 970 | } |
| 971 | #else |
| 972 | for (i = 0; i < num; i++) |
| 973 | { |
| 974 | png_byte buf[3]; |
| 975 | |
| 976 | png_crc_read(png_ptr, buf, 3); |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 977 | /* Don't depend upon png_color being any order */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 978 | palette[i].red = buf[0]; |
| 979 | palette[i].green = buf[1]; |
| 980 | palette[i].blue = buf[2]; |
| 981 | } |
| 982 | #endif |
| 983 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 984 | /* If we actually need the PLTE chunk (ie for a paletted image), we do |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 985 | * whatever the normal CRC configuration tells us. However, if we |
| 986 | * have an RGB image, the PLTE can be considered ancillary, so |
| 987 | * we will act as though it is. |
| 988 | */ |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 989 | #ifndef PNG_READ_OPT_PLTE_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 990 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 991 | #endif |
| 992 | { |
| 993 | png_crc_finish(png_ptr, 0); |
| 994 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 995 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 996 | #ifndef PNG_READ_OPT_PLTE_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 997 | else if (png_crc_error(png_ptr)) /* Only if we have a CRC error */ |
| 998 | { |
| 999 | /* If we don't want to use the data from an ancillary chunk, |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1000 | * we have two options: an error abort, or a warning and we |
| 1001 | * ignore the data in this chunk (which should be OK, since |
| 1002 | * it's considered ancillary for a RGB or RGBA image). |
| 1003 | * |
| 1004 | * IMPLEMENTATION NOTE: this is only here because png_crc_finish uses the |
| 1005 | * chunk type to determine whether to check the ancillary or the critical |
| 1006 | * flags. |
| 1007 | */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1008 | if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE)) |
| 1009 | { |
| 1010 | if (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) |
| 1011 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1012 | png_chunk_benign_error(png_ptr, "CRC error"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1013 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1014 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1015 | else |
| 1016 | { |
| 1017 | png_chunk_warning(png_ptr, "CRC error"); |
| 1018 | return; |
| 1019 | } |
| 1020 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1021 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1022 | /* Otherwise, we (optionally) emit a warning and use the chunk. */ |
| 1023 | else if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)) |
| 1024 | { |
| 1025 | png_chunk_warning(png_ptr, "CRC error"); |
| 1026 | } |
| 1027 | } |
| 1028 | #endif |
| 1029 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1030 | /* TODO: png_set_PLTE has the side effect of setting png_ptr->palette to its |
| 1031 | * own copy of the palette. This has the side effect that when png_start_row |
| 1032 | * is called (this happens after any call to png_read_update_info) the |
| 1033 | * info_ptr palette gets changed. This is extremely unexpected and |
| 1034 | * confusing. |
| 1035 | * |
| 1036 | * Fix this by not sharing the palette in this way. |
| 1037 | */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1038 | png_set_PLTE(png_ptr, info_ptr, palette, num); |
| 1039 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1040 | /* The three chunks, bKGD, hIST and tRNS *must* appear after PLTE and before |
| 1041 | * IDAT. Prior to 1.6.0 this was not checked; instead the code merely |
| 1042 | * checked the apparent validity of a tRNS chunk inserted before PLTE on a |
| 1043 | * palette PNG. 1.6.0 attempts to rigorously follow the standard and |
| 1044 | * therefore does a benign error if the erroneous condition is detected *and* |
| 1045 | * cancels the tRNS if the benign error returns. The alternative is to |
| 1046 | * amend the standard since it would be rather hypocritical of the standards |
| 1047 | * maintainers to ignore it. |
| 1048 | */ |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1049 | #ifdef PNG_READ_tRNS_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1050 | if (png_ptr->num_trans > 0 || |
| 1051 | (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS) != 0)) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1052 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1053 | /* Cancel this because otherwise it would be used if the transforms |
| 1054 | * require it. Don't cancel the 'valid' flag because this would prevent |
| 1055 | * detection of duplicate chunks. |
| 1056 | */ |
| 1057 | png_ptr->num_trans = 0; |
| 1058 | |
| 1059 | if (info_ptr != NULL) |
| 1060 | info_ptr->num_trans = 0; |
| 1061 | |
| 1062 | png_chunk_benign_error(png_ptr, "tRNS must be after"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1063 | } |
| 1064 | #endif |
| 1065 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1066 | #ifdef PNG_READ_hIST_SUPPORTED |
| 1067 | if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST) != 0) |
| 1068 | png_chunk_benign_error(png_ptr, "hIST must be after"); |
| 1069 | #endif |
| 1070 | |
| 1071 | #ifdef PNG_READ_bKGD_SUPPORTED |
| 1072 | if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD) != 0) |
| 1073 | png_chunk_benign_error(png_ptr, "bKGD must be after"); |
| 1074 | #endif |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | void /* PRIVATE */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1078 | png_handle_IEND(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1079 | { |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1080 | png_debug(1, "in png_handle_IEND"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1081 | |
| 1082 | if (!(png_ptr->mode & PNG_HAVE_IHDR) || !(png_ptr->mode & PNG_HAVE_IDAT)) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1083 | png_chunk_error(png_ptr, "out of place"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1084 | |
| 1085 | png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND); |
| 1086 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1087 | png_crc_finish(png_ptr, length); |
| 1088 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1089 | if (length != 0) |
| 1090 | png_chunk_benign_error(png_ptr, "invalid"); |
| 1091 | |
| 1092 | PNG_UNUSED(info_ptr) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1093 | } |
| 1094 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1095 | #ifdef PNG_READ_gAMA_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1096 | void /* PRIVATE */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1097 | png_handle_gAMA(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1098 | { |
| 1099 | png_fixed_point igamma; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1100 | png_byte buf[4]; |
| 1101 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1102 | png_debug(1, "in png_handle_gAMA"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1103 | |
| 1104 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1105 | png_chunk_error(png_ptr, "missing IHDR"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1106 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1107 | else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1108 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1109 | png_crc_finish(png_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1110 | png_chunk_benign_error(png_ptr, "out of place"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1111 | return; |
| 1112 | } |
| 1113 | |
| 1114 | if (length != 4) |
| 1115 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1116 | png_crc_finish(png_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1117 | png_chunk_benign_error(png_ptr, "invalid"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1118 | return; |
| 1119 | } |
| 1120 | |
| 1121 | png_crc_read(png_ptr, buf, 4); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1122 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1123 | if (png_crc_finish(png_ptr, 0)) |
| 1124 | return; |
| 1125 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1126 | igamma = png_get_fixed_point(NULL, buf); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1127 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1128 | png_colorspace_set_gamma(png_ptr, &png_ptr->colorspace, igamma); |
| 1129 | png_colorspace_sync(png_ptr, info_ptr); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1130 | } |
| 1131 | #endif |
| 1132 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1133 | #ifdef PNG_READ_sBIT_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1134 | void /* PRIVATE */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1135 | png_handle_sBIT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1136 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1137 | unsigned int truelen; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1138 | png_byte buf[4]; |
| 1139 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1140 | png_debug(1, "in png_handle_sBIT"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1141 | |
| 1142 | buf[0] = buf[1] = buf[2] = buf[3] = 0; |
| 1143 | |
| 1144 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1145 | png_chunk_error(png_ptr, "missing IHDR"); |
| 1146 | |
| 1147 | else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1148 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1149 | png_crc_finish(png_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1150 | png_chunk_benign_error(png_ptr, "out of place"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1151 | return; |
| 1152 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1153 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1154 | if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT)) |
| 1155 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1156 | png_crc_finish(png_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1157 | png_chunk_benign_error(png_ptr, "duplicate"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1158 | return; |
| 1159 | } |
| 1160 | |
| 1161 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 1162 | truelen = 3; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1163 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1164 | else |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1165 | truelen = png_ptr->channels; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1166 | |
| 1167 | if (length != truelen || length > 4) |
| 1168 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1169 | png_chunk_benign_error(png_ptr, "invalid"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1170 | png_crc_finish(png_ptr, length); |
| 1171 | return; |
| 1172 | } |
| 1173 | |
| 1174 | png_crc_read(png_ptr, buf, truelen); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1175 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1176 | if (png_crc_finish(png_ptr, 0)) |
| 1177 | return; |
| 1178 | |
| 1179 | if (png_ptr->color_type & PNG_COLOR_MASK_COLOR) |
| 1180 | { |
| 1181 | png_ptr->sig_bit.red = buf[0]; |
| 1182 | png_ptr->sig_bit.green = buf[1]; |
| 1183 | png_ptr->sig_bit.blue = buf[2]; |
| 1184 | png_ptr->sig_bit.alpha = buf[3]; |
| 1185 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1186 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1187 | else |
| 1188 | { |
| 1189 | png_ptr->sig_bit.gray = buf[0]; |
| 1190 | png_ptr->sig_bit.red = buf[0]; |
| 1191 | png_ptr->sig_bit.green = buf[0]; |
| 1192 | png_ptr->sig_bit.blue = buf[0]; |
| 1193 | png_ptr->sig_bit.alpha = buf[1]; |
| 1194 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1195 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1196 | png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit)); |
| 1197 | } |
| 1198 | #endif |
| 1199 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1200 | #ifdef PNG_READ_cHRM_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1201 | void /* PRIVATE */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1202 | png_handle_cHRM(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1203 | { |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1204 | png_byte buf[32]; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1205 | png_xy xy; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1206 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1207 | png_debug(1, "in png_handle_cHRM"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1208 | |
| 1209 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1210 | png_chunk_error(png_ptr, "missing IHDR"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1211 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1212 | else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1213 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1214 | png_crc_finish(png_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1215 | png_chunk_benign_error(png_ptr, "out of place"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1216 | return; |
| 1217 | } |
| 1218 | |
| 1219 | if (length != 32) |
| 1220 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1221 | png_crc_finish(png_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1222 | png_chunk_benign_error(png_ptr, "invalid"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1223 | return; |
| 1224 | } |
| 1225 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1226 | png_crc_read(png_ptr, buf, 32); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1227 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1228 | if (png_crc_finish(png_ptr, 0)) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1229 | return; |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1230 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1231 | xy.whitex = png_get_fixed_point(NULL, buf); |
| 1232 | xy.whitey = png_get_fixed_point(NULL, buf + 4); |
| 1233 | xy.redx = png_get_fixed_point(NULL, buf + 8); |
| 1234 | xy.redy = png_get_fixed_point(NULL, buf + 12); |
| 1235 | xy.greenx = png_get_fixed_point(NULL, buf + 16); |
| 1236 | xy.greeny = png_get_fixed_point(NULL, buf + 20); |
| 1237 | xy.bluex = png_get_fixed_point(NULL, buf + 24); |
| 1238 | xy.bluey = png_get_fixed_point(NULL, buf + 28); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1239 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1240 | if (xy.whitex == PNG_FIXED_ERROR || |
| 1241 | xy.whitey == PNG_FIXED_ERROR || |
| 1242 | xy.redx == PNG_FIXED_ERROR || |
| 1243 | xy.redy == PNG_FIXED_ERROR || |
| 1244 | xy.greenx == PNG_FIXED_ERROR || |
| 1245 | xy.greeny == PNG_FIXED_ERROR || |
| 1246 | xy.bluex == PNG_FIXED_ERROR || |
| 1247 | xy.bluey == PNG_FIXED_ERROR) |
| 1248 | { |
| 1249 | png_chunk_benign_error(png_ptr, "invalid values"); |
| 1250 | return; |
| 1251 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1252 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1253 | /* If a colorspace error has already been output skip this chunk */ |
| 1254 | if (png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) |
| 1255 | return; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1256 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1257 | if (png_ptr->colorspace.flags & PNG_COLORSPACE_FROM_cHRM) |
| 1258 | { |
| 1259 | png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID; |
| 1260 | png_colorspace_sync(png_ptr, info_ptr); |
| 1261 | png_chunk_benign_error(png_ptr, "duplicate"); |
| 1262 | return; |
| 1263 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1264 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1265 | png_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM; |
| 1266 | (void)png_colorspace_set_chromaticities(png_ptr, &png_ptr->colorspace, &xy, |
| 1267 | 1/*prefer cHRM values*/); |
| 1268 | png_colorspace_sync(png_ptr, info_ptr); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1269 | } |
| 1270 | #endif |
| 1271 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1272 | #ifdef PNG_READ_sRGB_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1273 | void /* PRIVATE */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1274 | png_handle_sRGB(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1275 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1276 | png_byte intent; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1277 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1278 | png_debug(1, "in png_handle_sRGB"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1279 | |
| 1280 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1281 | png_chunk_error(png_ptr, "missing IHDR"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1282 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1283 | else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1284 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1285 | png_crc_finish(png_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1286 | png_chunk_benign_error(png_ptr, "out of place"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1287 | return; |
| 1288 | } |
| 1289 | |
| 1290 | if (length != 1) |
| 1291 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1292 | png_crc_finish(png_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1293 | png_chunk_benign_error(png_ptr, "invalid"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1294 | return; |
| 1295 | } |
| 1296 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1297 | png_crc_read(png_ptr, &intent, 1); |
| 1298 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1299 | if (png_crc_finish(png_ptr, 0)) |
| 1300 | return; |
| 1301 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1302 | /* If a colorspace error has already been output skip this chunk */ |
| 1303 | if (png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) |
| 1304 | return; |
| 1305 | |
| 1306 | /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect |
| 1307 | * this. |
| 1308 | */ |
| 1309 | if (png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1310 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1311 | png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID; |
| 1312 | png_colorspace_sync(png_ptr, info_ptr); |
| 1313 | png_chunk_benign_error(png_ptr, "too many profiles"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1314 | return; |
| 1315 | } |
| 1316 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1317 | (void)png_colorspace_set_sRGB(png_ptr, &png_ptr->colorspace, intent); |
| 1318 | png_colorspace_sync(png_ptr, info_ptr); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1319 | } |
| 1320 | #endif /* PNG_READ_sRGB_SUPPORTED */ |
| 1321 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1322 | #ifdef PNG_READ_iCCP_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1323 | void /* PRIVATE */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1324 | png_handle_iCCP(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
| 1325 | /* Note: this does not properly handle profiles that are > 64K under DOS */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1326 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1327 | png_const_charp errmsg = NULL; /* error message output, or no error */ |
| 1328 | int finished = 0; /* crc checked */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1329 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1330 | png_debug(1, "in png_handle_iCCP"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1331 | |
| 1332 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1333 | png_chunk_error(png_ptr, "missing IHDR"); |
| 1334 | |
| 1335 | else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1336 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1337 | png_crc_finish(png_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1338 | png_chunk_benign_error(png_ptr, "out of place"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1339 | return; |
| 1340 | } |
| 1341 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1342 | /* Consistent with all the above colorspace handling an obviously *invalid* |
| 1343 | * chunk is just ignored, so does not invalidate the color space. An |
| 1344 | * alternative is to set the 'invalid' flags at the start of this routine |
| 1345 | * and only clear them in they were not set before and all the tests pass. |
| 1346 | * The minimum 'deflate' stream is assumed to be just the 2 byte header and 4 |
| 1347 | * byte checksum. The keyword must be one character and there is a |
| 1348 | * terminator (0) byte and the compression method. |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 1349 | */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1350 | if (length < 9) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1351 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1352 | png_crc_finish(png_ptr, length); |
| 1353 | png_chunk_benign_error(png_ptr, "too short"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1354 | return; |
| 1355 | } |
| 1356 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1357 | /* If a colorspace error has already been output skip this chunk */ |
| 1358 | if (png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1359 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1360 | png_crc_finish(png_ptr, length); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1361 | return; |
| 1362 | } |
| 1363 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1364 | /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect |
| 1365 | * this. |
| 1366 | */ |
| 1367 | if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) == 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1368 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1369 | uInt read_length, keyword_length; |
| 1370 | char keyword[81]; |
| 1371 | |
| 1372 | /* Find the keyword; the keyword plus separator and compression method |
| 1373 | * bytes can be at most 81 characters long. |
| 1374 | */ |
| 1375 | read_length = 81; /* maximum */ |
| 1376 | if (read_length > length) |
| 1377 | read_length = (uInt)length; |
| 1378 | |
| 1379 | png_crc_read(png_ptr, (png_bytep)keyword, read_length); |
| 1380 | length -= read_length; |
| 1381 | |
| 1382 | keyword_length = 0; |
| 1383 | while (keyword_length < 80 && keyword_length < read_length && |
| 1384 | keyword[keyword_length] != 0) |
| 1385 | ++keyword_length; |
| 1386 | |
| 1387 | /* TODO: make the keyword checking common */ |
| 1388 | if (keyword_length >= 1 && keyword_length <= 79) |
| 1389 | { |
| 1390 | /* We only understand '0' compression - deflate - so if we get a |
| 1391 | * different value we can't safely decode the chunk. |
| 1392 | */ |
| 1393 | if (keyword_length+1 < read_length && |
| 1394 | keyword[keyword_length+1] == PNG_COMPRESSION_TYPE_BASE) |
| 1395 | { |
| 1396 | read_length -= keyword_length+2; |
| 1397 | |
| 1398 | if (png_inflate_claim(png_ptr, png_iCCP) == Z_OK) |
| 1399 | { |
| 1400 | Byte profile_header[132]; |
| 1401 | Byte local_buffer[PNG_INFLATE_BUF_SIZE]; |
| 1402 | png_alloc_size_t size = (sizeof profile_header); |
| 1403 | |
| 1404 | png_ptr->zstream.next_in = (Bytef*)keyword + (keyword_length+2); |
| 1405 | png_ptr->zstream.avail_in = read_length; |
| 1406 | (void)png_inflate_read(png_ptr, local_buffer, |
| 1407 | (sizeof local_buffer), &length, profile_header, &size, |
| 1408 | 0/*finish: don't, because the output is too small*/); |
| 1409 | |
| 1410 | if (size == 0) |
| 1411 | { |
| 1412 | /* We have the ICC profile header; do the basic header checks. |
| 1413 | */ |
| 1414 | const png_uint_32 profile_length = |
| 1415 | png_get_uint_32(profile_header); |
| 1416 | |
| 1417 | if (png_icc_check_length(png_ptr, &png_ptr->colorspace, |
| 1418 | keyword, profile_length)) |
| 1419 | { |
| 1420 | /* The length is apparently ok, so we can check the 132 |
| 1421 | * byte header. |
| 1422 | */ |
| 1423 | if (png_icc_check_header(png_ptr, &png_ptr->colorspace, |
| 1424 | keyword, profile_length, profile_header, |
| 1425 | png_ptr->color_type)) |
| 1426 | { |
| 1427 | /* Now read the tag table; a variable size buffer is |
| 1428 | * needed at this point, allocate one for the whole |
| 1429 | * profile. The header check has already validated |
| 1430 | * that none of these stuff will overflow. |
| 1431 | */ |
| 1432 | const png_uint_32 tag_count = png_get_uint_32( |
| 1433 | profile_header+128); |
| 1434 | png_bytep profile = png_read_buffer(png_ptr, |
| 1435 | profile_length, 2/*silent*/); |
| 1436 | |
| 1437 | if (profile != NULL) |
| 1438 | { |
| 1439 | memcpy(profile, profile_header, |
| 1440 | (sizeof profile_header)); |
| 1441 | |
| 1442 | size = 12 * tag_count; |
| 1443 | |
| 1444 | (void)png_inflate_read(png_ptr, local_buffer, |
| 1445 | (sizeof local_buffer), &length, |
| 1446 | profile + (sizeof profile_header), &size, 0); |
| 1447 | |
| 1448 | /* Still expect a a buffer error because we expect |
| 1449 | * there to be some tag data! |
| 1450 | */ |
| 1451 | if (size == 0) |
| 1452 | { |
| 1453 | if (png_icc_check_tag_table(png_ptr, |
| 1454 | &png_ptr->colorspace, keyword, profile_length, |
| 1455 | profile)) |
| 1456 | { |
| 1457 | /* The profile has been validated for basic |
| 1458 | * security issues, so read the whole thing in. |
| 1459 | */ |
| 1460 | size = profile_length - (sizeof profile_header) |
| 1461 | - 12 * tag_count; |
| 1462 | |
| 1463 | (void)png_inflate_read(png_ptr, local_buffer, |
| 1464 | (sizeof local_buffer), &length, |
| 1465 | profile + (sizeof profile_header) + |
| 1466 | 12 * tag_count, &size, 1/*finish*/); |
| 1467 | |
| 1468 | if (length > 0 && !(png_ptr->flags & |
| 1469 | PNG_FLAG_BENIGN_ERRORS_WARN)) |
| 1470 | errmsg = "extra compressed data"; |
| 1471 | |
| 1472 | /* But otherwise allow extra data: */ |
| 1473 | else if (size == 0) |
| 1474 | { |
| 1475 | if (length > 0) |
| 1476 | { |
| 1477 | /* This can be handled completely, so |
| 1478 | * keep going. |
| 1479 | */ |
| 1480 | png_chunk_warning(png_ptr, |
| 1481 | "extra compressed data"); |
| 1482 | } |
| 1483 | |
| 1484 | png_crc_finish(png_ptr, length); |
| 1485 | finished = 1; |
| 1486 | |
| 1487 | # ifdef PNG_sRGB_SUPPORTED |
| 1488 | /* Check for a match against sRGB */ |
| 1489 | png_icc_set_sRGB(png_ptr, |
| 1490 | &png_ptr->colorspace, profile, |
| 1491 | png_ptr->zstream.adler); |
| 1492 | # endif |
| 1493 | |
| 1494 | /* Steal the profile for info_ptr. */ |
| 1495 | if (info_ptr != NULL) |
| 1496 | { |
| 1497 | png_free_data(png_ptr, info_ptr, |
| 1498 | PNG_FREE_ICCP, 0); |
| 1499 | |
| 1500 | info_ptr->iccp_name = png_voidcast(char*, |
| 1501 | png_malloc_base(png_ptr, |
| 1502 | keyword_length+1)); |
| 1503 | if (info_ptr->iccp_name != NULL) |
| 1504 | { |
| 1505 | memcpy(info_ptr->iccp_name, keyword, |
| 1506 | keyword_length+1); |
| 1507 | info_ptr->iccp_proflen = |
| 1508 | profile_length; |
| 1509 | info_ptr->iccp_profile = profile; |
| 1510 | png_ptr->read_buffer = NULL; /*steal*/ |
| 1511 | info_ptr->free_me |= PNG_FREE_ICCP; |
| 1512 | info_ptr->valid |= PNG_INFO_iCCP; |
| 1513 | } |
| 1514 | |
| 1515 | else |
| 1516 | { |
| 1517 | png_ptr->colorspace.flags |= |
| 1518 | PNG_COLORSPACE_INVALID; |
| 1519 | errmsg = "out of memory"; |
| 1520 | } |
| 1521 | } |
| 1522 | |
| 1523 | /* else the profile remains in the read |
| 1524 | * buffer which gets reused for subsequent |
| 1525 | * chunks. |
| 1526 | */ |
| 1527 | |
| 1528 | if (info_ptr != NULL) |
| 1529 | png_colorspace_sync(png_ptr, info_ptr); |
| 1530 | |
| 1531 | if (errmsg == NULL) |
| 1532 | { |
| 1533 | png_ptr->zowner = 0; |
| 1534 | return; |
| 1535 | } |
| 1536 | } |
| 1537 | |
| 1538 | else if (size > 0) |
| 1539 | errmsg = "truncated"; |
| 1540 | |
| 1541 | else |
| 1542 | errmsg = png_ptr->zstream.msg; |
| 1543 | } |
| 1544 | |
| 1545 | /* else png_icc_check_tag_table output an error */ |
| 1546 | } |
| 1547 | |
| 1548 | else /* profile truncated */ |
| 1549 | errmsg = png_ptr->zstream.msg; |
| 1550 | } |
| 1551 | |
| 1552 | else |
| 1553 | errmsg = "out of memory"; |
| 1554 | } |
| 1555 | |
| 1556 | /* else png_icc_check_header output an error */ |
| 1557 | } |
| 1558 | |
| 1559 | /* else png_icc_check_length output an error */ |
| 1560 | } |
| 1561 | |
| 1562 | else /* profile truncated */ |
| 1563 | errmsg = png_ptr->zstream.msg; |
| 1564 | |
| 1565 | /* Release the stream */ |
| 1566 | png_ptr->zowner = 0; |
| 1567 | } |
| 1568 | |
| 1569 | else /* png_inflate_claim failed */ |
| 1570 | errmsg = png_ptr->zstream.msg; |
| 1571 | } |
| 1572 | |
| 1573 | else |
| 1574 | errmsg = "bad compression method"; /* or missing */ |
| 1575 | } |
| 1576 | |
| 1577 | else |
| 1578 | errmsg = "bad keyword"; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1579 | } |
| 1580 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1581 | else |
| 1582 | errmsg = "too many profiles"; |
| 1583 | |
| 1584 | /* Failure: the reason is in 'errmsg' */ |
| 1585 | if (!finished) |
| 1586 | png_crc_finish(png_ptr, length); |
| 1587 | |
| 1588 | png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID; |
| 1589 | png_colorspace_sync(png_ptr, info_ptr); |
| 1590 | if (errmsg != NULL) /* else already output */ |
| 1591 | png_chunk_benign_error(png_ptr, errmsg); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1592 | } |
| 1593 | #endif /* PNG_READ_iCCP_SUPPORTED */ |
| 1594 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1595 | #ifdef PNG_READ_sPLT_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1596 | void /* PRIVATE */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1597 | png_handle_sPLT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1598 | /* Note: this does not properly handle chunks that are > 64K under DOS */ |
| 1599 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1600 | png_bytep entry_start, buffer; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1601 | png_sPLT_t new_palette; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1602 | png_sPLT_entryp pp; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1603 | png_uint_32 data_length; |
| 1604 | int entry_size, i; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1605 | png_uint_32 skip = 0; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1606 | png_uint_32 dl; |
| 1607 | png_size_t max_dl; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1608 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1609 | png_debug(1, "in png_handle_sPLT"); |
| 1610 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1611 | #ifdef PNG_USER_LIMITS_SUPPORTED |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1612 | if (png_ptr->user_chunk_cache_max != 0) |
| 1613 | { |
| 1614 | if (png_ptr->user_chunk_cache_max == 1) |
| 1615 | { |
| 1616 | png_crc_finish(png_ptr, length); |
| 1617 | return; |
| 1618 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1619 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1620 | if (--png_ptr->user_chunk_cache_max == 1) |
| 1621 | { |
| 1622 | png_warning(png_ptr, "No space in chunk cache for sPLT"); |
| 1623 | png_crc_finish(png_ptr, length); |
| 1624 | return; |
| 1625 | } |
| 1626 | } |
| 1627 | #endif |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1628 | |
| 1629 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1630 | png_chunk_error(png_ptr, "missing IHDR"); |
| 1631 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1632 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 1633 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1634 | png_crc_finish(png_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1635 | png_chunk_benign_error(png_ptr, "out of place"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1636 | return; |
| 1637 | } |
| 1638 | |
| 1639 | #ifdef PNG_MAX_MALLOC_64K |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1640 | if (length > 65535U) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1641 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1642 | png_crc_finish(png_ptr, length); |
| 1643 | png_chunk_benign_error(png_ptr, "too large to fit in memory"); |
| 1644 | return; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1645 | } |
| 1646 | #endif |
| 1647 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1648 | buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/); |
| 1649 | if (buffer == NULL) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1650 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1651 | png_crc_finish(png_ptr, length); |
| 1652 | png_chunk_benign_error(png_ptr, "out of memory"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1653 | return; |
| 1654 | } |
| 1655 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1656 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1657 | /* WARNING: this may break if size_t is less than 32 bits; it is assumed |
| 1658 | * that the PNG_MAX_MALLOC_64K test is enabled in this case, but this is a |
| 1659 | * potential breakage point if the types in pngconf.h aren't exactly right. |
| 1660 | */ |
| 1661 | png_crc_read(png_ptr, buffer, length); |
| 1662 | |
| 1663 | if (png_crc_finish(png_ptr, skip)) |
| 1664 | return; |
| 1665 | |
| 1666 | buffer[length] = 0; |
| 1667 | |
| 1668 | for (entry_start = buffer; *entry_start; entry_start++) |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 1669 | /* Empty loop to find end of name */ ; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1670 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1671 | ++entry_start; |
| 1672 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 1673 | /* A sample depth should follow the separator, and we should be on it */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1674 | if (entry_start > buffer + length - 2) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1675 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1676 | png_warning(png_ptr, "malformed sPLT chunk"); |
| 1677 | return; |
| 1678 | } |
| 1679 | |
| 1680 | new_palette.depth = *entry_start++; |
| 1681 | entry_size = (new_palette.depth == 8 ? 6 : 10); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1682 | /* This must fit in a png_uint_32 because it is derived from the original |
| 1683 | * chunk data length. |
| 1684 | */ |
| 1685 | data_length = length - (png_uint_32)(entry_start - buffer); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1686 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 1687 | /* Integrity-check the data length */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1688 | if (data_length % entry_size) |
| 1689 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1690 | png_warning(png_ptr, "sPLT chunk has bad length"); |
| 1691 | return; |
| 1692 | } |
| 1693 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1694 | dl = (png_int_32)(data_length / entry_size); |
| 1695 | max_dl = PNG_SIZE_MAX / (sizeof (png_sPLT_entry)); |
| 1696 | |
| 1697 | if (dl > max_dl) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1698 | { |
| 1699 | png_warning(png_ptr, "sPLT chunk too long"); |
| 1700 | return; |
| 1701 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1702 | |
| 1703 | new_palette.nentries = (png_int_32)(data_length / entry_size); |
| 1704 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1705 | new_palette.entries = (png_sPLT_entryp)png_malloc_warn( |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1706 | png_ptr, new_palette.nentries * (sizeof (png_sPLT_entry))); |
| 1707 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1708 | if (new_palette.entries == NULL) |
| 1709 | { |
| 1710 | png_warning(png_ptr, "sPLT chunk requires too much memory"); |
| 1711 | return; |
| 1712 | } |
| 1713 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1714 | #ifdef PNG_POINTER_INDEXING_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1715 | for (i = 0; i < new_palette.nentries; i++) |
| 1716 | { |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1717 | pp = new_palette.entries + i; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1718 | |
| 1719 | if (new_palette.depth == 8) |
| 1720 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1721 | pp->red = *entry_start++; |
| 1722 | pp->green = *entry_start++; |
| 1723 | pp->blue = *entry_start++; |
| 1724 | pp->alpha = *entry_start++; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1725 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1726 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1727 | else |
| 1728 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1729 | pp->red = png_get_uint_16(entry_start); entry_start += 2; |
| 1730 | pp->green = png_get_uint_16(entry_start); entry_start += 2; |
| 1731 | pp->blue = png_get_uint_16(entry_start); entry_start += 2; |
| 1732 | pp->alpha = png_get_uint_16(entry_start); entry_start += 2; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1733 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1734 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1735 | pp->frequency = png_get_uint_16(entry_start); entry_start += 2; |
| 1736 | } |
| 1737 | #else |
| 1738 | pp = new_palette.entries; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1739 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1740 | for (i = 0; i < new_palette.nentries; i++) |
| 1741 | { |
| 1742 | |
| 1743 | if (new_palette.depth == 8) |
| 1744 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1745 | pp[i].red = *entry_start++; |
| 1746 | pp[i].green = *entry_start++; |
| 1747 | pp[i].blue = *entry_start++; |
| 1748 | pp[i].alpha = *entry_start++; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1749 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1750 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1751 | else |
| 1752 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1753 | pp[i].red = png_get_uint_16(entry_start); entry_start += 2; |
| 1754 | pp[i].green = png_get_uint_16(entry_start); entry_start += 2; |
| 1755 | pp[i].blue = png_get_uint_16(entry_start); entry_start += 2; |
| 1756 | pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1757 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1758 | |
| 1759 | pp[i].frequency = png_get_uint_16(entry_start); entry_start += 2; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1760 | } |
| 1761 | #endif |
| 1762 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 1763 | /* Discard all chunk data except the name and stash that */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1764 | new_palette.name = (png_charp)buffer; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1765 | |
| 1766 | png_set_sPLT(png_ptr, info_ptr, &new_palette, 1); |
| 1767 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1768 | png_free(png_ptr, new_palette.entries); |
| 1769 | } |
| 1770 | #endif /* PNG_READ_sPLT_SUPPORTED */ |
| 1771 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1772 | #ifdef PNG_READ_tRNS_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1773 | void /* PRIVATE */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1774 | png_handle_tRNS(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1775 | { |
| 1776 | png_byte readbuf[PNG_MAX_PALETTE_LENGTH]; |
| 1777 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1778 | png_debug(1, "in png_handle_tRNS"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1779 | |
| 1780 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1781 | png_chunk_error(png_ptr, "missing IHDR"); |
| 1782 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1783 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 1784 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1785 | png_crc_finish(png_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1786 | png_chunk_benign_error(png_ptr, "out of place"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1787 | return; |
| 1788 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1789 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1790 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS)) |
| 1791 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1792 | png_crc_finish(png_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1793 | png_chunk_benign_error(png_ptr, "duplicate"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1794 | return; |
| 1795 | } |
| 1796 | |
| 1797 | if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY) |
| 1798 | { |
| 1799 | png_byte buf[2]; |
| 1800 | |
| 1801 | if (length != 2) |
| 1802 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1803 | png_crc_finish(png_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1804 | png_chunk_benign_error(png_ptr, "invalid"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1805 | return; |
| 1806 | } |
| 1807 | |
| 1808 | png_crc_read(png_ptr, buf, 2); |
| 1809 | png_ptr->num_trans = 1; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1810 | png_ptr->trans_color.gray = png_get_uint_16(buf); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1811 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1812 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1813 | else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB) |
| 1814 | { |
| 1815 | png_byte buf[6]; |
| 1816 | |
| 1817 | if (length != 6) |
| 1818 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1819 | png_crc_finish(png_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1820 | png_chunk_benign_error(png_ptr, "invalid"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1821 | return; |
| 1822 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1823 | |
| 1824 | png_crc_read(png_ptr, buf, length); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1825 | png_ptr->num_trans = 1; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1826 | png_ptr->trans_color.red = png_get_uint_16(buf); |
| 1827 | png_ptr->trans_color.green = png_get_uint_16(buf + 2); |
| 1828 | png_ptr->trans_color.blue = png_get_uint_16(buf + 4); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1829 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1830 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1831 | else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 1832 | { |
| 1833 | if (!(png_ptr->mode & PNG_HAVE_PLTE)) |
| 1834 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1835 | /* TODO: is this actually an error in the ISO spec? */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1836 | png_crc_finish(png_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1837 | png_chunk_benign_error(png_ptr, "out of place"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1838 | return; |
| 1839 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1840 | |
| 1841 | if (length > png_ptr->num_palette || length > PNG_MAX_PALETTE_LENGTH || |
| 1842 | length == 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1843 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1844 | png_crc_finish(png_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1845 | png_chunk_benign_error(png_ptr, "invalid"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1846 | return; |
| 1847 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1848 | |
| 1849 | png_crc_read(png_ptr, readbuf, length); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1850 | png_ptr->num_trans = (png_uint_16)length; |
| 1851 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1852 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1853 | else |
| 1854 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1855 | png_crc_finish(png_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1856 | png_chunk_benign_error(png_ptr, "invalid with alpha channel"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1857 | return; |
| 1858 | } |
| 1859 | |
| 1860 | if (png_crc_finish(png_ptr, 0)) |
| 1861 | { |
| 1862 | png_ptr->num_trans = 0; |
| 1863 | return; |
| 1864 | } |
| 1865 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1866 | /* TODO: this is a horrible side effect in the palette case because the |
| 1867 | * png_struct ends up with a pointer to the tRNS buffer owned by the |
| 1868 | * png_info. Fix this. |
| 1869 | */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1870 | png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans, |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1871 | &(png_ptr->trans_color)); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1872 | } |
| 1873 | #endif |
| 1874 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1875 | #ifdef PNG_READ_bKGD_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1876 | void /* PRIVATE */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1877 | png_handle_bKGD(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1878 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1879 | unsigned int truelen; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1880 | png_byte buf[6]; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1881 | png_color_16 background; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1882 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1883 | png_debug(1, "in png_handle_bKGD"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1884 | |
| 1885 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1886 | png_chunk_error(png_ptr, "missing IHDR"); |
| 1887 | |
| 1888 | else if ((png_ptr->mode & PNG_HAVE_IDAT) || |
| 1889 | (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && |
| 1890 | !(png_ptr->mode & PNG_HAVE_PLTE))) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1891 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1892 | png_crc_finish(png_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1893 | png_chunk_benign_error(png_ptr, "out of place"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1894 | return; |
| 1895 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1896 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1897 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD)) |
| 1898 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1899 | png_crc_finish(png_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1900 | png_chunk_benign_error(png_ptr, "duplicate"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1901 | return; |
| 1902 | } |
| 1903 | |
| 1904 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 1905 | truelen = 1; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1906 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1907 | else if (png_ptr->color_type & PNG_COLOR_MASK_COLOR) |
| 1908 | truelen = 6; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1909 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1910 | else |
| 1911 | truelen = 2; |
| 1912 | |
| 1913 | if (length != truelen) |
| 1914 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1915 | png_crc_finish(png_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1916 | png_chunk_benign_error(png_ptr, "invalid"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1917 | return; |
| 1918 | } |
| 1919 | |
| 1920 | png_crc_read(png_ptr, buf, truelen); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1921 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1922 | if (png_crc_finish(png_ptr, 0)) |
| 1923 | return; |
| 1924 | |
| 1925 | /* We convert the index value into RGB components so that we can allow |
| 1926 | * arbitrary RGB values for background when we have transparency, and |
| 1927 | * so it is easy to determine the RGB values of the background color |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1928 | * from the info_ptr struct. |
| 1929 | */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1930 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 1931 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1932 | background.index = buf[0]; |
| 1933 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1934 | if (info_ptr && info_ptr->num_palette) |
| 1935 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1936 | if (buf[0] >= info_ptr->num_palette) |
| 1937 | { |
| 1938 | png_chunk_benign_error(png_ptr, "invalid index"); |
| 1939 | return; |
| 1940 | } |
| 1941 | |
| 1942 | background.red = (png_uint_16)png_ptr->palette[buf[0]].red; |
| 1943 | background.green = (png_uint_16)png_ptr->palette[buf[0]].green; |
| 1944 | background.blue = (png_uint_16)png_ptr->palette[buf[0]].blue; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1945 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1946 | |
| 1947 | else |
| 1948 | background.red = background.green = background.blue = 0; |
| 1949 | |
| 1950 | background.gray = 0; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1951 | } |
| 1952 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1953 | else if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) /* GRAY */ |
| 1954 | { |
| 1955 | background.index = 0; |
| 1956 | background.red = |
| 1957 | background.green = |
| 1958 | background.blue = |
| 1959 | background.gray = png_get_uint_16(buf); |
| 1960 | } |
| 1961 | |
| 1962 | else |
| 1963 | { |
| 1964 | background.index = 0; |
| 1965 | background.red = png_get_uint_16(buf); |
| 1966 | background.green = png_get_uint_16(buf + 2); |
| 1967 | background.blue = png_get_uint_16(buf + 4); |
| 1968 | background.gray = 0; |
| 1969 | } |
| 1970 | |
| 1971 | png_set_bKGD(png_ptr, info_ptr, &background); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1972 | } |
| 1973 | #endif |
| 1974 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1975 | #ifdef PNG_READ_hIST_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1976 | void /* PRIVATE */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1977 | png_handle_hIST(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1978 | { |
| 1979 | unsigned int num, i; |
| 1980 | png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH]; |
| 1981 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1982 | png_debug(1, "in png_handle_hIST"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1983 | |
| 1984 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1985 | png_chunk_error(png_ptr, "missing IHDR"); |
| 1986 | |
| 1987 | else if ((png_ptr->mode & PNG_HAVE_IDAT) || !(png_ptr->mode & PNG_HAVE_PLTE)) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1988 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1989 | png_crc_finish(png_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1990 | png_chunk_benign_error(png_ptr, "out of place"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1991 | return; |
| 1992 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1993 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1994 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST)) |
| 1995 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1996 | png_crc_finish(png_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1997 | png_chunk_benign_error(png_ptr, "duplicate"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1998 | return; |
| 1999 | } |
| 2000 | |
| 2001 | num = length / 2 ; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2002 | |
| 2003 | if (num != png_ptr->num_palette || num > PNG_MAX_PALETTE_LENGTH) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2004 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2005 | png_crc_finish(png_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2006 | png_chunk_benign_error(png_ptr, "invalid"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2007 | return; |
| 2008 | } |
| 2009 | |
| 2010 | for (i = 0; i < num; i++) |
| 2011 | { |
| 2012 | png_byte buf[2]; |
| 2013 | |
| 2014 | png_crc_read(png_ptr, buf, 2); |
| 2015 | readbuf[i] = png_get_uint_16(buf); |
| 2016 | } |
| 2017 | |
| 2018 | if (png_crc_finish(png_ptr, 0)) |
| 2019 | return; |
| 2020 | |
| 2021 | png_set_hIST(png_ptr, info_ptr, readbuf); |
| 2022 | } |
| 2023 | #endif |
| 2024 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2025 | #ifdef PNG_READ_pHYs_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2026 | void /* PRIVATE */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2027 | png_handle_pHYs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2028 | { |
| 2029 | png_byte buf[9]; |
| 2030 | png_uint_32 res_x, res_y; |
| 2031 | int unit_type; |
| 2032 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2033 | png_debug(1, "in png_handle_pHYs"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2034 | |
| 2035 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2036 | png_chunk_error(png_ptr, "missing IHDR"); |
| 2037 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2038 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 2039 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2040 | png_crc_finish(png_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2041 | png_chunk_benign_error(png_ptr, "out of place"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2042 | return; |
| 2043 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2044 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2045 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs)) |
| 2046 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2047 | png_crc_finish(png_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2048 | png_chunk_benign_error(png_ptr, "duplicate"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2049 | return; |
| 2050 | } |
| 2051 | |
| 2052 | if (length != 9) |
| 2053 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2054 | png_crc_finish(png_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2055 | png_chunk_benign_error(png_ptr, "invalid"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2056 | return; |
| 2057 | } |
| 2058 | |
| 2059 | png_crc_read(png_ptr, buf, 9); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2060 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2061 | if (png_crc_finish(png_ptr, 0)) |
| 2062 | return; |
| 2063 | |
| 2064 | res_x = png_get_uint_32(buf); |
| 2065 | res_y = png_get_uint_32(buf + 4); |
| 2066 | unit_type = buf[8]; |
| 2067 | png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type); |
| 2068 | } |
| 2069 | #endif |
| 2070 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2071 | #ifdef PNG_READ_oFFs_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2072 | void /* PRIVATE */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2073 | png_handle_oFFs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2074 | { |
| 2075 | png_byte buf[9]; |
| 2076 | png_int_32 offset_x, offset_y; |
| 2077 | int unit_type; |
| 2078 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2079 | png_debug(1, "in png_handle_oFFs"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2080 | |
| 2081 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2082 | png_chunk_error(png_ptr, "missing IHDR"); |
| 2083 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2084 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 2085 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2086 | png_crc_finish(png_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2087 | png_chunk_benign_error(png_ptr, "out of place"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2088 | return; |
| 2089 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2090 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2091 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs)) |
| 2092 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2093 | png_crc_finish(png_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2094 | png_chunk_benign_error(png_ptr, "duplicate"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2095 | return; |
| 2096 | } |
| 2097 | |
| 2098 | if (length != 9) |
| 2099 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2100 | png_crc_finish(png_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2101 | png_chunk_benign_error(png_ptr, "invalid"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2102 | return; |
| 2103 | } |
| 2104 | |
| 2105 | png_crc_read(png_ptr, buf, 9); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2106 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2107 | if (png_crc_finish(png_ptr, 0)) |
| 2108 | return; |
| 2109 | |
| 2110 | offset_x = png_get_int_32(buf); |
| 2111 | offset_y = png_get_int_32(buf + 4); |
| 2112 | unit_type = buf[8]; |
| 2113 | png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type); |
| 2114 | } |
| 2115 | #endif |
| 2116 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2117 | #ifdef PNG_READ_pCAL_SUPPORTED |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 2118 | /* Read the pCAL chunk (described in the PNG Extensions document) */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2119 | void /* PRIVATE */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2120 | png_handle_pCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2121 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2122 | png_int_32 X0, X1; |
| 2123 | png_byte type, nparams; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2124 | png_bytep buffer, buf, units, endptr; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2125 | png_charpp params; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2126 | int i; |
| 2127 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2128 | png_debug(1, "in png_handle_pCAL"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2129 | |
| 2130 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2131 | png_chunk_error(png_ptr, "missing IHDR"); |
| 2132 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2133 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 2134 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2135 | png_crc_finish(png_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2136 | png_chunk_benign_error(png_ptr, "out of place"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2137 | return; |
| 2138 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2139 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2140 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL)) |
| 2141 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2142 | png_crc_finish(png_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2143 | png_chunk_benign_error(png_ptr, "duplicate"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2144 | return; |
| 2145 | } |
| 2146 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2147 | png_debug1(2, "Allocating and reading pCAL chunk data (%u bytes)", |
| 2148 | length + 1); |
| 2149 | |
| 2150 | buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/); |
| 2151 | |
| 2152 | if (buffer == NULL) |
| 2153 | { |
| 2154 | png_crc_finish(png_ptr, length); |
| 2155 | png_chunk_benign_error(png_ptr, "out of memory"); |
| 2156 | return; |
| 2157 | } |
| 2158 | |
| 2159 | png_crc_read(png_ptr, buffer, length); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2160 | |
| 2161 | if (png_crc_finish(png_ptr, 0)) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2162 | return; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2163 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2164 | buffer[length] = 0; /* Null terminate the last string */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2165 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2166 | png_debug(3, "Finding end of pCAL purpose string"); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2167 | for (buf = buffer; *buf; buf++) |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 2168 | /* Empty loop */ ; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2169 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2170 | endptr = buffer + length; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2171 | |
| 2172 | /* We need to have at least 12 bytes after the purpose string |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2173 | * in order to get the parameter information. |
| 2174 | */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2175 | if (endptr <= buf + 12) |
| 2176 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2177 | png_chunk_benign_error(png_ptr, "invalid"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2178 | return; |
| 2179 | } |
| 2180 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2181 | png_debug(3, "Reading pCAL X0, X1, type, nparams, and units"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2182 | X0 = png_get_int_32((png_bytep)buf+1); |
| 2183 | X1 = png_get_int_32((png_bytep)buf+5); |
| 2184 | type = buf[9]; |
| 2185 | nparams = buf[10]; |
| 2186 | units = buf + 11; |
| 2187 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2188 | png_debug(3, "Checking pCAL equation type and number of parameters"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2189 | /* Check that we have the right number of parameters for known |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2190 | * equation types. |
| 2191 | */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2192 | if ((type == PNG_EQUATION_LINEAR && nparams != 2) || |
| 2193 | (type == PNG_EQUATION_BASE_E && nparams != 3) || |
| 2194 | (type == PNG_EQUATION_ARBITRARY && nparams != 3) || |
| 2195 | (type == PNG_EQUATION_HYPERBOLIC && nparams != 4)) |
| 2196 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2197 | png_chunk_benign_error(png_ptr, "invalid parameter count"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2198 | return; |
| 2199 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2200 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2201 | else if (type >= PNG_EQUATION_LAST) |
| 2202 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2203 | png_chunk_benign_error(png_ptr, "unrecognized equation type"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2204 | } |
| 2205 | |
| 2206 | for (buf = units; *buf; buf++) |
| 2207 | /* Empty loop to move past the units string. */ ; |
| 2208 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2209 | png_debug(3, "Allocating pCAL parameters array"); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2210 | |
| 2211 | params = png_voidcast(png_charpp, png_malloc_warn(png_ptr, |
| 2212 | nparams * (sizeof (png_charp)))); |
| 2213 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2214 | if (params == NULL) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2215 | { |
| 2216 | png_chunk_benign_error(png_ptr, "out of memory"); |
| 2217 | return; |
| 2218 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2219 | |
| 2220 | /* Get pointers to the start of each parameter string. */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2221 | for (i = 0; i < nparams; i++) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2222 | { |
| 2223 | buf++; /* Skip the null string terminator from previous parameter. */ |
| 2224 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2225 | png_debug1(3, "Reading pCAL parameter %d", i); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2226 | |
| 2227 | for (params[i] = (png_charp)buf; buf <= endptr && *buf != 0; buf++) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2228 | /* Empty loop to move past each parameter string */ ; |
| 2229 | |
| 2230 | /* Make sure we haven't run out of data yet */ |
| 2231 | if (buf > endptr) |
| 2232 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2233 | png_free(png_ptr, params); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2234 | png_chunk_benign_error(png_ptr, "invalid data"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2235 | return; |
| 2236 | } |
| 2237 | } |
| 2238 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2239 | png_set_pCAL(png_ptr, info_ptr, (png_charp)buffer, X0, X1, type, nparams, |
| 2240 | (png_charp)units, params); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2241 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2242 | png_free(png_ptr, params); |
| 2243 | } |
| 2244 | #endif |
| 2245 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2246 | #ifdef PNG_READ_sCAL_SUPPORTED |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 2247 | /* Read the sCAL chunk */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2248 | void /* PRIVATE */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2249 | png_handle_sCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2250 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2251 | png_bytep buffer; |
| 2252 | png_size_t i; |
| 2253 | int state; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2254 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2255 | png_debug(1, "in png_handle_sCAL"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2256 | |
| 2257 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2258 | png_chunk_error(png_ptr, "missing IHDR"); |
| 2259 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2260 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 2261 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2262 | png_crc_finish(png_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2263 | png_chunk_benign_error(png_ptr, "out of place"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2264 | return; |
| 2265 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2266 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2267 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL)) |
| 2268 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2269 | png_crc_finish(png_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2270 | png_chunk_benign_error(png_ptr, "duplicate"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2271 | return; |
| 2272 | } |
| 2273 | |
Eric Vannier | 66dce0d | 2011-07-20 17:03:29 -0700 | [diff] [blame] | 2274 | /* Need unit type, width, \0, height: minimum 4 bytes */ |
| 2275 | else if (length < 4) |
| 2276 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2277 | png_crc_finish(png_ptr, length); |
| 2278 | png_chunk_benign_error(png_ptr, "invalid"); |
| 2279 | return; |
| 2280 | } |
| 2281 | |
| 2282 | png_debug1(2, "Allocating and reading sCAL chunk data (%u bytes)", |
| 2283 | length + 1); |
| 2284 | |
| 2285 | buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/); |
| 2286 | |
| 2287 | if (buffer == NULL) |
| 2288 | { |
| 2289 | png_chunk_benign_error(png_ptr, "out of memory"); |
Eric Vannier | 66dce0d | 2011-07-20 17:03:29 -0700 | [diff] [blame] | 2290 | png_crc_finish(png_ptr, length); |
| 2291 | return; |
| 2292 | } |
| 2293 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2294 | png_crc_read(png_ptr, buffer, length); |
| 2295 | buffer[length] = 0; /* Null terminate the last string */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2296 | |
| 2297 | if (png_crc_finish(png_ptr, 0)) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2298 | return; |
| 2299 | |
| 2300 | /* Validate the unit. */ |
| 2301 | if (buffer[0] != 1 && buffer[0] != 2) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2302 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2303 | png_chunk_benign_error(png_ptr, "invalid unit"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2304 | return; |
| 2305 | } |
| 2306 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2307 | /* Validate the ASCII numbers, need two ASCII numbers separated by |
| 2308 | * a '\0' and they need to fit exactly in the chunk data. |
| 2309 | */ |
| 2310 | i = 1; |
| 2311 | state = 0; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2312 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2313 | if (!png_check_fp_number((png_const_charp)buffer, length, &state, &i) || |
| 2314 | i >= length || buffer[i++] != 0) |
| 2315 | png_chunk_benign_error(png_ptr, "bad width format"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2316 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2317 | else if (!PNG_FP_IS_POSITIVE(state)) |
| 2318 | png_chunk_benign_error(png_ptr, "non-positive width"); |
| 2319 | |
| 2320 | else |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2321 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2322 | png_size_t heighti = i; |
| 2323 | |
| 2324 | state = 0; |
| 2325 | if (!png_check_fp_number((png_const_charp)buffer, length, &state, &i) || |
| 2326 | i != length) |
| 2327 | png_chunk_benign_error(png_ptr, "bad height format"); |
| 2328 | |
| 2329 | else if (!PNG_FP_IS_POSITIVE(state)) |
| 2330 | png_chunk_benign_error(png_ptr, "non-positive height"); |
| 2331 | |
| 2332 | else |
| 2333 | /* This is the (only) success case. */ |
| 2334 | png_set_sCAL_s(png_ptr, info_ptr, buffer[0], |
| 2335 | (png_charp)buffer+1, (png_charp)buffer+heighti); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2336 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2337 | } |
| 2338 | #endif |
| 2339 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2340 | #ifdef PNG_READ_tIME_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2341 | void /* PRIVATE */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2342 | png_handle_tIME(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2343 | { |
| 2344 | png_byte buf[7]; |
| 2345 | png_time mod_time; |
| 2346 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2347 | png_debug(1, "in png_handle_tIME"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2348 | |
| 2349 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2350 | png_chunk_error(png_ptr, "missing IHDR"); |
| 2351 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2352 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME)) |
| 2353 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2354 | png_crc_finish(png_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2355 | png_chunk_benign_error(png_ptr, "duplicate"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2356 | return; |
| 2357 | } |
| 2358 | |
| 2359 | if (png_ptr->mode & PNG_HAVE_IDAT) |
| 2360 | png_ptr->mode |= PNG_AFTER_IDAT; |
| 2361 | |
| 2362 | if (length != 7) |
| 2363 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2364 | png_crc_finish(png_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2365 | png_chunk_benign_error(png_ptr, "invalid"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2366 | return; |
| 2367 | } |
| 2368 | |
| 2369 | png_crc_read(png_ptr, buf, 7); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2370 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2371 | if (png_crc_finish(png_ptr, 0)) |
| 2372 | return; |
| 2373 | |
| 2374 | mod_time.second = buf[6]; |
| 2375 | mod_time.minute = buf[5]; |
| 2376 | mod_time.hour = buf[4]; |
| 2377 | mod_time.day = buf[3]; |
| 2378 | mod_time.month = buf[2]; |
| 2379 | mod_time.year = png_get_uint_16(buf); |
| 2380 | |
| 2381 | png_set_tIME(png_ptr, info_ptr, &mod_time); |
| 2382 | } |
| 2383 | #endif |
| 2384 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2385 | #ifdef PNG_READ_tEXt_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2386 | /* Note: this does not properly handle chunks that are > 64K under DOS */ |
| 2387 | void /* PRIVATE */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2388 | png_handle_tEXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2389 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2390 | png_text text_info; |
| 2391 | png_bytep buffer; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2392 | png_charp key; |
| 2393 | png_charp text; |
| 2394 | png_uint_32 skip = 0; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2395 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2396 | png_debug(1, "in png_handle_tEXt"); |
| 2397 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2398 | #ifdef PNG_USER_LIMITS_SUPPORTED |
| 2399 | if (png_ptr->user_chunk_cache_max != 0) |
| 2400 | { |
| 2401 | if (png_ptr->user_chunk_cache_max == 1) |
| 2402 | { |
| 2403 | png_crc_finish(png_ptr, length); |
| 2404 | return; |
| 2405 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2406 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2407 | if (--png_ptr->user_chunk_cache_max == 1) |
| 2408 | { |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2409 | png_crc_finish(png_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2410 | png_chunk_benign_error(png_ptr, "no space in chunk cache"); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2411 | return; |
| 2412 | } |
| 2413 | } |
| 2414 | #endif |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2415 | |
| 2416 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2417 | png_chunk_error(png_ptr, "missing IHDR"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2418 | |
| 2419 | if (png_ptr->mode & PNG_HAVE_IDAT) |
| 2420 | png_ptr->mode |= PNG_AFTER_IDAT; |
| 2421 | |
| 2422 | #ifdef PNG_MAX_MALLOC_64K |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2423 | if (length > 65535U) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2424 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2425 | png_crc_finish(png_ptr, length); |
| 2426 | png_chunk_benign_error(png_ptr, "too large to fit in memory"); |
| 2427 | return; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2428 | } |
| 2429 | #endif |
| 2430 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2431 | buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/); |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2432 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2433 | if (buffer == NULL) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2434 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2435 | png_chunk_benign_error(png_ptr, "out of memory"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2436 | return; |
| 2437 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2438 | |
| 2439 | png_crc_read(png_ptr, buffer, length); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2440 | |
| 2441 | if (png_crc_finish(png_ptr, skip)) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2442 | return; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2443 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2444 | key = (png_charp)buffer; |
| 2445 | key[length] = 0; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2446 | |
| 2447 | for (text = key; *text; text++) |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 2448 | /* Empty loop to find end of key */ ; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2449 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2450 | if (text != key + length) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2451 | text++; |
| 2452 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2453 | text_info.compression = PNG_TEXT_COMPRESSION_NONE; |
| 2454 | text_info.key = key; |
| 2455 | text_info.lang = NULL; |
| 2456 | text_info.lang_key = NULL; |
| 2457 | text_info.itxt_length = 0; |
| 2458 | text_info.text = text; |
| 2459 | text_info.text_length = strlen(text); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2460 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2461 | if (png_set_text_2(png_ptr, info_ptr, &text_info, 1)) |
| 2462 | png_warning(png_ptr, "Insufficient memory to process text chunk"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2463 | } |
| 2464 | #endif |
| 2465 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2466 | #ifdef PNG_READ_zTXt_SUPPORTED |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 2467 | /* Note: this does not correctly handle chunks that are > 64K under DOS */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2468 | void /* PRIVATE */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2469 | png_handle_zTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2470 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2471 | png_const_charp errmsg = NULL; |
| 2472 | png_bytep buffer; |
| 2473 | png_uint_32 keyword_length; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2474 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2475 | png_debug(1, "in png_handle_zTXt"); |
| 2476 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2477 | #ifdef PNG_USER_LIMITS_SUPPORTED |
| 2478 | if (png_ptr->user_chunk_cache_max != 0) |
| 2479 | { |
| 2480 | if (png_ptr->user_chunk_cache_max == 1) |
| 2481 | { |
| 2482 | png_crc_finish(png_ptr, length); |
| 2483 | return; |
| 2484 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2485 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2486 | if (--png_ptr->user_chunk_cache_max == 1) |
| 2487 | { |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2488 | png_crc_finish(png_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2489 | png_chunk_benign_error(png_ptr, "no space in chunk cache"); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2490 | return; |
| 2491 | } |
| 2492 | } |
| 2493 | #endif |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2494 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2495 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2496 | png_chunk_error(png_ptr, "missing IHDR"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2497 | |
| 2498 | if (png_ptr->mode & PNG_HAVE_IDAT) |
| 2499 | png_ptr->mode |= PNG_AFTER_IDAT; |
| 2500 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2501 | buffer = png_read_buffer(png_ptr, length, 2/*silent*/); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2502 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2503 | if (buffer == NULL) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2504 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2505 | png_crc_finish(png_ptr, length); |
| 2506 | png_chunk_benign_error(png_ptr, "out of memory"); |
| 2507 | return; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2508 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2509 | |
| 2510 | png_crc_read(png_ptr, buffer, length); |
| 2511 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2512 | if (png_crc_finish(png_ptr, 0)) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2513 | return; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2514 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2515 | /* TODO: also check that the keyword contents match the spec! */ |
| 2516 | for (keyword_length = 0; |
| 2517 | keyword_length < length && buffer[keyword_length] != 0; |
| 2518 | ++keyword_length) |
| 2519 | /* Empty loop to find end of name */ ; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2520 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2521 | if (keyword_length > 79 || keyword_length < 1) |
| 2522 | errmsg = "bad keyword"; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2523 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2524 | /* zTXt must have some LZ data after the keyword, although it may expand to |
| 2525 | * zero bytes; we need a '\0' at the end of the keyword, the compression type |
| 2526 | * then the LZ data: |
| 2527 | */ |
| 2528 | else if (keyword_length + 3 > length) |
| 2529 | errmsg = "truncated"; |
| 2530 | |
| 2531 | else if (buffer[keyword_length+1] != PNG_COMPRESSION_TYPE_BASE) |
| 2532 | errmsg = "unknown compression type"; |
| 2533 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2534 | else |
| 2535 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2536 | png_alloc_size_t uncompressed_length = PNG_SIZE_MAX; |
| 2537 | |
| 2538 | /* TODO: at present png_decompress_chunk imposes a single application |
| 2539 | * level memory limit, this should be split to different values for iCCP |
| 2540 | * and text chunks. |
| 2541 | */ |
| 2542 | if (png_decompress_chunk(png_ptr, length, keyword_length+2, |
| 2543 | &uncompressed_length, 1/*terminate*/) == Z_STREAM_END) |
| 2544 | { |
| 2545 | png_text text; |
| 2546 | |
| 2547 | /* It worked; png_ptr->read_buffer now looks like a tEXt chunk except |
| 2548 | * for the extra compression type byte and the fact that it isn't |
| 2549 | * necessarily '\0' terminated. |
| 2550 | */ |
| 2551 | buffer = png_ptr->read_buffer; |
| 2552 | buffer[uncompressed_length+(keyword_length+2)] = 0; |
| 2553 | |
| 2554 | text.compression = PNG_TEXT_COMPRESSION_zTXt; |
| 2555 | text.key = (png_charp)buffer; |
| 2556 | text.text = (png_charp)(buffer + keyword_length+2); |
| 2557 | text.text_length = uncompressed_length; |
| 2558 | text.itxt_length = 0; |
| 2559 | text.lang = NULL; |
| 2560 | text.lang_key = NULL; |
| 2561 | |
| 2562 | if (png_set_text_2(png_ptr, info_ptr, &text, 1)) |
| 2563 | errmsg = "insufficient memory"; |
| 2564 | } |
| 2565 | |
| 2566 | else |
| 2567 | errmsg = png_ptr->zstream.msg; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2568 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2569 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2570 | if (errmsg != NULL) |
| 2571 | png_chunk_benign_error(png_ptr, errmsg); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2572 | } |
| 2573 | #endif |
| 2574 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2575 | #ifdef PNG_READ_iTXt_SUPPORTED |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 2576 | /* Note: this does not correctly handle chunks that are > 64K under DOS */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2577 | void /* PRIVATE */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2578 | png_handle_iTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2579 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2580 | png_const_charp errmsg = NULL; |
| 2581 | png_bytep buffer; |
| 2582 | png_uint_32 prefix_length; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2583 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2584 | png_debug(1, "in png_handle_iTXt"); |
| 2585 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2586 | #ifdef PNG_USER_LIMITS_SUPPORTED |
| 2587 | if (png_ptr->user_chunk_cache_max != 0) |
| 2588 | { |
| 2589 | if (png_ptr->user_chunk_cache_max == 1) |
| 2590 | { |
| 2591 | png_crc_finish(png_ptr, length); |
| 2592 | return; |
| 2593 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2594 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2595 | if (--png_ptr->user_chunk_cache_max == 1) |
| 2596 | { |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2597 | png_crc_finish(png_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2598 | png_chunk_benign_error(png_ptr, "no space in chunk cache"); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2599 | return; |
| 2600 | } |
| 2601 | } |
| 2602 | #endif |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2603 | |
| 2604 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2605 | png_chunk_error(png_ptr, "missing IHDR"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2606 | |
| 2607 | if (png_ptr->mode & PNG_HAVE_IDAT) |
| 2608 | png_ptr->mode |= PNG_AFTER_IDAT; |
| 2609 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2610 | buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2611 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2612 | if (buffer == NULL) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2613 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2614 | png_crc_finish(png_ptr, length); |
| 2615 | png_chunk_benign_error(png_ptr, "out of memory"); |
| 2616 | return; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2617 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2618 | |
| 2619 | png_crc_read(png_ptr, buffer, length); |
| 2620 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2621 | if (png_crc_finish(png_ptr, 0)) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2622 | return; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2623 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2624 | /* First the keyword. */ |
| 2625 | for (prefix_length=0; |
| 2626 | prefix_length < length && buffer[prefix_length] != 0; |
| 2627 | ++prefix_length) |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 2628 | /* Empty loop */ ; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2629 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2630 | /* Perform a basic check on the keyword length here. */ |
| 2631 | if (prefix_length > 79 || prefix_length < 1) |
| 2632 | errmsg = "bad keyword"; |
| 2633 | |
| 2634 | /* Expect keyword, compression flag, compression type, language, translated |
| 2635 | * keyword (both may be empty but are 0 terminated) then the text, which may |
| 2636 | * be empty. |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 2637 | */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2638 | else if (prefix_length + 5 > length) |
| 2639 | errmsg = "truncated"; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2640 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2641 | else if (buffer[prefix_length+1] == 0 || |
| 2642 | (buffer[prefix_length+1] == 1 && |
| 2643 | buffer[prefix_length+2] == PNG_COMPRESSION_TYPE_BASE)) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2644 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2645 | int compressed = buffer[prefix_length+1] != 0; |
| 2646 | png_uint_32 language_offset, translated_keyword_offset; |
| 2647 | png_alloc_size_t uncompressed_length = 0; |
| 2648 | |
| 2649 | /* Now the language tag */ |
| 2650 | prefix_length += 3; |
| 2651 | language_offset = prefix_length; |
| 2652 | |
| 2653 | for (; prefix_length < length && buffer[prefix_length] != 0; |
| 2654 | ++prefix_length) |
| 2655 | /* Empty loop */ ; |
| 2656 | |
| 2657 | /* WARNING: the length may be invalid here, this is checked below. */ |
| 2658 | translated_keyword_offset = ++prefix_length; |
| 2659 | |
| 2660 | for (; prefix_length < length && buffer[prefix_length] != 0; |
| 2661 | ++prefix_length) |
| 2662 | /* Empty loop */ ; |
| 2663 | |
| 2664 | /* prefix_length should now be at the trailing '\0' of the translated |
| 2665 | * keyword, but it may already be over the end. None of this arithmetic |
| 2666 | * can overflow because chunks are at most 2^31 bytes long, but on 16-bit |
| 2667 | * systems the available allocaton may overflow. |
| 2668 | */ |
| 2669 | ++prefix_length; |
| 2670 | |
| 2671 | if (!compressed && prefix_length <= length) |
| 2672 | uncompressed_length = length - prefix_length; |
| 2673 | |
| 2674 | else if (compressed && prefix_length < length) |
| 2675 | { |
| 2676 | uncompressed_length = PNG_SIZE_MAX; |
| 2677 | |
| 2678 | /* TODO: at present png_decompress_chunk imposes a single application |
| 2679 | * level memory limit, this should be split to different values for |
| 2680 | * iCCP and text chunks. |
| 2681 | */ |
| 2682 | if (png_decompress_chunk(png_ptr, length, prefix_length, |
| 2683 | &uncompressed_length, 1/*terminate*/) == Z_STREAM_END) |
| 2684 | buffer = png_ptr->read_buffer; |
| 2685 | |
| 2686 | else |
| 2687 | errmsg = png_ptr->zstream.msg; |
| 2688 | } |
| 2689 | |
| 2690 | else |
| 2691 | errmsg = "truncated"; |
| 2692 | |
| 2693 | if (errmsg == NULL) |
| 2694 | { |
| 2695 | png_text text; |
| 2696 | |
| 2697 | buffer[uncompressed_length+prefix_length] = 0; |
| 2698 | |
| 2699 | if (compressed) |
| 2700 | text.compression = PNG_ITXT_COMPRESSION_NONE; |
| 2701 | |
| 2702 | else |
| 2703 | text.compression = PNG_ITXT_COMPRESSION_zTXt; |
| 2704 | |
| 2705 | text.key = (png_charp)buffer; |
| 2706 | text.lang = (png_charp)buffer + language_offset; |
| 2707 | text.lang_key = (png_charp)buffer + translated_keyword_offset; |
| 2708 | text.text = (png_charp)buffer + prefix_length; |
| 2709 | text.text_length = 0; |
| 2710 | text.itxt_length = uncompressed_length; |
| 2711 | |
| 2712 | if (png_set_text_2(png_ptr, info_ptr, &text, 1)) |
| 2713 | errmsg = "insufficient memory"; |
| 2714 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2715 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2716 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2717 | else |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2718 | errmsg = "bad compression info"; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2719 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2720 | if (errmsg != NULL) |
| 2721 | png_chunk_benign_error(png_ptr, errmsg); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2722 | } |
| 2723 | #endif |
| 2724 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2725 | #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED |
| 2726 | /* Utility function for png_handle_unknown; set up png_ptr::unknown_chunk */ |
| 2727 | static int |
| 2728 | png_cache_unknown_chunk(png_structrp png_ptr, png_uint_32 length) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2729 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2730 | png_alloc_size_t limit = PNG_SIZE_MAX; |
| 2731 | |
| 2732 | if (png_ptr->unknown_chunk.data != NULL) |
| 2733 | { |
| 2734 | png_free(png_ptr, png_ptr->unknown_chunk.data); |
| 2735 | png_ptr->unknown_chunk.data = NULL; |
| 2736 | } |
| 2737 | |
| 2738 | # ifdef PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED |
| 2739 | if (png_ptr->user_chunk_malloc_max > 0 && |
| 2740 | png_ptr->user_chunk_malloc_max < limit) |
| 2741 | limit = png_ptr->user_chunk_malloc_max; |
| 2742 | |
| 2743 | # elif PNG_USER_CHUNK_MALLOC_MAX > 0 |
| 2744 | if (PNG_USER_CHUNK_MALLOC_MAX < limit) |
| 2745 | limit = PNG_USER_CHUNK_MALLOC_MAX; |
| 2746 | # endif |
| 2747 | |
| 2748 | if (length <= limit) |
| 2749 | { |
| 2750 | PNG_CSTRING_FROM_CHUNK(png_ptr->unknown_chunk.name, png_ptr->chunk_name); |
| 2751 | /* The following is safe because of the PNG_SIZE_MAX init above */ |
| 2752 | png_ptr->unknown_chunk.size = (png_size_t)length/*SAFE*/; |
| 2753 | /* 'mode' is a flag array, only the bottom four bits matter here */ |
| 2754 | png_ptr->unknown_chunk.location = (png_byte)png_ptr->mode/*SAFE*/; |
| 2755 | |
| 2756 | if (length == 0) |
| 2757 | png_ptr->unknown_chunk.data = NULL; |
| 2758 | |
| 2759 | else |
| 2760 | { |
| 2761 | /* Do a 'warn' here - it is handled below. */ |
| 2762 | png_ptr->unknown_chunk.data = png_voidcast(png_bytep, |
| 2763 | png_malloc_warn(png_ptr, length)); |
| 2764 | } |
| 2765 | } |
| 2766 | |
| 2767 | if (png_ptr->unknown_chunk.data == NULL && length > 0) |
| 2768 | { |
| 2769 | /* This is benign because we clean up correctly */ |
| 2770 | png_crc_finish(png_ptr, length); |
| 2771 | png_chunk_benign_error(png_ptr, "unknown chunk exceeds memory limits"); |
| 2772 | return 0; |
| 2773 | } |
| 2774 | |
| 2775 | else |
| 2776 | { |
| 2777 | if (length > 0) |
| 2778 | png_crc_read(png_ptr, png_ptr->unknown_chunk.data, length); |
| 2779 | png_crc_finish(png_ptr, 0); |
| 2780 | return 1; |
| 2781 | } |
| 2782 | } |
| 2783 | #endif /* PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */ |
| 2784 | |
| 2785 | /* Handle an unknown, or known but disabled, chunk */ |
| 2786 | void /* PRIVATE */ |
| 2787 | png_handle_unknown(png_structrp png_ptr, png_inforp info_ptr, |
| 2788 | png_uint_32 length, int keep) |
| 2789 | { |
| 2790 | int handled = 0; /* the chunk was handled */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2791 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2792 | png_debug(1, "in png_handle_unknown"); |
| 2793 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2794 | #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2795 | /* NOTE: this code is based on the code in libpng-1.4.12 except for fixing |
| 2796 | * the bug which meant that setting a non-default behavior for a specific |
| 2797 | * chunk would be ignored (the default was always used unless a user |
| 2798 | * callback was installed). |
| 2799 | * |
| 2800 | * 'keep' is the value from the png_chunk_unknown_handling, the setting for |
| 2801 | * this specific chunk_name, if PNG_HANDLE_AS_UNKNOWN_SUPPORTED, if not it |
| 2802 | * will always be PNG_HANDLE_CHUNK_AS_DEFAULT and it needs to be set here. |
| 2803 | * This is just an optimization to avoid multiple calls to the lookup |
| 2804 | * function. |
| 2805 | */ |
| 2806 | # ifndef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
| 2807 | # ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED |
| 2808 | keep = png_chunk_unknown_handling(png_ptr, png_ptr->chunk_name); |
| 2809 | # endif |
| 2810 | # endif |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2811 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2812 | /* One of the following methods will read the chunk or skip it (at least one |
| 2813 | * of these is always defined because this is the only way to switch on |
| 2814 | * PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) |
| 2815 | */ |
| 2816 | # ifdef PNG_READ_USER_CHUNKS_SUPPORTED |
| 2817 | /* The user callback takes precedence over the chunk keep value, but the |
| 2818 | * keep value is still required to validate a save of a critical chunk. |
| 2819 | */ |
| 2820 | if (png_ptr->read_user_chunk_fn != NULL) |
| 2821 | { |
| 2822 | if (png_cache_unknown_chunk(png_ptr, length)) |
| 2823 | { |
| 2824 | /* Callback to user unknown chunk handler */ |
| 2825 | int ret = (*(png_ptr->read_user_chunk_fn))(png_ptr, |
| 2826 | &png_ptr->unknown_chunk); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2827 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2828 | /* ret is: |
| 2829 | * negative: An error occured, png_chunk_error will be called. |
| 2830 | * zero: The chunk was not handled, the chunk will be discarded |
| 2831 | * unless png_set_keep_unknown_chunks has been used to set |
| 2832 | * a 'keep' behavior for this particular chunk, in which |
| 2833 | * case that will be used. A critical chunk will cause an |
| 2834 | * error at this point unless it is to be saved. |
| 2835 | * positive: The chunk was handled, libpng will ignore/discard it. |
| 2836 | */ |
| 2837 | if (ret < 0) |
| 2838 | png_chunk_error(png_ptr, "error in user chunk"); |
| 2839 | |
| 2840 | else if (ret == 0) |
| 2841 | { |
| 2842 | /* If the keep value is 'default' or 'never' override it, but |
| 2843 | * still error out on critical chunks unless the keep value is |
| 2844 | * 'always' While this is weird it is the behavior in 1.4.12. |
| 2845 | * A possible improvement would be to obey the value set for the |
| 2846 | * chunk, but this would be an API change that would probably |
| 2847 | * damage some applications. |
| 2848 | * |
| 2849 | * The png_app_warning below catches the case that matters, where |
| 2850 | * the application has not set specific save or ignore for this |
| 2851 | * chunk or global save or ignore. |
| 2852 | */ |
| 2853 | if (keep < PNG_HANDLE_CHUNK_IF_SAFE) |
| 2854 | { |
| 2855 | # ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED |
| 2856 | if (png_ptr->unknown_default < PNG_HANDLE_CHUNK_IF_SAFE) |
| 2857 | { |
| 2858 | png_chunk_warning(png_ptr, "Saving unknown chunk:"); |
| 2859 | png_app_warning(png_ptr, |
| 2860 | "forcing save of an unhandled chunk;" |
| 2861 | " please call png_set_keep_unknown_chunks"); |
| 2862 | /* with keep = PNG_HANDLE_CHUNK_IF_SAFE */ |
| 2863 | } |
| 2864 | # endif |
| 2865 | keep = PNG_HANDLE_CHUNK_IF_SAFE; |
| 2866 | } |
| 2867 | } |
| 2868 | |
| 2869 | else /* chunk was handled */ |
| 2870 | { |
| 2871 | handled = 1; |
| 2872 | /* Critical chunks can be safely discarded at this point. */ |
| 2873 | keep = PNG_HANDLE_CHUNK_NEVER; |
| 2874 | } |
| 2875 | } |
| 2876 | |
| 2877 | else |
| 2878 | keep = PNG_HANDLE_CHUNK_NEVER; /* insufficient memory */ |
| 2879 | } |
| 2880 | |
| 2881 | else |
| 2882 | /* Use the SAVE_UNKNOWN_CHUNKS code or skip the chunk */ |
| 2883 | # endif /* PNG_READ_USER_CHUNKS_SUPPORTED */ |
| 2884 | |
| 2885 | # ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED |
| 2886 | { |
| 2887 | /* keep is currently just the per-chunk setting, if there was no |
| 2888 | * setting change it to the global default now (not that this may |
| 2889 | * still be AS_DEFAULT) then obtain the cache of the chunk if required, |
| 2890 | * if not simply skip the chunk. |
| 2891 | */ |
| 2892 | if (keep == PNG_HANDLE_CHUNK_AS_DEFAULT) |
| 2893 | keep = png_ptr->unknown_default; |
| 2894 | |
| 2895 | if (keep == PNG_HANDLE_CHUNK_ALWAYS || |
| 2896 | (keep == PNG_HANDLE_CHUNK_IF_SAFE && |
| 2897 | PNG_CHUNK_ANCILLARY(png_ptr->chunk_name))) |
| 2898 | { |
| 2899 | if (!png_cache_unknown_chunk(png_ptr, length)) |
| 2900 | keep = PNG_HANDLE_CHUNK_NEVER; |
| 2901 | } |
| 2902 | |
| 2903 | else |
| 2904 | png_crc_finish(png_ptr, length); |
| 2905 | } |
| 2906 | # else |
| 2907 | # ifndef PNG_READ_USER_CHUNKS_SUPPORTED |
| 2908 | # error no method to support READ_UNKNOWN_CHUNKS |
| 2909 | # endif |
| 2910 | |
| 2911 | { |
| 2912 | /* If here there is no read callback pointer set and no support is |
| 2913 | * compiled in to just save the unknown chunks, so simply skip this |
| 2914 | * chunk. If 'keep' is something other than AS_DEFAULT or NEVER then |
| 2915 | * the app has erroneously asked for unknown chunk saving when there |
| 2916 | * is no support. |
| 2917 | */ |
| 2918 | if (keep > PNG_HANDLE_CHUNK_NEVER) |
| 2919 | png_app_error(png_ptr, "no unknown chunk support available"); |
| 2920 | |
| 2921 | png_crc_finish(png_ptr, length); |
| 2922 | } |
| 2923 | # endif |
| 2924 | |
| 2925 | # ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED |
| 2926 | /* Now store the chunk in the chunk list if appropriate, and if the limits |
| 2927 | * permit it. |
| 2928 | */ |
| 2929 | if (keep == PNG_HANDLE_CHUNK_ALWAYS || |
| 2930 | (keep == PNG_HANDLE_CHUNK_IF_SAFE && |
| 2931 | PNG_CHUNK_ANCILLARY(png_ptr->chunk_name))) |
| 2932 | { |
| 2933 | # ifdef PNG_USER_LIMITS_SUPPORTED |
| 2934 | switch (png_ptr->user_chunk_cache_max) |
| 2935 | { |
| 2936 | case 2: |
| 2937 | png_ptr->user_chunk_cache_max = 1; |
| 2938 | png_chunk_benign_error(png_ptr, "no space in chunk cache"); |
| 2939 | /* FALL THROUGH */ |
| 2940 | case 1: |
| 2941 | /* NOTE: prior to 1.6.0 this case resulted in an unknown critical |
| 2942 | * chunk being skipped, now there will be a hard error below. |
| 2943 | */ |
| 2944 | break; |
| 2945 | |
| 2946 | default: /* not at limit */ |
| 2947 | --(png_ptr->user_chunk_cache_max); |
| 2948 | /* FALL THROUGH */ |
| 2949 | case 0: /* no limit */ |
| 2950 | # endif /* PNG_USER_LIMITS_SUPPORTED */ |
| 2951 | /* Here when the limit isn't reached or when limits are compiled |
| 2952 | * out; store the chunk. |
| 2953 | */ |
| 2954 | png_set_unknown_chunks(png_ptr, info_ptr, |
| 2955 | &png_ptr->unknown_chunk, 1); |
| 2956 | handled = 1; |
| 2957 | # ifdef PNG_USER_LIMITS_SUPPORTED |
| 2958 | break; |
| 2959 | } |
| 2960 | # endif |
| 2961 | } |
| 2962 | # else /* no store support! */ |
| 2963 | PNG_UNUSED(info_ptr) |
| 2964 | # error untested code (reading unknown chunks with no store support) |
| 2965 | # endif |
| 2966 | |
| 2967 | /* Regardless of the error handling below the cached data (if any) can be |
| 2968 | * freed now. Notice that the data is not freed if there is a png_error, but |
| 2969 | * it will be freed by destroy_read_struct. |
| 2970 | */ |
| 2971 | if (png_ptr->unknown_chunk.data != NULL) |
| 2972 | png_free(png_ptr, png_ptr->unknown_chunk.data); |
| 2973 | png_ptr->unknown_chunk.data = NULL; |
| 2974 | |
| 2975 | #else /* !PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */ |
| 2976 | /* There is no support to read an unknown chunk, so just skip it. */ |
| 2977 | png_crc_finish(png_ptr, length); |
| 2978 | PNG_UNUSED(info_ptr) |
| 2979 | PNG_UNUSED(keep) |
| 2980 | #endif /* !PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */ |
| 2981 | |
| 2982 | /* Check for unhandled critical chunks */ |
| 2983 | if (!handled && PNG_CHUNK_CRITICAL(png_ptr->chunk_name)) |
| 2984 | png_chunk_error(png_ptr, "unhandled critical chunk"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2985 | } |
| 2986 | |
| 2987 | /* This function is called to verify that a chunk name is valid. |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2988 | * This function can't have the "critical chunk check" incorporated |
| 2989 | * into it, since in the future we will need to be able to call user |
| 2990 | * functions to handle unknown critical chunks after we check that |
| 2991 | * the chunk name itself is valid. |
| 2992 | */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2993 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2994 | /* Bit hacking: the test for an invalid byte in the 4 byte chunk name is: |
| 2995 | * |
| 2996 | * ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97)) |
| 2997 | */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2998 | |
| 2999 | void /* PRIVATE */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3000 | png_check_chunk_name(png_structrp png_ptr, png_uint_32 chunk_name) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3001 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3002 | int i; |
| 3003 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 3004 | png_debug(1, "in png_check_chunk_name"); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3005 | |
| 3006 | for (i=1; i<=4; ++i) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3007 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3008 | int c = chunk_name & 0xff; |
| 3009 | |
| 3010 | if (c < 65 || c > 122 || (c > 90 && c < 97)) |
| 3011 | png_chunk_error(png_ptr, "invalid chunk type"); |
| 3012 | |
| 3013 | chunk_name >>= 8; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3014 | } |
| 3015 | } |
| 3016 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3017 | /* Combines the row recently read in with the existing pixels in the row. This |
| 3018 | * routine takes care of alpha and transparency if requested. This routine also |
| 3019 | * handles the two methods of progressive display of interlaced images, |
| 3020 | * depending on the 'display' value; if 'display' is true then the whole row |
| 3021 | * (dp) is filled from the start by replicating the available pixels. If |
| 3022 | * 'display' is false only those pixels present in the pass are filled in. |
| 3023 | */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3024 | void /* PRIVATE */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3025 | png_combine_row(png_const_structrp png_ptr, png_bytep dp, int display) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3026 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3027 | unsigned int pixel_depth = png_ptr->transformed_pixel_depth; |
| 3028 | png_const_bytep sp = png_ptr->row_buf + 1; |
| 3029 | png_uint_32 row_width = png_ptr->width; |
| 3030 | unsigned int pass = png_ptr->pass; |
| 3031 | png_bytep end_ptr = 0; |
| 3032 | png_byte end_byte = 0; |
| 3033 | unsigned int end_mask; |
| 3034 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 3035 | png_debug(1, "in png_combine_row"); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3036 | |
| 3037 | /* Added in 1.5.6: it should not be possible to enter this routine until at |
| 3038 | * least one row has been read from the PNG data and transformed. |
| 3039 | */ |
| 3040 | if (pixel_depth == 0) |
| 3041 | png_error(png_ptr, "internal row logic error"); |
| 3042 | |
| 3043 | /* Added in 1.5.4: the pixel depth should match the information returned by |
| 3044 | * any call to png_read_update_info at this point. Do not continue if we got |
| 3045 | * this wrong. |
| 3046 | */ |
| 3047 | if (png_ptr->info_rowbytes != 0 && png_ptr->info_rowbytes != |
| 3048 | PNG_ROWBYTES(pixel_depth, row_width)) |
| 3049 | png_error(png_ptr, "internal row size calculation error"); |
| 3050 | |
| 3051 | /* Don't expect this to ever happen: */ |
| 3052 | if (row_width == 0) |
| 3053 | png_error(png_ptr, "internal row width error"); |
| 3054 | |
| 3055 | /* Preserve the last byte in cases where only part of it will be overwritten, |
| 3056 | * the multiply below may overflow, we don't care because ANSI-C guarantees |
| 3057 | * we get the low bits. |
| 3058 | */ |
| 3059 | end_mask = (pixel_depth * row_width) & 7; |
| 3060 | if (end_mask != 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3061 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3062 | /* end_ptr == NULL is a flag to say do nothing */ |
| 3063 | end_ptr = dp + PNG_ROWBYTES(pixel_depth, row_width) - 1; |
| 3064 | end_byte = *end_ptr; |
| 3065 | # ifdef PNG_READ_PACKSWAP_SUPPORTED |
| 3066 | if (png_ptr->transformations & PNG_PACKSWAP) /* little-endian byte */ |
| 3067 | end_mask = 0xff << end_mask; |
| 3068 | |
| 3069 | else /* big-endian byte */ |
| 3070 | # endif |
| 3071 | end_mask = 0xff >> end_mask; |
| 3072 | /* end_mask is now the bits to *keep* from the destination row */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3073 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3074 | |
| 3075 | /* For non-interlaced images this reduces to a memcpy(). A memcpy() |
| 3076 | * will also happen if interlacing isn't supported or if the application |
| 3077 | * does not call png_set_interlace_handling(). In the latter cases the |
| 3078 | * caller just gets a sequence of the unexpanded rows from each interlace |
| 3079 | * pass. |
| 3080 | */ |
| 3081 | #ifdef PNG_READ_INTERLACING_SUPPORTED |
| 3082 | if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE) && |
| 3083 | pass < 6 && (display == 0 || |
| 3084 | /* The following copies everything for 'display' on passes 0, 2 and 4. */ |
| 3085 | (display == 1 && (pass & 1) != 0))) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3086 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3087 | /* Narrow images may have no bits in a pass; the caller should handle |
| 3088 | * this, but this test is cheap: |
| 3089 | */ |
| 3090 | if (row_width <= PNG_PASS_START_COL(pass)) |
| 3091 | return; |
| 3092 | |
| 3093 | if (pixel_depth < 8) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3094 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3095 | /* For pixel depths up to 4 bpp the 8-pixel mask can be expanded to fit |
| 3096 | * into 32 bits, then a single loop over the bytes using the four byte |
| 3097 | * values in the 32-bit mask can be used. For the 'display' option the |
| 3098 | * expanded mask may also not require any masking within a byte. To |
| 3099 | * make this work the PACKSWAP option must be taken into account - it |
| 3100 | * simply requires the pixels to be reversed in each byte. |
| 3101 | * |
| 3102 | * The 'regular' case requires a mask for each of the first 6 passes, |
| 3103 | * the 'display' case does a copy for the even passes in the range |
| 3104 | * 0..6. This has already been handled in the test above. |
| 3105 | * |
| 3106 | * The masks are arranged as four bytes with the first byte to use in |
| 3107 | * the lowest bits (little-endian) regardless of the order (PACKSWAP or |
| 3108 | * not) of the pixels in each byte. |
| 3109 | * |
| 3110 | * NOTE: the whole of this logic depends on the caller of this function |
| 3111 | * only calling it on rows appropriate to the pass. This function only |
| 3112 | * understands the 'x' logic; the 'y' logic is handled by the caller. |
| 3113 | * |
| 3114 | * The following defines allow generation of compile time constant bit |
| 3115 | * masks for each pixel depth and each possibility of swapped or not |
| 3116 | * swapped bytes. Pass 'p' is in the range 0..6; 'x', a pixel index, |
| 3117 | * is in the range 0..7; and the result is 1 if the pixel is to be |
| 3118 | * copied in the pass, 0 if not. 'S' is for the sparkle method, 'B' |
| 3119 | * for the block method. |
| 3120 | * |
| 3121 | * With some compilers a compile time expression of the general form: |
| 3122 | * |
| 3123 | * (shift >= 32) ? (a >> (shift-32)) : (b >> shift) |
| 3124 | * |
| 3125 | * Produces warnings with values of 'shift' in the range 33 to 63 |
| 3126 | * because the right hand side of the ?: expression is evaluated by |
| 3127 | * the compiler even though it isn't used. Microsoft Visual C (various |
| 3128 | * versions) and the Intel C compiler are known to do this. To avoid |
| 3129 | * this the following macros are used in 1.5.6. This is a temporary |
| 3130 | * solution to avoid destabilizing the code during the release process. |
| 3131 | */ |
| 3132 | # if PNG_USE_COMPILE_TIME_MASKS |
| 3133 | # define PNG_LSR(x,s) ((x)>>((s) & 0x1f)) |
| 3134 | # define PNG_LSL(x,s) ((x)<<((s) & 0x1f)) |
| 3135 | # else |
| 3136 | # define PNG_LSR(x,s) ((x)>>(s)) |
| 3137 | # define PNG_LSL(x,s) ((x)<<(s)) |
| 3138 | # endif |
| 3139 | # define S_COPY(p,x) (((p)<4 ? PNG_LSR(0x80088822,(3-(p))*8+(7-(x))) :\ |
| 3140 | PNG_LSR(0xaa55ff00,(7-(p))*8+(7-(x)))) & 1) |
| 3141 | # define B_COPY(p,x) (((p)<4 ? PNG_LSR(0xff0fff33,(3-(p))*8+(7-(x))) :\ |
| 3142 | PNG_LSR(0xff55ff00,(7-(p))*8+(7-(x)))) & 1) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3143 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3144 | /* Return a mask for pass 'p' pixel 'x' at depth 'd'. The mask is |
| 3145 | * little endian - the first pixel is at bit 0 - however the extra |
| 3146 | * parameter 's' can be set to cause the mask position to be swapped |
| 3147 | * within each byte, to match the PNG format. This is done by XOR of |
| 3148 | * the shift with 7, 6 or 4 for bit depths 1, 2 and 4. |
| 3149 | */ |
| 3150 | # define PIXEL_MASK(p,x,d,s) \ |
| 3151 | (PNG_LSL(((PNG_LSL(1U,(d)))-1),(((x)*(d))^((s)?8-(d):0)))) |
| 3152 | |
| 3153 | /* Hence generate the appropriate 'block' or 'sparkle' pixel copy mask. |
| 3154 | */ |
| 3155 | # define S_MASKx(p,x,d,s) (S_COPY(p,x)?PIXEL_MASK(p,x,d,s):0) |
| 3156 | # define B_MASKx(p,x,d,s) (B_COPY(p,x)?PIXEL_MASK(p,x,d,s):0) |
| 3157 | |
| 3158 | /* Combine 8 of these to get the full mask. For the 1-bpp and 2-bpp |
| 3159 | * cases the result needs replicating, for the 4-bpp case the above |
| 3160 | * generates a full 32 bits. |
| 3161 | */ |
| 3162 | # define MASK_EXPAND(m,d) ((m)*((d)==1?0x01010101:((d)==2?0x00010001:1))) |
| 3163 | |
| 3164 | # define S_MASK(p,d,s) MASK_EXPAND(S_MASKx(p,0,d,s) + S_MASKx(p,1,d,s) +\ |
| 3165 | S_MASKx(p,2,d,s) + S_MASKx(p,3,d,s) + S_MASKx(p,4,d,s) +\ |
| 3166 | S_MASKx(p,5,d,s) + S_MASKx(p,6,d,s) + S_MASKx(p,7,d,s), d) |
| 3167 | |
| 3168 | # define B_MASK(p,d,s) MASK_EXPAND(B_MASKx(p,0,d,s) + B_MASKx(p,1,d,s) +\ |
| 3169 | B_MASKx(p,2,d,s) + B_MASKx(p,3,d,s) + B_MASKx(p,4,d,s) +\ |
| 3170 | B_MASKx(p,5,d,s) + B_MASKx(p,6,d,s) + B_MASKx(p,7,d,s), d) |
| 3171 | |
| 3172 | #if PNG_USE_COMPILE_TIME_MASKS |
| 3173 | /* Utility macros to construct all the masks for a depth/swap |
| 3174 | * combination. The 's' parameter says whether the format is PNG |
| 3175 | * (big endian bytes) or not. Only the three odd-numbered passes are |
| 3176 | * required for the display/block algorithm. |
| 3177 | */ |
| 3178 | # define S_MASKS(d,s) { S_MASK(0,d,s), S_MASK(1,d,s), S_MASK(2,d,s),\ |
| 3179 | S_MASK(3,d,s), S_MASK(4,d,s), S_MASK(5,d,s) } |
| 3180 | |
| 3181 | # define B_MASKS(d,s) { B_MASK(1,d,s), S_MASK(3,d,s), S_MASK(5,d,s) } |
| 3182 | |
| 3183 | # define DEPTH_INDEX(d) ((d)==1?0:((d)==2?1:2)) |
| 3184 | |
| 3185 | /* Hence the pre-compiled masks indexed by PACKSWAP (or not), depth and |
| 3186 | * then pass: |
| 3187 | */ |
| 3188 | static PNG_CONST png_uint_32 row_mask[2/*PACKSWAP*/][3/*depth*/][6] = |
| 3189 | { |
| 3190 | /* Little-endian byte masks for PACKSWAP */ |
| 3191 | { S_MASKS(1,0), S_MASKS(2,0), S_MASKS(4,0) }, |
| 3192 | /* Normal (big-endian byte) masks - PNG format */ |
| 3193 | { S_MASKS(1,1), S_MASKS(2,1), S_MASKS(4,1) } |
| 3194 | }; |
| 3195 | |
| 3196 | /* display_mask has only three entries for the odd passes, so index by |
| 3197 | * pass>>1. |
| 3198 | */ |
| 3199 | static PNG_CONST png_uint_32 display_mask[2][3][3] = |
| 3200 | { |
| 3201 | /* Little-endian byte masks for PACKSWAP */ |
| 3202 | { B_MASKS(1,0), B_MASKS(2,0), B_MASKS(4,0) }, |
| 3203 | /* Normal (big-endian byte) masks - PNG format */ |
| 3204 | { B_MASKS(1,1), B_MASKS(2,1), B_MASKS(4,1) } |
| 3205 | }; |
| 3206 | |
| 3207 | # define MASK(pass,depth,display,png)\ |
| 3208 | ((display)?display_mask[png][DEPTH_INDEX(depth)][pass>>1]:\ |
| 3209 | row_mask[png][DEPTH_INDEX(depth)][pass]) |
| 3210 | |
| 3211 | #else /* !PNG_USE_COMPILE_TIME_MASKS */ |
| 3212 | /* This is the runtime alternative: it seems unlikely that this will |
| 3213 | * ever be either smaller or faster than the compile time approach. |
| 3214 | */ |
| 3215 | # define MASK(pass,depth,display,png)\ |
| 3216 | ((display)?B_MASK(pass,depth,png):S_MASK(pass,depth,png)) |
| 3217 | #endif /* !PNG_USE_COMPILE_TIME_MASKS */ |
| 3218 | |
| 3219 | /* Use the appropriate mask to copy the required bits. In some cases |
| 3220 | * the byte mask will be 0 or 0xff, optimize these cases. row_width is |
| 3221 | * the number of pixels, but the code copies bytes, so it is necessary |
| 3222 | * to special case the end. |
| 3223 | */ |
| 3224 | png_uint_32 pixels_per_byte = 8 / pixel_depth; |
| 3225 | png_uint_32 mask; |
| 3226 | |
| 3227 | # ifdef PNG_READ_PACKSWAP_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3228 | if (png_ptr->transformations & PNG_PACKSWAP) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3229 | mask = MASK(pass, pixel_depth, display, 0); |
| 3230 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3231 | else |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3232 | # endif |
| 3233 | mask = MASK(pass, pixel_depth, display, 1); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3234 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3235 | for (;;) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3236 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3237 | png_uint_32 m; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3238 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3239 | /* It doesn't matter in the following if png_uint_32 has more than |
| 3240 | * 32 bits because the high bits always match those in m<<24; it is, |
| 3241 | * however, essential to use OR here, not +, because of this. |
| 3242 | */ |
| 3243 | m = mask; |
| 3244 | mask = (m >> 8) | (m << 24); /* rotate right to good compilers */ |
| 3245 | m &= 0xff; |
| 3246 | |
| 3247 | if (m != 0) /* something to copy */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3248 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3249 | if (m != 0xff) |
| 3250 | *dp = (png_byte)((*dp & ~m) | (*sp & m)); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3251 | else |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3252 | *dp = *sp; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3253 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3254 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3255 | /* NOTE: this may overwrite the last byte with garbage if the image |
| 3256 | * is not an exact number of bytes wide; libpng has always done |
| 3257 | * this. |
| 3258 | */ |
| 3259 | if (row_width <= pixels_per_byte) |
| 3260 | break; /* May need to restore part of the last byte */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3261 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3262 | row_width -= pixels_per_byte; |
| 3263 | ++dp; |
| 3264 | ++sp; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3265 | } |
| 3266 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3267 | |
| 3268 | else /* pixel_depth >= 8 */ |
| 3269 | { |
| 3270 | unsigned int bytes_to_copy, bytes_to_jump; |
| 3271 | |
| 3272 | /* Validate the depth - it must be a multiple of 8 */ |
| 3273 | if (pixel_depth & 7) |
| 3274 | png_error(png_ptr, "invalid user transform pixel depth"); |
| 3275 | |
| 3276 | pixel_depth >>= 3; /* now in bytes */ |
| 3277 | row_width *= pixel_depth; |
| 3278 | |
| 3279 | /* Regardless of pass number the Adam 7 interlace always results in a |
| 3280 | * fixed number of pixels to copy then to skip. There may be a |
| 3281 | * different number of pixels to skip at the start though. |
| 3282 | */ |
| 3283 | { |
| 3284 | unsigned int offset = PNG_PASS_START_COL(pass) * pixel_depth; |
| 3285 | |
| 3286 | row_width -= offset; |
| 3287 | dp += offset; |
| 3288 | sp += offset; |
| 3289 | } |
| 3290 | |
| 3291 | /* Work out the bytes to copy. */ |
| 3292 | if (display) |
| 3293 | { |
| 3294 | /* When doing the 'block' algorithm the pixel in the pass gets |
| 3295 | * replicated to adjacent pixels. This is why the even (0,2,4,6) |
| 3296 | * passes are skipped above - the entire expanded row is copied. |
| 3297 | */ |
| 3298 | bytes_to_copy = (1<<((6-pass)>>1)) * pixel_depth; |
| 3299 | |
| 3300 | /* But don't allow this number to exceed the actual row width. */ |
| 3301 | if (bytes_to_copy > row_width) |
| 3302 | bytes_to_copy = row_width; |
| 3303 | } |
| 3304 | |
| 3305 | else /* normal row; Adam7 only ever gives us one pixel to copy. */ |
| 3306 | bytes_to_copy = pixel_depth; |
| 3307 | |
| 3308 | /* In Adam7 there is a constant offset between where the pixels go. */ |
| 3309 | bytes_to_jump = PNG_PASS_COL_OFFSET(pass) * pixel_depth; |
| 3310 | |
| 3311 | /* And simply copy these bytes. Some optimization is possible here, |
| 3312 | * depending on the value of 'bytes_to_copy'. Special case the low |
| 3313 | * byte counts, which we know to be frequent. |
| 3314 | * |
| 3315 | * Notice that these cases all 'return' rather than 'break' - this |
| 3316 | * avoids an unnecessary test on whether to restore the last byte |
| 3317 | * below. |
| 3318 | */ |
| 3319 | switch (bytes_to_copy) |
| 3320 | { |
| 3321 | case 1: |
| 3322 | for (;;) |
| 3323 | { |
| 3324 | *dp = *sp; |
| 3325 | |
| 3326 | if (row_width <= bytes_to_jump) |
| 3327 | return; |
| 3328 | |
| 3329 | dp += bytes_to_jump; |
| 3330 | sp += bytes_to_jump; |
| 3331 | row_width -= bytes_to_jump; |
| 3332 | } |
| 3333 | |
| 3334 | case 2: |
| 3335 | /* There is a possibility of a partial copy at the end here; this |
| 3336 | * slows the code down somewhat. |
| 3337 | */ |
| 3338 | do |
| 3339 | { |
| 3340 | dp[0] = sp[0], dp[1] = sp[1]; |
| 3341 | |
| 3342 | if (row_width <= bytes_to_jump) |
| 3343 | return; |
| 3344 | |
| 3345 | sp += bytes_to_jump; |
| 3346 | dp += bytes_to_jump; |
| 3347 | row_width -= bytes_to_jump; |
| 3348 | } |
| 3349 | while (row_width > 1); |
| 3350 | |
| 3351 | /* And there can only be one byte left at this point: */ |
| 3352 | *dp = *sp; |
| 3353 | return; |
| 3354 | |
| 3355 | case 3: |
| 3356 | /* This can only be the RGB case, so each copy is exactly one |
| 3357 | * pixel and it is not necessary to check for a partial copy. |
| 3358 | */ |
| 3359 | for(;;) |
| 3360 | { |
| 3361 | dp[0] = sp[0], dp[1] = sp[1], dp[2] = sp[2]; |
| 3362 | |
| 3363 | if (row_width <= bytes_to_jump) |
| 3364 | return; |
| 3365 | |
| 3366 | sp += bytes_to_jump; |
| 3367 | dp += bytes_to_jump; |
| 3368 | row_width -= bytes_to_jump; |
| 3369 | } |
| 3370 | |
| 3371 | default: |
| 3372 | #if PNG_ALIGN_TYPE != PNG_ALIGN_NONE |
| 3373 | /* Check for double byte alignment and, if possible, use a |
| 3374 | * 16-bit copy. Don't attempt this for narrow images - ones that |
| 3375 | * are less than an interlace panel wide. Don't attempt it for |
| 3376 | * wide bytes_to_copy either - use the memcpy there. |
| 3377 | */ |
| 3378 | if (bytes_to_copy < 16 /*else use memcpy*/ && |
| 3379 | png_isaligned(dp, png_uint_16) && |
| 3380 | png_isaligned(sp, png_uint_16) && |
| 3381 | bytes_to_copy % (sizeof (png_uint_16)) == 0 && |
| 3382 | bytes_to_jump % (sizeof (png_uint_16)) == 0) |
| 3383 | { |
| 3384 | /* Everything is aligned for png_uint_16 copies, but try for |
| 3385 | * png_uint_32 first. |
| 3386 | */ |
| 3387 | if (png_isaligned(dp, png_uint_32) && |
| 3388 | png_isaligned(sp, png_uint_32) && |
| 3389 | bytes_to_copy % (sizeof (png_uint_32)) == 0 && |
| 3390 | bytes_to_jump % (sizeof (png_uint_32)) == 0) |
| 3391 | { |
| 3392 | png_uint_32p dp32 = png_aligncast(png_uint_32p,dp); |
| 3393 | png_const_uint_32p sp32 = png_aligncastconst( |
| 3394 | png_const_uint_32p, sp); |
| 3395 | size_t skip = (bytes_to_jump-bytes_to_copy) / |
| 3396 | (sizeof (png_uint_32)); |
| 3397 | |
| 3398 | do |
| 3399 | { |
| 3400 | size_t c = bytes_to_copy; |
| 3401 | do |
| 3402 | { |
| 3403 | *dp32++ = *sp32++; |
| 3404 | c -= (sizeof (png_uint_32)); |
| 3405 | } |
| 3406 | while (c > 0); |
| 3407 | |
| 3408 | if (row_width <= bytes_to_jump) |
| 3409 | return; |
| 3410 | |
| 3411 | dp32 += skip; |
| 3412 | sp32 += skip; |
| 3413 | row_width -= bytes_to_jump; |
| 3414 | } |
| 3415 | while (bytes_to_copy <= row_width); |
| 3416 | |
| 3417 | /* Get to here when the row_width truncates the final copy. |
| 3418 | * There will be 1-3 bytes left to copy, so don't try the |
| 3419 | * 16-bit loop below. |
| 3420 | */ |
| 3421 | dp = (png_bytep)dp32; |
| 3422 | sp = (png_const_bytep)sp32; |
| 3423 | do |
| 3424 | *dp++ = *sp++; |
| 3425 | while (--row_width > 0); |
| 3426 | return; |
| 3427 | } |
| 3428 | |
| 3429 | /* Else do it in 16-bit quantities, but only if the size is |
| 3430 | * not too large. |
| 3431 | */ |
| 3432 | else |
| 3433 | { |
| 3434 | png_uint_16p dp16 = png_aligncast(png_uint_16p, dp); |
| 3435 | png_const_uint_16p sp16 = png_aligncastconst( |
| 3436 | png_const_uint_16p, sp); |
| 3437 | size_t skip = (bytes_to_jump-bytes_to_copy) / |
| 3438 | (sizeof (png_uint_16)); |
| 3439 | |
| 3440 | do |
| 3441 | { |
| 3442 | size_t c = bytes_to_copy; |
| 3443 | do |
| 3444 | { |
| 3445 | *dp16++ = *sp16++; |
| 3446 | c -= (sizeof (png_uint_16)); |
| 3447 | } |
| 3448 | while (c > 0); |
| 3449 | |
| 3450 | if (row_width <= bytes_to_jump) |
| 3451 | return; |
| 3452 | |
| 3453 | dp16 += skip; |
| 3454 | sp16 += skip; |
| 3455 | row_width -= bytes_to_jump; |
| 3456 | } |
| 3457 | while (bytes_to_copy <= row_width); |
| 3458 | |
| 3459 | /* End of row - 1 byte left, bytes_to_copy > row_width: */ |
| 3460 | dp = (png_bytep)dp16; |
| 3461 | sp = (png_const_bytep)sp16; |
| 3462 | do |
| 3463 | *dp++ = *sp++; |
| 3464 | while (--row_width > 0); |
| 3465 | return; |
| 3466 | } |
| 3467 | } |
| 3468 | #endif /* PNG_ALIGN_ code */ |
| 3469 | |
| 3470 | /* The true default - use a memcpy: */ |
| 3471 | for (;;) |
| 3472 | { |
| 3473 | memcpy(dp, sp, bytes_to_copy); |
| 3474 | |
| 3475 | if (row_width <= bytes_to_jump) |
| 3476 | return; |
| 3477 | |
| 3478 | sp += bytes_to_jump; |
| 3479 | dp += bytes_to_jump; |
| 3480 | row_width -= bytes_to_jump; |
| 3481 | if (bytes_to_copy > row_width) |
| 3482 | bytes_to_copy = row_width; |
| 3483 | } |
| 3484 | } |
| 3485 | |
| 3486 | /* NOT REACHED*/ |
| 3487 | } /* pixel_depth >= 8 */ |
| 3488 | |
| 3489 | /* Here if pixel_depth < 8 to check 'end_ptr' below. */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3490 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3491 | else |
| 3492 | #endif |
| 3493 | |
| 3494 | /* If here then the switch above wasn't used so just memcpy the whole row |
| 3495 | * from the temporary row buffer (notice that this overwrites the end of the |
| 3496 | * destination row if it is a partial byte.) |
| 3497 | */ |
| 3498 | memcpy(dp, sp, PNG_ROWBYTES(pixel_depth, row_width)); |
| 3499 | |
| 3500 | /* Restore the overwritten bits from the last byte if necessary. */ |
| 3501 | if (end_ptr != NULL) |
| 3502 | *end_ptr = (png_byte)((end_byte & end_mask) | (*end_ptr & ~end_mask)); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3503 | } |
| 3504 | |
| 3505 | #ifdef PNG_READ_INTERLACING_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3506 | void /* PRIVATE */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3507 | png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass, |
| 3508 | png_uint_32 transformations /* Because these may affect the byte layout */) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3509 | { |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 3510 | /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ |
| 3511 | /* Offset to next interlace block */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3512 | static PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3513 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 3514 | png_debug(1, "in png_do_read_interlace"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3515 | if (row != NULL && row_info != NULL) |
| 3516 | { |
| 3517 | png_uint_32 final_width; |
| 3518 | |
| 3519 | final_width = row_info->width * png_pass_inc[pass]; |
| 3520 | |
| 3521 | switch (row_info->pixel_depth) |
| 3522 | { |
| 3523 | case 1: |
| 3524 | { |
| 3525 | png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 3); |
| 3526 | png_bytep dp = row + (png_size_t)((final_width - 1) >> 3); |
| 3527 | int sshift, dshift; |
| 3528 | int s_start, s_end, s_inc; |
| 3529 | int jstop = png_pass_inc[pass]; |
| 3530 | png_byte v; |
| 3531 | png_uint_32 i; |
| 3532 | int j; |
| 3533 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 3534 | #ifdef PNG_READ_PACKSWAP_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3535 | if (transformations & PNG_PACKSWAP) |
| 3536 | { |
| 3537 | sshift = (int)((row_info->width + 7) & 0x07); |
| 3538 | dshift = (int)((final_width + 7) & 0x07); |
| 3539 | s_start = 7; |
| 3540 | s_end = 0; |
| 3541 | s_inc = -1; |
| 3542 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3543 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3544 | else |
| 3545 | #endif |
| 3546 | { |
| 3547 | sshift = 7 - (int)((row_info->width + 7) & 0x07); |
| 3548 | dshift = 7 - (int)((final_width + 7) & 0x07); |
| 3549 | s_start = 0; |
| 3550 | s_end = 7; |
| 3551 | s_inc = 1; |
| 3552 | } |
| 3553 | |
| 3554 | for (i = 0; i < row_info->width; i++) |
| 3555 | { |
| 3556 | v = (png_byte)((*sp >> sshift) & 0x01); |
| 3557 | for (j = 0; j < jstop; j++) |
| 3558 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3559 | unsigned int tmp = *dp & (0x7f7f >> (7 - dshift)); |
| 3560 | tmp |= v << dshift; |
| 3561 | *dp = (png_byte)(tmp & 0xff); |
| 3562 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3563 | if (dshift == s_end) |
| 3564 | { |
| 3565 | dshift = s_start; |
| 3566 | dp--; |
| 3567 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3568 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3569 | else |
| 3570 | dshift += s_inc; |
| 3571 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3572 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3573 | if (sshift == s_end) |
| 3574 | { |
| 3575 | sshift = s_start; |
| 3576 | sp--; |
| 3577 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3578 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3579 | else |
| 3580 | sshift += s_inc; |
| 3581 | } |
| 3582 | break; |
| 3583 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3584 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3585 | case 2: |
| 3586 | { |
| 3587 | png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2); |
| 3588 | png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2); |
| 3589 | int sshift, dshift; |
| 3590 | int s_start, s_end, s_inc; |
| 3591 | int jstop = png_pass_inc[pass]; |
| 3592 | png_uint_32 i; |
| 3593 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 3594 | #ifdef PNG_READ_PACKSWAP_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3595 | if (transformations & PNG_PACKSWAP) |
| 3596 | { |
| 3597 | sshift = (int)(((row_info->width + 3) & 0x03) << 1); |
| 3598 | dshift = (int)(((final_width + 3) & 0x03) << 1); |
| 3599 | s_start = 6; |
| 3600 | s_end = 0; |
| 3601 | s_inc = -2; |
| 3602 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3603 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3604 | else |
| 3605 | #endif |
| 3606 | { |
| 3607 | sshift = (int)((3 - ((row_info->width + 3) & 0x03)) << 1); |
| 3608 | dshift = (int)((3 - ((final_width + 3) & 0x03)) << 1); |
| 3609 | s_start = 0; |
| 3610 | s_end = 6; |
| 3611 | s_inc = 2; |
| 3612 | } |
| 3613 | |
| 3614 | for (i = 0; i < row_info->width; i++) |
| 3615 | { |
| 3616 | png_byte v; |
| 3617 | int j; |
| 3618 | |
| 3619 | v = (png_byte)((*sp >> sshift) & 0x03); |
| 3620 | for (j = 0; j < jstop; j++) |
| 3621 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3622 | unsigned int tmp = *dp & (0x3f3f >> (6 - dshift)); |
| 3623 | tmp |= v << dshift; |
| 3624 | *dp = (png_byte)(tmp & 0xff); |
| 3625 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3626 | if (dshift == s_end) |
| 3627 | { |
| 3628 | dshift = s_start; |
| 3629 | dp--; |
| 3630 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3631 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3632 | else |
| 3633 | dshift += s_inc; |
| 3634 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3635 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3636 | if (sshift == s_end) |
| 3637 | { |
| 3638 | sshift = s_start; |
| 3639 | sp--; |
| 3640 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3641 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3642 | else |
| 3643 | sshift += s_inc; |
| 3644 | } |
| 3645 | break; |
| 3646 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3647 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3648 | case 4: |
| 3649 | { |
| 3650 | png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 1); |
| 3651 | png_bytep dp = row + (png_size_t)((final_width - 1) >> 1); |
| 3652 | int sshift, dshift; |
| 3653 | int s_start, s_end, s_inc; |
| 3654 | png_uint_32 i; |
| 3655 | int jstop = png_pass_inc[pass]; |
| 3656 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 3657 | #ifdef PNG_READ_PACKSWAP_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3658 | if (transformations & PNG_PACKSWAP) |
| 3659 | { |
| 3660 | sshift = (int)(((row_info->width + 1) & 0x01) << 2); |
| 3661 | dshift = (int)(((final_width + 1) & 0x01) << 2); |
| 3662 | s_start = 4; |
| 3663 | s_end = 0; |
| 3664 | s_inc = -4; |
| 3665 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3666 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3667 | else |
| 3668 | #endif |
| 3669 | { |
| 3670 | sshift = (int)((1 - ((row_info->width + 1) & 0x01)) << 2); |
| 3671 | dshift = (int)((1 - ((final_width + 1) & 0x01)) << 2); |
| 3672 | s_start = 0; |
| 3673 | s_end = 4; |
| 3674 | s_inc = 4; |
| 3675 | } |
| 3676 | |
| 3677 | for (i = 0; i < row_info->width; i++) |
| 3678 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3679 | png_byte v = (png_byte)((*sp >> sshift) & 0x0f); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3680 | int j; |
| 3681 | |
| 3682 | for (j = 0; j < jstop; j++) |
| 3683 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3684 | unsigned int tmp = *dp & (0xf0f >> (4 - dshift)); |
| 3685 | tmp |= v << dshift; |
| 3686 | *dp = (png_byte)(tmp & 0xff); |
| 3687 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3688 | if (dshift == s_end) |
| 3689 | { |
| 3690 | dshift = s_start; |
| 3691 | dp--; |
| 3692 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3693 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3694 | else |
| 3695 | dshift += s_inc; |
| 3696 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3697 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3698 | if (sshift == s_end) |
| 3699 | { |
| 3700 | sshift = s_start; |
| 3701 | sp--; |
| 3702 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3703 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3704 | else |
| 3705 | sshift += s_inc; |
| 3706 | } |
| 3707 | break; |
| 3708 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3709 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3710 | default: |
| 3711 | { |
| 3712 | png_size_t pixel_bytes = (row_info->pixel_depth >> 3); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3713 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 3714 | png_bytep sp = row + (png_size_t)(row_info->width - 1) |
| 3715 | * pixel_bytes; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3716 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3717 | png_bytep dp = row + (png_size_t)(final_width - 1) * pixel_bytes; |
| 3718 | |
| 3719 | int jstop = png_pass_inc[pass]; |
| 3720 | png_uint_32 i; |
| 3721 | |
| 3722 | for (i = 0; i < row_info->width; i++) |
| 3723 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3724 | png_byte v[8]; /* SAFE; pixel_depth does not exceed 64 */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3725 | int j; |
| 3726 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3727 | memcpy(v, sp, pixel_bytes); |
| 3728 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3729 | for (j = 0; j < jstop; j++) |
| 3730 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3731 | memcpy(dp, v, pixel_bytes); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3732 | dp -= pixel_bytes; |
| 3733 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3734 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3735 | sp -= pixel_bytes; |
| 3736 | } |
| 3737 | break; |
| 3738 | } |
| 3739 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3740 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3741 | row_info->width = final_width; |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 3742 | row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3743 | } |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 3744 | #ifndef PNG_READ_PACKSWAP_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3745 | PNG_UNUSED(transformations) /* Silence compiler warning */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3746 | #endif |
| 3747 | } |
| 3748 | #endif /* PNG_READ_INTERLACING_SUPPORTED */ |
| 3749 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3750 | static void |
| 3751 | png_read_filter_row_sub(png_row_infop row_info, png_bytep row, |
| 3752 | png_const_bytep prev_row) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3753 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3754 | png_size_t i; |
| 3755 | png_size_t istop = row_info->rowbytes; |
| 3756 | unsigned int bpp = (row_info->pixel_depth + 7) >> 3; |
| 3757 | png_bytep rp = row + bpp; |
| 3758 | |
| 3759 | PNG_UNUSED(prev_row) |
| 3760 | |
| 3761 | for (i = bpp; i < istop; i++) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3762 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3763 | *rp = (png_byte)(((int)(*rp) + (int)(*(rp-bpp))) & 0xff); |
| 3764 | rp++; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3765 | } |
| 3766 | } |
| 3767 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3768 | static void |
| 3769 | png_read_filter_row_up(png_row_infop row_info, png_bytep row, |
| 3770 | png_const_bytep prev_row) |
Joseph Wen | 4ce0ee1 | 2010-08-20 10:42:22 +0800 | [diff] [blame] | 3771 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3772 | png_size_t i; |
| 3773 | png_size_t istop = row_info->rowbytes; |
| 3774 | png_bytep rp = row; |
| 3775 | png_const_bytep pp = prev_row; |
Joseph Wen | 4ce0ee1 | 2010-08-20 10:42:22 +0800 | [diff] [blame] | 3776 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3777 | for (i = 0; i < istop; i++) |
| 3778 | { |
| 3779 | *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff); |
| 3780 | rp++; |
| 3781 | } |
Joseph Wen | 4ce0ee1 | 2010-08-20 10:42:22 +0800 | [diff] [blame] | 3782 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3783 | |
| 3784 | static void |
| 3785 | png_read_filter_row_avg(png_row_infop row_info, png_bytep row, |
| 3786 | png_const_bytep prev_row) |
| 3787 | { |
| 3788 | png_size_t i; |
| 3789 | png_bytep rp = row; |
| 3790 | png_const_bytep pp = prev_row; |
| 3791 | unsigned int bpp = (row_info->pixel_depth + 7) >> 3; |
| 3792 | png_size_t istop = row_info->rowbytes - bpp; |
| 3793 | |
| 3794 | for (i = 0; i < bpp; i++) |
| 3795 | { |
| 3796 | *rp = (png_byte)(((int)(*rp) + |
| 3797 | ((int)(*pp++) / 2 )) & 0xff); |
| 3798 | |
| 3799 | rp++; |
| 3800 | } |
| 3801 | |
| 3802 | for (i = 0; i < istop; i++) |
| 3803 | { |
| 3804 | *rp = (png_byte)(((int)(*rp) + |
| 3805 | (int)(*pp++ + *(rp-bpp)) / 2 ) & 0xff); |
| 3806 | |
| 3807 | rp++; |
| 3808 | } |
| 3809 | } |
| 3810 | |
| 3811 | static void |
| 3812 | png_read_filter_row_paeth_1byte_pixel(png_row_infop row_info, png_bytep row, |
| 3813 | png_const_bytep prev_row) |
| 3814 | { |
| 3815 | png_bytep rp_end = row + row_info->rowbytes; |
| 3816 | int a, c; |
| 3817 | |
| 3818 | /* First pixel/byte */ |
| 3819 | c = *prev_row++; |
| 3820 | a = *row + c; |
| 3821 | *row++ = (png_byte)a; |
| 3822 | |
| 3823 | /* Remainder */ |
| 3824 | while (row < rp_end) |
| 3825 | { |
| 3826 | int b, pa, pb, pc, p; |
| 3827 | |
| 3828 | a &= 0xff; /* From previous iteration or start */ |
| 3829 | b = *prev_row++; |
| 3830 | |
| 3831 | p = b - c; |
| 3832 | pc = a - c; |
| 3833 | |
| 3834 | # ifdef PNG_USE_ABS |
| 3835 | pa = abs(p); |
| 3836 | pb = abs(pc); |
| 3837 | pc = abs(p + pc); |
| 3838 | # else |
| 3839 | pa = p < 0 ? -p : p; |
| 3840 | pb = pc < 0 ? -pc : pc; |
| 3841 | pc = (p + pc) < 0 ? -(p + pc) : p + pc; |
| 3842 | # endif |
| 3843 | |
| 3844 | /* Find the best predictor, the least of pa, pb, pc favoring the earlier |
| 3845 | * ones in the case of a tie. |
| 3846 | */ |
| 3847 | if (pb < pa) pa = pb, a = b; |
| 3848 | if (pc < pa) a = c; |
| 3849 | |
| 3850 | /* Calculate the current pixel in a, and move the previous row pixel to c |
| 3851 | * for the next time round the loop |
| 3852 | */ |
| 3853 | c = b; |
| 3854 | a += *row; |
| 3855 | *row++ = (png_byte)a; |
| 3856 | } |
| 3857 | } |
| 3858 | |
| 3859 | static void |
| 3860 | png_read_filter_row_paeth_multibyte_pixel(png_row_infop row_info, png_bytep row, |
| 3861 | png_const_bytep prev_row) |
| 3862 | { |
| 3863 | int bpp = (row_info->pixel_depth + 7) >> 3; |
| 3864 | png_bytep rp_end = row + bpp; |
| 3865 | |
| 3866 | /* Process the first pixel in the row completely (this is the same as 'up' |
| 3867 | * because there is only one candidate predictor for the first row). |
| 3868 | */ |
| 3869 | while (row < rp_end) |
| 3870 | { |
| 3871 | int a = *row + *prev_row++; |
| 3872 | *row++ = (png_byte)a; |
| 3873 | } |
| 3874 | |
| 3875 | /* Remainder */ |
| 3876 | rp_end += row_info->rowbytes - bpp; |
| 3877 | |
| 3878 | while (row < rp_end) |
| 3879 | { |
| 3880 | int a, b, c, pa, pb, pc, p; |
| 3881 | |
| 3882 | c = *(prev_row - bpp); |
| 3883 | a = *(row - bpp); |
| 3884 | b = *prev_row++; |
| 3885 | |
| 3886 | p = b - c; |
| 3887 | pc = a - c; |
| 3888 | |
| 3889 | # ifdef PNG_USE_ABS |
| 3890 | pa = abs(p); |
| 3891 | pb = abs(pc); |
| 3892 | pc = abs(p + pc); |
| 3893 | # else |
| 3894 | pa = p < 0 ? -p : p; |
| 3895 | pb = pc < 0 ? -pc : pc; |
| 3896 | pc = (p + pc) < 0 ? -(p + pc) : p + pc; |
| 3897 | # endif |
| 3898 | |
| 3899 | if (pb < pa) pa = pb, a = b; |
| 3900 | if (pc < pa) a = c; |
| 3901 | |
| 3902 | c = b; |
| 3903 | a += *row; |
| 3904 | *row++ = (png_byte)a; |
| 3905 | } |
| 3906 | } |
| 3907 | |
| 3908 | static void |
| 3909 | png_init_filter_functions(png_structrp pp) |
| 3910 | /* This function is called once for every PNG image to set the |
| 3911 | * implementations required to reverse the filtering of PNG rows. Reversing |
| 3912 | * the filter is the first transformation performed on the row data. It is |
| 3913 | * performed in place, therefore an implementation can be selected based on |
| 3914 | * the image pixel format. If the implementation depends on image width then |
| 3915 | * take care to ensure that it works correctly if the image is interlaced - |
| 3916 | * interlacing causes the actual row width to vary. |
| 3917 | */ |
| 3918 | { |
| 3919 | unsigned int bpp = (pp->pixel_depth + 7) >> 3; |
| 3920 | |
| 3921 | pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub; |
| 3922 | pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up; |
| 3923 | pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg; |
| 3924 | if (bpp == 1) |
| 3925 | pp->read_filter[PNG_FILTER_VALUE_PAETH-1] = |
| 3926 | png_read_filter_row_paeth_1byte_pixel; |
| 3927 | else |
| 3928 | pp->read_filter[PNG_FILTER_VALUE_PAETH-1] = |
| 3929 | png_read_filter_row_paeth_multibyte_pixel; |
| 3930 | |
| 3931 | #ifdef PNG_FILTER_OPTIMIZATIONS |
| 3932 | /* To use this define PNG_FILTER_OPTIMIZATIONS as the name of a function to |
| 3933 | * call to install hardware optimizations for the above functions; simply |
| 3934 | * replace whatever elements of the pp->read_filter[] array with a hardware |
| 3935 | * specific (or, for that matter, generic) optimization. |
| 3936 | * |
| 3937 | * To see an example of this examine what configure.ac does when |
| 3938 | * --enable-arm-neon is specified on the command line. |
| 3939 | */ |
| 3940 | PNG_FILTER_OPTIMIZATIONS(pp, bpp); |
Joseph Wen | 4ce0ee1 | 2010-08-20 10:42:22 +0800 | [diff] [blame] | 3941 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3942 | } |
| 3943 | |
| 3944 | void /* PRIVATE */ |
| 3945 | png_read_filter_row(png_structrp pp, png_row_infop row_info, png_bytep row, |
| 3946 | png_const_bytep prev_row, int filter) |
| 3947 | { |
| 3948 | /* OPTIMIZATION: DO NOT MODIFY THIS FUNCTION, instead #define |
| 3949 | * PNG_FILTER_OPTIMIZATIONS to a function that overrides the generic |
| 3950 | * implementations. See png_init_filter_functions above. |
| 3951 | */ |
| 3952 | if (pp->read_filter[0] == NULL) |
| 3953 | png_init_filter_functions(pp); |
| 3954 | if (filter > PNG_FILTER_VALUE_NONE && filter < PNG_FILTER_VALUE_LAST) |
| 3955 | pp->read_filter[filter-1](row_info, row, prev_row); |
| 3956 | } |
Joseph Wen | 4ce0ee1 | 2010-08-20 10:42:22 +0800 | [diff] [blame] | 3957 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 3958 | #ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3959 | void /* PRIVATE */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3960 | png_read_IDAT_data(png_structrp png_ptr, png_bytep output, |
| 3961 | png_alloc_size_t avail_out) |
| 3962 | { |
| 3963 | /* Loop reading IDATs and decompressing the result into output[avail_out] */ |
| 3964 | png_ptr->zstream.next_out = output; |
| 3965 | png_ptr->zstream.avail_out = 0; /* safety: set below */ |
| 3966 | |
| 3967 | if (output == NULL) |
| 3968 | avail_out = 0; |
| 3969 | |
| 3970 | do |
| 3971 | { |
| 3972 | int ret; |
| 3973 | png_byte tmpbuf[PNG_INFLATE_BUF_SIZE]; |
| 3974 | |
| 3975 | if (png_ptr->zstream.avail_in == 0) |
| 3976 | { |
| 3977 | uInt avail_in; |
| 3978 | png_bytep buffer; |
| 3979 | |
| 3980 | while (png_ptr->idat_size == 0) |
| 3981 | { |
John Reck | 90bf0d2 | 2013-08-14 10:37:42 -0700 | [diff] [blame] | 3982 | #ifdef PNG_INDEX_SUPPORTED |
| 3983 | if (png_ptr->index) { |
| 3984 | png_opt_crc_finish(png_ptr, 0); |
| 3985 | png_ptr->index->stream_idat_position = png_ptr->total_data_read; |
| 3986 | } else |
| 3987 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3988 | png_crc_finish(png_ptr, 0); |
| 3989 | |
| 3990 | png_ptr->idat_size = png_read_chunk_header(png_ptr); |
| 3991 | /* This is an error even in the 'check' case because the code just |
| 3992 | * consumed a non-IDAT header. |
| 3993 | */ |
| 3994 | if (png_ptr->chunk_name != png_IDAT) |
| 3995 | png_error(png_ptr, "Not enough image data"); |
| 3996 | } |
| 3997 | |
| 3998 | avail_in = png_ptr->IDAT_read_size; |
| 3999 | |
| 4000 | if (avail_in > png_ptr->idat_size) |
| 4001 | avail_in = (uInt)png_ptr->idat_size; |
| 4002 | |
| 4003 | /* A PNG with a gradually increasing IDAT size will defeat this attempt |
| 4004 | * to minimize memory usage by causing lots of re-allocs, but |
| 4005 | * realistically doing IDAT_read_size re-allocs is not likely to be a |
| 4006 | * big problem. |
| 4007 | */ |
| 4008 | buffer = png_read_buffer(png_ptr, avail_in, 0/*error*/); |
| 4009 | |
| 4010 | png_crc_read(png_ptr, buffer, avail_in); |
| 4011 | png_ptr->idat_size -= avail_in; |
| 4012 | |
| 4013 | png_ptr->zstream.next_in = buffer; |
| 4014 | png_ptr->zstream.avail_in = avail_in; |
| 4015 | } |
| 4016 | |
| 4017 | /* And set up the output side. */ |
| 4018 | if (output != NULL) /* standard read */ |
| 4019 | { |
| 4020 | uInt out = ZLIB_IO_MAX; |
| 4021 | |
| 4022 | if (out > avail_out) |
| 4023 | out = (uInt)avail_out; |
| 4024 | |
| 4025 | avail_out -= out; |
| 4026 | png_ptr->zstream.avail_out = out; |
| 4027 | } |
| 4028 | |
| 4029 | else /* after last row, checking for end */ |
| 4030 | { |
| 4031 | png_ptr->zstream.next_out = tmpbuf; |
| 4032 | png_ptr->zstream.avail_out = (sizeof tmpbuf); |
| 4033 | } |
| 4034 | |
| 4035 | /* Use NO_FLUSH; this gives zlib the maximum opportunity to optimize the |
| 4036 | * process. If the LZ stream is truncated the sequential reader will |
| 4037 | * terminally damage the stream, above, by reading the chunk header of the |
| 4038 | * following chunk (it then exits with png_error). |
| 4039 | * |
| 4040 | * TODO: deal more elegantly with truncated IDAT lists. |
| 4041 | */ |
| 4042 | ret = inflate(&png_ptr->zstream, Z_NO_FLUSH); |
| 4043 | |
| 4044 | /* Take the unconsumed output back. */ |
| 4045 | if (output != NULL) |
| 4046 | avail_out += png_ptr->zstream.avail_out; |
| 4047 | |
| 4048 | else /* avail_out counts the extra bytes */ |
| 4049 | avail_out += (sizeof tmpbuf) - png_ptr->zstream.avail_out; |
| 4050 | |
| 4051 | png_ptr->zstream.avail_out = 0; |
| 4052 | |
| 4053 | if (ret == Z_STREAM_END) |
| 4054 | { |
| 4055 | /* Do this for safety; we won't read any more into this row. */ |
| 4056 | png_ptr->zstream.next_out = NULL; |
| 4057 | |
| 4058 | png_ptr->mode |= PNG_AFTER_IDAT; |
| 4059 | png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED; |
| 4060 | |
| 4061 | if (png_ptr->zstream.avail_in > 0 || png_ptr->idat_size > 0) |
| 4062 | png_chunk_benign_error(png_ptr, "Extra compressed data"); |
| 4063 | break; |
| 4064 | } |
| 4065 | |
| 4066 | if (ret != Z_OK) |
John Reck | 90bf0d2 | 2013-08-14 10:37:42 -0700 | [diff] [blame] | 4067 | #ifdef PNG_INDEX_SUPPORTED |
| 4068 | if (png_ptr->index && png_ptr->row_number != png_ptr->height - 1) |
| 4069 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4070 | { |
| 4071 | png_zstream_error(png_ptr, ret); |
| 4072 | |
| 4073 | if (output != NULL) |
| 4074 | png_chunk_error(png_ptr, png_ptr->zstream.msg); |
| 4075 | |
| 4076 | else /* checking */ |
| 4077 | { |
| 4078 | png_chunk_benign_error(png_ptr, png_ptr->zstream.msg); |
| 4079 | return; |
| 4080 | } |
| 4081 | } |
| 4082 | } while (avail_out > 0); |
| 4083 | |
| 4084 | if (avail_out > 0) |
| 4085 | { |
| 4086 | /* The stream ended before the image; this is the same as too few IDATs so |
| 4087 | * should be handled the same way. |
| 4088 | */ |
| 4089 | if (output != NULL) |
| 4090 | png_error(png_ptr, "Not enough image data"); |
| 4091 | |
| 4092 | else /* the deflate stream contained extra data */ |
| 4093 | png_chunk_benign_error(png_ptr, "Too much image data"); |
| 4094 | } |
| 4095 | } |
| 4096 | |
| 4097 | void /* PRIVATE */ |
| 4098 | png_read_finish_IDAT(png_structrp png_ptr) |
| 4099 | { |
| 4100 | /* We don't need any more data and the stream should have ended, however the |
| 4101 | * LZ end code may actually not have been processed. In this case we must |
| 4102 | * read it otherwise stray unread IDAT data or, more likely, an IDAT chunk |
| 4103 | * may still remain to be consumed. |
| 4104 | */ |
| 4105 | if (!(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED)) |
| 4106 | { |
| 4107 | /* The NULL causes png_read_IDAT_data to swallow any remaining bytes in |
| 4108 | * the compressed stream, but the stream may be damaged too, so even after |
| 4109 | * this call we may need to terminate the zstream ownership. |
| 4110 | */ |
| 4111 | png_read_IDAT_data(png_ptr, NULL, 0); |
| 4112 | png_ptr->zstream.next_out = NULL; /* safety */ |
| 4113 | |
| 4114 | /* Now clear everything out for safety; the following may not have been |
| 4115 | * done. |
| 4116 | */ |
| 4117 | if (!(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED)) |
| 4118 | { |
| 4119 | png_ptr->mode |= PNG_AFTER_IDAT; |
| 4120 | png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED; |
| 4121 | } |
| 4122 | } |
| 4123 | |
| 4124 | /* If the zstream has not been released do it now *and* terminate the reading |
| 4125 | * of the final IDAT chunk. |
| 4126 | */ |
| 4127 | if (png_ptr->zowner == png_IDAT) |
| 4128 | { |
| 4129 | /* Always do this; the pointers otherwise point into the read buffer. */ |
| 4130 | png_ptr->zstream.next_in = NULL; |
| 4131 | png_ptr->zstream.avail_in = 0; |
| 4132 | |
| 4133 | /* Now we no longer own the zstream. */ |
| 4134 | png_ptr->zowner = 0; |
| 4135 | |
| 4136 | /* The slightly weird semantics of the sequential IDAT reading is that we |
| 4137 | * are always in or at the end of an IDAT chunk, so we always need to do a |
| 4138 | * crc_finish here. If idat_size is non-zero we also need to read the |
| 4139 | * spurious bytes at the end of the chunk now. |
| 4140 | */ |
| 4141 | (void)png_crc_finish(png_ptr, png_ptr->idat_size); |
| 4142 | } |
| 4143 | } |
| 4144 | |
John Reck | 90bf0d2 | 2013-08-14 10:37:42 -0700 | [diff] [blame] | 4145 | #ifdef PNG_INDEX_SUPPORTED |
| 4146 | void /* PRIVATE */ |
| 4147 | png_set_interlaced_pass(png_structp png_ptr, int pass) |
| 4148 | { |
| 4149 | /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ |
| 4150 | /* Start of interlace block */ |
| 4151 | PNG_CONST int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; |
| 4152 | /* Offset to next interlace block */ |
| 4153 | PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; |
| 4154 | /* Start of interlace block in the y direction */ |
| 4155 | PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1}; |
| 4156 | /* Offset to next interlace block in the y direction */ |
| 4157 | PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; |
| 4158 | png_ptr->pass = pass; |
| 4159 | png_ptr->iwidth = (png_ptr->width + |
| 4160 | png_pass_inc[png_ptr->pass] - 1 - |
| 4161 | png_pass_start[png_ptr->pass]) / |
| 4162 | png_pass_inc[png_ptr->pass]; |
| 4163 | } |
| 4164 | #endif |
| 4165 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4166 | void /* PRIVATE */ |
| 4167 | png_read_finish_row(png_structrp png_ptr) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4168 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4169 | #ifdef PNG_READ_INTERLACING_SUPPORTED |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 4170 | /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4171 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 4172 | /* Start of interlace block */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4173 | static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4174 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 4175 | /* Offset to next interlace block */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4176 | static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4177 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 4178 | /* Start of interlace block in the y direction */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4179 | static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1}; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4180 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 4181 | /* Offset to next interlace block in the y direction */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4182 | static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4183 | #endif /* PNG_READ_INTERLACING_SUPPORTED */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4184 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 4185 | png_debug(1, "in png_read_finish_row"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4186 | png_ptr->row_number++; |
| 4187 | if (png_ptr->row_number < png_ptr->num_rows) |
| 4188 | return; |
| 4189 | |
| 4190 | #ifdef PNG_READ_INTERLACING_SUPPORTED |
| 4191 | if (png_ptr->interlaced) |
| 4192 | { |
| 4193 | png_ptr->row_number = 0; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4194 | |
| 4195 | /* TO DO: don't do this if prev_row isn't needed (requires |
| 4196 | * read-ahead of the next row's filter byte. |
| 4197 | */ |
| 4198 | memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1); |
| 4199 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4200 | do |
| 4201 | { |
| 4202 | png_ptr->pass++; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4203 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4204 | if (png_ptr->pass >= 7) |
| 4205 | break; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4206 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4207 | png_ptr->iwidth = (png_ptr->width + |
| 4208 | png_pass_inc[png_ptr->pass] - 1 - |
| 4209 | png_pass_start[png_ptr->pass]) / |
| 4210 | png_pass_inc[png_ptr->pass]; |
| 4211 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4212 | if (!(png_ptr->transformations & PNG_INTERLACE)) |
| 4213 | { |
| 4214 | png_ptr->num_rows = (png_ptr->height + |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4215 | png_pass_yinc[png_ptr->pass] - 1 - |
| 4216 | png_pass_ystart[png_ptr->pass]) / |
| 4217 | png_pass_yinc[png_ptr->pass]; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4218 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4219 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4220 | else /* if (png_ptr->transformations & PNG_INTERLACE) */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4221 | break; /* libpng deinterlacing sees every row */ |
| 4222 | |
| 4223 | } while (png_ptr->num_rows == 0 || png_ptr->iwidth == 0); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4224 | |
| 4225 | if (png_ptr->pass < 7) |
| 4226 | return; |
| 4227 | } |
| 4228 | #endif /* PNG_READ_INTERLACING_SUPPORTED */ |
| 4229 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4230 | /* Here after at the end of the last row of the last pass. */ |
| 4231 | png_read_finish_IDAT(png_ptr); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4232 | } |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 4233 | #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4234 | |
| 4235 | void /* PRIVATE */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4236 | png_read_start_row(png_structrp png_ptr) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4237 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4238 | #ifdef PNG_READ_INTERLACING_SUPPORTED |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 4239 | /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4240 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 4241 | /* Start of interlace block */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4242 | static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4243 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 4244 | /* Offset to next interlace block */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4245 | static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4246 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 4247 | /* Start of interlace block in the y direction */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4248 | static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1}; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4249 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 4250 | /* Offset to next interlace block in the y direction */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4251 | static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4252 | #endif |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4253 | |
| 4254 | int max_pixel_depth; |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 4255 | png_size_t row_bytes; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4256 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 4257 | png_debug(1, "in png_read_start_row"); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4258 | |
| 4259 | #ifdef PNG_READ_TRANSFORMS_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4260 | png_init_read_transformations(png_ptr); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4261 | #endif |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4262 | #ifdef PNG_READ_INTERLACING_SUPPORTED |
| 4263 | if (png_ptr->interlaced) |
| 4264 | { |
| 4265 | if (!(png_ptr->transformations & PNG_INTERLACE)) |
| 4266 | png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 - |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4267 | png_pass_ystart[0]) / png_pass_yinc[0]; |
| 4268 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4269 | else |
| 4270 | png_ptr->num_rows = png_ptr->height; |
| 4271 | |
| 4272 | png_ptr->iwidth = (png_ptr->width + |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4273 | png_pass_inc[png_ptr->pass] - 1 - |
| 4274 | png_pass_start[png_ptr->pass]) / |
| 4275 | png_pass_inc[png_ptr->pass]; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4276 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4277 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4278 | else |
| 4279 | #endif /* PNG_READ_INTERLACING_SUPPORTED */ |
| 4280 | { |
| 4281 | png_ptr->num_rows = png_ptr->height; |
| 4282 | png_ptr->iwidth = png_ptr->width; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4283 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4284 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4285 | max_pixel_depth = png_ptr->pixel_depth; |
| 4286 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4287 | /* WARNING: * png_read_transform_info (pngrtran.c) performs a simpliar set of |
| 4288 | * calculations to calculate the final pixel depth, then |
| 4289 | * png_do_read_transforms actually does the transforms. This means that the |
| 4290 | * code which effectively calculates this value is actually repeated in three |
| 4291 | * separate places. They must all match. Innocent changes to the order of |
| 4292 | * transformations can and will break libpng in a way that causes memory |
| 4293 | * overwrites. |
| 4294 | * |
| 4295 | * TODO: fix this. |
| 4296 | */ |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 4297 | #ifdef PNG_READ_PACK_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4298 | if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8) |
| 4299 | max_pixel_depth = 8; |
| 4300 | #endif |
| 4301 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 4302 | #ifdef PNG_READ_EXPAND_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4303 | if (png_ptr->transformations & PNG_EXPAND) |
| 4304 | { |
| 4305 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 4306 | { |
| 4307 | if (png_ptr->num_trans) |
| 4308 | max_pixel_depth = 32; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4309 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4310 | else |
| 4311 | max_pixel_depth = 24; |
| 4312 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4313 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4314 | else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY) |
| 4315 | { |
| 4316 | if (max_pixel_depth < 8) |
| 4317 | max_pixel_depth = 8; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4318 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4319 | if (png_ptr->num_trans) |
| 4320 | max_pixel_depth *= 2; |
| 4321 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4322 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4323 | else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB) |
| 4324 | { |
| 4325 | if (png_ptr->num_trans) |
| 4326 | { |
| 4327 | max_pixel_depth *= 4; |
| 4328 | max_pixel_depth /= 3; |
| 4329 | } |
| 4330 | } |
| 4331 | } |
| 4332 | #endif |
| 4333 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4334 | #ifdef PNG_READ_EXPAND_16_SUPPORTED |
| 4335 | if (png_ptr->transformations & PNG_EXPAND_16) |
| 4336 | { |
| 4337 | # ifdef PNG_READ_EXPAND_SUPPORTED |
| 4338 | /* In fact it is an error if it isn't supported, but checking is |
| 4339 | * the safe way. |
| 4340 | */ |
| 4341 | if (png_ptr->transformations & PNG_EXPAND) |
| 4342 | { |
| 4343 | if (png_ptr->bit_depth < 16) |
| 4344 | max_pixel_depth *= 2; |
| 4345 | } |
| 4346 | else |
| 4347 | # endif |
| 4348 | png_ptr->transformations &= ~PNG_EXPAND_16; |
| 4349 | } |
| 4350 | #endif |
| 4351 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 4352 | #ifdef PNG_READ_FILLER_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4353 | if (png_ptr->transformations & (PNG_FILLER)) |
| 4354 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4355 | if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4356 | { |
| 4357 | if (max_pixel_depth <= 8) |
| 4358 | max_pixel_depth = 16; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4359 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4360 | else |
| 4361 | max_pixel_depth = 32; |
| 4362 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4363 | |
| 4364 | else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB || |
| 4365 | png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4366 | { |
| 4367 | if (max_pixel_depth <= 32) |
| 4368 | max_pixel_depth = 32; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4369 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4370 | else |
| 4371 | max_pixel_depth = 64; |
| 4372 | } |
| 4373 | } |
| 4374 | #endif |
| 4375 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 4376 | #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4377 | if (png_ptr->transformations & PNG_GRAY_TO_RGB) |
| 4378 | { |
| 4379 | if ( |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 4380 | #ifdef PNG_READ_EXPAND_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4381 | (png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) || |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4382 | #endif |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 4383 | #ifdef PNG_READ_FILLER_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4384 | (png_ptr->transformations & (PNG_FILLER)) || |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4385 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4386 | png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4387 | { |
| 4388 | if (max_pixel_depth <= 16) |
| 4389 | max_pixel_depth = 32; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4390 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4391 | else |
| 4392 | max_pixel_depth = 64; |
| 4393 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4394 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4395 | else |
| 4396 | { |
| 4397 | if (max_pixel_depth <= 8) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4398 | { |
| 4399 | if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4400 | max_pixel_depth = 32; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4401 | |
| 4402 | else |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4403 | max_pixel_depth = 24; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4404 | } |
| 4405 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4406 | else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
| 4407 | max_pixel_depth = 64; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4408 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4409 | else |
| 4410 | max_pixel_depth = 48; |
| 4411 | } |
| 4412 | } |
| 4413 | #endif |
| 4414 | |
| 4415 | #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \ |
| 4416 | defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 4417 | if (png_ptr->transformations & PNG_USER_TRANSFORM) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4418 | { |
| 4419 | int user_pixel_depth = png_ptr->user_transform_depth * |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4420 | png_ptr->user_transform_channels; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4421 | |
| 4422 | if (user_pixel_depth > max_pixel_depth) |
| 4423 | max_pixel_depth = user_pixel_depth; |
| 4424 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4425 | #endif |
| 4426 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4427 | /* This value is stored in png_struct and double checked in the row read |
| 4428 | * code. |
| 4429 | */ |
| 4430 | png_ptr->maximum_pixel_depth = (png_byte)max_pixel_depth; |
| 4431 | png_ptr->transformed_pixel_depth = 0; /* calculated on demand */ |
| 4432 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 4433 | /* Align the width on the next larger 8 pixels. Mainly used |
| 4434 | * for interlacing |
| 4435 | */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4436 | row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7)); |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 4437 | /* Calculate the maximum bytes needed, adding a byte and a pixel |
| 4438 | * for safety's sake |
| 4439 | */ |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 4440 | row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) + |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4441 | 1 + ((max_pixel_depth + 7) >> 3); |
| 4442 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4443 | #ifdef PNG_MAX_MALLOC_64K |
| 4444 | if (row_bytes > (png_uint_32)65536L) |
| 4445 | png_error(png_ptr, "This image requires a row greater than 64KB"); |
| 4446 | #endif |
| 4447 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4448 | if (row_bytes + 48 > png_ptr->old_big_row_buf_size) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4449 | { |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 4450 | png_free(png_ptr, png_ptr->big_row_buf); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4451 | png_free(png_ptr, png_ptr->big_prev_row); |
| 4452 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 4453 | if (png_ptr->interlaced) |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 4454 | png_ptr->big_row_buf = (png_bytep)png_calloc(png_ptr, |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4455 | row_bytes + 48); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 4456 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4457 | else |
| 4458 | png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes + 48); |
| 4459 | |
| 4460 | png_ptr->big_prev_row = (png_bytep)png_malloc(png_ptr, row_bytes + 48); |
| 4461 | |
| 4462 | #ifdef PNG_ALIGNED_MEMORY_SUPPORTED |
| 4463 | /* Use 16-byte aligned memory for row_buf with at least 16 bytes |
| 4464 | * of padding before and after row_buf; treat prev_row similarly. |
| 4465 | * NOTE: the alignment is to the start of the pixels, one beyond the start |
| 4466 | * of the buffer, because of the filter byte. Prior to libpng 1.5.6 this |
| 4467 | * was incorrect; the filter byte was aligned, which had the exact |
| 4468 | * opposite effect of that intended. |
| 4469 | */ |
| 4470 | { |
| 4471 | png_bytep temp = png_ptr->big_row_buf + 32; |
| 4472 | int extra = (int)((temp - (png_bytep)0) & 0x0f); |
| 4473 | png_ptr->row_buf = temp - extra - 1/*filter byte*/; |
| 4474 | |
| 4475 | temp = png_ptr->big_prev_row + 32; |
| 4476 | extra = (int)((temp - (png_bytep)0) & 0x0f); |
| 4477 | png_ptr->prev_row = temp - extra - 1/*filter byte*/; |
| 4478 | } |
| 4479 | |
| 4480 | #else |
| 4481 | /* Use 31 bytes of padding before and 17 bytes after row_buf. */ |
| 4482 | png_ptr->row_buf = png_ptr->big_row_buf + 31; |
| 4483 | png_ptr->prev_row = png_ptr->big_prev_row + 31; |
| 4484 | #endif |
| 4485 | png_ptr->old_big_row_buf_size = row_bytes + 48; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4486 | } |
| 4487 | |
| 4488 | #ifdef PNG_MAX_MALLOC_64K |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4489 | if (png_ptr->rowbytes > 65535) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4490 | png_error(png_ptr, "This image requires a row greater than 64KB"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4491 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4492 | #endif |
| 4493 | if (png_ptr->rowbytes > (PNG_SIZE_MAX - 1)) |
| 4494 | png_error(png_ptr, "Row has too many bytes to allocate in memory"); |
| 4495 | |
| 4496 | memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1); |
| 4497 | |
| 4498 | png_debug1(3, "width = %u,", png_ptr->width); |
| 4499 | png_debug1(3, "height = %u,", png_ptr->height); |
| 4500 | png_debug1(3, "iwidth = %u,", png_ptr->iwidth); |
| 4501 | png_debug1(3, "num_rows = %u,", png_ptr->num_rows); |
| 4502 | png_debug1(3, "rowbytes = %lu,", (unsigned long)png_ptr->rowbytes); |
| 4503 | png_debug1(3, "irowbytes = %lu", |
| 4504 | (unsigned long)PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1); |
| 4505 | |
| 4506 | /* The sequential reader needs a buffer for IDAT, but the progressive reader |
| 4507 | * does not, so free the read buffer now regardless; the sequential reader |
| 4508 | * reallocates it on demand. |
| 4509 | */ |
| 4510 | if (png_ptr->read_buffer) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4511 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4512 | png_bytep buffer = png_ptr->read_buffer; |
| 4513 | |
| 4514 | png_ptr->read_buffer_size = 0; |
| 4515 | png_ptr->read_buffer = NULL; |
| 4516 | png_free(png_ptr, buffer); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4517 | } |
| 4518 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4519 | /* Finally claim the zstream for the inflate of the IDAT data, use the bits |
| 4520 | * value from the stream (note that this will result in a fatal error if the |
| 4521 | * IDAT stream has a bogus deflate header window_bits value, but this should |
| 4522 | * not be happening any longer!) |
| 4523 | */ |
| 4524 | if (png_inflate_claim(png_ptr, png_IDAT) != Z_OK) |
| 4525 | png_error(png_ptr, png_ptr->zstream.msg); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4526 | |
| 4527 | png_ptr->flags |= PNG_FLAG_ROW_INIT; |
| 4528 | } |
| 4529 | #endif /* PNG_READ_SUPPORTED */ |