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