2 /* pngerror.c - stub functions for i/o and memory allocation
4 * Last changed in libpng 1.5.1 [February 3, 2011]
5 * Copyright (c) 1998-2011 Glenn Randers-Pehrson
6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
9 * This code is released under the libpng license.
10 * For conditions of distribution and use, see the disclaimer
11 * and license in png.h
13 * This file provides a location for all error handling. Users who
14 * need special error handling are expected to write replacement functions
15 * and use png_set_error_fn() to use those functions. See the instructions
21 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
23 static PNG_FUNCTION(void, png_default_error
,PNGARG((png_structp png_ptr
,
24 png_const_charp error_message
)),PNG_NORETURN
);
26 #ifdef PNG_WARNINGS_SUPPORTED
27 static void /* PRIVATE */
28 png_default_warning
PNGARG((png_structp png_ptr
,
29 png_const_charp warning_message
));
30 #endif /* PNG_WARNINGS_SUPPORTED */
32 /* This function is called whenever there is a fatal error. This function
33 * should not be changed. If there is a need to handle errors differently,
34 * you should supply a replacement error function and use png_set_error_fn()
35 * to replace the error function at run-time.
37 #ifdef PNG_ERROR_TEXT_SUPPORTED
38 PNG_FUNCTION(void,PNGAPI
39 png_error
,(png_structp png_ptr
, png_const_charp error_message
),PNG_NORETURN
)
41 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
46 (PNG_FLAG_STRIP_ERROR_NUMBERS
|PNG_FLAG_STRIP_ERROR_TEXT
))
48 if (*error_message
== PNG_LITERAL_SHARP
)
50 /* Strip "#nnnn " from beginning of error message. */
52 for (offset
= 1; offset
<15; offset
++)
53 if (error_message
[offset
] == ' ')
56 if (png_ptr
->flags
&PNG_FLAG_STRIP_ERROR_TEXT
)
59 for (i
= 0; i
< offset
- 1; i
++)
60 msg
[i
] = error_message
[i
+ 1];
66 error_message
+= offset
;
71 if (png_ptr
->flags
&PNG_FLAG_STRIP_ERROR_TEXT
)
81 if (png_ptr
!= NULL
&& png_ptr
->error_fn
!= NULL
)
82 (*(png_ptr
->error_fn
))(png_ptr
, error_message
);
84 /* If the custom handler doesn't exist, or if it returns,
85 use the default handler, which will not return. */
86 png_default_error(png_ptr
, error_message
);
89 PNG_FUNCTION(void,PNGAPI
90 png_err
,(png_structp png_ptr
),PNG_NORETURN
)
92 if (png_ptr
!= NULL
&& png_ptr
->error_fn
!= NULL
)
93 (*(png_ptr
->error_fn
))(png_ptr
, '\0');
95 /* If the custom handler doesn't exist, or if it returns,
96 use the default handler, which will not return. */
97 png_default_error(png_ptr
, '\0');
99 #endif /* PNG_ERROR_TEXT_SUPPORTED */
101 #ifdef PNG_WARNINGS_SUPPORTED
102 /* This function is called whenever there is a non-fatal error. This function
103 * should not be changed. If there is a need to handle warnings differently,
104 * you should supply a replacement warning function and use
105 * png_set_error_fn() to replace the warning function at run-time.
108 png_warning(png_structp png_ptr
, png_const_charp warning_message
)
113 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
115 (PNG_FLAG_STRIP_ERROR_NUMBERS
|PNG_FLAG_STRIP_ERROR_TEXT
))
118 if (*warning_message
== PNG_LITERAL_SHARP
)
120 for (offset
= 1; offset
< 15; offset
++)
121 if (warning_message
[offset
] == ' ')
126 if (png_ptr
!= NULL
&& png_ptr
->warning_fn
!= NULL
)
127 (*(png_ptr
->warning_fn
))(png_ptr
, warning_message
+ offset
);
129 png_default_warning(png_ptr
, warning_message
+ offset
);
131 #endif /* PNG_WARNINGS_SUPPORTED */
133 #ifdef PNG_BENIGN_ERRORS_SUPPORTED
135 png_benign_error(png_structp png_ptr
, png_const_charp error_message
)
137 if (png_ptr
->flags
& PNG_FLAG_BENIGN_ERRORS_WARN
)
138 png_warning(png_ptr
, error_message
);
140 png_error(png_ptr
, error_message
);
144 /* These utilities are used internally to build an error message that relates
145 * to the current chunk. The chunk name comes from png_ptr->chunk_name,
146 * this is used to prefix the message. The message is limited in length
147 * to 63 bytes, the name characters are output as hex digits wrapped in []
148 * if the character is invalid.
150 #define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
151 static PNG_CONST
char png_digit
[16] = {
152 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
153 'A', 'B', 'C', 'D', 'E', 'F'
156 #define PNG_MAX_ERROR_TEXT 64
157 #if defined(PNG_WARNINGS_SUPPORTED) || defined(PNG_ERROR_TEXT_SUPPORTED)
158 static void /* PRIVATE */
159 png_format_buffer(png_structp png_ptr
, png_charp buffer
, png_const_charp
162 int iout
= 0, iin
= 0;
166 int c
= png_ptr
->chunk_name
[iin
++];
169 buffer
[iout
++] = PNG_LITERAL_LEFT_SQUARE_BRACKET
;
170 buffer
[iout
++] = png_digit
[(c
& 0xf0) >> 4];
171 buffer
[iout
++] = png_digit
[c
& 0x0f];
172 buffer
[iout
++] = PNG_LITERAL_RIGHT_SQUARE_BRACKET
;
177 buffer
[iout
++] = (png_byte
)c
;
181 if (error_message
== NULL
)
186 buffer
[iout
++] = ':';
187 buffer
[iout
++] = ' ';
188 png_memcpy(buffer
+ iout
, error_message
, PNG_MAX_ERROR_TEXT
);
189 buffer
[iout
+ PNG_MAX_ERROR_TEXT
- 1] = '\0';
192 #endif /* PNG_WARNINGS_SUPPORTED || PNG_ERROR_TEXT_SUPPORTED */
194 #if defined(PNG_READ_SUPPORTED) && defined(PNG_ERROR_TEXT_SUPPORTED)
195 PNG_FUNCTION(void,PNGAPI
196 png_chunk_error
,(png_structp png_ptr
, png_const_charp error_message
),
199 char msg
[18+PNG_MAX_ERROR_TEXT
];
201 png_error(png_ptr
, error_message
);
205 png_format_buffer(png_ptr
, msg
, error_message
);
206 png_error(png_ptr
, msg
);
209 #endif /* PNG_READ_SUPPORTED && PNG_ERROR_TEXT_SUPPORTED */
211 #ifdef PNG_WARNINGS_SUPPORTED
213 png_chunk_warning(png_structp png_ptr
, png_const_charp warning_message
)
215 char msg
[18+PNG_MAX_ERROR_TEXT
];
217 png_warning(png_ptr
, warning_message
);
221 png_format_buffer(png_ptr
, msg
, warning_message
);
222 png_warning(png_ptr
, msg
);
225 #endif /* PNG_WARNINGS_SUPPORTED */
227 #ifdef PNG_READ_SUPPORTED
228 #ifdef PNG_BENIGN_ERRORS_SUPPORTED
230 png_chunk_benign_error(png_structp png_ptr
, png_const_charp error_message
)
232 if (png_ptr
->flags
& PNG_FLAG_BENIGN_ERRORS_WARN
)
233 png_chunk_warning(png_ptr
, error_message
);
236 png_chunk_error(png_ptr
, error_message
);
239 #endif /* PNG_READ_SUPPORTED */
241 #ifdef PNG_ERROR_TEXT_SUPPORTED
242 #ifdef PNG_FLOATING_POINT_SUPPORTED
244 png_fixed_error
,(png_structp png_ptr
, png_const_charp name
),PNG_NORETURN
)
246 # define fixed_message "fixed point overflow in "
247 # define fixed_message_ln ((sizeof fixed_message)-1)
249 char msg
[fixed_message_ln
+PNG_MAX_ERROR_TEXT
];
250 png_memcpy(msg
, fixed_message
, fixed_message_ln
);
252 if (name
!= NULL
) while (iin
< (PNG_MAX_ERROR_TEXT
-1) && name
[iin
] != 0)
254 msg
[fixed_message_ln
+ iin
] = name
[iin
];
257 msg
[fixed_message_ln
+ iin
] = 0;
258 png_error(png_ptr
, msg
);
263 #ifdef PNG_SETJMP_SUPPORTED
264 /* This API only exists if ANSI-C style error handling is used,
265 * otherwise it is necessary for png_default_error to be overridden.
268 png_set_longjmp_fn(png_structp png_ptr
, png_longjmp_ptr longjmp_fn
,
271 if (png_ptr
== NULL
|| jmp_buf_size
!= png_sizeof(jmp_buf))
274 png_ptr
->longjmp_fn
= longjmp_fn
;
275 return &png_ptr
->png_jmpbuf
;
279 /* This is the default error handling function. Note that replacements for
280 * this function MUST NOT RETURN, or the program will likely crash. This
281 * function is used by default, or if the program supplies NULL for the
282 * error function pointer in png_set_error_fn().
284 static PNG_FUNCTION(void /* PRIVATE */,
285 png_default_error
,(png_structp png_ptr
, png_const_charp error_message
),
288 #ifdef PNG_CONSOLE_IO_SUPPORTED
289 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
290 if (*error_message
== PNG_LITERAL_SHARP
)
292 /* Strip "#nnnn " from beginning of error message. */
294 char error_number
[16];
295 for (offset
= 0; offset
<15; offset
++)
297 error_number
[offset
] = error_message
[offset
+ 1];
298 if (error_message
[offset
] == ' ')
302 if ((offset
> 1) && (offset
< 15))
304 error_number
[offset
- 1] = '\0';
305 fprintf(stderr
, "libpng error no. %s: %s",
306 error_number
, error_message
+ offset
+ 1);
307 fprintf(stderr
, PNG_STRING_NEWLINE
);
312 fprintf(stderr
, "libpng error: %s, offset=%d",
313 error_message
, offset
);
314 fprintf(stderr
, PNG_STRING_NEWLINE
);
320 fprintf(stderr
, "libpng error: %s", error_message
);
321 fprintf(stderr
, PNG_STRING_NEWLINE
);
324 #ifndef PNG_CONSOLE_IO_SUPPORTED
325 PNG_UNUSED(error_message
) /* Make compiler happy */
327 png_longjmp(png_ptr
, 1);
330 PNG_FUNCTION(void,PNGAPI
331 png_longjmp
,(png_structp png_ptr
, int val
),PNG_NORETURN
)
333 #ifdef PNG_SETJMP_SUPPORTED
334 if (png_ptr
&& png_ptr
->longjmp_fn
)
336 # ifdef USE_FAR_KEYWORD
339 png_memcpy(png_jmpbuf
, png_ptr
->png_jmpbuf
, png_sizeof(jmp_buf));
340 png_ptr
->longjmp_fn(png_jmpbuf
, val
);
344 png_ptr
->longjmp_fn(png_ptr
->png_jmpbuf
, val
);
348 /* Here if not setjmp support or if png_ptr is null. */
352 #ifdef PNG_WARNINGS_SUPPORTED
353 /* This function is called when there is a warning, but the library thinks
354 * it can continue anyway. Replacement functions don't have to do anything
355 * here if you don't want them to. In the default configuration, png_ptr is
356 * not used, but it is passed in case it may be useful.
358 static void /* PRIVATE */
359 png_default_warning(png_structp png_ptr
, png_const_charp warning_message
)
361 #ifdef PNG_CONSOLE_IO_SUPPORTED
362 # ifdef PNG_ERROR_NUMBERS_SUPPORTED
363 if (*warning_message
== PNG_LITERAL_SHARP
)
366 char warning_number
[16];
367 for (offset
= 0; offset
< 15; offset
++)
369 warning_number
[offset
] = warning_message
[offset
+ 1];
370 if (warning_message
[offset
] == ' ')
374 if ((offset
> 1) && (offset
< 15))
376 warning_number
[offset
+ 1] = '\0';
377 fprintf(stderr
, "libpng warning no. %s: %s",
378 warning_number
, warning_message
+ offset
);
379 fprintf(stderr
, PNG_STRING_NEWLINE
);
384 fprintf(stderr
, "libpng warning: %s",
386 fprintf(stderr
, PNG_STRING_NEWLINE
);
393 fprintf(stderr
, "libpng warning: %s", warning_message
);
394 fprintf(stderr
, PNG_STRING_NEWLINE
);
397 PNG_UNUSED(warning_message
) /* Make compiler happy */
399 PNG_UNUSED(png_ptr
) /* Make compiler happy */
401 #endif /* PNG_WARNINGS_SUPPORTED */
403 /* This function is called when the application wants to use another method
404 * of handling errors and warnings. Note that the error function MUST NOT
405 * return to the calling routine or serious problems will occur. The return
406 * method used in the default routine calls longjmp(png_ptr->png_jmpbuf, 1)
409 png_set_error_fn(png_structp png_ptr
, png_voidp error_ptr
,
410 png_error_ptr error_fn
, png_error_ptr warning_fn
)
415 png_ptr
->error_ptr
= error_ptr
;
416 png_ptr
->error_fn
= error_fn
;
417 png_ptr
->warning_fn
= warning_fn
;
421 /* This function returns a pointer to the error_ptr associated with the user
422 * functions. The application should free any memory associated with this
423 * pointer before png_write_destroy and png_read_destroy are called.
426 png_get_error_ptr(png_const_structp png_ptr
)
431 return ((png_voidp
)png_ptr
->error_ptr
);
435 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
437 png_set_strip_error_numbers(png_structp png_ptr
, png_uint_32 strip_mode
)
442 ((~(PNG_FLAG_STRIP_ERROR_NUMBERS
|
443 PNG_FLAG_STRIP_ERROR_TEXT
))&strip_mode
);
447 #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */