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