source: liacs/MIR2010/SourceCode/cximage/png/pngerror.c@ 214

Last change on this file since 214 was 95, checked in by Rick van der Zwet, 15 years ago

Bad boy, improper move of directory

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