Repo for the search and displace ingest module that takes odf, docx and pdf and transforms it into .md to be used with search and displace operations
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

462 lines
13 KiB

3 years ago
  1. unit imjerror;
  2. { This file contains simple error-reporting and trace-message routines.
  3. These are suitable for Unix-like systems and others where writing to
  4. stderr is the right thing to do. Many applications will want to replace
  5. some or all of these routines.
  6. These routines are used by both the compression and decompression code. }
  7. { Source: jerror.c; Copyright (C) 1991-1996, Thomas G. Lane. }
  8. { note: format_message still contains a hack }
  9. interface
  10. {$I imjconfig.inc}
  11. uses
  12. imjmorecfg,
  13. imjdeferr,
  14. imjpeglib;
  15. {
  16. jversion;
  17. }
  18. const
  19. EXIT_FAILURE = 1; { define halt() codes if not provided }
  20. {GLOBAL}
  21. function jpeg_std_error (var err : jpeg_error_mgr) : jpeg_error_mgr_ptr;
  22. procedure ERREXIT(cinfo : j_common_ptr; code : J_MESSAGE_CODE);
  23. procedure ERREXIT1(cinfo : j_common_ptr; code : J_MESSAGE_CODE; p1 : uInt);
  24. procedure ERREXIT2(cinfo : j_common_ptr; code : J_MESSAGE_CODE; p1 : int; p2 : int);
  25. procedure ERREXIT3(cinfo : j_common_ptr; code : J_MESSAGE_CODE;
  26. p1 : int; p2 : int; p3 : int);
  27. procedure ERREXIT4(cinfo : j_common_ptr; code : J_MESSAGE_CODE;
  28. p1 : int; p2 : int; p3 : int; p4 : int);
  29. procedure ERREXITS(cinfo : j_common_ptr;code : J_MESSAGE_CODE;
  30. str : AnsiString);
  31. { Nonfatal errors (we can keep going, but the data is probably corrupt) }
  32. procedure WARNMS(cinfo : j_common_ptr; code : J_MESSAGE_CODE);
  33. procedure WARNMS1(cinfo : j_common_ptr;code : J_MESSAGE_CODE; p1 : int);
  34. procedure WARNMS2(cinfo : j_common_ptr; code : J_MESSAGE_CODE;
  35. p1 : int; p2 : int);
  36. { Informational/debugging messages }
  37. procedure TRACEMS(cinfo : j_common_ptr; lvl : int; code : J_MESSAGE_CODE);
  38. procedure TRACEMS1(cinfo : j_common_ptr; lvl : int;
  39. code : J_MESSAGE_CODE; p1 : long);
  40. procedure TRACEMS2(cinfo : j_common_ptr; lvl : int; code : J_MESSAGE_CODE;
  41. p1 : int;
  42. p2 : int);
  43. procedure TRACEMS3(cinfo : j_common_ptr;
  44. lvl : int;
  45. code : J_MESSAGE_CODE;
  46. p1 : int; p2 : int; p3 : int);
  47. procedure TRACEMS4(cinfo : j_common_ptr; lvl : int; code : J_MESSAGE_CODE;
  48. p1 : int; p2 : int; p3 : int; p4 : int);
  49. procedure TRACEMS5(cinfo : j_common_ptr; lvl : int; code : J_MESSAGE_CODE;
  50. p1 : int; p2 : int; p3 : int; p4 : int; p5 : int);
  51. procedure TRACEMS8(cinfo : j_common_ptr; lvl : int; code : J_MESSAGE_CODE;
  52. p1 : int; p2 : int; p3 : int; p4 : int;
  53. p5 : int; p6 : int; p7 : int; p8 : int);
  54. procedure TRACEMSS(cinfo : j_common_ptr; lvl : int;
  55. code : J_MESSAGE_CODE; str : AnsiString);
  56. implementation
  57. { How to format a message string, in format_message() ? }
  58. {$IFDEF OS2}
  59. {$DEFINE NO_FORMAT}
  60. {$ENDIF}
  61. {$IFDEF FPC}
  62. {$DEFINE NO_FORMAT}
  63. {$ENDIF}
  64. uses
  65. {$IFNDEF NO_FORMAT}
  66. {$IFDEF VER70}
  67. drivers, { Turbo Vision unit with FormatStr }
  68. {$ELSE}
  69. sysutils, { Delphi Unit with Format() }
  70. {$ENDIF}
  71. {$ENDIF}
  72. imjcomapi;
  73. { Error exit handler: must not return to caller.
  74. Applications may override this if they want to get control back after
  75. an error. Typically one would longjmp somewhere instead of exiting.
  76. The setjmp buffer can be made a private field within an expanded error
  77. handler object. Note that the info needed to generate an error message
  78. is stored in the error object, so you can generate the message now or
  79. later, at your convenience.
  80. You should make sure that the JPEG object is cleaned up (with jpeg_abort
  81. or jpeg_destroy) at some point. }
  82. {METHODDEF}
  83. procedure error_exit (cinfo : j_common_ptr);
  84. begin
  85. { Always display the message }
  86. cinfo^.err^.output_message(cinfo);
  87. { Let the memory manager delete any temp files before we die }
  88. jpeg_destroy(cinfo);
  89. halt(EXIT_FAILURE);
  90. end;
  91. { Actual output of an error or trace message.
  92. Applications may override this method to send JPEG messages somewhere
  93. other than stderr. }
  94. { Macros to simplify using the error and trace message stuff }
  95. { The first parameter is either type of cinfo pointer }
  96. { Fatal errors (print message and exit) }
  97. procedure ERREXIT(cinfo : j_common_ptr; code : J_MESSAGE_CODE);
  98. begin
  99. cinfo^.err^.msg_code := ord(code);
  100. cinfo^.err^.error_exit(cinfo);
  101. end;
  102. procedure ERREXIT1(cinfo : j_common_ptr; code : J_MESSAGE_CODE; p1 : uInt);
  103. begin
  104. cinfo^.err^.msg_code := ord(code);
  105. cinfo^.err^.msg_parm.i[0] := p1;
  106. cinfo^.err^.error_exit (cinfo);
  107. end;
  108. procedure ERREXIT2(cinfo : j_common_ptr; code : J_MESSAGE_CODE;
  109. p1 : int; p2 : int);
  110. begin
  111. cinfo^.err^.msg_code := ord(code);
  112. cinfo^.err^.msg_parm.i[0] := p1;
  113. cinfo^.err^.msg_parm.i[1] := p2;
  114. cinfo^.err^.error_exit (cinfo);
  115. end;
  116. procedure ERREXIT3(cinfo : j_common_ptr; code : J_MESSAGE_CODE;
  117. p1 : int; p2 : int; p3 : int);
  118. begin
  119. cinfo^.err^.msg_code := ord(code);
  120. cinfo^.err^.msg_parm.i[0] := p1;
  121. cinfo^.err^.msg_parm.i[1] := p2;
  122. cinfo^.err^.msg_parm.i[2] := p3;
  123. cinfo^.err^.error_exit (cinfo);
  124. end;
  125. procedure ERREXIT4(cinfo : j_common_ptr; code : J_MESSAGE_CODE;
  126. p1 : int; p2 : int; p3 : int; p4 : int);
  127. begin
  128. cinfo^.err^.msg_code := ord(code);
  129. cinfo^.err^.msg_parm.i[0] := p1;
  130. cinfo^.err^.msg_parm.i[1] := p2;
  131. cinfo^.err^.msg_parm.i[2] := p3;
  132. cinfo^.err^.msg_parm.i[3] := p4;
  133. cinfo^.err^.error_exit (cinfo);
  134. end;
  135. procedure ERREXITS(cinfo : j_common_ptr;code : J_MESSAGE_CODE;
  136. str : AnsiString);
  137. begin
  138. cinfo^.err^.msg_code := ord(code);
  139. cinfo^.err^.msg_parm.s := str; { string[JMSG_STR_PARM_MAX] }
  140. cinfo^.err^.error_exit (cinfo);
  141. end;
  142. { Nonfatal errors (we can keep going, but the data is probably corrupt) }
  143. procedure WARNMS(cinfo : j_common_ptr; code : J_MESSAGE_CODE);
  144. begin
  145. cinfo^.err^.msg_code := ord(code);
  146. cinfo^.err^.emit_message(cinfo, -1);
  147. end;
  148. procedure WARNMS1(cinfo : j_common_ptr;code : J_MESSAGE_CODE; p1 : int);
  149. begin
  150. cinfo^.err^.msg_code := ord(code);
  151. cinfo^.err^.msg_parm.i[0] := p1;
  152. cinfo^.err^.emit_message (cinfo, -1);
  153. end;
  154. procedure WARNMS2(cinfo : j_common_ptr; code : J_MESSAGE_CODE;
  155. p1 : int; p2 : int);
  156. begin
  157. cinfo^.err^.msg_code := ord(code);
  158. cinfo^.err^.msg_parm.i[0] := p1;
  159. cinfo^.err^.msg_parm.i[1] := p2;
  160. cinfo^.err^.emit_message (cinfo, -1);
  161. end;
  162. { Informational/debugging messages }
  163. procedure TRACEMS(cinfo : j_common_ptr; lvl : int; code : J_MESSAGE_CODE);
  164. begin
  165. cinfo^.err^.msg_code := ord(code);
  166. cinfo^.err^.emit_message(cinfo, lvl);
  167. end;
  168. procedure TRACEMS1(cinfo : j_common_ptr; lvl : int;
  169. code : J_MESSAGE_CODE; p1 : long);
  170. begin
  171. cinfo^.err^.msg_code := ord(code);
  172. cinfo^.err^.msg_parm.i[0] := p1;
  173. cinfo^.err^.emit_message (cinfo, lvl);
  174. end;
  175. procedure TRACEMS2(cinfo : j_common_ptr; lvl : int; code : J_MESSAGE_CODE;
  176. p1 : int;
  177. p2 : int);
  178. begin
  179. cinfo^.err^.msg_code := ord(code);
  180. cinfo^.err^.msg_parm.i[0] := p1;
  181. cinfo^.err^.msg_parm.i[1] := p2;
  182. cinfo^.err^.emit_message (cinfo, lvl);
  183. end;
  184. procedure TRACEMS3(cinfo : j_common_ptr;
  185. lvl : int;
  186. code : J_MESSAGE_CODE;
  187. p1 : int; p2 : int; p3 : int);
  188. var
  189. _mp : int8array;
  190. begin
  191. _mp[0] := p1; _mp[1] := p2; _mp[2] := p3;
  192. cinfo^.err^.msg_parm.i := _mp;
  193. cinfo^.err^.msg_code := ord(code);
  194. cinfo^.err^.emit_message (cinfo, lvl);
  195. end;
  196. procedure TRACEMS4(cinfo : j_common_ptr; lvl : int; code : J_MESSAGE_CODE;
  197. p1 : int; p2 : int; p3 : int; p4 : int);
  198. var
  199. _mp : int8array;
  200. begin
  201. _mp[0] := p1; _mp[1] := p2; _mp[2] := p3; _mp[3] := p4;
  202. cinfo^.err^.msg_parm.i := _mp;
  203. cinfo^.err^.msg_code := ord(code);
  204. cinfo^.err^.emit_message (cinfo, lvl);
  205. end;
  206. procedure TRACEMS5(cinfo : j_common_ptr; lvl : int; code : J_MESSAGE_CODE;
  207. p1 : int; p2 : int; p3 : int; p4 : int; p5 : int);
  208. var
  209. _mp : ^int8array;
  210. begin
  211. _mp := @cinfo^.err^.msg_parm.i;
  212. _mp^[0] := p1; _mp^[1] := p2; _mp^[2] := p3;
  213. _mp^[3] := p4; _mp^[5] := p5;
  214. cinfo^.err^.msg_code := ord(code);
  215. cinfo^.err^.emit_message (cinfo, lvl);
  216. end;
  217. procedure TRACEMS8(cinfo : j_common_ptr; lvl : int; code : J_MESSAGE_CODE;
  218. p1 : int; p2 : int; p3 : int; p4 : int;
  219. p5 : int; p6 : int; p7 : int; p8 : int);
  220. var
  221. _mp : int8array;
  222. begin
  223. _mp[0] := p1; _mp[1] := p2; _mp[2] := p3; _mp[3] := p4;
  224. _mp[4] := p5; _mp[5] := p6; _mp[6] := p7; _mp[7] := p8;
  225. cinfo^.err^.msg_parm.i := _mp;
  226. cinfo^.err^.msg_code := ord(code);
  227. cinfo^.err^.emit_message (cinfo, lvl);
  228. end;
  229. procedure TRACEMSS(cinfo : j_common_ptr; lvl : int;
  230. code : J_MESSAGE_CODE; str : AnsiString);
  231. begin
  232. cinfo^.err^.msg_code := ord(code);
  233. cinfo^.err^.msg_parm.s := str; { string JMSG_STR_PARM_MAX }
  234. cinfo^.err^.emit_message (cinfo, lvl);
  235. end;
  236. {METHODDEF}
  237. procedure output_message (cinfo : j_common_ptr);
  238. var
  239. buffer : AnsiString; {[JMSG_LENGTH_MAX];}
  240. begin
  241. { Create the message }
  242. cinfo^.err^.format_message (cinfo, buffer);
  243. { Send it to stderr, adding a newline }
  244. WriteLn(output, buffer);
  245. end;
  246. { Decide whether to emit a trace or warning message.
  247. msg_level is one of:
  248. -1: recoverable corrupt-data warning, may want to abort.
  249. 0: important advisory messages (always display to user).
  250. 1: first level of tracing detail.
  251. 2,3,...: successively more detailed tracing messages.
  252. An application might override this method if it wanted to abort on warnings
  253. or change the policy about which messages to display. }
  254. {METHODDEF}
  255. procedure emit_message (cinfo : j_common_ptr; msg_level : int);
  256. var
  257. err : jpeg_error_mgr_ptr;
  258. begin
  259. err := cinfo^.err;
  260. if (msg_level < 0) then
  261. begin
  262. { It's a warning message. Since corrupt files may generate many warnings,
  263. the policy implemented here is to show only the first warning,
  264. unless trace_level >= 3. }
  265. if (err^.num_warnings = 0) or (err^.trace_level >= 3) then
  266. err^.output_message(cinfo);
  267. { Always count warnings in num_warnings. }
  268. Inc( err^.num_warnings );
  269. end
  270. else
  271. begin
  272. { It's a trace message. Show it if trace_level >= msg_level. }
  273. if (err^.trace_level >= msg_level) then
  274. err^.output_message (cinfo);
  275. end;
  276. end;
  277. { Format a message string for the most recent JPEG error or message.
  278. The message is stored into buffer, which should be at least JMSG_LENGTH_MAX
  279. characters. Note that no '\n' character is added to the string.
  280. Few applications should need to override this method. }
  281. {METHODDEF}
  282. procedure format_message (cinfo : j_common_ptr; var buffer : AnsiString);
  283. var
  284. err : jpeg_error_mgr_ptr;
  285. msg_code : J_MESSAGE_CODE;
  286. msgtext : AnsiString;
  287. isstring : boolean;
  288. begin
  289. err := cinfo^.err;
  290. msg_code := J_MESSAGE_CODE(err^.msg_code);
  291. msgtext := '';
  292. { Look up message string in proper table }
  293. if (msg_code > JMSG_NOMESSAGE)
  294. and (msg_code <= J_MESSAGE_CODE(err^.last_jpeg_message)) then
  295. begin
  296. msgtext := err^.jpeg_message_table^[msg_code];
  297. end
  298. else
  299. if (err^.addon_message_table <> NIL) and
  300. (msg_code >= err^.first_addon_message) and
  301. (msg_code <= err^.last_addon_message) then
  302. begin
  303. msgtext := err^.addon_message_table^[J_MESSAGE_CODE
  304. (ord(msg_code) - ord(err^.first_addon_message))];
  305. end;
  306. { Defend against bogus message number }
  307. if (msgtext = '') then
  308. begin
  309. err^.msg_parm.i[0] := int(msg_code);
  310. msgtext := err^.jpeg_message_table^[JMSG_NOMESSAGE];
  311. end;
  312. { Check for string parameter, as indicated by %s in the message text }
  313. isstring := Pos('%s', msgtext) > 0;
  314. { Format the message into the passed buffer }
  315. if (isstring) then
  316. buffer := Concat(msgtext, err^.msg_parm.s)
  317. else
  318. begin
  319. {$IFDEF VER70}
  320. FormatStr(buffer, msgtext, err^.msg_parm.i);
  321. {$ELSE}
  322. {$IFDEF NO_FORMAT}
  323. buffer := msgtext;
  324. {$ELSE}
  325. buffer := Format(msgtext, [
  326. err^.msg_parm.i[0], err^.msg_parm.i[1],
  327. err^.msg_parm.i[2], err^.msg_parm.i[3],
  328. err^.msg_parm.i[4], err^.msg_parm.i[5],
  329. err^.msg_parm.i[6], err^.msg_parm.i[7] ]);
  330. {$ENDIF}
  331. {$ENDIF}
  332. end;
  333. end;
  334. { Reset error state variables at start of a new image.
  335. This is called during compression startup to reset trace/error
  336. processing to default state, without losing any application-specific
  337. method pointers. An application might possibly want to override
  338. this method if it has additional error processing state. }
  339. {METHODDEF}
  340. procedure reset_error_mgr (cinfo : j_common_ptr);
  341. begin
  342. cinfo^.err^.num_warnings := 0;
  343. { trace_level is not reset since it is an application-supplied parameter }
  344. cinfo^.err^.msg_code := 0; { may be useful as a flag for "no error" }
  345. end;
  346. { Fill in the standard error-handling methods in a jpeg_error_mgr object.
  347. Typical call is:
  348. cinfo : jpeg_compress_struct;
  349. err : jpeg_error_mgr;
  350. cinfo.err := jpeg_std_error(@err);
  351. after which the application may override some of the methods. }
  352. {GLOBAL}
  353. function jpeg_std_error (var err : jpeg_error_mgr) : jpeg_error_mgr_ptr;
  354. begin
  355. err.error_exit := error_exit;
  356. err.emit_message := emit_message;
  357. err.output_message := output_message;
  358. err.format_message := format_message;
  359. err.reset_error_mgr := reset_error_mgr;
  360. err.trace_level := 0; { default := no tracing }
  361. err.num_warnings := 0; { no warnings emitted yet }
  362. err.msg_code := 0; { may be useful as a flag for "no error" }
  363. { Initialize message table pointers }
  364. err.jpeg_message_table := @jpeg_std_message_table;
  365. err.last_jpeg_message := pred(JMSG_LASTMSGCODE);
  366. err.addon_message_table := NIL;
  367. err.first_addon_message := JMSG_NOMESSAGE; { for safety }
  368. err.last_addon_message := JMSG_NOMESSAGE;
  369. jpeg_std_error := @err;
  370. end;
  371. end.