pngerror.c
Upload User: kairuinn
Upload Date: 2009-02-07
Package Size: 2922k
Code Size: 9k
Category:

Graph program

Development Platform:

Visual C++

  1. /* pngerror.c - stub functions for i/o and memory allocation
  2.  *
  3.  * libpng 1.2.5 - October 3, 2002
  4.  * For conditions of distribution and use, see copyright notice in png.h
  5.  * Copyright (c) 1998-2002 Glenn Randers-Pehrson
  6.  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  7.  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
  8.  *
  9.  * This file provides a location for all error handling.  Users who
  10.  * need special error handling are expected to write replacement functions
  11.  * and use png_set_error_fn() to use those functions.  See the instructions
  12.  * at each function.
  13.  */
  14. #define PNG_INTERNAL
  15. #include "png.h"
  16. static void /* PRIVATE */
  17. png_default_error PNGARG((png_structp png_ptr,
  18.   png_const_charp error_message));
  19. static void /* PRIVATE */
  20. png_default_warning PNGARG((png_structp png_ptr,
  21.   png_const_charp warning_message));
  22. /* This function is called whenever there is a fatal error.  This function
  23.  * should not be changed.  If there is a need to handle errors differently,
  24.  * you should supply a replacement error function and use png_set_error_fn()
  25.  * to replace the error function at run-time.
  26.  */
  27. void PNGAPI
  28. png_error(png_structp png_ptr, png_const_charp error_message)
  29. {
  30. #ifdef PNG_ERROR_NUMBERS_SUPPORTED
  31.    char msg[16];
  32.    if (png_ptr->flags&(PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))
  33.    {
  34.      int offset = 0;
  35.      if (*error_message == '#')
  36.      {
  37.          for (offset=1; offset<15; offset++)
  38.             if (*(error_message+offset) == ' ')
  39.                 break;
  40.          if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT)
  41.          {
  42.             int i;
  43.             for (i=0; i<offset-1; i++)
  44.                msg[i]=error_message[i+1];
  45.             msg[i]='';
  46.             error_message=msg;
  47.          }
  48.          else
  49.             error_message+=offset;
  50.      }
  51.      else
  52.      {
  53.          if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT)
  54.          {
  55.             msg[0]='0';        
  56.             msg[1]='';
  57.             error_message=msg;
  58.          }
  59.      }
  60.    }
  61. #endif
  62.    if (png_ptr->error_fn != NULL)
  63.       (*(png_ptr->error_fn))(png_ptr, error_message);
  64.    /* if the following returns or doesn't exist, use the default function,
  65.       which will not return */
  66.    png_default_error(png_ptr, error_message);
  67. }
  68. /* This function is called whenever there is a non-fatal error.  This function
  69.  * should not be changed.  If there is a need to handle warnings differently,
  70.  * you should supply a replacement warning function and use
  71.  * png_set_error_fn() to replace the warning function at run-time.
  72.  */
  73. void PNGAPI
  74. png_warning(png_structp png_ptr, png_const_charp warning_message)
  75. {
  76.      int offset = 0;
  77. #ifdef PNG_ERROR_NUMBERS_SUPPORTED
  78.    if (png_ptr->flags&(PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))
  79. #endif
  80.    {
  81.      if (*warning_message == '#')
  82.      {
  83.          for (offset=1; offset<15; offset++)
  84.             if (*(warning_message+offset) == ' ')
  85.                 break;
  86.      }
  87.    }
  88.    if (png_ptr->warning_fn != NULL)
  89.       (*(png_ptr->warning_fn))(png_ptr,
  90.          (png_const_charp)(warning_message+offset));
  91.    else
  92.       png_default_warning(png_ptr, (png_const_charp)(warning_message+offset));
  93. }
  94. /* These utilities are used internally to build an error message that relates
  95.  * to the current chunk.  The chunk name comes from png_ptr->chunk_name,
  96.  * this is used to prefix the message.  The message is limited in length
  97.  * to 63 bytes, the name characters are output as hex digits wrapped in []
  98.  * if the character is invalid.
  99.  */
  100. #define isnonalpha(c) ((c) < 41 || (c) > 122 || ((c) > 90 && (c) < 97))
  101. static PNG_CONST char png_digit[16] = {
  102.    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
  103.    'F' };
  104. static void /* PRIVATE */
  105. png_format_buffer(png_structp png_ptr, png_charp buffer, png_const_charp
  106.    error_message)
  107. {
  108.    int iout = 0, iin = 0;
  109.    while (iin < 4)
  110.    {
  111.       int c = png_ptr->chunk_name[iin++];
  112.       if (isnonalpha(c))
  113.       {
  114.          buffer[iout++] = '[';
  115.          buffer[iout++] = png_digit[(c & 0xf0) >> 4];
  116.          buffer[iout++] = png_digit[c & 0x0f];
  117.          buffer[iout++] = ']';
  118.       }
  119.       else
  120.       {
  121.          buffer[iout++] = (png_byte)c;
  122.       }
  123.    }
  124.    if (error_message == NULL)
  125.       buffer[iout] = 0;
  126.    else
  127.    {
  128.       buffer[iout++] = ':';
  129.       buffer[iout++] = ' ';
  130.       png_memcpy(buffer+iout, error_message, 64);
  131.       buffer[iout+63] = 0;
  132.    }
  133. }
  134. void PNGAPI
  135. png_chunk_error(png_structp png_ptr, png_const_charp error_message)
  136. {
  137.    char msg[18+64];
  138.    png_format_buffer(png_ptr, msg, error_message);
  139.    png_error(png_ptr, msg);
  140. }
  141. void PNGAPI
  142. png_chunk_warning(png_structp png_ptr, png_const_charp warning_message)
  143. {
  144.    char msg[18+64];
  145.    png_format_buffer(png_ptr, msg, warning_message);
  146.    png_warning(png_ptr, msg);
  147. }
  148. /* This is the default error handling function.  Note that replacements for
  149.  * this function MUST NOT RETURN, or the program will likely crash.  This
  150.  * function is used by default, or if the program supplies NULL for the
  151.  * error function pointer in png_set_error_fn().
  152.  */
  153. static void /* PRIVATE */
  154. png_default_error(png_structp png_ptr, png_const_charp error_message)
  155. {
  156. #ifndef PNG_NO_CONSOLE_IO
  157. #ifdef PNG_ERROR_NUMBERS_SUPPORTED
  158.    if (*error_message == '#')
  159.    {
  160.      int offset;
  161.      char error_number[16];
  162.      for (offset=0; offset<15; offset++)
  163.      {
  164.          error_number[offset] = *(error_message+offset+1);
  165.          if (*(error_message+offset) == ' ')
  166.              break;
  167.      }
  168.      if((offset > 1) && (offset < 15))
  169.      {
  170.        error_number[offset-1]='';
  171.        fprintf(stderr, "libpng error no. %s: %sn", error_number,
  172.           error_message+offset);
  173.      }
  174.      else
  175.        fprintf(stderr, "libpng error: %s, offset=%dn", error_message,offset);
  176.    }
  177.    else
  178. #endif
  179.    fprintf(stderr, "libpng error: %sn", error_message);
  180. #else
  181.    if (error_message)
  182.      /* make compiler happy */ ;
  183. #endif
  184. #ifdef PNG_SETJMP_SUPPORTED
  185. #  ifdef USE_FAR_KEYWORD
  186.    {
  187.       jmp_buf jmpbuf;
  188.       png_memcpy(jmpbuf,png_ptr->jmpbuf,sizeof(jmp_buf));
  189.       longjmp(jmpbuf, 1);
  190.    }
  191. #  else
  192.    longjmp(png_ptr->jmpbuf, 1);
  193. # endif
  194. #else
  195.    if (png_ptr)
  196.      /* make compiler happy */ ;
  197.    PNG_ABORT();
  198. #endif
  199. }
  200. /* This function is called when there is a warning, but the library thinks
  201.  * it can continue anyway.  Replacement functions don't have to do anything
  202.  * here if you don't want them to.  In the default configuration, png_ptr is
  203.  * not used, but it is passed in case it may be useful.
  204.  */
  205. static void /* PRIVATE */
  206. png_default_warning(png_structp png_ptr, png_const_charp warning_message)
  207. {
  208. #ifndef PNG_NO_CONSOLE_IO
  209. #  ifdef PNG_ERROR_NUMBERS_SUPPORTED
  210.    if (*warning_message == '#')
  211.    {
  212.      int offset;
  213.      char warning_number[16];
  214.      for (offset=0; offset<15; offset++)
  215.      {
  216.         warning_number[offset]=*(warning_message+offset+1);
  217.         if (*(warning_message+offset) == ' ')
  218.             break;
  219.      }
  220.      if((offset > 1) && (offset < 15))
  221.      {
  222.        warning_number[offset-1]='';
  223.        fprintf(stderr, "libpng warning no. %s: %sn", warning_number,
  224.           warning_message+offset);
  225.      }
  226.      else
  227.        fprintf(stderr, "libpng warning: %sn", warning_message);
  228.    }
  229.    else
  230. #  endif
  231.      fprintf(stderr, "libpng warning: %sn", warning_message);
  232. #else
  233.    if (warning_message)
  234.      /* appease compiler */ ;
  235. #endif
  236.    if (png_ptr)
  237.       return;
  238. }
  239. /* This function is called when the application wants to use another method
  240.  * of handling errors and warnings.  Note that the error function MUST NOT
  241.  * return to the calling routine or serious problems will occur.  The return
  242.  * method used in the default routine calls longjmp(png_ptr->jmpbuf, 1)
  243.  */
  244. void PNGAPI
  245. png_set_error_fn(png_structp png_ptr, png_voidp error_ptr,
  246.    png_error_ptr error_fn, png_error_ptr warning_fn)
  247. {
  248.    png_ptr->error_ptr = error_ptr;
  249.    png_ptr->error_fn = error_fn;
  250.    png_ptr->warning_fn = warning_fn;
  251. }
  252. /* This function returns a pointer to the error_ptr associated with the user
  253.  * functions.  The application should free any memory associated with this
  254.  * pointer before png_write_destroy and png_read_destroy are called.
  255.  */
  256. png_voidp PNGAPI
  257. png_get_error_ptr(png_structp png_ptr)
  258. {
  259.    return ((png_voidp)png_ptr->error_ptr);
  260. }
  261. #ifdef PNG_ERROR_NUMBERS_SUPPORTED
  262. void PNGAPI
  263. png_set_strip_error_numbers(png_structp png_ptr, png_uint_32 strip_mode)
  264. {
  265.    if(png_ptr != NULL)
  266.    {
  267.      png_ptr->flags &=
  268.        ((~(PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))&strip_mode);
  269.    }
  270. }
  271. #endif