(metux) configure.ac: removed broken and obsolete AC_C_CONST
[mirror-ossqm-libpng.git] / pngerror.c
blob8290bb4106efbd5f18563a7c69f82e73bf47982c
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
16 * at each function.
19 #include "pngpriv.h"
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
42 char msg[16];
43 if (png_ptr != NULL)
45 if (png_ptr->flags&
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. */
51 int offset;
52 for (offset = 1; offset<15; offset++)
53 if (error_message[offset] == ' ')
54 break;
56 if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT)
58 int i;
59 for (i = 0; i < offset - 1; i++)
60 msg[i] = error_message[i + 1];
61 msg[i - 1] = '\0';
62 error_message = msg;
65 else
66 error_message += offset;
69 else
71 if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT)
73 msg[0] = '0';
74 msg[1] = '\0';
75 error_message = msg;
80 #endif
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);
88 #else
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.
107 void PNGAPI
108 png_warning(png_structp png_ptr, png_const_charp warning_message)
110 int offset = 0;
111 if (png_ptr != NULL)
113 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
114 if (png_ptr->flags&
115 (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))
116 #endif
118 if (*warning_message == PNG_LITERAL_SHARP)
120 for (offset = 1; offset < 15; offset++)
121 if (warning_message[offset] == ' ')
122 break;
126 if (png_ptr != NULL && png_ptr->warning_fn != NULL)
127 (*(png_ptr->warning_fn))(png_ptr, warning_message + offset);
128 else
129 png_default_warning(png_ptr, warning_message + offset);
131 #endif /* PNG_WARNINGS_SUPPORTED */
133 #ifdef PNG_BENIGN_ERRORS_SUPPORTED
134 void PNGAPI
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);
139 else
140 png_error(png_ptr, error_message);
142 #endif
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
160 error_message)
162 int iout = 0, iin = 0;
164 while (iin < 4)
166 int c = png_ptr->chunk_name[iin++];
167 if (isnonalpha(c))
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;
175 else
177 buffer[iout++] = (png_byte)c;
181 if (error_message == NULL)
182 buffer[iout] = '\0';
184 else
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),
197 PNG_NORETURN)
199 char msg[18+PNG_MAX_ERROR_TEXT];
200 if (png_ptr == NULL)
201 png_error(png_ptr, error_message);
203 else
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
212 void PNGAPI
213 png_chunk_warning(png_structp png_ptr, png_const_charp warning_message)
215 char msg[18+PNG_MAX_ERROR_TEXT];
216 if (png_ptr == NULL)
217 png_warning(png_ptr, warning_message);
219 else
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
229 void PNGAPI
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);
235 else
236 png_chunk_error(png_ptr, error_message);
238 #endif
239 #endif /* PNG_READ_SUPPORTED */
241 #ifdef PNG_ERROR_TEXT_SUPPORTED
242 #ifdef PNG_FLOATING_POINT_SUPPORTED
243 PNG_FUNCTION(void,
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)
248 int iin;
249 char msg[fixed_message_ln+PNG_MAX_ERROR_TEXT];
250 png_memcpy(msg, fixed_message, fixed_message_ln);
251 iin = 0;
252 if (name != NULL) while (iin < (PNG_MAX_ERROR_TEXT-1) && name[iin] != 0)
254 msg[fixed_message_ln + iin] = name[iin];
255 ++iin;
257 msg[fixed_message_ln + iin] = 0;
258 png_error(png_ptr, msg);
260 #endif
261 #endif
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.
267 jmp_buf* PNGAPI
268 png_set_longjmp_fn(png_structp png_ptr, png_longjmp_ptr longjmp_fn,
269 size_t jmp_buf_size)
271 if (png_ptr == NULL || jmp_buf_size != png_sizeof(jmp_buf))
272 return NULL;
274 png_ptr->longjmp_fn = longjmp_fn;
275 return &png_ptr->png_jmpbuf;
277 #endif
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),
286 PNG_NORETURN)
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. */
293 int offset;
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] == ' ')
299 break;
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);
310 else
312 fprintf(stderr, "libpng error: %s, offset=%d",
313 error_message, offset);
314 fprintf(stderr, PNG_STRING_NEWLINE);
317 else
318 #endif
320 fprintf(stderr, "libpng error: %s", error_message);
321 fprintf(stderr, PNG_STRING_NEWLINE);
323 #endif
324 #ifndef PNG_CONSOLE_IO_SUPPORTED
325 PNG_UNUSED(error_message) /* Make compiler happy */
326 #endif
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
338 jmp_buf png_jmpbuf;
339 png_memcpy(png_jmpbuf, png_ptr->png_jmpbuf, png_sizeof(jmp_buf));
340 png_ptr->longjmp_fn(png_jmpbuf, val);
343 # else
344 png_ptr->longjmp_fn(png_ptr->png_jmpbuf, val);
345 # endif
347 #endif
348 /* Here if not setjmp support or if png_ptr is null. */
349 PNG_ABORT();
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)
365 int offset;
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] == ' ')
371 break;
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);
382 else
384 fprintf(stderr, "libpng warning: %s",
385 warning_message);
386 fprintf(stderr, PNG_STRING_NEWLINE);
389 else
390 # endif
393 fprintf(stderr, "libpng warning: %s", warning_message);
394 fprintf(stderr, PNG_STRING_NEWLINE);
396 #else
397 PNG_UNUSED(warning_message) /* Make compiler happy */
398 #endif
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)
408 void PNGAPI
409 png_set_error_fn(png_structp png_ptr, png_voidp error_ptr,
410 png_error_ptr error_fn, png_error_ptr warning_fn)
412 if (png_ptr == NULL)
413 return;
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.
425 png_voidp PNGAPI
426 png_get_error_ptr(png_const_structp png_ptr)
428 if (png_ptr == NULL)
429 return NULL;
431 return ((png_voidp)png_ptr->error_ptr);
435 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
436 void PNGAPI
437 png_set_strip_error_numbers(png_structp png_ptr, png_uint_32 strip_mode)
439 if (png_ptr != NULL)
441 png_ptr->flags &=
442 ((~(PNG_FLAG_STRIP_ERROR_NUMBERS |
443 PNG_FLAG_STRIP_ERROR_TEXT))&strip_mode);
446 #endif
447 #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */