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.

520 lines
17 KiB

3 years ago
  1. Unit impaszlib;
  2. { Original:
  3. zlib.h -- interface of the 'zlib' general purpose compression library
  4. version 1.1.0, Feb 24th, 1998
  5. Copyright (C) 1995-1998 Jean-loup Gailly and Mark Adler
  6. This software is provided 'as-is', without any express or implied
  7. warranty. In no event will the authors be held liable for any damages
  8. arising from the use of this software.
  9. Permission is granted to anyone to use this software for any purpose,
  10. including commercial applications, and to alter it and redistribute it
  11. freely, subject to the following restrictions:
  12. 1. The origin of this software must not be misrepresented; you must not
  13. claim that you wrote the original software. If you use this software
  14. in a product, an acknowledgment in the product documentation would be
  15. appreciated but is not required.
  16. 2. Altered source versions must be plainly marked as such, and must not be
  17. misrepresented as being the original software.
  18. 3. This notice may not be removed or altered from any source distribution.
  19. Jean-loup Gailly Mark Adler
  20. jloup@gzip.org madler@alumni.caltech.edu
  21. The data format used by the zlib library is described by RFCs (Request for
  22. Comments) 1950 to 1952 in the files ftp://ds.internic.net/rfc/rfc1950.txt
  23. (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
  24. Pascal tranlastion
  25. Copyright (C) 1998 by Jacques Nomssi Nzali
  26. For conditions of distribution and use, see copyright notice in readme.txt
  27. }
  28. interface
  29. {$I imzconf.inc}
  30. uses
  31. imzutil;
  32. { zconf.h -- configuration of the zlib compression library }
  33. { zutil.c -- target dependent utility functions for the compression library }
  34. { The 'zlib' compression library provides in-memory compression and
  35. decompression functions, including integrity checks of the uncompressed
  36. data. This version of the library supports only one compression method
  37. (deflation) but other algorithms will be added later and will have the same
  38. stream interface.
  39. Compression can be done in a single step if the buffers are large
  40. enough (for example if an input file is mmap'ed), or can be done by
  41. repeated calls of the compression function. In the latter case, the
  42. application must provide more input and/or consume the output
  43. (providing more output space) before each call.
  44. The library also supports reading and writing files in gzip (.gz) format
  45. with an interface similar to that of stdio.
  46. The library does not install any signal handler. The decoder checks
  47. the consistency of the compressed data, so the library should never
  48. crash even in case of corrupted input. }
  49. { Compile with -DMAXSEG_64K if the alloc function cannot allocate more
  50. than 64k bytes at a time (needed on systems with 16-bit int). }
  51. { Maximum value for memLevel in deflateInit2 }
  52. const
  53. MAX_MEM_LEVEL = 9;
  54. DEF_MEM_LEVEL = 8; { if MAX_MEM_LEVEL > 8 }
  55. { Maximum value for windowBits in deflateInit2 and inflateInit2 }
  56. const
  57. MAX_WBITS = 15; { 32K LZ77 window }
  58. { default windowBits for decompression. MAX_WBITS is for compression only }
  59. const
  60. DEF_WBITS = MAX_WBITS;
  61. { The memory requirements for deflate are (in bytes):
  62. 1 shl (windowBits+2) + 1 shl (memLevel+9)
  63. that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values)
  64. plus a few kilobytes for small objects. For example, if you want to reduce
  65. the default memory requirements from 256K to 128K, compile with
  66. DMAX_WBITS=14 DMAX_MEM_LEVEL=7
  67. Of course this will generally degrade compression (there's no free lunch).
  68. The memory requirements for inflate are (in bytes) 1 shl windowBits
  69. that is, 32K for windowBits=15 (default value) plus a few kilobytes
  70. for small objects. }
  71. { Huffman code lookup table entry--this entry is four bytes for machines
  72. that have 16-bit pointers (e.g. PC's in the small or medium model). }
  73. type
  74. pInflate_huft = ^inflate_huft;
  75. inflate_huft = Record
  76. Exop, { number of extra bits or operation }
  77. bits : Byte; { number of bits in this code or subcode }
  78. {pad : uInt;} { pad structure to a power of 2 (4 bytes for }
  79. { 16-bit, 8 bytes for 32-bit int's) }
  80. base : uInt; { literal, length base, or distance base }
  81. { or table offset }
  82. End;
  83. type
  84. huft_field = Array[0..(MaxInt div SizeOf(inflate_huft))-1] of inflate_huft;
  85. huft_ptr = ^huft_field;
  86. type
  87. ppInflate_huft = ^pInflate_huft;
  88. type
  89. inflate_codes_mode = ( { waiting for "i:"=input, "o:"=output, "x:"=nothing }
  90. START, { x: set up for LEN }
  91. LEN, { i: get length/literal/eob next }
  92. LENEXT, { i: getting length extra (have base) }
  93. DIST, { i: get distance next }
  94. DISTEXT, { i: getting distance extra }
  95. COPY, { o: copying bytes in window, waiting for space }
  96. LIT, { o: got literal, waiting for output space }
  97. WASH, { o: got eob, possibly still output waiting }
  98. ZEND, { x: got eob and all data flushed }
  99. BADCODE); { x: got error }
  100. { inflate codes private state }
  101. type
  102. pInflate_codes_state = ^inflate_codes_state;
  103. inflate_codes_state = record
  104. mode : inflate_codes_mode; { current inflate_codes mode }
  105. { mode dependent information }
  106. len : uInt;
  107. sub : record { submode }
  108. Case Byte of
  109. 0:(code : record { if LEN or DIST, where in tree }
  110. tree : pInflate_huft; { pointer into tree }
  111. need : uInt; { bits needed }
  112. end);
  113. 1:(lit : uInt); { if LIT, literal }
  114. 2:(copy: record { if EXT or COPY, where and how much }
  115. get : uInt; { bits to get for extra }
  116. dist : uInt; { distance back to copy from }
  117. end);
  118. end;
  119. { mode independent information }
  120. lbits : Byte; { ltree bits decoded per branch }
  121. dbits : Byte; { dtree bits decoder per branch }
  122. ltree : pInflate_huft; { literal/length/eob tree }
  123. dtree : pInflate_huft; { distance tree }
  124. end;
  125. type
  126. check_func = function(check : uLong;
  127. buf : pBytef;
  128. {const buf : array of byte;}
  129. len : uInt) : uLong;
  130. type
  131. inflate_block_mode =
  132. (ZTYPE, { get type bits (3, including end bit) }
  133. LENS, { get lengths for stored }
  134. STORED, { processing stored block }
  135. TABLE, { get table lengths }
  136. BTREE, { get bit lengths tree for a dynamic block }
  137. DTREE, { get length, distance trees for a dynamic block }
  138. CODES, { processing fixed or dynamic block }
  139. DRY, { output remaining window bytes }
  140. BLKDONE, { finished last block, done }
  141. BLKBAD); { got a data error--stuck here }
  142. type
  143. pInflate_blocks_state = ^inflate_blocks_state;
  144. { inflate blocks semi-private state }
  145. inflate_blocks_state = record
  146. mode : inflate_block_mode; { current inflate_block mode }
  147. { mode dependent information }
  148. sub : record { submode }
  149. case Byte of
  150. 0:(left : uInt); { if STORED, bytes left to copy }
  151. 1:(trees : record { if DTREE, decoding info for trees }
  152. table : uInt; { table lengths (14 bits) }
  153. index : uInt; { index into blens (or border) }
  154. blens : PuIntArray; { bit lengths of codes }
  155. bb : uInt; { bit length tree depth }
  156. tb : pInflate_huft; { bit length decoding tree }
  157. end);
  158. 2:(decode : record { if CODES, current state }
  159. tl : pInflate_huft;
  160. td : pInflate_huft; { trees to free }
  161. codes : pInflate_codes_state;
  162. end);
  163. end;
  164. last : boolean; { true if this block is the last block }
  165. { mode independent information }
  166. bitk : uInt; { bits in bit buffer }
  167. bitb : uLong; { bit buffer }
  168. hufts : huft_ptr; {pInflate_huft;} { single malloc for tree space }
  169. window : pBytef; { sliding window }
  170. zend : pBytef; { one byte after sliding window }
  171. read : pBytef; { window read pointer }
  172. write : pBytef; { window write pointer }
  173. checkfn : check_func; { check function }
  174. check : uLong; { check on output }
  175. end;
  176. type
  177. inflate_mode = (
  178. METHOD, { waiting for method byte }
  179. FLAG, { waiting for flag byte }
  180. DICT4, { four dictionary check bytes to go }
  181. DICT3, { three dictionary check bytes to go }
  182. DICT2, { two dictionary check bytes to go }
  183. DICT1, { one dictionary check byte to go }
  184. DICT0, { waiting for inflateSetDictionary }
  185. BLOCKS, { decompressing blocks }
  186. CHECK4, { four check bytes to go }
  187. CHECK3, { three check bytes to go }
  188. CHECK2, { two check bytes to go }
  189. CHECK1, { one check byte to go }
  190. DONE, { finished check, done }
  191. BAD); { got an error--stay here }
  192. { inflate private state }
  193. type
  194. pInternal_state = ^internal_state; { or point to a deflate_state record }
  195. internal_state = record
  196. mode : inflate_mode; { current inflate mode }
  197. { mode dependent information }
  198. sub : record { submode }
  199. case byte of
  200. 0:(method : uInt); { if FLAGS, method byte }
  201. 1:(check : record { if CHECK, check values to compare }
  202. was : uLong; { computed check value }
  203. need : uLong; { stream check value }
  204. end);
  205. 2:(marker : uInt); { if BAD, inflateSync's marker bytes count }
  206. end;
  207. { mode independent information }
  208. nowrap : boolean; { flag for no wrapper }
  209. wbits : uInt; { log2(window size) (8..15, defaults to 15) }
  210. blocks : pInflate_blocks_state; { current inflate_blocks state }
  211. end;
  212. type
  213. alloc_func = function(opaque : voidpf; items : uInt; size : uInt) : voidpf;
  214. free_func = procedure(opaque : voidpf; address : voidpf);
  215. type
  216. z_streamp = ^z_stream;
  217. z_stream = record
  218. next_in : pBytef; { next input byte }
  219. avail_in : uInt; { number of bytes available at next_in }
  220. total_in : uLong; { total nb of input bytes read so far }
  221. next_out : pBytef; { next output byte should be put there }
  222. avail_out : uInt; { remaining free space at next_out }
  223. total_out : uLong; { total nb of bytes output so far }
  224. msg : string[255]; { last error message, '' if no error }
  225. state : pInternal_state; { not visible by applications }
  226. zalloc : alloc_func; { used to allocate the internal state }
  227. zfree : free_func; { used to free the internal state }
  228. opaque : voidpf; { private data object passed to zalloc and zfree }
  229. data_type : int; { best guess about the data type: ascii or binary }
  230. adler : uLong; { adler32 value of the uncompressed data }
  231. reserved : uLong; { reserved for future use }
  232. end;
  233. { The application must update next_in and avail_in when avail_in has
  234. dropped to zero. It must update next_out and avail_out when avail_out
  235. has dropped to zero. The application must initialize zalloc, zfree and
  236. opaque before calling the init function. All other fields are set by the
  237. compression library and must not be updated by the application.
  238. The opaque value provided by the application will be passed as the first
  239. parameter for calls of zalloc and zfree. This can be useful for custom
  240. memory management. The compression library attaches no meaning to the
  241. opaque value.
  242. zalloc must return Z_NULL if there is not enough memory for the object.
  243. On 16-bit systems, the functions zalloc and zfree must be able to allocate
  244. exactly 65536 bytes, but will not be required to allocate more than this
  245. if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS,
  246. pointers returned by zalloc for objects of exactly 65536 bytes *must*
  247. have their offset normalized to zero. The default allocation function
  248. provided by this library ensures this (see zutil.c). To reduce memory
  249. requirements and avoid any allocation of 64K objects, at the expense of
  250. compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h).
  251. The fields total_in and total_out can be used for statistics or
  252. progress reports. After compression, total_in holds the total size of
  253. the uncompressed data and may be saved for use in the decompressor
  254. (particularly if the decompressor wants to decompress everything in
  255. a single step). }
  256. const { constants }
  257. Z_NO_FLUSH = 0;
  258. Z_PARTIAL_FLUSH = 1;
  259. Z_SYNC_FLUSH = 2;
  260. Z_FULL_FLUSH = 3;
  261. Z_FINISH = 4;
  262. { Allowed flush values; see deflate() below for details }
  263. Z_OK = 0;
  264. Z_STREAM_END = 1;
  265. Z_NEED_DICT = 2;
  266. Z_ERRNO = (-1);
  267. Z_STREAM_ERROR = (-2);
  268. Z_DATA_ERROR = (-3);
  269. Z_MEM_ERROR = (-4);
  270. Z_BUF_ERROR = (-5);
  271. Z_VERSION_ERROR = (-6);
  272. { Return codes for the compression/decompression functions. Negative
  273. values are errors, positive values are used for special but normal events.}
  274. Z_NO_COMPRESSION = 0;
  275. Z_BEST_SPEED = 1;
  276. Z_BEST_COMPRESSION = 9;
  277. Z_DEFAULT_COMPRESSION = (-1);
  278. { compression levels }
  279. Z_FILTERED = 1;
  280. Z_HUFFMAN_ONLY = 2;
  281. Z_DEFAULT_STRATEGY = 0;
  282. { compression strategy; see deflateInit2() below for details }
  283. Z_BINARY = 0;
  284. Z_ASCII = 1;
  285. Z_UNKNOWN = 2;
  286. { Possible values of the data_type field }
  287. Z_DEFLATED = 8;
  288. { The deflate compression method (the only one supported in this version) }
  289. Z_NULL = NIL; { for initializing zalloc, zfree, opaque }
  290. {$IFDEF GZIO}
  291. var
  292. errno : int;
  293. {$ENDIF}
  294. { common constants }
  295. { The three kinds of block type }
  296. const
  297. STORED_BLOCK = 0;
  298. STATIC_TREES = 1;
  299. DYN_TREES = 2;
  300. { The minimum and maximum match lengths }
  301. const
  302. MIN_MATCH = 3;
  303. MAX_MATCH = 258;
  304. const
  305. PRESET_DICT = $20; { preset dictionary flag in zlib header }
  306. {$IFDEF DEBUG}
  307. procedure Assert(cond : boolean; msg : AnsiString);
  308. {$ENDIF}
  309. procedure Trace(x : AnsiString);
  310. procedure Tracev(x : AnsiString);
  311. procedure Tracevv(x : AnsiString);
  312. procedure Tracevvv(x : AnsiString);
  313. procedure Tracec(c : boolean; x : AnsiString);
  314. procedure Tracecv(c : boolean; x : AnsiString);
  315. function zlibVersion : AnsiString;
  316. { The application can compare zlibVersion and ZLIB_VERSION for consistency.
  317. If the first character differs, the library code actually used is
  318. not compatible with the zlib.h header file used by the application.
  319. This check is automatically made by deflateInit and inflateInit. }
  320. function zError(err : int) : AnsiString;
  321. function ZALLOC (var strm : z_stream; items : uInt; size : uInt) : voidpf;
  322. procedure ZFREE (var strm : z_stream; ptr : voidpf);
  323. procedure TRY_FREE (var strm : z_stream; ptr : voidpf);
  324. const
  325. ZLIB_VERSION : string[10] = '1.1.2';
  326. const
  327. z_errbase = Z_NEED_DICT;
  328. z_errmsg : Array[0..9] of string[21] = { indexed by 2-zlib_error }
  329. ('need dictionary', { Z_NEED_DICT 2 }
  330. 'stream end', { Z_STREAM_END 1 }
  331. '', { Z_OK 0 }
  332. 'file error', { Z_ERRNO (-1) }
  333. 'stream error', { Z_STREAM_ERROR (-2) }
  334. 'data error', { Z_DATA_ERROR (-3) }
  335. 'insufficient memory', { Z_MEM_ERROR (-4) }
  336. 'buffer error', { Z_BUF_ERROR (-5) }
  337. 'incompatible version',{ Z_VERSION_ERROR (-6) }
  338. '');
  339. const
  340. z_verbose : int = 1;
  341. function deflateInit_(var Stream: z_stream; Level: LongInt; const Version: AnsiString;
  342. Stream_size: LongInt): LongInt;
  343. function inflateInit_(var Stream: z_stream; const Version: AnsiString;
  344. Stream_size: Longint): LongInt;
  345. {$IFDEF DEBUG}
  346. procedure z_error (m : string);
  347. {$ENDIF}
  348. implementation
  349. uses
  350. imzdeflate, imzinflate;
  351. function deflateInit_(var Stream: z_stream; Level: LongInt; const Version: AnsiString;
  352. Stream_size: LongInt): LongInt;
  353. begin
  354. Result := imzdeflate.deflateInit_(@Stream, Level, Version, Stream_size);
  355. end;
  356. function inflateInit_(var Stream: z_stream; const Version: AnsiString;
  357. Stream_size: Longint): LongInt;
  358. begin
  359. Result := imzinflate.inflateInit_(@Stream, Version, Stream_size);
  360. end;
  361. function zError(err : int) : AnsiString;
  362. begin
  363. zError := z_errmsg[Z_NEED_DICT-err];
  364. end;
  365. function zlibVersion : AnsiString;
  366. begin
  367. zlibVersion := ZLIB_VERSION;
  368. end;
  369. procedure z_error (m : AnsiString);
  370. begin
  371. WriteLn(output, m);
  372. Write('Zlib - Halt...');
  373. ReadLn;
  374. Halt(1);
  375. end;
  376. procedure Assert(cond : boolean; msg : AnsiString);
  377. begin
  378. if not cond then
  379. z_error(msg);
  380. end;
  381. procedure Trace(x : AnsiString);
  382. begin
  383. WriteLn(x);
  384. end;
  385. procedure Tracev(x : AnsiString);
  386. begin
  387. if (z_verbose>0) then
  388. WriteLn(x);
  389. end;
  390. procedure Tracevv(x : AnsiString);
  391. begin
  392. if (z_verbose>1) then
  393. WriteLn(x);
  394. end;
  395. procedure Tracevvv(x : AnsiString);
  396. begin
  397. if (z_verbose>2) then
  398. WriteLn(x);
  399. end;
  400. procedure Tracec(c : boolean; x : AnsiString);
  401. begin
  402. if (z_verbose>0) and (c) then
  403. WriteLn(x);
  404. end;
  405. procedure Tracecv(c : boolean; x : AnsiString);
  406. begin
  407. if (z_verbose>1) and c then
  408. WriteLn(x);
  409. end;
  410. function ZALLOC (var strm : z_stream; items : uInt; size : uInt) : voidpf;
  411. begin
  412. ZALLOC := strm.zalloc(strm.opaque, items, size);
  413. end;
  414. procedure ZFREE (var strm : z_stream; ptr : voidpf);
  415. begin
  416. strm.zfree(strm.opaque, ptr);
  417. end;
  418. procedure TRY_FREE (var strm : z_stream; ptr : voidpf);
  419. begin
  420. {if @strm <> Z_NULL then}
  421. strm.zfree(strm.opaque, ptr);
  422. end;
  423. end.