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