Back to index

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