Main Page | Class List | Directories | File List | Class Members | File Members

trees.c File Reference

#include <config.h>
#include <ctype.h>
#include "tailor.h"
#include "gzip.h"

Go to the source code of this file.

Classes

struct  ct_data
struct  tree_desc

Defines

#define MAX_BITS   15
#define MAX_BL_BITS   7
#define LENGTH_CODES   29
#define LITERALS   256
#define END_BLOCK   256
#define L_CODES   (LITERALS+1+LENGTH_CODES)
#define D_CODES   30
#define BL_CODES   19
#define STORED_BLOCK   0
#define STATIC_TREES   1
#define DYN_TREES   2
#define LIT_BUFSIZE   0x8000
#define REP_3_6   16
#define REPZ_3_10   17
#define REPZ_11_138   18
#define Freq   fc.freq
#define Code   fc.code
#define Dad   dl.dad
#define Len   dl.len
#define HEAP_SIZE   (2*L_CODES+1)
#define l_buf   inbuf
#define send_code(c, tree)   send_bits(tree[c].Code, tree[c].Len)
#define d_code(dist)   ((dist) < 256 ? dist_code[dist] : dist_code[256+((dist)>>7)])
#define MAX(a, b)   (a >= b ? a : b)
#define SMALLEST   1
#define pqremove(tree, top)
#define smaller(tree, n, m)

Typedefs

typedef ct_data ct_data
typedef tree_desc tree_desc

Functions

local void init_block OF ((void))
void ct_init (ush *attr, int *methodp)
local void init_block ()
local void pqdownheap (ct_data near *tree, int k)
local void gen_bitlen (tree_desc near *desc)
local void gen_codes (ct_data near *tree, int max_code)
local void build_tree (tree_desc near *desc)
local void scan_tree (ct_data near *tree, int max_code)
local void send_tree (ct_data near *tree, int max_code)
local int build_bl_tree ()
local void send_all_trees (int lcodes, int dcodes, int blcodes)
off_t flush_block (char *buf, ulg stored_len, int eof)
int ct_tally (int dist, int lc)
local void compress_block (ct_data near *ltree, ct_data near *dtree)
local void set_file_type ()

Variables

local int near extra_lbits [LENGTH_CODES] = {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}
local int near extra_dbits [D_CODES] = {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}
local int near extra_blbits [BL_CODES] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7}
local ct_data near dyn_ltree [HEAP_SIZE]
local ct_data near dyn_dtree [2 *D_CODES+1]
local ct_data near static_ltree [L_CODES+2]
local ct_data near static_dtree [D_CODES]
local ct_data near bl_tree [2 *BL_CODES+1]
local tree_desc near l_desc
local tree_desc near d_desc
local tree_desc near bl_desc
local ush near bl_count [MAX_BITS+1]
local uch near bl_order [BL_CODES] = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15}
local int near heap [2 *L_CODES+1]
local int heap_len
local int heap_max
local uch near depth [2 *L_CODES+1]
local uch length_code [MAX_MATCH-MIN_MATCH+1]
local uch dist_code [512]
local int near base_length [LENGTH_CODES]
local int near base_dist [D_CODES]
local uch near flag_buf [(LIT_BUFSIZE/8)]
local unsigned last_lit
local unsigned last_dist
local unsigned last_flags
local uch flags
local uch flag_bit
local ulg opt_len
local ulg static_len
local off_t compressed_len
local off_t input_len
ushfile_type
int * file_method
long block_start
unsigned near strstart


Define Documentation

#define BL_CODES   19
 

Definition at line 104 of file trees.c.

Referenced by build_bl_tree(), init_block(), and send_all_trees().

#define Code   fc.code
 

Definition at line 186 of file trees.c.

Referenced by gen_codes(), and longest_match().

#define d_code dist   )     ((dist) < 256 ? dist_code[dist] : dist_code[256+((dist)>>7)])
 

Definition at line 330 of file trees.c.

