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.

242 lines
6.9 KiB

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