media/libpng/pngerror.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libpng/pngerror.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,958 @@
     1.4 +
     1.5 +/* pngerror.c - stub functions for i/o and memory allocation
     1.6 + *
     1.7 + * Last changed in libpng 1.6.10 [March 6, 2014]
     1.8 + * Copyright (c) 1998-2014 Glenn Randers-Pehrson
     1.9 + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
    1.10 + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
    1.11 + *
    1.12 + * This code is released under the libpng license.
    1.13 + * For conditions of distribution and use, see the disclaimer
    1.14 + * and license in png.h
    1.15 + *
    1.16 + * This file provides a location for all error handling.  Users who
    1.17 + * need special error handling are expected to write replacement functions
    1.18 + * and use png_set_error_fn() to use those functions.  See the instructions
    1.19 + * at each function.
    1.20 + */
    1.21 +
    1.22 +#include "pngpriv.h"
    1.23 +
    1.24 +#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
    1.25 +
    1.26 +static PNG_FUNCTION(void, png_default_error,PNGARG((png_const_structrp png_ptr,
    1.27 +    png_const_charp error_message)),PNG_NORETURN);
    1.28 +
    1.29 +#ifdef PNG_WARNINGS_SUPPORTED
    1.30 +static void /* PRIVATE */
    1.31 +png_default_warning PNGARG((png_const_structrp png_ptr,
    1.32 +   png_const_charp warning_message));
    1.33 +#endif /* PNG_WARNINGS_SUPPORTED */
    1.34 +
    1.35 +/* This function is called whenever there is a fatal error.  This function
    1.36 + * should not be changed.  If there is a need to handle errors differently,
    1.37 + * you should supply a replacement error function and use png_set_error_fn()
    1.38 + * to replace the error function at run-time.
    1.39 + */
    1.40 +#ifdef PNG_ERROR_TEXT_SUPPORTED
    1.41 +PNG_FUNCTION(void,PNGAPI
    1.42 +png_error,(png_const_structrp png_ptr, png_const_charp error_message),
    1.43 +   PNG_NORETURN)
    1.44 +{
    1.45 +#ifdef PNG_ERROR_NUMBERS_SUPPORTED
    1.46 +   char msg[16];
    1.47 +   if (png_ptr != NULL)
    1.48 +   {
    1.49 +      if (png_ptr->flags&
    1.50 +         (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))
    1.51 +      {
    1.52 +         if (*error_message == PNG_LITERAL_SHARP)
    1.53 +         {
    1.54 +            /* Strip "#nnnn " from beginning of error message. */
    1.55 +            int offset;
    1.56 +            for (offset = 1; offset<15; offset++)
    1.57 +               if (error_message[offset] == ' ')
    1.58 +                  break;
    1.59 +
    1.60 +            if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT)
    1.61 +            {
    1.62 +               int i;
    1.63 +               for (i = 0; i < offset - 1; i++)
    1.64 +                  msg[i] = error_message[i + 1];
    1.65 +               msg[i - 1] = '\0';
    1.66 +               error_message = msg;
    1.67 +            }
    1.68 +
    1.69 +            else
    1.70 +               error_message += offset;
    1.71 +      }
    1.72 +
    1.73 +      else
    1.74 +      {
    1.75 +         if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT)
    1.76 +         {
    1.77 +            msg[0] = '0';
    1.78 +            msg[1] = '\0';
    1.79 +            error_message = msg;
    1.80 +         }
    1.81 +       }
    1.82 +     }
    1.83 +   }
    1.84 +#endif
    1.85 +   if (png_ptr != NULL && png_ptr->error_fn != NULL)
    1.86 +      (*(png_ptr->error_fn))(png_constcast(png_structrp,png_ptr),
    1.87 +          error_message);
    1.88 +
    1.89 +   /* If the custom handler doesn't exist, or if it returns,
    1.90 +      use the default handler, which will not return. */
    1.91 +   png_default_error(png_ptr, error_message);
    1.92 +}
    1.93 +#else
    1.94 +PNG_FUNCTION(void,PNGAPI
    1.95 +png_err,(png_const_structrp png_ptr),PNG_NORETURN)
    1.96 +{
    1.97 +   /* Prior to 1.5.2 the error_fn received a NULL pointer, expressed
    1.98 +    * erroneously as '\0', instead of the empty string "".  This was
    1.99 +    * apparently an error, introduced in libpng-1.2.20, and png_default_error
   1.100 +    * will crash in this case.
   1.101 +    */
   1.102 +   if (png_ptr != NULL && png_ptr->error_fn != NULL)
   1.103 +      (*(png_ptr->error_fn))(png_constcast(png_structrp,png_ptr), "");
   1.104 +
   1.105 +   /* If the custom handler doesn't exist, or if it returns,
   1.106 +      use the default handler, which will not return. */
   1.107 +   png_default_error(png_ptr, "");
   1.108 +}
   1.109 +#endif /* PNG_ERROR_TEXT_SUPPORTED */
   1.110 +
   1.111 +/* Utility to safely appends strings to a buffer.  This never errors out so
   1.112 + * error checking is not required in the caller.
   1.113 + */
   1.114 +size_t
   1.115 +png_safecat(png_charp buffer, size_t bufsize, size_t pos,
   1.116 +   png_const_charp string)
   1.117 +{
   1.118 +   if (buffer != NULL && pos < bufsize)
   1.119 +   {
   1.120 +      if (string != NULL)
   1.121 +         while (*string != '\0' && pos < bufsize-1)
   1.122 +           buffer[pos++] = *string++;
   1.123 +
   1.124 +      buffer[pos] = '\0';
   1.125 +   }
   1.126 +
   1.127 +   return pos;
   1.128 +}
   1.129 +
   1.130 +#if defined(PNG_WARNINGS_SUPPORTED) || defined(PNG_TIME_RFC1123_SUPPORTED)
   1.131 +/* Utility to dump an unsigned value into a buffer, given a start pointer and
   1.132 + * and end pointer (which should point just *beyond* the end of the buffer!)
   1.133 + * Returns the pointer to the start of the formatted string.
   1.134 + */
   1.135 +png_charp
   1.136 +png_format_number(png_const_charp start, png_charp end, int format,
   1.137 +   png_alloc_size_t number)
   1.138 +{
   1.139 +   int count = 0;    /* number of digits output */
   1.140 +   int mincount = 1; /* minimum number required */
   1.141 +   int output = 0;   /* digit output (for the fixed point format) */
   1.142 +
   1.143 +   *--end = '\0';
   1.144 +
   1.145 +   /* This is written so that the loop always runs at least once, even with
   1.146 +    * number zero.
   1.147 +    */
   1.148 +   while (end > start && (number != 0 || count < mincount))
   1.149 +   {
   1.150 +
   1.151 +      static const char digits[] = "0123456789ABCDEF";
   1.152 +
   1.153 +      switch (format)
   1.154 +      {
   1.155 +         case PNG_NUMBER_FORMAT_fixed:
   1.156 +            /* Needs five digits (the fraction) */
   1.157 +            mincount = 5;
   1.158 +            if (output || number % 10 != 0)
   1.159 +            {
   1.160 +               *--end = digits[number % 10];
   1.161 +               output = 1;
   1.162 +            }
   1.163 +            number /= 10;
   1.164 +            break;
   1.165 +
   1.166 +         case PNG_NUMBER_FORMAT_02u:
   1.167 +            /* Expects at least 2 digits. */
   1.168 +            mincount = 2;
   1.169 +            /* FALL THROUGH */
   1.170 +
   1.171 +         case PNG_NUMBER_FORMAT_u:
   1.172 +            *--end = digits[number % 10];
   1.173 +            number /= 10;
   1.174 +            break;
   1.175 +
   1.176 +         case PNG_NUMBER_FORMAT_02x:
   1.177 +            /* This format expects at least two digits */
   1.178 +            mincount = 2;
   1.179 +            /* FALL THROUGH */
   1.180 +
   1.181 +         case PNG_NUMBER_FORMAT_x:
   1.182 +            *--end = digits[number & 0xf];
   1.183 +            number >>= 4;
   1.184 +            break;
   1.185 +
   1.186 +         default: /* an error */
   1.187 +            number = 0;
   1.188 +            break;
   1.189 +      }
   1.190 +
   1.191 +      /* Keep track of the number of digits added */
   1.192 +      ++count;
   1.193 +
   1.194 +      /* Float a fixed number here: */
   1.195 +      if (format == PNG_NUMBER_FORMAT_fixed) if (count == 5) if (end > start)
   1.196 +      {
   1.197 +         /* End of the fraction, but maybe nothing was output?  In that case
   1.198 +          * drop the decimal point.  If the number is a true zero handle that
   1.199 +          * here.
   1.200 +          */
   1.201 +         if (output)
   1.202 +            *--end = '.';
   1.203 +         else if (number == 0) /* and !output */
   1.204 +            *--end = '0';
   1.205 +      }
   1.206 +   }
   1.207 +
   1.208 +   return end;
   1.209 +}
   1.210 +#endif
   1.211 +
   1.212 +#ifdef PNG_WARNINGS_SUPPORTED
   1.213 +/* This function is called whenever there is a non-fatal error.  This function
   1.214 + * should not be changed.  If there is a need to handle warnings differently,
   1.215 + * you should supply a replacement warning function and use
   1.216 + * png_set_error_fn() to replace the warning function at run-time.
   1.217 + */
   1.218 +void PNGAPI
   1.219 +png_warning(png_const_structrp png_ptr, png_const_charp warning_message)
   1.220 +{
   1.221 +   int offset = 0;
   1.222 +   if (png_ptr != NULL)
   1.223 +   {
   1.224 +#ifdef PNG_ERROR_NUMBERS_SUPPORTED
   1.225 +   if (png_ptr->flags&
   1.226 +       (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))
   1.227 +#endif
   1.228 +      {
   1.229 +         if (*warning_message == PNG_LITERAL_SHARP)
   1.230 +         {
   1.231 +            for (offset = 1; offset < 15; offset++)
   1.232 +               if (warning_message[offset] == ' ')
   1.233 +                  break;
   1.234 +         }
   1.235 +      }
   1.236 +   }
   1.237 +   if (png_ptr != NULL && png_ptr->warning_fn != NULL)
   1.238 +      (*(png_ptr->warning_fn))(png_constcast(png_structrp,png_ptr),
   1.239 +         warning_message + offset);
   1.240 +   else
   1.241 +      png_default_warning(png_ptr, warning_message + offset);
   1.242 +}
   1.243 +
   1.244 +/* These functions support 'formatted' warning messages with up to
   1.245 + * PNG_WARNING_PARAMETER_COUNT parameters.  In the format string the parameter
   1.246 + * is introduced by @<number>, where 'number' starts at 1.  This follows the
   1.247 + * standard established by X/Open for internationalizable error messages.
   1.248 + */
   1.249 +void
   1.250 +png_warning_parameter(png_warning_parameters p, int number,
   1.251 +   png_const_charp string)
   1.252 +{
   1.253 +   if (number > 0 && number <= PNG_WARNING_PARAMETER_COUNT)
   1.254 +      (void)png_safecat(p[number-1], (sizeof p[number-1]), 0, string);
   1.255 +}
   1.256 +
   1.257 +void
   1.258 +png_warning_parameter_unsigned(png_warning_parameters p, int number, int format,
   1.259 +   png_alloc_size_t value)
   1.260 +{
   1.261 +   char buffer[PNG_NUMBER_BUFFER_SIZE];
   1.262 +   png_warning_parameter(p, number, PNG_FORMAT_NUMBER(buffer, format, value));
   1.263 +}
   1.264 +
   1.265 +void
   1.266 +png_warning_parameter_signed(png_warning_parameters p, int number, int format,
   1.267 +   png_int_32 value)
   1.268 +{
   1.269 +   png_alloc_size_t u;
   1.270 +   png_charp str;
   1.271 +   char buffer[PNG_NUMBER_BUFFER_SIZE];
   1.272 +
   1.273 +   /* Avoid overflow by doing the negate in a png_alloc_size_t: */
   1.274 +   u = (png_alloc_size_t)value;
   1.275 +   if (value < 0)
   1.276 +      u = ~u + 1;
   1.277 +
   1.278 +   str = PNG_FORMAT_NUMBER(buffer, format, u);
   1.279 +
   1.280 +   if (value < 0 && str > buffer)
   1.281 +      *--str = '-';
   1.282 +
   1.283 +   png_warning_parameter(p, number, str);
   1.284 +}
   1.285 +
   1.286 +void
   1.287 +png_formatted_warning(png_const_structrp png_ptr, png_warning_parameters p,
   1.288 +   png_const_charp message)
   1.289 +{
   1.290 +   /* The internal buffer is just 192 bytes - enough for all our messages,
   1.291 +    * overflow doesn't happen because this code checks!  If someone figures
   1.292 +    * out how to send us a message longer than 192 bytes, all that will
   1.293 +    * happen is that the message will be truncated appropriately.
   1.294 +    */
   1.295 +   size_t i = 0; /* Index in the msg[] buffer: */
   1.296 +   char msg[192];
   1.297 +
   1.298 +   /* Each iteration through the following loop writes at most one character
   1.299 +    * to msg[i++] then returns here to validate that there is still space for
   1.300 +    * the trailing '\0'.  It may (in the case of a parameter) read more than
   1.301 +    * one character from message[]; it must check for '\0' and continue to the
   1.302 +    * test if it finds the end of string.
   1.303 +    */
   1.304 +   while (i<(sizeof msg)-1 && *message != '\0')
   1.305 +   {
   1.306 +      /* '@' at end of string is now just printed (previously it was skipped);
   1.307 +       * it is an error in the calling code to terminate the string with @.
   1.308 +       */
   1.309 +      if (p != NULL && *message == '@' && message[1] != '\0')
   1.310 +      {
   1.311 +         int parameter_char = *++message; /* Consume the '@' */
   1.312 +         static const char valid_parameters[] = "123456789";
   1.313 +         int parameter = 0;
   1.314 +
   1.315 +         /* Search for the parameter digit, the index in the string is the
   1.316 +          * parameter to use.
   1.317 +          */
   1.318 +         while (valid_parameters[parameter] != parameter_char &&
   1.319 +            valid_parameters[parameter] != '\0')
   1.320 +            ++parameter;
   1.321 +
   1.322 +         /* If the parameter digit is out of range it will just get printed. */
   1.323 +         if (parameter < PNG_WARNING_PARAMETER_COUNT)
   1.324 +         {
   1.325 +            /* Append this parameter */
   1.326 +            png_const_charp parm = p[parameter];
   1.327 +            png_const_charp pend = p[parameter] + (sizeof p[parameter]);
   1.328 +
   1.329 +            /* No need to copy the trailing '\0' here, but there is no guarantee
   1.330 +             * that parm[] has been initialized, so there is no guarantee of a
   1.331 +             * trailing '\0':
   1.332 +             */
   1.333 +            while (i<(sizeof msg)-1 && *parm != '\0' && parm < pend)
   1.334 +               msg[i++] = *parm++;
   1.335 +
   1.336 +            /* Consume the parameter digit too: */
   1.337 +            ++message;
   1.338 +            continue;
   1.339 +         }
   1.340 +
   1.341 +         /* else not a parameter and there is a character after the @ sign; just
   1.342 +          * copy that.  This is known not to be '\0' because of the test above.
   1.343 +          */
   1.344 +      }
   1.345 +
   1.346 +      /* At this point *message can't be '\0', even in the bad parameter case
   1.347 +       * above where there is a lone '@' at the end of the message string.
   1.348 +       */
   1.349 +      msg[i++] = *message++;
   1.350 +   }
   1.351 +
   1.352 +   /* i is always less than (sizeof msg), so: */
   1.353 +   msg[i] = '\0';
   1.354 +
   1.355 +   /* And this is the formatted message. It may be larger than
   1.356 +    * PNG_MAX_ERROR_TEXT, but that is only used for 'chunk' errors and these
   1.357 +    * are not (currently) formatted.
   1.358 +    */
   1.359 +   png_warning(png_ptr, msg);
   1.360 +}
   1.361 +#endif /* PNG_WARNINGS_SUPPORTED */
   1.362 +
   1.363 +#ifdef PNG_BENIGN_ERRORS_SUPPORTED
   1.364 +void PNGAPI
   1.365 +png_benign_error(png_const_structrp png_ptr, png_const_charp error_message)
   1.366 +{
   1.367 +   if (png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN)
   1.368 +   {
   1.369 +#     ifdef PNG_READ_SUPPORTED
   1.370 +         if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0 &&
   1.371 +            png_ptr->chunk_name != 0)
   1.372 +            png_chunk_warning(png_ptr, error_message);
   1.373 +         else
   1.374 +#     endif
   1.375 +      png_warning(png_ptr, error_message);
   1.376 +   }
   1.377 +
   1.378 +   else
   1.379 +   {
   1.380 +#     ifdef PNG_READ_SUPPORTED
   1.381 +         if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0 &&
   1.382 +            png_ptr->chunk_name != 0)
   1.383 +            png_chunk_error(png_ptr, error_message);
   1.384 +         else
   1.385 +#     endif
   1.386 +      png_error(png_ptr, error_message);
   1.387 +   }
   1.388 +
   1.389 +#  ifndef PNG_ERROR_TEXT_SUPPORTED
   1.390 +      PNG_UNUSED(error_message)
   1.391 +#  endif
   1.392 +}
   1.393 +
   1.394 +void /* PRIVATE */
   1.395 +png_app_warning(png_const_structrp png_ptr, png_const_charp error_message)
   1.396 +{
   1.397 +  if (png_ptr->flags & PNG_FLAG_APP_WARNINGS_WARN)
   1.398 +     png_warning(png_ptr, error_message);
   1.399 +  else
   1.400 +     png_error(png_ptr, error_message);
   1.401 +
   1.402 +#  ifndef PNG_ERROR_TEXT_SUPPORTED
   1.403 +      PNG_UNUSED(error_message)
   1.404 +#  endif
   1.405 +}
   1.406 +
   1.407 +void /* PRIVATE */
   1.408 +png_app_error(png_const_structrp png_ptr, png_const_charp error_message)
   1.409 +{
   1.410 +  if (png_ptr->flags & PNG_FLAG_APP_ERRORS_WARN)
   1.411 +     png_warning(png_ptr, error_message);
   1.412 +  else
   1.413 +     png_error(png_ptr, error_message);
   1.414 +
   1.415 +#  ifndef PNG_ERROR_TEXT_SUPPORTED
   1.416 +      PNG_UNUSED(error_message)
   1.417 +#  endif
   1.418 +}
   1.419 +#endif /* BENIGN_ERRORS */
   1.420 +
   1.421 +/* These utilities are used internally to build an error message that relates
   1.422 + * to the current chunk.  The chunk name comes from png_ptr->chunk_name,
   1.423 + * this is used to prefix the message.  The message is limited in length
   1.424 + * to 63 bytes, the name characters are output as hex digits wrapped in []
   1.425 + * if the character is invalid.
   1.426 + */
   1.427 +#define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
   1.428 +static PNG_CONST char png_digit[16] = {
   1.429 +   '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
   1.430 +   'A', 'B', 'C', 'D', 'E', 'F'
   1.431 +};
   1.432 +
   1.433 +#define PNG_MAX_ERROR_TEXT 196 /* Currently limited be profile_error in png.c */
   1.434 +#if defined(PNG_WARNINGS_SUPPORTED) || \
   1.435 +   (defined(PNG_READ_SUPPORTED) && defined(PNG_ERROR_TEXT_SUPPORTED))
   1.436 +static void /* PRIVATE */
   1.437 +png_format_buffer(png_const_structrp png_ptr, png_charp buffer, png_const_charp
   1.438 +    error_message)
   1.439 +{
   1.440 +   png_uint_32 chunk_name = png_ptr->chunk_name;
   1.441 +   int iout = 0, ishift = 24;
   1.442 +
   1.443 +   while (ishift >= 0)
   1.444 +   {
   1.445 +      int c = (int)(chunk_name >> ishift) & 0xff;
   1.446 +
   1.447 +      ishift -= 8;
   1.448 +      if (isnonalpha(c))
   1.449 +      {
   1.450 +         buffer[iout++] = PNG_LITERAL_LEFT_SQUARE_BRACKET;
   1.451 +         buffer[iout++] = png_digit[(c & 0xf0) >> 4];
   1.452 +         buffer[iout++] = png_digit[c & 0x0f];
   1.453 +         buffer[iout++] = PNG_LITERAL_RIGHT_SQUARE_BRACKET;
   1.454 +      }
   1.455 +
   1.456 +      else
   1.457 +      {
   1.458 +         buffer[iout++] = (char)c;
   1.459 +      }
   1.460 +   }
   1.461 +
   1.462 +   if (error_message == NULL)
   1.463 +      buffer[iout] = '\0';
   1.464 +
   1.465 +   else
   1.466 +   {
   1.467 +      int iin = 0;
   1.468 +
   1.469 +      buffer[iout++] = ':';
   1.470 +      buffer[iout++] = ' ';
   1.471 +
   1.472 +      while (iin < PNG_MAX_ERROR_TEXT-1 && error_message[iin] != '\0')
   1.473 +         buffer[iout++] = error_message[iin++];
   1.474 +
   1.475 +      /* iin < PNG_MAX_ERROR_TEXT, so the following is safe: */
   1.476 +      buffer[iout] = '\0';
   1.477 +   }
   1.478 +}
   1.479 +#endif /* PNG_WARNINGS_SUPPORTED || PNG_ERROR_TEXT_SUPPORTED */
   1.480 +
   1.481 +#if defined(PNG_READ_SUPPORTED) && defined(PNG_ERROR_TEXT_SUPPORTED)
   1.482 +PNG_FUNCTION(void,PNGAPI
   1.483 +png_chunk_error,(png_const_structrp png_ptr, png_const_charp error_message),
   1.484 +   PNG_NORETURN)
   1.485 +{
   1.486 +   char msg[18+PNG_MAX_ERROR_TEXT];
   1.487 +   if (png_ptr == NULL)
   1.488 +      png_error(png_ptr, error_message);
   1.489 +
   1.490 +   else
   1.491 +   {
   1.492 +      png_format_buffer(png_ptr, msg, error_message);
   1.493 +      png_error(png_ptr, msg);
   1.494 +   }
   1.495 +}
   1.496 +#endif /* PNG_READ_SUPPORTED && PNG_ERROR_TEXT_SUPPORTED */
   1.497 +
   1.498 +#ifdef PNG_WARNINGS_SUPPORTED
   1.499 +void PNGAPI
   1.500 +png_chunk_warning(png_const_structrp png_ptr, png_const_charp warning_message)
   1.501 +{
   1.502 +   char msg[18+PNG_MAX_ERROR_TEXT];
   1.503 +   if (png_ptr == NULL)
   1.504 +      png_warning(png_ptr, warning_message);
   1.505 +
   1.506 +   else
   1.507 +   {
   1.508 +      png_format_buffer(png_ptr, msg, warning_message);
   1.509 +      png_warning(png_ptr, msg);
   1.510 +   }
   1.511 +}
   1.512 +#endif /* PNG_WARNINGS_SUPPORTED */
   1.513 +
   1.514 +#ifdef PNG_READ_SUPPORTED
   1.515 +#ifdef PNG_BENIGN_ERRORS_SUPPORTED
   1.516 +void PNGAPI
   1.517 +png_chunk_benign_error(png_const_structrp png_ptr, png_const_charp
   1.518 +    error_message)
   1.519 +{
   1.520 +   if (png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN)
   1.521 +      png_chunk_warning(png_ptr, error_message);
   1.522 +
   1.523 +   else
   1.524 +      png_chunk_error(png_ptr, error_message);
   1.525 +
   1.526 +#  ifndef PNG_ERROR_TEXT_SUPPORTED
   1.527 +      PNG_UNUSED(error_message)
   1.528 +#  endif
   1.529 +}
   1.530 +#endif
   1.531 +#endif /* PNG_READ_SUPPORTED */
   1.532 +
   1.533 +void /* PRIVATE */
   1.534 +png_chunk_report(png_const_structrp png_ptr, png_const_charp message, int error)
   1.535 +{
   1.536 +#  ifndef PNG_WARNINGS_SUPPORTED
   1.537 +      PNG_UNUSED(message)
   1.538 +#  endif
   1.539 +
   1.540 +   /* This is always supported, but for just read or just write it
   1.541 +    * unconditionally does the right thing.
   1.542 +    */
   1.543 +#  if defined(PNG_READ_SUPPORTED) && defined(PNG_WRITE_SUPPORTED)
   1.544 +      if (png_ptr->mode & PNG_IS_READ_STRUCT)
   1.545 +#  endif
   1.546 +
   1.547 +#  ifdef PNG_READ_SUPPORTED
   1.548 +      {
   1.549 +         if (error < PNG_CHUNK_ERROR)
   1.550 +            png_chunk_warning(png_ptr, message);
   1.551 +
   1.552 +         else
   1.553 +            png_chunk_benign_error(png_ptr, message);
   1.554 +      }
   1.555 +#  endif
   1.556 +
   1.557 +#  if defined(PNG_READ_SUPPORTED) && defined(PNG_WRITE_SUPPORTED)
   1.558 +      else if (!(png_ptr->mode & PNG_IS_READ_STRUCT))
   1.559 +#  endif
   1.560 +
   1.561 +#  ifdef PNG_WRITE_SUPPORTED
   1.562 +      {
   1.563 +         if (error < PNG_CHUNK_WRITE_ERROR)
   1.564 +            png_app_warning(png_ptr, message);
   1.565 +
   1.566 +         else
   1.567 +            png_app_error(png_ptr, message);
   1.568 +      }
   1.569 +#  endif
   1.570 +}
   1.571 +
   1.572 +#ifdef PNG_ERROR_TEXT_SUPPORTED
   1.573 +#ifdef PNG_FLOATING_POINT_SUPPORTED
   1.574 +PNG_FUNCTION(void,
   1.575 +png_fixed_error,(png_const_structrp png_ptr, png_const_charp name),PNG_NORETURN)
   1.576 +{
   1.577 +#  define fixed_message "fixed point overflow in "
   1.578 +#  define fixed_message_ln ((sizeof fixed_message)-1)
   1.579 +   int  iin;
   1.580 +   char msg[fixed_message_ln+PNG_MAX_ERROR_TEXT];
   1.581 +   memcpy(msg, fixed_message, fixed_message_ln);
   1.582 +   iin = 0;
   1.583 +   if (name != NULL) while (iin < (PNG_MAX_ERROR_TEXT-1) && name[iin] != 0)
   1.584 +   {
   1.585 +      msg[fixed_message_ln + iin] = name[iin];
   1.586 +      ++iin;
   1.587 +   }
   1.588 +   msg[fixed_message_ln + iin] = 0;
   1.589 +   png_error(png_ptr, msg);
   1.590 +}
   1.591 +#endif
   1.592 +#endif
   1.593 +
   1.594 +#ifdef PNG_SETJMP_SUPPORTED
   1.595 +/* This API only exists if ANSI-C style error handling is used,
   1.596 + * otherwise it is necessary for png_default_error to be overridden.
   1.597 + */
   1.598 +jmp_buf* PNGAPI
   1.599 +png_set_longjmp_fn(png_structrp png_ptr, png_longjmp_ptr longjmp_fn,
   1.600 +    size_t jmp_buf_size)
   1.601 +{
   1.602 +   /* From libpng 1.6.0 the app gets one chance to set a 'jmpbuf_size' value
   1.603 +    * and it must not change after that.  Libpng doesn't care how big the
   1.604 +    * buffer is, just that it doesn't change.
   1.605 +    *
   1.606 +    * If the buffer size is no *larger* than the size of jmp_buf when libpng is
   1.607 +    * compiled a built in jmp_buf is returned; this preserves the pre-1.6.0
   1.608 +    * semantics that this call will not fail.  If the size is larger, however,
   1.609 +    * the buffer is allocated and this may fail, causing the function to return
   1.610 +    * NULL.
   1.611 +    */
   1.612 +   if (png_ptr == NULL)
   1.613 +      return NULL;
   1.614 +
   1.615 +   if (png_ptr->jmp_buf_ptr == NULL)
   1.616 +   {
   1.617 +      png_ptr->jmp_buf_size = 0; /* not allocated */
   1.618 +
   1.619 +      if (jmp_buf_size <= (sizeof png_ptr->jmp_buf_local))
   1.620 +         png_ptr->jmp_buf_ptr = &png_ptr->jmp_buf_local;
   1.621 +
   1.622 +      else
   1.623 +      {
   1.624 +         png_ptr->jmp_buf_ptr = png_voidcast(jmp_buf *,
   1.625 +            png_malloc_warn(png_ptr, jmp_buf_size));
   1.626 +
   1.627 +         if (png_ptr->jmp_buf_ptr == NULL)
   1.628 +            return NULL; /* new NULL return on OOM */
   1.629 +
   1.630 +         png_ptr->jmp_buf_size = jmp_buf_size;
   1.631 +      }
   1.632 +   }
   1.633 +
   1.634 +   else /* Already allocated: check the size */
   1.635 +   {
   1.636 +      size_t size = png_ptr->jmp_buf_size;
   1.637 +
   1.638 +      if (size == 0)
   1.639 +      {
   1.640 +         size = (sizeof png_ptr->jmp_buf_local);
   1.641 +         if (png_ptr->jmp_buf_ptr != &png_ptr->jmp_buf_local)
   1.642 +         {
   1.643 +            /* This is an internal error in libpng: somehow we have been left
   1.644 +             * with a stack allocated jmp_buf when the application regained
   1.645 +             * control.  It's always possible to fix this up, but for the moment
   1.646 +             * this is a png_error because that makes it easy to detect.
   1.647 +             */
   1.648 +            png_error(png_ptr, "Libpng jmp_buf still allocated");
   1.649 +            /* png_ptr->jmp_buf_ptr = &png_ptr->jmp_buf_local; */
   1.650 +         }
   1.651 +      }
   1.652 +
   1.653 +      if (size != jmp_buf_size)
   1.654 +      {
   1.655 +         png_warning(png_ptr, "Application jmp_buf size changed");
   1.656 +         return NULL; /* caller will probably crash: no choice here */
   1.657 +      }
   1.658 +   }
   1.659 +
   1.660 +   /* Finally fill in the function, now we have a satisfactory buffer. It is
   1.661 +    * valid to change the function on every call.
   1.662 +    */
   1.663 +   png_ptr->longjmp_fn = longjmp_fn;
   1.664 +   return png_ptr->jmp_buf_ptr;
   1.665 +}
   1.666 +
   1.667 +void /* PRIVATE */
   1.668 +png_free_jmpbuf(png_structrp png_ptr)
   1.669 +{
   1.670 +   if (png_ptr != NULL)
   1.671 +   {
   1.672 +      jmp_buf *jb = png_ptr->jmp_buf_ptr;
   1.673 +
   1.674 +      /* A size of 0 is used to indicate a local, stack, allocation of the
   1.675 +       * pointer; used here and in png.c
   1.676 +       */
   1.677 +      if (jb != NULL && png_ptr->jmp_buf_size > 0)
   1.678 +      {
   1.679 +
   1.680 +         /* This stuff is so that a failure to free the error control structure
   1.681 +          * does not leave libpng in a state with no valid error handling: the
   1.682 +          * free always succeeds, if there is an error it gets ignored.
   1.683 +          */
   1.684 +         if (jb != &png_ptr->jmp_buf_local)
   1.685 +         {
   1.686 +            /* Make an internal, libpng, jmp_buf to return here */
   1.687 +            jmp_buf free_jmp_buf;
   1.688 +
   1.689 +            if (!setjmp(free_jmp_buf))
   1.690 +            {
   1.691 +               png_ptr->jmp_buf_ptr = &free_jmp_buf; /* come back here */
   1.692 +               png_ptr->jmp_buf_size = 0; /* stack allocation */
   1.693 +               png_ptr->longjmp_fn = longjmp;
   1.694 +               png_free(png_ptr, jb); /* Return to setjmp on error */
   1.695 +            }
   1.696 +         }
   1.697 +      }
   1.698 +
   1.699 +      /* *Always* cancel everything out: */
   1.700 +      png_ptr->jmp_buf_size = 0;
   1.701 +      png_ptr->jmp_buf_ptr = NULL;
   1.702 +      png_ptr->longjmp_fn = 0;
   1.703 +   }
   1.704 +}
   1.705 +#endif
   1.706 +
   1.707 +/* This is the default error handling function.  Note that replacements for
   1.708 + * this function MUST NOT RETURN, or the program will likely crash.  This
   1.709 + * function is used by default, or if the program supplies NULL for the
   1.710 + * error function pointer in png_set_error_fn().
   1.711 + */
   1.712 +static PNG_FUNCTION(void /* PRIVATE */,
   1.713 +png_default_error,(png_const_structrp png_ptr, png_const_charp error_message),
   1.714 +   PNG_NORETURN)
   1.715 +{
   1.716 +#ifdef PNG_CONSOLE_IO_SUPPORTED
   1.717 +#ifdef PNG_ERROR_NUMBERS_SUPPORTED
   1.718 +   /* Check on NULL only added in 1.5.4 */
   1.719 +   if (error_message != NULL && *error_message == PNG_LITERAL_SHARP)
   1.720 +   {
   1.721 +      /* Strip "#nnnn " from beginning of error message. */
   1.722 +      int offset;
   1.723 +      char error_number[16];
   1.724 +      for (offset = 0; offset<15; offset++)
   1.725 +      {
   1.726 +         error_number[offset] = error_message[offset + 1];
   1.727 +         if (error_message[offset] == ' ')
   1.728 +            break;
   1.729 +      }
   1.730 +
   1.731 +      if ((offset > 1) && (offset < 15))
   1.732 +      {
   1.733 +         error_number[offset - 1] = '\0';
   1.734 +         fprintf(stderr, "libpng error no. %s: %s",
   1.735 +             error_number, error_message + offset + 1);
   1.736 +         fprintf(stderr, PNG_STRING_NEWLINE);
   1.737 +      }
   1.738 +
   1.739 +      else
   1.740 +      {
   1.741 +         fprintf(stderr, "libpng error: %s, offset=%d",
   1.742 +             error_message, offset);
   1.743 +         fprintf(stderr, PNG_STRING_NEWLINE);
   1.744 +      }
   1.745 +   }
   1.746 +   else
   1.747 +#endif
   1.748 +   {
   1.749 +      fprintf(stderr, "libpng error: %s", error_message ? error_message :
   1.750 +         "undefined");
   1.751 +      fprintf(stderr, PNG_STRING_NEWLINE);
   1.752 +   }
   1.753 +#else
   1.754 +   PNG_UNUSED(error_message) /* Make compiler happy */
   1.755 +#endif
   1.756 +   png_longjmp(png_ptr, 1);
   1.757 +}
   1.758 +
   1.759 +PNG_FUNCTION(void,PNGAPI
   1.760 +png_longjmp,(png_const_structrp png_ptr, int val),PNG_NORETURN)
   1.761 +{
   1.762 +#ifdef PNG_SETJMP_SUPPORTED
   1.763 +   if (png_ptr && png_ptr->longjmp_fn && png_ptr->jmp_buf_ptr)
   1.764 +      png_ptr->longjmp_fn(*png_ptr->jmp_buf_ptr, val);
   1.765 +#endif
   1.766 +
   1.767 +   /* If control reaches this point, png_longjmp() must not return. The only
   1.768 +    * choice is to terminate the whole process (or maybe the thread); to do
   1.769 +    * this the ANSI-C abort() function is used unless a different method is 
   1.770 +    * implemented by overriding the default configuration setting for
   1.771 +    * PNG_ABORT().
   1.772 +    */
   1.773 +   PNG_ABORT();
   1.774 +}
   1.775 +
   1.776 +#ifdef PNG_WARNINGS_SUPPORTED
   1.777 +/* This function is called when there is a warning, but the library thinks
   1.778 + * it can continue anyway.  Replacement functions don't have to do anything
   1.779 + * here if you don't want them to.  In the default configuration, png_ptr is
   1.780 + * not used, but it is passed in case it may be useful.
   1.781 + */
   1.782 +static void /* PRIVATE */
   1.783 +png_default_warning(png_const_structrp png_ptr, png_const_charp warning_message)
   1.784 +{
   1.785 +#ifdef PNG_CONSOLE_IO_SUPPORTED
   1.786 +#  ifdef PNG_ERROR_NUMBERS_SUPPORTED
   1.787 +   if (*warning_message == PNG_LITERAL_SHARP)
   1.788 +   {
   1.789 +      int offset;
   1.790 +      char warning_number[16];
   1.791 +      for (offset = 0; offset < 15; offset++)
   1.792 +      {
   1.793 +         warning_number[offset] = warning_message[offset + 1];
   1.794 +         if (warning_message[offset] == ' ')
   1.795 +            break;
   1.796 +      }
   1.797 +
   1.798 +      if ((offset > 1) && (offset < 15))
   1.799 +      {
   1.800 +         warning_number[offset + 1] = '\0';
   1.801 +         fprintf(stderr, "libpng warning no. %s: %s",
   1.802 +             warning_number, warning_message + offset);
   1.803 +         fprintf(stderr, PNG_STRING_NEWLINE);
   1.804 +      }
   1.805 +
   1.806 +      else
   1.807 +      {
   1.808 +         fprintf(stderr, "libpng warning: %s",
   1.809 +             warning_message);
   1.810 +         fprintf(stderr, PNG_STRING_NEWLINE);
   1.811 +      }
   1.812 +   }
   1.813 +   else
   1.814 +#  endif
   1.815 +
   1.816 +   {
   1.817 +      fprintf(stderr, "libpng warning: %s", warning_message);
   1.818 +      fprintf(stderr, PNG_STRING_NEWLINE);
   1.819 +   }
   1.820 +#else
   1.821 +   PNG_UNUSED(warning_message) /* Make compiler happy */
   1.822 +#endif
   1.823 +   PNG_UNUSED(png_ptr) /* Make compiler happy */
   1.824 +}
   1.825 +#endif /* PNG_WARNINGS_SUPPORTED */
   1.826 +
   1.827 +/* This function is called when the application wants to use another method
   1.828 + * of handling errors and warnings.  Note that the error function MUST NOT
   1.829 + * return to the calling routine or serious problems will occur.  The return
   1.830 + * method used in the default routine calls longjmp(png_ptr->jmp_buf_ptr, 1)
   1.831 + */
   1.832 +void PNGAPI
   1.833 +png_set_error_fn(png_structrp png_ptr, png_voidp error_ptr,
   1.834 +    png_error_ptr error_fn, png_error_ptr warning_fn)
   1.835 +{
   1.836 +   if (png_ptr == NULL)
   1.837 +      return;
   1.838 +
   1.839 +   png_ptr->error_ptr = error_ptr;
   1.840 +   png_ptr->error_fn = error_fn;
   1.841 +#ifdef PNG_WARNINGS_SUPPORTED
   1.842 +   png_ptr->warning_fn = warning_fn;
   1.843 +#else
   1.844 +   PNG_UNUSED(warning_fn)
   1.845 +#endif
   1.846 +}
   1.847 +
   1.848 +
   1.849 +/* This function returns a pointer to the error_ptr associated with the user
   1.850 + * functions.  The application should free any memory associated with this
   1.851 + * pointer before png_write_destroy and png_read_destroy are called.
   1.852 + */
   1.853 +png_voidp PNGAPI
   1.854 +png_get_error_ptr(png_const_structrp png_ptr)
   1.855 +{
   1.856 +   if (png_ptr == NULL)
   1.857 +      return NULL;
   1.858 +
   1.859 +   return ((png_voidp)png_ptr->error_ptr);
   1.860 +}
   1.861 +
   1.862 +
   1.863 +#ifdef PNG_ERROR_NUMBERS_SUPPORTED
   1.864 +void PNGAPI
   1.865 +png_set_strip_error_numbers(png_structrp png_ptr, png_uint_32 strip_mode)
   1.866 +{
   1.867 +   if (png_ptr != NULL)
   1.868 +   {
   1.869 +      png_ptr->flags &=
   1.870 +         ((~(PNG_FLAG_STRIP_ERROR_NUMBERS |
   1.871 +         PNG_FLAG_STRIP_ERROR_TEXT))&strip_mode);
   1.872 +   }
   1.873 +}
   1.874 +#endif
   1.875 +
   1.876 +#if defined(PNG_SIMPLIFIED_READ_SUPPORTED) ||\
   1.877 +   defined(PNG_SIMPLIFIED_WRITE_SUPPORTED)
   1.878 +   /* Currently the above both depend on SETJMP_SUPPORTED, however it would be
   1.879 +    * possible to implement without setjmp support just so long as there is some
   1.880 +    * way to handle the error return here:
   1.881 +    */
   1.882 +PNG_FUNCTION(void /* PRIVATE */, (PNGCBAPI
   1.883 +png_safe_error),(png_structp png_nonconst_ptr, png_const_charp error_message),
   1.884 +   PNG_NORETURN)
   1.885 +{
   1.886 +   const png_const_structrp png_ptr = png_nonconst_ptr;
   1.887 +   png_imagep image = png_voidcast(png_imagep, png_ptr->error_ptr);
   1.888 +
   1.889 +   /* An error is always logged here, overwriting anything (typically a warning)
   1.890 +    * that is already there:
   1.891 +    */
   1.892 +   if (image != NULL)
   1.893 +   {
   1.894 +      png_safecat(image->message, (sizeof image->message), 0, error_message);
   1.895 +      image->warning_or_error |= PNG_IMAGE_ERROR;
   1.896 +
   1.897 +      /* Retrieve the jmp_buf from within the png_control, making this work for
   1.898 +       * C++ compilation too is pretty tricky: C++ wants a pointer to the first
   1.899 +       * element of a jmp_buf, but C doesn't tell us the type of that.
   1.900 +       */
   1.901 +      if (image->opaque != NULL && image->opaque->error_buf != NULL)
   1.902 +         longjmp(png_control_jmp_buf(image->opaque), 1);
   1.903 +
   1.904 +      /* Missing longjmp buffer, the following is to help debugging: */
   1.905 +      {
   1.906 +         size_t pos = png_safecat(image->message, (sizeof image->message), 0,
   1.907 +            "bad longjmp: ");
   1.908 +         png_safecat(image->message, (sizeof image->message), pos,
   1.909 +             error_message);
   1.910 +      }
   1.911 +   }
   1.912 +
   1.913 +   /* Here on an internal programming error. */
   1.914 +   abort();
   1.915 +}
   1.916 +
   1.917 +#ifdef PNG_WARNINGS_SUPPORTED
   1.918 +void /* PRIVATE */ PNGCBAPI
   1.919 +png_safe_warning(png_structp png_nonconst_ptr, png_const_charp warning_message)
   1.920 +{
   1.921 +   const png_const_structrp png_ptr = png_nonconst_ptr;
   1.922 +   png_imagep image = png_voidcast(png_imagep, png_ptr->error_ptr);
   1.923 +
   1.924 +   /* A warning is only logged if there is no prior warning or error. */
   1.925 +   if (image->warning_or_error == 0)
   1.926 +   {
   1.927 +      png_safecat(image->message, (sizeof image->message), 0, warning_message);
   1.928 +      image->warning_or_error |= PNG_IMAGE_WARNING;
   1.929 +   }
   1.930 +}
   1.931 +#endif
   1.932 +
   1.933 +int /* PRIVATE */
   1.934 +png_safe_execute(png_imagep image_in, int (*function)(png_voidp), png_voidp arg)
   1.935 +{
   1.936 +   volatile png_imagep image = image_in;
   1.937 +   volatile int result;
   1.938 +   volatile png_voidp saved_error_buf;
   1.939 +   jmp_buf safe_jmpbuf;
   1.940 +
   1.941 +   /* Safely execute function(arg) with png_error returning to this function. */
   1.942 +   saved_error_buf = image->opaque->error_buf;
   1.943 +   result = setjmp(safe_jmpbuf) == 0;
   1.944 +
   1.945 +   if (result)
   1.946 +   {
   1.947 +
   1.948 +      image->opaque->error_buf = safe_jmpbuf;
   1.949 +      result = function(arg);
   1.950 +   }
   1.951 +
   1.952 +   image->opaque->error_buf = saved_error_buf;
   1.953 +
   1.954 +   /* And do the cleanup prior to any failure return. */
   1.955 +   if (!result)
   1.956 +      png_image_free(image);
   1.957 +
   1.958 +   return result;
   1.959 +}
   1.960 +#endif /* SIMPLIFIED READ/WRITE */
   1.961 +#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */

mercurial