michael@0: /* michael@0: * jerror.c michael@0: * michael@0: * Copyright (C) 1991-1998, Thomas G. Lane. michael@0: * This file is part of the Independent JPEG Group's software. michael@0: * For conditions of distribution and use, see the accompanying README file. michael@0: * michael@0: * This file contains simple error-reporting and trace-message routines. michael@0: * These are suitable for Unix-like systems and others where writing to michael@0: * stderr is the right thing to do. Many applications will want to replace michael@0: * some or all of these routines. michael@0: * michael@0: * If you define USE_WINDOWS_MESSAGEBOX in jconfig.h or in the makefile, michael@0: * you get a Windows-specific hack to display error messages in a dialog box. michael@0: * It ain't much, but it beats dropping error messages into the bit bucket, michael@0: * which is what happens to output to stderr under most Windows C compilers. michael@0: * michael@0: * These routines are used by both the compression and decompression code. michael@0: */ michael@0: michael@0: /* this is not a core library module, so it doesn't define JPEG_INTERNALS */ michael@0: #include "jinclude.h" michael@0: #include "jpeglib.h" michael@0: #include "jversion.h" michael@0: #include "jerror.h" michael@0: michael@0: #ifdef USE_WINDOWS_MESSAGEBOX michael@0: #include michael@0: #endif michael@0: michael@0: #ifndef EXIT_FAILURE /* define exit() codes if not provided */ michael@0: #define EXIT_FAILURE 1 michael@0: #endif michael@0: michael@0: michael@0: /* michael@0: * Create the message string table. michael@0: * We do this from the master message list in jerror.h by re-reading michael@0: * jerror.h with a suitable definition for macro JMESSAGE. michael@0: * The message table is made an external symbol just in case any applications michael@0: * want to refer to it directly. michael@0: */ michael@0: michael@0: #ifdef NEED_SHORT_EXTERNAL_NAMES michael@0: #define jpeg_std_message_table jMsgTable michael@0: #endif michael@0: michael@0: #define JMESSAGE(code,string) string , michael@0: michael@0: const char * const jpeg_std_message_table[] = { michael@0: #include "jerror.h" michael@0: NULL michael@0: }; michael@0: michael@0: michael@0: /* michael@0: * Error exit handler: must not return to caller. michael@0: * michael@0: * Applications may override this if they want to get control back after michael@0: * an error. Typically one would longjmp somewhere instead of exiting. michael@0: * The setjmp buffer can be made a private field within an expanded error michael@0: * handler object. Note that the info needed to generate an error message michael@0: * is stored in the error object, so you can generate the message now or michael@0: * later, at your convenience. michael@0: * You should make sure that the JPEG object is cleaned up (with jpeg_abort michael@0: * or jpeg_destroy) at some point. michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: error_exit (j_common_ptr cinfo) michael@0: { michael@0: /* Always display the message */ michael@0: (*cinfo->err->output_message) (cinfo); michael@0: michael@0: /* Let the memory manager delete any temp files before we die */ michael@0: jpeg_destroy(cinfo); michael@0: michael@0: exit(EXIT_FAILURE); michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Actual output of an error or trace message. michael@0: * Applications may override this method to send JPEG messages somewhere michael@0: * other than stderr. michael@0: * michael@0: * On Windows, printing to stderr is generally completely useless, michael@0: * so we provide optional code to produce an error-dialog popup. michael@0: * Most Windows applications will still prefer to override this routine, michael@0: * but if they don't, it'll do something at least marginally useful. michael@0: * michael@0: * NOTE: to use the library in an environment that doesn't support the michael@0: * C stdio library, you may have to delete the call to fprintf() entirely, michael@0: * not just not use this routine. michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: output_message (j_common_ptr cinfo) michael@0: { michael@0: char buffer[JMSG_LENGTH_MAX]; michael@0: michael@0: /* Create the message */ michael@0: (*cinfo->err->format_message) (cinfo, buffer); michael@0: michael@0: #ifdef USE_WINDOWS_MESSAGEBOX michael@0: /* Display it in a message dialog box */ michael@0: MessageBox(GetActiveWindow(), buffer, "JPEG Library Error", michael@0: MB_OK | MB_ICONERROR); michael@0: #else michael@0: /* Send it to stderr, adding a newline */ michael@0: fprintf(stderr, "%s\n", buffer); michael@0: #endif michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Decide whether to emit a trace or warning message. michael@0: * msg_level is one of: michael@0: * -1: recoverable corrupt-data warning, may want to abort. michael@0: * 0: important advisory messages (always display to user). michael@0: * 1: first level of tracing detail. michael@0: * 2,3,...: successively more detailed tracing messages. michael@0: * An application might override this method if it wanted to abort on warnings michael@0: * or change the policy about which messages to display. michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: emit_message (j_common_ptr cinfo, int msg_level) michael@0: { michael@0: struct jpeg_error_mgr * err = cinfo->err; michael@0: michael@0: if (msg_level < 0) { michael@0: /* It's a warning message. Since corrupt files may generate many warnings, michael@0: * the policy implemented here is to show only the first warning, michael@0: * unless trace_level >= 3. michael@0: */ michael@0: if (err->num_warnings == 0 || err->trace_level >= 3) michael@0: (*err->output_message) (cinfo); michael@0: /* Always count warnings in num_warnings. */ michael@0: err->num_warnings++; michael@0: } else { michael@0: /* It's a trace message. Show it if trace_level >= msg_level. */ michael@0: if (err->trace_level >= msg_level) michael@0: (*err->output_message) (cinfo); michael@0: } michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Format a message string for the most recent JPEG error or message. michael@0: * The message is stored into buffer, which should be at least JMSG_LENGTH_MAX michael@0: * characters. Note that no '\n' character is added to the string. michael@0: * Few applications should need to override this method. michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: format_message (j_common_ptr cinfo, char * buffer) michael@0: { michael@0: struct jpeg_error_mgr * err = cinfo->err; michael@0: int msg_code = err->msg_code; michael@0: const char * msgtext = NULL; michael@0: const char * msgptr; michael@0: char ch; michael@0: boolean isstring; michael@0: michael@0: /* Look up message string in proper table */ michael@0: if (msg_code > 0 && msg_code <= err->last_jpeg_message) { michael@0: msgtext = err->jpeg_message_table[msg_code]; michael@0: } else if (err->addon_message_table != NULL && michael@0: msg_code >= err->first_addon_message && michael@0: msg_code <= err->last_addon_message) { michael@0: msgtext = err->addon_message_table[msg_code - err->first_addon_message]; michael@0: } michael@0: michael@0: /* Defend against bogus message number */ michael@0: if (msgtext == NULL) { michael@0: err->msg_parm.i[0] = msg_code; michael@0: msgtext = err->jpeg_message_table[0]; michael@0: } michael@0: michael@0: /* Check for string parameter, as indicated by %s in the message text */ michael@0: isstring = FALSE; michael@0: msgptr = msgtext; michael@0: while ((ch = *msgptr++) != '\0') { michael@0: if (ch == '%') { michael@0: if (*msgptr == 's') isstring = TRUE; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: /* Format the message into the passed buffer */ michael@0: if (isstring) michael@0: sprintf(buffer, msgtext, err->msg_parm.s); michael@0: else michael@0: sprintf(buffer, msgtext, michael@0: err->msg_parm.i[0], err->msg_parm.i[1], michael@0: err->msg_parm.i[2], err->msg_parm.i[3], michael@0: err->msg_parm.i[4], err->msg_parm.i[5], michael@0: err->msg_parm.i[6], err->msg_parm.i[7]); michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Reset error state variables at start of a new image. michael@0: * This is called during compression startup to reset trace/error michael@0: * processing to default state, without losing any application-specific michael@0: * method pointers. An application might possibly want to override michael@0: * this method if it has additional error processing state. michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: reset_error_mgr (j_common_ptr cinfo) michael@0: { michael@0: cinfo->err->num_warnings = 0; michael@0: /* trace_level is not reset since it is an application-supplied parameter */ michael@0: cinfo->err->msg_code = 0; /* may be useful as a flag for "no error" */ michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Fill in the standard error-handling methods in a jpeg_error_mgr object. michael@0: * Typical call is: michael@0: * struct jpeg_compress_struct cinfo; michael@0: * struct jpeg_error_mgr err; michael@0: * michael@0: * cinfo.err = jpeg_std_error(&err); michael@0: * after which the application may override some of the methods. michael@0: */ michael@0: michael@0: GLOBAL(struct jpeg_error_mgr *) michael@0: jpeg_std_error (struct jpeg_error_mgr * err) michael@0: { michael@0: err->error_exit = error_exit; michael@0: err->emit_message = emit_message; michael@0: err->output_message = output_message; michael@0: err->format_message = format_message; michael@0: err->reset_error_mgr = reset_error_mgr; michael@0: michael@0: err->trace_level = 0; /* default = no tracing */ michael@0: err->num_warnings = 0; /* no warnings emitted yet */ michael@0: err->msg_code = 0; /* may be useful as a flag for "no error" */ michael@0: michael@0: /* Initialize message table pointers */ michael@0: err->jpeg_message_table = jpeg_std_message_table; michael@0: err->last_jpeg_message = (int) JMSG_LASTMSGCODE - 1; michael@0: michael@0: err->addon_message_table = NULL; michael@0: err->first_addon_message = 0; /* for safety */ michael@0: err->last_addon_message = 0; michael@0: michael@0: return err; michael@0: }