0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 2362N/A * published by the Free Software Foundation. Oracle designates this 0N/A * particular file as subject to the "Classpath" exception as provided 2362N/A * by Oracle in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 0N/A * This file is available under and governed by the GNU General Public 0N/A * License version 2 only, as published by the Free Software Foundation. 0N/A * However, the following notice accompanied the original version of this 0N/A * file and, per its terms, should not be removed: 4418N/A * Last changed in libpng 1.5.4 [July 7, 2011] 4418N/A * Copyright (c) 1998-2011 Glenn Randers-Pehrson 0N/A * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) 0N/A * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) 4418N/A * This code is released under the libpng license. 4418N/A * For conditions of distribution and use, see the disclaimer 0N/A * This file contains routines that are only called from within 0N/A * libpng itself during the course of reading an image. 4418N/A/* The following is a variation on the above for use with the fixed 4418N/A * point values used for gAMA and cHRM. Instead of png_error it 4418N/A * issues a warning and returns (-1) - an invalid value because both 4418N/A * gAMA and cHRM use *unsigned* integers for fixed point values. 4418N/A /* The caller can turn off the warning by passing NULL. */ 4418N/A/* NOTE: the read macros will obscure these definitions, so that if 4418N/A * PNG_USE_READ_MACROS is set the library will not use them internally, 4418N/A * but the APIs will still be available externally. 4418N/A * The parentheses around "PNGAPI function_name" in the following three 4418N/A * functions are necessary because they allow the macros to co-exist with 4418N/A * these (unused but exported) functions. 0N/A/* Grab an unsigned 32-bit integer from a buffer in big-endian format. */ 0N/A/* Grab a signed 32-bit integer from a buffer in big-endian format. The 4418N/A * data is stored in the PNG file in two's complement format and there 4418N/A * is no guarantee that a 'png_int_32' is exactly 32 bits, therefore 4418N/A * the following code does a two's complement to native conversion. 4418N/A if ((
uval &
0x80000000L) == 0)
/* non-negative */ 0N/A/* Grab an unsigned 16-bit integer from a buffer in big-endian format. */ 4418N/A /* ANSI-C requires an int value to accomodate at least 16 bits so this 4418N/A * works and allows the compiler not to worry about possible narrowing 4418N/A * on 32 bit systems. (Pre-ANSI systems did not make integers smaller 4418N/A#
endif /* PNG_READ_INT_FUNCTIONS_SUPPORTED */ 4418N/A/* Read and check the PNG file signature */ 4418N/A /* Exit if the user application does not expect a signature. */ 4418N/A /* The signature must be serialized in a single I/O call. */ 4418N/A/* Read the chunk header (length + type name). 4418N/A * Put the type name into png_ptr->chunk_name, and return the length. 4418N/A /* Read the length and the chunk name. 4418N/A * This must be performed in a single I/O call. 4418N/A /* Put the chunk name into png_ptr->chunk_name. */ 4418N/A /* Reset the crc and run it over the chunk name. */ 4418N/A /* Check to see if chunk name is valid. */ 0N/A/* Read data, and (optionally) run it through the CRC. */ 0N/A/* Optionally skip data and then check the CRC. Depending on whether we 4418N/A * are reading a ancillary or critical chunk, and how the program has set 4418N/A * things up, we may calculate the CRC on the data and print a message. 4418N/A * Returns '1' if there was a CRC error, '0' otherwise. 0N/A/* Compare the CRC stored in the PNG file with that calculated by libpng from 4418N/A * the data it has read thus far. 4418N/A /* The chunk CRC must be serialized in a single I/O call. */ 4418N/A /* zlib can't necessarily handle more than 65535 bytes at once (i.e. it can't 4418N/A * even necessarily handle 65536 bytes) because the type uInt is "16 bits or 4418N/A * more". Consequently it is necessary to chunk the input to zlib. This 4418N/A * code uses ZLIB_IO_MAX, from pngpriv.h, as the maximum (the maximum value 4418N/A * that can be stored in a uInt.) It is possible to set ZLIB_IO_MAX to a 4418N/A * advantage, because it forces access of the input data to be separated from 4418N/A * at least some of the use by some period of time. 4418N/A /* avail_in is set below from 'size' */ 4418N/A /* The setting of 'avail_in' used to be outside the loop, by setting it 4418N/A * inside it is possible to chunk the input to zlib and simply rely on 4418N/A * zlib to advance the 'next_in' pointer. This allows arbitrary amounts o 4418N/A * data to be passed through zlib at the unavoidable cost of requiring a 4418N/A * window save (memcpy of up to 32768 output bytes) every ZLIB_IO_MAX 4418N/A /* The value is less than ZLIB_IO_MAX so the cast is safe: */ 4418N/A /* Reset the output buffer each time round - we empty it 4418N/A * after every inflate call. 4418N/A /* Termination conditions - always reset the zstream, it 4418N/A * must be left in inflateInit state. 4418N/A /* Now handle the error codes - the API always returns 0 4418N/A * and the error message is dumped into the uncompressed 4418N/A msg =
"Buffer error in compressed datastream";
4418N/A msg =
"Data error in compressed datastream";
4418N/A /* 0 means an error - notice that this code simply ignores 4418N/A * zero length compressed chunks as a result. 0N/A * Decompress trailing data in a chunk. The assumption is that chunkdata 0N/A * points at an allocated area holding the contents of a chunk with a 0N/A * trailing compressed part. What we get back is an allocated area 0N/A * holding the original prefix part and an uncompressed version of the 0N/A * trailing part (the malloc area passed in is freed). 4418N/A /* The caller should guarantee this */ 4418N/A /* The recovery is to delete the chunk. */ 4418N/A /* Now check the limits on this chunk - if the limit fails the 4418N/A * compressed data will be removed, the prefix will remain. 4418N/A /* If the size is zero either there was an error and a message 4418N/A * has already been output (warning) or the size really is zero 4418N/A * and we have nothing to do - the code will exit through the 4418N/A /* Success (maybe) - really uncompress the chunk. */ 4418N/A return;
/* The success return! */ 0N/A else /* if (comp_type != PNG_COMPRESSION_TYPE_BASE) */ 4418N/A /* The recovery is to simply drop the data. */ 4418N/A /* Generic error return - leave the prefix, delete the compressed 4418N/A * data, reallocate the chunkdata to remove the potentially large 4418N/A * amount of compressed data. 4418N/A /* This is an extra zero in the 'uncompressed' part. */ 4418N/A /* Ignore a malloc error here - it is safe. */ 4418N/A#
endif /* PNG_READ_COMPRESSED_TEXT_SUPPORTED */ 4418N/A/* Read and check the IDHR chunk */ 4418N/A /* Set internal variables */ 4418N/A /* Find number of channels */ 4418N/A default:
/* invalid, png_set_IHDR calls png_error */ 4418N/A /* Set up other useful info */ 4418N/A/* Read and check the palette */ 4418N/A "Ignoring PLTE chunk in grayscale PNG");
4418N/A /* Don't depend upon png_color being any order */ 4418N/A /* If we actually need the PLTE chunk (ie for a paletted image), we do 4418N/A * whatever the normal CRC configuration tells us. However, if we 4418N/A * have an RGB image, the PLTE can be considered ancillary, so 4418N/A * we will act as though it is. 0N/A /* If we don't want to use the data from an ancillary chunk, 4418N/A * we have two options: an error abort, or a warning and we 4418N/A * ignore the data in this chunk (which should be OK, since 4418N/A * it's considered ancillary for a RGB or RGBA image). 0N/A /* Otherwise, we (optionally) emit a warning and use the chunk. */ 0N/A /* Should be an error, but we can cope with it */ 4418N/A /* Check for zero gamma or an error. */ 4418N/A "Ignoring gAMA chunk with out of range gamma");
4418N/A "Ignoring incorrect gAMA value @1 when sRGB is also present");
4418N/A#
endif /* PNG_READ_sRGB_SUPPORTED */ 4418N/A /* Gamma correction on read is supported. */ 4418N/A /* And set the 'info' structure members. */ 0N/A /* Should be an error, but we can cope with it */ 0N/A /* Should be an error, but we can cope with it */ 4418N/A "Ignoring incorrect cHRM white(@1,@2) r(@3,@4)g(@5,@6)b(@7,@8) " 4418N/A "when sRGB is also present");
4418N/A#
endif /* PNG_READ_sRGB_SUPPORTED */ 4418N/A /* Store the _white values as default coefficients for the rgb to gray 4418N/A * operation if it is supported. 4418N/A /* png_set_background has not been called, the coefficients must be in 4418N/A * range for the following to work without overflow. 4418N/A /* The y values are chromaticities: Y/X+Y+Z, the weights for the gray 4418N/A * transformation are simply the normalized Y values for red, green and 0N/A /* Should be an error, but we can cope with it */ 4418N/A "Ignoring incorrect gAMA value @1 when sRGB is also present");
0N/A#
endif /* PNG_READ_gAMA_SUPPORTED */ 4418N/A "Ignoring incorrect cHRM value when sRGB is also present");
0N/A#
endif /* PNG_READ_cHRM_SUPPORTED */ 0N/A#
endif /* PNG_READ_sRGB_SUPPORTED */ 0N/A/* Note: this does not properly handle chunks that are > 64K under DOS */ 0N/A /* Should be an error, but we can cope with it */ 4418N/A /* Empty loop to find end of name */ ;
4418N/A /* There should be at least one zero (the compression type byte) 4418N/A * following the separator, and we should be on it 4418N/A /* Compression_type should always be zero */ 0N/A /* Check the profile_size recorded in the first 32 bits of the ICC profile */ 4418N/A /* NOTE: the following guarantees that 'profile_length' fits into 32 bits, 4418N/A * because profile_size is a 32 bit value. 4418N/A /* And the following guarantees that profile_size == profile_length. */ 4418N/A "Ignoring iCCP chunk with declared size = @1 and actual length = @2");
0N/A#
endif /* PNG_READ_iCCP_SUPPORTED */ 0N/A/* Note: this does not properly handle chunks that are > 64K under DOS */ 4418N/A /* WARNING: this may break if size_t is less than 32 bits; it is assumed 4418N/A * that the PNG_MAX_MALLOC_64K test is enabled in this case, but this is a 4418N/A * potential breakage point if the types in pngconf.h aren't exactly right. 4418N/A /* Empty loop to find end of name */ ;
4418N/A /* A sample depth should follow the separator, and we should be on it */ 4418N/A /* This must fit in a png_uint_32 because it is derived from the original 4418N/A * chunk data length (and use 'length', not 'slength' here for clarity - 4418N/A * they are guaranteed to be the same, see the tests above.) 4418N/A /* Integrity-check the data length */ 4418N/A /* Discard all chunk data except the name and stash that */ 0N/A#
endif /* PNG_READ_sPLT_SUPPORTED */ 0N/A /* Should be an error, but we can cope with it. */ 0N/A /* We convert the index value into RGB components so that we can allow 0N/A * arbitrary RGB values for background when we have transparency, and 0N/A * so it is easy to determine the RGB values of the background color 4418N/A * from the info_ptr struct. 4418N/A/* Read the pCAL chunk (described in the PNG Extensions document) */ 0N/A /* We need to have at least 12 bytes after the purpose string 4418N/A * in order to get the parameter information. 0N/A /* Check that we have the right number of parameters for known 0N/A /* Empty loop to move past the units string. */ ;
0N/A /* Get pointers to the start of each parameter string. */ 0N/A buf++;
/* Skip the null string terminator from previous parameter. */ 0N/A /* Empty loop to move past each parameter string */ ;
0N/A /* Make sure we haven't run out of data yet */ 4418N/A /* Need unit type, width, \0, height: minimum 4 bytes */ 4418N/A /* Validate the ASCII numbers, need two ASCII numbers separated by 4418N/A * a '\0' and they need to fit exactly in the chunk data. 4418N/A "Invalid sCAL chunk ignored: non-positive height");
4418N/A /* This is the (only) success case. */ 4418N/A /* Clean up - just free the temporarily allocated buffer. */ 0N/A/* Note: this does not properly handle chunks that are > 64K under DOS */ 4418N/A /* Empty loop to find end of key */ ;
4418N/A/* Note: this does not correctly handle chunks that are > 64K under DOS */ 0N/A /* We will no doubt have problems with chunks even half this size, but 4418N/A * there is no hard and fast rule to tell us where to stop. 0N/A /* zTXt must have some text after the chunkdataword */ 4418N/A/* Note: this does not correctly handle chunks that are > 64K under DOS */ 0N/A /* We will no doubt have problems with chunks even half this size, but 4418N/A * there is no hard and fast rule to tell us where to stop. 0N/A /* iTXt must have a language tag (possibly empty), two compression bytes, 4418N/A * translated keyword (possibly empty), and possibly some text after the 0N/A/* This function is called when we haven't found a handler for a 4418N/A * chunk. If there isn't a problem with the chunk itself (ie bad 4418N/A * chunk name, CRC, or a critical chunk), the chunk is silently ignored 4418N/A * -- unless the PNG_FLAG_UNKNOWN_CHUNKS_SUPPORTED flag is on in which 4418N/A * case it will be saved away to be written out later. 4418N/A /* Callback to user unknown chunk handler */ 0N/A/* This function is called to verify that a chunk name is valid. 4418N/A * This function can't have the "critical chunk check" incorporated 4418N/A * into it, since in the future we will need to be able to call user 4418N/A * functions to handle unknown critical chunks after we check that 4418N/A * the chunk name itself is valid. 0N/A#
define isnonalpha(c) ((c) <
65 || (c) >
122 || ((c) >
90 && (c) <
97))
0N/A/* Combines the row recently read in with the existing pixels in the 4418N/A * row. This routine takes care of alpha and transparency if requested. 4418N/A * This routine also handles the two methods of progressive display 4418N/A * of interlaced images, depending on the mask value. 4418N/A * The mask value describes which pixels are to be combined with 4418N/A * the row. The pattern always repeats every 8 pixels, so just 8 4418N/A * bits are needed. A one indicates the pixel is to be combined, 4418N/A * a zero indicates the pixel is to be skipped. This is in addition 4418N/A * to any alpha or transparency value associated with the pixel. If 4418N/A * you want all pixels to be combined, pass 0xff (255) in mask. 4418N/A /* Added in 1.5.4: the row_info should match the information returned by any 4418N/A * call to png_read_update_info at this point. Do not continue if we got 4418N/A /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ 4418N/A /* Offset to next interlace block */ 0N/A#
endif /* PNG_READ_INTERLACING_SUPPORTED */ 4418N/A for (i = 0; i <
istop; i++)
/* Use leftover rp,pp */ 0N/A if (pa <= pb && pa <= pc) 4418N/A /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ 4418N/A /* Start of interlace block */ 4418N/A /* Offset to next interlace block */ 4418N/A /* Start of interlace block in the y direction */ 4418N/A /* Offset to next interlace block in the y direction */ 4418N/A#
endif /* PNG_READ_INTERLACING_SUPPORTED */ 0N/A else /* if (png_ptr->transformations & PNG_INTERLACE) */ 4418N/A break;
/* libpng deinterlacing sees every row */ 4418N/A#
endif /* PNG_READ_INTERLACING_SUPPORTED */ 4418N/A#
endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ 4418N/A /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ 4418N/A /* Start of interlace block */ 4418N/A /* Offset to next interlace block */ 4418N/A /* Start of interlace block in the y direction */ 4418N/A /* Offset to next interlace block in the y direction */ 4418N/A#
endif /* PNG_READ_INTERLACING_SUPPORTED */ 4418N/A /* In fact it is an error if it isn't supported, but checking is 4418N/A /* Align the width on the next larger 8 pixels. Mainly used 4418N/A /* Calculate the maximum bytes needed, adding a byte and a pixel 4418N/A /* Use 16-byte aligned memory for row_buf with at least 16 bytes 4418N/A * of padding before and after row_buf. 4418N/A /* Use 32 bytes of padding before and 16 bytes after row_buf. */ 0N/A#
endif /* PNG_READ_SUPPORTED */