Back to index

lightning-sunbird  0.9+nobinonly
jerror.c
Go to the documentation of this file.
00001 /*
00002  * jerror.c
00003  *
00004  * Copyright (C) 1991-1998, Thomas G. Lane.
00005  * This file is part of the Independent JPEG Group's software.
00006  * For conditions of distribution and use, see the accompanying README file.
00007  *
00008  * This file contains simple error-reporting and trace-message routines.
00009  * These are suitable for Unix-like systems and others where writing to
00010  * stderr is the right thing to do.  Many applications will want to replace
00011  * some or all of these routines.
00012  *
00013  * If you define USE_WINDOWS_MESSAGEBOX in jconfig.h or in the makefile,
00014  * you get a Windows-specific hack to display error messages in a dialog box.
00015  * It ain't much, but it beats dropping error messages into the bit bucket,
00016  * which is what happens to output to stderr under most Windows C compilers.
00017  *
00018  * These routines are used by both the compression and decompression code.
00019  */
00020 
00021 /*
00022  * This file has been modified for the Mozilla/Netscape environment.
00023  * Modifications are distributed under the Netscape Public License and are
00024  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
00025  * Reserved.
00026  */
00027 
00028 /* this is not a core library module, so it doesn't define JPEG_INTERNALS */
00029 #include "jinclude.h"
00030 #include "jpeglib.h"
00031 #include "jversion.h"
00032 #include "jerror.h"
00033 
00034 #ifdef USE_WINDOWS_MESSAGEBOX
00035 #include <windows.h>
00036 #endif
00037 
00038 #ifndef EXIT_FAILURE        /* define exit() codes if not provided */
00039 #define EXIT_FAILURE  1
00040 #endif
00041 
00042 
00043 /*
00044  * Create the message string table.
00045  * We do this from the master message list in jerror.h by re-reading
00046  * jerror.h with a suitable definition for macro JMESSAGE.
00047  * The message table is made an external symbol just in case any applications
00048  * want to refer to it directly.
00049  */
00050 
00051 #ifdef NEED_SHORT_EXTERNAL_NAMES
00052 #define jpeg_std_message_table     jMsgTable
00053 #endif
00054 
00055 #define JMESSAGE(code,string)      string ,
00056 
00057 const char * const jpeg_std_message_table[] = {
00058 #include "jerror.h"
00059   NULL
00060 };
00061 
00062 
00063 /*
00064  * Error exit handler: must not return to caller.
00065  *
00066  * Applications may override this if they want to get control back after
00067  * an error.  Typically one would longjmp somewhere instead of exiting.
00068  * The setjmp buffer can be made a private field within an expanded error
00069  * handler object.  Note that the info needed to generate an error message
00070  * is stored in the error object, so you can generate the message now or
00071  * later, at your convenience.
00072  * You should make sure that the JPEG object is cleaned up (with jpeg_abort
00073  * or jpeg_destroy) at some point.
00074  */
00075 
00076 METHODDEF(void)
00077 error_exit (j_common_ptr cinfo)
00078 {
00079   /* Always display the message */
00080   (*cinfo->err->output_message) (cinfo);
00081 
00082   /* Let the memory manager delete any temp files before we die */
00083   jpeg_destroy(cinfo);
00084 
00085 /* Mozilla mod: in some Windows environments, the exit() function doesn't
00086  * even exist, so don't compile a reference to it.  Heaven help you if
00087  * you fail to provide a replacement error_exit function, because the
00088  * IJG library will NOT handle control returning from error_exit!
00089  */
00090 
00091 #ifndef XP_WIN
00092   exit(EXIT_FAILURE);
00093 #endif
00094 }
00095 
00096 
00097 /*
00098  * Actual output of an error or trace message.
00099  * Applications may override this method to send JPEG messages somewhere
00100  * other than stderr.
00101  *
00102  * On Windows, printing to stderr is generally completely useless,
00103  * so we provide optional code to produce an error-dialog popup.
00104  * Most Windows applications will still prefer to override this routine,
00105  * but if they don't, it'll do something at least marginally useful.
00106  *
00107  * NOTE: to use the library in an environment that doesn't support the
00108  * C stdio library, you may have to delete the call to fprintf() entirely,
00109  * not just not use this routine.
00110  */
00111 
00112 METHODDEF(void)
00113 output_message (j_common_ptr cinfo)
00114 {
00115   char buffer[JMSG_LENGTH_MAX];
00116 
00117   /* Create the message */
00118   (*cinfo->err->format_message) (cinfo, buffer);
00119 }
00120 
00121 
00122 /*
00123  * Decide whether to emit a trace or warning message.
00124  * msg_level is one of:
00125  *   -1: recoverable corrupt-data warning, may want to abort.
00126  *    0: important advisory messages (always display to user).
00127  *    1: first level of tracing detail.
00128  *    2,3,...: successively more detailed tracing messages.
00129  * An application might override this method if it wanted to abort on warnings
00130  * or change the policy about which messages to display.
00131  */
00132 
00133 METHODDEF(void)
00134 emit_message (j_common_ptr cinfo, int msg_level)
00135 {
00136   struct jpeg_error_mgr * err = cinfo->err;
00137 
00138   if (msg_level < 0) {
00139     /* It's a warning message.  Since corrupt files may generate many warnings,
00140      * the policy implemented here is to show only the first warning,
00141      * unless trace_level >= 3.
00142      */
00143     if (err->num_warnings == 0 || err->trace_level >= 3)
00144       (*err->output_message) (cinfo);
00145     /* Always count warnings in num_warnings. */
00146     err->num_warnings++;
00147   } else {
00148     /* It's a trace message.  Show it if trace_level >= msg_level. */
00149     if (err->trace_level >= msg_level)
00150       (*err->output_message) (cinfo);
00151   }
00152 }
00153 
00154 
00155 /*
00156  * Format a message string for the most recent JPEG error or message.
00157  * The message is stored into buffer, which should be at least JMSG_LENGTH_MAX
00158  * characters.  Note that no '\n' character is added to the string.
00159  * Few applications should need to override this method.
00160  */
00161 
00162 METHODDEF(void)
00163 format_message (j_common_ptr cinfo, char * buffer)
00164 {
00165   struct jpeg_error_mgr * err = cinfo->err;
00166   int msg_code = err->msg_code;
00167   const char * msgtext = NULL;
00168   const char * msgptr;
00169   char ch;
00170   boolean isstring;
00171 
00172   /* Look up message string in proper table */
00173   if (msg_code > 0 && msg_code <= err->last_jpeg_message) {
00174     msgtext = err->jpeg_message_table[msg_code];
00175   } else if (err->addon_message_table != NULL &&
00176             msg_code >= err->first_addon_message &&
00177             msg_code <= err->last_addon_message) {
00178     msgtext = err->addon_message_table[msg_code - err->first_addon_message];
00179   }
00180 
00181   /* Defend against bogus message number */
00182   if (msgtext == NULL) {
00183     err->msg_parm.i[0] = msg_code;
00184     msgtext = err->jpeg_message_table[0];
00185   }
00186 
00187   /* Check for string parameter, as indicated by %s in the message text */
00188   isstring = FALSE;
00189   msgptr = msgtext;
00190   while ((ch = *msgptr++) != '\0') {
00191     if (ch == '%') {
00192       if (*msgptr == 's') isstring = TRUE;
00193       break;
00194     }
00195   }
00196 
00197   /* Format the message into the passed buffer */
00198   if (isstring)
00199     sprintf(buffer, msgtext, err->msg_parm.s);
00200   else
00201     sprintf(buffer, msgtext,
00202            err->msg_parm.i[0], err->msg_parm.i[1],
00203            err->msg_parm.i[2], err->msg_parm.i[3],
00204            err->msg_parm.i[4], err->msg_parm.i[5],
00205            err->msg_parm.i[6], err->msg_parm.i[7]);
00206 }
00207 
00208 
00209 /*
00210  * Reset error state variables at start of a new image.
00211  * This is called during compression startup to reset trace/error
00212  * processing to default state, without losing any application-specific
00213  * method pointers.  An application might possibly want to override
00214  * this method if it has additional error processing state.
00215  */
00216 
00217 METHODDEF(void)
00218 reset_error_mgr (j_common_ptr cinfo)
00219 {
00220   cinfo->err->num_warnings = 0;
00221   /* trace_level is not reset since it is an application-supplied parameter */
00222   cinfo->err->msg_code = 0; /* may be useful as a flag for "no error" */
00223 }
00224 
00225 
00226 /*
00227  * Fill in the standard error-handling methods in a jpeg_error_mgr object.
00228  * Typical call is:
00229  *     struct jpeg_compress_struct cinfo;
00230  *     struct jpeg_error_mgr err;
00231  *
00232  *     cinfo.err = jpeg_std_error(&err);
00233  * after which the application may override some of the methods.
00234  */
00235 
00236 GLOBAL(struct jpeg_error_mgr *)
00237 jpeg_std_error (struct jpeg_error_mgr * err)
00238 {
00239   err->error_exit = error_exit;
00240   err->emit_message = emit_message;
00241   err->output_message = output_message;
00242   err->format_message = format_message;
00243   err->reset_error_mgr = reset_error_mgr;
00244 
00245   err->trace_level = 0;            /* default = no tracing */
00246   err->num_warnings = 0;    /* no warnings emitted yet */
00247   err->msg_code = 0;        /* may be useful as a flag for "no error" */
00248 
00249   /* Initialize message table pointers */
00250   err->jpeg_message_table = jpeg_std_message_table;
00251   err->last_jpeg_message = (int) JMSG_LASTMSGCODE - 1;
00252 
00253   err->addon_message_table = NULL;
00254   err->first_addon_message = 0;    /* for safety */
00255   err->last_addon_message = 0;
00256 
00257   return err;
00258 }