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