-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtrees.pas
executable file
·2249 lines (1968 loc) · 77.6 KB
/
trees.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Unit trees;
{$T-}
{$define ORG_DEBUG}
{
trees.c -- output deflated data using Huffman coding
Copyright (C) 1995-1998 Jean-loup Gailly
Pascal tranlastion
Copyright (C) 1998 by Jacques Nomssi Nzali
For conditions of distribution and use, see copyright notice in readme.txt
}
{
* ALGORITHM
*
* The "deflation" process uses several Huffman trees. The more
* common source values are represented by shorter bit sequences.
*
* Each code tree is stored in a compressed form which is itself
* a Huffman encoding of the lengths of all the code strings (in
* ascending order by source values). The actual code strings are
* reconstructed from the lengths in the inflate process, as described
* in the deflate specification.
*
* REFERENCES
*
* Deutsch, L.P.,"'Deflate' Compressed Data Format Specification".
* Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc
*
* Storer, James A.
* Data Compression: Methods and Theory, pp. 49-50.
* Computer Science Press, 1988. ISBN 0-7167-8156-5.
*
* Sedgewick, R.
* Algorithms, p290.
* Addison-Wesley, 1983. ISBN 0-201-06672-6.
}
interface
{$I zconf.inc}
uses
{$ifdef DEBUG}
strutils,
{$ENDIF}
zutil, zlib;
{ ===========================================================================
Internal compression state. }
const
LENGTH_CODES = 29;
{ number of length codes, not counting the special END_BLOCK code }
LITERALS = 256;
{ number of literal bytes 0..255 }
L_CODES = (LITERALS+1+LENGTH_CODES);
{ number of Literal or Length codes, including the END_BLOCK code }
D_CODES = 30;
{ number of distance codes }
BL_CODES = 19;
{ number of codes used to transfer the bit lengths }
HEAP_SIZE = (2*L_CODES+1);
{ maximum heap size }
MAX_BITS = 15;
{ All codes must not exceed MAX_BITS bits }
const
INIT_STATE = 42;
BUSY_STATE = 113;
FINISH_STATE = 666;
{ Stream status }
{ Data structure describing a single value and its code string. }
type
ct_data_ptr = ^ct_data;
ct_data = record
fc : record
case byte of
0:(freq : ush); { frequency count }
1:(code : ush); { bit string }
end;
dl : record
case byte of
0:(dad : ush); { father node in Huffman tree }
1:(len : ush); { length of bit string }
end;
end;
{ Freq = fc.freq
Code = fc.code
Dad = dl.dad
Len = dl.len }
type
ltree_type = array[0..HEAP_SIZE-1] of ct_data; { literal and length tree }
dtree_type = array[0..2*D_CODES+1-1] of ct_data; { distance tree }
htree_type = array[0..2*BL_CODES+1-1] of ct_data; { Huffman tree for bit lengths }
{ generic tree type }
tree_type = array[0..(MaxMemBlock div SizeOf(ct_data))-1] of ct_data;
tree_ptr = ^tree_type;
ltree_ptr = ^ltree_type;
dtree_ptr = ^dtree_type;
htree_ptr = ^htree_type;
type
static_tree_desc_ptr = ^static_tree_desc;
static_tree_desc =
record
{const} static_tree : tree_ptr; { static tree or NIL }
{const} extra_bits : pzIntfArray; { extra bits for each code or NIL }
extra_base : int; { base index for extra_bits }
elems : int; { max number of elements in the tree }
max_length : int; { max bit length for the codes }
end;
tree_desc_ptr = ^tree_desc;
tree_desc = record
dyn_tree : tree_ptr; { the dynamic tree }
max_code : int; { largest code with non zero frequency }
stat_desc : static_tree_desc_ptr; { the corresponding static tree }
end;
type
Pos = ush;
Posf = Pos; {FAR}
IPos = uInt;
pPosf = ^Posf;
zPosfArray = array[0..(MaxMemBlock div SizeOf(Posf))-1] of Posf;
pzPosfArray = ^zPosfArray;
{ A Pos is an index in the character window. We use short instead of int to
save space in the various tables. IPos is used only for parameter passing.}
type
deflate_state_ptr = ^deflate_state;
deflate_state = record
strm : z_streamp; { pointer back to this zlib stream }
status : int; { as the name implies }
pending_buf : pzByteArray; { output still pending }
pending_buf_size : ulg; { size of pending_buf }
pending_out : pBytef; { next pending byte to output to the stream }
pending : int; { nb of bytes in the pending buffer }
noheader : int; { suppress zlib header and adler32 }
data_type : Byte; { UNKNOWN, BINARY or ASCII }
method : Byte; { STORED (for zip only) or DEFLATED }
last_flush : int; { value of flush param for previous deflate call }
{ used by deflate.pas: }
w_size : uInt; { LZ77 window size (32K by default) }
w_bits : uInt; { log2(w_size) (8..16) }
w_mask : uInt; { w_size - 1 }
window : pzByteArray;
{ Sliding window. Input bytes are read into the second half of the window,
and move to the first half later to keep a dictionary of at least wSize
bytes. With this organization, matches are limited to a distance of
wSize-MAX_MATCH bytes, but this ensures that IO is always
performed with a length multiple of the block size. Also, it limits
the window size to 64K, which is quite useful on MSDOS.
To do: use the user input buffer as sliding window. }
window_size : ulg;
{ Actual size of window: 2*wSize, except when the user input buffer
is directly used as sliding window. }
prev : pzPosfArray;
{ Link to older string with same hash index. To limit the size of this
array to 64K, this link is maintained only for the last 32K strings.
An index in this array is thus a window index modulo 32K. }
head : pzPosfArray; { Heads of the hash chains or NIL. }
ins_h : uInt; { hash index of string to be inserted }
hash_size : uInt; { number of elements in hash table }
hash_bits : uInt; { log2(hash_size) }
hash_mask : uInt; { hash_size-1 }
hash_shift : uInt;
{ Number of bits by which ins_h must be shifted at each input
step. It must be such that after MIN_MATCH steps, the oldest
byte no longer takes part in the hash key, that is:
hash_shift * MIN_MATCH >= hash_bits }
block_start : long;
{ Window position at the beginning of the current output block. Gets
negative when the window is moved backwards. }
match_length : uInt; { length of best match }
prev_match : IPos; { previous match }
match_available : boolean; { set if previous match exists }
strstart : uInt; { start of string to insert }
match_start : uInt; { start of matching string }
lookahead : uInt; { number of valid bytes ahead in window }
prev_length : uInt;
{ Length of the best match at previous step. Matches not greater than this
are discarded. This is used in the lazy match evaluation. }
max_chain_length : uInt;
{ To speed up deflation, hash chains are never searched beyond this
length. A higher limit improves compression ratio but degrades the
speed. }
{ moved to the end because Borland Pascal won't accept the following:
max_lazy_match : uInt;
max_insert_length : uInt absolute max_lazy_match;
}
level : int; { compression level (1..9) }
strategy : int; { favor or force Huffman coding}
good_match : uInt;
{ Use a faster search when the previous match is longer than this }
nice_match : int; { Stop searching when current match exceeds this }
{ used by trees.pas: }
{ Didn't use ct_data typedef below to supress compiler warning }
dyn_ltree : ltree_type; { literal and length tree }
dyn_dtree : dtree_type; { distance tree }
bl_tree : htree_type; { Huffman tree for bit lengths }
l_desc : tree_desc; { desc. for literal tree }
d_desc : tree_desc; { desc. for distance tree }
bl_desc : tree_desc; { desc. for bit length tree }
bl_count : array[0..MAX_BITS+1-1] of ush;
{ number of codes at each bit length for an optimal tree }
heap : array[0..2*L_CODES+1-1] of int; { heap used to build the Huffman trees }
heap_len : int; { number of elements in the heap }
heap_max : int; { element of largest frequency }
{ The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
The same heap array is used to build all trees. }
depth : array[0..2*L_CODES+1-1] of uch;
{ Depth of each subtree used as tie breaker for trees of equal frequency }
l_buf : puchfArray; { buffer for literals or lengths }
lit_bufsize : uInt;
{ Size of match buffer for literals/lengths. There are 4 reasons for
limiting lit_bufsize to 64K:
- frequencies can be kept in 16 bit counters
- if compression is not successful for the first block, all input
data is still in the window so we can still emit a stored block even
when input comes from standard input. (This can also be done for
all blocks if lit_bufsize is not greater than 32K.)
- if compression is not successful for a file smaller than 64K, we can
even emit a stored file instead of a stored block (saving 5 bytes).
This is applicable only for zip (not gzip or zlib).
- creating new Huffman trees less frequently may not provide fast
adaptation to changes in the input data statistics. (Take for
example a binary file with poorly compressible code followed by
a highly compressible string table.) Smaller buffer sizes give
fast adaptation but have of course the overhead of transmitting
trees more frequently.
- I can't count above 4 }
last_lit : uInt; { running index in l_buf }
d_buf : pushfArray;
{ Buffer for distances. To simplify the code, d_buf and l_buf have
the same number of elements. To use different lengths, an extra flag
array would be necessary. }
opt_len : ulg; { bit length of current block with optimal trees }
static_len : ulg; { bit length of current block with static trees }
compressed_len : ulg; { total bit length of compressed file }
matches : uInt; { number of string matches in current block }
last_eob_len : int; { bit length of EOB code for last block }
{$ifdef DEBUG}
bits_sent : ulg; { bit length of the compressed data }
{$endif}
bi_buf : ush;
{ Output buffer. bits are inserted starting at the bottom (least
significant bits). }
bi_valid : int;
{ Number of valid bits in bi_buf. All bits above the last valid bit
are always zero. }
case byte of
0:(max_lazy_match : uInt);
{ Attempt to find a better match only when the current match is strictly
smaller than this value. This mechanism is used only for compression
levels >= 4. }
1:(max_insert_length : uInt);
{ Insert new strings in the hash table only if the match length is not
greater than this length. This saves time but degrades compression.
max_insert_length is used only for compression levels <= 3. }
end;
procedure _tr_init (var s : deflate_state);
function _tr_tally (var s : deflate_state;
dist : unsigned;
lc : unsigned) : boolean;
function _tr_flush_block (var s : deflate_state;
buf : pcharf;
stored_len : ulg;
eof : boolean) : ulg;
procedure _tr_align(var s : deflate_state);
procedure _tr_stored_block(var s : deflate_state;
buf : pcharf;
stored_len : ulg;
eof : boolean);
implementation
{ #define GEN_TREES_H }
{$ifndef GEN_TREES_H}
{ header created automatically with -DGEN_TREES_H }
const
DIST_CODE_LEN = 512; { see definition of array dist_code below }
{ The static literal tree. Since the bit lengths are imposed, there is no
need for the L_CODES extra codes used during heap construction. However
The codes 286 and 287 are needed to build a canonical tree (see _tr_init
below). }
var
static_ltree : array[0..L_CODES+2-1] of ct_data = (
{ fc:(freq, code) dl:(dad,len) }
(fc:(freq: 12);dl:(len: 8)), (fc:(freq:140);dl:(len: 8)), (fc:(freq: 76);dl:(len: 8)),
(fc:(freq:204);dl:(len: 8)), (fc:(freq: 44);dl:(len: 8)), (fc:(freq:172);dl:(len: 8)),
(fc:(freq:108);dl:(len: 8)), (fc:(freq:236);dl:(len: 8)), (fc:(freq: 28);dl:(len: 8)),
(fc:(freq:156);dl:(len: 8)), (fc:(freq: 92);dl:(len: 8)), (fc:(freq:220);dl:(len: 8)),
(fc:(freq: 60);dl:(len: 8)), (fc:(freq:188);dl:(len: 8)), (fc:(freq:124);dl:(len: 8)),
(fc:(freq:252);dl:(len: 8)), (fc:(freq: 2);dl:(len: 8)), (fc:(freq:130);dl:(len: 8)),
(fc:(freq: 66);dl:(len: 8)), (fc:(freq:194);dl:(len: 8)), (fc:(freq: 34);dl:(len: 8)),
(fc:(freq:162);dl:(len: 8)), (fc:(freq: 98);dl:(len: 8)), (fc:(freq:226);dl:(len: 8)),
(fc:(freq: 18);dl:(len: 8)), (fc:(freq:146);dl:(len: 8)), (fc:(freq: 82);dl:(len: 8)),
(fc:(freq:210);dl:(len: 8)), (fc:(freq: 50);dl:(len: 8)), (fc:(freq:178);dl:(len: 8)),
(fc:(freq:114);dl:(len: 8)), (fc:(freq:242);dl:(len: 8)), (fc:(freq: 10);dl:(len: 8)),
(fc:(freq:138);dl:(len: 8)), (fc:(freq: 74);dl:(len: 8)), (fc:(freq:202);dl:(len: 8)),
(fc:(freq: 42);dl:(len: 8)), (fc:(freq:170);dl:(len: 8)), (fc:(freq:106);dl:(len: 8)),
(fc:(freq:234);dl:(len: 8)), (fc:(freq: 26);dl:(len: 8)), (fc:(freq:154);dl:(len: 8)),
(fc:(freq: 90);dl:(len: 8)), (fc:(freq:218);dl:(len: 8)), (fc:(freq: 58);dl:(len: 8)),
(fc:(freq:186);dl:(len: 8)), (fc:(freq:122);dl:(len: 8)), (fc:(freq:250);dl:(len: 8)),
(fc:(freq: 6);dl:(len: 8)), (fc:(freq:134);dl:(len: 8)), (fc:(freq: 70);dl:(len: 8)),
(fc:(freq:198);dl:(len: 8)), (fc:(freq: 38);dl:(len: 8)), (fc:(freq:166);dl:(len: 8)),
(fc:(freq:102);dl:(len: 8)), (fc:(freq:230);dl:(len: 8)), (fc:(freq: 22);dl:(len: 8)),
(fc:(freq:150);dl:(len: 8)), (fc:(freq: 86);dl:(len: 8)), (fc:(freq:214);dl:(len: 8)),
(fc:(freq: 54);dl:(len: 8)), (fc:(freq:182);dl:(len: 8)), (fc:(freq:118);dl:(len: 8)),
(fc:(freq:246);dl:(len: 8)), (fc:(freq: 14);dl:(len: 8)), (fc:(freq:142);dl:(len: 8)),
(fc:(freq: 78);dl:(len: 8)), (fc:(freq:206);dl:(len: 8)), (fc:(freq: 46);dl:(len: 8)),
(fc:(freq:174);dl:(len: 8)), (fc:(freq:110);dl:(len: 8)), (fc:(freq:238);dl:(len: 8)),
(fc:(freq: 30);dl:(len: 8)), (fc:(freq:158);dl:(len: 8)), (fc:(freq: 94);dl:(len: 8)),
(fc:(freq:222);dl:(len: 8)), (fc:(freq: 62);dl:(len: 8)), (fc:(freq:190);dl:(len: 8)),
(fc:(freq:126);dl:(len: 8)), (fc:(freq:254);dl:(len: 8)), (fc:(freq: 1);dl:(len: 8)),
(fc:(freq:129);dl:(len: 8)), (fc:(freq: 65);dl:(len: 8)), (fc:(freq:193);dl:(len: 8)),
(fc:(freq: 33);dl:(len: 8)), (fc:(freq:161);dl:(len: 8)), (fc:(freq: 97);dl:(len: 8)),
(fc:(freq:225);dl:(len: 8)), (fc:(freq: 17);dl:(len: 8)), (fc:(freq:145);dl:(len: 8)),
(fc:(freq: 81);dl:(len: 8)), (fc:(freq:209);dl:(len: 8)), (fc:(freq: 49);dl:(len: 8)),
(fc:(freq:177);dl:(len: 8)), (fc:(freq:113);dl:(len: 8)), (fc:(freq:241);dl:(len: 8)),
(fc:(freq: 9);dl:(len: 8)), (fc:(freq:137);dl:(len: 8)), (fc:(freq: 73);dl:(len: 8)),
(fc:(freq:201);dl:(len: 8)), (fc:(freq: 41);dl:(len: 8)), (fc:(freq:169);dl:(len: 8)),
(fc:(freq:105);dl:(len: 8)), (fc:(freq:233);dl:(len: 8)), (fc:(freq: 25);dl:(len: 8)),
(fc:(freq:153);dl:(len: 8)), (fc:(freq: 89);dl:(len: 8)), (fc:(freq:217);dl:(len: 8)),
(fc:(freq: 57);dl:(len: 8)), (fc:(freq:185);dl:(len: 8)), (fc:(freq:121);dl:(len: 8)),
(fc:(freq:249);dl:(len: 8)), (fc:(freq: 5);dl:(len: 8)), (fc:(freq:133);dl:(len: 8)),
(fc:(freq: 69);dl:(len: 8)), (fc:(freq:197);dl:(len: 8)), (fc:(freq: 37);dl:(len: 8)),
(fc:(freq:165);dl:(len: 8)), (fc:(freq:101);dl:(len: 8)), (fc:(freq:229);dl:(len: 8)),
(fc:(freq: 21);dl:(len: 8)), (fc:(freq:149);dl:(len: 8)), (fc:(freq: 85);dl:(len: 8)),
(fc:(freq:213);dl:(len: 8)), (fc:(freq: 53);dl:(len: 8)), (fc:(freq:181);dl:(len: 8)),
(fc:(freq:117);dl:(len: 8)), (fc:(freq:245);dl:(len: 8)), (fc:(freq: 13);dl:(len: 8)),
(fc:(freq:141);dl:(len: 8)), (fc:(freq: 77);dl:(len: 8)), (fc:(freq:205);dl:(len: 8)),
(fc:(freq: 45);dl:(len: 8)), (fc:(freq:173);dl:(len: 8)), (fc:(freq:109);dl:(len: 8)),
(fc:(freq:237);dl:(len: 8)), (fc:(freq: 29);dl:(len: 8)), (fc:(freq:157);dl:(len: 8)),
(fc:(freq: 93);dl:(len: 8)), (fc:(freq:221);dl:(len: 8)), (fc:(freq: 61);dl:(len: 8)),
(fc:(freq:189);dl:(len: 8)), (fc:(freq:125);dl:(len: 8)), (fc:(freq:253);dl:(len: 8)),
(fc:(freq: 19);dl:(len: 9)), (fc:(freq:275);dl:(len: 9)), (fc:(freq:147);dl:(len: 9)),
(fc:(freq:403);dl:(len: 9)), (fc:(freq: 83);dl:(len: 9)), (fc:(freq:339);dl:(len: 9)),
(fc:(freq:211);dl:(len: 9)), (fc:(freq:467);dl:(len: 9)), (fc:(freq: 51);dl:(len: 9)),
(fc:(freq:307);dl:(len: 9)), (fc:(freq:179);dl:(len: 9)), (fc:(freq:435);dl:(len: 9)),
(fc:(freq:115);dl:(len: 9)), (fc:(freq:371);dl:(len: 9)), (fc:(freq:243);dl:(len: 9)),
(fc:(freq:499);dl:(len: 9)), (fc:(freq: 11);dl:(len: 9)), (fc:(freq:267);dl:(len: 9)),
(fc:(freq:139);dl:(len: 9)), (fc:(freq:395);dl:(len: 9)), (fc:(freq: 75);dl:(len: 9)),
(fc:(freq:331);dl:(len: 9)), (fc:(freq:203);dl:(len: 9)), (fc:(freq:459);dl:(len: 9)),
(fc:(freq: 43);dl:(len: 9)), (fc:(freq:299);dl:(len: 9)), (fc:(freq:171);dl:(len: 9)),
(fc:(freq:427);dl:(len: 9)), (fc:(freq:107);dl:(len: 9)), (fc:(freq:363);dl:(len: 9)),
(fc:(freq:235);dl:(len: 9)), (fc:(freq:491);dl:(len: 9)), (fc:(freq: 27);dl:(len: 9)),
(fc:(freq:283);dl:(len: 9)), (fc:(freq:155);dl:(len: 9)), (fc:(freq:411);dl:(len: 9)),
(fc:(freq: 91);dl:(len: 9)), (fc:(freq:347);dl:(len: 9)), (fc:(freq:219);dl:(len: 9)),
(fc:(freq:475);dl:(len: 9)), (fc:(freq: 59);dl:(len: 9)), (fc:(freq:315);dl:(len: 9)),
(fc:(freq:187);dl:(len: 9)), (fc:(freq:443);dl:(len: 9)), (fc:(freq:123);dl:(len: 9)),
(fc:(freq:379);dl:(len: 9)), (fc:(freq:251);dl:(len: 9)), (fc:(freq:507);dl:(len: 9)),
(fc:(freq: 7);dl:(len: 9)), (fc:(freq:263);dl:(len: 9)), (fc:(freq:135);dl:(len: 9)),
(fc:(freq:391);dl:(len: 9)), (fc:(freq: 71);dl:(len: 9)), (fc:(freq:327);dl:(len: 9)),
(fc:(freq:199);dl:(len: 9)), (fc:(freq:455);dl:(len: 9)), (fc:(freq: 39);dl:(len: 9)),
(fc:(freq:295);dl:(len: 9)), (fc:(freq:167);dl:(len: 9)), (fc:(freq:423);dl:(len: 9)),
(fc:(freq:103);dl:(len: 9)), (fc:(freq:359);dl:(len: 9)), (fc:(freq:231);dl:(len: 9)),
(fc:(freq:487);dl:(len: 9)), (fc:(freq: 23);dl:(len: 9)), (fc:(freq:279);dl:(len: 9)),
(fc:(freq:151);dl:(len: 9)), (fc:(freq:407);dl:(len: 9)), (fc:(freq: 87);dl:(len: 9)),
(fc:(freq:343);dl:(len: 9)), (fc:(freq:215);dl:(len: 9)), (fc:(freq:471);dl:(len: 9)),
(fc:(freq: 55);dl:(len: 9)), (fc:(freq:311);dl:(len: 9)), (fc:(freq:183);dl:(len: 9)),
(fc:(freq:439);dl:(len: 9)), (fc:(freq:119);dl:(len: 9)), (fc:(freq:375);dl:(len: 9)),
(fc:(freq:247);dl:(len: 9)), (fc:(freq:503);dl:(len: 9)), (fc:(freq: 15);dl:(len: 9)),
(fc:(freq:271);dl:(len: 9)), (fc:(freq:143);dl:(len: 9)), (fc:(freq:399);dl:(len: 9)),
(fc:(freq: 79);dl:(len: 9)), (fc:(freq:335);dl:(len: 9)), (fc:(freq:207);dl:(len: 9)),
(fc:(freq:463);dl:(len: 9)), (fc:(freq: 47);dl:(len: 9)), (fc:(freq:303);dl:(len: 9)),
(fc:(freq:175);dl:(len: 9)), (fc:(freq:431);dl:(len: 9)), (fc:(freq:111);dl:(len: 9)),
(fc:(freq:367);dl:(len: 9)), (fc:(freq:239);dl:(len: 9)), (fc:(freq:495);dl:(len: 9)),
(fc:(freq: 31);dl:(len: 9)), (fc:(freq:287);dl:(len: 9)), (fc:(freq:159);dl:(len: 9)),
(fc:(freq:415);dl:(len: 9)), (fc:(freq: 95);dl:(len: 9)), (fc:(freq:351);dl:(len: 9)),
(fc:(freq:223);dl:(len: 9)), (fc:(freq:479);dl:(len: 9)), (fc:(freq: 63);dl:(len: 9)),
(fc:(freq:319);dl:(len: 9)), (fc:(freq:191);dl:(len: 9)), (fc:(freq:447);dl:(len: 9)),
(fc:(freq:127);dl:(len: 9)), (fc:(freq:383);dl:(len: 9)), (fc:(freq:255);dl:(len: 9)),
(fc:(freq:511);dl:(len: 9)), (fc:(freq: 0);dl:(len: 7)), (fc:(freq: 64);dl:(len: 7)),
(fc:(freq: 32);dl:(len: 7)), (fc:(freq: 96);dl:(len: 7)), (fc:(freq: 16);dl:(len: 7)),
(fc:(freq: 80);dl:(len: 7)), (fc:(freq: 48);dl:(len: 7)), (fc:(freq:112);dl:(len: 7)),
(fc:(freq: 8);dl:(len: 7)), (fc:(freq: 72);dl:(len: 7)), (fc:(freq: 40);dl:(len: 7)),
(fc:(freq:104);dl:(len: 7)), (fc:(freq: 24);dl:(len: 7)), (fc:(freq: 88);dl:(len: 7)),
(fc:(freq: 56);dl:(len: 7)), (fc:(freq:120);dl:(len: 7)), (fc:(freq: 4);dl:(len: 7)),
(fc:(freq: 68);dl:(len: 7)), (fc:(freq: 36);dl:(len: 7)), (fc:(freq:100);dl:(len: 7)),
(fc:(freq: 20);dl:(len: 7)), (fc:(freq: 84);dl:(len: 7)), (fc:(freq: 52);dl:(len: 7)),
(fc:(freq:116);dl:(len: 7)), (fc:(freq: 3);dl:(len: 8)), (fc:(freq:131);dl:(len: 8)),
(fc:(freq: 67);dl:(len: 8)), (fc:(freq:195);dl:(len: 8)), (fc:(freq: 35);dl:(len: 8)),
(fc:(freq:163);dl:(len: 8)), (fc:(freq: 99);dl:(len: 8)), (fc:(freq:227);dl:(len: 8))
);
{ The static distance tree. (Actually a trivial tree since all lens use
5 bits.) }
static_dtree : array[0..D_CODES-1] of ct_data = (
(fc:(freq: 0); dl:(len:5)), (fc:(freq:16); dl:(len:5)), (fc:(freq: 8); dl:(len:5)),
(fc:(freq:24); dl:(len:5)), (fc:(freq: 4); dl:(len:5)), (fc:(freq:20); dl:(len:5)),
(fc:(freq:12); dl:(len:5)), (fc:(freq:28); dl:(len:5)), (fc:(freq: 2); dl:(len:5)),
(fc:(freq:18); dl:(len:5)), (fc:(freq:10); dl:(len:5)), (fc:(freq:26); dl:(len:5)),
(fc:(freq: 6); dl:(len:5)), (fc:(freq:22); dl:(len:5)), (fc:(freq:14); dl:(len:5)),
(fc:(freq:30); dl:(len:5)), (fc:(freq: 1); dl:(len:5)), (fc:(freq:17); dl:(len:5)),
(fc:(freq: 9); dl:(len:5)), (fc:(freq:25); dl:(len:5)), (fc:(freq: 5); dl:(len:5)),
(fc:(freq:21); dl:(len:5)), (fc:(freq:13); dl:(len:5)), (fc:(freq:29); dl:(len:5)),
(fc:(freq: 3); dl:(len:5)), (fc:(freq:19); dl:(len:5)), (fc:(freq:11); dl:(len:5)),
(fc:(freq:27); dl:(len:5)), (fc:(freq: 7); dl:(len:5)), (fc:(freq:23); dl:(len:5))
);
{ Distance codes. The first 256 values correspond to the distances
3 .. 258, the last 256 values correspond to the top 8 bits of
the 15 bit distances. }
_dist_code : array[0..DIST_CODE_LEN-1] of uch = (
0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8,
8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13,
13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 0, 16, 17,
18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22,
23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27,
27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29
);
{ length code for each normalized match length (0 == MIN_MATCH) }
_length_code : array[0..MAX_MATCH-MIN_MATCH+1-1] of uch = (
0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 12,
13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16,
17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19,
19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22,
22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23,
23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28
);
{ First normalized length for each code (0 = MIN_MATCH) }
base_length : array[0..LENGTH_CODES-1] of int = (
0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56,
64, 80, 96, 112, 128, 160, 192, 224, 0
);
{ First normalized distance for each code (0 = distance of 1) }
base_dist : array[0..D_CODES-1] of int = (
0, 1, 2, 3, 4, 6, 8, 12, 16, 24,
32, 48, 64, 96, 128, 192, 256, 384, 512, 768,
1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576
);
{$endif}
{ Output a byte on the stream.
IN assertion: there is enough room in pending_buf.
macro put_byte(s, c)
begin
s^.pending_buf^[s^.pending] := (c);
Inc(s^.pending);
end
}
const
MIN_LOOKAHEAD = (MAX_MATCH+MIN_MATCH+1);
{ Minimum amount of lookahead, except at the end of the input file.
See deflate.c for comments about the MIN_MATCH+1. }
{macro d_code(dist)
if (dist) < 256 then
:= _dist_code[dist]
else
:= _dist_code[256+((dist) shr 7)]);
Mapping from a distance to a distance code. dist is the distance - 1 and
must not have side effects. _dist_code[256] and _dist_code[257] are never
used. }
{$ifndef ORG_DEBUG}
{ Inline versions of _tr_tally for speed: }
#if defined(GEN_TREES_H) || !defined(STDC)
extern uch _length_code[];
extern uch _dist_code[];
#else
extern const uch _length_code[];
extern const uch _dist_code[];
#endif
macro _tr_tally_lit(s, c, flush)
var
cc : uch;
begin
cc := (c);
s^.d_buf[s^.last_lit] := 0;
s^.l_buf[s^.last_lit] := cc;
Inc(s^.last_lit);
Inc(s^.dyn_ltree[cc].fc.Freq);
flush := (s^.last_lit = s^.lit_bufsize-1);
end;
macro _tr_tally_dist(s, distance, length, flush) \
var
len : uch;
dist : ush;
begin
len := (length);
dist := (distance);
s^.d_buf[s^.last_lit] := dist;
s^.l_buf[s^.last_lit] = len;
Inc(s^.last_lit);
Dec(dist);
Inc(s^.dyn_ltree[_length_code[len]+LITERALS+1].fc.Freq);
Inc(s^.dyn_dtree[d_code(dist)].Freq);
flush := (s^.last_lit = s^.lit_bufsize-1);
end;
{$endif}
{ ===========================================================================
Constants }
const
MAX_BL_BITS = 7;
{ Bit length codes must not exceed MAX_BL_BITS bits }
const
END_BLOCK = 256;
{ end of block literal code }
const
REP_3_6 = 16;
{ repeat previous bit length 3-6 times (2 bits of repeat count) }
const
REPZ_3_10 = 17;
{ repeat a zero length 3-10 times (3 bits of repeat count) }
const
REPZ_11_138 = 18;
{ repeat a zero length 11-138 times (7 bits of repeat count) }
{local}
const
extra_lbits : array[0..LENGTH_CODES-1] of int
{ extra bits for each length code }
= (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);
{local}
const
extra_dbits : array[0..D_CODES-1] of int
{ extra bits for each distance code }
= (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);
{local}
const
extra_blbits : array[0..BL_CODES-1] of int { extra bits for each bit length code }
= (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7);
{local}
const
bl_order : array[0..BL_CODES-1] of uch
= (16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15);
{ The lengths of the bit length codes are sent in order of decreasing
probability, to avoid transmitting the lengths for unused bit length codes.
}
const
Buf_size = (8 * 2*sizeof(char));
{ Number of bits used within bi_buf. (bi_buf might be implemented on
more than 16 bits on some systems.) }
{ ===========================================================================
Local data. These are initialized only once. }
{$ifdef GEN_TREES_H)}
{ non ANSI compilers may not accept trees.h }
const
DIST_CODE_LEN = 512; { see definition of array dist_code below }
{local}
var
static_ltree : array[0..L_CODES+2-1] of ct_data;
{ The static literal tree. Since the bit lengths are imposed, there is no
need for the L_CODES extra codes used during heap construction. However
The codes 286 and 287 are needed to build a canonical tree (see _tr_init
below). }
{local}
static_dtree : array[0..D_CODES-1] of ct_data;
{ The static distance tree. (Actually a trivial tree since all codes use
5 bits.) }
_dist_code : array[0..DIST_CODE_LEN-1] of uch;
{ Distance codes. The first 256 values correspond to the distances
3 .. 258, the last 256 values correspond to the top 8 bits of
the 15 bit distances. }
_length_code : array[0..MAX_MATCH-MIN_MATCH+1-1] of uch;
{ length code for each normalized match length (0 == MIN_MATCH) }
{local}
base_length : array[0..LENGTH_CODES-1] of int;
{ First normalized length for each code (0 = MIN_MATCH) }
{local}
base_dist : array[0..D_CODES-1] of int;
{ First normalized distance for each code (0 = distance of 1) }
{$endif} { GEN_TREES_H }
{local}
const
static_l_desc : static_tree_desc =
(static_tree: {tree_ptr}(@(static_ltree)); { pointer to array of ct_data }
extra_bits: {pzIntfArray}(@(extra_lbits)); { pointer to array of int }
extra_base: LITERALS+1;
elems: L_CODES;
max_length: MAX_BITS);
{local}
const
static_d_desc : static_tree_desc =
(static_tree: {tree_ptr}(@(static_dtree));
extra_bits: {pzIntfArray}(@(extra_dbits));
extra_base : 0;
elems: D_CODES;
max_length: MAX_BITS);
{local}
const
static_bl_desc : static_tree_desc =
(static_tree: {tree_ptr}(NIL);
extra_bits: {pzIntfArray}@(extra_blbits);
extra_base : 0;
elems: BL_CODES;
max_length: MAX_BL_BITS);
(* ===========================================================================
Local (static) routines in this file. }
procedure tr_static_init;
procedure init_block(var deflate_state);
procedure pqdownheap(var s : deflate_state;
var tree : ct_data;
k : int);
procedure gen_bitlen(var s : deflate_state;
var desc : tree_desc);
procedure gen_codes(var tree : ct_data;
max_code : int;
bl_count : pushf);
procedure build_tree(var s : deflate_state;
var desc : tree_desc);
procedure scan_tree(var s : deflate_state;
var tree : ct_data;
max_code : int);
procedure send_tree(var s : deflate_state;
var tree : ct_data;
max_code : int);
function build_bl_tree(var deflate_state) : int;
procedure send_all_trees(var deflate_state;
lcodes : int;
dcodes : int;
blcodes : int);
procedure compress_block(var s : deflate_state;
var ltree : ct_data;
var dtree : ct_data);
procedure set_data_type(var s : deflate_state);
function bi_reverse(value : unsigned;
length : int) : unsigned;
procedure bi_windup(var deflate_state);
procedure bi_flush(var deflate_state);
procedure copy_block(var deflate_state;
buf : pcharf;
len : unsigned;
header : int);
*)
{$ifdef GEN_TREES_H}
{local}
procedure gen_trees_header;
{$endif}
(*
{ ===========================================================================
Output a short LSB first on the stream.
IN assertion: there is enough room in pendingBuf. }
macro put_short(s, w)
begin
{put_byte(s, (uch)((w) & 0xff));}
s.pending_buf^[s.pending] := uch((w) and $ff);
Inc(s.pending);
{put_byte(s, (uch)((ush)(w) >> 8));}
s.pending_buf^[s.pending] := uch(ush(w) shr 8);;
Inc(s.pending);
end
*)
{ ===========================================================================
Send a value on a given number of bits.
IN assertion: length <= 16 and value fits in length bits. }
{$ifdef ORG_DEBUG}
{local}
procedure send_bits(var s : deflate_state;
value : int; { value to send }
length : int); { number of bits }
begin
{$ifdef DEBUG}
Tracevv(' l '+IntToStr(length)+ ' v '+IntToStr(value));
Assert((length > 0) and (length <= 15), 'invalid length');
Inc(s.bits_sent, ulg(length));
{$ENDIF}
{ If not enough room in bi_buf, use (valid) bits from bi_buf and
(16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
unused bits in value. }
{$IFOPT Q+} {$Q-} {$DEFINE NoOverflowCheck} {$ENDIF}
{$IFOPT R+} {$R-} {$DEFINE NoRangeCheck} {$ENDIF}
if (s.bi_valid > int(Buf_size) - length) then
begin
s.bi_buf := s.bi_buf or int(value shl s.bi_valid);
{put_short(s, s.bi_buf);}
s.pending_buf^[s.pending] := uch(s.bi_buf and $ff);
Inc(s.pending);
s.pending_buf^[s.pending] := uch(ush(s.bi_buf) shr 8);;
Inc(s.pending);
s.bi_buf := ush(value) shr (Buf_size - s.bi_valid);
Inc(s.bi_valid, length - Buf_size);
end
else
begin
s.bi_buf := s.bi_buf or int(value shl s.bi_valid);
Inc(s.bi_valid, length);
end;
{$IFDEF NoOverflowCheck} {$Q+} {$UNDEF NoOverflowCheck} {$ENDIF}
{$IFDEF NoRangeCheck} {$Q+} {$UNDEF NoRangeCheck} {$ENDIF}
end;
{$else} { !DEBUG }
macro send_code(s, c, tree)
begin
send_bits(s, tree[c].Code, tree[c].Len);
{ Send a code of the given tree. c and tree must not have side effects }
end
macro send_bits(s, value, length) \
begin int len := length;\
if (s^.bi_valid > (int)Buf_size - len) begin\
int val := value;\
s^.bi_buf |= (val << s^.bi_valid);\
{put_short(s, s.bi_buf);}
s.pending_buf^[s.pending] := uch(s.bi_buf and $ff);
Inc(s.pending);
s.pending_buf^[s.pending] := uch(ush(s.bi_buf) shr 8);;
Inc(s.pending);
s^.bi_buf := (ush)val >> (Buf_size - s^.bi_valid);\
s^.bi_valid += len - Buf_size;\
end else begin\
s^.bi_buf |= (value) << s^.bi_valid;\
s^.bi_valid += len;\
end\
end;
{$endif} { DEBUG }
{ ===========================================================================
Reverse the first len bits of a code, using straightforward code (a faster
method would use a table)
IN assertion: 1 <= len <= 15 }
{local}
function bi_reverse(code : unsigned; { the value to invert }
len : int) : unsigned; { its bit length }
var
res : unsigned; {register}
begin
res := 0;
repeat
res := res or (code and 1);
code := code shr 1;
res := res shl 1;
Dec(len);
until (len <= 0);
bi_reverse := res shr 1;
end;
{ ===========================================================================
Generate the codes for a given tree and bit counts (which need not be
optimal).
IN assertion: the array bl_count contains the bit length statistics for
the given tree and the field len is set for all tree elements.
OUT assertion: the field code is set for all tree elements of non
zero code length. }
{local}
procedure gen_codes(tree : tree_ptr; { the tree to decorate }
max_code : int; { largest code with non zero frequency }
var bl_count : array of ushf); { number of codes at each bit length }
var
next_code : array[0..MAX_BITS+1-1] of ush; { next code value for each bit length }
code : ush; { running code value }
bits : int; { bit index }
n : int; { code index }
var
len : int;
begin
code := 0;
{ The distribution counts are first used to generate the code values
without bit reversal. }
for bits := 1 to MAX_BITS do
begin
code := ((code + bl_count[bits-1]) shl 1);
next_code[bits] := code;
end;
{ Check that the bit counts in bl_count are consistent. The last code
must be all ones. }
{$IFDEF DEBUG}
Assert (code + bl_count[MAX_BITS]-1 = (1 shl MAX_BITS)-1,
'inconsistent bit counts');
Tracev(#13'gen_codes: max_code '+IntToStr(max_code));
{$ENDIF}
for n := 0 to max_code do
begin
len := tree^[n].dl.Len;
if (len = 0) then
continue;
{ Now reverse the bits }
tree^[n].fc.Code := bi_reverse(next_code[len], len);
Inc(next_code[len]);
{$ifdef DEBUG}
if (n>31) and (n<128) then
Tracecv(tree <> tree_ptr(@static_ltree),
(^M'n #'+IntToStr(n)+' '+char(n)+' l '+IntToStr(len)+' c '+
IntToStr(tree^[n].fc.Code)+' ('+IntToStr(next_code[len]-1)+')'))
else
Tracecv(tree <> tree_ptr(@static_ltree),
(^M'n #'+IntToStr(n)+' l '+IntToStr(len)+' c '+
IntToStr(tree^[n].fc.Code)+' ('+IntToStr(next_code[len]-1)+')'));
{$ENDIF}
end;
end;
{ ===========================================================================
Genererate the file trees.h describing the static trees. }
{$ifdef GEN_TREES_H}
macro SEPARATOR(i, last, width)
if (i) = (last) then
( ^M');'^M^M
else \
if (i) mod (width) = (width)-1 then
','^M
else
', '
procedure gen_trees_header;
var
header : system.text;
i : int;
begin
system.assign(header, 'trees.inc');
{$I-}
ReWrite(header);
{$I+}
Assert (IOresult <> 0, 'Can''t open trees.h');
WriteLn(header,
'{ header created automatically with -DGEN_TREES_H }'^M);
WriteLn(header, 'local const ct_data static_ltree[L_CODES+2] := (');
for i := 0 to L_CODES+2-1 do
begin
WriteLn(header, '((%3u),(%3u))%s', static_ltree[i].Code,
static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5));
end;
WriteLn(header, 'local const ct_data static_dtree[D_CODES] := (');
for i := 0 to D_CODES-1 do
begin
WriteLn(header, '((%2u),(%2u))%s', static_dtree[i].Code,
static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5));
end;
WriteLn(header, 'const uch _dist_code[DIST_CODE_LEN] := (');
for i := 0 to DIST_CODE_LEN-1 do
begin
WriteLn(header, '%2u%s', _dist_code[i],
SEPARATOR(i, DIST_CODE_LEN-1, 20));
end;
WriteLn(header, 'const uch _length_code[MAX_MATCH-MIN_MATCH+1]= (');
for i := 0 to MAX_MATCH-MIN_MATCH+1-1 do
begin
WriteLn(header, '%2u%s', _length_code[i],
SEPARATOR(i, MAX_MATCH-MIN_MATCH, 20));
end;
WriteLn(header, 'local const int base_length[LENGTH_CODES] := (');
for i := 0 to LENGTH_CODES-1 do
begin
WriteLn(header, '%1u%s', base_length[i],
SEPARATOR(i, LENGTH_CODES-1, 20));
end;
WriteLn(header, 'local const int base_dist[D_CODES] := (');
for i := 0 to D_CODES-1 do
begin
WriteLn(header, '%5u%s', base_dist[i],
SEPARATOR(i, D_CODES-1, 10));
end;
close(header);
end;
{$endif} { GEN_TREES_H }