blob: 622b4130707fdf1ac9c9a5651ebb1f004514fe60 [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-Pehrson1531bd62012-01-01 14:45:04 -06005 * Copyright (c) 1998-2012 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)
Glenn Randers-Pehrson0522f262011-12-29 10:02:24 -060099 (*(png_ptr->error_fn))(png_constcast(png_structrp,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 */
John Bowler00c6a9a2012-01-14 19:44:43 -0600289 size_t i = 0; /* Index in the msg[] buffer: */
John Bowler88b77cc2011-05-05 06:49:55 -0500290 char msg[128];
291
John Bowler00c6a9a2012-01-14 19:44:43 -0600292 /* Each iteration through the following loop writes at most one character
293 * to msg[i++] then returns here to validate that there is still space for
294 * the trailing '\0'. It may (in the case of a parameter) read more than
295 * one character from message[]; it must check for '\0' and continue to the
296 * test if it finds the end of string.
297 */
298 while (i<(sizeof msg)-1 && *message != '\0')
John Bowler88b77cc2011-05-05 06:49:55 -0500299 {
John Bowler00c6a9a2012-01-14 19:44:43 -0600300 /* '@' at end of string is now just printed (previously it was skipped);
301 * it is an error in the calling code to terminate the string with @.
302 */
303 if (p != NULL && *message == '@' && message[1] != '\0')
John Bowler88b77cc2011-05-05 06:49:55 -0500304 {
John Bowler00c6a9a2012-01-14 19:44:43 -0600305 int parameter_char = *++message; /* Consume the '@' */
306 static const char valid_parameters[] = "123456789";
307 int parameter = 0;
John Bowler88b77cc2011-05-05 06:49:55 -0500308
John Bowler00c6a9a2012-01-14 19:44:43 -0600309 /* Search for the parameter digit, the index in the string is the
310 * parameter to use.
311 */
312 while (valid_parameters[parameter] != parameter_char &&
313 valid_parameters[parameter] != '\0')
314 ++parameter;
John Bowler88b77cc2011-05-05 06:49:55 -0500315
John Bowler00c6a9a2012-01-14 19:44:43 -0600316 /* If the parameter digit is out of range it will just get printed. */
317 if (parameter < PNG_WARNING_PARAMETER_COUNT)
John Bowler88b77cc2011-05-05 06:49:55 -0500318 {
319 /* Append this parameter */
320 png_const_charp parm = p[parameter];
321 png_const_charp pend = p[parameter] + (sizeof p[parameter]);
322
323 /* No need to copy the trailing '\0' here, but there is no guarantee
324 * that parm[] has been initialized, so there is no guarantee of a
325 * trailing '\0':
326 */
John Bowler00c6a9a2012-01-14 19:44:43 -0600327 while (i<(sizeof msg)-1 && *parm != '\0' && parm < pend)
328 msg[i++] = *parm++;
John Bowler88b77cc2011-05-05 06:49:55 -0500329
John Bowler00c6a9a2012-01-14 19:44:43 -0600330 /* Consume the parameter digit too: */
John Bowler88b77cc2011-05-05 06:49:55 -0500331 ++message;
332 continue;
333 }
334
335 /* else not a parameter and there is a character after the @ sign; just
John Bowler00c6a9a2012-01-14 19:44:43 -0600336 * copy that. This is known not to be '\0' because of the test above.
John Bowler88b77cc2011-05-05 06:49:55 -0500337 */
338 }
339
340 /* At this point *message can't be '\0', even in the bad parameter case
341 * above where there is a lone '@' at the end of the message string.
342 */
John Bowler00c6a9a2012-01-14 19:44:43 -0600343 msg[i++] = *message++;
John Bowler88b77cc2011-05-05 06:49:55 -0500344 }
345
346 /* i is always less than (sizeof msg), so: */
347 msg[i] = '\0';
348
John Bowler00c6a9a2012-01-14 19:44:43 -0600349 /* And this is the formatted message, it may be larger than
350 * PNG_MAX_ERROR_TEXT, but that is only used for 'chunk' errors and these are
351 * not (currently) formatted.
352 */
John Bowler88b77cc2011-05-05 06:49:55 -0500353 png_warning(png_ptr, msg);
354}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500355#endif /* PNG_WARNINGS_SUPPORTED */
Guy Schalnat6d764711995-12-19 03:22:19 -0600356
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500357#ifdef PNG_BENIGN_ERRORS_SUPPORTED
358void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600359png_benign_error(png_const_structrp png_ptr, png_const_charp error_message)
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500360{
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500361 if (png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN)
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600362 png_warning(png_ptr, error_message);
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500363 else
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600364 png_error(png_ptr, error_message);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500365}
366#endif
367
Glenn Randers-Pehrsoncfbed9b2002-05-21 18:06:08 -0500368/* These utilities are used internally to build an error message that relates
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600369 * to the current chunk. The chunk name comes from png_ptr->chunk_name,
370 * this is used to prefix the message. The message is limited in length
371 * to 63 bytes, the name characters are output as hex digits wrapped in []
372 * if the character is invalid.
373 */
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500374#define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
Glenn Randers-Pehrson7cd899c1998-03-07 16:17:42 -0600375static PNG_CONST char png_digit[16] = {
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500376 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
377 'A', 'B', 'C', 'D', 'E', 'F'
378};
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600379
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500380#define PNG_MAX_ERROR_TEXT 64
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500381#if defined(PNG_WARNINGS_SUPPORTED) || defined(PNG_ERROR_TEXT_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500382static void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -0600383png_format_buffer(png_const_structrp png_ptr, png_charp buffer, png_const_charp
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600384 error_message)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600385{
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500386 png_uint_32 chunk_name = png_ptr->chunk_name;
387 int iout = 0, ishift = 24;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600388
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500389 while (ishift >= 0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600390 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500391 int c = (int)(chunk_name >> ishift) & 0xff;
392
393 ishift -= 8;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600394 if (isnonalpha(c))
395 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500396 buffer[iout++] = PNG_LITERAL_LEFT_SQUARE_BRACKET;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600397 buffer[iout++] = png_digit[(c & 0xf0) >> 4];
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600398 buffer[iout++] = png_digit[c & 0x0f];
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500399 buffer[iout++] = PNG_LITERAL_RIGHT_SQUARE_BRACKET;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600400 }
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 buffer[iout++] = (char)c;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600405 }
406 }
407
Glenn Randers-Pehrson837a3d12002-05-10 20:19:58 -0500408 if (error_message == NULL)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500409 buffer[iout] = '\0';
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500410
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600411 else
412 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500413 int iin = 0;
414
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600415 buffer[iout++] = ':';
416 buffer[iout++] = ' ';
Glenn Randers-Pehrson07e1d342011-06-07 14:35:30 -0500417
Glenn Randers-Pehrson07e1d342011-06-07 14:35:30 -0500418 while (iin < PNG_MAX_ERROR_TEXT-1 && error_message[iin] != '\0')
419 buffer[iout++] = error_message[iin++];
420
421 /* iin < PNG_MAX_ERROR_TEXT, so the following is safe: */
422 buffer[iout] = '\0';
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600423 }
424}
Glenn Randers-Pehrson8bdfb472010-04-17 18:05:01 -0500425#endif /* PNG_WARNINGS_SUPPORTED || PNG_ERROR_TEXT_SUPPORTED */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600426
Glenn Randers-Pehrson8bdfb472010-04-17 18:05:01 -0500427#if defined(PNG_READ_SUPPORTED) && defined(PNG_ERROR_TEXT_SUPPORTED)
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500428PNG_FUNCTION(void,PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600429png_chunk_error,(png_const_structrp png_ptr, png_const_charp error_message),
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500430 PNG_NORETURN)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600431{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500432 char msg[18+PNG_MAX_ERROR_TEXT];
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600433 if (png_ptr == NULL)
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600434 png_error(png_ptr, error_message);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500435
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500436 else
437 {
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600438 png_format_buffer(png_ptr, msg, error_message);
439 png_error(png_ptr, msg);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500440 }
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600441}
Glenn Randers-Pehrson8bdfb472010-04-17 18:05:01 -0500442#endif /* PNG_READ_SUPPORTED && PNG_ERROR_TEXT_SUPPORTED */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600443
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500444#ifdef PNG_WARNINGS_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500445void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600446png_chunk_warning(png_const_structrp png_ptr, png_const_charp warning_message)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600447{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500448 char msg[18+PNG_MAX_ERROR_TEXT];
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600449 if (png_ptr == NULL)
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600450 png_warning(png_ptr, warning_message);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500451
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500452 else
453 {
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600454 png_format_buffer(png_ptr, msg, warning_message);
455 png_warning(png_ptr, msg);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500456 }
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600457}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500458#endif /* PNG_WARNINGS_SUPPORTED */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600459
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500460#ifdef PNG_READ_SUPPORTED
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500461#ifdef PNG_BENIGN_ERRORS_SUPPORTED
462void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600463png_chunk_benign_error(png_const_structrp png_ptr, png_const_charp error_message)
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500464{
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600465 if (png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN)
466 png_chunk_warning(png_ptr, error_message);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500467
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600468 else
469 png_chunk_error(png_ptr, error_message);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500470}
471#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500472#endif /* PNG_READ_SUPPORTED */
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500473
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500474#ifdef PNG_ERROR_TEXT_SUPPORTED
475#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500476PNG_FUNCTION(void,
John Bowler5d567862011-12-24 09:12:00 -0600477png_fixed_error,(png_const_structrp png_ptr, png_const_charp name),PNG_NORETURN)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500478{
479# define fixed_message "fixed point overflow in "
480# define fixed_message_ln ((sizeof fixed_message)-1)
481 int iin;
482 char msg[fixed_message_ln+PNG_MAX_ERROR_TEXT];
483 png_memcpy(msg, fixed_message, fixed_message_ln);
484 iin = 0;
485 if (name != NULL) while (iin < (PNG_MAX_ERROR_TEXT-1) && name[iin] != 0)
486 {
487 msg[fixed_message_ln + iin] = name[iin];
488 ++iin;
489 }
490 msg[fixed_message_ln + iin] = 0;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500491 png_error(png_ptr, msg);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500492}
493#endif
494#endif
495
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -0600496#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrsone69b55d2010-01-01 10:29:06 -0600497/* This API only exists if ANSI-C style error handling is used,
498 * otherwise it is necessary for png_default_error to be overridden.
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -0600499 */
500jmp_buf* PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600501png_set_longjmp_fn(png_structrp png_ptr, png_longjmp_ptr longjmp_fn,
Glenn Randers-Pehrsone69b55d2010-01-01 10:29:06 -0600502 size_t jmp_buf_size)
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -0600503{
John Bowlerd332c672011-12-21 17:36:12 -0600504 /* From libpng 1.6.0 the app gets one chance to set a 'jmpbuf_size' value
505 * and it must not change after that. Libpng doesn't care how big the
506 * buffer is, just that it doesn't change.
507 *
508 * If the buffer size is no *larger* than the size of jmp_buf when libpng is
509 * compiled a built in jmp_buf is returned; this preserves the pre-1.6.0
510 * semantics that this call will not fail. If the size is larger, however,
511 * the buffer is allocated and this may fail, causing the function to return
512 * NULL.
513 */
514 if (png_ptr == NULL)
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -0600515 return NULL;
516
John Bowlerd332c672011-12-21 17:36:12 -0600517 if (png_ptr->jmp_buf_ptr == NULL)
518 {
519 png_ptr->jmp_buf_size = 0; /* not allocated */
520
521 if (jmp_buf_size <= sizeof png_ptr->jmp_buf_local)
522 png_ptr->jmp_buf_ptr = &png_ptr->jmp_buf_local;
523
524 else
525 {
526 png_ptr->jmp_buf_ptr = png_voidcast(jmp_buf *,
527 png_malloc_warn(png_ptr, jmp_buf_size));
528
529 if (png_ptr->jmp_buf_ptr == NULL)
530 return NULL; /* new NULL return on OOM */
531
532 png_ptr->jmp_buf_size = jmp_buf_size;
533 }
534 }
535
536 else /* Already allocated: check the size */
537 {
538 size_t size = png_ptr->jmp_buf_size;
539
540 if (size == 0)
541 {
542 size = sizeof png_ptr->jmp_buf_local;
543 if (png_ptr->jmp_buf_ptr != &png_ptr->jmp_buf_local)
544 {
545 /* This is an internal error in libpng: somehow we have been left
546 * with a stack allocated jmp_buf when the application regained
547 * control. It's always possible to fix this up, but for the moment
548 * this is a png_error because that makes it easy to detect.
549 */
550 png_error(png_ptr, "Libpng jmp_buf still allocated");
551 /* png_ptr->jmp_buf_ptr = &png_ptr->jmp_buf_local; */
552 }
553 }
554
555 if (size != jmp_buf_size)
556 {
557 png_warning(png_ptr, "Application jmp_buf size changed");
558 return NULL; /* caller will probably crash: no choice here */
559 }
560 }
561
562 /* Finally fill in the function, now we have a satisfactory buffer. It is
563 * valid to change the function on every call.
564 */
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -0600565 png_ptr->longjmp_fn = longjmp_fn;
John Bowlerd332c672011-12-21 17:36:12 -0600566 return png_ptr->jmp_buf_ptr;
567}
568
569void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -0600570png_free_jmpbuf(png_structrp png_ptr)
John Bowlerd332c672011-12-21 17:36:12 -0600571{
572 if (png_ptr != NULL)
573 {
574 jmp_buf *jb = png_ptr->jmp_buf_ptr;
575
576 /* A size of 0 is used to indicate a local, stack, allocation of the
577 * pointer; used here and in png.c
578 */
579 if (jb != NULL && png_ptr->jmp_buf_size > 0)
580 {
581
582 /* This stuff is so that a failure to free the error control structure
583 * does not leave libpng in a state with no valid error handling: the
584 * free always succeeds, if there is an error it gets ignored.
585 */
586 if (jb != &png_ptr->jmp_buf_local)
587 {
588 /* Make an internal, libpng, jmp_buf to return here */
589 jmp_buf free_jmp_buf;
590
591 if (!setjmp(free_jmp_buf))
592 {
593 png_ptr->jmp_buf_ptr = &free_jmp_buf; /* come back here */
594 png_ptr->jmp_buf_size = 0; /* stack allocation */
595 png_ptr->longjmp_fn = longjmp;
596 png_free(png_ptr, jb); /* Return to setjmp on error */
597 }
598 }
599 }
600
601 /* *Always* cancel everything out: */
602 png_ptr->jmp_buf_size = 0;
603 png_ptr->jmp_buf_ptr = NULL;
604 png_ptr->longjmp_fn = 0;
605 }
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -0600606}
607#endif
608
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600609/* This is the default error handling function. Note that replacements for
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600610 * this function MUST NOT RETURN, or the program will likely crash. This
611 * function is used by default, or if the program supplies NULL for the
612 * error function pointer in png_set_error_fn().
613 */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500614static PNG_FUNCTION(void /* PRIVATE */,
John Bowler5d567862011-12-24 09:12:00 -0600615png_default_error,(png_const_structrp png_ptr, png_const_charp error_message),
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500616 PNG_NORETURN)
Guy Schalnat6d764711995-12-19 03:22:19 -0600617{
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500618#ifdef PNG_CONSOLE_IO_SUPPORTED
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500619#ifdef PNG_ERROR_NUMBERS_SUPPORTED
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -0500620 /* Check on NULL only added in 1.5.4 */
John Bowler88b77cc2011-05-05 06:49:55 -0500621 if (error_message != NULL && *error_message == PNG_LITERAL_SHARP)
Glenn Randers-Pehrson8fb550c2009-03-21 08:15:32 -0500622 {
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600623 /* Strip "#nnnn " from beginning of error message. */
624 int offset;
625 char error_number[16];
626 for (offset = 0; offset<15; offset++)
627 {
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500628 error_number[offset] = error_message[offset + 1];
629 if (error_message[offset] == ' ')
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600630 break;
631 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500632
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600633 if ((offset > 1) && (offset < 15))
634 {
635 error_number[offset - 1] = '\0';
636 fprintf(stderr, "libpng error no. %s: %s",
637 error_number, error_message + offset + 1);
638 fprintf(stderr, PNG_STRING_NEWLINE);
639 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500640
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600641 else
642 {
643 fprintf(stderr, "libpng error: %s, offset=%d",
644 error_message, offset);
645 fprintf(stderr, PNG_STRING_NEWLINE);
646 }
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500647 }
648 else
649#endif
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500650 {
John Bowler88b77cc2011-05-05 06:49:55 -0500651 fprintf(stderr, "libpng error: %s", error_message ? error_message :
652 "undefined");
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500653 fprintf(stderr, PNG_STRING_NEWLINE);
654 }
John Bowler88b77cc2011-05-05 06:49:55 -0500655#else
Glenn Randers-Pehrsond546f432010-12-04 20:41:36 -0600656 PNG_UNUSED(error_message) /* Make compiler happy */
Glenn Randers-Pehrsonf98726a2010-02-19 09:54:53 -0600657#endif
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500658 png_longjmp(png_ptr, 1);
Glenn Randers-Pehrsonf98726a2010-02-19 09:54:53 -0600659}
Guy Schalnat6d764711995-12-19 03:22:19 -0600660
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500661PNG_FUNCTION(void,PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600662png_longjmp,(png_const_structrp png_ptr, int val),PNG_NORETURN)
Glenn Randers-Pehrsonf98726a2010-02-19 09:54:53 -0600663{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600664#ifdef PNG_SETJMP_SUPPORTED
John Bowlerd332c672011-12-21 17:36:12 -0600665 if (png_ptr && png_ptr->longjmp_fn && png_ptr->jmp_buf_ptr)
666 png_ptr->longjmp_fn(*png_ptr->jmp_buf_ptr, val);
Guy Schalnat6d764711995-12-19 03:22:19 -0600667#endif
John Bowlerbaeb6d12011-11-26 18:21:02 -0600668
Glenn Randers-Pehrsond29033f2009-11-07 10:46:42 -0600669 /* Here if not setjmp support or if png_ptr is null. */
670 PNG_ABORT();
Guy Schalnat6d764711995-12-19 03:22:19 -0600671}
672
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500673#ifdef PNG_WARNINGS_SUPPORTED
Guy Schalnat69b14481996-01-10 02:56:49 -0600674/* This function is called when there is a warning, but the library thinks
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600675 * it can continue anyway. Replacement functions don't have to do anything
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500676 * here if you don't want them to. In the default configuration, png_ptr is
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600677 * not used, but it is passed in case it may be useful.
678 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500679static void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -0600680png_default_warning(png_const_structrp png_ptr, png_const_charp warning_message)
Guy Schalnat6d764711995-12-19 03:22:19 -0600681{
Glenn Randers-Pehrson2f89d762009-10-13 18:04:41 -0500682#ifdef PNG_CONSOLE_IO_SUPPORTED
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500683# ifdef PNG_ERROR_NUMBERS_SUPPORTED
Glenn Randers-Pehrson43aaf6e2008-08-05 22:17:03 -0500684 if (*warning_message == PNG_LITERAL_SHARP)
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500685 {
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600686 int offset;
687 char warning_number[16];
688 for (offset = 0; offset < 15; offset++)
689 {
690 warning_number[offset] = warning_message[offset + 1];
691 if (warning_message[offset] == ' ')
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500692 break;
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600693 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500694
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600695 if ((offset > 1) && (offset < 15))
696 {
697 warning_number[offset + 1] = '\0';
698 fprintf(stderr, "libpng warning no. %s: %s",
699 warning_number, warning_message + offset);
700 fprintf(stderr, PNG_STRING_NEWLINE);
701 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500702
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600703 else
704 {
705 fprintf(stderr, "libpng warning: %s",
706 warning_message);
707 fprintf(stderr, PNG_STRING_NEWLINE);
708 }
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500709 }
710 else
711# endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500712
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500713 {
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600714 fprintf(stderr, "libpng warning: %s", warning_message);
715 fprintf(stderr, PNG_STRING_NEWLINE);
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500716 }
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500717#else
Glenn Randers-Pehrsond546f432010-12-04 20:41:36 -0600718 PNG_UNUSED(warning_message) /* Make compiler happy */
Guy Schalnat6d764711995-12-19 03:22:19 -0600719#endif
Glenn Randers-Pehrsond546f432010-12-04 20:41:36 -0600720 PNG_UNUSED(png_ptr) /* Make compiler happy */
Guy Schalnat6d764711995-12-19 03:22:19 -0600721}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500722#endif /* PNG_WARNINGS_SUPPORTED */
Guy Schalnat6d764711995-12-19 03:22:19 -0600723
Guy Schalnat69b14481996-01-10 02:56:49 -0600724/* This function is called when the application wants to use another method
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600725 * of handling errors and warnings. Note that the error function MUST NOT
726 * return to the calling routine or serious problems will occur. The return
John Bowlerd332c672011-12-21 17:36:12 -0600727 * method used in the default routine calls longjmp(png_ptr->jmp_buf_ptr, 1)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600728 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500729void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600730png_set_error_fn(png_structrp png_ptr, png_voidp error_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600731 png_error_ptr error_fn, png_error_ptr warning_fn)
Guy Schalnat6d764711995-12-19 03:22:19 -0600732{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600733 if (png_ptr == NULL)
734 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500735
Guy Schalnate5a37791996-06-05 15:50:50 -0500736 png_ptr->error_ptr = error_ptr;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600737 png_ptr->error_fn = error_fn;
John Bowler88b77cc2011-05-05 06:49:55 -0500738#ifdef PNG_WARNINGS_SUPPORTED
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600739 png_ptr->warning_fn = warning_fn;
John Bowler88b77cc2011-05-05 06:49:55 -0500740#else
741 PNG_UNUSED(warning_fn)
742#endif
Guy Schalnat6d764711995-12-19 03:22:19 -0600743}
744
745
Guy Schalnate5a37791996-06-05 15:50:50 -0500746/* This function returns a pointer to the error_ptr associated with the user
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600747 * functions. The application should free any memory associated with this
748 * pointer before png_write_destroy and png_read_destroy are called.
749 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500750png_voidp PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600751png_get_error_ptr(png_const_structrp png_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -0600752{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600753 if (png_ptr == NULL)
754 return NULL;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500755
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600756 return ((png_voidp)png_ptr->error_ptr);
Guy Schalnat6d764711995-12-19 03:22:19 -0600757}
758
759
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500760#ifdef PNG_ERROR_NUMBERS_SUPPORTED
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600761void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600762png_set_strip_error_numbers(png_structrp png_ptr, png_uint_32 strip_mode)
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500763{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500764 if (png_ptr != NULL)
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500765 {
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600766 png_ptr->flags &=
767 ((~(PNG_FLAG_STRIP_ERROR_NUMBERS |
768 PNG_FLAG_STRIP_ERROR_TEXT))&strip_mode);
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500769 }
770}
771#endif
John Bowler7875d532011-11-07 22:33:49 -0600772
773#if defined PNG_SIMPLIFIED_READ_SUPPORTED ||\
774 defined PNG_SIMPLIFIED_WRITE_SUPPORTED
775 /* Currently the above both depend on SETJMP_SUPPORTED, however it would be
776 * possible to implement without setjmp support just so long as there is some
777 * way to handle the error return here:
778 */
779PNG_FUNCTION(void /* PRIVATE */,
John Bowler40b26032011-12-22 08:09:15 -0600780png_safe_error,(png_structp png_nonconst_ptr, png_const_charp error_message),
John Bowler7875d532011-11-07 22:33:49 -0600781 PNG_NORETURN)
782{
John Bowler5d567862011-12-24 09:12:00 -0600783 const png_const_structrp png_ptr = png_nonconst_ptr;
John Bowler4fa96a42011-11-16 16:39:16 -0600784 png_imagep image = png_voidcast(png_imagep, png_ptr->error_ptr);
John Bowler7875d532011-11-07 22:33:49 -0600785
786 /* An error is always logged here, overwriting anything (typically a warning)
787 * that is already there:
788 */
789 if (image != NULL)
790 {
791 png_safecat(image->message, sizeof image->message, 0, error_message);
792 image->warning_or_error = 1;
793
John Bowler4fa96a42011-11-16 16:39:16 -0600794 /* Retrieve the jmp_buf from within the png_control, making this work for
795 * C++ compilation too is pretty tricky: C++ wants a pointer to the first
796 * element of a jmp_buf, but C doesn't tell us the type of that.
797 */
John Bowler7875d532011-11-07 22:33:49 -0600798 if (image->opaque != NULL && image->opaque->error_buf != NULL)
John Bowler4fa96a42011-11-16 16:39:16 -0600799 longjmp(png_control_jmp_buf(image->opaque), 1);
John Bowler7875d532011-11-07 22:33:49 -0600800
801 /* Missing longjmp buffer, the following is to help debugging: */
802 {
803 size_t pos = png_safecat(image->message, sizeof image->message, 0,
804 "bad longjmp: ");
805 png_safecat(image->message, sizeof image->message, pos, error_message);
806 }
807 }
808
809 /* Here on an internal programming error. */
810 abort();
811}
812
813#ifdef PNG_WARNINGS_SUPPORTED
814void /* PRIVATE */
John Bowler40b26032011-12-22 08:09:15 -0600815png_safe_warning(png_structp png_nonconst_ptr, png_const_charp warning_message)
John Bowler7875d532011-11-07 22:33:49 -0600816{
John Bowler5d567862011-12-24 09:12:00 -0600817 const png_const_structrp png_ptr = png_nonconst_ptr;
John Bowler4fa96a42011-11-16 16:39:16 -0600818 png_imagep image = png_voidcast(png_imagep, png_ptr->error_ptr);
John Bowler7875d532011-11-07 22:33:49 -0600819
Glenn Randers-Pehrson84b0da92011-11-10 06:32:19 -0600820 /* A warning is only logged if there is no prior warning or error. */
John Bowler7875d532011-11-07 22:33:49 -0600821 if (image->warning_or_error == 0)
822 {
823 png_safecat(image->message, sizeof image->message, 0, warning_message);
824 image->warning_or_error = 2;
825 }
826}
827#endif
828
829int /* PRIVATE */
830png_safe_execute(png_imagep imageIn, int (*function)(png_voidp), png_voidp arg)
831{
832 volatile png_imagep image = imageIn;
833 volatile int result;
834 volatile png_voidp saved_error_buf;
835 jmp_buf safe_jmpbuf;
836
837 /* Safely execute function(arg) with png_error returning to this function. */
838 saved_error_buf = image->opaque->error_buf;
839 result = setjmp(safe_jmpbuf) == 0;
840
841 if (result)
842 {
843
844 image->opaque->error_buf = safe_jmpbuf;
845 result = function(arg);
846 }
847
848 image->opaque->error_buf = saved_error_buf;
849
850 /* And do the cleanup prior to any failure return. */
851 if (!result)
852 png_image_free(image);
853
854 return result;
855}
856#endif /* SIMPLIFIED READ/WRITE */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600857#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */