rev |
line source |
nuclear@26
|
1 /*
|
nuclear@26
|
2 * jerror.c
|
nuclear@26
|
3 *
|
nuclear@26
|
4 * Copyright (C) 1991-1998, Thomas G. Lane.
|
nuclear@26
|
5 * This file is part of the Independent JPEG Group's software.
|
nuclear@26
|
6 * For conditions of distribution and use, see the accompanying README file.
|
nuclear@26
|
7 *
|
nuclear@26
|
8 * This file contains simple error-reporting and trace-message routines.
|
nuclear@26
|
9 * These are suitable for Unix-like systems and others where writing to
|
nuclear@26
|
10 * stderr is the right thing to do. Many applications will want to replace
|
nuclear@26
|
11 * some or all of these routines.
|
nuclear@26
|
12 *
|
nuclear@26
|
13 * If you define USE_WINDOWS_MESSAGEBOX in jconfig.h or in the makefile,
|
nuclear@26
|
14 * you get a Windows-specific hack to display error messages in a dialog box.
|
nuclear@26
|
15 * It ain't much, but it beats dropping error messages into the bit bucket,
|
nuclear@26
|
16 * which is what happens to output to stderr under most Windows C compilers.
|
nuclear@26
|
17 *
|
nuclear@26
|
18 * These routines are used by both the compression and decompression code.
|
nuclear@26
|
19 */
|
nuclear@26
|
20
|
nuclear@26
|
21 /* this is not a core library module, so it doesn't define JPEG_INTERNALS */
|
nuclear@26
|
22 #include "jinclude.h"
|
nuclear@26
|
23 #include "jpeglib.h"
|
nuclear@26
|
24 #include "jversion.h"
|
nuclear@26
|
25 #include "jerror.h"
|
nuclear@26
|
26
|
nuclear@26
|
27 #ifdef USE_WINDOWS_MESSAGEBOX
|
nuclear@26
|
28 #include <windows.h>
|
nuclear@26
|
29 #endif
|
nuclear@26
|
30
|
nuclear@26
|
31 #ifndef EXIT_FAILURE /* define exit() codes if not provided */
|
nuclear@26
|
32 #define EXIT_FAILURE 1
|
nuclear@26
|
33 #endif
|
nuclear@26
|
34
|
nuclear@26
|
35
|
nuclear@26
|
36 /*
|
nuclear@26
|
37 * Create the message string table.
|
nuclear@26
|
38 * We do this from the master message list in jerror.h by re-reading
|
nuclear@26
|
39 * jerror.h with a suitable definition for macro JMESSAGE.
|
nuclear@26
|
40 * The message table is made an external symbol just in case any applications
|
nuclear@26
|
41 * want to refer to it directly.
|
nuclear@26
|
42 */
|
nuclear@26
|
43
|
nuclear@26
|
44 #ifdef NEED_SHORT_EXTERNAL_NAMES
|
nuclear@26
|
45 #define jpeg_std_message_table jMsgTable
|
nuclear@26
|
46 #endif
|
nuclear@26
|
47
|
nuclear@26
|
48 #define JMESSAGE(code,string) string ,
|
nuclear@26
|
49
|
nuclear@26
|
50 const char * const jpeg_std_message_table[] = {
|
nuclear@26
|
51 #include "jerror.h"
|
nuclear@26
|
52 NULL
|
nuclear@26
|
53 };
|
nuclear@26
|
54
|
nuclear@26
|
55
|
nuclear@26
|
56 /*
|
nuclear@26
|
57 * Error exit handler: must not return to caller.
|
nuclear@26
|
58 *
|
nuclear@26
|
59 * Applications may override this if they want to get control back after
|
nuclear@26
|
60 * an error. Typically one would longjmp somewhere instead of exiting.
|
nuclear@26
|
61 * The setjmp buffer can be made a private field within an expanded error
|
nuclear@26
|
62 * handler object. Note that the info needed to generate an error message
|
nuclear@26
|
63 * is stored in the error object, so you can generate the message now or
|
nuclear@26
|
64 * later, at your convenience.
|
nuclear@26
|
65 * You should make sure that the JPEG object is cleaned up (with jpeg_abort
|
nuclear@26
|
66 * or jpeg_destroy) at some point.
|
nuclear@26
|
67 */
|
nuclear@26
|
68
|
nuclear@26
|
69 METHODDEF(void)
|
nuclear@26
|
70 error_exit (j_common_ptr cinfo)
|
nuclear@26
|
71 {
|
nuclear@26
|
72 /* Always display the message */
|
nuclear@26
|
73 (*cinfo->err->output_message) (cinfo);
|
nuclear@26
|
74
|
nuclear@26
|
75 /* Let the memory manager delete any temp files before we die */
|
nuclear@26
|
76 jpeg_destroy(cinfo);
|
nuclear@26
|
77
|
nuclear@26
|
78 exit(EXIT_FAILURE);
|
nuclear@26
|
79 }
|
nuclear@26
|
80
|
nuclear@26
|
81
|
nuclear@26
|
82 /*
|
nuclear@26
|
83 * Actual output of an error or trace message.
|
nuclear@26
|
84 * Applications may override this method to send JPEG messages somewhere
|
nuclear@26
|
85 * other than stderr.
|
nuclear@26
|
86 *
|
nuclear@26
|
87 * On Windows, printing to stderr is generally completely useless,
|
nuclear@26
|
88 * so we provide optional code to produce an error-dialog popup.
|
nuclear@26
|
89 * Most Windows applications will still prefer to override this routine,
|
nuclear@26
|
90 * but if they don't, it'll do something at least marginally useful.
|
nuclear@26
|
91 *
|
nuclear@26
|
92 * NOTE: to use the library in an environment that doesn't support the
|
nuclear@26
|
93 * C stdio library, you may have to delete the call to fprintf() entirely,
|
nuclear@26
|
94 * not just not use this routine.
|
nuclear@26
|
95 */
|
nuclear@26
|
96
|
nuclear@26
|
97 METHODDEF(void)
|
nuclear@26
|
98 output_message (j_common_ptr cinfo)
|
nuclear@26
|
99 {
|
nuclear@26
|
100 char buffer[JMSG_LENGTH_MAX];
|
nuclear@26
|
101
|
nuclear@26
|
102 /* Create the message */
|
nuclear@26
|
103 (*cinfo->err->format_message) (cinfo, buffer);
|
nuclear@26
|
104
|
nuclear@26
|
105 #ifdef USE_WINDOWS_MESSAGEBOX
|
nuclear@26
|
106 /* Display it in a message dialog box */
|
nuclear@26
|
107 MessageBox(GetActiveWindow(), buffer, "JPEG Library Error",
|
nuclear@26
|
108 MB_OK | MB_ICONERROR);
|
nuclear@26
|
109 #else
|
nuclear@26
|
110 /* Send it to stderr, adding a newline */
|
nuclear@26
|
111 fprintf(stderr, "%s\n", buffer);
|
nuclear@26
|
112 #endif
|
nuclear@26
|
113 }
|
nuclear@26
|
114
|
nuclear@26
|
115
|
nuclear@26
|
116 /*
|
nuclear@26
|
117 * Decide whether to emit a trace or warning message.
|
nuclear@26
|
118 * msg_level is one of:
|
nuclear@26
|
119 * -1: recoverable corrupt-data warning, may want to abort.
|
nuclear@26
|
120 * 0: important advisory messages (always display to user).
|
nuclear@26
|
121 * 1: first level of tracing detail.
|
nuclear@26
|
122 * 2,3,...: successively more detailed tracing messages.
|
nuclear@26
|
123 * An application might override this method if it wanted to abort on warnings
|
nuclear@26
|
124 * or change the policy about which messages to display.
|
nuclear@26
|
125 */
|
nuclear@26
|
126
|
nuclear@26
|
127 METHODDEF(void)
|
nuclear@26
|
128 emit_message (j_common_ptr cinfo, int msg_level)
|
nuclear@26
|
129 {
|
nuclear@26
|
130 struct jpeg_error_mgr * err = cinfo->err;
|
nuclear@26
|
131
|
nuclear@26
|
132 if (msg_level < 0) {
|
nuclear@26
|
133 /* It's a warning message. Since corrupt files may generate many warnings,
|
nuclear@26
|
134 * the policy implemented here is to show only the first warning,
|
nuclear@26
|
135 * unless trace_level >= 3.
|
nuclear@26
|
136 */
|
nuclear@26
|
137 if (err->num_warnings == 0 || err->trace_level >= 3)
|
nuclear@26
|
138 (*err->output_message) (cinfo);
|
nuclear@26
|
139 /* Always count warnings in num_warnings. */
|
nuclear@26
|
140 err->num_warnings++;
|
nuclear@26
|
141 } else {
|
nuclear@26
|
142 /* It's a trace message. Show it if trace_level >= msg_level. */
|
nuclear@26
|
143 if (err->trace_level >= msg_level)
|
nuclear@26
|
144 (*err->output_message) (cinfo);
|
nuclear@26
|
145 }
|
nuclear@26
|
146 }
|
nuclear@26
|
147
|
nuclear@26
|
148
|
nuclear@26
|
149 /*
|
nuclear@26
|
150 * Format a message string for the most recent JPEG error or message.
|
nuclear@26
|
151 * The message is stored into buffer, which should be at least JMSG_LENGTH_MAX
|
nuclear@26
|
152 * characters. Note that no '\n' character is added to the string.
|
nuclear@26
|
153 * Few applications should need to override this method.
|
nuclear@26
|
154 */
|
nuclear@26
|
155
|
nuclear@26
|
156 METHODDEF(void)
|
nuclear@26
|
157 format_message (j_common_ptr cinfo, char * buffer)
|
nuclear@26
|
158 {
|
nuclear@26
|
159 struct jpeg_error_mgr * err = cinfo->err;
|
nuclear@26
|
160 int msg_code = err->msg_code;
|
nuclear@26
|
161 const char * msgtext = NULL;
|
nuclear@26
|
162 const char * msgptr;
|
nuclear@26
|
163 char ch;
|
nuclear@26
|
164 boolean isstring;
|
nuclear@26
|
165
|
nuclear@26
|
166 /* Look up message string in proper table */
|
nuclear@26
|
167 if (msg_code > 0 && msg_code <= err->last_jpeg_message) {
|
nuclear@26
|
168 msgtext = err->jpeg_message_table[msg_code];
|
nuclear@26
|
169 } else if (err->addon_message_table != NULL &&
|
nuclear@26
|
170 msg_code >= err->first_addon_message &&
|
nuclear@26
|
171 msg_code <= err->last_addon_message) {
|
nuclear@26
|
172 msgtext = err->addon_message_table[msg_code - err->first_addon_message];
|
nuclear@26
|
173 }
|
nuclear@26
|
174
|
nuclear@26
|
175 /* Defend against bogus message number */
|
nuclear@26
|
176 if (msgtext == NULL) {
|
nuclear@26
|
177 err->msg_parm.i[0] = msg_code;
|
nuclear@26
|
178 msgtext = err->jpeg_message_table[0];
|
nuclear@26
|
179 }
|
nuclear@26
|
180
|
nuclear@26
|
181 /* Check for string parameter, as indicated by %s in the message text */
|
nuclear@26
|
182 isstring = FALSE;
|
nuclear@26
|
183 msgptr = msgtext;
|
nuclear@26
|
184 while ((ch = *msgptr++) != '\0') {
|
nuclear@26
|
185 if (ch == '%') {
|
nuclear@26
|
186 if (*msgptr == 's') isstring = TRUE;
|
nuclear@26
|
187 break;
|
nuclear@26
|
188 }
|
nuclear@26
|
189 }
|
nuclear@26
|
190
|
nuclear@26
|
191 /* Format the message into the passed buffer */
|
nuclear@26
|
192 if (isstring)
|
nuclear@26
|
193 sprintf(buffer, msgtext, err->msg_parm.s);
|
nuclear@26
|
194 else
|
nuclear@26
|
195 sprintf(buffer, msgtext,
|
nuclear@26
|
196 err->msg_parm.i[0], err->msg_parm.i[1],
|
nuclear@26
|
197 err->msg_parm.i[2], err->msg_parm.i[3],
|
nuclear@26
|
198 err->msg_parm.i[4], err->msg_parm.i[5],
|
nuclear@26
|
199 err->msg_parm.i[6], err->msg_parm.i[7]);
|
nuclear@26
|
200 }
|
nuclear@26
|
201
|
nuclear@26
|
202
|
nuclear@26
|
203 /*
|
nuclear@26
|
204 * Reset error state variables at start of a new image.
|
nuclear@26
|
205 * This is called during compression startup to reset trace/error
|
nuclear@26
|
206 * processing to default state, without losing any application-specific
|
nuclear@26
|
207 * method pointers. An application might possibly want to override
|
nuclear@26
|
208 * this method if it has additional error processing state.
|
nuclear@26
|
209 */
|
nuclear@26
|
210
|
nuclear@26
|
211 METHODDEF(void)
|
nuclear@26
|
212 reset_error_mgr (j_common_ptr cinfo)
|
nuclear@26
|
213 {
|
nuclear@26
|
214 cinfo->err->num_warnings = 0;
|
nuclear@26
|
215 /* trace_level is not reset since it is an application-supplied parameter */
|
nuclear@26
|
216 cinfo->err->msg_code = 0; /* may be useful as a flag for "no error" */
|
nuclear@26
|
217 }
|
nuclear@26
|
218
|
nuclear@26
|
219
|
nuclear@26
|
220 /*
|
nuclear@26
|
221 * Fill in the standard error-handling methods in a jpeg_error_mgr object.
|
nuclear@26
|
222 * Typical call is:
|
nuclear@26
|
223 * struct jpeg_compress_struct cinfo;
|
nuclear@26
|
224 * struct jpeg_error_mgr err;
|
nuclear@26
|
225 *
|
nuclear@26
|
226 * cinfo.err = jpeg_std_error(&err);
|
nuclear@26
|
227 * after which the application may override some of the methods.
|
nuclear@26
|
228 */
|
nuclear@26
|
229
|
nuclear@26
|
230 GLOBAL(struct jpeg_error_mgr *)
|
nuclear@26
|
231 jpeg_std_error (struct jpeg_error_mgr * err)
|
nuclear@26
|
232 {
|
nuclear@26
|
233 err->error_exit = error_exit;
|
nuclear@26
|
234 err->emit_message = emit_message;
|
nuclear@26
|
235 err->output_message = output_message;
|
nuclear@26
|
236 err->format_message = format_message;
|
nuclear@26
|
237 err->reset_error_mgr = reset_error_mgr;
|
nuclear@26
|
238
|
nuclear@26
|
239 err->trace_level = 0; /* default = no tracing */
|
nuclear@26
|
240 err->num_warnings = 0; /* no warnings emitted yet */
|
nuclear@26
|
241 err->msg_code = 0; /* may be useful as a flag for "no error" */
|
nuclear@26
|
242
|
nuclear@26
|
243 /* Initialize message table pointers */
|
nuclear@26
|
244 err->jpeg_message_table = jpeg_std_message_table;
|
nuclear@26
|
245 err->last_jpeg_message = (int) JMSG_LASTMSGCODE - 1;
|
nuclear@26
|
246
|
nuclear@26
|
247 err->addon_message_table = NULL;
|
nuclear@26
|
248 err->first_addon_message = 0; /* for safety */
|
nuclear@26
|
249 err->last_addon_message = 0;
|
nuclear@26
|
250
|
nuclear@26
|
251 return err;
|
nuclear@26
|
252 }
|