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