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