fukasawa e60969
fukasawa e60969
/* pngerror.c - stub functions for i/o and memory allocation
fukasawa e60969
 *
fukasawa e60969
 * Last changed in libpng 1.6.15 [November 20, 2014]
fukasawa e60969
 * Copyright (c) 1998-2002,2004,2006-2014 Glenn Randers-Pehrson
fukasawa e60969
 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
fukasawa e60969
 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
fukasawa e60969
 *
fukasawa e60969
 * This code is released under the libpng license.
fukasawa e60969
 * For conditions of distribution and use, see the disclaimer
fukasawa e60969
 * and license in png.h
fukasawa e60969
 *
fukasawa e60969
 * This file provides a location for all error handling.  Users who
fukasawa e60969
 * need special error handling are expected to write replacement functions
fukasawa e60969
 * and use png_set_error_fn() to use those functions.  See the instructions
fukasawa e60969
 * at each function.
fukasawa e60969
 */
fukasawa e60969
fukasawa e60969
#include "pngpriv.h"
fukasawa e60969
fukasawa e60969
#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
fukasawa e60969
fukasawa e60969
static PNG_FUNCTION(void, png_default_error,PNGARG((png_const_structrp png_ptr,
fukasawa e60969
    png_const_charp error_message)),PNG_NORETURN);
fukasawa e60969
fukasawa e60969
#ifdef PNG_WARNINGS_SUPPORTED
fukasawa e60969
static void /* PRIVATE */
fukasawa e60969
png_default_warning PNGARG((png_const_structrp png_ptr,
fukasawa e60969
   png_const_charp warning_message));
fukasawa e60969
#endif /* WARNINGS */
fukasawa e60969
fukasawa e60969
/* This function is called whenever there is a fatal error.  This function
fukasawa e60969
 * should not be changed.  If there is a need to handle errors differently,
fukasawa e60969
 * you should supply a replacement error function and use png_set_error_fn()
fukasawa e60969
 * to replace the error function at run-time.
fukasawa e60969
 */
fukasawa e60969
#ifdef PNG_ERROR_TEXT_SUPPORTED
fukasawa e60969
PNG_FUNCTION(void,PNGAPI
fukasawa e60969
png_error,(png_const_structrp png_ptr, png_const_charp error_message),
fukasawa e60969
   PNG_NORETURN)
