blob: e479fc3ac90d07dd0c006dbac179c09c227f22a2 [file] [log] [blame]
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00001/*
2 * jerror.c
3 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00004 * Copyright (C) 1991-1994, Thomas G. Lane.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00005 * This file is part of the Independent JPEG Group's software.
6 * For conditions of distribution and use, see the accompanying README file.
7 *
8 * This file contains simple error-reporting and trace-message routines.
9 * These are suitable for Unix-like systems and others where writing to
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000010 * stderr is the right thing to do. Many applications will want to replace
11 * some or all of these routines.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000012 *
13 * These routines are used by both the compression and decompression code.
14 */
15
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000016/* this is not a core library module, so it doesn't define JPEG_INTERNALS */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000017#include "jinclude.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000018#include "jpeglib.h"
19#include "jversion.h"
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000020#include "jerror.h"
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000021
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000022#ifndef EXIT_FAILURE /* define exit() codes if not provided */
23#define EXIT_FAILURE 1
24#endif
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000025
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000026
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000027/*
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000028 * Create the message string table.
29 * We do this from the master message list in jerror.h by re-reading
30 * jerror.h with a suitable definition for macro JMESSAGE.
31 * The message table is made an external symbol just in case any applications
32 * want to refer to it directly.
33 */
34
35#ifdef NEED_SHORT_EXTERNAL_NAMES
36#define jpeg_std_message_table jMsgTable
37#endif
38
39#define JMESSAGE(code,string) string ,
40
41const char * const jpeg_std_message_table[] = {
42#include "jerror.h"
43 NULL
44};
45
46
47/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000048 * Error exit handler: must not return to caller.
49 *
50 * Applications may override this if they want to get control back after
51 * an error. Typically one would longjmp somewhere instead of exiting.
52 * The setjmp buffer can be made a private field within an expanded error
53 * handler object. Note that the info needed to generate an error message
54 * is stored in the error object, so you can generate the message now or
55 * later, at your convenience.
56 * You should make sure that the JPEG object is cleaned up (with jpeg_abort
57 * or jpeg_destroy) at some point.
58 */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000059
60METHODDEF void
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000061error_exit (j_common_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000062{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000063 /* Always display the message */
64 (*cinfo->err->output_message) (cinfo);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000065
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000066 /* Let the memory manager delete any temp files before we die */
67 jpeg_destroy(cinfo);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000068
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000069 exit(EXIT_FAILURE);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000070}
71
72
73/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000074 * Actual output of an error or trace message.
75 * Applications may override this method to send JPEG messages somewhere
76 * other than stderr.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000077 */
78
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000079METHODDEF void
80output_message (j_common_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000081{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000082 char buffer[JMSG_LENGTH_MAX];
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000083
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000084 /* Create the message */
85 (*cinfo->err->format_message) (cinfo, buffer);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000086
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000087 /* Send it to stderr, adding a newline */
88 fprintf(stderr, "%s\n", buffer);
89}
Thomas G. Lane88aeed41992-12-10 00:00:00 +000090
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000091
92/*
93 * Decide whether to emit a trace or warning message.
94 * msg_level is one of:
95 * -1: recoverable corrupt-data warning, may want to abort.
96 * 0: important advisory messages (always display to user).
97 * 1: first level of tracing detail.
98 * 2,3,...: successively more detailed tracing messages.
99 * An application might override this method if it wanted to abort on warnings
100 * or change the policy about which messages to display.
101 */
102
103METHODDEF void
104emit_message (j_common_ptr cinfo, int msg_level)
105{
106 struct jpeg_error_mgr * err = cinfo->err;
107
108 if (msg_level < 0) {
109 /* It's a warning message. Since corrupt files may generate many warnings,
110 * the policy implemented here is to show only the first warning,
111 * unless trace_level >= 3.
112 */
113 if (err->num_warnings == 0 || err->trace_level >= 3)
114 (*err->output_message) (cinfo);
115 /* Always count warnings in num_warnings. */
116 err->num_warnings++;
117 } else {
118 /* It's a trace message. Show it if trace_level >= msg_level. */
119 if (err->trace_level >= msg_level)
120 (*err->output_message) (cinfo);
121 }
122}
123
124
125/*
126 * Format a message string for the most recent JPEG error or message.
127 * The message is stored into buffer, which should be at least JMSG_LENGTH_MAX
128 * characters. Note that no '\n' character is added to the string.
129 * Few applications should need to override this method.
130 */
131
132METHODDEF void
133format_message (j_common_ptr cinfo, char * buffer)
134{
135 struct jpeg_error_mgr * err = cinfo->err;
136 int msg_code = err->msg_code;
137 const char * msgtext = NULL;
138 const char * msgptr;
139 char ch;
140 boolean isstring;
141
142 /* Look up message string in proper table */
143 if (msg_code > 0 && msg_code <= err->last_jpeg_message) {
144 msgtext = err->jpeg_message_table[msg_code];
145 } else if (err->addon_message_table != NULL &&
146 msg_code >= err->first_addon_message &&
147 msg_code <= err->last_addon_message) {
148 msgtext = err->addon_message_table[msg_code - err->first_addon_message];
149 }
150
151 /* Defend against bogus message number */
152 if (msgtext == NULL) {
153 err->msg_parm.i[0] = msg_code;
154 msgtext = err->jpeg_message_table[0];
155 }
156
157 /* Check for string parameter, as indicated by %s in the message text */
158 isstring = FALSE;
159 msgptr = msgtext;
160 while ((ch = *msgptr++) != '\0') {
161 if (ch == '%') {
162 if (*msgptr == 's') isstring = TRUE;
163 break;
164 }
165 }
166
167 /* Format the message into the passed buffer */
168 if (isstring)
169 sprintf(buffer, msgtext, err->msg_parm.s);
170 else
171 sprintf(buffer, msgtext,
172 err->msg_parm.i[0], err->msg_parm.i[1],
173 err->msg_parm.i[2], err->msg_parm.i[3],
174 err->msg_parm.i[4], err->msg_parm.i[5],
175 err->msg_parm.i[6], err->msg_parm.i[7]);
176}
177
178
179/*
180 * Reset error state variables at start of a new image.
181 * This is called during compression startup to reset trace/error
182 * processing to default state, without losing any application-specific
183 * method pointers. An application might possibly want to override
184 * this method if it has additional error processing state.
185 */
186
187METHODDEF void
188reset_error_mgr (j_common_ptr cinfo)
189{
190 cinfo->err->num_warnings = 0;
191 /* trace_level is not reset since it is an application-supplied parameter */
192 cinfo->err->msg_code = 0; /* may be useful as a flag for "no error" */
193}
194
195
196/*
197 * Fill in the standard error-handling methods in a jpeg_error_mgr object.
198 * Typical call is:
199 * struct jpeg_compress_struct cinfo;
200 * struct jpeg_error_mgr err;
201 *
202 * cinfo.err = jpeg_std_error(&err);
203 * after which the application may override some of the methods.
204 */
205
206GLOBAL struct jpeg_error_mgr *
207jpeg_std_error (struct jpeg_error_mgr * err)
208{
209 err->error_exit = error_exit;
210 err->emit_message = emit_message;
211 err->output_message = output_message;
212 err->format_message = format_message;
213 err->reset_error_mgr = reset_error_mgr;
214
215 err->trace_level = 0; /* default = no tracing */
216 err->num_warnings = 0; /* no warnings emitted yet */
217 err->msg_code = 0; /* may be useful as a flag for "no error" */
218
219 /* Initialize message table pointers */
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000220 err->jpeg_message_table = jpeg_std_message_table;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000221 err->last_jpeg_message = (int) JMSG_LASTMSGCODE - 1;
222
223 err->addon_message_table = NULL;
224 err->first_addon_message = 0; /* for safety */
225 err->last_addon_message = 0;
226
227 return err;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000228}