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