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