2 /* pngerror.c - stub functions for i/o and memory allocation
4 * Last changed in libpng 1.2.22 [October 13, 2007]
5 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1998-2007 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.)
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
19 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
20 static void /* PRIVATE */
21 png_default_error PNGARG((png_structp png_ptr,
22 png_const_charp error_message));
23 #ifndef PNG_NO_WARNINGS
24 static void /* PRIVATE */
25 png_default_warning PNGARG((png_structp png_ptr,
26 png_const_charp warning_message));
27 #endif /* PNG_NO_WARNINGS */
29 /* This function is called whenever there is a fatal error. This function
30 * should not be changed. If there is a need to handle errors differently,
31 * you should supply a replacement error function and use png_set_error_fn()
32 * to replace the error function at run-time.
34 #ifndef PNG_NO_ERROR_TEXT
36 png_error(png_structp png_ptr, png_const_charp error_message)
38 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
43 (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))
45 if (*error_message == '#')
48 for (offset=1; offset<15; offset++)
49 if (*(error_message+offset) == ' ')
51 if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT)
54 for (i=0; i<offset-1; i++)
55 msg[i]=error_message[i+1];
60 error_message+=offset;
64 if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT)
74 if (png_ptr != NULL && png_ptr->error_fn != NULL)
75 (*(png_ptr->error_fn))(png_ptr, error_message);
77 /* If the custom handler doesn't exist, or if it returns,
78 use the default handler, which will not return. */
79 png_default_error(png_ptr, error_message);
83 png_err(png_structp png_ptr)
85 if (png_ptr != NULL && png_ptr->error_fn != NULL)
86 (*(png_ptr->error_fn))(png_ptr, '\0');
88 /* If the custom handler doesn't exist, or if it returns,
89 use the default handler, which will not return. */
90 png_default_error(png_ptr, '\0');
92 #endif /* PNG_NO_ERROR_TEXT */
94 #ifndef PNG_NO_WARNINGS
95 /* This function is called whenever there is a non-fatal error. This function
96 * should not be changed. If there is a need to handle warnings differently,
97 * you should supply a replacement warning function and use
98 * png_set_error_fn() to replace the warning function at run-time.
101 png_warning(png_structp png_ptr, png_const_charp warning_message)
106 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
108 (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))
111 if (*warning_message == '#')
113 for (offset=1; offset<15; offset++)
114 if (*(warning_message+offset) == ' ')
118 if (png_ptr != NULL && png_ptr->warning_fn != NULL)
119 (*(png_ptr->warning_fn))(png_ptr, warning_message+offset);
122 png_default_warning(png_ptr, warning_message+offset);
124 #endif /* PNG_NO_WARNINGS */
127 /* These utilities are used internally to build an error message that relates
128 * to the current chunk. The chunk name comes from png_ptr->chunk_name,
129 * this is used to prefix the message. The message is limited in length
130 * to 63 bytes, the name characters are output as hex digits wrapped in []
131 * if the character is invalid.
133 #define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
134 static PNG_CONST char png_digit[16] = {
135 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
136 'A', 'B', 'C', 'D', 'E', 'F'
139 #define PNG_MAX_ERROR_TEXT 64
141 #if !defined(PNG_NO_WARNINGS) || !defined(PNG_NO_ERROR_TEXT)
142 static void /* PRIVATE */
143 png_format_buffer(png_structp png_ptr, png_charp buffer, png_const_charp
146 int iout = 0, iin = 0;
150 int c = png_ptr->chunk_name[iin++];
153 buffer[iout++] = '[';
154 buffer[iout++] = png_digit[(c & 0xf0) >> 4];
155 buffer[iout++] = png_digit[c & 0x0f];
156 buffer[iout++] = ']';
160 buffer[iout++] = (png_byte)c;
164 if (error_message == NULL)
168 buffer[iout++] = ':';
169 buffer[iout++] = ' ';
170 png_memcpy(buffer+iout, error_message, PNG_MAX_ERROR_TEXT);
171 buffer[iout+PNG_MAX_ERROR_TEXT-1] = '\0';
175 #ifdef PNG_READ_SUPPORTED
177 png_chunk_error(png_structp png_ptr, png_const_charp error_message)
179 char msg[18+PNG_MAX_ERROR_TEXT];
181 png_error(png_ptr, error_message);
184 png_format_buffer(png_ptr, msg, error_message);
185 png_error(png_ptr, msg);
188 #endif /* PNG_READ_SUPPORTED */
189 #endif /* !defined(PNG_NO_WARNINGS) || !defined(PNG_NO_ERROR_TEXT) */
191 #ifndef PNG_NO_WARNINGS
193 png_chunk_warning(png_structp png_ptr, png_const_charp warning_message)
195 char msg[18+PNG_MAX_ERROR_TEXT];
197 png_warning(png_ptr, warning_message);
200 png_format_buffer(png_ptr, msg, warning_message);
201 png_warning(png_ptr, msg);
204 #endif /* PNG_NO_WARNINGS */
207 /* This is the default error handling function. Note that replacements for
208 * this function MUST NOT RETURN, or the program will likely crash. This
209 * function is used by default, or if the program supplies NULL for the
210 * error function pointer in png_set_error_fn().
212 static void /* PRIVATE */
213 png_default_error(png_structp png_ptr, png_const_charp error_message)
215 #ifndef PNG_NO_CONSOLE_IO
216 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
217 if (*error_message == '#')
220 char error_number[16];
221 for (offset=0; offset<15; offset++)
223 error_number[offset] = *(error_message+offset+1);
224 if (*(error_message+offset) == ' ')
227 if((offset > 1) && (offset < 15))
229 error_number[offset-1]='\0';
230 fprintf(stderr, "libpng error no. %s: %s\n", error_number,
231 error_message+offset);
234 fprintf(stderr, "libpng error: %s, offset=%d\n", error_message,offset);
238 fprintf(stderr, "libpng error: %s\n", error_message);
241 #ifdef PNG_SETJMP_SUPPORTED
244 # ifdef USE_FAR_KEYWORD
247 png_memcpy(jmpbuf, png_ptr->jmpbuf, png_sizeof(jmp_buf));
251 longjmp(png_ptr->jmpbuf, 1);
257 #ifdef PNG_NO_CONSOLE_IO
258 error_message = error_message; /* make compiler happy */
262 #ifndef PNG_NO_WARNINGS
263 /* This function is called when there is a warning, but the library thinks
264 * it can continue anyway. Replacement functions don't have to do anything
265 * here if you don't want them to. In the default configuration, png_ptr is
266 * not used, but it is passed in case it may be useful.
268 static void /* PRIVATE */
269 png_default_warning(png_structp png_ptr, png_const_charp warning_message)
271 #ifndef PNG_NO_CONSOLE_IO
272 # ifdef PNG_ERROR_NUMBERS_SUPPORTED
273 if (*warning_message == '#')
276 char warning_number[16];
277 for (offset=0; offset<15; offset++)
279 warning_number[offset]=*(warning_message+offset+1);
280 if (*(warning_message+offset) == ' ')
283 if((offset > 1) && (offset < 15))
285 warning_number[offset-1]='\0';
286 fprintf(stderr, "libpng warning no. %s: %s\n", warning_number,
287 warning_message+offset);
290 fprintf(stderr, "libpng warning: %s\n", warning_message);
294 fprintf(stderr, "libpng warning: %s\n", warning_message);
296 warning_message = warning_message; /* make compiler happy */
298 png_ptr = png_ptr; /* make compiler happy */
300 #endif /* PNG_NO_WARNINGS */
302 /* This function is called when the application wants to use another method
303 * of handling errors and warnings. Note that the error function MUST NOT
304 * return to the calling routine or serious problems will occur. The return
305 * method used in the default routine calls longjmp(png_ptr->jmpbuf, 1)
308 png_set_error_fn(png_structp png_ptr, png_voidp error_ptr,
309 png_error_ptr error_fn, png_error_ptr warning_fn)
313 png_ptr->error_ptr = error_ptr;
314 png_ptr->error_fn = error_fn;
315 png_ptr->warning_fn = warning_fn;
319 /* This function returns a pointer to the error_ptr associated with the user
320 * functions. The application should free any memory associated with this
321 * pointer before png_write_destroy and png_read_destroy are called.
324 png_get_error_ptr(png_structp png_ptr)
328 return ((png_voidp)png_ptr->error_ptr);
332 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
334 png_set_strip_error_numbers(png_structp png_ptr, png_uint_32 strip_mode)
339 ((~(PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))&strip_mode);
343 #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */