blob: 3358d400d3f1882f19828fc841f4eb25b5f5b34f [file] [log] [blame]
Guy Schalnat6d764711995-12-19 03:22:19 -06001
2/* pngerror.c - stub functions for i/o and memory allocation
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06003 *
Glenn Randers-Pehrson8f8fb6a1998-03-09 23:02:06 -06004 * libpng 1.0.0a
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
7 * Copyright (c) 1996, 1997 Andreas Dilger
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06008 * Copyright (c) 1998, Glenn Randers-Pehrson
Glenn Randers-Pehrson8f8fb6a1998-03-09 23:02:06 -06009 * March 9, 1998
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060010 *
11 * This file provides a location for all error handling. Users which
12 * need special error handling are expected to write replacement functions
13 * and use png_set_error_fn() to use those functions. See the instructions
14 * at each function.
15 */
Guy Schalnat6d764711995-12-19 03:22:19 -060016
17#define PNG_INTERNAL
18#include "png.h"
19
Guy Schalnate5a37791996-06-05 15:50:50 -050020static void png_default_error PNGARG((png_structp png_ptr,
21 png_const_charp message));
22static void png_default_warning PNGARG((png_structp png_ptr,
23 png_const_charp message));
24
Guy Schalnat69b14481996-01-10 02:56:49 -060025/* This function is called whenever there is a fatal error. This function
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060026 * should not be changed. If there is a need to handle errors differently,
27 * you should supply a replacement error function and use png_set_error_fn()
28 * to replace the error function at run-time.
29 */
Guy Schalnat6d764711995-12-19 03:22:19 -060030void
31png_error(png_structp png_ptr, png_const_charp message)
32{
Andreas Dilger47a0c421997-05-16 02:46:07 -050033 if (png_ptr->error_fn != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060034 (*(png_ptr->error_fn))(png_ptr, message);
Guy Schalnat6d764711995-12-19 03:22:19 -060035
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060036 /* if the following returns or doesn't exist, use the default function,
37 which will not return */
38 png_default_error(png_ptr, message);
Guy Schalnat6d764711995-12-19 03:22:19 -060039}
40
Guy Schalnat69b14481996-01-10 02:56:49 -060041/* This function is called whenever there is a non-fatal error. This function
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060042 * should not be changed. If there is a need to handle warnings differently,
43 * you should supply a replacement warning function and use
44 * png_set_error_fn() to replace the warning function at run-time.
45 */
Guy Schalnat6d764711995-12-19 03:22:19 -060046void
47png_warning(png_structp png_ptr, png_const_charp message)
48{
Andreas Dilger47a0c421997-05-16 02:46:07 -050049 if (png_ptr->warning_fn != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060050 (*(png_ptr->warning_fn))(png_ptr, message);
51 else
52 png_default_warning(png_ptr, message);
Guy Schalnat6d764711995-12-19 03:22:19 -060053}
54
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -060055/* These utilities are used internally to build an error message which relates
56 * to the current chunk. The chunk name comes from png_ptr->chunk_name,
57 * this is used to prefix the message. The message is limited in length
58 * to 63 bytes, the name characters are output as hex digits wrapped in []
59 * if the character is invalid.
60 */
61#define isnonalpha(c) ((c) < 41 || (c) > 122 || ((c) > 90 && (c) < 97))
Glenn Randers-Pehrson7cd899c1998-03-07 16:17:42 -060062static PNG_CONST char png_digit[16] = {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -060063 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
64};
65
66static void
67png_format_buffer(png_structp png_ptr, png_charp buffer, png_const_charp message)
68{
69 int iout = 0, iin = 0;
70
71 while (iin < 4) {
72 int c = png_ptr->chunk_name[iin++];
73 if (isnonalpha(c)) {
74 buffer[iout++] = '[';
75 buffer[iout++] = png_digit[(c & 0xf0) >> 4];
76 buffer[iout++] = png_digit[c & 0xf];
77 buffer[iout++] = ']';
78 } else {
79 buffer[iout++] = c;
80 }
81 }
82
83 if (message == NULL)
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -060084 buffer[iout] = 0;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -060085 else {
86 buffer[iout++] = ':';
87 buffer[iout++] = ' ';
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -060088 png_memcpy(buffer+iout, message, 64);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -060089 buffer[iout+63] = 0;
90 }
91}
92
93void
94png_chunk_error(png_structp png_ptr, png_const_charp message)
95{
96 char msg[16+64];
97 png_format_buffer(png_ptr, msg, message);
98 png_error(png_ptr, msg);
99}
100
101void
102png_chunk_warning(png_structp png_ptr, png_const_charp message)
103{
104 char msg[16+64];
105 png_format_buffer(png_ptr, msg, message);
106 png_warning(png_ptr, msg);
107}
108
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600109/* This is the default error handling function. Note that replacements for
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600110 * this function MUST NOT RETURN, or the program will likely crash. This
111 * function is used by default, or if the program supplies NULL for the
112 * error function pointer in png_set_error_fn().
113 */
Guy Schalnate5a37791996-06-05 15:50:50 -0500114static void
Guy Schalnat6d764711995-12-19 03:22:19 -0600115png_default_error(png_structp png_ptr, png_const_charp message)
116{
117#ifndef PNG_NO_STDIO
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600118 fprintf(stderr, "libpng error: %s\n", message);
Guy Schalnat6d764711995-12-19 03:22:19 -0600119#endif
120
121#ifdef USE_FAR_KEYWORD
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600122 {
123 jmp_buf jmpbuf;
124 png_memcpy(jmpbuf,png_ptr->jmpbuf,sizeof(jmp_buf));
125 longjmp(jmpbuf, 1);
126 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600127#else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600128 longjmp(png_ptr->jmpbuf, 1);
Guy Schalnat6d764711995-12-19 03:22:19 -0600129#endif
130}
131
Guy Schalnat69b14481996-01-10 02:56:49 -0600132/* This function is called when there is a warning, but the library thinks
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600133 * it can continue anyway. Replacement functions don't have to do anything
134 * here if you don't want to. In the default configuration, png_ptr is
135 * not used, but it is passed in case it may be useful.
136 */
Guy Schalnate5a37791996-06-05 15:50:50 -0500137static void
Guy Schalnat6d764711995-12-19 03:22:19 -0600138png_default_warning(png_structp png_ptr, png_const_charp message)
139{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500140 if (png_ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600141 return;
Guy Schalnat6d764711995-12-19 03:22:19 -0600142
143#ifndef PNG_NO_STDIO
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600144 fprintf(stderr, "libpng warning: %s\n", message);
Guy Schalnat6d764711995-12-19 03:22:19 -0600145#endif
146}
147
Guy Schalnat69b14481996-01-10 02:56:49 -0600148/* This function is called when the application wants to use another method
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600149 * of handling errors and warnings. Note that the error function MUST NOT
150 * return to the calling routine or serious problems will occur. The return
151 * method used in the default routine calls longjmp(png_ptr->jmpbuf, 1)
152 */
Guy Schalnat6d764711995-12-19 03:22:19 -0600153void
Guy Schalnate5a37791996-06-05 15:50:50 -0500154png_set_error_fn(png_structp png_ptr, png_voidp error_ptr,
155 png_error_ptr error_fn, png_error_ptr warning_fn)
Guy Schalnat6d764711995-12-19 03:22:19 -0600156{
Guy Schalnate5a37791996-06-05 15:50:50 -0500157 png_ptr->error_ptr = error_ptr;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600158 png_ptr->error_fn = error_fn;
159 png_ptr->warning_fn = warning_fn;
Guy Schalnat6d764711995-12-19 03:22:19 -0600160}
161
162
Guy Schalnate5a37791996-06-05 15:50:50 -0500163/* This function returns a pointer to the error_ptr associated with the user
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600164 * functions. The application should free any memory associated with this
165 * pointer before png_write_destroy and png_read_destroy are called.
166 */
Guy Schalnat6d764711995-12-19 03:22:19 -0600167png_voidp
Guy Schalnate5a37791996-06-05 15:50:50 -0500168png_get_error_ptr(png_structp png_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -0600169{
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600170 return ((png_voidp)png_ptr->error_ptr);
Guy Schalnat6d764711995-12-19 03:22:19 -0600171}
172
173
174