jerror.cpp 7.0 KB

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