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