Referenced by compress_block(), and ct_tally().

#define D_CODES   30
 

Definition at line 101 of file trees.c.

Referenced by compress_block(), ct_init(), ct_tally(), init_block(), and send_all_trees().

#define Dad   dl.dad
 

Definition at line 187 of file trees.c.

#define DYN_TREES   2
 

Definition at line 119 of file trees.c.

Referenced by flush_block().

#define END_BLOCK   256
 

Definition at line 95 of file trees.c.

Referenced by compress_block(), and init_block().

#define Freq   fc.freq
 

Definition at line 185 of file trees.c.

Referenced by build_tree().

#define HEAP_SIZE   (2*L_CODES+1)
 

Definition at line 190 of file trees.c.

Referenced by build_tree(), and gen_bitlen().

#define l_buf   inbuf
 

Definition at line 265 of file trees.c.

Referenced by compress_block(), and ct_tally().

#define L_CODES   (LITERALS+1+LENGTH_CODES)
 

Definition at line 98 of file trees.c.

Referenced by ct_init(), init_block(), and send_all_trees().

#define Len   dl.len
 

Definition at line 188 of file trees.c.

Referenced by build_bl_tree(), ct_init(), gen_bitlen(), and send_all_trees().

#define LENGTH_CODES   29
 

Definition at line 89 of file trees.c.

Referenced by ct_init().

#define LIT_BUFSIZE   0x8000
 

Definition at line 129 of file trees.c.

Referenced by ct_tally().

#define LITERALS   256
 

Definition at line 92 of file trees.c.

Referenced by compress_block(), ct_tally(), read_tree(), and set_file_type().

#define MAX a,
 )     (a >= b ? a : b)
 

Definition at line 337 of file trees.c.

Referenced by build_tree().

#define MAX_BITS   15
 

Definition at line 83 of file trees.c.

Referenced by ct_init(), gen_bitlen(), and gen_codes().

#define MAX_BL_BITS   7
 

Definition at line 86 of file trees.c.

#define pqremove tree,
top   ) 
 

Value:

{\
    top = heap[SMALLEST]; \
    heap[SMALLEST] = heap[heap_len--]; \
    pqdownheap(tree, SMALLEST); \
}

Definition at line 443 of file trees.c.

Referenced by build_tree().

#define REP_3_6   16
 

Definition at line 160 of file trees.c.

Referenced by scan_tree(), and send_tree().

#define REPZ_11_138   18
 

Definition at line 166 of file trees.c.

Referenced by scan_tree(), and send_tree().

#define REPZ_3_10   17
 

Definition at line 163 of file trees.c.

Referenced by scan_tree(), and send_tree().

#define send_code c,
tree   )     send_bits(tree[c].Code, tree[c].Len)
 

Definition at line 321 of file trees.c.

Referenced by compress_block(), and send_tree().

#define smaller tree,
n,
 ) 
 

Value:

(tree[n].Freq < tree[m].Freq || \
   (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m]))

Definition at line 454 of file trees.c.

Referenced by pqdownheap().

#define SMALLEST   1
 

Definition at line 435 of file trees.c.

Referenced by build_tree().

#define STATIC_TREES   1
 

Definition at line 118 of file trees.c.

Referenced by flush_block().

#define STORED_BLOCK   0
 

Definition at line 117 of file trees.c.

Referenced by flush_block().


Typedef Documentation

typedef struct ct_data ct_data
 

typedef struct tree_desc tree_desc
 


Function Documentation

local int build_bl_tree  ) 
 

Definition at line 803 of file trees.c.

References BL_CODES, bl_desc, bl_order, bl_tree, build_tree(), d_desc, dyn_dtree, dyn_ltree, l_desc, Len, tree_desc::max_code, near, opt_len, scan_tree(), static_len, and Tracev.

Referenced by flush_block().

00804 {
00805     int max_blindex;  /* index of last bit length code of non zero freq */
00806 
00807     /* Determine the bit length frequencies for literal and distance trees */
00808     scan_tree((ct_data near *)dyn_ltree, l_desc.max_code);
00809     scan_tree((ct_data near *)dyn_dtree, d_desc.max_code);
00810 
00811     /* Build the bit length tree: */
00812     build_tree((tree_desc near *)(&bl_desc));
00813     /* opt_len now includes the length of the tree representations, except
00814      * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
00815      */
00816 
00817     /* Determine the number of bit length codes to send. The pkzip format
00818      * requires that at least 4 bit length codes be sent. (appnote.txt says
00819      * 3 but the actual value used is 4.)
00820      */
00821     for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
00822         if (bl_tree[bl_order[max_blindex]].Len != 0) break;
00823     }
00824     /* Update opt_len to include the bit length tree and counts */
00825     opt_len += 3*(max_blindex+1) + 5+5+4;
00826     Tracev((stderr, "\ndyn trees: dyn %lu, stat %lu", opt_len, static_len));
00827 
00828     return max_blindex;
00829 }

local void build_tree tree_desc near *  desc  ) 
 

Definition at line 622 of file trees.c.

References bl_tree, depth, Freq, gen_bitlen(), gen_codes(), heap, heap_len, heap_max, HEAP_SIZE, MAX, near, opt_len, pqdownheap(), pqremove, SMALLEST, and static_len.

Referenced by build_bl_tree(), flush_block(), and unpack().

00624 {
00625     ct_data near *tree   = desc->dyn_tree;
00626     ct_data near *stree  = desc->static_tree;
00627     int elems            = desc->elems;
00628     int n, m;          /* iterate over heap elements */
00629     int max_code = -1; /* largest code with non zero frequency */
00630     int node = elems;  /* next internal node of the tree */
00631 
00632     /* Construct the initial heap, with least frequent element in
00633      * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
00634      * heap[0] is not used.
00635      */
00636     heap_len = 0, heap_max = HEAP_SIZE;
00637 
00638     for (n = 0; n < elems; n++) {
00639         if (tree[n].Freq != 0) {
00640             heap[++heap_len] = max_code = n;
00641             depth[n] = 0;
00642         } else {
00643             tree[n].Len = 0;
00644         }
00645     }
00646 
00647     /* The pkzip format requires that at least one distance code exists,
00648      * and that at least one bit should be sent even if there is only one
00649      * possible code. So to avoid special checks later on we force at least
00650      * two codes of non zero frequency.
00651      */
00652     while (heap_len < 2) {
00653         int new = heap[++heap_len] = (max_code < 2 ? ++max_code : 0);
00654         tree[new].Freq = 1;
00655         depth[new] = 0;
00656         opt_len--; if (stree) static_len -= stree[new].Len;
00657         /* new is 0 or 1 so it does not have extra bits */
00658     }
00659     desc->max_code = max_code;
00660 
00661     /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
00662      * establish sub-heaps of increasing lengths:
00663      */
00664     for (n = heap_len/2; n >= 1; n--) pqdownheap(tree, n);
00665 
00666     /* Construct the Huffman tree by repeatedly combining the least two
00667      * frequent nodes.
00668      */
00669     do {
00670         pqremove(tree, n);   /* n = node of least frequency */
00671         m = heap[SMALLEST];  /* m = node of next least frequency */
00672 
00673         heap[--heap_max] = n; /* keep the nodes sorted by frequency */
00674         heap[--heap_max] = m;
00675 
00676         /* Create a new node father of n and m */
00677         tree[node].Freq = tree[n].Freq + tree[m].Freq;
00678         depth[node] = (uch) (MAX(depth[n], depth[m]) + 1);
00679         tree[n].Dad = tree[m].Dad = (ush)node;
00680 #ifdef DUMP_BL_TREE
00681         if (tree == bl_tree) {
00682             fprintf(stderr,"\nnode %d(%d), sons %d(%d) %d(%d)",
00683                     node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq);
00684         }
00685 #endif
00686         /* and insert the new node in the heap */
00687         heap[SMALLEST] = node++;
00688         pqdownheap(tree, SMALLEST);
00689 
00690     } while (heap_len >= 2);
00691 
00692     heap[--heap_max] = heap[SMALLEST];
00693 
00694     /* At this point, the fields freq and dad are set. We can now
00695      * generate the bit lengths.
00696      */
00697     gen_bitlen((tree_desc near *)desc);
00698 
00699     /* The field len is now set, we can generate the bit codes */
00700     gen_codes ((ct_data near *)tree, max_code);
00701 }

