1/*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5/*
6 * jerror.c
7 *
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.
11 *
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.
16 *
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.
21 *
22 * These routines are used by both the compression and decompression code.
23 */
24
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"
30
31#ifdef USE_WINDOWS_MESSAGEBOX
32#include <windows.h>
33#endif
34
35#ifndef EXIT_FAILURE            /* define exit() codes if not provided */
36#define EXIT_FAILURE  1
37#endif
38
39
40/*
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.
46 */
47
48#ifdef NEED_SHORT_EXTERNAL_NAMES
49#define jpeg_std_message_table  jMsgTable
50#endif
51
52#define JMESSAGE(code,string)   string ,
53
54const char * const jpeg_std_message_table[] = {
55#include "jerror.h"
56  NULL
57};
58
59
60/*
61 * Error exit handler: must not return to caller.
62 *
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.
71 */
72
73METHODDEF(void)
74error_exit (j_common_ptr cinfo)
75{
76  /* Always display the message */
77  (*cinfo->err->output_message) (cinfo);
78
79  /* Let the memory manager delete any temp files before we die */
80  jpeg_destroy(cinfo);
81
82  /*
83   * This should never happen since the Java library replaces the
84   * error_exit pointer in the error handler structs it uses.
85   *
86   * exit(EXIT_FAILURE);
87   */
88}
89
90
91/*
92 * Actual output of an error or trace message.
93 * Applications may override this method to send JPEG messages somewhere
94 * other than stderr.
95 *
96 * On Windows, printing to stderr is generally completely useless,
97 * so we provide optional code to produce an error-dialog popup.
98 * Most Windows applications will still prefer to override this routine,
99 * but if they don't, it'll do something at least marginally useful.
100 *
101 * NOTE: to use the library in an environment that doesn't support the
102 * C stdio library, you may have to delete the call to fprintf() entirely,
103 * not just not use this routine.
104 */
105
106METHODDEF(void)
107output_message (j_common_ptr cinfo)
108{
109  char buffer[JMSG_LENGTH_MAX];
110
111  /* Create the message */
112  (*cinfo->err->format_message) (cinfo, buffer);
113
114#ifdef USE_WINDOWS_MESSAGEBOX
115  /* Display it in a message dialog box */
116  MessageBox(GetActiveWindow(), buffer, "JPEG Library Error",
117             MB_OK | MB_ICONERROR);
118#else
119  /* Send it to stderr, adding a newline */
120  fprintf(stderr, "%s\n", buffer);
121#endif
122}
123
124
125/*
126 * Decide whether to emit a trace or warning message.
127 * msg_level is one of:
128 *   -1: recoverable corrupt-data warning, may want to abort.
129 *    0: important advisory messages (always display to user).
130 *    1: first level of tracing detail.
131 *    2,3,...: successively more detailed tracing messages.
132 * An application might override this method if it wanted to abort on warnings
133 * or change the policy about which messages to display.
134 */
135
136METHODDEF(void)
137emit_message (j_common_ptr cinfo, int msg_level)
138{
139  struct jpeg_error_mgr * err = cinfo->err;
140
141  if (msg_level < 0) {
142    /* It's a warning message.  Since corrupt files may generate many warnings,
143     * the policy implemented here is to show only the first warning,
144     * unless trace_level >= 3.
145     */
146    if (err->num_warnings == 0 || err->trace_level >= 3)
147      (*err->output_message) (cinfo);
148    /* Always count warnings in num_warnings. */
149    err->num_warnings++;
150  } else {
151    /* It's a trace message.  Show it if trace_level >= msg_level. */
152    if (err->trace_level >= msg_level)
153      (*err->output_message) (cinfo);
154  }
155}
156
157
158/*
159 * Format a message string for the most recent JPEG error or message.
160 * The message is stored into buffer, which should be at least JMSG_LENGTH_MAX
161 * characters.  Note that no '\n' character is added to the string.
162 * Few applications should need to override this method.
163 */
164
165METHODDEF(void)
166format_message (j_common_ptr cinfo, char * buffer)
167{
168
169/* Had to kill this function altogether
170   to avoid linking to VM when building the splash screen with static libjpeg */
171
172#ifndef SPLASHSCREEN
173  int jio_snprintf(char *str, size_t count, const char *fmt, ...);
174  struct jpeg_error_mgr * err = cinfo->err;
175  int msg_code = err->msg_code;
176  const char * msgtext = NULL;
177  const char * msgptr;
178  char ch;
179  boolean isstring;
180
181  /* Look up message string in proper table */
182  if (msg_code > 0 && msg_code <= err->last_jpeg_message) {
183    msgtext = err->jpeg_message_table[msg_code];
184  } else if (err->addon_message_table != NULL &&
185             msg_code >= err->first_addon_message &&
186             msg_code <= err->last_addon_message) {
187    msgtext = err->addon_message_table[msg_code - err->first_addon_message];
188  }
189
190  /* Defend against bogus message number */
191  if (msgtext == NULL) {
192    err->msg_parm.i[0] = msg_code;
193    msgtext = err->jpeg_message_table[0];
194  }
195
196  /* Check for string parameter, as indicated by %s in the message text */
197  isstring = FALSE;
198  msgptr = msgtext;
199  while ((ch = *msgptr++) != '\0') {
200    if (ch == '%') {
201      if (*msgptr == 's') isstring = TRUE;
202      break;
203    }
204  }
205
206  /* Format the message into the passed buffer */
207  if (isstring)
208    /* Buffer size is JMSG_LENGTH_MAX, quietly truncate on overflow */
209    (void) jio_snprintf(buffer, JMSG_LENGTH_MAX, msgtext, err->msg_parm.s);
210  else
211    /* Buffer size is JMSG_LENGTH_MAX, quietly truncate on overflow */
212    (void) jio_snprintf(buffer, JMSG_LENGTH_MAX, msgtext,
213                        err->msg_parm.i[0], err->msg_parm.i[1],
214                        err->msg_parm.i[2], err->msg_parm.i[3],
215                        err->msg_parm.i[4], err->msg_parm.i[5],
216                        err->msg_parm.i[6], err->msg_parm.i[7]);
217#else /* SPLASHSCREEN */
218        *buffer = '\0';
219#endif /* SPLASHSCREEN */
220}
221
222
223/*
224 * Reset error state variables at start of a new image.
225 * This is called during compression startup to reset trace/error
226 * processing to default state, without losing any application-specific
227 * method pointers.  An application might possibly want to override
228 * this method if it has additional error processing state.
229 */
230
231METHODDEF(void)
232reset_error_mgr (j_common_ptr cinfo)
233{
234  cinfo->err->num_warnings = 0;
235  /* trace_level is not reset since it is an application-supplied parameter */
236  cinfo->err->msg_code = 0;     /* may be useful as a flag for "no error" */
237}
238
239
240/*
241 * Fill in the standard error-handling methods in a jpeg_error_mgr object.
242 * Typical call is:
243 *      struct jpeg_compress_struct cinfo;
244 *      struct jpeg_error_mgr err;
245 *
246 *      cinfo.err = jpeg_std_error(&err);
247 * after which the application may override some of the methods.
248 */
249
250GLOBAL(struct jpeg_error_mgr *)
251jpeg_std_error (struct jpeg_error_mgr * err)
252{
253  err->error_exit = error_exit;
254  err->emit_message = emit_message;
255  err->output_message = output_message;
256  err->format_message = format_message;
257  err->reset_error_mgr = reset_error_mgr;
258
259  err->trace_level = 0;         /* default = no tracing */
260  err->num_warnings = 0;        /* no warnings emitted yet */
261  err->msg_code = 0;            /* may be useful as a flag for "no error" */
262
263  /* Initialize message table pointers */
264  err->jpeg_message_table = jpeg_std_message_table;
265  err->last_jpeg_message = (int) JMSG_LASTMSGCODE - 1;
266
267  err->addon_message_table = NULL;
268  err->first_addon_message = 0; /* for safety */
269  err->last_addon_message = 0;
270
271  return err;
272}
273