jerror.c (7566B)
1 /* 2 * jerror.c 3 * 4 * This file was part of the Independent JPEG Group's software: 5 * Copyright (C) 1991-1998, Thomas G. Lane. 6 * libjpeg-turbo Modifications: 7 * Copyright (C) 2022, 2024, D. R. Commander. 8 * For conditions of distribution and use, see the accompanying README.ijg 9 * file. 10 * 11 * This file contains simple error-reporting and trace-message routines. 12 * These are suitable for Unix-like systems and others where writing to 13 * stderr is the right thing to do. Many applications will want to replace 14 * some or all of these routines. 15 * 16 * If you define USE_WINDOWS_MESSAGEBOX in jconfig.h or in the makefile, 17 * you get a Windows-specific hack to display error messages in a dialog box. 18 * It ain't much, but it beats dropping error messages into the bit bucket, 19 * which is what happens to output to stderr under most Windows C compilers. 20 * 21 * These routines are used by both the compression and decompression code. 22 */ 23 24 /* this is not a core library module, so it doesn't define JPEG_INTERNALS */ 25 #include "jinclude.h" 26 #include "jpeglib.h" 27 #include "jversion.h" 28 #include "jerror.h" 29 30 #ifdef USE_WINDOWS_MESSAGEBOX 31 #include <windows.h> 32 #endif 33 34 #ifndef EXIT_FAILURE /* define exit() codes if not provided */ 35 #define EXIT_FAILURE 1 36 #endif 37 38 39 /* 40 * Create the message string table. 41 * We do this from the master message list in jerror.h by re-reading 42 * jerror.h with a suitable definition for macro JMESSAGE. 43 */ 44 45 #define JMESSAGE(code, string) string, 46 47 static const 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 66 METHODDEF(void) 67 error_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 94 METHODDEF(void) 95 output_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 fprintf(stderr, "%s\n", buffer); 109 #endif 110 } 111 112 113 /* 114 * Decide whether to emit a trace or warning message. 115 * msg_level is one of: 116 * -1: recoverable corrupt-data warning, may want to abort. 117 * 0: important advisory messages (always display to user). 118 * 1: first level of tracing detail. 119 * 2,3,...: successively more detailed tracing messages. 120 * An application might override this method if it wanted to abort on warnings 121 * or change the policy about which messages to display. 122 */ 123 124 METHODDEF(void) 125 emit_message(j_common_ptr cinfo, int msg_level) 126 { 127 struct jpeg_error_mgr *err = cinfo->err; 128 129 if (msg_level < 0) { 130 /* It's a warning message. Since corrupt files may generate many warnings, 131 * the policy implemented here is to show only the first warning, 132 * unless trace_level >= 3. 133 */ 134 if (err->num_warnings == 0 || err->trace_level >= 3) 135 (*err->output_message) (cinfo); 136 /* Always count warnings in num_warnings. */ 137 err->num_warnings++; 138 } else { 139 /* It's a trace message. Show it if trace_level >= msg_level. */ 140 if (err->trace_level >= msg_level) 141 (*err->output_message) (cinfo); 142 } 143 } 144 145 146 /* 147 * Format a message string for the most recent JPEG error or message. 148 * The message is stored into buffer, which should be at least JMSG_LENGTH_MAX 149 * characters. Note that no '\n' character is added to the string. 150 * Few applications should need to override this method. 151 */ 152 153 METHODDEF(void) 154 format_message(j_common_ptr cinfo, char *buffer) 155 { 156 struct jpeg_error_mgr *err = cinfo->err; 157 int msg_code = err->msg_code; 158 const char *msgtext = NULL; 159 const char *msgptr; 160 char ch; 161 boolean isstring; 162 163 /* Look up message string in proper table */ 164 if (msg_code > 0 && msg_code <= err->last_jpeg_message) { 165 msgtext = err->jpeg_message_table[msg_code]; 166 } else if (err->addon_message_table != NULL && 167 msg_code >= err->first_addon_message && 168 msg_code <= err->last_addon_message) { 169 msgtext = err->addon_message_table[msg_code - err->first_addon_message]; 170 } 171 172 /* Defend against bogus message number */ 173 if (msgtext == NULL) { 174 err->msg_parm.i[0] = msg_code; 175 msgtext = err->jpeg_message_table[0]; 176 } 177 178 /* Check for string parameter, as indicated by %s in the message text */ 179 isstring = FALSE; 180 msgptr = msgtext; 181 while ((ch = *msgptr++) != '\0') { 182 if (ch == '%') { 183 if (*msgptr == 's') isstring = TRUE; 184 break; 185 } 186 } 187 188 /* Format the message into the passed buffer */ 189 if (isstring) 190 SNPRINTF(buffer, JMSG_LENGTH_MAX, msgtext, err->msg_parm.s); 191 else 192 SNPRINTF(buffer, JMSG_LENGTH_MAX, msgtext, 193 err->msg_parm.i[0], err->msg_parm.i[1], 194 err->msg_parm.i[2], err->msg_parm.i[3], 195 err->msg_parm.i[4], err->msg_parm.i[5], 196 err->msg_parm.i[6], err->msg_parm.i[7]); 197 } 198 199 200 /* 201 * Reset error state variables at start of a new image. 202 * This is called during compression startup to reset trace/error 203 * processing to default state, without losing any application-specific 204 * method pointers. An application might possibly want to override 205 * this method if it has additional error processing state. 206 */ 207 208 METHODDEF(void) 209 reset_error_mgr(j_common_ptr cinfo) 210 { 211 cinfo->err->num_warnings = 0; 212 /* trace_level is not reset since it is an application-supplied parameter */ 213 cinfo->err->msg_code = 0; /* may be useful as a flag for "no error" */ 214 } 215 216 217 /* 218 * Fill in the standard error-handling methods in a jpeg_error_mgr object. 219 * Typical call is: 220 * struct jpeg_compress_struct cinfo; 221 * struct jpeg_error_mgr err; 222 * 223 * cinfo.err = jpeg_std_error(&err); 224 * after which the application may override some of the methods. 225 */ 226 227 GLOBAL(struct jpeg_error_mgr *) 228 jpeg_std_error(struct jpeg_error_mgr *err) 229 { 230 memset(err, 0, sizeof(struct jpeg_error_mgr)); 231 232 err->error_exit = error_exit; 233 err->emit_message = emit_message; 234 err->output_message = output_message; 235 err->format_message = format_message; 236 err->reset_error_mgr = reset_error_mgr; 237 238 /* Initialize message table pointers */ 239 err->jpeg_message_table = jpeg_std_message_table; 240 err->last_jpeg_message = (int)JMSG_LASTMSGCODE - 1; 241 242 return err; 243 }