6 trees.c -- output deflated data using Huffman coding
\r
7 Copyright (C) 1995-1998 Jean-loup Gailly
\r
10 Copyright (C) 1998 by Jacques Nomssi Nzali
\r
11 For conditions of distribution and use, see copyright notice in readme.paszlib
\r
17 * The "deflation" process uses several Huffman trees. The more
\r
18 * common source values are represented by shorter bit sequences.
\r
20 * Each code tree is stored in a compressed form which is itself
\r
21 * a Huffman encoding of the lengths of all the code strings (in
\r
22 * ascending order by source values). The actual code strings are
\r
23 * reconstructed from the lengths in the inflate process, as described
\r
24 * in the deflate specification.
\r
28 * Deutsch, L.P.,"'Deflate' Compressed Data Format Specification".
\r
29 * Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc
\r
32 * Data Compression: Methods and Theory, pp. 49-50.
\r
33 * Computer Science Press, 1988. ISBN 0-7167-8156-5.
\r
37 * Addison-Wesley, 1983. ISBN 0-201-06672-6.
\r
50 { ===========================================================================
\r
51 Internal compression state. }
\r
55 { number of length codes, not counting the special END_BLOCK code }
\r
58 { number of literal bytes 0..255 }
\r
60 L_CODES = (LITERALS+1+LENGTH_CODES);
\r
61 { number of Literal or Length codes, including the END_BLOCK code }
\r
64 { number of distance codes }
\r
67 { number of codes used to transfer the bit lengths }
\r
69 HEAP_SIZE = (2*L_CODES+1);
\r
70 { maximum heap size }
\r
73 { All codes must not exceed MAX_BITS bits }
\r
82 { Data structure describing a single value and its code string. }
\r
84 ct_data_ptr = ^ct_data;
\r
88 0:(freq : ush); { frequency count }
\r
89 1:(code : ush); { bit string }
\r
93 0:(dad : ush); { father node in Huffman tree }
\r
94 1:(len : ush); { length of bit string }
\r
104 ltree_type = array[0..HEAP_SIZE-1] of ct_data; { literal and length tree }
\r
105 dtree_type = array[0..2*D_CODES+1-1] of ct_data; { distance tree }
\r
106 htree_type = array[0..2*BL_CODES+1-1] of ct_data; { Huffman tree for bit lengths }
\r
107 { generic tree type }
\r
108 tree_type = array[0..(MaxMemBlock div SizeOf(ct_data))-1] of ct_data;
\r
110 tree_ptr = ^tree_type;
\r
111 ltree_ptr = ^ltree_type;
\r
112 dtree_ptr = ^dtree_type;
\r
113 htree_ptr = ^htree_type;
\r
117 static_tree_desc_ptr = ^static_tree_desc;
\r
120 {const} static_tree : tree_ptr; { static tree or NIL }
\r
121 {const} extra_bits : pzIntfArray; { extra bits for each code or NIL }
\r
122 extra_base : int; { base index for extra_bits }
\r
123 elems : int; { max number of elements in the tree }
\r
124 max_length : int; { max bit length for the codes }
\r
127 tree_desc_ptr = ^tree_desc;
\r
129 dyn_tree : tree_ptr; { the dynamic tree }
\r
130 max_code : int; { largest code with non zero frequency }
\r
131 stat_desc : static_tree_desc_ptr; { the corresponding static tree }
\r
141 zPosfArray = array[0..(MaxMemBlock div SizeOf(Posf))-1] of Posf;
\r
142 pzPosfArray = ^zPosfArray;
\r
144 { A Pos is an index in the character window. We use short instead of int to
\r
145 save space in the various tables. IPos is used only for parameter passing.}
\r
148 deflate_state_ptr = ^deflate_state;
\r
149 deflate_state = record
\r
150 strm : z_streamp; { pointer back to this zlib stream }
\r
151 status : int; { as the name implies }
\r
152 pending_buf : pzByteArray; { output still pending }
\r
153 pending_buf_size : ulg; { size of pending_buf }
\r
154 pending_out : pBytef; { next pending byte to output to the stream }
\r
155 pending : int; { nb of bytes in the pending buffer }
\r
156 noheader : int; { suppress zlib header and adler32 }
\r
157 data_type : Byte; { UNKNOWN, BINARY or ASCII }
\r
158 method : Byte; { STORED (for zip only) or DEFLATED }
\r
159 last_flush : int; { value of flush param for previous deflate call }
\r
161 { used by deflate.pas: }
\r
163 w_size : uInt; { LZ77 window size (32K by default) }
\r
164 w_bits : uInt; { log2(w_size) (8..16) }
\r
165 w_mask : uInt; { w_size - 1 }
\r
167 window : pzByteArray;
\r
168 { Sliding window. Input bytes are read into the second half of the window,
\r
169 and move to the first half later to keep a dictionary of at least wSize
\r
170 bytes. With this organization, matches are limited to a distance of
\r
171 wSize-MAX_MATCH bytes, but this ensures that IO is always
\r
172 performed with a length multiple of the block size. Also, it limits
\r
173 the window size to 64K, which is quite useful on MSDOS.
\r
174 To do: use the user input buffer as sliding window. }
\r
177 { Actual size of window: 2*wSize, except when the user input buffer
\r
178 is directly used as sliding window. }
\r
180 prev : pzPosfArray;
\r
181 { Link to older string with same hash index. To limit the size of this
\r
182 array to 64K, this link is maintained only for the last 32K strings.
\r
183 An index in this array is thus a window index modulo 32K. }
\r
185 head : pzPosfArray; { Heads of the hash chains or NIL. }
\r
187 ins_h : uInt; { hash index of string to be inserted }
\r
188 hash_size : uInt; { number of elements in hash table }
\r
189 hash_bits : uInt; { log2(hash_size) }
\r
190 hash_mask : uInt; { hash_size-1 }
\r
193 { Number of bits by which ins_h must be shifted at each input
\r
194 step. It must be such that after MIN_MATCH steps, the oldest
\r
195 byte no longer takes part in the hash key, that is:
\r
196 hash_shift * MIN_MATCH >= hash_bits }
\r
198 block_start : long;
\r
199 { Window position at the beginning of the current output block. Gets
\r
200 negative when the window is moved backwards. }
\r
202 match_length : uInt; { length of best match }
\r
203 prev_match : IPos; { previous match }
\r
204 match_available : boolean; { set if previous match exists }
\r
205 strstart : uInt; { start of string to insert }
\r
206 match_start : uInt; { start of matching string }
\r
207 lookahead : uInt; { number of valid bytes ahead in window }
\r
209 prev_length : uInt;
\r
210 { Length of the best match at previous step. Matches not greater than this
\r
211 are discarded. This is used in the lazy match evaluation. }
\r
213 max_chain_length : uInt;
\r
214 { To speed up deflation, hash chains are never searched beyond this
\r
215 length. A higher limit improves compression ratio but degrades the
\r
218 { moved to the end because Borland Pascal won't accept the following:
\r
219 max_lazy_match : uInt;
\r
220 max_insert_length : uInt absolute max_lazy_match;
\r
223 level : int; { compression level (1..9) }
\r
224 strategy : int; { favor or force Huffman coding}
\r
227 { Use a faster search when the previous match is longer than this }
\r
229 nice_match : int; { Stop searching when current match exceeds this }
\r
231 { used by trees.pas: }
\r
232 { Didn't use ct_data typedef below to supress compiler warning }
\r
233 dyn_ltree : ltree_type; { literal and length tree }
\r
234 dyn_dtree : dtree_type; { distance tree }
\r
235 bl_tree : htree_type; { Huffman tree for bit lengths }
\r
237 l_desc : tree_desc; { desc. for literal tree }
\r
238 d_desc : tree_desc; { desc. for distance tree }
\r
239 bl_desc : tree_desc; { desc. for bit length tree }
\r
241 bl_count : array[0..MAX_BITS+1-1] of ush;
\r
242 { number of codes at each bit length for an optimal tree }
\r
244 heap : array[0..2*L_CODES+1-1] of int; { heap used to build the Huffman trees }
\r
245 heap_len : int; { number of elements in the heap }
\r
246 heap_max : int; { element of largest frequency }
\r
247 { The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
\r
248 The same heap array is used to build all trees. }
\r
250 depth : array[0..2*L_CODES+1-1] of uch;
\r
251 { Depth of each subtree used as tie breaker for trees of equal frequency }
\r
254 l_buf : puchfArray; { buffer for literals or lengths }
\r
256 lit_bufsize : uInt;
\r
257 { Size of match buffer for literals/lengths. There are 4 reasons for
\r
258 limiting lit_bufsize to 64K:
\r
259 - frequencies can be kept in 16 bit counters
\r
260 - if compression is not successful for the first block, all input
\r
261 data is still in the window so we can still emit a stored block even
\r
262 when input comes from standard input. (This can also be done for
\r
263 all blocks if lit_bufsize is not greater than 32K.)
\r
264 - if compression is not successful for a file smaller than 64K, we can
\r
265 even emit a stored file instead of a stored block (saving 5 bytes).
\r
266 This is applicable only for zip (not gzip or zlib).
\r
267 - creating new Huffman trees less frequently may not provide fast
\r
268 adaptation to changes in the input data statistics. (Take for
\r
269 example a binary file with poorly compressible code followed by
\r
270 a highly compressible string table.) Smaller buffer sizes give
\r
271 fast adaptation but have of course the overhead of transmitting
\r
272 trees more frequently.
\r
273 - I can't count above 4 }
\r
276 last_lit : uInt; { running index in l_buf }
\r
278 d_buf : pushfArray;
\r
279 { Buffer for distances. To simplify the code, d_buf and l_buf have
\r
280 the same number of elements. To use different lengths, an extra flag
\r
281 array would be necessary. }
\r
283 opt_len : ulg; { bit length of current block with optimal trees }
\r
284 static_len : ulg; { bit length of current block with static trees }
\r
285 compressed_len : ulg; { total bit length of compressed file }
\r
286 matches : uInt; { number of string matches in current block }
\r
287 last_eob_len : int; { bit length of EOB code for last block }
\r
290 bits_sent : ulg; { bit length of the compressed data }
\r
294 { Output buffer. bits are inserted starting at the bottom (least
\r
295 significant bits). }
\r
298 { Number of valid bits in bi_buf. All bits above the last valid bit
\r
302 0:(max_lazy_match : uInt);
\r
303 { Attempt to find a better match only when the current match is strictly
\r
304 smaller than this value. This mechanism is used only for compression
\r
307 1:(max_insert_length : uInt);
\r
308 { Insert new strings in the hash table only if the match length is not
\r
309 greater than this length. This saves time but degrades compression.
\r
310 max_insert_length is used only for compression levels <= 3. }
\r
313 procedure _tr_init (var s : deflate_state);
\r
315 function _tr_tally (var s : deflate_state;
\r
317 lc : unsigned) : boolean;
\r
319 function _tr_flush_block (var s : deflate_state;
\r
322 eof : boolean) : ulg;
\r
324 procedure _tr_align(var s : deflate_state);
\r
326 procedure _tr_stored_block(var s : deflate_state;
\r
333 { #define GEN_TREES_H }
\r
335 {$ifndef GEN_TREES_H}
\r
336 { header created automatically with -DGEN_TREES_H }
\r
339 DIST_CODE_LEN = 512; { see definition of array dist_code below }
\r
341 { The static literal tree. Since the bit lengths are imposed, there is no
\r
342 need for the L_CODES extra codes used during heap construction. However
\r
343 The codes 286 and 287 are needed to build a canonical tree (see _tr_init
\r
346 static_ltree : array[0..L_CODES+2-1] of ct_data = (
\r
347 { fc:(freq, code) dl:(dad,len) }
\r
348 (fc:(freq: 12);dl:(len: 8)), (fc:(freq:140);dl:(len: 8)), (fc:(freq: 76);dl:(len: 8)),
\r
349 (fc:(freq:204);dl:(len: 8)), (fc:(freq: 44);dl:(len: 8)), (fc:(freq:172);dl:(len: 8)),
\r
350 (fc:(freq:108);dl:(len: 8)), (fc:(freq:236);dl:(len: 8)), (fc:(freq: 28);dl:(len: 8)),
\r
351 (fc:(freq:156);dl:(len: 8)), (fc:(freq: 92);dl:(len: 8)), (fc:(freq:220);dl:(len: 8)),
\r
352 (fc:(freq: 60);dl:(len: 8)), (fc:(freq:188);dl:(len: 8)), (fc:(freq:124);dl:(len: 8)),
\r
353 (fc:(freq:252);dl:(len: 8)), (fc:(freq: 2);dl:(len: 8)), (fc:(freq:130);dl:(len: 8)),
\r
354 (fc:(freq: 66);dl:(len: 8)), (fc:(freq:194);dl:(len: 8)), (fc:(freq: 34);dl:(len: 8)),
\r
355 (fc:(freq:162);dl:(len: 8)), (fc:(freq: 98);dl:(len: 8)), (fc:(freq:226);dl:(len: 8)),
\r
356 (fc:(freq: 18);dl:(len: 8)), (fc:(freq:146);dl:(len: 8)), (fc:(freq: 82);dl:(len: 8)),
\r
357 (fc:(freq:210);dl:(len: 8)), (fc:(freq: 50);dl:(len: 8)), (fc:(freq:178);dl:(len: 8)),
\r
358 (fc:(freq:114);dl:(len: 8)), (fc:(freq:242);dl:(len: 8)), (fc:(freq: 10);dl:(len: 8)),
\r
359 (fc:(freq:138);dl:(len: 8)), (fc:(freq: 74);dl:(len: 8)), (fc:(freq:202);dl:(len: 8)),
\r
360 (fc:(freq: 42);dl:(len: 8)), (fc:(freq:170);dl:(len: 8)), (fc:(freq:106);dl:(len: 8)),
\r
361 (fc:(freq:234);dl:(len: 8)), (fc:(freq: 26);dl:(len: 8)), (fc:(freq:154);dl:(len: 8)),
\r
362 (fc:(freq: 90);dl:(len: 8)), (fc:(freq:218);dl:(len: 8)), (fc:(freq: 58);dl:(len: 8)),
\r
363 (fc:(freq:186);dl:(len: 8)), (fc:(freq:122);dl:(len: 8)), (fc:(freq:250);dl:(len: 8)),
\r
364 (fc:(freq: 6);dl:(len: 8)), (fc:(freq:134);dl:(len: 8)), (fc:(freq: 70);dl:(len: 8)),
\r
365 (fc:(freq:198);dl:(len: 8)), (fc:(freq: 38);dl:(len: 8)), (fc:(freq:166);dl:(len: 8)),
\r
366 (fc:(freq:102);dl:(len: 8)), (fc:(freq:230);dl:(len: 8)), (fc:(freq: 22);dl:(len: 8)),
\r
367 (fc:(freq:150);dl:(len: 8)), (fc:(freq: 86);dl:(len: 8)), (fc:(freq:214);dl:(len: 8)),
\r
368 (fc:(freq: 54);dl:(len: 8)), (fc:(freq:182);dl:(len: 8)), (fc:(freq:118);dl:(len: 8)),
\r
369 (fc:(freq:246);dl:(len: 8)), (fc:(freq: 14);dl:(len: 8)), (fc:(freq:142);dl:(len: 8)),
\r
370 (fc:(freq: 78);dl:(len: 8)), (fc:(freq:206);dl:(len: 8)), (fc:(freq: 46);dl:(len: 8)),
\r
371 (fc:(freq:174);dl:(len: 8)), (fc:(freq:110);dl:(len: 8)), (fc:(freq:238);dl:(len: 8)),
\r
372 (fc:(freq: 30);dl:(len: 8)), (fc:(freq:158);dl:(len: 8)), (fc:(freq: 94);dl:(len: 8)),
\r
373 (fc:(freq:222);dl:(len: 8)), (fc:(freq: 62);dl:(len: 8)), (fc:(freq:190);dl:(len: 8)),
\r
374 (fc:(freq:126);dl:(len: 8)), (fc:(freq:254);dl:(len: 8)), (fc:(freq: 1);dl:(len: 8)),
\r
375 (fc:(freq:129);dl:(len: 8)), (fc:(freq: 65);dl:(len: 8)), (fc:(freq:193);dl:(len: 8)),
\r
376 (fc:(freq: 33);dl:(len: 8)), (fc:(freq:161);dl:(len: 8)), (fc:(freq: 97);dl:(len: 8)),
\r
377 (fc:(freq:225);dl:(len: 8)), (fc:(freq: 17);dl:(len: 8)), (fc:(freq:145);dl:(len: 8)),
\r
378 (fc:(freq: 81);dl:(len: 8)), (fc:(freq:209);dl:(len: 8)), (fc:(freq: 49);dl:(len: 8)),
\r
379 (fc:(freq:177);dl:(len: 8)), (fc:(freq:113);dl:(len: 8)), (fc:(freq:241);dl:(len: 8)),
\r
380 (fc:(freq: 9);dl:(len: 8)), (fc:(freq:137);dl:(len: 8)), (fc:(freq: 73);dl:(len: 8)),
\r
381 (fc:(freq:201);dl:(len: 8)), (fc:(freq: 41);dl:(len: 8)), (fc:(freq:169);dl:(len: 8)),
\r
382 (fc:(freq:105);dl:(len: 8)), (fc:(freq:233);dl:(len: 8)), (fc:(freq: 25);dl:(len: 8)),
\r
383 (fc:(freq:153);dl:(len: 8)), (fc:(freq: 89);dl:(len: 8)), (fc:(freq:217);dl:(len: 8)),
\r
384 (fc:(freq: 57);dl:(len: 8)), (fc:(freq:185);dl:(len: 8)), (fc:(freq:121);dl:(len: 8)),
\r
385 (fc:(freq:249);dl:(len: 8)), (fc:(freq: 5);dl:(len: 8)), (fc:(freq:133);dl:(len: 8)),
\r
386 (fc:(freq: 69);dl:(len: 8)), (fc:(freq:197);dl:(len: 8)), (fc:(freq: 37);dl:(len: 8)),
\r
387 (fc:(freq:165);dl:(len: 8)), (fc:(freq:101);dl:(len: 8)), (fc:(freq:229);dl:(len: 8)),
\r
388 (fc:(freq: 21);dl:(len: 8)), (fc:(freq:149);dl:(len: 8)), (fc:(freq: 85);dl:(len: 8)),
\r
389 (fc:(freq:213);dl:(len: 8)), (fc:(freq: 53);dl:(len: 8)), (fc:(freq:181);dl:(len: 8)),
\r
390 (fc:(freq:117);dl:(len: 8)), (fc:(freq:245);dl:(len: 8)), (fc:(freq: 13);dl:(len: 8)),
\r
391 (fc:(freq:141);dl:(len: 8)), (fc:(freq: 77);dl:(len: 8)), (fc:(freq:205);dl:(len: 8)),
\r
392 (fc:(freq: 45);dl:(len: 8)), (fc:(freq:173);dl:(len: 8)), (fc:(freq:109);dl:(len: 8)),
\r
393 (fc:(freq:237);dl:(len: 8)), (fc:(freq: 29);dl:(len: 8)), (fc:(freq:157);dl:(len: 8)),
\r
394 (fc:(freq: 93);dl:(len: 8)), (fc:(freq:221);dl:(len: 8)), (fc:(freq: 61);dl:(len: 8)),
\r
395 (fc:(freq:189);dl:(len: 8)), (fc:(freq:125);dl:(len: 8)), (fc:(freq:253);dl:(len: 8)),
\r
396 (fc:(freq: 19);dl:(len: 9)), (fc:(freq:275);dl:(len: 9)), (fc:(freq:147);dl:(len: 9)),
\r
397 (fc:(freq:403);dl:(len: 9)), (fc:(freq: 83);dl:(len: 9)), (fc:(freq:339);dl:(len: 9)),
\r
398 (fc:(freq:211);dl:(len: 9)), (fc:(freq:467);dl:(len: 9)), (fc:(freq: 51);dl:(len: 9)),
\r
399 (fc:(freq:307);dl:(len: 9)), (fc:(freq:179);dl:(len: 9)), (fc:(freq:435);dl:(len: 9)),
\r
400 (fc:(freq:115);dl:(len: 9)), (fc:(freq:371);dl:(len: 9)), (fc:(freq:243);dl:(len: 9)),
\r
401 (fc:(freq:499);dl:(len: 9)), (fc:(freq: 11);dl:(len: 9)), (fc:(freq:267);dl:(len: 9)),
\r
402 (fc:(freq:139);dl:(len: 9)), (fc:(freq:395);dl:(len: 9)), (fc:(freq: 75);dl:(len: 9)),
\r
403 (fc:(freq:331);dl:(len: 9)), (fc:(freq:203);dl:(len: 9)), (fc:(freq:459);dl:(len: 9)),
\r
404 (fc:(freq: 43);dl:(len: 9)), (fc:(freq:299);dl:(len: 9)), (fc:(freq:171);dl:(len: 9)),
\r
405 (fc:(freq:427);dl:(len: 9)), (fc:(freq:107);dl:(len: 9)), (fc:(freq:363);dl:(len: 9)),
\r
406 (fc:(freq:235);dl:(len: 9)), (fc:(freq:491);dl:(len: 9)), (fc:(freq: 27);dl:(len: 9)),
\r
407 (fc:(freq:283);dl:(len: 9)), (fc:(freq:155);dl:(len: 9)), (fc:(freq:411);dl:(len: 9)),
\r
408 (fc:(freq: 91);dl:(len: 9)), (fc:(freq:347);dl:(len: 9)), (fc:(freq:219);dl:(len: 9)),
\r
409 (fc:(freq:475);dl:(len: 9)), (fc:(freq: 59);dl:(len: 9)), (fc:(freq:315);dl:(len: 9)),
\r
410 (fc:(freq:187);dl:(len: 9)), (fc:(freq:443);dl:(len: 9)), (fc:(freq:123);dl:(len: 9)),
\r
411 (fc:(freq:379);dl:(len: 9)), (fc:(freq:251);dl:(len: 9)), (fc:(freq:507);dl:(len: 9)),
\r
412 (fc:(freq: 7);dl:(len: 9)), (fc:(freq:263);dl:(len: 9)), (fc:(freq:135);dl:(len: 9)),
\r
413 (fc:(freq:391);dl:(len: 9)), (fc:(freq: 71);dl:(len: 9)), (fc:(freq:327);dl:(len: 9)),
\r
414 (fc:(freq:199);dl:(len: 9)), (fc:(freq:455);dl:(len: 9)), (fc:(freq: 39);dl:(len: 9)),
\r
415 (fc:(freq:295);dl:(len: 9)), (fc:(freq:167);dl:(len: 9)), (fc:(freq:423);dl:(len: 9)),
\r
416 (fc:(freq:103);dl:(len: 9)), (fc:(freq:359);dl:(len: 9)), (fc:(freq:231);dl:(len: 9)),
\r
417 (fc:(freq:487);dl:(len: 9)), (fc:(freq: 23);dl:(len: 9)), (fc:(freq:279);dl:(len: 9)),
\r
418 (fc:(freq:151);dl:(len: 9)), (fc:(freq:407);dl:(len: 9)), (fc:(freq: 87);dl:(len: 9)),
\r
419 (fc:(freq:343);dl:(len: 9)), (fc:(freq:215);dl:(len: 9)), (fc:(freq:471);dl:(len: 9)),
\r
420 (fc:(freq: 55);dl:(len: 9)), (fc:(freq:311);dl:(len: 9)), (fc:(freq:183);dl:(len: 9)),
\r
421 (fc:(freq:439);dl:(len: 9)), (fc:(freq:119);dl:(len: 9)), (fc:(freq:375);dl:(len: 9)),
\r
422 (fc:(freq:247);dl:(len: 9)), (fc:(freq:503);dl:(len: 9)), (fc:(freq: 15);dl:(len: 9)),
\r
423 (fc:(freq:271);dl:(len: 9)), (fc:(freq:143);dl:(len: 9)), (fc:(freq:399);dl:(len: 9)),
\r
424 (fc:(freq: 79);dl:(len: 9)), (fc:(freq:335);dl:(len: 9)), (fc:(freq:207);dl:(len: 9)),
\r
425 (fc:(freq:463);dl:(len: 9)), (fc:(freq: 47);dl:(len: 9)), (fc:(freq:303);dl:(len: 9)),
\r
426 (fc:(freq:175);dl:(len: 9)), (fc:(freq:431);dl:(len: 9)), (fc:(freq:111);dl:(len: 9)),
\r
427 (fc:(freq:367);dl:(len: 9)), (fc:(freq:239);dl:(len: 9)), (fc:(freq:495);dl:(len: 9)),
\r
428 (fc:(freq: 31);dl:(len: 9)), (fc:(freq:287);dl:(len: 9)), (fc:(freq:159);dl:(len: 9)),
\r
429 (fc:(freq:415);dl:(len: 9)), (fc:(freq: 95);dl:(len: 9)), (fc:(freq:351);dl:(len: 9)),
\r
430 (fc:(freq:223);dl:(len: 9)), (fc:(freq:479);dl:(len: 9)), (fc:(freq: 63);dl:(len: 9)),
\r
431 (fc:(freq:319);dl:(len: 9)), (fc:(freq:191);dl:(len: 9)), (fc:(freq:447);dl:(len: 9)),
\r
432 (fc:(freq:127);dl:(len: 9)), (fc:(freq:383);dl:(len: 9)), (fc:(freq:255);dl:(len: 9)),
\r
433 (fc:(freq:511);dl:(len: 9)), (fc:(freq: 0);dl:(len: 7)), (fc:(freq: 64);dl:(len: 7)),
\r
434 (fc:(freq: 32);dl:(len: 7)), (fc:(freq: 96);dl:(len: 7)), (fc:(freq: 16);dl:(len: 7)),
\r
435 (fc:(freq: 80);dl:(len: 7)), (fc:(freq: 48);dl:(len: 7)), (fc:(freq:112);dl:(len: 7)),
\r
436 (fc:(freq: 8);dl:(len: 7)), (fc:(freq: 72);dl:(len: 7)), (fc:(freq: 40);dl:(len: 7)),
\r
437 (fc:(freq:104);dl:(len: 7)), (fc:(freq: 24);dl:(len: 7)), (fc:(freq: 88);dl:(len: 7)),
\r
438 (fc:(freq: 56);dl:(len: 7)), (fc:(freq:120);dl:(len: 7)), (fc:(freq: 4);dl:(len: 7)),
\r
439 (fc:(freq: 68);dl:(len: 7)), (fc:(freq: 36);dl:(len: 7)), (fc:(freq:100);dl:(len: 7)),
\r
440 (fc:(freq: 20);dl:(len: 7)), (fc:(freq: 84);dl:(len: 7)), (fc:(freq: 52);dl:(len: 7)),
\r
441 (fc:(freq:116);dl:(len: 7)), (fc:(freq: 3);dl:(len: 8)), (fc:(freq:131);dl:(len: 8)),
\r
442 (fc:(freq: 67);dl:(len: 8)), (fc:(freq:195);dl:(len: 8)), (fc:(freq: 35);dl:(len: 8)),
\r
443 (fc:(freq:163);dl:(len: 8)), (fc:(freq: 99);dl:(len: 8)), (fc:(freq:227);dl:(len: 8))
\r
447 { The static distance tree. (Actually a trivial tree since all lens use
\r
449 static_dtree : array[0..D_CODES-1] of ct_data = (
\r
450 (fc:(freq: 0); dl:(len:5)), (fc:(freq:16); dl:(len:5)), (fc:(freq: 8); dl:(len:5)),
\r
451 (fc:(freq:24); dl:(len:5)), (fc:(freq: 4); dl:(len:5)), (fc:(freq:20); dl:(len:5)),
\r
452 (fc:(freq:12); dl:(len:5)), (fc:(freq:28); dl:(len:5)), (fc:(freq: 2); dl:(len:5)),
\r
453 (fc:(freq:18); dl:(len:5)), (fc:(freq:10); dl:(len:5)), (fc:(freq:26); dl:(len:5)),
\r
454 (fc:(freq: 6); dl:(len:5)), (fc:(freq:22); dl:(len:5)), (fc:(freq:14); dl:(len:5)),
\r
455 (fc:(freq:30); dl:(len:5)), (fc:(freq: 1); dl:(len:5)), (fc:(freq:17); dl:(len:5)),
\r
456 (fc:(freq: 9); dl:(len:5)), (fc:(freq:25); dl:(len:5)), (fc:(freq: 5); dl:(len:5)),
\r
457 (fc:(freq:21); dl:(len:5)), (fc:(freq:13); dl:(len:5)), (fc:(freq:29); dl:(len:5)),
\r
458 (fc:(freq: 3); dl:(len:5)), (fc:(freq:19); dl:(len:5)), (fc:(freq:11); dl:(len:5)),
\r
459 (fc:(freq:27); dl:(len:5)), (fc:(freq: 7); dl:(len:5)), (fc:(freq:23); dl:(len:5))
\r
462 { Distance codes. The first 256 values correspond to the distances
\r
463 3 .. 258, the last 256 values correspond to the top 8 bits of
\r
464 the 15 bit distances. }
\r
465 _dist_code : array[0..DIST_CODE_LEN-1] of uch = (
\r
466 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8,
\r
467 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10,
\r
468 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
\r
469 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
\r
470 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13,
\r
471 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
\r
472 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
\r
473 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
\r
474 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
\r
475 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15,
\r
476 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
\r
477 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
\r
478 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 0, 16, 17,
\r
479 18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22,
\r
480 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
\r
481 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
\r
482 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
\r
483 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27,
\r
484 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
\r
485 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
\r
486 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
\r
487 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
\r
488 28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
\r
489 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
\r
490 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
\r
491 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29
\r
494 { length code for each normalized match length (0 == MIN_MATCH) }
\r
495 _length_code : array[0..MAX_MATCH-MIN_MATCH+1-1] of uch = (
\r
496 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 12,
\r
497 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16,
\r
498 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19,
\r
499 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
\r
500 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22,
\r
501 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23,
\r
502 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
\r
503 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
\r
504 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
\r
505 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26,
\r
506 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
\r
507 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
\r
508 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28
\r
512 { First normalized length for each code (0 = MIN_MATCH) }
\r
513 base_length : array[0..LENGTH_CODES-1] of int = (
\r
514 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56,
\r
515 64, 80, 96, 112, 128, 160, 192, 224, 0
\r
519 { First normalized distance for each code (0 = distance of 1) }
\r
520 base_dist : array[0..D_CODES-1] of int = (
\r
521 0, 1, 2, 3, 4, 6, 8, 12, 16, 24,
\r
522 32, 48, 64, 96, 128, 192, 256, 384, 512, 768,
\r
523 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576
\r
527 { Output a byte on the stream.
\r
528 IN assertion: there is enough room in pending_buf.
\r
529 macro put_byte(s, c)
\r
531 s^.pending_buf^[s^.pending] := (c);
\r
537 MIN_LOOKAHEAD = (MAX_MATCH+MIN_MATCH+1);
\r
538 { Minimum amount of lookahead, except at the end of the input file.
\r
539 See deflate.c for comments about the MIN_MATCH+1. }
\r
541 {macro d_code(dist)
\r
542 if (dist) < 256 then
\r
543 := _dist_code[dist]
\r
545 := _dist_code[256+((dist) shr 7)]);
\r
546 Mapping from a distance to a distance code. dist is the distance - 1 and
\r
547 must not have side effects. _dist_code[256] and _dist_code[257] are never
\r
550 {$ifndef ORG_DEBUG}
\r
551 { Inline versions of _tr_tally for speed: }
\r
553 #if defined(GEN_TREES_H) || !defined(STDC)
\r
554 extern uch _length_code[];
\r
555 extern uch _dist_code[];
\r
557 extern const uch _length_code[];
\r
558 extern const uch _dist_code[];
\r
561 macro _tr_tally_lit(s, c, flush)
\r
566 s^.d_buf[s^.last_lit] := 0;
\r
567 s^.l_buf[s^.last_lit] := cc;
\r
569 Inc(s^.dyn_ltree[cc].fc.Freq);
\r
570 flush := (s^.last_lit = s^.lit_bufsize-1);
\r
573 macro _tr_tally_dist(s, distance, length, flush) \
\r
579 dist := (distance);
\r
580 s^.d_buf[s^.last_lit] := dist;
\r
581 s^.l_buf[s^.last_lit] = len;
\r
584 Inc(s^.dyn_ltree[_length_code[len]+LITERALS+1].fc.Freq);
\r
585 Inc(s^.dyn_dtree[d_code(dist)].Freq);
\r
586 flush := (s^.last_lit = s^.lit_bufsize-1);
\r
591 { ===========================================================================
\r
596 { Bit length codes must not exceed MAX_BL_BITS bits }
\r
600 { end of block literal code }
\r
604 { repeat previous bit length 3-6 times (2 bits of repeat count) }
\r
608 { repeat a zero length 3-10 times (3 bits of repeat count) }
\r
612 { repeat a zero length 11-138 times (7 bits of repeat count) }
\r
616 extra_lbits : array[0..LENGTH_CODES-1] of int
\r
617 { extra bits for each length code }
\r
618 = (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);
\r
622 extra_dbits : array[0..D_CODES-1] of int
\r
623 { extra bits for each distance code }
\r
624 = (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);
\r
628 extra_blbits : array[0..BL_CODES-1] of int { extra bits for each bit length code }
\r
629 = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7);
\r
633 bl_order : array[0..BL_CODES-1] of uch
\r
634 = (16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15);
\r
635 { The lengths of the bit length codes are sent in order of decreasing
\r
636 probability, to avoid transmitting the lengths for unused bit length codes.
\r
640 Buf_size = (8 * 2*sizeof(char));
\r
641 { Number of bits used within bi_buf. (bi_buf might be implemented on
\r
642 more than 16 bits on some systems.) }
\r
644 { ===========================================================================
\r
645 Local data. These are initialized only once. }
\r
648 {$ifdef GEN_TREES_H)}
\r
649 { non ANSI compilers may not accept trees.h }
\r
652 DIST_CODE_LEN = 512; { see definition of array dist_code below }
\r
656 static_ltree : array[0..L_CODES+2-1] of ct_data;
\r
657 { The static literal tree. Since the bit lengths are imposed, there is no
\r
658 need for the L_CODES extra codes used during heap construction. However
\r
659 The codes 286 and 287 are needed to build a canonical tree (see _tr_init
\r
663 static_dtree : array[0..D_CODES-1] of ct_data;
\r
664 { The static distance tree. (Actually a trivial tree since all codes use
\r
667 _dist_code : array[0..DIST_CODE_LEN-1] of uch;
\r
668 { Distance codes. The first 256 values correspond to the distances
\r
669 3 .. 258, the last 256 values correspond to the top 8 bits of
\r
670 the 15 bit distances. }
\r
672 _length_code : array[0..MAX_MATCH-MIN_MATCH+1-1] of uch;
\r
673 { length code for each normalized match length (0 == MIN_MATCH) }
\r
676 base_length : array[0..LENGTH_CODES-1] of int;
\r
677 { First normalized length for each code (0 = MIN_MATCH) }
\r
680 base_dist : array[0..D_CODES-1] of int;
\r
681 { First normalized distance for each code (0 = distance of 1) }
\r
683 {$endif} { GEN_TREES_H }
\r
687 static_l_desc : static_tree_desc =
\r
688 (static_tree: {tree_ptr}(@(static_ltree)); { pointer to array of ct_data }
\r
689 extra_bits: {pzIntfArray}(@(extra_lbits)); { pointer to array of int }
\r
690 extra_base: LITERALS+1;
\r
692 max_length: MAX_BITS);
\r
696 static_d_desc : static_tree_desc =
\r
697 (static_tree: {tree_ptr}(@(static_dtree));
\r
698 extra_bits: {pzIntfArray}(@(extra_dbits));
\r
701 max_length: MAX_BITS);
\r
705 static_bl_desc : static_tree_desc =
\r
706 (static_tree: {tree_ptr}(NIL);
\r
707 extra_bits: {pzIntfArray}@(extra_blbits);
\r
710 max_length: MAX_BL_BITS);
\r
712 (* ===========================================================================
\r
713 Local (static) routines in this file. }
\r
715 procedure tr_static_init;
\r
716 procedure init_block(var deflate_state);
\r
717 procedure pqdownheap(var s : deflate_state;
\r
718 var tree : ct_data;
\r
720 procedure gen_bitlen(var s : deflate_state;
\r
721 var desc : tree_desc);
\r
722 procedure gen_codes(var tree : ct_data;
\r
725 procedure build_tree(var s : deflate_state;
\r
726 var desc : tree_desc);
\r
727 procedure scan_tree(var s : deflate_state;
\r
728 var tree : ct_data;
\r
730 procedure send_tree(var s : deflate_state;
\r
731 var tree : ct_data;
\r
733 function build_bl_tree(var deflate_state) : int;
\r
734 procedure send_all_trees(var deflate_state;
\r
738 procedure compress_block(var s : deflate_state;
\r
739 var ltree : ct_data;
\r
740 var dtree : ct_data);
\r
741 procedure set_data_type(var s : deflate_state);
\r
742 function bi_reverse(value : unsigned;
\r
743 length : int) : unsigned;
\r
744 procedure bi_windup(var deflate_state);
\r
745 procedure bi_flush(var deflate_state);
\r
746 procedure copy_block(var deflate_state;
\r
752 {$ifdef GEN_TREES_H}
\r
754 procedure gen_trees_header;
\r
758 { ===========================================================================
\r
759 Output a short LSB first on the stream.
\r
760 IN assertion: there is enough room in pendingBuf. }
\r
762 macro put_short(s, w)
\r
764 {put_byte(s, (uch)((w) & 0xff));}
\r
765 s.pending_buf^[s.pending] := uch((w) and $ff);
\r
768 {put_byte(s, (uch)((ush)(w) >> 8));}
\r
769 s.pending_buf^[s.pending] := uch(ush(w) shr 8);;
\r
774 { ===========================================================================
\r
775 Send a value on a given number of bits.
\r
776 IN assertion: length <= 16 and value fits in length bits. }
\r
781 procedure send_bits(var s : deflate_state;
\r
782 value : int; { value to send }
\r
783 length : int); { number of bits }
\r
786 Tracevv(' l '+IntToStr(length)+ ' v '+IntToStr(value));
\r
787 Assert((length > 0) and (length <= 15), 'invalid length');
\r
788 Inc(s.bits_sent, ulg(length));
\r
791 { If not enough room in bi_buf, use (valid) bits from bi_buf and
\r
792 (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
\r
793 unused bits in value. }
\r
794 {$IFOPT Q+} {$Q-} {$DEFINE NoOverflowCheck} {$ENDIF}
\r
795 {$IFOPT R+} {$R-} {$DEFINE NoRangeCheck} {$ENDIF}
\r
796 if (s.bi_valid > int(Buf_size) - length) then
\r
798 s.bi_buf := s.bi_buf or int(value shl s.bi_valid);
\r
799 {put_short(s, s.bi_buf);}
\r
800 s.pending_buf^[s.pending] := uch(s.bi_buf and $ff);
\r
802 s.pending_buf^[s.pending] := uch(ush(s.bi_buf) shr 8);;
\r
805 s.bi_buf := ush(value) shr (Buf_size - s.bi_valid);
\r
806 Inc(s.bi_valid, length - Buf_size);
\r
810 s.bi_buf := s.bi_buf or int(value shl s.bi_valid);
\r
811 Inc(s.bi_valid, length);
\r
813 {$IFDEF NoOverflowCheck} {$Q+} {$UNDEF NoOverflowCheck} {$ENDIF}
\r
814 {$IFDEF NoRangeCheck} {$Q+} {$UNDEF NoRangeCheck} {$ENDIF}
\r
820 macro send_code(s, c, tree)
\r
822 send_bits(s, tree[c].Code, tree[c].Len);
\r
823 { Send a code of the given tree. c and tree must not have side effects }
\r
826 macro send_bits(s, value, length) \
\r
827 begin int len := length;\
\r
828 if (s^.bi_valid > (int)Buf_size - len) begin\
\r
830 s^.bi_buf |= (val << s^.bi_valid);\
\r
831 {put_short(s, s.bi_buf);}
\r
832 s.pending_buf^[s.pending] := uch(s.bi_buf and $ff);
\r
834 s.pending_buf^[s.pending] := uch(ush(s.bi_buf) shr 8);;
\r
837 s^.bi_buf := (ush)val >> (Buf_size - s^.bi_valid);\
\r
838 s^.bi_valid += len - Buf_size;\
\r
840 s^.bi_buf |= (value) << s^.bi_valid;\
\r
841 s^.bi_valid += len;\
\r
846 { ===========================================================================
\r
847 Reverse the first len bits of a code, using straightforward code (a faster
\r
848 method would use a table)
\r
849 IN assertion: 1 <= len <= 15 }
\r
852 function bi_reverse(code : unsigned; { the value to invert }
\r
853 len : int) : unsigned; { its bit length }
\r
856 res : unsigned; {register}
\r
860 res := res or (code and 1);
\r
861 code := code shr 1;
\r
865 bi_reverse := res shr 1;
\r
868 { ===========================================================================
\r
869 Generate the codes for a given tree and bit counts (which need not be
\r
871 IN assertion: the array bl_count contains the bit length statistics for
\r
872 the given tree and the field len is set for all tree elements.
\r
873 OUT assertion: the field code is set for all tree elements of non
\r
874 zero code length. }
\r
877 procedure gen_codes(tree : tree_ptr; { the tree to decorate }
\r
878 max_code : int; { largest code with non zero frequency }
\r
879 var bl_count : array of ushf); { number of codes at each bit length }
\r
882 next_code : array[0..MAX_BITS+1-1] of ush; { next code value for each bit length }
\r
883 code : ush; { running code value }
\r
884 bits : int; { bit index }
\r
885 n : int; { code index }
\r
891 { The distribution counts are first used to generate the code values
\r
892 without bit reversal. }
\r
894 for bits := 1 to MAX_BITS do
\r
896 code := ((code + bl_count[bits-1]) shl 1);
\r
897 next_code[bits] := code;
\r
899 { Check that the bit counts in bl_count are consistent. The last code
\r
900 must be all ones. }
\r
903 Assert (code + bl_count[MAX_BITS]-1 = (1 shl MAX_BITS)-1,
\r
904 'inconsistent bit counts');
\r
905 Tracev(#13'gen_codes: max_code '+IntToStr(max_code));
\r
908 for n := 0 to max_code do
\r
910 len := tree^[n].dl.Len;
\r
913 { Now reverse the bits }
\r
914 tree^[n].fc.Code := bi_reverse(next_code[len], len);
\r
915 Inc(next_code[len]);
\r
917 if (n>31) and (n<128) then
\r
918 Tracecv(tree <> tree_ptr(@static_ltree),
\r
919 (^M'n #'+IntToStr(n)+' '+char(n)+' l '+IntToStr(len)+' c '+
\r
920 IntToStr(tree^[n].fc.Code)+' ('+IntToStr(next_code[len]-1)+')'))
\r
922 Tracecv(tree <> tree_ptr(@static_ltree),
\r
923 (^M'n #'+IntToStr(n)+' l '+IntToStr(len)+' c '+
\r
924 IntToStr(tree^[n].fc.Code)+' ('+IntToStr(next_code[len]-1)+')'));
\r
929 { ===========================================================================
\r
930 Genererate the file trees.h describing the static trees. }
\r
931 {$ifdef GEN_TREES_H}
\r
933 macro SEPARATOR(i, last, width)
\r
934 if (i) = (last) then
\r
937 if (i) mod (width) = (width)-1 then
\r
942 procedure gen_trees_header;
\r
944 header : system.text;
\r
947 system.assign(header, 'trees.inc');
\r
951 Assert (IOresult <> 0, 'Can''t open trees.h');
\r
953 '{ header created automatically with -DGEN_TREES_H }'^M);
\r
955 WriteLn(header, 'local const ct_data static_ltree[L_CODES+2] := (');
\r
956 for i := 0 to L_CODES+2-1 do
\r
958 WriteLn(header, '((%3u),(%3u))%s', static_ltree[i].Code,
\r
959 static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5));
\r
962 WriteLn(header, 'local const ct_data static_dtree[D_CODES] := (');
\r
963 for i := 0 to D_CODES-1 do
\r
965 WriteLn(header, '((%2u),(%2u))%s', static_dtree[i].Code,
\r
966 static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5));
\r
969 WriteLn(header, 'const uch _dist_code[DIST_CODE_LEN] := (');
\r
970 for i := 0 to DIST_CODE_LEN-1 do
\r
972 WriteLn(header, '%2u%s', _dist_code[i],
\r
973 SEPARATOR(i, DIST_CODE_LEN-1, 20));
\r
976 WriteLn(header, 'const uch _length_code[MAX_MATCH-MIN_MATCH+1]= (');
\r
977 for i := 0 to MAX_MATCH-MIN_MATCH+1-1 do
\r
979 WriteLn(header, '%2u%s', _length_code[i],
\r
980 SEPARATOR(i, MAX_MATCH-MIN_MATCH, 20));
\r
983 WriteLn(header, 'local const int base_length[LENGTH_CODES] := (');
\r
984 for i := 0 to LENGTH_CODES-1 do
\r
986 WriteLn(header, '%1u%s', base_length[i],
\r
987 SEPARATOR(i, LENGTH_CODES-1, 20));
\r
990 WriteLn(header, 'local const int base_dist[D_CODES] := (');
\r
991 for i := 0 to D_CODES-1 do
\r
993 WriteLn(header, '%5u%s', base_dist[i],
\r
994 SEPARATOR(i, D_CODES-1, 10));
\r
999 {$endif} { GEN_TREES_H }
\r
1002 { ===========================================================================
\r
1003 Initialize the various 'constant' tables. }
\r
1006 procedure tr_static_init;
\r
1008 {$ifdef GEN_TREES_H}
\r
1010 static_init_done : boolean = FALSE;
\r
1012 n : int; { iterates over tree elements }
\r
1013 bits : int; { bit counter }
\r
1014 length : int; { length value }
\r
1015 code : int; { code value }
\r
1016 dist : int; { distance index }
\r
1017 bl_count : array[0..MAX_BITS+1-1] of ush;
\r
1018 { number of codes at each bit length for an optimal tree }
\r
1020 if (static_init_done) then
\r
1023 { Initialize the mapping length (0..255) -> length code (0..28) }
\r
1025 for code := 0 to LENGTH_CODES-1-1 do
\r
1027 base_length[code] := length;
\r
1028 for n := 0 to (1 shl extra_lbits[code])-1 do
\r
1030 _length_code[length] := uch(code);
\r
1034 Assert (length = 256, 'tr_static_init: length <> 256');
\r
1035 { Note that the length 255 (match length 258) can be represented
\r
1036 in two different ways: code 284 + 5 bits or code 285, so we
\r
1037 overwrite length_code[255] to use the best encoding: }
\r
1039 _length_code[length-1] := uch(code);
\r
1041 { Initialize the mapping dist (0..32K) -> dist code (0..29) }
\r
1043 for code := 0 to 16-1 do
\r
1045 base_dist[code] := dist;
\r
1046 for n := 0 to (1 shl extra_dbits[code])-1 do
\r
1048 _dist_code[dist] := uch(code);
\r
1052 Assert (dist = 256, 'tr_static_init: dist <> 256');
\r
1053 dist := dist shr 7; { from now on, all distances are divided by 128 }
\r
1054 for code := 16 to D_CODES-1 do
\r
1056 base_dist[code] := dist shl 7;
\r
1057 for n := 0 to (1 shl (extra_dbits[code]-7))-1 do
\r
1059 _dist_code[256 + dist] := uch(code);
\r
1063 Assert (dist = 256, 'tr_static_init: 256+dist <> 512');
\r
1065 { Construct the codes of the static literal tree }
\r
1066 for bits := 0 to MAX_BITS do
\r
1067 bl_count[bits] := 0;
\r
1069 while (n <= 143) do
\r
1071 static_ltree[n].dl.Len := 8;
\r
1075 while (n <= 255) do
\r
1077 static_ltree[n].dl.Len := 9;
\r
1081 while (n <= 279) do
\r
1083 static_ltree[n].dl.Len := 7;
\r
1087 while (n <= 287) do
\r
1089 static_ltree[n].dl.Len := 8;
\r
1094 { Codes 286 and 287 do not exist, but we must include them in the
\r
1095 tree construction to get a canonical Huffman tree (longest code
\r
1098 gen_codes(tree_ptr(@static_ltree), L_CODES+1, bl_count);
\r
1100 { The static distance tree is trivial: }
\r
1101 for n := 0 to D_CODES-1 do
\r
1103 static_dtree[n].dl.Len := 5;
\r
1104 static_dtree[n].fc.Code := bi_reverse(unsigned(n), 5);
\r
1106 static_init_done := TRUE;
\r
1108 gen_trees_header; { save to include file }
\r
1111 {$endif} { GEN_TREES_H) }
\r
1114 { ===========================================================================
\r
1115 Initialize a new block. }
\r
1118 procedure init_block(var s : deflate_state);
\r
1120 n : int; { iterates over tree elements }
\r
1122 { Initialize the trees. }
\r
1123 for n := 0 to L_CODES-1 do
\r
1124 s.dyn_ltree[n].fc.Freq := 0;
\r
1125 for n := 0 to D_CODES-1 do
\r
1126 s.dyn_dtree[n].fc.Freq := 0;
\r
1127 for n := 0 to BL_CODES-1 do
\r
1128 s.bl_tree[n].fc.Freq := 0;
\r
1130 s.dyn_ltree[END_BLOCK].fc.Freq := 1;
\r
1131 s.static_len := Long(0);
\r
1132 s.opt_len := Long(0);
\r
1139 { Index within the heap array of least frequent node in the Huffman tree }
\r
1141 { ===========================================================================
\r
1142 Initialize the tree data structures for a new zlib stream. }
\r
1143 procedure _tr_init(var s : deflate_state);
\r
1147 s.compressed_len := Long(0);
\r
1149 s.l_desc.dyn_tree := tree_ptr(@s.dyn_ltree);
\r
1150 s.l_desc.stat_desc := @static_l_desc;
\r
1152 s.d_desc.dyn_tree := tree_ptr(@s.dyn_dtree);
\r
1153 s.d_desc.stat_desc := @static_d_desc;
\r
1155 s.bl_desc.dyn_tree := tree_ptr(@s.bl_tree);
\r
1156 s.bl_desc.stat_desc := @static_bl_desc;
\r
1160 s.last_eob_len := 8; { enough lookahead for inflate }
\r
1162 s.bits_sent := Long(0);
\r
1165 { Initialize the first block of the first file: }
\r
1169 { ===========================================================================
\r
1170 Remove the smallest element from the heap and recreate the heap with
\r
1171 one less element. Updates heap and heap_len.
\r
1173 macro pqremove(s, tree, top)
\r
1175 top := s.heap[SMALLEST];
\r
1176 s.heap[SMALLEST] := s.heap[s.heap_len];
\r
1178 pqdownheap(s, tree, SMALLEST);
\r
1182 { ===========================================================================
\r
1183 Compares to subtrees, using the tree depth as tie breaker when
\r
1184 the subtrees have equal frequency. This minimizes the worst case length.
\r
1186 macro smaller(tree, n, m, depth)
\r
1187 ( (tree[n].Freq < tree[m].Freq) or
\r
1188 ((tree[n].Freq = tree[m].Freq) and (depth[n] <= depth[m])) )
\r
1191 { ===========================================================================
\r
1192 Restore the heap property by moving down the tree starting at node k,
\r
1193 exchanging a node with the smallest of its two sons if necessary, stopping
\r
1194 when the heap property is re-established (each father smaller than its
\r
1198 procedure pqdownheap(var s : deflate_state;
\r
1199 var tree : tree_type; { the tree to restore }
\r
1200 k : int); { node to move down }
\r
1206 j := k shl 1; { left son of k }
\r
1207 while (j <= s.heap_len) do
\r
1209 { Set j to the smallest of the two sons: }
\r
1210 if (j < s.heap_len) and
\r
1211 {smaller(tree, s.heap[j+1], s.heap[j], s.depth)}
\r
1212 ( (tree[s.heap[j+1]].fc.Freq < tree[s.heap[j]].fc.Freq) or
\r
1213 ((tree[s.heap[j+1]].fc.Freq = tree[s.heap[j]].fc.Freq) and
\r
1214 (s.depth[s.heap[j+1]] <= s.depth[s.heap[j]])) ) then
\r
1218 { Exit if v is smaller than both sons }
\r
1219 if {(smaller(tree, v, s.heap[j], s.depth))}
\r
1220 ( (tree[v].fc.Freq < tree[s.heap[j]].fc.Freq) or
\r
1221 ((tree[v].fc.Freq = tree[s.heap[j]].fc.Freq) and
\r
1222 (s.depth[v] <= s.depth[s.heap[j]])) ) then
\r
1224 { Exchange v with the smallest son }
\r
1225 s.heap[k] := s.heap[j];
\r
1228 { And continue down the tree, setting j to the left son of k }
\r
1234 { ===========================================================================
\r
1235 Compute the optimal bit lengths for a tree and update the total bit length
\r
1236 for the current block.
\r
1237 IN assertion: the fields freq and dad are set, heap[heap_max] and
\r
1238 above are the tree nodes sorted by increasing frequency.
\r
1239 OUT assertions: the field len is set to the optimal bit length, the
\r
1240 array bl_count contains the frequencies for each bit length.
\r
1241 The length opt_len is updated; static_len is also updated if stree is
\r
1245 procedure gen_bitlen(var s : deflate_state;
\r
1246 var desc : tree_desc); { the tree descriptor }
\r
1250 stree : tree_ptr; {const}
\r
1251 extra : pzIntfArray; {const}
\r
1254 h : int; { heap index }
\r
1255 n, m : int; { iterate over the tree elements }
\r
1256 bits : int; { bit length }
\r
1257 xbits : int; { extra bits }
\r
1258 f : ush; { frequency }
\r
1259 overflow : int; { number of elements with bit length too large }
\r
1261 tree := desc.dyn_tree;
\r
1262 max_code := desc.max_code;
\r
1263 stree := desc.stat_desc^.static_tree;
\r
1264 extra := desc.stat_desc^.extra_bits;
\r
1265 base := desc.stat_desc^.extra_base;
\r
1266 max_length := desc.stat_desc^.max_length;
\r
1269 for bits := 0 to MAX_BITS do
\r
1270 s.bl_count[bits] := 0;
\r
1272 { In a first pass, compute the optimal bit lengths (which may
\r
1273 overflow in the case of the bit length tree). }
\r
1275 tree^[s.heap[s.heap_max]].dl.Len := 0; { root of the heap }
\r
1277 for h := s.heap_max+1 to HEAP_SIZE-1 do
\r
1280 bits := tree^[tree^[n].dl.Dad].dl.Len + 1;
\r
1281 if (bits > max_length) then
\r
1283 bits := max_length;
\r
1286 tree^[n].dl.Len := ush(bits);
\r
1287 { We overwrite tree[n].dl.Dad which is no longer needed }
\r
1289 if (n > max_code) then
\r
1290 continue; { not a leaf node }
\r
1292 Inc(s.bl_count[bits]);
\r
1294 if (n >= base) then
\r
1295 xbits := extra^[n-base];
\r
1296 f := tree^[n].fc.Freq;
\r
1297 Inc(s.opt_len, ulg(f) * (bits + xbits));
\r
1298 if (stree <> NIL) then
\r
1299 Inc(s.static_len, ulg(f) * (stree^[n].dl.Len + xbits));
\r
1301 if (overflow = 0) then
\r
1304 Tracev(^M'bit length overflow');
\r
1306 { This happens for example on obj2 and pic of the Calgary corpus }
\r
1308 { Find the first bit length which could increase: }
\r
1310 bits := max_length-1;
\r
1311 while (s.bl_count[bits] = 0) do
\r
1313 Dec(s.bl_count[bits]); { move one leaf down the tree }
\r
1314 Inc(s.bl_count[bits+1], 2); { move one overflow item as its brother }
\r
1315 Dec(s.bl_count[max_length]);
\r
1316 { The brother of the overflow item also moves one step up,
\r
1317 but this does not affect bl_count[max_length] }
\r
1320 until (overflow <= 0);
\r
1322 { Now recompute all bit lengths, scanning in increasing frequency.
\r
1323 h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
\r
1324 lengths instead of fixing only the wrong ones. This idea is taken
\r
1325 from 'ar' written by Haruhiko Okumura.) }
\r
1326 h := HEAP_SIZE; { Delphi3: compiler warning w/o this }
\r
1327 for bits := max_length downto 1 do
\r
1329 n := s.bl_count[bits];
\r
1334 if (m > max_code) then
\r
1336 if (tree^[m].dl.Len <> unsigned(bits)) then
\r
1339 Trace('code '+IntToStr(m)+' bits '+IntToStr(tree^[m].dl.Len)
\r
1340 +'.'+IntToStr(bits));
\r
1342 Inc(s.opt_len, (long(bits) - long(tree^[m].dl.Len))
\r
1343 * long(tree^[m].fc.Freq) );
\r
1344 tree^[m].dl.Len := ush(bits);
\r
1351 { ===========================================================================
\r
1352 Construct one Huffman tree and assigns the code bit strings and lengths.
\r
1353 Update the total bit length for the current block.
\r
1354 IN assertion: the field freq is set for all tree elements.
\r
1355 OUT assertions: the fields len and code are set to the optimal bit length
\r
1356 and corresponding code. The length opt_len is updated; static_len is
\r
1357 also updated if stree is not null. The field max_code is set. }
\r
1360 procedure build_tree(var s : deflate_state;
\r
1361 var desc : tree_desc); { the tree descriptor }
\r
1365 stree : tree_ptr; {const}
\r
1367 n, m : int; { iterate over heap elements }
\r
1368 max_code : int; { largest code with non zero frequency }
\r
1369 node : int; { new node being created }
\r
1371 tree := desc.dyn_tree;
\r
1372 stree := desc.stat_desc^.static_tree;
\r
1373 elems := desc.stat_desc^.elems;
\r
1376 { Construct the initial heap, with least frequent element in
\r
1377 heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
\r
1378 heap[0] is not used. }
\r
1380 s.heap_max := HEAP_SIZE;
\r
1382 for n := 0 to elems-1 do
\r
1384 if (tree^[n].fc.Freq <> 0) then
\r
1388 s.heap[s.heap_len] := n;
\r
1393 tree^[n].dl.Len := 0;
\r
1397 { The pkzip format requires that at least one distance code exists,
\r
1398 and that at least one bit should be sent even if there is only one
\r
1399 possible code. So to avoid special checks later on we force at least
\r
1400 two codes of non zero frequency. }
\r
1402 while (s.heap_len < 2) do
\r
1405 if (max_code < 2) then
\r
1408 s.heap[s.heap_len] := max_code;
\r
1413 s.heap[s.heap_len] := 0;
\r
1416 tree^[node].fc.Freq := 1;
\r
1417 s.depth[node] := 0;
\r
1419 if (stree <> NIL) then
\r
1420 Dec(s.static_len, stree^[node].dl.Len);
\r
1421 { node is 0 or 1 so it does not have extra bits }
\r
1423 desc.max_code := max_code;
\r
1425 { The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
\r
1426 establish sub-heaps of increasing lengths: }
\r
1428 for n := s.heap_len div 2 downto 1 do
\r
1429 pqdownheap(s, tree^, n);
\r
1431 { Construct the Huffman tree by repeatedly combining the least two
\r
1434 node := elems; { next internal node of the tree }
\r
1436 {pqremove(s, tree, n);} { n := node of least frequency }
\r
1437 n := s.heap[SMALLEST];
\r
1438 s.heap[SMALLEST] := s.heap[s.heap_len];
\r
1440 pqdownheap(s, tree^, SMALLEST);
\r
1442 m := s.heap[SMALLEST]; { m := node of next least frequency }
\r
1445 s.heap[s.heap_max] := n; { keep the nodes sorted by frequency }
\r
1447 s.heap[s.heap_max] := m;
\r
1449 { Create a new node father of n and m }
\r
1450 tree^[node].fc.Freq := tree^[n].fc.Freq + tree^[m].fc.Freq;
\r
1452 if (s.depth[n] >= s.depth[m]) then
\r
1453 s.depth[node] := uch (s.depth[n] + 1)
\r
1455 s.depth[node] := uch (s.depth[m] + 1);
\r
1457 tree^[m].dl.Dad := ush(node);
\r
1458 tree^[n].dl.Dad := ush(node);
\r
1459 {$ifdef DUMP_BL_TREE}
\r
1460 if (tree = tree_ptr(@s.bl_tree)) then
\r
1462 WriteLn(#13'node ',node,'(',tree^[node].fc.Freq,') sons ',n,
\r
1463 '(',tree^[n].fc.Freq,') ', m, '(',tree^[m].fc.Freq,')');
\r
1466 { and insert the new node in the heap }
\r
1467 s.heap[SMALLEST] := node;
\r
1469 pqdownheap(s, tree^, SMALLEST);
\r
1471 until (s.heap_len < 2);
\r
1474 s.heap[s.heap_max] := s.heap[SMALLEST];
\r
1476 { At this point, the fields freq and dad are set. We can now
\r
1477 generate the bit lengths. }
\r
1479 gen_bitlen(s, desc);
\r
1481 { The field len is now set, we can generate the bit codes }
\r
1482 gen_codes (tree, max_code, s.bl_count);
\r
1485 { ===========================================================================
\r
1486 Scan a literal or distance tree to determine the frequencies of the codes
\r
1487 in the bit length tree. }
\r
1490 procedure scan_tree(var s : deflate_state;
\r
1491 var tree : array of ct_data; { the tree to be scanned }
\r
1492 max_code : int); { and its largest code of non zero frequency }
\r
1494 n : int; { iterates over all tree elements }
\r
1495 prevlen : int; { last emitted length }
\r
1496 curlen : int; { length of current code }
\r
1497 nextlen : int; { length of next code }
\r
1498 count : int; { repeat count of the current code }
\r
1499 max_count : int; { max repeat count }
\r
1500 min_count : int; { min repeat count }
\r
1503 nextlen := tree[0].dl.Len;
\r
1508 if (nextlen = 0) then
\r
1513 tree[max_code+1].dl.Len := ush($ffff); { guard }
\r
1515 for n := 0 to max_code do
\r
1517 curlen := nextlen;
\r
1518 nextlen := tree[n+1].dl.Len;
\r
1520 if (count < max_count) and (curlen = nextlen) then
\r
1523 if (count < min_count) then
\r
1524 Inc(s.bl_tree[curlen].fc.Freq, count)
\r
1526 if (curlen <> 0) then
\r
1528 if (curlen <> prevlen) then
\r
1529 Inc(s.bl_tree[curlen].fc.Freq);
\r
1530 Inc(s.bl_tree[REP_3_6].fc.Freq);
\r
1533 if (count <= 10) then
\r
1534 Inc(s.bl_tree[REPZ_3_10].fc.Freq)
\r
1536 Inc(s.bl_tree[REPZ_11_138].fc.Freq);
\r
1539 prevlen := curlen;
\r
1540 if (nextlen = 0) then
\r
1546 if (curlen = nextlen) then
\r
1559 { ===========================================================================
\r
1560 Send a literal or distance tree in compressed form, using the codes in
\r
1564 procedure send_tree(var s : deflate_state;
\r
1565 var tree : array of ct_data; { the tree to be scanned }
\r
1566 max_code : int); { and its largest code of non zero frequency }
\r
1569 n : int; { iterates over all tree elements }
\r
1570 prevlen : int; { last emitted length }
\r
1571 curlen : int; { length of current code }
\r
1572 nextlen : int; { length of next code }
\r
1573 count : int; { repeat count of the current code }
\r
1574 max_count : int; { max repeat count }
\r
1575 min_count : int; { min repeat count }
\r
1578 nextlen := tree[0].dl.Len;
\r
1583 { tree[max_code+1].dl.Len := -1; } { guard already set }
\r
1584 if (nextlen = 0) then
\r
1590 for n := 0 to max_code do
\r
1592 curlen := nextlen;
\r
1593 nextlen := tree[n+1].dl.Len;
\r
1595 if (count < max_count) and (curlen = nextlen) then
\r
1598 if (count < min_count) then
\r
1602 Tracevvv(#13'cd '+IntToStr(curlen));
\r
1604 send_bits(s, s.bl_tree[curlen].fc.Code, s.bl_tree[curlen].dl.Len);
\r
1606 until (count = 0);
\r
1609 if (curlen <> 0) then
\r
1611 if (curlen <> prevlen) then
\r
1614 Tracevvv(#13'cd '+IntToStr(curlen));
\r
1616 send_bits(s, s.bl_tree[curlen].fc.Code, s.bl_tree[curlen].dl.Len);
\r
1620 Assert((count >= 3) and (count <= 6), ' 3_6?');
\r
1623 Tracevvv(#13'cd '+IntToStr(REP_3_6));
\r
1625 send_bits(s, s.bl_tree[REP_3_6].fc.Code, s.bl_tree[REP_3_6].dl.Len);
\r
1626 send_bits(s, count-3, 2);
\r
1629 if (count <= 10) then
\r
1632 Tracevvv(#13'cd '+IntToStr(REPZ_3_10));
\r
1634 send_bits(s, s.bl_tree[REPZ_3_10].fc.Code, s.bl_tree[REPZ_3_10].dl.Len);
\r
1635 send_bits(s, count-3, 3);
\r
1640 Tracevvv(#13'cd '+IntToStr(REPZ_11_138));
\r
1642 send_bits(s, s.bl_tree[REPZ_11_138].fc.Code, s.bl_tree[REPZ_11_138].dl.Len);
\r
1643 send_bits(s, count-11, 7);
\r
1646 prevlen := curlen;
\r
1647 if (nextlen = 0) then
\r
1653 if (curlen = nextlen) then
\r
1666 { ===========================================================================
\r
1667 Construct the Huffman tree for the bit lengths and return the index in
\r
1668 bl_order of the last bit length code to send. }
\r
1671 function build_bl_tree(var s : deflate_state) : int;
\r
1673 max_blindex : int; { index of last bit length code of non zero freq }
\r
1675 { Determine the bit length frequencies for literal and distance trees }
\r
1676 scan_tree(s, s.dyn_ltree, s.l_desc.max_code);
\r
1677 scan_tree(s, s.dyn_dtree, s.d_desc.max_code);
\r
1679 { Build the bit length tree: }
\r
1680 build_tree(s, s.bl_desc);
\r
1681 { opt_len now includes the length of the tree representations, except
\r
1682 the lengths of the bit lengths codes and the 5+5+4 bits for the counts. }
\r
1684 { Determine the number of bit length codes to send. The pkzip format
\r
1685 requires that at least 4 bit length codes be sent. (appnote.txt says
\r
1686 3 but the actual value used is 4.) }
\r
1688 for max_blindex := BL_CODES-1 downto 3 do
\r
1690 if (s.bl_tree[bl_order[max_blindex]].dl.Len <> 0) then
\r
1693 { Update opt_len to include the bit length tree and counts }
\r
1694 Inc(s.opt_len, 3*(max_blindex+1) + 5+5+4);
\r
1696 Tracev(^M'dyn trees: dyn %ld, stat %ld {s.opt_len, s.static_len}');
\r
1699 build_bl_tree := max_blindex;
\r
1702 { ===========================================================================
\r
1703 Send the header for a block using dynamic Huffman trees: the counts, the
\r
1704 lengths of the bit length codes, the literal tree and the distance tree.
\r
1705 IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4. }
\r
1708 procedure send_all_trees(var s : deflate_state;
\r
1711 blcodes : int); { number of codes for each tree }
\r
1713 rank : int; { index in bl_order }
\r
1716 Assert ((lcodes >= 257) and (dcodes >= 1) and (blcodes >= 4),
\r
1717 'not enough codes');
\r
1718 Assert ((lcodes <= L_CODES) and (dcodes <= D_CODES)
\r
1719 and (blcodes <= BL_CODES), 'too many codes');
\r
1720 Tracev(^M'bl counts: ');
\r
1722 send_bits(s, lcodes-257, 5); { not +255 as stated in appnote.txt }
\r
1723 send_bits(s, dcodes-1, 5);
\r
1724 send_bits(s, blcodes-4, 4); { not -3 as stated in appnote.txt }
\r
1725 for rank := 0 to blcodes-1 do
\r
1728 Tracev(^M'bl code '+IntToStr(bl_order[rank]));
\r
1730 send_bits(s, s.bl_tree[bl_order[rank]].dl.Len, 3);
\r
1733 Tracev(^M'bl tree: sent '+IntToStr(s.bits_sent));
\r
1736 send_tree(s, s.dyn_ltree, lcodes-1); { literal tree }
\r
1738 Tracev(^M'lit tree: sent '+IntToStr(s.bits_sent));
\r
1741 send_tree(s, s.dyn_dtree, dcodes-1); { distance tree }
\r
1743 Tracev(^M'dist tree: sent '+IntToStr(s.bits_sent));
\r
1747 { ===========================================================================
\r
1748 Flush the bit buffer and align the output on a byte boundary }
\r
1751 procedure bi_windup(var s : deflate_state);
\r
1753 if (s.bi_valid > 8) then
\r
1755 {put_short(s, s.bi_buf);}
\r
1756 s.pending_buf^[s.pending] := uch(s.bi_buf and $ff);
\r
1758 s.pending_buf^[s.pending] := uch(ush(s.bi_buf) shr 8);;
\r
1762 if (s.bi_valid > 0) then
\r
1764 {put_byte(s, (Byte)s^.bi_buf);}
\r
1765 s.pending_buf^[s.pending] := Byte(s.bi_buf);
\r
1771 s.bits_sent := (s.bits_sent+7) and (not 7);
\r
1775 { ===========================================================================
\r
1776 Copy a stored block, storing first the length and its
\r
1777 one's complement if requested. }
\r
1780 procedure copy_block(var s : deflate_state;
\r
1781 buf : pcharf; { the input data }
\r
1782 len : unsigned; { its length }
\r
1783 header : boolean); { true if block header must be written }
\r
1785 bi_windup(s); { align on byte boundary }
\r
1786 s.last_eob_len := 8; { enough lookahead for inflate }
\r
1790 {put_short(s, (ush)len);}
\r
1791 s.pending_buf^[s.pending] := uch(ush(len) and $ff);
\r
1793 s.pending_buf^[s.pending] := uch(ush(len) shr 8);;
\r
1795 {put_short(s, (ush)~len);}
\r
1796 s.pending_buf^[s.pending] := uch(ush(not len) and $ff);
\r
1798 s.pending_buf^[s.pending] := uch(ush(not len) shr 8);;
\r
1802 Inc(s.bits_sent, 2*16);
\r
1806 Inc(s.bits_sent, ulg(len shl 3));
\r
1808 while (len <> 0) do
\r
1811 {put_byte(s, *buf++);}
\r
1812 s.pending_buf^[s.pending] := buf^;
\r
1819 { ===========================================================================
\r
1820 Send a stored block }
\r
1822 procedure _tr_stored_block(var s : deflate_state;
\r
1823 buf : pcharf; { input block }
\r
1824 stored_len : ulg; { length of input block }
\r
1825 eof : boolean); { true if this is the last block for a file }
\r
1828 send_bits(s, (STORED_BLOCK shl 1)+ord(eof), 3); { send block type }
\r
1829 s.compressed_len := (s.compressed_len + 3 + 7) and ulg(not Long(7));
\r
1830 Inc(s.compressed_len, (stored_len + 4) shl 3);
\r
1832 copy_block(s, buf, unsigned(stored_len), TRUE); { with header }
\r
1835 { ===========================================================================
\r
1836 Flush the bit buffer, keeping at most 7 bits in it. }
\r
1839 procedure bi_flush(var s : deflate_state);
\r
1841 if (s.bi_valid = 16) then
\r
1843 {put_short(s, s.bi_buf);}
\r
1844 s.pending_buf^[s.pending] := uch(s.bi_buf and $ff);
\r
1846 s.pending_buf^[s.pending] := uch(ush(s.bi_buf) shr 8);;
\r
1853 if (s.bi_valid >= 8) then
\r
1855 {put_byte(s, (Byte)s^.bi_buf);}
\r
1856 s.pending_buf^[s.pending] := Byte(s.bi_buf);
\r
1859 s.bi_buf := s.bi_buf shr 8;
\r
1860 Dec(s.bi_valid, 8);
\r
1865 { ===========================================================================
\r
1866 Send one empty static block to give enough lookahead for inflate.
\r
1867 This takes 10 bits, of which 7 may remain in the bit buffer.
\r
1868 The current inflate code requires 9 bits of lookahead. If the
\r
1869 last two codes for the previous block (real code plus EOB) were coded
\r
1870 on 5 bits or less, inflate may have only 5+3 bits of lookahead to decode
\r
1871 the last real code. In this case we send two empty static blocks instead
\r
1872 of one. (There are no problems if the previous block is stored or fixed.)
\r
1873 To simplify the code, we assume the worst case of last real code encoded
\r
1874 on one bit only. }
\r
1876 procedure _tr_align(var s : deflate_state);
\r
1878 send_bits(s, STATIC_TREES shl 1, 3);
\r
1880 Tracevvv(#13'cd '+IntToStr(END_BLOCK));
\r
1882 send_bits(s, static_ltree[END_BLOCK].fc.Code, static_ltree[END_BLOCK].dl.Len);
\r
1883 Inc(s.compressed_len, Long(10)); { 3 for block type, 7 for EOB }
\r
1885 { Of the 10 bits for the empty block, we have already sent
\r
1886 (10 - bi_valid) bits. The lookahead for the last real code (before
\r
1887 the EOB of the previous block) was thus at least one plus the length
\r
1888 of the EOB plus what we have just sent of the empty static block. }
\r
1889 if (1 + s.last_eob_len + 10 - s.bi_valid < 9) then
\r
1891 send_bits(s, STATIC_TREES shl 1, 3);
\r
1893 Tracevvv(#13'cd '+IntToStr(END_BLOCK));
\r
1895 send_bits(s, static_ltree[END_BLOCK].fc.Code, static_ltree[END_BLOCK].dl.Len);
\r
1896 Inc(s.compressed_len, Long(10));
\r
1899 s.last_eob_len := 7;
\r
1902 { ===========================================================================
\r
1903 Set the data type to ASCII or BINARY, using a crude approximation:
\r
1904 binary if more than 20% of the bytes are <= 6 or >= 128, ascii otherwise.
\r
1905 IN assertion: the fields freq of dyn_ltree are set and the total of all
\r
1906 frequencies does not exceed 64K (to fit in an int on 16 bit machines). }
\r
1909 procedure set_data_type(var s : deflate_state);
\r
1912 ascii_freq : unsigned;
\r
1913 bin_freq : unsigned;
\r
1921 Inc(bin_freq, s.dyn_ltree[n].fc.Freq);
\r
1924 while (n < 128) do
\r
1926 Inc(ascii_freq, s.dyn_ltree[n].fc.Freq);
\r
1929 while (n < LITERALS) do
\r
1931 Inc(bin_freq, s.dyn_ltree[n].fc.Freq);
\r
1934 if (bin_freq > (ascii_freq shr 2)) then
\r
1935 s.data_type := Byte(Z_BINARY)
\r
1937 s.data_type := Byte(Z_ASCII);
\r
1940 { ===========================================================================
\r
1941 Send the block data compressed using the given Huffman trees }
\r
1944 procedure compress_block(var s : deflate_state;
\r
1945 var ltree : array of ct_data; { literal tree }
\r
1946 var dtree : array of ct_data); { distance tree }
\r
1948 dist : unsigned; { distance of matched string }
\r
1949 lc : int; { match length or unmatched char (if dist == 0) }
\r
1950 lx : unsigned; { running index in l_buf }
\r
1951 code : unsigned; { the code to send }
\r
1952 extra : int; { number of extra bits to send }
\r
1955 if (s.last_lit <> 0) then
\r
1957 dist := s.d_buf^[lx];
\r
1958 lc := s.l_buf^[lx];
\r
1960 if (dist = 0) then
\r
1962 { send a literal byte }
\r
1964 Tracevvv(#13'cd '+IntToStr(lc));
\r
1965 Tracecv((lc > 31) and (lc < 128), ' '+char(lc)+' ');
\r
1967 send_bits(s, ltree[lc].fc.Code, ltree[lc].dl.Len);
\r
1971 { Here, lc is the match length - MIN_MATCH }
\r
1972 code := _length_code[lc];
\r
1973 { send the length code }
\r
1975 Tracevvv(#13'cd '+IntToStr(code+LITERALS+1));
\r
1977 send_bits(s, ltree[code+LITERALS+1].fc.Code, ltree[code+LITERALS+1].dl.Len);
\r
1978 extra := extra_lbits[code];
\r
1979 if (extra <> 0) then
\r
1981 Dec(lc, base_length[code]);
\r
1982 send_bits(s, lc, extra); { send the extra length bits }
\r
1984 Dec(dist); { dist is now the match distance - 1 }
\r
1985 {code := d_code(dist);}
\r
1986 if (dist < 256) then
\r
1987 code := _dist_code[dist]
\r
1989 code := _dist_code[256+(dist shr 7)];
\r
1992 Assert (code < D_CODES, 'bad d_code');
\r
1995 { send the distance code }
\r
1997 Tracevvv(#13'cd '+IntToStr(code));
\r
1999 send_bits(s, dtree[code].fc.Code, dtree[code].dl.Len);
\r
2000 extra := extra_dbits[code];
\r
2001 if (extra <> 0) then
\r
2003 Dec(dist, base_dist[code]);
\r
2004 send_bits(s, dist, extra); { send the extra distance bits }
\r
2006 end; { literal or match pair ? }
\r
2008 { Check that the overlay between pending_buf and d_buf+l_buf is ok: }
\r
2010 Assert(s.pending < s.lit_bufsize + 2*lx, 'pendingBuf overflow');
\r
2012 until (lx >= s.last_lit);
\r
2015 Tracevvv(#13'cd '+IntToStr(END_BLOCK));
\r
2017 send_bits(s, ltree[END_BLOCK].fc.Code, ltree[END_BLOCK].dl.Len);
\r
2018 s.last_eob_len := ltree[END_BLOCK].dl.Len;
\r
2022 { ===========================================================================
\r
2023 Determine the best encoding for the current block: dynamic trees, static
\r
2024 trees or store, and output the encoded block to the zip file. This function
\r
2025 returns the total compressed length for the file so far. }
\r
2027 function _tr_flush_block (var s : deflate_state;
\r
2028 buf : pcharf; { input block, or NULL if too old }
\r
2029 stored_len : ulg; { length of input block }
\r
2030 eof : boolean) : ulg; { true if this is the last block for a file }
\r
2032 opt_lenb, static_lenb : ulg; { opt_len and static_len in bytes }
\r
2033 max_blindex : int; { index of last bit length code of non zero freq }
\r
2037 { Build the Huffman trees unless a stored block is forced }
\r
2038 if (s.level > 0) then
\r
2040 { Check if the file is ascii or binary }
\r
2041 if (s.data_type = Z_UNKNOWN) then
\r
2044 { Construct the literal and distance trees }
\r
2045 build_tree(s, s.l_desc);
\r
2047 Tracev(^M'lit data: dyn %ld, stat %ld {s.opt_len, s.static_len}');
\r
2050 build_tree(s, s.d_desc);
\r
2052 Tracev(^M'dist data: dyn %ld, stat %ld {s.opt_len, s.static_len}');
\r
2054 { At this point, opt_len and static_len are the total bit lengths of
\r
2055 the compressed block data, excluding the tree representations. }
\r
2057 { Build the bit length tree for the above two trees, and get the index
\r
2058 in bl_order of the last bit length code to send. }
\r
2059 max_blindex := build_bl_tree(s);
\r
2061 { Determine the best encoding. Compute first the block length in bytes}
\r
2062 opt_lenb := (s.opt_len+3+7) shr 3;
\r
2063 static_lenb := (s.static_len+3+7) shr 3;
\r
2066 Tracev(^M'opt %lu(%lu) stat %lu(%lu) stored %lu lit %u '+
\r
2067 '{opt_lenb, s.opt_len, static_lenb, s.static_len, stored_len,'+
\r
2071 if (static_lenb <= opt_lenb) then
\r
2072 opt_lenb := static_lenb;
\r
2078 Assert(buf <> pcharf(NIL), 'lost buf');
\r
2080 static_lenb := stored_len + 5;
\r
2081 opt_lenb := static_lenb; { force a stored block }
\r
2084 { If compression failed and this is the first and last block,
\r
2085 and if the .zip file can be seeked (to rewrite the local header),
\r
2086 the whole file is transformed into a stored file: }
\r
2088 {$ifdef STORED_FILE_OK}
\r
2089 {$ifdef FORCE_STORED_FILE}
\r
2090 if eof and (s.compressed_len = Long(0)) then
\r
2091 begin { force stored file }
\r
2093 if (stored_len <= opt_lenb) and eof and (s.compressed_len=Long(0))
\r
2094 and seekable()) do
\r
2097 { Since LIT_BUFSIZE <= 2*WSIZE, the input data must be there: }
\r
2098 if (buf = pcharf(0)) then
\r
2099 error ('block vanished');
\r
2101 copy_block(buf, unsigned(stored_len), 0); { without header }
\r
2102 s.compressed_len := stored_len shl 3;
\r
2103 s.method := STORED;
\r
2106 {$endif} { STORED_FILE_OK }
\r
2108 {$ifdef FORCE_STORED}
\r
2109 if (buf <> pchar(0)) then
\r
2110 begin { force stored block }
\r
2112 if (stored_len+4 <= opt_lenb) and (buf <> pcharf(0)) then
\r
2114 { 4: two words for the lengths }
\r
2116 { The test buf <> NULL is only necessary if LIT_BUFSIZE > WSIZE.
\r
2117 Otherwise we can't have processed more than WSIZE input bytes since
\r
2118 the last block flush, because compression would have been
\r
2119 successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
\r
2120 transform a block into a stored block. }
\r
2122 _tr_stored_block(s, buf, stored_len, eof);
\r
2124 {$ifdef FORCE_STATIC}
\r
2127 if (static_lenb >= 0) then
\r
2128 begin { force static trees }
\r
2132 if (static_lenb = opt_lenb) then
\r
2135 send_bits(s, (STATIC_TREES shl 1)+ord(eof), 3);
\r
2136 compress_block(s, static_ltree, static_dtree);
\r
2137 Inc(s.compressed_len, 3 + s.static_len);
\r
2141 send_bits(s, (DYN_TREES shl 1)+ord(eof), 3);
\r
2142 send_all_trees(s, s.l_desc.max_code+1, s.d_desc.max_code+1,
\r
2144 compress_block(s, s.dyn_ltree, s.dyn_dtree);
\r
2145 Inc(s.compressed_len, 3 + s.opt_len);
\r
2148 Assert (s.compressed_len = s.bits_sent, 'bad compressed size');
\r
2155 Inc(s.compressed_len, 7); { align on byte boundary }
\r
2158 Tracev(#13'comprlen %lu(%lu) {s.compressed_len shr 3,'+
\r
2159 's.compressed_len-7*ord(eof)}');
\r
2162 _tr_flush_block := s.compressed_len shr 3;
\r
2166 { ===========================================================================
\r
2167 Save the match info and tally the frequency counts. Return true if
\r
2168 the current block must be flushed. }
\r
2170 function _tr_tally (var s : deflate_state;
\r
2171 dist : unsigned; { distance of matched string }
\r
2172 lc : unsigned) : boolean; { match length-MIN_MATCH or unmatched char (if dist=0) }
\r
2178 {$ifdef TRUNCATE_BLOCK}
\r
2185 s.d_buf^[s.last_lit] := ush(dist);
\r
2186 s.l_buf^[s.last_lit] := uch(lc);
\r
2188 if (dist = 0) then
\r
2190 { lc is the unmatched char }
\r
2191 Inc(s.dyn_ltree[lc].fc.Freq);
\r
2196 { Here, lc is the match length - MIN_MATCH }
\r
2197 Dec(dist); { dist := match distance - 1 }
\r
2199 {macro d_code(dist)}
\r
2200 if (dist) < 256 then
\r
2201 code := _dist_code[dist]
\r
2203 code := _dist_code[256+(dist shr 7)];
\r
2205 {macro MAX_DIST(s) <=> ((s)^.w_size-MIN_LOOKAHEAD)
\r
2206 In order to simplify the code, particularly on 16 bit machines, match
\r
2207 distances are limited to MAX_DIST instead of WSIZE. }
\r
2208 MAX_DIST := ush(s.w_size-MIN_LOOKAHEAD);
\r
2209 Assert((dist < ush(MAX_DIST)) and
\r
2210 (ush(lc) <= ush(MAX_MATCH-MIN_MATCH)) and
\r
2211 (ush(code) < ush(D_CODES)), '_tr_tally: bad match');
\r
2213 Inc(s.dyn_ltree[_length_code[lc]+LITERALS+1].fc.Freq);
\r
2214 {s.dyn_dtree[d_code(dist)].Freq++;}
\r
2215 Inc(s.dyn_dtree[code].fc.Freq);
\r
2218 {$ifdef TRUNCATE_BLOCK}
\r
2219 { Try to guess if it is profitable to stop the current block here }
\r
2220 if (s.last_lit and $1fff = 0) and (s.level > 2) then
\r
2222 { Compute an upper bound for the compressed length }
\r
2223 out_length := ulg(s.last_lit)*Long(8);
\r
2224 in_length := ulg(long(s.strstart) - s.block_start);
\r
2225 for dcode := 0 to D_CODES-1 do
\r
2227 Inc(out_length, ulg(s.dyn_dtree[dcode].fc.Freq *
\r
2228 (Long(5)+extra_dbits[dcode])) );
\r
2230 out_length := out_length shr 3;
\r
2232 Tracev(^M'last_lit %u, in %ld, out ~%ld(%ld%%) ');
\r
2233 { s.last_lit, in_length, out_length,
\r
2234 Long(100) - out_length*Long(100) div in_length)); }
\r
2236 if (s.matches < s.last_lit div 2) and (out_length < in_length div 2) then
\r
2238 _tr_tally := TRUE;
\r
2243 _tr_tally := (s.last_lit = s.lit_bufsize-1);
\r
2244 { We avoid equality with lit_bufsize because of wraparound at 64K
\r
2245 on 16 bit machines and because stored blocks are restricted to
\r