blob: c2b8c73e0239a35634f0a5685b090c0de0c1857f [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-Pehrson13831bc2011-12-21 08:28:28 -06004 * Last changed in libpng 1.6.0 [(PENDING RELEASE)]
Glenn Randers-Pehrson64b863c2011-01-04 09:57:06 -06005 * Copyright (c) 1998-2011 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-Pehrsonbeb572e2006-08-19 13:59:24 -050019#include "pngpriv.h"
Guy Schalnat6d764711995-12-19 03:22:19 -060020
Glenn Randers-Pehrsonc3cd22b2010-03-08 21:10:25 -060021#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
22
John Bowler5d567862011-12-24 09:12:00 -060023static PNG_FUNCTION(void, png_default_error,PNGARG((png_const_structrp png_ptr,
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -050024 png_const_charp error_message)),PNG_NORETURN);
25
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -050026#ifdef PNG_WARNINGS_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050027static void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -060028png_default_warning PNGARG((png_const_structrp png_ptr,
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -050029 png_const_charp warning_message));
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -050030#endif /* PNG_WARNINGS_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -050031
Guy Schalnat69b14481996-01-10 02:56:49 -060032/* This function is called whenever there is a fatal error. This function
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060033 * 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.
36 */
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -050037#ifdef PNG_ERROR_TEXT_SUPPORTED
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -050038PNG_FUNCTION(void,PNGAPI
John Bowler5d567862011-12-24 09:12:00 -060039png_error,(png_const_structrp png_ptr, png_const_charp error_message),
John Bowler40b26032011-12-22 08:09:15 -060040 PNG_NORETURN)
Guy Schalnat6d764711995-12-19 03:22:19 -060041{
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -050042#ifdef PNG_ERROR_NUMBERS_SUPPORTED
43 char msg[16];
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -060044 if (png_ptr != NULL)
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -050045 {
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050046 if (png_ptr->flags&
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -060047 (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050048 {
49 if (*error_message == PNG_LITERAL_SHARP)
50 {
51 /* Strip "#nnnn " from beginning of error message. */
52 int offset;
53 for (offset = 1; offset<15; offset++)
54 if (error_message[offset] == ' ')
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050055 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050056
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
66 else
67 error_message += offset;
68 }
69
70 else
71 {
72 if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT)
73 {
74 msg[0] = '0';
75 msg[1] = '\0';
76 error_message = msg;
77 }
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050078 }
79 }
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -050080 }
81#endif
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -050082 if (png_ptr != NULL && png_ptr->error_fn != NULL)
John Bowler5d567862011-12-24 09:12:00 -060083 (*(png_ptr->error_fn))(png_constcast(png_structrp,png_ptr), error_message);
Guy Schalnat6d764711995-12-19 03:22:19 -060084
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -050085 /* If the custom handler doesn't exist, or if it returns,
86 use the default handler, which will not return. */
Glenn Randers-Pehrson837a3d12002-05-10 20:19:58 -050087 png_default_error(png_ptr, error_message);
Guy Schalnat6d764711995-12-19 03:22:19 -060088}
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050089#else
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -050090PNG_FUNCTION(void,PNGAPI
John Bowler5d567862011-12-24 09:12:00 -060091png_err,(png_const_structrp png_ptr),PNG_NORETURN)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050092{
Glenn Randers-Pehrsonbb4f77c2011-05-16 20:40:59 -050093 /* Prior to 1.5.2 the error_fn received a NULL pointer, expressed
94 * erroneously as '\0', instead of the empty string "". This was
95 * apparently an error, introduced in libpng-1.2.20, and png_default_error
96 * will crash in this case.
John Bowler88b77cc2011-05-05 06:49:55 -050097 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050098 if (png_ptr != NULL && png_ptr->error_fn != NULL)
John Bowler88b77cc2011-05-05 06:49:55 -050099 (*(png_ptr->error_fn))(png_ptr, "");
Guy Schalnat6d764711995-12-19 03:22:19 -0600100
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500101 /* If the custom handler doesn't exist, or if it returns,
102 use the default handler, which will not return. */
John Bowler88b77cc2011-05-05 06:49:55 -0500103 png_default_error(png_ptr, "");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500104}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500105#endif /* PNG_ERROR_TEXT_SUPPORTED */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500106
John Bowler88b77cc2011-05-05 06:49:55 -0500107/* Utility to safely appends strings to a buffer. This never errors out so
108 * error checking is not required in the caller.
109 */
110size_t
111png_safecat(png_charp buffer, size_t bufsize, size_t pos,
112 png_const_charp string)
113{
114 if (buffer != NULL && pos < bufsize)
115 {
John Bowlerc5bef942011-05-05 17:35:39 -0500116 if (string != NULL)
117 while (*string != '\0' && pos < bufsize-1)
118 buffer[pos++] = *string++;
John Bowler88b77cc2011-05-05 06:49:55 -0500119
120 buffer[pos] = '\0';
121 }
122
123 return pos;
124}
125
John Bowlerc5bef942011-05-05 17:35:39 -0500126#if defined(PNG_WARNINGS_SUPPORTED) || defined(PNG_TIME_RFC1123_SUPPORTED)
John Bowler88b77cc2011-05-05 06:49:55 -0500127/* Utility to dump an unsigned value into a buffer, given a start pointer and
128 * and end pointer (which should point just *beyond* the end of the buffer!)
129 * Returns the pointer to the start of the formatted string.
130 */
131png_charp
132png_format_number(png_const_charp start, png_charp end, int format,
133 png_alloc_size_t number)
134{
135 int count = 0; /* number of digits output */
136 int mincount = 1; /* minimum number required */
137 int output = 0; /* digit output (for the fixed point format) */
138
139 *--end = '\0';
140
141 /* This is written so that the loop always runs at least once, even with
142 * number zero.
143 */
144 while (end > start && (number != 0 || count < mincount))
145 {
146
147 static const char digits[] = "0123456789ABCDEF";
148
149 switch (format)
150 {
151 case PNG_NUMBER_FORMAT_fixed:
152 /* Needs five digits (the fraction) */
153 mincount = 5;
154 if (output || number % 10 != 0)
155 {
156 *--end = digits[number % 10];
157 output = 1;
158 }
159 number /= 10;
160 break;
161
162 case PNG_NUMBER_FORMAT_02u:
163 /* Expects at least 2 digits. */
164 mincount = 2;
165 /* fall through */
166
167 case PNG_NUMBER_FORMAT_u:
168 *--end = digits[number % 10];
169 number /= 10;
170 break;
171
172 case PNG_NUMBER_FORMAT_02x:
173 /* This format expects at least two digits */
174 mincount = 2;
175 /* fall through */
176
177 case PNG_NUMBER_FORMAT_x:
178 *--end = digits[number & 0xf];
179 number >>= 4;
180 break;
181
182 default: /* an error */
183 number = 0;
184 break;
185 }
186
187 /* Keep track of the number of digits added */
188 ++count;
189
190 /* Float a fixed number here: */
191 if (format == PNG_NUMBER_FORMAT_fixed) if (count == 5) if (end > start)
192 {
193 /* End of the fraction, but maybe nothing was output? In that case
194 * drop the decimal point. If the number is a true zero handle that
195 * here.
196 */
197 if (output)
198 *--end = '.';
199 else if (number == 0) /* and !output */
200 *--end = '0';
201 }
202 }
203
204 return end;
205}
206#endif
207
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500208#ifdef PNG_WARNINGS_SUPPORTED
Guy Schalnat69b14481996-01-10 02:56:49 -0600209/* This function is called whenever there is a non-fatal error. This function
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600210 * should not be changed. If there is a need to handle warnings differently,
211 * you should supply a replacement warning function and use
212 * png_set_error_fn() to replace the warning function at run-time.
213 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500214void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600215png_warning(png_const_structrp png_ptr, png_const_charp warning_message)
Guy Schalnat6d764711995-12-19 03:22:19 -0600216{
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500217 int offset = 0;
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600218 if (png_ptr != NULL)
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500219 {
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600220#ifdef PNG_ERROR_NUMBERS_SUPPORTED
221 if (png_ptr->flags&
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600222 (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600223#endif
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600224 {
225 if (*warning_message == PNG_LITERAL_SHARP)
226 {
227 for (offset = 1; offset < 15; offset++)
228 if (warning_message[offset] == ' ')
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500229 break;
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600230 }
231 }
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500232 }
Glenn Randers-Pehrson398b5a32008-11-23 06:48:29 -0600233 if (png_ptr != NULL && png_ptr->warning_fn != NULL)
John Bowler5d567862011-12-24 09:12:00 -0600234 (*(png_ptr->warning_fn))(png_constcast(png_structrp,png_ptr),
John Bowler40b26032011-12-22 08:09:15 -0600235 warning_message + offset);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600236 else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500237 png_default_warning(png_ptr, warning_message + offset);
Guy Schalnat6d764711995-12-19 03:22:19 -0600238}
John Bowler88b77cc2011-05-05 06:49:55 -0500239
240/* These functions support 'formatted' warning messages with up to
241 * PNG_WARNING_PARAMETER_COUNT parameters. In the format string the parameter
242 * is introduced by @<number>, where 'number' starts at 1. This follows the
243 * standard established by X/Open for internationalizable error messages.
244 */
245void
246png_warning_parameter(png_warning_parameters p, int number,
247 png_const_charp string)
248{
249 if (number > 0 && number <= PNG_WARNING_PARAMETER_COUNT)
250 (void)png_safecat(p[number-1], (sizeof p[number-1]), 0, string);
251}
252
253void
254png_warning_parameter_unsigned(png_warning_parameters p, int number, int format,
255 png_alloc_size_t value)
256{
257 char buffer[PNG_NUMBER_BUFFER_SIZE];
258 png_warning_parameter(p, number, PNG_FORMAT_NUMBER(buffer, format, value));
259}
260
261void
262png_warning_parameter_signed(png_warning_parameters p, int number, int format,
263 png_int_32 value)
264{
265 png_alloc_size_t u;
266 png_charp str;
267 char buffer[PNG_NUMBER_BUFFER_SIZE];
268
269 /* Avoid overflow by doing the negate in a png_alloc_size_t: */
270 u = (png_alloc_size_t)value;
271 if (value < 0)
272 u = ~u + 1;
273
274 str = PNG_FORMAT_NUMBER(buffer, format, u);
275
276 if (value < 0 && str > buffer)
277 *--str = '-';
278
279 png_warning_parameter(p, number, str);
280}
281
282void
John Bowler5d567862011-12-24 09:12:00 -0600283png_formatted_warning(png_const_structrp png_ptr, png_warning_parameters p,
John Bowler88b77cc2011-05-05 06:49:55 -0500284 png_const_charp message)
285{
286 /* The internal buffer is just 128 bytes - enough for all our messages,
287 * overflow doesn't happen because this code checks!
288 */
289 size_t i;
290 char msg[128];
291
292 for (i=0; i<(sizeof msg)-1 && *message != '\0'; ++i)
293 {
294 if (*message == '@')
295 {
296 int parameter = -1;
297 switch (*++message)
298 {
299 case '1':
300 parameter = 0;
301 break;
302
303 case '2':
304 parameter = 1;
305 break;
306
307 case '\0':
308 continue; /* To break out of the for loop above. */
309
310 default:
311 break;
312 }
313
314 if (parameter >= 0 && parameter < PNG_WARNING_PARAMETER_COUNT)
315 {
316 /* Append this parameter */
317 png_const_charp parm = p[parameter];
318 png_const_charp pend = p[parameter] + (sizeof p[parameter]);
319
320 /* No need to copy the trailing '\0' here, but there is no guarantee
321 * that parm[] has been initialized, so there is no guarantee of a
322 * trailing '\0':
323 */
324 for (; i<(sizeof msg)-1 && parm != '\0' && parm < pend; ++i)
325 msg[i] = *parm++;
326
327 ++message;
328 continue;
329 }
330
331 /* else not a parameter and there is a character after the @ sign; just
332 * copy that.
333 */
334 }
335
336 /* At this point *message can't be '\0', even in the bad parameter case
337 * above where there is a lone '@' at the end of the message string.
338 */
339 msg[i] = *message++;
340 }
341
342 /* i is always less than (sizeof msg), so: */
343 msg[i] = '\0';
344
345 /* And this is the formatted message: */
346 png_warning(png_ptr, msg);
347}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500348#endif /* PNG_WARNINGS_SUPPORTED */
Guy Schalnat6d764711995-12-19 03:22:19 -0600349
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500350#ifdef PNG_BENIGN_ERRORS_SUPPORTED
351void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600352png_benign_error(png_const_structrp png_ptr, png_const_charp error_message)
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500353{
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500354 if (png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN)
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600355 png_warning(png_ptr, error_message);
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500356 else
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600357 png_error(png_ptr, error_message);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500358}
359#endif
360
Glenn Randers-Pehrsoncfbed9b2002-05-21 18:06:08 -0500361/* These utilities are used internally to build an error message that relates
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600362 * to the current chunk. The chunk name comes from png_ptr->chunk_name,
363 * this is used to prefix the message. The message is limited in length
364 * to 63 bytes, the name characters are output as hex digits wrapped in []
365 * if the character is invalid.
366 */
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500367#define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
Glenn Randers-Pehrson7cd899c1998-03-07 16:17:42 -0600368static PNG_CONST char png_digit[16] = {
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500369 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
370 'A', 'B', 'C', 'D', 'E', 'F'
371};
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600372
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500373#define PNG_MAX_ERROR_TEXT 64
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500374#if defined(PNG_WARNINGS_SUPPORTED) || defined(PNG_ERROR_TEXT_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500375static void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -0600376png_format_buffer(png_const_structrp png_ptr, png_charp buffer, png_const_charp
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600377 error_message)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600378{
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500379 png_uint_32 chunk_name = png_ptr->chunk_name;
380 int iout = 0, ishift = 24;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600381
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500382 while (ishift >= 0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600383 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500384 int c = (int)(chunk_name >> ishift) & 0xff;
385
386 ishift -= 8;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600387 if (isnonalpha(c))
388 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500389 buffer[iout++] = PNG_LITERAL_LEFT_SQUARE_BRACKET;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600390 buffer[iout++] = png_digit[(c & 0xf0) >> 4];
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600391 buffer[iout++] = png_digit[c & 0x0f];
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500392 buffer[iout++] = PNG_LITERAL_RIGHT_SQUARE_BRACKET;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600393 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500394
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600395 else
396 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500397 buffer[iout++] = (char)c;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600398 }
399 }
400
Glenn Randers-Pehrson837a3d12002-05-10 20:19:58 -0500401 if (error_message == NULL)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500402 buffer[iout] = '\0';
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500403
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600404 else
405 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500406 int iin = 0;
407
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600408 buffer[iout++] = ':';
409 buffer[iout++] = ' ';
Glenn Randers-Pehrson07e1d342011-06-07 14:35:30 -0500410
Glenn Randers-Pehrson07e1d342011-06-07 14:35:30 -0500411 while (iin < PNG_MAX_ERROR_TEXT-1 && error_message[iin] != '\0')
412 buffer[iout++] = error_message[iin++];
413
414 /* iin < PNG_MAX_ERROR_TEXT, so the following is safe: */
415 buffer[iout] = '\0';
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600416 }
417}
Glenn Randers-Pehrson8bdfb472010-04-17 18:05:01 -0500418#endif /* PNG_WARNINGS_SUPPORTED || PNG_ERROR_TEXT_SUPPORTED */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600419
Glenn Randers-Pehrson8bdfb472010-04-17 18:05:01 -0500420#if defined(PNG_READ_SUPPORTED) && defined(PNG_ERROR_TEXT_SUPPORTED)
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500421PNG_FUNCTION(void,PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600422png_chunk_error,(png_const_structrp png_ptr, png_const_charp error_message),
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500423 PNG_NORETURN)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600424{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500425 char msg[18+PNG_MAX_ERROR_TEXT];
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600426 if (png_ptr == NULL)
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600427 png_error(png_ptr, error_message);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500428
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500429 else
430 {
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600431 png_format_buffer(png_ptr, msg, error_message);
432 png_error(png_ptr, msg);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500433 }
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600434}
Glenn Randers-Pehrson8bdfb472010-04-17 18:05:01 -0500435#endif /* PNG_READ_SUPPORTED && PNG_ERROR_TEXT_SUPPORTED */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600436
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500437#ifdef PNG_WARNINGS_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500438void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600439png_chunk_warning(png_const_structrp png_ptr, png_const_charp warning_message)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600440{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500441 char msg[18+PNG_MAX_ERROR_TEXT];
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600442 if (png_ptr == NULL)
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600443 png_warning(png_ptr, warning_message);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500444
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500445 else
446 {
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600447 png_format_buffer(png_ptr, msg, warning_message);
448 png_warning(png_ptr, msg);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500449 }
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600450}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500451#endif /* PNG_WARNINGS_SUPPORTED */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600452
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500453#ifdef PNG_READ_SUPPORTED
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500454#ifdef PNG_BENIGN_ERRORS_SUPPORTED
455void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600456png_chunk_benign_error(png_const_structrp png_ptr, png_const_charp error_message)
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500457{
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600458 if (png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN)
459 png_chunk_warning(png_ptr, error_message);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500460
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600461 else
462 png_chunk_error(png_ptr, error_message);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500463}
464#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500465#endif /* PNG_READ_SUPPORTED */
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500466
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500467#ifdef PNG_ERROR_TEXT_SUPPORTED
468#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500469PNG_FUNCTION(void,
John Bowler5d567862011-12-24 09:12:00 -0600470png_fixed_error,(png_const_structrp png_ptr, png_const_charp name),PNG_NORETURN)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500471{
472# define fixed_message "fixed point overflow in "
473# define fixed_message_ln ((sizeof fixed_message)-1)
474 int iin;
475 char msg[fixed_message_ln+PNG_MAX_ERROR_TEXT];
476 png_memcpy(msg, fixed_message, fixed_message_ln);
477 iin = 0;
478 if (name != NULL) while (iin < (PNG_MAX_ERROR_TEXT-1) && name[iin] != 0)
479 {
480 msg[fixed_message_ln + iin] = name[iin];
481 ++iin;
482 }
483 msg[fixed_message_ln + iin] = 0;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500484 png_error(png_ptr, msg);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500485}
486#endif
487#endif
488
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -0600489#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrsone69b55d2010-01-01 10:29:06 -0600490/* This API only exists if ANSI-C style error handling is used,
491 * otherwise it is necessary for png_default_error to be overridden.
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -0600492 */
493jmp_buf* PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600494png_set_longjmp_fn(png_structrp png_ptr, png_longjmp_ptr longjmp_fn,
Glenn Randers-Pehrsone69b55d2010-01-01 10:29:06 -0600495 size_t jmp_buf_size)
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -0600496{
John Bowlerd332c672011-12-21 17:36:12 -0600497 /* From libpng 1.6.0 the app gets one chance to set a 'jmpbuf_size' value
498 * and it must not change after that. Libpng doesn't care how big the
499 * buffer is, just that it doesn't change.
500 *
501 * If the buffer size is no *larger* than the size of jmp_buf when libpng is
502 * compiled a built in jmp_buf is returned; this preserves the pre-1.6.0
503 * semantics that this call will not fail. If the size is larger, however,
504 * the buffer is allocated and this may fail, causing the function to return
505 * NULL.
506 */
507 if (png_ptr == NULL)
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -0600508 return NULL;
509
John Bowlerd332c672011-12-21 17:36:12 -0600510 if (png_ptr->jmp_buf_ptr == NULL)
511 {
512 png_ptr->jmp_buf_size = 0; /* not allocated */
513
514 if (jmp_buf_size <= sizeof png_ptr->jmp_buf_local)
515 png_ptr->jmp_buf_ptr = &png_ptr->jmp_buf_local;
516
517 else
518 {
519 png_ptr->jmp_buf_ptr = png_voidcast(jmp_buf *,
520 png_malloc_warn(png_ptr, jmp_buf_size));
521
522 if (png_ptr->jmp_buf_ptr == NULL)
523 return NULL; /* new NULL return on OOM */
524
525 png_ptr->jmp_buf_size = jmp_buf_size;
526 }
527 }
528
529 else /* Already allocated: check the size */
530 {
531 size_t size = png_ptr->jmp_buf_size;
532
533 if (size == 0)
534 {
535 size = sizeof png_ptr->jmp_buf_local;
536 if (png_ptr->jmp_buf_ptr != &png_ptr->jmp_buf_local)
537 {
538 /* This is an internal error in libpng: somehow we have been left
539 * with a stack allocated jmp_buf when the application regained
540 * control. It's always possible to fix this up, but for the moment
541 * this is a png_error because that makes it easy to detect.
542 */
543 png_error(png_ptr, "Libpng jmp_buf still allocated");
544 /* png_ptr->jmp_buf_ptr = &png_ptr->jmp_buf_local; */
545 }
546 }
547
548 if (size != jmp_buf_size)
549 {
550 png_warning(png_ptr, "Application jmp_buf size changed");
551 return NULL; /* caller will probably crash: no choice here */
552 }
553 }
554
555 /* Finally fill in the function, now we have a satisfactory buffer. It is
556 * valid to change the function on every call.
557 */
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -0600558 png_ptr->longjmp_fn = longjmp_fn;
John Bowlerd332c672011-12-21 17:36:12 -0600559 return png_ptr->jmp_buf_ptr;
560}
561
562void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -0600563png_free_jmpbuf(png_structrp png_ptr)
John Bowlerd332c672011-12-21 17:36:12 -0600564{
565 if (png_ptr != NULL)
566 {
567 jmp_buf *jb = png_ptr->jmp_buf_ptr;
568
569 /* A size of 0 is used to indicate a local, stack, allocation of the
570 * pointer; used here and in png.c
571 */
572 if (jb != NULL && png_ptr->jmp_buf_size > 0)
573 {
574
575 /* This stuff is so that a failure to free the error control structure
576 * does not leave libpng in a state with no valid error handling: the
577 * free always succeeds, if there is an error it gets ignored.
578 */
579 if (jb != &png_ptr->jmp_buf_local)
580 {
581 /* Make an internal, libpng, jmp_buf to return here */
582 jmp_buf free_jmp_buf;
583
584 if (!setjmp(free_jmp_buf))
585 {
586 png_ptr->jmp_buf_ptr = &free_jmp_buf; /* come back here */
587 png_ptr->jmp_buf_size = 0; /* stack allocation */
588 png_ptr->longjmp_fn = longjmp;
589 png_free(png_ptr, jb); /* Return to setjmp on error */
590 }
591 }
592 }
593
594 /* *Always* cancel everything out: */
595 png_ptr->jmp_buf_size = 0;
596 png_ptr->jmp_buf_ptr = NULL;
597 png_ptr->longjmp_fn = 0;
598 }
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -0600599}
600#endif
601
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600602/* This is the default error handling function. Note that replacements for
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600603 * this function MUST NOT RETURN, or the program will likely crash. This
604 * function is used by default, or if the program supplies NULL for the
605 * error function pointer in png_set_error_fn().
606 */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500607static PNG_FUNCTION(void /* PRIVATE */,
John Bowler5d567862011-12-24 09:12:00 -0600608png_default_error,(png_const_structrp png_ptr, png_const_charp error_message),
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500609 PNG_NORETURN)
Guy Schalnat6d764711995-12-19 03:22:19 -0600610{
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500611#ifdef PNG_CONSOLE_IO_SUPPORTED
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500612#ifdef PNG_ERROR_NUMBERS_SUPPORTED
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -0500613 /* Check on NULL only added in 1.5.4 */
John Bowler88b77cc2011-05-05 06:49:55 -0500614 if (error_message != NULL && *error_message == PNG_LITERAL_SHARP)
Glenn Randers-Pehrson8fb550c2009-03-21 08:15:32 -0500615 {
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600616 /* Strip "#nnnn " from beginning of error message. */
617 int offset;
618 char error_number[16];
619 for (offset = 0; offset<15; offset++)
620 {
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500621 error_number[offset] = error_message[offset + 1];
622 if (error_message[offset] == ' ')
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600623 break;
624 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500625
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600626 if ((offset > 1) && (offset < 15))
627 {
628 error_number[offset - 1] = '\0';
629 fprintf(stderr, "libpng error no. %s: %s",
630 error_number, error_message + offset + 1);
631 fprintf(stderr, PNG_STRING_NEWLINE);
632 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500633
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600634 else
635 {
636 fprintf(stderr, "libpng error: %s, offset=%d",
637 error_message, offset);
638 fprintf(stderr, PNG_STRING_NEWLINE);
639 }
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500640 }
641 else
642#endif
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500643 {
John Bowler88b77cc2011-05-05 06:49:55 -0500644 fprintf(stderr, "libpng error: %s", error_message ? error_message :
645 "undefined");
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500646 fprintf(stderr, PNG_STRING_NEWLINE);
647 }
John Bowler88b77cc2011-05-05 06:49:55 -0500648#else
Glenn Randers-Pehrsond546f432010-12-04 20:41:36 -0600649 PNG_UNUSED(error_message) /* Make compiler happy */
Glenn Randers-Pehrsonf98726a2010-02-19 09:54:53 -0600650#endif
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500651 png_longjmp(png_ptr, 1);
Glenn Randers-Pehrsonf98726a2010-02-19 09:54:53 -0600652}
Guy Schalnat6d764711995-12-19 03:22:19 -0600653
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500654PNG_FUNCTION(void,PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600655png_longjmp,(png_const_structrp png_ptr, int val),PNG_NORETURN)
Glenn Randers-Pehrsonf98726a2010-02-19 09:54:53 -0600656{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600657#ifdef PNG_SETJMP_SUPPORTED
John Bowlerd332c672011-12-21 17:36:12 -0600658 if (png_ptr && png_ptr->longjmp_fn && png_ptr->jmp_buf_ptr)
659 png_ptr->longjmp_fn(*png_ptr->jmp_buf_ptr, val);
Guy Schalnat6d764711995-12-19 03:22:19 -0600660#endif
John Bowlerbaeb6d12011-11-26 18:21:02 -0600661
Glenn Randers-Pehrsond29033f2009-11-07 10:46:42 -0600662 /* Here if not setjmp support or if png_ptr is null. */
663 PNG_ABORT();
Guy Schalnat6d764711995-12-19 03:22:19 -0600664}
665
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500666#ifdef PNG_WARNINGS_SUPPORTED
Guy Schalnat69b14481996-01-10 02:56:49 -0600667/* This function is called when there is a warning, but the library thinks
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600668 * it can continue anyway. Replacement functions don't have to do anything
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500669 * here if you don't want them to. In the default configuration, png_ptr is
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600670 * not used, but it is passed in case it may be useful.
671 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500672static void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -0600673png_default_warning(png_const_structrp png_ptr, png_const_charp warning_message)
Guy Schalnat6d764711995-12-19 03:22:19 -0600674{
Glenn Randers-Pehrson2f89d762009-10-13 18:04:41 -0500675#ifdef PNG_CONSOLE_IO_SUPPORTED
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500676# ifdef PNG_ERROR_NUMBERS_SUPPORTED
Glenn Randers-Pehrson43aaf6e2008-08-05 22:17:03 -0500677 if (*warning_message == PNG_LITERAL_SHARP)
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500678 {
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600679 int offset;
680 char warning_number[16];
681 for (offset = 0; offset < 15; offset++)
682 {
683 warning_number[offset] = warning_message[offset + 1];
684 if (warning_message[offset] == ' ')
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500685 break;
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600686 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500687
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600688 if ((offset > 1) && (offset < 15))
689 {
690 warning_number[offset + 1] = '\0';
691 fprintf(stderr, "libpng warning no. %s: %s",
692 warning_number, warning_message + offset);
693 fprintf(stderr, PNG_STRING_NEWLINE);
694 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500695
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600696 else
697 {
698 fprintf(stderr, "libpng warning: %s",
699 warning_message);
700 fprintf(stderr, PNG_STRING_NEWLINE);
701 }
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500702 }
703 else
704# endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500705
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500706 {
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600707 fprintf(stderr, "libpng warning: %s", warning_message);
708 fprintf(stderr, PNG_STRING_NEWLINE);
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500709 }
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500710#else
Glenn Randers-Pehrsond546f432010-12-04 20:41:36 -0600711 PNG_UNUSED(warning_message) /* Make compiler happy */
Guy Schalnat6d764711995-12-19 03:22:19 -0600712#endif
Glenn Randers-Pehrsond546f432010-12-04 20:41:36 -0600713 PNG_UNUSED(png_ptr) /* Make compiler happy */
Guy Schalnat6d764711995-12-19 03:22:19 -0600714}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500715#endif /* PNG_WARNINGS_SUPPORTED */
Guy Schalnat6d764711995-12-19 03:22:19 -0600716
Guy Schalnat69b14481996-01-10 02:56:49 -0600717/* This function is called when the application wants to use another method
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600718 * of handling errors and warnings. Note that the error function MUST NOT
719 * return to the calling routine or serious problems will occur. The return
John Bowlerd332c672011-12-21 17:36:12 -0600720 * method used in the default routine calls longjmp(png_ptr->jmp_buf_ptr, 1)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600721 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500722void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600723png_set_error_fn(png_structrp png_ptr, png_voidp error_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600724 png_error_ptr error_fn, png_error_ptr warning_fn)
Guy Schalnat6d764711995-12-19 03:22:19 -0600725{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600726 if (png_ptr == NULL)
727 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500728
Guy Schalnate5a37791996-06-05 15:50:50 -0500729 png_ptr->error_ptr = error_ptr;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600730 png_ptr->error_fn = error_fn;
John Bowler88b77cc2011-05-05 06:49:55 -0500731#ifdef PNG_WARNINGS_SUPPORTED
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600732 png_ptr->warning_fn = warning_fn;
John Bowler88b77cc2011-05-05 06:49:55 -0500733#else
734 PNG_UNUSED(warning_fn)
735#endif
Guy Schalnat6d764711995-12-19 03:22:19 -0600736}
737
738
Guy Schalnate5a37791996-06-05 15:50:50 -0500739/* This function returns a pointer to the error_ptr associated with the user
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600740 * functions. The application should free any memory associated with this
741 * pointer before png_write_destroy and png_read_destroy are called.
742 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500743png_voidp PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600744png_get_error_ptr(png_const_structrp png_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -0600745{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600746 if (png_ptr == NULL)
747 return NULL;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500748
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600749 return ((png_voidp)png_ptr->error_ptr);
Guy Schalnat6d764711995-12-19 03:22:19 -0600750}
751
752
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500753#ifdef PNG_ERROR_NUMBERS_SUPPORTED
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600754void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600755png_set_strip_error_numbers(png_structrp png_ptr, png_uint_32 strip_mode)
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500756{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500757 if (png_ptr != NULL)
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500758 {
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600759 png_ptr->flags &=
760 ((~(PNG_FLAG_STRIP_ERROR_NUMBERS |
761 PNG_FLAG_STRIP_ERROR_TEXT))&strip_mode);
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500762 }
763}
764#endif
John Bowler7875d532011-11-07 22:33:49 -0600765
766#if defined PNG_SIMPLIFIED_READ_SUPPORTED ||\
767 defined PNG_SIMPLIFIED_WRITE_SUPPORTED
768 /* Currently the above both depend on SETJMP_SUPPORTED, however it would be
769 * possible to implement without setjmp support just so long as there is some
770 * way to handle the error return here:
771 */
772PNG_FUNCTION(void /* PRIVATE */,
John Bowler40b26032011-12-22 08:09:15 -0600773png_safe_error,(png_structp png_nonconst_ptr, png_const_charp error_message),
John Bowler7875d532011-11-07 22:33:49 -0600774 PNG_NORETURN)
775{
John Bowler5d567862011-12-24 09:12:00 -0600776 const png_const_structrp png_ptr = png_nonconst_ptr;
John Bowler4fa96a42011-11-16 16:39:16 -0600777 png_imagep image = png_voidcast(png_imagep, png_ptr->error_ptr);
John Bowler7875d532011-11-07 22:33:49 -0600778
779 /* An error is always logged here, overwriting anything (typically a warning)
780 * that is already there:
781 */
782 if (image != NULL)
783 {
784 png_safecat(image->message, sizeof image->message, 0, error_message);
785 image->warning_or_error = 1;
786
John Bowler4fa96a42011-11-16 16:39:16 -0600787 /* Retrieve the jmp_buf from within the png_control, making this work for
788 * C++ compilation too is pretty tricky: C++ wants a pointer to the first
789 * element of a jmp_buf, but C doesn't tell us the type of that.
790 */
John Bowler7875d532011-11-07 22:33:49 -0600791 if (image->opaque != NULL && image->opaque->error_buf != NULL)
John Bowler4fa96a42011-11-16 16:39:16 -0600792 longjmp(png_control_jmp_buf(image->opaque), 1);
John Bowler7875d532011-11-07 22:33:49 -0600793
794 /* Missing longjmp buffer, the following is to help debugging: */
795 {
796 size_t pos = png_safecat(image->message, sizeof image->message, 0,
797 "bad longjmp: ");
798 png_safecat(image->message, sizeof image->message, pos, error_message);
799 }
800 }
801
802 /* Here on an internal programming error. */
803 abort();
804}
805
806#ifdef PNG_WARNINGS_SUPPORTED
807void /* PRIVATE */
John Bowler40b26032011-12-22 08:09:15 -0600808png_safe_warning(png_structp png_nonconst_ptr, png_const_charp warning_message)
John Bowler7875d532011-11-07 22:33:49 -0600809{
John Bowler5d567862011-12-24 09:12:00 -0600810 const png_const_structrp png_ptr = png_nonconst_ptr;
John Bowler4fa96a42011-11-16 16:39:16 -0600811 png_imagep image = png_voidcast(png_imagep, png_ptr->error_ptr);
John Bowler7875d532011-11-07 22:33:49 -0600812
Glenn Randers-Pehrson84b0da92011-11-10 06:32:19 -0600813 /* A warning is only logged if there is no prior warning or error. */
John Bowler7875d532011-11-07 22:33:49 -0600814 if (image->warning_or_error == 0)
815 {
816 png_safecat(image->message, sizeof image->message, 0, warning_message);
817 image->warning_or_error = 2;
818 }
819}
820#endif
821
822int /* PRIVATE */
823png_safe_execute(png_imagep imageIn, int (*function)(png_voidp), png_voidp arg)
824{
825 volatile png_imagep image = imageIn;
826 volatile int result;
827 volatile png_voidp saved_error_buf;
828 jmp_buf safe_jmpbuf;
829
830 /* Safely execute function(arg) with png_error returning to this function. */
831 saved_error_buf = image->opaque->error_buf;
832 result = setjmp(safe_jmpbuf) == 0;
833
834 if (result)
835 {
836
837 image->opaque->error_buf = safe_jmpbuf;
838 result = function(arg);
839 }
840
841 image->opaque->error_buf = saved_error_buf;
842
843 /* And do the cleanup prior to any failure return. */
844 if (!result)
845 png_image_free(image);
846
847 return result;
848}
849#endif /* SIMPLIFIED READ/WRITE */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600850#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */