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