1*4a5d661aSToomas Soome /* trees.c -- output deflated data using Huffman coding
2*4a5d661aSToomas Soome * Copyright (C) 1995-2012 Jean-loup Gailly
3*4a5d661aSToomas Soome * detect_data_type() function provided freely by Cosmin Truta, 2006
4*4a5d661aSToomas Soome * For conditions of distribution and use, see copyright notice in zlib.h
5*4a5d661aSToomas Soome */
6*4a5d661aSToomas Soome
7*4a5d661aSToomas Soome /*
8*4a5d661aSToomas Soome * ALGORITHM
9*4a5d661aSToomas Soome *
10*4a5d661aSToomas Soome * The "deflation" process uses several Huffman trees. The more
11*4a5d661aSToomas Soome * common source values are represented by shorter bit sequences.
12*4a5d661aSToomas Soome *
13*4a5d661aSToomas Soome * Each code tree is stored in a compressed form which is itself
14*4a5d661aSToomas Soome * a Huffman encoding of the lengths of all the code strings (in
15*4a5d661aSToomas Soome * ascending order by source values). The actual code strings are
16*4a5d661aSToomas Soome * reconstructed from the lengths in the inflate process, as described
17*4a5d661aSToomas Soome * in the deflate specification.
18*4a5d661aSToomas Soome *
19*4a5d661aSToomas Soome * REFERENCES
20*4a5d661aSToomas Soome *
21*4a5d661aSToomas Soome * Deutsch, L.P.,"'Deflate' Compressed Data Format Specification".
22*4a5d661aSToomas Soome * Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc
23*4a5d661aSToomas Soome *
24*4a5d661aSToomas Soome * Storer, James A.
25*4a5d661aSToomas Soome * Data Compression: Methods and Theory, pp. 49-50.
26*4a5d661aSToomas Soome * Computer Science Press, 1988. ISBN 0-7167-8156-5.
27*4a5d661aSToomas Soome *
28*4a5d661aSToomas Soome * Sedgewick, R.
29*4a5d661aSToomas Soome * Algorithms, p290.
30*4a5d661aSToomas Soome * Addison-Wesley, 1983. ISBN 0-201-06672-6.
31*4a5d661aSToomas Soome */
32*4a5d661aSToomas Soome
33*4a5d661aSToomas Soome /* @(#) $Id$ */
34*4a5d661aSToomas Soome
35*4a5d661aSToomas Soome /* #define GEN_TREES_H */
36*4a5d661aSToomas Soome
37*4a5d661aSToomas Soome #include "deflate.h"
38*4a5d661aSToomas Soome
39*4a5d661aSToomas Soome #ifdef DEBUG
40*4a5d661aSToomas Soome # include <ctype.h>
41*4a5d661aSToomas Soome #endif
42*4a5d661aSToomas Soome
43*4a5d661aSToomas Soome /* ===========================================================================
44*4a5d661aSToomas Soome * Constants
45*4a5d661aSToomas Soome */
46*4a5d661aSToomas Soome
47*4a5d661aSToomas Soome #define MAX_BL_BITS 7
48*4a5d661aSToomas Soome /* Bit length codes must not exceed MAX_BL_BITS bits */
49*4a5d661aSToomas Soome
50*4a5d661aSToomas Soome #define END_BLOCK 256
51*4a5d661aSToomas Soome /* end of block literal code */
52*4a5d661aSToomas Soome
53*4a5d661aSToomas Soome #define REP_3_6 16
54*4a5d661aSToomas Soome /* repeat previous bit length 3-6 times (2 bits of repeat count) */
55*4a5d661aSToomas Soome
56*4a5d661aSToomas Soome #define REPZ_3_10 17
57*4a5d661aSToomas Soome /* repeat a zero length 3-10 times (3 bits of repeat count) */
58*4a5d661aSToomas Soome
59*4a5d661aSToomas Soome #define REPZ_11_138 18
60*4a5d661aSToomas Soome /* repeat a zero length 11-138 times (7 bits of repeat count) */
61*4a5d661aSToomas Soome
62*4a5d661aSToomas Soome local const int extra_lbits[LENGTH_CODES] /* extra bits for each length code */
63*4a5d661aSToomas Soome = {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};
64*4a5d661aSToomas Soome
65*4a5d661aSToomas Soome local const int extra_dbits[D_CODES] /* extra bits for each distance code */
66*4a5d661aSToomas Soome = {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};
67*4a5d661aSToomas Soome
68*4a5d661aSToomas Soome local const int extra_blbits[BL_CODES]/* extra bits for each bit length code */
69*4a5d661aSToomas Soome = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7};
70*4a5d661aSToomas Soome
71*4a5d661aSToomas Soome local const uch bl_order[BL_CODES]
72*4a5d661aSToomas Soome = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15};
73*4a5d661aSToomas Soome /* The lengths of the bit length codes are sent in order of decreasing
74*4a5d661aSToomas Soome * probability, to avoid transmitting the lengths for unused bit length codes.
75*4a5d661aSToomas Soome */
76*4a5d661aSToomas Soome
77*4a5d661aSToomas Soome /* ===========================================================================
78*4a5d661aSToomas Soome * Local data. These are initialized only once.
79*4a5d661aSToomas Soome */
80*4a5d661aSToomas Soome
81*4a5d661aSToomas Soome #define DIST_CODE_LEN 512 /* see definition of array dist_code below */
82*4a5d661aSToomas Soome
83*4a5d661aSToomas Soome #if defined(GEN_TREES_H) || !defined(STDC)
84*4a5d661aSToomas Soome /* non ANSI compilers may not accept trees.h */
85*4a5d661aSToomas Soome
86*4a5d661aSToomas Soome local ct_data static_ltree[L_CODES+2];
87*4a5d661aSToomas Soome /* The static literal tree. Since the bit lengths are imposed, there is no
88*4a5d661aSToomas Soome * need for the L_CODES extra codes used during heap construction. However
89*4a5d661aSToomas Soome * The codes 286 and 287 are needed to build a canonical tree (see _tr_init
90*4a5d661aSToomas Soome * below).
91*4a5d661aSToomas Soome */
92*4a5d661aSToomas Soome
93*4a5d661aSToomas Soome local ct_data static_dtree[D_CODES];
94*4a5d661aSToomas Soome /* The static distance tree. (Actually a trivial tree since all codes use
95*4a5d661aSToomas Soome * 5 bits.)
96*4a5d661aSToomas Soome */
97*4a5d661aSToomas Soome
98*4a5d661aSToomas Soome uch _dist_code[DIST_CODE_LEN];
99*4a5d661aSToomas Soome /* Distance codes. The first 256 values correspond to the distances
100*4a5d661aSToomas Soome * 3 .. 258, the last 256 values correspond to the top 8 bits of
101*4a5d661aSToomas Soome * the 15 bit distances.
102*4a5d661aSToomas Soome */
103*4a5d661aSToomas Soome
104*4a5d661aSToomas Soome uch _length_code[MAX_MATCH-MIN_MATCH+1];
105*4a5d661aSToomas Soome /* length code for each normalized match length (0 == MIN_MATCH) */
106*4a5d661aSToomas Soome
107*4a5d661aSToomas Soome local int base_length[LENGTH_CODES];
108*4a5d661aSToomas Soome /* First normalized length for each code (0 = MIN_MATCH) */
109*4a5d661aSToomas Soome
110*4a5d661aSToomas Soome local int base_dist[D_CODES];
111*4a5d661aSToomas Soome /* First normalized distance for each code (0 = distance of 1) */
112*4a5d661aSToomas Soome
113*4a5d661aSToomas Soome #else
114*4a5d661aSToomas Soome # include "trees.h"
115*4a5d661aSToomas Soome #endif /* GEN_TREES_H */
116*4a5d661aSToomas Soome
117*4a5d661aSToomas Soome struct static_tree_desc_s {
118*4a5d661aSToomas Soome const ct_data *static_tree; /* static tree or NULL */
119*4a5d661aSToomas Soome const intf *extra_bits; /* extra bits for each code or NULL */
120*4a5d661aSToomas Soome int extra_base; /* base index for extra_bits */
121*4a5d661aSToomas Soome int elems; /* max number of elements in the tree */
122*4a5d661aSToomas Soome int max_length; /* max bit length for the codes */
123*4a5d661aSToomas Soome };
124*4a5d661aSToomas Soome
125*4a5d661aSToomas Soome local static_tree_desc static_l_desc =
126*4a5d661aSToomas Soome {static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS};
127*4a5d661aSToomas Soome
128*4a5d661aSToomas Soome local static_tree_desc static_d_desc =
129*4a5d661aSToomas Soome {static_dtree, extra_dbits, 0, D_CODES, MAX_BITS};
130*4a5d661aSToomas Soome
131*4a5d661aSToomas Soome local static_tree_desc static_bl_desc =
132*4a5d661aSToomas Soome {(const ct_data *)0, extra_blbits, 0, BL_CODES, MAX_BL_BITS};
133*4a5d661aSToomas Soome
134*4a5d661aSToomas Soome /* ===========================================================================
135*4a5d661aSToomas Soome * Local (static) routines in this file.
136*4a5d661aSToomas Soome */
137*4a5d661aSToomas Soome
138*4a5d661aSToomas Soome local void tr_static_init OF((void));
139*4a5d661aSToomas Soome local void init_block OF((deflate_state *s));
140*4a5d661aSToomas Soome local void pqdownheap OF((deflate_state *s, ct_data *tree, int k));
141*4a5d661aSToomas Soome local void gen_bitlen OF((deflate_state *s, tree_desc *desc));
142*4a5d661aSToomas Soome local void gen_codes OF((ct_data *tree, int max_code, ushf *bl_count));
143*4a5d661aSToomas Soome local void build_tree OF((deflate_state *s, tree_desc *desc));
144*4a5d661aSToomas Soome local void scan_tree OF((deflate_state *s, ct_data *tree, int max_code));
145*4a5d661aSToomas Soome local void send_tree OF((deflate_state *s, ct_data *tree, int max_code));
146*4a5d661aSToomas Soome local int build_bl_tree OF((deflate_state *s));
147*4a5d661aSToomas Soome local void send_all_trees OF((deflate_state *s, int lcodes, int dcodes,
148*4a5d661aSToomas Soome int blcodes));
149*4a5d661aSToomas Soome local void compress_block OF((deflate_state *s, const ct_data *ltree,
150*4a5d661aSToomas Soome const ct_data *dtree));
151*4a5d661aSToomas Soome local int detect_data_type OF((deflate_state *s));
152*4a5d661aSToomas Soome local unsigned bi_reverse OF((unsigned value, int length));
153*4a5d661aSToomas Soome local void bi_windup OF((deflate_state *s));
154*4a5d661aSToomas Soome local void bi_flush OF((deflate_state *s));
155*4a5d661aSToomas Soome local void copy_block OF((deflate_state *s, charf *buf, unsigned len,
156*4a5d661aSToomas Soome int header));
157*4a5d661aSToomas Soome
158*4a5d661aSToomas Soome #ifdef GEN_TREES_H
159*4a5d661aSToomas Soome local void gen_trees_header OF((void));
160*4a5d661aSToomas Soome #endif
161*4a5d661aSToomas Soome
162*4a5d661aSToomas Soome #ifndef DEBUG
163*4a5d661aSToomas Soome # define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len)
164*4a5d661aSToomas Soome /* Send a code of the given tree. c and tree must not have side effects */
165*4a5d661aSToomas Soome
166*4a5d661aSToomas Soome #else /* DEBUG */
167*4a5d661aSToomas Soome # define send_code(s, c, tree) \
168*4a5d661aSToomas Soome { if (z_verbose>2) fprintf(stderr,"\ncd %3d ",(c)); \
169*4a5d661aSToomas Soome send_bits(s, tree[c].Code, tree[c].Len); }
170*4a5d661aSToomas Soome #endif
171*4a5d661aSToomas Soome
172*4a5d661aSToomas Soome /* ===========================================================================
173*4a5d661aSToomas Soome * Output a short LSB first on the stream.
174*4a5d661aSToomas Soome * IN assertion: there is enough room in pendingBuf.
175*4a5d661aSToomas Soome */
176*4a5d661aSToomas Soome #define put_short(s, w) { \
177*4a5d661aSToomas Soome put_byte(s, (uch)((w) & 0xff)); \
178*4a5d661aSToomas Soome put_byte(s, (uch)((ush)(w) >> 8)); \
179*4a5d661aSToomas Soome }
180*4a5d661aSToomas Soome
181*4a5d661aSToomas Soome /* ===========================================================================
182*4a5d661aSToomas Soome * Send a value on a given number of bits.
183*4a5d661aSToomas Soome * IN assertion: length <= 16 and value fits in length bits.
184*4a5d661aSToomas Soome */
185*4a5d661aSToomas Soome #ifdef DEBUG
186*4a5d661aSToomas Soome local void send_bits OF((deflate_state *s, int value, int length));
187*4a5d661aSToomas Soome
send_bits(s,value,length)188*4a5d661aSToomas Soome local void send_bits(s, value, length)
189*4a5d661aSToomas Soome deflate_state *s;
190*4a5d661aSToomas Soome int value; /* value to send */
191*4a5d661aSToomas Soome int length; /* number of bits */
192*4a5d661aSToomas Soome {
193*4a5d661aSToomas Soome Tracevv((stderr," l %2d v %4x ", length, value));
194*4a5d661aSToomas Soome Assert(length > 0 && length <= 15, "invalid length");
195*4a5d661aSToomas Soome s->bits_sent += (ulg)length;
196*4a5d661aSToomas Soome
197*4a5d661aSToomas Soome /* If not enough room in bi_buf, use (valid) bits from bi_buf and
198*4a5d661aSToomas Soome * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
199*4a5d661aSToomas Soome * unused bits in value.
200*4a5d661aSToomas Soome */
201*4a5d661aSToomas Soome if (s->bi_valid > (int)Buf_size - length) {
202*4a5d661aSToomas Soome s->bi_buf |= (ush)value << s->bi_valid;
203*4a5d661aSToomas Soome put_short(s, s->bi_buf);
204*4a5d661aSToomas Soome s->bi_buf = (ush)value >> (Buf_size - s->bi_valid);
205*4a5d661aSToomas Soome s->bi_valid += length - Buf_size;
206*4a5d661aSToomas Soome } else {
207*4a5d661aSToomas Soome s->bi_buf |= (ush)value << s->bi_valid;
208*4a5d661aSToomas Soome s->bi_valid += length;
209*4a5d661aSToomas Soome }
210*4a5d661aSToomas Soome }
211*4a5d661aSToomas Soome #else /* !DEBUG */
212*4a5d661aSToomas Soome
213*4a5d661aSToomas Soome #define send_bits(s, value, length) \
214*4a5d661aSToomas Soome { int len = length;\
215*4a5d661aSToomas Soome if (s->bi_valid > (int)Buf_size - len) {\
216*4a5d661aSToomas Soome int val = value;\
217*4a5d661aSToomas Soome s->bi_buf |= (ush)val << s->bi_valid;\
218*4a5d661aSToomas Soome put_short(s, s->bi_buf);\
219*4a5d661aSToomas Soome s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\
220*4a5d661aSToomas Soome s->bi_valid += len - Buf_size;\
221*4a5d661aSToomas Soome } else {\
222*4a5d661aSToomas Soome s->bi_buf |= (ush)(value) << s->bi_valid;\
223*4a5d661aSToomas Soome s->bi_valid += len;\
224*4a5d661aSToomas Soome }\
225*4a5d661aSToomas Soome }
226*4a5d661aSToomas Soome #endif /* DEBUG */
227*4a5d661aSToomas Soome
228*4a5d661aSToomas Soome
229*4a5d661aSToomas Soome /* the arguments must not have side effects */
230*4a5d661aSToomas Soome
231*4a5d661aSToomas Soome /* ===========================================================================
232*4a5d661aSToomas Soome * Initialize the various 'constant' tables.
233*4a5d661aSToomas Soome */
tr_static_init()234*4a5d661aSToomas Soome local void tr_static_init()
235*4a5d661aSToomas Soome {
236*4a5d661aSToomas Soome #if defined(GEN_TREES_H) || !defined(STDC)
237*4a5d661aSToomas Soome static int static_init_done = 0;
238*4a5d661aSToomas Soome int n; /* iterates over tree elements */
239*4a5d661aSToomas Soome int bits; /* bit counter */
240*4a5d661aSToomas Soome int length; /* length value */
241*4a5d661aSToomas Soome int code; /* code value */
242*4a5d661aSToomas Soome int dist; /* distance index */
243*4a5d661aSToomas Soome ush bl_count[MAX_BITS+1];
244*4a5d661aSToomas Soome /* number of codes at each bit length for an optimal tree */
245*4a5d661aSToomas Soome
246*4a5d661aSToomas Soome if (static_init_done) return;
247*4a5d661aSToomas Soome
248*4a5d661aSToomas Soome /* For some embedded targets, global variables are not initialized: */
249*4a5d661aSToomas Soome #ifdef NO_INIT_GLOBAL_POINTERS
250*4a5d661aSToomas Soome static_l_desc.static_tree = static_ltree;
251*4a5d661aSToomas Soome static_l_desc.extra_bits = extra_lbits;
252*4a5d661aSToomas Soome static_d_desc.static_tree = static_dtree;
253*4a5d661aSToomas Soome static_d_desc.extra_bits = extra_dbits;
254*4a5d661aSToomas Soome static_bl_desc.extra_bits = extra_blbits;
255*4a5d661aSToomas Soome #endif
256*4a5d661aSToomas Soome
257*4a5d661aSToomas Soome /* Initialize the mapping length (0..255) -> length code (0..28) */
258*4a5d661aSToomas Soome length = 0;
259*4a5d661aSToomas Soome for (code = 0; code < LENGTH_CODES-1; code++) {
260*4a5d661aSToomas Soome base_length[code] = length;
261*4a5d661aSToomas Soome for (n = 0; n < (1<<extra_lbits[code]); n++) {
262*4a5d661aSToomas Soome _length_code[length++] = (uch)code;
263*4a5d661aSToomas Soome }
264*4a5d661aSToomas Soome }
265*4a5d661aSToomas Soome Assert (length == 256, "tr_static_init: length != 256");
266*4a5d661aSToomas Soome /* Note that the length 255 (match length 258) can be represented
267*4a5d661aSToomas Soome * in two different ways: code 284 + 5 bits or code 285, so we
268*4a5d661aSToomas Soome * overwrite length_code[255] to use the best encoding:
269*4a5d661aSToomas Soome */
270*4a5d661aSToomas Soome _length_code[length-1] = (uch)code;
271*4a5d661aSToomas Soome
272*4a5d661aSToomas Soome /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
273*4a5d661aSToomas Soome dist = 0;
274*4a5d661aSToomas Soome for (code = 0 ; code < 16; code++) {
275*4a5d661aSToomas Soome base_dist[code] = dist;
276*4a5d661aSToomas Soome for (n = 0; n < (1<<extra_dbits[code]); n++) {
277*4a5d661aSToomas Soome _dist_code[dist++] = (uch)code;
278*4a5d661aSToomas Soome }
279*4a5d661aSToomas Soome }
280*4a5d661aSToomas Soome Assert (dist == 256, "tr_static_init: dist != 256");
281*4a5d661aSToomas Soome dist >>= 7; /* from now on, all distances are divided by 128 */
282*4a5d661aSToomas Soome for ( ; code < D_CODES; code++) {
283*4a5d661aSToomas Soome base_dist[code] = dist << 7;
284*4a5d661aSToomas Soome for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) {
285*4a5d661aSToomas Soome _dist_code[256 + dist++] = (uch)code;
286*4a5d661aSToomas Soome }
287*4a5d661aSToomas Soome }
288*4a5d661aSToomas Soome Assert (dist == 256, "tr_static_init: 256+dist != 512");
289*4a5d661aSToomas Soome
290*4a5d661aSToomas Soome /* Construct the codes of the static literal tree */
291*4a5d661aSToomas Soome for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;
292*4a5d661aSToomas Soome n = 0;
293*4a5d661aSToomas Soome while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++;
294*4a5d661aSToomas Soome while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++;
295*4a5d661aSToomas Soome while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++;
296*4a5d661aSToomas Soome while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++;
297*4a5d661aSToomas Soome /* Codes 286 and 287 do not exist, but we must include them in the
298*4a5d661aSToomas Soome * tree construction to get a canonical Huffman tree (longest code
299*4a5d661aSToomas Soome * all ones)
300*4a5d661aSToomas Soome */
301*4a5d661aSToomas Soome gen_codes((ct_data *)static_ltree, L_CODES+1, bl_count);
302*4a5d661aSToomas Soome
303*4a5d661aSToomas Soome /* The static distance tree is trivial: */
304*4a5d661aSToomas Soome for (n = 0; n < D_CODES; n++) {
305*4a5d661aSToomas Soome static_dtree[n].Len = 5;
306*4a5d661aSToomas Soome static_dtree[n].Code = bi_reverse((unsigned)n, 5);
307*4a5d661aSToomas Soome }
308*4a5d661aSToomas Soome static_init_done = 1;
309*4a5d661aSToomas Soome
310*4a5d661aSToomas Soome # ifdef GEN_TREES_H
311*4a5d661aSToomas Soome gen_trees_header();
312*4a5d661aSToomas Soome # endif
313*4a5d661aSToomas Soome #endif /* defined(GEN_TREES_H) || !defined(STDC) */
314*4a5d661aSToomas Soome }
315*4a5d661aSToomas Soome
316*4a5d661aSToomas Soome /* ===========================================================================
317*4a5d661aSToomas Soome * Genererate the file trees.h describing the static trees.
318*4a5d661aSToomas Soome */
319*4a5d661aSToomas Soome #ifdef GEN_TREES_H
320*4a5d661aSToomas Soome # ifndef DEBUG
321*4a5d661aSToomas Soome # include <stdio.h>
322*4a5d661aSToomas Soome # endif
323*4a5d661aSToomas Soome
324*4a5d661aSToomas Soome # define SEPARATOR(i, last, width) \
325*4a5d661aSToomas Soome ((i) == (last)? "\n};\n\n" : \
326*4a5d661aSToomas Soome ((i) % (width) == (width)-1 ? ",\n" : ", "))
327*4a5d661aSToomas Soome
gen_trees_header()328*4a5d661aSToomas Soome void gen_trees_header()
329*4a5d661aSToomas Soome {
330*4a5d661aSToomas Soome FILE *header = fopen("trees.h", "w");
331*4a5d661aSToomas Soome int i;
332*4a5d661aSToomas Soome
333*4a5d661aSToomas Soome Assert (header != NULL, "Can't open trees.h");
334*4a5d661aSToomas Soome fprintf(header,
335*4a5d661aSToomas Soome "/* header created automatically with -DGEN_TREES_H */\n\n");
336*4a5d661aSToomas Soome
337*4a5d661aSToomas Soome fprintf(header, "local const ct_data static_ltree[L_CODES+2] = {\n");
338*4a5d661aSToomas Soome for (i = 0; i < L_CODES+2; i++) {
339*4a5d661aSToomas Soome fprintf(header, "{{%3u},{%3u}}%s", static_ltree[i].Code,
340*4a5d661aSToomas Soome static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5));
341*4a5d661aSToomas Soome }
342*4a5d661aSToomas Soome
343*4a5d661aSToomas Soome fprintf(header, "local const ct_data static_dtree[D_CODES] = {\n");
344*4a5d661aSToomas Soome for (i = 0; i < D_CODES; i++) {
345*4a5d661aSToomas Soome fprintf(header, "{{%2u},{%2u}}%s", static_dtree[i].Code,
346*4a5d661aSToomas Soome static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5));
347*4a5d661aSToomas Soome }
348*4a5d661aSToomas Soome
349*4a5d661aSToomas Soome fprintf(header, "const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = {\n");
350*4a5d661aSToomas Soome for (i = 0; i < DIST_CODE_LEN; i++) {
351*4a5d661aSToomas Soome fprintf(header, "%2u%s", _dist_code[i],
352*4a5d661aSToomas Soome SEPARATOR(i, DIST_CODE_LEN-1, 20));
353*4a5d661aSToomas Soome }
354*4a5d661aSToomas Soome
355*4a5d661aSToomas Soome fprintf(header,
356*4a5d661aSToomas Soome "const uch ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= {\n");
357*4a5d661aSToomas Soome for (i = 0; i < MAX_MATCH-MIN_MATCH+1; i++) {
358*4a5d661aSToomas Soome fprintf(header, "%2u%s", _length_code[i],
359*4a5d661aSToomas Soome SEPARATOR(i, MAX_MATCH-MIN_MATCH, 20));
360*4a5d661aSToomas Soome }
361*4a5d661aSToomas Soome
362*4a5d661aSToomas Soome fprintf(header, "local const int base_length[LENGTH_CODES] = {\n");
363*4a5d661aSToomas Soome for (i = 0; i < LENGTH_CODES; i++) {
364*4a5d661aSToomas Soome fprintf(header, "%1u%s", base_length[i],
365*4a5d661aSToomas Soome SEPARATOR(i, LENGTH_CODES-1, 20));
366*4a5d661aSToomas Soome }
367*4a5d661aSToomas Soome
368*4a5d661aSToomas Soome fprintf(header, "local const int base_dist[D_CODES] = {\n");
369*4a5d661aSToomas Soome for (i = 0; i < D_CODES; i++) {
370*4a5d661aSToomas Soome fprintf(header, "%5u%s", base_dist[i],
371*4a5d661aSToomas Soome SEPARATOR(i, D_CODES-1, 10));
372*4a5d661aSToomas Soome }
373*4a5d661aSToomas Soome
374*4a5d661aSToomas Soome fclose(header);
375*4a5d661aSToomas Soome }
376*4a5d661aSToomas Soome #endif /* GEN_TREES_H */
377*4a5d661aSToomas Soome
378*4a5d661aSToomas Soome /* ===========================================================================
379*4a5d661aSToomas Soome * Initialize the tree data structures for a new zlib stream.
380*4a5d661aSToomas Soome */
_tr_init(s)381*4a5d661aSToomas Soome void ZLIB_INTERNAL _tr_init(s)
382*4a5d661aSToomas Soome deflate_state *s;
383*4a5d661aSToomas Soome {
384*4a5d661aSToomas Soome tr_static_init();
385*4a5d661aSToomas Soome
386*4a5d661aSToomas Soome s->l_desc.dyn_tree = s->dyn_ltree;
387*4a5d661aSToomas Soome s->l_desc.stat_desc = &static_l_desc;
388*4a5d661aSToomas Soome
389*4a5d661aSToomas Soome s->d_desc.dyn_tree = s->dyn_dtree;
390*4a5d661aSToomas Soome s->d_desc.stat_desc = &static_d_desc;
391*4a5d661aSToomas Soome
392*4a5d661aSToomas Soome s->bl_desc.dyn_tree = s->bl_tree;
393*4a5d661aSToomas Soome s->bl_desc.stat_desc = &static_bl_desc;
394*4a5d661aSToomas Soome
395*4a5d661aSToomas Soome s->bi_buf = 0;
396*4a5d661aSToomas Soome s->bi_valid = 0;
397*4a5d661aSToomas Soome #ifdef DEBUG
398*4a5d661aSToomas Soome s->compressed_len = 0L;
399*4a5d661aSToomas Soome s->bits_sent = 0L;
400*4a5d661aSToomas Soome #endif
401*4a5d661aSToomas Soome
402*4a5d661aSToomas Soome /* Initialize the first block of the first file: */
403*4a5d661aSToomas Soome init_block(s);
404*4a5d661aSToomas Soome }
405*4a5d661aSToomas Soome
406*4a5d661aSToomas Soome /* ===========================================================================
407*4a5d661aSToomas Soome * Initialize a new block.
408*4a5d661aSToomas Soome */
init_block(s)409*4a5d661aSToomas Soome local void init_block(s)
410*4a5d661aSToomas Soome deflate_state *s;
411*4a5d661aSToomas Soome {
412*4a5d661aSToomas Soome int n; /* iterates over tree elements */
413*4a5d661aSToomas Soome
414*4a5d661aSToomas Soome /* Initialize the trees. */
415*4a5d661aSToomas Soome for (n = 0; n < L_CODES; n++) s->dyn_ltree[n].Freq = 0;
416*4a5d661aSToomas Soome for (n = 0; n < D_CODES; n++) s->dyn_dtree[n].Freq = 0;
417*4a5d661aSToomas Soome for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0;
418*4a5d661aSToomas Soome
419*4a5d661aSToomas Soome s->dyn_ltree[END_BLOCK].Freq = 1;
420*4a5d661aSToomas Soome s->opt_len = s->static_len = 0L;
421*4a5d661aSToomas Soome s->last_lit = s->matches = 0;
422*4a5d661aSToomas Soome }
423*4a5d661aSToomas Soome
424*4a5d661aSToomas Soome #define SMALLEST 1
425*4a5d661aSToomas Soome /* Index within the heap array of least frequent node in the Huffman tree */
426*4a5d661aSToomas Soome
427*4a5d661aSToomas Soome
428*4a5d661aSToomas Soome /* ===========================================================================
429*4a5d661aSToomas Soome * Remove the smallest element from the heap and recreate the heap with
430*4a5d661aSToomas Soome * one less element. Updates heap and heap_len.
431*4a5d661aSToomas Soome */
432*4a5d661aSToomas Soome #define pqremove(s, tree, top) \
433*4a5d661aSToomas Soome {\
434*4a5d661aSToomas Soome top = s->heap[SMALLEST]; \
435*4a5d661aSToomas Soome s->heap[SMALLEST] = s->heap[s->heap_len--]; \
436*4a5d661aSToomas Soome pqdownheap(s, tree, SMALLEST); \
437*4a5d661aSToomas Soome }
438*4a5d661aSToomas Soome
439*4a5d661aSToomas Soome /* ===========================================================================
440*4a5d661aSToomas Soome * Compares to subtrees, using the tree depth as tie breaker when
441*4a5d661aSToomas Soome * the subtrees have equal frequency. This minimizes the worst case length.
442*4a5d661aSToomas Soome */
443*4a5d661aSToomas Soome #define smaller(tree, n, m, depth) \
444*4a5d661aSToomas Soome (tree[n].Freq < tree[m].Freq || \
445*4a5d661aSToomas Soome (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m]))
446*4a5d661aSToomas Soome
447*4a5d661aSToomas Soome /* ===========================================================================
448*4a5d661aSToomas Soome * Restore the heap property by moving down the tree starting at node k,
449*4a5d661aSToomas Soome * exchanging a node with the smallest of its two sons if necessary, stopping
450*4a5d661aSToomas Soome * when the heap property is re-established (each father smaller than its
451*4a5d661aSToomas Soome * two sons).
452*4a5d661aSToomas Soome */
pqdownheap(s,tree,k)453*4a5d661aSToomas Soome local void pqdownheap(s, tree, k)
454*4a5d661aSToomas Soome deflate_state *s;
455*4a5d661aSToomas Soome ct_data *tree; /* the tree to restore */
456*4a5d661aSToomas Soome int k; /* node to move down */
457*4a5d661aSToomas Soome {
458*4a5d661aSToomas Soome int v = s->heap[k];
459*4a5d661aSToomas Soome int j = k << 1; /* left son of k */
460*4a5d661aSToomas Soome while (j <= s->heap_len) {
461*4a5d661aSToomas Soome /* Set j to the smallest of the two sons: */
462*4a5d661aSToomas Soome if (j < s->heap_len &&
463*4a5d661aSToomas Soome smaller(tree, s->heap[j+1], s->heap[j], s->depth)) {
464*4a5d661aSToomas Soome j++;
465*4a5d661aSToomas Soome }
466*4a5d661aSToomas Soome /* Exit if v is smaller than both sons */
467*4a5d661aSToomas Soome if (smaller(tree, v, s->heap[j], s->depth)) break;
468*4a5d661aSToomas Soome
469*4a5d661aSToomas Soome /* Exchange v with the smallest son */
470*4a5d661aSToomas Soome s->heap[k] = s->heap[j]; k = j;
471*4a5d661aSToomas Soome
472*4a5d661aSToomas Soome /* And continue down the tree, setting j to the left son of k */
473*4a5d661aSToomas Soome j <<= 1;
474*4a5d661aSToomas Soome }
475*4a5d661aSToomas Soome s->heap[k] = v;
476*4a5d661aSToomas Soome }
477*4a5d661aSToomas Soome
478*4a5d661aSToomas Soome /* ===========================================================================
479*4a5d661aSToomas Soome * Compute the optimal bit lengths for a tree and update the total bit length
480*4a5d661aSToomas Soome * for the current block.
481*4a5d661aSToomas Soome * IN assertion: the fields freq and dad are set, heap[heap_max] and
482*4a5d661aSToomas Soome * above are the tree nodes sorted by increasing frequency.
483*4a5d661aSToomas Soome * OUT assertions: the field len is set to the optimal bit length, the
484*4a5d661aSToomas Soome * array bl_count contains the frequencies for each bit length.
485*4a5d661aSToomas Soome * The length opt_len is updated; static_len is also updated if stree is
486*4a5d661aSToomas Soome * not null.
487*4a5d661aSToomas Soome */
gen_bitlen(s,desc)488*4a5d661aSToomas Soome local void gen_bitlen(s, desc)
489*4a5d661aSToomas Soome deflate_state *s;
490*4a5d661aSToomas Soome tree_desc *desc; /* the tree descriptor */
491*4a5d661aSToomas Soome {
492*4a5d661aSToomas Soome ct_data *tree = desc->dyn_tree;
493*4a5d661aSToomas Soome int max_code = desc->max_code;
494*4a5d661aSToomas Soome const ct_data *stree = desc->stat_desc->static_tree;
495*4a5d661aSToomas Soome const intf *extra = desc->stat_desc->extra_bits;
496*4a5d661aSToomas Soome int base = desc->stat_desc->extra_base;
497*4a5d661aSToomas Soome int max_length = desc->stat_desc->max_length;
498*4a5d661aSToomas Soome int h; /* heap index */
499*4a5d661aSToomas Soome int n, m; /* iterate over the tree elements */
500*4a5d661aSToomas Soome int bits; /* bit length */
501*4a5d661aSToomas Soome int xbits; /* extra bits */
502*4a5d661aSToomas Soome ush f; /* frequency */
503*4a5d661aSToomas Soome int overflow = 0; /* number of elements with bit length too large */
504*4a5d661aSToomas Soome
505*4a5d661aSToomas Soome for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0;
506*4a5d661aSToomas Soome
507*4a5d661aSToomas Soome /* In a first pass, compute the optimal bit lengths (which may
508*4a5d661aSToomas Soome * overflow in the case of the bit length tree).
509*4a5d661aSToomas Soome */
510*4a5d661aSToomas Soome tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */
511*4a5d661aSToomas Soome
512*4a5d661aSToomas Soome for (h = s->heap_max+1; h < HEAP_SIZE; h++) {
513*4a5d661aSToomas Soome n = s->heap[h];
514*4a5d661aSToomas Soome bits = tree[tree[n].Dad].Len + 1;
515*4a5d661aSToomas Soome if (bits > max_length) bits = max_length, overflow++;
516*4a5d661aSToomas Soome tree[n].Len = (ush)bits;
517*4a5d661aSToomas Soome /* We overwrite tree[n].Dad which is no longer needed */
518*4a5d661aSToomas Soome
519*4a5d661aSToomas Soome if (n > max_code) continue; /* not a leaf node */
520*4a5d661aSToomas Soome
521*4a5d661aSToomas Soome s->bl_count[bits]++;
522*4a5d661aSToomas Soome xbits = 0;
523*4a5d661aSToomas Soome if (n >= base) xbits = extra[n-base];
524*4a5d661aSToomas Soome f = tree[n].Freq;
525*4a5d661aSToomas Soome s->opt_len += (ulg)f * (bits + xbits);
526*4a5d661aSToomas Soome if (stree) s->static_len += (ulg)f * (stree[n].Len + xbits);
527*4a5d661aSToomas Soome }
528*4a5d661aSToomas Soome if (overflow == 0) return;
529*4a5d661aSToomas Soome
530*4a5d661aSToomas Soome Trace((stderr,"\nbit length overflow\n"));
531*4a5d661aSToomas Soome /* This happens for example on obj2 and pic of the Calgary corpus */
532*4a5d661aSToomas Soome
533*4a5d661aSToomas Soome /* Find the first bit length which could increase: */
534*4a5d661aSToomas Soome do {
535*4a5d661aSToomas Soome bits = max_length-1;
536*4a5d661aSToomas Soome while (s->bl_count[bits] == 0) bits--;
537*4a5d661aSToomas Soome s->bl_count[bits]--; /* move one leaf down the tree */
538*4a5d661aSToomas Soome s->bl_count[bits+1] += 2; /* move one overflow item as its brother */
539*4a5d661aSToomas Soome s->bl_count[max_length]--;
540*4a5d661aSToomas Soome /* The brother of the overflow item also moves one step up,
541*4a5d661aSToomas Soome * but this does not affect bl_count[max_length]
542*4a5d661aSToomas Soome */
543*4a5d661aSToomas Soome overflow -= 2;
544*4a5d661aSToomas Soome } while (overflow > 0);
545*4a5d661aSToomas Soome
546*4a5d661aSToomas Soome /* Now recompute all bit lengths, scanning in increasing frequency.
547*4a5d661aSToomas Soome * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
548*4a5d661aSToomas Soome * lengths instead of fixing only the wrong ones. This idea is taken
549*4a5d661aSToomas Soome * from 'ar' written by Haruhiko Okumura.)
550*4a5d661aSToomas Soome */
551*4a5d661aSToomas Soome for (bits = max_length; bits != 0; bits--) {
552*4a5d661aSToomas Soome n = s->bl_count[bits];
553*4a5d661aSToomas Soome while (n != 0) {
554*4a5d661aSToomas Soome m = s->heap[--h];
555*4a5d661aSToomas Soome if (m > max_code) continue;
556*4a5d661aSToomas Soome if ((unsigned) tree[m].Len != (unsigned) bits) {
557*4a5d661aSToomas Soome Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
558*4a5d661aSToomas Soome s->opt_len += ((long)bits - (long)tree[m].Len)
559*4a5d661aSToomas Soome *(long)tree[m].Freq;
560*4a5d661aSToomas Soome tree[m].Len = (ush)bits;
561*4a5d661aSToomas Soome }
562*4a5d661aSToomas Soome n--;
563*4a5d661aSToomas Soome }
564*4a5d661aSToomas Soome }
565*4a5d661aSToomas Soome }
566*4a5d661aSToomas Soome
567*4a5d661aSToomas Soome /* ===========================================================================
568*4a5d661aSToomas Soome * Generate the codes for a given tree and bit counts (which need not be
569*4a5d661aSToomas Soome * optimal).
570*4a5d661aSToomas Soome * IN assertion: the array bl_count contains the bit length statistics for
571*4a5d661aSToomas Soome * the given tree and the field len is set for all tree elements.
572*4a5d661aSToomas Soome * OUT assertion: the field code is set for all tree elements of non
573*4a5d661aSToomas Soome * zero code length.
574*4a5d661aSToomas Soome */
gen_codes(tree,max_code,bl_count)575*4a5d661aSToomas Soome local void gen_codes (tree, max_code, bl_count)
576*4a5d661aSToomas Soome ct_data *tree; /* the tree to decorate */
577*4a5d661aSToomas Soome int max_code; /* largest code with non zero frequency */
578*4a5d661aSToomas Soome ushf *bl_count; /* number of codes at each bit length */
579*4a5d661aSToomas Soome {
580*4a5d661aSToomas Soome ush next_code[MAX_BITS+1]; /* next code value for each bit length */
581*4a5d661aSToomas Soome ush code = 0; /* running code value */
582*4a5d661aSToomas Soome int bits; /* bit index */
583*4a5d661aSToomas Soome int n; /* code index */
584*4a5d661aSToomas Soome
585*4a5d661aSToomas Soome /* The distribution counts are first used to generate the code values
586*4a5d661aSToomas Soome * without bit reversal.
587*4a5d661aSToomas Soome */
588*4a5d661aSToomas Soome for (bits = 1; bits <= MAX_BITS; bits++) {
589*4a5d661aSToomas Soome next_code[bits] = code = (code + bl_count[bits-1]) << 1;
590*4a5d661aSToomas Soome }
591*4a5d661aSToomas Soome /* Check that the bit counts in bl_count are consistent. The last code
592*4a5d661aSToomas Soome * must be all ones.
593*4a5d661aSToomas Soome */
594*4a5d661aSToomas Soome Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
595*4a5d661aSToomas Soome "inconsistent bit counts");
596*4a5d661aSToomas Soome Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
597*4a5d661aSToomas Soome
598*4a5d661aSToomas Soome for (n = 0; n <= max_code; n++) {
599*4a5d661aSToomas Soome int len = tree[n].Len;
600*4a5d661aSToomas Soome if (len == 0) continue;
601*4a5d661aSToomas Soome /* Now reverse the bits */
602*4a5d661aSToomas Soome tree[n].Code = bi_reverse(next_code[len]++, len);
603*4a5d661aSToomas Soome
604*4a5d661aSToomas Soome Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
605*4a5d661aSToomas Soome n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
606*4a5d661aSToomas Soome }
607*4a5d661aSToomas Soome }
608*4a5d661aSToomas Soome
609*4a5d661aSToomas Soome /* ===========================================================================
610*4a5d661aSToomas Soome * Construct one Huffman tree and assigns the code bit strings and lengths.
611*4a5d661aSToomas Soome * Update the total bit length for the current block.
612*4a5d661aSToomas Soome * IN assertion: the field freq is set for all tree elements.
613*4a5d661aSToomas Soome * OUT assertions: the fields len and code are set to the optimal bit length
614*4a5d661aSToomas Soome * and corresponding code. The length opt_len is updated; static_len is
615*4a5d661aSToomas Soome * also updated if stree is not null. The field max_code is set.
616*4a5d661aSToomas Soome */
build_tree(s,desc)617*4a5d661aSToomas Soome local void build_tree(s, desc)
618*4a5d661aSToomas Soome deflate_state *s;
619*4a5d661aSToomas Soome tree_desc *desc; /* the tree descriptor */
620*4a5d661aSToomas Soome {
621*4a5d661aSToomas Soome ct_data *tree = desc->dyn_tree;
622*4a5d661aSToomas Soome const ct_data *stree = desc->stat_desc->static_tree;
623*4a5d661aSToomas Soome int elems = desc->stat_desc->elems;
624*4a5d661aSToomas Soome int n, m; /* iterate over heap elements */
625*4a5d661aSToomas Soome int max_code = -1; /* largest code with non zero frequency */
626*4a5d661aSToomas Soome int node; /* new node being created */
627*4a5d661aSToomas Soome
628*4a5d661aSToomas Soome /* Construct the initial heap, with least frequent element in
629*4a5d661aSToomas Soome * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
630*4a5d661aSToomas Soome * heap[0] is not used.
631*4a5d661aSToomas Soome */
632*4a5d661aSToomas Soome s->heap_len = 0, s->heap_max = HEAP_SIZE;
633*4a5d661aSToomas Soome
634*4a5d661aSToomas Soome for (n = 0; n < elems; n++) {
635*4a5d661aSToomas Soome if (tree[n].Freq != 0) {
636*4a5d661aSToomas Soome s->heap[++(s->heap_len)] = max_code = n;
637*4a5d661aSToomas Soome s->depth[n] = 0;
638*4a5d661aSToomas Soome } else {
639*4a5d661aSToomas Soome tree[n].Len = 0;
640*4a5d661aSToomas Soome }
641*4a5d661aSToomas Soome }
642*4a5d661aSToomas Soome
643*4a5d661aSToomas Soome /* The pkzip format requires that at least one distance code exists,
644*4a5d661aSToomas Soome * and that at least one bit should be sent even if there is only one
645*4a5d661aSToomas Soome * possible code. So to avoid special checks later on we force at least
646*4a5d661aSToomas Soome * two codes of non zero frequency.
647*4a5d661aSToomas Soome */
648*4a5d661aSToomas Soome while (s->heap_len < 2) {
649*4a5d661aSToomas Soome node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0);
650*4a5d661aSToomas Soome tree[node].Freq = 1;
651*4a5d661aSToomas Soome s->depth[node] = 0;
652*4a5d661aSToomas Soome s->opt_len--; if (stree) s->static_len -= stree[node].Len;
653*4a5d661aSToomas Soome /* node is 0 or 1 so it does not have extra bits */
654*4a5d661aSToomas Soome }
655*4a5d661aSToomas Soome desc->max_code = max_code;
656*4a5d661aSToomas Soome
657*4a5d661aSToomas Soome /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
658*4a5d661aSToomas Soome * establish sub-heaps of increasing lengths:
659*4a5d661aSToomas Soome */
660*4a5d661aSToomas Soome for (n = s->heap_len/2; n >= 1; n--) pqdownheap(s, tree, n);
661*4a5d661aSToomas Soome
662*4a5d661aSToomas Soome /* Construct the Huffman tree by repeatedly combining the least two
663*4a5d661aSToomas Soome * frequent nodes.
664*4a5d661aSToomas Soome */
665*4a5d661aSToomas Soome node = elems; /* next internal node of the tree */
666*4a5d661aSToomas Soome do {
667*4a5d661aSToomas Soome pqremove(s, tree, n); /* n = node of least frequency */
668*4a5d661aSToomas Soome m = s->heap[SMALLEST]; /* m = node of next least frequency */
669*4a5d661aSToomas Soome
670*4a5d661aSToomas Soome s->heap[--(s->heap_max)] = n; /* keep the nodes sorted by frequency */
671*4a5d661aSToomas Soome s->heap[--(s->heap_max)] = m;
672*4a5d661aSToomas Soome
673*4a5d661aSToomas Soome /* Create a new node father of n and m */
674*4a5d661aSToomas Soome tree[node].Freq = tree[n].Freq + tree[m].Freq;
675*4a5d661aSToomas Soome s->depth[node] = (uch)((s->depth[n] >= s->depth[m] ?
676*4a5d661aSToomas Soome s->depth[n] : s->depth[m]) + 1);
677*4a5d661aSToomas Soome tree[n].Dad = tree[m].Dad = (ush)node;
678*4a5d661aSToomas Soome #ifdef DUMP_BL_TREE
679*4a5d661aSToomas Soome if (tree == s->bl_tree) {
680*4a5d661aSToomas Soome fprintf(stderr,"\nnode %d(%d), sons %d(%d) %d(%d)",
681*4a5d661aSToomas Soome node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq);
682*4a5d661aSToomas Soome }
683*4a5d661aSToomas Soome #endif
684*4a5d661aSToomas Soome /* and insert the new node in the heap */
685*4a5d661aSToomas Soome s->heap[SMALLEST] = node++;
686*4a5d661aSToomas Soome pqdownheap(s, tree, SMALLEST);
687*4a5d661aSToomas Soome
688*4a5d661aSToomas Soome } while (s->heap_len >= 2);
689*4a5d661aSToomas Soome
690*4a5d661aSToomas Soome s->heap[--(s->heap_max)] = s->heap[SMALLEST];
691*4a5d661aSToomas Soome
692*4a5d661aSToomas Soome /* At this point, the fields freq and dad are set. We can now
693*4a5d661aSToomas Soome * generate the bit lengths.
694*4a5d661aSToomas Soome */
695*4a5d661aSToomas Soome gen_bitlen(s, (tree_desc *)desc);
696*4a5d661aSToomas Soome
697*4a5d661aSToomas Soome /* The field len is now set, we can generate the bit codes */
698*4a5d661aSToomas Soome gen_codes ((ct_data *)tree, max_code, s->bl_count);
699*4a5d661aSToomas Soome }
700*4a5d661aSToomas Soome
701*4a5d661aSToomas Soome /* ===========================================================================
702*4a5d661aSToomas Soome * Scan a literal or distance tree to determine the frequencies of the codes
703*4a5d661aSToomas Soome * in the bit length tree.
704*4a5d661aSToomas Soome */
scan_tree(s,tree,max_code)705*4a5d661aSToomas Soome local void scan_tree (s, tree, max_code)
706*4a5d661aSToomas Soome deflate_state *s;
707*4a5d661aSToomas Soome ct_data *tree; /* the tree to be scanned */
708*4a5d661aSToomas Soome int max_code; /* and its largest code of non zero frequency */
709*4a5d661aSToomas Soome {
710*4a5d661aSToomas Soome int n; /* iterates over all tree elements */
711*4a5d661aSToomas Soome int prevlen = -1; /* last emitted length */
712*4a5d661aSToomas Soome int curlen; /* length of current code */
713*4a5d661aSToomas Soome int nextlen = tree[0].Len; /* length of next code */
714*4a5d661aSToomas Soome int count = 0; /* repeat count of the current code */
715*4a5d661aSToomas Soome int max_count = 7; /* max repeat count */
716*4a5d661aSToomas Soome int min_count = 4; /* min repeat count */
717*4a5d661aSToomas Soome
718*4a5d661aSToomas Soome if (nextlen == 0) max_count = 138, min_count = 3;
719*4a5d661aSToomas Soome tree[max_code+1].Len = (ush)0xffff; /* guard */
720*4a5d661aSToomas Soome
721*4a5d661aSToomas Soome for (n = 0; n <= max_code; n++) {
722*4a5d661aSToomas Soome curlen = nextlen; nextlen = tree[n+1].Len;
723*4a5d661aSToomas Soome if (++count < max_count && curlen == nextlen) {
724*4a5d661aSToomas Soome continue;
725*4a5d661aSToomas Soome } else if (count < min_count) {
726*4a5d661aSToomas Soome s->bl_tree[curlen].Freq += count;
727*4a5d661aSToomas Soome } else if (curlen != 0) {
728*4a5d661aSToomas Soome if (curlen != prevlen) s->bl_tree[curlen].Freq++;
729*4a5d661aSToomas Soome s->bl_tree[REP_3_6].Freq++;
730*4a5d661aSToomas Soome } else if (count <= 10) {
731*4a5d661aSToomas Soome s->bl_tree[REPZ_3_10].Freq++;
732*4a5d661aSToomas Soome } else {
733*4a5d661aSToomas Soome s->bl_tree[REPZ_11_138].Freq++;
734*4a5d661aSToomas Soome }
735*4a5d661aSToomas Soome count = 0; prevlen = curlen;
736*4a5d661aSToomas Soome if (nextlen == 0) {
737*4a5d661aSToomas Soome max_count = 138, min_count = 3;
738*4a5d661aSToomas Soome } else if (curlen == nextlen) {
739*4a5d661aSToomas Soome max_count = 6, min_count = 3;
740*4a5d661aSToomas Soome } else {
741*4a5d661aSToomas Soome max_count = 7, min_count = 4;
742*4a5d661aSToomas Soome }
743*4a5d661aSToomas Soome }
744*4a5d661aSToomas Soome }
745*4a5d661aSToomas Soome
746*4a5d661aSToomas Soome /* ===========================================================================
747*4a5d661aSToomas Soome * Send a literal or distance tree in compressed form, using the codes in
748*4a5d661aSToomas Soome * bl_tree.
749*4a5d661aSToomas Soome */
send_tree(s,tree,max_code)750*4a5d661aSToomas Soome local void send_tree (s, tree, max_code)
751*4a5d661aSToomas Soome deflate_state *s;
752*4a5d661aSToomas Soome ct_data *tree; /* the tree to be scanned */
753*4a5d661aSToomas Soome int max_code; /* and its largest code of non zero frequency */
754*4a5d661aSToomas Soome {
755*4a5d661aSToomas Soome int n; /* iterates over all tree elements */
756*4a5d661aSToomas Soome int prevlen = -1; /* last emitted length */
757*4a5d661aSToomas Soome int curlen; /* length of current code */
758*4a5d661aSToomas Soome int nextlen = tree[0].Len; /* length of next code */
759*4a5d661aSToomas Soome int count = 0; /* repeat count of the current code */
760*4a5d661aSToomas Soome int max_count = 7; /* max repeat count */
761*4a5d661aSToomas Soome int min_count = 4; /* min repeat count */
762*4a5d661aSToomas Soome
763*4a5d661aSToomas Soome /* tree[max_code+1].Len = -1; */ /* guard already set */
764*4a5d661aSToomas Soome if (nextlen == 0) max_count = 138, min_count = 3;
765*4a5d661aSToomas Soome
766*4a5d661aSToomas Soome for (n = 0; n <= max_code; n++) {
767*4a5d661aSToomas Soome curlen = nextlen; nextlen = tree[n+1].Len;
768*4a5d661aSToomas Soome if (++count < max_count && curlen == nextlen) {
769*4a5d661aSToomas Soome continue;
770*4a5d661aSToomas Soome } else if (count < min_count) {
771*4a5d661aSToomas Soome do { send_code(s, curlen, s->bl_tree); } while (--count != 0);
772*4a5d661aSToomas Soome
773*4a5d661aSToomas Soome } else if (curlen != 0) {
774*4a5d661aSToomas Soome if (curlen != prevlen) {
775*4a5d661aSToomas Soome send_code(s, curlen, s->bl_tree); count--;
776*4a5d661aSToomas Soome }
777*4a5d661aSToomas Soome Assert(count >= 3 && count <= 6, " 3_6?");
778*4a5d661aSToomas Soome send_code(s, REP_3_6, s->bl_tree); send_bits(s, count-3, 2);
779*4a5d661aSToomas Soome
780*4a5d661aSToomas Soome } else if (count <= 10) {
781*4a5d661aSToomas Soome send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count-3, 3);
782*4a5d661aSToomas Soome
783*4a5d661aSToomas Soome } else {
784*4a5d661aSToomas Soome send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count-11, 7);
785*4a5d661aSToomas Soome }
786*4a5d661aSToomas Soome count = 0; prevlen = curlen;
787*4a5d661aSToomas Soome if (nextlen == 0) {
788*4a5d661aSToomas Soome max_count = 138, min_count = 3;
789*4a5d661aSToomas Soome } else if (curlen == nextlen) {
790*4a5d661aSToomas Soome max_count = 6, min_count = 3;
791*4a5d661aSToomas Soome } else {
792*4a5d661aSToomas Soome max_count = 7, min_count = 4;
793*4a5d661aSToomas Soome }
794*4a5d661aSToomas Soome }
795*4a5d661aSToomas Soome }
796*4a5d661aSToomas Soome
797*4a5d661aSToomas Soome /* ===========================================================================
798*4a5d661aSToomas Soome * Construct the Huffman tree for the bit lengths and return the index in
799*4a5d661aSToomas Soome * bl_order of the last bit length code to send.
800*4a5d661aSToomas Soome */
build_bl_tree(s)801*4a5d661aSToomas Soome local int build_bl_tree(s)
802*4a5d661aSToomas Soome deflate_state *s;
803*4a5d661aSToomas Soome {
804*4a5d661aSToomas Soome int max_blindex; /* index of last bit length code of non zero freq */
805*4a5d661aSToomas Soome
806*4a5d661aSToomas Soome /* Determine the bit length frequencies for literal and distance trees */
807*4a5d661aSToomas Soome scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code);
808*4a5d661aSToomas Soome scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code);
809*4a5d661aSToomas Soome
810*4a5d661aSToomas Soome /* Build the bit length tree: */
811*4a5d661aSToomas Soome build_tree(s, (tree_desc *)(&(s->bl_desc)));
812*4a5d661aSToomas Soome /* opt_len now includes the length of the tree representations, except
813*4a5d661aSToomas Soome * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
814*4a5d661aSToomas Soome */
815*4a5d661aSToomas Soome
816*4a5d661aSToomas Soome /* Determine the number of bit length codes to send. The pkzip format
817*4a5d661aSToomas Soome * requires that at least 4 bit length codes be sent. (appnote.txt says
818*4a5d661aSToomas Soome * 3 but the actual value used is 4.)
819*4a5d661aSToomas Soome */
820*4a5d661aSToomas Soome for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
821*4a5d661aSToomas Soome if (s->bl_tree[bl_order[max_blindex]].Len != 0) break;
822*4a5d661aSToomas Soome }
823*4a5d661aSToomas Soome /* Update opt_len to include the bit length tree and counts */
824*4a5d661aSToomas Soome s->opt_len += 3*(max_blindex+1) + 5+5+4;
825*4a5d661aSToomas Soome Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
826*4a5d661aSToomas Soome s->opt_len, s->static_len));
827*4a5d661aSToomas Soome
828*4a5d661aSToomas Soome return max_blindex;
829*4a5d661aSToomas Soome }
830*4a5d661aSToomas Soome
831*4a5d661aSToomas Soome /* ===========================================================================
832*4a5d661aSToomas Soome * Send the header for a block using dynamic Huffman trees: the counts, the
833*4a5d661aSToomas Soome * lengths of the bit length codes, the literal tree and the distance tree.
834*4a5d661aSToomas Soome * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
835*4a5d661aSToomas Soome */
send_all_trees(s,lcodes,dcodes,blcodes)836*4a5d661aSToomas Soome local void send_all_trees(s, lcodes, dcodes, blcodes)
837*4a5d661aSToomas Soome deflate_state *s;
838*4a5d661aSToomas Soome int lcodes, dcodes, blcodes; /* number of codes for each tree */
839*4a5d661aSToomas Soome {
840*4a5d661aSToomas Soome int rank; /* index in bl_order */
841*4a5d661aSToomas Soome
842*4a5d661aSToomas Soome Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
843*4a5d661aSToomas Soome Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
844*4a5d661aSToomas Soome "too many codes");
845*4a5d661aSToomas Soome Tracev((stderr, "\nbl counts: "));
846*4a5d661aSToomas Soome send_bits(s, lcodes-257, 5); /* not +255 as stated in appnote.txt */
847*4a5d661aSToomas Soome send_bits(s, dcodes-1, 5);
848*4a5d661aSToomas Soome send_bits(s, blcodes-4, 4); /* not -3 as stated in appnote.txt */
849*4a5d661aSToomas Soome for (rank = 0; rank < blcodes; rank++) {
850*4a5d661aSToomas Soome Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
851*4a5d661aSToomas Soome send_bits(s, s->bl_tree[bl_order[rank]].Len, 3);
852*4a5d661aSToomas Soome }
853*4a5d661aSToomas Soome Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
854*4a5d661aSToomas Soome
855*4a5d661aSToomas Soome send_tree(s, (ct_data *)s->dyn_ltree, lcodes-1); /* literal tree */
856*4a5d661aSToomas Soome Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
857*4a5d661aSToomas Soome
858*4a5d661aSToomas Soome send_tree(s, (ct_data *)s->dyn_dtree, dcodes-1); /* distance tree */
859*4a5d661aSToomas Soome Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
860*4a5d661aSToomas Soome }
861*4a5d661aSToomas Soome
862*4a5d661aSToomas Soome /* ===========================================================================
863*4a5d661aSToomas Soome * Send a stored block
864*4a5d661aSToomas Soome */
_tr_stored_block(s,buf,stored_len,last)865*4a5d661aSToomas Soome void ZLIB_INTERNAL _tr_stored_block(s, buf, stored_len, last)
866*4a5d661aSToomas Soome deflate_state *s;
867*4a5d661aSToomas Soome charf *buf; /* input block */
868*4a5d661aSToomas Soome ulg stored_len; /* length of input block */
869*4a5d661aSToomas Soome int last; /* one if this is the last block for a file */
870*4a5d661aSToomas Soome {
871*4a5d661aSToomas Soome send_bits(s, (STORED_BLOCK<<1)+last, 3); /* send block type */
872*4a5d661aSToomas Soome #ifdef DEBUG
873*4a5d661aSToomas Soome s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L;
874*4a5d661aSToomas Soome s->compressed_len += (stored_len + 4) << 3;
875*4a5d661aSToomas Soome #endif
876*4a5d661aSToomas Soome copy_block(s, buf, (unsigned)stored_len, 1); /* with header */
877*4a5d661aSToomas Soome }
878*4a5d661aSToomas Soome
879*4a5d661aSToomas Soome /* ===========================================================================
880*4a5d661aSToomas Soome * Flush the bits in the bit buffer to pending output (leaves at most 7 bits)
881*4a5d661aSToomas Soome */
_tr_flush_bits(s)882*4a5d661aSToomas Soome void ZLIB_INTERNAL _tr_flush_bits(s)
883*4a5d661aSToomas Soome deflate_state *s;
884*4a5d661aSToomas Soome {
885*4a5d661aSToomas Soome bi_flush(s);
886*4a5d661aSToomas Soome }
887*4a5d661aSToomas Soome
888*4a5d661aSToomas Soome /* ===========================================================================
889*4a5d661aSToomas Soome * Send one empty static block to give enough lookahead for inflate.
890*4a5d661aSToomas Soome * This takes 10 bits, of which 7 may remain in the bit buffer.
891*4a5d661aSToomas Soome */
_tr_align(s)892*4a5d661aSToomas Soome void ZLIB_INTERNAL _tr_align(s)
893*4a5d661aSToomas Soome deflate_state *s;
894*4a5d661aSToomas Soome {
895*4a5d661aSToomas Soome send_bits(s, STATIC_TREES<<1, 3);
896*4a5d661aSToomas Soome send_code(s, END_BLOCK, static_ltree);
897*4a5d661aSToomas Soome #ifdef DEBUG
898*4a5d661aSToomas Soome s->compressed_len += 10L; /* 3 for block type, 7 for EOB */
899*4a5d661aSToomas Soome #endif
900*4a5d661aSToomas Soome bi_flush(s);
901*4a5d661aSToomas Soome }
902*4a5d661aSToomas Soome
903*4a5d661aSToomas Soome /* ===========================================================================
904*4a5d661aSToomas Soome * Determine the best encoding for the current block: dynamic trees, static
905*4a5d661aSToomas Soome * trees or store, and output the encoded block to the zip file.
906*4a5d661aSToomas Soome */
_tr_flush_block(s,buf,stored_len,last)907*4a5d661aSToomas Soome void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last)
908*4a5d661aSToomas Soome deflate_state *s;
909*4a5d661aSToomas Soome charf *buf; /* input block, or NULL if too old */
910*4a5d661aSToomas Soome ulg stored_len; /* length of input block */
911*4a5d661aSToomas Soome int last; /* one if this is the last block for a file */
912*4a5d661aSToomas Soome {
913*4a5d661aSToomas Soome ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
914*4a5d661aSToomas Soome int max_blindex = 0; /* index of last bit length code of non zero freq */
915*4a5d661aSToomas Soome
916*4a5d661aSToomas Soome /* Build the Huffman trees unless a stored block is forced */
917*4a5d661aSToomas Soome if (s->level > 0) {
918*4a5d661aSToomas Soome
919*4a5d661aSToomas Soome /* Check if the file is binary or text */
920*4a5d661aSToomas Soome if (s->strm->data_type == Z_UNKNOWN)
921*4a5d661aSToomas Soome s->strm->data_type = detect_data_type(s);
922*4a5d661aSToomas Soome
923*4a5d661aSToomas Soome /* Construct the literal and distance trees */
924*4a5d661aSToomas Soome build_tree(s, (tree_desc *)(&(s->l_desc)));
925*4a5d661aSToomas Soome Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
926*4a5d661aSToomas Soome s->static_len));
927*4a5d661aSToomas Soome
928*4a5d661aSToomas Soome build_tree(s, (tree_desc *)(&(s->d_desc)));
929*4a5d661aSToomas Soome Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
930*4a5d661aSToomas Soome s->static_len));
931*4a5d661aSToomas Soome /* At this point, opt_len and static_len are the total bit lengths of
932*4a5d661aSToomas Soome * the compressed block data, excluding the tree representations.
933*4a5d661aSToomas Soome */
934*4a5d661aSToomas Soome
935*4a5d661aSToomas Soome /* Build the bit length tree for the above two trees, and get the index
936*4a5d661aSToomas Soome * in bl_order of the last bit length code to send.
937*4a5d661aSToomas Soome */
938*4a5d661aSToomas Soome max_blindex = build_bl_tree(s);
939*4a5d661aSToomas Soome
940*4a5d661aSToomas Soome /* Determine the best encoding. Compute the block lengths in bytes. */
941*4a5d661aSToomas Soome opt_lenb = (s->opt_len+3+7)>>3;
942*4a5d661aSToomas Soome static_lenb = (s->static_len+3+7)>>3;
943*4a5d661aSToomas Soome
944*4a5d661aSToomas Soome Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
945*4a5d661aSToomas Soome opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
946*4a5d661aSToomas Soome s->last_lit));
947*4a5d661aSToomas Soome
948*4a5d661aSToomas Soome if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
949*4a5d661aSToomas Soome
950*4a5d661aSToomas Soome } else {
951*4a5d661aSToomas Soome Assert(buf != (char*)0, "lost buf");
952*4a5d661aSToomas Soome opt_lenb = static_lenb = stored_len + 5; /* force a stored block */
953*4a5d661aSToomas Soome }
954*4a5d661aSToomas Soome
955*4a5d661aSToomas Soome #ifdef FORCE_STORED
956*4a5d661aSToomas Soome if (buf != (char*)0) { /* force stored block */
957*4a5d661aSToomas Soome #else
958*4a5d661aSToomas Soome if (stored_len+4 <= opt_lenb && buf != (char*)0) {
959*4a5d661aSToomas Soome /* 4: two words for the lengths */
960*4a5d661aSToomas Soome #endif
961*4a5d661aSToomas Soome /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
962*4a5d661aSToomas Soome * Otherwise we can't have processed more than WSIZE input bytes since
963*4a5d661aSToomas Soome * the last block flush, because compression would have been
964*4a5d661aSToomas Soome * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
965*4a5d661aSToomas Soome * transform a block into a stored block.
966*4a5d661aSToomas Soome */
967*4a5d661aSToomas Soome _tr_stored_block(s, buf, stored_len, last);
968*4a5d661aSToomas Soome
969*4a5d661aSToomas Soome #ifdef FORCE_STATIC
970*4a5d661aSToomas Soome } else if (static_lenb >= 0) { /* force static trees */
971*4a5d661aSToomas Soome #else
972*4a5d661aSToomas Soome } else if (s->strategy == Z_FIXED || static_lenb == opt_lenb) {
973*4a5d661aSToomas Soome #endif
974*4a5d661aSToomas Soome send_bits(s, (STATIC_TREES<<1)+last, 3);
975*4a5d661aSToomas Soome compress_block(s, (const ct_data *)static_ltree,
976*4a5d661aSToomas Soome (const ct_data *)static_dtree);
977*4a5d661aSToomas Soome #ifdef DEBUG
978*4a5d661aSToomas Soome s->compressed_len += 3 + s->static_len;
979*4a5d661aSToomas Soome #endif
980*4a5d661aSToomas Soome } else {
981*4a5d661aSToomas Soome send_bits(s, (DYN_TREES<<1)+last, 3);
982*4a5d661aSToomas Soome send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1,
983*4a5d661aSToomas Soome max_blindex+1);
984*4a5d661aSToomas Soome compress_block(s, (const ct_data *)s->dyn_ltree,
985*4a5d661aSToomas Soome (const ct_data *)s->dyn_dtree);
986*4a5d661aSToomas Soome #ifdef DEBUG
987*4a5d661aSToomas Soome s->compressed_len += 3 + s->opt_len;
988*4a5d661aSToomas Soome #endif
989*4a5d661aSToomas Soome }
990*4a5d661aSToomas Soome Assert (s->compressed_len == s->bits_sent, "bad compressed size");
991*4a5d661aSToomas Soome /* The above check is made mod 2^32, for files larger than 512 MB
992*4a5d661aSToomas Soome * and uLong implemented on 32 bits.
993*4a5d661aSToomas Soome */
994*4a5d661aSToomas Soome init_block(s);
995*4a5d661aSToomas Soome
996*4a5d661aSToomas Soome if (last) {
997*4a5d661aSToomas Soome bi_windup(s);
998*4a5d661aSToomas Soome #ifdef DEBUG
999*4a5d661aSToomas Soome s->compressed_len += 7; /* align on byte boundary */
1000*4a5d661aSToomas Soome #endif
1001*4a5d661aSToomas Soome }
1002*4a5d661aSToomas Soome Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
1003*4a5d661aSToomas Soome s->compressed_len-7*last));
1004*4a5d661aSToomas Soome }
1005*4a5d661aSToomas Soome
1006*4a5d661aSToomas Soome /* ===========================================================================
1007*4a5d661aSToomas Soome * Save the match info and tally the frequency counts. Return true if
1008*4a5d661aSToomas Soome * the current block must be flushed.
1009*4a5d661aSToomas Soome */
_tr_tally(s,dist,lc)1010*4a5d661aSToomas Soome int ZLIB_INTERNAL _tr_tally (s, dist, lc)
1011*4a5d661aSToomas Soome deflate_state *s;
1012*4a5d661aSToomas Soome unsigned dist; /* distance of matched string */
1013*4a5d661aSToomas Soome unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */
1014*4a5d661aSToomas Soome {
1015*4a5d661aSToomas Soome s->d_buf[s->last_lit] = (ush)dist;
1016*4a5d661aSToomas Soome s->l_buf[s->last_lit++] = (uch)lc;
1017*4a5d661aSToomas Soome if (dist == 0) {
1018*4a5d661aSToomas Soome /* lc is the unmatched char */
1019*4a5d661aSToomas Soome s->dyn_ltree[lc].Freq++;
1020*4a5d661aSToomas Soome } else {
1021*4a5d661aSToomas Soome s->matches++;
1022*4a5d661aSToomas Soome /* Here, lc is the match length - MIN_MATCH */
1023*4a5d661aSToomas Soome dist--; /* dist = match distance - 1 */
1024*4a5d661aSToomas Soome Assert((ush)dist < (ush)MAX_DIST(s) &&
1025*4a5d661aSToomas Soome (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
1026*4a5d661aSToomas Soome (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match");
1027*4a5d661aSToomas Soome
1028*4a5d661aSToomas Soome s->dyn_ltree[_length_code[lc]+LITERALS+1].Freq++;
1029*4a5d661aSToomas Soome s->dyn_dtree[d_code(dist)].Freq++;
1030*4a5d661aSToomas Soome }
1031*4a5d661aSToomas Soome
1032*4a5d661aSToomas Soome #ifdef TRUNCATE_BLOCK
1033*4a5d661aSToomas Soome /* Try to guess if it is profitable to stop the current block here */
1034*4a5d661aSToomas Soome if ((s->last_lit & 0x1fff) == 0 && s->level > 2) {
1035*4a5d661aSToomas Soome /* Compute an upper bound for the compressed length */
1036*4a5d661aSToomas Soome ulg out_length = (ulg)s->last_lit*8L;
1037*4a5d661aSToomas Soome ulg in_length = (ulg)((long)s->strstart - s->block_start);
1038*4a5d661aSToomas Soome int dcode;
1039*4a5d661aSToomas Soome for (dcode = 0; dcode < D_CODES; dcode++) {
1040*4a5d661aSToomas Soome out_length += (ulg)s->dyn_dtree[dcode].Freq *
1041*4a5d661aSToomas Soome (5L+extra_dbits[dcode]);
1042*4a5d661aSToomas Soome }
1043*4a5d661aSToomas Soome out_length >>= 3;
1044*4a5d661aSToomas Soome Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
1045*4a5d661aSToomas Soome s->last_lit, in_length, out_length,
1046*4a5d661aSToomas Soome 100L - out_length*100L/in_length));
1047*4a5d661aSToomas Soome if (s->matches < s->last_lit/2 && out_length < in_length/2) return 1;
1048*4a5d661aSToomas Soome }
1049*4a5d661aSToomas Soome #endif
1050*4a5d661aSToomas Soome return (s->last_lit == s->lit_bufsize-1);
1051*4a5d661aSToomas Soome /* We avoid equality with lit_bufsize because of wraparound at 64K
1052*4a5d661aSToomas Soome * on 16 bit machines and because stored blocks are restricted to
1053*4a5d661aSToomas Soome * 64K-1 bytes.
1054*4a5d661aSToomas Soome */
1055*4a5d661aSToomas Soome }
1056*4a5d661aSToomas Soome
1057*4a5d661aSToomas Soome /* ===========================================================================
1058*4a5d661aSToomas Soome * Send the block data compressed using the given Huffman trees
1059*4a5d661aSToomas Soome */
compress_block(s,ltree,dtree)1060*4a5d661aSToomas Soome local void compress_block(s, ltree, dtree)
1061*4a5d661aSToomas Soome deflate_state *s;
1062*4a5d661aSToomas Soome const ct_data *ltree; /* literal tree */
1063*4a5d661aSToomas Soome const ct_data *dtree; /* distance tree */
1064*4a5d661aSToomas Soome {
1065*4a5d661aSToomas Soome unsigned dist; /* distance of matched string */
1066*4a5d661aSToomas Soome int lc; /* match length or unmatched char (if dist == 0) */
1067*4a5d661aSToomas Soome unsigned lx = 0; /* running index in l_buf */
1068*4a5d661aSToomas Soome unsigned code; /* the code to send */
1069*4a5d661aSToomas Soome int extra; /* number of extra bits to send */
1070*4a5d661aSToomas Soome
1071*4a5d661aSToomas Soome if (s->last_lit != 0) do {
1072*4a5d661aSToomas Soome dist = s->d_buf[lx];
1073*4a5d661aSToomas Soome lc = s->l_buf[lx++];
1074*4a5d661aSToomas Soome if (dist == 0) {
1075*4a5d661aSToomas Soome send_code(s, lc, ltree); /* send a literal byte */
1076*4a5d661aSToomas Soome Tracecv(isgraph(lc), (stderr," '%c' ", lc));
1077*4a5d661aSToomas Soome } else {
1078*4a5d661aSToomas Soome /* Here, lc is the match length - MIN_MATCH */
1079*4a5d661aSToomas Soome code = _length_code[lc];
1080*4a5d661aSToomas Soome send_code(s, code+LITERALS+1, ltree); /* send the length code */
1081*4a5d661aSToomas Soome extra = extra_lbits[code];
1082*4a5d661aSToomas Soome if (extra != 0) {
1083*4a5d661aSToomas Soome lc -= base_length[code];
1084*4a5d661aSToomas Soome send_bits(s, lc, extra); /* send the extra length bits */
1085*4a5d661aSToomas Soome }
1086*4a5d661aSToomas Soome dist--; /* dist is now the match distance - 1 */
1087*4a5d661aSToomas Soome code = d_code(dist);
1088*4a5d661aSToomas Soome Assert (code < D_CODES, "bad d_code");
1089*4a5d661aSToomas Soome
1090*4a5d661aSToomas Soome send_code(s, code, dtree); /* send the distance code */
1091*4a5d661aSToomas Soome extra = extra_dbits[code];
1092*4a5d661aSToomas Soome if (extra != 0) {
1093*4a5d661aSToomas Soome dist -= base_dist[code];
1094*4a5d661aSToomas Soome send_bits(s, dist, extra); /* send the extra distance bits */
1095*4a5d661aSToomas Soome }
1096*4a5d661aSToomas Soome } /* literal or match pair ? */
1097*4a5d661aSToomas Soome
1098*4a5d661aSToomas Soome /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */
1099*4a5d661aSToomas Soome Assert((uInt)(s->pending) < s->lit_bufsize + 2*lx,
1100*4a5d661aSToomas Soome "pendingBuf overflow");
1101*4a5d661aSToomas Soome
1102*4a5d661aSToomas Soome } while (lx < s->last_lit);
1103*4a5d661aSToomas Soome
1104*4a5d661aSToomas Soome send_code(s, END_BLOCK, ltree);
1105*4a5d661aSToomas Soome }
1106*4a5d661aSToomas Soome
1107*4a5d661aSToomas Soome /* ===========================================================================
1108*4a5d661aSToomas Soome * Check if the data type is TEXT or BINARY, using the following algorithm:
1109*4a5d661aSToomas Soome * - TEXT if the two conditions below are satisfied:
1110*4a5d661aSToomas Soome * a) There are no non-portable control characters belonging to the
1111*4a5d661aSToomas Soome * "black list" (0..6, 14..25, 28..31).
1112*4a5d661aSToomas Soome * b) There is at least one printable character belonging to the
1113*4a5d661aSToomas Soome * "white list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255).
1114*4a5d661aSToomas Soome * - BINARY otherwise.
1115*4a5d661aSToomas Soome * - The following partially-portable control characters form a
1116*4a5d661aSToomas Soome * "gray list" that is ignored in this detection algorithm:
1117*4a5d661aSToomas Soome * (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}).
1118*4a5d661aSToomas Soome * IN assertion: the fields Freq of dyn_ltree are set.
1119*4a5d661aSToomas Soome */
detect_data_type(s)1120*4a5d661aSToomas Soome local int detect_data_type(s)
1121*4a5d661aSToomas Soome deflate_state *s;
1122*4a5d661aSToomas Soome {
1123*4a5d661aSToomas Soome /* black_mask is the bit mask of black-listed bytes
1124*4a5d661aSToomas Soome * set bits 0..6, 14..25, and 28..31
1125*4a5d661aSToomas Soome * 0xf3ffc07f = binary 11110011111111111100000001111111
1126*4a5d661aSToomas Soome */
1127*4a5d661aSToomas Soome unsigned long black_mask = 0xf3ffc07fUL;
1128*4a5d661aSToomas Soome int n;
1129*4a5d661aSToomas Soome
1130*4a5d661aSToomas Soome /* Check for non-textual ("black-listed") bytes. */
1131*4a5d661aSToomas Soome for (n = 0; n <= 31; n++, black_mask >>= 1)
1132*4a5d661aSToomas Soome if ((black_mask & 1) && (s->dyn_ltree[n].Freq != 0))
1133*4a5d661aSToomas Soome return Z_BINARY;
1134*4a5d661aSToomas Soome
1135*4a5d661aSToomas Soome /* Check for textual ("white-listed") bytes. */
1136*4a5d661aSToomas Soome if (s->dyn_ltree[9].Freq != 0 || s->dyn_ltree[10].Freq != 0
1137*4a5d661aSToomas Soome || s->dyn_ltree[13].Freq != 0)
1138*4a5d661aSToomas Soome return Z_TEXT;
1139*4a5d661aSToomas Soome for (n = 32; n < LITERALS; n++)
1140*4a5d661aSToomas Soome if (s->dyn_ltree[n].Freq != 0)
1141*4a5d661aSToomas Soome return Z_TEXT;
1142*4a5d661aSToomas Soome
1143*4a5d661aSToomas Soome /* There are no "black-listed" or "white-listed" bytes:
1144*4a5d661aSToomas Soome * this stream either is empty or has tolerated ("gray-listed") bytes only.
1145*4a5d661aSToomas Soome */
1146*4a5d661aSToomas Soome return Z_BINARY;
1147*4a5d661aSToomas Soome }
1148*4a5d661aSToomas Soome
1149*4a5d661aSToomas Soome /* ===========================================================================
1150*4a5d661aSToomas Soome * Reverse the first len bits of a code, using straightforward code (a faster
1151*4a5d661aSToomas Soome * method would use a table)
1152*4a5d661aSToomas Soome * IN assertion: 1 <= len <= 15
1153*4a5d661aSToomas Soome */
bi_reverse(code,len)1154*4a5d661aSToomas Soome local unsigned bi_reverse(code, len)
1155*4a5d661aSToomas Soome unsigned code; /* the value to invert */
1156*4a5d661aSToomas Soome int len; /* its bit length */
1157*4a5d661aSToomas Soome {
1158*4a5d661aSToomas Soome register unsigned res = 0;
1159*4a5d661aSToomas Soome do {
1160*4a5d661aSToomas Soome res |= code & 1;
1161*4a5d661aSToomas Soome code >>= 1, res <<= 1;
1162*4a5d661aSToomas Soome } while (--len > 0);
1163*4a5d661aSToomas Soome return res >> 1;
1164*4a5d661aSToomas Soome }
1165*4a5d661aSToomas Soome
1166*4a5d661aSToomas Soome /* ===========================================================================
1167*4a5d661aSToomas Soome * Flush the bit buffer, keeping at most 7 bits in it.
1168*4a5d661aSToomas Soome */
bi_flush(s)1169*4a5d661aSToomas Soome local void bi_flush(s)
1170*4a5d661aSToomas Soome deflate_state *s;
1171*4a5d661aSToomas Soome {
1172*4a5d661aSToomas Soome if (s->bi_valid == 16) {
1173*4a5d661aSToomas Soome put_short(s, s->bi_buf);
1174*4a5d661aSToomas Soome s->bi_buf = 0;
1175*4a5d661aSToomas Soome s->bi_valid = 0;
1176*4a5d661aSToomas Soome } else if (s->bi_valid >= 8) {
1177*4a5d661aSToomas Soome put_byte(s, (Byte)s->bi_buf);
1178*4a5d661aSToomas Soome s->bi_buf >>= 8;
1179*4a5d661aSToomas Soome s->bi_valid -= 8;
1180*4a5d661aSToomas Soome }
1181*4a5d661aSToomas Soome }
1182*4a5d661aSToomas Soome
1183*4a5d661aSToomas Soome /* ===========================================================================
1184*4a5d661aSToomas Soome * Flush the bit buffer and align the output on a byte boundary
1185*4a5d661aSToomas Soome */
bi_windup(s)1186*4a5d661aSToomas Soome local void bi_windup(s)
1187*4a5d661aSToomas Soome deflate_state *s;
1188*4a5d661aSToomas Soome {
1189*4a5d661aSToomas Soome if (s->bi_valid > 8) {
1190*4a5d661aSToomas Soome put_short(s, s->bi_buf);
1191*4a5d661aSToomas Soome } else if (s->bi_valid > 0) {
1192*4a5d661aSToomas Soome put_byte(s, (Byte)s->bi_buf);
1193*4a5d661aSToomas Soome }
1194*4a5d661aSToomas Soome s->bi_buf = 0;
1195*4a5d661aSToomas Soome s->bi_valid = 0;
1196*4a5d661aSToomas Soome #ifdef DEBUG
1197*4a5d661aSToomas Soome s->bits_sent = (s->bits_sent+7) & ~7;
1198*4a5d661aSToomas Soome #endif
1199*4a5d661aSToomas Soome }
1200*4a5d661aSToomas Soome
1201*4a5d661aSToomas Soome /* ===========================================================================
1202*4a5d661aSToomas Soome * Copy a stored block, storing first the length and its
1203*4a5d661aSToomas Soome * one's complement if requested.
1204*4a5d661aSToomas Soome */
copy_block(s,buf,len,header)1205*4a5d661aSToomas Soome local void copy_block(s, buf, len, header)
1206*4a5d661aSToomas Soome deflate_state *s;
1207*4a5d661aSToomas Soome charf *buf; /* the input data */
1208*4a5d661aSToomas Soome unsigned len; /* its length */
1209*4a5d661aSToomas Soome int header; /* true if block header must be written */
1210*4a5d661aSToomas Soome {
1211*4a5d661aSToomas Soome bi_windup(s); /* align on byte boundary */
1212*4a5d661aSToomas Soome
1213*4a5d661aSToomas Soome if (header) {
1214*4a5d661aSToomas Soome put_short(s, (ush)len);
1215*4a5d661aSToomas Soome put_short(s, (ush)~len);
1216*4a5d661aSToomas Soome #ifdef DEBUG
1217*4a5d661aSToomas Soome s->bits_sent += 2*16;
1218*4a5d661aSToomas Soome #endif
1219*4a5d661aSToomas Soome }
1220*4a5d661aSToomas Soome #ifdef DEBUG
1221*4a5d661aSToomas Soome s->bits_sent += (ulg)len<<3;
1222*4a5d661aSToomas Soome #endif
1223*4a5d661aSToomas Soome while (len--) {
1224*4a5d661aSToomas Soome put_byte(s, *buf++);
1225*4a5d661aSToomas Soome }
1226*4a5d661aSToomas Soome }
1227