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


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sun Apr 2 2017 03:43:56