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