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