blob: 7bc98fb11e4c7a282c824cff47c162f9c679abc6 [file] [log] [blame]
The Android Open Source Project893912b2009-03-03 19:30:05 -08001
2/* pngerror.c - stub functions for i/o and memory allocation
3 *
Patrick Scott5f6bd842010-06-28 16:55:16 -04004 * Last changed in libpng 1.2.41 [December 3, 2009]
Patrick Scotta0bb96c2009-07-22 11:50:02 -04005 * Copyright (c) 1998-2009 Glenn Randers-Pehrson
The Android Open Source Project893912b2009-03-03 19:30:05 -08006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
8 *
Patrick Scotta0bb96c2009-07-22 11:50:02 -04009 * This code is released under the libpng license.
10 * For conditions of distribution and use, see the disclaimer
11 * and license in png.h
12 *
The Android Open Source Project893912b2009-03-03 19:30:05 -080013 * This file provides a location for all error handling. Users who
14 * 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 */
18
19#define PNG_INTERNAL
Patrick Scott5f6bd842010-06-28 16:55:16 -040020#define PNG_NO_PEDANTIC_WARNINGS
The Android Open Source Project893912b2009-03-03 19:30:05 -080021#include "png.h"
The Android Open Source Project893912b2009-03-03 19:30:05 -080022#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
The Android Open Source Project4215dd12009-03-09 11:52:12 -070023
The Android Open Source Project893912b2009-03-03 19:30:05 -080024static void /* PRIVATE */
25png_default_error PNGARG((png_structp png_ptr,
Patrick Scott5f6bd842010-06-28 16:55:16 -040026 png_const_charp error_message)) PNG_NORETURN;
27#ifdef PNG_WARNINGS_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -080028static void /* PRIVATE */
29png_default_warning PNGARG((png_structp png_ptr,
30 png_const_charp warning_message));
Patrick Scott5f6bd842010-06-28 16:55:16 -040031#endif /* PNG_WARNINGS_SUPPORTED */
The Android Open Source Project893912b2009-03-03 19:30:05 -080032
33/* This function is called whenever there is a fatal error. This function
34 * should not be changed. If there is a need to handle errors differently,
35 * you should supply a replacement error function and use png_set_error_fn()
36 * to replace the error function at run-time.
37 */
Patrick Scott5f6bd842010-06-28 16:55:16 -040038#ifdef PNG_ERROR_TEXT_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -080039void PNGAPI
40png_error(png_structp png_ptr, png_const_charp error_message)
41{
42#ifdef PNG_ERROR_NUMBERS_SUPPORTED
43 char msg[16];
44 if (png_ptr != NULL)
45 {
46 if (png_ptr->flags&
47 (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))
48 {
Patrick Scott5f6bd842010-06-28 16:55:16 -040049 if (*error_message == PNG_LITERAL_SHARP)
The Android Open Source Project893912b2009-03-03 19:30:05 -080050 {
Patrick Scotta0bb96c2009-07-22 11:50:02 -040051 /* Strip "#nnnn " from beginning of error message. */
The Android Open Source Project893912b2009-03-03 19:30:05 -080052 int offset;
The Android Open Source Project4215dd12009-03-09 11:52:12 -070053 for (offset = 1; offset<15; offset++)
54 if (error_message[offset] == ' ')
The Android Open Source Project893912b2009-03-03 19:30:05 -080055 break;
56 if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT)
57 {
58 int i;
The Android Open Source Project4215dd12009-03-09 11:52:12 -070059 for (i = 0; i < offset - 1; i++)
60 msg[i] = error_message[i + 1];
61 msg[i - 1] = '\0';
62 error_message = msg;
The Android Open Source Project893912b2009-03-03 19:30:05 -080063 }
64 else
The Android Open Source Project4215dd12009-03-09 11:52:12 -070065 error_message += offset;
The Android Open Source Project893912b2009-03-03 19:30:05 -080066 }
67 else
68 {
69 if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT)
70 {
The Android Open Source Project4215dd12009-03-09 11:52:12 -070071 msg[0] = '0';
72 msg[1] = '\0';
73 error_message = msg;
The Android Open Source Project893912b2009-03-03 19:30:05 -080074 }
75 }
76 }
77 }
78#endif
79 if (png_ptr != NULL && png_ptr->error_fn != NULL)
80 (*(png_ptr->error_fn))(png_ptr, error_message);
81
82 /* If the custom handler doesn't exist, or if it returns,
83 use the default handler, which will not return. */
84 png_default_error(png_ptr, error_message);
85}
86#else
87void PNGAPI
88png_err(png_structp png_ptr)
89{
90 if (png_ptr != NULL && png_ptr->error_fn != NULL)
91 (*(png_ptr->error_fn))(png_ptr, '\0');
92
93 /* If the custom handler doesn't exist, or if it returns,
94 use the default handler, which will not return. */
95 png_default_error(png_ptr, '\0');
96}
Patrick Scott5f6bd842010-06-28 16:55:16 -040097#endif /* PNG_ERROR_TEXT_SUPPORTED */
The Android Open Source Project893912b2009-03-03 19:30:05 -080098
Patrick Scott5f6bd842010-06-28 16:55:16 -040099#ifdef PNG_WARNINGS_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800100/* This function is called whenever there is a non-fatal error. This function
101 * should not be changed. If there is a need to handle warnings differently,
102 * you should supply a replacement warning function and use
103 * png_set_error_fn() to replace the warning function at run-time.
104 */
105void PNGAPI
106png_warning(png_structp png_ptr, png_const_charp warning_message)
107{
108 int offset = 0;
109 if (png_ptr != NULL)
110 {
111#ifdef PNG_ERROR_NUMBERS_SUPPORTED
112 if (png_ptr->flags&
113 (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))
114#endif
115 {
Patrick Scott5f6bd842010-06-28 16:55:16 -0400116 if (*warning_message == PNG_LITERAL_SHARP)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800117 {
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700118 for (offset = 1; offset < 15; offset++)
119 if (warning_message[offset] == ' ')
The Android Open Source Project893912b2009-03-03 19:30:05 -0800120 break;
121 }
122 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800123 }
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700124 if (png_ptr != NULL && png_ptr->warning_fn != NULL)
125 (*(png_ptr->warning_fn))(png_ptr, warning_message + offset);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800126 else
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700127 png_default_warning(png_ptr, warning_message + offset);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800128}
Patrick Scott5f6bd842010-06-28 16:55:16 -0400129#endif /* PNG_WARNINGS_SUPPORTED */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800130
Patrick Scott5f6bd842010-06-28 16:55:16 -0400131#ifdef PNG_BENIGN_ERRORS_SUPPORTED
132void PNGAPI
133png_benign_error(png_structp png_ptr, png_const_charp error_message)
134{
135 if (png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN)
136 png_warning(png_ptr, error_message);
137 else
138 png_error(png_ptr, error_message);
139}
140#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -0800141
142/* These utilities are used internally to build an error message that relates
143 * to the current chunk. The chunk name comes from png_ptr->chunk_name,
144 * this is used to prefix the message. The message is limited in length
145 * to 63 bytes, the name characters are output as hex digits wrapped in []
146 * if the character is invalid.
147 */
148#define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
149static PNG_CONST char png_digit[16] = {
150 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
151 'A', 'B', 'C', 'D', 'E', 'F'
152};
153
154#define PNG_MAX_ERROR_TEXT 64
Patrick Scott5f6bd842010-06-28 16:55:16 -0400155#if defined(PNG_WARNINGS_SUPPORTED) || defined(PNG_ERROR_TEXT_SUPPORTED)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800156static void /* PRIVATE */
157png_format_buffer(png_structp png_ptr, png_charp buffer, png_const_charp
158 error_message)
159{
160 int iout = 0, iin = 0;
161
162 while (iin < 4)
163 {
164 int c = png_ptr->chunk_name[iin++];
165 if (isnonalpha(c))
166 {
Patrick Scott5f6bd842010-06-28 16:55:16 -0400167 buffer[iout++] = PNG_LITERAL_LEFT_SQUARE_BRACKET;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800168 buffer[iout++] = png_digit[(c & 0xf0) >> 4];
169 buffer[iout++] = png_digit[c & 0x0f];
Patrick Scott5f6bd842010-06-28 16:55:16 -0400170 buffer[iout++] = PNG_LITERAL_RIGHT_SQUARE_BRACKET;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800171 }
172 else
173 {
174 buffer[iout++] = (png_byte)c;
175 }
176 }
177
178 if (error_message == NULL)
179 buffer[iout] = '\0';
180 else
181 {
182 buffer[iout++] = ':';
183 buffer[iout++] = ' ';
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700184 png_memcpy(buffer + iout, error_message, PNG_MAX_ERROR_TEXT);
185 buffer[iout + PNG_MAX_ERROR_TEXT - 1] = '\0';
The Android Open Source Project893912b2009-03-03 19:30:05 -0800186 }
187}
188
189#ifdef PNG_READ_SUPPORTED
190void PNGAPI
191png_chunk_error(png_structp png_ptr, png_const_charp error_message)
192{
193 char msg[18+PNG_MAX_ERROR_TEXT];
194 if (png_ptr == NULL)
195 png_error(png_ptr, error_message);
196 else
197 {
198 png_format_buffer(png_ptr, msg, error_message);
199 png_error(png_ptr, msg);
200 }
201}
202#endif /* PNG_READ_SUPPORTED */
Patrick Scott5f6bd842010-06-28 16:55:16 -0400203#endif /* PNG_WARNINGS_SUPPORTED || PNG_ERROR_TEXT_SUPPORTED */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800204
Patrick Scott5f6bd842010-06-28 16:55:16 -0400205#ifdef PNG_WARNINGS_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800206void PNGAPI
207png_chunk_warning(png_structp png_ptr, png_const_charp warning_message)
208{
209 char msg[18+PNG_MAX_ERROR_TEXT];
210 if (png_ptr == NULL)
211 png_warning(png_ptr, warning_message);
212 else
213 {
214 png_format_buffer(png_ptr, msg, warning_message);
215 png_warning(png_ptr, msg);
216 }
217}
Patrick Scott5f6bd842010-06-28 16:55:16 -0400218#endif /* PNG_WARNINGS_SUPPORTED */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800219
Patrick Scott5f6bd842010-06-28 16:55:16 -0400220#ifdef PNG_READ_SUPPORTED
221#ifdef PNG_BENIGN_ERRORS_SUPPORTED
222void PNGAPI
223png_chunk_benign_error(png_structp png_ptr, png_const_charp error_message)
224{
225 if (png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN)
226 png_chunk_warning(png_ptr, error_message);
227 else
228 png_chunk_error(png_ptr, error_message);
229}
230#endif
231#endif /* PNG_READ_SUPPORTED */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800232
233/* This is the default error handling function. Note that replacements for
234 * this function MUST NOT RETURN, or the program will likely crash. This
235 * function is used by default, or if the program supplies NULL for the
236 * error function pointer in png_set_error_fn().
237 */
238static void /* PRIVATE */
239png_default_error(png_structp png_ptr, png_const_charp error_message)
240{
Patrick Scott5f6bd842010-06-28 16:55:16 -0400241#ifdef PNG_CONSOLE_IO_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800242#ifdef PNG_ERROR_NUMBERS_SUPPORTED
Patrick Scott5f6bd842010-06-28 16:55:16 -0400243 if (*error_message == PNG_LITERAL_SHARP)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800244 {
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400245 /* Strip "#nnnn " from beginning of error message. */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800246 int offset;
247 char error_number[16];
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700248 for (offset = 0; offset<15; offset++)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800249 {
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700250 error_number[offset] = error_message[offset + 1];
251 if (error_message[offset] == ' ')
The Android Open Source Project893912b2009-03-03 19:30:05 -0800252 break;
253 }
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700254 if ((offset > 1) && (offset < 15))
The Android Open Source Project893912b2009-03-03 19:30:05 -0800255 {
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700256 error_number[offset - 1] = '\0';
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400257 fprintf(stderr, "libpng error no. %s: %s",
258 error_number, error_message + offset + 1);
259 fprintf(stderr, PNG_STRING_NEWLINE);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800260 }
261 else
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400262 {
263 fprintf(stderr, "libpng error: %s, offset=%d",
264 error_message, offset);
265 fprintf(stderr, PNG_STRING_NEWLINE);
266 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800267 }
268 else
269#endif
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400270 {
271 fprintf(stderr, "libpng error: %s", error_message);
272 fprintf(stderr, PNG_STRING_NEWLINE);
273 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800274#endif
275
276#ifdef PNG_SETJMP_SUPPORTED
277 if (png_ptr)
278 {
279# ifdef USE_FAR_KEYWORD
280 {
281 jmp_buf jmpbuf;
282 png_memcpy(jmpbuf, png_ptr->jmpbuf, png_sizeof(jmp_buf));
Patrick Scott5f6bd842010-06-28 16:55:16 -0400283 longjmp(jmpbuf,1);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800284 }
285# else
286 longjmp(png_ptr->jmpbuf, 1);
287# endif
288 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800289#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -0400290 /* Here if not setjmp support or if png_ptr is null. */
291 PNG_ABORT();
292#ifndef PNG_CONSOLE_IO_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400293 error_message = error_message; /* Make compiler happy */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800294#endif
295}
296
Patrick Scott5f6bd842010-06-28 16:55:16 -0400297#ifdef PNG_WARNINGS_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800298/* This function is called when there is a warning, but the library thinks
299 * it can continue anyway. Replacement functions don't have to do anything
300 * here if you don't want them to. In the default configuration, png_ptr is
301 * not used, but it is passed in case it may be useful.
302 */
303static void /* PRIVATE */
304png_default_warning(png_structp png_ptr, png_const_charp warning_message)
305{
Patrick Scott5f6bd842010-06-28 16:55:16 -0400306#ifdef PNG_CONSOLE_IO_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800307# ifdef PNG_ERROR_NUMBERS_SUPPORTED
Patrick Scott5f6bd842010-06-28 16:55:16 -0400308 if (*warning_message == PNG_LITERAL_SHARP)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800309 {
310 int offset;
311 char warning_number[16];
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700312 for (offset = 0; offset < 15; offset++)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800313 {
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700314 warning_number[offset] = warning_message[offset + 1];
315 if (warning_message[offset] == ' ')
The Android Open Source Project893912b2009-03-03 19:30:05 -0800316 break;
317 }
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700318 if ((offset > 1) && (offset < 15))
The Android Open Source Project893912b2009-03-03 19:30:05 -0800319 {
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700320 warning_number[offset + 1] = '\0';
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400321 fprintf(stderr, "libpng warning no. %s: %s",
322 warning_number, warning_message + offset);
323 fprintf(stderr, PNG_STRING_NEWLINE);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800324 }
325 else
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400326 {
327 fprintf(stderr, "libpng warning: %s",
328 warning_message);
329 fprintf(stderr, PNG_STRING_NEWLINE);
330 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800331 }
332 else
333# endif
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400334 {
335 fprintf(stderr, "libpng warning: %s", warning_message);
336 fprintf(stderr, PNG_STRING_NEWLINE);
337 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800338#else
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400339 warning_message = warning_message; /* Make compiler happy */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800340#endif
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400341 png_ptr = png_ptr; /* Make compiler happy */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800342}
Patrick Scott5f6bd842010-06-28 16:55:16 -0400343#endif /* PNG_WARNINGS_SUPPORTED */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800344
345/* This function is called when the application wants to use another method
346 * of handling errors and warnings. Note that the error function MUST NOT
347 * return to the calling routine or serious problems will occur. The return
348 * method used in the default routine calls longjmp(png_ptr->jmpbuf, 1)
349 */
350void PNGAPI
351png_set_error_fn(png_structp png_ptr, png_voidp error_ptr,
352 png_error_ptr error_fn, png_error_ptr warning_fn)
353{
354 if (png_ptr == NULL)
355 return;
356 png_ptr->error_ptr = error_ptr;
357 png_ptr->error_fn = error_fn;
358 png_ptr->warning_fn = warning_fn;
359}
360
361
362/* This function returns a pointer to the error_ptr associated with the user
363 * functions. The application should free any memory associated with this
364 * pointer before png_write_destroy and png_read_destroy are called.
365 */
366png_voidp PNGAPI
367png_get_error_ptr(png_structp png_ptr)
368{
369 if (png_ptr == NULL)
370 return NULL;
371 return ((png_voidp)png_ptr->error_ptr);
372}
373
374
375#ifdef PNG_ERROR_NUMBERS_SUPPORTED
376void PNGAPI
377png_set_strip_error_numbers(png_structp png_ptr, png_uint_32 strip_mode)
378{
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700379 if (png_ptr != NULL)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800380 {
381 png_ptr->flags &=
382 ((~(PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))&strip_mode);
383 }
384}
385#endif
386#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */