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