blob: c9a1781ad0f1beed21f356bb4665ee5be8fa1b58 [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 *
John Bowler550bab02011-06-14 06:17:26 -05004 * Last changed in libpng 1.5.4 [(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{
377 int iout = 0, iin = 0;
378
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600379 while (iin < 4)
380 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600381 int c = png_ptr->chunk_name[iin++];
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600382 if (isnonalpha(c))
383 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500384 buffer[iout++] = PNG_LITERAL_LEFT_SQUARE_BRACKET;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600385 buffer[iout++] = png_digit[(c & 0xf0) >> 4];
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600386 buffer[iout++] = png_digit[c & 0x0f];
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500387 buffer[iout++] = PNG_LITERAL_RIGHT_SQUARE_BRACKET;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600388 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500389
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600390 else
391 {
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500392 buffer[iout++] = (png_byte)c;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600393 }
394 }
395
Glenn Randers-Pehrson837a3d12002-05-10 20:19:58 -0500396 if (error_message == NULL)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500397 buffer[iout] = '\0';
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500398
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600399 else
400 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600401 buffer[iout++] = ':';
402 buffer[iout++] = ' ';
Glenn Randers-Pehrson07e1d342011-06-07 14:35:30 -0500403
404 iin = 0;
405 while (iin < PNG_MAX_ERROR_TEXT-1 && error_message[iin] != '\0')
406 buffer[iout++] = error_message[iin++];
407
408 /* iin < PNG_MAX_ERROR_TEXT, so the following is safe: */
409 buffer[iout] = '\0';
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600410 }
411}
Glenn Randers-Pehrson8bdfb472010-04-17 18:05:01 -0500412#endif /* PNG_WARNINGS_SUPPORTED || PNG_ERROR_TEXT_SUPPORTED */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600413
Glenn Randers-Pehrson8bdfb472010-04-17 18:05:01 -0500414#if defined(PNG_READ_SUPPORTED) && defined(PNG_ERROR_TEXT_SUPPORTED)
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500415PNG_FUNCTION(void,PNGAPI
416png_chunk_error,(png_structp png_ptr, png_const_charp error_message),
417 PNG_NORETURN)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600418{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500419 char msg[18+PNG_MAX_ERROR_TEXT];
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600420 if (png_ptr == NULL)
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600421 png_error(png_ptr, error_message);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500422
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500423 else
424 {
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600425 png_format_buffer(png_ptr, msg, error_message);
426 png_error(png_ptr, msg);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500427 }
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600428}
Glenn Randers-Pehrson8bdfb472010-04-17 18:05:01 -0500429#endif /* PNG_READ_SUPPORTED && PNG_ERROR_TEXT_SUPPORTED */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600430
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500431#ifdef PNG_WARNINGS_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500432void PNGAPI
Glenn Randers-Pehrson837a3d12002-05-10 20:19:58 -0500433png_chunk_warning(png_structp png_ptr, png_const_charp warning_message)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600434{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500435 char msg[18+PNG_MAX_ERROR_TEXT];
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600436 if (png_ptr == NULL)
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600437 png_warning(png_ptr, warning_message);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500438
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500439 else
440 {
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600441 png_format_buffer(png_ptr, msg, warning_message);
442 png_warning(png_ptr, msg);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500443 }
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600444}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500445#endif /* PNG_WARNINGS_SUPPORTED */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600446
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500447#ifdef PNG_READ_SUPPORTED
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500448#ifdef PNG_BENIGN_ERRORS_SUPPORTED
449void PNGAPI
450png_chunk_benign_error(png_structp png_ptr, png_const_charp error_message)
451{
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600452 if (png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN)
453 png_chunk_warning(png_ptr, error_message);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500454
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600455 else
456 png_chunk_error(png_ptr, error_message);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500457}
458#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500459#endif /* PNG_READ_SUPPORTED */
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500460
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500461#ifdef PNG_ERROR_TEXT_SUPPORTED
462#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500463PNG_FUNCTION(void,
464png_fixed_error,(png_structp png_ptr, png_const_charp name),PNG_NORETURN)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500465{
466# define fixed_message "fixed point overflow in "
467# define fixed_message_ln ((sizeof fixed_message)-1)
468 int iin;
469 char msg[fixed_message_ln+PNG_MAX_ERROR_TEXT];
470 png_memcpy(msg, fixed_message, fixed_message_ln);
471 iin = 0;
472 if (name != NULL) while (iin < (PNG_MAX_ERROR_TEXT-1) && name[iin] != 0)
473 {
474 msg[fixed_message_ln + iin] = name[iin];
475 ++iin;
476 }
477 msg[fixed_message_ln + iin] = 0;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500478 png_error(png_ptr, msg);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500479}
480#endif
481#endif
482
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -0600483#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrsone69b55d2010-01-01 10:29:06 -0600484/* This API only exists if ANSI-C style error handling is used,
485 * otherwise it is necessary for png_default_error to be overridden.
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -0600486 */
487jmp_buf* PNGAPI
Glenn Randers-Pehrsone69b55d2010-01-01 10:29:06 -0600488png_set_longjmp_fn(png_structp png_ptr, png_longjmp_ptr longjmp_fn,
489 size_t jmp_buf_size)
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -0600490{
491 if (png_ptr == NULL || jmp_buf_size != png_sizeof(jmp_buf))
492 return NULL;
493
494 png_ptr->longjmp_fn = longjmp_fn;
John Bowlere6dc85b2011-04-27 14:47:15 -0500495 return &png_ptr->longjmp_buffer;
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -0600496}
497#endif
498
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600499/* This is the default error handling function. Note that replacements for
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600500 * this function MUST NOT RETURN, or the program will likely crash. This
501 * function is used by default, or if the program supplies NULL for the
502 * error function pointer in png_set_error_fn().
503 */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500504static PNG_FUNCTION(void /* PRIVATE */,
505png_default_error,(png_structp png_ptr, png_const_charp error_message),
506 PNG_NORETURN)
Guy Schalnat6d764711995-12-19 03:22:19 -0600507{
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500508#ifdef PNG_CONSOLE_IO_SUPPORTED
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500509#ifdef PNG_ERROR_NUMBERS_SUPPORTED
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -0500510 /* Check on NULL only added in 1.5.4 */
John Bowler88b77cc2011-05-05 06:49:55 -0500511 if (error_message != NULL && *error_message == PNG_LITERAL_SHARP)
Glenn Randers-Pehrson8fb550c2009-03-21 08:15:32 -0500512 {
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600513 /* Strip "#nnnn " from beginning of error message. */
514 int offset;
515 char error_number[16];
516 for (offset = 0; offset<15; offset++)
517 {
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500518 error_number[offset] = error_message[offset + 1];
519 if (error_message[offset] == ' ')
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600520 break;
521 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500522
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600523 if ((offset > 1) && (offset < 15))
524 {
525 error_number[offset - 1] = '\0';
526 fprintf(stderr, "libpng error no. %s: %s",
527 error_number, error_message + offset + 1);
528 fprintf(stderr, PNG_STRING_NEWLINE);
529 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500530
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600531 else
532 {
533 fprintf(stderr, "libpng error: %s, offset=%d",
534 error_message, offset);
535 fprintf(stderr, PNG_STRING_NEWLINE);
536 }
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500537 }
538 else
539#endif
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500540 {
John Bowler88b77cc2011-05-05 06:49:55 -0500541 fprintf(stderr, "libpng error: %s", error_message ? error_message :
542 "undefined");
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500543 fprintf(stderr, PNG_STRING_NEWLINE);
544 }
John Bowler88b77cc2011-05-05 06:49:55 -0500545#else
Glenn Randers-Pehrsond546f432010-12-04 20:41:36 -0600546 PNG_UNUSED(error_message) /* Make compiler happy */
Glenn Randers-Pehrsonf98726a2010-02-19 09:54:53 -0600547#endif
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500548 png_longjmp(png_ptr, 1);
Glenn Randers-Pehrsonf98726a2010-02-19 09:54:53 -0600549}
Guy Schalnat6d764711995-12-19 03:22:19 -0600550
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500551PNG_FUNCTION(void,PNGAPI
552png_longjmp,(png_structp png_ptr, int val),PNG_NORETURN)
Glenn Randers-Pehrsonf98726a2010-02-19 09:54:53 -0600553{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600554#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -0600555 if (png_ptr && png_ptr->longjmp_fn)
Glenn Randers-Pehrson6b12c082006-11-14 10:53:30 -0600556 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600557# ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500558 {
John Bowlere6dc85b2011-04-27 14:47:15 -0500559 jmp_buf tmp_jmpbuf;
560 png_memcpy(tmp_jmpbuf, png_ptr->longjmp_buffer, png_sizeof(jmp_buf));
561 png_ptr->longjmp_fn(tmp_jmpbuf, val);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500562 }
563
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600564# else
John Bowlere6dc85b2011-04-27 14:47:15 -0500565 png_ptr->longjmp_fn(png_ptr->longjmp_buffer, val);
Glenn Randers-Pehrson6b12c082006-11-14 10:53:30 -0600566# endif
567 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600568#endif
Glenn Randers-Pehrsond29033f2009-11-07 10:46:42 -0600569 /* Here if not setjmp support or if png_ptr is null. */
570 PNG_ABORT();
Guy Schalnat6d764711995-12-19 03:22:19 -0600571}
572
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500573#ifdef PNG_WARNINGS_SUPPORTED
Guy Schalnat69b14481996-01-10 02:56:49 -0600574/* This function is called when there is a warning, but the library thinks
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600575 * it can continue anyway. Replacement functions don't have to do anything
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500576 * here if you don't want them to. In the default configuration, png_ptr is
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600577 * not used, but it is passed in case it may be useful.
578 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500579static void /* PRIVATE */
Glenn Randers-Pehrson837a3d12002-05-10 20:19:58 -0500580png_default_warning(png_structp png_ptr, png_const_charp warning_message)
Guy Schalnat6d764711995-12-19 03:22:19 -0600581{
Glenn Randers-Pehrson2f89d762009-10-13 18:04:41 -0500582#ifdef PNG_CONSOLE_IO_SUPPORTED
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500583# ifdef PNG_ERROR_NUMBERS_SUPPORTED
Glenn Randers-Pehrson43aaf6e2008-08-05 22:17:03 -0500584 if (*warning_message == PNG_LITERAL_SHARP)
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500585 {
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600586 int offset;
587 char warning_number[16];
588 for (offset = 0; offset < 15; offset++)
589 {
590 warning_number[offset] = warning_message[offset + 1];
591 if (warning_message[offset] == ' ')
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500592 break;
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600593 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500594
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600595 if ((offset > 1) && (offset < 15))
596 {
597 warning_number[offset + 1] = '\0';
598 fprintf(stderr, "libpng warning no. %s: %s",
599 warning_number, warning_message + offset);
600 fprintf(stderr, PNG_STRING_NEWLINE);
601 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500602
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600603 else
604 {
605 fprintf(stderr, "libpng warning: %s",
606 warning_message);
607 fprintf(stderr, PNG_STRING_NEWLINE);
608 }
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500609 }
610 else
611# endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500612
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500613 {
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600614 fprintf(stderr, "libpng warning: %s", warning_message);
615 fprintf(stderr, PNG_STRING_NEWLINE);
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500616 }
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500617#else
Glenn Randers-Pehrsond546f432010-12-04 20:41:36 -0600618 PNG_UNUSED(warning_message) /* Make compiler happy */
Guy Schalnat6d764711995-12-19 03:22:19 -0600619#endif
Glenn Randers-Pehrsond546f432010-12-04 20:41:36 -0600620 PNG_UNUSED(png_ptr) /* Make compiler happy */
Guy Schalnat6d764711995-12-19 03:22:19 -0600621}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500622#endif /* PNG_WARNINGS_SUPPORTED */
Guy Schalnat6d764711995-12-19 03:22:19 -0600623
Guy Schalnat69b14481996-01-10 02:56:49 -0600624/* This function is called when the application wants to use another method
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600625 * of handling errors and warnings. Note that the error function MUST NOT
626 * return to the calling routine or serious problems will occur. The return
John Bowlere6dc85b2011-04-27 14:47:15 -0500627 * method used in the default routine calls longjmp(png_ptr->longjmp_buffer, 1)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600628 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500629void PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -0500630png_set_error_fn(png_structp png_ptr, png_voidp error_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600631 png_error_ptr error_fn, png_error_ptr warning_fn)
Guy Schalnat6d764711995-12-19 03:22:19 -0600632{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600633 if (png_ptr == NULL)
634 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500635
Guy Schalnate5a37791996-06-05 15:50:50 -0500636 png_ptr->error_ptr = error_ptr;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600637 png_ptr->error_fn = error_fn;
John Bowler88b77cc2011-05-05 06:49:55 -0500638#ifdef PNG_WARNINGS_SUPPORTED
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600639 png_ptr->warning_fn = warning_fn;
John Bowler88b77cc2011-05-05 06:49:55 -0500640#else
641 PNG_UNUSED(warning_fn)
642#endif
Guy Schalnat6d764711995-12-19 03:22:19 -0600643}
644
645
Guy Schalnate5a37791996-06-05 15:50:50 -0500646/* This function returns a pointer to the error_ptr associated with the user
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600647 * functions. The application should free any memory associated with this
648 * pointer before png_write_destroy and png_read_destroy are called.
649 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500650png_voidp PNGAPI
John Bowler0a5c9c02011-01-22 17:36:34 -0600651png_get_error_ptr(png_const_structp png_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -0600652{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600653 if (png_ptr == NULL)
654 return NULL;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500655
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600656 return ((png_voidp)png_ptr->error_ptr);
Guy Schalnat6d764711995-12-19 03:22:19 -0600657}
658
659
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500660#ifdef PNG_ERROR_NUMBERS_SUPPORTED
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600661void PNGAPI
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500662png_set_strip_error_numbers(png_structp png_ptr, png_uint_32 strip_mode)
663{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500664 if (png_ptr != NULL)
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500665 {
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600666 png_ptr->flags &=
667 ((~(PNG_FLAG_STRIP_ERROR_NUMBERS |
668 PNG_FLAG_STRIP_ERROR_TEXT))&strip_mode);
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500669 }
670}
671#endif
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600672#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */