pngerror.c

Go to the documentation of this file.
00001 
00002 /* pngerror.c - stub functions for i/o and memory allocation
00003  *
00004  * Last changed in libpng 1.2.22 [November 6, 2007]
00005  * For conditions of distribution and use, see copyright notice in png.h
00006  * Copyright (c) 1998-2007 Glenn Randers-Pehrson
00007  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
00008  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
00009  *
00010  * This file provides a location for all error handling.  Users who
00011  * need special error handling are expected to write replacement functions
00012  * and use png_set_error_fn() to use those functions.  See the instructions
00013  * at each function.
00014  */
00015 
00016 #define PNG_INTERNAL
00017 #include "png.h"
00018 
00019 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
00020 static void /* PRIVATE */
00021 png_default_error PNGARG((png_structp png_ptr,
00022   png_const_charp error_message));
00023 #ifndef PNG_NO_WARNINGS
00024 static void /* PRIVATE */
00025 png_default_warning PNGARG((png_structp png_ptr,
00026   png_const_charp warning_message));
00027 #endif /* PNG_NO_WARNINGS */
00028 
00029 /* This function is called whenever there is a fatal error.  This function
00030  * should not be changed.  If there is a need to handle errors differently,
00031  * you should supply a replacement error function and use png_set_error_fn()
00032  * to replace the error function at run-time.
00033  */
00034 #ifndef PNG_NO_ERROR_TEXT
00035 void PNGAPI
00036 png_error(png_structp png_ptr, png_const_charp error_message)
00037 {
00038 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
00039    char msg[16];
00040    if (png_ptr != NULL)
00041    {
00042      if (png_ptr->flags&
00043        (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))
00044      {
00045        if (*error_message == '#')
00046        {
00047            int offset;
00048            for (offset=1; offset<15; offset++)
00049               if (*(error_message+offset) == ' ')
00050                   break;
00051            if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT)
00052            {
00053               int i;
00054               for (i=0; i<offset-1; i++)
00055                  msg[i]=error_message[i+1];
00056               msg[i]='\0';
00057               error_message=msg;
00058            }
00059            else
00060               error_message+=offset;
00061        }
00062        else
00063        {
00064            if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT)
00065            {
00066               msg[0]='0';
00067               msg[1]='\0';
00068               error_message=msg;
00069            }
00070        }
00071      }
00072    }
00073 #endif
00074    if (png_ptr != NULL && png_ptr->error_fn != NULL)
00075       (*(png_ptr->error_fn))(png_ptr, error_message);
00076 
00077    /* If the custom handler doesn't exist, or if it returns,
00078       use the default handler, which will not return. */
00079    png_default_error(png_ptr, error_message);
00080 }
00081 #else
00082 void PNGAPI
00083 png_err(png_structp png_ptr)
00084 {
00085    if (png_ptr != NULL && png_ptr->error_fn != NULL)
00086       (*(png_ptr->error_fn))(png_ptr, '\0');
00087 
00088    /* If the custom handler doesn't exist, or if it returns,
00089       use the default handler, which will not return. */
00090    png_default_error(png_ptr, '\0');
00091 }
00092 #endif /* PNG_NO_ERROR_TEXT */
00093 
00094 #ifndef PNG_NO_WARNINGS
00095 /* This function is called whenever there is a non-fatal error.  This function
00096  * should not be changed.  If there is a need to handle warnings differently,
00097  * you should supply a replacement warning function and use
00098  * png_set_error_fn() to replace the warning function at run-time.
00099  */
00100 void PNGAPI
00101 png_warning(png_structp png_ptr, png_const_charp warning_message)
00102 {
00103    int offset = 0;
00104    if (png_ptr != NULL)
00105    {
00106 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
00107    if (png_ptr->flags&
00108      (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))
00109 #endif
00110      {
00111        if (*warning_message == '#')
00112        {
00113            for (offset=1; offset<15; offset++)
00114               if (*(warning_message+offset) == ' ')
00115                   break;
00116        }
00117      }
00118      if (png_ptr != NULL && png_ptr->warning_fn != NULL)
00119         (*(png_ptr->warning_fn))(png_ptr, warning_message+offset);
00120    }
00121    else
00122       png_default_warning(png_ptr, warning_message+offset);
00123 }
00124 #endif /* PNG_NO_WARNINGS */
00125 
00126 
00127 /* These utilities are used internally to build an error message that relates
00128  * to the current chunk.  The chunk name comes from png_ptr->chunk_name,
00129  * this is used to prefix the message.  The message is limited in length
00130  * to 63 bytes, the name characters are output as hex digits wrapped in []
00131  * if the character is invalid.
00132  */
00133 #define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
00134 static PNG_CONST char png_digit[16] = {
00135    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
00136    'A', 'B', 'C', 'D', 'E', 'F'
00137 };
00138 
00139 #define PNG_MAX_ERROR_TEXT 64
00140 
00141 #if !defined(PNG_NO_WARNINGS) || !defined(PNG_NO_ERROR_TEXT)
00142 static void /* PRIVATE */
00143 png_format_buffer(png_structp png_ptr, png_charp buffer, png_const_charp
00144    error_message)
00145 {
00146    int iout = 0, iin = 0;
00147 
00148    while (iin < 4)
00149    {
00150       int c = png_ptr->chunk_name[iin++];
00151       if (isnonalpha(c))
00152       {
00153          buffer[iout++] = '[';
00154          buffer[iout++] = png_digit[(c & 0xf0) >> 4];
00155          buffer[iout++] = png_digit[c & 0x0f];
00156          buffer[iout++] = ']';
00157       }
00158       else
00159       {
00160          buffer[iout++] = (png_byte)c;
00161       }
00162    }
00163 
00164    if (error_message == NULL)
00165       buffer[iout] = '\0';
00166    else
00167    {
00168       buffer[iout++] = ':';
00169       buffer[iout++] = ' ';
00170       png_memcpy(buffer+iout, error_message, PNG_MAX_ERROR_TEXT);
00171       buffer[iout+PNG_MAX_ERROR_TEXT-1] = '\0';
00172    }
00173 }
00174 
00175 #ifdef PNG_READ_SUPPORTED
00176 void PNGAPI
00177 png_chunk_error(png_structp png_ptr, png_const_charp error_message)
00178 {
00179    char msg[18+PNG_MAX_ERROR_TEXT];
00180    if (png_ptr == NULL)
00181      png_error(png_ptr, error_message);
00182    else
00183    {
00184      png_format_buffer(png_ptr, msg, error_message);
00185      png_error(png_ptr, msg);
00186    }
00187 }
00188 #endif /* PNG_READ_SUPPORTED */
00189 #endif /* !defined(PNG_NO_WARNINGS) || !defined(PNG_NO_ERROR_TEXT) */
00190 
00191 #ifndef PNG_NO_WARNINGS
00192 void PNGAPI
00193 png_chunk_warning(png_structp png_ptr, png_const_charp warning_message)
00194 {
00195    char msg[18+PNG_MAX_ERROR_TEXT];
00196    if (png_ptr == NULL)
00197      png_warning(png_ptr, warning_message);
00198    else
00199    {
00200      png_format_buffer(png_ptr, msg, warning_message);
00201      png_warning(png_ptr, msg);
00202    }
00203 }
00204 #endif /* PNG_NO_WARNINGS */
00205 
00206 
00207 /* This is the default error handling function.  Note that replacements for
00208  * this function MUST NOT RETURN, or the program will likely crash.  This
00209  * function is used by default, or if the program supplies NULL for the
00210  * error function pointer in png_set_error_fn().
00211  */
00212 static void /* PRIVATE */
00213 png_default_error(png_structp png_ptr, png_const_charp error_message)
00214 {
00215 #ifndef PNG_NO_CONSOLE_IO
00216 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
00217    if (*error_message == '#')
00218    {
00219      int offset;
00220      char error_number[16];
00221      for (offset=0; offset<15; offset++)
00222      {
00223          error_number[offset] = *(error_message+offset+1);
00224          if (*(error_message+offset) == ' ')
00225              break;
00226      }
00227      if((offset > 1) && (offset < 15))
00228      {
00229        error_number[offset-1]='\0';
00230        fprintf(stderr, "libpng error no. %s: %s\n", error_number,
00231           error_message+offset);
00232      }
00233      else
00234        fprintf(stderr, "libpng error: %s, offset=%d\n", error_message,offset);
00235    }
00236    else
00237 #endif
00238    fprintf(stderr, "libpng error: %s\n", error_message);
00239 #endif
00240 
00241 #ifdef PNG_SETJMP_SUPPORTED
00242    if (png_ptr)
00243    {
00244 #  ifdef USE_FAR_KEYWORD
00245    {
00246       jmp_buf jmpbuf;
00247       png_memcpy(jmpbuf, png_ptr->jmpbuf, png_sizeof(jmp_buf));
00248       longjmp(jmpbuf, 1);
00249    }
00250 #  else
00251    longjmp(png_ptr->jmpbuf, 1);
00252 #  endif
00253    }
00254 #else
00255    PNG_ABORT();
00256 #endif
00257 #ifdef PNG_NO_CONSOLE_IO
00258    error_message = error_message; /* make compiler happy */
00259 #endif
00260 }
00261 
00262 #ifndef PNG_NO_WARNINGS
00263 /* This function is called when there is a warning, but the library thinks
00264  * it can continue anyway.  Replacement functions don't have to do anything
00265  * here if you don't want them to.  In the default configuration, png_ptr is
00266  * not used, but it is passed in case it may be useful.
00267  */
00268 static void /* PRIVATE */
00269 png_default_warning(png_structp png_ptr, png_const_charp warning_message)
00270 {
00271 #ifndef PNG_NO_CONSOLE_IO
00272 #  ifdef PNG_ERROR_NUMBERS_SUPPORTED
00273    if (*warning_message == '#')
00274    {
00275      int offset;
00276      char warning_number[16];
00277      for (offset=0; offset<15; offset++)
00278      {
00279         warning_number[offset]=*(warning_message+offset+1);
00280         if (*(warning_message+offset) == ' ')
00281             break;
00282      }
00283      if((offset > 1) && (offset < 15))
00284      {
00285        warning_number[offset-1]='\0';
00286        fprintf(stderr, "libpng warning no. %s: %s\n", warning_number,
00287           warning_message+offset);
00288      }
00289      else
00290        fprintf(stderr, "libpng warning: %s\n", warning_message);
00291    }
00292    else
00293 #  endif
00294      fprintf(stderr, "libpng warning: %s\n", warning_message);
00295 #else
00296    warning_message = warning_message; /* make compiler happy */
00297 #endif
00298    png_ptr = png_ptr; /* make compiler happy */
00299 }
00300 #endif /* PNG_NO_WARNINGS */
00301 
00302 /* This function is called when the application wants to use another method
00303  * of handling errors and warnings.  Note that the error function MUST NOT
00304  * return to the calling routine or serious problems will occur.  The return
00305  * method used in the default routine calls longjmp(png_ptr->jmpbuf, 1)
00306  */
00307 void PNGAPI
00308 png_set_error_fn(png_structp png_ptr, png_voidp error_ptr,
00309    png_error_ptr error_fn, png_error_ptr warning_fn)
00310 {
00311    if (png_ptr == NULL)
00312       return;
00313    png_ptr->error_ptr = error_ptr;
00314    png_ptr->error_fn = error_fn;
00315    png_ptr->warning_fn = warning_fn;
00316 }
00317 
00318 
00319 /* This function returns a pointer to the error_ptr associated with the user
00320  * functions.  The application should free any memory associated with this
00321  * pointer before png_write_destroy and png_read_destroy are called.
00322  */
00323 png_voidp PNGAPI
00324 png_get_error_ptr(png_structp png_ptr)
00325 {
00326    if (png_ptr == NULL)
00327       return NULL;
00328    return ((png_voidp)png_ptr->error_ptr);
00329 }
00330 
00331 
00332 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
00333 void PNGAPI
00334 png_set_strip_error_numbers(png_structp png_ptr, png_uint_32 strip_mode)
00335 {
00336    if(png_ptr != NULL)
00337    {
00338      png_ptr->flags &=
00339        ((~(PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))&strip_mode);
00340    }
00341 }
00342 #endif
00343 #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */

Generated on Tue Jul 5 14:13:31 2011 for ROOT_528-00b_version by  doxygen 1.5.1