jerror.c 7.7 KB

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