Home | History | Annotate | Download | only in libpng-1.2.19
      1 
      2 /* pngerror.c - stub functions for i/o and memory allocation
      3  *
      4  * Last changed in libpng 1.2.19 August 18, 2007
      5  * For conditions of distribution and use, see copyright notice in png.h
      6  * Copyright (c) 1998-2007 Glenn Randers-Pehrson
      7  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
      8  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
      9  *
     10  * This file provides a location for all error handling.  Users who
     11  * need special error handling are expected to write replacement functions
     12  * and use png_set_error_fn() to use those functions.  See the instructions
     13  * at each function.
     14  */
     15 
     16 #define PNG_INTERNAL
     17 #include "png.h"
     18 
     19 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
     20 static void /* PRIVATE */
     21 png_default_error PNGARG((png_structp png_ptr,
     22   png_const_charp error_message));
     23 #ifndef PNG_NO_WARNINGS
     24 static void /* PRIVATE */
     25 png_default_warning PNGARG((png_structp png_ptr,
     26   png_const_charp warning_message));
     27 #endif /* PNG_NO_WARNINGS */
     28 
     29 /* This function is called whenever there is a fatal error.  This function
     30  * should not be changed.  If there is a need to handle errors differently,
     31  * you should supply a replacement error function and use png_set_error_fn()
     32  * to replace the error function at run-time.
     33  */
     34 void PNGAPI
     35 png_error(png_structp png_ptr, png_const_charp error_message)
     36 {
     37 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
     38    char msg[16];
     39    if (png_ptr != NULL)
     40    {
     41      if (png_ptr->flags&
     42        (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))
     43      {
     44        if (*error_message == '#')
     45        {
     46            int offset;
     47            for (offset=1; offset<15; offset++)
     48               if (*(error_message+offset) == ' ')
     49                   break;
     50            if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT)
     51            {
     52               int i;
     53               for (i=0; i<offset-1; i++)
     54                  msg[i]=error_message[i+1];
     55               msg[i]='\0';
     56               error_message=msg;
     57            }
     58            else
     59               error_message+=offset;
     60        }
     61        else
     62        {
     63            if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT)
     64            {
     65               msg[0]='0';
     66               msg[1]='\0';
     67               error_message=msg;
     68            }
     69        }
     70      }
     71    }
     72 #endif
     73    if (png_ptr != NULL && png_ptr->error_fn != NULL)
     74       (*(png_ptr->error_fn))(png_ptr, error_message);
     75 
     76    /* If the custom handler doesn't exist, or if it returns,
     77       use the default handler, which will not return. */
     78    png_default_error(png_ptr, error_message);
     79 }
     80 
     81 #ifndef PNG_NO_WARNINGS
     82 /* This function is called whenever there is a non-fatal error.  This function
     83  * should not be changed.  If there is a need to handle warnings differently,
     84  * you should supply a replacement warning function and use
     85  * png_set_error_fn() to replace the warning function at run-time.
     86  */
     87 void PNGAPI
     88 png_warning(png_structp png_ptr, png_const_charp warning_message)
     89 {
     90    int offset = 0;
     91    if (png_ptr != NULL)
     92    {
     93 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
     94    if (png_ptr->flags&
     95      (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))
     96 #endif
     97      {
     98        if (*warning_message == '#')
     99        {
    100            for (offset=1; offset<15; offset++)
    101               if (*(warning_message+offset) == ' ')
    102                   break;
    103        }
    104      }
    105      if (png_ptr != NULL && png_ptr->warning_fn != NULL)
    106         (*(png_ptr->warning_fn))(png_ptr, warning_message+offset);
    107    }
    108    else
    109       png_default_warning(png_ptr, warning_message+offset);
    110 }
    111 #endif /* PNG_NO_WARNINGS */
    112 
    113 
    114 /* These utilities are used internally to build an error message that relates
    115  * to the current chunk.  The chunk name comes from png_ptr->chunk_name,
    116  * this is used to prefix the message.  The message is limited in length
    117  * to 63 bytes, the name characters are output as hex digits wrapped in []
    118  * if the character is invalid.
    119  */
    120 #define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
    121 static PNG_CONST char png_digit[16] = {
    122    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    123    'A', 'B', 'C', 'D', 'E', 'F'
    124 };
    125 
    126 static void /* PRIVATE */
    127 png_format_buffer(png_structp png_ptr, png_charp buffer, png_const_charp
    128    error_message)
    129 {
    130    int iout = 0, iin = 0;
    131 
    132    while (iin < 4)
    133    {
    134       int c = png_ptr->chunk_name[iin++];
    135       if (isnonalpha(c))
    136       {
    137          buffer[iout++] = '[';
    138          buffer[iout++] = png_digit[(c & 0xf0) >> 4];
    139          buffer[iout++] = png_digit[c & 0x0f];
    140          buffer[iout++] = ']';
    141       }
    142       else
    143       {
    144          buffer[iout++] = (png_byte)c;
    145       }
    146    }
    147 
    148    if (error_message == NULL)
    149       buffer[iout] = 0;
    150    else
    151    {
    152       buffer[iout++] = ':';
    153       buffer[iout++] = ' ';
    154       png_strncpy(buffer+iout, error_message, 63);
    155       buffer[iout+63] = 0;
    156    }
    157 }
    158 
    159 #ifdef PNG_READ_SUPPORTED
    160 void PNGAPI
    161 png_chunk_error(png_structp png_ptr, png_const_charp error_message)
    162 {
    163    char msg[18+64];
    164    if (png_ptr == NULL)
    165      png_error(png_ptr, error_message);
    166    else
    167    {
    168      png_format_buffer(png_ptr, msg, error_message);
    169      png_error(png_ptr, msg);
    170    }
    171 }
    172 
    173 #ifndef PNG_NO_WARNINGS
    174 void PNGAPI
    175 png_chunk_warning(png_structp png_ptr, png_const_charp warning_message)
    176 {
    177    char msg[18+64];
    178    if (png_ptr == NULL)
    179      png_warning(png_ptr, warning_message);
    180    else
    181    {
    182      png_format_buffer(png_ptr, msg, warning_message);
    183      png_warning(png_ptr, msg);
    184    }
    185 }
    186 #endif /* PNG_NO_WARNINGS */
    187 
    188 #endif /* PNG_READ_SUPPORTED */
    189 
    190 /* This is the default error handling function.  Note that replacements for
    191  * this function MUST NOT RETURN, or the program will likely crash.  This
    192  * function is used by default, or if the program supplies NULL for the
    193  * error function pointer in png_set_error_fn().
    194  */
    195 static void /* PRIVATE */
    196 png_default_error(png_structp png_ptr, png_const_charp error_message)
    197 {
    198 #ifndef PNG_NO_CONSOLE_IO
    199 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
    200    if (*error_message == '#')
    201    {
    202      int offset;
    203      char error_number[16];
    204      for (offset=0; offset<15; offset++)
    205      {
    206          error_number[offset] = *(error_message+offset+1);
    207          if (*(error_message+offset) == ' ')
    208              break;
    209      }
    210      if((offset > 1) && (offset < 15))
    211      {
    212        error_number[offset-1]='\0';
    213        fprintf(stderr, "libpng error no. %s: %s\n", error_number,
    214           error_message+offset);
    215      }
    216      else
    217        fprintf(stderr, "libpng error: %s, offset=%d\n", error_message,offset);
    218    }
    219    else
    220 #endif
    221    fprintf(stderr, "libpng error: %s\n", error_message);
    222 #endif
    223 
    224 #ifdef PNG_SETJMP_SUPPORTED
    225    if (png_ptr)
    226    {
    227 #  ifdef USE_FAR_KEYWORD
    228    {
    229       jmp_buf jmpbuf;
    230       png_memcpy(jmpbuf, png_ptr->jmpbuf, png_sizeof(jmp_buf));
    231       longjmp(jmpbuf, 1);
    232    }
    233 #  else
    234    longjmp(png_ptr->jmpbuf, 1);
    235 #  endif
    236    }
    237 #else
    238    PNG_ABORT();
    239 #endif
    240 #ifdef PNG_NO_CONSOLE_IO
    241    error_message = error_message; /* make compiler happy */
    242 #endif
    243 }
    244 
    245 #ifndef PNG_NO_WARNINGS
    246 /* This function is called when there is a warning, but the library thinks
    247  * it can continue anyway.  Replacement functions don't have to do anything
    248  * here if you don't want them to.  In the default configuration, png_ptr is
    249  * not used, but it is passed in case it may be useful.
    250  */
    251 static void /* PRIVATE */
    252 png_default_warning(png_structp png_ptr, png_const_charp warning_message)
    253 {
    254 #ifndef PNG_NO_CONSOLE_IO
    255 #  ifdef PNG_ERROR_NUMBERS_SUPPORTED
    256    if (*warning_message == '#')
    257    {
    258      int offset;
    259      char warning_number[16];
    260      for (offset=0; offset<15; offset++)
    261      {
    262         warning_number[offset]=*(warning_message+offset+1);
    263         if (*(warning_message+offset) == ' ')
    264             break;
    265      }
    266      if((offset > 1) && (offset < 15))
    267      {
    268        warning_number[offset-1]='\0';
    269        fprintf(stderr, "libpng warning no. %s: %s\n", warning_number,
    270           warning_message+offset);
    271      }
    272      else
    273        fprintf(stderr, "libpng warning: %s\n", warning_message);
    274    }
    275    else
    276 #  endif
    277      fprintf(stderr, "libpng warning: %s\n", warning_message);
    278 #else
    279    warning_message = warning_message; /* make compiler happy */
    280 #endif
    281    png_ptr = png_ptr; /* make compiler happy */
    282 }
    283 #endif /* PNG_NO_WARNINGS */
    284 
    285 /* This function is called when the application wants to use another method
    286  * of handling errors and warnings.  Note that the error function MUST NOT
    287  * return to the calling routine or serious problems will occur.  The return
    288  * method used in the default routine calls longjmp(png_ptr->jmpbuf, 1)
    289  */
    290 void PNGAPI
    291 png_set_error_fn(png_structp png_ptr, png_voidp error_ptr,
    292    png_error_ptr error_fn, png_error_ptr warning_fn)
    293 {
    294    if (png_ptr == NULL)
    295       return;
    296    png_ptr->error_ptr = error_ptr;
    297    png_ptr->error_fn = error_fn;
    298    png_ptr->warning_fn = warning_fn;
    299 }
    300 
    301 
    302 /* This function returns a pointer to the error_ptr associated with the user
    303  * functions.  The application should free any memory associated with this
    304  * pointer before png_write_destroy and png_read_destroy are called.
    305  */
    306 png_voidp PNGAPI
    307 png_get_error_ptr(png_structp png_ptr)
    308 {
    309    if (png_ptr == NULL)
    310       return NULL;
    311    return ((png_voidp)png_ptr->error_ptr);
    312 }
    313 
    314 
    315 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
    316 void PNGAPI
    317 png_set_strip_error_numbers(png_structp png_ptr, png_uint_32 strip_mode)
    318 {
    319    if(png_ptr != NULL)
    320    {
    321      png_ptr->flags &=
    322        ((~(PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))&strip_mode);
    323    }
    324 }
    325 #endif
    326 #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */
    327