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