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