2 /* pngerror.c - stub functions for i/o and memory allocation
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.)
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 #if !defined(PNG_NO_WARNINGS) || !defined(PNG_NO_ERROR_TEXT)
140 static void /* PRIVATE */
141 png_format_buffer(png_structp png_ptr
, png_charp buffer
, png_const_charp
144 int iout
= 0, iin
= 0;
148 int c
= png_ptr
->chunk_name
[iin
++];
151 buffer
[iout
++] = '[';
152 buffer
[iout
++] = png_digit
[(c
& 0xf0) >> 4];
153 buffer
[iout
++] = png_digit
[c
& 0x0f];
154 buffer
[iout
++] = ']';
158 buffer
[iout
++] = (png_byte
)c
;
162 if (error_message
== NULL
)
166 buffer
[iout
++] = ':';
167 buffer
[iout
++] = ' ';
168 png_strncpy(buffer
+iout
, error_message
, 63);
173 #ifdef PNG_READ_SUPPORTED
175 png_chunk_error(png_structp png_ptr
, png_const_charp error_message
)
179 png_error(png_ptr
, error_message
);
182 png_format_buffer(png_ptr
, msg
, error_message
);
183 png_error(png_ptr
, msg
);
186 #endif /* PNG_READ_SUPPORTED */
187 #endif /* !defined(PNG_NO_WARNINGS) || !defined(PNG_NO_ERROR_TEXT) */
189 #ifndef PNG_NO_WARNINGS
191 png_chunk_warning(png_structp png_ptr
, png_const_charp warning_message
)
195 png_warning(png_ptr
, warning_message
);
198 png_format_buffer(png_ptr
, msg
, warning_message
);
199 png_warning(png_ptr
, msg
);
202 #endif /* PNG_NO_WARNINGS */
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().
210 static void /* PRIVATE */
211 png_default_error(png_structp png_ptr
, png_const_charp error_message
)
213 #ifndef PNG_NO_CONSOLE_IO
214 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
215 if (*error_message
== '#')
218 char error_number
[16];
219 for (offset
=0; offset
<15; offset
++)
221 error_number
[offset
] = *(error_message
+offset
+1);
222 if (*(error_message
+offset
) == ' ')
225 if((offset
> 1) && (offset
< 15))
227 error_number
[offset
-1]='\0';
228 fprintf(stderr
, "libpng error no. %s: %s\n", error_number
,
229 error_message
+offset
);
232 fprintf(stderr
, "libpng error: %s, offset=%d\n", error_message
,offset
);
236 fprintf(stderr
, "libpng error: %s\n", error_message
);
239 #ifdef PNG_SETJMP_SUPPORTED
242 # ifdef USE_FAR_KEYWORD
245 png_memcpy(jmpbuf
, png_ptr
->jmpbuf
, png_sizeof(jmp_buf));
249 longjmp(png_ptr
->jmpbuf
, 1);
255 #ifdef PNG_NO_CONSOLE_IO
256 error_message
= error_message
; /* make compiler happy */
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.
266 static void /* PRIVATE */
267 png_default_warning(png_structp png_ptr
, png_const_charp warning_message
)
269 #ifndef PNG_NO_CONSOLE_IO
270 # ifdef PNG_ERROR_NUMBERS_SUPPORTED
271 if (*warning_message
== '#')
274 char warning_number
[16];
275 for (offset
=0; offset
<15; offset
++)
277 warning_number
[offset
]=*(warning_message
+offset
+1);
278 if (*(warning_message
+offset
) == ' ')
281 if((offset
> 1) && (offset
< 15))
283 warning_number
[offset
-1]='\0';
284 fprintf(stderr
, "libpng warning no. %s: %s\n", warning_number
,
285 warning_message
+offset
);
288 fprintf(stderr
, "libpng warning: %s\n", warning_message
);
292 fprintf(stderr
, "libpng warning: %s\n", warning_message
);
294 warning_message
= warning_message
; /* make compiler happy */
296 png_ptr
= png_ptr
; /* make compiler happy */
298 #endif /* PNG_NO_WARNINGS */
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)
306 png_set_error_fn(png_structp png_ptr
, png_voidp error_ptr
,
307 png_error_ptr error_fn
, png_error_ptr warning_fn
)
311 png_ptr
->error_ptr
= error_ptr
;
312 png_ptr
->error_fn
= error_fn
;
313 png_ptr
->warning_fn
= warning_fn
;
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.
322 png_get_error_ptr(png_structp png_ptr
)
326 return ((png_voidp
)png_ptr
->error_ptr
);
330 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
332 png_set_strip_error_numbers(png_structp png_ptr
, png_uint_32 strip_mode
)
337 ((~(PNG_FLAG_STRIP_ERROR_NUMBERS
|PNG_FLAG_STRIP_ERROR_TEXT
))&strip_mode
);
341 #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */