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