home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 February / PCWorld_2002-02_cd.bin / Software / Vyzkuste / pdflib / pdflib-4.0.1.sit / pdflib-4.0.1 / png / pngerror.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-07-04  |  5.7 KB  |  194 lines  |  [TEXT/CWIE]

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