jerror.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 void jpg_Error( const char * fmt, ... );
  21. extern void 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. char buffer[JMSG_LENGTH_MAX];
  55. /* Create the message */
  56. ( *cinfo->err->format_message )( cinfo, buffer );
  57. /* Let the memory manager delete any temp files before we die */
  58. jpeg_destroy( cinfo );
  59. jpg_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. char buffer[JMSG_LENGTH_MAX];
  69. /* Create the message */
  70. ( *cinfo->err->format_message )( cinfo, buffer );
  71. /* Send it to stderr, adding a newline */
  72. jpg_Printf( "%s\n", buffer );
  73. }
  74. /*
  75. * Decide whether to emit a trace or warning message.
  76. * msg_level is one of:
  77. * -1: recoverable corrupt-data warning, may want to abort.
  78. * 0: important advisory messages (always display to user).
  79. * 1: first level of tracing detail.
  80. * 2,3,...: successively more detailed tracing messages.
  81. * An application might override this method if it wanted to abort on warnings
  82. * or change the policy about which messages to display.
  83. */
  84. METHODDEF void
  85. emit_message( j_common_ptr cinfo, int msg_level ) {
  86. struct jpeg_error_mgr * err = cinfo->err;
  87. if ( msg_level < 0 ) {
  88. /* It's a warning message. Since corrupt files may generate many warnings,
  89. * the policy implemented here is to show only the first warning,
  90. * unless trace_level >= 3.
  91. */
  92. if ( ( err->num_warnings == 0 ) || ( err->trace_level >= 3 ) ) {
  93. ( *err->output_message )( cinfo );
  94. }
  95. /* Always count warnings in num_warnings. */
  96. err->num_warnings++;
  97. } else {
  98. /* It's a trace message. Show it if trace_level >= msg_level. */
  99. if ( err->trace_level >= msg_level ) {
  100. ( *err->output_message )( cinfo );
  101. }
  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. struct jpeg_error_mgr * err = cinfo->err;
  113. int msg_code = err->msg_code;
  114. const char * msgtext = NULL;
  115. const char * msgptr;
  116. char ch;
  117. boolean isstring;
  118. /* Look up message string in proper table */
  119. if ( ( msg_code > 0 ) && ( msg_code <= err->last_jpeg_message ) ) {
  120. msgtext = err->jpeg_message_table[msg_code];
  121. } else if ( err->addon_message_table != NULL &&
  122. msg_code >= err->first_addon_message &&
  123. msg_code <= err->last_addon_message ) {
  124. msgtext = err->addon_message_table[msg_code - err->first_addon_message];
  125. }
  126. /* Defend against bogus message number */
  127. if ( msgtext == NULL ) {
  128. err->msg_parm.i[0] = msg_code;
  129. msgtext = err->jpeg_message_table[0];
  130. }
  131. /* Check for string parameter, as indicated by %s in the message text */
  132. isstring = FALSE;
  133. msgptr = msgtext;
  134. while ( ( ch = *msgptr++ ) != '\0' ) {
  135. if ( ch == '%' ) {
  136. if ( *msgptr == 's' ) {
  137. isstring = TRUE;
  138. }
  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. /*
  154. * Reset error state variables at start of a new image.
  155. * This is called during compression startup to reset trace/error
  156. * processing to default state, without losing any application-specific
  157. * method pointers. An application might possibly want to override
  158. * this method if it has additional error processing state.
  159. */
  160. METHODDEF void
  161. reset_error_mgr( j_common_ptr cinfo ) {
  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. 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. }