Ignore:
Timestamp:
Feb 11, 2010, 11:19:06 PM (16 years ago)
Author:
Dmitry A. Kuminov
Message:

trunk: Merged in qt 4.6.1 sources.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/3rdparty/libpng/pngrutil.c

    r2 r561  
    22/* pngrutil.c - utilities to read a PNG file
    33 *
    4  * Last changed in libpng 1.2.27 [April 29, 2008]
    5  * For conditions of distribution and use, see copyright notice in png.h
    6  * Copyright (c) 1998-2008 Glenn Randers-Pehrson
     4 * Last changed in libpng 1.2.38 [July 16, 2009]
     5 * Copyright (c) 1998-2009 Glenn Randers-Pehrson
    76 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
    87 * (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
    912 *
    1013 * This file contains routines that are only called from within
     
    1417#define PNG_INTERNAL
    1518#include "png.h"
    16 
    1719#if defined(PNG_READ_SUPPORTED)
    1820
     
    2325#ifdef PNG_FLOATING_POINT_SUPPORTED
    2426#  if defined(WIN32_WCE_OLD)
    25 /* strtod() function is not supported on WindowsCE */
     27/* The strtod() function is not supported on WindowsCE */
    2628__inline double png_strtod(png_structp png_ptr, PNG_CONST char *nptr, char **endptr)
    2729{
     
    3133
    3234   len = MultiByteToWideChar(CP_ACP, 0, nptr, -1, NULL, 0);
    33    str = (wchar_t *)png_malloc(png_ptr, len * sizeof(wchar_t));
     35   str = (wchar_t *)png_malloc(png_ptr, len * png_sizeof(wchar_t));
    3436   if ( NULL != str )
    3537   {
     
    5052png_get_uint_31(png_structp png_ptr, png_bytep buf)
    5153{
     54#ifdef PNG_READ_BIG_ENDIAN_SUPPORTED
    5255   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
    5363   if (i > PNG_UINT_31_MAX)
    5464     png_error(png_ptr, "PNG unsigned integer out of range.");
     
    7080/* Grab a signed 32-bit integer from a buffer in big-endian format.  The
    7181 * data is stored in the PNG file in two's complement format, and it is
    72  * assumed that the machine format for signed integers is the same. */
     82 * assumed that the machine format for signed integers is the same.
     83 */
    7384png_int_32 PNGAPI
    7485png_get_int_32(png_bytep buf)
     
    93104#endif /* PNG_READ_BIG_ENDIAN_SUPPORTED */
    94105
     106/* Read the chunk header (length + type name).
     107 * Put the type name into png_ptr->chunk_name, and return the length.
     108 */
     109png_uint_32 /* PRIVATE */
     110png_read_chunk_header(png_structp png_ptr)
     111{
     112   png_byte buf[8];
     113   png_uint_32 length;
     114
     115   /* Read the length and the chunk name */
     116   png_read_data(png_ptr, buf, 8);
     117   length = png_get_uint_31(png_ptr, buf);
     118
     119   /* Put the chunk name into png_ptr->chunk_name */
     120   png_memcpy(png_ptr->chunk_name, buf + 4, 4);
     121
     122   png_debug2(0, "Reading %s chunk, length = %lu",
     123      png_ptr->chunk_name, length);
     124
     125   /* Reset the crc and run it over the chunk name */
     126   png_reset_crc(png_ptr);
     127   png_calculate_crc(png_ptr, png_ptr->chunk_name, 4);
     128
     129   /* Check to see if chunk name is valid */
     130   png_check_chunk_name(png_ptr, png_ptr->chunk_name);
     131
     132   return length;
     133}
     134
    95135/* Read data, and (optionally) run it through the CRC. */
    96136void /* PRIVATE */
    97137png_crc_read(png_structp png_ptr, png_bytep buf, png_size_t length)
    98138{
    99    if(png_ptr == NULL) return;
     139   if (png_ptr == NULL)
     140      return;
    100141   png_read_data(png_ptr, buf, length);
    101142   png_calculate_crc(png_ptr, buf, length);
     
    103144
    104145/* Optionally skip data and then check the CRC.  Depending on whether we
    105    are reading a ancillary or critical chunk, and how the program has set
    106    things up, we may calculate the CRC on the data and print a message.
    107    Returns '1' if there was a CRC error, '0' otherwise. */
     146 * are reading a ancillary or critical chunk, and how the program has set
     147 * things up, we may calculate the CRC on the data and print a message.
     148 * Returns '1' if there was a CRC error, '0' otherwise.
     149 */
    108150int /* PRIVATE */
    109151png_crc_finish(png_structp png_ptr, png_uint_32 skip)
     
    124166   {
    125167      if (((png_ptr->chunk_name[0] & 0x20) &&                /* Ancillary */
    126            !(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)) ||
     168          !(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)) ||
    127169          (!(png_ptr->chunk_name[0] & 0x20) &&             /* Critical  */
    128170          (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE)))
     
    141183
    142184/* Compare the CRC stored in the PNG file with that calculated by libpng from
    143    the data it has read thus far. */
     185 * the data it has read thus far.
     186 */
    144187int /* PRIVATE */
    145188png_crc_error(png_structp png_ptr)
     
    181224 * trailing part (the malloc area passed in is freed).
    182225 */
    183 png_charp /* PRIVATE */
     226void /* PRIVATE */
    184227png_decompress_chunk(png_structp png_ptr, int comp_type,
    185                               png_charp chunkdata, png_size_t chunklength,
     228                              png_size_t chunklength,
    186229                              png_size_t prefix_size, png_size_t *newlength)
    187230{
     
    193236   {
    194237      int ret = Z_OK;
    195       png_ptr->zstream.next_in = (png_bytep)(chunkdata + prefix_size);
     238      png_ptr->zstream.next_in = (png_bytep)(png_ptr->chunkdata + prefix_size);
    196239      png_ptr->zstream.avail_in = (uInt)(chunklength - prefix_size);
    197240      png_ptr->zstream.next_out = png_ptr->zbuf;
     
    219262               if (text ==  NULL)
    220263                 {
    221                     png_free(png_ptr,chunkdata);
    222                     png_error(png_ptr,"Not enough memory to decompress chunk");
     264                    png_free(png_ptr, png_ptr->chunkdata);
     265                    png_ptr->chunkdata = NULL;
     266                    png_error(png_ptr, "Not enough memory to decompress chunk");
    223267                 }
    224                png_memcpy(text, chunkdata, prefix_size);
     268               png_memcpy(text, png_ptr->chunkdata, prefix_size);
    225269            }
    226270
     
    228272
    229273            /* Copy what we can of the error message into the text chunk */
    230             text_size = (png_size_t)(chunklength - (text - chunkdata) - 1);
    231             text_size = png_sizeof(msg) > text_size ? text_size :
    232                png_sizeof(msg);
     274            text_size = (png_size_t)(chunklength -
     275              (text - png_ptr->chunkdata) - 1);
     276            if (text_size > png_sizeof(msg))
     277               text_size = png_sizeof(msg);
    233278            png_memcpy(text + prefix_size, msg, text_size);
    234279            break;
     
    242287               text = (png_charp)png_malloc_warn(png_ptr, text_size + 1);
    243288               if (text ==  NULL)
    244                  {
    245                     png_free(png_ptr,chunkdata);
    246                     png_error(png_ptr,"Not enough memory to decompress chunk.");
    247                  }
     289               {
     290                  png_free(png_ptr, png_ptr->chunkdata);
     291                  png_ptr->chunkdata = NULL;
     292                  png_error(png_ptr,
     293                    "Not enough memory to decompress chunk.");
     294               }
    248295               png_memcpy(text + prefix_size, png_ptr->zbuf,
    249296                    text_size - prefix_size);
    250                png_memcpy(text, chunkdata, prefix_size);
     297               png_memcpy(text, png_ptr->chunkdata, prefix_size);
    251298               *(text + text_size) = 0x00;
    252299            }
     
    262309               {
    263310                  png_free(png_ptr, tmp);
    264                   png_free(png_ptr, chunkdata);
    265                   png_error(png_ptr,"Not enough memory to decompress chunk..");
     311                  png_free(png_ptr, png_ptr->chunkdata);
     312                  png_ptr->chunkdata = NULL;
     313                  png_error(png_ptr,
     314                    "Not enough memory to decompress chunk..");
    266315               }
    267316               png_memcpy(text, tmp, text_size);
     
    290339                "Buffer error in compressed datastream in %s chunk",
    291340                png_ptr->chunk_name);
     341
    292342         else if (ret == Z_DATA_ERROR)
    293343            png_snprintf(umsg, 52,
    294344                "Data error in compressed datastream in %s chunk",
    295345                png_ptr->chunk_name);
     346
    296347         else
    297348            png_snprintf(umsg, 52,
    298349                "Incomplete compressed datastream in %s chunk",
    299350                png_ptr->chunk_name);
     351
    300352         png_warning(png_ptr, umsg);
    301353#else
     
    303355            "Incomplete compressed datastream in chunk other than IDAT");
    304356#endif
    305          text_size=prefix_size;
     357         text_size = prefix_size;
    306358         if (text ==  NULL)
    307359         {
     
    309361            if (text == NULL)
    310362              {
    311                 png_free(png_ptr, chunkdata);
    312                 png_error(png_ptr,"Not enough memory for text.");
     363                png_free(png_ptr, png_ptr->chunkdata);
     364                png_ptr->chunkdata = NULL;
     365                png_error(png_ptr, "Not enough memory for text.");
    313366              }
    314             png_memcpy(text, chunkdata, prefix_size);
     367            png_memcpy(text, png_ptr->chunkdata, prefix_size);
    315368         }
    316369         *(text + text_size) = 0x00;
     
    320373      png_ptr->zstream.avail_in = 0;
    321374
    322       png_free(png_ptr, chunkdata);
    323       chunkdata = text;
     375      png_free(png_ptr, png_ptr->chunkdata);
     376      png_ptr->chunkdata = text;
    324377      *newlength=text_size;
    325378   }
     
    329382      char umsg[50];
    330383
    331       png_snprintf(umsg, 50,
    332          "Unknown zTXt compression type %d", comp_type);
     384      png_snprintf(umsg, 50, "Unknown zTXt compression type %d", comp_type);
    333385      png_warning(png_ptr, umsg);
    334386#else
     
    336388#endif
    337389
    338       *(chunkdata + prefix_size) = 0x00;
    339       *newlength=prefix_size;
    340    }
    341 
    342    return chunkdata;
    343 }
    344 #endif
    345 
    346 /* read and check the IDHR chunk */
     390      *(png_ptr->chunkdata + prefix_size) = 0x00;
     391      *newlength = prefix_size;
     392   }
     393}
     394#endif
     395
     396/* Read and check the IDHR chunk */
    347397void /* PRIVATE */
    348398png_handle_IHDR(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
     
    353403   int interlace_type;
    354404
    355    png_debug(1, "in png_handle_IHDR\n");
     405   png_debug(1, "in png_handle_IHDR");
    356406
    357407   if (png_ptr->mode & PNG_HAVE_IHDR)
    358408      png_error(png_ptr, "Out of place IHDR");
    359409
    360    /* check the length */
     410   /* Check the length */
    361411   if (length != 13)
    362412      png_error(png_ptr, "Invalid IHDR chunk");
     
    375425   interlace_type = buf[12];
    376426
    377    /* set internal variables */
     427   /* Set internal variables */
    378428   png_ptr->width = width;
    379429   png_ptr->height = height;
     
    386436   png_ptr->compression_type = (png_byte)compression_type;
    387437
    388    /* find number of channels */
     438   /* Find number of channels */
    389439   switch (png_ptr->color_type)
    390440   {
     
    393443         png_ptr->channels = 1;
    394444         break;
     445
    395446      case PNG_COLOR_TYPE_RGB:
    396447         png_ptr->channels = 3;
    397448         break;
     449
    398450      case PNG_COLOR_TYPE_GRAY_ALPHA:
    399451         png_ptr->channels = 2;
    400452         break;
     453
    401454      case PNG_COLOR_TYPE_RGB_ALPHA:
    402455         png_ptr->channels = 4;
     
    404457   }
    405458
    406    /* set up other useful info */
     459   /* Set up other useful info */
    407460   png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth *
    408461   png_ptr->channels);
    409    png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth,png_ptr->width);
    410    png_debug1(3,"bit_depth = %d\n", png_ptr->bit_depth);
    411    png_debug1(3,"channels = %d\n", png_ptr->channels);
    412    png_debug1(3,"rowbytes = %lu\n", png_ptr->rowbytes);
     462   png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width);
     463   png_debug1(3, "bit_depth = %d", png_ptr->bit_depth);
     464   png_debug1(3, "channels = %d", png_ptr->channels);
     465   png_debug1(3, "rowbytes = %lu", png_ptr->rowbytes);
    413466   png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
    414467      color_type, interlace_type, compression_type, filter_type);
    415468}
    416469
    417 /* read and check the palette */
     470/* Read and check the palette */
    418471void /* PRIVATE */
    419472png_handle_PLTE(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
     
    425478#endif
    426479
    427    png_debug(1, "in png_handle_PLTE\n");
     480   png_debug(1, "in png_handle_PLTE");
    428481
    429482   if (!(png_ptr->mode & PNG_HAVE_IHDR))
    430483      png_error(png_ptr, "Missing IHDR before PLTE");
     484
    431485   else if (png_ptr->mode & PNG_HAVE_IDAT)
    432486   {
     
    435489      return;
    436490   }
     491
    437492   else if (png_ptr->mode & PNG_HAVE_PLTE)
    438493      png_error(png_ptr, "Duplicate PLTE chunk");
     
    463518         return;
    464519      }
     520
    465521      else
    466522      {
     
    487543
    488544      png_crc_read(png_ptr, buf, 3);
    489       /* don't depend upon png_color being any order */
     545      /* Don't depend upon png_color being any order */
    490546      palette[i].red = buf[0];
    491547      palette[i].green = buf[1];
     
    495551
    496552   /* If we actually NEED the PLTE chunk (ie for a paletted image), we do
    497       whatever the normal CRC configuration tells us.  However, if we
    498       have an RGB image, the PLTE can be considered ancillary, so
    499       we will act as though it is. */
     553    * whatever the normal CRC configuration tells us.  However, if we
     554    * have an RGB image, the PLTE can be considered ancillary, so
     555    * we will act as though it is.
     556    */
    500557#if !defined(PNG_READ_OPT_PLTE_SUPPORTED)
    501558   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
     
    557614png_handle_IEND(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
    558615{
    559    png_debug(1, "in png_handle_IEND\n");
     616   png_debug(1, "in png_handle_IEND");
    560617
    561618   if (!(png_ptr->mode & PNG_HAVE_IHDR) || !(png_ptr->mode & PNG_HAVE_IDAT))
     
    572629   png_crc_finish(png_ptr, length);
    573630
    574    info_ptr =info_ptr; /* quiet compiler warnings about unused info_ptr */
     631   info_ptr = info_ptr; /* Quiet compiler warnings about unused info_ptr */
    575632}
    576633
     
    585642   png_byte buf[4];
    586643
    587    png_debug(1, "in png_handle_gAMA\n");
     644   png_debug(1, "in png_handle_gAMA");
    588645
    589646   if (!(png_ptr->mode & PNG_HAVE_IHDR))
     
    622679
    623680   igamma = (png_fixed_point)png_get_uint_32(buf);
    624    /* check for zero gamma */
     681   /* Check for zero gamma */
    625682   if (igamma == 0)
    626683      {
     
    637694           "Ignoring incorrect gAMA value when sRGB is also present");
    638695#ifndef PNG_NO_CONSOLE_IO
    639          fprintf(stderr, "gamma = (%d/100000)\n", (int)igamma);
     696         fprintf(stderr, "gamma = (%d/100000)", (int)igamma);
    640697#endif
    641698         return;
     
    663720   png_byte buf[4];
    664721
    665    png_debug(1, "in png_handle_sBIT\n");
     722   png_debug(1, "in png_handle_sBIT");
    666723
    667724   buf[0] = buf[1] = buf[2] = buf[3] = 0;
     
    726783png_handle_cHRM(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
    727784{
    728    png_byte buf[4];
     785   png_byte buf[32];
    729786#ifdef PNG_FLOATING_POINT_SUPPORTED
    730787   float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y;
     
    735792   png_uint_32 uint_x, uint_y;
    736793
    737    png_debug(1, "in png_handle_cHRM\n");
     794   png_debug(1, "in png_handle_cHRM");
    738795
    739796   if (!(png_ptr->mode & PNG_HAVE_IHDR))
     
    767824   }
    768825
    769    png_crc_read(png_ptr, buf, 4);
     826   png_crc_read(png_ptr, buf, 32);
     827   if (png_crc_finish(png_ptr, 0))
     828      return;
     829
    770830   uint_x = png_get_uint_32(buf);
    771 
    772    png_crc_read(png_ptr, buf, 4);
    773    uint_y = png_get_uint_32(buf);
    774 
    775    if (uint_x > 80000L || uint_y > 80000L ||
    776       uint_x + uint_y > 100000L)
    777    {
    778       png_warning(png_ptr, "Invalid cHRM white point");
    779       png_crc_finish(png_ptr, 24);
    780       return;
    781    }
     831   uint_y = png_get_uint_32(buf + 4);
    782832   int_x_white = (png_fixed_point)uint_x;
    783833   int_y_white = (png_fixed_point)uint_y;
    784834
    785    png_crc_read(png_ptr, buf, 4);
    786    uint_x = png_get_uint_32(buf);
    787 
    788    png_crc_read(png_ptr, buf, 4);
    789    uint_y = png_get_uint_32(buf);
    790 
    791    if (uint_x + uint_y > 100000L)
    792    {
    793       png_warning(png_ptr, "Invalid cHRM red point");
    794       png_crc_finish(png_ptr, 16);
    795       return;
    796    }
     835   uint_x = png_get_uint_32(buf + 8);
     836   uint_y = png_get_uint_32(buf + 12);
    797837   int_x_red = (png_fixed_point)uint_x;
    798838   int_y_red = (png_fixed_point)uint_y;
    799839
    800    png_crc_read(png_ptr, buf, 4);
    801    uint_x = png_get_uint_32(buf);
    802 
    803    png_crc_read(png_ptr, buf, 4);
    804    uint_y = png_get_uint_32(buf);
    805 
    806    if (uint_x + uint_y > 100000L)
    807    {
    808       png_warning(png_ptr, "Invalid cHRM green point");
    809       png_crc_finish(png_ptr, 8);
    810       return;
    811    }
     840   uint_x = png_get_uint_32(buf + 16);
     841   uint_y = png_get_uint_32(buf + 20);
    812842   int_x_green = (png_fixed_point)uint_x;
    813843   int_y_green = (png_fixed_point)uint_y;
    814844
    815    png_crc_read(png_ptr, buf, 4);
    816    uint_x = png_get_uint_32(buf);
    817 
    818    png_crc_read(png_ptr, buf, 4);
    819    uint_y = png_get_uint_32(buf);
    820 
    821    if (uint_x + uint_y > 100000L)
    822    {
    823       png_warning(png_ptr, "Invalid cHRM blue point");
    824       png_crc_finish(png_ptr, 0);
    825       return;
    826    }
     845   uint_x = png_get_uint_32(buf + 24);
     846   uint_y = png_get_uint_32(buf + 28);
    827847   int_x_blue = (png_fixed_point)uint_x;
    828848   int_y_blue = (png_fixed_point)uint_y;
     
    855875#ifndef PNG_NO_CONSOLE_IO
    856876#ifdef PNG_FLOATING_POINT_SUPPORTED
    857             fprintf(stderr,"wx=%f, wy=%f, rx=%f, ry=%f\n",
     877            fprintf(stderr, "wx=%f, wy=%f, rx=%f, ry=%f\n",
    858878               white_x, white_y, red_x, red_y);
    859             fprintf(stderr,"gx=%f, gy=%f, bx=%f, by=%f\n",
     879            fprintf(stderr, "gx=%f, gy=%f, bx=%f, by=%f\n",
    860880               green_x, green_y, blue_x, blue_y);
    861881#else
    862             fprintf(stderr,"wx=%ld, wy=%ld, rx=%ld, ry=%ld\n",
     882            fprintf(stderr, "wx=%ld, wy=%ld, rx=%ld, ry=%ld\n",
    863883               int_x_white, int_y_white, int_x_red, int_y_red);
    864             fprintf(stderr,"gx=%ld, gy=%ld, bx=%ld, by=%ld\n",
     884            fprintf(stderr, "gx=%ld, gy=%ld, bx=%ld, by=%ld\n",
    865885               int_x_green, int_y_green, int_x_blue, int_y_blue);
    866886#endif
    867887#endif /* PNG_NO_CONSOLE_IO */
    868888         }
    869          png_crc_finish(png_ptr, 0);
    870889         return;
    871890      }
     
    881900      int_y_green, int_x_blue, int_y_blue);
    882901#endif
    883    if (png_crc_finish(png_ptr, 0))
    884       return;
    885902}
    886903#endif
     
    893910   png_byte buf[1];
    894911
    895    png_debug(1, "in png_handle_sRGB\n");
     912   png_debug(1, "in png_handle_sRGB");
    896913
    897914   if (!(png_ptr->mode & PNG_HAVE_IHDR))
     
    926943
    927944   intent = buf[0];
    928    /* check for bad intent */
     945   /* Check for bad intent */
    929946   if (intent >= PNG_sRGB_INTENT_LAST)
    930947   {
     
    950967#ifndef PNG_NO_CONSOLE_IO
    951968#  ifdef PNG_FIXED_POINT_SUPPORTED
    952          fprintf(stderr,"incorrect gamma=(%d/100000)\n",(int)png_ptr->int_gamma);
     969         fprintf(stderr, "incorrect gamma=(%d/100000)\n",
     970            (int)png_ptr->int_gamma);
    953971#  else
    954972#    ifdef PNG_FLOATING_POINT_SUPPORTED
    955          fprintf(stderr,"incorrect gamma=%f\n",png_ptr->gamma);
     973         fprintf(stderr, "incorrect gamma=%f\n", png_ptr->gamma);
    956974#    endif
    957975#  endif
     
    9881006/* Note: this does not properly handle chunks that are > 64K under DOS */
    9891007{
    990    png_charp chunkdata;
    9911008   png_byte compression_type;
    9921009   png_bytep pC;
     
    9961013   png_size_t slength, prefix_length, data_length;
    9971014
    998    png_debug(1, "in png_handle_iCCP\n");
     1015   png_debug(1, "in png_handle_iCCP");
    9991016
    10001017   if (!(png_ptr->mode & PNG_HAVE_IHDR))
     
    10261043#endif
    10271044
    1028    chunkdata = (png_charp)png_malloc(png_ptr, length + 1);
     1045   png_free(png_ptr, png_ptr->chunkdata);
     1046   png_ptr->chunkdata = (png_charp)png_malloc(png_ptr, length + 1);
    10291047   slength = (png_size_t)length;
    1030    png_crc_read(png_ptr, (png_bytep)chunkdata, slength);
     1048   png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
    10311049
    10321050   if (png_crc_finish(png_ptr, skip))
    10331051   {
    1034       png_free(png_ptr, chunkdata);
    1035       return;
    1036    }
    1037 
    1038    chunkdata[slength] = 0x00;
    1039 
    1040    for (profile = chunkdata; *profile; profile++)
    1041       /* empty loop to find end of name */ ;
     1052      png_free(png_ptr, png_ptr->chunkdata);
     1053      png_ptr->chunkdata = NULL;
     1054      return;
     1055   }
     1056
     1057   png_ptr->chunkdata[slength] = 0x00;
     1058
     1059   for (profile = png_ptr->chunkdata; *profile; profile++)
     1060      /* Empty loop to find end of name */ ;
    10421061
    10431062   ++profile;
    10441063
    1045    /* there should be at least one zero (the compression type byte)
    1046       following the separator, and we should be on it  */
    1047    if ( profile >= chunkdata + slength - 1)
    1048    {
    1049       png_free(png_ptr, chunkdata);
     1064   /* There should be at least one zero (the compression type byte)
     1065    * following the separator, and we should be on it
     1066    */
     1067   if ( profile >= png_ptr->chunkdata + slength - 1)
     1068   {
     1069      png_free(png_ptr, png_ptr->chunkdata);
     1070      png_ptr->chunkdata = NULL;
    10501071      png_warning(png_ptr, "Malformed iCCP chunk");
    10511072      return;
    10521073   }
    10531074
    1054    /* compression_type should always be zero */
     1075   /* Compression_type should always be zero */
    10551076   compression_type = *profile++;
    10561077   if (compression_type)
    10571078   {
    10581079      png_warning(png_ptr, "Ignoring nonzero compression type in iCCP chunk");
    1059       compression_type=0x00;  /* Reset it to zero (libpng-1.0.6 through 1.0.8
     1080      compression_type = 0x00;  /* Reset it to zero (libpng-1.0.6 through 1.0.8
    10601081                                 wrote nonzero) */
    10611082   }
    10621083
    1063    prefix_length = profile - chunkdata;
    1064    chunkdata = png_decompress_chunk(png_ptr, compression_type, chunkdata,
    1065                                     slength, prefix_length, &data_length);
     1084   prefix_length = profile - png_ptr->chunkdata;
     1085   png_decompress_chunk(png_ptr, compression_type,
     1086     slength, prefix_length, &data_length);
    10661087
    10671088   profile_length = data_length - prefix_length;
     
    10691090   if ( prefix_length > data_length || profile_length < 4)
    10701091   {
    1071       png_free(png_ptr, chunkdata);
     1092      png_free(png_ptr, png_ptr->chunkdata);
     1093      png_ptr->chunkdata = NULL;
    10721094      png_warning(png_ptr, "Profile size field missing from iCCP chunk");
    10731095      return;
     
    10751097
    10761098   /* Check the profile_size recorded in the first 32 bits of the ICC profile */
    1077    pC = (png_bytep)(chunkdata+prefix_length);
    1078    profile_size = ((*(pC  ))<<24) |
    1079                   ((*(pC+1))<<16) |
    1080                   ((*(pC+2))<< 8) |
    1081                   ((*(pC+3))    );
    1082 
    1083    if(profile_size < profile_length)
     1099   pC = (png_bytep)(png_ptr->chunkdata + prefix_length);
     1100   profile_size = ((*(pC    ))<<24) |
     1101                  ((*(pC + 1))<<16) |
     1102                  ((*(pC + 2))<< 8) |
     1103                  ((*(pC + 3))    );
     1104
     1105   if (profile_size < profile_length)
    10841106      profile_length = profile_size;
    10851107
    1086    if(profile_size > profile_length)
    1087    {
    1088       png_free(png_ptr, chunkdata);
     1108   if (profile_size > profile_length)
     1109   {
     1110      png_free(png_ptr, png_ptr->chunkdata);
     1111      png_ptr->chunkdata = NULL;
    10891112      png_warning(png_ptr, "Ignoring truncated iCCP profile.");
    10901113      return;
    10911114   }
    10921115
    1093    png_set_iCCP(png_ptr, info_ptr, chunkdata, compression_type,
    1094                 chunkdata + prefix_length, profile_length);
    1095    png_free(png_ptr, chunkdata);
     1116   png_set_iCCP(png_ptr, info_ptr, png_ptr->chunkdata,
     1117     compression_type, png_ptr->chunkdata + prefix_length, profile_length);
     1118   png_free(png_ptr, png_ptr->chunkdata);
     1119   png_ptr->chunkdata = NULL;
    10961120}
    10971121#endif /* PNG_READ_iCCP_SUPPORTED */
     
    11021126/* Note: this does not properly handle chunks that are > 64K under DOS */
    11031127{
    1104    png_bytep chunkdata;
    11051128   png_bytep entry_start;
    11061129   png_sPLT_t new_palette;
     
    11121135   png_size_t slength;
    11131136
    1114    png_debug(1, "in png_handle_sPLT\n");
     1137   png_debug(1, "in png_handle_sPLT");
     1138
    11151139
    11161140   if (!(png_ptr->mode & PNG_HAVE_IHDR))
     
    11321156#endif
    11331157
    1134    chunkdata = (png_bytep)png_malloc(png_ptr, length + 1);
     1158   png_free(png_ptr, png_ptr->chunkdata);
     1159   png_ptr->chunkdata = (png_charp)png_malloc(png_ptr, length + 1);
    11351160   slength = (png_size_t)length;
    1136    png_crc_read(png_ptr, (png_bytep)chunkdata, slength);
     1161   png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
    11371162
    11381163   if (png_crc_finish(png_ptr, skip))
    11391164   {
    1140       png_free(png_ptr, chunkdata);
    1141       return;
    1142    }
    1143 
    1144    chunkdata[slength] = 0x00;
    1145 
    1146    for (entry_start = chunkdata; *entry_start; entry_start++)
    1147       /* empty loop to find end of name */ ;
     1165      png_free(png_ptr, png_ptr->chunkdata);
     1166      png_ptr->chunkdata = NULL;
     1167      return;
     1168   }
     1169
     1170   png_ptr->chunkdata[slength] = 0x00;
     1171
     1172   for (entry_start = (png_bytep)png_ptr->chunkdata; *entry_start; entry_start++)
     1173      /* Empty loop to find end of name */ ;
    11481174   ++entry_start;
    11491175
    1150    /* a sample depth should follow the separator, and we should be on it  */
    1151    if (entry_start > chunkdata + slength - 2)
    1152    {
    1153       png_free(png_ptr, chunkdata);
     1176   /* A sample depth should follow the separator, and we should be on it  */
     1177   if (entry_start > (png_bytep)png_ptr->chunkdata + slength - 2)
     1178   {
     1179      png_free(png_ptr, png_ptr->chunkdata);
     1180      png_ptr->chunkdata = NULL;
    11541181      png_warning(png_ptr, "malformed sPLT chunk");
    11551182      return;
     
    11581185   new_palette.depth = *entry_start++;
    11591186   entry_size = (new_palette.depth == 8 ? 6 : 10);
    1160    data_length = (slength - (entry_start - chunkdata));
    1161 
    1162    /* integrity-check the data length */
     1187   data_length = (slength - (entry_start - (png_bytep)png_ptr->chunkdata));
     1188
     1189   /* Integrity-check the data length */
    11631190   if (data_length % entry_size)
    11641191   {
    1165       png_free(png_ptr, chunkdata);
     1192      png_free(png_ptr, png_ptr->chunkdata);
     1193      png_ptr->chunkdata = NULL;
    11661194      png_warning(png_ptr, "sPLT chunk has bad length");
    11671195      return;
     
    11691197
    11701198   new_palette.nentries = (png_int_32) ( data_length / entry_size);
    1171    if ((png_uint_32) new_palette.nentries > (png_uint_32) (PNG_SIZE_MAX /
    1172        png_sizeof(png_sPLT_entry)))
     1199   if ((png_uint_32) new_palette.nentries >
     1200       (png_uint_32) (PNG_SIZE_MAX / png_sizeof(png_sPLT_entry)))
    11731201   {
    11741202       png_warning(png_ptr, "sPLT chunk too long");
     
    12271255#endif
    12281256
    1229    /* discard all chunk data except the name and stash that */
    1230    new_palette.name = (png_charp)chunkdata;
     1257   /* Discard all chunk data except the name and stash that */
     1258   new_palette.name = png_ptr->chunkdata;
    12311259
    12321260   png_set_sPLT(png_ptr, info_ptr, &new_palette, 1);
    12331261
    1234    png_free(png_ptr, chunkdata);
     1262   png_free(png_ptr, png_ptr->chunkdata);
     1263   png_ptr->chunkdata = NULL;
    12351264   png_free(png_ptr, new_palette.entries);
    12361265}
     
    12431272   png_byte readbuf[PNG_MAX_PALETTE_LENGTH];
    12441273
    1245    png_debug(1, "in png_handle_tRNS\n");
     1274   png_debug(1, "in png_handle_tRNS");
    12461275
    12471276   if (!(png_ptr->mode & PNG_HAVE_IHDR))
     
    13391368   png_byte buf[6];
    13401369
    1341    png_debug(1, "in png_handle_bKGD\n");
     1370   png_debug(1, "in png_handle_bKGD");
    13421371
    13431372   if (!(png_ptr->mode & PNG_HAVE_IHDR))
     
    13901419      if (info_ptr && info_ptr->num_palette)
    13911420      {
    1392           if(buf[0] > info_ptr->num_palette)
     1421          if (buf[0] >= info_ptr->num_palette)
    13931422          {
    13941423             png_warning(png_ptr, "Incorrect bKGD chunk index value");
     
    14281457   png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH];
    14291458
    1430    png_debug(1, "in png_handle_hIST\n");
     1459   png_debug(1, "in png_handle_hIST");
    14311460
    14321461   if (!(png_ptr->mode & PNG_HAVE_IHDR))
     
    14831512   int unit_type;
    14841513
    1485    png_debug(1, "in png_handle_pHYs\n");
     1514   png_debug(1, "in png_handle_pHYs");
    14861515
    14871516   if (!(png_ptr->mode & PNG_HAVE_IHDR))
     
    15261555   int unit_type;
    15271556
    1528    png_debug(1, "in png_handle_oFFs\n");
     1557   png_debug(1, "in png_handle_oFFs");
    15291558
    15301559   if (!(png_ptr->mode & PNG_HAVE_IHDR))
     
    15621591
    15631592#if defined(PNG_READ_pCAL_SUPPORTED)
    1564 /* read the pCAL chunk (described in the PNG Extensions document) */
     1593/* Read the pCAL chunk (described in the PNG Extensions document) */
    15651594void /* PRIVATE */
    15661595png_handle_pCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
    15671596{
    1568    png_charp purpose;
    15691597   png_int_32 X0, X1;
    15701598   png_byte type, nparams;
     
    15741602   int i;
    15751603
    1576    png_debug(1, "in png_handle_pCAL\n");
     1604   png_debug(1, "in png_handle_pCAL");
    15771605
    15781606   if (!(png_ptr->mode & PNG_HAVE_IHDR))
     
    15911619   }
    15921620
    1593    png_debug1(2, "Allocating and reading pCAL chunk data (%lu bytes)\n",
     1621   png_debug1(2, "Allocating and reading pCAL chunk data (%lu bytes)",
    15941622      length + 1);
    1595    purpose = (png_charp)png_malloc_warn(png_ptr, length + 1);
    1596    if (purpose == NULL)
     1623   png_free(png_ptr, png_ptr->chunkdata);
     1624   png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
     1625   if (png_ptr->chunkdata == NULL)
    15971626     {
    15981627       png_warning(png_ptr, "No memory for pCAL purpose.");
     
    16001629     }
    16011630   slength = (png_size_t)length;
    1602    png_crc_read(png_ptr, (png_bytep)purpose, slength);
     1631   png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
    16031632
    16041633   if (png_crc_finish(png_ptr, 0))
    16051634   {
    1606       png_free(png_ptr, purpose);
    1607       return;
    1608    }
    1609 
    1610    purpose[slength] = 0x00; /* null terminate the last string */
    1611 
    1612    png_debug(3, "Finding end of pCAL purpose string\n");
    1613    for (buf = purpose; *buf; buf++)
    1614       /* empty loop */ ;
    1615 
    1616    endptr = purpose + slength;
     1635      png_free(png_ptr, png_ptr->chunkdata);
     1636      png_ptr->chunkdata = NULL;
     1637      return;
     1638   }
     1639
     1640   png_ptr->chunkdata[slength] = 0x00; /* Null terminate the last string */
     1641
     1642   png_debug(3, "Finding end of pCAL purpose string");
     1643   for (buf = png_ptr->chunkdata; *buf; buf++)
     1644      /* Empty loop */ ;
     1645
     1646   endptr = png_ptr->chunkdata + slength;
    16171647
    16181648   /* We need to have at least 12 bytes after the purpose string
     
    16211651   {
    16221652      png_warning(png_ptr, "Invalid pCAL data");
    1623       png_free(png_ptr, purpose);
    1624       return;
    1625    }
    1626 
    1627    png_debug(3, "Reading pCAL X0, X1, type, nparams, and units\n");
     1653      png_free(png_ptr, png_ptr->chunkdata);
     1654      png_ptr->chunkdata = NULL;
     1655      return;
     1656   }
     1657
     1658   png_debug(3, "Reading pCAL X0, X1, type, nparams, and units");
    16281659   X0 = png_get_int_32((png_bytep)buf+1);
    16291660   X1 = png_get_int_32((png_bytep)buf+5);
     
    16321663   units = buf + 11;
    16331664
    1634    png_debug(3, "Checking pCAL equation type and number of parameters\n");
     1665   png_debug(3, "Checking pCAL equation type and number of parameters");
    16351666   /* Check that we have the right number of parameters for known
    16361667      equation types. */
     
    16411672   {
    16421673      png_warning(png_ptr, "Invalid pCAL parameters for equation type");
    1643       png_free(png_ptr, purpose);
     1674      png_free(png_ptr, png_ptr->chunkdata);
     1675      png_ptr->chunkdata = NULL;
    16441676      return;
    16451677   }
     
    16521684      /* Empty loop to move past the units string. */ ;
    16531685
    1654    png_debug(3, "Allocating pCAL parameters array\n");
    1655    params = (png_charpp)png_malloc_warn(png_ptr, (png_uint_32)(nparams
    1656       *png_sizeof(png_charp))) ;
     1686   png_debug(3, "Allocating pCAL parameters array");
     1687   params = (png_charpp)png_malloc_warn(png_ptr,
     1688      (png_uint_32)(nparams * png_sizeof(png_charp))) ;
    16571689   if (params == NULL)
    16581690     {
    1659        png_free(png_ptr, purpose);
     1691       png_free(png_ptr, png_ptr->chunkdata);
     1692       png_ptr->chunkdata = NULL;
    16601693       png_warning(png_ptr, "No memory for pCAL params.");
    16611694       return;
     
    16671700      buf++; /* Skip the null string terminator from previous parameter. */
    16681701
    1669       png_debug1(3, "Reading pCAL parameter %d\n", i);
     1702      png_debug1(3, "Reading pCAL parameter %d", i);
    16701703      for (params[i] = buf; buf <= endptr && *buf != 0x00; buf++)
    16711704         /* Empty loop to move past each parameter string */ ;
     
    16751708      {
    16761709         png_warning(png_ptr, "Invalid pCAL data");
    1677          png_free(png_ptr, purpose);
     1710         png_free(png_ptr, png_ptr->chunkdata);
     1711         png_ptr->chunkdata = NULL;
    16781712         png_free(png_ptr, params);
    16791713         return;
     
    16811715   }
    16821716
    1683    png_set_pCAL(png_ptr, info_ptr, purpose, X0, X1, type, nparams,
     1717   png_set_pCAL(png_ptr, info_ptr, png_ptr->chunkdata, X0, X1, type, nparams,
    16841718      units, params);
    16851719
    1686    png_free(png_ptr, purpose);
     1720   png_free(png_ptr, png_ptr->chunkdata);
     1721   png_ptr->chunkdata = NULL;
    16871722   png_free(png_ptr, params);
    16881723}
     
    16901725
    16911726#if defined(PNG_READ_sCAL_SUPPORTED)
    1692 /* read the sCAL chunk */
     1727/* Read the sCAL chunk */
    16931728void /* PRIVATE */
    16941729png_handle_sCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
    16951730{
    1696    png_charp buffer, ep;
     1731   png_charp ep;
    16971732#ifdef PNG_FLOATING_POINT_SUPPORTED
    16981733   double width, height;
     
    17051740   png_size_t slength;
    17061741
    1707    png_debug(1, "in png_handle_sCAL\n");
     1742   png_debug(1, "in png_handle_sCAL");
    17081743
    17091744   if (!(png_ptr->mode & PNG_HAVE_IHDR))
     
    17221757   }
    17231758
    1724    png_debug1(2, "Allocating and reading sCAL chunk data (%lu bytes)\n",
     1759   png_debug1(2, "Allocating and reading sCAL chunk data (%lu bytes)",
    17251760      length + 1);
    1726    buffer = (png_charp)png_malloc_warn(png_ptr, length + 1);
    1727    if (buffer == NULL)
    1728      {
    1729        png_warning(png_ptr, "Out of memory while processing sCAL chunk");
    1730        return;
    1731      }
     1761   png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
     1762   if (png_ptr->chunkdata == NULL)
     1763   {
     1764      png_warning(png_ptr, "Out of memory while processing sCAL chunk");
     1765      return;
     1766   }
    17321767   slength = (png_size_t)length;
    1733    png_crc_read(png_ptr, (png_bytep)buffer, slength);
     1768   png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
    17341769
    17351770   if (png_crc_finish(png_ptr, 0))
    17361771   {
    1737       png_free(png_ptr, buffer);
    1738       return;
    1739    }
    1740 
    1741    buffer[slength] = 0x00; /* null terminate the last string */
    1742 
    1743    ep = buffer + 1;        /* skip unit byte */
     1772      png_free(png_ptr, png_ptr->chunkdata);
     1773      png_ptr->chunkdata = NULL;
     1774      return;
     1775   }
     1776
     1777   png_ptr->chunkdata[slength] = 0x00; /* Null terminate the last string */
     1778
     1779   ep = png_ptr->chunkdata + 1;        /* Skip unit byte */
    17441780
    17451781#ifdef PNG_FLOATING_POINT_SUPPORTED
     
    17471783   if (*vp)
    17481784   {
    1749        png_warning(png_ptr, "malformed width string in sCAL chunk");
    1750        return;
     1785      png_warning(png_ptr, "malformed width string in sCAL chunk");
     1786      return;
    17511787   }
    17521788#else
     
    17541790   swidth = (png_charp)png_malloc_warn(png_ptr, png_strlen(ep) + 1);
    17551791   if (swidth == NULL)
    1756      {
    1757        png_warning(png_ptr, "Out of memory while processing sCAL chunk width");
    1758        return;
    1759      }
     1792   {
     1793      png_warning(png_ptr, "Out of memory while processing sCAL chunk width");
     1794      return;
     1795   }
    17601796   png_memcpy(swidth, ep, (png_size_t)png_strlen(ep));
    17611797#endif
    17621798#endif
    17631799
    1764    for (ep = buffer; *ep; ep++)
    1765       /* empty loop */ ;
     1800   for (ep = png_ptr->chunkdata; *ep; ep++)
     1801      /* Empty loop */ ;
    17661802   ep++;
    17671803
    1768    if (buffer + slength < ep)
    1769    {
    1770        png_warning(png_ptr, "Truncated sCAL chunk");
     1804   if (png_ptr->chunkdata + slength < ep)
     1805   {
     1806      png_warning(png_ptr, "Truncated sCAL chunk");
    17711807#if defined(PNG_FIXED_POINT_SUPPORTED) && \
    17721808    !defined(PNG_FLOATING_POINT_SUPPORTED)
    1773        png_free(png_ptr, swidth);
    1774 #endif
    1775       png_free(png_ptr, buffer);
    1776        return;
     1809      png_free(png_ptr, swidth);
     1810#endif
     1811      png_free(png_ptr, png_ptr->chunkdata);
     1812      png_ptr->chunkdata = NULL;
     1813      return;
    17771814   }
    17781815
     
    17811818   if (*vp)
    17821819   {
    1783        png_warning(png_ptr, "malformed height string in sCAL chunk");
    1784        return;
     1820      png_warning(png_ptr, "malformed height string in sCAL chunk");
     1821      return;
    17851822   }
    17861823#else
     
    17881825   sheight = (png_charp)png_malloc_warn(png_ptr, png_strlen(ep) + 1);
    17891826   if (sheight == NULL)
    1790      {
    1791        png_warning(png_ptr, "Out of memory while processing sCAL chunk height");
    1792        return;
    1793      }
     1827   {
     1828      png_warning(png_ptr, "Out of memory while processing sCAL chunk height");
     1829      return;
     1830   }
    17941831   png_memcpy(sheight, ep, (png_size_t)png_strlen(ep));
    17951832#endif
    17961833#endif
    17971834
    1798    if (buffer + slength < ep
     1835   if (png_ptr->chunkdata + slength < ep
    17991836#ifdef PNG_FLOATING_POINT_SUPPORTED
    18001837      || width <= 0. || height <= 0.
     
    18031840   {
    18041841      png_warning(png_ptr, "Invalid sCAL data");
    1805       png_free(png_ptr, buffer);
     1842      png_free(png_ptr, png_ptr->chunkdata);
     1843      png_ptr->chunkdata = NULL;
    18061844#if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
    18071845      png_free(png_ptr, swidth);
     
    18131851
    18141852#ifdef PNG_FLOATING_POINT_SUPPORTED
    1815    png_set_sCAL(png_ptr, info_ptr, buffer[0], width, height);
     1853   png_set_sCAL(png_ptr, info_ptr, png_ptr->chunkdata[0], width, height);
    18161854#else
    18171855#ifdef PNG_FIXED_POINT_SUPPORTED
    1818    png_set_sCAL_s(png_ptr, info_ptr, buffer[0], swidth, sheight);
    1819 #endif
    1820 #endif
    1821 
    1822    png_free(png_ptr, buffer);
     1856   png_set_sCAL_s(png_ptr, info_ptr, png_ptr->chunkdata[0], swidth, sheight);
     1857#endif
     1858#endif
     1859
     1860   png_free(png_ptr, png_ptr->chunkdata);
     1861   png_ptr->chunkdata = NULL;
    18231862#if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
    18241863   png_free(png_ptr, swidth);
     
    18351874   png_time mod_time;
    18361875
    1837    png_debug(1, "in png_handle_tIME\n");
     1876   png_debug(1, "in png_handle_tIME");
    18381877
    18391878   if (!(png_ptr->mode & PNG_HAVE_IHDR))
     
    18831922   int ret;
    18841923
    1885    png_debug(1, "in png_handle_tEXt\n");
     1924   png_debug(1, "in png_handle_tEXt");
     1925
    18861926
    18871927   if (!(png_ptr->mode & PNG_HAVE_IHDR))
     
    19001940#endif
    19011941
    1902    key = (png_charp)png_malloc_warn(png_ptr, length + 1);
    1903    if (key == NULL)
     1942   png_free(png_ptr, png_ptr->chunkdata);
     1943
     1944   png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
     1945   if (png_ptr->chunkdata == NULL)
    19041946   {
    19051947     png_warning(png_ptr, "No memory to process text chunk.");
     
    19071949   }
    19081950   slength = (png_size_t)length;
    1909    png_crc_read(png_ptr, (png_bytep)key, slength);
     1951   png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
    19101952
    19111953   if (png_crc_finish(png_ptr, skip))
    19121954   {
    1913       png_free(png_ptr, key);
    1914       return;
    1915    }
     1955      png_free(png_ptr, png_ptr->chunkdata);
     1956      png_ptr->chunkdata = NULL;
     1957      return;
     1958   }
     1959
     1960   key = png_ptr->chunkdata;
    19161961
    19171962   key[slength] = 0x00;
    19181963
    19191964   for (text = key; *text; text++)
    1920       /* empty loop to find end of key */ ;
     1965      /* Empty loop to find end of key */ ;
    19211966
    19221967   if (text != key + slength)
     
    19281973   {
    19291974     png_warning(png_ptr, "Not enough memory to process text chunk.");
    1930      png_free(png_ptr, key);
     1975     png_free(png_ptr, png_ptr->chunkdata);
     1976     png_ptr->chunkdata = NULL;
    19311977     return;
    19321978   }
     
    19411987   text_ptr->text_length = png_strlen(text);
    19421988
    1943    ret=png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
    1944 
    1945    png_free(png_ptr, key);
     1989   ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
     1990
     1991   png_free(png_ptr, png_ptr->chunkdata);
     1992   png_ptr->chunkdata = NULL;
    19461993   png_free(png_ptr, text_ptr);
    19471994   if (ret)
     
    19511998
    19521999#if defined(PNG_READ_zTXt_SUPPORTED)
    1953 /* note: this does not correctly handle chunks that are > 64K under DOS */
     2000/* Note: this does not correctly handle chunks that are > 64K under DOS */
    19542001void /* PRIVATE */
    19552002png_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
    19562003{
    19572004   png_textp text_ptr;
    1958    png_charp chunkdata;
    19592005   png_charp text;
    19602006   int comp_type;
     
    19622008   png_size_t slength, prefix_len, data_len;
    19632009
    1964    png_debug(1, "in png_handle_zTXt\n");
     2010   png_debug(1, "in png_handle_zTXt");
     2011
     2012
    19652013   if (!(png_ptr->mode & PNG_HAVE_IHDR))
    19662014      png_error(png_ptr, "Missing IHDR before zTXt");
     
    19742022   if (length > (png_uint_32)65535L)
    19752023   {
    1976      png_warning(png_ptr,"zTXt chunk too large to fit in memory");
     2024     png_warning(png_ptr, "zTXt chunk too large to fit in memory");
    19772025     png_crc_finish(png_ptr, length);
    19782026     return;
     
    19802028#endif
    19812029
    1982    chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
    1983    if (chunkdata == NULL)
    1984    {
    1985      png_warning(png_ptr,"Out of memory processing zTXt chunk.");
     2030   png_free(png_ptr, png_ptr->chunkdata);
     2031   png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
     2032   if (png_ptr->chunkdata == NULL)
     2033   {
     2034     png_warning(png_ptr, "Out of memory processing zTXt chunk.");
    19862035     return;
    19872036   }
    19882037   slength = (png_size_t)length;
    1989    png_crc_read(png_ptr, (png_bytep)chunkdata, slength);
     2038   png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
    19902039   if (png_crc_finish(png_ptr, 0))
    19912040   {
    1992       png_free(png_ptr, chunkdata);
    1993       return;
    1994    }
    1995 
    1996    chunkdata[slength] = 0x00;
    1997 
    1998    for (text = chunkdata; *text; text++)
    1999       /* empty loop */ ;
     2041      png_free(png_ptr, png_ptr->chunkdata);
     2042      png_ptr->chunkdata = NULL;
     2043      return;
     2044   }
     2045
     2046   png_ptr->chunkdata[slength] = 0x00;
     2047
     2048   for (text = png_ptr->chunkdata; *text; text++)
     2049      /* Empty loop */ ;
    20002050
    20012051   /* zTXt must have some text after the chunkdataword */
    2002    if (text >= chunkdata + slength - 2)
     2052   if (text >= png_ptr->chunkdata + slength - 2)
    20032053   {
    20042054      png_warning(png_ptr, "Truncated zTXt chunk");
    2005       png_free(png_ptr, chunkdata);
     2055      png_free(png_ptr, png_ptr->chunkdata);
     2056      png_ptr->chunkdata = NULL;
    20062057      return;
    20072058   }
     
    20142065          comp_type = PNG_TEXT_COMPRESSION_zTXt;
    20152066       }
    2016        text++;        /* skip the compression_method byte */
    2017    }
    2018    prefix_len = text - chunkdata;
    2019 
    2020    chunkdata = (png_charp)png_decompress_chunk(png_ptr, comp_type, chunkdata,
    2021                                     (png_size_t)length, prefix_len, &data_len);
     2067       text++;        /* Skip the compression_method byte */
     2068   }
     2069   prefix_len = text - png_ptr->chunkdata;
     2070
     2071   png_decompress_chunk(png_ptr, comp_type,
     2072     (png_size_t)length, prefix_len, &data_len);
    20222073
    20232074   text_ptr = (png_textp)png_malloc_warn(png_ptr,
    2024      (png_uint_32)png_sizeof(png_text));
     2075      (png_uint_32)png_sizeof(png_text));
    20252076   if (text_ptr == NULL)
    20262077   {
    2027      png_warning(png_ptr,"Not enough memory to process zTXt chunk.");
    2028      png_free(png_ptr, chunkdata);
     2078     png_warning(png_ptr, "Not enough memory to process zTXt chunk.");
     2079     png_free(png_ptr, png_ptr->chunkdata);
     2080     png_ptr->chunkdata = NULL;
    20292081     return;
    20302082   }
    20312083   text_ptr->compression = comp_type;
    2032    text_ptr->key = chunkdata;
     2084   text_ptr->key = png_ptr->chunkdata;
    20332085#ifdef PNG_iTXt_SUPPORTED
    20342086   text_ptr->lang = NULL;
     
    20362088   text_ptr->itxt_length = 0;
    20372089#endif
    2038    text_ptr->text = chunkdata + prefix_len;
     2090   text_ptr->text = png_ptr->chunkdata + prefix_len;
    20392091   text_ptr->text_length = data_len;
    20402092
    2041    ret=png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
     2093   ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
    20422094
    20432095   png_free(png_ptr, text_ptr);
    2044    png_free(png_ptr, chunkdata);
     2096   png_free(png_ptr, png_ptr->chunkdata);
     2097   png_ptr->chunkdata = NULL;
    20452098   if (ret)
    20462099     png_error(png_ptr, "Insufficient memory to store zTXt chunk.");
     
    20492102
    20502103#if defined(PNG_READ_iTXt_SUPPORTED)
    2051 /* note: this does not correctly handle chunks that are > 64K under DOS */
     2104/* Note: this does not correctly handle chunks that are > 64K under DOS */
    20522105void /* PRIVATE */
    20532106png_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
    20542107{
    20552108   png_textp text_ptr;
    2056    png_charp chunkdata;
    20572109   png_charp key, lang, text, lang_key;
    20582110   int comp_flag;
     
    20612113   png_size_t slength, prefix_len, data_len;
    20622114
    2063    png_debug(1, "in png_handle_iTXt\n");
     2115   png_debug(1, "in png_handle_iTXt");
     2116
    20642117
    20652118   if (!(png_ptr->mode & PNG_HAVE_IHDR))
     
    20742127   if (length > (png_uint_32)65535L)
    20752128   {
    2076      png_warning(png_ptr,"iTXt chunk too large to fit in memory");
     2129     png_warning(png_ptr, "iTXt chunk too large to fit in memory");
    20772130     png_crc_finish(png_ptr, length);
    20782131     return;
     
    20802133#endif
    20812134
    2082    chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
    2083    if (chunkdata == NULL)
     2135   png_free(png_ptr, png_ptr->chunkdata);
     2136   png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
     2137   if (png_ptr->chunkdata == NULL)
    20842138   {
    20852139     png_warning(png_ptr, "No memory to process iTXt chunk.");
     
    20872141   }
    20882142   slength = (png_size_t)length;
    2089    png_crc_read(png_ptr, (png_bytep)chunkdata, slength);
     2143   png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
    20902144   if (png_crc_finish(png_ptr, 0))
    20912145   {
    2092       png_free(png_ptr, chunkdata);
    2093       return;
    2094    }
    2095 
    2096    chunkdata[slength] = 0x00;
    2097 
    2098    for (lang = chunkdata; *lang; lang++)
    2099       /* empty loop */ ;
    2100    lang++;        /* skip NUL separator */
     2146      png_free(png_ptr, png_ptr->chunkdata);
     2147      png_ptr->chunkdata = NULL;
     2148      return;
     2149   }
     2150
     2151   png_ptr->chunkdata[slength] = 0x00;
     2152
     2153   for (lang = png_ptr->chunkdata; *lang; lang++)
     2154      /* Empty loop */ ;
     2155   lang++;        /* Skip NUL separator */
    21012156
    21022157   /* iTXt must have a language tag (possibly empty), two compression bytes,
    2103       translated keyword (possibly empty), and possibly some text after the
    2104       keyword */
    2105 
    2106    if (lang >= chunkdata + slength - 3)
     2158    * translated keyword (possibly empty), and possibly some text after the
     2159    * keyword
     2160    */
     2161
     2162   if (lang >= png_ptr->chunkdata + slength - 3)
    21072163   {
    21082164      png_warning(png_ptr, "Truncated iTXt chunk");
    2109       png_free(png_ptr, chunkdata);
     2165      png_free(png_ptr, png_ptr->chunkdata);
     2166      png_ptr->chunkdata = NULL;
    21102167      return;
    21112168   }
     
    21172174
    21182175   for (lang_key = lang; *lang_key; lang_key++)
    2119       /* empty loop */ ;
    2120    lang_key++;        /* skip NUL separator */
    2121 
    2122    if (lang_key >= chunkdata + slength)
     2176      /* Empty loop */ ;
     2177   lang_key++;        /* Skip NUL separator */
     2178
     2179   if (lang_key >= png_ptr->chunkdata + slength)
    21232180   {
    21242181      png_warning(png_ptr, "Truncated iTXt chunk");
    2125       png_free(png_ptr, chunkdata);
     2182      png_free(png_ptr, png_ptr->chunkdata);
     2183      png_ptr->chunkdata = NULL;
    21262184      return;
    21272185   }
    21282186
    21292187   for (text = lang_key; *text; text++)
    2130       /* empty loop */ ;
    2131    text++;        /* skip NUL separator */
    2132    if (text >= chunkdata + slength)
     2188      /* Empty loop */ ;
     2189   text++;        /* Skip NUL separator */
     2190   if (text >= png_ptr->chunkdata + slength)
    21332191   {
    21342192      png_warning(png_ptr, "Malformed iTXt chunk");
    2135       png_free(png_ptr, chunkdata);
    2136       return;
    2137    }
    2138 
    2139    prefix_len = text - chunkdata;
    2140 
    2141    key=chunkdata;
     2193      png_free(png_ptr, png_ptr->chunkdata);
     2194      png_ptr->chunkdata = NULL;
     2195      return;
     2196   }
     2197
     2198   prefix_len = text - png_ptr->chunkdata;
     2199
     2200   key=png_ptr->chunkdata;
    21422201   if (comp_flag)
    2143        chunkdata = png_decompress_chunk(png_ptr, comp_type, chunkdata,
    2144           (size_t)length, prefix_len, &data_len);
     2202       png_decompress_chunk(png_ptr, comp_type,
     2203         (size_t)length, prefix_len, &data_len);
    21452204   else
    2146        data_len=png_strlen(chunkdata + prefix_len);
     2205       data_len = png_strlen(png_ptr->chunkdata + prefix_len);
    21472206   text_ptr = (png_textp)png_malloc_warn(png_ptr,
    21482207      (png_uint_32)png_sizeof(png_text));
    21492208   if (text_ptr == NULL)
    21502209   {
    2151      png_warning(png_ptr,"Not enough memory to process iTXt chunk.");
    2152      png_free(png_ptr, chunkdata);
     2210     png_warning(png_ptr, "Not enough memory to process iTXt chunk.");
     2211     png_free(png_ptr, png_ptr->chunkdata);
     2212     png_ptr->chunkdata = NULL;
    21532213     return;
    21542214   }
    21552215   text_ptr->compression = (int)comp_flag + 1;
    2156    text_ptr->lang_key = chunkdata+(lang_key-key);
    2157    text_ptr->lang = chunkdata+(lang-key);
     2216   text_ptr->lang_key = png_ptr->chunkdata + (lang_key - key);
     2217   text_ptr->lang = png_ptr->chunkdata + (lang - key);
    21582218   text_ptr->itxt_length = data_len;
    21592219   text_ptr->text_length = 0;
    2160    text_ptr->key = chunkdata;
    2161    text_ptr->text = chunkdata + prefix_len;
    2162 
    2163    ret=png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
     2220   text_ptr->key = png_ptr->chunkdata;
     2221   text_ptr->text = png_ptr->chunkdata + prefix_len;
     2222
     2223   ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
    21642224
    21652225   png_free(png_ptr, text_ptr);
    2166    png_free(png_ptr, chunkdata);
     2226   png_free(png_ptr, png_ptr->chunkdata);
     2227   png_ptr->chunkdata = NULL;
    21672228   if (ret)
    21682229     png_error(png_ptr, "Insufficient memory to store iTXt chunk.");
     
    21802241   png_uint_32 skip = 0;
    21812242
    2182    png_debug(1, "in png_handle_unknown\n");
     2243   png_debug(1, "in png_handle_unknown");
     2244
    21832245
    21842246   if (png_ptr->mode & PNG_HAVE_IDAT)
     
    21872249      PNG_CONST PNG_IDAT;
    21882250#endif
    2189       if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))  /* not an IDAT */
     2251      if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))  /* Not an IDAT */
    21902252         png_ptr->mode |= PNG_AFTER_IDAT;
    21912253   }
    21922254
    2193    png_check_chunk_name(png_ptr, png_ptr->chunk_name);
    2194 
    21952255   if (!(png_ptr->chunk_name[0] & 0x20))
    21962256   {
    2197 #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
    2198       if(png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
     2257#if defined(PNG_HANDLE_AS_UNKNOWN_SUPPORTED)
     2258      if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
    21992259           PNG_HANDLE_CHUNK_ALWAYS
    22002260#if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
     
    22072267
    22082268#if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
    2209    if ((png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS) ||
    2210        (png_ptr->read_user_chunk_fn != NULL))
     2269   if ((png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS)
     2270#if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
     2271       || (png_ptr->read_user_chunk_fn != NULL)
     2272#endif
     2273        )
    22112274   {
    22122275#ifdef PNG_MAX_MALLOC_64K
     
    22192282#endif
    22202283       png_memcpy((png_charp)png_ptr->unknown_chunk.name,
    2221                   (png_charp)png_ptr->chunk_name, 
     2284                  (png_charp)png_ptr->chunk_name,
    22222285                  png_sizeof(png_ptr->unknown_chunk.name));
    22232286       png_ptr->unknown_chunk.name[png_sizeof(png_ptr->unknown_chunk.name)-1] = '\0';
     
    22312294       }
    22322295#if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
    2233        if(png_ptr->read_user_chunk_fn != NULL)
     2296       if (png_ptr->read_user_chunk_fn != NULL)
    22342297       {
    2235           /* callback to user unknown chunk handler */
     2298          /* Callback to user unknown chunk handler */
    22362299          int ret;
    22372300          ret = (*(png_ptr->read_user_chunk_fn))
     
    22422305          {
    22432306             if (!(png_ptr->chunk_name[0] & 0x20))
    2244                 if(png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
     2307#if defined(PNG_HANDLE_AS_UNKNOWN_SUPPORTED)
     2308                if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
    22452309                     PNG_HANDLE_CHUNK_ALWAYS)
     2310#endif
    22462311                   png_chunk_error(png_ptr, "unknown critical chunk");
    22472312             png_set_unknown_chunks(png_ptr, info_ptr,
     
    22622327
    22632328#if !defined(PNG_READ_USER_CHUNKS_SUPPORTED)
    2264    info_ptr = info_ptr; /* quiet compiler warnings about unused info_ptr */
     2329   info_ptr = info_ptr; /* Quiet compiler warnings about unused info_ptr */
    22652330#endif
    22662331}
     
    22772342png_check_chunk_name(png_structp png_ptr, png_bytep chunk_name)
    22782343{
    2279    png_debug(1, "in png_check_chunk_name\n");
     2344   png_debug(1, "in png_check_chunk_name");
    22802345   if (isnonalpha(chunk_name[0]) || isnonalpha(chunk_name[1]) ||
    22812346       isnonalpha(chunk_name[2]) || isnonalpha(chunk_name[3]))
     
    22992364png_combine_row(png_structp png_ptr, png_bytep row, int mask)
    23002365{
    2301    png_debug(1,"in png_combine_row\n");
     2366   png_debug(1, "in png_combine_row");
    23022367   if (mask == 0xff)
    23032368   {
     
    25102575   png_uint_32 transformations = png_ptr->transformations;
    25112576#ifdef PNG_USE_LOCAL_ARRAYS
    2512    /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
    2513    /* offset to next interlace block */
     2577   /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
     2578   /* Offset to next interlace block */
    25142579   PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
    25152580#endif
    25162581
    2517    png_debug(1,"in png_do_read_interlace\n");
     2582   png_debug(1, "in png_do_read_interlace");
    25182583   if (row != NULL && row_info != NULL)
    25192584   {
     
    27162781      }
    27172782      row_info->width = final_width;
    2718       row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,final_width);
     2783      row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width);
    27192784   }
    27202785#if !defined(PNG_READ_PACKSWAP_SUPPORTED)
    2721    transformations = transformations; /* silence compiler warning */
     2786   transformations = transformations; /* Silence compiler warning */
    27222787#endif
    27232788}
     
    27282793   png_bytep prev_row, int filter)
    27292794{
    2730    png_debug(1, "in png_read_filter_row\n");
    2731    png_debug2(2,"row = %lu, filter = %d\n", png_ptr->row_number, filter);
     2795   png_debug(1, "in png_read_filter_row");
     2796   png_debug2(2, "row = %lu, filter = %d", png_ptr->row_number, filter);
    27322797   switch (filter)
    27332798   {
     
    28032868         }
    28042869
    2805          for (i = 0; i < istop; i++)   /* use leftover rp,pp */
     2870         for (i = 0; i < istop; i++)   /* Use leftover rp,pp */
    28062871         {
    28072872            int a, b, c, pa, pb, pc, p;
     
    28332898             */
    28342899
    2835             p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
     2900            p = (pa <= pb && pa <= pc) ? a : (pb <= pc) ? b : c;
    28362901
    28372902            *rp = (png_byte)(((int)(*rp) + p) & 0xff);
     
    28422907      default:
    28432908         png_warning(png_ptr, "Ignoring bad adaptive filter type");
    2844          *row=0;
     2909         *row = 0;
    28452910         break;
    28462911   }
    28472912}
    28482913
     2914#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
    28492915void /* PRIVATE */
    28502916png_read_finish_row(png_structp png_ptr)
     
    28522918#ifdef PNG_USE_LOCAL_ARRAYS
    28532919#ifdef PNG_READ_INTERLACING_SUPPORTED
    2854    /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
    2855 
    2856    /* start of interlace block */
     2920   /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
     2921
     2922   /* Start of interlace block */
    28572923   PNG_CONST int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
    28582924
    2859    /* offset to next interlace block */
     2925   /* Offset to next interlace block */
    28602926   PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
    28612927
    2862    /* start of interlace block in the y direction */
     2928   /* Start of interlace block in the y direction */
    28632929   PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
    28642930
    2865    /* offset to next interlace block in the y direction */
     2931   /* Offset to next interlace block in the y direction */
    28662932   PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
    28672933#endif /* PNG_READ_INTERLACING_SUPPORTED */
    28682934#endif
    28692935
    2870    png_debug(1, "in png_read_finish_row\n");
     2936   png_debug(1, "in png_read_finish_row");
    28712937   png_ptr->row_number++;
    28722938   if (png_ptr->row_number < png_ptr->num_rows)
     
    29202986      png_ptr->zstream.next_out = (Byte *)&extra;
    29212987      png_ptr->zstream.avail_out = (uInt)1;
    2922       for(;;)
     2988      for (;;)
    29232989      {
    29242990         if (!(png_ptr->zstream.avail_in))
     
    29783044   png_ptr->mode |= PNG_AFTER_IDAT;
    29793045}
     3046#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
    29803047
    29813048void /* PRIVATE */
     
    29843051#ifdef PNG_USE_LOCAL_ARRAYS
    29853052#ifdef PNG_READ_INTERLACING_SUPPORTED
    2986    /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
    2987 
    2988    /* start of interlace block */
     3053   /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
     3054
     3055   /* Start of interlace block */
    29893056   PNG_CONST int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
    29903057
    2991    /* offset to next interlace block */
     3058   /* Offset to next interlace block */
    29923059   PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
    29933060
    2994    /* start of interlace block in the y direction */
     3061   /* Start of interlace block in the y direction */
    29953062   PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
    29963063
    2997    /* offset to next interlace block in the y direction */
     3064   /* Offset to next interlace block in the y direction */
    29983065   PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
    29993066#endif
     
    30013068
    30023069   int max_pixel_depth;
    3003    png_uint_32 row_bytes;
    3004 
    3005    png_debug(1, "in png_read_start_row\n");
     3070   png_size_t row_bytes;
     3071
     3072   png_debug(1, "in png_read_start_row");
    30063073   png_ptr->zstream.avail_in = 0;
    30073074   png_init_read_transformations(png_ptr);
     
    30203087         png_pass_inc[png_ptr->pass];
    30213088
    3022          row_bytes = PNG_ROWBYTES(png_ptr->pixel_depth,png_ptr->iwidth) + 1;
    3023 
    3024          png_ptr->irowbytes = (png_size_t)row_bytes;
    3025          if((png_uint_32)png_ptr->irowbytes != row_bytes)
    3026             png_error(png_ptr, "Rowbytes overflow in png_read_start_row");
     3089         png_ptr->irowbytes =
     3090            PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1;
    30273091   }
    30283092   else
     
    31263190#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
    31273191defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
    3128    if(png_ptr->transformations & PNG_USER_TRANSFORM)
     3192   if (png_ptr->transformations & PNG_USER_TRANSFORM)
    31293193     {
    3130        int user_pixel_depth=png_ptr->user_transform_depth*
     3194       int user_pixel_depth = png_ptr->user_transform_depth*
    31313195         png_ptr->user_transform_channels;
    3132        if(user_pixel_depth > max_pixel_depth)
     3196       if (user_pixel_depth > max_pixel_depth)
    31333197         max_pixel_depth=user_pixel_depth;
    31343198     }
    31353199#endif
    31363200
    3137    /* align the width on the next larger 8 pixels.  Mainly used
    3138       for interlacing */
     3201   /* Align the width on the next larger 8 pixels.  Mainly used
     3202    * for interlacing
     3203    */
    31393204   row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7));
    3140    /* calculate the maximum bytes needed, adding a byte and a pixel
    3141       for safety's sake */
    3142    row_bytes = PNG_ROWBYTES(max_pixel_depth,row_bytes) +
     3205   /* Calculate the maximum bytes needed, adding a byte and a pixel
     3206    * for safety's sake
     3207    */
     3208   row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) +
    31433209      1 + ((max_pixel_depth + 7) >> 3);
    31443210#ifdef PNG_MAX_MALLOC_64K
     
    31473213#endif
    31483214
    3149    if(row_bytes + 64 > png_ptr->old_big_row_buf_size)
    3150    {
    3151      png_free(png_ptr,png_ptr->big_row_buf);
    3152      png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes+64);
    3153      png_ptr->row_buf = png_ptr->big_row_buf+32;
    3154      png_ptr->old_big_row_buf_size = row_bytes+64;
     3215   if (row_bytes + 64 > png_ptr->old_big_row_buf_size)
     3216   {
     3217     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)
     3220       png_memset(png_ptr->big_row_buf, 0, row_bytes + 64);
     3221     png_ptr->row_buf = png_ptr->big_row_buf + 32;
     3222     png_ptr->old_big_row_buf_size = row_bytes + 64;
    31553223   }
    31563224
    31573225#ifdef PNG_MAX_MALLOC_64K
    3158    if ((png_uint_32)png_ptr->rowbytes + 1 > (png_uint_32)65536L)
     3226   if ((png_uint_32)row_bytes + 1 > (png_uint_32)65536L)
    31593227      png_error(png_ptr, "This image requires a row greater than 64KB");
    31603228#endif
    3161    if ((png_uint_32)png_ptr->rowbytes > (png_uint_32)(PNG_SIZE_MAX - 1))
     3229   if ((png_uint_32)row_bytes > (png_uint_32)(PNG_SIZE_MAX - 1))
    31623230      png_error(png_ptr, "Row has too many bytes to allocate in memory.");
    31633231
    3164    if(png_ptr->rowbytes+1 > png_ptr->old_prev_row_size)
    3165    {
    3166      png_free(png_ptr,png_ptr->prev_row);
    3167      png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)(
    3168         png_ptr->rowbytes + 1));
    3169      png_ptr->old_prev_row_size = png_ptr->rowbytes+1;
    3170    }
    3171 
    3172    png_memset_check(png_ptr, png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
    3173 
    3174    png_debug1(3, "width = %lu,\n", png_ptr->width);
    3175    png_debug1(3, "height = %lu,\n", png_ptr->height);
    3176    png_debug1(3, "iwidth = %lu,\n", png_ptr->iwidth);
    3177    png_debug1(3, "num_rows = %lu\n", png_ptr->num_rows);
    3178    png_debug1(3, "rowbytes = %lu,\n", png_ptr->rowbytes);
    3179    png_debug1(3, "irowbytes = %lu,\n", png_ptr->irowbytes);
     3232   if (row_bytes + 1 > png_ptr->old_prev_row_size)
     3233   {
     3234      png_free(png_ptr, png_ptr->prev_row);
     3235      png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)(
     3236        row_bytes + 1));
     3237      png_memset_check(png_ptr, png_ptr->prev_row, 0, row_bytes + 1);
     3238      png_ptr->old_prev_row_size = row_bytes + 1;
     3239   }
     3240
     3241   png_ptr->rowbytes = row_bytes;
     3242
     3243   png_debug1(3, "width = %lu,", png_ptr->width);
     3244   png_debug1(3, "height = %lu,", png_ptr->height);
     3245   png_debug1(3, "iwidth = %lu,", png_ptr->iwidth);
     3246   png_debug1(3, "num_rows = %lu,", png_ptr->num_rows);
     3247   png_debug1(3, "rowbytes = %lu,", png_ptr->rowbytes);
     3248   png_debug1(3, "irowbytes = %lu", png_ptr->irowbytes);
    31803249
    31813250   png_ptr->flags |= PNG_FLAG_ROW_INIT;
Note: See TracChangeset for help on using the changeset viewer.