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