xref: /reactos/dll/3rdparty/libpng/pngerror.c (revision b61b1815)
1c2c66affSColin Finck 
2c2c66affSColin Finck /* pngerror.c - stub functions for i/o and memory allocation
3c2c66affSColin Finck  *
4*b61b1815SThomas Faber  * Copyright (c) 2018 Cosmin Truta
5c2c66affSColin Finck  * Copyright (c) 1998-2002,2004,2006-2017 Glenn Randers-Pehrson
6*b61b1815SThomas Faber  * Copyright (c) 1996-1997 Andreas Dilger
7*b61b1815SThomas Faber  * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.
8c2c66affSColin Finck  *
9c2c66affSColin Finck  * This code is released under the libpng license.
10c2c66affSColin Finck  * For conditions of distribution and use, see the disclaimer
11c2c66affSColin Finck  * and license in png.h
12c2c66affSColin Finck  *
13c2c66affSColin Finck  * This file provides a location for all error handling.  Users who
14c2c66affSColin Finck  * need special error handling are expected to write replacement functions
15c2c66affSColin Finck  * and use png_set_error_fn() to use those functions.  See the instructions
16c2c66affSColin Finck  * at each function.
17c2c66affSColin Finck  */
18c2c66affSColin Finck 
19c2c66affSColin Finck #include "pngpriv.h"
20c2c66affSColin Finck 
21c2c66affSColin Finck #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
22c2c66affSColin Finck 
23c2c66affSColin Finck static PNG_FUNCTION(void, png_default_error,PNGARG((png_const_structrp png_ptr,
24c2c66affSColin Finck     png_const_charp error_message)),PNG_NORETURN);
25c2c66affSColin Finck 
26c2c66affSColin Finck #ifdef PNG_WARNINGS_SUPPORTED
27c2c66affSColin Finck static void /* PRIVATE */
28c2c66affSColin Finck png_default_warning PNGARG((png_const_structrp png_ptr,
29c2c66affSColin Finck     png_const_charp warning_message));
30c2c66affSColin Finck #endif /* WARNINGS */
31c2c66affSColin Finck 
32c2c66affSColin Finck /* This function is called whenever there is a fatal error.  This function
33c2c66affSColin Finck  * should not be changed.  If there is a need to handle errors differently,
34c2c66affSColin Finck  * you should supply a replacement error function and use png_set_error_fn()
35c2c66affSColin Finck  * to replace the error function at run-time.
36c2c66affSColin Finck  */
37c2c66affSColin Finck #ifdef PNG_ERROR_TEXT_SUPPORTED
38c2c66affSColin Finck PNG_FUNCTION(void,PNGAPI
39c2c66affSColin Finck png_error,(png_const_structrp png_ptr, png_const_charp error_message),
40c2c66affSColin Finck     PNG_NORETURN)
41c2c66affSColin Finck {
42c2c66affSColin Finck #ifdef PNG_ERROR_NUMBERS_SUPPORTED
43c2c66affSColin Finck    char msg[16];
44c2c66affSColin Finck    if (png_ptr != NULL)
45c2c66affSColin Finck    {
46c2c66affSColin Finck       if ((png_ptr->flags &
47c2c66affSColin Finck          (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT)) != 0)
48c2c66affSColin Finck       {
49c2c66affSColin Finck          if (*error_message == PNG_LITERAL_SHARP)
50c2c66affSColin Finck          {
51c2c66affSColin Finck             /* Strip "#nnnn " from beginning of error message. */
52c2c66affSColin Finck             int offset;
53c2c66affSColin Finck             for (offset = 1; offset<15; offset++)
54c2c66affSColin Finck                if (error_message[offset] == ' ')
55c2c66affSColin Finck                   break;
56c2c66affSColin Finck 
57c2c66affSColin Finck             if ((png_ptr->flags & PNG_FLAG_STRIP_ERROR_TEXT) != 0)
58c2c66affSColin Finck             {
59c2c66affSColin Finck                int i;
60c2c66affSColin Finck                for (i = 0; i < offset - 1; i++)
61c2c66affSColin Finck                   msg[i] = error_message[i + 1];
62c2c66affSColin Finck                msg[i - 1] = '\0';
63c2c66affSColin Finck                error_message = msg;
64c2c66affSColin Finck             }
65c2c66affSColin Finck 
66c2c66affSColin Finck             else
67c2c66affSColin Finck                error_message += offset;
68c2c66affSColin Finck          }
69c2c66affSColin Finck 
70c2c66affSColin Finck          else
71c2c66affSColin Finck          {
72c2c66affSColin Finck             if ((png_ptr->flags & PNG_FLAG_STRIP_ERROR_TEXT) != 0)
73c2c66affSColin Finck             {
74c2c66affSColin Finck                msg[0] = '0';
75c2c66affSColin Finck                msg[1] = '\0';
76c2c66affSColin Finck                error_message = msg;
77c2c66affSColin Finck             }
78c2c66affSColin Finck          }
79c2c66affSColin Finck       }
80c2c66affSColin Finck    }
81c2c66affSColin Finck #endif
82c2c66affSColin Finck    if (png_ptr != NULL && png_ptr->error_fn != NULL)
83c2c66affSColin Finck       (*(png_ptr->error_fn))(png_constcast(png_structrp,png_ptr),
84c2c66affSColin Finck           error_message);
85c2c66affSColin Finck 
86c2c66affSColin Finck    /* If the custom handler doesn't exist, or if it returns,
87c2c66affSColin Finck       use the default handler, which will not return. */
88c2c66affSColin Finck    png_default_error(png_ptr, error_message);
89c2c66affSColin Finck }
90c2c66affSColin Finck #else
91c2c66affSColin Finck PNG_FUNCTION(void,PNGAPI
92c2c66affSColin Finck png_err,(png_const_structrp png_ptr),PNG_NORETURN)
93c2c66affSColin Finck {
94c2c66affSColin Finck    /* Prior to 1.5.2 the error_fn received a NULL pointer, expressed
95c2c66affSColin Finck     * erroneously as '\0', instead of the empty string "".  This was
96c2c66affSColin Finck     * apparently an error, introduced in libpng-1.2.20, and png_default_error
97c2c66affSColin Finck     * will crash in this case.
98c2c66affSColin Finck     */
99c2c66affSColin Finck    if (png_ptr != NULL && png_ptr->error_fn != NULL)
100c2c66affSColin Finck       (*(png_ptr->error_fn))(png_constcast(png_structrp,png_ptr), "");
101c2c66affSColin Finck 
102c2c66affSColin Finck    /* If the custom handler doesn't exist, or if it returns,
103c2c66affSColin Finck       use the default handler, which will not return. */
104c2c66affSColin Finck    png_default_error(png_ptr, "");
105c2c66affSColin Finck }
106c2c66affSColin Finck #endif /* ERROR_TEXT */
107c2c66affSColin Finck 
108c2c66affSColin Finck /* Utility to safely appends strings to a buffer.  This never errors out so
109c2c66affSColin Finck  * error checking is not required in the caller.
110c2c66affSColin Finck  */
111c2c66affSColin Finck size_t
png_safecat(png_charp buffer,size_t bufsize,size_t pos,png_const_charp string)112c2c66affSColin Finck png_safecat(png_charp buffer, size_t bufsize, size_t pos,
113c2c66affSColin Finck     png_const_charp string)
114c2c66affSColin Finck {
115c2c66affSColin Finck    if (buffer != NULL && pos < bufsize)
116c2c66affSColin Finck    {
117c2c66affSColin Finck       if (string != NULL)
118c2c66affSColin Finck          while (*string != '\0' && pos < bufsize-1)
119c2c66affSColin Finck            buffer[pos++] = *string++;
120c2c66affSColin Finck 
121c2c66affSColin Finck       buffer[pos] = '\0';
122c2c66affSColin Finck    }
123c2c66affSColin Finck 
124c2c66affSColin Finck    return pos;
125c2c66affSColin Finck }
126c2c66affSColin Finck 
127c2c66affSColin Finck #if defined(PNG_WARNINGS_SUPPORTED) || defined(PNG_TIME_RFC1123_SUPPORTED)
128c2c66affSColin Finck /* Utility to dump an unsigned value into a buffer, given a start pointer and
129c2c66affSColin Finck  * and end pointer (which should point just *beyond* the end of the buffer!)
130c2c66affSColin Finck  * Returns the pointer to the start of the formatted string.
131c2c66affSColin Finck  */
132c2c66affSColin Finck png_charp
png_format_number(png_const_charp start,png_charp end,int format,png_alloc_size_t number)133c2c66affSColin Finck png_format_number(png_const_charp start, png_charp end, int format,
134c2c66affSColin Finck     png_alloc_size_t number)
135c2c66affSColin Finck {
136c2c66affSColin Finck    int count = 0;    /* number of digits output */
137c2c66affSColin Finck    int mincount = 1; /* minimum number required */
138c2c66affSColin Finck    int output = 0;   /* digit output (for the fixed point format) */
139c2c66affSColin Finck 
140c2c66affSColin Finck    *--end = '\0';
141c2c66affSColin Finck 
142c2c66affSColin Finck    /* This is written so that the loop always runs at least once, even with
143c2c66affSColin Finck     * number zero.
144c2c66affSColin Finck     */
145c2c66affSColin Finck    while (end > start && (number != 0 || count < mincount))
146c2c66affSColin Finck    {
147c2c66affSColin Finck 
148c2c66affSColin Finck       static const char digits[] = "0123456789ABCDEF";
149c2c66affSColin Finck 
150c2c66affSColin Finck       switch (format)
151c2c66affSColin Finck       {
152c2c66affSColin Finck          case PNG_NUMBER_FORMAT_fixed:
153c2c66affSColin Finck             /* Needs five digits (the fraction) */
154c2c66affSColin Finck             mincount = 5;
155c2c66affSColin Finck             if (output != 0 || number % 10 != 0)
156c2c66affSColin Finck             {
157c2c66affSColin Finck                *--end = digits[number % 10];
158c2c66affSColin Finck                output = 1;
159c2c66affSColin Finck             }
160c2c66affSColin Finck             number /= 10;
161c2c66affSColin Finck             break;
162c2c66affSColin Finck 
163c2c66affSColin Finck          case PNG_NUMBER_FORMAT_02u:
164c2c66affSColin Finck             /* Expects at least 2 digits. */
165c2c66affSColin Finck             mincount = 2;
166c2c66affSColin Finck             /* FALLTHROUGH */
167c2c66affSColin Finck 
168c2c66affSColin Finck          case PNG_NUMBER_FORMAT_u:
169c2c66affSColin Finck             *--end = digits[number % 10];
170c2c66affSColin Finck             number /= 10;
171c2c66affSColin Finck             break;
172c2c66affSColin Finck 
173c2c66affSColin Finck          case PNG_NUMBER_FORMAT_02x:
174c2c66affSColin Finck             /* This format expects at least two digits */
175c2c66affSColin Finck             mincount = 2;
176c2c66affSColin Finck             /* FALLTHROUGH */
177c2c66affSColin Finck 
178c2c66affSColin Finck          case PNG_NUMBER_FORMAT_x:
179c2c66affSColin Finck             *--end = digits[number & 0xf];
180c2c66affSColin Finck             number >>= 4;
181c2c66affSColin Finck             break;
182c2c66affSColin Finck 
183c2c66affSColin Finck          default: /* an error */
184c2c66affSColin Finck             number = 0;
185c2c66affSColin Finck             break;
186c2c66affSColin Finck       }
187c2c66affSColin Finck 
188c2c66affSColin Finck       /* Keep track of the number of digits added */
189c2c66affSColin Finck       ++count;
190c2c66affSColin Finck 
191c2c66affSColin Finck       /* Float a fixed number here: */
192c2c66affSColin Finck       if ((format == PNG_NUMBER_FORMAT_fixed) && (count == 5) && (end > start))
193c2c66affSColin Finck       {
194c2c66affSColin Finck          /* End of the fraction, but maybe nothing was output?  In that case
195c2c66affSColin Finck           * drop the decimal point.  If the number is a true zero handle that
196c2c66affSColin Finck           * here.
197c2c66affSColin Finck           */
198c2c66affSColin Finck          if (output != 0)
199c2c66affSColin Finck             *--end = '.';
200c2c66affSColin Finck          else if (number == 0) /* and !output */
201c2c66affSColin Finck             *--end = '0';
202c2c66affSColin Finck       }
203c2c66affSColin Finck    }
204c2c66affSColin Finck 
205c2c66affSColin Finck    return end;
206c2c66affSColin Finck }
207c2c66affSColin Finck #endif
208c2c66affSColin Finck 
209c2c66affSColin Finck #ifdef PNG_WARNINGS_SUPPORTED
210c2c66affSColin Finck /* This function is called whenever there is a non-fatal error.  This function
211c2c66affSColin Finck  * should not be changed.  If there is a need to handle warnings differently,
212c2c66affSColin Finck  * you should supply a replacement warning function and use
213c2c66affSColin Finck  * png_set_error_fn() to replace the warning function at run-time.
214c2c66affSColin Finck  */
215c2c66affSColin Finck void PNGAPI
png_warning(png_const_structrp png_ptr,png_const_charp warning_message)216c2c66affSColin Finck png_warning(png_const_structrp png_ptr, png_const_charp warning_message)
217c2c66affSColin Finck {
218c2c66affSColin Finck    int offset = 0;
219c2c66affSColin Finck    if (png_ptr != NULL)
220c2c66affSColin Finck    {
221c2c66affSColin Finck #ifdef PNG_ERROR_NUMBERS_SUPPORTED
222c2c66affSColin Finck    if ((png_ptr->flags &
223c2c66affSColin Finck        (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT)) != 0)
224c2c66affSColin Finck #endif
225c2c66affSColin Finck       {
226c2c66affSColin Finck          if (*warning_message == PNG_LITERAL_SHARP)
227c2c66affSColin Finck          {
228c2c66affSColin Finck             for (offset = 1; offset < 15; offset++)
229c2c66affSColin Finck                if (warning_message[offset] == ' ')
230c2c66affSColin Finck                   break;
231c2c66affSColin Finck          }
232c2c66affSColin Finck       }
233c2c66affSColin Finck    }
234c2c66affSColin Finck    if (png_ptr != NULL && png_ptr->warning_fn != NULL)
235c2c66affSColin Finck       (*(png_ptr->warning_fn))(png_constcast(png_structrp,png_ptr),
236c2c66affSColin Finck           warning_message + offset);
237c2c66affSColin Finck    else
238c2c66affSColin Finck       png_default_warning(png_ptr, warning_message + offset);
239c2c66affSColin Finck }
240c2c66affSColin Finck 
241c2c66affSColin Finck /* These functions support 'formatted' warning messages with up to
242c2c66affSColin Finck  * PNG_WARNING_PARAMETER_COUNT parameters.  In the format string the parameter
243c2c66affSColin Finck  * is introduced by @<number>, where 'number' starts at 1.  This follows the
244c2c66affSColin Finck  * standard established by X/Open for internationalizable error messages.
245c2c66affSColin Finck  */
246c2c66affSColin Finck void
png_warning_parameter(png_warning_parameters p,int number,png_const_charp string)247c2c66affSColin Finck png_warning_parameter(png_warning_parameters p, int number,
248c2c66affSColin Finck     png_const_charp string)
249c2c66affSColin Finck {
250c2c66affSColin Finck    if (number > 0 && number <= PNG_WARNING_PARAMETER_COUNT)
251c2c66affSColin Finck       (void)png_safecat(p[number-1], (sizeof p[number-1]), 0, string);
252c2c66affSColin Finck }
253c2c66affSColin Finck 
254c2c66affSColin Finck void
png_warning_parameter_unsigned(png_warning_parameters p,int number,int format,png_alloc_size_t value)255c2c66affSColin Finck png_warning_parameter_unsigned(png_warning_parameters p, int number, int format,
256c2c66affSColin Finck     png_alloc_size_t value)
257c2c66affSColin Finck {
258c2c66affSColin Finck    char buffer[PNG_NUMBER_BUFFER_SIZE];
259c2c66affSColin Finck    png_warning_parameter(p, number, PNG_FORMAT_NUMBER(buffer, format, value));
260c2c66affSColin Finck }
261c2c66affSColin Finck 
262c2c66affSColin Finck void
png_warning_parameter_signed(png_warning_parameters p,int number,int format,png_int_32 value)263c2c66affSColin Finck png_warning_parameter_signed(png_warning_parameters p, int number, int format,
264c2c66affSColin Finck     png_int_32 value)
265c2c66affSColin Finck {
266c2c66affSColin Finck    png_alloc_size_t u;
267c2c66affSColin Finck    png_charp str;
268c2c66affSColin Finck    char buffer[PNG_NUMBER_BUFFER_SIZE];
269c2c66affSColin Finck 
270c2c66affSColin Finck    /* Avoid overflow by doing the negate in a png_alloc_size_t: */
271c2c66affSColin Finck    u = (png_alloc_size_t)value;
272c2c66affSColin Finck    if (value < 0)
273c2c66affSColin Finck       u = ~u + 1;
274c2c66affSColin Finck 
275c2c66affSColin Finck    str = PNG_FORMAT_NUMBER(buffer, format, u);
276c2c66affSColin Finck 
277c2c66affSColin Finck    if (value < 0 && str > buffer)
278c2c66affSColin Finck       *--str = '-';
279c2c66affSColin Finck 
280c2c66affSColin Finck    png_warning_parameter(p, number, str);
281c2c66affSColin Finck }
282c2c66affSColin Finck 
283c2c66affSColin Finck void
png_formatted_warning(png_const_structrp png_ptr,png_warning_parameters p,png_const_charp message)284c2c66affSColin Finck png_formatted_warning(png_const_structrp png_ptr, png_warning_parameters p,
285c2c66affSColin Finck     png_const_charp message)
286c2c66affSColin Finck {
287c2c66affSColin Finck    /* The internal buffer is just 192 bytes - enough for all our messages,
288c2c66affSColin Finck     * overflow doesn't happen because this code checks!  If someone figures
289c2c66affSColin Finck     * out how to send us a message longer than 192 bytes, all that will
290c2c66affSColin Finck     * happen is that the message will be truncated appropriately.
291c2c66affSColin Finck     */
292c2c66affSColin Finck    size_t i = 0; /* Index in the msg[] buffer: */
293c2c66affSColin Finck    char msg[192];
294c2c66affSColin Finck 
295c2c66affSColin Finck    /* Each iteration through the following loop writes at most one character
296c2c66affSColin Finck     * to msg[i++] then returns here to validate that there is still space for
297c2c66affSColin Finck     * the trailing '\0'.  It may (in the case of a parameter) read more than
298c2c66affSColin Finck     * one character from message[]; it must check for '\0' and continue to the
299c2c66affSColin Finck     * test if it finds the end of string.
300c2c66affSColin Finck     */
301c2c66affSColin Finck    while (i<(sizeof msg)-1 && *message != '\0')
302c2c66affSColin Finck    {
303c2c66affSColin Finck       /* '@' at end of string is now just printed (previously it was skipped);
304c2c66affSColin Finck        * it is an error in the calling code to terminate the string with @.
305c2c66affSColin Finck        */
306c2c66affSColin Finck       if (p != NULL && *message == '@' && message[1] != '\0')
307c2c66affSColin Finck       {
308c2c66affSColin Finck          int parameter_char = *++message; /* Consume the '@' */
309c2c66affSColin Finck          static const char valid_parameters[] = "123456789";
310c2c66affSColin Finck          int parameter = 0;
311c2c66affSColin Finck 
312c2c66affSColin Finck          /* Search for the parameter digit, the index in the string is the
313c2c66affSColin Finck           * parameter to use.
314c2c66affSColin Finck           */
315c2c66affSColin Finck          while (valid_parameters[parameter] != parameter_char &&
316c2c66affSColin Finck             valid_parameters[parameter] != '\0')
317c2c66affSColin Finck             ++parameter;
318c2c66affSColin Finck 
319c2c66affSColin Finck          /* If the parameter digit is out of range it will just get printed. */
320c2c66affSColin Finck          if (parameter < PNG_WARNING_PARAMETER_COUNT)
321c2c66affSColin Finck          {
322c2c66affSColin Finck             /* Append this parameter */
323c2c66affSColin Finck             png_const_charp parm = p[parameter];
324c2c66affSColin Finck             png_const_charp pend = p[parameter] + (sizeof p[parameter]);
325c2c66affSColin Finck 
326c2c66affSColin Finck             /* No need to copy the trailing '\0' here, but there is no guarantee
327c2c66affSColin Finck              * that parm[] has been initialized, so there is no guarantee of a
328c2c66affSColin Finck              * trailing '\0':
329c2c66affSColin Finck              */
330c2c66affSColin Finck             while (i<(sizeof msg)-1 && *parm != '\0' && parm < pend)
331c2c66affSColin Finck                msg[i++] = *parm++;
332c2c66affSColin Finck 
333c2c66affSColin Finck             /* Consume the parameter digit too: */
334c2c66affSColin Finck             ++message;
335c2c66affSColin Finck             continue;
336c2c66affSColin Finck          }
337c2c66affSColin Finck 
338c2c66affSColin Finck          /* else not a parameter and there is a character after the @ sign; just
339c2c66affSColin Finck           * copy that.  This is known not to be '\0' because of the test above.
340c2c66affSColin Finck           */
341c2c66affSColin Finck       }
342c2c66affSColin Finck 
343c2c66affSColin Finck       /* At this point *message can't be '\0', even in the bad parameter case
344c2c66affSColin Finck        * above where there is a lone '@' at the end of the message string.
345c2c66affSColin Finck        */
346c2c66affSColin Finck       msg[i++] = *message++;
347c2c66affSColin Finck    }
348c2c66affSColin Finck 
349c2c66affSColin Finck    /* i is always less than (sizeof msg), so: */
350c2c66affSColin Finck    msg[i] = '\0';
351c2c66affSColin Finck 
352c2c66affSColin Finck    /* And this is the formatted message. It may be larger than
353c2c66affSColin Finck     * PNG_MAX_ERROR_TEXT, but that is only used for 'chunk' errors and these
354c2c66affSColin Finck     * are not (currently) formatted.
355c2c66affSColin Finck     */
356c2c66affSColin Finck    png_warning(png_ptr, msg);
357c2c66affSColin Finck }
358c2c66affSColin Finck #endif /* WARNINGS */
359c2c66affSColin Finck 
360c2c66affSColin Finck #ifdef PNG_BENIGN_ERRORS_SUPPORTED
361c2c66affSColin Finck void PNGAPI
png_benign_error(png_const_structrp png_ptr,png_const_charp error_message)362c2c66affSColin Finck png_benign_error(png_const_structrp png_ptr, png_const_charp error_message)
363c2c66affSColin Finck {
364c2c66affSColin Finck    if ((png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN) != 0)
365c2c66affSColin Finck    {
366c2c66affSColin Finck #     ifdef PNG_READ_SUPPORTED
367c2c66affSColin Finck          if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0 &&
368c2c66affSColin Finck             png_ptr->chunk_name != 0)
369c2c66affSColin Finck             png_chunk_warning(png_ptr, error_message);
370c2c66affSColin Finck          else
371c2c66affSColin Finck #     endif
372c2c66affSColin Finck       png_warning(png_ptr, error_message);
373c2c66affSColin Finck    }
374c2c66affSColin Finck 
375c2c66affSColin Finck    else
376c2c66affSColin Finck    {
377c2c66affSColin Finck #     ifdef PNG_READ_SUPPORTED
378c2c66affSColin Finck          if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0 &&
379c2c66affSColin Finck             png_ptr->chunk_name != 0)
380c2c66affSColin Finck             png_chunk_error(png_ptr, error_message);
381c2c66affSColin Finck          else
382c2c66affSColin Finck #     endif
383c2c66affSColin Finck       png_error(png_ptr, error_message);
384c2c66affSColin Finck    }
385c2c66affSColin Finck 
386c2c66affSColin Finck #  ifndef PNG_ERROR_TEXT_SUPPORTED
387c2c66affSColin Finck       PNG_UNUSED(error_message)
388c2c66affSColin Finck #  endif
389c2c66affSColin Finck }
390c2c66affSColin Finck 
391c2c66affSColin Finck void /* PRIVATE */
png_app_warning(png_const_structrp png_ptr,png_const_charp error_message)392c2c66affSColin Finck png_app_warning(png_const_structrp png_ptr, png_const_charp error_message)
393c2c66affSColin Finck {
394c2c66affSColin Finck    if ((png_ptr->flags & PNG_FLAG_APP_WARNINGS_WARN) != 0)
395c2c66affSColin Finck       png_warning(png_ptr, error_message);
396c2c66affSColin Finck    else
397c2c66affSColin Finck       png_error(png_ptr, error_message);
398c2c66affSColin Finck 
399c2c66affSColin Finck #  ifndef PNG_ERROR_TEXT_SUPPORTED
400c2c66affSColin Finck       PNG_UNUSED(error_message)
401c2c66affSColin Finck #  endif
402c2c66affSColin Finck }
403c2c66affSColin Finck 
404c2c66affSColin Finck void /* PRIVATE */
png_app_error(png_const_structrp png_ptr,png_const_charp error_message)405c2c66affSColin Finck png_app_error(png_const_structrp png_ptr, png_const_charp error_message)
406c2c66affSColin Finck {
407c2c66affSColin Finck    if ((png_ptr->flags & PNG_FLAG_APP_ERRORS_WARN) != 0)
408c2c66affSColin Finck       png_warning(png_ptr, error_message);
409c2c66affSColin Finck    else
410c2c66affSColin Finck       png_error(png_ptr, error_message);
411c2c66affSColin Finck 
412c2c66affSColin Finck #  ifndef PNG_ERROR_TEXT_SUPPORTED
413c2c66affSColin Finck       PNG_UNUSED(error_message)
414c2c66affSColin Finck #  endif
415c2c66affSColin Finck }
416c2c66affSColin Finck #endif /* BENIGN_ERRORS */
417c2c66affSColin Finck 
418c2c66affSColin Finck #define PNG_MAX_ERROR_TEXT 196 /* Currently limited by profile_error in png.c */
419c2c66affSColin Finck #if defined(PNG_WARNINGS_SUPPORTED) || \
420c2c66affSColin Finck    (defined(PNG_READ_SUPPORTED) && defined(PNG_ERROR_TEXT_SUPPORTED))
421c2c66affSColin Finck /* These utilities are used internally to build an error message that relates
422c2c66affSColin Finck  * to the current chunk.  The chunk name comes from png_ptr->chunk_name,
423c2c66affSColin Finck  * which is used to prefix the message.  The message is limited in length
424c2c66affSColin Finck  * to 63 bytes. The name characters are output as hex digits wrapped in []
425c2c66affSColin Finck  * if the character is invalid.
426c2c66affSColin Finck  */
427c2c66affSColin Finck #define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
428*b61b1815SThomas Faber static const char png_digit[16] = {
429c2c66affSColin Finck    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
430c2c66affSColin Finck    'A', 'B', 'C', 'D', 'E', 'F'
431c2c66affSColin Finck };
432c2c66affSColin Finck 
433c2c66affSColin Finck static void /* PRIVATE */
png_format_buffer(png_const_structrp png_ptr,png_charp buffer,png_const_charp error_message)434c2c66affSColin Finck png_format_buffer(png_const_structrp png_ptr, png_charp buffer, png_const_charp
435c2c66affSColin Finck     error_message)
436c2c66affSColin Finck {
437c2c66affSColin Finck    png_uint_32 chunk_name = png_ptr->chunk_name;
438c2c66affSColin Finck    int iout = 0, ishift = 24;
439c2c66affSColin Finck 
440c2c66affSColin Finck    while (ishift >= 0)
441c2c66affSColin Finck    {
442c2c66affSColin Finck       int c = (int)(chunk_name >> ishift) & 0xff;
443c2c66affSColin Finck 
444c2c66affSColin Finck       ishift -= 8;
445c2c66affSColin Finck       if (isnonalpha(c) != 0)
446c2c66affSColin Finck       {
447c2c66affSColin Finck          buffer[iout++] = PNG_LITERAL_LEFT_SQUARE_BRACKET;
448c2c66affSColin Finck          buffer[iout++] = png_digit[(c & 0xf0) >> 4];
449c2c66affSColin Finck          buffer[iout++] = png_digit[c & 0x0f];
450c2c66affSColin Finck          buffer[iout++] = PNG_LITERAL_RIGHT_SQUARE_BRACKET;
451c2c66affSColin Finck       }
452c2c66affSColin Finck 
453c2c66affSColin Finck       else
454c2c66affSColin Finck       {
455c2c66affSColin Finck          buffer[iout++] = (char)c;
456c2c66affSColin Finck       }
457c2c66affSColin Finck    }
458c2c66affSColin Finck 
459c2c66affSColin Finck    if (error_message == NULL)
460c2c66affSColin Finck       buffer[iout] = '\0';
461c2c66affSColin Finck 
462c2c66affSColin Finck    else
463c2c66affSColin Finck    {
464c2c66affSColin Finck       int iin = 0;
465c2c66affSColin Finck 
466c2c66affSColin Finck       buffer[iout++] = ':';
467c2c66affSColin Finck       buffer[iout++] = ' ';
468c2c66affSColin Finck 
469c2c66affSColin Finck       while (iin < PNG_MAX_ERROR_TEXT-1 && error_message[iin] != '\0')
470c2c66affSColin Finck          buffer[iout++] = error_message[iin++];
471c2c66affSColin Finck 
472c2c66affSColin Finck       /* iin < PNG_MAX_ERROR_TEXT, so the following is safe: */
473c2c66affSColin Finck       buffer[iout] = '\0';
474c2c66affSColin Finck    }
475c2c66affSColin Finck }
476c2c66affSColin Finck #endif /* WARNINGS || ERROR_TEXT */
477c2c66affSColin Finck 
478c2c66affSColin Finck #if defined(PNG_READ_SUPPORTED) && defined(PNG_ERROR_TEXT_SUPPORTED)
479c2c66affSColin Finck PNG_FUNCTION(void,PNGAPI
480c2c66affSColin Finck png_chunk_error,(png_const_structrp png_ptr, png_const_charp error_message),
481c2c66affSColin Finck     PNG_NORETURN)
482c2c66affSColin Finck {
483c2c66affSColin Finck    char msg[18+PNG_MAX_ERROR_TEXT];
484c2c66affSColin Finck    if (png_ptr == NULL)
485c2c66affSColin Finck       png_error(png_ptr, error_message);
486c2c66affSColin Finck 
487c2c66affSColin Finck    else
488c2c66affSColin Finck    {
489c2c66affSColin Finck       png_format_buffer(png_ptr, msg, error_message);
490c2c66affSColin Finck       png_error(png_ptr, msg);
491c2c66affSColin Finck    }
492c2c66affSColin Finck }
493c2c66affSColin Finck #endif /* READ && ERROR_TEXT */
494c2c66affSColin Finck 
495c2c66affSColin Finck #ifdef PNG_WARNINGS_SUPPORTED
496c2c66affSColin Finck void PNGAPI
png_chunk_warning(png_const_structrp png_ptr,png_const_charp warning_message)497c2c66affSColin Finck png_chunk_warning(png_const_structrp png_ptr, png_const_charp warning_message)
498c2c66affSColin Finck {
499c2c66affSColin Finck    char msg[18+PNG_MAX_ERROR_TEXT];
500c2c66affSColin Finck    if (png_ptr == NULL)
501c2c66affSColin Finck       png_warning(png_ptr, warning_message);
502c2c66affSColin Finck 
503c2c66affSColin Finck    else
504c2c66affSColin Finck    {
505c2c66affSColin Finck       png_format_buffer(png_ptr, msg, warning_message);
506c2c66affSColin Finck       png_warning(png_ptr, msg);
507c2c66affSColin Finck    }
508c2c66affSColin Finck }
509c2c66affSColin Finck #endif /* WARNINGS */
510c2c66affSColin Finck 
511c2c66affSColin Finck #ifdef PNG_READ_SUPPORTED
512c2c66affSColin Finck #ifdef PNG_BENIGN_ERRORS_SUPPORTED
513c2c66affSColin Finck void PNGAPI
png_chunk_benign_error(png_const_structrp png_ptr,png_const_charp error_message)514c2c66affSColin Finck png_chunk_benign_error(png_const_structrp png_ptr, png_const_charp
515c2c66affSColin Finck     error_message)
516c2c66affSColin Finck {
517c2c66affSColin Finck    if ((png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN) != 0)
518c2c66affSColin Finck       png_chunk_warning(png_ptr, error_message);
519c2c66affSColin Finck 
520c2c66affSColin Finck    else
521c2c66affSColin Finck       png_chunk_error(png_ptr, error_message);
522c2c66affSColin Finck 
523c2c66affSColin Finck #  ifndef PNG_ERROR_TEXT_SUPPORTED
524c2c66affSColin Finck       PNG_UNUSED(error_message)
525c2c66affSColin Finck #  endif
526c2c66affSColin Finck }
527c2c66affSColin Finck #endif
528c2c66affSColin Finck #endif /* READ */
529c2c66affSColin Finck 
530c2c66affSColin Finck void /* PRIVATE */
png_chunk_report(png_const_structrp png_ptr,png_const_charp message,int error)531c2c66affSColin Finck png_chunk_report(png_const_structrp png_ptr, png_const_charp message, int error)
532c2c66affSColin Finck {
533c2c66affSColin Finck #  ifndef PNG_WARNINGS_SUPPORTED
534c2c66affSColin Finck       PNG_UNUSED(message)
535c2c66affSColin Finck #  endif
536c2c66affSColin Finck 
537c2c66affSColin Finck    /* This is always supported, but for just read or just write it
538c2c66affSColin Finck     * unconditionally does the right thing.
539c2c66affSColin Finck     */
540c2c66affSColin Finck #  if defined(PNG_READ_SUPPORTED) && defined(PNG_WRITE_SUPPORTED)
541c2c66affSColin Finck       if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0)
542c2c66affSColin Finck #  endif
543c2c66affSColin Finck 
544c2c66affSColin Finck #  ifdef PNG_READ_SUPPORTED
545c2c66affSColin Finck       {
546c2c66affSColin Finck          if (error < PNG_CHUNK_ERROR)
547c2c66affSColin Finck             png_chunk_warning(png_ptr, message);
548c2c66affSColin Finck 
549c2c66affSColin Finck          else
550c2c66affSColin Finck             png_chunk_benign_error(png_ptr, message);
551c2c66affSColin Finck       }
552c2c66affSColin Finck #  endif
553c2c66affSColin Finck 
554c2c66affSColin Finck #  if defined(PNG_READ_SUPPORTED) && defined(PNG_WRITE_SUPPORTED)
555c2c66affSColin Finck       else if ((png_ptr->mode & PNG_IS_READ_STRUCT) == 0)
556c2c66affSColin Finck #  endif
557c2c66affSColin Finck 
558c2c66affSColin Finck #  ifdef PNG_WRITE_SUPPORTED
559c2c66affSColin Finck       {
560c2c66affSColin Finck          if (error < PNG_CHUNK_WRITE_ERROR)
561c2c66affSColin Finck             png_app_warning(png_ptr, message);
562c2c66affSColin Finck 
563c2c66affSColin Finck          else
564c2c66affSColin Finck             png_app_error(png_ptr, message);
565c2c66affSColin Finck       }
566c2c66affSColin Finck #  endif
567c2c66affSColin Finck }
568c2c66affSColin Finck 
569c2c66affSColin Finck #ifdef PNG_ERROR_TEXT_SUPPORTED
570c2c66affSColin Finck #ifdef PNG_FLOATING_POINT_SUPPORTED
571c2c66affSColin Finck PNG_FUNCTION(void,
572c2c66affSColin Finck png_fixed_error,(png_const_structrp png_ptr, png_const_charp name),PNG_NORETURN)
573c2c66affSColin Finck {
574c2c66affSColin Finck #  define fixed_message "fixed point overflow in "
575c2c66affSColin Finck #  define fixed_message_ln ((sizeof fixed_message)-1)
576c2c66affSColin Finck    unsigned int  iin;
577c2c66affSColin Finck    char msg[fixed_message_ln+PNG_MAX_ERROR_TEXT];
578c2c66affSColin Finck    memcpy(msg, fixed_message, fixed_message_ln);
579c2c66affSColin Finck    iin = 0;
580c2c66affSColin Finck    if (name != NULL)
581c2c66affSColin Finck       while (iin < (PNG_MAX_ERROR_TEXT-1) && name[iin] != 0)
582c2c66affSColin Finck       {
583c2c66affSColin Finck          msg[fixed_message_ln + iin] = name[iin];
584c2c66affSColin Finck          ++iin;
585c2c66affSColin Finck       }
586c2c66affSColin Finck    msg[fixed_message_ln + iin] = 0;
587c2c66affSColin Finck    png_error(png_ptr, msg);
588c2c66affSColin Finck }
589c2c66affSColin Finck #endif
590c2c66affSColin Finck #endif
591c2c66affSColin Finck 
592c2c66affSColin Finck #ifdef PNG_SETJMP_SUPPORTED
593c2c66affSColin Finck /* This API only exists if ANSI-C style error handling is used,
594c2c66affSColin Finck  * otherwise it is necessary for png_default_error to be overridden.
595c2c66affSColin Finck  */
596c2c66affSColin Finck jmp_buf* PNGAPI
png_set_longjmp_fn(png_structrp png_ptr,png_longjmp_ptr longjmp_fn,size_t jmp_buf_size)597c2c66affSColin Finck png_set_longjmp_fn(png_structrp png_ptr, png_longjmp_ptr longjmp_fn,
598c2c66affSColin Finck     size_t jmp_buf_size)
599c2c66affSColin Finck {
600c2c66affSColin Finck    /* From libpng 1.6.0 the app gets one chance to set a 'jmpbuf_size' value
601c2c66affSColin Finck     * and it must not change after that.  Libpng doesn't care how big the
602c2c66affSColin Finck     * buffer is, just that it doesn't change.
603c2c66affSColin Finck     *
604c2c66affSColin Finck     * If the buffer size is no *larger* than the size of jmp_buf when libpng is
605c2c66affSColin Finck     * compiled a built in jmp_buf is returned; this preserves the pre-1.6.0
606c2c66affSColin Finck     * semantics that this call will not fail.  If the size is larger, however,
607c2c66affSColin Finck     * the buffer is allocated and this may fail, causing the function to return
608c2c66affSColin Finck     * NULL.
609c2c66affSColin Finck     */
610c2c66affSColin Finck    if (png_ptr == NULL)
611c2c66affSColin Finck       return NULL;
612c2c66affSColin Finck 
613c2c66affSColin Finck    if (png_ptr->jmp_buf_ptr == NULL)
614c2c66affSColin Finck    {
615c2c66affSColin Finck       png_ptr->jmp_buf_size = 0; /* not allocated */
616c2c66affSColin Finck 
617c2c66affSColin Finck       if (jmp_buf_size <= (sizeof png_ptr->jmp_buf_local))
618c2c66affSColin Finck          png_ptr->jmp_buf_ptr = &png_ptr->jmp_buf_local;
619c2c66affSColin Finck 
620c2c66affSColin Finck       else
621c2c66affSColin Finck       {
622c2c66affSColin Finck          png_ptr->jmp_buf_ptr = png_voidcast(jmp_buf *,
623c2c66affSColin Finck              png_malloc_warn(png_ptr, jmp_buf_size));
624c2c66affSColin Finck 
625c2c66affSColin Finck          if (png_ptr->jmp_buf_ptr == NULL)
626c2c66affSColin Finck             return NULL; /* new NULL return on OOM */
627c2c66affSColin Finck 
628c2c66affSColin Finck          png_ptr->jmp_buf_size = jmp_buf_size;
629c2c66affSColin Finck       }
630c2c66affSColin Finck    }
631c2c66affSColin Finck 
632c2c66affSColin Finck    else /* Already allocated: check the size */
633c2c66affSColin Finck    {
634c2c66affSColin Finck       size_t size = png_ptr->jmp_buf_size;
635c2c66affSColin Finck 
636c2c66affSColin Finck       if (size == 0)
637c2c66affSColin Finck       {
638c2c66affSColin Finck          size = (sizeof png_ptr->jmp_buf_local);
639c2c66affSColin Finck          if (png_ptr->jmp_buf_ptr != &png_ptr->jmp_buf_local)
640c2c66affSColin Finck          {
641c2c66affSColin Finck             /* This is an internal error in libpng: somehow we have been left
642c2c66affSColin Finck              * with a stack allocated jmp_buf when the application regained
643c2c66affSColin Finck              * control.  It's always possible to fix this up, but for the moment
644c2c66affSColin Finck              * this is a png_error because that makes it easy to detect.
645c2c66affSColin Finck              */
646c2c66affSColin Finck             png_error(png_ptr, "Libpng jmp_buf still allocated");
647c2c66affSColin Finck             /* png_ptr->jmp_buf_ptr = &png_ptr->jmp_buf_local; */
648c2c66affSColin Finck          }
649c2c66affSColin Finck       }
650c2c66affSColin Finck 
651c2c66affSColin Finck       if (size != jmp_buf_size)
652c2c66affSColin Finck       {
653c2c66affSColin Finck          png_warning(png_ptr, "Application jmp_buf size changed");
654c2c66affSColin Finck          return NULL; /* caller will probably crash: no choice here */
655c2c66affSColin Finck       }
656c2c66affSColin Finck    }
657c2c66affSColin Finck 
658c2c66affSColin Finck    /* Finally fill in the function, now we have a satisfactory buffer. It is
659c2c66affSColin Finck     * valid to change the function on every call.
660c2c66affSColin Finck     */
661c2c66affSColin Finck    png_ptr->longjmp_fn = longjmp_fn;
662c2c66affSColin Finck    return png_ptr->jmp_buf_ptr;
663c2c66affSColin Finck }
664c2c66affSColin Finck 
665c2c66affSColin Finck void /* PRIVATE */
png_free_jmpbuf(png_structrp png_ptr)666c2c66affSColin Finck png_free_jmpbuf(png_structrp png_ptr)
667c2c66affSColin Finck {
668c2c66affSColin Finck    if (png_ptr != NULL)
669c2c66affSColin Finck    {
670c2c66affSColin Finck       jmp_buf *jb = png_ptr->jmp_buf_ptr;
671c2c66affSColin Finck 
672c2c66affSColin Finck       /* A size of 0 is used to indicate a local, stack, allocation of the
673c2c66affSColin Finck        * pointer; used here and in png.c
674c2c66affSColin Finck        */
675c2c66affSColin Finck       if (jb != NULL && png_ptr->jmp_buf_size > 0)
676c2c66affSColin Finck       {
677c2c66affSColin Finck 
678c2c66affSColin Finck          /* This stuff is so that a failure to free the error control structure
679c2c66affSColin Finck           * does not leave libpng in a state with no valid error handling: the
680c2c66affSColin Finck           * free always succeeds, if there is an error it gets ignored.
681c2c66affSColin Finck           */
682c2c66affSColin Finck          if (jb != &png_ptr->jmp_buf_local)
683c2c66affSColin Finck          {
684c2c66affSColin Finck             /* Make an internal, libpng, jmp_buf to return here */
685c2c66affSColin Finck             jmp_buf free_jmp_buf;
686c2c66affSColin Finck 
687c2c66affSColin Finck             if (!setjmp(free_jmp_buf))
688c2c66affSColin Finck             {
689c2c66affSColin Finck                png_ptr->jmp_buf_ptr = &free_jmp_buf; /* come back here */
690c2c66affSColin Finck                png_ptr->jmp_buf_size = 0; /* stack allocation */
691c2c66affSColin Finck                png_ptr->longjmp_fn = longjmp;
692c2c66affSColin Finck                png_free(png_ptr, jb); /* Return to setjmp on error */
693c2c66affSColin Finck             }
694c2c66affSColin Finck          }
695c2c66affSColin Finck       }
696c2c66affSColin Finck 
697c2c66affSColin Finck       /* *Always* cancel everything out: */
698c2c66affSColin Finck       png_ptr->jmp_buf_size = 0;
699c2c66affSColin Finck       png_ptr->jmp_buf_ptr = NULL;
700c2c66affSColin Finck       png_ptr->longjmp_fn = 0;
701c2c66affSColin Finck    }
702c2c66affSColin Finck }
703c2c66affSColin Finck #endif
704c2c66affSColin Finck 
705c2c66affSColin Finck /* This is the default error handling function.  Note that replacements for
706c2c66affSColin Finck  * this function MUST NOT RETURN, or the program will likely crash.  This
707c2c66affSColin Finck  * function is used by default, or if the program supplies NULL for the
708c2c66affSColin Finck  * error function pointer in png_set_error_fn().
709c2c66affSColin Finck  */
710c2c66affSColin Finck static PNG_FUNCTION(void /* PRIVATE */,
711c2c66affSColin Finck png_default_error,(png_const_structrp png_ptr, png_const_charp error_message),
712c2c66affSColin Finck     PNG_NORETURN)
713c2c66affSColin Finck {
714c2c66affSColin Finck #ifdef PNG_CONSOLE_IO_SUPPORTED
715c2c66affSColin Finck #ifdef PNG_ERROR_NUMBERS_SUPPORTED
716c2c66affSColin Finck    /* Check on NULL only added in 1.5.4 */
717c2c66affSColin Finck    if (error_message != NULL && *error_message == PNG_LITERAL_SHARP)
718c2c66affSColin Finck    {
719c2c66affSColin Finck       /* Strip "#nnnn " from beginning of error message. */
720c2c66affSColin Finck       int offset;
721c2c66affSColin Finck       char error_number[16];
722c2c66affSColin Finck       for (offset = 0; offset<15; offset++)
723c2c66affSColin Finck       {
724c2c66affSColin Finck          error_number[offset] = error_message[offset + 1];
725c2c66affSColin Finck          if (error_message[offset] == ' ')
726c2c66affSColin Finck             break;
727c2c66affSColin Finck       }
728c2c66affSColin Finck 
729c2c66affSColin Finck       if ((offset > 1) && (offset < 15))
730c2c66affSColin Finck       {
731c2c66affSColin Finck          error_number[offset - 1] = '\0';
732c2c66affSColin Finck          fprintf(stderr, "libpng error no. %s: %s",
733c2c66affSColin Finck              error_number, error_message + offset + 1);
734c2c66affSColin Finck          fprintf(stderr, PNG_STRING_NEWLINE);
735c2c66affSColin Finck       }
736c2c66affSColin Finck 
737c2c66affSColin Finck       else
738c2c66affSColin Finck       {
739c2c66affSColin Finck          fprintf(stderr, "libpng error: %s, offset=%d",
740c2c66affSColin Finck              error_message, offset);
741c2c66affSColin Finck          fprintf(stderr, PNG_STRING_NEWLINE);
742c2c66affSColin Finck       }
743c2c66affSColin Finck    }
744c2c66affSColin Finck    else
745c2c66affSColin Finck #endif
746c2c66affSColin Finck    {
747c2c66affSColin Finck       fprintf(stderr, "libpng error: %s", error_message ? error_message :
748c2c66affSColin Finck          "undefined");
749c2c66affSColin Finck       fprintf(stderr, PNG_STRING_NEWLINE);
750c2c66affSColin Finck    }
751c2c66affSColin Finck #else
752c2c66affSColin Finck    PNG_UNUSED(error_message) /* Make compiler happy */
753c2c66affSColin Finck #endif
754c2c66affSColin Finck    png_longjmp(png_ptr, 1);
755c2c66affSColin Finck }
756c2c66affSColin Finck 
757c2c66affSColin Finck PNG_FUNCTION(void,PNGAPI
758c2c66affSColin Finck png_longjmp,(png_const_structrp png_ptr, int val),PNG_NORETURN)
759c2c66affSColin Finck {
760c2c66affSColin Finck #ifdef PNG_SETJMP_SUPPORTED
761c2c66affSColin Finck    if (png_ptr != NULL && png_ptr->longjmp_fn != NULL &&
762c2c66affSColin Finck        png_ptr->jmp_buf_ptr != NULL)
763c2c66affSColin Finck       png_ptr->longjmp_fn(*png_ptr->jmp_buf_ptr, val);
764c2c66affSColin Finck #else
765c2c66affSColin Finck    PNG_UNUSED(png_ptr)
766c2c66affSColin Finck    PNG_UNUSED(val)
767c2c66affSColin Finck #endif
768c2c66affSColin Finck 
769c2c66affSColin Finck    /* If control reaches this point, png_longjmp() must not return. The only
770c2c66affSColin Finck     * choice is to terminate the whole process (or maybe the thread); to do
771c2c66affSColin Finck     * this the ANSI-C abort() function is used unless a different method is
772c2c66affSColin Finck     * implemented by overriding the default configuration setting for
773c2c66affSColin Finck     * PNG_ABORT().
774c2c66affSColin Finck     */
775c2c66affSColin Finck    PNG_ABORT();
776c2c66affSColin Finck }
777c2c66affSColin Finck 
778c2c66affSColin Finck #ifdef PNG_WARNINGS_SUPPORTED
779c2c66affSColin Finck /* This function is called when there is a warning, but the library thinks
780c2c66affSColin Finck  * it can continue anyway.  Replacement functions don't have to do anything
781c2c66affSColin Finck  * here if you don't want them to.  In the default configuration, png_ptr is
782c2c66affSColin Finck  * not used, but it is passed in case it may be useful.
783c2c66affSColin Finck  */
784c2c66affSColin Finck static void /* PRIVATE */
png_default_warning(png_const_structrp png_ptr,png_const_charp warning_message)785c2c66affSColin Finck png_default_warning(png_const_structrp png_ptr, png_const_charp warning_message)
786c2c66affSColin Finck {
787c2c66affSColin Finck #ifdef PNG_CONSOLE_IO_SUPPORTED
788c2c66affSColin Finck #  ifdef PNG_ERROR_NUMBERS_SUPPORTED
789c2c66affSColin Finck    if (*warning_message == PNG_LITERAL_SHARP)
790c2c66affSColin Finck    {
791c2c66affSColin Finck       int offset;
792c2c66affSColin Finck       char warning_number[16];
793c2c66affSColin Finck       for (offset = 0; offset < 15; offset++)
794c2c66affSColin Finck       {
795c2c66affSColin Finck          warning_number[offset] = warning_message[offset + 1];
796c2c66affSColin Finck          if (warning_message[offset] == ' ')
797c2c66affSColin Finck             break;
798c2c66affSColin Finck       }
799c2c66affSColin Finck 
800c2c66affSColin Finck       if ((offset > 1) && (offset < 15))
801c2c66affSColin Finck       {
802c2c66affSColin Finck          warning_number[offset + 1] = '\0';
803c2c66affSColin Finck          fprintf(stderr, "libpng warning no. %s: %s",
804c2c66affSColin Finck              warning_number, warning_message + offset);
805c2c66affSColin Finck          fprintf(stderr, PNG_STRING_NEWLINE);
806c2c66affSColin Finck       }
807c2c66affSColin Finck 
808c2c66affSColin Finck       else
809c2c66affSColin Finck       {
810c2c66affSColin Finck          fprintf(stderr, "libpng warning: %s",
811c2c66affSColin Finck              warning_message);
812c2c66affSColin Finck          fprintf(stderr, PNG_STRING_NEWLINE);
813c2c66affSColin Finck       }
814c2c66affSColin Finck    }
815c2c66affSColin Finck    else
816c2c66affSColin Finck #  endif
817c2c66affSColin Finck 
818c2c66affSColin Finck    {
819c2c66affSColin Finck       fprintf(stderr, "libpng warning: %s", warning_message);
820c2c66affSColin Finck       fprintf(stderr, PNG_STRING_NEWLINE);
821c2c66affSColin Finck    }
822c2c66affSColin Finck #else
823c2c66affSColin Finck    PNG_UNUSED(warning_message) /* Make compiler happy */
824c2c66affSColin Finck #endif
825c2c66affSColin Finck    PNG_UNUSED(png_ptr) /* Make compiler happy */
826c2c66affSColin Finck }
827c2c66affSColin Finck #endif /* WARNINGS */
828c2c66affSColin Finck 
829c2c66affSColin Finck /* This function is called when the application wants to use another method
830c2c66affSColin Finck  * of handling errors and warnings.  Note that the error function MUST NOT
831c2c66affSColin Finck  * return to the calling routine or serious problems will occur.  The return
832c2c66affSColin Finck  * method used in the default routine calls longjmp(png_ptr->jmp_buf_ptr, 1)
833c2c66affSColin Finck  */
834c2c66affSColin Finck void PNGAPI
png_set_error_fn(png_structrp png_ptr,png_voidp error_ptr,png_error_ptr error_fn,png_error_ptr warning_fn)835c2c66affSColin Finck png_set_error_fn(png_structrp png_ptr, png_voidp error_ptr,
836c2c66affSColin Finck     png_error_ptr error_fn, png_error_ptr warning_fn)
837c2c66affSColin Finck {
838c2c66affSColin Finck    if (png_ptr == NULL)
839c2c66affSColin Finck       return;
840c2c66affSColin Finck 
841c2c66affSColin Finck    png_ptr->error_ptr = error_ptr;
842c2c66affSColin Finck    png_ptr->error_fn = error_fn;
843c2c66affSColin Finck #ifdef PNG_WARNINGS_SUPPORTED
844c2c66affSColin Finck    png_ptr->warning_fn = warning_fn;
845c2c66affSColin Finck #else
846c2c66affSColin Finck    PNG_UNUSED(warning_fn)
847c2c66affSColin Finck #endif
848c2c66affSColin Finck }
849c2c66affSColin Finck 
850c2c66affSColin Finck 
851c2c66affSColin Finck /* This function returns a pointer to the error_ptr associated with the user
852c2c66affSColin Finck  * functions.  The application should free any memory associated with this
853c2c66affSColin Finck  * pointer before png_write_destroy and png_read_destroy are called.
854c2c66affSColin Finck  */
855c2c66affSColin Finck png_voidp PNGAPI
png_get_error_ptr(png_const_structrp png_ptr)856c2c66affSColin Finck png_get_error_ptr(png_const_structrp png_ptr)
857c2c66affSColin Finck {
858c2c66affSColin Finck    if (png_ptr == NULL)
859c2c66affSColin Finck       return NULL;
860c2c66affSColin Finck 
861c2c66affSColin Finck    return ((png_voidp)png_ptr->error_ptr);
862c2c66affSColin Finck }
863c2c66affSColin Finck 
864c2c66affSColin Finck 
865c2c66affSColin Finck #ifdef PNG_ERROR_NUMBERS_SUPPORTED
866c2c66affSColin Finck void PNGAPI
png_set_strip_error_numbers(png_structrp png_ptr,png_uint_32 strip_mode)867c2c66affSColin Finck png_set_strip_error_numbers(png_structrp png_ptr, png_uint_32 strip_mode)
868c2c66affSColin Finck {
869c2c66affSColin Finck    if (png_ptr != NULL)
870c2c66affSColin Finck    {
871c2c66affSColin Finck       png_ptr->flags &=
872c2c66affSColin Finck          ((~(PNG_FLAG_STRIP_ERROR_NUMBERS |
873c2c66affSColin Finck          PNG_FLAG_STRIP_ERROR_TEXT))&strip_mode);
874c2c66affSColin Finck    }
875c2c66affSColin Finck }
876c2c66affSColin Finck #endif
877c2c66affSColin Finck 
878c2c66affSColin Finck #if defined(PNG_SIMPLIFIED_READ_SUPPORTED) ||\
879c2c66affSColin Finck    defined(PNG_SIMPLIFIED_WRITE_SUPPORTED)
880c2c66affSColin Finck    /* Currently the above both depend on SETJMP_SUPPORTED, however it would be
881c2c66affSColin Finck     * possible to implement without setjmp support just so long as there is some
882c2c66affSColin Finck     * way to handle the error return here:
883c2c66affSColin Finck     */
884c2c66affSColin Finck PNG_FUNCTION(void /* PRIVATE */, (PNGCBAPI
885c2c66affSColin Finck png_safe_error),(png_structp png_nonconst_ptr, png_const_charp error_message),
886c2c66affSColin Finck     PNG_NORETURN)
887c2c66affSColin Finck {
888*b61b1815SThomas Faber    png_const_structrp png_ptr = png_nonconst_ptr;
889c2c66affSColin Finck    png_imagep image = png_voidcast(png_imagep, png_ptr->error_ptr);
890c2c66affSColin Finck 
891c2c66affSColin Finck    /* An error is always logged here, overwriting anything (typically a warning)
892c2c66affSColin Finck     * that is already there:
893c2c66affSColin Finck     */
894c2c66affSColin Finck    if (image != NULL)
895c2c66affSColin Finck    {
896c2c66affSColin Finck       png_safecat(image->message, (sizeof image->message), 0, error_message);
897c2c66affSColin Finck       image->warning_or_error |= PNG_IMAGE_ERROR;
898c2c66affSColin Finck 
899c2c66affSColin Finck       /* Retrieve the jmp_buf from within the png_control, making this work for
900c2c66affSColin Finck        * C++ compilation too is pretty tricky: C++ wants a pointer to the first
901c2c66affSColin Finck        * element of a jmp_buf, but C doesn't tell us the type of that.
902c2c66affSColin Finck        */
903c2c66affSColin Finck       if (image->opaque != NULL && image->opaque->error_buf != NULL)
904c2c66affSColin Finck          longjmp(png_control_jmp_buf(image->opaque), 1);
905c2c66affSColin Finck 
906c2c66affSColin Finck       /* Missing longjmp buffer, the following is to help debugging: */
907c2c66affSColin Finck       {
908c2c66affSColin Finck          size_t pos = png_safecat(image->message, (sizeof image->message), 0,
909c2c66affSColin Finck              "bad longjmp: ");
910c2c66affSColin Finck          png_safecat(image->message, (sizeof image->message), pos,
911c2c66affSColin Finck              error_message);
912c2c66affSColin Finck       }
913c2c66affSColin Finck    }
914c2c66affSColin Finck 
915c2c66affSColin Finck    /* Here on an internal programming error. */
916c2c66affSColin Finck    abort();
917c2c66affSColin Finck }
918c2c66affSColin Finck 
919c2c66affSColin Finck #ifdef PNG_WARNINGS_SUPPORTED
920c2c66affSColin Finck void /* PRIVATE */ PNGCBAPI
png_safe_warning(png_structp png_nonconst_ptr,png_const_charp warning_message)921c2c66affSColin Finck png_safe_warning(png_structp png_nonconst_ptr, png_const_charp warning_message)
922c2c66affSColin Finck {
923*b61b1815SThomas Faber    png_const_structrp png_ptr = png_nonconst_ptr;
924c2c66affSColin Finck    png_imagep image = png_voidcast(png_imagep, png_ptr->error_ptr);
925c2c66affSColin Finck 
926c2c66affSColin Finck    /* A warning is only logged if there is no prior warning or error. */
927c2c66affSColin Finck    if (image->warning_or_error == 0)
928c2c66affSColin Finck    {
929c2c66affSColin Finck       png_safecat(image->message, (sizeof image->message), 0, warning_message);
930c2c66affSColin Finck       image->warning_or_error |= PNG_IMAGE_WARNING;
931c2c66affSColin Finck    }
932c2c66affSColin Finck }
933c2c66affSColin Finck #endif
934c2c66affSColin Finck 
935c2c66affSColin Finck int /* PRIVATE */
png_safe_execute(png_imagep image_in,int (* function)(png_voidp),png_voidp arg)936c2c66affSColin Finck png_safe_execute(png_imagep image_in, int (*function)(png_voidp), png_voidp arg)
937c2c66affSColin Finck {
938c2c66affSColin Finck    volatile png_imagep image = image_in;
939c2c66affSColin Finck    volatile int result;
940c2c66affSColin Finck    volatile png_voidp saved_error_buf;
941c2c66affSColin Finck    jmp_buf safe_jmpbuf;
942c2c66affSColin Finck 
943c2c66affSColin Finck    /* Safely execute function(arg) with png_error returning to this function. */
944c2c66affSColin Finck    saved_error_buf = image->opaque->error_buf;
945c2c66affSColin Finck    result = setjmp(safe_jmpbuf) == 0;
946c2c66affSColin Finck 
947c2c66affSColin Finck    if (result != 0)
948c2c66affSColin Finck    {
949c2c66affSColin Finck 
950c2c66affSColin Finck       image->opaque->error_buf = safe_jmpbuf;
951c2c66affSColin Finck       result = function(arg);
952c2c66affSColin Finck    }
953c2c66affSColin Finck 
954c2c66affSColin Finck    image->opaque->error_buf = saved_error_buf;
955c2c66affSColin Finck 
956c2c66affSColin Finck    /* And do the cleanup prior to any failure return. */
957c2c66affSColin Finck    if (result == 0)
958c2c66affSColin Finck       png_image_free(image);
959c2c66affSColin Finck 
960c2c66affSColin Finck    return result;
961c2c66affSColin Finck }
962c2c66affSColin Finck #endif /* SIMPLIFIED READ || SIMPLIFIED_WRITE */
963c2c66affSColin Finck #endif /* READ || WRITE */
964