jerror.c 7.7 KB

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