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