blob: 2e3d4ccbabd1942cbb6b63d149b2295e40c05a59 [file] [log] [blame]
Guy Schalnat6d764711995-12-19 03:22:19 -06001
2/* pngerror.c - stub functions for i/o and memory allocation
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06003 *
Glenn Randers-Pehrson65a22372010-03-03 05:38:29 -06004 * Last changed in libpng 1.5.0 [March 3, 2010]
Glenn Randers-Pehrsone69b55d2010-01-01 10:29:06 -06005 * Copyright (c) 1998-2010 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06008 *
Glenn Randers-Pehrsonbfbf8652009-06-26 21:46:52 -05009 * This code is released under the libpng license.
Glenn Randers-Pehrsonc332bbc2009-06-25 13:43:50 -050010 * For conditions of distribution and use, see the disclaimer
Glenn Randers-Pehrson037023b2009-06-24 10:27:36 -050011 * and license in png.h
Glenn Randers-Pehrson3e61d792009-06-24 09:31:28 -050012 *
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -050013 * This file provides a location for all error handling. Users who
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060014 * 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.
17 */
Guy Schalnat6d764711995-12-19 03:22:19 -060018
Glenn Randers-Pehrsonf9795312010-02-09 01:16:48 -060019#define PNG_EXPOSE_INTERNAL_STRUCTURES
Glenn Randers-Pehrson03f9b022009-12-04 08:40:41 -060020#define PNG_NO_PEDANTIC_WARNINGS
Guy Schalnat6d764711995-12-19 03:22:19 -060021#include "png.h"
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050022#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050023#include "pngpriv.h"
Guy Schalnat6d764711995-12-19 03:22:19 -060024
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050025static void /* PRIVATE */
26png_default_error PNGARG((png_structp png_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -060027 png_const_charp error_message)) PNG_NORETURN;
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -050028#ifdef PNG_WARNINGS_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050029static void /* PRIVATE */
30png_default_warning PNGARG((png_structp png_ptr,
Glenn Randers-Pehrson837a3d12002-05-10 20:19:58 -050031 png_const_charp warning_message));
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -050032#endif /* PNG_WARNINGS_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -050033
Guy Schalnat69b14481996-01-10 02:56:49 -060034/* This function is called whenever there is a fatal error. This function
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060035 * should not be changed. If there is a need to handle errors differently,
36 * you should supply a replacement error function and use png_set_error_fn()
37 * to replace the error function at run-time.
38 */
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -050039#ifdef PNG_ERROR_TEXT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050040void PNGAPI
Glenn Randers-Pehrson837a3d12002-05-10 20:19:58 -050041png_error(png_structp png_ptr, png_const_charp error_message)
Guy Schalnat6d764711995-12-19 03:22:19 -060042{
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -050043#ifdef PNG_ERROR_NUMBERS_SUPPORTED
44 char msg[16];
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -060045 if (png_ptr != NULL)
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -050046 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050047 if (png_ptr->flags&
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -060048 (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050049 {
Glenn Randers-Pehrson43aaf6e2008-08-05 22:17:03 -050050 if (*error_message == PNG_LITERAL_SHARP)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050051 {
52 /* Strip "#nnnn " from beginning of error message. */
53 int offset;
54 for (offset = 1; offset<15; offset++)
55 if (error_message[offset] == ' ')
56 break;
57 if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT)
58 {
59 int i;
60 for (i = 0; i < offset - 1; i++)
61 msg[i] = error_message[i + 1];
62 msg[i - 1] = '\0';
63 error_message = msg;
64 }
65 else
66 error_message += offset;
67 }
68 else
69 {
70 if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT)
71 {
72 msg[0] = '0';
73 msg[1] = '\0';
74 error_message = msg;
75 }
76 }
77 }
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -050078 }
79#endif
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -050080 if (png_ptr != NULL && png_ptr->error_fn != NULL)
Glenn Randers-Pehrson837a3d12002-05-10 20:19:58 -050081 (*(png_ptr->error_fn))(png_ptr, error_message);
Guy Schalnat6d764711995-12-19 03:22:19 -060082
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -050083 /* If the custom handler doesn't exist, or if it returns,
84 use the default handler, which will not return. */
Glenn Randers-Pehrson837a3d12002-05-10 20:19:58 -050085 png_default_error(png_ptr, error_message);
Guy Schalnat6d764711995-12-19 03:22:19 -060086}
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050087#else
88void PNGAPI
89png_err(png_structp png_ptr)
90{
91 if (png_ptr != NULL && png_ptr->error_fn != NULL)
92 (*(png_ptr->error_fn))(png_ptr, '\0');
Guy Schalnat6d764711995-12-19 03:22:19 -060093
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050094 /* If the custom handler doesn't exist, or if it returns,
95 use the default handler, which will not return. */
96 png_default_error(png_ptr, '\0');
97}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -050098#endif /* PNG_ERROR_TEXT_SUPPORTED */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050099
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500100#ifdef PNG_WARNINGS_SUPPORTED
Guy Schalnat69b14481996-01-10 02:56:49 -0600101/* This function is called whenever there is a non-fatal error. This function
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600102 * should not be changed. If there is a need to handle warnings differently,
103 * you should supply a replacement warning function and use
104 * png_set_error_fn() to replace the warning function at run-time.
105 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500106void PNGAPI
Glenn Randers-Pehrson837a3d12002-05-10 20:19:58 -0500107png_warning(png_structp png_ptr, png_const_charp warning_message)
Guy Schalnat6d764711995-12-19 03:22:19 -0600108{
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500109 int offset = 0;
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600110 if (png_ptr != NULL)
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500111 {
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600112#ifdef PNG_ERROR_NUMBERS_SUPPORTED
113 if (png_ptr->flags&
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600114 (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600115#endif
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600116 {
117 if (*warning_message == PNG_LITERAL_SHARP)
118 {
119 for (offset = 1; offset < 15; offset++)
120 if (warning_message[offset] == ' ')
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500121 break;
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600122 }
123 }
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500124 }
Glenn Randers-Pehrson398b5a32008-11-23 06:48:29 -0600125 if (png_ptr != NULL && png_ptr->warning_fn != NULL)
126 (*(png_ptr->warning_fn))(png_ptr, warning_message + offset);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600127 else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500128 png_default_warning(png_ptr, warning_message + offset);
Guy Schalnat6d764711995-12-19 03:22:19 -0600129}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500130#endif /* PNG_WARNINGS_SUPPORTED */
Guy Schalnat6d764711995-12-19 03:22:19 -0600131
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500132#ifdef PNG_BENIGN_ERRORS_SUPPORTED
133void PNGAPI
134png_benign_error(png_structp png_ptr, png_const_charp error_message)
135{
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500136 if (png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN)
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600137 png_warning(png_ptr, error_message);
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500138 else
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600139 png_error(png_ptr, error_message);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500140}
141#endif
142
Glenn Randers-Pehrsoncfbed9b2002-05-21 18:06:08 -0500143/* These utilities are used internally to build an error message that relates
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600144 * to the current chunk. The chunk name comes from png_ptr->chunk_name,
145 * this is used to prefix the message. The message is limited in length
146 * to 63 bytes, the name characters are output as hex digits wrapped in []
147 * if the character is invalid.
148 */
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500149#define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
Glenn Randers-Pehrson7cd899c1998-03-07 16:17:42 -0600150static PNG_CONST char png_digit[16] = {
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500151 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
152 'A', 'B', 'C', 'D', 'E', 'F'
153};
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600154
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500155#define PNG_MAX_ERROR_TEXT 64
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500156#if defined(PNG_WARNINGS_SUPPORTED) || defined(PNG_ERROR_TEXT_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500157static void /* PRIVATE */
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500158png_format_buffer(png_structp png_ptr, png_charp buffer, png_const_charp
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600159 error_message)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600160{
161 int iout = 0, iin = 0;
162
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600163 while (iin < 4)
164 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600165 int c = png_ptr->chunk_name[iin++];
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600166 if (isnonalpha(c))
167 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500168 buffer[iout++] = PNG_LITERAL_LEFT_SQUARE_BRACKET;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600169 buffer[iout++] = png_digit[(c & 0xf0) >> 4];
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600170 buffer[iout++] = png_digit[c & 0x0f];
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500171 buffer[iout++] = PNG_LITERAL_RIGHT_SQUARE_BRACKET;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600172 }
173 else
174 {
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500175 buffer[iout++] = (png_byte)c;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600176 }
177 }
178
Glenn Randers-Pehrson837a3d12002-05-10 20:19:58 -0500179 if (error_message == NULL)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500180 buffer[iout] = '\0';
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600181 else
182 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600183 buffer[iout++] = ':';
184 buffer[iout++] = ' ';
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500185 png_memcpy(buffer + iout, error_message, PNG_MAX_ERROR_TEXT);
186 buffer[iout + PNG_MAX_ERROR_TEXT - 1] = '\0';
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600187 }
188}
189
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500190#ifdef PNG_READ_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500191void PNGAPI
Glenn Randers-Pehrson837a3d12002-05-10 20:19:58 -0500192png_chunk_error(png_structp png_ptr, png_const_charp error_message)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600193{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500194 char msg[18+PNG_MAX_ERROR_TEXT];
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600195 if (png_ptr == NULL)
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600196 png_error(png_ptr, error_message);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500197 else
198 {
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600199 png_format_buffer(png_ptr, msg, error_message);
200 png_error(png_ptr, msg);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500201 }
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600202}
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500203#endif /* PNG_READ_SUPPORTED */
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500204#endif /* PNG_WARNINGS_SUPPORTED || PNG_ERROR_TEXT_SUPPORTED */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600205
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500206#ifdef PNG_WARNINGS_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500207void PNGAPI
Glenn Randers-Pehrson837a3d12002-05-10 20:19:58 -0500208png_chunk_warning(png_structp png_ptr, png_const_charp warning_message)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600209{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500210 char msg[18+PNG_MAX_ERROR_TEXT];
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600211 if (png_ptr == NULL)
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600212 png_warning(png_ptr, warning_message);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500213 else
214 {
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600215 png_format_buffer(png_ptr, msg, warning_message);
216 png_warning(png_ptr, msg);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500217 }
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600218}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500219#endif /* PNG_WARNINGS_SUPPORTED */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600220
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500221#ifdef PNG_READ_SUPPORTED
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500222#ifdef PNG_BENIGN_ERRORS_SUPPORTED
223void PNGAPI
224png_chunk_benign_error(png_structp png_ptr, png_const_charp error_message)
225{
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600226 if (png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN)
227 png_chunk_warning(png_ptr, error_message);
228 else
229 png_chunk_error(png_ptr, error_message);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500230}
231#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500232#endif /* PNG_READ_SUPPORTED */
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500233
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -0600234#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrsone69b55d2010-01-01 10:29:06 -0600235/* This API only exists if ANSI-C style error handling is used,
236 * otherwise it is necessary for png_default_error to be overridden.
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -0600237 */
238jmp_buf* PNGAPI
Glenn Randers-Pehrsone69b55d2010-01-01 10:29:06 -0600239png_set_longjmp_fn(png_structp png_ptr, png_longjmp_ptr longjmp_fn,
240 size_t jmp_buf_size)
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -0600241{
242 if (png_ptr == NULL || jmp_buf_size != png_sizeof(jmp_buf))
243 return NULL;
244
245 png_ptr->longjmp_fn = longjmp_fn;
Glenn Randers-Pehrson95ca51b2010-02-19 14:09:09 -0600246 return &png_ptr->png_jmpbuf;
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -0600247}
248#endif
249
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600250/* This is the default error handling function. Note that replacements for
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600251 * this function MUST NOT RETURN, or the program will likely crash. This
252 * function is used by default, or if the program supplies NULL for the
253 * error function pointer in png_set_error_fn().
254 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500255static void /* PRIVATE */
Glenn Randers-Pehrson837a3d12002-05-10 20:19:58 -0500256png_default_error(png_structp png_ptr, png_const_charp error_message)
Guy Schalnat6d764711995-12-19 03:22:19 -0600257{
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500258#ifdef PNG_CONSOLE_IO_SUPPORTED
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500259#ifdef PNG_ERROR_NUMBERS_SUPPORTED
Glenn Randers-Pehrson43aaf6e2008-08-05 22:17:03 -0500260 if (*error_message == PNG_LITERAL_SHARP)
Glenn Randers-Pehrson8fb550c2009-03-21 08:15:32 -0500261 {
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600262 /* Strip "#nnnn " from beginning of error message. */
263 int offset;
264 char error_number[16];
265 for (offset = 0; offset<15; offset++)
266 {
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500267 error_number[offset] = error_message[offset + 1];
268 if (error_message[offset] == ' ')
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600269 break;
270 }
271 if ((offset > 1) && (offset < 15))
272 {
273 error_number[offset - 1] = '\0';
274 fprintf(stderr, "libpng error no. %s: %s",
275 error_number, error_message + offset + 1);
276 fprintf(stderr, PNG_STRING_NEWLINE);
277 }
278 else
279 {
280 fprintf(stderr, "libpng error: %s, offset=%d",
281 error_message, offset);
282 fprintf(stderr, PNG_STRING_NEWLINE);
283 }
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500284 }
285 else
286#endif
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500287 {
288 fprintf(stderr, "libpng error: %s", error_message);
289 fprintf(stderr, PNG_STRING_NEWLINE);
290 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600291#endif
Glenn Randers-Pehrsonf98726a2010-02-19 09:54:53 -0600292 png_longjmp(png_ptr, 1);
293#ifndef PNG_CONSOLE_IO_SUPPORTED
294 error_message = error_message; /* Make compiler happy */
295#endif
296}
Guy Schalnat6d764711995-12-19 03:22:19 -0600297
Glenn Randers-Pehrsonf98726a2010-02-19 09:54:53 -0600298void PNGAPI
299png_longjmp(png_structp png_ptr, int val)
300{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600301#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -0600302 if (png_ptr && png_ptr->longjmp_fn)
Glenn Randers-Pehrson6b12c082006-11-14 10:53:30 -0600303 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600304# ifdef USE_FAR_KEYWORD
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600305 {
Glenn Randers-Pehrson95ca51b2010-02-19 14:09:09 -0600306 jmp_buf png_jmpbuf;
307 png_memcpy(png_jmpbuf, png_ptr->png_jmpbuf, png_sizeof(jmp_buf));
308 png_ptr->longjmp_fn(png_jmpbuf, val);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600309 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600310# else
Glenn Randers-Pehrson95ca51b2010-02-19 14:09:09 -0600311 png_ptr->longjmp_fn(png_ptr->png_jmpbuf, val);
Glenn Randers-Pehrson6b12c082006-11-14 10:53:30 -0600312# endif
313 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600314#endif
Glenn Randers-Pehrsond29033f2009-11-07 10:46:42 -0600315 /* Here if not setjmp support or if png_ptr is null. */
316 PNG_ABORT();
Guy Schalnat6d764711995-12-19 03:22:19 -0600317}
318
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500319#ifdef PNG_WARNINGS_SUPPORTED
Guy Schalnat69b14481996-01-10 02:56:49 -0600320/* This function is called when there is a warning, but the library thinks
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600321 * it can continue anyway. Replacement functions don't have to do anything
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500322 * here if you don't want them to. In the default configuration, png_ptr is
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600323 * not used, but it is passed in case it may be useful.
324 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500325static void /* PRIVATE */
Glenn Randers-Pehrson837a3d12002-05-10 20:19:58 -0500326png_default_warning(png_structp png_ptr, png_const_charp warning_message)
Guy Schalnat6d764711995-12-19 03:22:19 -0600327{
Glenn Randers-Pehrson2f89d762009-10-13 18:04:41 -0500328#ifdef PNG_CONSOLE_IO_SUPPORTED
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500329# ifdef PNG_ERROR_NUMBERS_SUPPORTED
Glenn Randers-Pehrson43aaf6e2008-08-05 22:17:03 -0500330 if (*warning_message == PNG_LITERAL_SHARP)
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500331 {
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600332 int offset;
333 char warning_number[16];
334 for (offset = 0; offset < 15; offset++)
335 {
336 warning_number[offset] = warning_message[offset + 1];
337 if (warning_message[offset] == ' ')
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500338 break;
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600339 }
340 if ((offset > 1) && (offset < 15))
341 {
342 warning_number[offset + 1] = '\0';
343 fprintf(stderr, "libpng warning no. %s: %s",
344 warning_number, warning_message + offset);
345 fprintf(stderr, PNG_STRING_NEWLINE);
346 }
347 else
348 {
349 fprintf(stderr, "libpng warning: %s",
350 warning_message);
351 fprintf(stderr, PNG_STRING_NEWLINE);
352 }
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500353 }
354 else
355# endif
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500356 {
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600357 fprintf(stderr, "libpng warning: %s", warning_message);
358 fprintf(stderr, PNG_STRING_NEWLINE);
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500359 }
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500360#else
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500361 warning_message = warning_message; /* Make compiler happy */
Guy Schalnat6d764711995-12-19 03:22:19 -0600362#endif
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500363 png_ptr = png_ptr; /* Make compiler happy */
Guy Schalnat6d764711995-12-19 03:22:19 -0600364}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500365#endif /* PNG_WARNINGS_SUPPORTED */
Guy Schalnat6d764711995-12-19 03:22:19 -0600366
Guy Schalnat69b14481996-01-10 02:56:49 -0600367/* This function is called when the application wants to use another method
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600368 * of handling errors and warnings. Note that the error function MUST NOT
369 * return to the calling routine or serious problems will occur. The return
Glenn Randers-Pehrson95ca51b2010-02-19 14:09:09 -0600370 * method used in the default routine calls longjmp(png_ptr->png_jmpbuf, 1)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600371 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500372void PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -0500373png_set_error_fn(png_structp png_ptr, png_voidp error_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600374 png_error_ptr error_fn, png_error_ptr warning_fn)
Guy Schalnat6d764711995-12-19 03:22:19 -0600375{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600376 if (png_ptr == NULL)
377 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500378 png_ptr->error_ptr = error_ptr;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600379 png_ptr->error_fn = error_fn;
380 png_ptr->warning_fn = warning_fn;
Guy Schalnat6d764711995-12-19 03:22:19 -0600381}
382
383
Guy Schalnate5a37791996-06-05 15:50:50 -0500384/* This function returns a pointer to the error_ptr associated with the user
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600385 * functions. The application should free any memory associated with this
386 * pointer before png_write_destroy and png_read_destroy are called.
387 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500388png_voidp PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -0500389png_get_error_ptr(png_structp png_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -0600390{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600391 if (png_ptr == NULL)
392 return NULL;
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600393 return ((png_voidp)png_ptr->error_ptr);
Guy Schalnat6d764711995-12-19 03:22:19 -0600394}
395
396
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500397#ifdef PNG_ERROR_NUMBERS_SUPPORTED
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600398void PNGAPI
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500399png_set_strip_error_numbers(png_structp png_ptr, png_uint_32 strip_mode)
400{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500401 if (png_ptr != NULL)
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500402 {
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600403 png_ptr->flags &=
404 ((~(PNG_FLAG_STRIP_ERROR_NUMBERS |
405 PNG_FLAG_STRIP_ERROR_TEXT))&strip_mode);
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500406 }
407}
408#endif
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600409#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */