1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/modules/zlib/src/trees.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,1226 @@ 1.4 +/* trees.c -- output deflated data using Huffman coding 1.5 + * Copyright (C) 1995-2012 Jean-loup Gailly 1.6 + * detect_data_type() function provided freely by Cosmin Truta, 2006 1.7 + * For conditions of distribution and use, see copyright notice in zlib.h 1.8 + */ 1.9 + 1.10 +/* 1.11 + * ALGORITHM 1.12 + * 1.13 + * The "deflation" process uses several Huffman trees. The more 1.14 + * common source values are represented by shorter bit sequences. 1.15 + * 1.16 + * Each code tree is stored in a compressed form which is itself 1.17 + * a Huffman encoding of the lengths of all the code strings (in 1.18 + * ascending order by source values). The actual code strings are 1.19 + * reconstructed from the lengths in the inflate process, as described 1.20 + * in the deflate specification. 1.21 + * 1.22 + * REFERENCES 1.23 + * 1.24 + * Deutsch, L.P.,"'Deflate' Compressed Data Format Specification". 1.25 + * Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc 1.26 + * 1.27 + * Storer, James A. 1.28 + * Data Compression: Methods and Theory, pp. 49-50. 1.29 + * Computer Science Press, 1988. ISBN 0-7167-8156-5. 1.30 + * 1.31 + * Sedgewick, R. 1.32 + * Algorithms, p290. 1.33 + * Addison-Wesley, 1983. ISBN 0-201-06672-6. 1.34 + */ 1.35 + 1.36 +/* @(#) $Id$ */ 1.37 + 1.38 +/* #define GEN_TREES_H */ 1.39 + 1.40 +#include "deflate.h" 1.41 + 1.42 +#ifdef DEBUG 1.43 +# include <ctype.h> 1.44 +#endif 1.45 + 1.46 +/* =========================================================================== 1.47 + * Constants 1.48 + */ 1.49 + 1.50 +#define MAX_BL_BITS 7 1.51 +/* Bit length codes must not exceed MAX_BL_BITS bits */ 1.52 + 1.53 +#define END_BLOCK 256 1.54 +/* end of block literal code */ 1.55 + 1.56 +#define REP_3_6 16 1.57 +/* repeat previous bit length 3-6 times (2 bits of repeat count) */ 1.58 + 1.59 +#define REPZ_3_10 17 1.60 +/* repeat a zero length 3-10 times (3 bits of repeat count) */ 1.61 + 1.62 +#define REPZ_11_138 18 1.63 +/* repeat a zero length 11-138 times (7 bits of repeat count) */ 1.64 + 1.65 +local const int extra_lbits[LENGTH_CODES] /* extra bits for each length code */ 1.66 + = {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}; 1.67 + 1.68 +local const int extra_dbits[D_CODES] /* extra bits for each distance code */ 1.69 + = {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}; 1.70 + 1.71 +local const int extra_blbits[BL_CODES]/* extra bits for each bit length code */ 1.72 + = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7}; 1.73 + 1.74 +local const uch bl_order[BL_CODES] 1.75 + = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15}; 1.76 +/* The lengths of the bit length codes are sent in order of decreasing 1.77 + * probability, to avoid transmitting the lengths for unused bit length codes. 1.78 + */ 1.79 + 1.80 +/* =========================================================================== 1.81 + * Local data. These are initialized only once. 1.82 + */ 1.83 + 1.84 +#define DIST_CODE_LEN 512 /* see definition of array dist_code below */ 1.85 + 1.86 +#if defined(GEN_TREES_H) || !defined(STDC) 1.87 +/* non ANSI compilers may not accept trees.h */ 1.88 + 1.89 +local ct_data static_ltree[L_CODES+2]; 1.90 +/* The static literal tree. Since the bit lengths are imposed, there is no 1.91 + * need for the L_CODES extra codes used during heap construction. However 1.92 + * The codes 286 and 287 are needed to build a canonical tree (see _tr_init 1.93 + * below). 1.94 + */ 1.95 + 1.96 +local ct_data static_dtree[D_CODES]; 1.97 +/* The static distance tree. (Actually a trivial tree since all codes use 1.98 + * 5 bits.) 1.99 + */ 1.100 + 1.101 +uch _dist_code[DIST_CODE_LEN]; 1.102 +/* Distance codes. The first 256 values correspond to the distances 1.103 + * 3 .. 258, the last 256 values correspond to the top 8 bits of 1.104 + * the 15 bit distances. 1.105 + */ 1.106 + 1.107 +uch _length_code[MAX_MATCH-MIN_MATCH+1]; 1.108 +/* length code for each normalized match length (0 == MIN_MATCH) */ 1.109 + 1.110 +local int base_length[LENGTH_CODES]; 1.111 +/* First normalized length for each code (0 = MIN_MATCH) */ 1.112 + 1.113 +local int base_dist[D_CODES]; 1.114 +/* First normalized distance for each code (0 = distance of 1) */ 1.115 + 1.116 +#else 1.117 +# include "trees.h" 1.118 +#endif /* GEN_TREES_H */ 1.119 + 1.120 +struct static_tree_desc_s { 1.121 + const ct_data *static_tree; /* static tree or NULL */ 1.122 + const intf *extra_bits; /* extra bits for each code or NULL */ 1.123 + int extra_base; /* base index for extra_bits */ 1.124 + int elems; /* max number of elements in the tree */ 1.125 + int max_length; /* max bit length for the codes */ 1.126 +}; 1.127 + 1.128 +local static_tree_desc static_l_desc = 1.129 +{static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS}; 1.130 + 1.131 +local static_tree_desc static_d_desc = 1.132 +{static_dtree, extra_dbits, 0, D_CODES, MAX_BITS}; 1.133 + 1.134 +local static_tree_desc static_bl_desc = 1.135 +{(const ct_data *)0, extra_blbits, 0, BL_CODES, MAX_BL_BITS}; 1.136 + 1.137 +/* =========================================================================== 1.138 + * Local (static) routines in this file. 1.139 + */ 1.140 + 1.141 +local void tr_static_init OF((void)); 1.142 +local void init_block OF((deflate_state *s)); 1.143 +local void pqdownheap OF((deflate_state *s, ct_data *tree, int k)); 1.144 +local void gen_bitlen OF((deflate_state *s, tree_desc *desc)); 1.145 +local void gen_codes OF((ct_data *tree, int max_code, ushf *bl_count)); 1.146 +local void build_tree OF((deflate_state *s, tree_desc *desc)); 1.147 +local void scan_tree OF((deflate_state *s, ct_data *tree, int max_code)); 1.148 +local void send_tree OF((deflate_state *s, ct_data *tree, int max_code)); 1.149 +local int build_bl_tree OF((deflate_state *s)); 1.150 +local void send_all_trees OF((deflate_state *s, int lcodes, int dcodes, 1.151 + int blcodes)); 1.152 +local void compress_block OF((deflate_state *s, const ct_data *ltree, 1.153 + const ct_data *dtree)); 1.154 +local int detect_data_type OF((deflate_state *s)); 1.155 +local unsigned bi_reverse OF((unsigned value, int length)); 1.156 +local void bi_windup OF((deflate_state *s)); 1.157 +local void bi_flush OF((deflate_state *s)); 1.158 +local void copy_block OF((deflate_state *s, charf *buf, unsigned len, 1.159 + int header)); 1.160 + 1.161 +#ifdef GEN_TREES_H 1.162 +local void gen_trees_header OF((void)); 1.163 +#endif 1.164 + 1.165 +#ifndef DEBUG 1.166 +# define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len) 1.167 + /* Send a code of the given tree. c and tree must not have side effects */ 1.168 + 1.169 +#else /* DEBUG */ 1.170 +# define send_code(s, c, tree) \ 1.171 + { if (z_verbose>2) fprintf(stderr,"\ncd %3d ",(c)); \ 1.172 + send_bits(s, tree[c].Code, tree[c].Len); } 1.173 +#endif 1.174 + 1.175 +/* =========================================================================== 1.176 + * Output a short LSB first on the stream. 1.177 + * IN assertion: there is enough room in pendingBuf. 1.178 + */ 1.179 +#define put_short(s, w) { \ 1.180 + put_byte(s, (uch)((w) & 0xff)); \ 1.181 + put_byte(s, (uch)((ush)(w) >> 8)); \ 1.182 +} 1.183 + 1.184 +/* =========================================================================== 1.185 + * Send a value on a given number of bits. 1.186 + * IN assertion: length <= 16 and value fits in length bits. 1.187 + */ 1.188 +#ifdef DEBUG 1.189 +local void send_bits OF((deflate_state *s, int value, int length)); 1.190 + 1.191 +local void send_bits(s, value, length) 1.192 + deflate_state *s; 1.193 + int value; /* value to send */ 1.194 + int length; /* number of bits */ 1.195 +{ 1.196 + Tracevv((stderr," l %2d v %4x ", length, value)); 1.197 + Assert(length > 0 && length <= 15, "invalid length"); 1.198 + s->bits_sent += (ulg)length; 1.199 + 1.200 + /* If not enough room in bi_buf, use (valid) bits from bi_buf and 1.201 + * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid)) 1.202 + * unused bits in value. 1.203 + */ 1.204 + if (s->bi_valid > (int)Buf_size - length) { 1.205 + s->bi_buf |= (ush)value << s->bi_valid; 1.206 + put_short(s, s->bi_buf); 1.207 + s->bi_buf = (ush)value >> (Buf_size - s->bi_valid); 1.208 + s->bi_valid += length - Buf_size; 1.209 + } else { 1.210 + s->bi_buf |= (ush)value << s->bi_valid; 1.211 + s->bi_valid += length; 1.212 + } 1.213 +} 1.214 +#else /* !DEBUG */ 1.215 + 1.216 +#define send_bits(s, value, length) \ 1.217 +{ int len = length;\ 1.218 + if (s->bi_valid > (int)Buf_size - len) {\ 1.219 + int val = value;\ 1.220 + s->bi_buf |= (ush)val << s->bi_valid;\ 1.221 + put_short(s, s->bi_buf);\ 1.222 + s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\ 1.223 + s->bi_valid += len - Buf_size;\ 1.224 + } else {\ 1.225 + s->bi_buf |= (ush)(value) << s->bi_valid;\ 1.226 + s->bi_valid += len;\ 1.227 + }\ 1.228 +} 1.229 +#endif /* DEBUG */ 1.230 + 1.231 + 1.232 +/* the arguments must not have side effects */ 1.233 + 1.234 +/* =========================================================================== 1.235 + * Initialize the various 'constant' tables. 1.236 + */ 1.237 +local void tr_static_init() 1.238 +{ 1.239 +#if defined(GEN_TREES_H) || !defined(STDC) 1.240 + static int static_init_done = 0; 1.241 + int n; /* iterates over tree elements */ 1.242 + int bits; /* bit counter */ 1.243 + int length; /* length value */ 1.244 + int code; /* code value */ 1.245 + int dist; /* distance index */ 1.246 + ush bl_count[MAX_BITS+1]; 1.247 + /* number of codes at each bit length for an optimal tree */ 1.248 + 1.249 + if (static_init_done) return; 1.250 + 1.251 + /* For some embedded targets, global variables are not initialized: */ 1.252 +#ifdef NO_INIT_GLOBAL_POINTERS 1.253 + static_l_desc.static_tree = static_ltree; 1.254 + static_l_desc.extra_bits = extra_lbits; 1.255 + static_d_desc.static_tree = static_dtree; 1.256 + static_d_desc.extra_bits = extra_dbits; 1.257 + static_bl_desc.extra_bits = extra_blbits; 1.258 +#endif 1.259 + 1.260 + /* Initialize the mapping length (0..255) -> length code (0..28) */ 1.261 + length = 0; 1.262 + for (code = 0; code < LENGTH_CODES-1; code++) { 1.263 + base_length[code] = length; 1.264 + for (n = 0; n < (1<<extra_lbits[code]); n++) { 1.265 + _length_code[length++] = (uch)code; 1.266 + } 1.267 + } 1.268 + Assert (length == 256, "tr_static_init: length != 256"); 1.269 + /* Note that the length 255 (match length 258) can be represented 1.270 + * in two different ways: code 284 + 5 bits or code 285, so we 1.271 + * overwrite length_code[255] to use the best encoding: 1.272 + */ 1.273 + _length_code[length-1] = (uch)code; 1.274 + 1.275 + /* Initialize the mapping dist (0..32K) -> dist code (0..29) */ 1.276 + dist = 0; 1.277 + for (code = 0 ; code < 16; code++) { 1.278 + base_dist[code] = dist; 1.279 + for (n = 0; n < (1<<extra_dbits[code]); n++) { 1.280 + _dist_code[dist++] = (uch)code; 1.281 + } 1.282 + } 1.283 + Assert (dist == 256, "tr_static_init: dist != 256"); 1.284 + dist >>= 7; /* from now on, all distances are divided by 128 */ 1.285 + for ( ; code < D_CODES; code++) { 1.286 + base_dist[code] = dist << 7; 1.287 + for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) { 1.288 + _dist_code[256 + dist++] = (uch)code; 1.289 + } 1.290 + } 1.291 + Assert (dist == 256, "tr_static_init: 256+dist != 512"); 1.292 + 1.293 + /* Construct the codes of the static literal tree */ 1.294 + for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0; 1.295 + n = 0; 1.296 + while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++; 1.297 + while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++; 1.298 + while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++; 1.299 + while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++; 1.300 + /* Codes 286 and 287 do not exist, but we must include them in the 1.301 + * tree construction to get a canonical Huffman tree (longest code 1.302 + * all ones) 1.303 + */ 1.304 + gen_codes((ct_data *)static_ltree, L_CODES+1, bl_count); 1.305 + 1.306 + /* The static distance tree is trivial: */ 1.307 + for (n = 0; n < D_CODES; n++) { 1.308 + static_dtree[n].Len = 5; 1.309 + static_dtree[n].Code = bi_reverse((unsigned)n, 5); 1.310 + } 1.311 + static_init_done = 1; 1.312 + 1.313 +# ifdef GEN_TREES_H 1.314 + gen_trees_header(); 1.315 +# endif 1.316 +#endif /* defined(GEN_TREES_H) || !defined(STDC) */ 1.317 +} 1.318 + 1.319 +/* =========================================================================== 1.320 + * Genererate the file trees.h describing the static trees. 1.321 + */ 1.322 +#ifdef GEN_TREES_H 1.323 +# ifndef DEBUG 1.324 +# include <stdio.h> 1.325 +# endif 1.326 + 1.327 +# define SEPARATOR(i, last, width) \ 1.328 + ((i) == (last)? "\n};\n\n" : \ 1.329 + ((i) % (width) == (width)-1 ? ",\n" : ", ")) 1.330 + 1.331 +void gen_trees_header() 1.332 +{ 1.333 + FILE *header = fopen("trees.h", "w"); 1.334 + int i; 1.335 + 1.336 + Assert (header != NULL, "Can't open trees.h"); 1.337 + fprintf(header, 1.338 + "/* header created automatically with -DGEN_TREES_H */\n\n"); 1.339 + 1.340 + fprintf(header, "local const ct_data static_ltree[L_CODES+2] = {\n"); 1.341 + for (i = 0; i < L_CODES+2; i++) { 1.342 + fprintf(header, "{{%3u},{%3u}}%s", static_ltree[i].Code, 1.343 + static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5)); 1.344 + } 1.345 + 1.346 + fprintf(header, "local const ct_data static_dtree[D_CODES] = {\n"); 1.347 + for (i = 0; i < D_CODES; i++) { 1.348 + fprintf(header, "{{%2u},{%2u}}%s", static_dtree[i].Code, 1.349 + static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5)); 1.350 + } 1.351 + 1.352 + fprintf(header, "const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = {\n"); 1.353 + for (i = 0; i < DIST_CODE_LEN; i++) { 1.354 + fprintf(header, "%2u%s", _dist_code[i], 1.355 + SEPARATOR(i, DIST_CODE_LEN-1, 20)); 1.356 + } 1.357 + 1.358 + fprintf(header, 1.359 + "const uch ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= {\n"); 1.360 + for (i = 0; i < MAX_MATCH-MIN_MATCH+1; i++) { 1.361 + fprintf(header, "%2u%s", _length_code[i], 1.362 + SEPARATOR(i, MAX_MATCH-MIN_MATCH, 20)); 1.363 + } 1.364 + 1.365 + fprintf(header, "local const int base_length[LENGTH_CODES] = {\n"); 1.366 + for (i = 0; i < LENGTH_CODES; i++) { 1.367 + fprintf(header, "%1u%s", base_length[i], 1.368 + SEPARATOR(i, LENGTH_CODES-1, 20)); 1.369 + } 1.370 + 1.371 + fprintf(header, "local const int base_dist[D_CODES] = {\n"); 1.372 + for (i = 0; i < D_CODES; i++) { 1.373 + fprintf(header, "%5u%s", base_dist[i], 1.374 + SEPARATOR(i, D_CODES-1, 10)); 1.375 + } 1.376 + 1.377 + fclose(header); 1.378 +} 1.379 +#endif /* GEN_TREES_H */ 1.380 + 1.381 +/* =========================================================================== 1.382 + * Initialize the tree data structures for a new zlib stream. 1.383 + */ 1.384 +void ZLIB_INTERNAL _tr_init(s) 1.385 + deflate_state *s; 1.386 +{ 1.387 + tr_static_init(); 1.388 + 1.389 + s->l_desc.dyn_tree = s->dyn_ltree; 1.390 + s->l_desc.stat_desc = &static_l_desc; 1.391 + 1.392 + s->d_desc.dyn_tree = s->dyn_dtree; 1.393 + s->d_desc.stat_desc = &static_d_desc; 1.394 + 1.395 + s->bl_desc.dyn_tree = s->bl_tree; 1.396 + s->bl_desc.stat_desc = &static_bl_desc; 1.397 + 1.398 + s->bi_buf = 0; 1.399 + s->bi_valid = 0; 1.400 +#ifdef DEBUG 1.401 + s->compressed_len = 0L; 1.402 + s->bits_sent = 0L; 1.403 +#endif 1.404 + 1.405 + /* Initialize the first block of the first file: */ 1.406 + init_block(s); 1.407 +} 1.408 + 1.409 +/* =========================================================================== 1.410 + * Initialize a new block. 1.411 + */ 1.412 +local void init_block(s) 1.413 + deflate_state *s; 1.414 +{ 1.415 + int n; /* iterates over tree elements */ 1.416 + 1.417 + /* Initialize the trees. */ 1.418 + for (n = 0; n < L_CODES; n++) s->dyn_ltree[n].Freq = 0; 1.419 + for (n = 0; n < D_CODES; n++) s->dyn_dtree[n].Freq = 0; 1.420 + for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0; 1.421 + 1.422 + s->dyn_ltree[END_BLOCK].Freq = 1; 1.423 + s->opt_len = s->static_len = 0L; 1.424 + s->last_lit = s->matches = 0; 1.425 +} 1.426 + 1.427 +#define SMALLEST 1 1.428 +/* Index within the heap array of least frequent node in the Huffman tree */ 1.429 + 1.430 + 1.431 +/* =========================================================================== 1.432 + * Remove the smallest element from the heap and recreate the heap with 1.433 + * one less element. Updates heap and heap_len. 1.434 + */ 1.435 +#define pqremove(s, tree, top) \ 1.436 +{\ 1.437 + top = s->heap[SMALLEST]; \ 1.438 + s->heap[SMALLEST] = s->heap[s->heap_len--]; \ 1.439 + pqdownheap(s, tree, SMALLEST); \ 1.440 +} 1.441 + 1.442 +/* =========================================================================== 1.443 + * Compares to subtrees, using the tree depth as tie breaker when 1.444 + * the subtrees have equal frequency. This minimizes the worst case length. 1.445 + */ 1.446 +#define smaller(tree, n, m, depth) \ 1.447 + (tree[n].Freq < tree[m].Freq || \ 1.448 + (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m])) 1.449 + 1.450 +/* =========================================================================== 1.451 + * Restore the heap property by moving down the tree starting at node k, 1.452 + * exchanging a node with the smallest of its two sons if necessary, stopping 1.453 + * when the heap property is re-established (each father smaller than its 1.454 + * two sons). 1.455 + */ 1.456 +local void pqdownheap(s, tree, k) 1.457 + deflate_state *s; 1.458 + ct_data *tree; /* the tree to restore */ 1.459 + int k; /* node to move down */ 1.460 +{ 1.461 + int v = s->heap[k]; 1.462 + int j = k << 1; /* left son of k */ 1.463 + while (j <= s->heap_len) { 1.464 + /* Set j to the smallest of the two sons: */ 1.465 + if (j < s->heap_len && 1.466 + smaller(tree, s->heap[j+1], s->heap[j], s->depth)) { 1.467 + j++; 1.468 + } 1.469 + /* Exit if v is smaller than both sons */ 1.470 + if (smaller(tree, v, s->heap[j], s->depth)) break; 1.471 + 1.472 + /* Exchange v with the smallest son */ 1.473 + s->heap[k] = s->heap[j]; k = j; 1.474 + 1.475 + /* And continue down the tree, setting j to the left son of k */ 1.476 + j <<= 1; 1.477 + } 1.478 + s->heap[k] = v; 1.479 +} 1.480 + 1.481 +/* =========================================================================== 1.482 + * Compute the optimal bit lengths for a tree and update the total bit length 1.483 + * for the current block. 1.484 + * IN assertion: the fields freq and dad are set, heap[heap_max] and 1.485 + * above are the tree nodes sorted by increasing frequency. 1.486 + * OUT assertions: the field len is set to the optimal bit length, the 1.487 + * array bl_count contains the frequencies for each bit length. 1.488 + * The length opt_len is updated; static_len is also updated if stree is 1.489 + * not null. 1.490 + */ 1.491 +local void gen_bitlen(s, desc) 1.492 + deflate_state *s; 1.493 + tree_desc *desc; /* the tree descriptor */ 1.494 +{ 1.495 + ct_data *tree = desc->dyn_tree; 1.496 + int max_code = desc->max_code; 1.497 + const ct_data *stree = desc->stat_desc->static_tree; 1.498 + const intf *extra = desc->stat_desc->extra_bits; 1.499 + int base = desc->stat_desc->extra_base; 1.500 + int max_length = desc->stat_desc->max_length; 1.501 + int h; /* heap index */ 1.502 + int n, m; /* iterate over the tree elements */ 1.503 + int bits; /* bit length */ 1.504 + int xbits; /* extra bits */ 1.505 + ush f; /* frequency */ 1.506 + int overflow = 0; /* number of elements with bit length too large */ 1.507 + 1.508 + for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0; 1.509 + 1.510 + /* In a first pass, compute the optimal bit lengths (which may 1.511 + * overflow in the case of the bit length tree). 1.512 + */ 1.513 + tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */ 1.514 + 1.515 + for (h = s->heap_max+1; h < HEAP_SIZE; h++) { 1.516 + n = s->heap[h]; 1.517 + bits = tree[tree[n].Dad].Len + 1; 1.518 + if (bits > max_length) bits = max_length, overflow++; 1.519 + tree[n].Len = (ush)bits; 1.520 + /* We overwrite tree[n].Dad which is no longer needed */ 1.521 + 1.522 + if (n > max_code) continue; /* not a leaf node */ 1.523 + 1.524 + s->bl_count[bits]++; 1.525 + xbits = 0; 1.526 + if (n >= base) xbits = extra[n-base]; 1.527 + f = tree[n].Freq; 1.528 + s->opt_len += (ulg)f * (bits + xbits); 1.529 + if (stree) s->static_len += (ulg)f * (stree[n].Len + xbits); 1.530 + } 1.531 + if (overflow == 0) return; 1.532 + 1.533 + Trace((stderr,"\nbit length overflow\n")); 1.534 + /* This happens for example on obj2 and pic of the Calgary corpus */ 1.535 + 1.536 + /* Find the first bit length which could increase: */ 1.537 + do { 1.538 + bits = max_length-1; 1.539 + while (s->bl_count[bits] == 0) bits--; 1.540 + s->bl_count[bits]--; /* move one leaf down the tree */ 1.541 + s->bl_count[bits+1] += 2; /* move one overflow item as its brother */ 1.542 + s->bl_count[max_length]--; 1.543 + /* The brother of the overflow item also moves one step up, 1.544 + * but this does not affect bl_count[max_length] 1.545 + */ 1.546 + overflow -= 2; 1.547 + } while (overflow > 0); 1.548 + 1.549 + /* Now recompute all bit lengths, scanning in increasing frequency. 1.550 + * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all 1.551 + * lengths instead of fixing only the wrong ones. This idea is taken 1.552 + * from 'ar' written by Haruhiko Okumura.) 1.553 + */ 1.554 + for (bits = max_length; bits != 0; bits--) { 1.555 + n = s->bl_count[bits]; 1.556 + while (n != 0) { 1.557 + m = s->heap[--h]; 1.558 + if (m > max_code) continue; 1.559 + if ((unsigned) tree[m].Len != (unsigned) bits) { 1.560 + Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits)); 1.561 + s->opt_len += ((long)bits - (long)tree[m].Len) 1.562 + *(long)tree[m].Freq; 1.563 + tree[m].Len = (ush)bits; 1.564 + } 1.565 + n--; 1.566 + } 1.567 + } 1.568 +} 1.569 + 1.570 +/* =========================================================================== 1.571 + * Generate the codes for a given tree and bit counts (which need not be 1.572 + * optimal). 1.573 + * IN assertion: the array bl_count contains the bit length statistics for 1.574 + * the given tree and the field len is set for all tree elements. 1.575 + * OUT assertion: the field code is set for all tree elements of non 1.576 + * zero code length. 1.577 + */ 1.578 +local void gen_codes (tree, max_code, bl_count) 1.579 + ct_data *tree; /* the tree to decorate */ 1.580 + int max_code; /* largest code with non zero frequency */ 1.581 + ushf *bl_count; /* number of codes at each bit length */ 1.582 +{ 1.583 + ush next_code[MAX_BITS+1]; /* next code value for each bit length */ 1.584 + ush code = 0; /* running code value */ 1.585 + int bits; /* bit index */ 1.586 + int n; /* code index */ 1.587 + 1.588 + /* The distribution counts are first used to generate the code values 1.589 + * without bit reversal. 1.590 + */ 1.591 + for (bits = 1; bits <= MAX_BITS; bits++) { 1.592 + next_code[bits] = code = (code + bl_count[bits-1]) << 1; 1.593 + } 1.594 + /* Check that the bit counts in bl_count are consistent. The last code 1.595 + * must be all ones. 1.596 + */ 1.597 + Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1, 1.598 + "inconsistent bit counts"); 1.599 + Tracev((stderr,"\ngen_codes: max_code %d ", max_code)); 1.600 + 1.601 + for (n = 0; n <= max_code; n++) { 1.602 + int len = tree[n].Len; 1.603 + if (len == 0) continue; 1.604 + /* Now reverse the bits */ 1.605 + tree[n].Code = bi_reverse(next_code[len]++, len); 1.606 + 1.607 + Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ", 1.608 + n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1)); 1.609 + } 1.610 +} 1.611 + 1.612 +/* =========================================================================== 1.613 + * Construct one Huffman tree and assigns the code bit strings and lengths. 1.614 + * Update the total bit length for the current block. 1.615 + * IN assertion: the field freq is set for all tree elements. 1.616 + * OUT assertions: the fields len and code are set to the optimal bit length 1.617 + * and corresponding code. The length opt_len is updated; static_len is 1.618 + * also updated if stree is not null. The field max_code is set. 1.619 + */ 1.620 +local void build_tree(s, desc) 1.621 + deflate_state *s; 1.622 + tree_desc *desc; /* the tree descriptor */ 1.623 +{ 1.624 + ct_data *tree = desc->dyn_tree; 1.625 + const ct_data *stree = desc->stat_desc->static_tree; 1.626 + int elems = desc->stat_desc->elems; 1.627 + int n, m; /* iterate over heap elements */ 1.628 + int max_code = -1; /* largest code with non zero frequency */ 1.629 + int node; /* new node being created */ 1.630 + 1.631 + /* Construct the initial heap, with least frequent element in 1.632 + * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1]. 1.633 + * heap[0] is not used. 1.634 + */ 1.635 + s->heap_len = 0, s->heap_max = HEAP_SIZE; 1.636 + 1.637 + for (n = 0; n < elems; n++) { 1.638 + if (tree[n].Freq != 0) { 1.639 + s->heap[++(s->heap_len)] = max_code = n; 1.640 + s->depth[n] = 0; 1.641 + } else { 1.642 + tree[n].Len = 0; 1.643 + } 1.644 + } 1.645 + 1.646 + /* The pkzip format requires that at least one distance code exists, 1.647 + * and that at least one bit should be sent even if there is only one 1.648 + * possible code. So to avoid special checks later on we force at least 1.649 + * two codes of non zero frequency. 1.650 + */ 1.651 + while (s->heap_len < 2) { 1.652 + node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0); 1.653 + tree[node].Freq = 1; 1.654 + s->depth[node] = 0; 1.655 + s->opt_len--; if (stree) s->static_len -= stree[node].Len; 1.656 + /* node is 0 or 1 so it does not have extra bits */ 1.657 + } 1.658 + desc->max_code = max_code; 1.659 + 1.660 + /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree, 1.661 + * establish sub-heaps of increasing lengths: 1.662 + */ 1.663 + for (n = s->heap_len/2; n >= 1; n--) pqdownheap(s, tree, n); 1.664 + 1.665 + /* Construct the Huffman tree by repeatedly combining the least two 1.666 + * frequent nodes. 1.667 + */ 1.668 + node = elems; /* next internal node of the tree */ 1.669 + do { 1.670 + pqremove(s, tree, n); /* n = node of least frequency */ 1.671 + m = s->heap[SMALLEST]; /* m = node of next least frequency */ 1.672 + 1.673 + s->heap[--(s->heap_max)] = n; /* keep the nodes sorted by frequency */ 1.674 + s->heap[--(s->heap_max)] = m; 1.675 + 1.676 + /* Create a new node father of n and m */ 1.677 + tree[node].Freq = tree[n].Freq + tree[m].Freq; 1.678 + s->depth[node] = (uch)((s->depth[n] >= s->depth[m] ? 1.679 + s->depth[n] : s->depth[m]) + 1); 1.680 + tree[n].Dad = tree[m].Dad = (ush)node; 1.681 +#ifdef DUMP_BL_TREE 1.682 + if (tree == s->bl_tree) { 1.683 + fprintf(stderr,"\nnode %d(%d), sons %d(%d) %d(%d)", 1.684 + node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq); 1.685 + } 1.686 +#endif 1.687 + /* and insert the new node in the heap */ 1.688 + s->heap[SMALLEST] = node++; 1.689 + pqdownheap(s, tree, SMALLEST); 1.690 + 1.691 + } while (s->heap_len >= 2); 1.692 + 1.693 + s->heap[--(s->heap_max)] = s->heap[SMALLEST]; 1.694 + 1.695 + /* At this point, the fields freq and dad are set. We can now 1.696 + * generate the bit lengths. 1.697 + */ 1.698 + gen_bitlen(s, (tree_desc *)desc); 1.699 + 1.700 + /* The field len is now set, we can generate the bit codes */ 1.701 + gen_codes ((ct_data *)tree, max_code, s->bl_count); 1.702 +} 1.703 + 1.704 +/* =========================================================================== 1.705 + * Scan a literal or distance tree to determine the frequencies of the codes 1.706 + * in the bit length tree. 1.707 + */ 1.708 +local void scan_tree (s, tree, max_code) 1.709 + deflate_state *s; 1.710 + ct_data *tree; /* the tree to be scanned */ 1.711 + int max_code; /* and its largest code of non zero frequency */ 1.712 +{ 1.713 + int n; /* iterates over all tree elements */ 1.714 + int prevlen = -1; /* last emitted length */ 1.715 + int curlen; /* length of current code */ 1.716 + int nextlen = tree[0].Len; /* length of next code */ 1.717 + int count = 0; /* repeat count of the current code */ 1.718 + int max_count = 7; /* max repeat count */ 1.719 + int min_count = 4; /* min repeat count */ 1.720 + 1.721 + if (nextlen == 0) max_count = 138, min_count = 3; 1.722 + tree[max_code+1].Len = (ush)0xffff; /* guard */ 1.723 + 1.724 + for (n = 0; n <= max_code; n++) { 1.725 + curlen = nextlen; nextlen = tree[n+1].Len; 1.726 + if (++count < max_count && curlen == nextlen) { 1.727 + continue; 1.728 + } else if (count < min_count) { 1.729 + s->bl_tree[curlen].Freq += count; 1.730 + } else if (curlen != 0) { 1.731 + if (curlen != prevlen) s->bl_tree[curlen].Freq++; 1.732 + s->bl_tree[REP_3_6].Freq++; 1.733 + } else if (count <= 10) { 1.734 + s->bl_tree[REPZ_3_10].Freq++; 1.735 + } else { 1.736 + s->bl_tree[REPZ_11_138].Freq++; 1.737 + } 1.738 + count = 0; prevlen = curlen; 1.739 + if (nextlen == 0) { 1.740 + max_count = 138, min_count = 3; 1.741 + } else if (curlen == nextlen) { 1.742 + max_count = 6, min_count = 3; 1.743 + } else { 1.744 + max_count = 7, min_count = 4; 1.745 + } 1.746 + } 1.747 +} 1.748 + 1.749 +/* =========================================================================== 1.750 + * Send a literal or distance tree in compressed form, using the codes in 1.751 + * bl_tree. 1.752 + */ 1.753 +local void send_tree (s, tree, max_code) 1.754 + deflate_state *s; 1.755 + ct_data *tree; /* the tree to be scanned */ 1.756 + int max_code; /* and its largest code of non zero frequency */ 1.757 +{ 1.758 + int n; /* iterates over all tree elements */ 1.759 + int prevlen = -1; /* last emitted length */ 1.760 + int curlen; /* length of current code */ 1.761 + int nextlen = tree[0].Len; /* length of next code */ 1.762 + int count = 0; /* repeat count of the current code */ 1.763 + int max_count = 7; /* max repeat count */ 1.764 + int min_count = 4; /* min repeat count */ 1.765 + 1.766 + /* tree[max_code+1].Len = -1; */ /* guard already set */ 1.767 + if (nextlen == 0) max_count = 138, min_count = 3; 1.768 + 1.769 + for (n = 0; n <= max_code; n++) { 1.770 + curlen = nextlen; nextlen = tree[n+1].Len; 1.771 + if (++count < max_count && curlen == nextlen) { 1.772 + continue; 1.773 + } else if (count < min_count) { 1.774 + do { send_code(s, curlen, s->bl_tree); } while (--count != 0); 1.775 + 1.776 + } else if (curlen != 0) { 1.777 + if (curlen != prevlen) { 1.778 + send_code(s, curlen, s->bl_tree); count--; 1.779 + } 1.780 + Assert(count >= 3 && count <= 6, " 3_6?"); 1.781 + send_code(s, REP_3_6, s->bl_tree); send_bits(s, count-3, 2); 1.782 + 1.783 + } else if (count <= 10) { 1.784 + send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count-3, 3); 1.785 + 1.786 + } else { 1.787 + send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count-11, 7); 1.788 + } 1.789 + count = 0; prevlen = curlen; 1.790 + if (nextlen == 0) { 1.791 + max_count = 138, min_count = 3; 1.792 + } else if (curlen == nextlen) { 1.793 + max_count = 6, min_count = 3; 1.794 + } else { 1.795 + max_count = 7, min_count = 4; 1.796 + } 1.797 + } 1.798 +} 1.799 + 1.800 +/* =========================================================================== 1.801 + * Construct the Huffman tree for the bit lengths and return the index in 1.802 + * bl_order of the last bit length code to send. 1.803 + */ 1.804 +local int build_bl_tree(s) 1.805 + deflate_state *s; 1.806 +{ 1.807 + int max_blindex; /* index of last bit length code of non zero freq */ 1.808 + 1.809 + /* Determine the bit length frequencies for literal and distance trees */ 1.810 + scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code); 1.811 + scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code); 1.812 + 1.813 + /* Build the bit length tree: */ 1.814 + build_tree(s, (tree_desc *)(&(s->bl_desc))); 1.815 + /* opt_len now includes the length of the tree representations, except 1.816 + * the lengths of the bit lengths codes and the 5+5+4 bits for the counts. 1.817 + */ 1.818 + 1.819 + /* Determine the number of bit length codes to send. The pkzip format 1.820 + * requires that at least 4 bit length codes be sent. (appnote.txt says 1.821 + * 3 but the actual value used is 4.) 1.822 + */ 1.823 + for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) { 1.824 + if (s->bl_tree[bl_order[max_blindex]].Len != 0) break; 1.825 + } 1.826 + /* Update opt_len to include the bit length tree and counts */ 1.827 + s->opt_len += 3*(max_blindex+1) + 5+5+4; 1.828 + Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld", 1.829 + s->opt_len, s->static_len)); 1.830 + 1.831 + return max_blindex; 1.832 +} 1.833 + 1.834 +/* =========================================================================== 1.835 + * Send the header for a block using dynamic Huffman trees: the counts, the 1.836 + * lengths of the bit length codes, the literal tree and the distance tree. 1.837 + * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4. 1.838 + */ 1.839 +local void send_all_trees(s, lcodes, dcodes, blcodes) 1.840 + deflate_state *s; 1.841 + int lcodes, dcodes, blcodes; /* number of codes for each tree */ 1.842 +{ 1.843 + int rank; /* index in bl_order */ 1.844 + 1.845 + Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes"); 1.846 + Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES, 1.847 + "too many codes"); 1.848 + Tracev((stderr, "\nbl counts: ")); 1.849 + send_bits(s, lcodes-257, 5); /* not +255 as stated in appnote.txt */ 1.850 + send_bits(s, dcodes-1, 5); 1.851 + send_bits(s, blcodes-4, 4); /* not -3 as stated in appnote.txt */ 1.852 + for (rank = 0; rank < blcodes; rank++) { 1.853 + Tracev((stderr, "\nbl code %2d ", bl_order[rank])); 1.854 + send_bits(s, s->bl_tree[bl_order[rank]].Len, 3); 1.855 + } 1.856 + Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent)); 1.857 + 1.858 + send_tree(s, (ct_data *)s->dyn_ltree, lcodes-1); /* literal tree */ 1.859 + Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent)); 1.860 + 1.861 + send_tree(s, (ct_data *)s->dyn_dtree, dcodes-1); /* distance tree */ 1.862 + Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent)); 1.863 +} 1.864 + 1.865 +/* =========================================================================== 1.866 + * Send a stored block 1.867 + */ 1.868 +void ZLIB_INTERNAL _tr_stored_block(s, buf, stored_len, last) 1.869 + deflate_state *s; 1.870 + charf *buf; /* input block */ 1.871 + ulg stored_len; /* length of input block */ 1.872 + int last; /* one if this is the last block for a file */ 1.873 +{ 1.874 + send_bits(s, (STORED_BLOCK<<1)+last, 3); /* send block type */ 1.875 +#ifdef DEBUG 1.876 + s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L; 1.877 + s->compressed_len += (stored_len + 4) << 3; 1.878 +#endif 1.879 + copy_block(s, buf, (unsigned)stored_len, 1); /* with header */ 1.880 +} 1.881 + 1.882 +/* =========================================================================== 1.883 + * Flush the bits in the bit buffer to pending output (leaves at most 7 bits) 1.884 + */ 1.885 +void ZLIB_INTERNAL _tr_flush_bits(s) 1.886 + deflate_state *s; 1.887 +{ 1.888 + bi_flush(s); 1.889 +} 1.890 + 1.891 +/* =========================================================================== 1.892 + * Send one empty static block to give enough lookahead for inflate. 1.893 + * This takes 10 bits, of which 7 may remain in the bit buffer. 1.894 + */ 1.895 +void ZLIB_INTERNAL _tr_align(s) 1.896 + deflate_state *s; 1.897 +{ 1.898 + send_bits(s, STATIC_TREES<<1, 3); 1.899 + send_code(s, END_BLOCK, static_ltree); 1.900 +#ifdef DEBUG 1.901 + s->compressed_len += 10L; /* 3 for block type, 7 for EOB */ 1.902 +#endif 1.903 + bi_flush(s); 1.904 +} 1.905 + 1.906 +/* =========================================================================== 1.907 + * Determine the best encoding for the current block: dynamic trees, static 1.908 + * trees or store, and output the encoded block to the zip file. 1.909 + */ 1.910 +void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last) 1.911 + deflate_state *s; 1.912 + charf *buf; /* input block, or NULL if too old */ 1.913 + ulg stored_len; /* length of input block */ 1.914 + int last; /* one if this is the last block for a file */ 1.915 +{ 1.916 + ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */ 1.917 + int max_blindex = 0; /* index of last bit length code of non zero freq */ 1.918 + 1.919 + /* Build the Huffman trees unless a stored block is forced */ 1.920 + if (s->level > 0) { 1.921 + 1.922 + /* Check if the file is binary or text */ 1.923 + if (s->strm->data_type == Z_UNKNOWN) 1.924 + s->strm->data_type = detect_data_type(s); 1.925 + 1.926 + /* Construct the literal and distance trees */ 1.927 + build_tree(s, (tree_desc *)(&(s->l_desc))); 1.928 + Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len, 1.929 + s->static_len)); 1.930 + 1.931 + build_tree(s, (tree_desc *)(&(s->d_desc))); 1.932 + Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len, 1.933 + s->static_len)); 1.934 + /* At this point, opt_len and static_len are the total bit lengths of 1.935 + * the compressed block data, excluding the tree representations. 1.936 + */ 1.937 + 1.938 + /* Build the bit length tree for the above two trees, and get the index 1.939 + * in bl_order of the last bit length code to send. 1.940 + */ 1.941 + max_blindex = build_bl_tree(s); 1.942 + 1.943 + /* Determine the best encoding. Compute the block lengths in bytes. */ 1.944 + opt_lenb = (s->opt_len+3+7)>>3; 1.945 + static_lenb = (s->static_len+3+7)>>3; 1.946 + 1.947 + Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ", 1.948 + opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len, 1.949 + s->last_lit)); 1.950 + 1.951 + if (static_lenb <= opt_lenb) opt_lenb = static_lenb; 1.952 + 1.953 + } else { 1.954 + Assert(buf != (char*)0, "lost buf"); 1.955 + opt_lenb = static_lenb = stored_len + 5; /* force a stored block */ 1.956 + } 1.957 + 1.958 +#ifdef FORCE_STORED 1.959 + if (buf != (char*)0) { /* force stored block */ 1.960 +#else 1.961 + if (stored_len+4 <= opt_lenb && buf != (char*)0) { 1.962 + /* 4: two words for the lengths */ 1.963 +#endif 1.964 + /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE. 1.965 + * Otherwise we can't have processed more than WSIZE input bytes since 1.966 + * the last block flush, because compression would have been 1.967 + * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to 1.968 + * transform a block into a stored block. 1.969 + */ 1.970 + _tr_stored_block(s, buf, stored_len, last); 1.971 + 1.972 +#ifdef FORCE_STATIC 1.973 + } else if (static_lenb >= 0) { /* force static trees */ 1.974 +#else 1.975 + } else if (s->strategy == Z_FIXED || static_lenb == opt_lenb) { 1.976 +#endif 1.977 + send_bits(s, (STATIC_TREES<<1)+last, 3); 1.978 + compress_block(s, (const ct_data *)static_ltree, 1.979 + (const ct_data *)static_dtree); 1.980 +#ifdef DEBUG 1.981 + s->compressed_len += 3 + s->static_len; 1.982 +#endif 1.983 + } else { 1.984 + send_bits(s, (DYN_TREES<<1)+last, 3); 1.985 + send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1, 1.986 + max_blindex+1); 1.987 + compress_block(s, (const ct_data *)s->dyn_ltree, 1.988 + (const ct_data *)s->dyn_dtree); 1.989 +#ifdef DEBUG 1.990 + s->compressed_len += 3 + s->opt_len; 1.991 +#endif 1.992 + } 1.993 + Assert (s->compressed_len == s->bits_sent, "bad compressed size"); 1.994 + /* The above check is made mod 2^32, for files larger than 512 MB 1.995 + * and uLong implemented on 32 bits. 1.996 + */ 1.997 + init_block(s); 1.998 + 1.999 + if (last) { 1.1000 + bi_windup(s); 1.1001 +#ifdef DEBUG 1.1002 + s->compressed_len += 7; /* align on byte boundary */ 1.1003 +#endif 1.1004 + } 1.1005 + Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3, 1.1006 + s->compressed_len-7*last)); 1.1007 +} 1.1008 + 1.1009 +/* =========================================================================== 1.1010 + * Save the match info and tally the frequency counts. Return true if 1.1011 + * the current block must be flushed. 1.1012 + */ 1.1013 +int ZLIB_INTERNAL _tr_tally (s, dist, lc) 1.1014 + deflate_state *s; 1.1015 + unsigned dist; /* distance of matched string */ 1.1016 + unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */ 1.1017 +{ 1.1018 + s->d_buf[s->last_lit] = (ush)dist; 1.1019 + s->l_buf[s->last_lit++] = (uch)lc; 1.1020 + if (dist == 0) { 1.1021 + /* lc is the unmatched char */ 1.1022 + s->dyn_ltree[lc].Freq++; 1.1023 + } else { 1.1024 + s->matches++; 1.1025 + /* Here, lc is the match length - MIN_MATCH */ 1.1026 + dist--; /* dist = match distance - 1 */ 1.1027 + Assert((ush)dist < (ush)MAX_DIST(s) && 1.1028 + (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) && 1.1029 + (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match"); 1.1030 + 1.1031 + s->dyn_ltree[_length_code[lc]+LITERALS+1].Freq++; 1.1032 + s->dyn_dtree[d_code(dist)].Freq++; 1.1033 + } 1.1034 + 1.1035 +#ifdef TRUNCATE_BLOCK 1.1036 + /* Try to guess if it is profitable to stop the current block here */ 1.1037 + if ((s->last_lit & 0x1fff) == 0 && s->level > 2) { 1.1038 + /* Compute an upper bound for the compressed length */ 1.1039 + ulg out_length = (ulg)s->last_lit*8L; 1.1040 + ulg in_length = (ulg)((long)s->strstart - s->block_start); 1.1041 + int dcode; 1.1042 + for (dcode = 0; dcode < D_CODES; dcode++) { 1.1043 + out_length += (ulg)s->dyn_dtree[dcode].Freq * 1.1044 + (5L+extra_dbits[dcode]); 1.1045 + } 1.1046 + out_length >>= 3; 1.1047 + Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ", 1.1048 + s->last_lit, in_length, out_length, 1.1049 + 100L - out_length*100L/in_length)); 1.1050 + if (s->matches < s->last_lit/2 && out_length < in_length/2) return 1; 1.1051 + } 1.1052 +#endif 1.1053 + return (s->last_lit == s->lit_bufsize-1); 1.1054 + /* We avoid equality with lit_bufsize because of wraparound at 64K 1.1055 + * on 16 bit machines and because stored blocks are restricted to 1.1056 + * 64K-1 bytes. 1.1057 + */ 1.1058 +} 1.1059 + 1.1060 +/* =========================================================================== 1.1061 + * Send the block data compressed using the given Huffman trees 1.1062 + */ 1.1063 +local void compress_block(s, ltree, dtree) 1.1064 + deflate_state *s; 1.1065 + const ct_data *ltree; /* literal tree */ 1.1066 + const ct_data *dtree; /* distance tree */ 1.1067 +{ 1.1068 + unsigned dist; /* distance of matched string */ 1.1069 + int lc; /* match length or unmatched char (if dist == 0) */ 1.1070 + unsigned lx = 0; /* running index in l_buf */ 1.1071 + unsigned code; /* the code to send */ 1.1072 + int extra; /* number of extra bits to send */ 1.1073 + 1.1074 + if (s->last_lit != 0) do { 1.1075 + dist = s->d_buf[lx]; 1.1076 + lc = s->l_buf[lx++]; 1.1077 + if (dist == 0) { 1.1078 + send_code(s, lc, ltree); /* send a literal byte */ 1.1079 + Tracecv(isgraph(lc), (stderr," '%c' ", lc)); 1.1080 + } else { 1.1081 + /* Here, lc is the match length - MIN_MATCH */ 1.1082 + code = _length_code[lc]; 1.1083 + send_code(s, code+LITERALS+1, ltree); /* send the length code */ 1.1084 + extra = extra_lbits[code]; 1.1085 + if (extra != 0) { 1.1086 + lc -= base_length[code]; 1.1087 + send_bits(s, lc, extra); /* send the extra length bits */ 1.1088 + } 1.1089 + dist--; /* dist is now the match distance - 1 */ 1.1090 + code = d_code(dist); 1.1091 + Assert (code < D_CODES, "bad d_code"); 1.1092 + 1.1093 + send_code(s, code, dtree); /* send the distance code */ 1.1094 + extra = extra_dbits[code]; 1.1095 + if (extra != 0) { 1.1096 + dist -= base_dist[code]; 1.1097 + send_bits(s, dist, extra); /* send the extra distance bits */ 1.1098 + } 1.1099 + } /* literal or match pair ? */ 1.1100 + 1.1101 + /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */ 1.1102 + Assert((uInt)(s->pending) < s->lit_bufsize + 2*lx, 1.1103 + "pendingBuf overflow"); 1.1104 + 1.1105 + } while (lx < s->last_lit); 1.1106 + 1.1107 + send_code(s, END_BLOCK, ltree); 1.1108 +} 1.1109 + 1.1110 +/* =========================================================================== 1.1111 + * Check if the data type is TEXT or BINARY, using the following algorithm: 1.1112 + * - TEXT if the two conditions below are satisfied: 1.1113 + * a) There are no non-portable control characters belonging to the 1.1114 + * "black list" (0..6, 14..25, 28..31). 1.1115 + * b) There is at least one printable character belonging to the 1.1116 + * "white list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255). 1.1117 + * - BINARY otherwise. 1.1118 + * - The following partially-portable control characters form a 1.1119 + * "gray list" that is ignored in this detection algorithm: 1.1120 + * (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}). 1.1121 + * IN assertion: the fields Freq of dyn_ltree are set. 1.1122 + */ 1.1123 +local int detect_data_type(s) 1.1124 + deflate_state *s; 1.1125 +{ 1.1126 + /* black_mask is the bit mask of black-listed bytes 1.1127 + * set bits 0..6, 14..25, and 28..31 1.1128 + * 0xf3ffc07f = binary 11110011111111111100000001111111 1.1129 + */ 1.1130 + unsigned long black_mask = 0xf3ffc07fUL; 1.1131 + int n; 1.1132 + 1.1133 + /* Check for non-textual ("black-listed") bytes. */ 1.1134 + for (n = 0; n <= 31; n++, black_mask >>= 1) 1.1135 + if ((black_mask & 1) && (s->dyn_ltree[n].Freq != 0)) 1.1136 + return Z_BINARY; 1.1137 + 1.1138 + /* Check for textual ("white-listed") bytes. */ 1.1139 + if (s->dyn_ltree[9].Freq != 0 || s->dyn_ltree[10].Freq != 0 1.1140 + || s->dyn_ltree[13].Freq != 0) 1.1141 + return Z_TEXT; 1.1142 + for (n = 32; n < LITERALS; n++) 1.1143 + if (s->dyn_ltree[n].Freq != 0) 1.1144 + return Z_TEXT; 1.1145 + 1.1146 + /* There are no "black-listed" or "white-listed" bytes: 1.1147 + * this stream either is empty or has tolerated ("gray-listed") bytes only. 1.1148 + */ 1.1149 + return Z_BINARY; 1.1150 +} 1.1151 + 1.1152 +/* =========================================================================== 1.1153 + * Reverse the first len bits of a code, using straightforward code (a faster 1.1154 + * method would use a table) 1.1155 + * IN assertion: 1 <= len <= 15 1.1156 + */ 1.1157 +local unsigned bi_reverse(code, len) 1.1158 + unsigned code; /* the value to invert */ 1.1159 + int len; /* its bit length */ 1.1160 +{ 1.1161 + register unsigned res = 0; 1.1162 + do { 1.1163 + res |= code & 1; 1.1164 + code >>= 1, res <<= 1; 1.1165 + } while (--len > 0); 1.1166 + return res >> 1; 1.1167 +} 1.1168 + 1.1169 +/* =========================================================================== 1.1170 + * Flush the bit buffer, keeping at most 7 bits in it. 1.1171 + */ 1.1172 +local void bi_flush(s) 1.1173 + deflate_state *s; 1.1174 +{ 1.1175 + if (s->bi_valid == 16) { 1.1176 + put_short(s, s->bi_buf); 1.1177 + s->bi_buf = 0; 1.1178 + s->bi_valid = 0; 1.1179 + } else if (s->bi_valid >= 8) { 1.1180 + put_byte(s, (Byte)s->bi_buf); 1.1181 + s->bi_buf >>= 8; 1.1182 + s->bi_valid -= 8; 1.1183 + } 1.1184 +} 1.1185 + 1.1186 +/* =========================================================================== 1.1187 + * Flush the bit buffer and align the output on a byte boundary 1.1188 + */ 1.1189 +local void bi_windup(s) 1.1190 + deflate_state *s; 1.1191 +{ 1.1192 + if (s->bi_valid > 8) { 1.1193 + put_short(s, s->bi_buf); 1.1194 + } else if (s->bi_valid > 0) { 1.1195 + put_byte(s, (Byte)s->bi_buf); 1.1196 + } 1.1197 + s->bi_buf = 0; 1.1198 + s->bi_valid = 0; 1.1199 +#ifdef DEBUG 1.1200 + s->bits_sent = (s->bits_sent+7) & ~7; 1.1201 +#endif 1.1202 +} 1.1203 + 1.1204 +/* =========================================================================== 1.1205 + * Copy a stored block, storing first the length and its 1.1206 + * one's complement if requested. 1.1207 + */ 1.1208 +local void copy_block(s, buf, len, header) 1.1209 + deflate_state *s; 1.1210 + charf *buf; /* the input data */ 1.1211 + unsigned len; /* its length */ 1.1212 + int header; /* true if block header must be written */ 1.1213 +{ 1.1214 + bi_windup(s); /* align on byte boundary */ 1.1215 + 1.1216 + if (header) { 1.1217 + put_short(s, (ush)len); 1.1218 + put_short(s, (ush)~len); 1.1219 +#ifdef DEBUG 1.1220 + s->bits_sent += 2*16; 1.1221 +#endif 1.1222 + } 1.1223 +#ifdef DEBUG 1.1224 + s->bits_sent += (ulg)len<<3; 1.1225 +#endif 1.1226 + while (len--) { 1.1227 + put_byte(s, *buf++); 1.1228 + } 1.1229 +}