michael@0: michael@0: /* pngrutil.c - utilities to read a PNG file michael@0: * michael@0: * Last changed in libpng 1.6.10 [March 6, 2014] michael@0: * Copyright (c) 1998-2014 Glenn Randers-Pehrson michael@0: * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) michael@0: * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) michael@0: * michael@0: * This code is released under the libpng license. michael@0: * For conditions of distribution and use, see the disclaimer michael@0: * and license in png.h michael@0: * michael@0: * This file contains routines that are only called from within michael@0: * libpng itself during the course of reading an image. michael@0: */ michael@0: michael@0: #include "pngpriv.h" michael@0: michael@0: #ifdef PNG_READ_SUPPORTED michael@0: michael@0: png_uint_32 PNGAPI michael@0: png_get_uint_31(png_const_structrp png_ptr, png_const_bytep buf) michael@0: { michael@0: png_uint_32 uval = png_get_uint_32(buf); michael@0: michael@0: if (uval > PNG_UINT_31_MAX) michael@0: png_error(png_ptr, "PNG unsigned integer out of range"); michael@0: michael@0: return (uval); michael@0: } michael@0: michael@0: #if defined(PNG_READ_gAMA_SUPPORTED) || defined(PNG_READ_cHRM_SUPPORTED) michael@0: /* The following is a variation on the above for use with the fixed michael@0: * point values used for gAMA and cHRM. Instead of png_error it michael@0: * issues a warning and returns (-1) - an invalid value because both michael@0: * gAMA and cHRM use *unsigned* integers for fixed point values. michael@0: */ michael@0: #define PNG_FIXED_ERROR (-1) michael@0: michael@0: static png_fixed_point /* PRIVATE */ michael@0: png_get_fixed_point(png_structrp png_ptr, png_const_bytep buf) michael@0: { michael@0: png_uint_32 uval = png_get_uint_32(buf); michael@0: michael@0: if (uval <= PNG_UINT_31_MAX) michael@0: return (png_fixed_point)uval; /* known to be in range */ michael@0: michael@0: /* The caller can turn off the warning by passing NULL. */ michael@0: if (png_ptr != NULL) michael@0: png_warning(png_ptr, "PNG fixed point integer out of range"); michael@0: michael@0: return PNG_FIXED_ERROR; michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_INT_FUNCTIONS_SUPPORTED michael@0: /* NOTE: the read macros will obscure these definitions, so that if michael@0: * PNG_USE_READ_MACROS is set the library will not use them internally, michael@0: * but the APIs will still be available externally. michael@0: * michael@0: * The parentheses around "PNGAPI function_name" in the following three michael@0: * functions are necessary because they allow the macros to co-exist with michael@0: * these (unused but exported) functions. michael@0: */ michael@0: michael@0: /* Grab an unsigned 32-bit integer from a buffer in big-endian format. */ michael@0: png_uint_32 (PNGAPI michael@0: png_get_uint_32)(png_const_bytep buf) michael@0: { michael@0: png_uint_32 uval = michael@0: ((png_uint_32)(*(buf )) << 24) + michael@0: ((png_uint_32)(*(buf + 1)) << 16) + michael@0: ((png_uint_32)(*(buf + 2)) << 8) + michael@0: ((png_uint_32)(*(buf + 3)) ) ; michael@0: michael@0: return uval; michael@0: } michael@0: michael@0: /* Grab a signed 32-bit integer from a buffer in big-endian format. The michael@0: * data is stored in the PNG file in two's complement format and there michael@0: * is no guarantee that a 'png_int_32' is exactly 32 bits, therefore michael@0: * the following code does a two's complement to native conversion. michael@0: */ michael@0: png_int_32 (PNGAPI michael@0: png_get_int_32)(png_const_bytep buf) michael@0: { michael@0: png_uint_32 uval = png_get_uint_32(buf); michael@0: if ((uval & 0x80000000) == 0) /* non-negative */ michael@0: return uval; michael@0: michael@0: uval = (uval ^ 0xffffffff) + 1; /* 2's complement: -x = ~x+1 */ michael@0: return -(png_int_32)uval; michael@0: } michael@0: michael@0: /* Grab an unsigned 16-bit integer from a buffer in big-endian format. */ michael@0: png_uint_16 (PNGAPI michael@0: png_get_uint_16)(png_const_bytep buf) michael@0: { michael@0: /* ANSI-C requires an int value to accomodate at least 16 bits so this michael@0: * works and allows the compiler not to worry about possible narrowing michael@0: * on 32 bit systems. (Pre-ANSI systems did not make integers smaller michael@0: * than 16 bits either.) michael@0: */ michael@0: unsigned int val = michael@0: ((unsigned int)(*buf) << 8) + michael@0: ((unsigned int)(*(buf + 1))); michael@0: michael@0: return (png_uint_16)val; michael@0: } michael@0: michael@0: #endif /* PNG_READ_INT_FUNCTIONS_SUPPORTED */ michael@0: michael@0: /* Read and check the PNG file signature */ michael@0: void /* PRIVATE */ michael@0: png_read_sig(png_structrp png_ptr, png_inforp info_ptr) michael@0: { michael@0: png_size_t num_checked, num_to_check; michael@0: michael@0: /* Exit if the user application does not expect a signature. */ michael@0: if (png_ptr->sig_bytes >= 8) michael@0: return; michael@0: michael@0: num_checked = png_ptr->sig_bytes; michael@0: num_to_check = 8 - num_checked; michael@0: michael@0: #ifdef PNG_IO_STATE_SUPPORTED michael@0: png_ptr->io_state = PNG_IO_READING | PNG_IO_SIGNATURE; michael@0: #endif michael@0: michael@0: /* The signature must be serialized in a single I/O call. */ michael@0: png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check); michael@0: png_ptr->sig_bytes = 8; michael@0: michael@0: if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check)) michael@0: { michael@0: if (num_checked < 4 && michael@0: png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4)) michael@0: png_error(png_ptr, "Not a PNG file"); michael@0: else michael@0: png_error(png_ptr, "PNG file corrupted by ASCII conversion"); michael@0: } michael@0: if (num_checked < 3) michael@0: png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE; michael@0: } michael@0: michael@0: /* Read the chunk header (length + type name). michael@0: * Put the type name into png_ptr->chunk_name, and return the length. michael@0: */ michael@0: png_uint_32 /* PRIVATE */ michael@0: png_read_chunk_header(png_structrp png_ptr) michael@0: { michael@0: png_byte buf[8]; michael@0: png_uint_32 length; michael@0: michael@0: #ifdef PNG_IO_STATE_SUPPORTED michael@0: png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_HDR; michael@0: #endif michael@0: michael@0: /* Read the length and the chunk name. michael@0: * This must be performed in a single I/O call. michael@0: */ michael@0: png_read_data(png_ptr, buf, 8); michael@0: length = png_get_uint_31(png_ptr, buf); michael@0: michael@0: /* Put the chunk name into png_ptr->chunk_name. */ michael@0: png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(buf+4); michael@0: michael@0: png_debug2(0, "Reading %lx chunk, length = %lu", michael@0: (unsigned long)png_ptr->chunk_name, (unsigned long)length); michael@0: michael@0: /* Reset the crc and run it over the chunk name. */ michael@0: png_reset_crc(png_ptr); michael@0: png_calculate_crc(png_ptr, buf + 4, 4); michael@0: michael@0: /* Check to see if chunk name is valid. */ michael@0: png_check_chunk_name(png_ptr, png_ptr->chunk_name); michael@0: michael@0: #ifdef PNG_IO_STATE_SUPPORTED michael@0: png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_DATA; michael@0: #endif michael@0: michael@0: return length; michael@0: } michael@0: michael@0: /* Read data, and (optionally) run it through the CRC. */ michael@0: void /* PRIVATE */ michael@0: png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length) michael@0: { michael@0: if (png_ptr == NULL) michael@0: return; michael@0: michael@0: png_read_data(png_ptr, buf, length); michael@0: png_calculate_crc(png_ptr, buf, length); michael@0: } michael@0: michael@0: /* Optionally skip data and then check the CRC. Depending on whether we michael@0: * are reading an ancillary or critical chunk, and how the program has set michael@0: * things up, we may calculate the CRC on the data and print a message. michael@0: * Returns '1' if there was a CRC error, '0' otherwise. michael@0: */ michael@0: int /* PRIVATE */ michael@0: png_crc_finish(png_structrp png_ptr, png_uint_32 skip) michael@0: { michael@0: /* The size of the local buffer for inflate is a good guess as to a michael@0: * reasonable size to use for buffering reads from the application. michael@0: */ michael@0: while (skip > 0) michael@0: { michael@0: png_uint_32 len; michael@0: png_byte tmpbuf[PNG_INFLATE_BUF_SIZE]; michael@0: michael@0: len = (sizeof tmpbuf); michael@0: if (len > skip) michael@0: len = skip; michael@0: skip -= len; michael@0: michael@0: png_crc_read(png_ptr, tmpbuf, len); michael@0: } michael@0: michael@0: if (png_crc_error(png_ptr)) michael@0: { michael@0: if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name) ? michael@0: !(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) : michael@0: (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE)) michael@0: { michael@0: png_chunk_warning(png_ptr, "CRC error"); michael@0: } michael@0: michael@0: else michael@0: png_chunk_error(png_ptr, "CRC error"); michael@0: michael@0: return (1); michael@0: } michael@0: michael@0: return (0); michael@0: } michael@0: michael@0: /* Compare the CRC stored in the PNG file with that calculated by libpng from michael@0: * the data it has read thus far. michael@0: */ michael@0: int /* PRIVATE */ michael@0: png_crc_error(png_structrp png_ptr) michael@0: { michael@0: png_byte crc_bytes[4]; michael@0: png_uint_32 crc; michael@0: int need_crc = 1; michael@0: michael@0: if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name)) michael@0: { michael@0: if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) == michael@0: (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN)) michael@0: need_crc = 0; michael@0: } michael@0: michael@0: else /* critical */ michael@0: { michael@0: if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) michael@0: need_crc = 0; michael@0: } michael@0: michael@0: #ifdef PNG_IO_STATE_SUPPORTED michael@0: png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_CRC; michael@0: #endif michael@0: michael@0: /* The chunk CRC must be serialized in a single I/O call. */ michael@0: png_read_data(png_ptr, crc_bytes, 4); michael@0: michael@0: if (need_crc) michael@0: { michael@0: crc = png_get_uint_32(crc_bytes); michael@0: return ((int)(crc != png_ptr->crc)); michael@0: } michael@0: michael@0: else michael@0: return (0); michael@0: } michael@0: michael@0: #if defined(PNG_READ_iCCP_SUPPORTED) || defined(PNG_READ_iTXt_SUPPORTED) ||\ michael@0: defined(PNG_READ_pCAL_SUPPORTED) || defined(PNG_READ_sCAL_SUPPORTED) ||\ michael@0: defined(PNG_READ_sPLT_SUPPORTED) || defined(PNG_READ_tEXt_SUPPORTED) ||\ michael@0: defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_SEQUENTIAL_READ_SUPPORTED) michael@0: /* Manage the read buffer; this simply reallocates the buffer if it is not small michael@0: * enough (or if it is not allocated). The routine returns a pointer to the michael@0: * buffer; if an error occurs and 'warn' is set the routine returns NULL, else michael@0: * it will call png_error (via png_malloc) on failure. (warn == 2 means michael@0: * 'silent'). michael@0: */ michael@0: static png_bytep michael@0: png_read_buffer(png_structrp png_ptr, png_alloc_size_t new_size, int warn) michael@0: { michael@0: png_bytep buffer = png_ptr->read_buffer; michael@0: michael@0: if (buffer != NULL && new_size > png_ptr->read_buffer_size) michael@0: { michael@0: png_ptr->read_buffer = NULL; michael@0: png_ptr->read_buffer = NULL; michael@0: png_ptr->read_buffer_size = 0; michael@0: png_free(png_ptr, buffer); michael@0: buffer = NULL; michael@0: } michael@0: michael@0: if (buffer == NULL) michael@0: { michael@0: buffer = png_voidcast(png_bytep, png_malloc_base(png_ptr, new_size)); michael@0: michael@0: if (buffer != NULL) michael@0: { michael@0: png_ptr->read_buffer = buffer; michael@0: png_ptr->read_buffer_size = new_size; michael@0: } michael@0: michael@0: else if (warn < 2) /* else silent */ michael@0: { michael@0: if (warn) michael@0: png_chunk_warning(png_ptr, "insufficient memory to read chunk"); michael@0: michael@0: else michael@0: png_chunk_error(png_ptr, "insufficient memory to read chunk"); michael@0: } michael@0: } michael@0: michael@0: return buffer; michael@0: } michael@0: #endif /* PNG_READ_iCCP|iTXt|pCAL|sCAL|sPLT|tEXt|zTXt|SEQUENTIAL_READ */ michael@0: michael@0: /* png_inflate_claim: claim the zstream for some nefarious purpose that involves michael@0: * decompression. Returns Z_OK on success, else a zlib error code. It checks michael@0: * the owner but, in final release builds, just issues a warning if some other michael@0: * chunk apparently owns the stream. Prior to release it does a png_error. michael@0: */ michael@0: static int michael@0: png_inflate_claim(png_structrp png_ptr, png_uint_32 owner) michael@0: { michael@0: if (png_ptr->zowner != 0) michael@0: { michael@0: char msg[64]; michael@0: michael@0: PNG_STRING_FROM_CHUNK(msg, png_ptr->zowner); michael@0: /* So the message that results is " using zstream"; this is an michael@0: * internal error, but is very useful for debugging. i18n requirements michael@0: * are minimal. michael@0: */ michael@0: (void)png_safecat(msg, (sizeof msg), 4, " using zstream"); michael@0: # if PNG_LIBPNG_BUILD_BASE_TYPE >= PNG_LIBPNG_BUILD_RC michael@0: png_chunk_warning(png_ptr, msg); michael@0: png_ptr->zowner = 0; michael@0: # else michael@0: png_chunk_error(png_ptr, msg); michael@0: # endif michael@0: } michael@0: michael@0: /* Implementation note: unlike 'png_deflate_claim' this internal function michael@0: * does not take the size of the data as an argument. Some efficiency could michael@0: * be gained by using this when it is known *if* the zlib stream itself does michael@0: * not record the number; however, this is an illusion: the original writer michael@0: * of the PNG may have selected a lower window size, and we really must michael@0: * follow that because, for systems with with limited capabilities, we michael@0: * would otherwise reject the application's attempts to use a smaller window michael@0: * size (zlib doesn't have an interface to say "this or lower"!). michael@0: * michael@0: * inflateReset2 was added to zlib 1.2.4; before this the window could not be michael@0: * reset, therefore it is necessary to always allocate the maximum window michael@0: * size with earlier zlibs just in case later compressed chunks need it. michael@0: */ michael@0: { michael@0: int ret; /* zlib return code */ michael@0: # if PNG_ZLIB_VERNUM >= 0x1240 michael@0: michael@0: # if defined(PNG_SET_OPTION_SUPPORTED) && \ michael@0: defined(PNG_MAXIMUM_INFLATE_WINDOW) michael@0: int window_bits; michael@0: michael@0: if (((png_ptr->options >> PNG_MAXIMUM_INFLATE_WINDOW) & 3) == michael@0: PNG_OPTION_ON) michael@0: window_bits = 15; michael@0: michael@0: else michael@0: window_bits = 0; michael@0: # else michael@0: # define window_bits 0 michael@0: # endif michael@0: # endif michael@0: michael@0: /* Set this for safety, just in case the previous owner left pointers to michael@0: * memory allocations. michael@0: */ michael@0: png_ptr->zstream.next_in = NULL; michael@0: png_ptr->zstream.avail_in = 0; michael@0: png_ptr->zstream.next_out = NULL; michael@0: png_ptr->zstream.avail_out = 0; michael@0: michael@0: if (png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) michael@0: { michael@0: # if PNG_ZLIB_VERNUM < 0x1240 michael@0: ret = inflateReset(&png_ptr->zstream); michael@0: # else michael@0: ret = inflateReset2(&png_ptr->zstream, window_bits); michael@0: # endif michael@0: } michael@0: michael@0: else michael@0: { michael@0: # if PNG_ZLIB_VERNUM < 0x1240 michael@0: ret = inflateInit(&png_ptr->zstream); michael@0: # else michael@0: ret = inflateInit2(&png_ptr->zstream, window_bits); michael@0: # endif michael@0: michael@0: if (ret == Z_OK) michael@0: png_ptr->flags |= PNG_FLAG_ZSTREAM_INITIALIZED; michael@0: } michael@0: michael@0: if (ret == Z_OK) michael@0: png_ptr->zowner = owner; michael@0: michael@0: else michael@0: png_zstream_error(png_ptr, ret); michael@0: michael@0: return ret; michael@0: } michael@0: michael@0: # ifdef window_bits michael@0: # undef window_bits michael@0: # endif michael@0: } michael@0: michael@0: #ifdef PNG_READ_COMPRESSED_TEXT_SUPPORTED michael@0: /* png_inflate now returns zlib error codes including Z_OK and Z_STREAM_END to michael@0: * allow the caller to do multiple calls if required. If the 'finish' flag is michael@0: * set Z_FINISH will be passed to the final inflate() call and Z_STREAM_END must michael@0: * be returned or there has been a problem, otherwise Z_SYNC_FLUSH is used and michael@0: * Z_OK or Z_STREAM_END will be returned on success. michael@0: * michael@0: * The input and output sizes are updated to the actual amounts of data consumed michael@0: * or written, not the amount available (as in a z_stream). The data pointers michael@0: * are not changed, so the next input is (data+input_size) and the next michael@0: * available output is (output+output_size). michael@0: */ michael@0: static int michael@0: png_inflate(png_structrp png_ptr, png_uint_32 owner, int finish, michael@0: /* INPUT: */ png_const_bytep input, png_uint_32p input_size_ptr, michael@0: /* OUTPUT: */ png_bytep output, png_alloc_size_t *output_size_ptr) michael@0: { michael@0: if (png_ptr->zowner == owner) /* Else not claimed */ michael@0: { michael@0: int ret; michael@0: png_alloc_size_t avail_out = *output_size_ptr; michael@0: png_uint_32 avail_in = *input_size_ptr; michael@0: michael@0: /* zlib can't necessarily handle more than 65535 bytes at once (i.e. it michael@0: * can't even necessarily handle 65536 bytes) because the type uInt is michael@0: * "16 bits or more". Consequently it is necessary to chunk the input to michael@0: * zlib. This code uses ZLIB_IO_MAX, from pngpriv.h, as the maximum (the michael@0: * maximum value that can be stored in a uInt.) It is possible to set michael@0: * ZLIB_IO_MAX to a lower value in pngpriv.h and this may sometimes have michael@0: * a performance advantage, because it reduces the amount of data accessed michael@0: * at each step and that may give the OS more time to page it in. michael@0: */ michael@0: png_ptr->zstream.next_in = PNGZ_INPUT_CAST(input); michael@0: /* avail_in and avail_out are set below from 'size' */ michael@0: png_ptr->zstream.avail_in = 0; michael@0: png_ptr->zstream.avail_out = 0; michael@0: michael@0: /* Read directly into the output if it is available (this is set to michael@0: * a local buffer below if output is NULL). michael@0: */ michael@0: if (output != NULL) michael@0: png_ptr->zstream.next_out = output; michael@0: michael@0: do michael@0: { michael@0: uInt avail; michael@0: Byte local_buffer[PNG_INFLATE_BUF_SIZE]; michael@0: michael@0: /* zlib INPUT BUFFER */ michael@0: /* The setting of 'avail_in' used to be outside the loop; by setting it michael@0: * inside it is possible to chunk the input to zlib and simply rely on michael@0: * zlib to advance the 'next_in' pointer. This allows arbitrary michael@0: * amounts of data to be passed through zlib at the unavoidable cost of michael@0: * requiring a window save (memcpy of up to 32768 output bytes) michael@0: * every ZLIB_IO_MAX input bytes. michael@0: */ michael@0: avail_in += png_ptr->zstream.avail_in; /* not consumed last time */ michael@0: michael@0: avail = ZLIB_IO_MAX; michael@0: michael@0: if (avail_in < avail) michael@0: avail = (uInt)avail_in; /* safe: < than ZLIB_IO_MAX */ michael@0: michael@0: avail_in -= avail; michael@0: png_ptr->zstream.avail_in = avail; michael@0: michael@0: /* zlib OUTPUT BUFFER */ michael@0: avail_out += png_ptr->zstream.avail_out; /* not written last time */ michael@0: michael@0: avail = ZLIB_IO_MAX; /* maximum zlib can process */ michael@0: michael@0: if (output == NULL) michael@0: { michael@0: /* Reset the output buffer each time round if output is NULL and michael@0: * make available the full buffer, up to 'remaining_space' michael@0: */ michael@0: png_ptr->zstream.next_out = local_buffer; michael@0: if ((sizeof local_buffer) < avail) michael@0: avail = (sizeof local_buffer); michael@0: } michael@0: michael@0: if (avail_out < avail) michael@0: avail = (uInt)avail_out; /* safe: < ZLIB_IO_MAX */ michael@0: michael@0: png_ptr->zstream.avail_out = avail; michael@0: avail_out -= avail; michael@0: michael@0: /* zlib inflate call */ michael@0: /* In fact 'avail_out' may be 0 at this point, that happens at the end michael@0: * of the read when the final LZ end code was not passed at the end of michael@0: * the previous chunk of input data. Tell zlib if we have reached the michael@0: * end of the output buffer. michael@0: */ michael@0: ret = inflate(&png_ptr->zstream, avail_out > 0 ? Z_NO_FLUSH : michael@0: (finish ? Z_FINISH : Z_SYNC_FLUSH)); michael@0: } while (ret == Z_OK); michael@0: michael@0: /* For safety kill the local buffer pointer now */ michael@0: if (output == NULL) michael@0: png_ptr->zstream.next_out = NULL; michael@0: michael@0: /* Claw back the 'size' and 'remaining_space' byte counts. */ michael@0: avail_in += png_ptr->zstream.avail_in; michael@0: avail_out += png_ptr->zstream.avail_out; michael@0: michael@0: /* Update the input and output sizes; the updated values are the amount michael@0: * consumed or written, effectively the inverse of what zlib uses. michael@0: */ michael@0: if (avail_out > 0) michael@0: *output_size_ptr -= avail_out; michael@0: michael@0: if (avail_in > 0) michael@0: *input_size_ptr -= avail_in; michael@0: michael@0: /* Ensure png_ptr->zstream.msg is set (even in the success case!) */ michael@0: png_zstream_error(png_ptr, ret); michael@0: return ret; michael@0: } michael@0: michael@0: else michael@0: { michael@0: /* This is a bad internal error. The recovery assigns to the zstream msg michael@0: * pointer, which is not owned by the caller, but this is safe; it's only michael@0: * used on errors! michael@0: */ michael@0: png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed"); michael@0: return Z_STREAM_ERROR; michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * Decompress trailing data in a chunk. The assumption is that read_buffer michael@0: * points at an allocated area holding the contents of a chunk with a michael@0: * trailing compressed part. What we get back is an allocated area michael@0: * holding the original prefix part and an uncompressed version of the michael@0: * trailing part (the malloc area passed in is freed). michael@0: */ michael@0: static int michael@0: png_decompress_chunk(png_structrp png_ptr, michael@0: png_uint_32 chunklength, png_uint_32 prefix_size, michael@0: png_alloc_size_t *newlength /* must be initialized to the maximum! */, michael@0: int terminate /*add a '\0' to the end of the uncompressed data*/) michael@0: { michael@0: /* TODO: implement different limits for different types of chunk. michael@0: * michael@0: * The caller supplies *newlength set to the maximum length of the michael@0: * uncompressed data, but this routine allocates space for the prefix and michael@0: * maybe a '\0' terminator too. We have to assume that 'prefix_size' is michael@0: * limited only by the maximum chunk size. michael@0: */ michael@0: png_alloc_size_t limit = PNG_SIZE_MAX; michael@0: michael@0: # ifdef PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED michael@0: if (png_ptr->user_chunk_malloc_max > 0 && michael@0: png_ptr->user_chunk_malloc_max < limit) michael@0: limit = png_ptr->user_chunk_malloc_max; michael@0: # elif PNG_USER_CHUNK_MALLOC_MAX > 0 michael@0: if (PNG_USER_CHUNK_MALLOC_MAX < limit) michael@0: limit = PNG_USER_CHUNK_MALLOC_MAX; michael@0: # endif michael@0: michael@0: if (limit >= prefix_size + (terminate != 0)) michael@0: { michael@0: int ret; michael@0: michael@0: limit -= prefix_size + (terminate != 0); michael@0: michael@0: if (limit < *newlength) michael@0: *newlength = limit; michael@0: michael@0: /* Now try to claim the stream. */ michael@0: ret = png_inflate_claim(png_ptr, png_ptr->chunk_name); michael@0: michael@0: if (ret == Z_OK) michael@0: { michael@0: png_uint_32 lzsize = chunklength - prefix_size; michael@0: michael@0: ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/, michael@0: /* input: */ png_ptr->read_buffer + prefix_size, &lzsize, michael@0: /* output: */ NULL, newlength); michael@0: michael@0: if (ret == Z_STREAM_END) michael@0: { michael@0: /* Use 'inflateReset' here, not 'inflateReset2' because this michael@0: * preserves the previously decided window size (otherwise it would michael@0: * be necessary to store the previous window size.) In practice michael@0: * this doesn't matter anyway, because png_inflate will call inflate michael@0: * with Z_FINISH in almost all cases, so the window will not be michael@0: * maintained. michael@0: */ michael@0: if (inflateReset(&png_ptr->zstream) == Z_OK) michael@0: { michael@0: /* Because of the limit checks above we know that the new, michael@0: * expanded, size will fit in a size_t (let alone an michael@0: * png_alloc_size_t). Use png_malloc_base here to avoid an michael@0: * extra OOM message. michael@0: */ michael@0: png_alloc_size_t new_size = *newlength; michael@0: png_alloc_size_t buffer_size = prefix_size + new_size + michael@0: (terminate != 0); michael@0: png_bytep text = png_voidcast(png_bytep, png_malloc_base(png_ptr, michael@0: buffer_size)); michael@0: michael@0: if (text != NULL) michael@0: { michael@0: ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/, michael@0: png_ptr->read_buffer + prefix_size, &lzsize, michael@0: text + prefix_size, newlength); michael@0: michael@0: if (ret == Z_STREAM_END) michael@0: { michael@0: if (new_size == *newlength) michael@0: { michael@0: if (terminate) michael@0: text[prefix_size + *newlength] = 0; michael@0: michael@0: if (prefix_size > 0) michael@0: memcpy(text, png_ptr->read_buffer, prefix_size); michael@0: michael@0: { michael@0: png_bytep old_ptr = png_ptr->read_buffer; michael@0: michael@0: png_ptr->read_buffer = text; michael@0: png_ptr->read_buffer_size = buffer_size; michael@0: text = old_ptr; /* freed below */ michael@0: } michael@0: } michael@0: michael@0: else michael@0: { michael@0: /* The size changed on the second read, there can be no michael@0: * guarantee that anything is correct at this point. michael@0: * The 'msg' pointer has been set to "unexpected end of michael@0: * LZ stream", which is fine, but return an error code michael@0: * that the caller won't accept. michael@0: */ michael@0: ret = PNG_UNEXPECTED_ZLIB_RETURN; michael@0: } michael@0: } michael@0: michael@0: else if (ret == Z_OK) michael@0: ret = PNG_UNEXPECTED_ZLIB_RETURN; /* for safety */ michael@0: michael@0: /* Free the text pointer (this is the old read_buffer on michael@0: * success) michael@0: */ michael@0: png_free(png_ptr, text); michael@0: michael@0: /* This really is very benign, but it's still an error because michael@0: * the extra space may otherwise be used as a Trojan Horse. michael@0: */ michael@0: if (ret == Z_STREAM_END && michael@0: chunklength - prefix_size != lzsize) michael@0: png_chunk_benign_error(png_ptr, "extra compressed data"); michael@0: } michael@0: michael@0: else michael@0: { michael@0: /* Out of memory allocating the buffer */ michael@0: ret = Z_MEM_ERROR; michael@0: png_zstream_error(png_ptr, Z_MEM_ERROR); michael@0: } michael@0: } michael@0: michael@0: else michael@0: { michael@0: /* inflateReset failed, store the error message */ michael@0: png_zstream_error(png_ptr, ret); michael@0: michael@0: if (ret == Z_STREAM_END) michael@0: ret = PNG_UNEXPECTED_ZLIB_RETURN; michael@0: } michael@0: } michael@0: michael@0: else if (ret == Z_OK) michael@0: ret = PNG_UNEXPECTED_ZLIB_RETURN; michael@0: michael@0: /* Release the claimed stream */ michael@0: png_ptr->zowner = 0; michael@0: } michael@0: michael@0: else /* the claim failed */ if (ret == Z_STREAM_END) /* impossible! */ michael@0: ret = PNG_UNEXPECTED_ZLIB_RETURN; michael@0: michael@0: return ret; michael@0: } michael@0: michael@0: else michael@0: { michael@0: /* Application/configuration limits exceeded */ michael@0: png_zstream_error(png_ptr, Z_MEM_ERROR); michael@0: return Z_MEM_ERROR; michael@0: } michael@0: } michael@0: #endif /* PNG_READ_COMPRESSED_TEXT_SUPPORTED */ michael@0: michael@0: #ifdef PNG_READ_iCCP_SUPPORTED michael@0: /* Perform a partial read and decompress, producing 'avail_out' bytes and michael@0: * reading from the current chunk as required. michael@0: */ michael@0: static int michael@0: png_inflate_read(png_structrp png_ptr, png_bytep read_buffer, uInt read_size, michael@0: png_uint_32p chunk_bytes, png_bytep next_out, png_alloc_size_t *out_size, michael@0: int finish) michael@0: { michael@0: if (png_ptr->zowner == png_ptr->chunk_name) michael@0: { michael@0: int ret; michael@0: michael@0: /* next_in and avail_in must have been initialized by the caller. */ michael@0: png_ptr->zstream.next_out = next_out; michael@0: png_ptr->zstream.avail_out = 0; /* set in the loop */ michael@0: michael@0: do michael@0: { michael@0: if (png_ptr->zstream.avail_in == 0) michael@0: { michael@0: if (read_size > *chunk_bytes) michael@0: read_size = (uInt)*chunk_bytes; michael@0: *chunk_bytes -= read_size; michael@0: michael@0: if (read_size > 0) michael@0: png_crc_read(png_ptr, read_buffer, read_size); michael@0: michael@0: png_ptr->zstream.next_in = read_buffer; michael@0: png_ptr->zstream.avail_in = read_size; michael@0: } michael@0: michael@0: if (png_ptr->zstream.avail_out == 0) michael@0: { michael@0: uInt avail = ZLIB_IO_MAX; michael@0: if (avail > *out_size) michael@0: avail = (uInt)*out_size; michael@0: *out_size -= avail; michael@0: michael@0: png_ptr->zstream.avail_out = avail; michael@0: } michael@0: michael@0: /* Use Z_SYNC_FLUSH when there is no more chunk data to ensure that all michael@0: * the available output is produced; this allows reading of truncated michael@0: * streams. michael@0: */ michael@0: ret = inflate(&png_ptr->zstream, michael@0: *chunk_bytes > 0 ? Z_NO_FLUSH : (finish ? Z_FINISH : Z_SYNC_FLUSH)); michael@0: } michael@0: while (ret == Z_OK && (*out_size > 0 || png_ptr->zstream.avail_out > 0)); michael@0: michael@0: *out_size += png_ptr->zstream.avail_out; michael@0: png_ptr->zstream.avail_out = 0; /* Should not be required, but is safe */ michael@0: michael@0: /* Ensure the error message pointer is always set: */ michael@0: png_zstream_error(png_ptr, ret); michael@0: return ret; michael@0: } michael@0: michael@0: else michael@0: { michael@0: png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed"); michael@0: return Z_STREAM_ERROR; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: /* Read and check the IDHR chunk */ michael@0: void /* PRIVATE */ michael@0: png_handle_IHDR(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) michael@0: { michael@0: png_byte buf[13]; michael@0: png_uint_32 width, height; michael@0: int bit_depth, color_type, compression_type, filter_type; michael@0: int interlace_type; michael@0: michael@0: png_debug(1, "in png_handle_IHDR"); michael@0: michael@0: if (png_ptr->mode & PNG_HAVE_IHDR) michael@0: png_chunk_error(png_ptr, "out of place"); michael@0: michael@0: /* Check the length */ michael@0: if (length != 13) michael@0: png_chunk_error(png_ptr, "invalid"); michael@0: michael@0: png_ptr->mode |= PNG_HAVE_IHDR; michael@0: michael@0: png_crc_read(png_ptr, buf, 13); michael@0: png_crc_finish(png_ptr, 0); michael@0: michael@0: width = png_get_uint_31(png_ptr, buf); michael@0: height = png_get_uint_31(png_ptr, buf + 4); michael@0: bit_depth = buf[8]; michael@0: color_type = buf[9]; michael@0: compression_type = buf[10]; michael@0: filter_type = buf[11]; michael@0: interlace_type = buf[12]; michael@0: michael@0: #ifdef PNG_READ_APNG_SUPPORTED michael@0: png_ptr->first_frame_width = width; michael@0: png_ptr->first_frame_height = height; michael@0: #endif michael@0: michael@0: /* Set internal variables */ michael@0: png_ptr->width = width; michael@0: png_ptr->height = height; michael@0: png_ptr->bit_depth = (png_byte)bit_depth; michael@0: png_ptr->interlaced = (png_byte)interlace_type; michael@0: png_ptr->color_type = (png_byte)color_type; michael@0: #ifdef PNG_MNG_FEATURES_SUPPORTED michael@0: png_ptr->filter_type = (png_byte)filter_type; michael@0: #endif michael@0: png_ptr->compression_type = (png_byte)compression_type; michael@0: michael@0: /* Find number of channels */ michael@0: switch (png_ptr->color_type) michael@0: { michael@0: default: /* invalid, png_set_IHDR calls png_error */ michael@0: case PNG_COLOR_TYPE_GRAY: michael@0: case PNG_COLOR_TYPE_PALETTE: michael@0: png_ptr->channels = 1; michael@0: break; michael@0: michael@0: case PNG_COLOR_TYPE_RGB: michael@0: png_ptr->channels = 3; michael@0: break; michael@0: michael@0: case PNG_COLOR_TYPE_GRAY_ALPHA: michael@0: png_ptr->channels = 2; michael@0: break; michael@0: michael@0: case PNG_COLOR_TYPE_RGB_ALPHA: michael@0: png_ptr->channels = 4; michael@0: break; michael@0: } michael@0: michael@0: /* Set up other useful info */ michael@0: png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth * michael@0: png_ptr->channels); michael@0: png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width); michael@0: png_debug1(3, "bit_depth = %d", png_ptr->bit_depth); michael@0: png_debug1(3, "channels = %d", png_ptr->channels); michael@0: png_debug1(3, "rowbytes = %lu", (unsigned long)png_ptr->rowbytes); michael@0: png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, michael@0: color_type, interlace_type, compression_type, filter_type); michael@0: } michael@0: michael@0: /* Read and check the palette */ michael@0: void /* PRIVATE */ michael@0: png_handle_PLTE(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) michael@0: { michael@0: png_color palette[PNG_MAX_PALETTE_LENGTH]; michael@0: int num, i; michael@0: #ifdef PNG_POINTER_INDEXING_SUPPORTED michael@0: png_colorp pal_ptr; michael@0: #endif michael@0: michael@0: png_debug(1, "in png_handle_PLTE"); michael@0: michael@0: if (!(png_ptr->mode & PNG_HAVE_IHDR)) michael@0: png_chunk_error(png_ptr, "missing IHDR"); michael@0: michael@0: /* Moved to before the 'after IDAT' check below because otherwise duplicate michael@0: * PLTE chunks are potentially ignored (the spec says there shall not be more michael@0: * than one PLTE, the error is not treated as benign, so this check trumps michael@0: * the requirement that PLTE appears before IDAT.) michael@0: */ michael@0: else if (png_ptr->mode & PNG_HAVE_PLTE) michael@0: png_chunk_error(png_ptr, "duplicate"); michael@0: michael@0: else if (png_ptr->mode & PNG_HAVE_IDAT) michael@0: { michael@0: /* This is benign because the non-benign error happened before, when an michael@0: * IDAT was encountered in a color-mapped image with no PLTE. michael@0: */ michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "out of place"); michael@0: return; michael@0: } michael@0: michael@0: png_ptr->mode |= PNG_HAVE_PLTE; michael@0: michael@0: if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "ignored in grayscale PNG"); michael@0: return; michael@0: } michael@0: michael@0: #ifndef PNG_READ_OPT_PLTE_SUPPORTED michael@0: if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: return; michael@0: } michael@0: #endif michael@0: michael@0: if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: michael@0: if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE) michael@0: png_chunk_benign_error(png_ptr, "invalid"); michael@0: michael@0: else michael@0: png_chunk_error(png_ptr, "invalid"); michael@0: michael@0: return; michael@0: } michael@0: michael@0: /* The cast is safe because 'length' is less than 3*PNG_MAX_PALETTE_LENGTH */ michael@0: num = (int)length / 3; michael@0: michael@0: #ifdef PNG_POINTER_INDEXING_SUPPORTED michael@0: for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++) michael@0: { michael@0: png_byte buf[3]; michael@0: michael@0: png_crc_read(png_ptr, buf, 3); michael@0: pal_ptr->red = buf[0]; michael@0: pal_ptr->green = buf[1]; michael@0: pal_ptr->blue = buf[2]; michael@0: } michael@0: #else michael@0: for (i = 0; i < num; i++) michael@0: { michael@0: png_byte buf[3]; michael@0: michael@0: png_crc_read(png_ptr, buf, 3); michael@0: /* Don't depend upon png_color being any order */ michael@0: palette[i].red = buf[0]; michael@0: palette[i].green = buf[1]; michael@0: palette[i].blue = buf[2]; michael@0: } michael@0: #endif michael@0: michael@0: /* If we actually need the PLTE chunk (ie for a paletted image), we do michael@0: * whatever the normal CRC configuration tells us. However, if we michael@0: * have an RGB image, the PLTE can be considered ancillary, so michael@0: * we will act as though it is. michael@0: */ michael@0: #ifndef PNG_READ_OPT_PLTE_SUPPORTED michael@0: if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) michael@0: #endif michael@0: { michael@0: png_crc_finish(png_ptr, 0); michael@0: } michael@0: michael@0: #ifndef PNG_READ_OPT_PLTE_SUPPORTED michael@0: else if (png_crc_error(png_ptr)) /* Only if we have a CRC error */ michael@0: { michael@0: /* If we don't want to use the data from an ancillary chunk, michael@0: * we have two options: an error abort, or a warning and we michael@0: * ignore the data in this chunk (which should be OK, since michael@0: * it's considered ancillary for a RGB or RGBA image). michael@0: * michael@0: * IMPLEMENTATION NOTE: this is only here because png_crc_finish uses the michael@0: * chunk type to determine whether to check the ancillary or the critical michael@0: * flags. michael@0: */ michael@0: if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE)) michael@0: { michael@0: if (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) michael@0: return; michael@0: michael@0: else michael@0: png_chunk_error(png_ptr, "CRC error"); michael@0: } michael@0: michael@0: /* Otherwise, we (optionally) emit a warning and use the chunk. */ michael@0: else if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)) michael@0: png_chunk_warning(png_ptr, "CRC error"); michael@0: } michael@0: #endif michael@0: michael@0: /* TODO: png_set_PLTE has the side effect of setting png_ptr->palette to its michael@0: * own copy of the palette. This has the side effect that when png_start_row michael@0: * is called (this happens after any call to png_read_update_info) the michael@0: * info_ptr palette gets changed. This is extremely unexpected and michael@0: * confusing. michael@0: * michael@0: * Fix this by not sharing the palette in this way. michael@0: */ michael@0: png_set_PLTE(png_ptr, info_ptr, palette, num); michael@0: michael@0: /* The three chunks, bKGD, hIST and tRNS *must* appear after PLTE and before michael@0: * IDAT. Prior to 1.6.0 this was not checked; instead the code merely michael@0: * checked the apparent validity of a tRNS chunk inserted before PLTE on a michael@0: * palette PNG. 1.6.0 attempts to rigorously follow the standard and michael@0: * therefore does a benign error if the erroneous condition is detected *and* michael@0: * cancels the tRNS if the benign error returns. The alternative is to michael@0: * amend the standard since it would be rather hypocritical of the standards michael@0: * maintainers to ignore it. michael@0: */ michael@0: #ifdef PNG_READ_tRNS_SUPPORTED michael@0: if (png_ptr->num_trans > 0 || michael@0: (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS) != 0)) michael@0: { michael@0: /* Cancel this because otherwise it would be used if the transforms michael@0: * require it. Don't cancel the 'valid' flag because this would prevent michael@0: * detection of duplicate chunks. michael@0: */ michael@0: png_ptr->num_trans = 0; michael@0: michael@0: if (info_ptr != NULL) michael@0: info_ptr->num_trans = 0; michael@0: michael@0: png_chunk_benign_error(png_ptr, "tRNS must be after"); michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_hIST_SUPPORTED michael@0: if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST) != 0) michael@0: png_chunk_benign_error(png_ptr, "hIST must be after"); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_bKGD_SUPPORTED michael@0: if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD) != 0) michael@0: png_chunk_benign_error(png_ptr, "bKGD must be after"); michael@0: #endif michael@0: } michael@0: michael@0: void /* PRIVATE */ michael@0: png_handle_IEND(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) michael@0: { michael@0: png_debug(1, "in png_handle_IEND"); michael@0: michael@0: if (!(png_ptr->mode & PNG_HAVE_IHDR) || !(png_ptr->mode & PNG_HAVE_IDAT)) michael@0: png_chunk_error(png_ptr, "out of place"); michael@0: michael@0: png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND); michael@0: michael@0: png_crc_finish(png_ptr, length); michael@0: michael@0: if (length != 0) michael@0: png_chunk_benign_error(png_ptr, "invalid"); michael@0: michael@0: PNG_UNUSED(info_ptr) michael@0: } michael@0: michael@0: #ifdef PNG_READ_gAMA_SUPPORTED michael@0: void /* PRIVATE */ michael@0: png_handle_gAMA(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) michael@0: { michael@0: png_fixed_point igamma; michael@0: png_byte buf[4]; michael@0: michael@0: png_debug(1, "in png_handle_gAMA"); michael@0: michael@0: if (!(png_ptr->mode & PNG_HAVE_IHDR)) michael@0: png_chunk_error(png_ptr, "missing IHDR"); michael@0: michael@0: else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "out of place"); michael@0: return; michael@0: } michael@0: michael@0: if (length != 4) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "invalid"); michael@0: return; michael@0: } michael@0: michael@0: png_crc_read(png_ptr, buf, 4); michael@0: michael@0: if (png_crc_finish(png_ptr, 0)) michael@0: return; michael@0: michael@0: igamma = png_get_fixed_point(NULL, buf); michael@0: michael@0: png_colorspace_set_gamma(png_ptr, &png_ptr->colorspace, igamma); michael@0: png_colorspace_sync(png_ptr, info_ptr); michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_sBIT_SUPPORTED michael@0: void /* PRIVATE */ michael@0: png_handle_sBIT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) michael@0: { michael@0: unsigned int truelen, i; michael@0: png_byte sample_depth; michael@0: png_byte buf[4]; michael@0: michael@0: png_debug(1, "in png_handle_sBIT"); michael@0: michael@0: if (!(png_ptr->mode & PNG_HAVE_IHDR)) michael@0: png_chunk_error(png_ptr, "missing IHDR"); michael@0: michael@0: else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "out of place"); michael@0: return; michael@0: } michael@0: michael@0: if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT)) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "duplicate"); michael@0: return; michael@0: } michael@0: michael@0: if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) michael@0: { michael@0: truelen = 3; michael@0: sample_depth = 8; michael@0: } michael@0: michael@0: else michael@0: { michael@0: truelen = png_ptr->channels; michael@0: sample_depth = png_ptr->bit_depth; michael@0: } michael@0: michael@0: if (length != truelen || length > 4) michael@0: { michael@0: png_chunk_benign_error(png_ptr, "invalid"); michael@0: png_crc_finish(png_ptr, length); michael@0: return; michael@0: } michael@0: michael@0: buf[0] = buf[1] = buf[2] = buf[3] = sample_depth; michael@0: png_crc_read(png_ptr, buf, truelen); michael@0: michael@0: if (png_crc_finish(png_ptr, 0)) michael@0: return; michael@0: michael@0: for (i=0; i sample_depth) michael@0: { michael@0: png_chunk_benign_error(png_ptr, "invalid"); michael@0: return; michael@0: } michael@0: michael@0: if (png_ptr->color_type & PNG_COLOR_MASK_COLOR) michael@0: { michael@0: png_ptr->sig_bit.red = buf[0]; michael@0: png_ptr->sig_bit.green = buf[1]; michael@0: png_ptr->sig_bit.blue = buf[2]; michael@0: png_ptr->sig_bit.alpha = buf[3]; michael@0: } michael@0: michael@0: else michael@0: { michael@0: png_ptr->sig_bit.gray = buf[0]; michael@0: png_ptr->sig_bit.red = buf[0]; michael@0: png_ptr->sig_bit.green = buf[0]; michael@0: png_ptr->sig_bit.blue = buf[0]; michael@0: png_ptr->sig_bit.alpha = buf[1]; michael@0: } michael@0: michael@0: png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit)); michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_cHRM_SUPPORTED michael@0: void /* PRIVATE */ michael@0: png_handle_cHRM(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) michael@0: { michael@0: png_byte buf[32]; michael@0: png_xy xy; michael@0: michael@0: png_debug(1, "in png_handle_cHRM"); michael@0: michael@0: if (!(png_ptr->mode & PNG_HAVE_IHDR)) michael@0: png_chunk_error(png_ptr, "missing IHDR"); michael@0: michael@0: else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "out of place"); michael@0: return; michael@0: } michael@0: michael@0: if (length != 32) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "invalid"); michael@0: return; michael@0: } michael@0: michael@0: png_crc_read(png_ptr, buf, 32); michael@0: michael@0: if (png_crc_finish(png_ptr, 0)) michael@0: return; michael@0: michael@0: xy.whitex = png_get_fixed_point(NULL, buf); michael@0: xy.whitey = png_get_fixed_point(NULL, buf + 4); michael@0: xy.redx = png_get_fixed_point(NULL, buf + 8); michael@0: xy.redy = png_get_fixed_point(NULL, buf + 12); michael@0: xy.greenx = png_get_fixed_point(NULL, buf + 16); michael@0: xy.greeny = png_get_fixed_point(NULL, buf + 20); michael@0: xy.bluex = png_get_fixed_point(NULL, buf + 24); michael@0: xy.bluey = png_get_fixed_point(NULL, buf + 28); michael@0: michael@0: if (xy.whitex == PNG_FIXED_ERROR || michael@0: xy.whitey == PNG_FIXED_ERROR || michael@0: xy.redx == PNG_FIXED_ERROR || michael@0: xy.redy == PNG_FIXED_ERROR || michael@0: xy.greenx == PNG_FIXED_ERROR || michael@0: xy.greeny == PNG_FIXED_ERROR || michael@0: xy.bluex == PNG_FIXED_ERROR || michael@0: xy.bluey == PNG_FIXED_ERROR) michael@0: { michael@0: png_chunk_benign_error(png_ptr, "invalid values"); michael@0: return; michael@0: } michael@0: michael@0: /* If a colorspace error has already been output skip this chunk */ michael@0: if (png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) michael@0: return; michael@0: michael@0: if (png_ptr->colorspace.flags & PNG_COLORSPACE_FROM_cHRM) michael@0: { michael@0: png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID; michael@0: png_colorspace_sync(png_ptr, info_ptr); michael@0: png_chunk_benign_error(png_ptr, "duplicate"); michael@0: return; michael@0: } michael@0: michael@0: png_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM; michael@0: (void)png_colorspace_set_chromaticities(png_ptr, &png_ptr->colorspace, &xy, michael@0: 1/*prefer cHRM values*/); michael@0: png_colorspace_sync(png_ptr, info_ptr); michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_sRGB_SUPPORTED michael@0: void /* PRIVATE */ michael@0: png_handle_sRGB(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) michael@0: { michael@0: png_byte intent; michael@0: michael@0: png_debug(1, "in png_handle_sRGB"); michael@0: michael@0: if (!(png_ptr->mode & PNG_HAVE_IHDR)) michael@0: png_chunk_error(png_ptr, "missing IHDR"); michael@0: michael@0: else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "out of place"); michael@0: return; michael@0: } michael@0: michael@0: if (length != 1) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "invalid"); michael@0: return; michael@0: } michael@0: michael@0: png_crc_read(png_ptr, &intent, 1); michael@0: michael@0: if (png_crc_finish(png_ptr, 0)) michael@0: return; michael@0: michael@0: /* If a colorspace error has already been output skip this chunk */ michael@0: if (png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) michael@0: return; michael@0: michael@0: /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect michael@0: * this. michael@0: */ michael@0: if (png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) michael@0: { michael@0: png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID; michael@0: png_colorspace_sync(png_ptr, info_ptr); michael@0: png_chunk_benign_error(png_ptr, "too many profiles"); michael@0: return; michael@0: } michael@0: michael@0: (void)png_colorspace_set_sRGB(png_ptr, &png_ptr->colorspace, intent); michael@0: png_colorspace_sync(png_ptr, info_ptr); michael@0: } michael@0: #endif /* PNG_READ_sRGB_SUPPORTED */ michael@0: michael@0: #ifdef PNG_READ_iCCP_SUPPORTED michael@0: void /* PRIVATE */ michael@0: png_handle_iCCP(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) michael@0: /* Note: this does not properly handle profiles that are > 64K under DOS */ michael@0: { michael@0: png_const_charp errmsg = NULL; /* error message output, or no error */ michael@0: int finished = 0; /* crc checked */ michael@0: michael@0: png_debug(1, "in png_handle_iCCP"); michael@0: michael@0: if (!(png_ptr->mode & PNG_HAVE_IHDR)) michael@0: png_chunk_error(png_ptr, "missing IHDR"); michael@0: michael@0: else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "out of place"); michael@0: return; michael@0: } michael@0: michael@0: /* Consistent with all the above colorspace handling an obviously *invalid* michael@0: * chunk is just ignored, so does not invalidate the color space. An michael@0: * alternative is to set the 'invalid' flags at the start of this routine michael@0: * and only clear them in they were not set before and all the tests pass. michael@0: * The minimum 'deflate' stream is assumed to be just the 2 byte header and 4 michael@0: * byte checksum. The keyword must be one character and there is a michael@0: * terminator (0) byte and the compression method. michael@0: */ michael@0: if (length < 9) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "too short"); michael@0: return; michael@0: } michael@0: michael@0: /* If a colorspace error has already been output skip this chunk */ michael@0: if (png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: return; michael@0: } michael@0: michael@0: /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect michael@0: * this. michael@0: */ michael@0: if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) == 0) michael@0: { michael@0: uInt read_length, keyword_length; michael@0: char keyword[81]; michael@0: michael@0: /* Find the keyword; the keyword plus separator and compression method michael@0: * bytes can be at most 81 characters long. michael@0: */ michael@0: read_length = 81; /* maximum */ michael@0: if (read_length > length) michael@0: read_length = (uInt)length; michael@0: michael@0: png_crc_read(png_ptr, (png_bytep)keyword, read_length); michael@0: length -= read_length; michael@0: michael@0: keyword_length = 0; michael@0: while (keyword_length < 80 && keyword_length < read_length && michael@0: keyword[keyword_length] != 0) michael@0: ++keyword_length; michael@0: michael@0: /* TODO: make the keyword checking common */ michael@0: if (keyword_length >= 1 && keyword_length <= 79) michael@0: { michael@0: /* We only understand '0' compression - deflate - so if we get a michael@0: * different value we can't safely decode the chunk. michael@0: */ michael@0: if (keyword_length+1 < read_length && michael@0: keyword[keyword_length+1] == PNG_COMPRESSION_TYPE_BASE) michael@0: { michael@0: read_length -= keyword_length+2; michael@0: michael@0: if (png_inflate_claim(png_ptr, png_iCCP) == Z_OK) michael@0: { michael@0: Byte profile_header[132]; michael@0: Byte local_buffer[PNG_INFLATE_BUF_SIZE]; michael@0: png_alloc_size_t size = (sizeof profile_header); michael@0: michael@0: png_ptr->zstream.next_in = (Bytef*)keyword + (keyword_length+2); michael@0: png_ptr->zstream.avail_in = read_length; michael@0: (void)png_inflate_read(png_ptr, local_buffer, michael@0: (sizeof local_buffer), &length, profile_header, &size, michael@0: 0/*finish: don't, because the output is too small*/); michael@0: michael@0: if (size == 0) michael@0: { michael@0: /* We have the ICC profile header; do the basic header checks. michael@0: */ michael@0: const png_uint_32 profile_length = michael@0: png_get_uint_32(profile_header); michael@0: michael@0: if (png_icc_check_length(png_ptr, &png_ptr->colorspace, michael@0: keyword, profile_length)) michael@0: { michael@0: /* The length is apparently ok, so we can check the 132 michael@0: * byte header. michael@0: */ michael@0: if (png_icc_check_header(png_ptr, &png_ptr->colorspace, michael@0: keyword, profile_length, profile_header, michael@0: png_ptr->color_type)) michael@0: { michael@0: /* Now read the tag table; a variable size buffer is michael@0: * needed at this point, allocate one for the whole michael@0: * profile. The header check has already validated michael@0: * that none of these stuff will overflow. michael@0: */ michael@0: const png_uint_32 tag_count = png_get_uint_32( michael@0: profile_header+128); michael@0: png_bytep profile = png_read_buffer(png_ptr, michael@0: profile_length, 2/*silent*/); michael@0: michael@0: if (profile != NULL) michael@0: { michael@0: memcpy(profile, profile_header, michael@0: (sizeof profile_header)); michael@0: michael@0: size = 12 * tag_count; michael@0: michael@0: (void)png_inflate_read(png_ptr, local_buffer, michael@0: (sizeof local_buffer), &length, michael@0: profile + (sizeof profile_header), &size, 0); michael@0: michael@0: /* Still expect a buffer error because we expect michael@0: * there to be some tag data! michael@0: */ michael@0: if (size == 0) michael@0: { michael@0: if (png_icc_check_tag_table(png_ptr, michael@0: &png_ptr->colorspace, keyword, profile_length, michael@0: profile)) michael@0: { michael@0: /* The profile has been validated for basic michael@0: * security issues, so read the whole thing in. michael@0: */ michael@0: size = profile_length - (sizeof profile_header) michael@0: - 12 * tag_count; michael@0: michael@0: (void)png_inflate_read(png_ptr, local_buffer, michael@0: (sizeof local_buffer), &length, michael@0: profile + (sizeof profile_header) + michael@0: 12 * tag_count, &size, 1/*finish*/); michael@0: michael@0: if (length > 0 && !(png_ptr->flags & michael@0: PNG_FLAG_BENIGN_ERRORS_WARN)) michael@0: errmsg = "extra compressed data"; michael@0: michael@0: /* But otherwise allow extra data: */ michael@0: else if (size == 0) michael@0: { michael@0: if (length > 0) michael@0: { michael@0: /* This can be handled completely, so michael@0: * keep going. michael@0: */ michael@0: png_chunk_warning(png_ptr, michael@0: "extra compressed data"); michael@0: } michael@0: michael@0: png_crc_finish(png_ptr, length); michael@0: finished = 1; michael@0: michael@0: # ifdef PNG_sRGB_SUPPORTED michael@0: /* Check for a match against sRGB */ michael@0: png_icc_set_sRGB(png_ptr, michael@0: &png_ptr->colorspace, profile, michael@0: png_ptr->zstream.adler); michael@0: # endif michael@0: michael@0: /* Steal the profile for info_ptr. */ michael@0: if (info_ptr != NULL) michael@0: { michael@0: png_free_data(png_ptr, info_ptr, michael@0: PNG_FREE_ICCP, 0); michael@0: michael@0: info_ptr->iccp_name = png_voidcast(char*, michael@0: png_malloc_base(png_ptr, michael@0: keyword_length+1)); michael@0: if (info_ptr->iccp_name != NULL) michael@0: { michael@0: memcpy(info_ptr->iccp_name, keyword, michael@0: keyword_length+1); michael@0: info_ptr->iccp_proflen = michael@0: profile_length; michael@0: info_ptr->iccp_profile = profile; michael@0: png_ptr->read_buffer = NULL; /*steal*/ michael@0: info_ptr->free_me |= PNG_FREE_ICCP; michael@0: info_ptr->valid |= PNG_INFO_iCCP; michael@0: } michael@0: michael@0: else michael@0: { michael@0: png_ptr->colorspace.flags |= michael@0: PNG_COLORSPACE_INVALID; michael@0: errmsg = "out of memory"; michael@0: } michael@0: } michael@0: michael@0: /* else the profile remains in the read michael@0: * buffer which gets reused for subsequent michael@0: * chunks. michael@0: */ michael@0: michael@0: if (info_ptr != NULL) michael@0: png_colorspace_sync(png_ptr, info_ptr); michael@0: michael@0: if (errmsg == NULL) michael@0: { michael@0: png_ptr->zowner = 0; michael@0: return; michael@0: } michael@0: } michael@0: michael@0: else if (size > 0) michael@0: errmsg = "truncated"; michael@0: michael@0: else michael@0: errmsg = png_ptr->zstream.msg; michael@0: } michael@0: michael@0: /* else png_icc_check_tag_table output an error */ michael@0: } michael@0: michael@0: else /* profile truncated */ michael@0: errmsg = png_ptr->zstream.msg; michael@0: } michael@0: michael@0: else michael@0: errmsg = "out of memory"; michael@0: } michael@0: michael@0: /* else png_icc_check_header output an error */ michael@0: } michael@0: michael@0: /* else png_icc_check_length output an error */ michael@0: } michael@0: michael@0: else /* profile truncated */ michael@0: errmsg = png_ptr->zstream.msg; michael@0: michael@0: /* Release the stream */ michael@0: png_ptr->zowner = 0; michael@0: } michael@0: michael@0: else /* png_inflate_claim failed */ michael@0: errmsg = png_ptr->zstream.msg; michael@0: } michael@0: michael@0: else michael@0: errmsg = "bad compression method"; /* or missing */ michael@0: } michael@0: michael@0: else michael@0: errmsg = "bad keyword"; michael@0: } michael@0: michael@0: else michael@0: errmsg = "too many profiles"; michael@0: michael@0: /* Failure: the reason is in 'errmsg' */ michael@0: if (!finished) michael@0: png_crc_finish(png_ptr, length); michael@0: michael@0: png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID; michael@0: png_colorspace_sync(png_ptr, info_ptr); michael@0: if (errmsg != NULL) /* else already output */ michael@0: png_chunk_benign_error(png_ptr, errmsg); michael@0: } michael@0: #endif /* PNG_READ_iCCP_SUPPORTED */ michael@0: michael@0: #ifdef PNG_READ_sPLT_SUPPORTED michael@0: void /* PRIVATE */ michael@0: png_handle_sPLT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) michael@0: /* Note: this does not properly handle chunks that are > 64K under DOS */ michael@0: { michael@0: png_bytep entry_start, buffer; michael@0: png_sPLT_t new_palette; michael@0: png_sPLT_entryp pp; michael@0: png_uint_32 data_length; michael@0: int entry_size, i; michael@0: png_uint_32 skip = 0; michael@0: png_uint_32 dl; michael@0: png_size_t max_dl; michael@0: michael@0: png_debug(1, "in png_handle_sPLT"); michael@0: michael@0: #ifdef PNG_USER_LIMITS_SUPPORTED michael@0: if (png_ptr->user_chunk_cache_max != 0) michael@0: { michael@0: if (png_ptr->user_chunk_cache_max == 1) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: return; michael@0: } michael@0: michael@0: if (--png_ptr->user_chunk_cache_max == 1) michael@0: { michael@0: png_warning(png_ptr, "No space in chunk cache for sPLT"); michael@0: png_crc_finish(png_ptr, length); michael@0: return; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: if (!(png_ptr->mode & PNG_HAVE_IHDR)) michael@0: png_chunk_error(png_ptr, "missing IHDR"); michael@0: michael@0: else if (png_ptr->mode & PNG_HAVE_IDAT) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "out of place"); michael@0: return; michael@0: } michael@0: michael@0: #ifdef PNG_MAX_MALLOC_64K michael@0: if (length > 65535U) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "too large to fit in memory"); michael@0: return; michael@0: } michael@0: #endif michael@0: michael@0: buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/); michael@0: if (buffer == NULL) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "out of memory"); michael@0: return; michael@0: } michael@0: michael@0: michael@0: /* WARNING: this may break if size_t is less than 32 bits; it is assumed michael@0: * that the PNG_MAX_MALLOC_64K test is enabled in this case, but this is a michael@0: * potential breakage point if the types in pngconf.h aren't exactly right. michael@0: */ michael@0: png_crc_read(png_ptr, buffer, length); michael@0: michael@0: if (png_crc_finish(png_ptr, skip)) michael@0: return; michael@0: michael@0: buffer[length] = 0; michael@0: michael@0: for (entry_start = buffer; *entry_start; entry_start++) michael@0: /* Empty loop to find end of name */ ; michael@0: michael@0: ++entry_start; michael@0: michael@0: /* A sample depth should follow the separator, and we should be on it */ michael@0: if (entry_start > buffer + length - 2) michael@0: { michael@0: png_warning(png_ptr, "malformed sPLT chunk"); michael@0: return; michael@0: } michael@0: michael@0: new_palette.depth = *entry_start++; michael@0: entry_size = (new_palette.depth == 8 ? 6 : 10); michael@0: /* This must fit in a png_uint_32 because it is derived from the original michael@0: * chunk data length. michael@0: */ michael@0: data_length = length - (png_uint_32)(entry_start - buffer); michael@0: michael@0: /* Integrity-check the data length */ michael@0: if (data_length % entry_size) michael@0: { michael@0: png_warning(png_ptr, "sPLT chunk has bad length"); michael@0: return; michael@0: } michael@0: michael@0: dl = (png_int_32)(data_length / entry_size); michael@0: max_dl = PNG_SIZE_MAX / (sizeof (png_sPLT_entry)); michael@0: michael@0: if (dl > max_dl) michael@0: { michael@0: png_warning(png_ptr, "sPLT chunk too long"); michael@0: return; michael@0: } michael@0: michael@0: new_palette.nentries = (png_int_32)(data_length / entry_size); michael@0: michael@0: new_palette.entries = (png_sPLT_entryp)png_malloc_warn( michael@0: png_ptr, new_palette.nentries * (sizeof (png_sPLT_entry))); michael@0: michael@0: if (new_palette.entries == NULL) michael@0: { michael@0: png_warning(png_ptr, "sPLT chunk requires too much memory"); michael@0: return; michael@0: } michael@0: michael@0: #ifdef PNG_POINTER_INDEXING_SUPPORTED michael@0: for (i = 0; i < new_palette.nentries; i++) michael@0: { michael@0: pp = new_palette.entries + i; michael@0: michael@0: if (new_palette.depth == 8) michael@0: { michael@0: pp->red = *entry_start++; michael@0: pp->green = *entry_start++; michael@0: pp->blue = *entry_start++; michael@0: pp->alpha = *entry_start++; michael@0: } michael@0: michael@0: else michael@0: { michael@0: pp->red = png_get_uint_16(entry_start); entry_start += 2; michael@0: pp->green = png_get_uint_16(entry_start); entry_start += 2; michael@0: pp->blue = png_get_uint_16(entry_start); entry_start += 2; michael@0: pp->alpha = png_get_uint_16(entry_start); entry_start += 2; michael@0: } michael@0: michael@0: pp->frequency = png_get_uint_16(entry_start); entry_start += 2; michael@0: } michael@0: #else michael@0: pp = new_palette.entries; michael@0: michael@0: for (i = 0; i < new_palette.nentries; i++) michael@0: { michael@0: michael@0: if (new_palette.depth == 8) michael@0: { michael@0: pp[i].red = *entry_start++; michael@0: pp[i].green = *entry_start++; michael@0: pp[i].blue = *entry_start++; michael@0: pp[i].alpha = *entry_start++; michael@0: } michael@0: michael@0: else michael@0: { michael@0: pp[i].red = png_get_uint_16(entry_start); entry_start += 2; michael@0: pp[i].green = png_get_uint_16(entry_start); entry_start += 2; michael@0: pp[i].blue = png_get_uint_16(entry_start); entry_start += 2; michael@0: pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2; michael@0: } michael@0: michael@0: pp[i].frequency = png_get_uint_16(entry_start); entry_start += 2; michael@0: } michael@0: #endif michael@0: michael@0: /* Discard all chunk data except the name and stash that */ michael@0: new_palette.name = (png_charp)buffer; michael@0: michael@0: png_set_sPLT(png_ptr, info_ptr, &new_palette, 1); michael@0: michael@0: png_free(png_ptr, new_palette.entries); michael@0: } michael@0: #endif /* PNG_READ_sPLT_SUPPORTED */ michael@0: michael@0: #ifdef PNG_READ_tRNS_SUPPORTED michael@0: void /* PRIVATE */ michael@0: png_handle_tRNS(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) michael@0: { michael@0: png_byte readbuf[PNG_MAX_PALETTE_LENGTH]; michael@0: michael@0: png_debug(1, "in png_handle_tRNS"); michael@0: michael@0: if (!(png_ptr->mode & PNG_HAVE_IHDR)) michael@0: png_chunk_error(png_ptr, "missing IHDR"); michael@0: michael@0: else if (png_ptr->mode & PNG_HAVE_IDAT) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "out of place"); michael@0: return; michael@0: } michael@0: michael@0: else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS)) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "duplicate"); michael@0: return; michael@0: } michael@0: michael@0: if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY) michael@0: { michael@0: png_byte buf[2]; michael@0: michael@0: if (length != 2) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "invalid"); michael@0: return; michael@0: } michael@0: michael@0: png_crc_read(png_ptr, buf, 2); michael@0: png_ptr->num_trans = 1; michael@0: png_ptr->trans_color.gray = png_get_uint_16(buf); michael@0: } michael@0: michael@0: else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB) michael@0: { michael@0: png_byte buf[6]; michael@0: michael@0: if (length != 6) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "invalid"); michael@0: return; michael@0: } michael@0: michael@0: png_crc_read(png_ptr, buf, length); michael@0: png_ptr->num_trans = 1; michael@0: png_ptr->trans_color.red = png_get_uint_16(buf); michael@0: png_ptr->trans_color.green = png_get_uint_16(buf + 2); michael@0: png_ptr->trans_color.blue = png_get_uint_16(buf + 4); michael@0: } michael@0: michael@0: else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) michael@0: { michael@0: if (!(png_ptr->mode & PNG_HAVE_PLTE)) michael@0: { michael@0: /* TODO: is this actually an error in the ISO spec? */ michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "out of place"); michael@0: return; michael@0: } michael@0: michael@0: if (length > png_ptr->num_palette || length > PNG_MAX_PALETTE_LENGTH || michael@0: length == 0) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "invalid"); michael@0: return; michael@0: } michael@0: michael@0: png_crc_read(png_ptr, readbuf, length); michael@0: png_ptr->num_trans = (png_uint_16)length; michael@0: } michael@0: michael@0: else michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "invalid with alpha channel"); michael@0: return; michael@0: } michael@0: michael@0: if (png_crc_finish(png_ptr, 0)) michael@0: { michael@0: png_ptr->num_trans = 0; michael@0: return; michael@0: } michael@0: michael@0: /* TODO: this is a horrible side effect in the palette case because the michael@0: * png_struct ends up with a pointer to the tRNS buffer owned by the michael@0: * png_info. Fix this. michael@0: */ michael@0: png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans, michael@0: &(png_ptr->trans_color)); michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_bKGD_SUPPORTED michael@0: void /* PRIVATE */ michael@0: png_handle_bKGD(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) michael@0: { michael@0: unsigned int truelen; michael@0: png_byte buf[6]; michael@0: png_color_16 background; michael@0: michael@0: png_debug(1, "in png_handle_bKGD"); michael@0: michael@0: if (!(png_ptr->mode & PNG_HAVE_IHDR)) michael@0: png_chunk_error(png_ptr, "missing IHDR"); michael@0: michael@0: else if ((png_ptr->mode & PNG_HAVE_IDAT) || michael@0: (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && michael@0: !(png_ptr->mode & PNG_HAVE_PLTE))) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "out of place"); michael@0: return; michael@0: } michael@0: michael@0: else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD)) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "duplicate"); michael@0: return; michael@0: } michael@0: michael@0: if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) michael@0: truelen = 1; michael@0: michael@0: else if (png_ptr->color_type & PNG_COLOR_MASK_COLOR) michael@0: truelen = 6; michael@0: michael@0: else michael@0: truelen = 2; michael@0: michael@0: if (length != truelen) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "invalid"); michael@0: return; michael@0: } michael@0: michael@0: png_crc_read(png_ptr, buf, truelen); michael@0: michael@0: if (png_crc_finish(png_ptr, 0)) michael@0: return; michael@0: michael@0: /* We convert the index value into RGB components so that we can allow michael@0: * arbitrary RGB values for background when we have transparency, and michael@0: * so it is easy to determine the RGB values of the background color michael@0: * from the info_ptr struct. michael@0: */ michael@0: if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) michael@0: { michael@0: background.index = buf[0]; michael@0: michael@0: if (info_ptr && info_ptr->num_palette) michael@0: { michael@0: if (buf[0] >= info_ptr->num_palette) michael@0: { michael@0: png_chunk_benign_error(png_ptr, "invalid index"); michael@0: return; michael@0: } michael@0: michael@0: background.red = (png_uint_16)png_ptr->palette[buf[0]].red; michael@0: background.green = (png_uint_16)png_ptr->palette[buf[0]].green; michael@0: background.blue = (png_uint_16)png_ptr->palette[buf[0]].blue; michael@0: } michael@0: michael@0: else michael@0: background.red = background.green = background.blue = 0; michael@0: michael@0: background.gray = 0; michael@0: } michael@0: michael@0: else if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) /* GRAY */ michael@0: { michael@0: background.index = 0; michael@0: background.red = michael@0: background.green = michael@0: background.blue = michael@0: background.gray = png_get_uint_16(buf); michael@0: } michael@0: michael@0: else michael@0: { michael@0: background.index = 0; michael@0: background.red = png_get_uint_16(buf); michael@0: background.green = png_get_uint_16(buf + 2); michael@0: background.blue = png_get_uint_16(buf + 4); michael@0: background.gray = 0; michael@0: } michael@0: michael@0: png_set_bKGD(png_ptr, info_ptr, &background); michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_hIST_SUPPORTED michael@0: void /* PRIVATE */ michael@0: png_handle_hIST(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) michael@0: { michael@0: unsigned int num, i; michael@0: png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH]; michael@0: michael@0: png_debug(1, "in png_handle_hIST"); michael@0: michael@0: if (!(png_ptr->mode & PNG_HAVE_IHDR)) michael@0: png_chunk_error(png_ptr, "missing IHDR"); michael@0: michael@0: else if ((png_ptr->mode & PNG_HAVE_IDAT) || !(png_ptr->mode & PNG_HAVE_PLTE)) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "out of place"); michael@0: return; michael@0: } michael@0: michael@0: else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST)) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "duplicate"); michael@0: return; michael@0: } michael@0: michael@0: num = length / 2 ; michael@0: michael@0: if (num != png_ptr->num_palette || num > PNG_MAX_PALETTE_LENGTH) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "invalid"); michael@0: return; michael@0: } michael@0: michael@0: for (i = 0; i < num; i++) michael@0: { michael@0: png_byte buf[2]; michael@0: michael@0: png_crc_read(png_ptr, buf, 2); michael@0: readbuf[i] = png_get_uint_16(buf); michael@0: } michael@0: michael@0: if (png_crc_finish(png_ptr, 0)) michael@0: return; michael@0: michael@0: png_set_hIST(png_ptr, info_ptr, readbuf); michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_pHYs_SUPPORTED michael@0: void /* PRIVATE */ michael@0: png_handle_pHYs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) michael@0: { michael@0: png_byte buf[9]; michael@0: png_uint_32 res_x, res_y; michael@0: int unit_type; michael@0: michael@0: png_debug(1, "in png_handle_pHYs"); michael@0: michael@0: if (!(png_ptr->mode & PNG_HAVE_IHDR)) michael@0: png_chunk_error(png_ptr, "missing IHDR"); michael@0: michael@0: else if (png_ptr->mode & PNG_HAVE_IDAT) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "out of place"); michael@0: return; michael@0: } michael@0: michael@0: else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs)) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "duplicate"); michael@0: return; michael@0: } michael@0: michael@0: if (length != 9) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "invalid"); michael@0: return; michael@0: } michael@0: michael@0: png_crc_read(png_ptr, buf, 9); michael@0: michael@0: if (png_crc_finish(png_ptr, 0)) michael@0: return; michael@0: michael@0: res_x = png_get_uint_32(buf); michael@0: res_y = png_get_uint_32(buf + 4); michael@0: unit_type = buf[8]; michael@0: png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type); michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_oFFs_SUPPORTED michael@0: void /* PRIVATE */ michael@0: png_handle_oFFs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) michael@0: { michael@0: png_byte buf[9]; michael@0: png_int_32 offset_x, offset_y; michael@0: int unit_type; michael@0: michael@0: png_debug(1, "in png_handle_oFFs"); michael@0: michael@0: if (!(png_ptr->mode & PNG_HAVE_IHDR)) michael@0: png_chunk_error(png_ptr, "missing IHDR"); michael@0: michael@0: else if (png_ptr->mode & PNG_HAVE_IDAT) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "out of place"); michael@0: return; michael@0: } michael@0: michael@0: else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs)) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "duplicate"); michael@0: return; michael@0: } michael@0: michael@0: if (length != 9) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "invalid"); michael@0: return; michael@0: } michael@0: michael@0: png_crc_read(png_ptr, buf, 9); michael@0: michael@0: if (png_crc_finish(png_ptr, 0)) michael@0: return; michael@0: michael@0: offset_x = png_get_int_32(buf); michael@0: offset_y = png_get_int_32(buf + 4); michael@0: unit_type = buf[8]; michael@0: png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type); michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_pCAL_SUPPORTED michael@0: /* Read the pCAL chunk (described in the PNG Extensions document) */ michael@0: void /* PRIVATE */ michael@0: png_handle_pCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) michael@0: { michael@0: png_int_32 X0, X1; michael@0: png_byte type, nparams; michael@0: png_bytep buffer, buf, units, endptr; michael@0: png_charpp params; michael@0: int i; michael@0: michael@0: png_debug(1, "in png_handle_pCAL"); michael@0: michael@0: if (!(png_ptr->mode & PNG_HAVE_IHDR)) michael@0: png_chunk_error(png_ptr, "missing IHDR"); michael@0: michael@0: else if (png_ptr->mode & PNG_HAVE_IDAT) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "out of place"); michael@0: return; michael@0: } michael@0: michael@0: else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL)) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "duplicate"); michael@0: return; michael@0: } michael@0: michael@0: png_debug1(2, "Allocating and reading pCAL chunk data (%u bytes)", michael@0: length + 1); michael@0: michael@0: buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/); michael@0: michael@0: if (buffer == NULL) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "out of memory"); michael@0: return; michael@0: } michael@0: michael@0: png_crc_read(png_ptr, buffer, length); michael@0: michael@0: if (png_crc_finish(png_ptr, 0)) michael@0: return; michael@0: michael@0: buffer[length] = 0; /* Null terminate the last string */ michael@0: michael@0: png_debug(3, "Finding end of pCAL purpose string"); michael@0: for (buf = buffer; *buf; buf++) michael@0: /* Empty loop */ ; michael@0: michael@0: endptr = buffer + length; michael@0: michael@0: /* We need to have at least 12 bytes after the purpose string michael@0: * in order to get the parameter information. michael@0: */ michael@0: if (endptr <= buf + 12) michael@0: { michael@0: png_chunk_benign_error(png_ptr, "invalid"); michael@0: return; michael@0: } michael@0: michael@0: png_debug(3, "Reading pCAL X0, X1, type, nparams, and units"); michael@0: X0 = png_get_int_32((png_bytep)buf+1); michael@0: X1 = png_get_int_32((png_bytep)buf+5); michael@0: type = buf[9]; michael@0: nparams = buf[10]; michael@0: units = buf + 11; michael@0: michael@0: png_debug(3, "Checking pCAL equation type and number of parameters"); michael@0: /* Check that we have the right number of parameters for known michael@0: * equation types. michael@0: */ michael@0: if ((type == PNG_EQUATION_LINEAR && nparams != 2) || michael@0: (type == PNG_EQUATION_BASE_E && nparams != 3) || michael@0: (type == PNG_EQUATION_ARBITRARY && nparams != 3) || michael@0: (type == PNG_EQUATION_HYPERBOLIC && nparams != 4)) michael@0: { michael@0: png_chunk_benign_error(png_ptr, "invalid parameter count"); michael@0: return; michael@0: } michael@0: michael@0: else if (type >= PNG_EQUATION_LAST) michael@0: { michael@0: png_chunk_benign_error(png_ptr, "unrecognized equation type"); michael@0: } michael@0: michael@0: for (buf = units; *buf; buf++) michael@0: /* Empty loop to move past the units string. */ ; michael@0: michael@0: png_debug(3, "Allocating pCAL parameters array"); michael@0: michael@0: params = png_voidcast(png_charpp, png_malloc_warn(png_ptr, michael@0: nparams * (sizeof (png_charp)))); michael@0: michael@0: if (params == NULL) michael@0: { michael@0: png_chunk_benign_error(png_ptr, "out of memory"); michael@0: return; michael@0: } michael@0: michael@0: /* Get pointers to the start of each parameter string. */ michael@0: for (i = 0; i < nparams; i++) michael@0: { michael@0: buf++; /* Skip the null string terminator from previous parameter. */ michael@0: michael@0: png_debug1(3, "Reading pCAL parameter %d", i); michael@0: michael@0: for (params[i] = (png_charp)buf; buf <= endptr && *buf != 0; buf++) michael@0: /* Empty loop to move past each parameter string */ ; michael@0: michael@0: /* Make sure we haven't run out of data yet */ michael@0: if (buf > endptr) michael@0: { michael@0: png_free(png_ptr, params); michael@0: png_chunk_benign_error(png_ptr, "invalid data"); michael@0: return; michael@0: } michael@0: } michael@0: michael@0: png_set_pCAL(png_ptr, info_ptr, (png_charp)buffer, X0, X1, type, nparams, michael@0: (png_charp)units, params); michael@0: michael@0: png_free(png_ptr, params); michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_sCAL_SUPPORTED michael@0: /* Read the sCAL chunk */ michael@0: void /* PRIVATE */ michael@0: png_handle_sCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) michael@0: { michael@0: png_bytep buffer; michael@0: png_size_t i; michael@0: int state; michael@0: michael@0: png_debug(1, "in png_handle_sCAL"); michael@0: michael@0: if (!(png_ptr->mode & PNG_HAVE_IHDR)) michael@0: png_chunk_error(png_ptr, "missing IHDR"); michael@0: michael@0: else if (png_ptr->mode & PNG_HAVE_IDAT) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "out of place"); michael@0: return; michael@0: } michael@0: michael@0: else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL)) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "duplicate"); michael@0: return; michael@0: } michael@0: michael@0: /* Need unit type, width, \0, height: minimum 4 bytes */ michael@0: else if (length < 4) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "invalid"); michael@0: return; michael@0: } michael@0: michael@0: png_debug1(2, "Allocating and reading sCAL chunk data (%u bytes)", michael@0: length + 1); michael@0: michael@0: buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/); michael@0: michael@0: if (buffer == NULL) michael@0: { michael@0: png_chunk_benign_error(png_ptr, "out of memory"); michael@0: png_crc_finish(png_ptr, length); michael@0: return; michael@0: } michael@0: michael@0: png_crc_read(png_ptr, buffer, length); michael@0: buffer[length] = 0; /* Null terminate the last string */ michael@0: michael@0: if (png_crc_finish(png_ptr, 0)) michael@0: return; michael@0: michael@0: /* Validate the unit. */ michael@0: if (buffer[0] != 1 && buffer[0] != 2) michael@0: { michael@0: png_chunk_benign_error(png_ptr, "invalid unit"); michael@0: return; michael@0: } michael@0: michael@0: /* Validate the ASCII numbers, need two ASCII numbers separated by michael@0: * a '\0' and they need to fit exactly in the chunk data. michael@0: */ michael@0: i = 1; michael@0: state = 0; michael@0: michael@0: if (!png_check_fp_number((png_const_charp)buffer, length, &state, &i) || michael@0: i >= length || buffer[i++] != 0) michael@0: png_chunk_benign_error(png_ptr, "bad width format"); michael@0: michael@0: else if (!PNG_FP_IS_POSITIVE(state)) michael@0: png_chunk_benign_error(png_ptr, "non-positive width"); michael@0: michael@0: else michael@0: { michael@0: png_size_t heighti = i; michael@0: michael@0: state = 0; michael@0: if (!png_check_fp_number((png_const_charp)buffer, length, &state, &i) || michael@0: i != length) michael@0: png_chunk_benign_error(png_ptr, "bad height format"); michael@0: michael@0: else if (!PNG_FP_IS_POSITIVE(state)) michael@0: png_chunk_benign_error(png_ptr, "non-positive height"); michael@0: michael@0: else michael@0: /* This is the (only) success case. */ michael@0: png_set_sCAL_s(png_ptr, info_ptr, buffer[0], michael@0: (png_charp)buffer+1, (png_charp)buffer+heighti); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_tIME_SUPPORTED michael@0: void /* PRIVATE */ michael@0: png_handle_tIME(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) michael@0: { michael@0: png_byte buf[7]; michael@0: png_time mod_time; michael@0: michael@0: png_debug(1, "in png_handle_tIME"); michael@0: michael@0: if (!(png_ptr->mode & PNG_HAVE_IHDR)) michael@0: png_chunk_error(png_ptr, "missing IHDR"); michael@0: michael@0: else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME)) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "duplicate"); michael@0: return; michael@0: } michael@0: michael@0: if (png_ptr->mode & PNG_HAVE_IDAT) michael@0: png_ptr->mode |= PNG_AFTER_IDAT; michael@0: michael@0: if (length != 7) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "invalid"); michael@0: return; michael@0: } michael@0: michael@0: png_crc_read(png_ptr, buf, 7); michael@0: michael@0: if (png_crc_finish(png_ptr, 0)) michael@0: return; michael@0: michael@0: mod_time.second = buf[6]; michael@0: mod_time.minute = buf[5]; michael@0: mod_time.hour = buf[4]; michael@0: mod_time.day = buf[3]; michael@0: mod_time.month = buf[2]; michael@0: mod_time.year = png_get_uint_16(buf); michael@0: michael@0: png_set_tIME(png_ptr, info_ptr, &mod_time); michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_tEXt_SUPPORTED michael@0: /* Note: this does not properly handle chunks that are > 64K under DOS */ michael@0: void /* PRIVATE */ michael@0: png_handle_tEXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) michael@0: { michael@0: png_text text_info; michael@0: png_bytep buffer; michael@0: png_charp key; michael@0: png_charp text; michael@0: png_uint_32 skip = 0; michael@0: michael@0: png_debug(1, "in png_handle_tEXt"); michael@0: michael@0: #ifdef PNG_USER_LIMITS_SUPPORTED michael@0: if (png_ptr->user_chunk_cache_max != 0) michael@0: { michael@0: if (png_ptr->user_chunk_cache_max == 1) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: return; michael@0: } michael@0: michael@0: if (--png_ptr->user_chunk_cache_max == 1) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "no space in chunk cache"); michael@0: return; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: if (!(png_ptr->mode & PNG_HAVE_IHDR)) michael@0: png_chunk_error(png_ptr, "missing IHDR"); michael@0: michael@0: if (png_ptr->mode & PNG_HAVE_IDAT) michael@0: png_ptr->mode |= PNG_AFTER_IDAT; michael@0: michael@0: #ifdef PNG_MAX_MALLOC_64K michael@0: if (length > 65535U) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "too large to fit in memory"); michael@0: return; michael@0: } michael@0: #endif michael@0: michael@0: buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/); michael@0: michael@0: if (buffer == NULL) michael@0: { michael@0: png_chunk_benign_error(png_ptr, "out of memory"); michael@0: return; michael@0: } michael@0: michael@0: png_crc_read(png_ptr, buffer, length); michael@0: michael@0: if (png_crc_finish(png_ptr, skip)) michael@0: return; michael@0: michael@0: key = (png_charp)buffer; michael@0: key[length] = 0; michael@0: michael@0: for (text = key; *text; text++) michael@0: /* Empty loop to find end of key */ ; michael@0: michael@0: if (text != key + length) michael@0: text++; michael@0: michael@0: text_info.compression = PNG_TEXT_COMPRESSION_NONE; michael@0: text_info.key = key; michael@0: text_info.lang = NULL; michael@0: text_info.lang_key = NULL; michael@0: text_info.itxt_length = 0; michael@0: text_info.text = text; michael@0: text_info.text_length = strlen(text); michael@0: michael@0: if (png_set_text_2(png_ptr, info_ptr, &text_info, 1)) michael@0: png_warning(png_ptr, "Insufficient memory to process text chunk"); michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_zTXt_SUPPORTED michael@0: /* Note: this does not correctly handle chunks that are > 64K under DOS */ michael@0: void /* PRIVATE */ michael@0: png_handle_zTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) michael@0: { michael@0: png_const_charp errmsg = NULL; michael@0: png_bytep buffer; michael@0: png_uint_32 keyword_length; michael@0: michael@0: png_debug(1, "in png_handle_zTXt"); michael@0: michael@0: #ifdef PNG_USER_LIMITS_SUPPORTED michael@0: if (png_ptr->user_chunk_cache_max != 0) michael@0: { michael@0: if (png_ptr->user_chunk_cache_max == 1) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: return; michael@0: } michael@0: michael@0: if (--png_ptr->user_chunk_cache_max == 1) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "no space in chunk cache"); michael@0: return; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: if (!(png_ptr->mode & PNG_HAVE_IHDR)) michael@0: png_chunk_error(png_ptr, "missing IHDR"); michael@0: michael@0: if (png_ptr->mode & PNG_HAVE_IDAT) michael@0: png_ptr->mode |= PNG_AFTER_IDAT; michael@0: michael@0: buffer = png_read_buffer(png_ptr, length, 2/*silent*/); michael@0: michael@0: if (buffer == NULL) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "out of memory"); michael@0: return; michael@0: } michael@0: michael@0: png_crc_read(png_ptr, buffer, length); michael@0: michael@0: if (png_crc_finish(png_ptr, 0)) michael@0: return; michael@0: michael@0: /* TODO: also check that the keyword contents match the spec! */ michael@0: for (keyword_length = 0; michael@0: keyword_length < length && buffer[keyword_length] != 0; michael@0: ++keyword_length) michael@0: /* Empty loop to find end of name */ ; michael@0: michael@0: if (keyword_length > 79 || keyword_length < 1) michael@0: errmsg = "bad keyword"; michael@0: michael@0: /* zTXt must have some LZ data after the keyword, although it may expand to michael@0: * zero bytes; we need a '\0' at the end of the keyword, the compression type michael@0: * then the LZ data: michael@0: */ michael@0: else if (keyword_length + 3 > length) michael@0: errmsg = "truncated"; michael@0: michael@0: else if (buffer[keyword_length+1] != PNG_COMPRESSION_TYPE_BASE) michael@0: errmsg = "unknown compression type"; michael@0: michael@0: else michael@0: { michael@0: png_alloc_size_t uncompressed_length = PNG_SIZE_MAX; michael@0: michael@0: /* TODO: at present png_decompress_chunk imposes a single application michael@0: * level memory limit, this should be split to different values for iCCP michael@0: * and text chunks. michael@0: */ michael@0: if (png_decompress_chunk(png_ptr, length, keyword_length+2, michael@0: &uncompressed_length, 1/*terminate*/) == Z_STREAM_END) michael@0: { michael@0: png_text text; michael@0: michael@0: /* It worked; png_ptr->read_buffer now looks like a tEXt chunk except michael@0: * for the extra compression type byte and the fact that it isn't michael@0: * necessarily '\0' terminated. michael@0: */ michael@0: buffer = png_ptr->read_buffer; michael@0: buffer[uncompressed_length+(keyword_length+2)] = 0; michael@0: michael@0: text.compression = PNG_TEXT_COMPRESSION_zTXt; michael@0: text.key = (png_charp)buffer; michael@0: text.text = (png_charp)(buffer + keyword_length+2); michael@0: text.text_length = uncompressed_length; michael@0: text.itxt_length = 0; michael@0: text.lang = NULL; michael@0: text.lang_key = NULL; michael@0: michael@0: if (png_set_text_2(png_ptr, info_ptr, &text, 1)) michael@0: errmsg = "insufficient memory"; michael@0: } michael@0: michael@0: else michael@0: errmsg = png_ptr->zstream.msg; michael@0: } michael@0: michael@0: if (errmsg != NULL) michael@0: png_chunk_benign_error(png_ptr, errmsg); michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_iTXt_SUPPORTED michael@0: /* Note: this does not correctly handle chunks that are > 64K under DOS */ michael@0: void /* PRIVATE */ michael@0: png_handle_iTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) michael@0: { michael@0: png_const_charp errmsg = NULL; michael@0: png_bytep buffer; michael@0: png_uint_32 prefix_length; michael@0: michael@0: png_debug(1, "in png_handle_iTXt"); michael@0: michael@0: #ifdef PNG_USER_LIMITS_SUPPORTED michael@0: if (png_ptr->user_chunk_cache_max != 0) michael@0: { michael@0: if (png_ptr->user_chunk_cache_max == 1) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: return; michael@0: } michael@0: michael@0: if (--png_ptr->user_chunk_cache_max == 1) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "no space in chunk cache"); michael@0: return; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: if (!(png_ptr->mode & PNG_HAVE_IHDR)) michael@0: png_chunk_error(png_ptr, "missing IHDR"); michael@0: michael@0: if (png_ptr->mode & PNG_HAVE_IDAT) michael@0: png_ptr->mode |= PNG_AFTER_IDAT; michael@0: michael@0: buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/); michael@0: michael@0: if (buffer == NULL) michael@0: { michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "out of memory"); michael@0: return; michael@0: } michael@0: michael@0: png_crc_read(png_ptr, buffer, length); michael@0: michael@0: if (png_crc_finish(png_ptr, 0)) michael@0: return; michael@0: michael@0: /* First the keyword. */ michael@0: for (prefix_length=0; michael@0: prefix_length < length && buffer[prefix_length] != 0; michael@0: ++prefix_length) michael@0: /* Empty loop */ ; michael@0: michael@0: /* Perform a basic check on the keyword length here. */ michael@0: if (prefix_length > 79 || prefix_length < 1) michael@0: errmsg = "bad keyword"; michael@0: michael@0: /* Expect keyword, compression flag, compression type, language, translated michael@0: * keyword (both may be empty but are 0 terminated) then the text, which may michael@0: * be empty. michael@0: */ michael@0: else if (prefix_length + 5 > length) michael@0: errmsg = "truncated"; michael@0: michael@0: else if (buffer[prefix_length+1] == 0 || michael@0: (buffer[prefix_length+1] == 1 && michael@0: buffer[prefix_length+2] == PNG_COMPRESSION_TYPE_BASE)) michael@0: { michael@0: int compressed = buffer[prefix_length+1] != 0; michael@0: png_uint_32 language_offset, translated_keyword_offset; michael@0: png_alloc_size_t uncompressed_length = 0; michael@0: michael@0: /* Now the language tag */ michael@0: prefix_length += 3; michael@0: language_offset = prefix_length; michael@0: michael@0: for (; prefix_length < length && buffer[prefix_length] != 0; michael@0: ++prefix_length) michael@0: /* Empty loop */ ; michael@0: michael@0: /* WARNING: the length may be invalid here, this is checked below. */ michael@0: translated_keyword_offset = ++prefix_length; michael@0: michael@0: for (; prefix_length < length && buffer[prefix_length] != 0; michael@0: ++prefix_length) michael@0: /* Empty loop */ ; michael@0: michael@0: /* prefix_length should now be at the trailing '\0' of the translated michael@0: * keyword, but it may already be over the end. None of this arithmetic michael@0: * can overflow because chunks are at most 2^31 bytes long, but on 16-bit michael@0: * systems the available allocaton may overflow. michael@0: */ michael@0: ++prefix_length; michael@0: michael@0: if (!compressed && prefix_length <= length) michael@0: uncompressed_length = length - prefix_length; michael@0: michael@0: else if (compressed && prefix_length < length) michael@0: { michael@0: uncompressed_length = PNG_SIZE_MAX; michael@0: michael@0: /* TODO: at present png_decompress_chunk imposes a single application michael@0: * level memory limit, this should be split to different values for michael@0: * iCCP and text chunks. michael@0: */ michael@0: if (png_decompress_chunk(png_ptr, length, prefix_length, michael@0: &uncompressed_length, 1/*terminate*/) == Z_STREAM_END) michael@0: buffer = png_ptr->read_buffer; michael@0: michael@0: else michael@0: errmsg = png_ptr->zstream.msg; michael@0: } michael@0: michael@0: else michael@0: errmsg = "truncated"; michael@0: michael@0: if (errmsg == NULL) michael@0: { michael@0: png_text text; michael@0: michael@0: buffer[uncompressed_length+prefix_length] = 0; michael@0: michael@0: if (compressed) michael@0: text.compression = PNG_ITXT_COMPRESSION_NONE; michael@0: michael@0: else michael@0: text.compression = PNG_ITXT_COMPRESSION_zTXt; michael@0: michael@0: text.key = (png_charp)buffer; michael@0: text.lang = (png_charp)buffer + language_offset; michael@0: text.lang_key = (png_charp)buffer + translated_keyword_offset; michael@0: text.text = (png_charp)buffer + prefix_length; michael@0: text.text_length = 0; michael@0: text.itxt_length = uncompressed_length; michael@0: michael@0: if (png_set_text_2(png_ptr, info_ptr, &text, 1)) michael@0: errmsg = "insufficient memory"; michael@0: } michael@0: } michael@0: michael@0: else michael@0: errmsg = "bad compression info"; michael@0: michael@0: if (errmsg != NULL) michael@0: png_chunk_benign_error(png_ptr, errmsg); michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_APNG_SUPPORTED michael@0: void /* PRIVATE */ michael@0: png_handle_acTL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) michael@0: { michael@0: png_byte data[8]; michael@0: png_uint_32 num_frames; michael@0: png_uint_32 num_plays; michael@0: png_uint_32 didSet; michael@0: michael@0: png_debug(1, "in png_handle_acTL"); michael@0: michael@0: if (!(png_ptr->mode & PNG_HAVE_IHDR)) michael@0: { michael@0: png_error(png_ptr, "Missing IHDR before acTL"); michael@0: } michael@0: else if (png_ptr->mode & PNG_HAVE_IDAT) michael@0: { michael@0: png_warning(png_ptr, "Invalid acTL after IDAT skipped"); michael@0: png_crc_finish(png_ptr, length); michael@0: return; michael@0: } michael@0: else if (png_ptr->mode & PNG_HAVE_acTL) michael@0: { michael@0: png_warning(png_ptr, "Duplicate acTL skipped"); michael@0: png_crc_finish(png_ptr, length); michael@0: return; michael@0: } michael@0: else if (length != 8) michael@0: { michael@0: png_warning(png_ptr, "acTL with invalid length skipped"); michael@0: png_crc_finish(png_ptr, length); michael@0: return; michael@0: } michael@0: michael@0: png_crc_read(png_ptr, data, 8); michael@0: png_crc_finish(png_ptr, 0); michael@0: michael@0: num_frames = png_get_uint_31(png_ptr, data); michael@0: num_plays = png_get_uint_31(png_ptr, data + 4); michael@0: michael@0: /* the set function will do error checking on num_frames */ michael@0: didSet = png_set_acTL(png_ptr, info_ptr, num_frames, num_plays); michael@0: if(didSet) michael@0: png_ptr->mode |= PNG_HAVE_acTL; michael@0: } michael@0: michael@0: void /* PRIVATE */ michael@0: png_handle_fcTL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) michael@0: { michael@0: png_byte data[22]; michael@0: png_uint_32 width; michael@0: png_uint_32 height; michael@0: png_uint_32 x_offset; michael@0: png_uint_32 y_offset; michael@0: png_uint_16 delay_num; michael@0: png_uint_16 delay_den; michael@0: png_byte dispose_op; michael@0: png_byte blend_op; michael@0: michael@0: png_debug(1, "in png_handle_fcTL"); michael@0: michael@0: png_ensure_sequence_number(png_ptr, length); michael@0: michael@0: if (!(png_ptr->mode & PNG_HAVE_IHDR)) michael@0: { michael@0: png_error(png_ptr, "Missing IHDR before fcTL"); michael@0: } michael@0: else if (png_ptr->mode & PNG_HAVE_IDAT) michael@0: { michael@0: /* for any frames other then the first this message may be misleading, michael@0: * but correct. PNG_HAVE_IDAT is unset before the frame head is read michael@0: * i can't think of a better message */ michael@0: png_warning(png_ptr, "Invalid fcTL after IDAT skipped"); michael@0: png_crc_finish(png_ptr, length-4); michael@0: return; michael@0: } michael@0: else if (png_ptr->mode & PNG_HAVE_fcTL) michael@0: { michael@0: png_warning(png_ptr, "Duplicate fcTL within one frame skipped"); michael@0: png_crc_finish(png_ptr, length-4); michael@0: return; michael@0: } michael@0: else if (length != 26) michael@0: { michael@0: png_warning(png_ptr, "fcTL with invalid length skipped"); michael@0: png_crc_finish(png_ptr, length-4); michael@0: return; michael@0: } michael@0: michael@0: png_crc_read(png_ptr, data, 22); michael@0: png_crc_finish(png_ptr, 0); michael@0: michael@0: width = png_get_uint_31(png_ptr, data); michael@0: height = png_get_uint_31(png_ptr, data + 4); michael@0: x_offset = png_get_uint_31(png_ptr, data + 8); michael@0: y_offset = png_get_uint_31(png_ptr, data + 12); michael@0: delay_num = png_get_uint_16(data + 16); michael@0: delay_den = png_get_uint_16(data + 18); michael@0: dispose_op = data[20]; michael@0: blend_op = data[21]; michael@0: michael@0: if (png_ptr->num_frames_read == 0 && (x_offset != 0 || y_offset != 0)) michael@0: { michael@0: png_warning(png_ptr, "fcTL for the first frame must have zero offset"); michael@0: return; michael@0: } michael@0: michael@0: if (info_ptr != NULL) michael@0: { michael@0: if (png_ptr->num_frames_read == 0 && michael@0: (width != info_ptr->width || height != info_ptr->height)) michael@0: { michael@0: png_warning(png_ptr, "size in first frame's fcTL must match " michael@0: "the size in IHDR"); michael@0: return; michael@0: } michael@0: michael@0: /* The set function will do more error checking */ michael@0: png_set_next_frame_fcTL(png_ptr, info_ptr, width, height, michael@0: x_offset, y_offset, delay_num, delay_den, michael@0: dispose_op, blend_op); michael@0: michael@0: png_read_reinit(png_ptr, info_ptr); michael@0: michael@0: png_ptr->mode |= PNG_HAVE_fcTL; michael@0: } michael@0: } michael@0: michael@0: void /* PRIVATE */ michael@0: png_have_info(png_structp png_ptr, png_infop info_ptr) michael@0: { michael@0: if((info_ptr->valid & PNG_INFO_acTL) && !(info_ptr->valid & PNG_INFO_fcTL)) michael@0: { michael@0: png_ptr->apng_flags |= PNG_FIRST_FRAME_HIDDEN; michael@0: info_ptr->num_frames++; michael@0: } michael@0: } michael@0: michael@0: void /* PRIVATE */ michael@0: png_handle_fdAT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) michael@0: { michael@0: png_ensure_sequence_number(png_ptr, length); michael@0: michael@0: /* This function is only called from png_read_end(), png_read_info(), michael@0: * and png_push_read_chunk() which means that: michael@0: * - the user doesn't want to read this frame michael@0: * - or this is an out-of-place fdAT michael@0: * in either case it is safe to ignore the chunk with a warning */ michael@0: png_warning(png_ptr, "ignoring fdAT chunk"); michael@0: png_crc_finish(png_ptr, length - 4); michael@0: PNG_UNUSED(info_ptr) michael@0: } michael@0: michael@0: void /* PRIVATE */ michael@0: png_ensure_sequence_number(png_structp png_ptr, png_uint_32 length) michael@0: { michael@0: png_byte data[4]; michael@0: png_uint_32 sequence_number; michael@0: michael@0: if (length < 4) michael@0: png_error(png_ptr, "invalid fcTL or fdAT chunk found"); michael@0: michael@0: png_crc_read(png_ptr, data, 4); michael@0: sequence_number = png_get_uint_31(png_ptr, data); michael@0: michael@0: if (sequence_number != png_ptr->next_seq_num) michael@0: png_error(png_ptr, "fcTL or fdAT chunk with out-of-order sequence " michael@0: "number found"); michael@0: michael@0: png_ptr->next_seq_num++; michael@0: } michael@0: #endif /* PNG_READ_APNG_SUPPORTED */ michael@0: michael@0: #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED michael@0: /* Utility function for png_handle_unknown; set up png_ptr::unknown_chunk */ michael@0: static int michael@0: png_cache_unknown_chunk(png_structrp png_ptr, png_uint_32 length) michael@0: { michael@0: png_alloc_size_t limit = PNG_SIZE_MAX; michael@0: michael@0: if (png_ptr->unknown_chunk.data != NULL) michael@0: { michael@0: png_free(png_ptr, png_ptr->unknown_chunk.data); michael@0: png_ptr->unknown_chunk.data = NULL; michael@0: } michael@0: michael@0: # ifdef PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED michael@0: if (png_ptr->user_chunk_malloc_max > 0 && michael@0: png_ptr->user_chunk_malloc_max < limit) michael@0: limit = png_ptr->user_chunk_malloc_max; michael@0: michael@0: # elif PNG_USER_CHUNK_MALLOC_MAX > 0 michael@0: if (PNG_USER_CHUNK_MALLOC_MAX < limit) michael@0: limit = PNG_USER_CHUNK_MALLOC_MAX; michael@0: # endif michael@0: michael@0: if (length <= limit) michael@0: { michael@0: PNG_CSTRING_FROM_CHUNK(png_ptr->unknown_chunk.name, png_ptr->chunk_name); michael@0: /* The following is safe because of the PNG_SIZE_MAX init above */ michael@0: png_ptr->unknown_chunk.size = (png_size_t)length/*SAFE*/; michael@0: /* 'mode' is a flag array, only the bottom four bits matter here */ michael@0: png_ptr->unknown_chunk.location = (png_byte)png_ptr->mode/*SAFE*/; michael@0: michael@0: if (length == 0) michael@0: png_ptr->unknown_chunk.data = NULL; michael@0: michael@0: else michael@0: { michael@0: /* Do a 'warn' here - it is handled below. */ michael@0: png_ptr->unknown_chunk.data = png_voidcast(png_bytep, michael@0: png_malloc_warn(png_ptr, length)); michael@0: } michael@0: } michael@0: michael@0: if (png_ptr->unknown_chunk.data == NULL && length > 0) michael@0: { michael@0: /* This is benign because we clean up correctly */ michael@0: png_crc_finish(png_ptr, length); michael@0: png_chunk_benign_error(png_ptr, "unknown chunk exceeds memory limits"); michael@0: return 0; michael@0: } michael@0: michael@0: else michael@0: { michael@0: if (length > 0) michael@0: png_crc_read(png_ptr, png_ptr->unknown_chunk.data, length); michael@0: png_crc_finish(png_ptr, 0); michael@0: return 1; michael@0: } michael@0: } michael@0: #endif /* PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */ michael@0: michael@0: /* Handle an unknown, or known but disabled, chunk */ michael@0: void /* PRIVATE */ michael@0: png_handle_unknown(png_structrp png_ptr, png_inforp info_ptr, michael@0: png_uint_32 length, int keep) michael@0: { michael@0: int handled = 0; /* the chunk was handled */ michael@0: michael@0: png_debug(1, "in png_handle_unknown"); michael@0: michael@0: #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED michael@0: /* NOTE: this code is based on the code in libpng-1.4.12 except for fixing michael@0: * the bug which meant that setting a non-default behavior for a specific michael@0: * chunk would be ignored (the default was always used unless a user michael@0: * callback was installed). michael@0: * michael@0: * 'keep' is the value from the png_chunk_unknown_handling, the setting for michael@0: * this specific chunk_name, if PNG_HANDLE_AS_UNKNOWN_SUPPORTED, if not it michael@0: * will always be PNG_HANDLE_CHUNK_AS_DEFAULT and it needs to be set here. michael@0: * This is just an optimization to avoid multiple calls to the lookup michael@0: * function. michael@0: */ michael@0: # ifndef PNG_HANDLE_AS_UNKNOWN_SUPPORTED michael@0: # ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED michael@0: keep = png_chunk_unknown_handling(png_ptr, png_ptr->chunk_name); michael@0: # endif michael@0: # endif michael@0: michael@0: /* One of the following methods will read the chunk or skip it (at least one michael@0: * of these is always defined because this is the only way to switch on michael@0: * PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) michael@0: */ michael@0: # ifdef PNG_READ_USER_CHUNKS_SUPPORTED michael@0: /* The user callback takes precedence over the chunk keep value, but the michael@0: * keep value is still required to validate a save of a critical chunk. michael@0: */ michael@0: if (png_ptr->read_user_chunk_fn != NULL) michael@0: { michael@0: if (png_cache_unknown_chunk(png_ptr, length)) michael@0: { michael@0: /* Callback to user unknown chunk handler */ michael@0: int ret = (*(png_ptr->read_user_chunk_fn))(png_ptr, michael@0: &png_ptr->unknown_chunk); michael@0: michael@0: /* ret is: michael@0: * negative: An error occured, png_chunk_error will be called. michael@0: * zero: The chunk was not handled, the chunk will be discarded michael@0: * unless png_set_keep_unknown_chunks has been used to set michael@0: * a 'keep' behavior for this particular chunk, in which michael@0: * case that will be used. A critical chunk will cause an michael@0: * error at this point unless it is to be saved. michael@0: * positive: The chunk was handled, libpng will ignore/discard it. michael@0: */ michael@0: if (ret < 0) michael@0: png_chunk_error(png_ptr, "error in user chunk"); michael@0: michael@0: else if (ret == 0) michael@0: { michael@0: /* If the keep value is 'default' or 'never' override it, but michael@0: * still error out on critical chunks unless the keep value is michael@0: * 'always' While this is weird it is the behavior in 1.4.12. michael@0: * A possible improvement would be to obey the value set for the michael@0: * chunk, but this would be an API change that would probably michael@0: * damage some applications. michael@0: * michael@0: * The png_app_warning below catches the case that matters, where michael@0: * the application has not set specific save or ignore for this michael@0: * chunk or global save or ignore. michael@0: */ michael@0: if (keep < PNG_HANDLE_CHUNK_IF_SAFE) michael@0: { michael@0: # ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED michael@0: if (png_ptr->unknown_default < PNG_HANDLE_CHUNK_IF_SAFE) michael@0: { michael@0: png_chunk_warning(png_ptr, "Saving unknown chunk:"); michael@0: png_app_warning(png_ptr, michael@0: "forcing save of an unhandled chunk;" michael@0: " please call png_set_keep_unknown_chunks"); michael@0: /* with keep = PNG_HANDLE_CHUNK_IF_SAFE */ michael@0: } michael@0: # endif michael@0: keep = PNG_HANDLE_CHUNK_IF_SAFE; michael@0: } michael@0: } michael@0: michael@0: else /* chunk was handled */ michael@0: { michael@0: handled = 1; michael@0: /* Critical chunks can be safely discarded at this point. */ michael@0: keep = PNG_HANDLE_CHUNK_NEVER; michael@0: } michael@0: } michael@0: michael@0: else michael@0: keep = PNG_HANDLE_CHUNK_NEVER; /* insufficient memory */ michael@0: } michael@0: michael@0: else michael@0: /* Use the SAVE_UNKNOWN_CHUNKS code or skip the chunk */ michael@0: # endif /* PNG_READ_USER_CHUNKS_SUPPORTED */ michael@0: michael@0: # ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED michael@0: { michael@0: /* keep is currently just the per-chunk setting, if there was no michael@0: * setting change it to the global default now (not that this may michael@0: * still be AS_DEFAULT) then obtain the cache of the chunk if required, michael@0: * if not simply skip the chunk. michael@0: */ michael@0: if (keep == PNG_HANDLE_CHUNK_AS_DEFAULT) michael@0: keep = png_ptr->unknown_default; michael@0: michael@0: if (keep == PNG_HANDLE_CHUNK_ALWAYS || michael@0: (keep == PNG_HANDLE_CHUNK_IF_SAFE && michael@0: PNG_CHUNK_ANCILLARY(png_ptr->chunk_name))) michael@0: { michael@0: if (!png_cache_unknown_chunk(png_ptr, length)) michael@0: keep = PNG_HANDLE_CHUNK_NEVER; michael@0: } michael@0: michael@0: else michael@0: png_crc_finish(png_ptr, length); michael@0: } michael@0: # else michael@0: # ifndef PNG_READ_USER_CHUNKS_SUPPORTED michael@0: # error no method to support READ_UNKNOWN_CHUNKS michael@0: # endif michael@0: michael@0: { michael@0: /* If here there is no read callback pointer set and no support is michael@0: * compiled in to just save the unknown chunks, so simply skip this michael@0: * chunk. If 'keep' is something other than AS_DEFAULT or NEVER then michael@0: * the app has erroneously asked for unknown chunk saving when there michael@0: * is no support. michael@0: */ michael@0: if (keep > PNG_HANDLE_CHUNK_NEVER) michael@0: png_app_error(png_ptr, "no unknown chunk support available"); michael@0: michael@0: png_crc_finish(png_ptr, length); michael@0: } michael@0: # endif michael@0: michael@0: # ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED michael@0: /* Now store the chunk in the chunk list if appropriate, and if the limits michael@0: * permit it. michael@0: */ michael@0: if (keep == PNG_HANDLE_CHUNK_ALWAYS || michael@0: (keep == PNG_HANDLE_CHUNK_IF_SAFE && michael@0: PNG_CHUNK_ANCILLARY(png_ptr->chunk_name))) michael@0: { michael@0: # ifdef PNG_USER_LIMITS_SUPPORTED michael@0: switch (png_ptr->user_chunk_cache_max) michael@0: { michael@0: case 2: michael@0: png_ptr->user_chunk_cache_max = 1; michael@0: png_chunk_benign_error(png_ptr, "no space in chunk cache"); michael@0: /* FALL THROUGH */ michael@0: case 1: michael@0: /* NOTE: prior to 1.6.0 this case resulted in an unknown critical michael@0: * chunk being skipped, now there will be a hard error below. michael@0: */ michael@0: break; michael@0: michael@0: default: /* not at limit */ michael@0: --(png_ptr->user_chunk_cache_max); michael@0: /* FALL THROUGH */ michael@0: case 0: /* no limit */ michael@0: # endif /* PNG_USER_LIMITS_SUPPORTED */ michael@0: /* Here when the limit isn't reached or when limits are compiled michael@0: * out; store the chunk. michael@0: */ michael@0: png_set_unknown_chunks(png_ptr, info_ptr, michael@0: &png_ptr->unknown_chunk, 1); michael@0: handled = 1; michael@0: # ifdef PNG_USER_LIMITS_SUPPORTED michael@0: break; michael@0: } michael@0: # endif michael@0: } michael@0: # else /* no store support: the chunk must be handled by the user callback */ michael@0: PNG_UNUSED(info_ptr) michael@0: # endif michael@0: michael@0: /* Regardless of the error handling below the cached data (if any) can be michael@0: * freed now. Notice that the data is not freed if there is a png_error, but michael@0: * it will be freed by destroy_read_struct. michael@0: */ michael@0: if (png_ptr->unknown_chunk.data != NULL) michael@0: png_free(png_ptr, png_ptr->unknown_chunk.data); michael@0: png_ptr->unknown_chunk.data = NULL; michael@0: michael@0: #else /* !PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */ michael@0: /* There is no support to read an unknown chunk, so just skip it. */ michael@0: png_crc_finish(png_ptr, length); michael@0: PNG_UNUSED(info_ptr) michael@0: PNG_UNUSED(keep) michael@0: #endif /* !PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */ michael@0: michael@0: /* Check for unhandled critical chunks */ michael@0: if (!handled && PNG_CHUNK_CRITICAL(png_ptr->chunk_name)) michael@0: png_chunk_error(png_ptr, "unhandled critical chunk"); michael@0: } michael@0: michael@0: /* This function is called to verify that a chunk name is valid. michael@0: * This function can't have the "critical chunk check" incorporated michael@0: * into it, since in the future we will need to be able to call user michael@0: * functions to handle unknown critical chunks after we check that michael@0: * the chunk name itself is valid. michael@0: */ michael@0: michael@0: /* Bit hacking: the test for an invalid byte in the 4 byte chunk name is: michael@0: * michael@0: * ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97)) michael@0: */ michael@0: michael@0: void /* PRIVATE */ michael@0: png_check_chunk_name(png_structrp png_ptr, png_uint_32 chunk_name) michael@0: { michael@0: int i; michael@0: michael@0: png_debug(1, "in png_check_chunk_name"); michael@0: michael@0: for (i=1; i<=4; ++i) michael@0: { michael@0: int c = chunk_name & 0xff; michael@0: michael@0: if (c < 65 || c > 122 || (c > 90 && c < 97)) michael@0: png_chunk_error(png_ptr, "invalid chunk type"); michael@0: michael@0: chunk_name >>= 8; michael@0: } michael@0: } michael@0: michael@0: /* Combines the row recently read in with the existing pixels in the row. This michael@0: * routine takes care of alpha and transparency if requested. This routine also michael@0: * handles the two methods of progressive display of interlaced images, michael@0: * depending on the 'display' value; if 'display' is true then the whole row michael@0: * (dp) is filled from the start by replicating the available pixels. If michael@0: * 'display' is false only those pixels present in the pass are filled in. michael@0: */ michael@0: void /* PRIVATE */ michael@0: png_combine_row(png_const_structrp png_ptr, png_bytep dp, int display) michael@0: { michael@0: unsigned int pixel_depth = png_ptr->transformed_pixel_depth; michael@0: png_const_bytep sp = png_ptr->row_buf + 1; michael@0: png_uint_32 row_width = png_ptr->width; michael@0: unsigned int pass = png_ptr->pass; michael@0: png_bytep end_ptr = 0; michael@0: png_byte end_byte = 0; michael@0: unsigned int end_mask; michael@0: michael@0: png_debug(1, "in png_combine_row"); michael@0: michael@0: /* Added in 1.5.6: it should not be possible to enter this routine until at michael@0: * least one row has been read from the PNG data and transformed. michael@0: */ michael@0: if (pixel_depth == 0) michael@0: png_error(png_ptr, "internal row logic error"); michael@0: michael@0: /* Added in 1.5.4: the pixel depth should match the information returned by michael@0: * any call to png_read_update_info at this point. Do not continue if we got michael@0: * this wrong. michael@0: */ michael@0: if (png_ptr->info_rowbytes != 0 && png_ptr->info_rowbytes != michael@0: PNG_ROWBYTES(pixel_depth, row_width)) michael@0: png_error(png_ptr, "internal row size calculation error"); michael@0: michael@0: /* Don't expect this to ever happen: */ michael@0: if (row_width == 0) michael@0: png_error(png_ptr, "internal row width error"); michael@0: michael@0: /* Preserve the last byte in cases where only part of it will be overwritten, michael@0: * the multiply below may overflow, we don't care because ANSI-C guarantees michael@0: * we get the low bits. michael@0: */ michael@0: end_mask = (pixel_depth * row_width) & 7; michael@0: if (end_mask != 0) michael@0: { michael@0: /* end_ptr == NULL is a flag to say do nothing */ michael@0: end_ptr = dp + PNG_ROWBYTES(pixel_depth, row_width) - 1; michael@0: end_byte = *end_ptr; michael@0: # ifdef PNG_READ_PACKSWAP_SUPPORTED michael@0: if (png_ptr->transformations & PNG_PACKSWAP) /* little-endian byte */ michael@0: end_mask = 0xff << end_mask; michael@0: michael@0: else /* big-endian byte */ michael@0: # endif michael@0: end_mask = 0xff >> end_mask; michael@0: /* end_mask is now the bits to *keep* from the destination row */ michael@0: } michael@0: michael@0: /* For non-interlaced images this reduces to a memcpy(). A memcpy() michael@0: * will also happen if interlacing isn't supported or if the application michael@0: * does not call png_set_interlace_handling(). In the latter cases the michael@0: * caller just gets a sequence of the unexpanded rows from each interlace michael@0: * pass. michael@0: */ michael@0: #ifdef PNG_READ_INTERLACING_SUPPORTED michael@0: if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE) && michael@0: pass < 6 && (display == 0 || michael@0: /* The following copies everything for 'display' on passes 0, 2 and 4. */ michael@0: (display == 1 && (pass & 1) != 0))) michael@0: { michael@0: /* Narrow images may have no bits in a pass; the caller should handle michael@0: * this, but this test is cheap: michael@0: */ michael@0: if (row_width <= PNG_PASS_START_COL(pass)) michael@0: return; michael@0: michael@0: if (pixel_depth < 8) michael@0: { michael@0: /* For pixel depths up to 4 bpp the 8-pixel mask can be expanded to fit michael@0: * into 32 bits, then a single loop over the bytes using the four byte michael@0: * values in the 32-bit mask can be used. For the 'display' option the michael@0: * expanded mask may also not require any masking within a byte. To michael@0: * make this work the PACKSWAP option must be taken into account - it michael@0: * simply requires the pixels to be reversed in each byte. michael@0: * michael@0: * The 'regular' case requires a mask for each of the first 6 passes, michael@0: * the 'display' case does a copy for the even passes in the range michael@0: * 0..6. This has already been handled in the test above. michael@0: * michael@0: * The masks are arranged as four bytes with the first byte to use in michael@0: * the lowest bits (little-endian) regardless of the order (PACKSWAP or michael@0: * not) of the pixels in each byte. michael@0: * michael@0: * NOTE: the whole of this logic depends on the caller of this function michael@0: * only calling it on rows appropriate to the pass. This function only michael@0: * understands the 'x' logic; the 'y' logic is handled by the caller. michael@0: * michael@0: * The following defines allow generation of compile time constant bit michael@0: * masks for each pixel depth and each possibility of swapped or not michael@0: * swapped bytes. Pass 'p' is in the range 0..6; 'x', a pixel index, michael@0: * is in the range 0..7; and the result is 1 if the pixel is to be michael@0: * copied in the pass, 0 if not. 'S' is for the sparkle method, 'B' michael@0: * for the block method. michael@0: * michael@0: * With some compilers a compile time expression of the general form: michael@0: * michael@0: * (shift >= 32) ? (a >> (shift-32)) : (b >> shift) michael@0: * michael@0: * Produces warnings with values of 'shift' in the range 33 to 63 michael@0: * because the right hand side of the ?: expression is evaluated by michael@0: * the compiler even though it isn't used. Microsoft Visual C (various michael@0: * versions) and the Intel C compiler are known to do this. To avoid michael@0: * this the following macros are used in 1.5.6. This is a temporary michael@0: * solution to avoid destabilizing the code during the release process. michael@0: */ michael@0: # if PNG_USE_COMPILE_TIME_MASKS michael@0: # define PNG_LSR(x,s) ((x)>>((s) & 0x1f)) michael@0: # define PNG_LSL(x,s) ((x)<<((s) & 0x1f)) michael@0: # else michael@0: # define PNG_LSR(x,s) ((x)>>(s)) michael@0: # define PNG_LSL(x,s) ((x)<<(s)) michael@0: # endif michael@0: # define S_COPY(p,x) (((p)<4 ? PNG_LSR(0x80088822,(3-(p))*8+(7-(x))) :\ michael@0: PNG_LSR(0xaa55ff00,(7-(p))*8+(7-(x)))) & 1) michael@0: # define B_COPY(p,x) (((p)<4 ? PNG_LSR(0xff0fff33,(3-(p))*8+(7-(x))) :\ michael@0: PNG_LSR(0xff55ff00,(7-(p))*8+(7-(x)))) & 1) michael@0: michael@0: /* Return a mask for pass 'p' pixel 'x' at depth 'd'. The mask is michael@0: * little endian - the first pixel is at bit 0 - however the extra michael@0: * parameter 's' can be set to cause the mask position to be swapped michael@0: * within each byte, to match the PNG format. This is done by XOR of michael@0: * the shift with 7, 6 or 4 for bit depths 1, 2 and 4. michael@0: */ michael@0: # define PIXEL_MASK(p,x,d,s) \ michael@0: (PNG_LSL(((PNG_LSL(1U,(d)))-1),(((x)*(d))^((s)?8-(d):0)))) michael@0: michael@0: /* Hence generate the appropriate 'block' or 'sparkle' pixel copy mask. michael@0: */ michael@0: # define S_MASKx(p,x,d,s) (S_COPY(p,x)?PIXEL_MASK(p,x,d,s):0) michael@0: # define B_MASKx(p,x,d,s) (B_COPY(p,x)?PIXEL_MASK(p,x,d,s):0) michael@0: michael@0: /* Combine 8 of these to get the full mask. For the 1-bpp and 2-bpp michael@0: * cases the result needs replicating, for the 4-bpp case the above michael@0: * generates a full 32 bits. michael@0: */ michael@0: # define MASK_EXPAND(m,d) ((m)*((d)==1?0x01010101:((d)==2?0x00010001:1))) michael@0: michael@0: # define S_MASK(p,d,s) MASK_EXPAND(S_MASKx(p,0,d,s) + S_MASKx(p,1,d,s) +\ michael@0: S_MASKx(p,2,d,s) + S_MASKx(p,3,d,s) + S_MASKx(p,4,d,s) +\ michael@0: S_MASKx(p,5,d,s) + S_MASKx(p,6,d,s) + S_MASKx(p,7,d,s), d) michael@0: michael@0: # define B_MASK(p,d,s) MASK_EXPAND(B_MASKx(p,0,d,s) + B_MASKx(p,1,d,s) +\ michael@0: B_MASKx(p,2,d,s) + B_MASKx(p,3,d,s) + B_MASKx(p,4,d,s) +\ michael@0: B_MASKx(p,5,d,s) + B_MASKx(p,6,d,s) + B_MASKx(p,7,d,s), d) michael@0: michael@0: #if PNG_USE_COMPILE_TIME_MASKS michael@0: /* Utility macros to construct all the masks for a depth/swap michael@0: * combination. The 's' parameter says whether the format is PNG michael@0: * (big endian bytes) or not. Only the three odd-numbered passes are michael@0: * required for the display/block algorithm. michael@0: */ michael@0: # define S_MASKS(d,s) { S_MASK(0,d,s), S_MASK(1,d,s), S_MASK(2,d,s),\ michael@0: S_MASK(3,d,s), S_MASK(4,d,s), S_MASK(5,d,s) } michael@0: michael@0: # define B_MASKS(d,s) { B_MASK(1,d,s), S_MASK(3,d,s), S_MASK(5,d,s) } michael@0: michael@0: # define DEPTH_INDEX(d) ((d)==1?0:((d)==2?1:2)) michael@0: michael@0: /* Hence the pre-compiled masks indexed by PACKSWAP (or not), depth and michael@0: * then pass: michael@0: */ michael@0: static PNG_CONST png_uint_32 row_mask[2/*PACKSWAP*/][3/*depth*/][6] = michael@0: { michael@0: /* Little-endian byte masks for PACKSWAP */ michael@0: { S_MASKS(1,0), S_MASKS(2,0), S_MASKS(4,0) }, michael@0: /* Normal (big-endian byte) masks - PNG format */ michael@0: { S_MASKS(1,1), S_MASKS(2,1), S_MASKS(4,1) } michael@0: }; michael@0: michael@0: /* display_mask has only three entries for the odd passes, so index by michael@0: * pass>>1. michael@0: */ michael@0: static PNG_CONST png_uint_32 display_mask[2][3][3] = michael@0: { michael@0: /* Little-endian byte masks for PACKSWAP */ michael@0: { B_MASKS(1,0), B_MASKS(2,0), B_MASKS(4,0) }, michael@0: /* Normal (big-endian byte) masks - PNG format */ michael@0: { B_MASKS(1,1), B_MASKS(2,1), B_MASKS(4,1) } michael@0: }; michael@0: michael@0: # define MASK(pass,depth,display,png)\ michael@0: ((display)?display_mask[png][DEPTH_INDEX(depth)][pass>>1]:\ michael@0: row_mask[png][DEPTH_INDEX(depth)][pass]) michael@0: michael@0: #else /* !PNG_USE_COMPILE_TIME_MASKS */ michael@0: /* This is the runtime alternative: it seems unlikely that this will michael@0: * ever be either smaller or faster than the compile time approach. michael@0: */ michael@0: # define MASK(pass,depth,display,png)\ michael@0: ((display)?B_MASK(pass,depth,png):S_MASK(pass,depth,png)) michael@0: #endif /* !PNG_USE_COMPILE_TIME_MASKS */ michael@0: michael@0: /* Use the appropriate mask to copy the required bits. In some cases michael@0: * the byte mask will be 0 or 0xff, optimize these cases. row_width is michael@0: * the number of pixels, but the code copies bytes, so it is necessary michael@0: * to special case the end. michael@0: */ michael@0: png_uint_32 pixels_per_byte = 8 / pixel_depth; michael@0: png_uint_32 mask; michael@0: michael@0: # ifdef PNG_READ_PACKSWAP_SUPPORTED michael@0: if (png_ptr->transformations & PNG_PACKSWAP) michael@0: mask = MASK(pass, pixel_depth, display, 0); michael@0: michael@0: else michael@0: # endif michael@0: mask = MASK(pass, pixel_depth, display, 1); michael@0: michael@0: for (;;) michael@0: { michael@0: png_uint_32 m; michael@0: michael@0: /* It doesn't matter in the following if png_uint_32 has more than michael@0: * 32 bits because the high bits always match those in m<<24; it is, michael@0: * however, essential to use OR here, not +, because of this. michael@0: */ michael@0: m = mask; michael@0: mask = (m >> 8) | (m << 24); /* rotate right to good compilers */ michael@0: m &= 0xff; michael@0: michael@0: if (m != 0) /* something to copy */ michael@0: { michael@0: if (m != 0xff) michael@0: *dp = (png_byte)((*dp & ~m) | (*sp & m)); michael@0: else michael@0: *dp = *sp; michael@0: } michael@0: michael@0: /* NOTE: this may overwrite the last byte with garbage if the image michael@0: * is not an exact number of bytes wide; libpng has always done michael@0: * this. michael@0: */ michael@0: if (row_width <= pixels_per_byte) michael@0: break; /* May need to restore part of the last byte */ michael@0: michael@0: row_width -= pixels_per_byte; michael@0: ++dp; michael@0: ++sp; michael@0: } michael@0: } michael@0: michael@0: else /* pixel_depth >= 8 */ michael@0: { michael@0: unsigned int bytes_to_copy, bytes_to_jump; michael@0: michael@0: /* Validate the depth - it must be a multiple of 8 */ michael@0: if (pixel_depth & 7) michael@0: png_error(png_ptr, "invalid user transform pixel depth"); michael@0: michael@0: pixel_depth >>= 3; /* now in bytes */ michael@0: row_width *= pixel_depth; michael@0: michael@0: /* Regardless of pass number the Adam 7 interlace always results in a michael@0: * fixed number of pixels to copy then to skip. There may be a michael@0: * different number of pixels to skip at the start though. michael@0: */ michael@0: { michael@0: unsigned int offset = PNG_PASS_START_COL(pass) * pixel_depth; michael@0: michael@0: row_width -= offset; michael@0: dp += offset; michael@0: sp += offset; michael@0: } michael@0: michael@0: /* Work out the bytes to copy. */ michael@0: if (display) michael@0: { michael@0: /* When doing the 'block' algorithm the pixel in the pass gets michael@0: * replicated to adjacent pixels. This is why the even (0,2,4,6) michael@0: * passes are skipped above - the entire expanded row is copied. michael@0: */ michael@0: bytes_to_copy = (1<<((6-pass)>>1)) * pixel_depth; michael@0: michael@0: /* But don't allow this number to exceed the actual row width. */ michael@0: if (bytes_to_copy > row_width) michael@0: bytes_to_copy = row_width; michael@0: } michael@0: michael@0: else /* normal row; Adam7 only ever gives us one pixel to copy. */ michael@0: bytes_to_copy = pixel_depth; michael@0: michael@0: /* In Adam7 there is a constant offset between where the pixels go. */ michael@0: bytes_to_jump = PNG_PASS_COL_OFFSET(pass) * pixel_depth; michael@0: michael@0: /* And simply copy these bytes. Some optimization is possible here, michael@0: * depending on the value of 'bytes_to_copy'. Special case the low michael@0: * byte counts, which we know to be frequent. michael@0: * michael@0: * Notice that these cases all 'return' rather than 'break' - this michael@0: * avoids an unnecessary test on whether to restore the last byte michael@0: * below. michael@0: */ michael@0: switch (bytes_to_copy) michael@0: { michael@0: case 1: michael@0: for (;;) michael@0: { michael@0: *dp = *sp; michael@0: michael@0: if (row_width <= bytes_to_jump) michael@0: return; michael@0: michael@0: dp += bytes_to_jump; michael@0: sp += bytes_to_jump; michael@0: row_width -= bytes_to_jump; michael@0: } michael@0: michael@0: case 2: michael@0: /* There is a possibility of a partial copy at the end here; this michael@0: * slows the code down somewhat. michael@0: */ michael@0: do michael@0: { michael@0: dp[0] = sp[0], dp[1] = sp[1]; michael@0: michael@0: if (row_width <= bytes_to_jump) michael@0: return; michael@0: michael@0: sp += bytes_to_jump; michael@0: dp += bytes_to_jump; michael@0: row_width -= bytes_to_jump; michael@0: } michael@0: while (row_width > 1); michael@0: michael@0: /* And there can only be one byte left at this point: */ michael@0: *dp = *sp; michael@0: return; michael@0: michael@0: case 3: michael@0: /* This can only be the RGB case, so each copy is exactly one michael@0: * pixel and it is not necessary to check for a partial copy. michael@0: */ michael@0: for(;;) michael@0: { michael@0: dp[0] = sp[0], dp[1] = sp[1], dp[2] = sp[2]; michael@0: michael@0: if (row_width <= bytes_to_jump) michael@0: return; michael@0: michael@0: sp += bytes_to_jump; michael@0: dp += bytes_to_jump; michael@0: row_width -= bytes_to_jump; michael@0: } michael@0: michael@0: default: michael@0: #if PNG_ALIGN_TYPE != PNG_ALIGN_NONE michael@0: /* Check for double byte alignment and, if possible, use a michael@0: * 16-bit copy. Don't attempt this for narrow images - ones that michael@0: * are less than an interlace panel wide. Don't attempt it for michael@0: * wide bytes_to_copy either - use the memcpy there. michael@0: */ michael@0: if (bytes_to_copy < 16 /*else use memcpy*/ && michael@0: png_isaligned(dp, png_uint_16) && michael@0: png_isaligned(sp, png_uint_16) && michael@0: bytes_to_copy % (sizeof (png_uint_16)) == 0 && michael@0: bytes_to_jump % (sizeof (png_uint_16)) == 0) michael@0: { michael@0: /* Everything is aligned for png_uint_16 copies, but try for michael@0: * png_uint_32 first. michael@0: */ michael@0: if (png_isaligned(dp, png_uint_32) && michael@0: png_isaligned(sp, png_uint_32) && michael@0: bytes_to_copy % (sizeof (png_uint_32)) == 0 && michael@0: bytes_to_jump % (sizeof (png_uint_32)) == 0) michael@0: { michael@0: png_uint_32p dp32 = png_aligncast(png_uint_32p,dp); michael@0: png_const_uint_32p sp32 = png_aligncastconst( michael@0: png_const_uint_32p, sp); michael@0: size_t skip = (bytes_to_jump-bytes_to_copy) / michael@0: (sizeof (png_uint_32)); michael@0: michael@0: do michael@0: { michael@0: size_t c = bytes_to_copy; michael@0: do michael@0: { michael@0: *dp32++ = *sp32++; michael@0: c -= (sizeof (png_uint_32)); michael@0: } michael@0: while (c > 0); michael@0: michael@0: if (row_width <= bytes_to_jump) michael@0: return; michael@0: michael@0: dp32 += skip; michael@0: sp32 += skip; michael@0: row_width -= bytes_to_jump; michael@0: } michael@0: while (bytes_to_copy <= row_width); michael@0: michael@0: /* Get to here when the row_width truncates the final copy. michael@0: * There will be 1-3 bytes left to copy, so don't try the michael@0: * 16-bit loop below. michael@0: */ michael@0: dp = (png_bytep)dp32; michael@0: sp = (png_const_bytep)sp32; michael@0: do michael@0: *dp++ = *sp++; michael@0: while (--row_width > 0); michael@0: return; michael@0: } michael@0: michael@0: /* Else do it in 16-bit quantities, but only if the size is michael@0: * not too large. michael@0: */ michael@0: else michael@0: { michael@0: png_uint_16p dp16 = png_aligncast(png_uint_16p, dp); michael@0: png_const_uint_16p sp16 = png_aligncastconst( michael@0: png_const_uint_16p, sp); michael@0: size_t skip = (bytes_to_jump-bytes_to_copy) / michael@0: (sizeof (png_uint_16)); michael@0: michael@0: do michael@0: { michael@0: size_t c = bytes_to_copy; michael@0: do michael@0: { michael@0: *dp16++ = *sp16++; michael@0: c -= (sizeof (png_uint_16)); michael@0: } michael@0: while (c > 0); michael@0: michael@0: if (row_width <= bytes_to_jump) michael@0: return; michael@0: michael@0: dp16 += skip; michael@0: sp16 += skip; michael@0: row_width -= bytes_to_jump; michael@0: } michael@0: while (bytes_to_copy <= row_width); michael@0: michael@0: /* End of row - 1 byte left, bytes_to_copy > row_width: */ michael@0: dp = (png_bytep)dp16; michael@0: sp = (png_const_bytep)sp16; michael@0: do michael@0: *dp++ = *sp++; michael@0: while (--row_width > 0); michael@0: return; michael@0: } michael@0: } michael@0: #endif /* PNG_ALIGN_ code */ michael@0: michael@0: /* The true default - use a memcpy: */ michael@0: for (;;) michael@0: { michael@0: memcpy(dp, sp, bytes_to_copy); michael@0: michael@0: if (row_width <= bytes_to_jump) michael@0: return; michael@0: michael@0: sp += bytes_to_jump; michael@0: dp += bytes_to_jump; michael@0: row_width -= bytes_to_jump; michael@0: if (bytes_to_copy > row_width) michael@0: bytes_to_copy = row_width; michael@0: } michael@0: } michael@0: michael@0: /* NOT REACHED*/ michael@0: } /* pixel_depth >= 8 */ michael@0: michael@0: /* Here if pixel_depth < 8 to check 'end_ptr' below. */ michael@0: } michael@0: else michael@0: #endif michael@0: michael@0: /* If here then the switch above wasn't used so just memcpy the whole row michael@0: * from the temporary row buffer (notice that this overwrites the end of the michael@0: * destination row if it is a partial byte.) michael@0: */ michael@0: memcpy(dp, sp, PNG_ROWBYTES(pixel_depth, row_width)); michael@0: michael@0: /* Restore the overwritten bits from the last byte if necessary. */ michael@0: if (end_ptr != NULL) michael@0: *end_ptr = (png_byte)((end_byte & end_mask) | (*end_ptr & ~end_mask)); michael@0: } michael@0: michael@0: #ifdef PNG_READ_INTERLACING_SUPPORTED michael@0: void /* PRIVATE */ michael@0: png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass, michael@0: png_uint_32 transformations /* Because these may affect the byte layout */) michael@0: { michael@0: /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ michael@0: /* Offset to next interlace block */ michael@0: static PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; michael@0: michael@0: png_debug(1, "in png_do_read_interlace"); michael@0: if (row != NULL && row_info != NULL) michael@0: { michael@0: png_uint_32 final_width; michael@0: michael@0: final_width = row_info->width * png_pass_inc[pass]; michael@0: michael@0: switch (row_info->pixel_depth) michael@0: { michael@0: case 1: michael@0: { michael@0: png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 3); michael@0: png_bytep dp = row + (png_size_t)((final_width - 1) >> 3); michael@0: int sshift, dshift; michael@0: int s_start, s_end, s_inc; michael@0: int jstop = png_pass_inc[pass]; michael@0: png_byte v; michael@0: png_uint_32 i; michael@0: int j; michael@0: michael@0: #ifdef PNG_READ_PACKSWAP_SUPPORTED michael@0: if (transformations & PNG_PACKSWAP) michael@0: { michael@0: sshift = (int)((row_info->width + 7) & 0x07); michael@0: dshift = (int)((final_width + 7) & 0x07); michael@0: s_start = 7; michael@0: s_end = 0; michael@0: s_inc = -1; michael@0: } michael@0: michael@0: else michael@0: #endif michael@0: { michael@0: sshift = 7 - (int)((row_info->width + 7) & 0x07); michael@0: dshift = 7 - (int)((final_width + 7) & 0x07); michael@0: s_start = 0; michael@0: s_end = 7; michael@0: s_inc = 1; michael@0: } michael@0: michael@0: for (i = 0; i < row_info->width; i++) michael@0: { michael@0: v = (png_byte)((*sp >> sshift) & 0x01); michael@0: for (j = 0; j < jstop; j++) michael@0: { michael@0: unsigned int tmp = *dp & (0x7f7f >> (7 - dshift)); michael@0: tmp |= v << dshift; michael@0: *dp = (png_byte)(tmp & 0xff); michael@0: michael@0: if (dshift == s_end) michael@0: { michael@0: dshift = s_start; michael@0: dp--; michael@0: } michael@0: michael@0: else michael@0: dshift += s_inc; michael@0: } michael@0: michael@0: if (sshift == s_end) michael@0: { michael@0: sshift = s_start; michael@0: sp--; michael@0: } michael@0: michael@0: else michael@0: sshift += s_inc; michael@0: } michael@0: break; michael@0: } michael@0: michael@0: case 2: michael@0: { michael@0: png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2); michael@0: png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2); michael@0: int sshift, dshift; michael@0: int s_start, s_end, s_inc; michael@0: int jstop = png_pass_inc[pass]; michael@0: png_uint_32 i; michael@0: michael@0: #ifdef PNG_READ_PACKSWAP_SUPPORTED michael@0: if (transformations & PNG_PACKSWAP) michael@0: { michael@0: sshift = (int)(((row_info->width + 3) & 0x03) << 1); michael@0: dshift = (int)(((final_width + 3) & 0x03) << 1); michael@0: s_start = 6; michael@0: s_end = 0; michael@0: s_inc = -2; michael@0: } michael@0: michael@0: else michael@0: #endif michael@0: { michael@0: sshift = (int)((3 - ((row_info->width + 3) & 0x03)) << 1); michael@0: dshift = (int)((3 - ((final_width + 3) & 0x03)) << 1); michael@0: s_start = 0; michael@0: s_end = 6; michael@0: s_inc = 2; michael@0: } michael@0: michael@0: for (i = 0; i < row_info->width; i++) michael@0: { michael@0: png_byte v; michael@0: int j; michael@0: michael@0: v = (png_byte)((*sp >> sshift) & 0x03); michael@0: for (j = 0; j < jstop; j++) michael@0: { michael@0: unsigned int tmp = *dp & (0x3f3f >> (6 - dshift)); michael@0: tmp |= v << dshift; michael@0: *dp = (png_byte)(tmp & 0xff); michael@0: michael@0: if (dshift == s_end) michael@0: { michael@0: dshift = s_start; michael@0: dp--; michael@0: } michael@0: michael@0: else michael@0: dshift += s_inc; michael@0: } michael@0: michael@0: if (sshift == s_end) michael@0: { michael@0: sshift = s_start; michael@0: sp--; michael@0: } michael@0: michael@0: else michael@0: sshift += s_inc; michael@0: } michael@0: break; michael@0: } michael@0: michael@0: case 4: michael@0: { michael@0: png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 1); michael@0: png_bytep dp = row + (png_size_t)((final_width - 1) >> 1); michael@0: int sshift, dshift; michael@0: int s_start, s_end, s_inc; michael@0: png_uint_32 i; michael@0: int jstop = png_pass_inc[pass]; michael@0: michael@0: #ifdef PNG_READ_PACKSWAP_SUPPORTED michael@0: if (transformations & PNG_PACKSWAP) michael@0: { michael@0: sshift = (int)(((row_info->width + 1) & 0x01) << 2); michael@0: dshift = (int)(((final_width + 1) & 0x01) << 2); michael@0: s_start = 4; michael@0: s_end = 0; michael@0: s_inc = -4; michael@0: } michael@0: michael@0: else michael@0: #endif michael@0: { michael@0: sshift = (int)((1 - ((row_info->width + 1) & 0x01)) << 2); michael@0: dshift = (int)((1 - ((final_width + 1) & 0x01)) << 2); michael@0: s_start = 0; michael@0: s_end = 4; michael@0: s_inc = 4; michael@0: } michael@0: michael@0: for (i = 0; i < row_info->width; i++) michael@0: { michael@0: png_byte v = (png_byte)((*sp >> sshift) & 0x0f); michael@0: int j; michael@0: michael@0: for (j = 0; j < jstop; j++) michael@0: { michael@0: unsigned int tmp = *dp & (0xf0f >> (4 - dshift)); michael@0: tmp |= v << dshift; michael@0: *dp = (png_byte)(tmp & 0xff); michael@0: michael@0: if (dshift == s_end) michael@0: { michael@0: dshift = s_start; michael@0: dp--; michael@0: } michael@0: michael@0: else michael@0: dshift += s_inc; michael@0: } michael@0: michael@0: if (sshift == s_end) michael@0: { michael@0: sshift = s_start; michael@0: sp--; michael@0: } michael@0: michael@0: else michael@0: sshift += s_inc; michael@0: } michael@0: break; michael@0: } michael@0: michael@0: default: michael@0: { michael@0: png_size_t pixel_bytes = (row_info->pixel_depth >> 3); michael@0: michael@0: png_bytep sp = row + (png_size_t)(row_info->width - 1) michael@0: * pixel_bytes; michael@0: michael@0: png_bytep dp = row + (png_size_t)(final_width - 1) * pixel_bytes; michael@0: michael@0: int jstop = png_pass_inc[pass]; michael@0: png_uint_32 i; michael@0: michael@0: for (i = 0; i < row_info->width; i++) michael@0: { michael@0: png_byte v[8]; /* SAFE; pixel_depth does not exceed 64 */ michael@0: int j; michael@0: michael@0: memcpy(v, sp, pixel_bytes); michael@0: michael@0: for (j = 0; j < jstop; j++) michael@0: { michael@0: memcpy(dp, v, pixel_bytes); michael@0: dp -= pixel_bytes; michael@0: } michael@0: michael@0: sp -= pixel_bytes; michael@0: } michael@0: break; michael@0: } michael@0: } michael@0: michael@0: row_info->width = final_width; michael@0: row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width); michael@0: } michael@0: #ifndef PNG_READ_PACKSWAP_SUPPORTED michael@0: PNG_UNUSED(transformations) /* Silence compiler warning */ michael@0: #endif michael@0: } michael@0: #endif /* PNG_READ_INTERLACING_SUPPORTED */ michael@0: michael@0: static void michael@0: png_read_filter_row_sub(png_row_infop row_info, png_bytep row, michael@0: png_const_bytep prev_row) michael@0: { michael@0: png_size_t i; michael@0: png_size_t istop = row_info->rowbytes; michael@0: unsigned int bpp = (row_info->pixel_depth + 7) >> 3; michael@0: png_bytep rp = row + bpp; michael@0: michael@0: PNG_UNUSED(prev_row) michael@0: michael@0: for (i = bpp; i < istop; i++) michael@0: { michael@0: *rp = (png_byte)(((int)(*rp) + (int)(*(rp-bpp))) & 0xff); michael@0: rp++; michael@0: } michael@0: } michael@0: michael@0: static void michael@0: png_read_filter_row_up(png_row_infop row_info, png_bytep row, michael@0: png_const_bytep prev_row) michael@0: { michael@0: png_size_t i; michael@0: png_size_t istop = row_info->rowbytes; michael@0: png_bytep rp = row; michael@0: png_const_bytep pp = prev_row; michael@0: michael@0: for (i = 0; i < istop; i++) michael@0: { michael@0: *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff); michael@0: rp++; michael@0: } michael@0: } michael@0: michael@0: static void michael@0: png_read_filter_row_avg(png_row_infop row_info, png_bytep row, michael@0: png_const_bytep prev_row) michael@0: { michael@0: png_size_t i; michael@0: png_bytep rp = row; michael@0: png_const_bytep pp = prev_row; michael@0: unsigned int bpp = (row_info->pixel_depth + 7) >> 3; michael@0: png_size_t istop = row_info->rowbytes - bpp; michael@0: michael@0: for (i = 0; i < bpp; i++) michael@0: { michael@0: *rp = (png_byte)(((int)(*rp) + michael@0: ((int)(*pp++) / 2 )) & 0xff); michael@0: michael@0: rp++; michael@0: } michael@0: michael@0: for (i = 0; i < istop; i++) michael@0: { michael@0: *rp = (png_byte)(((int)(*rp) + michael@0: (int)(*pp++ + *(rp-bpp)) / 2 ) & 0xff); michael@0: michael@0: rp++; michael@0: } michael@0: } michael@0: michael@0: static void michael@0: png_read_filter_row_paeth_1byte_pixel(png_row_infop row_info, png_bytep row, michael@0: png_const_bytep prev_row) michael@0: { michael@0: png_bytep rp_end = row + row_info->rowbytes; michael@0: int a, c; michael@0: michael@0: /* First pixel/byte */ michael@0: c = *prev_row++; michael@0: a = *row + c; michael@0: *row++ = (png_byte)a; michael@0: michael@0: /* Remainder */ michael@0: while (row < rp_end) michael@0: { michael@0: int b, pa, pb, pc, p; michael@0: michael@0: a &= 0xff; /* From previous iteration or start */ michael@0: b = *prev_row++; michael@0: michael@0: p = b - c; michael@0: pc = a - c; michael@0: michael@0: # ifdef PNG_USE_ABS michael@0: pa = abs(p); michael@0: pb = abs(pc); michael@0: pc = abs(p + pc); michael@0: # else michael@0: pa = p < 0 ? -p : p; michael@0: pb = pc < 0 ? -pc : pc; michael@0: pc = (p + pc) < 0 ? -(p + pc) : p + pc; michael@0: # endif michael@0: michael@0: /* Find the best predictor, the least of pa, pb, pc favoring the earlier michael@0: * ones in the case of a tie. michael@0: */ michael@0: if (pb < pa) pa = pb, a = b; michael@0: if (pc < pa) a = c; michael@0: michael@0: /* Calculate the current pixel in a, and move the previous row pixel to c michael@0: * for the next time round the loop michael@0: */ michael@0: c = b; michael@0: a += *row; michael@0: *row++ = (png_byte)a; michael@0: } michael@0: } michael@0: michael@0: static void michael@0: png_read_filter_row_paeth_multibyte_pixel(png_row_infop row_info, png_bytep row, michael@0: png_const_bytep prev_row) michael@0: { michael@0: int bpp = (row_info->pixel_depth + 7) >> 3; michael@0: png_bytep rp_end = row + bpp; michael@0: michael@0: /* Process the first pixel in the row completely (this is the same as 'up' michael@0: * because there is only one candidate predictor for the first row). michael@0: */ michael@0: while (row < rp_end) michael@0: { michael@0: int a = *row + *prev_row++; michael@0: *row++ = (png_byte)a; michael@0: } michael@0: michael@0: /* Remainder */ michael@0: rp_end += row_info->rowbytes - bpp; michael@0: michael@0: while (row < rp_end) michael@0: { michael@0: int a, b, c, pa, pb, pc, p; michael@0: michael@0: c = *(prev_row - bpp); michael@0: a = *(row - bpp); michael@0: b = *prev_row++; michael@0: michael@0: p = b - c; michael@0: pc = a - c; michael@0: michael@0: # ifdef PNG_USE_ABS michael@0: pa = abs(p); michael@0: pb = abs(pc); michael@0: pc = abs(p + pc); michael@0: # else michael@0: pa = p < 0 ? -p : p; michael@0: pb = pc < 0 ? -pc : pc; michael@0: pc = (p + pc) < 0 ? -(p + pc) : p + pc; michael@0: # endif michael@0: michael@0: if (pb < pa) pa = pb, a = b; michael@0: if (pc < pa) a = c; michael@0: michael@0: a += *row; michael@0: *row++ = (png_byte)a; michael@0: } michael@0: } michael@0: michael@0: static void michael@0: png_init_filter_functions(png_structrp pp) michael@0: /* This function is called once for every PNG image (except for PNG images michael@0: * that only use PNG_FILTER_VALUE_NONE for all rows) to set the michael@0: * implementations required to reverse the filtering of PNG rows. Reversing michael@0: * the filter is the first transformation performed on the row data. It is michael@0: * performed in place, therefore an implementation can be selected based on michael@0: * the image pixel format. If the implementation depends on image width then michael@0: * take care to ensure that it works correctly if the image is interlaced - michael@0: * interlacing causes the actual row width to vary. michael@0: */ michael@0: { michael@0: unsigned int bpp = (pp->pixel_depth + 7) >> 3; michael@0: michael@0: pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub; michael@0: pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up; michael@0: pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg; michael@0: if (bpp == 1) michael@0: pp->read_filter[PNG_FILTER_VALUE_PAETH-1] = michael@0: png_read_filter_row_paeth_1byte_pixel; michael@0: else michael@0: pp->read_filter[PNG_FILTER_VALUE_PAETH-1] = michael@0: png_read_filter_row_paeth_multibyte_pixel; michael@0: michael@0: #ifdef PNG_FILTER_OPTIMIZATIONS michael@0: /* To use this define PNG_FILTER_OPTIMIZATIONS as the name of a function to michael@0: * call to install hardware optimizations for the above functions; simply michael@0: * replace whatever elements of the pp->read_filter[] array with a hardware michael@0: * specific (or, for that matter, generic) optimization. michael@0: * michael@0: * To see an example of this examine what configure.ac does when michael@0: * --enable-arm-neon is specified on the command line. michael@0: */ michael@0: PNG_FILTER_OPTIMIZATIONS(pp, bpp); michael@0: #endif michael@0: } michael@0: michael@0: void /* PRIVATE */ michael@0: png_read_filter_row(png_structrp pp, png_row_infop row_info, png_bytep row, michael@0: png_const_bytep prev_row, int filter) michael@0: { michael@0: /* OPTIMIZATION: DO NOT MODIFY THIS FUNCTION, instead #define michael@0: * PNG_FILTER_OPTIMIZATIONS to a function that overrides the generic michael@0: * implementations. See png_init_filter_functions above. michael@0: */ michael@0: if (filter > PNG_FILTER_VALUE_NONE && filter < PNG_FILTER_VALUE_LAST) michael@0: { michael@0: if (pp->read_filter[0] == NULL) michael@0: png_init_filter_functions(pp); michael@0: michael@0: pp->read_filter[filter-1](row_info, row, prev_row); michael@0: } michael@0: } michael@0: michael@0: #ifdef PNG_SEQUENTIAL_READ_SUPPORTED michael@0: void /* PRIVATE */ michael@0: png_read_IDAT_data(png_structrp png_ptr, png_bytep output, michael@0: png_alloc_size_t avail_out) michael@0: { michael@0: /* Loop reading IDATs and decompressing the result into output[avail_out] */ michael@0: png_ptr->zstream.next_out = output; michael@0: png_ptr->zstream.avail_out = 0; /* safety: set below */ michael@0: michael@0: if (output == NULL) michael@0: avail_out = 0; michael@0: michael@0: do michael@0: { michael@0: int ret; michael@0: png_byte tmpbuf[PNG_INFLATE_BUF_SIZE]; michael@0: michael@0: if (png_ptr->zstream.avail_in == 0) michael@0: { michael@0: uInt avail_in; michael@0: png_bytep buffer; michael@0: michael@0: #ifdef PNG_READ_APNG_SUPPORTED michael@0: png_uint_32 bytes_to_skip = 0; michael@0: michael@0: while (png_ptr->idat_size == 0 || bytes_to_skip != 0) michael@0: { michael@0: png_crc_finish(png_ptr, bytes_to_skip); michael@0: bytes_to_skip = 0; michael@0: michael@0: png_ptr->idat_size = png_read_chunk_header(png_ptr); michael@0: if (png_ptr->num_frames_read == 0) michael@0: { michael@0: if (png_ptr->chunk_name != png_IDAT) michael@0: png_error(png_ptr, "Not enough image data"); michael@0: } michael@0: else michael@0: { michael@0: if (png_ptr->chunk_name == png_IEND) michael@0: png_error(png_ptr, "Not enough image data"); michael@0: if (png_ptr->chunk_name != png_fdAT) michael@0: { michael@0: png_warning(png_ptr, "Skipped (ignored) a chunk " michael@0: "between APNG chunks"); michael@0: bytes_to_skip = png_ptr->idat_size; michael@0: continue; michael@0: } michael@0: michael@0: png_ensure_sequence_number(png_ptr, png_ptr->idat_size); michael@0: michael@0: png_ptr->idat_size -= 4; michael@0: } michael@0: } michael@0: #else michael@0: while (png_ptr->idat_size == 0) michael@0: { michael@0: png_crc_finish(png_ptr, 0); michael@0: michael@0: png_ptr->idat_size = png_read_chunk_header(png_ptr); michael@0: /* This is an error even in the 'check' case because the code just michael@0: * consumed a non-IDAT header. michael@0: */ michael@0: if (png_ptr->chunk_name != png_IDAT) michael@0: png_error(png_ptr, "Not enough image data"); michael@0: } michael@0: #endif /* PNG_READ_APNG_SUPPORTED */ michael@0: michael@0: avail_in = png_ptr->IDAT_read_size; michael@0: michael@0: if (avail_in > png_ptr->idat_size) michael@0: avail_in = (uInt)png_ptr->idat_size; michael@0: michael@0: /* A PNG with a gradually increasing IDAT size will defeat this attempt michael@0: * to minimize memory usage by causing lots of re-allocs, but michael@0: * realistically doing IDAT_read_size re-allocs is not likely to be a michael@0: * big problem. michael@0: */ michael@0: buffer = png_read_buffer(png_ptr, avail_in, 0/*error*/); michael@0: michael@0: png_crc_read(png_ptr, buffer, avail_in); michael@0: png_ptr->idat_size -= avail_in; michael@0: michael@0: png_ptr->zstream.next_in = buffer; michael@0: png_ptr->zstream.avail_in = avail_in; michael@0: } michael@0: michael@0: /* And set up the output side. */ michael@0: if (output != NULL) /* standard read */ michael@0: { michael@0: uInt out = ZLIB_IO_MAX; michael@0: michael@0: if (out > avail_out) michael@0: out = (uInt)avail_out; michael@0: michael@0: avail_out -= out; michael@0: png_ptr->zstream.avail_out = out; michael@0: } michael@0: michael@0: else /* after last row, checking for end */ michael@0: { michael@0: png_ptr->zstream.next_out = tmpbuf; michael@0: png_ptr->zstream.avail_out = (sizeof tmpbuf); michael@0: } michael@0: michael@0: /* Use NO_FLUSH; this gives zlib the maximum opportunity to optimize the michael@0: * process. If the LZ stream is truncated the sequential reader will michael@0: * terminally damage the stream, above, by reading the chunk header of the michael@0: * following chunk (it then exits with png_error). michael@0: * michael@0: * TODO: deal more elegantly with truncated IDAT lists. michael@0: */ michael@0: ret = inflate(&png_ptr->zstream, Z_NO_FLUSH); michael@0: michael@0: /* Take the unconsumed output back. */ michael@0: if (output != NULL) michael@0: avail_out += png_ptr->zstream.avail_out; michael@0: michael@0: else /* avail_out counts the extra bytes */ michael@0: avail_out += (sizeof tmpbuf) - png_ptr->zstream.avail_out; michael@0: michael@0: png_ptr->zstream.avail_out = 0; michael@0: michael@0: if (ret == Z_STREAM_END) michael@0: { michael@0: /* Do this for safety; we won't read any more into this row. */ michael@0: png_ptr->zstream.next_out = NULL; michael@0: michael@0: png_ptr->mode |= PNG_AFTER_IDAT; michael@0: png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED; michael@0: #ifdef PNG_READ_APNG_SUPPORTED michael@0: png_ptr->num_frames_read++; michael@0: #endif michael@0: michael@0: if (png_ptr->zstream.avail_in > 0 || png_ptr->idat_size > 0) michael@0: png_chunk_benign_error(png_ptr, "Extra compressed data"); michael@0: break; michael@0: } michael@0: michael@0: if (ret != Z_OK) michael@0: { michael@0: png_zstream_error(png_ptr, ret); michael@0: michael@0: if (output != NULL) michael@0: png_chunk_error(png_ptr, png_ptr->zstream.msg); michael@0: michael@0: else /* checking */ michael@0: { michael@0: png_chunk_benign_error(png_ptr, png_ptr->zstream.msg); michael@0: return; michael@0: } michael@0: } michael@0: } while (avail_out > 0); michael@0: michael@0: if (avail_out > 0) michael@0: { michael@0: /* The stream ended before the image; this is the same as too few IDATs so michael@0: * should be handled the same way. michael@0: */ michael@0: if (output != NULL) michael@0: png_error(png_ptr, "Not enough image data"); michael@0: michael@0: else /* the deflate stream contained extra data */ michael@0: png_chunk_benign_error(png_ptr, "Too much image data"); michael@0: } michael@0: } michael@0: michael@0: void /* PRIVATE */ michael@0: png_read_finish_IDAT(png_structrp png_ptr) michael@0: { michael@0: /* We don't need any more data and the stream should have ended, however the michael@0: * LZ end code may actually not have been processed. In this case we must michael@0: * read it otherwise stray unread IDAT data or, more likely, an IDAT chunk michael@0: * may still remain to be consumed. michael@0: */ michael@0: if (!(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED)) michael@0: { michael@0: /* The NULL causes png_read_IDAT_data to swallow any remaining bytes in michael@0: * the compressed stream, but the stream may be damaged too, so even after michael@0: * this call we may need to terminate the zstream ownership. michael@0: */ michael@0: png_read_IDAT_data(png_ptr, NULL, 0); michael@0: png_ptr->zstream.next_out = NULL; /* safety */ michael@0: michael@0: /* Now clear everything out for safety; the following may not have been michael@0: * done. michael@0: */ michael@0: if (!(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED)) michael@0: { michael@0: png_ptr->mode |= PNG_AFTER_IDAT; michael@0: png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED; michael@0: } michael@0: } michael@0: michael@0: /* If the zstream has not been released do it now *and* terminate the reading michael@0: * of the final IDAT chunk. michael@0: */ michael@0: if (png_ptr->zowner == png_IDAT) michael@0: { michael@0: /* Always do this; the pointers otherwise point into the read buffer. */ michael@0: png_ptr->zstream.next_in = NULL; michael@0: png_ptr->zstream.avail_in = 0; michael@0: michael@0: /* Now we no longer own the zstream. */ michael@0: png_ptr->zowner = 0; michael@0: michael@0: /* The slightly weird semantics of the sequential IDAT reading is that we michael@0: * are always in or at the end of an IDAT chunk, so we always need to do a michael@0: * crc_finish here. If idat_size is non-zero we also need to read the michael@0: * spurious bytes at the end of the chunk now. michael@0: */ michael@0: (void)png_crc_finish(png_ptr, png_ptr->idat_size); michael@0: } michael@0: } michael@0: michael@0: void /* PRIVATE */ michael@0: png_read_finish_row(png_structrp png_ptr) michael@0: { michael@0: #ifdef PNG_READ_INTERLACING_SUPPORTED michael@0: /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ michael@0: michael@0: /* Start of interlace block */ michael@0: static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; michael@0: michael@0: /* Offset to next interlace block */ michael@0: static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; michael@0: michael@0: /* Start of interlace block in the y direction */ michael@0: static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1}; michael@0: michael@0: /* Offset to next interlace block in the y direction */ michael@0: static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; michael@0: #endif /* PNG_READ_INTERLACING_SUPPORTED */ michael@0: michael@0: png_debug(1, "in png_read_finish_row"); michael@0: png_ptr->row_number++; michael@0: if (png_ptr->row_number < png_ptr->num_rows) michael@0: return; michael@0: michael@0: #ifdef PNG_READ_INTERLACING_SUPPORTED michael@0: if (png_ptr->interlaced) michael@0: { michael@0: png_ptr->row_number = 0; michael@0: michael@0: /* TO DO: don't do this if prev_row isn't needed (requires michael@0: * read-ahead of the next row's filter byte. michael@0: */ michael@0: memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1); michael@0: michael@0: do michael@0: { michael@0: png_ptr->pass++; michael@0: michael@0: if (png_ptr->pass >= 7) michael@0: break; michael@0: michael@0: png_ptr->iwidth = (png_ptr->width + michael@0: png_pass_inc[png_ptr->pass] - 1 - michael@0: png_pass_start[png_ptr->pass]) / michael@0: png_pass_inc[png_ptr->pass]; michael@0: michael@0: if (!(png_ptr->transformations & PNG_INTERLACE)) michael@0: { michael@0: png_ptr->num_rows = (png_ptr->height + michael@0: png_pass_yinc[png_ptr->pass] - 1 - michael@0: png_pass_ystart[png_ptr->pass]) / michael@0: png_pass_yinc[png_ptr->pass]; michael@0: } michael@0: michael@0: else /* if (png_ptr->transformations & PNG_INTERLACE) */ michael@0: break; /* libpng deinterlacing sees every row */ michael@0: michael@0: } while (png_ptr->num_rows == 0 || png_ptr->iwidth == 0); michael@0: michael@0: if (png_ptr->pass < 7) michael@0: return; michael@0: } michael@0: #endif /* PNG_READ_INTERLACING_SUPPORTED */ michael@0: michael@0: /* Here after at the end of the last row of the last pass. */ michael@0: png_read_finish_IDAT(png_ptr); michael@0: } michael@0: #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ michael@0: michael@0: void /* PRIVATE */ michael@0: png_read_start_row(png_structrp png_ptr) michael@0: { michael@0: #ifdef PNG_READ_INTERLACING_SUPPORTED michael@0: /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ michael@0: michael@0: /* Start of interlace block */ michael@0: static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; michael@0: michael@0: /* Offset to next interlace block */ michael@0: static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; michael@0: michael@0: /* Start of interlace block in the y direction */ michael@0: static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1}; michael@0: michael@0: /* Offset to next interlace block in the y direction */ michael@0: static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; michael@0: #endif michael@0: michael@0: int max_pixel_depth; michael@0: png_size_t row_bytes; michael@0: michael@0: png_debug(1, "in png_read_start_row"); michael@0: michael@0: #ifdef PNG_READ_TRANSFORMS_SUPPORTED michael@0: png_init_read_transformations(png_ptr); michael@0: #endif michael@0: #ifdef PNG_READ_INTERLACING_SUPPORTED michael@0: if (png_ptr->interlaced) michael@0: { michael@0: if (!(png_ptr->transformations & PNG_INTERLACE)) michael@0: png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 - michael@0: png_pass_ystart[0]) / png_pass_yinc[0]; michael@0: michael@0: else michael@0: png_ptr->num_rows = png_ptr->height; michael@0: michael@0: png_ptr->iwidth = (png_ptr->width + michael@0: png_pass_inc[png_ptr->pass] - 1 - michael@0: png_pass_start[png_ptr->pass]) / michael@0: png_pass_inc[png_ptr->pass]; michael@0: } michael@0: michael@0: else michael@0: #endif /* PNG_READ_INTERLACING_SUPPORTED */ michael@0: { michael@0: png_ptr->num_rows = png_ptr->height; michael@0: png_ptr->iwidth = png_ptr->width; michael@0: } michael@0: michael@0: max_pixel_depth = png_ptr->pixel_depth; michael@0: michael@0: /* WARNING: * png_read_transform_info (pngrtran.c) performs a simpliar set of michael@0: * calculations to calculate the final pixel depth, then michael@0: * png_do_read_transforms actually does the transforms. This means that the michael@0: * code which effectively calculates this value is actually repeated in three michael@0: * separate places. They must all match. Innocent changes to the order of michael@0: * transformations can and will break libpng in a way that causes memory michael@0: * overwrites. michael@0: * michael@0: * TODO: fix this. michael@0: */ michael@0: #ifdef PNG_READ_PACK_SUPPORTED michael@0: if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8) michael@0: max_pixel_depth = 8; michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_EXPAND_SUPPORTED michael@0: if (png_ptr->transformations & PNG_EXPAND) michael@0: { michael@0: if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) michael@0: { michael@0: if (png_ptr->num_trans) michael@0: max_pixel_depth = 32; michael@0: michael@0: else michael@0: max_pixel_depth = 24; michael@0: } michael@0: michael@0: else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY) michael@0: { michael@0: if (max_pixel_depth < 8) michael@0: max_pixel_depth = 8; michael@0: michael@0: if (png_ptr->num_trans) michael@0: max_pixel_depth *= 2; michael@0: } michael@0: michael@0: else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB) michael@0: { michael@0: if (png_ptr->num_trans) michael@0: { michael@0: max_pixel_depth *= 4; michael@0: max_pixel_depth /= 3; michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_EXPAND_16_SUPPORTED michael@0: if (png_ptr->transformations & PNG_EXPAND_16) michael@0: { michael@0: # ifdef PNG_READ_EXPAND_SUPPORTED michael@0: /* In fact it is an error if it isn't supported, but checking is michael@0: * the safe way. michael@0: */ michael@0: if (png_ptr->transformations & PNG_EXPAND) michael@0: { michael@0: if (png_ptr->bit_depth < 16) michael@0: max_pixel_depth *= 2; michael@0: } michael@0: else michael@0: # endif michael@0: png_ptr->transformations &= ~PNG_EXPAND_16; michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_FILLER_SUPPORTED michael@0: if (png_ptr->transformations & (PNG_FILLER)) michael@0: { michael@0: if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY) michael@0: { michael@0: if (max_pixel_depth <= 8) michael@0: max_pixel_depth = 16; michael@0: michael@0: else michael@0: max_pixel_depth = 32; michael@0: } michael@0: michael@0: else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB || michael@0: png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) michael@0: { michael@0: if (max_pixel_depth <= 32) michael@0: max_pixel_depth = 32; michael@0: michael@0: else michael@0: max_pixel_depth = 64; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED michael@0: if (png_ptr->transformations & PNG_GRAY_TO_RGB) michael@0: { michael@0: if ( michael@0: #ifdef PNG_READ_EXPAND_SUPPORTED michael@0: (png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) || michael@0: #endif michael@0: #ifdef PNG_READ_FILLER_SUPPORTED michael@0: (png_ptr->transformations & (PNG_FILLER)) || michael@0: #endif michael@0: png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) michael@0: { michael@0: if (max_pixel_depth <= 16) michael@0: max_pixel_depth = 32; michael@0: michael@0: else michael@0: max_pixel_depth = 64; michael@0: } michael@0: michael@0: else michael@0: { michael@0: if (max_pixel_depth <= 8) michael@0: { michael@0: if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA) michael@0: max_pixel_depth = 32; michael@0: michael@0: else michael@0: max_pixel_depth = 24; michael@0: } michael@0: michael@0: else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA) michael@0: max_pixel_depth = 64; michael@0: michael@0: else michael@0: max_pixel_depth = 48; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \ michael@0: defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) michael@0: if (png_ptr->transformations & PNG_USER_TRANSFORM) michael@0: { michael@0: int user_pixel_depth = png_ptr->user_transform_depth * michael@0: png_ptr->user_transform_channels; michael@0: michael@0: if (user_pixel_depth > max_pixel_depth) michael@0: max_pixel_depth = user_pixel_depth; michael@0: } michael@0: #endif michael@0: michael@0: /* This value is stored in png_struct and double checked in the row read michael@0: * code. michael@0: */ michael@0: png_ptr->maximum_pixel_depth = (png_byte)max_pixel_depth; michael@0: png_ptr->transformed_pixel_depth = 0; /* calculated on demand */ michael@0: michael@0: /* Align the width on the next larger 8 pixels. Mainly used michael@0: * for interlacing michael@0: */ michael@0: row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7)); michael@0: /* Calculate the maximum bytes needed, adding a byte and a pixel michael@0: * for safety's sake michael@0: */ michael@0: row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) + michael@0: 1 + ((max_pixel_depth + 7) >> 3); michael@0: michael@0: #ifdef PNG_MAX_MALLOC_64K michael@0: if (row_bytes > (png_uint_32)65536L) michael@0: png_error(png_ptr, "This image requires a row greater than 64KB"); michael@0: #endif michael@0: michael@0: if (row_bytes + 48 > png_ptr->old_big_row_buf_size) michael@0: { michael@0: png_free(png_ptr, png_ptr->big_row_buf); michael@0: png_free(png_ptr, png_ptr->big_prev_row); michael@0: michael@0: if (png_ptr->interlaced) michael@0: png_ptr->big_row_buf = (png_bytep)png_calloc(png_ptr, michael@0: row_bytes + 48); michael@0: michael@0: else michael@0: png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes + 48); michael@0: michael@0: png_ptr->big_prev_row = (png_bytep)png_malloc(png_ptr, row_bytes + 48); michael@0: michael@0: #ifdef PNG_ALIGNED_MEMORY_SUPPORTED michael@0: /* Use 16-byte aligned memory for row_buf with at least 16 bytes michael@0: * of padding before and after row_buf; treat prev_row similarly. michael@0: * NOTE: the alignment is to the start of the pixels, one beyond the start michael@0: * of the buffer, because of the filter byte. Prior to libpng 1.5.6 this michael@0: * was incorrect; the filter byte was aligned, which had the exact michael@0: * opposite effect of that intended. michael@0: */ michael@0: { michael@0: png_bytep temp = png_ptr->big_row_buf + 32; michael@0: int extra = (int)((temp - (png_bytep)0) & 0x0f); michael@0: png_ptr->row_buf = temp - extra - 1/*filter byte*/; michael@0: michael@0: temp = png_ptr->big_prev_row + 32; michael@0: extra = (int)((temp - (png_bytep)0) & 0x0f); michael@0: png_ptr->prev_row = temp - extra - 1/*filter byte*/; michael@0: } michael@0: michael@0: #else michael@0: /* Use 31 bytes of padding before and 17 bytes after row_buf. */ michael@0: png_ptr->row_buf = png_ptr->big_row_buf + 31; michael@0: png_ptr->prev_row = png_ptr->big_prev_row + 31; michael@0: #endif michael@0: png_ptr->old_big_row_buf_size = row_bytes + 48; michael@0: } michael@0: michael@0: #ifdef PNG_MAX_MALLOC_64K michael@0: if (png_ptr->rowbytes > 65535) michael@0: png_error(png_ptr, "This image requires a row greater than 64KB"); michael@0: michael@0: #endif michael@0: if (png_ptr->rowbytes > (PNG_SIZE_MAX - 1)) michael@0: png_error(png_ptr, "Row has too many bytes to allocate in memory"); michael@0: michael@0: memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1); michael@0: michael@0: png_debug1(3, "width = %u,", png_ptr->width); michael@0: png_debug1(3, "height = %u,", png_ptr->height); michael@0: png_debug1(3, "iwidth = %u,", png_ptr->iwidth); michael@0: png_debug1(3, "num_rows = %u,", png_ptr->num_rows); michael@0: png_debug1(3, "rowbytes = %lu,", (unsigned long)png_ptr->rowbytes); michael@0: png_debug1(3, "irowbytes = %lu", michael@0: (unsigned long)PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1); michael@0: michael@0: /* The sequential reader needs a buffer for IDAT, but the progressive reader michael@0: * does not, so free the read buffer now regardless; the sequential reader michael@0: * reallocates it on demand. michael@0: */ michael@0: if (png_ptr->read_buffer) michael@0: { michael@0: png_bytep buffer = png_ptr->read_buffer; michael@0: michael@0: png_ptr->read_buffer_size = 0; michael@0: png_ptr->read_buffer = NULL; michael@0: png_free(png_ptr, buffer); michael@0: } michael@0: michael@0: /* Finally claim the zstream for the inflate of the IDAT data, use the bits michael@0: * value from the stream (note that this will result in a fatal error if the michael@0: * IDAT stream has a bogus deflate header window_bits value, but this should michael@0: * not be happening any longer!) michael@0: */ michael@0: if (png_inflate_claim(png_ptr, png_IDAT) != Z_OK) michael@0: png_error(png_ptr, png_ptr->zstream.msg); michael@0: michael@0: png_ptr->flags |= PNG_FLAG_ROW_INIT; michael@0: } michael@0: michael@0: #ifdef PNG_READ_APNG_SUPPORTED michael@0: /* This function is to be called after the main IDAT set has been read and michael@0: * before a new IDAT is read. It resets some parts of png_ptr michael@0: * to make them usable by the read functions again */ michael@0: void /* PRIVATE */ michael@0: png_read_reset(png_structp png_ptr) michael@0: { michael@0: png_ptr->mode &= ~PNG_HAVE_IDAT; michael@0: png_ptr->mode &= ~PNG_AFTER_IDAT; michael@0: png_ptr->row_number = 0; michael@0: png_ptr->pass = 0; michael@0: } michael@0: michael@0: void /* PRIVATE */ michael@0: png_read_reinit(png_structp png_ptr, png_infop info_ptr) michael@0: { michael@0: png_ptr->width = info_ptr->next_frame_width; michael@0: png_ptr->height = info_ptr->next_frame_height; michael@0: png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth,png_ptr->width); michael@0: png_ptr->info_rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, michael@0: png_ptr->width); michael@0: if (png_ptr->prev_row) michael@0: memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1); michael@0: } michael@0: michael@0: #ifdef PNG_PROGRESSIVE_READ_SUPPORTED michael@0: /* same as png_read_reset() but for the progressive reader */ michael@0: void /* PRIVATE */ michael@0: png_progressive_read_reset(png_structp png_ptr) michael@0: { michael@0: #ifdef PNG_READ_INTERLACING_SUPPORTED michael@0: /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ michael@0: michael@0: /* Start of interlace block */ michael@0: static PNG_CONST png_byte png_pass_start[] = {0, 4, 0, 2, 0, 1, 0}; michael@0: michael@0: /* Offset to next interlace block */ michael@0: static PNG_CONST png_byte png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1}; michael@0: michael@0: /* Start of interlace block in the y direction */ michael@0: static PNG_CONST png_byte png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1}; michael@0: michael@0: /* Offset to next interlace block in the y direction */ michael@0: static PNG_CONST png_byte png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2}; michael@0: michael@0: if (png_ptr->interlaced) michael@0: { michael@0: if (!(png_ptr->transformations & PNG_INTERLACE)) michael@0: png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 - michael@0: png_pass_ystart[0]) / png_pass_yinc[0]; michael@0: else michael@0: png_ptr->num_rows = png_ptr->height; michael@0: michael@0: png_ptr->iwidth = (png_ptr->width + michael@0: png_pass_inc[png_ptr->pass] - 1 - michael@0: png_pass_start[png_ptr->pass]) / michael@0: png_pass_inc[png_ptr->pass]; michael@0: } michael@0: else michael@0: #endif /* PNG_READ_INTERLACING_SUPPORTED */ michael@0: { michael@0: png_ptr->num_rows = png_ptr->height; michael@0: png_ptr->iwidth = png_ptr->width; michael@0: } michael@0: png_ptr->flags &= ~PNG_FLAG_ZSTREAM_ENDED; michael@0: if (inflateReset(&(png_ptr->zstream)) != Z_OK) michael@0: png_error(png_ptr, "inflateReset failed"); michael@0: png_ptr->zstream.avail_in = 0; michael@0: png_ptr->zstream.next_in = 0; michael@0: png_ptr->zstream.next_out = png_ptr->row_buf; michael@0: png_ptr->zstream.avail_out = (uInt)PNG_ROWBYTES(png_ptr->pixel_depth, michael@0: png_ptr->iwidth) + 1; michael@0: } michael@0: #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */ michael@0: #endif /* PNG_READ_APNG_SUPPORTED */ michael@0: #endif /* PNG_READ_SUPPORTED */