1 /* $XConsortium: jerror.c /main/2 1996/05/09 03:50:22 drk $ */
5 * Copyright (C) 1991-1996, Thomas G. Lane.
6 * This file is part of the Independent JPEG Group's software.
7 * For conditions of distribution and use, see the accompanying README file.
9 * This file contains simple error-reporting and trace-message routines.
10 * These are suitable for Unix-like systems and others where writing to
11 * stderr is the right thing to do. Many applications will want to replace
12 * some or all of these routines.
14 * These routines are used by both the compression and decompression code.
17 /* this is not a core library module, so it doesn't define JPEG_INTERNALS */
23 #ifndef EXIT_FAILURE /* define exit() codes if not provided */
24 #define EXIT_FAILURE 1
29 * Create the message string table.
30 * We do this from the master message list in jerror.h by re-reading
31 * jerror.h with a suitable definition for macro JMESSAGE.
32 * The message table is made an external symbol just in case any applications
33 * want to refer to it directly.
36 #ifdef NEED_SHORT_EXTERNAL_NAMES
37 #define jpeg_std_message_table jMsgTable
40 #define JMESSAGE(code,string) string ,
42 const char * const jpeg_std_message_table[] = {
49 * Error exit handler: must not return to caller.
51 * Applications may override this if they want to get control back after
52 * an error. Typically one would longjmp somewhere instead of exiting.
53 * The setjmp buffer can be made a private field within an expanded error
54 * handler object. Note that the info needed to generate an error message
55 * is stored in the error object, so you can generate the message now or
56 * later, at your convenience.
57 * You should make sure that the JPEG object is cleaned up (with jpeg_abort
58 * or jpeg_destroy) at some point.
62 error_exit (j_common_ptr cinfo)
64 /* Always display the message */
65 (*cinfo->err->output_message) (cinfo);
67 /* Let the memory manager delete any temp files before we die */
75 * Actual output of an error or trace message.
76 * Applications may override this method to send JPEG messages somewhere
81 output_message (j_common_ptr cinfo)
83 char buffer[JMSG_LENGTH_MAX];
85 /* Create the message */
86 (*cinfo->err->format_message) (cinfo, buffer);
88 /* Send it to stderr, adding a newline */
89 fprintf(stderr, "%s\n", buffer);
94 * Decide whether to emit a trace or warning message.
95 * msg_level is one of:
96 * -1: recoverable corrupt-data warning, may want to abort.
97 * 0: important advisory messages (always display to user).
98 * 1: first level of tracing detail.
99 * 2,3,...: successively more detailed tracing messages.
100 * An application might override this method if it wanted to abort on warnings
101 * or change the policy about which messages to display.
105 emit_message (j_common_ptr cinfo, int msg_level)
107 struct jpeg_error_mgr * err = cinfo->err;
110 /* It's a warning message. Since corrupt files may generate many warnings,
111 * the policy implemented here is to show only the first warning,
112 * unless trace_level >= 3.
114 if (err->num_warnings == 0 || err->trace_level >= 3)
115 (*err->output_message) (cinfo);
116 /* Always count warnings in num_warnings. */
119 /* It's a trace message. Show it if trace_level >= msg_level. */
120 if (err->trace_level >= msg_level)
121 (*err->output_message) (cinfo);
127 * Format a message string for the most recent JPEG error or message.
128 * The message is stored into buffer, which should be at least JMSG_LENGTH_MAX
129 * characters. Note that no '\n' character is added to the string.
130 * Few applications should need to override this method.
134 format_message (j_common_ptr cinfo, char * buffer)
136 struct jpeg_error_mgr * err = cinfo->err;
137 int msg_code = err->msg_code;
138 const char * msgtext = NULL;
143 /* Look up message string in proper table */
144 if (msg_code > 0 && msg_code <= err->last_jpeg_message) {
145 msgtext = err->jpeg_message_table[msg_code];
146 } else if (err->addon_message_table != NULL &&
147 msg_code >= err->first_addon_message &&
148 msg_code <= err->last_addon_message) {
149 msgtext = err->addon_message_table[msg_code - err->first_addon_message];
152 /* Defend against bogus message number */
153 if (msgtext == NULL) {
154 err->msg_parm.i[0] = msg_code;
155 msgtext = err->jpeg_message_table[0];
158 /* Check for string parameter, as indicated by %s in the message text */
161 while ((ch = *msgptr++) != '\0') {
163 if (*msgptr == 's') isstring = TRUE;
168 /* Format the message into the passed buffer */
170 sprintf(buffer, msgtext, err->msg_parm.s);
172 sprintf(buffer, msgtext,
173 err->msg_parm.i[0], err->msg_parm.i[1],
174 err->msg_parm.i[2], err->msg_parm.i[3],
175 err->msg_parm.i[4], err->msg_parm.i[5],
176 err->msg_parm.i[6], err->msg_parm.i[7]);
181 * Reset error state variables at start of a new image.
182 * This is called during compression startup to reset trace/error
183 * processing to default state, without losing any application-specific
184 * method pointers. An application might possibly want to override
185 * this method if it has additional error processing state.
189 reset_error_mgr (j_common_ptr cinfo)
191 cinfo->err->num_warnings = 0;
192 /* trace_level is not reset since it is an application-supplied parameter */
193 cinfo->err->msg_code = 0; /* may be useful as a flag for "no error" */
198 * Fill in the standard error-handling methods in a jpeg_error_mgr object.
200 * struct jpeg_compress_struct cinfo;
201 * struct jpeg_error_mgr err;
203 * cinfo.err = jpeg_std_error(&err);
204 * after which the application may override some of the methods.
207 GLOBAL(struct jpeg_error_mgr *)
208 jpeg_std_error (struct jpeg_error_mgr * err)
210 err->error_exit = error_exit;
211 err->emit_message = emit_message;
212 err->output_message = output_message;
213 err->format_message = format_message;
214 err->reset_error_mgr = reset_error_mgr;
216 err->trace_level = 0; /* default = no tracing */
217 err->num_warnings = 0; /* no warnings emitted yet */
218 err->msg_code = 0; /* may be useful as a flag for "no error" */
220 /* Initialize message table pointers */
221 err->jpeg_message_table = jpeg_std_message_table;
222 err->last_jpeg_message = (int) JMSG_LASTMSGCODE - 1;
224 err->addon_message_table = NULL;
225 err->first_addon_message = 0; /* for safety */
226 err->last_addon_message = 0;