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