local void compress_block ct_data near *  ltree,
ct_data near *  dtree
 

Definition at line 1020 of file trees.c.

References Assert, base_dist, base_length, d_code, D_CODES, END_BLOCK, extra_dbits, extra_lbits, flag_buf, l_buf, last_lit, length_code, LITERALS, send_bits(), send_code, and Tracecv.

Referenced by flush_block().

01023 {
01024     unsigned dist;      /* distance of matched string */
01025     int lc;             /* match length or unmatched char (if dist == 0) */
01026     unsigned lx = 0;    /* running index in l_buf */
01027     unsigned dx = 0;    /* running index in d_buf */
01028     unsigned fx = 0;    /* running index in flag_buf */
01029     uch flag = 0;       /* current flags */
01030     unsigned code;      /* the code to send */
01031     int extra;          /* number of extra bits to send */
01032 
01033     if (last_lit != 0) do {
01034         if ((lx & 7) == 0) flag = flag_buf[fx++];
01035         lc = l_buf[lx++];
01036         if ((flag & 1) == 0) {
01037             send_code(lc, ltree); /* send a literal byte */
01038             Tracecv(isgraph(lc), (stderr," '%c' ", lc));
01039         } else {
01040             /* Here, lc is the match length - MIN_MATCH */
01041             code = length_code[lc];
01042             send_code(code+LITERALS+1, ltree); /* send the length code */
01043             extra = extra_lbits[code];
01044             if (extra != 0) {
01045                 lc -= base_length[code];
01046                 send_bits(lc, extra);        /* send the extra length bits */
01047             }
01048             dist = d_buf[dx++];
01049             /* Here, dist is the match distance - 1 */
01050             code = d_code(dist);
01051             Assert (code < D_CODES, "bad d_code");
01052 
01053             send_code(code, dtree);       /* send the distance code */
01054             extra = extra_dbits[code];
01055             if (extra != 0) {
01056                 dist -= base_dist[code];
01057                 send_bits(dist, extra);   /* send the extra distance bits */
01058             }
01059         } /* literal or match pair ? */
01060         flag >>= 1;
01061     } while (lx < last_lit);
01062 
01063     send_code(END_BLOCK, ltree);
01064 }

void ct_init ush attr,
int *  methodp
 

Definition at line 345 of file trees.c.

References Assert, base_dist, base_length, bi_reverse(), bl_count, compressed_len, D_CODES, dist_code, extra_dbits, extra_lbits, file_method, file_type, gen_codes(), init_block(), input_len, L_CODES, Len, length_code, LENGTH_CODES, MAX_BITS, near, static_dtree, and static_ltree.

Referenced by zip().

00348 {
00349     int n;        /* iterates over tree elements */
00350     int bits;     /* bit counter */
00351     int length;   /* length value */
00352     int code;     /* code value */
00353     int dist;     /* distance index */
00354 
00355     file_type = attr;
00356     file_method = methodp;
00357     compressed_len = input_len = 0L;
00358 
00359     if (static_dtree[0].Len != 0) return; /* ct_init already called */
00360 
00361     /* Initialize the mapping length (0..255) -> length code (0..28) */
00362     length = 0;
00363     for (code = 0; code < LENGTH_CODES-1; code++) {
00364         base_length[code] = length;
00365         for (n = 0; n < (1<<extra_lbits[code]); n++) {
00366             length_code[length++] = (uch)code;
00367         }
00368     }
00369     Assert (length == 256, "ct_init: length != 256");
00370     /* Note that the length 255 (match length 258) can be represented
00371      * in two different ways: code 284 + 5 bits or code 285, so we
00372      * overwrite length_code[255] to use the best encoding:
00373      */
00374     length_code[length-1] = (uch)code;
00375 
00376     /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
00377     dist = 0;
00378     for (code = 0 ; code < 16; code++) {
00379         base_dist[code] = dist;
00380         for (n = 0; n < (1<<extra_dbits[code]); n++) {
00381             dist_code[dist++] = (uch)code;
00382         }
00383     }
00384     Assert (dist == 256, "ct_init: dist != 256");
00385     dist >>= 7; /* from now on, all distances are divided by 128 */
00386     for ( ; code < D_CODES; code++) {
00387         base_dist[code] = dist << 7;
00388         for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) {
00389             dist_code[256 + dist++] = (uch)code;
00390         }
00391     }
00392     Assert (dist == 256, "ct_init: 256+dist != 512");
00393 
00394     /* Construct the codes of the static literal tree */
00395     for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;
00396     n = 0;
00397     while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++;
00398     while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++;
00399     while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++;
00400     while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++;
00401     /* Codes 286 and 287 do not exist, but we must include them in the
00402      * tree construction to get a canonical Huffman tree (longest code
00403      * all ones)
00404      */
00405     gen_codes((ct_data near *)static_ltree, L_CODES+1);
00406 
00407     /* The static distance tree is trivial: */
00408     for (n = 0; n < D_CODES; n++) {
00409         static_dtree[n].Len = 5;
00410         static_dtree[n].Code = bi_reverse(n, 5);
00411     }
00412 
00413     /* Initialize the first block of the first file: */
00414     init_block();
00415 }

int ct_tally int  dist,
int  lc
 

Definition at line 967 of file trees.c.

References Assert, block_start, d_code, D_CODES, DIST_BUFSIZE, dyn_dtree, dyn_ltree, extra_dbits, flag_bit, flag_buf, flags, l_buf, last_dist, last_flags, last_lit, length_code, level, LIT_BUFSIZE, LITERALS, MAX_DIST, MAX_MATCH, MIN_MATCH, strstart, and Trace.

Referenced by deflate(), and deflate_fast().

