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