jerror.c 6.8 KB

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