Coverage Report

Created: 2025-06-13 06:29

/src/gdal/build/frmts/jpeg/libjpeg12/jerror12.c
Line
Count
Source (jump to first uncovered line)
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
#include "jinclude.h"
23
#include "jpeglib.h"
24
#include "jversion.h"
25
#include "jerror.h"
26
27
#ifdef USE_WINDOWS_MESSAGEBOX
28
#include <windows.h>
29
#endif
30
31
#ifndef EXIT_FAILURE    /* define exit() codes if not provided */
32
#define EXIT_FAILURE  1
33
#endif
34
35
36
/*
37
 * Create the message string table.
38
 * We do this from the master message list in jerror.h by re-reading
39
 * jerror.h with a suitable definition for macro JMESSAGE.
40
 * The message table is made an external symbol just in case any applications
41
 * want to refer to it directly.
42
 */
43
44
#ifdef NEED_SHORT_EXTERNAL_NAMES
45
#define jpeg_std_message_table  jMsgTable
46
#endif
47
48
#ifdef NEED_12_BIT_NAMES
49
0
#define jpeg_std_message_table  jpeg_std_message_table_12
50
#elif defined(RENAME_INTERNAL_LIBJPEG_SYMBOLS)
51
#define jpeg_std_message_table  gdal_jpeg_std_message_table
52
#endif
53
54
#define JMESSAGE(code,string) string ,
55
56
const char * const jpeg_std_message_table[] = {
57
#include "jerror.h"
58
  NULL
59
};
60
61
62
/*
63
 * Error exit handler: must not return to caller.
64
 *
65
 * Applications may override this if they want to get control back after
66
 * an error.  Typically one would longjmp somewhere instead of exiting.
67
 * The setjmp buffer can be made a private field within an expanded error
68
 * handler object.  Note that the info needed to generate an error message
69
 * is stored in the error object, so you can generate the message now or
70
 * later, at your convenience.
71
 * You should make sure that the JPEG object is cleaned up (with jpeg_abort
72
 * or jpeg_destroy) at some point.
73
 */
74
75
METHODDEF(void)
76
error_exit (j_common_ptr cinfo) LIBJPEG_NO_RETURN;
77
78
METHODDEF(void)
79
error_exit (j_common_ptr cinfo)
80
0
{
81
  /* Always display the message */
82
0
  (*cinfo->err->output_message) (cinfo);
83
84
  /* Let the memory manager delete any temp files before we die */
85
0
  jpeg_destroy(cinfo);
86
87
0
  exit(EXIT_FAILURE);
88
0
}
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
106
METHODDEF(void)
107
output_message (j_common_ptr cinfo)
108
0
{
109
0
  char buffer[JMSG_LENGTH_MAX];
110
111
  /* Create the message */
112
0
  (*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
0
  fprintf(stderr, "%s\n", buffer);
121
0
#endif
122
0
}
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
136
METHODDEF(void)
137
emit_message (j_common_ptr cinfo, int msg_level)
138
0
{
139
0
  struct jpeg_error_mgr * err = cinfo->err;
140
141
0
  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
0
    if (err->num_warnings == 0 || err->trace_level >= 3)
147
0
      (*err->output_message) (cinfo);
148
    /* Always count warnings in num_warnings. */
149
0
    err->num_warnings++;
150
0
  } else {
151
    /* It's a trace message.  Show it if trace_level >= msg_level. */
152
0
    if (err->trace_level >= msg_level)
153
0
      (*err->output_message) (cinfo);
154
0
  }
155
0
}
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
165
METHODDEF(void)
166
format_message (j_common_ptr cinfo, char * buffer)
167
0
{
168
0
  struct jpeg_error_mgr * err = cinfo->err;
169
0
  int msg_code = err->msg_code;
170
0
  const char * msgtext = NULL;
171
0
  const char * msgptr;
172
0
  char ch;
173
0
  boolean isstring;
174
175
  /* Look up message string in proper table */
176
0
  if (msg_code > 0 && msg_code <= err->last_jpeg_message) {
177
0
    msgtext = err->jpeg_message_table[msg_code];
178
0
  } else if (err->addon_message_table != NULL &&
179
0
       msg_code >= err->first_addon_message &&
180
0
       msg_code <= err->last_addon_message) {
181
0
    msgtext = err->addon_message_table[msg_code - err->first_addon_message];
182
0
  }
183
184
  /* Defend against bogus message number */
185
0
  if (msgtext == NULL) {
186
0
    err->msg_parm.i[0] = msg_code;
187
0
    msgtext = err->jpeg_message_table[0];
188
0
  }
189
190
  /* Check for string parameter, as indicated by %s in the message text */
191
0
  isstring = FALSE;
192
0
  msgptr = msgtext;
193
0
  while ((ch = *msgptr++) != '\0') {
194
0
    if (ch == '%') {
195
0
      if (*msgptr == 's') isstring = TRUE;
196
0
      break;
197
0
    }
198
0
  }
199
200
  /* Format the message into the passed buffer */
201
0
  if (isstring)
202
0
    sprintf(buffer, msgtext, err->msg_parm.s);
203
0
  else
204
0
    sprintf(buffer, msgtext,
205
0
      err->msg_parm.i[0], err->msg_parm.i[1],
206
0
      err->msg_parm.i[2], err->msg_parm.i[3],
207
0
      err->msg_parm.i[4], err->msg_parm.i[5],
208
0
      err->msg_parm.i[6], err->msg_parm.i[7]);
209
0
}
210
211
212
/*
213
 * Reset error state variables at start of a new image.
214
 * This is called during compression startup to reset trace/error
215
 * processing to default state, without losing any application-specific
216
 * method pointers.  An application might possibly want to override
217
 * this method if it has additional error processing state.
218
 */
219
220
METHODDEF(void)
221
reset_error_mgr (j_common_ptr cinfo)
222
0
{
223
0
  cinfo->err->num_warnings = 0;
224
  /* trace_level is not reset since it is an application-supplied parameter */
225
0
  cinfo->err->msg_code = 0; /* may be useful as a flag for "no error" */
226
0
}
227
228
229
/*
230
 * Fill in the standard error-handling methods in a jpeg_error_mgr object.
231
 * Typical call is:
232
 *  struct jpeg_compress_struct cinfo;
233
 *  struct jpeg_error_mgr err;
234
 *
235
 *  cinfo.err = jpeg_std_error(&err);
236
 * after which the application may override some of the methods.
237
 */
238
239
GLOBAL(struct jpeg_error_mgr *)
240
jpeg_std_error (struct jpeg_error_mgr * err)
241
0
{
242
0
  err->error_exit = error_exit;
243
0
  err->emit_message = emit_message;
244
0
  err->output_message = output_message;
245
0
  err->format_message = format_message;
246
0
  err->reset_error_mgr = reset_error_mgr;
247
248
0
  err->trace_level = 0;   /* default = no tracing */
249
0
  err->num_warnings = 0;  /* no warnings emitted yet */
250
0
  err->msg_code = 0;    /* may be useful as a flag for "no error" */
251
252
  /* Initialize message table pointers */
253
0
  err->jpeg_message_table = jpeg_std_message_table;
254
0
  err->last_jpeg_message = (int) JMSG_LASTMSGCODE - 1;
255
256
0
  err->addon_message_table = NULL;
257
0
  err->first_addon_message = 0; /* for safety */
258
0
  err->last_addon_message = 0;
259
260
0
  return err;
261
0
}