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