source: svn/newcon3bcm2_21bu/dst/dlib/src/ZLIB/trees.c

Last change on this file was 76, checked in by megakiss, 10 years ago

1W 대기전력을 만족시키기 위하여 POWEROFF시 튜너를 Standby 상태로 함

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