jerror.c 7.7 KB

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