Back to index

plt-scheme  4.2.1
pngerror.c
Go to the documentation of this file.
00001 
00002 /* pngerror.c - stub functions for i/o and memory allocation
00003  *
00004  * libpng 1.2.5 - October 3, 2002
00005  * For conditions of distribution and use, see copyright notice in png.h
00006  * Copyright (c) 1998-2002 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      int offset = 0;
00039      if (*error_message == '#')
00040      {
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->error_fn != NULL)
00067       (*(png_ptr->error_fn))(png_ptr, error_message);
00068 
00069    /* if the following returns or doesn't exist, use the default function,
00070       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->warning_fn != NULL)
00095       (*(png_ptr->warning_fn))(png_ptr,
00096          (png_const_charp)(warning_message+offset));
00097    else
00098       png_default_warning(png_ptr, (png_const_charp)(warning_message+offset));
00099 }
00100 
00101 /* These utilities are used internally to build an error message that relates
00102  * to the current chunk.  The chunk name comes from png_ptr->chunk_name,
00103  * this is used to prefix the message.  The message is limited in length
00104  * to 63 bytes, the name characters are output as hex digits wrapped in []
00105  * if the character is invalid.
00106  */
00107 #define isnonalpha(c) ((c) < 41 || (c) > 122 || ((c) > 90 && (c) < 97))
00108 static PNG_CONST char png_digit[16] = {
00109    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
00110    'F' };
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_memcpy(buffer+iout, error_message, 64);
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 #else
00194    if (error_message)
00195      /* make compiler happy */ ;
00196 #endif
00197 
00198 #ifdef PNG_SETJMP_SUPPORTED
00199 #  ifdef USE_FAR_KEYWORD
00200    {
00201       jmp_buf jmpbuf;
00202       png_memcpy(jmpbuf,png_ptr->jmpbuf,sizeof(jmp_buf));
00203       longjmp(jmpbuf, 1);
00204    }
00205 #  else
00206    longjmp(png_ptr->jmpbuf, 1);
00207 # endif
00208 #else
00209    if (png_ptr)
00210      /* make compiler happy */ ;
00211    PNG_ABORT();
00212 #endif
00213 }
00214 
00215 /* This function is called when there is a warning, but the library thinks
00216  * it can continue anyway.  Replacement functions don't have to do anything
00217  * here if you don't want them to.  In the default configuration, png_ptr is
00218  * not used, but it is passed in case it may be useful.
00219  */
00220 static void /* PRIVATE */
00221 png_default_warning(png_structp png_ptr, png_const_charp warning_message)
00222 {
00223 #ifndef PNG_NO_CONSOLE_IO
00224 #  ifdef PNG_ERROR_NUMBERS_SUPPORTED
00225    if (*warning_message == '#')
00226    {
00227      int offset;
00228      char warning_number[16];
00229      for (offset=0; offset<15; offset++)
00230      {
00231         warning_number[offset]=*(warning_message+offset+1);
00232         if (*(warning_message+offset) == ' ')
00233             break;
00234      }
00235      if((offset > 1) && (offset < 15))
00236      {
00237        warning_number[offset-1]='\0';
00238        fprintf(stderr, "libpng warning no. %s: %s\n", warning_number,
00239           warning_message+offset);
00240      }
00241      else
00242        fprintf(stderr, "libpng warning: %s\n", warning_message);
00243    }
00244    else
00245 #  endif
00246      fprintf(stderr, "libpng warning: %s\n", warning_message);
00247 #else
00248    if (warning_message)
00249      /* appease compiler */ ;
00250 #endif
00251    if (png_ptr)
00252       return;
00253 }
00254 
00255 /* This function is called when the application wants to use another method
00256  * of handling errors and warnings.  Note that the error function MUST NOT
00257  * return to the calling routine or serious problems will occur.  The return
00258  * method used in the default routine calls longjmp(png_ptr->jmpbuf, 1)
00259  */
00260 void PNGAPI
00261 png_set_error_fn(png_structp png_ptr, png_voidp error_ptr,
00262    png_error_ptr error_fn, png_error_ptr warning_fn)
00263 {
00264    png_ptr->error_ptr = error_ptr;
00265    png_ptr->error_fn = error_fn;
00266    png_ptr->warning_fn = warning_fn;
00267 }
00268 
00269 
00270 /* This function returns a pointer to the error_ptr associated with the user
00271  * functions.  The application should free any memory associated with this
00272  * pointer before png_write_destroy and png_read_destroy are called.
00273  */
00274 png_voidp PNGAPI
00275 png_get_error_ptr(png_structp png_ptr)
00276 {
00277    return ((png_voidp)png_ptr->error_ptr);
00278 }
00279 
00280 
00281 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
00282 void PNGAPI
00283 png_set_strip_error_numbers(png_structp png_ptr, png_uint_32 strip_mode)
00284 {
00285    if(png_ptr != NULL)
00286    {
00287      png_ptr->flags &=
00288        ((~(PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))&strip_mode);
00289    }
00290 }
00291 #endif