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