Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* |
michael@0 | 2 | * jerror.c |
michael@0 | 3 | * |
michael@0 | 4 | * Copyright (C) 1991-1998, Thomas G. Lane. |
michael@0 | 5 | * This file is part of the Independent JPEG Group's software. |
michael@0 | 6 | * For conditions of distribution and use, see the accompanying README file. |
michael@0 | 7 | * |
michael@0 | 8 | * This file contains simple error-reporting and trace-message routines. |
michael@0 | 9 | * These are suitable for Unix-like systems and others where writing to |
michael@0 | 10 | * stderr is the right thing to do. Many applications will want to replace |
michael@0 | 11 | * some or all of these routines. |
michael@0 | 12 | * |
michael@0 | 13 | * If you define USE_WINDOWS_MESSAGEBOX in jconfig.h or in the makefile, |
michael@0 | 14 | * you get a Windows-specific hack to display error messages in a dialog box. |
michael@0 | 15 | * It ain't much, but it beats dropping error messages into the bit bucket, |
michael@0 | 16 | * which is what happens to output to stderr under most Windows C compilers. |
michael@0 | 17 | * |
michael@0 | 18 | * These routines are used by both the compression and decompression code. |
michael@0 | 19 | */ |
michael@0 | 20 | |
michael@0 | 21 | /* this is not a core library module, so it doesn't define JPEG_INTERNALS */ |
michael@0 | 22 | #include "jinclude.h" |
michael@0 | 23 | #include "jpeglib.h" |
michael@0 | 24 | #include "jversion.h" |
michael@0 | 25 | #include "jerror.h" |
michael@0 | 26 | |
michael@0 | 27 | #ifdef USE_WINDOWS_MESSAGEBOX |
michael@0 | 28 | #include <windows.h> |
michael@0 | 29 | #endif |
michael@0 | 30 | |
michael@0 | 31 | #ifndef EXIT_FAILURE /* define exit() codes if not provided */ |
michael@0 | 32 | #define EXIT_FAILURE 1 |
michael@0 | 33 | #endif |
michael@0 | 34 | |
michael@0 | 35 | |
michael@0 | 36 | /* |
michael@0 | 37 | * Create the message string table. |
michael@0 | 38 | * We do this from the master message list in jerror.h by re-reading |
michael@0 | 39 | * jerror.h with a suitable definition for macro JMESSAGE. |
michael@0 | 40 | * The message table is made an external symbol just in case any applications |
michael@0 | 41 | * want to refer to it directly. |
michael@0 | 42 | */ |
michael@0 | 43 | |
michael@0 | 44 | #ifdef NEED_SHORT_EXTERNAL_NAMES |
michael@0 | 45 | #define jpeg_std_message_table jMsgTable |
michael@0 | 46 | #endif |
michael@0 | 47 | |
michael@0 | 48 | #define JMESSAGE(code,string) string , |
michael@0 | 49 | |
michael@0 | 50 | const char * const jpeg_std_message_table[] = { |
michael@0 | 51 | #include "jerror.h" |
michael@0 | 52 | NULL |
michael@0 | 53 | }; |
michael@0 | 54 | |
michael@0 | 55 | |
michael@0 | 56 | /* |
michael@0 | 57 | * Error exit handler: must not return to caller. |
michael@0 | 58 | * |
michael@0 | 59 | * Applications may override this if they want to get control back after |
michael@0 | 60 | * an error. Typically one would longjmp somewhere instead of exiting. |
michael@0 | 61 | * The setjmp buffer can be made a private field within an expanded error |
michael@0 | 62 | * handler object. Note that the info needed to generate an error message |
michael@0 | 63 | * is stored in the error object, so you can generate the message now or |
michael@0 | 64 | * later, at your convenience. |
michael@0 | 65 | * You should make sure that the JPEG object is cleaned up (with jpeg_abort |
michael@0 | 66 | * or jpeg_destroy) at some point. |
michael@0 | 67 | */ |
michael@0 | 68 | |
michael@0 | 69 | METHODDEF(void) |
michael@0 | 70 | error_exit (j_common_ptr cinfo) |
michael@0 | 71 | { |
michael@0 | 72 | /* Always display the message */ |
michael@0 | 73 | (*cinfo->err->output_message) (cinfo); |
michael@0 | 74 | |
michael@0 | 75 | /* Let the memory manager delete any temp files before we die */ |
michael@0 | 76 | jpeg_destroy(cinfo); |
michael@0 | 77 | |
michael@0 | 78 | exit(EXIT_FAILURE); |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | |
michael@0 | 82 | /* |
michael@0 | 83 | * Actual output of an error or trace message. |
michael@0 | 84 | * Applications may override this method to send JPEG messages somewhere |
michael@0 | 85 | * other than stderr. |
michael@0 | 86 | * |
michael@0 | 87 | * On Windows, printing to stderr is generally completely useless, |
michael@0 | 88 | * so we provide optional code to produce an error-dialog popup. |
michael@0 | 89 | * Most Windows applications will still prefer to override this routine, |
michael@0 | 90 | * but if they don't, it'll do something at least marginally useful. |
michael@0 | 91 | * |
michael@0 | 92 | * NOTE: to use the library in an environment that doesn't support the |
michael@0 | 93 | * C stdio library, you may have to delete the call to fprintf() entirely, |
michael@0 | 94 | * not just not use this routine. |
michael@0 | 95 | */ |
michael@0 | 96 | |
michael@0 | 97 | METHODDEF(void) |
michael@0 | 98 | output_message (j_common_ptr cinfo) |
michael@0 | 99 | { |
michael@0 | 100 | char buffer[JMSG_LENGTH_MAX]; |
michael@0 | 101 | |
michael@0 | 102 | /* Create the message */ |
michael@0 | 103 | (*cinfo->err->format_message) (cinfo, buffer); |
michael@0 | 104 | |
michael@0 | 105 | #ifdef USE_WINDOWS_MESSAGEBOX |
michael@0 | 106 | /* Display it in a message dialog box */ |
michael@0 | 107 | MessageBox(GetActiveWindow(), buffer, "JPEG Library Error", |
michael@0 | 108 | MB_OK | MB_ICONERROR); |
michael@0 | 109 | #else |
michael@0 | 110 | /* Send it to stderr, adding a newline */ |
michael@0 | 111 | fprintf(stderr, "%s\n", buffer); |
michael@0 | 112 | #endif |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | |
michael@0 | 116 | /* |
michael@0 | 117 | * Decide whether to emit a trace or warning message. |
michael@0 | 118 | * msg_level is one of: |
michael@0 | 119 | * -1: recoverable corrupt-data warning, may want to abort. |
michael@0 | 120 | * 0: important advisory messages (always display to user). |
michael@0 | 121 | * 1: first level of tracing detail. |
michael@0 | 122 | * 2,3,...: successively more detailed tracing messages. |
michael@0 | 123 | * An application might override this method if it wanted to abort on warnings |
michael@0 | 124 | * or change the policy about which messages to display. |
michael@0 | 125 | */ |
michael@0 | 126 | |
michael@0 | 127 | METHODDEF(void) |
michael@0 | 128 | emit_message (j_common_ptr cinfo, int msg_level) |
michael@0 | 129 | { |
michael@0 | 130 | struct jpeg_error_mgr * err = cinfo->err; |
michael@0 | 131 | |
michael@0 | 132 | if (msg_level < 0) { |
michael@0 | 133 | /* It's a warning message. Since corrupt files may generate many warnings, |
michael@0 | 134 | * the policy implemented here is to show only the first warning, |
michael@0 | 135 | * unless trace_level >= 3. |
michael@0 | 136 | */ |
michael@0 | 137 | if (err->num_warnings == 0 || err->trace_level >= 3) |
michael@0 | 138 | (*err->output_message) (cinfo); |
michael@0 | 139 | /* Always count warnings in num_warnings. */ |
michael@0 | 140 | err->num_warnings++; |
michael@0 | 141 | } else { |
michael@0 | 142 | /* It's a trace message. Show it if trace_level >= msg_level. */ |
michael@0 | 143 | if (err->trace_level >= msg_level) |
michael@0 | 144 | (*err->output_message) (cinfo); |
michael@0 | 145 | } |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | |
michael@0 | 149 | /* |
michael@0 | 150 | * Format a message string for the most recent JPEG error or message. |
michael@0 | 151 | * The message is stored into buffer, which should be at least JMSG_LENGTH_MAX |
michael@0 | 152 | * characters. Note that no '\n' character is added to the string. |
michael@0 | 153 | * Few applications should need to override this method. |
michael@0 | 154 | */ |
michael@0 | 155 | |
michael@0 | 156 | METHODDEF(void) |
michael@0 | 157 | format_message (j_common_ptr cinfo, char * buffer) |
michael@0 | 158 | { |
michael@0 | 159 | struct jpeg_error_mgr * err = cinfo->err; |
michael@0 | 160 | int msg_code = err->msg_code; |
michael@0 | 161 | const char * msgtext = NULL; |
michael@0 | 162 | const char * msgptr; |
michael@0 | 163 | char ch; |
michael@0 | 164 | boolean isstring; |
michael@0 | 165 | |
michael@0 | 166 | /* Look up message string in proper table */ |
michael@0 | 167 | if (msg_code > 0 && msg_code <= err->last_jpeg_message) { |
michael@0 | 168 | msgtext = err->jpeg_message_table[msg_code]; |
michael@0 | 169 | } else if (err->addon_message_table != NULL && |
michael@0 | 170 | msg_code >= err->first_addon_message && |
michael@0 | 171 | msg_code <= err->last_addon_message) { |
michael@0 | 172 | msgtext = err->addon_message_table[msg_code - err->first_addon_message]; |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | /* Defend against bogus message number */ |
michael@0 | 176 | if (msgtext == NULL) { |
michael@0 | 177 | err->msg_parm.i[0] = msg_code; |
michael@0 | 178 | msgtext = err->jpeg_message_table[0]; |
michael@0 | 179 | } |
michael@0 | 180 | |
michael@0 | 181 | /* Check for string parameter, as indicated by %s in the message text */ |
michael@0 | 182 | isstring = FALSE; |
michael@0 | 183 | msgptr = msgtext; |
michael@0 | 184 | while ((ch = *msgptr++) != '\0') { |
michael@0 | 185 | if (ch == '%') { |
michael@0 | 186 | if (*msgptr == 's') isstring = TRUE; |
michael@0 | 187 | break; |
michael@0 | 188 | } |
michael@0 | 189 | } |
michael@0 | 190 | |
michael@0 | 191 | /* Format the message into the passed buffer */ |
michael@0 | 192 | if (isstring) |
michael@0 | 193 | sprintf(buffer, msgtext, err->msg_parm.s); |
michael@0 | 194 | else |
michael@0 | 195 | sprintf(buffer, msgtext, |
michael@0 | 196 | err->msg_parm.i[0], err->msg_parm.i[1], |
michael@0 | 197 | err->msg_parm.i[2], err->msg_parm.i[3], |
michael@0 | 198 | err->msg_parm.i[4], err->msg_parm.i[5], |
michael@0 | 199 | err->msg_parm.i[6], err->msg_parm.i[7]); |
michael@0 | 200 | } |
michael@0 | 201 | |
michael@0 | 202 | |
michael@0 | 203 | /* |
michael@0 | 204 | * Reset error state variables at start of a new image. |
michael@0 | 205 | * This is called during compression startup to reset trace/error |
michael@0 | 206 | * processing to default state, without losing any application-specific |
michael@0 | 207 | * method pointers. An application might possibly want to override |
michael@0 | 208 | * this method if it has additional error processing state. |
michael@0 | 209 | */ |
michael@0 | 210 | |
michael@0 | 211 | METHODDEF(void) |
michael@0 | 212 | reset_error_mgr (j_common_ptr cinfo) |
michael@0 | 213 | { |
michael@0 | 214 | cinfo->err->num_warnings = 0; |
michael@0 | 215 | /* trace_level is not reset since it is an application-supplied parameter */ |
michael@0 | 216 | cinfo->err->msg_code = 0; /* may be useful as a flag for "no error" */ |
michael@0 | 217 | } |
michael@0 | 218 | |
michael@0 | 219 | |
michael@0 | 220 | /* |
michael@0 | 221 | * Fill in the standard error-handling methods in a jpeg_error_mgr object. |
michael@0 | 222 | * Typical call is: |
michael@0 | 223 | * struct jpeg_compress_struct cinfo; |
michael@0 | 224 | * struct jpeg_error_mgr err; |
michael@0 | 225 | * |
michael@0 | 226 | * cinfo.err = jpeg_std_error(&err); |
michael@0 | 227 | * after which the application may override some of the methods. |
michael@0 | 228 | */ |
michael@0 | 229 | |
michael@0 | 230 | GLOBAL(struct jpeg_error_mgr *) |
michael@0 | 231 | jpeg_std_error (struct jpeg_error_mgr * err) |
michael@0 | 232 | { |
michael@0 | 233 | err->error_exit = error_exit; |
michael@0 | 234 | err->emit_message = emit_message; |
michael@0 | 235 | err->output_message = output_message; |
michael@0 | 236 | err->format_message = format_message; |
michael@0 | 237 | err->reset_error_mgr = reset_error_mgr; |
michael@0 | 238 | |
michael@0 | 239 | err->trace_level = 0; /* default = no tracing */ |
michael@0 | 240 | err->num_warnings = 0; /* no warnings emitted yet */ |
michael@0 | 241 | err->msg_code = 0; /* may be useful as a flag for "no error" */ |
michael@0 | 242 | |
michael@0 | 243 | /* Initialize message table pointers */ |
michael@0 | 244 | err->jpeg_message_table = jpeg_std_message_table; |
michael@0 | 245 | err->last_jpeg_message = (int) JMSG_LASTMSGCODE - 1; |
michael@0 | 246 | |
michael@0 | 247 | err->addon_message_table = NULL; |
michael@0 | 248 | err->first_addon_message = 0; /* for safety */ |
michael@0 | 249 | err->last_addon_message = 0; |
michael@0 | 250 | |
michael@0 | 251 | return err; |
michael@0 | 252 | } |