home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / flash078.zip / flashsource-r0_7_8.zip / libpng / pngerror.c < prev    next >
C/C++ Source or Header  |  2001-04-27  |  6KB  |  193 lines

  1.  
  2. /* pngerror.c - stub functions for i/o and memory allocation
  3.  *
  4.  * libpng 1.0.11 - April 27, 2001
  5.  * For conditions of distribution and use, see copyright notice in png.h
  6.  * Copyright (c) 1998-2001 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. static void /* PRIVATE */
  20. png_default_error PNGARG((png_structp png_ptr,
  21.                                       png_const_charp message));
  22. static void /* PRIVATE */
  23. png_default_warning PNGARG((png_structp png_ptr,
  24.                                         png_const_charp message));
  25.  
  26. /* This function is called whenever there is a fatal error.  This function
  27.  * should not be changed.  If there is a need to handle errors differently,
  28.  * you should supply a replacement error function and use png_set_error_fn()
  29.  * to replace the error function at run-time.
  30.  */
  31. void PNGAPI
  32. png_error(png_structp png_ptr, png_const_charp message)
  33. {
  34.    if (png_ptr->error_fn != NULL)
  35.       (*(png_ptr->error_fn))(png_ptr, message);
  36.  
  37.    /* if the following returns or doesn't exist, use the default function,
  38.       which will not return */
  39.    png_default_error(png_ptr, message);
  40. }
  41.  
  42. /* This function is called whenever there is a non-fatal error.  This function
  43.  * should not be changed.  If there is a need to handle warnings differently,
  44.  * you should supply a replacement warning function and use
  45.  * png_set_error_fn() to replace the warning function at run-time.
  46.  */
  47. void PNGAPI
  48. png_warning(png_structp png_ptr, png_const_charp message)
  49. {
  50.    if (png_ptr->warning_fn != NULL)
  51.       (*(png_ptr->warning_fn))(png_ptr, message);
  52.    else
  53.       png_default_warning(png_ptr, message);
  54. }
  55.  
  56. /* These utilities are used internally to build an error message that relates
  57.  * to the current chunk.  The chunk name comes from png_ptr->chunk_name,
  58.  * this is used to prefix the message.  The message is limited in length
  59.  * to 63 bytes, the name characters are output as hex digits wrapped in []
  60.  * if the character is invalid.
  61.  */
  62. #define isnonalpha(c) ((c) < 41 || (c) > 122 || ((c) > 90 && (c) < 97))
  63. static PNG_CONST char png_digit[16] = {
  64.    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
  65.    'F' };
  66.  
  67. static void /* PRIVATE */
  68. png_format_buffer(png_structp png_ptr, png_charp buffer, png_const_charp
  69.    message)
  70. {
  71.    int iout = 0, iin = 0;
  72.  
  73.    while (iin < 4)
  74.    {
  75.       int c = png_ptr->chunk_name[iin++];
  76.       if (isnonalpha(c))
  77.       {
  78.          buffer[iout++] = '[';
  79.          buffer[iout++] = png_digit[(c & 0xf0) >> 4];
  80.          buffer[iout++] = png_digit[c & 0x0f];
  81.          buffer[iout++] = ']';
  82.       }
  83.       else
  84.       {
  85.          buffer[iout++] = (png_byte)c;
  86.       }
  87.    }
  88.  
  89.    if (message == NULL)
  90.       buffer[iout] = 0;
  91.    else
  92.    {
  93.       buffer[iout++] = ':';
  94.       buffer[iout++] = ' ';
  95.       png_memcpy(buffer+iout, message, 64);
  96.       buffer[iout+63] = 0;
  97.    }
  98. }
  99.  
  100. void PNGAPI
  101. png_chunk_error(png_structp png_ptr, png_const_charp message)
  102. {
  103.    char msg[18+64];
  104.    png_format_buffer(png_ptr, msg, message);
  105.    png_error(png_ptr, msg);
  106. }
  107.  
  108. void PNGAPI
  109. png_chunk_warning(png_structp png_ptr, png_const_charp message)
  110. {
  111.    char msg[18+64];
  112.    png_format_buffer(png_ptr, msg, message);
  113.    png_warning(png_ptr, msg);
  114. }
  115.  
  116. /* This is the default error handling function.  Note that replacements for
  117.  * this function MUST NOT RETURN, or the program will likely crash.  This
  118.  * function is used by default, or if the program supplies NULL for the
  119.  * error function pointer in png_set_error_fn().
  120.  */
  121. static void /* PRIVATE */
  122. png_default_error(png_structp png_ptr, png_const_charp message)
  123. {
  124. #ifndef PNG_NO_CONSOLE_IO
  125.    fprintf(stderr, "libpng error: %s\n", message);
  126. #else
  127.    if (message)
  128.      /* make compiler happy */ ;
  129. #endif
  130.  
  131. #ifdef PNG_SETJMP_SUPPORTED
  132. #  ifdef USE_FAR_KEYWORD
  133.    {
  134.       jmp_buf jmpbuf;
  135.       png_memcpy(jmpbuf,png_ptr->jmpbuf,sizeof(jmp_buf));
  136.       longjmp(jmpbuf, 1);
  137.    }
  138. #  else
  139.    longjmp(png_ptr->jmpbuf, 1);
  140. # endif
  141. #else
  142.    if (png_ptr)
  143.      /* make compiler happy */ ;
  144.    PNG_ABORT();
  145. #endif
  146. }
  147.  
  148. /* This function is called when there is a warning, but the library thinks
  149.  * it can continue anyway.  Replacement functions don't have to do anything
  150.  * here if you don't want them to.  In the default configuration, png_ptr is
  151.  * not used, but it is passed in case it may be useful.
  152.  */
  153. static void /* PRIVATE */
  154. png_default_warning(png_structp png_ptr, png_const_charp message)
  155. {
  156. #ifndef PNG_NO_CONSOLE_IO
  157.    fprintf(stderr, "libpng warning: %s\n", message);
  158. #else
  159.    if (message)
  160.      /* appease compiler */ ;
  161. #endif
  162.    if (png_ptr)
  163.       return;
  164. }
  165.  
  166. /* This function is called when the application wants to use another method
  167.  * of handling errors and warnings.  Note that the error function MUST NOT
  168.  * return to the calling routine or serious problems will occur.  The return
  169.  * method used in the default routine calls longjmp(png_ptr->jmpbuf, 1)
  170.  */
  171. void PNGAPI
  172. png_set_error_fn(png_structp png_ptr, png_voidp error_ptr,
  173.    png_error_ptr error_fn, png_error_ptr warning_fn)
  174. {
  175.    png_ptr->error_ptr = error_ptr;
  176.    png_ptr->error_fn = error_fn;
  177.    png_ptr->warning_fn = warning_fn;
  178. }
  179.  
  180.  
  181. /* This function returns a pointer to the error_ptr associated with the user
  182.  * functions.  The application should free any memory associated with this
  183.  * pointer before png_write_destroy and png_read_destroy are called.
  184.  */
  185. png_voidp PNGAPI
  186. png_get_error_ptr(png_structp png_ptr)
  187. {
  188.    return ((png_voidp)png_ptr->error_ptr);
  189. }
  190.  
  191.  
  192.  
  193.