/*1* jerror.c2*3* Copyright (C) 1991-1998, Thomas G. Lane.4* Modified 2012-2015 by Guido Vollbeding.5* This file is part of the Independent JPEG Group's software.6* For conditions of distribution and use, see the accompanying README file.7*8* This file contains simple error-reporting and trace-message routines.9* These are suitable for Unix-like systems and others where writing to10* stderr is the right thing to do. Many applications will want to replace11* some or all of these routines.12*13* If you define USE_WINDOWS_MESSAGEBOX in jconfig.h or in the makefile,14* you get a Windows-specific hack to display error messages in a dialog box.15* It ain't much, but it beats dropping error messages into the bit bucket,16* which is what happens to output to stderr under most Windows C compilers.17*18* These routines are used by both the compression and decompression code.19*/2021#ifdef USE_WINDOWS_MESSAGEBOX22#include <windows.h>23#endif2425/* this is not a core library module, so it doesn't define JPEG_INTERNALS */26#include "jinclude.h"27#include "jpeglib.h"28#include "jversion.h"29#include "jerror.h"3031#ifndef EXIT_FAILURE /* define exit() codes if not provided */32#define EXIT_FAILURE 133#endif343536/*37* Create the message string table.38* We do this from the master message list in jerror.h by re-reading39* jerror.h with a suitable definition for macro JMESSAGE.40* The message table is made an external symbol just in case any applications41* want to refer to it directly.42*/4344#ifdef NEED_SHORT_EXTERNAL_NAMES45#define jpeg_std_message_table jMsgTable46#endif4748#define JMESSAGE(code,string) string ,4950const char * const jpeg_std_message_table[] = {51#include "jerror.h"52NULL53};545556/*57* Error exit handler: must not return to caller.58*59* Applications may override this if they want to get control back after60* an error. Typically one would longjmp somewhere instead of exiting.61* The setjmp buffer can be made a private field within an expanded error62* handler object. Note that the info needed to generate an error message63* is stored in the error object, so you can generate the message now or64* later, at your convenience.65* You should make sure that the JPEG object is cleaned up (with jpeg_abort66* or jpeg_destroy) at some point.67*/6869METHODDEF(noreturn_t)70error_exit (j_common_ptr cinfo)71{72/* Always display the message */73(*cinfo->err->output_message) (cinfo);7475/* Let the memory manager delete any temp files before we die */76jpeg_destroy(cinfo);7778exit(EXIT_FAILURE);79}808182/*83* Actual output of an error or trace message.84* Applications may override this method to send JPEG messages somewhere85* other than stderr.86*87* On Windows, printing to stderr is generally completely useless,88* so we provide optional code to produce an error-dialog popup.89* Most Windows applications will still prefer to override this routine,90* but if they don't, it'll do something at least marginally useful.91*92* NOTE: to use the library in an environment that doesn't support the93* C stdio library, you may have to delete the call to fprintf() entirely,94* not just not use this routine.95*/9697METHODDEF(void)98output_message (j_common_ptr cinfo)99{100char buffer[JMSG_LENGTH_MAX];101102/* Create the message */103(*cinfo->err->format_message) (cinfo, buffer);104105#ifdef USE_WINDOWS_MESSAGEBOX106/* Display it in a message dialog box */107MessageBox(GetActiveWindow(), buffer, "JPEG Library Error",108MB_OK | MB_ICONERROR);109#else110/* Send it to stderr, adding a newline */111fprintf(stderr, "%s\n", buffer);112#endif113}114115116/*117* Decide whether to emit a trace or warning message.118* msg_level is one of:119* -1: recoverable corrupt-data warning, may want to abort.120* 0: important advisory messages (always display to user).121* 1: first level of tracing detail.122* 2,3,...: successively more detailed tracing messages.123* An application might override this method if it wanted to abort on warnings124* or change the policy about which messages to display.125*/126127METHODDEF(void)128emit_message (j_common_ptr cinfo, int msg_level)129{130struct jpeg_error_mgr * err = cinfo->err;131132if (msg_level < 0) {133/* It's a warning message. Since corrupt files may generate many warnings,134* the policy implemented here is to show only the first warning,135* unless trace_level >= 3.136*/137if (err->num_warnings == 0 || err->trace_level >= 3)138(*err->output_message) (cinfo);139/* Always count warnings in num_warnings. */140err->num_warnings++;141} else {142/* It's a trace message. Show it if trace_level >= msg_level. */143if (err->trace_level >= msg_level)144(*err->output_message) (cinfo);145}146}147148149/*150* Format a message string for the most recent JPEG error or message.151* The message is stored into buffer, which should be at least JMSG_LENGTH_MAX152* characters. Note that no '\n' character is added to the string.153* Few applications should need to override this method.154*/155156METHODDEF(void)157format_message (j_common_ptr cinfo, char * buffer)158{159struct jpeg_error_mgr * err = cinfo->err;160int msg_code = err->msg_code;161const char * msgtext = NULL;162const char * msgptr;163char ch;164boolean isstring;165166/* Look up message string in proper table */167if (msg_code > 0 && msg_code <= err->last_jpeg_message) {168msgtext = err->jpeg_message_table[msg_code];169} else if (err->addon_message_table != NULL &&170msg_code >= err->first_addon_message &&171msg_code <= err->last_addon_message) {172msgtext = err->addon_message_table[msg_code - err->first_addon_message];173}174175/* Defend against bogus message number */176if (msgtext == NULL) {177err->msg_parm.i[0] = msg_code;178msgtext = err->jpeg_message_table[0];179}180181/* Check for string parameter, as indicated by %s in the message text */182isstring = FALSE;183msgptr = msgtext;184while ((ch = *msgptr++) != '\0') {185if (ch == '%') {186if (*msgptr == 's') isstring = TRUE;187break;188}189}190191/* Format the message into the passed buffer */192if (isstring)193sprintf(buffer, msgtext, err->msg_parm.s);194else195sprintf(buffer, msgtext,196err->msg_parm.i[0], err->msg_parm.i[1],197err->msg_parm.i[2], err->msg_parm.i[3],198err->msg_parm.i[4], err->msg_parm.i[5],199err->msg_parm.i[6], err->msg_parm.i[7]);200}201202203/*204* Reset error state variables at start of a new image.205* This is called during compression startup to reset trace/error206* processing to default state, without losing any application-specific207* method pointers. An application might possibly want to override208* this method if it has additional error processing state.209*/210211METHODDEF(void)212reset_error_mgr (j_common_ptr cinfo)213{214cinfo->err->num_warnings = 0;215/* trace_level is not reset since it is an application-supplied parameter */216cinfo->err->msg_code = 0; /* may be useful as a flag for "no error" */217}218219220/*221* Fill in the standard error-handling methods in a jpeg_error_mgr object.222* Typical call is:223* struct jpeg_compress_struct cinfo;224* struct jpeg_error_mgr err;225*226* cinfo.err = jpeg_std_error(&err);227* after which the application may override some of the methods.228*/229230GLOBAL(struct jpeg_error_mgr *)231jpeg_std_error (struct jpeg_error_mgr * err)232{233err->error_exit = error_exit;234err->emit_message = emit_message;235err->output_message = output_message;236err->format_message = format_message;237err->reset_error_mgr = reset_error_mgr;238239err->trace_level = 0; /* default = no tracing */240err->num_warnings = 0; /* no warnings emitted yet */241err->msg_code = 0; /* may be useful as a flag for "no error" */242243/* Initialize message table pointers */244err->jpeg_message_table = jpeg_std_message_table;245err->last_jpeg_message = (int) JMSG_LASTMSGCODE - 1;246247err->addon_message_table = NULL;248err->first_addon_message = 0; /* for safety */249err->last_addon_message = 0;250251return err;252}253254255