1 | |
2 | /* pngerror.c - stub functions for i/o and memory allocation |
3 | * |
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.) |
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 | #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 */ |
28 | |
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. |
33 | */ |
34 | #ifndef PNG_NO_ERROR_TEXT |
35 | void PNGAPI |
36 | png_error(png_structp png_ptr, png_const_charp error_message) |
37 | { |
38 | #ifdef PNG_ERROR_NUMBERS_SUPPORTED |
39 | char msg[16]; |
40 | if (png_ptr != NULL) |
41 | { |
42 | if (png_ptr->flags& |
43 | (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT)) |
44 | { |
45 | if (*error_message == '#') |
46 | { |
47 | int offset; |
48 | for (offset=1; offset<15; offset++) |
49 | if (*(error_message+offset) == ' ') |
50 | break; |
51 | if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT) |
52 | { |
53 | int i; |
54 | for (i=0; i<offset-1; i++) |
55 | msg[i]=error_message[i+1]; |
56 | msg[i]='\0'; |
57 | error_message=msg; |
58 | } |
59 | else |
60 | error_message+=offset; |
61 | } |
62 | else |
63 | { |
64 | if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT) |
65 | { |
66 | msg[0]='0'; |
67 | msg[1]='\0'; |
68 | error_message=msg; |
69 | } |
70 | } |
71 | } |
72 | } |
73 | #endif |
74 | if (png_ptr != NULL && png_ptr->error_fn != NULL) |
75 | (*(png_ptr->error_fn))(png_ptr, error_message); |
76 | |
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); |
80 | } |
81 | #else |
82 | void PNGAPI |
83 | png_err(png_structp png_ptr) |
84 | { |
85 | if (png_ptr != NULL && png_ptr->error_fn != NULL) |
86 | (*(png_ptr->error_fn))(png_ptr, '\0'); |
87 | |
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'); |
91 | } |
92 | #endif /* PNG_NO_ERROR_TEXT */ |
93 | |
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. |
99 | */ |
100 | void PNGAPI |
101 | png_warning(png_structp png_ptr, png_const_charp warning_message) |
102 | { |
103 | int offset = 0; |
104 | if (png_ptr != NULL) |
105 | { |
106 | #ifdef PNG_ERROR_NUMBERS_SUPPORTED |
107 | if (png_ptr->flags& |
108 | (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT)) |
109 | #endif |
110 | { |
111 | if (*warning_message == '#') |
112 | { |
113 | for (offset=1; offset<15; offset++) |
114 | if (*(warning_message+offset) == ' ') |
115 | break; |
116 | } |
117 | } |
118 | if (png_ptr != NULL && png_ptr->warning_fn != NULL) |
119 | (*(png_ptr->warning_fn))(png_ptr, warning_message+offset); |
120 | } |
121 | else |
122 | png_default_warning(png_ptr, warning_message+offset); |
123 | } |
124 | #endif /* PNG_NO_WARNINGS */ |
125 | |
126 | |
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. |
132 | */ |
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' |
137 | }; |
138 | |
139 | #define PNG_MAX_ERROR_TEXT 64 |
140 | |
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 |
144 | error_message) |
145 | { |
146 | int iout = 0, iin = 0; |
147 | |
148 | while (iin < 4) |
149 | { |
150 | int c = png_ptr->chunk_name[iin++]; |
151 | if (isnonalpha(c)) |
152 | { |
153 | buffer[iout++] = '['; |
154 | buffer[iout++] = png_digit[(c & 0xf0) >> 4]; |
155 | buffer[iout++] = png_digit[c & 0x0f]; |
156 | buffer[iout++] = ']'; |
157 | } |
158 | else |
159 | { |
160 | buffer[iout++] = (png_byte)c; |
161 | } |
162 | } |
163 | |
164 | if (error_message == NULL) |
165 | buffer[iout] = '\0'; |
166 | else |
167 | { |
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'; |
172 | } |
173 | } |
174 | |
175 | #ifdef PNG_READ_SUPPORTED |
176 | void PNGAPI |
177 | png_chunk_error(png_structp png_ptr, png_const_charp error_message) |
178 | { |
179 | char msg[18+PNG_MAX_ERROR_TEXT]; |
180 | if (png_ptr == NULL) |
181 | png_error(png_ptr, error_message); |
182 | else |
183 | { |
184 | png_format_buffer(png_ptr, msg, error_message); |
185 | png_error(png_ptr, msg); |
186 | } |
187 | } |
188 | #endif /* PNG_READ_SUPPORTED */ |
189 | #endif /* !defined(PNG_NO_WARNINGS) || !defined(PNG_NO_ERROR_TEXT) */ |
190 | |
191 | #ifndef PNG_NO_WARNINGS |
192 | void PNGAPI |
193 | png_chunk_warning(png_structp png_ptr, png_const_charp warning_message) |
194 | { |
195 | char msg[18+PNG_MAX_ERROR_TEXT]; |
196 | if (png_ptr == NULL) |
197 | png_warning(png_ptr, warning_message); |
198 | else |
199 | { |
200 | png_format_buffer(png_ptr, msg, warning_message); |
201 | png_warning(png_ptr, msg); |
202 | } |
203 | } |
204 | #endif /* PNG_NO_WARNINGS */ |
205 | |
206 | |
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(). |
211 | */ |
212 | static void /* PRIVATE */ |
213 | png_default_error(png_structp png_ptr, png_const_charp error_message) |
214 | { |
215 | #ifndef PNG_NO_CONSOLE_IO |
216 | #ifdef PNG_ERROR_NUMBERS_SUPPORTED |
217 | if (*error_message == '#') |
218 | { |
219 | int offset; |
220 | char error_number[16]; |
221 | for (offset=0; offset<15; offset++) |
222 | { |
223 | error_number[offset] = *(error_message+offset+1); |
224 | if (*(error_message+offset) == ' ') |
225 | break; |
226 | } |
227 | if((offset > 1) && (offset < 15)) |
228 | { |
229 | error_number[offset-1]='\0'; |
230 | fprintf(stderr, "libpng error no. %s: %s\n" , error_number, |
231 | error_message+offset); |
232 | } |
233 | else |
234 | fprintf(stderr, "libpng error: %s, offset=%d\n" , error_message,offset); |
235 | } |
236 | else |
237 | #endif |
238 | fprintf(stderr, "libpng error: %s\n" , error_message); |
239 | #endif |
240 | |
241 | #ifdef PNG_SETJMP_SUPPORTED |
242 | if (png_ptr) |
243 | { |
244 | # ifdef USE_FAR_KEYWORD |
245 | { |
246 | jmp_buf jmpbuf; |
247 | png_memcpy(jmpbuf, png_ptr->jmpbuf, png_sizeof(jmp_buf)); |
248 | longjmp(jmpbuf, 1); |
249 | } |
250 | # else |
251 | longjmp(png_ptr->jmpbuf, 1); |
252 | # endif |
253 | } |
254 | #else |
255 | PNG_ABORT(); |
256 | #endif |
257 | #ifdef PNG_NO_CONSOLE_IO |
258 | error_message = error_message; /* make compiler happy */ |
259 | #endif |
260 | } |
261 | |
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. |
267 | */ |
268 | static void /* PRIVATE */ |
269 | png_default_warning(png_structp png_ptr, png_const_charp warning_message) |
270 | { |
271 | #ifndef PNG_NO_CONSOLE_IO |
272 | # ifdef PNG_ERROR_NUMBERS_SUPPORTED |
273 | if (*warning_message == '#') |
274 | { |
275 | int offset; |
276 | char warning_number[16]; |
277 | for (offset=0; offset<15; offset++) |
278 | { |
279 | warning_number[offset]=*(warning_message+offset+1); |
280 | if (*(warning_message+offset) == ' ') |
281 | break; |
282 | } |
283 | if((offset > 1) && (offset < 15)) |
284 | { |
285 | warning_number[offset-1]='\0'; |
286 | fprintf(stderr, "libpng warning no. %s: %s\n" , warning_number, |
287 | warning_message+offset); |
288 | } |
289 | else |
290 | fprintf(stderr, "libpng warning: %s\n" , warning_message); |
291 | } |
292 | else |
293 | # endif |
294 | fprintf(stderr, "libpng warning: %s\n" , warning_message); |
295 | #else |
296 | warning_message = warning_message; /* make compiler happy */ |
297 | #endif |
298 | png_ptr = png_ptr; /* make compiler happy */ |
299 | } |
300 | #endif /* PNG_NO_WARNINGS */ |
301 | |
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) |
306 | */ |
307 | void PNGAPI |
308 | png_set_error_fn(png_structp png_ptr, png_voidp error_ptr, |
309 | png_error_ptr error_fn, png_error_ptr warning_fn) |
310 | { |
311 | if (png_ptr == NULL) |
312 | return; |
313 | png_ptr->error_ptr = error_ptr; |
314 | png_ptr->error_fn = error_fn; |
315 | png_ptr->warning_fn = warning_fn; |
316 | } |
317 | |
318 | |
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. |
322 | */ |
323 | png_voidp PNGAPI |
324 | png_get_error_ptr(png_structp png_ptr) |
325 | { |
326 | if (png_ptr == NULL) |
327 | return NULL; |
328 | return ((png_voidp)png_ptr->error_ptr); |
329 | } |
330 | |
331 | |
332 | #ifdef PNG_ERROR_NUMBERS_SUPPORTED |
333 | void PNGAPI |
334 | png_set_strip_error_numbers(png_structp png_ptr, png_uint_32 strip_mode) |
335 | { |
336 | if(png_ptr != NULL) |
337 | { |
338 | png_ptr->flags &= |
339 | ((~(PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))&strip_mode); |
340 | } |
341 | } |
342 | #endif |
343 | #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */ |
344 | |