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