00970 {
00971     l_buf[last_lit++] = (uch)lc;
00972     if (dist == 0) {
00973         /* lc is the unmatched char */
00974         dyn_ltree[lc].Freq++;
00975     } else {
00976         /* Here, lc is the match length - MIN_MATCH */
00977         dist--;             /* dist = match distance - 1 */
00978         Assert((ush)dist < (ush)MAX_DIST &&
00979                (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
00980                (ush)d_code(dist) < (ush)D_CODES,  "ct_tally: bad match");
00981 
00982         dyn_ltree[length_code[lc]+LITERALS+1].Freq++;
00983         dyn_dtree[d_code(dist)].Freq++;
00984 
00985         d_buf[last_dist++] = (ush)dist;
00986         flags |= flag_bit;
00987     }
00988     flag_bit <<= 1;
00989 
00990     /* Output the flags if they fill a byte: */
00991     if ((last_lit & 7) == 0) {
00992         flag_buf[last_flags++] = flags;
00993         flags = 0, flag_bit = 1;
00994     }
00995     /* Try to guess if it is profitable to stop the current block here */
00996     if (level > 2 && (last_lit & 0xfff) == 0) {
00997         /* Compute an upper bound for the compressed length */
00998         ulg out_length = (ulg)last_lit*8L;
00999         ulg in_length = (ulg)strstart-block_start;
01000         int dcode;
01001         for (dcode = 0; dcode < D_CODES; dcode++) {
01002             out_length += (ulg)dyn_dtree[dcode].Freq*(5L+extra_dbits[dcode]);
01003         }
01004         out_length >>= 3;
01005         Trace((stderr,"\nlast_lit %u, last_dist %u, in %ld, out ~%ld(%ld%%) ",
01006                last_lit, last_dist, in_length, out_length,
01007                100L - out_length*100L/in_length));
01008         if (last_dist < last_lit/2 && out_length < in_length/2) return 1;
01009     }
01010     return (last_lit == LIT_BUFSIZE-1 || last_dist == DIST_BUFSIZE);
01011     /* We avoid equality with LIT_BUFSIZE because of wraparound at 64K
01012      * on 16 bit machines and because stored blocks are restricted to
01013      * 64K-1 bytes.
01014      */
01015 }

off_t flush_block char *  buf,
ulg  stored_len,
int  eof
 

Definition at line 863 of file trees.c.

References Assert, bi_windup(), build_bl_tree(), build_tree(), bytes_in, compress_block(), compressed_len, copy_block(), d_desc, dyn_dtree, dyn_ltree, DYN_TREES, file_method, file_type, flag_buf, flags, gzip_error(), init_block(), input_len, l_desc, last_dist, last_flags, last_lit, level, near, opt_len, seekable, send_all_trees(), send_bits(), set_file_type(), static_dtree, static_len, static_ltree, STATIC_TREES, STORED, STORED_BLOCK, Trace, Tracev, and UNKNOWN.

00867 {
00868     ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
00869     int max_blindex;  /* index of last bit length code of non zero freq */
00870 
00871     flag_buf[last_flags] = flags; /* Save the flags for the last 8 items */
00872 
00873      /* Check if the file is ascii or binary */
00874     if (*file_type == (ush)UNKNOWN) set_file_type();
00875 
00876     /* Construct the literal and distance trees */
00877     build_tree((tree_desc near *)(&l_desc));
00878     Tracev((stderr, "\nlit data: dyn %lu, stat %lu", opt_len, static_len));
00879 
00880     build_tree((tree_desc near *)(&d_desc));
00881     Tracev((stderr, "\ndist data: dyn %lu, stat %lu", opt_len, static_len));
00882     /* At this point, opt_len and static_len are the total bit lengths of
00883      * the compressed block data, excluding the tree representations.
00884      */
00885 
00886     /* Build the bit length tree for the above two trees, and get the index
00887      * in bl_order of the last bit length code to send.
00888      */
00889     max_blindex = build_bl_tree();
00890 
00891     /* Determine the best encoding. Compute first the block length in bytes */
00892     opt_lenb = (opt_len+3+7)>>3;
00893     static_lenb = (static_len+3+7)>>3;
00894     input_len += stored_len; /* for debugging only */
00895 
00896     Trace((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u dist %u ",
00897             opt_lenb, opt_len, static_lenb, static_len, stored_len,
00898             last_lit, last_dist));
00899 
00900     if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
00901 
00902     /* If compression failed and this is the first and last block,
00903      * and if the zip file can be seeked (to rewrite the local header),
00904      * the whole file is transformed into a stored file:
00905      */
00906 #ifdef FORCE_METHOD
00907     if (level == 1 && eof && compressed_len == 0L) { /* force stored file */
00908 #else
00909     if (stored_len <= opt_lenb && eof && compressed_len == 0L && seekable()) {
00910 #endif
00911         /* Since LIT_BUFSIZE <= 2*WSIZE, the input data must be there: */
00912         if (!buf)
00913           gzip_error ("block vanished");
00914 
00915         copy_block(buf, (unsigned)stored_len, 0); /* without header */
00916         compressed_len = stored_len << 3;
00917         *file_method = STORED;
00918 
00919 #ifdef FORCE_METHOD
00920     } else if (level == 2 && buf != (char*)0) { /* force stored block */
00921 #else
00922     } else if (stored_len+4 <= opt_lenb && buf != (char*)0) {
00923                        /* 4: two words for the lengths */
00924 #endif
00925         /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
00926          * Otherwise we can't have processed more than WSIZE input bytes since
00927          * the last block flush, because compression would have been
00928          * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
00929          * transform a block into a stored block.
00930          */
00931         send_bits((STORED_BLOCK<<1)+eof, 3);  /* send block type */
00932         compressed_len = (compressed_len + 3 + 7) & ~7L;
00933         compressed_len += (stored_len + 4) << 3;
00934 
00935         copy_block(buf, (unsigned)stored_len, 1); /* with header */
00936 
00937 #ifdef FORCE_METHOD
00938     } else if (level == 3) { /* force static trees */
00939 #else
00940     } else if (static_lenb == opt_lenb) {
00941 #endif
00942         send_bits((STATIC_TREES<<1)+eof, 3);
00943         compress_block((ct_data near *)static_ltree, (ct_data near *)static_dtree);
00944         compressed_len += 3 + static_len;
00945     } else {
00946         send_bits((DYN_TREES<<1)+eof, 3);
00947         send_all_trees(l_desc.max_code+1, d_desc.max_code+1, max_blindex+1);
00948         compress_block((ct_data near *)dyn_ltree, (ct_data near *)dyn_dtree);
00949         compressed_len += 3 + opt_len;
00950     }
00951     Assert (compressed_len == bits_sent, "bad compressed size");
00952     init_block();
00953 
00954     if (eof) {
00955         Assert (input_len == bytes_in, "bad input size");
00956         bi_windup();
00957         compressed_len += 7;  /* align on byte boundary */
00958     }
00959 
00960     return compressed_len >> 3;
00961 }

local void gen_bitlen tree_desc near *  desc  ) 
 

Definition at line 496 of file trees.c.

References bl_count, heap, heap_max, HEAP_SIZE, Len, MAX_BITS, near, opt_len, static_len, and Trace.

Referenced by build_tree().

00498 {
00499     ct_data near *tree  = desc->dyn_tree;
00500     int near *extra     = desc->extra_bits;
00501     int base            = desc->extra_base;
00502     int max_code        = desc->max_code;
00503     int max_length      = desc->max_length;
00504     ct_data near *stree = desc->static_tree;
00505     int h;              /* heap index */
00506     int n, m;           /* iterate over the tree elements */
00507     int bits;           /* bit length */
00508     int xbits;          /* extra bits */
00509     ush f;              /* frequency */
00510     int overflow = 0;   /* number of elements with bit length too large */
00511 
00512     for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;
00513 
00514     /* In a first pass, compute the optimal bit lengths (which may
00515      * overflow in the case of the bit length tree).
00516      */
00517     tree[heap[heap_max]].Len = 0; /* root of the heap */
00518 
00519     for (h = heap_max+1; h < HEAP_SIZE; h++) {
00520         n = heap[h];
00521         bits = tree[tree[n].Dad].Len + 1;
00522         if (bits > max_length) bits = max_length, overflow++;
00523         tree[n].Len = (ush)bits;
00524         /* We overwrite tree[n].Dad which is no longer needed */
00525 
00526         if (n > max_code) continue; /* not a leaf node */
00527 
00528         bl_count[bits]++;
00529         xbits = 0;
00530         if (n >= base) xbits = extra[n-base];
00531         f = tree[n].Freq;
00532         opt_len += (ulg)f * (bits + xbits);
00533         if (stree) static_len += (ulg)f * (stree[n].Len + xbits);
00534     }
00535     if (overflow == 0) return;
00536 
00537     Trace((stderr,"\nbit length overflow\n"));
00538     /* This happens for example on obj2 and pic of the Calgary corpus */
00539 
00540     /* Find the first bit length which could increase: */
00541     do {
00542         bits = max_length-1;
00543         while (bl_count[bits] == 0) bits--;
00544         bl_count[bits]--;      /* move one leaf down the tree */
00545         bl_count[bits+1] += 2; /* move one overflow item as its brother */
00546         bl_count[max_length]--;
00547         /* The brother of the overflow item also moves one step up,
00548          * but this does not affect bl_count[max_length]
00549          */
00550         overflow -= 2;
00551     } while (overflow > 0);
00552 
00553     /* Now recompute all bit lengths, scanning in increasing frequency.
00554      * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
00555      * lengths instead of fixing only the wrong ones. This idea is taken
00556      * from 'ar' written by Haruhiko Okumura.)
00557      */
00558     for (bits = max_length; bits != 0; bits--) {
00559         n = bl_count[bits];
00560         while (n != 0) {
00561             m = heap[--h];
00562             if (m > max_code) continue;
00563             if (tree[m].Len != (unsigned) bits) {
00564                 Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
00565                 opt_len += ((long)bits-(long)tree[m].Len)*(long)tree[m].Freq;
00566                 tree[m].Len = (ush)bits;
00567             }
00568             n--;
00569         }
00570     }
00571 }

local void gen_codes ct_data near *  tree,
int  max_code
 

Definition at line 581 of file trees.c.

References Assert, bi_reverse(), bl_count, Code, MAX_BITS, static_ltree, Tracec, and Tracev.

Referenced by build_tree(), and ct_init().

00584 {
00585     ush next_code[MAX_BITS+1]; /* next code value for each bit length */
00586     ush code = 0;              /* running code value */
00587     int bits;                  /* bit index */
00588     int n;                     /* code index */
00589 
00590     /* The distribution counts are first used to generate the code values
00591      * without bit reversal.
00592      */
00593     for (bits = 1; bits <= MAX_BITS; bits++) {
00594         next_code[bits] = code = (code + bl_count[bits-1]) << 1;
00595     }
00596     /* Check that the bit counts in bl_count are consistent. The last code
00597      * must be all ones.
00598      */
00599     Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
00600             "inconsistent bit counts");
00601     Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
00602 
00603     for (n = 0;  n <= max_code; n++) {
00604         int len = tree[n].Len;
00605         if (len == 0) continue;
00606         /* Now reverse the bits */
00607         tree[n].Code = bi_reverse(next_code[len]++, len);
00608 
00609         Tracec(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
00610              n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
00611     }
00612 }

local void init_block  ) 
 

