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.

2248 lines
75 KiB

3 years ago
  1. Unit imtrees;
  2. {$T-}
  3. {$define ORG_DEBUG}
  4. {
  5. trees.c -- output deflated data using Huffman coding
  6. Copyright (C) 1995-1998 Jean-loup Gailly
  7. Pascal tranlastion
  8. Copyright (C) 1998 by Jacques Nomssi Nzali
  9. For conditions of distribution and use, see copyright notice in readme.txt
  10. }
  11. {
  12. * ALGORITHM
  13. *
  14. * The "deflation" process uses several Huffman trees. The more
  15. * common source values are represented by shorter bit sequences.
  16. *
  17. * Each code tree is stored in a compressed form which is itself
  18. * a Huffman encoding of the lengths of all the code strings (in
  19. * ascending order by source values). The actual code strings are
  20. * reconstructed from the lengths in the inflate process, as described
  21. * in the deflate specification.
  22. *
  23. * REFERENCES
  24. *
  25. * Deutsch, L.P.,"'Deflate' Compressed Data Format Specification".
  26. * Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc
  27. *
  28. * Storer, James A.
  29. * Data Compression: Methods and Theory, pp. 49-50.
  30. * Computer Science Press, 1988. ISBN 0-7167-8156-5.
  31. *
  32. * Sedgewick, R.
  33. * Algorithms, p290.
  34. * Addison-Wesley, 1983. ISBN 0-201-06672-6.
  35. }
  36. interface
  37. {$I imzconf.inc}
  38. uses
  39. {$ifdef DEBUG}
  40. SysUtils, strutils,
  41. {$ENDIF}
  42. imzutil, impaszlib;
  43. { ===========================================================================
  44. Internal compression state. }
  45. const
  46. LENGTH_CODES = 29;
  47. { number of length codes, not counting the special END_BLOCK code }
  48. LITERALS = 256;
  49. { number of literal bytes 0..255 }
  50. L_CODES = (LITERALS+1+LENGTH_CODES);
  51. { number of Literal or Length codes, including the END_BLOCK code }
  52. D_CODES = 30;
  53. { number of distance codes }
  54. BL_CODES = 19;
  55. { number of codes used to transfer the bit lengths }
  56. HEAP_SIZE = (2*L_CODES+1);
  57. { maximum heap size }
  58. MAX_BITS = 15;
  59. { All codes must not exceed MAX_BITS bits }
  60. const
  61. INIT_STATE = 42;
  62. BUSY_STATE = 113;
  63. FINISH_STATE = 666;
  64. { Stream status }
  65. { Data structure describing a single value and its code string. }
  66. type
  67. ct_data_ptr = ^ct_data;
  68. ct_data = record
  69. fc : record
  70. case byte of
  71. 0:(freq : ush); { frequency count }
  72. 1:(code : ush); { bit string }
  73. end;
  74. dl : record
  75. case byte of
  76. 0:(dad : ush); { father node in Huffman tree }
  77. 1:(len : ush); { length of bit string }
  78. end;
  79. end;
  80. { Freq = fc.freq
  81. Code = fc.code
  82. Dad = dl.dad
  83. Len = dl.len }
  84. type
  85. ltree_type = array[0..HEAP_SIZE-1] of ct_data; { literal and length tree }
  86. dtree_type = array[0..2*D_CODES+1-1] of ct_data; { distance tree }
  87. htree_type = array[0..2*BL_CODES+1-1] of ct_data; { Huffman tree for bit lengths }
  88. { generic tree type }
  89. tree_type = array[0..(MaxInt div SizeOf(ct_data))-1] of ct_data;
  90. tree_ptr = ^tree_type;
  91. ltree_ptr = ^ltree_type;
  92. dtree_ptr = ^dtree_type;
  93. htree_ptr = ^htree_type;
  94. type
  95. static_tree_desc_ptr = ^static_tree_desc;
  96. static_tree_desc =
  97. record
  98. {const} static_tree : tree_ptr; { static tree or NIL }
  99. {const} extra_bits : pzIntfArray; { extra bits for each code or NIL }
  100. extra_base : int; { base index for extra_bits }
  101. elems : int; { max number of elements in the tree }
  102. max_length : int; { max bit length for the codes }
  103. end;
  104. tree_desc_ptr = ^tree_desc;
  105. tree_desc = record
  106. dyn_tree : tree_ptr; { the dynamic tree }
  107. max_code : int; { largest code with non zero frequency }
  108. stat_desc : static_tree_desc_ptr; { the corresponding static tree }
  109. end;
  110. type
  111. Pos = ush;
  112. Posf = Pos; {FAR}
  113. IPos = uInt;
  114. pPosf = ^Posf;
  115. zPosfArray = array[0..(MaxInt div SizeOf(Posf))-1] of Posf;
  116. pzPosfArray = ^zPosfArray;
  117. { A Pos is an index in the character window. We use short instead of int to
  118. save space in the various tables. IPos is used only for parameter passing.}
  119. type
  120. deflate_state_ptr = ^deflate_state;
  121. deflate_state = record
  122. strm : z_streamp; { pointer back to this zlib stream }
  123. status : int; { as the name implies }
  124. pending_buf : pzByteArray; { output still pending }
  125. pending_buf_size : ulg; { size of pending_buf }
  126. pending_out : pBytef; { next pending byte to output to the stream }
  127. pending : int; { nb of bytes in the pending buffer }
  128. noheader : int; { suppress zlib header and adler32 }
  129. data_type : Byte; { UNKNOWN, BINARY or ASCII }
  130. method : Byte; { STORED (for zip only) or DEFLATED }
  131. last_flush : int; { value of flush param for previous deflate call }
  132. { used by deflate.pas: }
  133. w_size : uInt; { LZ77 window size (32K by default) }
  134. w_bits : uInt; { log2(w_size) (8..16) }
  135. w_mask : uInt; { w_size - 1 }
  136. window : pzByteArray;
  137. { Sliding window. Input bytes are read into the second half of the window,
  138. and move to the first half later to keep a dictionary of at least wSize
  139. bytes. With this organization, matches are limited to a distance of
  140. wSize-MAX_MATCH bytes, but this ensures that IO is always
  141. performed with a length multiple of the block size. Also, it limits
  142. the window size to 64K, which is quite useful on MSDOS.
  143. To do: use the user input buffer as sliding window. }
  144. window_size : ulg;
  145. { Actual size of window: 2*wSize, except when the user input buffer
  146. is directly used as sliding window. }
  147. prev : pzPosfArray;
  148. { Link to older string with same hash index. To limit the size of this
  149. array to 64K, this link is maintained only for the last 32K strings.
  150. An index in this array is thus a window index modulo 32K. }
  151. head : pzPosfArray; { Heads of the hash chains or NIL. }
  152. ins_h : uInt; { hash index of string to be inserted }
  153. hash_size : uInt; { number of elements in hash table }
  154. hash_bits : uInt; { log2(hash_size) }
  155. hash_mask : uInt; { hash_size-1 }
  156. hash_shift : uInt;
  157. { Number of bits by which ins_h must be shifted at each input
  158. step. It must be such that after MIN_MATCH steps, the oldest
  159. byte no longer takes part in the hash key, that is:
  160. hash_shift * MIN_MATCH >= hash_bits }
  161. block_start : long;
  162. { Window position at the beginning of the current output block. Gets
  163. negative when the window is moved backwards. }
  164. match_length : uInt; { length of best match }
  165. prev_match : IPos; { previous match }
  166. match_available : boolean; { set if previous match exists }
  167. strstart : uInt; { start of string to insert }
  168. match_start : uInt; { start of matching string }
  169. lookahead : uInt; { number of valid bytes ahead in window }
  170. prev_length : uInt;
  171. { Length of the best match at previous step. Matches not greater than this
  172. are discarded. This is used in the lazy match evaluation. }
  173. max_chain_length : uInt;
  174. { To speed up deflation, hash chains are never searched beyond this
  175. length. A higher limit improves compression ratio but degrades the
  176. speed. }
  177. { moved to the end because Borland Pascal won't accept the following:
  178. max_lazy_match : uInt;
  179. max_insert_length : uInt absolute max_lazy_match;
  180. }
  181. level : int; { compression level (1..9) }
  182. strategy : int; { favor or force Huffman coding}
  183. good_match : uInt;
  184. { Use a faster search when the previous match is longer than this }
  185. nice_match : int; { Stop searching when current match exceeds this }
  186. { used by trees.pas: }
  187. { Didn't use ct_data typedef below to supress compiler warning }
  188. dyn_ltree : ltree_type; { literal and length tree }
  189. dyn_dtree : dtree_type; { distance tree }
  190. bl_tree : htree_type; { Huffman tree for bit lengths }
  191. l_desc : tree_desc; { desc. for literal tree }
  192. d_desc : tree_desc; { desc. for distance tree }
  193. bl_desc : tree_desc; { desc. for bit length tree }
  194. bl_count : array[0..MAX_BITS+1-1] of ush;
  195. { number of codes at each bit length for an optimal tree }
  196. heap : array[0..2*L_CODES+1-1] of int; { heap used to build the Huffman trees }
  197. heap_len : int; { number of elements in the heap }
  198. heap_max : int; { element of largest frequency }
  199. { The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
  200. The same heap array is used to build all trees. }
  201. depth : array[0..2*L_CODES+1-1] of uch;
  202. { Depth of each subtree used as tie breaker for trees of equal frequency }
  203. l_buf : puchfArray; { buffer for literals or lengths }
  204. lit_bufsize : uInt;
  205. { Size of match buffer for literals/lengths. There are 4 reasons for
  206. limiting lit_bufsize to 64K:
  207. - frequencies can be kept in 16 bit counters
  208. - if compression is not successful for the first block, all input
  209. data is still in the window so we can still emit a stored block even
  210. when input comes from standard input. (This can also be done for
  211. all blocks if lit_bufsize is not greater than 32K.)
  212. - if compression is not successful for a file smaller than 64K, we can
  213. even emit a stored file instead of a stored block (saving 5 bytes).
  214. This is applicable only for zip (not gzip or zlib).
  215. - creating new Huffman trees less frequently may not provide fast
  216. adaptation to changes in the input data statistics. (Take for
  217. example a binary file with poorly compressible code followed by
  218. a highly compressible string table.) Smaller buffer sizes give
  219. fast adaptation but have of course the overhead of transmitting
  220. trees more frequently.
  221. - I can't count above 4 }
  222. last_lit : uInt; { running index in l_buf }
  223. d_buf : pushfArray;
  224. { Buffer for distances. To simplify the code, d_buf and l_buf have
  225. the same number of elements. To use different lengths, an extra flag
  226. array would be necessary. }
  227. opt_len : ulg; { bit length of current block with optimal trees }
  228. static_len : ulg; { bit length of current block with static trees }
  229. compressed_len : ulg; { total bit length of compressed file }
  230. matches : uInt; { number of string matches in current block }
  231. last_eob_len : int; { bit length of EOB code for last block }
  232. {$ifdef DEBUG}
  233. bits_sent : ulg; { bit length of the compressed data }
  234. {$endif}
  235. bi_buf : ush;
  236. { Output buffer. bits are inserted starting at the bottom (least
  237. significant bits). }
  238. bi_valid : int;
  239. { Number of valid bits in bi_buf. All bits above the last valid bit
  240. are always zero. }
  241. case byte of
  242. 0:(max_lazy_match : uInt);
  243. { Attempt to find a better match only when the current match is strictly
  244. smaller than this value. This mechanism is used only for compression
  245. levels >= 4. }
  246. 1:(max_insert_length : uInt);
  247. { Insert new strings in the hash table only if the match length is not
  248. greater than this length. This saves time but degrades compression.
  249. max_insert_length is used only for compression levels <= 3. }
  250. end;
  251. procedure _tr_init (var s : deflate_state);
  252. function _tr_tally (var s : deflate_state;
  253. dist : unsigned;
  254. lc : unsigned) : boolean;
  255. function _tr_flush_block (var s : deflate_state;
  256. buf : pcharf;
  257. stored_len : ulg;
  258. eof : boolean) : ulg;
  259. procedure _tr_align(var s : deflate_state);
  260. procedure _tr_stored_block(var s : deflate_state;
  261. buf : pcharf;
  262. stored_len : ulg;
  263. eof : boolean);
  264. implementation
  265. { #define GEN_TREES_H }
  266. {$ifndef GEN_TREES_H}
  267. { header created automatically with -DGEN_TREES_H }
  268. const
  269. DIST_CODE_LEN = 512; { see definition of array dist_code below }
  270. { The static literal tree. Since the bit lengths are imposed, there is no
  271. need for the L_CODES extra codes used during heap construction. However
  272. The codes 286 and 287 are needed to build a canonical tree (see _tr_init
  273. below). }
  274. var
  275. static_ltree : array[0..L_CODES+2-1] of ct_data = (
  276. { fc:(freq, code) dl:(dad,len) }
  277. (fc:(freq: 12);dl:(len: 8)), (fc:(freq:140);dl:(len: 8)), (fc:(freq: 76);dl:(len: 8)),
  278. (fc:(freq:204);dl:(len: 8)), (fc:(freq: 44);dl:(len: 8)), (fc:(freq:172);dl:(len: 8)),
  279. (fc:(freq:108);dl:(len: 8)), (fc:(freq:236);dl:(len: 8)), (fc:(freq: 28);dl:(len: 8)),
  280. (fc:(freq:156);dl:(len: 8)), (fc:(freq: 92);dl:(len: 8)), (fc:(freq:220);dl:(len: 8)),
  281. (fc:(freq: 60);dl:(len: 8)), (fc:(freq:188);dl:(len: 8)), (fc:(freq:124);dl:(len: 8)),
  282. (fc:(freq:252);dl:(len: 8)), (fc:(freq: 2);dl:(len: 8)), (fc:(freq:130);dl:(len: 8)),
  283. (fc:(freq: 66);dl:(len: 8)), (fc:(freq:194);dl:(len: 8)), (fc:(freq: 34);dl:(len: 8)),
  284. (fc:(freq:162);dl:(len: 8)), (fc:(freq: 98);dl:(len: 8)), (fc:(freq:226);dl:(len: 8)),
  285. (fc:(freq: 18);dl:(len: 8)), (fc:(freq:146);dl:(len: 8)), (fc:(freq: 82);dl:(len: 8)),
  286. (fc:(freq:210);dl:(len: 8)), (fc:(freq: 50);dl:(len: 8)), (fc:(freq:178);dl:(len: 8)),
  287. (fc:(freq:114);dl:(len: 8)), (fc:(freq:242);dl:(len: 8)), (fc:(freq: 10);dl:(len: 8)),
  288. (fc:(freq:138);dl:(len: 8)), (fc:(freq: 74);dl:(len: 8)), (fc:(freq:202);dl:(len: 8)),
  289. (fc:(freq: 42);dl:(len: 8)), (fc:(freq:170);dl:(len: 8)), (fc:(freq:106);dl:(len: 8)),
  290. (fc:(freq:234);dl:(len: 8)), (fc:(freq: 26);dl:(len: 8)), (fc:(freq:154);dl:(len: 8)),
  291. (fc:(freq: 90);dl:(len: 8)), (fc:(freq:218);dl:(len: 8)), (fc:(freq: 58);dl:(len: 8)),
  292. (fc:(freq:186);dl:(len: 8)), (fc:(freq:122);dl:(len: 8)), (fc:(freq:250);dl:(len: 8)),
  293. (fc:(freq: 6);dl:(len: 8)), (fc:(freq:134);dl:(len: 8)), (fc:(freq: 70);dl:(len: 8)),
  294. (fc:(freq:198);dl:(len: 8)), (fc:(freq: 38);dl:(len: 8)), (fc:(freq:166);dl:(len: 8)),
  295. (fc:(freq:102);dl:(len: 8)), (fc:(freq:230);dl:(len: 8)), (fc:(freq: 22);dl:(len: 8)),
  296. (fc:(freq:150);dl:(len: 8)), (fc:(freq: 86);dl:(len: 8)), (fc:(freq:214);dl:(len: 8)),
  297. (fc:(freq: 54);dl:(len: 8)), (fc:(freq:182);dl:(len: 8)), (fc:(freq:118);dl:(len: 8)),
  298. (fc:(freq:246);dl:(len: 8)), (fc:(freq: 14);dl:(len: 8)), (fc:(freq:142);dl:(len: 8)),
  299. (fc:(freq: 78);dl:(len: 8)), (fc:(freq:206);dl:(len: 8)), (fc:(freq: 46);dl:(len: 8)),
  300. (fc:(freq:174);dl:(len: 8)), (fc:(freq:110);dl:(len: 8)), (fc:(freq:238);dl:(len: 8)),
  301. (fc:(freq: 30);dl:(len: 8)), (fc:(freq:158);dl:(len: 8)), (fc:(freq: 94);dl:(len: 8)),
  302. (fc:(freq:222);dl:(len: 8)), (fc:(freq: 62);dl:(len: 8)), (fc:(freq:190);dl:(len: 8)),
  303. (fc:(freq:126);dl:(len: 8)), (fc:(freq:254);dl:(len: 8)), (fc:(freq: 1);dl:(len: 8)),
  304. (fc:(freq:129);dl:(len: 8)), (fc:(freq: 65);dl:(len: 8)), (fc:(freq:193);dl:(len: 8)),
  305. (fc:(freq: 33);dl:(len: 8)), (fc:(freq:161);dl:(len: 8)), (fc:(freq: 97);dl:(len: 8)),
  306. (fc:(freq:225);dl:(len: 8)), (fc:(freq: 17);dl:(len: 8)), (fc:(freq:145);dl:(len: 8)),
  307. (fc:(freq: 81);dl:(len: 8)), (fc:(freq:209);dl:(len: 8)), (fc:(freq: 49);dl:(len: 8)),
  308. (fc:(freq:177);dl:(len: 8)), (fc:(freq:113);dl:(len: 8)), (fc:(freq:241);dl:(len: 8)),
  309. (fc:(freq: 9);dl:(len: 8)), (fc:(freq:137);dl:(len: 8)), (fc:(freq: 73);dl:(len: 8)),
  310. (fc:(freq:201);dl:(len: 8)), (fc:(freq: 41);dl:(len: 8)), (fc:(freq:169);dl:(len: 8)),
  311. (fc:(freq:105);dl:(len: 8)), (fc:(freq:233);dl:(len: 8)), (fc:(freq: 25);dl:(len: 8)),
  312. (fc:(freq:153);dl:(len: 8)), (fc:(freq: 89);dl:(len: 8)), (fc:(freq:217);dl:(len: 8)),
  313. (fc:(freq: 57);dl:(len: 8)), (fc:(freq:185);dl:(len: 8)), (fc:(freq:121);dl:(len: 8)),
  314. (fc:(freq:249);dl:(len: 8)), (fc:(freq: 5);dl:(len: 8)), (fc:(freq:133);dl:(len: 8)),
  315. (fc:(freq: 69);dl:(len: 8)), (fc:(freq:197);dl:(len: 8)), (fc:(freq: 37);dl:(len: 8)),
  316. (fc:(freq:165);dl:(len: 8)), (fc:(freq:101);dl:(len: 8)), (fc:(freq:229);dl:(len: 8)),
  317. (fc:(freq: 21);dl:(len: 8)), (fc:(freq:149);dl:(len: 8)), (fc:(freq: 85);dl:(len: 8)),
  318. (fc:(freq:213);dl:(len: 8)), (fc:(freq: 53);dl:(len: 8)), (fc:(freq:181);dl:(len: 8)),
  319. (fc:(freq:117);dl:(len: 8)), (fc:(freq:245);dl:(len: 8)), (fc:(freq: 13);dl:(len: 8)),
  320. (fc:(freq:141);dl:(len: 8)), (fc:(freq: 77);dl:(len: 8)), (fc:(freq:205);dl:(len: 8)),
  321. (fc:(freq: 45);dl:(len: 8)), (fc:(freq:173);dl:(len: 8)), (fc:(freq:109);dl:(len: 8)),
  322. (fc:(freq:237);dl:(len: 8)), (fc:(freq: 29);dl:(len: 8)), (fc:(freq:157);dl:(len: 8)),
  323. (fc:(freq: 93);dl:(len: 8)), (fc:(freq:221);dl:(len: 8)), (fc:(freq: 61);dl:(len: 8)),
  324. (fc:(freq:189);dl:(len: 8)), (fc:(freq:125);dl:(len: 8)), (fc:(freq:253);dl:(len: 8)),
  325. (fc:(freq: 19);dl:(len: 9)), (fc:(freq:275);dl:(len: 9)), (fc:(freq:147);dl:(len: 9)),
  326. (fc:(freq:403);dl:(len: 9)), (fc:(freq: 83);dl:(len: 9)), (fc:(freq:339);dl:(len: 9)),
  327. (fc:(freq:211);dl:(len: 9)), (fc:(freq:467);dl:(len: 9)), (fc:(freq: 51);dl:(len: 9)),
  328. (fc:(freq:307);dl:(len: 9)), (fc:(freq:179);dl:(len: 9)), (fc:(freq:435);dl:(len: 9)),
  329. (fc:(freq:115);dl:(len: 9)), (fc:(freq:371);dl:(len: 9)), (fc:(freq:243);dl:(len: 9)),
  330. (fc:(freq:499);dl:(len: 9)), (fc:(freq: 11);dl:(len: 9)), (fc:(freq:267);dl:(len: 9)),
  331. (fc:(freq:139);dl:(len: 9)), (fc:(freq:395);dl:(len: 9)), (fc:(freq: 75);dl:(len: 9)),
  332. (fc:(freq:331);dl:(len: 9)), (fc:(freq:203);dl:(len: 9)), (fc:(freq:459);dl:(len: 9)),
  333. (fc:(freq: 43);dl:(len: 9)), (fc:(freq:299);dl:(len: 9)), (fc:(freq:171);dl:(len: 9)),
  334. (fc:(freq:427);dl:(len: 9)), (fc:(freq:107);dl:(len: 9)), (fc:(freq:363);dl:(len: 9)),
  335. (fc:(freq:235);dl:(len: 9)), (fc:(freq:491);dl:(len: 9)), (fc:(freq: 27);dl:(len: 9)),
  336. (fc:(freq:283);dl:(len: 9)), (fc:(freq:155);dl:(len: 9)), (fc:(freq:411);dl:(len: 9)),
  337. (fc:(freq: 91);dl:(len: 9)), (fc:(freq:347);dl:(len: 9)), (fc:(freq:219);dl:(len: 9)),
  338. (fc:(freq:475);dl:(len: 9)), (fc:(freq: 59);dl:(len: 9)), (fc:(freq:315);dl:(len: 9)),
  339. (fc:(freq:187);dl:(len: 9)), (fc:(freq:443);dl:(len: 9)), (fc:(freq:123);dl:(len: 9)),
  340. (fc:(freq:379);dl:(len: 9)), (fc:(freq:251);dl:(len: 9)), (fc:(freq:507);dl:(len: 9)),
  341. (fc:(freq: 7);dl:(len: 9)), (fc:(freq:263);dl:(len: 9)), (fc:(freq:135);dl:(len: 9)),
  342. (fc:(freq:391);dl:(len: 9)), (fc:(freq: 71);dl:(len: 9)), (fc:(freq:327);dl:(len: 9)),
  343. (fc:(freq:199);dl:(len: 9)), (fc:(freq:455);dl:(len: 9)), (fc:(freq: 39);dl:(len: 9)),
  344. (fc:(freq:295);dl:(len: 9)), (fc:(freq:167);dl:(len: 9)), (fc:(freq:423);dl:(len: 9)),
  345. (fc:(freq:103);dl:(len: 9)), (fc:(freq:359);dl:(len: 9)), (fc:(freq:231);dl:(len: 9)),
  346. (fc:(freq:487);dl:(len: 9)), (fc:(freq: 23);dl:(len: 9)), (fc:(freq:279);dl:(len: 9)),
  347. (fc:(freq:151);dl:(len: 9)), (fc:(freq:407);dl:(len: 9)), (fc:(freq: 87);dl:(len: 9)),
  348. (fc:(freq:343);dl:(len: 9)), (fc:(freq:215);dl:(len: 9)), (fc:(freq:471);dl:(len: 9)),
  349. (fc:(freq: 55);dl:(len: 9)), (fc:(freq:311);dl:(len: 9)), (fc:(freq:183);dl:(len: 9)),
  350. (fc:(freq:439);dl:(len: 9)), (fc:(freq:119);dl:(len: 9)), (fc:(freq:375);dl:(len: 9)),
  351. (fc:(freq:247);dl:(len: 9)), (fc:(freq:503);dl:(len: 9)), (fc:(freq: 15);dl:(len: 9)),
  352. (fc:(freq:271);dl:(len: 9)), (fc:(freq:143);dl:(len: 9)), (fc:(freq:399);dl:(len: 9)),
  353. (fc:(freq: 79);dl:(len: 9)), (fc:(freq:335);dl:(len: 9)), (fc:(freq:207);dl:(len: 9)),
  354. (fc:(freq:463);dl:(len: 9)), (fc:(freq: 47);dl:(len: 9)), (fc:(freq:303);dl:(len: 9)),
  355. (fc:(freq:175);dl:(len: 9)), (fc:(freq:431);dl:(len: 9)), (fc:(freq:111);dl:(len: 9)),
  356. (fc:(freq:367);dl:(len: 9)), (fc:(freq:239);dl:(len: 9)), (fc:(freq:495);dl:(len: 9)),
  357. (fc:(freq: 31);dl:(len: 9)), (fc:(freq:287);dl:(len: 9)), (fc:(freq:159);dl:(len: 9)),
  358. (fc:(freq:415);dl:(len: 9)), (fc:(freq: 95);dl:(len: 9)), (fc:(freq:351);dl:(len: 9)),
  359. (fc:(freq:223);dl:(len: 9)), (fc:(freq:479);dl:(len: 9)), (fc:(freq: 63);dl:(len: 9)),
  360. (fc:(freq:319);dl:(len: 9)), (fc:(freq:191);dl:(len: 9)), (fc:(freq:447);dl:(len: 9)),
  361. (fc:(freq:127);dl:(len: 9)), (fc:(freq:383);dl:(len: 9)), (fc:(freq:255);dl:(len: 9)),
  362. (fc:(freq:511);dl:(len: 9)), (fc:(freq: 0);dl:(len: 7)), (fc:(freq: 64);dl:(len: 7)),
  363. (fc:(freq: 32);dl:(len: 7)), (fc:(freq: 96);dl:(len: 7)), (fc:(freq: 16);dl:(len: 7)),
  364. (fc:(freq: 80);dl:(len: 7)), (fc:(freq: 48);dl:(len: 7)), (fc:(freq:112);dl:(len: 7)),
  365. (fc:(freq: 8);dl:(len: 7)), (fc:(freq: 72);dl:(len: 7)), (fc:(freq: 40);dl:(len: 7)),
  366. (fc:(freq:104);dl:(len: 7)), (fc:(freq: 24);dl:(len: 7)), (fc:(freq: 88);dl:(len: 7)),
  367. (fc:(freq: 56);dl:(len: 7)), (fc:(freq:120);dl:(len: 7)), (fc:(freq: 4);dl:(len: 7)),
  368. (fc:(freq: 68);dl:(len: 7)), (fc:(freq: 36);dl:(len: 7)), (fc:(freq:100);dl:(len: 7)),
  369. (fc:(freq: 20);dl:(len: 7)), (fc:(freq: 84);dl:(len: 7)), (fc:(freq: 52);dl:(len: 7)),
  370. (fc:(freq:116);dl:(len: 7)), (fc:(freq: 3);dl:(len: 8)), (fc:(freq:131);dl:(len: 8)),
  371. (fc:(freq: 67);dl:(len: 8)), (fc:(freq:195);dl:(len: 8)), (fc:(freq: 35);dl:(len: 8)),
  372. (fc:(freq:163);dl:(len: 8)), (fc:(freq: 99);dl:(len: 8)), (fc:(freq:227);dl:(len: 8))
  373. );
  374. { The static distance tree. (Actually a trivial tree since all lens use
  375. 5 bits.) }
  376. static_dtree : array[0..D_CODES-1] of ct_data = (
  377. (fc:(freq: 0); dl:(len:5)), (fc:(freq:16); dl:(len:5)), (fc:(freq: 8); dl:(len:5)),
  378. (fc:(freq:24); dl:(len:5)), (fc:(freq: 4); dl:(len:5)), (fc:(freq:20); dl:(len:5)),
  379. (fc:(freq:12); dl:(len:5)), (fc:(freq:28); dl:(len:5)), (fc:(freq: 2); dl:(len:5)),
  380. (fc:(freq:18); dl:(len:5)), (fc:(freq:10); dl:(len:5)), (fc:(freq:26); dl:(len:5)),
  381. (fc:(freq: 6); dl:(len:5)), (fc:(freq:22); dl:(len:5)), (fc:(freq:14); dl:(len:5)),
  382. (fc:(freq:30); dl:(len:5)), (fc:(freq: 1); dl:(len:5)), (fc:(freq:17); dl:(len:5)),
  383. (fc:(freq: 9); dl:(len:5)), (fc:(freq:25); dl:(len:5)), (fc:(freq: 5); dl:(len:5)),
  384. (fc:(freq:21); dl:(len:5)), (fc:(freq:13); dl:(len:5)), (fc:(freq:29); dl:(len:5)),
  385. (fc:(freq: 3); dl:(len:5)), (fc:(freq:19); dl:(len:5)), (fc:(freq:11); dl:(len:5)),
  386. (fc:(freq:27); dl:(len:5)), (fc:(freq: 7); dl:(len:5)), (fc:(freq:23); dl:(len:5))
  387. );
  388. { Distance codes. The first 256 values correspond to the distances
  389. 3 .. 258, the last 256 values correspond to the top 8 bits of
  390. the 15 bit distances. }
  391. _dist_code : array[0..DIST_CODE_LEN-1] of uch = (
  392. 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8,
  393. 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10,
  394. 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
  395. 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
  396. 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13,
  397. 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
  398. 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
  399. 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
  400. 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
  401. 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15,
  402. 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  403. 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  404. 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 0, 16, 17,
  405. 18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22,
  406. 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  407. 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
  408. 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
  409. 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27,
  410. 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
  411. 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
  412. 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
  413. 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
  414. 28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
  415. 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
  416. 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
  417. 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29
  418. );
  419. { length code for each normalized match length (0 == MIN_MATCH) }
  420. _length_code : array[0..MAX_MATCH-MIN_MATCH+1-1] of uch = (
  421. 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 12,
  422. 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16,
  423. 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19,
  424. 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
  425. 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22,
  426. 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23,
  427. 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  428. 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  429. 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
  430. 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26,
  431. 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
  432. 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
  433. 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28
  434. );
  435. { First normalized length for each code (0 = MIN_MATCH) }
  436. base_length : array[0..LENGTH_CODES-1] of int = (
  437. 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56,
  438. 64, 80, 96, 112, 128, 160, 192, 224, 0
  439. );
  440. { First normalized distance for each code (0 = distance of 1) }
  441. base_dist : array[0..D_CODES-1] of int = (
  442. 0, 1, 2, 3, 4, 6, 8, 12, 16, 24,
  443. 32, 48, 64, 96, 128, 192, 256, 384, 512, 768,
  444. 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576
  445. );
  446. {$endif}
  447. { Output a byte on the stream.
  448. IN assertion: there is enough room in pending_buf.
  449. macro put_byte(s, c)
  450. begin
  451. s^.pending_buf^[s^.pending] := (c);
  452. Inc(s^.pending);
  453. end
  454. }
  455. const
  456. MIN_LOOKAHEAD = (MAX_MATCH+MIN_MATCH+1);
  457. { Minimum amount of lookahead, except at the end of the input file.
  458. See deflate.c for comments about the MIN_MATCH+1. }
  459. {macro d_code(dist)
  460. if (dist) < 256 then
  461. := _dist_code[dist]
  462. else
  463. := _dist_code[256+((dist) shr 7)]);
  464. Mapping from a distance to a distance code. dist is the distance - 1 and
  465. must not have side effects. _dist_code[256] and _dist_code[257] are never
  466. used. }
  467. {$ifndef ORG_DEBUG}
  468. { Inline versions of _tr_tally for speed: }
  469. #if defined(GEN_TREES_H) || !defined(STDC)
  470. extern uch _length_code[];
  471. extern uch _dist_code[];
  472. #else
  473. extern const uch _length_code[];
  474. extern const uch _dist_code[];
  475. #endif
  476. macro _tr_tally_lit(s, c, flush)
  477. var
  478. cc : uch;
  479. begin
  480. cc := (c);
  481. s^.d_buf[s^.last_lit] := 0;
  482. s^.l_buf[s^.last_lit] := cc;
  483. Inc(s^.last_lit);
  484. Inc(s^.dyn_ltree[cc].fc.Freq);
  485. flush := (s^.last_lit = s^.lit_bufsize-1);
  486. end;
  487. macro _tr_tally_dist(s, distance, length, flush) \
  488. var
  489. len : uch;
  490. dist : ush;
  491. begin
  492. len := (length);
  493. dist := (distance);
  494. s^.d_buf[s^.last_lit] := dist;
  495. s^.l_buf[s^.last_lit] = len;
  496. Inc(s^.last_lit);
  497. Dec(dist);
  498. Inc(s^.dyn_ltree[_length_code[len]+LITERALS+1].fc.Freq);
  499. Inc(s^.dyn_dtree[d_code(dist)].Freq);
  500. flush := (s^.last_lit = s^.lit_bufsize-1);
  501. end;
  502. {$endif}
  503. { ===========================================================================
  504. Constants }
  505. const
  506. MAX_BL_BITS = 7;
  507. { Bit length codes must not exceed MAX_BL_BITS bits }
  508. const
  509. END_BLOCK = 256;
  510. { end of block literal code }
  511. const
  512. REP_3_6 = 16;
  513. { repeat previous bit length 3-6 times (2 bits of repeat count) }
  514. const
  515. REPZ_3_10 = 17;
  516. { repeat a zero length 3-10 times (3 bits of repeat count) }
  517. const
  518. REPZ_11_138 = 18;
  519. { repeat a zero length 11-138 times (7 bits of repeat count) }
  520. {local}
  521. const
  522. extra_lbits : array[0..LENGTH_CODES-1] of int
  523. { extra bits for each length code }
  524. = (0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0);
  525. {local}
  526. const
  527. extra_dbits : array[0..D_CODES-1] of int
  528. { extra bits for each distance code }
  529. = (0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13);
  530. {local}
  531. const
  532. extra_blbits : array[0..BL_CODES-1] of int { extra bits for each bit length code }
  533. = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7);
  534. {local}
  535. const
  536. bl_order : array[0..BL_CODES-1] of uch
  537. = (16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15);
  538. { The lengths of the bit length codes are sent in order of decreasing
  539. probability, to avoid transmitting the lengths for unused bit length codes.
  540. }
  541. const
  542. Buf_size = (8 * 2*sizeof(uch));
  543. { Number of bits used within bi_buf. (bi_buf might be implemented on
  544. more than 16 bits on some systems.) }
  545. { ===========================================================================
  546. Local data. These are initialized only once. }
  547. {$ifdef GEN_TREES_H)}
  548. { non ANSI compilers may not accept trees.h }
  549. const
  550. DIST_CODE_LEN = 512; { see definition of array dist_code below }
  551. {local}
  552. var
  553. static_ltree : array[0..L_CODES+2-1] of ct_data;
  554. { The static literal tree. Since the bit lengths are imposed, there is no
  555. need for the L_CODES extra codes used during heap construction. However
  556. The codes 286 and 287 are needed to build a canonical tree (see _tr_init
  557. below). }
  558. {local}
  559. static_dtree : array[0..D_CODES-1] of ct_data;
  560. { The static distance tree. (Actually a trivial tree since all codes use
  561. 5 bits.) }
  562. _dist_code : array[0..DIST_CODE_LEN-1] of uch;
  563. { Distance codes. The first 256 values correspond to the distances
  564. 3 .. 258, the last 256 values correspond to the top 8 bits of
  565. the 15 bit distances. }
  566. _length_code : array[0..MAX_MATCH-MIN_MATCH+1-1] of uch;
  567. { length code for each normalized match length (0 == MIN_MATCH) }
  568. {local}
  569. base_length : array[0..LENGTH_CODES-1] of int;
  570. { First normalized length for each code (0 = MIN_MATCH) }
  571. {local}
  572. base_dist : array[0..D_CODES-1] of int;
  573. { First normalized distance for each code (0 = distance of 1) }
  574. {$endif} { GEN_TREES_H }
  575. {local}
  576. const
  577. static_l_desc : static_tree_desc =
  578. (static_tree: {tree_ptr}(@(static_ltree)); { pointer to array of ct_data }
  579. extra_bits: {pzIntfArray}(@(extra_lbits)); { pointer to array of int }
  580. extra_base: LITERALS+1;
  581. elems: L_CODES;
  582. max_length: MAX_BITS);
  583. {local}
  584. const
  585. static_d_desc : static_tree_desc =
  586. (static_tree: {tree_ptr}(@(static_dtree));
  587. extra_bits: {pzIntfArray}(@(extra_dbits));
  588. extra_base : 0;
  589. elems: D_CODES;
  590. max_length: MAX_BITS);
  591. {local}
  592. const
  593. static_bl_desc : static_tree_desc =
  594. (static_tree: {tree_ptr}(NIL);
  595. extra_bits: {pzIntfArray}@(extra_blbits);
  596. extra_base : 0;
  597. elems: BL_CODES;
  598. max_length: MAX_BL_BITS);
  599. (* ===========================================================================
  600. Local (static) routines in this file. }
  601. procedure tr_static_init;
  602. procedure init_block(var deflate_state);
  603. procedure pqdownheap(var s : deflate_state;
  604. var tree : ct_data;
  605. k : int);
  606. procedure gen_bitlen(var s : deflate_state;
  607. var desc : tree_desc);
  608. procedure gen_codes(var tree : ct_data;
  609. max_code : int;
  610. bl_count : pushf);
  611. procedure build_tree(var s : deflate_state;
  612. var desc : tree_desc);
  613. procedure scan_tree(var s : deflate_state;
  614. var tree : ct_data;
  615. max_code : int);
  616. procedure send_tree(var s : deflate_state;
  617. var tree : ct_data;
  618. max_code : int);
  619. function build_bl_tree(var deflate_state) : int;
  620. procedure send_all_trees(var deflate_state;
  621. lcodes : int;
  622. dcodes : int;
  623. blcodes : int);
  624. procedure compress_block(var s : deflate_state;
  625. var ltree : ct_data;
  626. var dtree : ct_data);
  627. procedure set_data_type(var s : deflate_state);
  628. function bi_reverse(value : unsigned;
  629. length : int) : unsigned;
  630. procedure bi_windup(var deflate_state);
  631. procedure bi_flush(var deflate_state);
  632. procedure copy_block(var deflate_state;
  633. buf : pcharf;
  634. len : unsigned;
  635. header : int);
  636. *)
  637. {$ifdef GEN_TREES_H}
  638. {local}
  639. procedure gen_trees_header;
  640. {$endif}
  641. (*
  642. { ===========================================================================
  643. Output a short LSB first on the stream.
  644. IN assertion: there is enough room in pendingBuf. }
  645. macro put_short(s, w)
  646. begin
  647. {put_byte(s, (uch)((w) & 0xff));}
  648. s.pending_buf^[s.pending] := uch((w) and $ff);
  649. Inc(s.pending);
  650. {put_byte(s, (uch)((ush)(w) >> 8));}
  651. s.pending_buf^[s.pending] := uch(ush(w) shr 8);;
  652. Inc(s.pending);
  653. end
  654. *)
  655. { ===========================================================================
  656. Send a value on a given number of bits.
  657. IN assertion: length <= 16 and value fits in length bits. }
  658. {$ifdef ORG_DEBUG}
  659. {local}
  660. procedure send_bits(var s : deflate_state;
  661. value : int; { value to send }
  662. length : int); { number of bits }
  663. begin
  664. {$ifdef DEBUG}
  665. Tracevv(' l '+IntToStr(length)+ ' v '+IntToStr(value));
  666. Assert((length > 0) and (length <= 15), 'invalid length');
  667. Inc(s.bits_sent, ulg(length));
  668. {$ENDIF}
  669. { If not enough room in bi_buf, use (valid) bits from bi_buf and
  670. (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
  671. unused bits in value. }
  672. {$IFOPT Q+} {$Q-} {$DEFINE NoOverflowCheck} {$ENDIF}
  673. {$IFOPT R+} {$R-} {$DEFINE NoRangeCheck} {$ENDIF}
  674. if (s.bi_valid > int(Buf_size) - length) then
  675. begin
  676. s.bi_buf := s.bi_buf or int(value shl s.bi_valid);
  677. {put_short(s, s.bi_buf);}
  678. s.pending_buf^[s.pending] := uch(s.bi_buf and $ff);
  679. Inc(s.pending);
  680. s.pending_buf^[s.pending] := uch(ush(s.bi_buf) shr 8);;
  681. Inc(s.pending);
  682. s.bi_buf := ush(value) shr (Buf_size - s.bi_valid);
  683. Inc(s.bi_valid, length - Buf_size);
  684. end
  685. else
  686. begin
  687. s.bi_buf := s.bi_buf or int(value shl s.bi_valid);
  688. Inc(s.bi_valid, length);
  689. end;
  690. {$IFDEF NoOverflowCheck} {$Q+} {$UNDEF NoOverflowCheck} {$ENDIF}
  691. {$IFDEF NoRangeCheck} {$Q+} {$UNDEF NoRangeCheck} {$ENDIF}
  692. end;
  693. {$else} { !DEBUG }
  694. macro send_code(s, c, tree)
  695. begin
  696. send_bits(s, tree[c].Code, tree[c].Len);
  697. { Send a code of the given tree. c and tree must not have side effects }
  698. end
  699. macro send_bits(s, value, length) \
  700. begin int len := length;\
  701. if (s^.bi_valid > (int)Buf_size - len) begin\
  702. int val := value;\
  703. s^.bi_buf |= (val << s^.bi_valid);\
  704. {put_short(s, s.bi_buf);}
  705. s.pending_buf^[s.pending] := uch(s.bi_buf and $ff);
  706. Inc(s.pending);
  707. s.pending_buf^[s.pending] := uch(ush(s.bi_buf) shr 8);;
  708. Inc(s.pending);
  709. s^.bi_buf := (ush)val >> (Buf_size - s^.bi_valid);\
  710. s^.bi_valid += len - Buf_size;\
  711. end else begin\
  712. s^.bi_buf |= (value) << s^.bi_valid;\
  713. s^.bi_valid += len;\
  714. end\
  715. end;
  716. {$endif} { DEBUG }
  717. { ===========================================================================
  718. Reverse the first len bits of a code, using straightforward code (a faster
  719. method would use a table)
  720. IN assertion: 1 <= len <= 15 }
  721. {local}
  722. function bi_reverse(code : unsigned; { the value to invert }
  723. len : int) : unsigned; { its bit length }
  724. var
  725. res : unsigned; {register}
  726. begin
  727. res := 0;
  728. repeat
  729. res := res or (code and 1);
  730. code := code shr 1;
  731. res := res shl 1;
  732. Dec(len);
  733. until (len <= 0);
  734. bi_reverse := res shr 1;
  735. end;
  736. { ===========================================================================
  737. Generate the codes for a given tree and bit counts (which need not be
  738. optimal).
  739. IN assertion: the array bl_count contains the bit length statistics for
  740. the given tree and the field len is set for all tree elements.
  741. OUT assertion: the field code is set for all tree elements of non
  742. zero code length. }
  743. {local}
  744. procedure gen_codes(tree : tree_ptr; { the tree to decorate }
  745. max_code : int; { largest code with non zero frequency }
  746. var bl_count : array of ushf); { number of codes at each bit length }
  747. var
  748. next_code : array[0..MAX_BITS+1-1] of ush; { next code value for each bit length }
  749. code : ush; { running code value }
  750. bits : int; { bit index }
  751. n : int; { code index }
  752. var
  753. len : int;
  754. begin
  755. code := 0;
  756. { The distribution counts are first used to generate the code values
  757. without bit reversal. }
  758. for bits := 1 to MAX_BITS do
  759. begin
  760. code := ((code + bl_count[bits-1]) shl 1);
  761. next_code[bits] := code;
  762. end;
  763. { Check that the bit counts in bl_count are consistent. The last code
  764. must be all ones. }
  765. {$IFDEF DEBUG}
  766. Assert (code + bl_count[MAX_BITS]-1 = (1 shl MAX_BITS)-1,
  767. 'inconsistent bit counts');
  768. Tracev(#13'gen_codes: max_code '+IntToStr(max_code));
  769. {$ENDIF}
  770. for n := 0 to max_code do
  771. begin
  772. len := tree^[n].dl.Len;
  773. if (len = 0) then
  774. continue;
  775. { Now reverse the bits }
  776. tree^[n].fc.Code := bi_reverse(next_code[len], len);
  777. Inc(next_code[len]);
  778. {$ifdef DEBUG}
  779. if (n>31) and (n<128) then
  780. Tracecv(tree <> tree_ptr(@static_ltree),
  781. (^M'n #'+IntToStr(n)+' '+AnsiChar(n)+' l '+IntToStr(len)+' c '+
  782. IntToStr(tree^[n].fc.Code)+' ('+IntToStr(next_code[len]-1)+')'))
  783. else
  784. Tracecv(tree <> tree_ptr(@static_ltree),
  785. (^M'n #'+IntToStr(n)+' l '+IntToStr(len)+' c '+
  786. IntToStr(tree^[n].fc.Code)+' ('+IntToStr(next_code[len]-1)+')'));
  787. {$ENDIF}
  788. end;
  789. end;
  790. { ===========================================================================
  791. Genererate the file trees.h describing the static trees. }
  792. {$ifdef GEN_TREES_H}
  793. macro SEPARATOR(i, last, width)
  794. if (i) = (last) then
  795. ( ^M');'^M^M
  796. else \
  797. if (i) mod (width) = (width)-1 then
  798. ','^M
  799. else
  800. ', '
  801. procedure gen_trees_header;
  802. var
  803. header : system.text;
  804. i : int;
  805. begin
  806. system.assign(header, 'trees.inc');
  807. {$I-}
  808. ReWrite(header);
  809. {$I+}
  810. Assert (IOresult <> 0, 'Can''t open trees.h');
  811. WriteLn(header,
  812. '{ header created automatically with -DGEN_TREES_H }'^M);
  813. WriteLn(header, 'local const ct_data static_ltree[L_CODES+2] := (');
  814. for i := 0 to L_CODES+2-1 do
  815. begin
  816. WriteLn(header, '((%3u),(%3u))%s', static_ltree[i].Code,
  817. static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5));
  818. end;
  819. WriteLn(header, 'local const ct_data static_dtree[D_CODES] := (');
  820. for i := 0 to D_CODES-1 do
  821. begin
  822. WriteLn(header, '((%2u),(%2u))%s', static_dtree[i].Code,
  823. static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5));
  824. end;
  825. WriteLn(header, 'const uch _dist_code[DIST_CODE_LEN] := (');
  826. for i := 0 to DIST_CODE_LEN-1 do
  827. begin
  828. WriteLn(header, '%2u%s', _dist_code[i],
  829. SEPARATOR(i, DIST_CODE_LEN-1, 20));
  830. end;
  831. WriteLn(header, 'const uch _length_code[MAX_MATCH-MIN_MATCH+1]= (');
  832. for i := 0 to MAX_MATCH-MIN_MATCH+1-1 do
  833. begin
  834. WriteLn(header, '%2u%s', _length_code[i],
  835. SEPARATOR(i, MAX_MATCH-MIN_MATCH, 20));
  836. end;
  837. WriteLn(header, 'local const int base_length[LENGTH_CODES] := (');
  838. for i := 0 to LENGTH_CODES-1 do
  839. begin
  840. WriteLn(header, '%1u%s', base_length[i],
  841. SEPARATOR(i, LENGTH_CODES-1, 20));
  842. end;
  843. WriteLn(header, 'local const int base_dist[D_CODES] := (');
  844. for i := 0 to D_CODES-1 do
  845. begin
  846. WriteLn(header, '%5u%s', base_dist[i],
  847. SEPARATOR(i, D_CODES-1, 10));
  848. end;
  849. close(header);
  850. end;
  851. {$endif} { GEN_TREES_H }
  852. { ===========================================================================
  853. Initialize the various 'constant' tables. }
  854. {local}
  855. procedure tr_static_init;
  856. {$ifdef GEN_TREES_H}
  857. const
  858. static_init_done : boolean = FALSE;
  859. var
  860. n : int; { iterates over tree elements }
  861. bits : int; { bit counter }
  862. length : int; { length value }
  863. code : int; { code value }
  864. dist : int; { distance index }
  865. bl_count : array[0..MAX_BITS+1-1] of ush;
  866. { number of codes at each bit length for an optimal tree }
  867. begin
  868. if (static_init_done) then
  869. exit;
  870. { Initialize the mapping length (0..255) -> length code (0..28) }
  871. length := 0;
  872. for code := 0 to LENGTH_CODES-1-1 do
  873. begin
  874. base_length[code] := length;
  875. for n := 0 to (1 shl extra_lbits[code])-1 do
  876. begin
  877. _length_code[length] := uch(code);
  878. Inc(length);
  879. end;
  880. end;
  881. Assert (length = 256, 'tr_static_init: length <> 256');
  882. { Note that the length 255 (match length 258) can be represented
  883. in two different ways: code 284 + 5 bits or code 285, so we
  884. overwrite length_code[255] to use the best encoding: }
  885. _length_code[length-1] := uch(code);
  886. { Initialize the mapping dist (0..32K) -> dist code (0..29) }
  887. dist := 0;
  888. for code := 0 to 16-1 do
  889. begin
  890. base_dist[code] := dist;
  891. for n := 0 to (1 shl extra_dbits[code])-1 do
  892. begin
  893. _dist_code[dist] := uch(code);
  894. Inc(dist);
  895. end;
  896. end;
  897. Assert (dist = 256, 'tr_static_init: dist <> 256');
  898. dist := dist shr 7; { from now on, all distances are divided by 128 }
  899. for code := 16 to D_CODES-1 do
  900. begin
  901. base_dist[code] := dist shl 7;
  902. for n := 0 to (1 shl (extra_dbits[code]-7))-1 do
  903. begin
  904. _dist_code[256 + dist] := uch(code);
  905. Inc(dist);
  906. end;
  907. end;
  908. Assert (dist = 256, 'tr_static_init: 256+dist <> 512');
  909. { Construct the codes of the static literal tree }
  910. for bits := 0 to MAX_BITS do
  911. bl_count[bits] := 0;
  912. n := 0;
  913. while (n <= 143) do
  914. begin
  915. static_ltree[n].dl.Len := 8;
  916. Inc(n);
  917. Inc(bl_count[8]);
  918. end;
  919. while (n <= 255) do
  920. begin
  921. static_ltree[n].dl.Len := 9;
  922. Inc(n);
  923. Inc(bl_count[9]);
  924. end;
  925. while (n <= 279) do
  926. begin
  927. static_ltree[n].dl.Len := 7;
  928. Inc(n);
  929. Inc(bl_count[7]);
  930. end;
  931. while (n <= 287) do
  932. begin
  933. static_ltree[n].dl.Len := 8;
  934. Inc(n);
  935. Inc(bl_count[8]);
  936. end;
  937. { Codes 286 and 287 do not exist, but we must include them in the
  938. tree construction to get a canonical Huffman tree (longest code
  939. all ones) }
  940. gen_codes(tree_ptr(@static_ltree), L_CODES+1, bl_count);
  941. { The static distance tree is trivial: }
  942. for n := 0 to D_CODES-1 do
  943. begin
  944. static_dtree[n].dl.Len := 5;
  945. static_dtree[n].fc.Code := bi_reverse(unsigned(n), 5);
  946. end;
  947. static_init_done := TRUE;
  948. gen_trees_header; { save to include file }
  949. {$else}
  950. begin
  951. {$endif} { GEN_TREES_H) }
  952. end;
  953. { ===========================================================================
  954. Initialize a new block. }
  955. {local}
  956. procedure init_block(var s : deflate_state);
  957. var
  958. n : int; { iterates over tree elements }
  959. begin
  960. { Initialize the trees. }
  961. for n := 0 to L_CODES-1 do
  962. s.dyn_ltree[n].fc.Freq := 0;
  963. for n := 0 to D_CODES-1 do
  964. s.dyn_dtree[n].fc.Freq := 0;
  965. for n := 0 to BL_CODES-1 do
  966. s.bl_tree[n].fc.Freq := 0;
  967. s.dyn_ltree[END_BLOCK].fc.Freq := 1;
  968. s.static_len := Long(0);
  969. s.opt_len := Long(0);
  970. s.matches := 0;
  971. s.last_lit := 0;
  972. end;
  973. const
  974. SMALLEST = 1;
  975. { Index within the heap array of least frequent node in the Huffman tree }
  976. { ===========================================================================
  977. Initialize the tree data structures for a new zlib stream. }
  978. procedure _tr_init(var s : deflate_state);
  979. begin
  980. tr_static_init;
  981. s.compressed_len := Long(0);
  982. s.l_desc.dyn_tree := tree_ptr(@s.dyn_ltree);
  983. s.l_desc.stat_desc := @static_l_desc;
  984. s.d_desc.dyn_tree := tree_ptr(@s.dyn_dtree);
  985. s.d_desc.stat_desc := @static_d_desc;
  986. s.bl_desc.dyn_tree := tree_ptr(@s.bl_tree);
  987. s.bl_desc.stat_desc := @static_bl_desc;
  988. s.bi_buf := 0;
  989. s.bi_valid := 0;
  990. s.last_eob_len := 8; { enough lookahead for inflate }
  991. {$ifdef DEBUG}
  992. s.bits_sent := Long(0);
  993. {$endif}
  994. { Initialize the first block of the first file: }
  995. init_block(s);
  996. end;
  997. { ===========================================================================
  998. Remove the smallest element from the heap and recreate the heap with
  999. one less element. Updates heap and heap_len.
  1000. macro pqremove(s, tree, top)
  1001. begin
  1002. top := s.heap[SMALLEST];
  1003. s.heap[SMALLEST] := s.heap[s.heap_len];
  1004. Dec(s.heap_len);
  1005. pqdownheap(s, tree, SMALLEST);
  1006. end
  1007. }
  1008. { ===========================================================================
  1009. Compares to subtrees, using the tree depth as tie breaker when
  1010. the subtrees have equal frequency. This minimizes the worst case length.
  1011. macro smaller(tree, n, m, depth)
  1012. ( (tree[n].Freq < tree[m].Freq) or
  1013. ((tree[n].Freq = tree[m].Freq) and (depth[n] <= depth[m])) )
  1014. }
  1015. { ===========================================================================
  1016. Restore the heap property by moving down the tree starting at node k,
  1017. exchanging a node with the smallest of its two sons if necessary, stopping
  1018. when the heap property is re-established (each father smaller than its
  1019. two sons). }
  1020. {local}
  1021. procedure pqdownheap(var s : deflate_state;
  1022. var tree : tree_type; { the tree to restore }
  1023. k : int); { node to move down }
  1024. var
  1025. v : int;
  1026. j : int;
  1027. begin
  1028. v := s.heap[k];
  1029. j := k shl 1; { left son of k }
  1030. while (j <= s.heap_len) do
  1031. begin
  1032. { Set j to the smallest of the two sons: }
  1033. if (j < s.heap_len) and
  1034. {smaller(tree, s.heap[j+1], s.heap[j], s.depth)}
  1035. ( (tree[s.heap[j+1]].fc.Freq < tree[s.heap[j]].fc.Freq) or
  1036. ((tree[s.heap[j+1]].fc.Freq = tree[s.heap[j]].fc.Freq) and
  1037. (s.depth[s.heap[j+1]] <= s.depth[s.heap[j]])) ) then
  1038. begin
  1039. Inc(j);
  1040. end;
  1041. { Exit if v is smaller than both sons }
  1042. if {(smaller(tree, v, s.heap[j], s.depth))}
  1043. ( (tree[v].fc.Freq < tree[s.heap[j]].fc.Freq) or
  1044. ((tree[v].fc.Freq = tree[s.heap[j]].fc.Freq) and
  1045. (s.depth[v] <= s.depth[s.heap[j]])) ) then
  1046. break;
  1047. { Exchange v with the smallest son }
  1048. s.heap[k] := s.heap[j];
  1049. k := j;
  1050. { And continue down the tree, setting j to the left son of k }
  1051. j := j shl 1;
  1052. end;
  1053. s.heap[k] := v;
  1054. end;
  1055. { ===========================================================================
  1056. Compute the optimal bit lengths for a tree and update the total bit length
  1057. for the current block.
  1058. IN assertion: the fields freq and dad are set, heap[heap_max] and
  1059. above are the tree nodes sorted by increasing frequency.
  1060. OUT assertions: the field len is set to the optimal bit length, the
  1061. array bl_count contains the frequencies for each bit length.
  1062. The length opt_len is updated; static_len is also updated if stree is
  1063. not null. }
  1064. {local}
  1065. procedure gen_bitlen(var s : deflate_state;
  1066. var desc : tree_desc); { the tree descriptor }
  1067. var
  1068. tree : tree_ptr;
  1069. max_code : int;
  1070. stree : tree_ptr; {const}
  1071. extra : pzIntfArray; {const}
  1072. base : int;
  1073. max_length : int;
  1074. h : int; { heap index }
  1075. n, m : int; { iterate over the tree elements }
  1076. bits : int; { bit length }
  1077. xbits : int; { extra bits }
  1078. f : ush; { frequency }
  1079. overflow : int; { number of elements with bit length too large }
  1080. begin
  1081. tree := desc.dyn_tree;
  1082. max_code := desc.max_code;
  1083. stree := desc.stat_desc^.static_tree;
  1084. extra := desc.stat_desc^.extra_bits;
  1085. base := desc.stat_desc^.extra_base;
  1086. max_length := desc.stat_desc^.max_length;
  1087. overflow := 0;
  1088. for bits := 0 to MAX_BITS do
  1089. s.bl_count[bits] := 0;
  1090. { In a first pass, compute the optimal bit lengths (which may
  1091. overflow in the case of the bit length tree). }
  1092. tree^[s.heap[s.heap_max]].dl.Len := 0; { root of the heap }
  1093. for h := s.heap_max+1 to HEAP_SIZE-1 do
  1094. begin
  1095. n := s.heap[h];
  1096. bits := tree^[tree^[n].dl.Dad].dl.Len + 1;
  1097. if (bits > max_length) then
  1098. begin
  1099. bits := max_length;
  1100. Inc(overflow);
  1101. end;
  1102. tree^[n].dl.Len := ush(bits);
  1103. { We overwrite tree[n].dl.Dad which is no longer needed }
  1104. if (n > max_code) then
  1105. continue; { not a leaf node }
  1106. Inc(s.bl_count[bits]);
  1107. xbits := 0;
  1108. if (n >= base) then
  1109. xbits := extra^[n-base];
  1110. f := tree^[n].fc.Freq;
  1111. Inc(s.opt_len, ulg(f) * (bits + xbits));
  1112. if (stree <> NIL) then
  1113. Inc(s.static_len, ulg(f) * (stree^[n].dl.Len + xbits));
  1114. end;
  1115. if (overflow = 0) then
  1116. exit;
  1117. {$ifdef DEBUG}
  1118. Tracev(^M'bit length overflow');
  1119. {$endif}
  1120. { This happens for example on obj2 and pic of the Calgary corpus }
  1121. { Find the first bit length which could increase: }
  1122. repeat
  1123. bits := max_length-1;
  1124. while (s.bl_count[bits] = 0) do
  1125. Dec(bits);
  1126. Dec(s.bl_count[bits]); { move one leaf down the tree }
  1127. Inc(s.bl_count[bits+1], 2); { move one overflow item as its brother }
  1128. Dec(s.bl_count[max_length]);
  1129. { The brother of the overflow item also moves one step up,
  1130. but this does not affect bl_count[max_length] }
  1131. Dec(overflow, 2);
  1132. until (overflow <= 0);
  1133. { Now recompute all bit lengths, scanning in increasing frequency.
  1134. h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
  1135. lengths instead of fixing only the wrong ones. This idea is taken
  1136. from 'ar' written by Haruhiko Okumura.) }
  1137. h := HEAP_SIZE; { Delphi3: compiler warning w/o this }
  1138. for bits := max_length downto 1 do
  1139. begin
  1140. n := s.bl_count[bits];
  1141. while (n <> 0) do
  1142. begin
  1143. Dec(h);
  1144. m := s.heap[h];
  1145. if (m > max_code) then
  1146. continue;
  1147. if (tree^[m].dl.Len <> unsigned(bits)) then
  1148. begin
  1149. {$ifdef DEBUG}
  1150. Trace('code '+IntToStr(m)+' bits '+IntToStr(tree^[m].dl.Len)
  1151. +'.'+IntToStr(bits));
  1152. {$ENDIF}
  1153. Inc(s.opt_len, (long(bits) - long(tree^[m].dl.Len))
  1154. * long(tree^[m].fc.Freq) );
  1155. tree^[m].dl.Len := ush(bits);
  1156. end;
  1157. Dec(n);
  1158. end;
  1159. end;
  1160. end;
  1161. { ===========================================================================
  1162. Construct one Huffman tree and assigns the code bit strings and lengths.
  1163. Update the total bit length for the current block.
  1164. IN assertion: the field freq is set for all tree elements.
  1165. OUT assertions: the fields len and code are set to the optimal bit length
  1166. and corresponding code. The length opt_len is updated; static_len is
  1167. also updated if stree is not null. The field max_code is set. }
  1168. {local}
  1169. procedure build_tree(var s : deflate_state;
  1170. var desc : tree_desc); { the tree descriptor }
  1171. var
  1172. tree : tree_ptr;
  1173. stree : tree_ptr; {const}
  1174. elems : int;
  1175. n, m : int; { iterate over heap elements }
  1176. max_code : int; { largest code with non zero frequency }
  1177. node : int; { new node being created }
  1178. begin
  1179. tree := desc.dyn_tree;
  1180. stree := desc.stat_desc^.static_tree;
  1181. elems := desc.stat_desc^.elems;
  1182. max_code := -1;
  1183. { Construct the initial heap, with least frequent element in
  1184. heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
  1185. heap[0] is not used. }
  1186. s.heap_len := 0;
  1187. s.heap_max := HEAP_SIZE;
  1188. for n := 0 to elems-1 do
  1189. begin
  1190. if (tree^[n].fc.Freq <> 0) then
  1191. begin
  1192. max_code := n;
  1193. Inc(s.heap_len);
  1194. s.heap[s.heap_len] := n;
  1195. s.depth[n] := 0;
  1196. end
  1197. else
  1198. begin
  1199. tree^[n].dl.Len := 0;
  1200. end;
  1201. end;
  1202. { The pkzip format requires that at least one distance code exists,
  1203. and that at least one bit should be sent even if there is only one
  1204. possible code. So to avoid special checks later on we force at least
  1205. two codes of non zero frequency. }
  1206. while (s.heap_len < 2) do
  1207. begin
  1208. Inc(s.heap_len);
  1209. if (max_code < 2) then
  1210. begin
  1211. Inc(max_code);
  1212. s.heap[s.heap_len] := max_code;
  1213. node := max_code;
  1214. end
  1215. else
  1216. begin
  1217. s.heap[s.heap_len] := 0;
  1218. node := 0;
  1219. end;
  1220. tree^[node].fc.Freq := 1;
  1221. s.depth[node] := 0;
  1222. Dec(s.opt_len);
  1223. if (stree <> NIL) then
  1224. Dec(s.static_len, stree^[node].dl.Len);
  1225. { node is 0 or 1 so it does not have extra bits }
  1226. end;
  1227. desc.max_code := max_code;
  1228. { The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
  1229. establish sub-heaps of increasing lengths: }
  1230. for n := s.heap_len div 2 downto 1 do
  1231. pqdownheap(s, tree^, n);
  1232. { Construct the Huffman tree by repeatedly combining the least two
  1233. frequent nodes. }
  1234. node := elems; { next internal node of the tree }
  1235. repeat
  1236. {pqremove(s, tree, n);} { n := node of least frequency }
  1237. n := s.heap[SMALLEST];
  1238. s.heap[SMALLEST] := s.heap[s.heap_len];
  1239. Dec(s.heap_len);
  1240. pqdownheap(s, tree^, SMALLEST);
  1241. m := s.heap[SMALLEST]; { m := node of next least frequency }
  1242. Dec(s.heap_max);
  1243. s.heap[s.heap_max] := n; { keep the nodes sorted by frequency }
  1244. Dec(s.heap_max);
  1245. s.heap[s.heap_max] := m;
  1246. { Create a new node father of n and m }
  1247. tree^[node].fc.Freq := tree^[n].fc.Freq + tree^[m].fc.Freq;
  1248. { maximum }
  1249. if (s.depth[n] >= s.depth[m]) then
  1250. s.depth[node] := uch (s.depth[n] + 1)
  1251. else
  1252. s.depth[node] := uch (s.depth[m] + 1);
  1253. tree^[m].dl.Dad := ush(node);
  1254. tree^[n].dl.Dad := ush(node);
  1255. {$ifdef DUMP_BL_TREE}
  1256. if (tree = tree_ptr(@s.bl_tree)) then
  1257. begin
  1258. WriteLn(#13'node ',node,'(',tree^[node].fc.Freq,') sons ',n,
  1259. '(',tree^[n].fc.Freq,') ', m, '(',tree^[m].fc.Freq,')');
  1260. end;
  1261. {$endif}
  1262. { and insert the new node in the heap }
  1263. s.heap[SMALLEST] := node;
  1264. Inc(node);
  1265. pqdownheap(s, tree^, SMALLEST);
  1266. until (s.heap_len < 2);
  1267. Dec(s.heap_max);
  1268. s.heap[s.heap_max] := s.heap[SMALLEST];
  1269. { At this point, the fields freq and dad are set. We can now
  1270. generate the bit lengths. }
  1271. gen_bitlen(s, desc);
  1272. { The field len is now set, we can generate the bit codes }
  1273. gen_codes (tree, max_code, s.bl_count);
  1274. end;
  1275. { ===========================================================================
  1276. Scan a literal or distance tree to determine the frequencies of the codes
  1277. in the bit length tree. }
  1278. {local}
  1279. procedure scan_tree(var s : deflate_state;
  1280. var tree : array of ct_data; { the tree to be scanned }
  1281. max_code : int); { and its largest code of non zero frequency }
  1282. var
  1283. n : int; { iterates over all tree elements }
  1284. prevlen : int; { last emitted length }
  1285. curlen : int; { length of current code }
  1286. nextlen : int; { length of next code }
  1287. count : int; { repeat count of the current code }
  1288. max_count : int; { max repeat count }
  1289. min_count : int; { min repeat count }
  1290. begin
  1291. prevlen := -1;
  1292. nextlen := tree[0].dl.Len;
  1293. count := 0;
  1294. max_count := 7;
  1295. min_count := 4;
  1296. if (nextlen = 0) then
  1297. begin
  1298. max_count := 138;
  1299. min_count := 3;
  1300. end;
  1301. tree[max_code+1].dl.Len := ush($ffff); { guard }
  1302. for n := 0 to max_code do
  1303. begin
  1304. curlen := nextlen;
  1305. nextlen := tree[n+1].dl.Len;
  1306. Inc(count);
  1307. if (count < max_count) and (curlen = nextlen) then
  1308. continue
  1309. else
  1310. if (count < min_count) then
  1311. Inc(s.bl_tree[curlen].fc.Freq, count)
  1312. else
  1313. if (curlen <> 0) then
  1314. begin
  1315. if (curlen <> prevlen) then
  1316. Inc(s.bl_tree[curlen].fc.Freq);
  1317. Inc(s.bl_tree[REP_3_6].fc.Freq);
  1318. end
  1319. else
  1320. if (count <= 10) then
  1321. Inc(s.bl_tree[REPZ_3_10].fc.Freq)
  1322. else
  1323. Inc(s.bl_tree[REPZ_11_138].fc.Freq);
  1324. count := 0;
  1325. prevlen := curlen;
  1326. if (nextlen = 0) then
  1327. begin
  1328. max_count := 138;
  1329. min_count := 3;
  1330. end
  1331. else
  1332. if (curlen = nextlen) then
  1333. begin
  1334. max_count := 6;
  1335. min_count := 3;
  1336. end
  1337. else
  1338. begin
  1339. max_count := 7;
  1340. min_count := 4;
  1341. end;
  1342. end;
  1343. end;
  1344. { ===========================================================================
  1345. Send a literal or distance tree in compressed form, using the codes in
  1346. bl_tree. }
  1347. {local}
  1348. procedure send_tree(var s : deflate_state;
  1349. var tree : array of ct_data; { the tree to be scanned }
  1350. max_code : int); { and its largest code of non zero frequency }
  1351. var
  1352. n : int; { iterates over all tree elements }
  1353. prevlen : int; { last emitted length }
  1354. curlen : int; { length of current code }
  1355. nextlen : int; { length of next code }
  1356. count : int; { repeat count of the current code }
  1357. max_count : int; { max repeat count }
  1358. min_count : int; { min repeat count }
  1359. begin
  1360. prevlen := -1;
  1361. nextlen := tree[0].dl.Len;
  1362. count := 0;
  1363. max_count := 7;
  1364. min_count := 4;
  1365. { tree[max_code+1].dl.Len := -1; } { guard already set }
  1366. if (nextlen = 0) then
  1367. begin
  1368. max_count := 138;
  1369. min_count := 3;
  1370. end;
  1371. for n := 0 to max_code do
  1372. begin
  1373. curlen := nextlen;
  1374. nextlen := tree[n+1].dl.Len;
  1375. Inc(count);
  1376. if (count < max_count) and (curlen = nextlen) then
  1377. continue
  1378. else
  1379. if (count < min_count) then
  1380. begin
  1381. repeat
  1382. {$ifdef DEBUG}
  1383. Tracevvv(#13'cd '+IntToStr(curlen));
  1384. {$ENDIF}
  1385. send_bits(s, s.bl_tree[curlen].fc.Code, s.bl_tree[curlen].dl.Len);
  1386. Dec(count);
  1387. until (count = 0);
  1388. end
  1389. else
  1390. if (curlen <> 0) then
  1391. begin
  1392. if (curlen <> prevlen) then
  1393. begin
  1394. {$ifdef DEBUG}
  1395. Tracevvv(#13'cd '+IntToStr(curlen));
  1396. {$ENDIF}
  1397. send_bits(s, s.bl_tree[curlen].fc.Code, s.bl_tree[curlen].dl.Len);
  1398. Dec(count);
  1399. end;
  1400. {$IFDEF DEBUG}
  1401. Assert((count >= 3) and (count <= 6), ' 3_6?');
  1402. {$ENDIF}
  1403. {$ifdef DEBUG}
  1404. Tracevvv(#13'cd '+IntToStr(REP_3_6));
  1405. {$ENDIF}
  1406. send_bits(s, s.bl_tree[REP_3_6].fc.Code, s.bl_tree[REP_3_6].dl.Len);
  1407. send_bits(s, count-3, 2);
  1408. end
  1409. else
  1410. if (count <= 10) then
  1411. begin
  1412. {$ifdef DEBUG}
  1413. Tracevvv(#13'cd '+IntToStr(REPZ_3_10));
  1414. {$ENDIF}
  1415. send_bits(s, s.bl_tree[REPZ_3_10].fc.Code, s.bl_tree[REPZ_3_10].dl.Len);
  1416. send_bits(s, count-3, 3);
  1417. end
  1418. else
  1419. begin
  1420. {$ifdef DEBUG}
  1421. Tracevvv(#13'cd '+IntToStr(REPZ_11_138));
  1422. {$ENDIF}
  1423. send_bits(s, s.bl_tree[REPZ_11_138].fc.Code, s.bl_tree[REPZ_11_138].dl.Len);
  1424. send_bits(s, count-11, 7);
  1425. end;
  1426. count := 0;
  1427. prevlen := curlen;
  1428. if (nextlen = 0) then
  1429. begin
  1430. max_count := 138;
  1431. min_count := 3;
  1432. end
  1433. else
  1434. if (curlen = nextlen) then
  1435. begin
  1436. max_count := 6;
  1437. min_count := 3;
  1438. end
  1439. else
  1440. begin
  1441. max_count := 7;
  1442. min_count := 4;
  1443. end;
  1444. end;
  1445. end;
  1446. { ===========================================================================
  1447. Construct the Huffman tree for the bit lengths and return the index in
  1448. bl_order of the last bit length code to send. }
  1449. {local}
  1450. function build_bl_tree(var s : deflate_state) : int;
  1451. var
  1452. max_blindex : int; { index of last bit length code of non zero freq }
  1453. begin
  1454. { Determine the bit length frequencies for literal and distance trees }
  1455. scan_tree(s, s.dyn_ltree, s.l_desc.max_code);
  1456. scan_tree(s, s.dyn_dtree, s.d_desc.max_code);
  1457. { Build the bit length tree: }
  1458. build_tree(s, s.bl_desc);
  1459. { opt_len now includes the length of the tree representations, except
  1460. the lengths of the bit lengths codes and the 5+5+4 bits for the counts. }
  1461. { Determine the number of bit length codes to send. The pkzip format
  1462. requires that at least 4 bit length codes be sent. (appnote.txt says
  1463. 3 but the actual value used is 4.) }
  1464. for max_blindex := BL_CODES-1 downto 3 do
  1465. begin
  1466. if (s.bl_tree[bl_order[max_blindex]].dl.Len <> 0) then
  1467. break;
  1468. end;
  1469. { Update opt_len to include the bit length tree and counts }
  1470. Inc(s.opt_len, 3*(max_blindex+1) + 5+5+4);
  1471. {$ifdef DEBUG}
  1472. Tracev(^M'dyn trees: dyn %ld, stat %ld {s.opt_len, s.static_len}');
  1473. {$ENDIF}
  1474. build_bl_tree := max_blindex;
  1475. end;
  1476. { ===========================================================================
  1477. Send the header for a block using dynamic Huffman trees: the counts, the
  1478. lengths of the bit length codes, the literal tree and the distance tree.
  1479. IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4. }
  1480. {local}
  1481. procedure send_all_trees(var s : deflate_state;
  1482. lcodes : int;
  1483. dcodes : int;
  1484. blcodes : int); { number of codes for each tree }
  1485. var
  1486. rank : int; { index in bl_order }
  1487. begin
  1488. {$IFDEF DEBUG}
  1489. Assert ((lcodes >= 257) and (dcodes >= 1) and (blcodes >= 4),
  1490. 'not enough codes');
  1491. Assert ((lcodes <= L_CODES) and (dcodes <= D_CODES)
  1492. and (blcodes <= BL_CODES), 'too many codes');
  1493. Tracev(^M'bl counts: ');
  1494. {$ENDIF}
  1495. send_bits(s, lcodes-257, 5); { not +255 as stated in appnote.txt }
  1496. send_bits(s, dcodes-1, 5);
  1497. send_bits(s, blcodes-4, 4); { not -3 as stated in appnote.txt }
  1498. for rank := 0 to blcodes-1 do
  1499. begin
  1500. {$ifdef DEBUG}
  1501. Tracev(^M'bl code '+IntToStr(bl_order[rank]));
  1502. {$ENDIF}
  1503. send_bits(s, s.bl_tree[bl_order[rank]].dl.Len, 3);
  1504. end;
  1505. {$ifdef DEBUG}
  1506. Tracev(^M'bl tree: sent '+IntToStr(s.bits_sent));
  1507. {$ENDIF}
  1508. send_tree(s, s.dyn_ltree, lcodes-1); { literal tree }
  1509. {$ifdef DEBUG}
  1510. Tracev(^M'lit tree: sent '+IntToStr(s.bits_sent));
  1511. {$ENDIF}
  1512. send_tree(s, s.dyn_dtree, dcodes-1); { distance tree }
  1513. {$ifdef DEBUG}
  1514. Tracev(^M'dist tree: sent '+IntToStr(s.bits_sent));
  1515. {$ENDIF}
  1516. end;
  1517. { ===========================================================================
  1518. Flush the bit buffer and align the output on a byte boundary }
  1519. {local}
  1520. procedure bi_windup(var s : deflate_state);
  1521. begin
  1522. if (s.bi_valid > 8) then
  1523. begin
  1524. {put_short(s, s.bi_buf);}
  1525. s.pending_buf^[s.pending] := uch(s.bi_buf and $ff);
  1526. Inc(s.pending);
  1527. s.pending_buf^[s.pending] := uch(ush(s.bi_buf) shr 8);;
  1528. Inc(s.pending);
  1529. end
  1530. else
  1531. if (s.bi_valid > 0) then
  1532. begin
  1533. {put_byte(s, (Byte)s^.bi_buf);}
  1534. s.pending_buf^[s.pending] := Byte(s.bi_buf);
  1535. Inc(s.pending);
  1536. end;
  1537. s.bi_buf := 0;
  1538. s.bi_valid := 0;
  1539. {$ifdef DEBUG}
  1540. s.bits_sent := (s.bits_sent+7) and (not 7);
  1541. {$endif}
  1542. end;
  1543. { ===========================================================================
  1544. Copy a stored block, storing first the length and its
  1545. one's complement if requested. }
  1546. {local}
  1547. procedure copy_block(var s : deflate_state;
  1548. buf : pcharf; { the input data }
  1549. len : unsigned; { its length }
  1550. header : boolean); { true if block header must be written }
  1551. begin
  1552. bi_windup(s); { align on byte boundary }
  1553. s.last_eob_len := 8; { enough lookahead for inflate }
  1554. if (header) then
  1555. begin
  1556. {put_short(s, (ush)len);}
  1557. s.pending_buf^[s.pending] := uch(ush(len) and $ff);
  1558. Inc(s.pending);
  1559. s.pending_buf^[s.pending] := uch(ush(len) shr 8);;
  1560. Inc(s.pending);
  1561. {put_short(s, (ush)~len);}
  1562. s.pending_buf^[s.pending] := uch(ush(not len) and $ff);
  1563. Inc(s.pending);
  1564. s.pending_buf^[s.pending] := uch(ush(not len) shr 8);;
  1565. Inc(s.pending);
  1566. {$ifdef DEBUG}
  1567. Inc(s.bits_sent, 2*16);
  1568. {$endif}
  1569. end;
  1570. {$ifdef DEBUG}
  1571. Inc(s.bits_sent, ulg(len shl 3));
  1572. {$endif}
  1573. while (len <> 0) do
  1574. begin
  1575. Dec(len);
  1576. {put_byte(s, *buf++);}
  1577. s.pending_buf^[s.pending] := buf^;
  1578. Inc(buf);
  1579. Inc(s.pending);
  1580. end;
  1581. end;
  1582. { ===========================================================================
  1583. Send a stored block }
  1584. procedure _tr_stored_block(var s : deflate_state;
  1585. buf : pcharf; { input block }
  1586. stored_len : ulg; { length of input block }
  1587. eof : boolean); { true if this is the last block for a file }
  1588. begin
  1589. send_bits(s, (STORED_BLOCK shl 1)+ord(eof), 3); { send block type }
  1590. s.compressed_len := (s.compressed_len + 3 + 7) and ulg(not Long(7));
  1591. Inc(s.compressed_len, (stored_len + 4) shl 3);
  1592. copy_block(s, buf, unsigned(stored_len), TRUE); { with header }
  1593. end;
  1594. { ===========================================================================
  1595. Flush the bit buffer, keeping at most 7 bits in it. }
  1596. {local}
  1597. procedure bi_flush(var s : deflate_state);
  1598. begin
  1599. if (s.bi_valid = 16) then
  1600. begin
  1601. {put_short(s, s.bi_buf);}
  1602. s.pending_buf^[s.pending] := uch(s.bi_buf and $ff);
  1603. Inc(s.pending);
  1604. s.pending_buf^[s.pending] := uch(ush(s.bi_buf) shr 8);;
  1605. Inc(s.pending);
  1606. s.bi_buf := 0;
  1607. s.bi_valid := 0;
  1608. end
  1609. else
  1610. if (s.bi_valid >= 8) then
  1611. begin
  1612. {put_byte(s, (Byte)s^.bi_buf);}
  1613. s.pending_buf^[s.pending] := Byte(s.bi_buf);
  1614. Inc(s.pending);
  1615. s.bi_buf := s.bi_buf shr 8;
  1616. Dec(s.bi_valid, 8);
  1617. end;
  1618. end;
  1619. { ===========================================================================
  1620. Send one empty static block to give enough lookahead for inflate.
  1621. This takes 10 bits, of which 7 may remain in the bit buffer.
  1622. The current inflate code requires 9 bits of lookahead. If the
  1623. last two codes for the previous block (real code plus EOB) were coded
  1624. on 5 bits or less, inflate may have only 5+3 bits of lookahead to decode
  1625. the last real code. In this case we send two empty static blocks instead
  1626. of one. (There are no problems if the previous block is stored or fixed.)
  1627. To simplify the code, we assume the worst case of last real code encoded
  1628. on one bit only. }
  1629. procedure _tr_align(var s : deflate_state);
  1630. begin
  1631. send_bits(s, STATIC_TREES shl 1, 3);
  1632. {$ifdef DEBUG}
  1633. Tracevvv(#13'cd '+IntToStr(END_BLOCK));
  1634. {$ENDIF}
  1635. send_bits(s, static_ltree[END_BLOCK].fc.Code, static_ltree[END_BLOCK].dl.Len);
  1636. Inc(s.compressed_len, Long(10)); { 3 for block type, 7 for EOB }
  1637. bi_flush(s);
  1638. { Of the 10 bits for the empty block, we have already sent
  1639. (10 - bi_valid) bits. The lookahead for the last real code (before
  1640. the EOB of the previous block) was thus at least one plus the length
  1641. of the EOB plus what we have just sent of the empty static block. }
  1642. if (1 + s.last_eob_len + 10 - s.bi_valid < 9) then
  1643. begin
  1644. send_bits(s, STATIC_TREES shl 1, 3);
  1645. {$ifdef DEBUG}
  1646. Tracevvv(#13'cd '+IntToStr(END_BLOCK));
  1647. {$ENDIF}
  1648. send_bits(s, static_ltree[END_BLOCK].fc.Code, static_ltree[END_BLOCK].dl.Len);
  1649. Inc(s.compressed_len, Long(10));
  1650. bi_flush(s);
  1651. end;
  1652. s.last_eob_len := 7;
  1653. end;
  1654. { ===========================================================================
  1655. Set the data type to ASCII or BINARY, using a crude approximation:
  1656. binary if more than 20% of the bytes are <= 6 or >= 128, ascii otherwise.
  1657. IN assertion: the fields freq of dyn_ltree are set and the total of all
  1658. frequencies does not exceed 64K (to fit in an int on 16 bit machines). }
  1659. {local}
  1660. procedure set_data_type(var s : deflate_state);
  1661. var
  1662. n : int;
  1663. ascii_freq : unsigned;
  1664. bin_freq : unsigned;
  1665. begin
  1666. n := 0;
  1667. ascii_freq := 0;
  1668. bin_freq := 0;
  1669. while (n < 7) do
  1670. begin
  1671. Inc(bin_freq, s.dyn_ltree[n].fc.Freq);
  1672. Inc(n);
  1673. end;
  1674. while (n < 128) do
  1675. begin
  1676. Inc(ascii_freq, s.dyn_ltree[n].fc.Freq);
  1677. Inc(n);
  1678. end;
  1679. while (n < LITERALS) do
  1680. begin
  1681. Inc(bin_freq, s.dyn_ltree[n].fc.Freq);
  1682. Inc(n);
  1683. end;
  1684. if (bin_freq > (ascii_freq shr 2)) then
  1685. s.data_type := Byte(Z_BINARY)
  1686. else
  1687. s.data_type := Byte(Z_ASCII);
  1688. end;
  1689. { ===========================================================================
  1690. Send the block data compressed using the given Huffman trees }
  1691. {local}
  1692. procedure compress_block(var s : deflate_state;
  1693. var ltree : array of ct_data; { literal tree }
  1694. var dtree : array of ct_data); { distance tree }
  1695. var
  1696. dist : unsigned; { distance of matched string }
  1697. lc : int; { match length or unmatched char (if dist == 0) }
  1698. lx : unsigned; { running index in l_buf }
  1699. code : unsigned; { the code to send }
  1700. extra : int; { number of extra bits to send }
  1701. begin
  1702. lx := 0;
  1703. if (s.last_lit <> 0) then
  1704. repeat
  1705. dist := s.d_buf^[lx];
  1706. lc := s.l_buf^[lx];
  1707. Inc(lx);
  1708. if (dist = 0) then
  1709. begin
  1710. { send a literal byte }
  1711. {$ifdef DEBUG}
  1712. Tracevvv(#13'cd '+IntToStr(lc));
  1713. Tracecv((lc > 31) and (lc < 128), ' '+AnsiChar(lc)+' ');
  1714. {$ENDIF}
  1715. send_bits(s, ltree[lc].fc.Code, ltree[lc].dl.Len);
  1716. end
  1717. else
  1718. begin
  1719. { Here, lc is the match length - MIN_MATCH }
  1720. code := _length_code[lc];
  1721. { send the length code }
  1722. {$ifdef DEBUG}
  1723. Tracevvv(#13'cd '+IntToStr(code+LITERALS+1));
  1724. {$ENDIF}
  1725. send_bits(s, ltree[code+LITERALS+1].fc.Code, ltree[code+LITERALS+1].dl.Len);
  1726. extra := extra_lbits[code];
  1727. if (extra <> 0) then
  1728. begin
  1729. Dec(lc, base_length[code]);
  1730. send_bits(s, lc, extra); { send the extra length bits }
  1731. end;
  1732. Dec(dist); { dist is now the match distance - 1 }
  1733. {code := d_code(dist);}
  1734. if (dist < 256) then
  1735. code := _dist_code[dist]
  1736. else
  1737. code := _dist_code[256+(dist shr 7)];
  1738. {$IFDEF DEBUG}
  1739. Assert (code < D_CODES, 'bad d_code');
  1740. {$ENDIF}
  1741. { send the distance code }
  1742. {$ifdef DEBUG}
  1743. Tracevvv(#13'cd '+IntToStr(code));
  1744. {$ENDIF}
  1745. send_bits(s, dtree[code].fc.Code, dtree[code].dl.Len);
  1746. extra := extra_dbits[code];
  1747. if (extra <> 0) then
  1748. begin
  1749. Dec(dist, base_dist[code]);
  1750. send_bits(s, dist, extra); { send the extra distance bits }
  1751. end;
  1752. end; { literal or match pair ? }
  1753. { Check that the overlay between pending_buf and d_buf+l_buf is ok: }
  1754. {$IFDEF DEBUG}
  1755. Assert(s.pending < s.lit_bufsize + 2*lx, 'pendingBuf overflow');
  1756. {$ENDIF}
  1757. until (lx >= s.last_lit);
  1758. {$ifdef DEBUG}
  1759. Tracevvv(#13'cd '+IntToStr(END_BLOCK));
  1760. {$ENDIF}
  1761. send_bits(s, ltree[END_BLOCK].fc.Code, ltree[END_BLOCK].dl.Len);
  1762. s.last_eob_len := ltree[END_BLOCK].dl.Len;
  1763. end;
  1764. { ===========================================================================
  1765. Determine the best encoding for the current block: dynamic trees, static
  1766. trees or store, and output the encoded block to the zip file. This function
  1767. returns the total compressed length for the file so far. }
  1768. function _tr_flush_block (var s : deflate_state;
  1769. buf : pcharf; { input block, or NULL if too old }
  1770. stored_len : ulg; { length of input block }
  1771. eof : boolean) : ulg; { true if this is the last block for a file }
  1772. var
  1773. opt_lenb, static_lenb : ulg; { opt_len and static_len in bytes }
  1774. max_blindex : int; { index of last bit length code of non zero freq }
  1775. begin
  1776. max_blindex := 0;
  1777. { Build the Huffman trees unless a stored block is forced }
  1778. if (s.level > 0) then
  1779. begin
  1780. { Check if the file is ascii or binary }
  1781. if (s.data_type = Z_UNKNOWN) then
  1782. set_data_type(s);
  1783. { Construct the literal and distance trees }
  1784. build_tree(s, s.l_desc);
  1785. {$ifdef DEBUG}
  1786. Tracev(^M'lit data: dyn %ld, stat %ld {s.opt_len, s.static_len}');
  1787. {$ENDIF}
  1788. build_tree(s, s.d_desc);
  1789. {$ifdef DEBUG}
  1790. Tracev(^M'dist data: dyn %ld, stat %ld {s.opt_len, s.static_len}');
  1791. {$ENDIF}
  1792. { At this point, opt_len and static_len are the total bit lengths of
  1793. the compressed block data, excluding the tree representations. }
  1794. { Build the bit length tree for the above two trees, and get the index
  1795. in bl_order of the last bit length code to send. }
  1796. max_blindex := build_bl_tree(s);
  1797. { Determine the best encoding. Compute first the block length in bytes}
  1798. opt_lenb := (s.opt_len+3+7) shr 3;
  1799. static_lenb := (s.static_len+3+7) shr 3;
  1800. {$ifdef DEBUG}
  1801. Tracev(^M'opt %lu(%lu) stat %lu(%lu) stored %lu lit %u '+
  1802. '{opt_lenb, s.opt_len, static_lenb, s.static_len, stored_len,'+
  1803. 's.last_lit}');
  1804. {$ENDIF}
  1805. if (static_lenb <= opt_lenb) then
  1806. opt_lenb := static_lenb;
  1807. end
  1808. else
  1809. begin
  1810. {$IFDEF DEBUG}
  1811. Assert(buf <> pcharf(NIL), 'lost buf');
  1812. {$ENDIF}
  1813. static_lenb := stored_len + 5;
  1814. opt_lenb := static_lenb; { force a stored block }
  1815. end;
  1816. { If compression failed and this is the first and last block,
  1817. and if the .zip file can be seeked (to rewrite the local header),
  1818. the whole file is transformed into a stored file: }
  1819. {$ifdef STORED_FILE_OK}
  1820. {$ifdef FORCE_STORED_FILE}
  1821. if eof and (s.compressed_len = Long(0)) then
  1822. begin { force stored file }
  1823. {$else}
  1824. if (stored_len <= opt_lenb) and eof and (s.compressed_len=Long(0))
  1825. and seekable()) do
  1826. begin
  1827. {$endif}
  1828. { Since LIT_BUFSIZE <= 2*WSIZE, the input data must be there: }
  1829. if (buf = pcharf(0)) then
  1830. error ('block vanished');
  1831. copy_block(buf, unsigned(stored_len), 0); { without header }
  1832. s.compressed_len := stored_len shl 3;
  1833. s.method := STORED;
  1834. end
  1835. else
  1836. {$endif} { STORED_FILE_OK }
  1837. {$ifdef FORCE_STORED}
  1838. if (buf <> pcharf(0)) then
  1839. begin { force stored block }
  1840. {$else}
  1841. if (stored_len+4 <= opt_lenb) and (buf <> pcharf(0)) then
  1842. begin
  1843. { 4: two words for the lengths }
  1844. {$endif}
  1845. { The test buf <> NULL is only necessary if LIT_BUFSIZE > WSIZE.
  1846. Otherwise we can't have processed more than WSIZE input bytes since
  1847. the last block flush, because compression would have been
  1848. successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
  1849. transform a block into a stored block. }
  1850. _tr_stored_block(s, buf, stored_len, eof);
  1851. {$ifdef FORCE_STATIC}
  1852. end
  1853. else
  1854. if (static_lenb >= 0) then
  1855. begin { force static trees }
  1856. {$else}
  1857. end
  1858. else
  1859. if (static_lenb = opt_lenb) then
  1860. begin
  1861. {$endif}
  1862. send_bits(s, (STATIC_TREES shl 1)+ord(eof), 3);
  1863. compress_block(s, static_ltree, static_dtree);
  1864. Inc(s.compressed_len, 3 + s.static_len);
  1865. end
  1866. else
  1867. begin
  1868. send_bits(s, (DYN_TREES shl 1)+ord(eof), 3);
  1869. send_all_trees(s, s.l_desc.max_code+1, s.d_desc.max_code+1,
  1870. max_blindex+1);
  1871. compress_block(s, s.dyn_ltree, s.dyn_dtree);
  1872. Inc(s.compressed_len, 3 + s.opt_len);
  1873. end;
  1874. {$ifdef DEBUG}
  1875. Assert (s.compressed_len = s.bits_sent, 'bad compressed size');
  1876. {$ENDIF}
  1877. init_block(s);
  1878. if (eof) then
  1879. begin
  1880. bi_windup(s);
  1881. Inc(s.compressed_len, 7); { align on byte boundary }
  1882. end;
  1883. {$ifdef DEBUG}
  1884. Tracev(#13'comprlen %lu(%lu) {s.compressed_len shr 3,'+
  1885. 's.compressed_len-7*ord(eof)}');
  1886. {$ENDIF}
  1887. _tr_flush_block := s.compressed_len shr 3;
  1888. end;
  1889. { ===========================================================================
  1890. Save the match info and tally the frequency counts. Return true if
  1891. the current block must be flushed. }
  1892. function _tr_tally (var s : deflate_state;
  1893. dist : unsigned; { distance of matched string }
  1894. lc : unsigned) : boolean; { match length-MIN_MATCH or unmatched char (if dist=0) }
  1895. var
  1896. {$IFDEF DEBUG}
  1897. MAX_DIST : ush;
  1898. {$ENDIF}
  1899. code : ush;
  1900. {$ifdef TRUNCATE_BLOCK}
  1901. var
  1902. out_length : ulg;
  1903. in_length : ulg;
  1904. dcode : int;
  1905. {$endif}
  1906. begin
  1907. s.d_buf^[s.last_lit] := ush(dist);
  1908. s.l_buf^[s.last_lit] := uch(lc);
  1909. Inc(s.last_lit);
  1910. if (dist = 0) then
  1911. begin
  1912. { lc is the unmatched char }
  1913. Inc(s.dyn_ltree[lc].fc.Freq);
  1914. end
  1915. else
  1916. begin
  1917. Inc(s.matches);
  1918. { Here, lc is the match length - MIN_MATCH }
  1919. Dec(dist); { dist := match distance - 1 }
  1920. {macro d_code(dist)}
  1921. if (dist) < 256 then
  1922. code := _dist_code[dist]
  1923. else
  1924. code := _dist_code[256+(dist shr 7)];
  1925. {$IFDEF DEBUG}
  1926. {macro MAX_DIST(s) <=> ((s)^.w_size-MIN_LOOKAHEAD)
  1927. In order to simplify the code, particularly on 16 bit machines, match
  1928. distances are limited to MAX_DIST instead of WSIZE. }
  1929. MAX_DIST := ush(s.w_size-MIN_LOOKAHEAD);
  1930. Assert((dist < ush(MAX_DIST)) and
  1931. (ush(lc) <= ush(MAX_MATCH-MIN_MATCH)) and
  1932. (ush(code) < ush(D_CODES)), '_tr_tally: bad match');
  1933. {$ENDIF}
  1934. Inc(s.dyn_ltree[_length_code[lc]+LITERALS+1].fc.Freq);
  1935. {s.dyn_dtree[d_code(dist)].Freq++;}
  1936. Inc(s.dyn_dtree[code].fc.Freq);
  1937. end;
  1938. {$ifdef TRUNCATE_BLOCK}
  1939. { Try to guess if it is profitable to stop the current block here }
  1940. if (s.last_lit and $1fff = 0) and (s.level > 2) then
  1941. begin
  1942. { Compute an upper bound for the compressed length }
  1943. out_length := ulg(s.last_lit)*Long(8);
  1944. in_length := ulg(long(s.strstart) - s.block_start);
  1945. for dcode := 0 to D_CODES-1 do
  1946. begin
  1947. Inc(out_length, ulg(s.dyn_dtree[dcode].fc.Freq *
  1948. (Long(5)+extra_dbits[dcode])) );
  1949. end;
  1950. out_length := out_length shr 3;
  1951. {$ifdef DEBUG}
  1952. Tracev(^M'last_lit %u, in %ld, out ~%ld(%ld%%) ');
  1953. { s.last_lit, in_length, out_length,
  1954. Long(100) - out_length*Long(100) div in_length)); }
  1955. {$ENDIF}
  1956. if (s.matches < s.last_lit div 2) and (out_length < in_length div 2) then
  1957. begin
  1958. _tr_tally := TRUE;
  1959. exit;
  1960. end;
  1961. end;
  1962. {$endif}
  1963. _tr_tally := (s.last_lit = s.lit_bufsize-1);
  1964. { We avoid equality with lit_bufsize because of wraparound at 64K
  1965. on 16 bit machines and because stored blocks are restricted to
  1966. 64K-1 bytes. }
  1967. end;
  1968. end.