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