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