2 /* pngerror.c - stub functions for i/o and memory allocation
4 * Last changed in libpng 1.2.13 November 13, 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.)
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 static void /* PRIVATE */
24 png_default_warning
PNGARG((png_structp png_ptr
,
25 png_const_charp warning_message
));
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.
33 png_error(png_structp png_ptr
, png_const_charp error_message
)
35 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
40 (PNG_FLAG_STRIP_ERROR_NUMBERS
|PNG_FLAG_STRIP_ERROR_TEXT
))
42 if (*error_message
== '#')
45 for (offset
=1; offset
<15; offset
++)
46 if (*(error_message
+offset
) == ' ')
48 if (png_ptr
->flags
&PNG_FLAG_STRIP_ERROR_TEXT
)
51 for (i
=0; i
<offset
-1; i
++)
52 msg
[i
]=error_message
[i
+1];
57 error_message
+=offset
;
61 if (png_ptr
->flags
&PNG_FLAG_STRIP_ERROR_TEXT
)
71 if (png_ptr
!= NULL
&& png_ptr
->error_fn
!= NULL
)
72 (*(png_ptr
->error_fn
))(png_ptr
, error_message
);
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
);
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.
85 png_warning(png_structp png_ptr
, png_const_charp warning_message
)
90 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
92 (PNG_FLAG_STRIP_ERROR_NUMBERS
|PNG_FLAG_STRIP_ERROR_TEXT
))
95 if (*warning_message
== '#')
97 for (offset
=1; offset
<15; offset
++)
98 if (*(warning_message
+offset
) == ' ')
102 if (png_ptr
!= NULL
&& png_ptr
->warning_fn
!= NULL
)
103 (*(png_ptr
->warning_fn
))(png_ptr
, warning_message
+offset
);
106 png_default_warning(png_ptr
, warning_message
+offset
);
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.
115 #define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
116 const 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'
121 static void /* PRIVATE */
122 png_format_buffer(png_structp png_ptr
, png_charp buffer
, png_const_charp
125 int iout
= 0, iin
= 0;
129 int c
= png_ptr
->chunk_name
[iin
++];
132 buffer
[iout
++] = '[';
133 buffer
[iout
++] = png_digit
[(c
& 0xf0) >> 4];
134 buffer
[iout
++] = png_digit
[c
& 0x0f];
135 buffer
[iout
++] = ']';
139 buffer
[iout
++] = (png_byte
)c
;
143 if (error_message
== NULL
)
147 buffer
[iout
++] = ':';
148 buffer
[iout
++] = ' ';
149 png_strncpy(buffer
+iout
, error_message
, 63);
155 png_chunk_error(png_structp png_ptr
, png_const_charp error_message
)
159 png_error(png_ptr
, error_message
);
162 png_format_buffer(png_ptr
, msg
, error_message
);
163 png_error(png_ptr
, msg
);
168 png_chunk_warning(png_structp png_ptr
, png_const_charp warning_message
)
172 png_warning(png_ptr
, warning_message
);
175 png_format_buffer(png_ptr
, msg
, warning_message
);
176 png_warning(png_ptr
, msg
);
180 /* This is the default error handling function. Note that replacements for
181 * this function MUST NOT RETURN, or the program will likely crash. This
182 * function is used by default, or if the program supplies NULL for the
183 * error function pointer in png_set_error_fn().
185 static void /* PRIVATE */
186 png_default_error(png_structp png_ptr
, png_const_charp error_message
)
188 #ifndef PNG_NO_CONSOLE_IO
189 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
190 if (*error_message
== '#')
193 char error_number
[16];
194 for (offset
=0; offset
<15; offset
++)
196 error_number
[offset
] = *(error_message
+offset
+1);
197 if (*(error_message
+offset
) == ' ')
200 if((offset
> 1) && (offset
< 15))
202 error_number
[offset
-1]='\0';
203 fprintf(stderr
, "libpng error no. %s: %s\n", error_number
,
204 error_message
+offset
);
207 fprintf(stderr
, "libpng error: %s, offset=%d\n", error_message
,offset
);
211 fprintf(stderr
, "libpng error: %s\n", error_message
);
214 #ifdef PNG_SETJMP_SUPPORTED
217 # ifdef USE_FAR_KEYWORD
220 png_memcpy(jmpbuf
,png_ptr
->jmpbuf
,png_sizeof(jmp_buf));
224 longjmp(png_ptr
->jmpbuf
, 1);
230 #ifdef PNG_NO_CONSOLE_IO
231 /* make compiler happy */ ;
232 if (&error_message
!= NULL
)
237 /* This function is called when there is a warning, but the library thinks
238 * it can continue anyway. Replacement functions don't have to do anything
239 * here if you don't want them to. In the default configuration, png_ptr is
240 * not used, but it is passed in case it may be useful.
242 static void /* PRIVATE */
243 png_default_warning(png_structp png_ptr
, png_const_charp warning_message
)
245 #ifndef PNG_NO_CONSOLE_IO
246 # ifdef PNG_ERROR_NUMBERS_SUPPORTED
247 if (*warning_message
== '#')
250 char warning_number
[16];
251 for (offset
=0; offset
<15; offset
++)
253 warning_number
[offset
]=*(warning_message
+offset
+1);
254 if (*(warning_message
+offset
) == ' ')
257 if((offset
> 1) && (offset
< 15))
259 warning_number
[offset
-1]='\0';
260 fprintf(stderr
, "libpng warning no. %s: %s\n", warning_number
,
261 warning_message
+offset
);
264 fprintf(stderr
, "libpng warning: %s\n", warning_message
);
268 fprintf(stderr
, "libpng warning: %s\n", warning_message
);
270 /* make compiler happy */ ;
274 /* make compiler happy */ ;
279 /* This function is called when the application wants to use another method
280 * of handling errors and warnings. Note that the error function MUST NOT
281 * return to the calling routine or serious problems will occur. The return
282 * method used in the default routine calls longjmp(png_ptr->jmpbuf, 1)
285 png_set_error_fn(png_structp png_ptr
, png_voidp error_ptr
,
286 png_error_ptr error_fn
, png_error_ptr warning_fn
)
290 png_ptr
->error_ptr
= error_ptr
;
291 png_ptr
->error_fn
= error_fn
;
292 png_ptr
->warning_fn
= warning_fn
;
296 /* This function returns a pointer to the error_ptr associated with the user
297 * functions. The application should free any memory associated with this
298 * pointer before png_write_destroy and png_read_destroy are called.
301 png_get_error_ptr(png_structp png_ptr
)
305 return ((png_voidp
)png_ptr
->error_ptr
);
309 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
311 png_set_strip_error_numbers(png_structp png_ptr
, png_uint_32 strip_mode
)
316 ((~(PNG_FLAG_STRIP_ERROR_NUMBERS
|PNG_FLAG_STRIP_ERROR_TEXT
))&strip_mode
);
320 #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */