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