Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

251 lines
7.1 KiB

  1. /*
  2. * jerror.c
  3. *
  4. * Copyright (C) 1991-1994, Thomas G. Lane.
  5. * This file is part of the Independent JPEG Group's software.
  6. * For conditions of distribution and use, see the accompanying README file.
  7. *
  8. * This file contains simple error-reporting and trace-message routines.
  9. * These are suitable for Unix-like systems and others where writing to
  10. * stderr is the right thing to do. Many applications will want to replace
  11. * some or all of these routines.
  12. *
  13. * These routines are used by both the compression and decompression code.
  14. */
  15. // Workaround for redefinition of INT32
  16. #define XMD_H 1
  17. /* this is not a core library module, so it doesn't define JPEG_INTERNALS */
  18. #include "jinclude.h"
  19. #include "jpeglib.h"
  20. #include "jversion.h"
  21. #include "jerror.h"
  22. #ifdef WIN32
  23. #include <windows.h>
  24. #endif
  25. #ifndef EXIT_FAILURE /* define exit() codes if not provided */
  26. #define EXIT_FAILURE 1
  27. #endif
  28. /*
  29. * Create the message string table.
  30. * We do this from the master message list in jerror.h by re-reading
  31. * jerror.h with a suitable definition for macro JMESSAGE.
  32. * The message table is made an external symbol just in case any applications
  33. * want to refer to it directly.
  34. */
  35. #ifdef NEED_SHORT_EXTERNAL_NAMES
  36. #define jpeg_std_message_table jMsgTable
  37. #endif
  38. #define JMESSAGE(code,string) string ,
  39. const char * const jpeg_std_message_table[] = {
  40. #include "jerror.h"
  41. NULL
  42. };
  43. /*
  44. * Error exit handler: must not return to caller.
  45. *
  46. * Applications may override this if they want to get control back after
  47. * an error. Typically one would longjmp somewhere instead of exiting.
  48. * The setjmp buffer can be made a private field within an expanded error
  49. * handler object. Note that the info needed to generate an error message
  50. * is stored in the error object, so you can generate the message now or
  51. * later, at your convenience.
  52. * You should make sure that the JPEG object is cleaned up (with jpeg_abort
  53. * or jpeg_destroy) at some point.
  54. */
  55. METHODDEF void
  56. error_exit (j_common_ptr cinfo)
  57. {
  58. #ifdef DEBUG
  59. ::OutputDebugString("JPEGLIB:Unexpectedly came to error condition.\n");
  60. DebugBreak();
  61. #endif
  62. /* Always display the message */
  63. (*cinfo->err->output_message) (cinfo);
  64. /* Let the memory manager delete any temp files before we die */
  65. jpeg_destroy(cinfo);
  66. #ifndef WIN32
  67. exit(EXIT_FAILURE);
  68. #else
  69. throw 99;
  70. #endif
  71. }
  72. /*
  73. * Actual output of an error or trace message.
  74. * Applications may override this method to send JPEG messages somewhere
  75. * other than stderr.
  76. */
  77. METHODDEF void
  78. output_message (j_common_ptr cinfo)
  79. {
  80. char buffer[JMSG_LENGTH_MAX];
  81. /* Create the message */
  82. (*cinfo->err->format_message) (cinfo, buffer);
  83. /* Send it to stderr, adding a newline */
  84. #ifdef WIN32
  85. #ifdef DEBUG
  86. ::OutputDebugString(buffer);
  87. ::OutputDebugString("\n");
  88. #endif
  89. #else
  90. fprintf(stderr, "%s\n", buffer);
  91. #endif
  92. }
  93. /*
  94. * Decide whether to emit a trace or warning message.
  95. * msg_level is one of:
  96. * -1: recoverable corrupt-data warning, may want to abort.
  97. * 0: important advisory messages (always display to user).
  98. * 1: first level of tracing detail.
  99. * 2,3,...: successively more detailed tracing messages.
  100. * An application might override this method if it wanted to abort on warnings
  101. * or change the policy about which messages to display.
  102. */
  103. METHODDEF void
  104. emit_message (j_common_ptr cinfo, int msg_level)
  105. {
  106. struct jpeg_error_mgr * err = cinfo->err;
  107. if (msg_level < 0) {
  108. /* It's a warning message. Since corrupt files may generate many warnings,
  109. * the policy implemented here is to show only the first warning,
  110. * unless trace_level >= 3.
  111. */
  112. if (err->num_warnings == 0 || err->trace_level >= 3)
  113. (*err->output_message) (cinfo);
  114. /* Always count warnings in num_warnings. */
  115. err->num_warnings++;
  116. } else {
  117. /* It's a trace message. Show it if trace_level >= msg_level. */
  118. if (err->trace_level >= msg_level)
  119. (*err->output_message) (cinfo);
  120. }
  121. }
  122. /*
  123. * Format a message string for the most recent JPEG error or message.
  124. * The message is stored into buffer, which should be at least JMSG_LENGTH_MAX
  125. * characters. Note that no '\n' character is added to the string.
  126. * Few applications should need to override this method.
  127. */
  128. METHODDEF void
  129. format_message (j_common_ptr cinfo, char * buffer)
  130. {
  131. struct jpeg_error_mgr * err = cinfo->err;
  132. int msg_code = err->msg_code;
  133. const char * msgtext = NULL;
  134. const char * msgptr;
  135. char ch;
  136. boolean isstring;
  137. /* Look up message string in proper table */
  138. if (msg_code > 0 && msg_code <= err->last_jpeg_message) {
  139. msgtext = err->jpeg_message_table[msg_code];
  140. } else if (err->addon_message_table != NULL &&
  141. msg_code >= err->first_addon_message &&
  142. msg_code <= err->last_addon_message) {
  143. msgtext = err->addon_message_table[msg_code - err->first_addon_message];
  144. }
  145. /* Defend against bogus message number */
  146. if (msgtext == NULL) {
  147. err->msg_parm.i[0] = msg_code;
  148. msgtext = err->jpeg_message_table[0];
  149. }
  150. /* Check for string parameter, as indicated by %s in the message text */
  151. isstring = FALSE;
  152. msgptr = msgtext;
  153. while ((ch = *msgptr++) != '\0') {
  154. if (ch == '%') {
  155. if (*msgptr == 's') isstring = TRUE;
  156. break;
  157. }
  158. }
  159. /* Format the message into the passed buffer */
  160. if (isstring)
  161. ::wsprintf(buffer, msgtext, err->msg_parm.s);
  162. else
  163. ::wsprintf(buffer, msgtext,
  164. err->msg_parm.i[0], err->msg_parm.i[1],
  165. err->msg_parm.i[2], err->msg_parm.i[3],
  166. err->msg_parm.i[4], err->msg_parm.i[5],
  167. err->msg_parm.i[6], err->msg_parm.i[7]);
  168. }
  169. /*
  170. * Reset error state variables at start of a new image.
  171. * This is called during compression startup to reset trace/error
  172. * processing to default state, without losing any application-specific
  173. * method pointers. An application might possibly want to override
  174. * this method if it has additional error processing state.
  175. */
  176. METHODDEF void
  177. reset_error_mgr (j_common_ptr cinfo)
  178. {
  179. cinfo->err->num_warnings = 0;
  180. /* trace_level is not reset since it is an application-supplied parameter */
  181. cinfo->err->msg_code = 0; /* may be useful as a flag for "no error" */
  182. }
  183. /*
  184. * Fill in the standard error-handling methods in a jpeg_error_mgr object.
  185. * Typical call is:
  186. * struct jpeg_compress_struct cinfo;
  187. * struct jpeg_error_mgr err;
  188. *
  189. * cinfo.err = jpeg_std_error(&err);
  190. * after which the application may override some of the methods.
  191. */
  192. GLOBAL struct jpeg_error_mgr *
  193. jpeg_std_error (struct jpeg_error_mgr * err)
  194. {
  195. err->error_exit = error_exit;
  196. err->emit_message = emit_message;
  197. err->output_message = output_message;
  198. err->format_message = format_message;
  199. err->reset_error_mgr = reset_error_mgr;
  200. err->trace_level = 0; /* default = no tracing */
  201. err->num_warnings = 0; /* no warnings emitted yet */
  202. err->msg_code = 0; /* may be useful as a flag for "no error" */
  203. /* Initialize message table pointers */
  204. err->jpeg_message_table = jpeg_std_message_table;
  205. err->last_jpeg_message = (int) JMSG_LASTMSGCODE - 1;
  206. err->addon_message_table = NULL;
  207. err->first_addon_message = 0; /* for safety */
  208. err->last_addon_message = 0;
  209. return err;
  210. }