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