istereo

annotate libs/zlib/trees.c @ 36:834503dcb486

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