blob: 40c77c837880e9b4f317f385625c55cbd8b1c71a [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * This code is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 only, as
6 * published by the Free Software Foundation. Sun designates this
7 * particular file as subject to the "Classpath" exception as provided
8 * by Sun in the LICENSE file that accompanied this code.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
22 * have any questions.
23 *
24 * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
25 */
26
27/* pngerror.c - stub functions for i/o and memory allocation
28 *
29 * This file is available under and governed by the GNU General Public
30 * License version 2 only, as published by the Free Software Foundation.
31 * However, the following notice accompanied the original version of this
32 * file and, per its terms, should not be removed:
33 *
34 * Last changed in libpng 1.2.13 November 13, 2006
35 * For conditions of distribution and use, see copyright notice in png.h
36 * Copyright (c) 1998-2006 Glenn Randers-Pehrson
37 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
38 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
39 *
40 * This file provides a location for all error handling. Users who
41 * need special error handling are expected to write replacement functions
42 * and use png_set_error_fn() to use those functions. See the instructions
43 * at each function.
44 */
45
46#define PNG_INTERNAL
47#include "png.h"
48
49#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
50static void /* PRIVATE */
51png_default_error PNGARG((png_structp png_ptr,
52 png_const_charp error_message));
53static void /* PRIVATE */
54png_default_warning PNGARG((png_structp png_ptr,
55 png_const_charp warning_message));
56
57/* This function is called whenever there is a fatal error. This function
58 * should not be changed. If there is a need to handle errors differently,
59 * you should supply a replacement error function and use png_set_error_fn()
60 * to replace the error function at run-time.
61 */
62void PNGAPI
63png_error(png_structp png_ptr, png_const_charp error_message)
64{
65#ifdef PNG_ERROR_NUMBERS_SUPPORTED
66 char msg[16];
67 if (png_ptr != NULL)
68 {
69 if (png_ptr->flags&
70 (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))
71 {
72 if (*error_message == '#')
73 {
74 int offset;
75 for (offset=1; offset<15; offset++)
76 if (*(error_message+offset) == ' ')
77 break;
78 if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT)
79 {
80 int i;
81 for (i=0; i<offset-1; i++)
82 msg[i]=error_message[i+1];
83 msg[i]='\0';
84 error_message=msg;
85 }
86 else
87 error_message+=offset;
88 }
89 else
90 {
91 if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT)
92 {
93 msg[0]='0';
94 msg[1]='\0';
95 error_message=msg;
96 }
97 }
98 }
99 }
100#endif
101 if (png_ptr != NULL && png_ptr->error_fn != NULL)
102 (*(png_ptr->error_fn))(png_ptr, error_message);
103
104 /* If the custom handler doesn't exist, or if it returns,
105 use the default handler, which will not return. */
106 png_default_error(png_ptr, error_message);
107}
108
109/* This function is called whenever there is a non-fatal error. This function
110 * should not be changed. If there is a need to handle warnings differently,
111 * you should supply a replacement warning function and use
112 * png_set_error_fn() to replace the warning function at run-time.
113 */
114void PNGAPI
115png_warning(png_structp png_ptr, png_const_charp warning_message)
116{
117 int offset = 0;
118 if (png_ptr != NULL)
119 {
120#ifdef PNG_ERROR_NUMBERS_SUPPORTED
121 if (png_ptr->flags&
122 (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))
123#endif
124 {
125 if (*warning_message == '#')
126 {
127 for (offset=1; offset<15; offset++)
128 if (*(warning_message+offset) == ' ')
129 break;
130 }
131 }
132 if (png_ptr != NULL && png_ptr->warning_fn != NULL)
133 (*(png_ptr->warning_fn))(png_ptr, warning_message+offset);
134 }
135 else
136 png_default_warning(png_ptr, warning_message+offset);
137}
138
139/* These utilities are used internally to build an error message that relates
140 * to the current chunk. The chunk name comes from png_ptr->chunk_name,
141 * this is used to prefix the message. The message is limited in length
142 * to 63 bytes, the name characters are output as hex digits wrapped in []
143 * if the character is invalid.
144 */
145#define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
146static PNG_CONST char png_digit[16] = {
147 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
148 'A', 'B', 'C', 'D', 'E', 'F'
149};
150
151static void /* PRIVATE */
152png_format_buffer(png_structp png_ptr, png_charp buffer, png_const_charp
153 error_message)
154{
155 int iout = 0, iin = 0;
156
157 while (iin < 4)
158 {
159 int c = png_ptr->chunk_name[iin++];
160 if (isnonalpha(c))
161 {
162 buffer[iout++] = '[';
163 buffer[iout++] = png_digit[(c & 0xf0) >> 4];
164 buffer[iout++] = png_digit[c & 0x0f];
165 buffer[iout++] = ']';
166 }
167 else
168 {
169 buffer[iout++] = (png_byte)c;
170 }
171 }
172
173 if (error_message == NULL)
174 buffer[iout] = 0;
175 else
176 {
177 buffer[iout++] = ':';
178 buffer[iout++] = ' ';
179 png_strncpy(buffer+iout, error_message, 63);
180 buffer[iout+63] = 0;
181 }
182}
183
184void PNGAPI
185png_chunk_error(png_structp png_ptr, png_const_charp error_message)
186{
187 char msg[18+64];
188 if (png_ptr == NULL)
189 png_error(png_ptr, error_message);
190 else
191 {
192 png_format_buffer(png_ptr, msg, error_message);
193 png_error(png_ptr, msg);
194 }
195}
196
197void PNGAPI
198png_chunk_warning(png_structp png_ptr, png_const_charp warning_message)
199{
200 char msg[18+64];
201 if (png_ptr == NULL)
202 png_warning(png_ptr, warning_message);
203 else
204 {
205 png_format_buffer(png_ptr, msg, warning_message);
206 png_warning(png_ptr, msg);
207 }
208}
209
210/* This is the default error handling function. Note that replacements for
211 * this function MUST NOT RETURN, or the program will likely crash. This
212 * function is used by default, or if the program supplies NULL for the
213 * error function pointer in png_set_error_fn().
214 */
215static void /* PRIVATE */
216png_default_error(png_structp png_ptr, png_const_charp error_message)
217{
218#ifndef PNG_NO_CONSOLE_IO
219#ifdef PNG_ERROR_NUMBERS_SUPPORTED
220 if (*error_message == '#')
221 {
222 int offset;
223 char error_number[16];
224 for (offset=0; offset<15; offset++)
225 {
226 error_number[offset] = *(error_message+offset+1);
227 if (*(error_message+offset) == ' ')
228 break;
229 }
230 if((offset > 1) && (offset < 15))
231 {
232 error_number[offset-1]='\0';
233 fprintf(stderr, "libpng error no. %s: %s\n", error_number,
234 error_message+offset);
235 }
236 else
237 fprintf(stderr, "libpng error: %s, offset=%d\n", error_message,offset);
238 }
239 else
240#endif
241 fprintf(stderr, "libpng error: %s\n", error_message);
242#endif
243
244#ifdef PNG_SETJMP_SUPPORTED
245 if (png_ptr)
246 {
247# ifdef USE_FAR_KEYWORD
248 {
249 jmp_buf jmpbuf;
250 png_memcpy(jmpbuf,png_ptr->jmpbuf,png_sizeof(jmp_buf));
251 longjmp(jmpbuf, 1);
252 }
253# else
254 longjmp(png_ptr->jmpbuf, 1);
255# endif
256 }
257#else
258 PNG_ABORT();
259#endif
260#ifdef PNG_NO_CONSOLE_IO
261 /* make compiler happy */ ;
262 if (&error_message != NULL)
263 return;
264#endif
265}
266
267/* This function is called when there is a warning, but the library thinks
268 * it can continue anyway. Replacement functions don't have to do anything
269 * here if you don't want them to. In the default configuration, png_ptr is
270 * not used, but it is passed in case it may be useful.
271 */
272static void /* PRIVATE */
273png_default_warning(png_structp png_ptr, png_const_charp warning_message)
274{
275#ifndef PNG_NO_CONSOLE_IO
276# ifdef PNG_ERROR_NUMBERS_SUPPORTED
277 if (*warning_message == '#')
278 {
279 int offset;
280 char warning_number[16];
281 for (offset=0; offset<15; offset++)
282 {
283 warning_number[offset]=*(warning_message+offset+1);
284 if (*(warning_message+offset) == ' ')
285 break;
286 }
287 if((offset > 1) && (offset < 15))
288 {
289 warning_number[offset-1]='\0';
290 fprintf(stderr, "libpng warning no. %s: %s\n", warning_number,
291 warning_message+offset);
292 }
293 else
294 fprintf(stderr, "libpng warning: %s\n", warning_message);
295 }
296 else
297# endif
298 fprintf(stderr, "libpng warning: %s\n", warning_message);
299#else
300 /* make compiler happy */ ;
301 if (warning_message)
302 return;
303#endif
304 /* make compiler happy */ ;
305 if (png_ptr)
306 return;
307}
308
309/* This function is called when the application wants to use another method
310 * of handling errors and warnings. Note that the error function MUST NOT
311 * return to the calling routine or serious problems will occur. The return
312 * method used in the default routine calls longjmp(png_ptr->jmpbuf, 1)
313 */
314void PNGAPI
315png_set_error_fn(png_structp png_ptr, png_voidp error_ptr,
316 png_error_ptr error_fn, png_error_ptr warning_fn)
317{
318 if (png_ptr == NULL)
319 return;
320 png_ptr->error_ptr = error_ptr;
321 png_ptr->error_fn = error_fn;
322 png_ptr->warning_fn = warning_fn;
323}
324
325
326/* This function returns a pointer to the error_ptr associated with the user
327 * functions. The application should free any memory associated with this
328 * pointer before png_write_destroy and png_read_destroy are called.
329 */
330png_voidp PNGAPI
331png_get_error_ptr(png_structp png_ptr)
332{
333 if (png_ptr == NULL)
334 return NULL;
335 return ((png_voidp)png_ptr->error_ptr);
336}
337
338
339#ifdef PNG_ERROR_NUMBERS_SUPPORTED
340void PNGAPI
341png_set_strip_error_numbers(png_structp png_ptr, png_uint_32 strip_mode)
342{
343 if(png_ptr != NULL)
344 {
345 png_ptr->flags &=
346 ((~(PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))&strip_mode);
347 }
348}
349#endif
350#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */