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