LCOV - code coverage report
Current view: top level - media/libpng - pngrutil.c (source / functions) Hit Total Coverage
Test: output.info Lines: 315 974 32.3 %
Date: 2017-07-14 16:53:18 Functions: 26 39 66.7 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13