kusano 7d535a
/*
kusano 7d535a
 * jerror.c
kusano 7d535a
 *
shun-iwasawa 82a8f5
 * This file was part of the Independent JPEG Group's software:
kusano 7d535a
 * Copyright (C) 1991-1998, Thomas G. Lane.
shun-iwasawa 82a8f5
 * It was modified by The libjpeg-turbo Project to include only code relevant
shun-iwasawa 82a8f5
 * to libjpeg-turbo.
shun-iwasawa 82a8f5
 * For conditions of distribution and use, see the accompanying README.ijg
shun-iwasawa 82a8f5
 * 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
shun-iwasawa 82a8f5
#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
shun-iwasawa 82a8f5
#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
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
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)
shun-iwasawa 82a8f5
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",
shun-iwasawa 82a8f5
             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)
shun-iwasawa 82a8f5
emit_message(j_common_ptr cinfo, int msg_level)
kusano 7d535a
{
shun-iwasawa 82a8f5
  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)
shun-iwasawa 82a8f5
format_message(j_common_ptr cinfo, char *buffer)
kusano 7d535a
{
shun-iwasawa 82a8f5
  struct jpeg_error_mgr *err = cinfo->err;
kusano 7d535a
  int msg_code = err->msg_code;
shun-iwasawa 82a8f5
  const char *msgtext = NULL;
shun-iwasawa 82a8f5
  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 &&
shun-iwasawa 82a8f5
             msg_code >= err->first_addon_message &&
shun-iwasawa 82a8f5
             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,
shun-iwasawa 82a8f5
            err->msg_parm.i[0], err->msg_parm.i[1],
shun-iwasawa 82a8f5
            err->msg_parm.i[2], err->msg_parm.i[3],
shun-iwasawa 82a8f5
            err->msg_parm.i[4], err->msg_parm.i[5],
shun-iwasawa 82a8f5
            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)
shun-iwasawa 82a8f5
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 */
shun-iwasawa 82a8f5
  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:
shun-iwasawa 82a8f5
 *      struct jpeg_compress_struct cinfo;
shun-iwasawa 82a8f5
 *      struct jpeg_error_mgr err;
kusano 7d535a
 *
shun-iwasawa 82a8f5
 *      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 *)
shun-iwasawa 82a8f5
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
shun-iwasawa 82a8f5
  err->trace_level = 0;         /* default = no tracing */
shun-iwasawa 82a8f5
  err->num_warnings = 0;        /* no warnings emitted yet */
shun-iwasawa 82a8f5
  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;
shun-iwasawa 82a8f5
  err->last_jpeg_message = (int)JMSG_LASTMSGCODE - 1;
kusano 7d535a
kusano 7d535a
  err->addon_message_table = NULL;
shun-iwasawa 82a8f5
  err->first_addon_message = 0; /* for safety */
kusano 7d535a
  err->last_addon_message = 0;
kusano 7d535a
kusano 7d535a
  return err;
kusano 7d535a
}