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