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