jerror.c 7.7 KB

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