src/3rdparty/libpng/pngrutil.c
branchGCC_SURGE
changeset 31 5daf16870df6
parent 30 5dc02b23752f
equal deleted inserted replaced
27:93b982ccede2 31:5daf16870df6
     1 
     1 
     2 /* pngrutil.c - utilities to read a PNG file
     2 /* pngrutil.c - utilities to read a PNG file
     3  *
     3  *
     4  * Last changed in libpng 1.2.38 [July 16, 2009]
     4  * Last changed in libpng 1.4.0 [January 3, 2010]
     5  * Copyright (c) 1998-2009 Glenn Randers-Pehrson
     5  * Copyright (c) 1998-2010 Glenn Randers-Pehrson
     6  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
     6  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
     7  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
     7  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
     8  *
     8  *
     9  * This code is released under the libpng license.
     9  * This code is released under the libpng license.
    10  * For conditions of distribution and use, see the disclaimer
    10  * For conditions of distribution and use, see the disclaimer
    12  *
    12  *
    13  * This file contains routines that are only called from within
    13  * This file contains routines that are only called from within
    14  * libpng itself during the course of reading an image.
    14  * libpng itself during the course of reading an image.
    15  */
    15  */
    16 
    16 
    17 #define PNG_INTERNAL
    17 #define PNG_NO_PEDANTIC_WARNINGS
    18 #include "png.h"
    18 #include "png.h"
    19 #if defined(PNG_READ_SUPPORTED)
    19 #ifdef PNG_READ_SUPPORTED
    20 
    20 #include "pngpriv.h"
    21 #if defined(_WIN32_WCE) && (_WIN32_WCE<0x500)
    21 
    22 #  define WIN32_WCE_OLD
       
    23 #endif
       
    24 
       
    25 #ifdef PNG_FLOATING_POINT_SUPPORTED
       
    26 #  if defined(WIN32_WCE_OLD)
       
    27 /* The strtod() function is not supported on WindowsCE */
       
    28 __inline double png_strtod(png_structp png_ptr, PNG_CONST char *nptr, char **endptr)
       
    29 {
       
    30    double result = 0;
       
    31    int len;
       
    32    wchar_t *str, *end;
       
    33 
       
    34    len = MultiByteToWideChar(CP_ACP, 0, nptr, -1, NULL, 0);
       
    35    str = (wchar_t *)png_malloc(png_ptr, len * png_sizeof(wchar_t));
       
    36    if ( NULL != str )
       
    37    {
       
    38       MultiByteToWideChar(CP_ACP, 0, nptr, -1, str, len);
       
    39       result = wcstod(str, &end);
       
    40       len = WideCharToMultiByte(CP_ACP, 0, end, -1, NULL, 0, NULL, NULL);
       
    41       *endptr = (char *)nptr + (png_strlen(nptr) - len + 1);
       
    42       png_free(png_ptr, str);
       
    43    }
       
    44    return result;
       
    45 }
       
    46 #  else
       
    47 #    define png_strtod(p,a,b) strtod(a,b)
    22 #    define png_strtod(p,a,b) strtod(a,b)
    48 #  endif
       
    49 #endif
       
    50 
       
    51 png_uint_32 PNGAPI
    23 png_uint_32 PNGAPI
    52 png_get_uint_31(png_structp png_ptr, png_bytep buf)
    24 png_get_uint_31(png_structp png_ptr, png_bytep buf)
    53 {
    25 {
    54 #ifdef PNG_READ_BIG_ENDIAN_SUPPORTED
       
    55    png_uint_32 i = png_get_uint_32(buf);
    26    png_uint_32 i = png_get_uint_32(buf);
    56 #else
       
    57    /* Avoid an extra function call by inlining the result. */
       
    58    png_uint_32 i = ((png_uint_32)(*buf) << 24) +
       
    59       ((png_uint_32)(*(buf + 1)) << 16) +
       
    60       ((png_uint_32)(*(buf + 2)) << 8) +
       
    61       (png_uint_32)(*(buf + 3));
       
    62 #endif
       
    63    if (i > PNG_UINT_31_MAX)
    27    if (i > PNG_UINT_31_MAX)
    64      png_error(png_ptr, "PNG unsigned integer out of range.");
    28      png_error(png_ptr, "PNG unsigned integer out of range");
    65    return (i);
    29    return (i);
    66 }
    30 }
    67 #ifndef PNG_READ_BIG_ENDIAN_SUPPORTED
    31 #ifndef PNG_USE_READ_MACROS
    68 /* Grab an unsigned 32-bit integer from a buffer in big-endian format. */
    32 /* Grab an unsigned 32-bit integer from a buffer in big-endian format. */
    69 png_uint_32 PNGAPI
    33 png_uint_32 PNGAPI
    70 png_get_uint_32(png_bytep buf)
    34 png_get_uint_32(png_bytep buf)
    71 {
    35 {
    72    png_uint_32 i = ((png_uint_32)(*buf) << 24) +
    36    png_uint_32 i = ((png_uint_32)(*buf) << 24) +
    99    png_uint_16 i = (png_uint_16)(((png_uint_16)(*buf) << 8) +
    63    png_uint_16 i = (png_uint_16)(((png_uint_16)(*buf) << 8) +
   100       (png_uint_16)(*(buf + 1)));
    64       (png_uint_16)(*(buf + 1)));
   101 
    65 
   102    return (i);
    66    return (i);
   103 }
    67 }
   104 #endif /* PNG_READ_BIG_ENDIAN_SUPPORTED */
    68 #endif /* PNG_USE_READ_MACROS */
   105 
    69 
   106 /* Read the chunk header (length + type name).
    70 /* Read the chunk header (length + type name).
   107  * Put the type name into png_ptr->chunk_name, and return the length.
    71  * Put the type name into png_ptr->chunk_name, and return the length.
   108  */
    72  */
   109 png_uint_32 /* PRIVATE */
    73 png_uint_32 /* PRIVATE */
   110 png_read_chunk_header(png_structp png_ptr)
    74 png_read_chunk_header(png_structp png_ptr)
   111 {
    75 {
   112    png_byte buf[8];
    76    png_byte buf[8];
   113    png_uint_32 length;
    77    png_uint_32 length;
   114 
    78 
       
    79 #ifdef PNG_IO_STATE_SUPPORTED
       
    80    /* Inform the I/O callback that the chunk header is being read.
       
    81     * PNG_IO_CHUNK_HDR requires a single I/O call.
       
    82     */
       
    83    png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_HDR;
       
    84 #endif
       
    85 
   115    /* Read the length and the chunk name */
    86    /* Read the length and the chunk name */
   116    png_read_data(png_ptr, buf, 8);
    87    png_read_data(png_ptr, buf, 8);
   117    length = png_get_uint_31(png_ptr, buf);
    88    length = png_get_uint_31(png_ptr, buf);
   118 
    89 
   119    /* Put the chunk name into png_ptr->chunk_name */
    90    /* Put the chunk name into png_ptr->chunk_name */
   126    png_reset_crc(png_ptr);
    97    png_reset_crc(png_ptr);
   127    png_calculate_crc(png_ptr, png_ptr->chunk_name, 4);
    98    png_calculate_crc(png_ptr, png_ptr->chunk_name, 4);
   128 
    99 
   129    /* Check to see if chunk name is valid */
   100    /* Check to see if chunk name is valid */
   130    png_check_chunk_name(png_ptr, png_ptr->chunk_name);
   101    png_check_chunk_name(png_ptr, png_ptr->chunk_name);
       
   102 
       
   103 #ifdef PNG_IO_STATE_SUPPORTED
       
   104    /* Inform the I/O callback that chunk data will (possibly) be read.
       
   105     * PNG_IO_CHUNK_DATA does NOT require a specific number of I/O calls.
       
   106     */
       
   107    png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_DATA;
       
   108 #endif
   131 
   109 
   132    return length;
   110    return length;
   133 }
   111 }
   134 
   112 
   135 /* Read data, and (optionally) run it through the CRC. */
   113 /* Read data, and (optionally) run it through the CRC. */
   171       {
   149       {
   172          png_chunk_warning(png_ptr, "CRC error");
   150          png_chunk_warning(png_ptr, "CRC error");
   173       }
   151       }
   174       else
   152       else
   175       {
   153       {
   176          png_chunk_error(png_ptr, "CRC error");
   154          png_chunk_benign_error(png_ptr, "CRC error");
       
   155          return (0);
   177       }
   156       }
   178       return (1);
   157       return (1);
   179    }
   158    }
   180 
   159 
   181    return (0);
   160    return (0);
   200    else                                                    /* critical */
   179    else                                                    /* critical */
   201    {
   180    {
   202       if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
   181       if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
   203          need_crc = 0;
   182          need_crc = 0;
   204    }
   183    }
       
   184 
       
   185 #ifdef PNG_IO_STATE_SUPPORTED
       
   186    /* Inform the I/O callback that the chunk CRC is being read */
       
   187    /* PNG_IO_CHUNK_CRC requires the I/O to be done at once */
       
   188    png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_CRC;
       
   189 #endif
   205 
   190 
   206    png_read_data(png_ptr, crc_bytes, 4);
   191    png_read_data(png_ptr, crc_bytes, 4);
   207 
   192 
   208    if (need_crc)
   193    if (need_crc)
   209    {
   194    {
   226 void /* PRIVATE */
   211 void /* PRIVATE */
   227 png_decompress_chunk(png_structp png_ptr, int comp_type,
   212 png_decompress_chunk(png_structp png_ptr, int comp_type,
   228                               png_size_t chunklength,
   213                               png_size_t chunklength,
   229                               png_size_t prefix_size, png_size_t *newlength)
   214                               png_size_t prefix_size, png_size_t *newlength)
   230 {
   215 {
   231    static PNG_CONST char msg[] = "Error decoding compressed text";
   216    static PNG_CONST char msg[] = "Error decoding compressed chunk";
   232    png_charp text;
   217    png_charp text;
   233    png_size_t text_size;
   218    png_size_t text_size;
   234 
   219 
   235    if (comp_type == PNG_COMPRESSION_TYPE_BASE)
   220    if (comp_type == PNG_COMPRESSION_TYPE_BASE)
   236    {
   221    {
   288                if (text ==  NULL)
   273                if (text ==  NULL)
   289                {
   274                {
   290                   png_free(png_ptr, png_ptr->chunkdata);
   275                   png_free(png_ptr, png_ptr->chunkdata);
   291                   png_ptr->chunkdata = NULL;
   276                   png_ptr->chunkdata = NULL;
   292                   png_error(png_ptr,
   277                   png_error(png_ptr,
   293                     "Not enough memory to decompress chunk.");
   278                     "Not enough memory to decompress chunk");
   294                }
   279                }
   295                png_memcpy(text + prefix_size, png_ptr->zbuf,
   280                png_memcpy(text + prefix_size, png_ptr->zbuf,
   296                     text_size - prefix_size);
   281                     text_size - prefix_size);
   297                png_memcpy(text, png_ptr->chunkdata, prefix_size);
   282                png_memcpy(text, png_ptr->chunkdata, prefix_size);
   298                *(text + text_size) = 0x00;
   283                *(text + text_size) = 0x00;
   300             else
   285             else
   301             {
   286             {
   302                png_charp tmp;
   287                png_charp tmp;
   303 
   288 
   304                tmp = text;
   289                tmp = text;
   305                text = (png_charp)png_malloc_warn(png_ptr,
   290 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
   306                   (png_uint_32)(text_size +
   291                if ((png_ptr->user_chunk_cache_max != 0) &&
   307                   png_ptr->zbuf_size - png_ptr->zstream.avail_out + 1));
   292                   (--png_ptr->user_chunk_cache_max == 0))
       
   293                {
       
   294                   png_warning(png_ptr, "No space in chunk cache");
       
   295                   text = NULL;
       
   296                }
       
   297 
       
   298                else
       
   299                {
       
   300 #endif
       
   301                   text = (png_charp)png_malloc_warn(png_ptr,
       
   302                      (png_size_t)(text_size +
       
   303                       png_ptr->zbuf_size - png_ptr->zstream.avail_out + 1));
       
   304 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
       
   305                }
       
   306 #endif
   308                if (text == NULL)
   307                if (text == NULL)
   309                {
   308                {
   310                   png_free(png_ptr, tmp);
   309                   png_free(png_ptr, tmp);
   311                   png_free(png_ptr, png_ptr->chunkdata);
   310                   png_free(png_ptr, png_ptr->chunkdata);
   312                   png_ptr->chunkdata = NULL;
   311                   png_ptr->chunkdata = NULL;
   313                   png_error(png_ptr,
   312                   png_error(png_ptr,
   314                     "Not enough memory to decompress chunk..");
   313                     "Not enough memory to decompress chunk");
   315                }
   314                }
   316                png_memcpy(text, tmp, text_size);
   315                png_memcpy(text, tmp, text_size);
   317                png_free(png_ptr, tmp);
   316                png_free(png_ptr, tmp);
   318                png_memcpy(text + text_size, png_ptr->zbuf,
   317                png_memcpy(text + text_size, png_ptr->zbuf,
   319                   (png_ptr->zbuf_size - png_ptr->zstream.avail_out));
   318                   (png_ptr->zbuf_size - png_ptr->zstream.avail_out));
   329             }
   328             }
   330          }
   329          }
   331       }
   330       }
   332       if (ret != Z_STREAM_END)
   331       if (ret != Z_STREAM_END)
   333       {
   332       {
   334 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
   333 #ifdef PNG_STDIO_SUPPORTED
   335          char umsg[52];
   334          char umsg[52];
   336 
   335 
   337          if (ret == Z_BUF_ERROR)
   336          if (ret == Z_BUF_ERROR)
   338             png_snprintf(umsg, 52,
   337             png_snprintf(umsg, 52,
   339                 "Buffer error in compressed datastream in %s chunk",
   338                 "Buffer error in compressed datastream in %s chunk",
   360             text = (png_charp)png_malloc_warn(png_ptr, text_size+1);
   359             text = (png_charp)png_malloc_warn(png_ptr, text_size+1);
   361             if (text == NULL)
   360             if (text == NULL)
   362               {
   361               {
   363                 png_free(png_ptr, png_ptr->chunkdata);
   362                 png_free(png_ptr, png_ptr->chunkdata);
   364                 png_ptr->chunkdata = NULL;
   363                 png_ptr->chunkdata = NULL;
   365                 png_error(png_ptr, "Not enough memory for text.");
   364                 png_error(png_ptr, "Not enough memory for text");
   366               }
   365               }
   367             png_memcpy(text, png_ptr->chunkdata, prefix_size);
   366             png_memcpy(text, png_ptr->chunkdata, prefix_size);
   368          }
   367          }
   369          *(text + text_size) = 0x00;
   368          *(text + text_size) = 0x00;
   370       }
   369       }
   376       png_ptr->chunkdata = text;
   375       png_ptr->chunkdata = text;
   377       *newlength=text_size;
   376       *newlength=text_size;
   378    }
   377    }
   379    else /* if (comp_type != PNG_COMPRESSION_TYPE_BASE) */
   378    else /* if (comp_type != PNG_COMPRESSION_TYPE_BASE) */
   380    {
   379    {
   381 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
   380 #ifdef PNG_STDIO_SUPPORTED
   382       char umsg[50];
   381       char umsg[50];
   383 
   382 
   384       png_snprintf(umsg, 50, "Unknown zTXt compression type %d", comp_type);
   383       png_snprintf(umsg, 50, "Unknown zTXt compression type %d", comp_type);
   385       png_warning(png_ptr, umsg);
   384       png_warning(png_ptr, umsg);
   386 #else
   385 #else
   428    png_ptr->width = width;
   427    png_ptr->width = width;
   429    png_ptr->height = height;
   428    png_ptr->height = height;
   430    png_ptr->bit_depth = (png_byte)bit_depth;
   429    png_ptr->bit_depth = (png_byte)bit_depth;
   431    png_ptr->interlaced = (png_byte)interlace_type;
   430    png_ptr->interlaced = (png_byte)interlace_type;
   432    png_ptr->color_type = (png_byte)color_type;
   431    png_ptr->color_type = (png_byte)color_type;
   433 #if defined(PNG_MNG_FEATURES_SUPPORTED)
   432 #ifdef PNG_MNG_FEATURES_SUPPORTED
   434    png_ptr->filter_type = (png_byte)filter_type;
   433    png_ptr->filter_type = (png_byte)filter_type;
   435 #endif
   434 #endif
   436    png_ptr->compression_type = (png_byte)compression_type;
   435    png_ptr->compression_type = (png_byte)compression_type;
   437 
   436 
   438    /* Find number of channels */
   437    /* Find number of channels */
   471 void /* PRIVATE */
   470 void /* PRIVATE */
   472 png_handle_PLTE(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
   471 png_handle_PLTE(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
   473 {
   472 {
   474    png_color palette[PNG_MAX_PALETTE_LENGTH];
   473    png_color palette[PNG_MAX_PALETTE_LENGTH];
   475    int num, i;
   474    int num, i;
   476 #ifndef PNG_NO_POINTER_INDEXING
   475 #ifdef PNG_POINTER_INDEXING_SUPPORTED
   477    png_colorp pal_ptr;
   476    png_colorp pal_ptr;
   478 #endif
   477 #endif
   479 
   478 
   480    png_debug(1, "in png_handle_PLTE");
   479    png_debug(1, "in png_handle_PLTE");
   481 
   480 
   499       png_warning(png_ptr,
   498       png_warning(png_ptr,
   500         "Ignoring PLTE chunk in grayscale PNG");
   499         "Ignoring PLTE chunk in grayscale PNG");
   501       png_crc_finish(png_ptr, length);
   500       png_crc_finish(png_ptr, length);
   502       return;
   501       return;
   503    }
   502    }
   504 #if !defined(PNG_READ_OPT_PLTE_SUPPORTED)
   503 #ifndef PNG_READ_OPT_PLTE_SUPPORTED
   505    if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
   504    if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
   506    {
   505    {
   507       png_crc_finish(png_ptr, length);
   506       png_crc_finish(png_ptr, length);
   508       return;
   507       return;
   509    }
   508    }
   524       }
   523       }
   525    }
   524    }
   526 
   525 
   527    num = (int)length / 3;
   526    num = (int)length / 3;
   528 
   527 
   529 #ifndef PNG_NO_POINTER_INDEXING
   528 #ifdef PNG_POINTER_INDEXING_SUPPORTED
   530    for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++)
   529    for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++)
   531    {
   530    {
   532       png_byte buf[3];
   531       png_byte buf[3];
   533 
   532 
   534       png_crc_read(png_ptr, buf, 3);
   533       png_crc_read(png_ptr, buf, 3);
   552    /* If we actually NEED the PLTE chunk (ie for a paletted image), we do
   551    /* If we actually NEED the PLTE chunk (ie for a paletted image), we do
   553     * whatever the normal CRC configuration tells us.  However, if we
   552     * whatever the normal CRC configuration tells us.  However, if we
   554     * have an RGB image, the PLTE can be considered ancillary, so
   553     * have an RGB image, the PLTE can be considered ancillary, so
   555     * we will act as though it is.
   554     * we will act as though it is.
   556     */
   555     */
   557 #if !defined(PNG_READ_OPT_PLTE_SUPPORTED)
   556 #ifndef PNG_READ_OPT_PLTE_SUPPORTED
   558    if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
   557    if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
   559 #endif
   558 #endif
   560    {
   559    {
   561       png_crc_finish(png_ptr, 0);
   560       png_crc_finish(png_ptr, 0);
   562    }
   561    }
   563 #if !defined(PNG_READ_OPT_PLTE_SUPPORTED)
   562 #ifndef PNG_READ_OPT_PLTE_SUPPORTED
   564    else if (png_crc_error(png_ptr))  /* Only if we have a CRC error */
   563    else if (png_crc_error(png_ptr))  /* Only if we have a CRC error */
   565    {
   564    {
   566       /* If we don't want to use the data from an ancillary chunk,
   565       /* If we don't want to use the data from an ancillary chunk,
   567          we have two options: an error abort, or a warning and we
   566          we have two options: an error abort, or a warning and we
   568          ignore the data in this chunk (which should be OK, since
   567          ignore the data in this chunk (which should be OK, since
   569          it's considered ancillary for a RGB or RGBA image). */
   568          it's considered ancillary for a RGB or RGBA image). */
   570       if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE))
   569       if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE))
   571       {
   570       {
   572          if (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)
   571          if (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)
   573          {
   572          {
   574             png_chunk_error(png_ptr, "CRC error");
   573             png_chunk_benign_error(png_ptr, "CRC error");
   575          }
   574          }
   576          else
   575          else
   577          {
   576          {
   578             png_chunk_warning(png_ptr, "CRC error");
   577             png_chunk_warning(png_ptr, "CRC error");
   579             return;
   578             return;
   587    }
   586    }
   588 #endif
   587 #endif
   589 
   588 
   590    png_set_PLTE(png_ptr, info_ptr, palette, num);
   589    png_set_PLTE(png_ptr, info_ptr, palette, num);
   591 
   590 
   592 #if defined(PNG_READ_tRNS_SUPPORTED)
   591 #ifdef PNG_READ_tRNS_SUPPORTED
   593    if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
   592    if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
   594    {
   593    {
   595       if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
   594       if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
   596       {
   595       {
   597          if (png_ptr->num_trans > (png_uint_16)num)
   596          if (png_ptr->num_trans > (png_uint_16)num)
   629    png_crc_finish(png_ptr, length);
   628    png_crc_finish(png_ptr, length);
   630 
   629 
   631    info_ptr = info_ptr; /* Quiet compiler warnings about unused info_ptr */
   630    info_ptr = info_ptr; /* Quiet compiler warnings about unused info_ptr */
   632 }
   631 }
   633 
   632 
   634 #if defined(PNG_READ_gAMA_SUPPORTED)
   633 #ifdef PNG_READ_gAMA_SUPPORTED
   635 void /* PRIVATE */
   634 void /* PRIVATE */
   636 png_handle_gAMA(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
   635 png_handle_gAMA(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
   637 {
   636 {
   638    png_fixed_point igamma;
   637    png_fixed_point igamma;
   639 #ifdef PNG_FLOATING_POINT_SUPPORTED
   638 #ifdef PNG_FLOATING_POINT_SUPPORTED
   654    else if (png_ptr->mode & PNG_HAVE_PLTE)
   653    else if (png_ptr->mode & PNG_HAVE_PLTE)
   655       /* Should be an error, but we can cope with it */
   654       /* Should be an error, but we can cope with it */
   656       png_warning(png_ptr, "Out of place gAMA chunk");
   655       png_warning(png_ptr, "Out of place gAMA chunk");
   657 
   656 
   658    if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA)
   657    if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA)
   659 #if defined(PNG_READ_sRGB_SUPPORTED)
   658 #ifdef PNG_READ_sRGB_SUPPORTED
   660       && !(info_ptr->valid & PNG_INFO_sRGB)
   659       && !(info_ptr->valid & PNG_INFO_sRGB)
   661 #endif
   660 #endif
   662       )
   661       )
   663    {
   662    {
   664       png_warning(png_ptr, "Duplicate gAMA chunk");
   663       png_warning(png_ptr, "Duplicate gAMA chunk");
   684          png_warning(png_ptr,
   683          png_warning(png_ptr,
   685            "Ignoring gAMA chunk with gamma=0");
   684            "Ignoring gAMA chunk with gamma=0");
   686          return;
   685          return;
   687       }
   686       }
   688 
   687 
   689 #if defined(PNG_READ_sRGB_SUPPORTED)
   688 #ifdef PNG_READ_sRGB_SUPPORTED
   690    if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB))
   689    if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB))
   691       if (PNG_OUT_OF_RANGE(igamma, 45500L, 500))
   690       if (PNG_OUT_OF_RANGE(igamma, 45500L, 500))
   692       {
   691       {
   693          png_warning(png_ptr,
   692          png_warning(png_ptr,
   694            "Ignoring incorrect gAMA value when sRGB is also present");
   693            "Ignoring incorrect gAMA value when sRGB is also present");
   695 #ifndef PNG_NO_CONSOLE_IO
   694 #ifdef PNG_CONSOLE_IO_SUPPORTED
   696          fprintf(stderr, "gamma = (%d/100000)", (int)igamma);
   695          fprintf(stderr, "gamma = (%d/100000)", (int)igamma);
   697 #endif
   696 #endif
   698          return;
   697          return;
   699       }
   698       }
   700 #endif /* PNG_READ_sRGB_SUPPORTED */
   699 #endif /* PNG_READ_sRGB_SUPPORTED */
   710    png_set_gAMA_fixed(png_ptr, info_ptr, igamma);
   709    png_set_gAMA_fixed(png_ptr, info_ptr, igamma);
   711 #endif
   710 #endif
   712 }
   711 }
   713 #endif
   712 #endif
   714 
   713 
   715 #if defined(PNG_READ_sBIT_SUPPORTED)
   714 #ifdef PNG_READ_sBIT_SUPPORTED
   716 void /* PRIVATE */
   715 void /* PRIVATE */
   717 png_handle_sBIT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
   716 png_handle_sBIT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
   718 {
   717 {
   719    png_size_t truelen;
   718    png_size_t truelen;
   720    png_byte buf[4];
   719    png_byte buf[4];
   776    }
   775    }
   777    png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit));
   776    png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit));
   778 }
   777 }
   779 #endif
   778 #endif
   780 
   779 
   781 #if defined(PNG_READ_cHRM_SUPPORTED)
   780 #ifdef PNG_READ_cHRM_SUPPORTED
   782 void /* PRIVATE */
   781 void /* PRIVATE */
   783 png_handle_cHRM(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
   782 png_handle_cHRM(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
   784 {
   783 {
   785    png_byte buf[32];
   784    png_byte buf[32];
   786 #ifdef PNG_FLOATING_POINT_SUPPORTED
   785 #ifdef PNG_FLOATING_POINT_SUPPORTED
   804    else if (png_ptr->mode & PNG_HAVE_PLTE)
   803    else if (png_ptr->mode & PNG_HAVE_PLTE)
   805       /* Should be an error, but we can cope with it */
   804       /* Should be an error, but we can cope with it */
   806       png_warning(png_ptr, "Missing PLTE before cHRM");
   805       png_warning(png_ptr, "Missing PLTE before cHRM");
   807 
   806 
   808    if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM)
   807    if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM)
   809 #if defined(PNG_READ_sRGB_SUPPORTED)
   808 #ifdef PNG_READ_sRGB_SUPPORTED
   810       && !(info_ptr->valid & PNG_INFO_sRGB)
   809       && !(info_ptr->valid & PNG_INFO_sRGB)
   811 #endif
   810 #endif
   812       )
   811       )
   813    {
   812    {
   814       png_warning(png_ptr, "Duplicate cHRM chunk");
   813       png_warning(png_ptr, "Duplicate cHRM chunk");
   856    green_y = (float)int_y_green / (float)100000.0;
   855    green_y = (float)int_y_green / (float)100000.0;
   857    blue_x  = (float)int_x_blue  / (float)100000.0;
   856    blue_x  = (float)int_x_blue  / (float)100000.0;
   858    blue_y  = (float)int_y_blue  / (float)100000.0;
   857    blue_y  = (float)int_y_blue  / (float)100000.0;
   859 #endif
   858 #endif
   860 
   859 
   861 #if defined(PNG_READ_sRGB_SUPPORTED)
   860 #ifdef PNG_READ_sRGB_SUPPORTED
   862    if ((info_ptr != NULL) && (info_ptr->valid & PNG_INFO_sRGB))
   861    if ((info_ptr != NULL) && (info_ptr->valid & PNG_INFO_sRGB))
   863       {
   862       {
   864       if (PNG_OUT_OF_RANGE(int_x_white, 31270,  1000) ||
   863       if (PNG_OUT_OF_RANGE(int_x_white, 31270,  1000) ||
   865           PNG_OUT_OF_RANGE(int_y_white, 32900,  1000) ||
   864           PNG_OUT_OF_RANGE(int_y_white, 32900,  1000) ||
   866           PNG_OUT_OF_RANGE(int_x_red,   64000L, 1000) ||
   865           PNG_OUT_OF_RANGE(int_x_red,   64000L, 1000) ||
   870           PNG_OUT_OF_RANGE(int_x_blue,  15000,  1000) ||
   869           PNG_OUT_OF_RANGE(int_x_blue,  15000,  1000) ||
   871           PNG_OUT_OF_RANGE(int_y_blue,   6000,  1000))
   870           PNG_OUT_OF_RANGE(int_y_blue,   6000,  1000))
   872          {
   871          {
   873             png_warning(png_ptr,
   872             png_warning(png_ptr,
   874               "Ignoring incorrect cHRM value when sRGB is also present");
   873               "Ignoring incorrect cHRM value when sRGB is also present");
   875 #ifndef PNG_NO_CONSOLE_IO
   874 #ifdef PNG_CONSOLE_IO_SUPPORTED
   876 #ifdef PNG_FLOATING_POINT_SUPPORTED
   875 #ifdef PNG_FLOATING_POINT_SUPPORTED
   877             fprintf(stderr, "wx=%f, wy=%f, rx=%f, ry=%f\n",
   876             fprintf(stderr, "wx=%f, wy=%f, rx=%f, ry=%f\n",
   878                white_x, white_y, red_x, red_y);
   877                white_x, white_y, red_x, red_y);
   879             fprintf(stderr, "gx=%f, gy=%f, bx=%f, by=%f\n",
   878             fprintf(stderr, "gx=%f, gy=%f, bx=%f, by=%f\n",
   880                green_x, green_y, blue_x, blue_y);
   879                green_x, green_y, blue_x, blue_y);
   882             fprintf(stderr, "wx=%ld, wy=%ld, rx=%ld, ry=%ld\n",
   881             fprintf(stderr, "wx=%ld, wy=%ld, rx=%ld, ry=%ld\n",
   883                int_x_white, int_y_white, int_x_red, int_y_red);
   882                int_x_white, int_y_white, int_x_red, int_y_red);
   884             fprintf(stderr, "gx=%ld, gy=%ld, bx=%ld, by=%ld\n",
   883             fprintf(stderr, "gx=%ld, gy=%ld, bx=%ld, by=%ld\n",
   885                int_x_green, int_y_green, int_x_blue, int_y_blue);
   884                int_x_green, int_y_green, int_x_blue, int_y_blue);
   886 #endif
   885 #endif
   887 #endif /* PNG_NO_CONSOLE_IO */
   886 #endif /* PNG_CONSOLE_IO_SUPPORTED */
   888          }
   887          }
   889          return;
   888          return;
   890       }
   889       }
   891 #endif /* PNG_READ_sRGB_SUPPORTED */
   890 #endif /* PNG_READ_sRGB_SUPPORTED */
   892 
   891 
   900       int_y_green, int_x_blue, int_y_blue);
   899       int_y_green, int_x_blue, int_y_blue);
   901 #endif
   900 #endif
   902 }
   901 }
   903 #endif
   902 #endif
   904 
   903 
   905 #if defined(PNG_READ_sRGB_SUPPORTED)
   904 #ifdef PNG_READ_sRGB_SUPPORTED
   906 void /* PRIVATE */
   905 void /* PRIVATE */
   907 png_handle_sRGB(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
   906 png_handle_sRGB(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
   908 {
   907 {
   909    int intent;
   908    int intent;
   910    png_byte buf[1];
   909    png_byte buf[1];
   962 #endif
   961 #endif
   963       if (PNG_OUT_OF_RANGE(igamma, 45500L, 500))
   962       if (PNG_OUT_OF_RANGE(igamma, 45500L, 500))
   964       {
   963       {
   965          png_warning(png_ptr,
   964          png_warning(png_ptr,
   966            "Ignoring incorrect gAMA value when sRGB is also present");
   965            "Ignoring incorrect gAMA value when sRGB is also present");
   967 #ifndef PNG_NO_CONSOLE_IO
   966 #ifdef PNG_CONSOLE_IO_SUPPORTED
   968 #  ifdef PNG_FIXED_POINT_SUPPORTED
   967 #  ifdef PNG_FIXED_POINT_SUPPORTED
   969          fprintf(stderr, "incorrect gamma=(%d/100000)\n",
   968          fprintf(stderr, "incorrect gamma=(%d/100000)\n",
   970             (int)png_ptr->int_gamma);
   969             (int)png_ptr->int_gamma);
   971 #  else
   970 #  else
   972 #    ifdef PNG_FLOATING_POINT_SUPPORTED
   971 #    ifdef PNG_FLOATING_POINT_SUPPORTED
   998 
   997 
   999    png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr, intent);
   998    png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr, intent);
  1000 }
   999 }
  1001 #endif /* PNG_READ_sRGB_SUPPORTED */
  1000 #endif /* PNG_READ_sRGB_SUPPORTED */
  1002 
  1001 
  1003 #if defined(PNG_READ_iCCP_SUPPORTED)
  1002 #ifdef PNG_READ_iCCP_SUPPORTED
  1004 void /* PRIVATE */
  1003 void /* PRIVATE */
  1005 png_handle_iCCP(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1004 png_handle_iCCP(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1006 /* Note: this does not properly handle chunks that are > 64K under DOS */
  1005 /* Note: this does not properly handle chunks that are > 64K under DOS */
  1007 {
  1006 {
  1008    png_byte compression_type;
  1007    png_byte compression_type;
  1107 
  1106 
  1108    if (profile_size > profile_length)
  1107    if (profile_size > profile_length)
  1109    {
  1108    {
  1110       png_free(png_ptr, png_ptr->chunkdata);
  1109       png_free(png_ptr, png_ptr->chunkdata);
  1111       png_ptr->chunkdata = NULL;
  1110       png_ptr->chunkdata = NULL;
  1112       png_warning(png_ptr, "Ignoring truncated iCCP profile.");
  1111       png_warning(png_ptr, "Ignoring truncated iCCP profile");
  1113       return;
  1112       return;
  1114    }
  1113    }
  1115 
  1114 
  1116    png_set_iCCP(png_ptr, info_ptr, png_ptr->chunkdata,
  1115    png_set_iCCP(png_ptr, info_ptr, png_ptr->chunkdata,
  1117      compression_type, png_ptr->chunkdata + prefix_length, profile_length);
  1116      compression_type, png_ptr->chunkdata + prefix_length, profile_length);
  1118    png_free(png_ptr, png_ptr->chunkdata);
  1117    png_free(png_ptr, png_ptr->chunkdata);
  1119    png_ptr->chunkdata = NULL;
  1118    png_ptr->chunkdata = NULL;
  1120 }
  1119 }
  1121 #endif /* PNG_READ_iCCP_SUPPORTED */
  1120 #endif /* PNG_READ_iCCP_SUPPORTED */
  1122 
  1121 
  1123 #if defined(PNG_READ_sPLT_SUPPORTED)
  1122 #ifdef PNG_READ_sPLT_SUPPORTED
  1124 void /* PRIVATE */
  1123 void /* PRIVATE */
  1125 png_handle_sPLT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1124 png_handle_sPLT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1126 /* Note: this does not properly handle chunks that are > 64K under DOS */
  1125 /* Note: this does not properly handle chunks that are > 64K under DOS */
  1127 {
  1126 {
  1128    png_bytep entry_start;
  1127    png_bytep entry_start;
  1129    png_sPLT_t new_palette;
  1128    png_sPLT_t new_palette;
  1130 #ifdef PNG_NO_POINTER_INDEXING
  1129 #ifdef PNG_POINTER_INDEXING_SUPPORTED
  1131    png_sPLT_entryp pp;
  1130    png_sPLT_entryp pp;
  1132 #endif
  1131 #endif
  1133    int data_length, entry_size, i;
  1132    int data_length, entry_size, i;
  1134    png_uint_32 skip = 0;
  1133    png_uint_32 skip = 0;
  1135    png_size_t slength;
  1134    png_size_t slength;
  1136 
  1135 
  1137    png_debug(1, "in png_handle_sPLT");
  1136    png_debug(1, "in png_handle_sPLT");
  1138 
  1137 
       
  1138 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
       
  1139 
       
  1140    if (png_ptr->user_chunk_cache_max != 0)
       
  1141    {
       
  1142       if (png_ptr->user_chunk_cache_max == 1)
       
  1143       {
       
  1144          png_crc_finish(png_ptr, length);
       
  1145          return;
       
  1146       }
       
  1147       if (--png_ptr->user_chunk_cache_max == 1)
       
  1148       {
       
  1149          png_warning(png_ptr, "No space in chunk cache for sPLT");
       
  1150          png_crc_finish(png_ptr, length);
       
  1151          return;
       
  1152       }
       
  1153    }
       
  1154 #endif
  1139 
  1155 
  1140    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1156    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1141       png_error(png_ptr, "Missing IHDR before sPLT");
  1157       png_error(png_ptr, "Missing IHDR before sPLT");
  1142    else if (png_ptr->mode & PNG_HAVE_IDAT)
  1158    else if (png_ptr->mode & PNG_HAVE_IDAT)
  1143    {
  1159    {
  1208    {
  1224    {
  1209        png_warning(png_ptr, "sPLT chunk requires too much memory");
  1225        png_warning(png_ptr, "sPLT chunk requires too much memory");
  1210        return;
  1226        return;
  1211    }
  1227    }
  1212 
  1228 
  1213 #ifndef PNG_NO_POINTER_INDEXING
  1229 #ifdef PNG_POINTER_INDEXING_SUPPORTED
  1214    for (i = 0; i < new_palette.nentries; i++)
  1230    for (i = 0; i < new_palette.nentries; i++)
  1215    {
  1231    {
  1216       png_sPLT_entryp pp = new_palette.entries + i;
  1232       pp = new_palette.entries + i;
  1217 
  1233 
  1218       if (new_palette.depth == 8)
  1234       if (new_palette.depth == 8)
  1219       {
  1235       {
  1220           pp->red = *entry_start++;
  1236           pp->red = *entry_start++;
  1221           pp->green = *entry_start++;
  1237           pp->green = *entry_start++;
  1263    png_ptr->chunkdata = NULL;
  1279    png_ptr->chunkdata = NULL;
  1264    png_free(png_ptr, new_palette.entries);
  1280    png_free(png_ptr, new_palette.entries);
  1265 }
  1281 }
  1266 #endif /* PNG_READ_sPLT_SUPPORTED */
  1282 #endif /* PNG_READ_sPLT_SUPPORTED */
  1267 
  1283 
  1268 #if defined(PNG_READ_tRNS_SUPPORTED)
  1284 #ifdef PNG_READ_tRNS_SUPPORTED
  1269 void /* PRIVATE */
  1285 void /* PRIVATE */
  1270 png_handle_tRNS(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1286 png_handle_tRNS(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1271 {
  1287 {
  1272    png_byte readbuf[PNG_MAX_PALETTE_LENGTH];
  1288    png_byte readbuf[PNG_MAX_PALETTE_LENGTH];
  1273 
  1289 
  1299          return;
  1315          return;
  1300       }
  1316       }
  1301 
  1317 
  1302       png_crc_read(png_ptr, buf, 2);
  1318       png_crc_read(png_ptr, buf, 2);
  1303       png_ptr->num_trans = 1;
  1319       png_ptr->num_trans = 1;
  1304       png_ptr->trans_values.gray = png_get_uint_16(buf);
  1320       png_ptr->trans_color.gray = png_get_uint_16(buf);
  1305    }
  1321    }
  1306    else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
  1322    else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
  1307    {
  1323    {
  1308       png_byte buf[6];
  1324       png_byte buf[6];
  1309 
  1325 
  1313          png_crc_finish(png_ptr, length);
  1329          png_crc_finish(png_ptr, length);
  1314          return;
  1330          return;
  1315       }
  1331       }
  1316       png_crc_read(png_ptr, buf, (png_size_t)length);
  1332       png_crc_read(png_ptr, buf, (png_size_t)length);
  1317       png_ptr->num_trans = 1;
  1333       png_ptr->num_trans = 1;
  1318       png_ptr->trans_values.red = png_get_uint_16(buf);
  1334       png_ptr->trans_color.red = png_get_uint_16(buf);
  1319       png_ptr->trans_values.green = png_get_uint_16(buf + 2);
  1335       png_ptr->trans_color.green = png_get_uint_16(buf + 2);
  1320       png_ptr->trans_values.blue = png_get_uint_16(buf + 4);
  1336       png_ptr->trans_color.blue = png_get_uint_16(buf + 4);
  1321    }
  1337    }
  1322    else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  1338    else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  1323    {
  1339    {
  1324       if (!(png_ptr->mode & PNG_HAVE_PLTE))
  1340       if (!(png_ptr->mode & PNG_HAVE_PLTE))
  1325       {
  1341       {
  1354       png_ptr->num_trans = 0;
  1370       png_ptr->num_trans = 0;
  1355       return;
  1371       return;
  1356    }
  1372    }
  1357 
  1373 
  1358    png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans,
  1374    png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans,
  1359       &(png_ptr->trans_values));
  1375       &(png_ptr->trans_color));
  1360 }
  1376 }
  1361 #endif
  1377 #endif
  1362 
  1378 
  1363 #if defined(PNG_READ_bKGD_SUPPORTED)
  1379 #ifdef PNG_READ_bKGD_SUPPORTED
  1364 void /* PRIVATE */
  1380 void /* PRIVATE */
  1365 png_handle_bKGD(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1381 png_handle_bKGD(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1366 {
  1382 {
  1367    png_size_t truelen;
  1383    png_size_t truelen;
  1368    png_byte buf[6];
  1384    png_byte buf[6];
  1447 
  1463 
  1448    png_set_bKGD(png_ptr, info_ptr, &(png_ptr->background));
  1464    png_set_bKGD(png_ptr, info_ptr, &(png_ptr->background));
  1449 }
  1465 }
  1450 #endif
  1466 #endif
  1451 
  1467 
  1452 #if defined(PNG_READ_hIST_SUPPORTED)
  1468 #ifdef PNG_READ_hIST_SUPPORTED
  1453 void /* PRIVATE */
  1469 void /* PRIVATE */
  1454 png_handle_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1470 png_handle_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1455 {
  1471 {
  1456    unsigned int num, i;
  1472    unsigned int num, i;
  1457    png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH];
  1473    png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH];
  1501 
  1517 
  1502    png_set_hIST(png_ptr, info_ptr, readbuf);
  1518    png_set_hIST(png_ptr, info_ptr, readbuf);
  1503 }
  1519 }
  1504 #endif
  1520 #endif
  1505 
  1521 
  1506 #if defined(PNG_READ_pHYs_SUPPORTED)
  1522 #ifdef PNG_READ_pHYs_SUPPORTED
  1507 void /* PRIVATE */
  1523 void /* PRIVATE */
  1508 png_handle_pHYs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1524 png_handle_pHYs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1509 {
  1525 {
  1510    png_byte buf[9];
  1526    png_byte buf[9];
  1511    png_uint_32 res_x, res_y;
  1527    png_uint_32 res_x, res_y;
  1544    unit_type = buf[8];
  1560    unit_type = buf[8];
  1545    png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type);
  1561    png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type);
  1546 }
  1562 }
  1547 #endif
  1563 #endif
  1548 
  1564 
  1549 #if defined(PNG_READ_oFFs_SUPPORTED)
  1565 #ifdef PNG_READ_oFFs_SUPPORTED
  1550 void /* PRIVATE */
  1566 void /* PRIVATE */
  1551 png_handle_oFFs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1567 png_handle_oFFs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1552 {
  1568 {
  1553    png_byte buf[9];
  1569    png_byte buf[9];
  1554    png_int_32 offset_x, offset_y;
  1570    png_int_32 offset_x, offset_y;
  1587    unit_type = buf[8];
  1603    unit_type = buf[8];
  1588    png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);
  1604    png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);
  1589 }
  1605 }
  1590 #endif
  1606 #endif
  1591 
  1607 
  1592 #if defined(PNG_READ_pCAL_SUPPORTED)
  1608 #ifdef PNG_READ_pCAL_SUPPORTED
  1593 /* Read the pCAL chunk (described in the PNG Extensions document) */
  1609 /* Read the pCAL chunk (described in the PNG Extensions document) */
  1594 void /* PRIVATE */
  1610 void /* PRIVATE */
  1595 png_handle_pCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1611 png_handle_pCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1596 {
  1612 {
  1597    png_int_32 X0, X1;
  1613    png_int_32 X0, X1;
  1622       length + 1);
  1638       length + 1);
  1623    png_free(png_ptr, png_ptr->chunkdata);
  1639    png_free(png_ptr, png_ptr->chunkdata);
  1624    png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
  1640    png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
  1625    if (png_ptr->chunkdata == NULL)
  1641    if (png_ptr->chunkdata == NULL)
  1626      {
  1642      {
  1627        png_warning(png_ptr, "No memory for pCAL purpose.");
  1643        png_warning(png_ptr, "No memory for pCAL purpose");
  1628        return;
  1644        return;
  1629      }
  1645      }
  1630    slength = (png_size_t)length;
  1646    slength = (png_size_t)length;
  1631    png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
  1647    png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
  1632 
  1648 
  1683    for (buf = units; *buf; buf++)
  1699    for (buf = units; *buf; buf++)
  1684       /* Empty loop to move past the units string. */ ;
  1700       /* Empty loop to move past the units string. */ ;
  1685 
  1701 
  1686    png_debug(3, "Allocating pCAL parameters array");
  1702    png_debug(3, "Allocating pCAL parameters array");
  1687    params = (png_charpp)png_malloc_warn(png_ptr,
  1703    params = (png_charpp)png_malloc_warn(png_ptr,
  1688       (png_uint_32)(nparams * png_sizeof(png_charp))) ;
  1704       (png_size_t)(nparams * png_sizeof(png_charp)));
  1689    if (params == NULL)
  1705    if (params == NULL)
  1690      {
  1706      {
  1691        png_free(png_ptr, png_ptr->chunkdata);
  1707        png_free(png_ptr, png_ptr->chunkdata);
  1692        png_ptr->chunkdata = NULL;
  1708        png_ptr->chunkdata = NULL;
  1693        png_warning(png_ptr, "No memory for pCAL params.");
  1709        png_warning(png_ptr, "No memory for pCAL params");
  1694        return;
  1710        return;
  1695      }
  1711      }
  1696 
  1712 
  1697    /* Get pointers to the start of each parameter string. */
  1713    /* Get pointers to the start of each parameter string. */
  1698    for (i = 0; i < (int)nparams; i++)
  1714    for (i = 0; i < (int)nparams; i++)
  1721    png_ptr->chunkdata = NULL;
  1737    png_ptr->chunkdata = NULL;
  1722    png_free(png_ptr, params);
  1738    png_free(png_ptr, params);
  1723 }
  1739 }
  1724 #endif
  1740 #endif
  1725 
  1741 
  1726 #if defined(PNG_READ_sCAL_SUPPORTED)
  1742 #ifdef PNG_READ_sCAL_SUPPORTED
  1727 /* Read the sCAL chunk */
  1743 /* Read the sCAL chunk */
  1728 void /* PRIVATE */
  1744 void /* PRIVATE */
  1729 png_handle_sCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1745 png_handle_sCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1730 {
  1746 {
  1731    png_charp ep;
  1747    png_charp ep;
  1791    if (swidth == NULL)
  1807    if (swidth == NULL)
  1792    {
  1808    {
  1793       png_warning(png_ptr, "Out of memory while processing sCAL chunk width");
  1809       png_warning(png_ptr, "Out of memory while processing sCAL chunk width");
  1794       return;
  1810       return;
  1795    }
  1811    }
  1796    png_memcpy(swidth, ep, (png_size_t)png_strlen(ep));
  1812    png_memcpy(swidth, ep, png_strlen(ep));
  1797 #endif
  1813 #endif
  1798 #endif
  1814 #endif
  1799 
  1815 
  1800    for (ep = png_ptr->chunkdata; *ep; ep++)
  1816    for (ep = png_ptr->chunkdata; *ep; ep++)
  1801       /* Empty loop */ ;
  1817       /* Empty loop */ ;
  1826    if (sheight == NULL)
  1842    if (sheight == NULL)
  1827    {
  1843    {
  1828       png_warning(png_ptr, "Out of memory while processing sCAL chunk height");
  1844       png_warning(png_ptr, "Out of memory while processing sCAL chunk height");
  1829       return;
  1845       return;
  1830    }
  1846    }
  1831    png_memcpy(sheight, ep, (png_size_t)png_strlen(ep));
  1847    png_memcpy(sheight, ep, png_strlen(ep));
  1832 #endif
  1848 #endif
  1833 #endif
  1849 #endif
  1834 
  1850 
  1835    if (png_ptr->chunkdata + slength < ep
  1851    if (png_ptr->chunkdata + slength < ep
  1836 #ifdef PNG_FLOATING_POINT_SUPPORTED
  1852 #ifdef PNG_FLOATING_POINT_SUPPORTED
  1864    png_free(png_ptr, sheight);
  1880    png_free(png_ptr, sheight);
  1865 #endif
  1881 #endif
  1866 }
  1882 }
  1867 #endif
  1883 #endif
  1868 
  1884 
  1869 #if defined(PNG_READ_tIME_SUPPORTED)
  1885 #ifdef PNG_READ_tIME_SUPPORTED
  1870 void /* PRIVATE */
  1886 void /* PRIVATE */
  1871 png_handle_tIME(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1887 png_handle_tIME(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1872 {
  1888 {
  1873    png_byte buf[7];
  1889    png_byte buf[7];
  1874    png_time mod_time;
  1890    png_time mod_time;
  1907 
  1923 
  1908    png_set_tIME(png_ptr, info_ptr, &mod_time);
  1924    png_set_tIME(png_ptr, info_ptr, &mod_time);
  1909 }
  1925 }
  1910 #endif
  1926 #endif
  1911 
  1927 
  1912 #if defined(PNG_READ_tEXt_SUPPORTED)
  1928 #ifdef PNG_READ_tEXt_SUPPORTED
  1913 /* Note: this does not properly handle chunks that are > 64K under DOS */
  1929 /* Note: this does not properly handle chunks that are > 64K under DOS */
  1914 void /* PRIVATE */
  1930 void /* PRIVATE */
  1915 png_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1931 png_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1916 {
  1932 {
  1917    png_textp text_ptr;
  1933    png_textp text_ptr;
  1921    png_size_t slength;
  1937    png_size_t slength;
  1922    int ret;
  1938    int ret;
  1923 
  1939 
  1924    png_debug(1, "in png_handle_tEXt");
  1940    png_debug(1, "in png_handle_tEXt");
  1925 
  1941 
       
  1942 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
       
  1943    if (png_ptr->user_chunk_cache_max != 0)
       
  1944    {
       
  1945       if (png_ptr->user_chunk_cache_max == 1)
       
  1946       {
       
  1947          png_crc_finish(png_ptr, length);
       
  1948          return;
       
  1949       }
       
  1950       if (--png_ptr->user_chunk_cache_max == 1)
       
  1951       {
       
  1952          png_warning(png_ptr, "No space in chunk cache for tEXt");
       
  1953          png_crc_finish(png_ptr, length);
       
  1954          return;
       
  1955       }
       
  1956    }
       
  1957 #endif
  1926 
  1958 
  1927    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1959    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1928       png_error(png_ptr, "Missing IHDR before tEXt");
  1960       png_error(png_ptr, "Missing IHDR before tEXt");
  1929 
  1961 
  1930    if (png_ptr->mode & PNG_HAVE_IDAT)
  1962    if (png_ptr->mode & PNG_HAVE_IDAT)
  1942    png_free(png_ptr, png_ptr->chunkdata);
  1974    png_free(png_ptr, png_ptr->chunkdata);
  1943 
  1975 
  1944    png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
  1976    png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
  1945    if (png_ptr->chunkdata == NULL)
  1977    if (png_ptr->chunkdata == NULL)
  1946    {
  1978    {
  1947      png_warning(png_ptr, "No memory to process text chunk.");
  1979      png_warning(png_ptr, "No memory to process text chunk");
  1948      return;
  1980      return;
  1949    }
  1981    }
  1950    slength = (png_size_t)length;
  1982    slength = (png_size_t)length;
  1951    png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
  1983    png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
  1952 
  1984 
  1966 
  1998 
  1967    if (text != key + slength)
  1999    if (text != key + slength)
  1968       text++;
  2000       text++;
  1969 
  2001 
  1970    text_ptr = (png_textp)png_malloc_warn(png_ptr,
  2002    text_ptr = (png_textp)png_malloc_warn(png_ptr,
  1971       (png_uint_32)png_sizeof(png_text));
  2003       png_sizeof(png_text));
  1972    if (text_ptr == NULL)
  2004    if (text_ptr == NULL)
  1973    {
  2005    {
  1974      png_warning(png_ptr, "Not enough memory to process text chunk.");
  2006      png_warning(png_ptr, "Not enough memory to process text chunk");
  1975      png_free(png_ptr, png_ptr->chunkdata);
  2007      png_free(png_ptr, png_ptr->chunkdata);
  1976      png_ptr->chunkdata = NULL;
  2008      png_ptr->chunkdata = NULL;
  1977      return;
  2009      return;
  1978    }
  2010    }
  1979    text_ptr->compression = PNG_TEXT_COMPRESSION_NONE;
  2011    text_ptr->compression = PNG_TEXT_COMPRESSION_NONE;
  1990 
  2022 
  1991    png_free(png_ptr, png_ptr->chunkdata);
  2023    png_free(png_ptr, png_ptr->chunkdata);
  1992    png_ptr->chunkdata = NULL;
  2024    png_ptr->chunkdata = NULL;
  1993    png_free(png_ptr, text_ptr);
  2025    png_free(png_ptr, text_ptr);
  1994    if (ret)
  2026    if (ret)
  1995      png_warning(png_ptr, "Insufficient memory to process text chunk.");
  2027      png_warning(png_ptr, "Insufficient memory to process text chunk");
  1996 }
  2028 }
  1997 #endif
  2029 #endif
  1998 
  2030 
  1999 #if defined(PNG_READ_zTXt_SUPPORTED)
  2031 #ifdef PNG_READ_zTXt_SUPPORTED
  2000 /* Note: this does not correctly handle chunks that are > 64K under DOS */
  2032 /* Note: this does not correctly handle chunks that are > 64K under DOS */
  2001 void /* PRIVATE */
  2033 void /* PRIVATE */
  2002 png_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  2034 png_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  2003 {
  2035 {
  2004    png_textp text_ptr;
  2036    png_textp text_ptr;
  2007    int ret;
  2039    int ret;
  2008    png_size_t slength, prefix_len, data_len;
  2040    png_size_t slength, prefix_len, data_len;
  2009 
  2041 
  2010    png_debug(1, "in png_handle_zTXt");
  2042    png_debug(1, "in png_handle_zTXt");
  2011 
  2043 
       
  2044 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
       
  2045    if (png_ptr->user_chunk_cache_max != 0)
       
  2046    {
       
  2047       if (png_ptr->user_chunk_cache_max == 1)
       
  2048       {
       
  2049          png_crc_finish(png_ptr, length);
       
  2050          return;
       
  2051       }
       
  2052       if (--png_ptr->user_chunk_cache_max == 1)
       
  2053       {
       
  2054          png_warning(png_ptr, "No space in chunk cache for zTXt");
       
  2055          png_crc_finish(png_ptr, length);
       
  2056          return;
       
  2057       }
       
  2058    }
       
  2059 #endif
  2012 
  2060 
  2013    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  2061    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  2014       png_error(png_ptr, "Missing IHDR before zTXt");
  2062       png_error(png_ptr, "Missing IHDR before zTXt");
  2015 
  2063 
  2016    if (png_ptr->mode & PNG_HAVE_IDAT)
  2064    if (png_ptr->mode & PNG_HAVE_IDAT)
  2029 
  2077 
  2030    png_free(png_ptr, png_ptr->chunkdata);
  2078    png_free(png_ptr, png_ptr->chunkdata);
  2031    png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
  2079    png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
  2032    if (png_ptr->chunkdata == NULL)
  2080    if (png_ptr->chunkdata == NULL)
  2033    {
  2081    {
  2034      png_warning(png_ptr, "Out of memory processing zTXt chunk.");
  2082      png_warning(png_ptr, "Out of memory processing zTXt chunk");
  2035      return;
  2083      return;
  2036    }
  2084    }
  2037    slength = (png_size_t)length;
  2085    slength = (png_size_t)length;
  2038    png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
  2086    png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
  2039    if (png_crc_finish(png_ptr, 0))
  2087    if (png_crc_finish(png_ptr, 0))
  2070 
  2118 
  2071    png_decompress_chunk(png_ptr, comp_type,
  2119    png_decompress_chunk(png_ptr, comp_type,
  2072      (png_size_t)length, prefix_len, &data_len);
  2120      (png_size_t)length, prefix_len, &data_len);
  2073 
  2121 
  2074    text_ptr = (png_textp)png_malloc_warn(png_ptr,
  2122    text_ptr = (png_textp)png_malloc_warn(png_ptr,
  2075       (png_uint_32)png_sizeof(png_text));
  2123       png_sizeof(png_text));
  2076    if (text_ptr == NULL)
  2124    if (text_ptr == NULL)
  2077    {
  2125    {
  2078      png_warning(png_ptr, "Not enough memory to process zTXt chunk.");
  2126      png_warning(png_ptr, "Not enough memory to process zTXt chunk");
  2079      png_free(png_ptr, png_ptr->chunkdata);
  2127      png_free(png_ptr, png_ptr->chunkdata);
  2080      png_ptr->chunkdata = NULL;
  2128      png_ptr->chunkdata = NULL;
  2081      return;
  2129      return;
  2082    }
  2130    }
  2083    text_ptr->compression = comp_type;
  2131    text_ptr->compression = comp_type;
  2094 
  2142 
  2095    png_free(png_ptr, text_ptr);
  2143    png_free(png_ptr, text_ptr);
  2096    png_free(png_ptr, png_ptr->chunkdata);
  2144    png_free(png_ptr, png_ptr->chunkdata);
  2097    png_ptr->chunkdata = NULL;
  2145    png_ptr->chunkdata = NULL;
  2098    if (ret)
  2146    if (ret)
  2099      png_error(png_ptr, "Insufficient memory to store zTXt chunk.");
  2147      png_error(png_ptr, "Insufficient memory to store zTXt chunk");
  2100 }
  2148 }
  2101 #endif
  2149 #endif
  2102 
  2150 
  2103 #if defined(PNG_READ_iTXt_SUPPORTED)
  2151 #ifdef PNG_READ_iTXt_SUPPORTED
  2104 /* Note: this does not correctly handle chunks that are > 64K under DOS */
  2152 /* Note: this does not correctly handle chunks that are > 64K under DOS */
  2105 void /* PRIVATE */
  2153 void /* PRIVATE */
  2106 png_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  2154 png_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  2107 {
  2155 {
  2108    png_textp text_ptr;
  2156    png_textp text_ptr;
  2112    int ret;
  2160    int ret;
  2113    png_size_t slength, prefix_len, data_len;
  2161    png_size_t slength, prefix_len, data_len;
  2114 
  2162 
  2115    png_debug(1, "in png_handle_iTXt");
  2163    png_debug(1, "in png_handle_iTXt");
  2116 
  2164 
       
  2165 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
       
  2166    if (png_ptr->user_chunk_cache_max != 0)
       
  2167    {
       
  2168       if (png_ptr->user_chunk_cache_max == 1)
       
  2169       {
       
  2170          png_crc_finish(png_ptr, length);
       
  2171          return;
       
  2172       }
       
  2173       if (--png_ptr->user_chunk_cache_max == 1)
       
  2174       {
       
  2175          png_warning(png_ptr, "No space in chunk cache for iTXt");
       
  2176          png_crc_finish(png_ptr, length);
       
  2177          return;
       
  2178       }
       
  2179    }
       
  2180 #endif
  2117 
  2181 
  2118    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  2182    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  2119       png_error(png_ptr, "Missing IHDR before iTXt");
  2183       png_error(png_ptr, "Missing IHDR before iTXt");
  2120 
  2184 
  2121    if (png_ptr->mode & PNG_HAVE_IDAT)
  2185    if (png_ptr->mode & PNG_HAVE_IDAT)
  2134 
  2198 
  2135    png_free(png_ptr, png_ptr->chunkdata);
  2199    png_free(png_ptr, png_ptr->chunkdata);
  2136    png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
  2200    png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
  2137    if (png_ptr->chunkdata == NULL)
  2201    if (png_ptr->chunkdata == NULL)
  2138    {
  2202    {
  2139      png_warning(png_ptr, "No memory to process iTXt chunk.");
  2203      png_warning(png_ptr, "No memory to process iTXt chunk");
  2140      return;
  2204      return;
  2141    }
  2205    }
  2142    slength = (png_size_t)length;
  2206    slength = (png_size_t)length;
  2143    png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
  2207    png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
  2144    if (png_crc_finish(png_ptr, 0))
  2208    if (png_crc_finish(png_ptr, 0))
  2202        png_decompress_chunk(png_ptr, comp_type,
  2266        png_decompress_chunk(png_ptr, comp_type,
  2203          (size_t)length, prefix_len, &data_len);
  2267          (size_t)length, prefix_len, &data_len);
  2204    else
  2268    else
  2205        data_len = png_strlen(png_ptr->chunkdata + prefix_len);
  2269        data_len = png_strlen(png_ptr->chunkdata + prefix_len);
  2206    text_ptr = (png_textp)png_malloc_warn(png_ptr,
  2270    text_ptr = (png_textp)png_malloc_warn(png_ptr,
  2207       (png_uint_32)png_sizeof(png_text));
  2271       png_sizeof(png_text));
  2208    if (text_ptr == NULL)
  2272    if (text_ptr == NULL)
  2209    {
  2273    {
  2210      png_warning(png_ptr, "Not enough memory to process iTXt chunk.");
  2274      png_warning(png_ptr, "Not enough memory to process iTXt chunk");
  2211      png_free(png_ptr, png_ptr->chunkdata);
  2275      png_free(png_ptr, png_ptr->chunkdata);
  2212      png_ptr->chunkdata = NULL;
  2276      png_ptr->chunkdata = NULL;
  2213      return;
  2277      return;
  2214    }
  2278    }
  2215    text_ptr->compression = (int)comp_flag + 1;
  2279    text_ptr->compression = (int)comp_flag + 1;
  2224 
  2288 
  2225    png_free(png_ptr, text_ptr);
  2289    png_free(png_ptr, text_ptr);
  2226    png_free(png_ptr, png_ptr->chunkdata);
  2290    png_free(png_ptr, png_ptr->chunkdata);
  2227    png_ptr->chunkdata = NULL;
  2291    png_ptr->chunkdata = NULL;
  2228    if (ret)
  2292    if (ret)
  2229      png_error(png_ptr, "Insufficient memory to store iTXt chunk.");
  2293      png_error(png_ptr, "Insufficient memory to store iTXt chunk");
  2230 }
  2294 }
  2231 #endif
  2295 #endif
  2232 
  2296 
  2233 /* This function is called when we haven't found a handler for a
  2297 /* This function is called when we haven't found a handler for a
  2234    chunk.  If there isn't a problem with the chunk itself (ie bad
  2298    chunk.  If there isn't a problem with the chunk itself (ie bad
  2240 {
  2304 {
  2241    png_uint_32 skip = 0;
  2305    png_uint_32 skip = 0;
  2242 
  2306 
  2243    png_debug(1, "in png_handle_unknown");
  2307    png_debug(1, "in png_handle_unknown");
  2244 
  2308 
       
  2309 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
       
  2310    if (png_ptr->user_chunk_cache_max != 0)
       
  2311    {
       
  2312       if (png_ptr->user_chunk_cache_max == 1)
       
  2313       {
       
  2314          png_crc_finish(png_ptr, length);
       
  2315          return;
       
  2316       }
       
  2317       if (--png_ptr->user_chunk_cache_max == 1)
       
  2318       {
       
  2319          png_warning(png_ptr, "No space in chunk cache for unknown chunk");
       
  2320          png_crc_finish(png_ptr, length);
       
  2321          return;
       
  2322       }
       
  2323    }
       
  2324 #endif
  2245 
  2325 
  2246    if (png_ptr->mode & PNG_HAVE_IDAT)
  2326    if (png_ptr->mode & PNG_HAVE_IDAT)
  2247    {
  2327    {
  2248 #ifdef PNG_USE_LOCAL_ARRAYS
  2328       PNG_IDAT;
  2249       PNG_CONST PNG_IDAT;
       
  2250 #endif
       
  2251       if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))  /* Not an IDAT */
  2329       if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))  /* Not an IDAT */
  2252          png_ptr->mode |= PNG_AFTER_IDAT;
  2330          png_ptr->mode |= PNG_AFTER_IDAT;
  2253    }
  2331    }
  2254 
  2332 
  2255    if (!(png_ptr->chunk_name[0] & 0x20))
  2333    if (!(png_ptr->chunk_name[0] & 0x20))
  2256    {
  2334    {
  2257 #if defined(PNG_HANDLE_AS_UNKNOWN_SUPPORTED)
  2335 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  2258       if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
  2336       if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
  2259            PNG_HANDLE_CHUNK_ALWAYS
  2337            PNG_HANDLE_CHUNK_ALWAYS
  2260 #if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
  2338 #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
  2261            && png_ptr->read_user_chunk_fn == NULL
  2339            && png_ptr->read_user_chunk_fn == NULL
  2262 #endif
  2340 #endif
  2263         )
  2341         )
  2264 #endif
  2342 #endif
  2265           png_chunk_error(png_ptr, "unknown critical chunk");
  2343           png_chunk_error(png_ptr, "unknown critical chunk");
  2266    }
  2344    }
  2267 
  2345 
  2268 #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
  2346 #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
  2269    if ((png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS)
  2347    if ((png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS)
  2270 #if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
  2348 #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
  2271        || (png_ptr->read_user_chunk_fn != NULL)
  2349        || (png_ptr->read_user_chunk_fn != NULL)
  2272 #endif
  2350 #endif
  2273         )
  2351         )
  2274    {
  2352    {
  2275 #ifdef PNG_MAX_MALLOC_64K
  2353 #ifdef PNG_MAX_MALLOC_64K
  2290        else
  2368        else
  2291        {
  2369        {
  2292          png_ptr->unknown_chunk.data = (png_bytep)png_malloc(png_ptr, length);
  2370          png_ptr->unknown_chunk.data = (png_bytep)png_malloc(png_ptr, length);
  2293          png_crc_read(png_ptr, (png_bytep)png_ptr->unknown_chunk.data, length);
  2371          png_crc_read(png_ptr, (png_bytep)png_ptr->unknown_chunk.data, length);
  2294        }
  2372        }
  2295 #if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
  2373 #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
  2296        if (png_ptr->read_user_chunk_fn != NULL)
  2374        if (png_ptr->read_user_chunk_fn != NULL)
  2297        {
  2375        {
  2298           /* Callback to user unknown chunk handler */
  2376           /* Callback to user unknown chunk handler */
  2299           int ret;
  2377           int ret;
  2300           ret = (*(png_ptr->read_user_chunk_fn))
  2378           ret = (*(png_ptr->read_user_chunk_fn))
  2302           if (ret < 0)
  2380           if (ret < 0)
  2303              png_chunk_error(png_ptr, "error in user chunk");
  2381              png_chunk_error(png_ptr, "error in user chunk");
  2304           if (ret == 0)
  2382           if (ret == 0)
  2305           {
  2383           {
  2306              if (!(png_ptr->chunk_name[0] & 0x20))
  2384              if (!(png_ptr->chunk_name[0] & 0x20))
  2307 #if defined(PNG_HANDLE_AS_UNKNOWN_SUPPORTED)
  2385 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  2308                 if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
  2386                 if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
  2309                      PNG_HANDLE_CHUNK_ALWAYS)
  2387                      PNG_HANDLE_CHUNK_ALWAYS)
  2310 #endif
  2388 #endif
  2311                    png_chunk_error(png_ptr, "unknown critical chunk");
  2389                    png_chunk_error(png_ptr, "unknown critical chunk");
  2312              png_set_unknown_chunks(png_ptr, info_ptr,
  2390              png_set_unknown_chunks(png_ptr, info_ptr,
  2323 #endif
  2401 #endif
  2324       skip = length;
  2402       skip = length;
  2325 
  2403 
  2326    png_crc_finish(png_ptr, skip);
  2404    png_crc_finish(png_ptr, skip);
  2327 
  2405 
  2328 #if !defined(PNG_READ_USER_CHUNKS_SUPPORTED)
  2406 #ifndef PNG_READ_USER_CHUNKS_SUPPORTED
  2329    info_ptr = info_ptr; /* Quiet compiler warnings about unused info_ptr */
  2407    info_ptr = info_ptr; /* Quiet compiler warnings about unused info_ptr */
  2330 #endif
  2408 #endif
  2331 }
  2409 }
  2332 
  2410 
  2333 /* This function is called to verify that a chunk name is valid.
  2411 /* This function is called to verify that a chunk name is valid.
  2381             int m = 0x80;
  2459             int m = 0x80;
  2382             int shift;
  2460             int shift;
  2383             png_uint_32 i;
  2461             png_uint_32 i;
  2384             png_uint_32 row_width = png_ptr->width;
  2462             png_uint_32 row_width = png_ptr->width;
  2385 
  2463 
  2386 #if defined(PNG_READ_PACKSWAP_SUPPORTED)
  2464 #ifdef PNG_READ_PACKSWAP_SUPPORTED
  2387             if (png_ptr->transformations & PNG_PACKSWAP)
  2465             if (png_ptr->transformations & PNG_PACKSWAP)
  2388             {
  2466             {
  2389                 s_start = 0;
  2467                 s_start = 0;
  2390                 s_end = 7;
  2468                 s_end = 7;
  2391                 s_inc = 1;
  2469                 s_inc = 1;
  2436             int shift;
  2514             int shift;
  2437             png_uint_32 i;
  2515             png_uint_32 i;
  2438             png_uint_32 row_width = png_ptr->width;
  2516             png_uint_32 row_width = png_ptr->width;
  2439             int value;
  2517             int value;
  2440 
  2518 
  2441 #if defined(PNG_READ_PACKSWAP_SUPPORTED)
  2519 #ifdef PNG_READ_PACKSWAP_SUPPORTED
  2442             if (png_ptr->transformations & PNG_PACKSWAP)
  2520             if (png_ptr->transformations & PNG_PACKSWAP)
  2443             {
  2521             {
  2444                s_start = 0;
  2522                s_start = 0;
  2445                s_end = 6;
  2523                s_end = 6;
  2446                s_inc = 2;
  2524                s_inc = 2;
  2488             int shift;
  2566             int shift;
  2489             png_uint_32 i;
  2567             png_uint_32 i;
  2490             png_uint_32 row_width = png_ptr->width;
  2568             png_uint_32 row_width = png_ptr->width;
  2491             int value;
  2569             int value;
  2492 
  2570 
  2493 #if defined(PNG_READ_PACKSWAP_SUPPORTED)
  2571 #ifdef PNG_READ_PACKSWAP_SUPPORTED
  2494             if (png_ptr->transformations & PNG_PACKSWAP)
  2572             if (png_ptr->transformations & PNG_PACKSWAP)
  2495             {
  2573             {
  2496                s_start = 0;
  2574                s_start = 0;
  2497                s_end = 4;
  2575                s_end = 4;
  2498                s_inc = 4;
  2576                s_inc = 4;
  2571 {
  2649 {
  2572    png_row_infop row_info = &(png_ptr->row_info);
  2650    png_row_infop row_info = &(png_ptr->row_info);
  2573    png_bytep row = png_ptr->row_buf + 1;
  2651    png_bytep row = png_ptr->row_buf + 1;
  2574    int pass = png_ptr->pass;
  2652    int pass = png_ptr->pass;
  2575    png_uint_32 transformations = png_ptr->transformations;
  2653    png_uint_32 transformations = png_ptr->transformations;
  2576 #ifdef PNG_USE_LOCAL_ARRAYS
       
  2577    /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  2654    /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  2578    /* Offset to next interlace block */
  2655    /* Offset to next interlace block */
  2579    PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
  2656    PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
  2580 #endif
       
  2581 
  2657 
  2582    png_debug(1, "in png_do_read_interlace");
  2658    png_debug(1, "in png_do_read_interlace");
  2583    if (row != NULL && row_info != NULL)
  2659    if (row != NULL && row_info != NULL)
  2584    {
  2660    {
  2585       png_uint_32 final_width;
  2661       png_uint_32 final_width;
  2597             int jstop = png_pass_inc[pass];
  2673             int jstop = png_pass_inc[pass];
  2598             png_byte v;
  2674             png_byte v;
  2599             png_uint_32 i;
  2675             png_uint_32 i;
  2600             int j;
  2676             int j;
  2601 
  2677 
  2602 #if defined(PNG_READ_PACKSWAP_SUPPORTED)
  2678 #ifdef PNG_READ_PACKSWAP_SUPPORTED
  2603             if (transformations & PNG_PACKSWAP)
  2679             if (transformations & PNG_PACKSWAP)
  2604             {
  2680             {
  2605                 sshift = (int)((row_info->width + 7) & 0x07);
  2681                 sshift = (int)((row_info->width + 7) & 0x07);
  2606                 dshift = (int)((final_width + 7) & 0x07);
  2682                 dshift = (int)((final_width + 7) & 0x07);
  2607                 s_start = 7;
  2683                 s_start = 7;
  2650             int sshift, dshift;
  2726             int sshift, dshift;
  2651             int s_start, s_end, s_inc;
  2727             int s_start, s_end, s_inc;
  2652             int jstop = png_pass_inc[pass];
  2728             int jstop = png_pass_inc[pass];
  2653             png_uint_32 i;
  2729             png_uint_32 i;
  2654 
  2730 
  2655 #if defined(PNG_READ_PACKSWAP_SUPPORTED)
  2731 #ifdef PNG_READ_PACKSWAP_SUPPORTED
  2656             if (transformations & PNG_PACKSWAP)
  2732             if (transformations & PNG_PACKSWAP)
  2657             {
  2733             {
  2658                sshift = (int)(((row_info->width + 3) & 0x03) << 1);
  2734                sshift = (int)(((row_info->width + 3) & 0x03) << 1);
  2659                dshift = (int)(((final_width + 3) & 0x03) << 1);
  2735                dshift = (int)(((final_width + 3) & 0x03) << 1);
  2660                s_start = 6;
  2736                s_start = 6;
  2706             int sshift, dshift;
  2782             int sshift, dshift;
  2707             int s_start, s_end, s_inc;
  2783             int s_start, s_end, s_inc;
  2708             png_uint_32 i;
  2784             png_uint_32 i;
  2709             int jstop = png_pass_inc[pass];
  2785             int jstop = png_pass_inc[pass];
  2710 
  2786 
  2711 #if defined(PNG_READ_PACKSWAP_SUPPORTED)
  2787 #ifdef PNG_READ_PACKSWAP_SUPPORTED
  2712             if (transformations & PNG_PACKSWAP)
  2788             if (transformations & PNG_PACKSWAP)
  2713             {
  2789             {
  2714                sshift = (int)(((row_info->width + 1) & 0x01) << 2);
  2790                sshift = (int)(((row_info->width + 1) & 0x01) << 2);
  2715                dshift = (int)(((final_width + 1) & 0x01) << 2);
  2791                dshift = (int)(((final_width + 1) & 0x01) << 2);
  2716                s_start = 4;
  2792                s_start = 4;
  2780          }
  2856          }
  2781       }
  2857       }
  2782       row_info->width = final_width;
  2858       row_info->width = final_width;
  2783       row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width);
  2859       row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width);
  2784    }
  2860    }
  2785 #if !defined(PNG_READ_PACKSWAP_SUPPORTED)
  2861 #ifndef PNG_READ_PACKSWAP_SUPPORTED
  2786    transformations = transformations; /* Silence compiler warning */
  2862    transformations = transformations; /* Silence compiler warning */
  2787 #endif
  2863 #endif
  2788 }
  2864 }
  2789 #endif /* PNG_READ_INTERLACING_SUPPORTED */
  2865 #endif /* PNG_READ_INTERLACING_SUPPORTED */
  2790 
  2866 
  2909          *row = 0;
  2985          *row = 0;
  2910          break;
  2986          break;
  2911    }
  2987    }
  2912 }
  2988 }
  2913 
  2989 
  2914 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
  2990 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  2915 void /* PRIVATE */
  2991 void /* PRIVATE */
  2916 png_read_finish_row(png_structp png_ptr)
  2992 png_read_finish_row(png_structp png_ptr)
  2917 {
  2993 {
  2918 #ifdef PNG_USE_LOCAL_ARRAYS
       
  2919 #ifdef PNG_READ_INTERLACING_SUPPORTED
  2994 #ifdef PNG_READ_INTERLACING_SUPPORTED
  2920    /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  2995    /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  2921 
  2996 
  2922    /* Start of interlace block */
  2997    /* Start of interlace block */
  2923    PNG_CONST int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
  2998    PNG_CONST int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
  2929    PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
  3004    PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
  2930 
  3005 
  2931    /* Offset to next interlace block in the y direction */
  3006    /* Offset to next interlace block in the y direction */
  2932    PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
  3007    PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
  2933 #endif /* PNG_READ_INTERLACING_SUPPORTED */
  3008 #endif /* PNG_READ_INTERLACING_SUPPORTED */
  2934 #endif
       
  2935 
  3009 
  2936    png_debug(1, "in png_read_finish_row");
  3010    png_debug(1, "in png_read_finish_row");
  2937    png_ptr->row_number++;
  3011    png_ptr->row_number++;
  2938    if (png_ptr->row_number < png_ptr->num_rows)
  3012    if (png_ptr->row_number < png_ptr->num_rows)
  2939       return;
  3013       return;
  2940 
  3014 
  2941 #ifdef PNG_READ_INTERLACING_SUPPORTED
  3015 #ifdef PNG_READ_INTERLACING_SUPPORTED
  2942    if (png_ptr->interlaced)
  3016    if (png_ptr->interlaced)
  2943    {
  3017    {
  2944       png_ptr->row_number = 0;
  3018       png_ptr->row_number = 0;
  2945       png_memset_check(png_ptr, png_ptr->prev_row, 0,
  3019       png_memset(png_ptr->prev_row, 0,
  2946          png_ptr->rowbytes + 1);
  3020          png_ptr->rowbytes + 1);
  2947       do
  3021       do
  2948       {
  3022       {
  2949          png_ptr->pass++;
  3023          png_ptr->pass++;
  2950          if (png_ptr->pass >= 7)
  3024          if (png_ptr->pass >= 7)
  2975    }
  3049    }
  2976 #endif /* PNG_READ_INTERLACING_SUPPORTED */
  3050 #endif /* PNG_READ_INTERLACING_SUPPORTED */
  2977 
  3051 
  2978    if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
  3052    if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
  2979    {
  3053    {
  2980 #ifdef PNG_USE_LOCAL_ARRAYS
  3054       PNG_IDAT;
  2981       PNG_CONST PNG_IDAT;
       
  2982 #endif
       
  2983       char extra;
  3055       char extra;
  2984       int ret;
  3056       int ret;
  2985 
  3057 
  2986       png_ptr->zstream.next_out = (Byte *)&extra;
  3058       png_ptr->zstream.next_out = (Byte *)&extra;
  2987       png_ptr->zstream.avail_out = (uInt)1;
  3059       png_ptr->zstream.avail_out = (uInt)1;
  3024             png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
  3096             png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
  3025                       "Decompression Error");
  3097                       "Decompression Error");
  3026 
  3098 
  3027          if (!(png_ptr->zstream.avail_out))
  3099          if (!(png_ptr->zstream.avail_out))
  3028          {
  3100          {
  3029             png_warning(png_ptr, "Extra compressed data.");
  3101             png_warning(png_ptr, "Extra compressed data");
  3030             png_ptr->mode |= PNG_AFTER_IDAT;
  3102             png_ptr->mode |= PNG_AFTER_IDAT;
  3031             png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
  3103             png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
  3032             break;
  3104             break;
  3033          }
  3105          }
  3034 
  3106 
  3041 
  3113 
  3042    inflateReset(&png_ptr->zstream);
  3114    inflateReset(&png_ptr->zstream);
  3043 
  3115 
  3044    png_ptr->mode |= PNG_AFTER_IDAT;
  3116    png_ptr->mode |= PNG_AFTER_IDAT;
  3045 }
  3117 }
  3046 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
  3118 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
  3047 
  3119 
  3048 void /* PRIVATE */
  3120 void /* PRIVATE */
  3049 png_read_start_row(png_structp png_ptr)
  3121 png_read_start_row(png_structp png_ptr)
  3050 {
  3122 {
  3051 #ifdef PNG_USE_LOCAL_ARRAYS
       
  3052 #ifdef PNG_READ_INTERLACING_SUPPORTED
  3123 #ifdef PNG_READ_INTERLACING_SUPPORTED
  3053    /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  3124    /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  3054 
  3125 
  3055    /* Start of interlace block */
  3126    /* Start of interlace block */
  3056    PNG_CONST int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
  3127    PNG_CONST int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
  3061    /* Start of interlace block in the y direction */
  3132    /* Start of interlace block in the y direction */
  3062    PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
  3133    PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
  3063 
  3134 
  3064    /* Offset to next interlace block in the y direction */
  3135    /* Offset to next interlace block in the y direction */
  3065    PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
  3136    PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
  3066 #endif
       
  3067 #endif
  3137 #endif
  3068 
  3138 
  3069    int max_pixel_depth;
  3139    int max_pixel_depth;
  3070    png_size_t row_bytes;
  3140    png_size_t row_bytes;
  3071 
  3141 
  3096       png_ptr->iwidth = png_ptr->width;
  3166       png_ptr->iwidth = png_ptr->width;
  3097       png_ptr->irowbytes = png_ptr->rowbytes + 1;
  3167       png_ptr->irowbytes = png_ptr->rowbytes + 1;
  3098    }
  3168    }
  3099    max_pixel_depth = png_ptr->pixel_depth;
  3169    max_pixel_depth = png_ptr->pixel_depth;
  3100 
  3170 
  3101 #if defined(PNG_READ_PACK_SUPPORTED)
  3171 #ifdef PNG_READ_PACK_SUPPORTED
  3102    if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8)
  3172    if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8)
  3103       max_pixel_depth = 8;
  3173       max_pixel_depth = 8;
  3104 #endif
  3174 #endif
  3105 
  3175 
  3106 #if defined(PNG_READ_EXPAND_SUPPORTED)
  3176 #ifdef PNG_READ_EXPAND_SUPPORTED
  3107    if (png_ptr->transformations & PNG_EXPAND)
  3177    if (png_ptr->transformations & PNG_EXPAND)
  3108    {
  3178    {
  3109       if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  3179       if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  3110       {
  3180       {
  3111          if (png_ptr->num_trans)
  3181          if (png_ptr->num_trans)
  3129          }
  3199          }
  3130       }
  3200       }
  3131    }
  3201    }
  3132 #endif
  3202 #endif
  3133 
  3203 
  3134 #if defined(PNG_READ_FILLER_SUPPORTED)
  3204 #ifdef PNG_READ_FILLER_SUPPORTED
  3135    if (png_ptr->transformations & (PNG_FILLER))
  3205    if (png_ptr->transformations & (PNG_FILLER))
  3136    {
  3206    {
  3137       if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  3207       if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  3138          max_pixel_depth = 32;
  3208          max_pixel_depth = 32;
  3139       else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
  3209       else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
  3151             max_pixel_depth = 64;
  3221             max_pixel_depth = 64;
  3152       }
  3222       }
  3153    }
  3223    }
  3154 #endif
  3224 #endif
  3155 
  3225 
  3156 #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
  3226 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
  3157    if (png_ptr->transformations & PNG_GRAY_TO_RGB)
  3227    if (png_ptr->transformations & PNG_GRAY_TO_RGB)
  3158    {
  3228    {
  3159       if (
  3229       if (
  3160 #if defined(PNG_READ_EXPAND_SUPPORTED)
  3230 #ifdef PNG_READ_EXPAND_SUPPORTED
  3161         (png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) ||
  3231         (png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) ||
  3162 #endif
  3232 #endif
  3163 #if defined(PNG_READ_FILLER_SUPPORTED)
  3233 #ifdef PNG_READ_FILLER_SUPPORTED
  3164         (png_ptr->transformations & (PNG_FILLER)) ||
  3234         (png_ptr->transformations & (PNG_FILLER)) ||
  3165 #endif
  3235 #endif
  3166         png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  3236         png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  3167       {
  3237       {
  3168          if (max_pixel_depth <= 16)
  3238          if (max_pixel_depth <= 16)
  3210 #ifdef PNG_MAX_MALLOC_64K
  3280 #ifdef PNG_MAX_MALLOC_64K
  3211    if (row_bytes > (png_uint_32)65536L)
  3281    if (row_bytes > (png_uint_32)65536L)
  3212       png_error(png_ptr, "This image requires a row greater than 64KB");
  3282       png_error(png_ptr, "This image requires a row greater than 64KB");
  3213 #endif
  3283 #endif
  3214 
  3284 
  3215    if (row_bytes + 64 > png_ptr->old_big_row_buf_size)
  3285    if (row_bytes + 48 > png_ptr->old_big_row_buf_size)
  3216    {
  3286    {
  3217      png_free(png_ptr, png_ptr->big_row_buf);
  3287      png_free(png_ptr, png_ptr->big_row_buf);
  3218      png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes + 64);
       
  3219      if (png_ptr->interlaced)
  3288      if (png_ptr->interlaced)
  3220        png_memset(png_ptr->big_row_buf, 0, row_bytes + 64);
  3289         png_ptr->big_row_buf = (png_bytep)png_calloc(png_ptr,
       
  3290             row_bytes + 48);
       
  3291      else
       
  3292         png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr,
       
  3293             row_bytes + 48);
       
  3294      png_ptr->old_big_row_buf_size = row_bytes + 48;
       
  3295 
       
  3296 #ifdef PNG_ALIGNED_MEMORY_SUPPORTED
       
  3297      /* Use 16-byte aligned memory for row_buf with at least 16 bytes
       
  3298       * of padding before and after row_buf.
       
  3299       */
       
  3300      png_ptr->row_buf = png_ptr->big_row_buf + 32
       
  3301          - (((png_alloc_size_t)&(png_ptr->big_row_buf[0]) + 15) % 16);
       
  3302      png_ptr->old_big_row_buf_size = row_bytes + 48;
       
  3303 #else
       
  3304      /* Use 32 bytes of padding before and 16 bytes after row_buf. */
  3221      png_ptr->row_buf = png_ptr->big_row_buf + 32;
  3305      png_ptr->row_buf = png_ptr->big_row_buf + 32;
  3222      png_ptr->old_big_row_buf_size = row_bytes + 64;
  3306 #endif
       
  3307      png_ptr->old_big_row_buf_size = row_bytes + 48;
  3223    }
  3308    }
  3224 
  3309 
  3225 #ifdef PNG_MAX_MALLOC_64K
  3310 #ifdef PNG_MAX_MALLOC_64K
  3226    if ((png_uint_32)row_bytes + 1 > (png_uint_32)65536L)
  3311    if ((png_uint_32)png_ptr->rowbytes + 1 > (png_uint_32)65536L)
  3227       png_error(png_ptr, "This image requires a row greater than 64KB");
  3312       png_error(png_ptr, "This image requires a row greater than 64KB");
  3228 #endif
  3313 #endif
  3229    if ((png_uint_32)row_bytes > (png_uint_32)(PNG_SIZE_MAX - 1))
  3314    if ((png_uint_32)png_ptr->rowbytes > (png_uint_32)(PNG_SIZE_MAX - 1))
  3230       png_error(png_ptr, "Row has too many bytes to allocate in memory.");
  3315       png_error(png_ptr, "Row has too many bytes to allocate in memory");
  3231 
  3316 
  3232    if (row_bytes + 1 > png_ptr->old_prev_row_size)
  3317    if (png_ptr->rowbytes + 1 > png_ptr->old_prev_row_size)
  3233    {
  3318    {
  3234       png_free(png_ptr, png_ptr->prev_row);
  3319       png_free(png_ptr, png_ptr->prev_row);
  3235       png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)(
  3320       png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)(
  3236         row_bytes + 1));
  3321         png_ptr->rowbytes + 1));
  3237       png_memset_check(png_ptr, png_ptr->prev_row, 0, row_bytes + 1);
  3322       png_ptr->old_prev_row_size = png_ptr->rowbytes + 1;
  3238       png_ptr->old_prev_row_size = row_bytes + 1;
  3323    }
  3239    }
  3324 
  3240 
  3325    png_memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
  3241    png_ptr->rowbytes = row_bytes;
       
  3242 
  3326 
  3243    png_debug1(3, "width = %lu,", png_ptr->width);
  3327    png_debug1(3, "width = %lu,", png_ptr->width);
  3244    png_debug1(3, "height = %lu,", png_ptr->height);
  3328    png_debug1(3, "height = %lu,", png_ptr->height);
  3245    png_debug1(3, "iwidth = %lu,", png_ptr->iwidth);
  3329    png_debug1(3, "iwidth = %lu,", png_ptr->iwidth);
  3246    png_debug1(3, "num_rows = %lu,", png_ptr->num_rows);
  3330    png_debug1(3, "num_rows = %lu,", png_ptr->num_rows);