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