]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/png/pngerror.c
using common code
[wxWidgets.git] / src / png / pngerror.c
... / ...
CommitLineData
1
2/* pngerror.c - stub functions for i/o and memory allocation
3 *
4 * Last changed in libpng 1.2.20 September 8, 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)
20static void /* PRIVATE */
21png_default_error PNGARG((png_structp png_ptr,
22 png_const_charp error_message));
23#ifndef PNG_NO_WARNINGS
24static void /* PRIVATE */
25png_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
35void PNGAPI
36png_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
82void PNGAPI
83png_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 */
100void PNGAPI
101png_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))
134static 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#if !defined(PNG_NO_WARNINGS) || !defined(PNG_NO_ERROR_TEXT)
140static void /* PRIVATE */
141png_format_buffer(png_structp png_ptr, png_charp buffer, png_const_charp
142 error_message)
143{
144 int iout = 0, iin = 0;
145
146 while (iin < 4)
147 {
148 int c = png_ptr->chunk_name[iin++];
149 if (isnonalpha(c))
150 {
151 buffer[iout++] = '[';
152 buffer[iout++] = png_digit[(c & 0xf0) >> 4];
153 buffer[iout++] = png_digit[c & 0x0f];
154 buffer[iout++] = ']';
155 }
156 else
157 {
158 buffer[iout++] = (png_byte)c;
159 }
160 }
161
162 if (error_message == NULL)
163 buffer[iout] = 0;
164 else
165 {
166 buffer[iout++] = ':';
167 buffer[iout++] = ' ';
168 png_strncpy(buffer+iout, error_message, 63);
169 buffer[iout+63] = 0;
170 }
171}
172
173#ifdef PNG_READ_SUPPORTED
174void PNGAPI
175png_chunk_error(png_structp png_ptr, png_const_charp error_message)
176{
177 char msg[18+64];
178 if (png_ptr == NULL)
179 png_error(png_ptr, error_message);
180 else
181 {
182 png_format_buffer(png_ptr, msg, error_message);
183 png_error(png_ptr, msg);
184 }
185}
186#endif /* PNG_READ_SUPPORTED */
187#endif /* !defined(PNG_NO_WARNINGS) || !defined(PNG_NO_ERROR_TEXT) */
188
189#ifndef PNG_NO_WARNINGS
190void PNGAPI
191png_chunk_warning(png_structp png_ptr, png_const_charp warning_message)
192{
193 char msg[18+64];
194 if (png_ptr == NULL)
195 png_warning(png_ptr, warning_message);
196 else
197 {
198 png_format_buffer(png_ptr, msg, warning_message);
199 png_warning(png_ptr, msg);
200 }
201}
202#endif /* PNG_NO_WARNINGS */
203
204
205/* This is the default error handling function. Note that replacements for
206 * this function MUST NOT RETURN, or the program will likely crash. This
207 * function is used by default, or if the program supplies NULL for the
208 * error function pointer in png_set_error_fn().
209 */
210static void /* PRIVATE */
211png_default_error(png_structp png_ptr, png_const_charp error_message)
212{
213#ifndef PNG_NO_CONSOLE_IO
214#ifdef PNG_ERROR_NUMBERS_SUPPORTED
215 if (*error_message == '#')
216 {
217 int offset;
218 char error_number[16];
219 for (offset=0; offset<15; offset++)
220 {
221 error_number[offset] = *(error_message+offset+1);
222 if (*(error_message+offset) == ' ')
223 break;
224 }
225 if((offset > 1) && (offset < 15))
226 {
227 error_number[offset-1]='\0';
228 fprintf(stderr, "libpng error no. %s: %s\n", error_number,
229 error_message+offset);
230 }
231 else
232 fprintf(stderr, "libpng error: %s, offset=%d\n", error_message,offset);
233 }
234 else
235#endif
236 fprintf(stderr, "libpng error: %s\n", error_message);
237#endif
238
239#ifdef PNG_SETJMP_SUPPORTED
240 if (png_ptr)
241 {
242# ifdef USE_FAR_KEYWORD
243 {
244 jmp_buf jmpbuf;
245 png_memcpy(jmpbuf, png_ptr->jmpbuf, png_sizeof(jmp_buf));
246 longjmp(jmpbuf, 1);
247 }
248# else
249 longjmp(png_ptr->jmpbuf, 1);
250# endif
251 }
252#else
253 PNG_ABORT();
254#endif
255#ifdef PNG_NO_CONSOLE_IO
256 error_message = error_message; /* make compiler happy */
257#endif
258}
259
260#ifndef PNG_NO_WARNINGS
261/* This function is called when there is a warning, but the library thinks
262 * it can continue anyway. Replacement functions don't have to do anything
263 * here if you don't want them to. In the default configuration, png_ptr is
264 * not used, but it is passed in case it may be useful.
265 */
266static void /* PRIVATE */
267png_default_warning(png_structp png_ptr, png_const_charp warning_message)
268{
269#ifndef PNG_NO_CONSOLE_IO
270# ifdef PNG_ERROR_NUMBERS_SUPPORTED
271 if (*warning_message == '#')
272 {
273 int offset;
274 char warning_number[16];
275 for (offset=0; offset<15; offset++)
276 {
277 warning_number[offset]=*(warning_message+offset+1);
278 if (*(warning_message+offset) == ' ')
279 break;
280 }
281 if((offset > 1) && (offset < 15))
282 {
283 warning_number[offset-1]='\0';
284 fprintf(stderr, "libpng warning no. %s: %s\n", warning_number,
285 warning_message+offset);
286 }
287 else
288 fprintf(stderr, "libpng warning: %s\n", warning_message);
289 }
290 else
291# endif
292 fprintf(stderr, "libpng warning: %s\n", warning_message);
293#else
294 warning_message = warning_message; /* make compiler happy */
295#endif
296 png_ptr = png_ptr; /* make compiler happy */
297}
298#endif /* PNG_NO_WARNINGS */
299
300/* This function is called when the application wants to use another method
301 * of handling errors and warnings. Note that the error function MUST NOT
302 * return to the calling routine or serious problems will occur. The return
303 * method used in the default routine calls longjmp(png_ptr->jmpbuf, 1)
304 */
305void PNGAPI
306png_set_error_fn(png_structp png_ptr, png_voidp error_ptr,
307 png_error_ptr error_fn, png_error_ptr warning_fn)
308{
309 if (png_ptr == NULL)
310 return;
311 png_ptr->error_ptr = error_ptr;
312 png_ptr->error_fn = error_fn;
313 png_ptr->warning_fn = warning_fn;
314}
315
316
317/* This function returns a pointer to the error_ptr associated with the user
318 * functions. The application should free any memory associated with this
319 * pointer before png_write_destroy and png_read_destroy are called.
320 */
321png_voidp PNGAPI
322png_get_error_ptr(png_structp png_ptr)
323{
324 if (png_ptr == NULL)
325 return NULL;
326 return ((png_voidp)png_ptr->error_ptr);
327}
328
329
330#ifdef PNG_ERROR_NUMBERS_SUPPORTED
331void PNGAPI
332png_set_strip_error_numbers(png_structp png_ptr, png_uint_32 strip_mode)
333{
334 if(png_ptr != NULL)
335 {
336 png_ptr->flags &=
337 ((~(PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))&strip_mode);
338 }
339}
340#endif
341#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */