src/3rdparty/libpng/pngerror.c
branchGCC_SURGE
changeset 31 5daf16870df6
parent 30 5dc02b23752f
equal deleted inserted replaced
27:93b982ccede2 31:5daf16870df6
     1 
     1 
     2 /* pngerror.c - stub functions for i/o and memory allocation
     2 /* pngerror.c - stub functions for i/o and memory allocation
     3  *
     3  *
     4  * Last changed in libpng 1.2.37 [June 4, 2009]
     4  * Last changed in libpng 1.4.0 [January 3, 2010]
     5  * Copyright (c) 1998-2009 Glenn Randers-Pehrson
     5  * Copyright (c) 1998-2010 Glenn Randers-Pehrson
     6  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
     6  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
     7  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
     7  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
     8  *
     8  *
     9  * This code is released under the libpng license.
     9  * This code is released under the libpng license.
    10  * For conditions of distribution and use, see the disclaimer
    10  * For conditions of distribution and use, see the disclaimer
    14  * need special error handling are expected to write replacement functions
    14  * need special error handling are expected to write replacement functions
    15  * and use png_set_error_fn() to use those functions.  See the instructions
    15  * and use png_set_error_fn() to use those functions.  See the instructions
    16  * at each function.
    16  * at each function.
    17  */
    17  */
    18 
    18 
    19 #define PNG_INTERNAL
    19 #define PNG_NO_PEDANTIC_WARNINGS
    20 #include "png.h"
    20 #include "png.h"
    21 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
    21 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
       
    22 #include "pngpriv.h"
    22 
    23 
    23 static void /* PRIVATE */
    24 static void /* PRIVATE */
    24 png_default_error PNGARG((png_structp png_ptr,
    25 png_default_error PNGARG((png_structp png_ptr,
    25   png_const_charp error_message));
    26   png_const_charp error_message)) PNG_NORETURN;
    26 #ifndef PNG_NO_WARNINGS
    27 #ifdef PNG_WARNINGS_SUPPORTED
    27 static void /* PRIVATE */
    28 static void /* PRIVATE */
    28 png_default_warning PNGARG((png_structp png_ptr,
    29 png_default_warning PNGARG((png_structp png_ptr,
    29   png_const_charp warning_message));
    30   png_const_charp warning_message));
    30 #endif /* PNG_NO_WARNINGS */
    31 #endif /* PNG_WARNINGS_SUPPORTED */
    31 
    32 
    32 /* This function is called whenever there is a fatal error.  This function
    33 /* This function is called whenever there is a fatal error.  This function
    33  * should not be changed.  If there is a need to handle errors differently,
    34  * should not be changed.  If there is a need to handle errors differently,
    34  * you should supply a replacement error function and use png_set_error_fn()
    35  * you should supply a replacement error function and use png_set_error_fn()
    35  * to replace the error function at run-time.
    36  * to replace the error function at run-time.
    36  */
    37  */
    37 #ifndef PNG_NO_ERROR_TEXT
    38 #ifdef PNG_ERROR_TEXT_SUPPORTED
    38 void PNGAPI
    39 void PNGAPI
    39 png_error(png_structp png_ptr, png_const_charp error_message)
    40 png_error(png_structp png_ptr, png_const_charp error_message)
    40 {
    41 {
    41 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
    42 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
    42    char msg[16];
    43    char msg[16];
    43    if (png_ptr != NULL)
    44    if (png_ptr != NULL)
    44    {
    45    {
    45      if (png_ptr->flags&
    46      if (png_ptr->flags&
    46        (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))
    47        (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))
    47      {
    48      {
    48        if (*error_message == '#')
    49        if (*error_message == PNG_LITERAL_SHARP)
    49        {
    50        {
    50            /* Strip "#nnnn " from beginning of error message. */
    51            /* Strip "#nnnn " from beginning of error message. */
    51            int offset;
    52            int offset;
    52            for (offset = 1; offset<15; offset++)
    53            for (offset = 1; offset<15; offset++)
    53               if (error_message[offset] == ' ')
    54               if (error_message[offset] == ' ')
    91 
    92 
    92    /* If the custom handler doesn't exist, or if it returns,
    93    /* If the custom handler doesn't exist, or if it returns,
    93       use the default handler, which will not return. */
    94       use the default handler, which will not return. */
    94    png_default_error(png_ptr, '\0');
    95    png_default_error(png_ptr, '\0');
    95 }
    96 }
    96 #endif /* PNG_NO_ERROR_TEXT */
    97 #endif /* PNG_ERROR_TEXT_SUPPORTED */
    97 
    98 
    98 #ifndef PNG_NO_WARNINGS
    99 #ifdef PNG_WARNINGS_SUPPORTED
    99 /* This function is called whenever there is a non-fatal error.  This function
   100 /* This function is called whenever there is a non-fatal error.  This function
   100  * should not be changed.  If there is a need to handle warnings differently,
   101  * should not be changed.  If there is a need to handle warnings differently,
   101  * you should supply a replacement warning function and use
   102  * you should supply a replacement warning function and use
   102  * png_set_error_fn() to replace the warning function at run-time.
   103  * png_set_error_fn() to replace the warning function at run-time.
   103  */
   104  */
   110 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
   111 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
   111    if (png_ptr->flags&
   112    if (png_ptr->flags&
   112      (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))
   113      (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))
   113 #endif
   114 #endif
   114      {
   115      {
   115        if (*warning_message == '#')
   116        if (*warning_message == PNG_LITERAL_SHARP)
   116        {
   117        {
   117            for (offset = 1; offset < 15; offset++)
   118            for (offset = 1; offset < 15; offset++)
   118               if (warning_message[offset] == ' ')
   119               if (warning_message[offset] == ' ')
   119                   break;
   120                   break;
   120        }
   121        }
   123    if (png_ptr != NULL && png_ptr->warning_fn != NULL)
   124    if (png_ptr != NULL && png_ptr->warning_fn != NULL)
   124       (*(png_ptr->warning_fn))(png_ptr, warning_message + offset);
   125       (*(png_ptr->warning_fn))(png_ptr, warning_message + offset);
   125    else
   126    else
   126       png_default_warning(png_ptr, warning_message + offset);
   127       png_default_warning(png_ptr, warning_message + offset);
   127 }
   128 }
   128 #endif /* PNG_NO_WARNINGS */
   129 #endif /* PNG_WARNINGS_SUPPORTED */
   129 
   130 
       
   131 #ifdef PNG_BENIGN_ERRORS_SUPPORTED
       
   132 void PNGAPI
       
   133 png_benign_error(png_structp png_ptr, png_const_charp error_message)
       
   134 {
       
   135   if (png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN)
       
   136     png_warning(png_ptr, error_message);
       
   137   else
       
   138     png_error(png_ptr, error_message);
       
   139 }
       
   140 #endif
   130 
   141 
   131 /* These utilities are used internally to build an error message that relates
   142 /* These utilities are used internally to build an error message that relates
   132  * to the current chunk.  The chunk name comes from png_ptr->chunk_name,
   143  * to the current chunk.  The chunk name comes from png_ptr->chunk_name,
   133  * this is used to prefix the message.  The message is limited in length
   144  * this is used to prefix the message.  The message is limited in length
   134  * to 63 bytes, the name characters are output as hex digits wrapped in []
   145  * to 63 bytes, the name characters are output as hex digits wrapped in []
   139    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
   150    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
   140    'A', 'B', 'C', 'D', 'E', 'F'
   151    'A', 'B', 'C', 'D', 'E', 'F'
   141 };
   152 };
   142 
   153 
   143 #define PNG_MAX_ERROR_TEXT 64
   154 #define PNG_MAX_ERROR_TEXT 64
   144 
   155 #if defined(PNG_WARNINGS_SUPPORTED) || defined(PNG_ERROR_TEXT_SUPPORTED)
   145 #if !defined(PNG_NO_WARNINGS) || !defined(PNG_NO_ERROR_TEXT)
       
   146 static void /* PRIVATE */
   156 static void /* PRIVATE */
   147 png_format_buffer(png_structp png_ptr, png_charp buffer, png_const_charp
   157 png_format_buffer(png_structp png_ptr, png_charp buffer, png_const_charp
   148    error_message)
   158    error_message)
   149 {
   159 {
   150    int iout = 0, iin = 0;
   160    int iout = 0, iin = 0;
   152    while (iin < 4)
   162    while (iin < 4)
   153    {
   163    {
   154       int c = png_ptr->chunk_name[iin++];
   164       int c = png_ptr->chunk_name[iin++];
   155       if (isnonalpha(c))
   165       if (isnonalpha(c))
   156       {
   166       {
   157          buffer[iout++] = '[';
   167          buffer[iout++] = PNG_LITERAL_LEFT_SQUARE_BRACKET;
   158          buffer[iout++] = png_digit[(c & 0xf0) >> 4];
   168          buffer[iout++] = png_digit[(c & 0xf0) >> 4];
   159          buffer[iout++] = png_digit[c & 0x0f];
   169          buffer[iout++] = png_digit[c & 0x0f];
   160          buffer[iout++] = ']';
   170          buffer[iout++] = PNG_LITERAL_RIGHT_SQUARE_BRACKET;
   161       }
   171       }
   162       else
   172       else
   163       {
   173       {
   164          buffer[iout++] = (png_byte)c;
   174          buffer[iout++] = (png_byte)c;
   165       }
   175       }
   188      png_format_buffer(png_ptr, msg, error_message);
   198      png_format_buffer(png_ptr, msg, error_message);
   189      png_error(png_ptr, msg);
   199      png_error(png_ptr, msg);
   190    }
   200    }
   191 }
   201 }
   192 #endif /* PNG_READ_SUPPORTED */
   202 #endif /* PNG_READ_SUPPORTED */
   193 #endif /* !defined(PNG_NO_WARNINGS) || !defined(PNG_NO_ERROR_TEXT) */
   203 #endif /* PNG_WARNINGS_SUPPORTED || PNG_ERROR_TEXT_SUPPORTED */
   194 
   204 
   195 #ifndef PNG_NO_WARNINGS
   205 #ifdef PNG_WARNINGS_SUPPORTED
   196 void PNGAPI
   206 void PNGAPI
   197 png_chunk_warning(png_structp png_ptr, png_const_charp warning_message)
   207 png_chunk_warning(png_structp png_ptr, png_const_charp warning_message)
   198 {
   208 {
   199    char msg[18+PNG_MAX_ERROR_TEXT];
   209    char msg[18+PNG_MAX_ERROR_TEXT];
   200    if (png_ptr == NULL)
   210    if (png_ptr == NULL)
   203    {
   213    {
   204      png_format_buffer(png_ptr, msg, warning_message);
   214      png_format_buffer(png_ptr, msg, warning_message);
   205      png_warning(png_ptr, msg);
   215      png_warning(png_ptr, msg);
   206    }
   216    }
   207 }
   217 }
   208 #endif /* PNG_NO_WARNINGS */
   218 #endif /* PNG_WARNINGS_SUPPORTED */
   209 
   219 
       
   220 #ifdef PNG_READ_SUPPORTED
       
   221 #ifdef PNG_BENIGN_ERRORS_SUPPORTED
       
   222 void PNGAPI
       
   223 png_chunk_benign_error(png_structp png_ptr, png_const_charp error_message)
       
   224 {
       
   225   if (png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN)
       
   226     png_chunk_warning(png_ptr, error_message);
       
   227   else
       
   228     png_chunk_error(png_ptr, error_message);
       
   229 }
       
   230 #endif
       
   231 #endif /* PNG_READ_SUPPORTED */
       
   232 
       
   233 #ifdef PNG_SETJMP_SUPPORTED
       
   234 /* This API only exists if ANSI-C style error handling is used,
       
   235  * otherwise it is necessary for png_default_error to be overridden.
       
   236  */
       
   237 jmp_buf* PNGAPI
       
   238 png_set_longjmp_fn(png_structp png_ptr, png_longjmp_ptr longjmp_fn,
       
   239     size_t jmp_buf_size)
       
   240 {
       
   241    if (png_ptr == NULL || jmp_buf_size != png_sizeof(jmp_buf))
       
   242       return NULL;
       
   243 
       
   244    png_ptr->longjmp_fn = longjmp_fn;
       
   245    return &png_ptr->jmpbuf;
       
   246 }
       
   247 #endif
   210 
   248 
   211 /* This is the default error handling function.  Note that replacements for
   249 /* This is the default error handling function.  Note that replacements for
   212  * this function MUST NOT RETURN, or the program will likely crash.  This
   250  * this function MUST NOT RETURN, or the program will likely crash.  This
   213  * function is used by default, or if the program supplies NULL for the
   251  * function is used by default, or if the program supplies NULL for the
   214  * error function pointer in png_set_error_fn().
   252  * error function pointer in png_set_error_fn().
   215  */
   253  */
   216 static void /* PRIVATE */
   254 static void /* PRIVATE */
   217 png_default_error(png_structp png_ptr, png_const_charp error_message)
   255 png_default_error(png_structp png_ptr, png_const_charp error_message)
   218 {
   256 {
   219 #ifndef PNG_NO_CONSOLE_IO
   257 #ifdef PNG_CONSOLE_IO_SUPPORTED
   220 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
   258 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
   221    if (*error_message == '#')
   259    if (*error_message == PNG_LITERAL_SHARP)
   222    {
   260    {
   223      /* Strip "#nnnn " from beginning of error message. */
   261      /* Strip "#nnnn " from beginning of error message. */
   224      int offset;
   262      int offset;
   225      char error_number[16];
   263      char error_number[16];
   226      for (offset = 0; offset<15; offset++)
   264      for (offset = 0; offset<15; offset++)
   250       fprintf(stderr, PNG_STRING_NEWLINE);
   288       fprintf(stderr, PNG_STRING_NEWLINE);
   251    }
   289    }
   252 #endif
   290 #endif
   253 
   291 
   254 #ifdef PNG_SETJMP_SUPPORTED
   292 #ifdef PNG_SETJMP_SUPPORTED
   255    if (png_ptr)
   293    if (png_ptr && png_ptr->longjmp_fn)
   256    {
   294    {
   257 #  ifdef USE_FAR_KEYWORD
   295 #  ifdef USE_FAR_KEYWORD
   258    {
   296    {
   259       jmp_buf jmpbuf;
   297       jmp_buf jmpbuf;
   260       png_memcpy(jmpbuf, png_ptr->jmpbuf, png_sizeof(jmp_buf));
   298       png_memcpy(jmpbuf, png_ptr->jmpbuf, png_sizeof(jmp_buf));
   261       longjmp(jmpbuf, 1);
   299      png_ptr->longjmp_fn(jmpbuf, 1);
   262    }
   300    }
   263 #  else
   301 #  else
   264    longjmp(png_ptr->jmpbuf, 1);
   302    png_ptr->longjmp_fn(png_ptr->jmpbuf, 1);
   265 #  endif
   303 #  endif
   266    }
   304    }
   267 #else
   305 #endif
       
   306    /* Here if not setjmp support or if png_ptr is null. */
   268    PNG_ABORT();
   307    PNG_ABORT();
   269 #endif
   308 #ifndef PNG_CONSOLE_IO_SUPPORTED
   270 #ifdef PNG_NO_CONSOLE_IO
       
   271    error_message = error_message; /* Make compiler happy */
   309    error_message = error_message; /* Make compiler happy */
   272 #endif
   310 #endif
   273 }
   311 }
   274 
   312 
   275 #ifndef PNG_NO_WARNINGS
   313 #ifdef PNG_WARNINGS_SUPPORTED
   276 /* This function is called when there is a warning, but the library thinks
   314 /* This function is called when there is a warning, but the library thinks
   277  * it can continue anyway.  Replacement functions don't have to do anything
   315  * it can continue anyway.  Replacement functions don't have to do anything
   278  * here if you don't want them to.  In the default configuration, png_ptr is
   316  * here if you don't want them to.  In the default configuration, png_ptr is
   279  * not used, but it is passed in case it may be useful.
   317  * not used, but it is passed in case it may be useful.
   280  */
   318  */
   281 static void /* PRIVATE */
   319 static void /* PRIVATE */
   282 png_default_warning(png_structp png_ptr, png_const_charp warning_message)
   320 png_default_warning(png_structp png_ptr, png_const_charp warning_message)
   283 {
   321 {
   284 #ifndef PNG_NO_CONSOLE_IO
   322 #ifdef PNG_CONSOLE_IO_SUPPORTED
   285 #  ifdef PNG_ERROR_NUMBERS_SUPPORTED
   323 #  ifdef PNG_ERROR_NUMBERS_SUPPORTED
   286    if (*warning_message == '#')
   324    if (*warning_message == PNG_LITERAL_SHARP)
   287    {
   325    {
   288      int offset;
   326      int offset;
   289      char warning_number[16];
   327      char warning_number[16];
   290      for (offset = 0; offset < 15; offset++)
   328      for (offset = 0; offset < 15; offset++)
   291      {
   329      {
   316 #else
   354 #else
   317    warning_message = warning_message; /* Make compiler happy */
   355    warning_message = warning_message; /* Make compiler happy */
   318 #endif
   356 #endif
   319    png_ptr = png_ptr; /* Make compiler happy */
   357    png_ptr = png_ptr; /* Make compiler happy */
   320 }
   358 }
   321 #endif /* PNG_NO_WARNINGS */
   359 #endif /* PNG_WARNINGS_SUPPORTED */
   322 
   360 
   323 /* This function is called when the application wants to use another method
   361 /* This function is called when the application wants to use another method
   324  * of handling errors and warnings.  Note that the error function MUST NOT
   362  * of handling errors and warnings.  Note that the error function MUST NOT
   325  * return to the calling routine or serious problems will occur.  The return
   363  * return to the calling routine or serious problems will occur.  The return
   326  * method used in the default routine calls longjmp(png_ptr->jmpbuf, 1)
   364  * method used in the default routine calls longjmp(png_ptr->jmpbuf, 1)