fukasawa e60969
{
fukasawa e60969
#ifdef PNG_ERROR_NUMBERS_SUPPORTED
fukasawa e60969
   char msg[16];
fukasawa e60969
   if (png_ptr != NULL)
fukasawa e60969
   {
fukasawa e60969
      if ((png_ptr->flags &
fukasawa e60969
         (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT)) != 0
fukasawa e60969
      {
fukasawa e60969
         if (*error_message == PNG_LITERAL_SHARP)
fukasawa e60969
         {
fukasawa e60969
            /* Strip "#nnnn " from beginning of error message. */
fukasawa e60969
            int offset;
fukasawa e60969
            for (offset = 1; offset<15; offset++)
fukasawa e60969
               if (error_message[offset] == ' ')
fukasawa e60969
                  break;
fukasawa e60969
fukasawa e60969
            if ((png_ptr->flags & PNG_FLAG_STRIP_ERROR_TEXT) != 0)
fukasawa e60969
            {
fukasawa e60969
               int i;
fukasawa e60969
               for (i = 0; i < offset - 1; i++)
fukasawa e60969
                  msg[i] = error_message[i + 1];
fukasawa e60969
               msg[i - 1] = '\0';
fukasawa e60969
               error_message = msg;
fukasawa e60969
            }
fukasawa e60969
fukasawa e60969
            else
fukasawa e60969
               error_message += offset;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
      {
fukasawa e60969
         if ((png_ptr->flags & PNG_FLAG_STRIP_ERROR_TEXT) != 0)
fukasawa e60969
         {
fukasawa e60969
            msg[0] = '0';
fukasawa e60969
            msg[1] = '\0';
fukasawa e60969
            error_message = msg;
fukasawa e60969
         }
fukasawa e60969
       }
fukasawa e60969
     }
fukasawa e60969
   }
fukasawa e60969
#endif
fukasawa e60969
   if (png_ptr != NULL && png_ptr->error_fn != NULL)
fukasawa e60969
      (*(png_ptr->error_fn))(png_constcast(png_structrp,png_ptr),
fukasawa e60969
          error_message);
fukasawa e60969
fukasawa e60969
   /* If the custom handler doesn't exist, or if it returns,
fukasawa e60969
      use the default handler, which will not return. */
fukasawa e60969
   png_default_error(png_ptr, error_message);
fukasawa e60969
}
fukasawa e60969
#else
fukasawa e60969
PNG_FUNCTION(void,PNGAPI
fukasawa e60969
png_err,(png_const_structrp png_ptr),PNG_NORETURN)
fukasawa e60969
{
fukasawa e60969
   /* Prior to 1.5.2 the error_fn received a NULL pointer, expressed
fukasawa e60969
    * erroneously as '\0', instead of the empty string "".  This was
fukasawa e60969
    * apparently an error, introduced in libpng-1.2.20, and png_default_error
fukasawa e60969
    * will crash in this case.
fukasawa e60969
    */
fukasawa e60969
   if (png_ptr != NULL && png_ptr->error_fn != NULL)
fukasawa e60969
      (*(png_ptr->error_fn))(png_constcast(png_structrp,png_ptr), "");
fukasawa e60969
fukasawa e60969
   /* If the custom handler doesn't exist, or if it returns,
fukasawa e60969
      use the default handler, which will not return. */
fukasawa e60969
   png_default_error(png_ptr, "");
fukasawa e60969
}
fukasawa e60969
#endif /* ERROR_TEXT */
fukasawa e60969
fukasawa e60969
/* Utility to safely appends strings to a buffer.  This never errors out so
fukasawa e60969
 * error checking is not required in the caller.
fukasawa e60969
 */
fukasawa e60969
size_t
fukasawa e60969
png_safecat(png_charp buffer, size_t bufsize, size_t pos,
fukasawa e60969
   png_const_charp string)
fukasawa e60969
{
fukasawa e60969
   if (buffer != NULL && pos < bufsize)
fukasawa e60969
   {
fukasawa e60969
      if (string != NULL)
fukasawa e60969
         while (*string != '\0' && pos < bufsize-1)
fukasawa e60969
           buffer[pos++] = *string++;
fukasawa e60969
fukasawa e60969
      buffer[pos] = '\0';
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   return pos;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
#if defined(PNG_WARNINGS_SUPPORTED) || defined(PNG_TIME_RFC1123_SUPPORTED)
fukasawa e60969
/* Utility to dump an unsigned value into a buffer, given a start pointer and
fukasawa e60969
 * and end pointer (which should point just *beyond* the end of the buffer!)
fukasawa e60969
 * Returns the pointer to the start of the formatted string.
fukasawa e60969
 */
fukasawa e60969
png_charp
fukasawa e60969
png_format_number(png_const_charp start, png_charp end, int format,
fukasawa e60969
   png_alloc_size_t number)
fukasawa e60969
{
fukasawa e60969
   int count = 0;    /* number of digits output */
fukasawa e60969
   int mincount = 1; /* minimum number required */
fukasawa e60969
   int output = 0;   /* digit output (for the fixed point format) */
fukasawa e60969
fukasawa e60969
   *--end = '\0';
fukasawa e60969
fukasawa e60969
   /* This is written so that the loop always runs at least once, even with
fukasawa e60969
    * number zero.
fukasawa e60969
    */
fukasawa e60969
   while (end > start && (number != 0 || count < mincount))
fukasawa e60969
   {
fukasawa e60969
fukasawa e60969
      static const char digits[] = "0123456789ABCDEF";
fukasawa e60969
fukasawa e60969
      switch (format)
fukasawa e60969
      {
fukasawa e60969
         case PNG_NUMBER_FORMAT_fixed:
fukasawa e60969
            /* Needs five digits (the fraction) */
fukasawa e60969
            mincount = 5;
fukasawa e60969
            if (output != 0 || number % 10 != 0)
fukasawa e60969
            {
fukasawa e60969
               *--end = digits[number % 10];
fukasawa e60969
               output = 1;
fukasawa e60969
            }
fukasawa e60969
            number /= 10;
fukasawa e60969
            break;
fukasawa e60969
fukasawa e60969
         case PNG_NUMBER_FORMAT_02u:
fukasawa e60969
            /* Expects at least 2 digits. */
fukasawa e60969
            mincount = 2;
fukasawa e60969
            /* FALL THROUGH */
fukasawa e60969
fukasawa e60969
         case PNG_NUMBER_FORMAT_u:
fukasawa e60969
            *--end = digits[number % 10];
fukasawa e60969
            number /= 10;
fukasawa e60969
            break;
fukasawa e60969
fukasawa e60969
         case PNG_NUMBER_FORMAT_02x:
fukasawa e60969
            /* This format expects at least two digits */
fukasawa e60969
            mincount = 2;
fukasawa e60969
            /* FALL THROUGH */
fukasawa e60969
fukasawa e60969
         case PNG_NUMBER_FORMAT_x:
fukasawa e60969
            *--end = digits[number & 0xf];
fukasawa e60969
            number >>= 4;
fukasawa e60969
            break;
fukasawa e60969
fukasawa e60969
         default: /* an error */
fukasawa e60969
            number = 0;
fukasawa e60969
            break;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      /* Keep track of the number of digits added */
fukasawa e60969
      ++count;
fukasawa e60969
fukasawa e60969
      /* Float a fixed number here: */
fukasawa e60969
      if ((format == PNG_NUMBER_FORMAT_fixed) && (count == 5) && (end > start))
fukasawa e60969
      {
fukasawa e60969
         /* End of the fraction, but maybe nothing was output?  In that case
fukasawa e60969
          * drop the decimal point.  If the number is a true zero handle that
fukasawa e60969
          * here.
fukasawa e60969
          */
fukasawa e60969
         if (output != 0)
fukasawa e60969
            *--end = '.';
fukasawa e60969
         else if (number == 0) /* and !output */
fukasawa e60969
            *--end = '0';
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   return end;
fukasawa e60969
}
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef PNG_WARNINGS_SUPPORTED
fukasawa e60969
/* This function is called whenever there is a non-fatal error.  This function
fukasawa e60969
 * should not be changed.  If there is a need to handle warnings differently,
fukasawa e60969
 * you should supply a replacement warning function and use
fukasawa e60969
 * png_set_error_fn() to replace the warning function at run-time.
fukasawa e60969
 */
fukasawa e60969
void PNGAPI
fukasawa e60969
png_warning(png_const_structrp png_ptr, png_const_charp warning_message)
fukasawa e60969
{
fukasawa e60969
   int offset = 0;
fukasawa e60969
   if (png_ptr != NULL)
fukasawa e60969
   {
fukasawa e60969
#ifdef PNG_ERROR_NUMBERS_SUPPORTED
fukasawa e60969
   if ((png_ptr->flags &
fukasawa e60969
       (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT)) != 0)
fukasawa e60969
#endif
fukasawa e60969
      {
fukasawa e60969
         if (*warning_message == PNG_LITERAL_SHARP)
fukasawa e60969
         {
fukasawa e60969
            for (offset = 1; offset < 15; offset++)
fukasawa e60969
               if (warning_message[offset] == ' ')
fukasawa e60969
                  break;
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
   if (png_ptr != NULL && png_ptr->warning_fn != NULL)
fukasawa e60969
      (*(png_ptr->warning_fn))(png_constcast(png_structrp,png_ptr),
fukasawa e60969
         warning_message + offset);
fukasawa e60969
   else
fukasawa e60969
      png_default_warning(png_ptr, warning_message + offset);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* These functions support 'formatted' warning messages with up to
fukasawa e60969
 * PNG_WARNING_PARAMETER_COUNT parameters.  In the format string the parameter
fukasawa e60969
 * is introduced by @<number>, where 'number' starts at 1.  This follows the</number>
fukasawa e60969
 * standard established by X/Open for internationalizable error messages.
fukasawa e60969
 */
fukasawa e60969
void
fukasawa e60969
png_warning_parameter(png_warning_parameters p, int number,
fukasawa e60969
   png_const_charp string)
fukasawa e60969
{
fukasawa e60969
   if (number > 0 && number <= PNG_WARNING_PARAMETER_COUNT)
fukasawa e60969
      (void)png_safecat(p[number-1], (sizeof p[number-1]), 0, string);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
void
fukasawa e60969
png_warning_parameter_unsigned(png_warning_parameters p, int number, int format,
fukasawa e60969
   png_alloc_size_t value)
fukasawa e60969
{
fukasawa e60969
   char buffer[PNG_NUMBER_BUFFER_SIZE];
fukasawa e60969
   png_warning_parameter(p, number, PNG_FORMAT_NUMBER(buffer, format, value));
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
void
fukasawa e60969
png_warning_parameter_signed(png_warning_parameters p, int number, int format,
fukasawa e60969
   png_int_32 value)
fukasawa e60969
{
fukasawa e60969
   png_alloc_size_t u;
fukasawa e60969
   png_charp str;
fukasawa e60969
   char buffer[PNG_NUMBER_BUFFER_SIZE];
fukasawa e60969
fukasawa e60969
   /* Avoid overflow by doing the negate in a png_alloc_size_t: */
fukasawa e60969
   u = (png_alloc_size_t)value;
fukasawa e60969
   if (value < 0)
fukasawa e60969
      u = ~u + 1;
fukasawa e60969
fukasawa e60969
   str = PNG_FORMAT_NUMBER(buffer, format, u);
fukasawa e60969
fukasawa e60969
   if (value < 0 && str > buffer)
fukasawa e60969
      *--str = '-';
fukasawa e60969
fukasawa e60969
   png_warning_parameter(p, number, str);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
void
fukasawa e60969
png_formatted_warning(png_const_structrp png_ptr, png_warning_parameters p,
fukasawa e60969
   png_const_charp message)
fukasawa e60969
{
fukasawa e60969
   /* The internal buffer is just 192 bytes - enough for all our messages,
fukasawa e60969
    * overflow doesn't happen because this code checks!  If someone figures
fukasawa e60969
    * out how to send us a message longer than 192 bytes, all that will
fukasawa e60969
    * happen is that the message will be truncated appropriately.
fukasawa e60969
    */
fukasawa e60969
   size_t i = 0; /* Index in the msg[] buffer: */
fukasawa e60969
   char msg[192];
fukasawa e60969
fukasawa e60969
   /* Each iteration through the following loop writes at most one character
fukasawa e60969
    * to msg[i++] then returns here to validate that there is still space for
fukasawa e60969
    * the trailing '\0'.  It may (in the case of a parameter) read more than
fukasawa e60969
    * one character from message[]; it must check for '\0' and continue to the
fukasawa e60969
    * test if it finds the end of string.
fukasawa e60969
    */
fukasawa e60969
   while (i<(sizeof msg)-1 && *message != '\0')
fukasawa e60969
   {
fukasawa e60969
      /* '@' at end of string is now just printed (previously it was skipped);
fukasawa e60969
       * it is an error in the calling code to terminate the string with @.
fukasawa e60969
       */
fukasawa e60969
      if (p != NULL && *message == '@' && message[1] != '\0')
fukasawa e60969
      {
fukasawa e60969
         int parameter_char = *++message; /* Consume the '@' */
fukasawa e60969
         static const char valid_parameters[] = "123456789";
fukasawa e60969
         int parameter = 0;
fukasawa e60969
fukasawa e60969
         /* Search for the parameter digit, the index in the string is the
fukasawa e60969
          * parameter to use.
fukasawa e60969
          */
fukasawa e60969
         while (valid_parameters[parameter] != parameter_char &&
fukasawa e60969
            valid_parameters[parameter] != '\0')
fukasawa e60969
            ++parameter;
fukasawa e60969
fukasawa e60969
         /* If the parameter digit is out of range it will just get printed. */
fukasawa e60969
         if (parameter < PNG_WARNING_PARAMETER_COUNT)
fukasawa e60969
         {
fukasawa e60969
            /* Append this parameter */
fukasawa e60969
            png_const_charp parm = p[parameter];
fukasawa e60969
            png_const_charp pend = p[parameter] + (sizeof p[parameter]);
fukasawa e60969
fukasawa e60969
            /* No need to copy the trailing '\0' here, but there is no guarantee
fukasawa e60969
             * that parm[] has been initialized, so there is no guarantee of a
fukasawa e60969
             * trailing '\0':
fukasawa e60969
             */
fukasawa e60969
            while (i<(sizeof msg)-1 && *parm != '\0' && parm < pend)
fukasawa e60969
               msg[i++] = *parm++;
fukasawa e60969
fukasawa e60969
            /* Consume the parameter digit too: */
fukasawa e60969
            ++message;
fukasawa e60969
            continue;
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         /* else not a parameter and there is a character after the @ sign; just
fukasawa e60969
          * copy that.  This is known not to be '\0' because of the test above.
fukasawa e60969
          */
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      /* At this point *message can't be '\0', even in the bad parameter case
fukasawa e60969
       * above where there is a lone '@' at the end of the message string.
fukasawa e60969
       */
fukasawa e60969
      msg[i++] = *message++;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* i is always less than (sizeof msg), so: */
fukasawa e60969
   msg[i] = '\0';
fukasawa e60969
fukasawa e60969
   /* And this is the formatted message. It may be larger than
fukasawa e60969
    * PNG_MAX_ERROR_TEXT, but that is only used for 'chunk' errors and these
fukasawa e60969
    * are not (currently) formatted.
fukasawa e60969
    */
fukasawa e60969
   png_warning(png_ptr, msg);
fukasawa e60969
}
fukasawa e60969
#endif /* WARNINGS */
fukasawa e60969
fukasawa e60969
#ifdef PNG_BENIGN_ERRORS_SUPPORTED
fukasawa e60969
void PNGAPI
fukasawa e60969
png_benign_error(png_const_structrp png_ptr, png_const_charp error_message)
fukasawa e60969
{
fukasawa e60969
   if ((png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN) != 0)
fukasawa e60969
   {
fukasawa e60969
#     ifdef PNG_READ_SUPPORTED
fukasawa e60969
         if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0 &&
fukasawa e60969
            png_ptr->chunk_name != 0)
fukasawa e60969
            png_chunk_warning(png_ptr, error_message);
fukasawa e60969
         else
fukasawa e60969
#     endif
fukasawa e60969
      png_warning(png_ptr, error_message);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
   {
fukasawa e60969
#     ifdef PNG_READ_SUPPORTED
fukasawa e60969
         if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0 &&
fukasawa e60969
            png_ptr->chunk_name != 0)
fukasawa e60969
            png_chunk_error(png_ptr, error_message);
fukasawa e60969
         else
fukasawa e60969
#     endif
fukasawa e60969
      png_error(png_ptr, error_message);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
#  ifndef PNG_ERROR_TEXT_SUPPORTED
fukasawa e60969
      PNG_UNUSED(error_message)
fukasawa e60969
#  endif
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_app_warning(png_const_structrp png_ptr, png_const_charp error_message)
fukasawa e60969
{
fukasawa e60969
  if ((png_ptr->flags & PNG_FLAG_APP_WARNINGS_WARN) != 0)
fukasawa e60969
     png_warning(png_ptr, error_message);
fukasawa e60969
  else
fukasawa e60969
     png_error(png_ptr, error_message);
fukasawa e60969
fukasawa e60969
#  ifndef PNG_ERROR_TEXT_SUPPORTED
fukasawa e60969
      PNG_UNUSED(error_message)
fukasawa e60969
#  endif
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_app_error(png_const_structrp png_ptr, png_const_charp error_message)
fukasawa e60969
{
fukasawa e60969
  if ((png_ptr->flags & PNG_FLAG_APP_ERRORS_WARN) != 0)
fukasawa e60969
     png_warning(png_ptr, error_message);
fukasawa e60969
  else
fukasawa e60969
     png_error(png_ptr, error_message);
fukasawa e60969
fukasawa e60969
#  ifndef PNG_ERROR_TEXT_SUPPORTED
fukasawa e60969
      PNG_UNUSED(error_message)
fukasawa e60969
#  endif
fukasawa e60969
}
fukasawa e60969
#endif /* BENIGN_ERRORS */
fukasawa e60969
fukasawa e60969
#define PNG_MAX_ERROR_TEXT 196 /* Currently limited by profile_error in png.c */
fukasawa e60969
#if defined(PNG_WARNINGS_SUPPORTED) || \
fukasawa e60969
   (defined(PNG_READ_SUPPORTED) && defined(PNG_ERROR_TEXT_SUPPORTED))
fukasawa e60969
/* These utilities are used internally to build an error message that relates
fukasawa e60969
 * to the current chunk.  The chunk name comes from png_ptr->chunk_name,
fukasawa e60969
 * which is used to prefix the message.  The message is limited in length
fukasawa e60969
 * to 63 bytes. The name characters are output as hex digits wrapped in []
fukasawa e60969
 * if the character is invalid.
fukasawa e60969
 */
fukasawa e60969
#define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
fukasawa e60969
static PNG_CONST char png_digit[16] = {
fukasawa e60969
   '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
fukasawa e60969
   'A', 'B', 'C', 'D', 'E', 'F'
fukasawa e60969
};
fukasawa e60969
fukasawa e60969
static void /* PRIVATE */
fukasawa e60969
png_format_buffer(png_const_structrp png_ptr, png_charp buffer, png_const_charp
fukasawa e60969
    error_message)
fukasawa e60969
{
fukasawa e60969
   png_uint_32 chunk_name = png_ptr->chunk_name;
fukasawa e60969
   int iout = 0, ishift = 24;
fukasawa e60969
fukasawa e60969
   while (ishift >= 0)
fukasawa e60969
   {
fukasawa e60969
      int c = (int)(chunk_name >> ishift) & 0xff;
fukasawa e60969
fukasawa e60969
      ishift -= 8;
fukasawa e60969
      if (isnonalpha(c) != 0)
fukasawa e60969
      {
fukasawa e60969
         buffer[iout++] = PNG_LITERAL_LEFT_SQUARE_BRACKET;
fukasawa e60969
         buffer[iout++] = png_digit[(c & 0xf0) >> 4];
fukasawa e60969
         buffer[iout++] = png_digit[c & 0x0f];
fukasawa e60969
         buffer[iout++] = PNG_LITERAL_RIGHT_SQUARE_BRACKET;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
      {
fukasawa e60969
         buffer[iout++] = (char)c;
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   if (error_message == NULL)
fukasawa e60969
      buffer[iout] = '\0';
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
   {
fukasawa e60969
      int iin = 0;
fukasawa e60969
fukasawa e60969
      buffer[iout++] = ':';
fukasawa e60969
      buffer[iout++] = ' ';
fukasawa e60969
fukasawa e60969
      while (iin < PNG_MAX_ERROR_TEXT-1 && error_message[iin] != '\0')
fukasawa e60969
         buffer[iout++] = error_message[iin++];
fukasawa e60969
fukasawa e60969
      /* iin < PNG_MAX_ERROR_TEXT, so the following is safe: */
fukasawa e60969
      buffer[iout] = '\0';
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
#endif /* WARNINGS || ERROR_TEXT */
fukasawa e60969
fukasawa e60969
#if defined(PNG_READ_SUPPORTED) && defined(PNG_ERROR_TEXT_SUPPORTED)
fukasawa e60969
PNG_FUNCTION(void,PNGAPI
fukasawa e60969
png_chunk_error,(png_const_structrp png_ptr, png_const_charp error_message),
fukasawa e60969
   PNG_NORETURN)
fukasawa e60969
{
fukasawa e60969
   char msg[18+PNG_MAX_ERROR_TEXT];
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
      png_error(png_ptr, error_message);
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
   {
fukasawa e60969
      png_format_buffer(png_ptr, msg, error_message);
fukasawa e60969
      png_error(png_ptr, msg);
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
#endif /* READ && ERROR_TEXT */
fukasawa e60969
fukasawa e60969
#ifdef PNG_WARNINGS_SUPPORTED
fukasawa e60969
void PNGAPI
fukasawa e60969
png_chunk_warning(png_const_structrp png_ptr, png_const_charp warning_message)
fukasawa e60969
{
fukasawa e60969
   char msg[18+PNG_MAX_ERROR_TEXT];
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
      png_warning(png_ptr, warning_message);
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
   {
fukasawa e60969
      png_format_buffer(png_ptr, msg, warning_message);
fukasawa e60969
      png_warning(png_ptr, msg);
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
#endif /* WARNINGS */
fukasawa e60969
fukasawa e60969
#ifdef PNG_READ_SUPPORTED
fukasawa e60969
#ifdef PNG_BENIGN_ERRORS_SUPPORTED
fukasawa e60969
void PNGAPI
fukasawa e60969
png_chunk_benign_error(png_const_structrp png_ptr, png_const_charp
fukasawa e60969
    error_message)
fukasawa e60969
{
fukasawa e60969
   if ((png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN) != 0)
fukasawa e60969
      png_chunk_warning(png_ptr, error_message);
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
      png_chunk_error(png_ptr, error_message);
fukasawa e60969
fukasawa e60969
#  ifndef PNG_ERROR_TEXT_SUPPORTED
fukasawa e60969
      PNG_UNUSED(error_message)
fukasawa e60969
#  endif
fukasawa e60969
}
fukasawa e60969
#endif
fukasawa e60969
#endif /* READ */
fukasawa e60969
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_chunk_report(png_const_structrp png_ptr, png_const_charp message, int error)
fukasawa e60969
{
fukasawa e60969
#  ifndef PNG_WARNINGS_SUPPORTED
fukasawa e60969
      PNG_UNUSED(message)
fukasawa e60969
#  endif
fukasawa e60969
fukasawa e60969
   /* This is always supported, but for just read or just write it
fukasawa e60969
    * unconditionally does the right thing.
fukasawa e60969
    */
fukasawa e60969
#  if defined(PNG_READ_SUPPORTED) && defined(PNG_WRITE_SUPPORTED)
fukasawa e60969
      if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0)
fukasawa e60969
#  endif
fukasawa e60969
fukasawa e60969
#  ifdef PNG_READ_SUPPORTED
fukasawa e60969
      {
fukasawa e60969
         if (error < PNG_CHUNK_ERROR)
fukasawa e60969
            png_chunk_warning(png_ptr, message);
fukasawa e60969
fukasawa e60969
         else
fukasawa e60969
            png_chunk_benign_error(png_ptr, message);
fukasawa e60969
      }
fukasawa e60969
#  endif
fukasawa e60969
fukasawa e60969
#  if defined(PNG_READ_SUPPORTED) && defined(PNG_WRITE_SUPPORTED)
fukasawa e60969
      else if ((png_ptr->mode & PNG_IS_READ_STRUCT) == 0)
fukasawa e60969
#  endif
fukasawa e60969
fukasawa e60969
#  ifdef PNG_WRITE_SUPPORTED
fukasawa e60969
      {
fukasawa e60969
         if (error < PNG_CHUNK_WRITE_ERROR)
fukasawa e60969
            png_app_warning(png_ptr, message);
fukasawa e60969
fukasawa e60969
         else
fukasawa e60969
            png_app_error(png_ptr, message);
fukasawa e60969
      }
fukasawa e60969
#  endif
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
#ifdef PNG_ERROR_TEXT_SUPPORTED
fukasawa e60969
#ifdef PNG_FLOATING_POINT_SUPPORTED
fukasawa e60969
PNG_FUNCTION(void,
fukasawa e60969
png_fixed_error,(png_const_structrp png_ptr, png_const_charp name),PNG_NORETURN)
fukasawa e60969
{
fukasawa e60969
#  define fixed_message "fixed point overflow in "
fukasawa e60969
#  define fixed_message_ln ((sizeof fixed_message)-1)
fukasawa e60969
   int  iin;
fukasawa e60969
   char msg[fixed_message_ln+PNG_MAX_ERROR_TEXT];
fukasawa e60969
   memcpy(msg, fixed_message, fixed_message_ln);
fukasawa e60969
   iin = 0;
fukasawa e60969
   if (name != NULL)
fukasawa e60969
      while (iin < (PNG_MAX_ERROR_TEXT-1) && name[iin] != 0)
fukasawa e60969
      {
fukasawa e60969
         msg[fixed_message_ln + iin] = name[iin];
fukasawa e60969
         ++iin;
fukasawa e60969
      }
fukasawa e60969
   msg[fixed_message_ln + iin] = 0;
fukasawa e60969
   png_error(png_ptr, msg);
fukasawa e60969
}
fukasawa e60969
#endif
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef PNG_SETJMP_SUPPORTED
fukasawa e60969
/* This API only exists if ANSI-C style error handling is used,
fukasawa e60969
 * otherwise it is necessary for png_default_error to be overridden.
fukasawa e60969
 */
fukasawa e60969
jmp_buf* PNGAPI
fukasawa e60969
png_set_longjmp_fn(png_structrp png_ptr, png_longjmp_ptr longjmp_fn,
fukasawa e60969
    size_t jmp_buf_size)
fukasawa e60969
{
fukasawa e60969
   /* From libpng 1.6.0 the app gets one chance to set a 'jmpbuf_size' value
fukasawa e60969
    * and it must not change after that.  Libpng doesn't care how big the
fukasawa e60969
    * buffer is, just that it doesn't change.
fukasawa e60969
    *
fukasawa e60969
    * If the buffer size is no *larger* than the size of jmp_buf when libpng is
fukasawa e60969
    * compiled a built in jmp_buf is returned; this preserves the pre-1.6.0
fukasawa e60969
    * semantics that this call will not fail.  If the size is larger, however,
fukasawa e60969
    * the buffer is allocated and this may fail, causing the function to return
fukasawa e60969
    * NULL.
fukasawa e60969
    */
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
      return NULL;
fukasawa e60969
fukasawa e60969
   if (png_ptr->jmp_buf_ptr == NULL)
fukasawa e60969
   {
fukasawa e60969
      png_ptr->jmp_buf_size = 0; /* not allocated */
fukasawa e60969
fukasawa e60969
      if (jmp_buf_size <= (sizeof png_ptr->jmp_buf_local))
fukasawa e60969
         png_ptr->jmp_buf_ptr = &png_ptr->jmp_buf_local;
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
      {
fukasawa e60969
         png_ptr->jmp_buf_ptr = png_voidcast(jmp_buf *,
fukasawa e60969
            png_malloc_warn(png_ptr, jmp_buf_size));
fukasawa e60969
fukasawa e60969
         if (png_ptr->jmp_buf_ptr == NULL)
fukasawa e60969
            return NULL; /* new NULL return on OOM */
fukasawa e60969
fukasawa e60969
         png_ptr->jmp_buf_size = jmp_buf_size;
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else /* Already allocated: check the size */
fukasawa e60969
   {
fukasawa e60969
      size_t size = png_ptr->jmp_buf_size;
fukasawa e60969
fukasawa e60969
      if (size == 0)
fukasawa e60969
      {
fukasawa e60969
         size = (sizeof png_ptr->jmp_buf_local);
fukasawa e60969
         if (png_ptr->jmp_buf_ptr != &png_ptr->jmp_buf_local)
fukasawa e60969
         {
fukasawa e60969
            /* This is an internal error in libpng: somehow we have been left
fukasawa e60969
             * with a stack allocated jmp_buf when the application regained
fukasawa e60969
             * control.  It's always possible to fix this up, but for the moment
fukasawa e60969
             * this is a png_error because that makes it easy to detect.
fukasawa e60969
             */
fukasawa e60969
            png_error(png_ptr, "Libpng jmp_buf still allocated");
fukasawa e60969
            /* png_ptr->jmp_buf_ptr = &png_ptr->jmp_buf_local; */
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      if (size != jmp_buf_size)
fukasawa e60969
      {
fukasawa e60969
         png_warning(png_ptr, "Application jmp_buf size changed");
fukasawa e60969
         return NULL; /* caller will probably crash: no choice here */
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* Finally fill in the function, now we have a satisfactory buffer. It is
fukasawa e60969
    * valid to change the function on every call.
fukasawa e60969
    */
fukasawa e60969
   png_ptr->longjmp_fn = longjmp_fn;
fukasawa e60969
   return png_ptr->jmp_buf_ptr;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_free_jmpbuf(png_structrp png_ptr)
fukasawa e60969
{
fukasawa e60969
   if (png_ptr != NULL)
fukasawa e60969
   {
fukasawa e60969
      jmp_buf *jb = png_ptr->jmp_buf_ptr;
fukasawa e60969
fukasawa e60969
      /* A size of 0 is used to indicate a local, stack, allocation of the
fukasawa e60969
       * pointer; used here and in png.c
fukasawa e60969
       */
fukasawa e60969
      if (jb != NULL && png_ptr->jmp_buf_size > 0)
fukasawa e60969
      {
fukasawa e60969
fukasawa e60969
         /* This stuff is so that a failure to free the error control structure
fukasawa e60969
          * does not leave libpng in a state with no valid error handling: the
fukasawa e60969
          * free always succeeds, if there is an error it gets ignored.
fukasawa e60969
          */
fukasawa e60969
         if (jb != &png_ptr->jmp_buf_local)
fukasawa e60969
         {
fukasawa e60969
            /* Make an internal, libpng, jmp_buf to return here */
fukasawa e60969
            jmp_buf free_jmp_buf;
fukasawa e60969
fukasawa e60969
            if (!setjmp(free_jmp_buf))
fukasawa e60969
            {
fukasawa e60969
               png_ptr->jmp_buf_ptr = &free_jmp_buf; /* come back here */
fukasawa e60969
               png_ptr->jmp_buf_size = 0; /* stack allocation */
fukasawa e60969
               png_ptr->longjmp_fn = longjmp;
fukasawa e60969
               png_free(png_ptr, jb); /* Return to setjmp on error */
fukasawa e60969
            }
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      /* *Always* cancel everything out: */
fukasawa e60969
      png_ptr->jmp_buf_size = 0;
fukasawa e60969
      png_ptr->jmp_buf_ptr = NULL;
fukasawa e60969
      png_ptr->longjmp_fn = 0;
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
/* This is the default error handling function.  Note that replacements for
fukasawa e60969
 * this function MUST NOT RETURN, or the program will likely crash.  This
fukasawa e60969
 * function is used by default, or if the program supplies NULL for the
fukasawa e60969
 * error function pointer in png_set_error_fn().
fukasawa e60969
 */
fukasawa e60969
static PNG_FUNCTION(void /* PRIVATE */,
fukasawa e60969
png_default_error,(png_const_structrp png_ptr, png_const_charp error_message),
fukasawa e60969
   PNG_NORETURN)
fukasawa e60969
{
fukasawa e60969
#ifdef PNG_CONSOLE_IO_SUPPORTED
fukasawa e60969
#ifdef PNG_ERROR_NUMBERS_SUPPORTED
fukasawa e60969
   /* Check on NULL only added in 1.5.4 */
fukasawa e60969
   if (error_message != NULL && *error_message == PNG_LITERAL_SHARP)
fukasawa e60969
   {
fukasawa e60969
      /* Strip "#nnnn " from beginning of error message. */
fukasawa e60969
      int offset;
fukasawa e60969
      char error_number[16];
fukasawa e60969
      for (offset = 0; offset<15; offset++)
fukasawa e60969
      {
fukasawa e60969
         error_number[offset] = error_message[offset + 1];
fukasawa e60969
         if (error_message[offset] == ' ')
fukasawa e60969
            break;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      if ((offset > 1) && (offset < 15))
fukasawa e60969
      {
fukasawa e60969
         error_number[offset - 1] = '\0';
fukasawa e60969
         fprintf(stderr, "libpng error no. %s: %s",
fukasawa e60969
             error_number, error_message + offset + 1);
fukasawa e60969
         fprintf(stderr, PNG_STRING_NEWLINE);
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
      {
fukasawa e60969
         fprintf(stderr, "libpng error: %s, offset=%d",
fukasawa e60969
             error_message, offset);
fukasawa e60969
         fprintf(stderr, PNG_STRING_NEWLINE);
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
   else
fukasawa e60969
#endif
fukasawa e60969
   {
fukasawa e60969
      fprintf(stderr, "libpng error: %s", error_message ? error_message :
fukasawa e60969
         "undefined");
fukasawa e60969
      fprintf(stderr, PNG_STRING_NEWLINE);
fukasawa e60969
   }
fukasawa e60969
#else
fukasawa e60969
   PNG_UNUSED(error_message) /* Make compiler happy */
fukasawa e60969
#endif
fukasawa e60969
   png_longjmp(png_ptr, 1);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
PNG_FUNCTION(void,PNGAPI
fukasawa e60969
png_longjmp,(png_const_structrp png_ptr, int val),PNG_NORETURN)
fukasawa e60969
{
fukasawa e60969
#ifdef PNG_SETJMP_SUPPORTED
fukasawa e60969
   if (png_ptr != NULL && png_ptr->longjmp_fn != NULL &&
fukasawa e60969
       png_ptr->jmp_buf_ptr != NULL)
fukasawa e60969
      png_ptr->longjmp_fn(*png_ptr->jmp_buf_ptr, val);
fukasawa e60969
#else
fukasawa e60969
   PNG_UNUSED(png_ptr)
fukasawa e60969
   PNG_UNUSED(val)
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   /* If control reaches this point, png_longjmp() must not return. The only
fukasawa e60969
    * choice is to terminate the whole process (or maybe the thread); to do
fukasawa e60969
    * this the ANSI-C abort() function is used unless a different method is
fukasawa e60969
    * implemented by overriding the default configuration setting for
fukasawa e60969
    * PNG_ABORT().
fukasawa e60969
    */
fukasawa e60969
   PNG_ABORT();
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
#ifdef PNG_WARNINGS_SUPPORTED
fukasawa e60969
/* This function is called when there is a warning, but the library thinks
fukasawa e60969
 * it can continue anyway.  Replacement functions don't have to do anything
fukasawa e60969
 * here if you don't want them to.  In the default configuration, png_ptr is
fukasawa e60969
 * not used, but it is passed in case it may be useful.
fukasawa e60969
 */
fukasawa e60969
static void /* PRIVATE */
fukasawa e60969
png_default_warning(png_const_structrp png_ptr, png_const_charp warning_message)
fukasawa e60969
{
fukasawa e60969
#ifdef PNG_CONSOLE_IO_SUPPORTED
fukasawa e60969
#  ifdef PNG_ERROR_NUMBERS_SUPPORTED
fukasawa e60969
   if (*warning_message == PNG_LITERAL_SHARP)
fukasawa e60969
   {
fukasawa e60969
      int offset;
fukasawa e60969
      char warning_number[16];
fukasawa e60969
      for (offset = 0; offset < 15; offset++)
fukasawa e60969
      {
fukasawa e60969
         warning_number[offset] = warning_message[offset + 1];
fukasawa e60969
         if (warning_message[offset] == ' ')
fukasawa e60969
            break;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      if ((offset > 1) && (offset < 15))
fukasawa e60969
      {
fukasawa e60969
         warning_number[offset + 1] = '\0';
fukasawa e60969
         fprintf(stderr, "libpng warning no. %s: %s",
fukasawa e60969
             warning_number, warning_message + offset);
fukasawa e60969
         fprintf(stderr, PNG_STRING_NEWLINE);
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
      {
fukasawa e60969
         fprintf(stderr, "libpng warning: %s",
fukasawa e60969
             warning_message);
fukasawa e60969
         fprintf(stderr, PNG_STRING_NEWLINE);
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
   else
fukasawa e60969
#  endif
fukasawa e60969
fukasawa e60969
   {
fukasawa e60969
      fprintf(stderr, "libpng warning: %s", warning_message);
fukasawa e60969
      fprintf(stderr, PNG_STRING_NEWLINE);
fukasawa e60969
   }
fukasawa e60969
#else
fukasawa e60969
   PNG_UNUSED(warning_message) /* Make compiler happy */
fukasawa e60969
#endif
fukasawa e60969
   PNG_UNUSED(png_ptr) /* Make compiler happy */
fukasawa e60969
}
fukasawa e60969
#endif /* WARNINGS */
fukasawa e60969
fukasawa e60969
/* This function is called when the application wants to use another method
fukasawa e60969
 * of handling errors and warnings.  Note that the error function MUST NOT
fukasawa e60969
 * return to the calling routine or serious problems will occur.  The return
fukasawa e60969
 * method used in the default routine calls longjmp(png_ptr->jmp_buf_ptr, 1)
fukasawa e60969
 */
fukasawa e60969
void PNGAPI
fukasawa e60969
png_set_error_fn(png_structrp png_ptr, png_voidp error_ptr,
fukasawa e60969
    png_error_ptr error_fn, png_error_ptr warning_fn)
fukasawa e60969
{
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
      return;
fukasawa e60969
fukasawa e60969
   png_ptr->error_ptr = error_ptr;
fukasawa e60969
   png_ptr->error_fn = error_fn;
fukasawa e60969
#ifdef PNG_WARNINGS_SUPPORTED
fukasawa e60969
   png_ptr->warning_fn = warning_fn;
fukasawa e60969
#else
fukasawa e60969
   PNG_UNUSED(warning_fn)
fukasawa e60969
#endif
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
fukasawa e60969
/* This function returns a pointer to the error_ptr associated with the user
fukasawa e60969
 * functions.  The application should free any memory associated with this
fukasawa e60969
 * pointer before png_write_destroy and png_read_destroy are called.
fukasawa e60969
 */
fukasawa e60969
png_voidp PNGAPI
fukasawa e60969
png_get_error_ptr(png_const_structrp png_ptr)
fukasawa e60969
{
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
      return NULL;
fukasawa e60969
fukasawa e60969
   return ((png_voidp)png_ptr->error_ptr);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
fukasawa e60969
#ifdef PNG_ERROR_NUMBERS_SUPPORTED
fukasawa e60969
void PNGAPI
fukasawa e60969
png_set_strip_error_numbers(png_structrp png_ptr, png_uint_32 strip_mode)
fukasawa e60969
{
fukasawa e60969
   if (png_ptr != NULL)
fukasawa e60969
   {
fukasawa e60969
      png_ptr->flags &=
fukasawa e60969
         ((~(PNG_FLAG_STRIP_ERROR_NUMBERS |
fukasawa e60969
         PNG_FLAG_STRIP_ERROR_TEXT))&strip_mode);
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#if defined(PNG_SIMPLIFIED_READ_SUPPORTED) ||\
fukasawa e60969
   defined(PNG_SIMPLIFIED_WRITE_SUPPORTED)
fukasawa e60969
   /* Currently the above both depend on SETJMP_SUPPORTED, however it would be
fukasawa e60969
    * possible to implement without setjmp support just so long as there is some
fukasawa e60969
    * way to handle the error return here:
fukasawa e60969
    */
fukasawa e60969
PNG_FUNCTION(void /* PRIVATE */, (PNGCBAPI
fukasawa e60969
png_safe_error),(png_structp png_nonconst_ptr, png_const_charp error_message),
fukasawa e60969
   PNG_NORETURN)
fukasawa e60969
{
fukasawa e60969
   const png_const_structrp png_ptr = png_nonconst_ptr;
fukasawa e60969
   png_imagep image = png_voidcast(png_imagep, png_ptr->error_ptr);
fukasawa e60969
fukasawa e60969
   /* An error is always logged here, overwriting anything (typically a warning)
fukasawa e60969
    * that is already there:
fukasawa e60969
    */
fukasawa e60969
   if (image != NULL)
fukasawa e60969
   {
fukasawa e60969
      png_safecat(image->message, (sizeof image->message), 0, error_message);
fukasawa e60969
      image->warning_or_error |= PNG_IMAGE_ERROR;
fukasawa e60969
fukasawa e60969
      /* Retrieve the jmp_buf from within the png_control, making this work for
fukasawa e60969
       * C++ compilation too is pretty tricky: C++ wants a pointer to the first
fukasawa e60969
       * element of a jmp_buf, but C doesn't tell us the type of that.
fukasawa e60969
       */
fukasawa e60969
      if (image->opaque != NULL && image->opaque->error_buf != NULL)
fukasawa e60969
         longjmp(png_control_jmp_buf(image->opaque), 1);
fukasawa e60969
fukasawa e60969
      /* Missing longjmp buffer, the following is to help debugging: */
fukasawa e60969
      {
fukasawa e60969
         size_t pos = png_safecat(image->message, (sizeof image->message), 0,
fukasawa e60969
            "bad longjmp: ");
fukasawa e60969
         png_safecat(image->message, (sizeof image->message), pos,
fukasawa e60969
             error_message);
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* Here on an internal programming error. */
fukasawa e60969
   abort();
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
#ifdef PNG_WARNINGS_SUPPORTED
fukasawa e60969
void /* PRIVATE */ PNGCBAPI
fukasawa e60969
png_safe_warning(png_structp png_nonconst_ptr, png_const_charp warning_message)
fukasawa e60969
{
fukasawa e60969
   const png_const_structrp png_ptr = png_nonconst_ptr;
fukasawa e60969
   png_imagep image = png_voidcast(png_imagep, png_ptr->error_ptr);
fukasawa e60969
fukasawa e60969
   /* A warning is only logged if there is no prior warning or error. */
fukasawa e60969
   if (image->warning_or_error == 0)
fukasawa e60969
   {
fukasawa e60969
      png_safecat(image->message, (sizeof image->message), 0, warning_message);
fukasawa e60969
      image->warning_or_error |= PNG_IMAGE_WARNING;
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
int /* PRIVATE */
fukasawa e60969
png_safe_execute(png_imagep image_in, int (*function)(png_voidp), png_voidp arg)
fukasawa e60969
{
fukasawa e60969
   volatile png_imagep image = image_in;
fukasawa e60969
   volatile int result;
fukasawa e60969
   volatile png_voidp saved_error_buf;
fukasawa e60969
   jmp_buf safe_jmpbuf;
fukasawa e60969
fukasawa e60969
   /* Safely execute function(arg) with png_error returning to this function. */
fukasawa e60969
   saved_error_buf = image->opaque->error_buf;
fukasawa e60969
   result = setjmp(safe_jmpbuf) == 0;
fukasawa e60969
fukasawa e60969
   if (result != 0)
fukasawa e60969
   {
fukasawa e60969
fukasawa e60969
      image->opaque->error_buf = safe_jmpbuf;
fukasawa e60969
      result = function(arg);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   image->opaque->error_buf = saved_error_buf;
fukasawa e60969
fukasawa e60969
   /* And do the cleanup prior to any failure return. */
fukasawa e60969
   if (result == 0)
fukasawa e60969
      png_image_free(image);
fukasawa e60969
fukasawa e60969
   return result;
fukasawa e60969
}
fukasawa e60969
#endif /* SIMPLIFIED READ || SIMPLIFIED_WRITE */
fukasawa e60969
#endif /* READ || WRITE */