Path: blob/master/3rdparty/libjpeg-turbo/src/jerror.c
16337 views
/*1* jerror.c2*3* This file was part of the Independent JPEG Group's software:4* Copyright (C) 1991-1998, Thomas G. Lane.5* It was modified by The libjpeg-turbo Project to include only code relevant6* to libjpeg-turbo.7* For conditions of distribution and use, see the accompanying README.ijg8* file.9*10* This file contains simple error-reporting and trace-message routines.11* These are suitable for Unix-like systems and others where writing to12* stderr is the right thing to do. Many applications will want to replace13* some or all of these routines.14*15* If you define USE_WINDOWS_MESSAGEBOX in jconfig.h or in the makefile,16* you get a Windows-specific hack to display error messages in a dialog box.17* It ain't much, but it beats dropping error messages into the bit bucket,18* which is what happens to output to stderr under most Windows C compilers.19*20* These routines are used by both the compression and decompression code.21*/2223/* this is not a core library module, so it doesn't define JPEG_INTERNALS */24#include "jinclude.h"25#include "jpeglib.h"26#include "jversion.h"27#include "jerror.h"2829#ifdef USE_WINDOWS_MESSAGEBOX30#include <windows.h>31#endif3233#ifndef EXIT_FAILURE /* define exit() codes if not provided */34#define EXIT_FAILURE 135#endif363738/*39* Create the message string table.40* We do this from the master message list in jerror.h by re-reading41* jerror.h with a suitable definition for macro JMESSAGE.42* The message table is made an external symbol just in case any applications43* want to refer to it directly.44*/4546#define JMESSAGE(code,string) string ,4748const char * const jpeg_std_message_table[] = {49#include "jerror.h"50NULL51};525354/*55* Error exit handler: must not return to caller.56*57* Applications may override this if they want to get control back after58* an error. Typically one would longjmp somewhere instead of exiting.59* The setjmp buffer can be made a private field within an expanded error60* handler object. Note that the info needed to generate an error message61* is stored in the error object, so you can generate the message now or62* later, at your convenience.63* You should make sure that the JPEG object is cleaned up (with jpeg_abort64* or jpeg_destroy) at some point.65*/6667METHODDEF(void)68error_exit (j_common_ptr cinfo)69{70/* Always display the message */71(*cinfo->err->output_message) (cinfo);7273/* Let the memory manager delete any temp files before we die */74jpeg_destroy(cinfo);7576exit(EXIT_FAILURE);77}787980/*81* Actual output of an error or trace message.82* Applications may override this method to send JPEG messages somewhere83* other than stderr.84*85* On Windows, printing to stderr is generally completely useless,86* so we provide optional code to produce an error-dialog popup.87* Most Windows applications will still prefer to override this routine,88* but if they don't, it'll do something at least marginally useful.89*90* NOTE: to use the library in an environment that doesn't support the91* C stdio library, you may have to delete the call to fprintf() entirely,92* not just not use this routine.93*/9495METHODDEF(void)96output_message (j_common_ptr cinfo)97{98char buffer[JMSG_LENGTH_MAX];99100/* Create the message */101(*cinfo->err->format_message) (cinfo, buffer);102103#ifdef USE_WINDOWS_MESSAGEBOX104/* Display it in a message dialog box */105MessageBox(GetActiveWindow(), buffer, "JPEG Library Error",106MB_OK | MB_ICONERROR);107#else108/* Send it to stderr, adding a newline */109fprintf(stderr, "%s\n", buffer);110#endif111}112113114/*115* Decide whether to emit a trace or warning message.116* msg_level is one of:117* -1: recoverable corrupt-data warning, may want to abort.118* 0: important advisory messages (always display to user).119* 1: first level of tracing detail.120* 2,3,...: successively more detailed tracing messages.121* An application might override this method if it wanted to abort on warnings122* or change the policy about which messages to display.123*/124125METHODDEF(void)126emit_message (j_common_ptr cinfo, int msg_level)127{128struct jpeg_error_mgr *err = cinfo->err;129130if (msg_level < 0) {131/* It's a warning message. Since corrupt files may generate many warnings,132* the policy implemented here is to show only the first warning,133* unless trace_level >= 3.134*/135if (err->num_warnings == 0 || err->trace_level >= 3)136(*err->output_message) (cinfo);137/* Always count warnings in num_warnings. */138err->num_warnings++;139} else {140/* It's a trace message. Show it if trace_level >= msg_level. */141if (err->trace_level >= msg_level)142(*err->output_message) (cinfo);143}144}145146147/*148* Format a message string for the most recent JPEG error or message.149* The message is stored into buffer, which should be at least JMSG_LENGTH_MAX150* characters. Note that no '\n' character is added to the string.151* Few applications should need to override this method.152*/153154METHODDEF(void)155format_message (j_common_ptr cinfo, char *buffer)156{157struct jpeg_error_mgr *err = cinfo->err;158int msg_code = err->msg_code;159const char *msgtext = NULL;160const char *msgptr;161char ch;162boolean isstring;163164/* Look up message string in proper table */165if (msg_code > 0 && msg_code <= err->last_jpeg_message) {166msgtext = err->jpeg_message_table[msg_code];167} else if (err->addon_message_table != NULL &&168msg_code >= err->first_addon_message &&169msg_code <= err->last_addon_message) {170msgtext = err->addon_message_table[msg_code - err->first_addon_message];171}172173/* Defend against bogus message number */174if (msgtext == NULL) {175err->msg_parm.i[0] = msg_code;176msgtext = err->jpeg_message_table[0];177}178179/* Check for string parameter, as indicated by %s in the message text */180isstring = FALSE;181msgptr = msgtext;182while ((ch = *msgptr++) != '\0') {183if (ch == '%') {184if (*msgptr == 's') isstring = TRUE;185break;186}187}188189/* Format the message into the passed buffer */190if (isstring)191sprintf(buffer, msgtext, err->msg_parm.s);192else193sprintf(buffer, msgtext,194err->msg_parm.i[0], err->msg_parm.i[1],195err->msg_parm.i[2], err->msg_parm.i[3],196err->msg_parm.i[4], err->msg_parm.i[5],197err->msg_parm.i[6], err->msg_parm.i[7]);198}199200201/*202* Reset error state variables at start of a new image.203* This is called during compression startup to reset trace/error204* processing to default state, without losing any application-specific205* method pointers. An application might possibly want to override206* this method if it has additional error processing state.207*/208209METHODDEF(void)210reset_error_mgr (j_common_ptr cinfo)211{212cinfo->err->num_warnings = 0;213/* trace_level is not reset since it is an application-supplied parameter */214cinfo->err->msg_code = 0; /* may be useful as a flag for "no error" */215}216217218/*219* Fill in the standard error-handling methods in a jpeg_error_mgr object.220* Typical call is:221* struct jpeg_compress_struct cinfo;222* struct jpeg_error_mgr err;223*224* cinfo.err = jpeg_std_error(&err);225* after which the application may override some of the methods.226*/227228GLOBAL(struct jpeg_error_mgr *)229jpeg_std_error (struct jpeg_error_mgr *err)230{231err->error_exit = error_exit;232err->emit_message = emit_message;233err->output_message = output_message;234err->format_message = format_message;235err->reset_error_mgr = reset_error_mgr;236237err->trace_level = 0; /* default = no tracing */238err->num_warnings = 0; /* no warnings emitted yet */239err->msg_code = 0; /* may be useful as a flag for "no error" */240241/* Initialize message table pointers */242err->jpeg_message_table = jpeg_std_message_table;243err->last_jpeg_message = (int) JMSG_LASTMSGCODE - 1;244245err->addon_message_table = NULL;246err->first_addon_message = 0; /* for safety */247err->last_addon_message = 0;248249return err;250}251252253