jerror.cpp 6.7 KB

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