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