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