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