Path: blob/master/src/java.desktop/share/native/libjavajpeg/jerror.c
41149 views
/*1* reserved comment block2* DO NOT REMOVE OR ALTER!3*/4/*5* jerror.c6*7* Copyright (C) 1991-1998, Thomas G. Lane.8* This file is part of the Independent JPEG Group's software.9* For conditions of distribution and use, see the accompanying README file.10*11* This file contains simple error-reporting and trace-message routines.12* These are suitable for Unix-like systems and others where writing to13* stderr is the right thing to do. Many applications will want to replace14* some or all of these routines.15*16* If you define USE_WINDOWS_MESSAGEBOX in jconfig.h or in the makefile,17* you get a Windows-specific hack to display error messages in a dialog box.18* It ain't much, but it beats dropping error messages into the bit bucket,19* which is what happens to output to stderr under most Windows C compilers.20*21* These routines are used by both the compression and decompression code.22*/2324/* this is not a core library module, so it doesn't define JPEG_INTERNALS */25#include "jinclude.h"26#include "jpeglib.h"27#include "jversion.h"28#include "jerror.h"2930#ifdef USE_WINDOWS_MESSAGEBOX31#include <windows.h>32#endif3334#ifndef EXIT_FAILURE /* define exit() codes if not provided */35#define EXIT_FAILURE 136#endif373839/*40* Create the message string table.41* We do this from the master message list in jerror.h by re-reading42* jerror.h with a suitable definition for macro JMESSAGE.43* The message table is made an external symbol just in case any applications44* want to refer to it directly.45*/4647#ifdef NEED_SHORT_EXTERNAL_NAMES48#define jpeg_std_message_table jMsgTable49#endif5051#define JMESSAGE(code,string) string ,5253const char * const jpeg_std_message_table[] = {54#include "jerror.h"55NULL56};575859/*60* Error exit handler: must not return to caller.61*62* Applications may override this if they want to get control back after63* an error. Typically one would longjmp somewhere instead of exiting.64* The setjmp buffer can be made a private field within an expanded error65* handler object. Note that the info needed to generate an error message66* is stored in the error object, so you can generate the message now or67* later, at your convenience.68* You should make sure that the JPEG object is cleaned up (with jpeg_abort69* or jpeg_destroy) at some point.70*/7172METHODDEF(void)73error_exit (j_common_ptr cinfo)74{75/* Always display the message */76(*cinfo->err->output_message) (cinfo);7778/* Let the memory manager delete any temp files before we die */79jpeg_destroy(cinfo);8081/*82* This should never happen since the Java library replaces the83* error_exit pointer in the error handler structs it uses.84*85* exit(EXIT_FAILURE);86*/87}888990/*91* Actual output of an error or trace message.92* Applications may override this method to send JPEG messages somewhere93* other than stderr.94*95* On Windows, printing to stderr is generally completely useless,96* so we provide optional code to produce an error-dialog popup.97* Most Windows applications will still prefer to override this routine,98* but if they don't, it'll do something at least marginally useful.99*100* NOTE: to use the library in an environment that doesn't support the101* C stdio library, you may have to delete the call to fprintf() entirely,102* not just not use this routine.103*/104105METHODDEF(void)106output_message (j_common_ptr cinfo)107{108char buffer[JMSG_LENGTH_MAX];109110/* Create the message */111(*cinfo->err->format_message) (cinfo, buffer);112113#ifdef USE_WINDOWS_MESSAGEBOX114/* Display it in a message dialog box */115MessageBox(GetActiveWindow(), buffer, "JPEG Library Error",116MB_OK | MB_ICONERROR);117#else118/* Send it to stderr, adding a newline */119fprintf(stderr, "%s\n", buffer);120#endif121}122123124/*125* Decide whether to emit a trace or warning message.126* msg_level is one of:127* -1: recoverable corrupt-data warning, may want to abort.128* 0: important advisory messages (always display to user).129* 1: first level of tracing detail.130* 2,3,...: successively more detailed tracing messages.131* An application might override this method if it wanted to abort on warnings132* or change the policy about which messages to display.133*/134135METHODDEF(void)136emit_message (j_common_ptr cinfo, int msg_level)137{138struct jpeg_error_mgr * err = cinfo->err;139140if (msg_level < 0) {141/* It's a warning message. Since corrupt files may generate many warnings,142* the policy implemented here is to show only the first warning,143* unless trace_level >= 3.144*/145if (err->num_warnings == 0 || err->trace_level >= 3)146(*err->output_message) (cinfo);147/* Always count warnings in num_warnings. */148err->num_warnings++;149} else {150/* It's a trace message. Show it if trace_level >= msg_level. */151if (err->trace_level >= msg_level)152(*err->output_message) (cinfo);153}154}155156157/*158* Format a message string for the most recent JPEG error or message.159* The message is stored into buffer, which should be at least JMSG_LENGTH_MAX160* characters. Note that no '\n' character is added to the string.161* Few applications should need to override this method.162*/163164METHODDEF(void)165format_message (j_common_ptr cinfo, char * buffer)166{167168/* Had to kill this function altogether169to avoid linking to VM when building the splash screen with static libjpeg */170171#ifndef SPLASHSCREEN172int jio_snprintf(char *str, size_t count, const char *fmt, ...);173struct jpeg_error_mgr * err = cinfo->err;174int msg_code = err->msg_code;175const char * msgtext = NULL;176const char * msgptr;177char ch;178boolean isstring;179180/* Look up message string in proper table */181if (msg_code > 0 && msg_code <= err->last_jpeg_message) {182msgtext = err->jpeg_message_table[msg_code];183} else if (err->addon_message_table != NULL &&184msg_code >= err->first_addon_message &&185msg_code <= err->last_addon_message) {186msgtext = err->addon_message_table[msg_code - err->first_addon_message];187}188189/* Defend against bogus message number */190if (msgtext == NULL) {191err->msg_parm.i[0] = msg_code;192msgtext = err->jpeg_message_table[0];193}194195/* Check for string parameter, as indicated by %s in the message text */196isstring = FALSE;197msgptr = msgtext;198while ((ch = *msgptr++) != '\0') {199if (ch == '%') {200if (*msgptr == 's') isstring = TRUE;201break;202}203}204205/* Format the message into the passed buffer */206if (isstring)207/* Buffer size is JMSG_LENGTH_MAX, quietly truncate on overflow */208(void) jio_snprintf(buffer, JMSG_LENGTH_MAX, msgtext, err->msg_parm.s);209else210/* Buffer size is JMSG_LENGTH_MAX, quietly truncate on overflow */211(void) jio_snprintf(buffer, JMSG_LENGTH_MAX, msgtext,212err->msg_parm.i[0], err->msg_parm.i[1],213err->msg_parm.i[2], err->msg_parm.i[3],214err->msg_parm.i[4], err->msg_parm.i[5],215err->msg_parm.i[6], err->msg_parm.i[7]);216#else /* SPLASHSCREEN */217*buffer = '\0';218#endif /* SPLASHSCREEN */219}220221222/*223* Reset error state variables at start of a new image.224* This is called during compression startup to reset trace/error225* processing to default state, without losing any application-specific226* method pointers. An application might possibly want to override227* this method if it has additional error processing state.228*/229230METHODDEF(void)231reset_error_mgr (j_common_ptr cinfo)232{233cinfo->err->num_warnings = 0;234/* trace_level is not reset since it is an application-supplied parameter */235cinfo->err->msg_code = 0; /* may be useful as a flag for "no error" */236}237238239/*240* Fill in the standard error-handling methods in a jpeg_error_mgr object.241* Typical call is:242* struct jpeg_compress_struct cinfo;243* struct jpeg_error_mgr err;244*245* cinfo.err = jpeg_std_error(&err);246* after which the application may override some of the methods.247*/248249GLOBAL(struct jpeg_error_mgr *)250jpeg_std_error (struct jpeg_error_mgr * err)251{252err->error_exit = error_exit;253err->emit_message = emit_message;254err->output_message = output_message;255err->format_message = format_message;256err->reset_error_mgr = reset_error_mgr;257258err->trace_level = 0; /* default = no tracing */259err->num_warnings = 0; /* no warnings emitted yet */260err->msg_code = 0; /* may be useful as a flag for "no error" */261262/* Initialize message table pointers */263err->jpeg_message_table = jpeg_std_message_table;264err->last_jpeg_message = (int) JMSG_LASTMSGCODE - 1;265266err->addon_message_table = NULL;267err->first_addon_message = 0; /* for safety */268err->last_addon_message = 0;269270return err;271}272273274