Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1.  
  2. /* pngerror.c - stub functions for i/o and memory allocation
  3.  *
  4.  * Last changed in libpng 1.5.1 [February 3, 2011]
  5.  * Copyright (c) 1998-2011 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 code is released under the libpng license.
  10.  * For conditions of distribution and use, see the disclaimer
  11.  * and license in png.h
  12.  *
  13.  * This file provides a location for all error handling.  Users who
  14.  * need special error handling are expected to write replacement functions
  15.  * and use png_set_error_fn() to use those functions.  See the instructions
  16.  * at each function.
  17.  */
  18.  
  19. #include "pngpriv.h"
  20.  
  21. #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
  22.  
  23. static PNG_FUNCTION(void, png_default_error,PNGARG((png_structp png_ptr,
  24.     png_const_charp error_message)),PNG_NORETURN);
  25.  
  26. #ifdef PNG_WARNINGS_SUPPORTED
  27. static void /* PRIVATE */
  28. png_default_warning PNGARG((png_structp png_ptr,
  29.    png_const_charp warning_message));
  30. #endif /* PNG_WARNINGS_SUPPORTED */
  31.  
  32. /* This function is called whenever there is a fatal error.  This function
  33.  * should not be changed.  If there is a need to handle errors differently,
  34.  * you should supply a replacement error function and use png_set_error_fn()
  35.  * to replace the error function at run-time.
  36.  */
  37. #ifdef PNG_ERROR_TEXT_SUPPORTED
  38. PNG_FUNCTION(void,PNGAPI
  39. png_error,(png_structp png_ptr, png_const_charp error_message),PNG_NORETURN)
  40. {
  41. #ifdef PNG_ERROR_NUMBERS_SUPPORTED
  42.    char msg[16];
  43.    if (png_ptr != NULL)
  44.    {
  45.       if (png_ptr->flags&
  46.          (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))
  47.       {
  48.          if (*error_message == PNG_LITERAL_SHARP)
  49.          {
  50.             /* Strip "#nnnn " from beginning of error message. */
  51.             int offset;
  52.             for (offset = 1; offset<15; offset++)
  53.                if (error_message[offset] == ' ')
  54.                   break;
  55.  
  56.             if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT)
  57.             {
  58.                int i;
  59.                for (i = 0; i < offset - 1; i++)
  60.                   msg[i] = error_message[i + 1];
  61.                msg[i - 1] = '\0';
  62.                error_message = msg;
  63.             }
  64.  
  65.             else
  66.                error_message += offset;
  67.       }
  68.  
  69.       else
  70.       {
  71.          if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT)
  72.          {
  73.             msg[0] = '0';
  74.             msg[1] = '\0';
  75.             error_message = msg;
  76.          }
  77.        }
  78.      }
  79.    }
  80. #endif
  81.    if (png_ptr != NULL && png_ptr->error_fn != NULL)
  82.       (*(png_ptr->error_fn))(png_ptr, error_message);
  83.  
  84.    /* If the custom handler doesn't exist, or if it returns,
  85.       use the default handler, which will not return. */
  86.    png_default_error(png_ptr, error_message);
  87. }
  88. #else
  89. PNG_FUNCTION(void,PNGAPI
  90. png_err,(png_structp png_ptr),PNG_NORETURN)
  91. {
  92.    if (png_ptr != NULL && png_ptr->error_fn != NULL)
  93.       (*(png_ptr->error_fn))(png_ptr, '\0');
  94.  
  95.    /* If the custom handler doesn't exist, or if it returns,
  96.       use the default handler, which will not return. */
  97.    png_default_error(png_ptr, '\0');
  98. }
  99. #endif /* PNG_ERROR_TEXT_SUPPORTED */
  100.  
  101. #ifdef PNG_WARNINGS_SUPPORTED
  102. /* This function is called whenever there is a non-fatal error.  This function
  103.  * should not be changed.  If there is a need to handle warnings differently,
  104.  * you should supply a replacement warning function and use
  105.  * png_set_error_fn() to replace the warning function at run-time.
  106.  */
  107. void PNGAPI
  108. png_warning(png_structp png_ptr, png_const_charp warning_message)
  109. {
  110.    int offset = 0;
  111.    if (png_ptr != NULL)
  112.    {
  113. #ifdef PNG_ERROR_NUMBERS_SUPPORTED
  114.    if (png_ptr->flags&
  115.        (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))
  116. #endif
  117.       {
  118.          if (*warning_message == PNG_LITERAL_SHARP)
  119.          {
  120.             for (offset = 1; offset < 15; offset++)
  121.                if (warning_message[offset] == ' ')
  122.                   break;
  123.          }
  124.       }
  125.    }
  126.    if (png_ptr != NULL && png_ptr->warning_fn != NULL)
  127.       (*(png_ptr->warning_fn))(png_ptr, warning_message + offset);
  128.    else
  129.       png_default_warning(png_ptr, warning_message + offset);
  130. }
  131. #endif /* PNG_WARNINGS_SUPPORTED */
  132.  
  133. #ifdef PNG_BENIGN_ERRORS_SUPPORTED
  134. void PNGAPI
  135. png_benign_error(png_structp png_ptr, png_const_charp error_message)
  136. {
  137.   if (png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN)
  138.      png_warning(png_ptr, error_message);
  139.   else
  140.      png_error(png_ptr, error_message);
  141. }
  142. #endif
  143.  
  144. /* These utilities are used internally to build an error message that relates
  145.  * to the current chunk.  The chunk name comes from png_ptr->chunk_name,
  146.  * this is used to prefix the message.  The message is limited in length
  147.  * to 63 bytes, the name characters are output as hex digits wrapped in []
  148.  * if the character is invalid.
  149.  */
  150. #define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
  151. static PNG_CONST char png_digit[16] = {
  152.    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  153.    'A', 'B', 'C', 'D', 'E', 'F'
  154. };
  155.  
  156. #define PNG_MAX_ERROR_TEXT 64
  157. #if defined(PNG_WARNINGS_SUPPORTED) || defined(PNG_ERROR_TEXT_SUPPORTED)
  158. static void /* PRIVATE */
  159. png_format_buffer(png_structp png_ptr, png_charp buffer, png_const_charp
  160.     error_message)
  161. {
  162.    int iout = 0, iin = 0;
  163.  
  164.    while (iin < 4)
  165.    {
  166.       int c = png_ptr->chunk_name[iin++];
  167.       if (isnonalpha(c))
  168.       {
  169.          buffer[iout++] = PNG_LITERAL_LEFT_SQUARE_BRACKET;
  170.          buffer[iout++] = png_digit[(c & 0xf0) >> 4];
  171.          buffer[iout++] = png_digit[c & 0x0f];
  172.          buffer[iout++] = PNG_LITERAL_RIGHT_SQUARE_BRACKET;
  173.       }
  174.  
  175.       else
  176.       {
  177.          buffer[iout++] = (png_byte)c;
  178.       }
  179.    }
  180.  
  181.    if (error_message == NULL)
  182.       buffer[iout] = '\0';
  183.  
  184.    else
  185.    {
  186.       buffer[iout++] = ':';
  187.       buffer[iout++] = ' ';
  188.       png_memcpy(buffer + iout, error_message, PNG_MAX_ERROR_TEXT);
  189.       buffer[iout + PNG_MAX_ERROR_TEXT - 1] = '\0';
  190.    }
  191. }
  192. #endif /* PNG_WARNINGS_SUPPORTED || PNG_ERROR_TEXT_SUPPORTED */
  193.  
  194. #if defined(PNG_READ_SUPPORTED) && defined(PNG_ERROR_TEXT_SUPPORTED)
  195. PNG_FUNCTION(void,PNGAPI
  196. png_chunk_error,(png_structp png_ptr, png_const_charp error_message),
  197.    PNG_NORETURN)
  198. {
  199.    char msg[18+PNG_MAX_ERROR_TEXT];
  200.    if (png_ptr == NULL)
  201.       png_error(png_ptr, error_message);
  202.  
  203.    else
  204.    {
  205.       png_format_buffer(png_ptr, msg, error_message);
  206.       png_error(png_ptr, msg);
  207.    }
  208. }
  209. #endif /* PNG_READ_SUPPORTED && PNG_ERROR_TEXT_SUPPORTED */
  210.  
  211. #ifdef PNG_WARNINGS_SUPPORTED
  212. void PNGAPI
  213. png_chunk_warning(png_structp png_ptr, png_const_charp warning_message)
  214. {
  215.    char msg[18+PNG_MAX_ERROR_TEXT];
  216.    if (png_ptr == NULL)
  217.       png_warning(png_ptr, warning_message);
  218.  
  219.    else
  220.    {
  221.       png_format_buffer(png_ptr, msg, warning_message);
  222.       png_warning(png_ptr, msg);
  223.    }
  224. }
  225. #endif /* PNG_WARNINGS_SUPPORTED */
  226.  
  227. #ifdef PNG_READ_SUPPORTED
  228. #ifdef PNG_BENIGN_ERRORS_SUPPORTED
  229. void PNGAPI
  230. png_chunk_benign_error(png_structp png_ptr, png_const_charp error_message)
  231. {
  232.    if (png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN)
  233.       png_chunk_warning(png_ptr, error_message);
  234.  
  235.    else
  236.       png_chunk_error(png_ptr, error_message);
  237. }
  238. #endif
  239. #endif /* PNG_READ_SUPPORTED */
  240.  
  241. #ifdef PNG_ERROR_TEXT_SUPPORTED
  242. #ifdef PNG_FLOATING_POINT_SUPPORTED
  243. PNG_FUNCTION(void,
  244. png_fixed_error,(png_structp png_ptr, png_const_charp name),PNG_NORETURN)
  245. {
  246. #  define fixed_message "fixed point overflow in "
  247. #  define fixed_message_ln ((sizeof fixed_message)-1)
  248.    int  iin;
  249.    char msg[fixed_message_ln+PNG_MAX_ERROR_TEXT];
  250.    png_memcpy(msg, fixed_message, fixed_message_ln);
  251.    iin = 0;
  252.    if (name != NULL) while (iin < (PNG_MAX_ERROR_TEXT-1) && name[iin] != 0)
  253.    {
  254.       msg[fixed_message_ln + iin] = name[iin];
  255.       ++iin;
  256.    }
  257.    msg[fixed_message_ln + iin] = 0;
  258.    png_error(png_ptr, msg);
  259. }
  260. #endif
  261. #endif
  262.  
  263. #ifdef PNG_SETJMP_SUPPORTED
  264. /* This API only exists if ANSI-C style error handling is used,
  265.  * otherwise it is necessary for png_default_error to be overridden.
  266.  */
  267. jmp_buf* PNGAPI
  268. png_set_longjmp_fn(png_structp png_ptr, png_longjmp_ptr longjmp_fn,
  269.     size_t jmp_buf_size)
  270. {
  271.    if (png_ptr == NULL || jmp_buf_size != png_sizeof(jmp_buf))
  272.       return NULL;
  273.  
  274.    png_ptr->longjmp_fn = longjmp_fn;
  275.    return &png_ptr->png_jmpbuf;
  276. }
  277. #endif
  278.  
  279. /* This is the default error handling function.  Note that replacements for
  280.  * this function MUST NOT RETURN, or the program will likely crash.  This
  281.  * function is used by default, or if the program supplies NULL for the
  282.  * error function pointer in png_set_error_fn().
  283.  */
  284. static PNG_FUNCTION(void /* PRIVATE */,
  285. png_default_error,(png_structp png_ptr, png_const_charp error_message),
  286.    PNG_NORETURN)
  287. {
  288. #ifdef PNG_CONSOLE_IO_SUPPORTED
  289. #ifdef PNG_ERROR_NUMBERS_SUPPORTED
  290.    if (*error_message == PNG_LITERAL_SHARP)
  291.    {
  292.       /* Strip "#nnnn " from beginning of error message. */
  293.       int offset;
  294.       char error_number[16];
  295.       for (offset = 0; offset<15; offset++)
  296.       {
  297.          error_number[offset] = error_message[offset + 1];
  298.          if (error_message[offset] == ' ')
  299.             break;
  300.       }
  301.  
  302.       if ((offset > 1) && (offset < 15))
  303.       {
  304.          error_number[offset - 1] = '\0';
  305.          fprintf(stderr, "libpng error no. %s: %s",
  306.              error_number, error_message + offset + 1);
  307.          fprintf(stderr, PNG_STRING_NEWLINE);
  308.       }
  309.  
  310.       else
  311.       {
  312.          fprintf(stderr, "libpng error: %s, offset=%d",
  313.              error_message, offset);
  314.          fprintf(stderr, PNG_STRING_NEWLINE);
  315.       }
  316.    }
  317.    else
  318. #endif
  319.    {
  320.       fprintf(stderr, "libpng error: %s", error_message);
  321.       fprintf(stderr, PNG_STRING_NEWLINE);
  322.    }
  323. #endif
  324. #ifndef PNG_CONSOLE_IO_SUPPORTED
  325.    PNG_UNUSED(error_message) /* Make compiler happy */
  326. #endif
  327.    png_longjmp(png_ptr, 1);
  328. }
  329.  
  330. PNG_FUNCTION(void,PNGAPI
  331. png_longjmp,(png_structp png_ptr, int val),PNG_NORETURN)
  332. {
  333. #ifdef PNG_SETJMP_SUPPORTED
  334.    if (png_ptr && png_ptr->longjmp_fn)
  335.    {
  336. #  ifdef USE_FAR_KEYWORD
  337.       {
  338.          jmp_buf png_jmpbuf;
  339.          png_memcpy(png_jmpbuf, png_ptr->png_jmpbuf, png_sizeof(jmp_buf));
  340.          png_ptr->longjmp_fn(png_jmpbuf, val);
  341.       }
  342.  
  343. #  else
  344.    png_ptr->longjmp_fn(png_ptr->png_jmpbuf, val);
  345. #  endif
  346.    }
  347. #endif
  348.    /* Here if not setjmp support or if png_ptr is null. */
  349.    PNG_ABORT();
  350. }
  351.  
  352. #ifdef PNG_WARNINGS_SUPPORTED
  353. /* This function is called when there is a warning, but the library thinks
  354.  * it can continue anyway.  Replacement functions don't have to do anything
  355.  * here if you don't want them to.  In the default configuration, png_ptr is
  356.  * not used, but it is passed in case it may be useful.
  357.  */
  358. static void /* PRIVATE */
  359. png_default_warning(png_structp png_ptr, png_const_charp warning_message)
  360. {
  361. #ifdef PNG_CONSOLE_IO_SUPPORTED
  362. #  ifdef PNG_ERROR_NUMBERS_SUPPORTED
  363.    if (*warning_message == PNG_LITERAL_SHARP)
  364.    {
  365.       int offset;
  366.       char warning_number[16];
  367.       for (offset = 0; offset < 15; offset++)
  368.       {
  369.          warning_number[offset] = warning_message[offset + 1];
  370.          if (warning_message[offset] == ' ')
  371.             break;
  372.       }
  373.  
  374.       if ((offset > 1) && (offset < 15))
  375.       {
  376.          warning_number[offset + 1] = '\0';
  377.          fprintf(stderr, "libpng warning no. %s: %s",
  378.              warning_number, warning_message + offset);
  379.          fprintf(stderr, PNG_STRING_NEWLINE);
  380.       }
  381.  
  382.       else
  383.       {
  384.          fprintf(stderr, "libpng warning: %s",
  385.              warning_message);
  386.          fprintf(stderr, PNG_STRING_NEWLINE);
  387.       }
  388.    }
  389.    else
  390. #  endif
  391.  
  392.    {
  393.       fprintf(stderr, "libpng warning: %s", warning_message);
  394.       fprintf(stderr, PNG_STRING_NEWLINE);
  395.    }
  396. #else
  397.    PNG_UNUSED(warning_message) /* Make compiler happy */
  398. #endif
  399.    PNG_UNUSED(png_ptr) /* Make compiler happy */
  400. }
  401. #endif /* PNG_WARNINGS_SUPPORTED */
  402.  
  403. /* This function is called when the application wants to use another method
  404.  * of handling errors and warnings.  Note that the error function MUST NOT
  405.  * return to the calling routine or serious problems will occur.  The return
  406.  * method used in the default routine calls longjmp(png_ptr->png_jmpbuf, 1)
  407.  */
  408. void PNGAPI
  409. png_set_error_fn(png_structp png_ptr, png_voidp error_ptr,
  410.     png_error_ptr error_fn, png_error_ptr warning_fn)
  411. {
  412.    if (png_ptr == NULL)
  413.       return;
  414.  
  415.    png_ptr->error_ptr = error_ptr;
  416.    png_ptr->error_fn = error_fn;
  417.    png_ptr->warning_fn = warning_fn;
  418. }
  419.  
  420.  
  421. /* This function returns a pointer to the error_ptr associated with the user
  422.  * functions.  The application should free any memory associated with this
  423.  * pointer before png_write_destroy and png_read_destroy are called.
  424.  */
  425. png_voidp PNGAPI
  426. png_get_error_ptr(png_const_structp png_ptr)
  427. {
  428.    if (png_ptr == NULL)
  429.       return NULL;
  430.  
  431.    return ((png_voidp)png_ptr->error_ptr);
  432. }
  433.  
  434.  
  435. #ifdef PNG_ERROR_NUMBERS_SUPPORTED
  436. void PNGAPI
  437. png_set_strip_error_numbers(png_structp png_ptr, png_uint_32 strip_mode)
  438. {
  439.    if (png_ptr != NULL)
  440.    {
  441.       png_ptr->flags &=
  442.          ((~(PNG_FLAG_STRIP_ERROR_NUMBERS |
  443.          PNG_FLAG_STRIP_ERROR_TEXT))&strip_mode);
  444.    }
  445. }
  446. #endif
  447. #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */
  448.