Definition at line 420 of file trees.c.

References BL_CODES, bl_tree, D_CODES, dyn_dtree, dyn_ltree, END_BLOCK, flag_bit, flags, L_CODES, last_dist, last_flags, last_lit, opt_len, and static_len.

Referenced by ct_init(), and flush_block().

00421 {
00422     int n; /* iterates over tree elements */
00423 
00424     /* Initialize the trees. */
00425     for (n = 0; n < L_CODES;  n++) dyn_ltree[n].Freq = 0;
00426     for (n = 0; n < D_CODES;  n++) dyn_dtree[n].Freq = 0;
00427     for (n = 0; n < BL_CODES; n++) bl_tree[n].Freq = 0;
00428 
00429     dyn_ltree[END_BLOCK].Freq = 1;
00430     opt_len = static_len = 0L;
00431     last_lit = last_dist = last_flags = 0;
00432     flags = 0; flag_bit = 1;
00433 }

local void init_block OF (void)   ) 
 

Definition at line 268 of file gzip.c.

00299 {
00300   fprintf (stderr, "Try `%s --help' for more information.\n",
00301            program_name);
00302   do_exit (ERROR);
00303 }

local void pqdownheap ct_data near *  tree,
int  k
 

Definition at line 464 of file trees.c.

References heap, heap_len, j, and smaller.

Referenced by build_tree().

00467 {
00468     int v = heap[k];
00469     int j = k << 1;  /* left son of k */
00470     while (j <= heap_len) {
00471         /* Set j to the smallest of the two sons: */
00472         if (j < heap_len && smaller(tree,