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