blob: 0b0c518b4b41e8611f9f061467278d5103bf31ae [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
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -050023static PNG_FUNCTION(void, png_default_error,PNGARG((png_structp png_ptr,
24 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 */
28png_default_warning PNGARG((png_structp 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
39png_error,(png_structp png_ptr, png_const_charp error_message),PNG_NORETURN)
Guy Schalnat6d764711995-12-19 03:22:19 -060040{
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -050041#ifdef PNG_ERROR_NUMBERS_SUPPORTED
42 char msg[16];
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -060043 if (png_ptr != NULL)
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -050044 {
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050045 if (png_ptr->flags&
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -060046 (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050047 {
48 if (*error_message == PNG_LITERAL_SHARP)
49 {
50 /* Strip "#nnnn " from beginning of error message. */
51 int offset;
52 for (offset = 1; offset<15; offset++)
53 if (error_message[offset] == ' ')
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050054 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050055
56 if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT)
57 {
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;
63 }
64
65 else
66 error_message += offset;
67 }
68
69 else
70 {
71 if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT)
72 {
73 msg[0] = '0';
74 msg[1] = '\0';
75 error_message = msg;
76 }
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050077 }
78 }
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -050079 }
80#endif
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -050081 if (png_ptr != NULL && png_ptr->error_fn != NULL)
Glenn Randers-Pehrson837a3d12002-05-10 20:19:58 -050082 (*(png_ptr->error_fn))(png_ptr, error_message);
Guy Schalnat6d764711995-12-19 03:22:19 -060083
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -050084 /* If the custom handler doesn't exist, or if it returns,
85 use the default handler, which will not return. */
Glenn Randers-Pehrson837a3d12002-05-10 20:19:58 -050086 png_default_error(png_ptr, error_message);
Guy Schalnat6d764711995-12-19 03:22:19 -060087}
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050088#else
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -050089PNG_FUNCTION(void,PNGAPI
90png_err,(png_structp png_ptr),PNG_NORETURN)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050091{
Glenn Randers-Pehrsonbb4f77c2011-05-16 20:40:59 -050092 /* Prior to 1.5.2 the error_fn received a NULL pointer, expressed
93 * erroneously as '\0', instead of the empty string "". This was
94 * apparently an error, introduced in libpng-1.2.20, and png_default_error
95 * will crash in this case.
John Bowler88b77cc2011-05-05 06:49:55 -050096 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050097 if (png_ptr != NULL && png_ptr->error_fn != NULL)
John Bowler88b77cc2011-05-05 06:49:55 -050098 (*(png_ptr->error_fn))(png_ptr, "");
Guy Schalnat6d764711995-12-19 03:22:19 -060099
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500100 /* If the custom handler doesn't exist, or if it returns,
101 use the default handler, which will not return. */
John Bowler88b77cc2011-05-05 06:49:55 -0500102 png_default_error(png_ptr, "");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500103}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500104#endif /* PNG_ERROR_TEXT_SUPPORTED */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500105
John Bowler88b77cc2011-05-05 06:49:55 -0500106/* Utility to safely appends strings to a buffer. This never errors out so
107 * error checking is not required in the caller.
108 */
109size_t
110png_safecat(png_charp buffer, size_t bufsize, size_t pos,
111 png_const_charp string)
112{
113 if (buffer != NULL && pos < bufsize)
114 {
John Bowlerc5bef942011-05-05 17:35:39 -0500115 if (string != NULL)
116 while (*string != '\0' && pos < bufsize-1)
117 buffer[pos++] = *string++;
John Bowler88b77cc2011-05-05 06:49:55 -0500118
119 buffer[pos] = '\0';
120 }
121
122 return pos;
123}
124
John Bowlerc5bef942011-05-05 17:35:39 -0500125#if defined(PNG_WARNINGS_SUPPORTED) || defined(PNG_TIME_RFC1123_SUPPORTED)
John Bowler88b77cc2011-05-05 06:49:55 -0500126/* Utility to dump an unsigned value into a buffer, given a start pointer and
127 * and end pointer (which should point just *beyond* the end of the buffer!)
128 * Returns the pointer to the start of the formatted string.
129 */
130png_charp
131png_format_number(png_const_charp start, png_charp end, int format,
132 png_alloc_size_t number)
133{
134 int count = 0; /* number of digits output */
135 int mincount = 1; /* minimum number required */
136 int output = 0; /* digit output (for the fixed point format) */
137
138 *--end = '\0';
139
140 /* This is written so that the loop always runs at least once, even with
141 * number zero.
142 */
143 while (end > start && (number != 0 || count < mincount))
144 {
145
146 static const char digits[] = "0123456789ABCDEF";
147
148 switch (format)
149 {
150 case PNG_NUMBER_FORMAT_fixed:
151 /* Needs five digits (the fraction) */
152 mincount = 5;
153 if (output || number % 10 != 0)
154 {
155 *--end = digits[number % 10];
156 output = 1;
157 }
158 number /= 10;
159 break;
160
161 case PNG_NUMBER_FORMAT_02u:
162 /* Expects at least 2 digits. */
163 mincount = 2;
164 /* fall through */
165
166 case PNG_NUMBER_FORMAT_u:
167 *--end = digits[number % 10];
168 number /= 10;
169 break;
170
171 case PNG_NUMBER_FORMAT_02x:
172 /* This format expects at least two digits */
173 mincount = 2;
174 /* fall through */
175
176 case PNG_NUMBER_FORMAT_x:
177 *--end = digits[number & 0xf];
178 number >>= 4;
179 break;
180
181 default: /* an error */
182 number = 0;
183 break;
184 }
185
186 /* Keep track of the number of digits added */
187 ++count;
188
189 /* Float a fixed number here: */
190 if (format == PNG_NUMBER_FORMAT_fixed) if (count == 5) if (end > start)
191 {
192 /* End of the fraction, but maybe nothing was output? In that case
193 * drop the decimal point. If the number is a true zero handle that
194 * here.
195 */
196 if (output)
197 *--end = '.';
198 else if (number == 0) /* and !output */
199 *--end = '0';
200 }
201 }
202
203 return end;
204}
205#endif
206
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500207#ifdef PNG_WARNINGS_SUPPORTED
Guy Schalnat69b14481996-01-10 02:56:49 -0600208/* This function is called whenever there is a non-fatal error. This function
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600209 * should not be changed. If there is a need to handle warnings differently,
210 * you should supply a replacement warning function and use
211 * png_set_error_fn() to replace the warning function at run-time.
212 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500213void PNGAPI
Glenn Randers-Pehrson837a3d12002-05-10 20:19:58 -0500214png_warning(png_structp png_ptr, png_const_charp warning_message)
Guy Schalnat6d764711995-12-19 03:22:19 -0600215{
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500216 int offset = 0;
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600217 if (png_ptr != NULL)
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500218 {
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600219#ifdef PNG_ERROR_NUMBERS_SUPPORTED
220 if (png_ptr->flags&
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600221 (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600222#endif
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600223 {
224 if (*warning_message == PNG_LITERAL_SHARP)
225 {
226 for (offset = 1; offset < 15; offset++)
227 if (warning_message[offset] == ' ')
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500228 break;
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600229 }
230 }
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500231 }
Glenn Randers-Pehrson398b5a32008-11-23 06:48:29 -0600232 if (png_ptr != NULL && png_ptr->warning_fn != NULL)
233 (*(png_ptr->warning_fn))(png_ptr, warning_message + offset);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600234 else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500235 png_default_warning(png_ptr, warning_message + offset);
Guy Schalnat6d764711995-12-19 03:22:19 -0600236}
John Bowler88b77cc2011-05-05 06:49:55 -0500237
238/* These functions support 'formatted' warning messages with up to
239 * PNG_WARNING_PARAMETER_COUNT parameters. In the format string the parameter
240 * is introduced by @<number>, where 'number' starts at 1. This follows the
241 * standard established by X/Open for internationalizable error messages.
242 */
243void
244png_warning_parameter(png_warning_parameters p, int number,
245 png_const_charp string)
246{
247 if (number > 0 && number <= PNG_WARNING_PARAMETER_COUNT)
248 (void)png_safecat(p[number-1], (sizeof p[number-1]), 0, string);
249}
250
251void
252png_warning_parameter_unsigned(png_warning_parameters p, int number, int format,
253 png_alloc_size_t value)
254{
255 char buffer[PNG_NUMBER_BUFFER_SIZE];
256 png_warning_parameter(p, number, PNG_FORMAT_NUMBER(buffer, format, value));
257}
258
259void
260png_warning_parameter_signed(png_warning_parameters p, int number, int format,
261 png_int_32 value)
262{
263 png_alloc_size_t u;
264 png_charp str;
265 char buffer[PNG_NUMBER_BUFFER_SIZE];
266
267 /* Avoid overflow by doing the negate in a png_alloc_size_t: */
268 u = (png_alloc_size_t)value;
269 if (value < 0)
270 u = ~u + 1;
271
272 str = PNG_FORMAT_NUMBER(buffer, format, u);
273
274 if (value < 0 && str > buffer)
275 *--str = '-';
276
277 png_warning_parameter(p, number, str);
278}
279
280void
281png_formatted_warning(png_structp png_ptr, png_warning_parameters p,
282 png_const_charp message)
283{
284 /* The internal buffer is just 128 bytes - enough for all our messages,
285 * overflow doesn't happen because this code checks!
286 */
287 size_t i;
288 char msg[128];
289
290 for (i=0; i<(sizeof msg)-1 && *message != '\0'; ++i)
291 {
292 if (*message == '@')
293 {
294 int parameter = -1;
295 switch (*++message)
296 {
297 case '1':
298 parameter = 0;
299 break;
300
301 case '2':
302 parameter = 1;
303 break;
304
305 case '\0':
306 continue; /* To break out of the for loop above. */
307
308 default:
309 break;
310 }
311
312 if (parameter >= 0 && parameter < PNG_WARNING_PARAMETER_COUNT)
313 {
314 /* Append this parameter */
315 png_const_charp parm = p[parameter];
316 png_const_charp pend = p[parameter] + (sizeof p[parameter]);
317
318 /* No need to copy the trailing '\0' here, but there is no guarantee
319 * that parm[] has been initialized, so there is no guarantee of a
320 * trailing '\0':
321 */
322 for (; i<(sizeof msg)-1 && parm != '\0' && parm < pend; ++i)
323 msg[i] = *parm++;
324
325 ++message;
326 continue;
327 }
328
329 /* else not a parameter and there is a character after the @ sign; just
330 * copy that.
331 */
332 }
333
334 /* At this point *message can't be '\0', even in the bad parameter case
335 * above where there is a lone '@' at the end of the message string.
336 */
337 msg[i] = *message++;
338 }
339
340 /* i is always less than (sizeof msg), so: */
341 msg[i] = '\0';
342
343 /* And this is the formatted message: */
344 png_warning(png_ptr, msg);
345}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500346#endif /* PNG_WARNINGS_SUPPORTED */
Guy Schalnat6d764711995-12-19 03:22:19 -0600347
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500348#ifdef PNG_BENIGN_ERRORS_SUPPORTED
349void PNGAPI
350png_benign_error(png_structp png_ptr, png_const_charp error_message)
351{
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500352 if (png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN)
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600353 png_warning(png_ptr, error_message);
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500354 else
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600355 png_error(png_ptr, error_message);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500356}
357#endif
358
Glenn Randers-Pehrsoncfbed9b2002-05-21 18:06:08 -0500359/* These utilities are used internally to build an error message that relates
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600360 * to the current chunk. The chunk name comes from png_ptr->chunk_name,
361 * this is used to prefix the message. The message is limited in length
362 * to 63 bytes, the name characters are output as hex digits wrapped in []
363 * if the character is invalid.
364 */
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500365#define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
Glenn Randers-Pehrson7cd899c1998-03-07 16:17:42 -0600366static PNG_CONST char png_digit[16] = {
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500367 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
368 'A', 'B', 'C', 'D', 'E', 'F'
369};
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600370
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500371#define PNG_MAX_ERROR_TEXT 64
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500372#if defined(PNG_WARNINGS_SUPPORTED) || defined(PNG_ERROR_TEXT_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500373static void /* PRIVATE */
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500374png_format_buffer(png_structp png_ptr, png_charp buffer, png_const_charp
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600375 error_message)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600376{
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500377 png_uint_32 chunk_name = png_ptr->chunk_name;
378 int iout = 0, ishift = 24;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600379
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500380 while (ishift >= 0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600381 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500382 int c = (int)(chunk_name >> ishift) & 0xff;
383
384 ishift -= 8;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600385 if (isnonalpha(c))
386 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500387 buffer[iout++] = PNG_LITERAL_LEFT_SQUARE_BRACKET;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600388 buffer[iout++] = png_digit[(c & 0xf0) >> 4];
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600389 buffer[iout++] = png_digit[c & 0x0f];
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500390 buffer[iout++] = PNG_LITERAL_RIGHT_SQUARE_BRACKET;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600391 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500392
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600393 else
394 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500395 buffer[iout++] = (char)c;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600396 }
397 }
398
Glenn Randers-Pehrson837a3d12002-05-10 20:19:58 -0500399 if (error_message == NULL)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500400 buffer[iout] = '\0';
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500401
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600402 else
403 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500404 int iin = 0;
405
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600406 buffer[iout++] = ':';
407 buffer[iout++] = ' ';
Glenn Randers-Pehrson07e1d342011-06-07 14:35:30 -0500408
Glenn Randers-Pehrson07e1d342011-06-07 14:35:30 -0500409 while (iin < PNG_MAX_ERROR_TEXT-1 && error_message[iin] != '\0')
410 buffer[iout++] = error_message[iin++];
411
412 /* iin < PNG_MAX_ERROR_TEXT, so the following is safe: */
413 buffer[iout] = '\0';
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600414 }
415}
Glenn Randers-Pehrson8bdfb472010-04-17 18:05:01 -0500416#endif /* PNG_WARNINGS_SUPPORTED || PNG_ERROR_TEXT_SUPPORTED */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600417
Glenn Randers-Pehrson8bdfb472010-04-17 18:05:01 -0500418#if defined(PNG_READ_SUPPORTED) && defined(PNG_ERROR_TEXT_SUPPORTED)
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500419PNG_FUNCTION(void,PNGAPI
420png_chunk_error,(png_structp png_ptr, png_const_charp error_message),
421 PNG_NORETURN)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600422{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500423 char msg[18+PNG_MAX_ERROR_TEXT];
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600424 if (png_ptr == NULL)
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600425 png_error(png_ptr, error_message);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500426
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500427 else
428 {
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600429 png_format_buffer(png_ptr, msg, error_message);
430 png_error(png_ptr, msg);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500431 }
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600432}
Glenn Randers-Pehrson8bdfb472010-04-17 18:05:01 -0500433#endif /* PNG_READ_SUPPORTED && PNG_ERROR_TEXT_SUPPORTED */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600434
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500435#ifdef PNG_WARNINGS_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500436void PNGAPI
Glenn Randers-Pehrson837a3d12002-05-10 20:19:58 -0500437png_chunk_warning(png_structp png_ptr, png_const_charp warning_message)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600438{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500439 char msg[18+PNG_MAX_ERROR_TEXT];
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600440 if (png_ptr == NULL)
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600441 png_warning(png_ptr, warning_message);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500442
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500443 else
444 {
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600445 png_format_buffer(png_ptr, msg, warning_message);
446 png_warning(png_ptr, msg);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500447 }
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600448}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500449#endif /* PNG_WARNINGS_SUPPORTED */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600450
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500451#ifdef PNG_READ_SUPPORTED
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500452#ifdef PNG_BENIGN_ERRORS_SUPPORTED
453void PNGAPI
454png_chunk_benign_error(png_structp png_ptr, png_const_charp error_message)
455{
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600456 if (png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN)
457 png_chunk_warning(png_ptr, error_message);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500458
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600459 else
460 png_chunk_error(png_ptr, error_message);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500461}
462#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500463#endif /* PNG_READ_SUPPORTED */
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500464
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500465#ifdef PNG_ERROR_TEXT_SUPPORTED
466#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500467PNG_FUNCTION(void,
468png_fixed_error,(png_structp png_ptr, png_const_charp name),PNG_NORETURN)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500469{
470# define fixed_message "fixed point overflow in "
471# define fixed_message_ln ((sizeof fixed_message)-1)
472 int iin;
473 char msg[fixed_message_ln+PNG_MAX_ERROR_TEXT];
474 png_memcpy(msg, fixed_message, fixed_message_ln);
475 iin = 0;
476 if (name != NULL) while (iin < (PNG_MAX_ERROR_TEXT-1) && name[iin] != 0)
477 {
478 msg[fixed_message_ln + iin] = name[iin];
479 ++iin;
480 }
481 msg[fixed_message_ln + iin] = 0;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500482 png_error(png_ptr, msg);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500483}
484#endif
485#endif
486
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -0600487#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrsone69b55d2010-01-01 10:29:06 -0600488/* This API only exists if ANSI-C style error handling is used,
489 * otherwise it is necessary for png_default_error to be overridden.
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -0600490 */
491jmp_buf* PNGAPI
Glenn Randers-Pehrsone69b55d2010-01-01 10:29:06 -0600492png_set_longjmp_fn(png_structp png_ptr, png_longjmp_ptr longjmp_fn,
493 size_t jmp_buf_size)
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -0600494{
John Bowlerd332c672011-12-21 17:36:12 -0600495 /* From libpng 1.6.0 the app gets one chance to set a 'jmpbuf_size' value
496 * and it must not change after that. Libpng doesn't care how big the
497 * buffer is, just that it doesn't change.
498 *
499 * If the buffer size is no *larger* than the size of jmp_buf when libpng is
500 * compiled a built in jmp_buf is returned; this preserves the pre-1.6.0
501 * semantics that this call will not fail. If the size is larger, however,
502 * the buffer is allocated and this may fail, causing the function to return
503 * NULL.
504 */
505 if (png_ptr == NULL)
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -0600506 return NULL;
507
John Bowlerd332c672011-12-21 17:36:12 -0600508 if (png_ptr->jmp_buf_ptr == NULL)
509 {
510 png_ptr->jmp_buf_size = 0; /* not allocated */
511
512 if (jmp_buf_size <= sizeof png_ptr->jmp_buf_local)
513 png_ptr->jmp_buf_ptr = &png_ptr->jmp_buf_local;
514
515 else
516 {
517 png_ptr->jmp_buf_ptr = png_voidcast(jmp_buf *,
518 png_malloc_warn(png_ptr, jmp_buf_size));
519
520 if (png_ptr->jmp_buf_ptr == NULL)
521 return NULL; /* new NULL return on OOM */
522
523 png_ptr->jmp_buf_size = jmp_buf_size;
524 }
525 }
526
527 else /* Already allocated: check the size */
528 {
529 size_t size = png_ptr->jmp_buf_size;
530
531 if (size == 0)
532 {
533 size = sizeof png_ptr->jmp_buf_local;
534 if (png_ptr->jmp_buf_ptr != &png_ptr->jmp_buf_local)
535 {
536 /* This is an internal error in libpng: somehow we have been left
537 * with a stack allocated jmp_buf when the application regained
538 * control. It's always possible to fix this up, but for the moment
539 * this is a png_error because that makes it easy to detect.
540 */
541 png_error(png_ptr, "Libpng jmp_buf still allocated");
542 /* png_ptr->jmp_buf_ptr = &png_ptr->jmp_buf_local; */
543 }
544 }
545
546 if (size != jmp_buf_size)
547 {
548 png_warning(png_ptr, "Application jmp_buf size changed");
549 return NULL; /* caller will probably crash: no choice here */
550 }
551 }
552
553 /* Finally fill in the function, now we have a satisfactory buffer. It is
554 * valid to change the function on every call.
555 */
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -0600556 png_ptr->longjmp_fn = longjmp_fn;
John Bowlerd332c672011-12-21 17:36:12 -0600557 return png_ptr->jmp_buf_ptr;
558}
559
560void /* PRIVATE */
561png_free_jmpbuf(png_structp png_ptr)
562{
563 if (png_ptr != NULL)
564 {
565 jmp_buf *jb = png_ptr->jmp_buf_ptr;
566
567 /* A size of 0 is used to indicate a local, stack, allocation of the
568 * pointer; used here and in png.c
569 */
570 if (jb != NULL && png_ptr->jmp_buf_size > 0)
571 {
572
573 /* This stuff is so that a failure to free the error control structure
574 * does not leave libpng in a state with no valid error handling: the
575 * free always succeeds, if there is an error it gets ignored.
576 */
577 if (jb != &png_ptr->jmp_buf_local)
578 {
579 /* Make an internal, libpng, jmp_buf to return here */
580 jmp_buf free_jmp_buf;
581
582 if (!setjmp(free_jmp_buf))
583 {
584 png_ptr->jmp_buf_ptr = &free_jmp_buf; /* come back here */
585 png_ptr->jmp_buf_size = 0; /* stack allocation */
586 png_ptr->longjmp_fn = longjmp;
587 png_free(png_ptr, jb); /* Return to setjmp on error */
588 }
589 }
590 }
591
592 /* *Always* cancel everything out: */
593 png_ptr->jmp_buf_size = 0;
594 png_ptr->jmp_buf_ptr = NULL;
595 png_ptr->longjmp_fn = 0;
596 }
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -0600597}
598#endif
599
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600600/* This is the default error handling function. Note that replacements for
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600601 * this function MUST NOT RETURN, or the program will likely crash. This
602 * function is used by default, or if the program supplies NULL for the
603 * error function pointer in png_set_error_fn().
604 */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500605static PNG_FUNCTION(void /* PRIVATE */,
606png_default_error,(png_structp png_ptr, png_const_charp error_message),
607 PNG_NORETURN)
Guy Schalnat6d764711995-12-19 03:22:19 -0600608{
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500609#ifdef PNG_CONSOLE_IO_SUPPORTED
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500610#ifdef PNG_ERROR_NUMBERS_SUPPORTED
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -0500611 /* Check on NULL only added in 1.5.4 */
John Bowler88b77cc2011-05-05 06:49:55 -0500612 if (error_message != NULL && *error_message == PNG_LITERAL_SHARP)
Glenn Randers-Pehrson8fb550c2009-03-21 08:15:32 -0500613 {
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600614 /* Strip "#nnnn " from beginning of error message. */
615 int offset;
616 char error_number[16];
617 for (offset = 0; offset<15; offset++)
618 {
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500619 error_number[offset] = error_message[offset + 1];
620 if (error_message[offset] == ' ')
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600621 break;
622 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500623
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600624 if ((offset > 1) && (offset < 15))
625 {
626 error_number[offset - 1] = '\0';
627 fprintf(stderr, "libpng error no. %s: %s",
628 error_number, error_message + offset + 1);
629 fprintf(stderr, PNG_STRING_NEWLINE);
630 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500631
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600632 else
633 {
634 fprintf(stderr, "libpng error: %s, offset=%d",
635 error_message, offset);
636 fprintf(stderr, PNG_STRING_NEWLINE);
637 }
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500638 }
639 else
640#endif
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500641 {
John Bowler88b77cc2011-05-05 06:49:55 -0500642 fprintf(stderr, "libpng error: %s", error_message ? error_message :
643 "undefined");
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500644 fprintf(stderr, PNG_STRING_NEWLINE);
645 }
John Bowler88b77cc2011-05-05 06:49:55 -0500646#else
Glenn Randers-Pehrsond546f432010-12-04 20:41:36 -0600647 PNG_UNUSED(error_message) /* Make compiler happy */
Glenn Randers-Pehrsonf98726a2010-02-19 09:54:53 -0600648#endif
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500649 png_longjmp(png_ptr, 1);
Glenn Randers-Pehrsonf98726a2010-02-19 09:54:53 -0600650}
Guy Schalnat6d764711995-12-19 03:22:19 -0600651
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500652PNG_FUNCTION(void,PNGAPI
653png_longjmp,(png_structp png_ptr, int val),PNG_NORETURN)
Glenn Randers-Pehrsonf98726a2010-02-19 09:54:53 -0600654{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600655#ifdef PNG_SETJMP_SUPPORTED
John Bowlerd332c672011-12-21 17:36:12 -0600656 if (png_ptr && png_ptr->longjmp_fn && png_ptr->jmp_buf_ptr)
657 png_ptr->longjmp_fn(*png_ptr->jmp_buf_ptr, val);
Guy Schalnat6d764711995-12-19 03:22:19 -0600658#endif
John Bowlerbaeb6d12011-11-26 18:21:02 -0600659
Glenn Randers-Pehrsond29033f2009-11-07 10:46:42 -0600660 /* Here if not setjmp support or if png_ptr is null. */
661 PNG_ABORT();
Guy Schalnat6d764711995-12-19 03:22:19 -0600662}
663
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500664#ifdef PNG_WARNINGS_SUPPORTED
Guy Schalnat69b14481996-01-10 02:56:49 -0600665/* This function is called when there is a warning, but the library thinks
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600666 * it can continue anyway. Replacement functions don't have to do anything
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500667 * here if you don't want them to. In the default configuration, png_ptr is
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600668 * not used, but it is passed in case it may be useful.
669 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500670static void /* PRIVATE */
Glenn Randers-Pehrson837a3d12002-05-10 20:19:58 -0500671png_default_warning(png_structp png_ptr, png_const_charp warning_message)
Guy Schalnat6d764711995-12-19 03:22:19 -0600672{
Glenn Randers-Pehrson2f89d762009-10-13 18:04:41 -0500673#ifdef PNG_CONSOLE_IO_SUPPORTED
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500674# ifdef PNG_ERROR_NUMBERS_SUPPORTED
Glenn Randers-Pehrson43aaf6e2008-08-05 22:17:03 -0500675 if (*warning_message == PNG_LITERAL_SHARP)
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500676 {
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600677 int offset;
678 char warning_number[16];
679 for (offset = 0; offset < 15; offset++)
680 {
681 warning_number[offset] = warning_message[offset + 1];
682 if (warning_message[offset] == ' ')
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500683 break;
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600684 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500685
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600686 if ((offset > 1) && (offset < 15))
687 {
688 warning_number[offset + 1] = '\0';
689 fprintf(stderr, "libpng warning no. %s: %s",
690 warning_number, warning_message + offset);
691 fprintf(stderr, PNG_STRING_NEWLINE);
692 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500693
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600694 else
695 {
696 fprintf(stderr, "libpng warning: %s",
697 warning_message);
698 fprintf(stderr, PNG_STRING_NEWLINE);
699 }
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500700 }
701 else
702# endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500703
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500704 {
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600705 fprintf(stderr, "libpng warning: %s", warning_message);
706 fprintf(stderr, PNG_STRING_NEWLINE);
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500707 }
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500708#else
Glenn Randers-Pehrsond546f432010-12-04 20:41:36 -0600709 PNG_UNUSED(warning_message) /* Make compiler happy */
Guy Schalnat6d764711995-12-19 03:22:19 -0600710#endif
Glenn Randers-Pehrsond546f432010-12-04 20:41:36 -0600711 PNG_UNUSED(png_ptr) /* Make compiler happy */
Guy Schalnat6d764711995-12-19 03:22:19 -0600712}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500713#endif /* PNG_WARNINGS_SUPPORTED */
Guy Schalnat6d764711995-12-19 03:22:19 -0600714
Guy Schalnat69b14481996-01-10 02:56:49 -0600715/* This function is called when the application wants to use another method
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600716 * of handling errors and warnings. Note that the error function MUST NOT
717 * return to the calling routine or serious problems will occur. The return
John Bowlerd332c672011-12-21 17:36:12 -0600718 * method used in the default routine calls longjmp(png_ptr->jmp_buf_ptr, 1)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600719 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500720void PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -0500721png_set_error_fn(png_structp png_ptr, png_voidp error_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600722 png_error_ptr error_fn, png_error_ptr warning_fn)
Guy Schalnat6d764711995-12-19 03:22:19 -0600723{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600724 if (png_ptr == NULL)
725 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500726
Guy Schalnate5a37791996-06-05 15:50:50 -0500727 png_ptr->error_ptr = error_ptr;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600728 png_ptr->error_fn = error_fn;
John Bowler88b77cc2011-05-05 06:49:55 -0500729#ifdef PNG_WARNINGS_SUPPORTED
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600730 png_ptr->warning_fn = warning_fn;
John Bowler88b77cc2011-05-05 06:49:55 -0500731#else
732 PNG_UNUSED(warning_fn)
733#endif
Guy Schalnat6d764711995-12-19 03:22:19 -0600734}
735
736
Guy Schalnate5a37791996-06-05 15:50:50 -0500737/* This function returns a pointer to the error_ptr associated with the user
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600738 * functions. The application should free any memory associated with this
739 * pointer before png_write_destroy and png_read_destroy are called.
740 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500741png_voidp PNGAPI
John Bowler0a5c9c02011-01-22 17:36:34 -0600742png_get_error_ptr(png_const_structp png_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -0600743{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600744 if (png_ptr == NULL)
745 return NULL;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500746
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600747 return ((png_voidp)png_ptr->error_ptr);
Guy Schalnat6d764711995-12-19 03:22:19 -0600748}
749
750
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500751#ifdef PNG_ERROR_NUMBERS_SUPPORTED
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600752void PNGAPI
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500753png_set_strip_error_numbers(png_structp png_ptr, png_uint_32 strip_mode)
754{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500755 if (png_ptr != NULL)
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500756 {
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600757 png_ptr->flags &=
758 ((~(PNG_FLAG_STRIP_ERROR_NUMBERS |
759 PNG_FLAG_STRIP_ERROR_TEXT))&strip_mode);
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500760 }
761}
762#endif
John Bowler7875d532011-11-07 22:33:49 -0600763
764#if defined PNG_SIMPLIFIED_READ_SUPPORTED ||\
765 defined PNG_SIMPLIFIED_WRITE_SUPPORTED
766 /* Currently the above both depend on SETJMP_SUPPORTED, however it would be
767 * possible to implement without setjmp support just so long as there is some
768 * way to handle the error return here:
769 */
770PNG_FUNCTION(void /* PRIVATE */,
771png_safe_error,(png_structp png_ptr, png_const_charp error_message),
772 PNG_NORETURN)
773{
John Bowler4fa96a42011-11-16 16:39:16 -0600774 png_imagep image = png_voidcast(png_imagep, png_ptr->error_ptr);
John Bowler7875d532011-11-07 22:33:49 -0600775
776 /* An error is always logged here, overwriting anything (typically a warning)
777 * that is already there:
778 */
779 if (image != NULL)
780 {
781 png_safecat(image->message, sizeof image->message, 0, error_message);
782 image->warning_or_error = 1;
783
John Bowler4fa96a42011-11-16 16:39:16 -0600784 /* Retrieve the jmp_buf from within the png_control, making this work for
785 * C++ compilation too is pretty tricky: C++ wants a pointer to the first
786 * element of a jmp_buf, but C doesn't tell us the type of that.
787 */
John Bowler7875d532011-11-07 22:33:49 -0600788 if (image->opaque != NULL && image->opaque->error_buf != NULL)
John Bowler4fa96a42011-11-16 16:39:16 -0600789 longjmp(png_control_jmp_buf(image->opaque), 1);
John Bowler7875d532011-11-07 22:33:49 -0600790
791 /* Missing longjmp buffer, the following is to help debugging: */
792 {
793 size_t pos = png_safecat(image->message, sizeof image->message, 0,
794 "bad longjmp: ");
795 png_safecat(image->message, sizeof image->message, pos, error_message);
796 }
797 }
798
799 /* Here on an internal programming error. */
800 abort();
801}
802
803#ifdef PNG_WARNINGS_SUPPORTED
804void /* PRIVATE */
805png_safe_warning(png_structp png_ptr, png_const_charp warning_message)
806{
John Bowler4fa96a42011-11-16 16:39:16 -0600807 png_imagep image = png_voidcast(png_imagep, png_ptr->error_ptr);
John Bowler7875d532011-11-07 22:33:49 -0600808
Glenn Randers-Pehrson84b0da92011-11-10 06:32:19 -0600809 /* A warning is only logged if there is no prior warning or error. */
John Bowler7875d532011-11-07 22:33:49 -0600810 if (image->warning_or_error == 0)
811 {
812 png_safecat(image->message, sizeof image->message, 0, warning_message);
813 image->warning_or_error = 2;
814 }
815}
816#endif
817
818int /* PRIVATE */
819png_safe_execute(png_imagep imageIn, int (*function)(png_voidp), png_voidp arg)
820{
821 volatile png_imagep image = imageIn;
822 volatile int result;
823 volatile png_voidp saved_error_buf;
824 jmp_buf safe_jmpbuf;
825
826 /* Safely execute function(arg) with png_error returning to this function. */
827 saved_error_buf = image->opaque->error_buf;
828 result = setjmp(safe_jmpbuf) == 0;
829
830 if (result)
831 {
832
833 image->opaque->error_buf = safe_jmpbuf;
834 result = function(arg);
835 }
836
837 image->opaque->error_buf = saved_error_buf;
838
839 /* And do the cleanup prior to any failure return. */
840 if (!result)
841 png_image_free(image);
842
843 return result;
844}
845#endif /* SIMPLIFIED READ/WRITE */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600846#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */