Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
trees.c
00001 /* trees.c -- output deflated data using Huffman coding 00002 * Copyright (C) 1995-2012 Jean-loup Gailly 00003 * detect_data_type() function provided freely by Cosmin Truta, 2006 00004 * For conditions of distribution and use, see copyright notice in zlib.h 00005 */ 00006 00007 /* 00008 * ALGORITHM 00009 * 00010 * The "deflation" process uses several Huffman trees. The more 00011 * common source values are represented by shorter bit sequences. 00012 * 00013 * Each code tree is stored in a compressed form which is itself 00014 * a Huffman encoding of the lengths of all the code strings (in 00015 * ascending order by source values). The actual code strings are 00016 * reconstructed from the lengths in the inflate process, as described 00017 * in the deflate specification. 00018 * 00019 * REFERENCES 00020 * 00021 * Deutsch, L.P.,"'Deflate' Compressed Data Format Specification". 00022 * Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc 00023 * 00024 * Storer, James A. 00025 * Data Compression: Methods and Theory, pp. 49-50. 00026 * Computer Science Press, 1988. ISBN 0-7167-8156-5. 00027 * 00028 * Sedgewick, R. 00029 * Algorithms, p290. 00030 * Addison-Wesley, 1983. ISBN 0-201-06672-6. 00031 */ 00032 00033 /* @(#) $Id$ */ 00034 00035 /* #define GEN_TREES_H */ 00036 00037 #include "deflate.h" 00038 00039 #ifdef DEBUG 00040 # include <ctype.h> 00041 #endif 00042 00043 /* =========================================================================== 00044 * Constants 00045 */ 00046 00047 #define MAX_BL_BITS 7 00048 /* Bit length codes must not exceed MAX_BL_BITS bits */ 00049 00050 #define END_BLOCK 256 00051 /* end of block literal code */ 00052 00053 #define REP_3_6 16 00054 /* repeat previous bit length 3-6 times (2 bits of repeat count) */ 00055 00056 #define REPZ_3_10 17 00057 /* repeat a zero length 3-10 times (3 bits of repeat count) */ 00058 00059 #define REPZ_11_138 18 00060 /* repeat a zero length 11-138 times (7 bits of repeat count) */ 00061 00062 local const int extra_lbits[LENGTH_CODES] /* extra bits for each length code */ 00063 = {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}; 00064 00065 local const int extra_dbits[D_CODES] /* extra bits for each distance code */ 00066 = {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}; 00067 00068 local const int extra_blbits[BL_CODES]/* extra bits for each bit length code */ 00069 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7}; 00070 00071 local const uch bl_order[BL_CODES] 00072 = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15}; 00073 /* The lengths of the bit length codes are sent in order of decreasing 00074 * probability, to avoid transmitting the lengths for unused bit length codes. 00075 */ 00076 00077 /* =========================================================================== 00078 * Local data. These are initialized only once. 00079 */ 00080 00081 #define DIST_CODE_LEN 512 /* see definition of array dist_code below */ 00082 00083 #if defined(GEN_TREES_H) || !defined(STDC) 00084 /* non ANSI compilers may not accept trees.h */ 00085 00086 local ct_data static_ltree[L_CODES+2]; 00087 /* The static literal tree. Since the bit lengths are imposed, there is no 00088 * need for the L_CODES extra codes used during heap construction. However 00089 * The codes 286 and 287 are needed to build a canonical tree (see _tr_init 00090 * below). 00091 */ 00092 00093 local ct_data static_dtree[D_CODES]; 00094 /* The static distance tree. (Actually a trivial tree since all codes use 00095 * 5 bits.) 00096 */ 00097 00098 uch _dist_code[DIST_CODE_LEN]; 00099 /* Distance codes. The first 256 values correspond to the distances 00100 * 3 .. 258, the last 256 values correspond to the top 8 bits of 00101 * the 15 bit distances. 00102 */ 00103 00104 uch _length_code[MAX_MATCH-MIN_MATCH+1]; 00105 /* length code for each normalized match length (0 == MIN_MATCH) */ 00106 00107 local int base_length[LENGTH_CODES]; 00108 /* First normalized length for each code (0 = MIN_MATCH) */ 00109 00110 local int base_dist[D_CODES]; 00111 /* First normalized distance for each code (0 = distance of 1) */ 00112 00113 #else 00114 # include "trees.h" 00115 #endif /* GEN_TREES_H */ 00116 00117 struct static_tree_desc_s { 00118 const ct_data *static_tree; /* static tree or NULL */ 00119 const intf *extra_bits; /* extra bits for each code or NULL */ 00120 int extra_base; /* base index for extra_bits */ 00121 int elems; /* max number of elements in the tree */ 00122 int max_length; /* max bit length for the codes */ 00123 }; 00124 00125 local static_tree_desc static_l_desc = 00126 {static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS}; 00127 00128 local static_tree_desc static_d_desc = 00129 {static_dtree, extra_dbits, 0, D_CODES, MAX_BITS}; 00130 00131 local static_tree_desc static_bl_desc = 00132 {(const ct_data *)0, extra_blbits, 0, BL_CODES, MAX_BL_BITS}; 00133 00134 /* =========================================================================== 00135 * Local (static) routines in this file. 00136 */ 00137 00138 local void tr_static_init OF((void)); 00139 local void init_block OF((deflate_state *s)); 00140 local void pqdownheap OF((deflate_state *s, ct_data *tree, int k)); 00141 local void gen_bitlen OF((deflate_state *s, tree_desc *desc)); 00142 local void gen_codes OF((ct_data *tree, int max_code, ushf *bl_count)); 00143 local void build_tree OF((deflate_state *s, tree_desc *desc)); 00144 local void scan_tree OF((deflate_state *s, ct_data *tree, int max_code)); 00145 local void send_tree OF((deflate_state *s, ct_data *tree, int max_code)); 00146 local int build_bl_tree OF((deflate_state *s)); 00147 local void send_all_trees OF((deflate_state *s, int lcodes, int dcodes, 00148 int blcodes)); 00149 local void compress_block OF((deflate_state *s, ct_data *ltree, 00150 ct_data *dtree)); 00151 local int detect_data_type OF((deflate_state *s)); 00152 local unsigned bi_reverse OF((unsigned value, int length)); 00153 local void bi_windup OF((deflate_state *s)); 00154 local void bi_flush OF((deflate_state *s)); 00155 local void copy_block OF((deflate_state *s, charf *buf, unsigned len, 00156 int header)); 00157 00158 #ifdef GEN_TREES_H 00159 local void gen_trees_header OF((void)); 00160 #endif 00161 00162 #ifndef DEBUG 00163 # define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len) 00164 /* Send a code of the given tree. c and tree must not have side effects */ 00165 00166 #else /* DEBUG */ 00167 # define send_code(s, c, tree) \ 00168 { if (z_verbose>2) fprintf(stderr,"\ncd %3d ",(c)); \ 00169 send_bits(s, tree[c].Code, tree[c].Len); } 00170 #endif 00171 00172 /* =========================================================================== 00173 * Output a short LSB first on the stream. 00174 * IN assertion: there is enough room in pendingBuf. 00175 */ 00176 #define put_short(s, w) { \ 00177 put_byte(s, (uch)((w) & 0xff)); \ 00178 put_byte(s, (uch)((ush)(w) >> 8)); \ 00179 } 00180 00181 /* =========================================================================== 00182 * Send a value on a given number of bits. 00183 * IN assertion: length <= 16 and value fits in length bits. 00184 */ 00185 #ifdef DEBUG 00186 local void send_bits OF((deflate_state *s, int value, int length)); 00187 00188 local void send_bits(s, value, length) 00189 deflate_state *s; 00190 int value; /* value to send */ 00191 int length; /* number of bits */ 00192 { 00193 Tracevv((stderr," l %2d v %4x ", length, value)); 00194 Assert(length > 0 && length <= 15, "invalid length"); 00195 s->bits_sent += (ulg)length; 00196 00197 /* If not enough room in bi_buf, use (valid) bits from bi_buf and 00198 * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid)) 00199 * unused bits in value. 00200 */ 00201 if (s->bi_valid > (int)Buf_size - length) { 00202 s->bi_buf |= (ush)value << s->bi_valid; 00203 put_short(s, s->bi_buf); 00204 s->bi_buf = (ush)value >> (Buf_size - s->bi_valid); 00205 s->bi_valid += length - Buf_size; 00206 } else { 00207 s->bi_buf |= (ush)value << s->bi_valid; 00208 s->bi_valid += length; 00209 } 00210 } 00211 #else /* !DEBUG */ 00212 00213 #define send_bits(s, value, length) \ 00214 { int len = length;\ 00215 if (s->bi_valid > (int)Buf_size - len) {\ 00216 int val = value;\ 00217 s->bi_buf |= (ush)val << s->bi_valid;\ 00218 put_short(s, s->bi_buf);\ 00219 s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\ 00220 s->bi_valid += len - Buf_size;\ 00221 } else {\ 00222 s->bi_buf |= (ush)(value) << s->bi_valid;\ 00223 s->bi_valid += len;\ 00224 }\ 00225 } 00226 #endif /* DEBUG */ 00227 00228 00229 /* the arguments must not have side effects */ 00230 00231 /* =========================================================================== 00232 * Initialize the various 'constant' tables. 00233 */ 00234 local void tr_static_init() 00235 { 00236 #if defined(GEN_TREES_H) || !defined(STDC) 00237 static int static_init_done = 0; 00238 int n; /* iterates over tree elements */ 00239 int bits; /* bit counter */ 00240 int length; /* length value */ 00241 int code; /* code value */ 00242 int dist; /* distance index */ 00243 ush bl_count[MAX_BITS+1]; 00244 /* number of codes at each bit length for an optimal tree */ 00245 00246 if (static_init_done) return; 00247 00248 /* For some embedded targets, global variables are not initialized: */ 00249 #ifdef NO_INIT_GLOBAL_POINTERS 00250 static_l_desc.static_tree = static_ltree; 00251 static_l_desc.extra_bits = extra_lbits; 00252 static_d_desc.static_tree = static_dtree; 00253 static_d_desc.extra_bits = extra_dbits; 00254 static_bl_desc.extra_bits = extra_blbits; 00255 #endif 00256 00257 /* Initialize the mapping length (0..255) -> length code (0..28) */ 00258 length = 0; 00259 for (code = 0; code < LENGTH_CODES-1; code++) { 00260 base_length[code] = length; 00261 for (n = 0; n < (1<<extra_lbits[code]); n++) { 00262 _length_code[length++] = (uch)code; 00263 } 00264 } 00265 Assert (length == 256, "tr_static_init: length != 256"); 00266 /* Note that the length 255 (match length 258) can be represented 00267 * in two different ways: code 284 + 5 bits or code 285, so we 00268 * overwrite length_code[255] to use the best encoding: 00269 */ 00270 _length_code[length-1] = (uch)code; 00271 00272 /* Initialize the mapping dist (0..32K) -> dist code (0..29) */ 00273 dist = 0; 00274 for (code = 0 ; code < 16; code++) { 00275 base_dist[code] = dist; 00276 for (n = 0; n < (1<<extra_dbits[code]); n++) { 00277 _dist_code[dist++] = (uch)code; 00278 } 00279 } 00280 Assert (dist == 256, "tr_static_init: dist != 256"); 00281 dist >>= 7; /* from now on, all distances are divided by 128 */ 00282 for ( ; code < D_CODES; code++) { 00283 base_dist[code] = dist << 7; 00284 for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) { 00285 _dist_code[256 + dist++] = (uch)code; 00286 } 00287 } 00288 Assert (dist == 256, "tr_static_init: 256+dist != 512"); 00289 00290 /* Construct the codes of the static literal tree */ 00291 for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0; 00292 n = 0; 00293 while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++; 00294 while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++; 00295 while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++; 00296 while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++; 00297 /* Codes 286 and 287 do not exist, but we must include them in the 00298 * tree construction to get a canonical Huffman tree (longest code 00299 * all ones) 00300 */ 00301 gen_codes((ct_data *)static_ltree, L_CODES+1, bl_count); 00302 00303 /* The static distance tree is trivial: */ 00304 for (n = 0; n < D_CODES; n++) { 00305 static_dtree[n].Len = 5; 00306 static_dtree[n].Code = bi_reverse((unsigned)n, 5); 00307 } 00308 static_init_done = 1; 00309 00310 # ifdef GEN_TREES_H 00311 gen_trees_header(); 00312 # endif 00313 #endif /* defined(GEN_TREES_H) || !defined(STDC) */ 00314 } 00315 00316 /* =========================================================================== 00317 * Genererate the file trees.h describing the static trees. 00318 */ 00319 #ifdef GEN_TREES_H 00320 # ifndef DEBUG 00321 # include <stdio.h> 00322 # endif 00323 00324 # define SEPARATOR(i, last, width) \ 00325 ((i) == (last)? "\n};\n\n" : \ 00326 ((i) % (width) == (width)-1 ? ",\n" : ", ")) 00327 00328 void gen_trees_header() 00329 { 00330 FILE *header = fopen("trees.h", "w"); 00331 int i; 00332 00333 Assert (header != NULL, "Can't open trees.h"); 00334 fprintf(header, 00335 "/* header created automatically with -DGEN_TREES_H */\n\n"); 00336 00337 fprintf(header, "local const ct_data static_ltree[L_CODES+2] = {\n"); 00338 for (i = 0; i < L_CODES+2; i++) { 00339 fprintf(header, "{{%3u},{%3u}}%s", static_ltree[i].Code, 00340 static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5)); 00341 } 00342 00343 fprintf(header, "local const ct_data static_dtree[D_CODES] = {\n"); 00344 for (i = 0; i < D_CODES; i++) { 00345 fprintf(header, "{{%2u},{%2u}}%s", static_dtree[i].Code, 00346 static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5)); 00347 } 00348 00349 fprintf(header, "const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = {\n"); 00350 for (i = 0; i < DIST_CODE_LEN; i++) { 00351 fprintf(header, "%2u%s", _dist_code[i], 00352 SEPARATOR(i, DIST_CODE_LEN-1, 20)); 00353 } 00354 00355 fprintf(header, 00356 "const uch ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= {\n"); 00357 for (i = 0; i < MAX_MATCH-MIN_MATCH+1; i++) { 00358 fprintf(header, "%2u%s", _length_code[i], 00359 SEPARATOR(i, MAX_MATCH-MIN_MATCH, 20)); 00360 } 00361 00362 fprintf(header, "local const int base_length[LENGTH_CODES] = {\n"); 00363 for (i = 0; i < LENGTH_CODES; i++) { 00364 fprintf(header, "%1u%s", base_length[i], 00365 SEPARATOR(i, LENGTH_CODES-1, 20)); 00366 } 00367 00368 fprintf(header, "local const int base_dist[D_CODES] = {\n"); 00369 for (i = 0; i < D_CODES; i++) { 00370 fprintf(header, "%5u%s", base_dist[i], 00371 SEPARATOR(i, D_CODES-1, 10)); 00372 } 00373 00374 fclose(header); 00375 } 00376 #endif /* GEN_TREES_H */ 00377 00378 /* =========================================================================== 00379 * Initialize the tree data structures for a new zlib stream. 00380 */ 00381 void ZLIB_INTERNAL _tr_init(s) 00382 deflate_state *s; 00383 { 00384 tr_static_init(); 00385 00386 s->l_desc.dyn_tree = s->dyn_ltree; 00387 s->l_desc.stat_desc = &static_l_desc; 00388 00389 s->d_desc.dyn_tree = s->dyn_dtree; 00390 s->d_desc.stat_desc = &static_d_desc; 00391 00392 s->bl_desc.dyn_tree = s->bl_tree; 00393 s->bl_desc.stat_desc = &static_bl_desc; 00394 00395 s->bi_buf = 0; 00396 s->bi_valid = 0; 00397 #ifdef DEBUG 00398 s->compressed_len = 0L; 00399 s->bits_sent = 0L; 00400 #endif 00401 00402 /* Initialize the first block of the first file: */ 00403 init_block(s); 00404 } 00405 00406 /* =========================================================================== 00407 * Initialize a new block. 00408 */ 00409 local void init_block(s) 00410 deflate_state *s; 00411 { 00412 int n; /* iterates over tree elements */ 00413 00414 /* Initialize the trees. */ 00415 for (n = 0; n < L_CODES; n++) s->dyn_ltree[n].Freq = 0; 00416 for (n = 0; n < D_CODES; n++) s->dyn_dtree[n].Freq = 0; 00417 for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0; 00418 00419 s->dyn_ltree[END_BLOCK].Freq = 1; 00420 s->opt_len = s->static_len = 0L; 00421 s->last_lit = s->matches = 0; 00422 } 00423 00424 #define SMALLEST 1 00425 /* Index within the heap array of least frequent node in the Huffman tree */ 00426 00427 00428 /* =========================================================================== 00429 * Remove the smallest element from the heap and recreate the heap with 00430 * one less element. Updates heap and heap_len. 00431 */ 00432 #define pqremove(s, tree, top) \ 00433 {\ 00434 top = s->heap[SMALLEST]; \ 00435 s->heap[SMALLEST] = s->heap[s->heap_len--]; \ 00436 pqdownheap(s, tree, SMALLEST); \ 00437 } 00438 00439 /* =========================================================================== 00440 * Compares to subtrees, using the tree depth as tie breaker when 00441 * the subtrees have equal frequency. This minimizes the worst case length. 00442 */ 00443 #define smaller(tree, n, m, depth) \ 00444 (tree[n].Freq < tree[m].Freq || \ 00445 (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m])) 00446 00447 /* =========================================================================== 00448 * Restore the heap property by moving down the tree starting at node k, 00449 * exchanging a node with the smallest of its two sons if necessary, stopping 00450 * when the heap property is re-established (each father smaller than its 00451 * two sons). 00452 */ 00453 local void pqdownheap(s, tree, k) 00454 deflate_state *s; 00455 ct_data *tree; /* the tree to restore */ 00456 int k; /* node to move down */ 00457 { 00458 int v = s->heap[k]; 00459 int j = k << 1; /* left son of k */ 00460 while (j <= s->heap_len) { 00461 /* Set j to the smallest of the two sons: */ 00462 if (j < s->heap_len && 00463 smaller(tree, s->heap[j+1], s->heap[j], s->depth)) { 00464 j++; 00465 } 00466 /* Exit if v is smaller than both sons */ 00467 if (smaller(tree, v, s->heap[j], s->depth)) break; 00468 00469 /* Exchange v with the smallest son */ 00470 s->heap[k] = s->heap[j]; k = j; 00471 00472 /* And continue down the tree, setting j to the left son of k */ 00473 j <<= 1; 00474 } 00475 s->heap[k] = v; 00476 } 00477 00478 /* =========================================================================== 00479 * Compute the optimal bit lengths for a tree and update the total bit length 00480 * for the current block. 00481 * IN assertion: the fields freq and dad are set, heap[heap_max] and 00482 * above are the tree nodes sorted by increasing frequency. 00483 * OUT assertions: the field len is set to the optimal bit length, the 00484 * array bl_count contains the frequencies for each bit length. 00485 * The length opt_len is updated; static_len is also updated if stree is 00486 * not null. 00487 */ 00488 local void gen_bitlen(s, desc) 00489 deflate_state *s; 00490 tree_desc *desc; /* the tree descriptor */ 00491 { 00492 ct_data *tree = desc->dyn_tree; 00493 int max_code = desc->max_code; 00494 const ct_data *stree = desc->stat_desc->static_tree; 00495 const intf *extra = desc->stat_desc->extra_bits; 00496 int base = desc->stat_desc->extra_base; 00497 int max_length = desc->stat_desc->max_length; 00498 int h; /* heap index */ 00499 int n, m; /* iterate over the tree elements */ 00500 int bits; /* bit length */ 00501 int xbits; /* extra bits */ 00502 ush f; /* frequency */ 00503 int overflow = 0; /* number of elements with bit length too large */ 00504 00505 for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0; 00506 00507 /* In a first pass, compute the optimal bit lengths (which may 00508 * overflow in the case of the bit length tree). 00509 */ 00510 tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */ 00511 00512 for (h = s->heap_max+1; h < HEAP_SIZE; h++) { 00513 n = s->heap[h]; 00514 bits = tree[tree[n].Dad].Len + 1; 00515 if (bits > max_length) bits = max_length, overflow++; 00516 tree[n].Len = (ush)bits; 00517 /* We overwrite tree[n].Dad which is no longer needed */ 00518 00519 if (n > max_code) continue; /* not a leaf node */ 00520 00521 s->bl_count[bits]++; 00522 xbits = 0; 00523 if (n >= base) xbits = extra[n-base]; 00524 f = tree[n].Freq; 00525 s->opt_len += (ulg)f * (bits + xbits); 00526 if (stree) s->static_len += (ulg)f * (stree[n].Len + xbits); 00527 } 00528 if (overflow == 0) return; 00529 00530 Trace((stderr,"\nbit length overflow\n")); 00531 /* This happens for example on obj2 and pic of the Calgary corpus */ 00532 00533 /* Find the first bit length which could increase: */ 00534 do { 00535 bits = max_length-1; 00536 while (s->bl_count[bits] == 0) bits--; 00537 s->bl_count[bits]--; /* move one leaf down the tree */ 00538 s->bl_count[bits+1] += 2; /* move one overflow item as its brother */ 00539 s->bl_count[max_length]--; 00540 /* The brother of the overflow item also moves one step up, 00541 * but this does not affect bl_count[max_length] 00542 */ 00543 overflow -= 2; 00544 } while (overflow > 0); 00545 00546 /* Now recompute all bit lengths, scanning in increasing frequency. 00547 * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all 00548 * lengths instead of fixing only the wrong ones. This idea is taken 00549 * from 'ar' written by Haruhiko Okumura.) 00550 */ 00551 for (bits = max_length; bits != 0; bits--) { 00552 n = s->bl_count[bits]; 00553 while (n != 0) { 00554 m = s->heap[--h]; 00555 if (m > max_code) continue; 00556 if ((unsigned) tree[m].Len != (unsigned) bits) { 00557 Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits)); 00558 s->opt_len += ((long)bits - (long)tree[m].Len) 00559 *(long)tree[m].Freq; 00560 tree[m].Len = (ush)bits; 00561 } 00562 n--; 00563 } 00564 } 00565 } 00566 00567 /* =========================================================================== 00568 * Generate the codes for a given tree and bit counts (which need not be 00569 * optimal). 00570 * IN assertion: the array bl_count contains the bit length statistics for 00571 * the given tree and the field len is set for all tree elements. 00572 * OUT assertion: the field code is set for all tree elements of non 00573 * zero code length. 00574 */ 00575 local void gen_codes (tree, max_code, bl_count) 00576 ct_data *tree; /* the tree to decorate */ 00577 int max_code; /* largest code with non zero frequency */ 00578 ushf *bl_count; /* number of codes at each bit length */ 00579 { 00580 ush next_code[MAX_BITS+1]; /* next code value for each bit length */ 00581 ush code = 0; /* running code value */ 00582 int bits; /* bit index */ 00583 int n; /* code index */ 00584 00585 /* The distribution counts are first used to generate the code values 00586 * without bit reversal. 00587 */ 00588 for (bits = 1; bits <= MAX_BITS; bits++) { 00589 next_code[bits] = code = (code + bl_count[bits-1]) << 1; 00590 } 00591 /* Check that the bit counts in bl_count are consistent. The last code 00592 * must be all ones. 00593 */ 00594 Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1, 00595 "inconsistent bit counts"); 00596 Tracev((stderr,"\ngen_codes: max_code %d ", max_code)); 00597 00598 for (n = 0; n <= max_code; n++) { 00599 int len = tree[n].Len; 00600 if (len == 0) continue; 00601 /* Now reverse the bits */ 00602 tree[n].Code = bi_reverse(next_code[len]++, len); 00603 00604 Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ", 00605 n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1)); 00606 } 00607 } 00608 00609 /* =========================================================================== 00610 * Construct one Huffman tree and assigns the code bit strings and lengths. 00611 * Update the total bit length for the current block. 00612 * IN assertion: the field freq is set for all tree elements. 00613 * OUT assertions: the fields len and code are set to the optimal bit length 00614 * and corresponding code. The length opt_len is updated; static_len is 00615 * also updated if stree is not null. The field max_code is set. 00616 */ 00617 local void build_tree(s, desc) 00618 deflate_state *s; 00619 tree_desc *desc; /* the tree descriptor */ 00620 { 00621 ct_data *tree = desc->dyn_tree; 00622 const ct_data *stree = desc->stat_desc->static_tree; 00623 int elems = desc->stat_desc->elems; 00624 int n, m; /* iterate over heap elements */ 00625 int max_code = -1; /* largest code with non zero frequency */ 00626 int node; /* new node being created */ 00627 00628 /* Construct the initial heap, with least frequent element in 00629 * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1]. 00630 * heap[0] is not used. 00631 */ 00632 s->heap_len = 0, s->heap_max = HEAP_SIZE; 00633 00634 for (n = 0; n < elems; n++) { 00635 if (tree[n].Freq != 0) { 00636 s->heap[++(s->heap_len)] = max_code = n; 00637 s->depth[n] = 0; 00638 } else { 00639 tree[n].Len = 0; 00640 } 00641 } 00642 00643 /* The pkzip format requires that at least one distance code exists, 00644 * and that at least one bit should be sent even if there is only one 00645 * possible code. So to avoid special checks later on we force at least 00646 * two codes of non zero frequency. 00647 */ 00648 while (s->heap_len < 2) { 00649 node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0); 00650 tree[node].Freq = 1; 00651 s->depth[node] = 0; 00652 s->opt_len--; if (stree) s->static_len -= stree[node].Len; 00653 /* node is 0 or 1 so it does not have extra bits */ 00654 } 00655 desc->max_code = max_code; 00656 00657 /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree, 00658 * establish sub-heaps of increasing lengths: 00659 */ 00660 for (n = s->heap_len/2; n >= 1; n--) pqdownheap(s, tree, n); 00661 00662 /* Construct the Huffman tree by repeatedly combining the least two 00663 * frequent nodes. 00664 */ 00665 node = elems; /* next internal node of the tree */ 00666 do { 00667 pqremove(s, tree, n); /* n = node of least frequency */ 00668 m = s->heap[SMALLEST]; /* m = node of next least frequency */ 00669 00670 s->heap[--(s->heap_max)] = n; /* keep the nodes sorted by frequency */ 00671 s->heap[--(s->heap_max)] = m; 00672 00673 /* Create a new node father of n and m */ 00674 tree[node].Freq = tree[n].Freq + tree[m].Freq; 00675 s->depth[node] = (uch)((s->depth[n] >= s->depth[m] ? 00676 s->depth[n] : s->depth[m]) + 1); 00677 tree[n].Dad = tree[m].Dad = (ush)node; 00678 #ifdef DUMP_BL_TREE 00679 if (tree == s->bl_tree) { 00680 fprintf(stderr,"\nnode %d(%d), sons %d(%d) %d(%d)", 00681 node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq); 00682 } 00683 #endif 00684 /* and insert the new node in the heap */ 00685 s->heap[SMALLEST] = node++; 00686 pqdownheap(s, tree, SMALLEST); 00687 00688 } while (s->heap_len >= 2); 00689 00690 s->heap[--(s->heap_max)] = s->heap[SMALLEST]; 00691 00692 /* At this point, the fields freq and dad are set. We can now 00693 * generate the bit lengths. 00694 */ 00695 gen_bitlen(s, (tree_desc *)desc); 00696 00697 /* The field len is now set, we can generate the bit codes */ 00698 gen_codes ((ct_data *)tree, max_code, s->bl_count); 00699 } 00700 00701 /* =========================================================================== 00702 * Scan a literal or distance tree to determine the frequencies of the codes 00703 * in the bit length tree. 00704 */ 00705 local void scan_tree (s, tree, max_code) 00706 deflate_state *s; 00707 ct_data *tree; /* the tree to be scanned */ 00708 int max_code; /* and its largest code of non zero frequency */ 00709 { 00710 int n; /* iterates over all tree elements */ 00711 int prevlen = -1; /* last emitted length */ 00712 int curlen; /* length of current code */ 00713 int nextlen = tree[0].Len; /* length of next code */ 00714 int count = 0; /* repeat count of the current code */ 00715 int max_count = 7; /* max repeat count */ 00716 int min_count = 4; /* min repeat count */ 00717 00718 if (nextlen == 0) max_count = 138, min_count = 3; 00719 tree[max_code+1].Len = (ush)0xffff; /* guard */ 00720 00721 for (n = 0; n <= max_code; n++) { 00722 curlen = nextlen; nextlen = tree[n+1].Len; 00723 if (++count < max_count && curlen == nextlen) { 00724 continue; 00725 } else if (count < min_count) { 00726 s->bl_tree[curlen].Freq += count; 00727 } else if (curlen != 0) { 00728 if (curlen != prevlen) s->bl_tree[curlen].Freq++; 00729 s->bl_tree[REP_3_6].Freq++; 00730 } else if (count <= 10) { 00731 s->bl_tree[REPZ_3_10].Freq++; 00732 } else { 00733 s->bl_tree[REPZ_11_138].Freq++; 00734 } 00735 count = 0; prevlen = curlen; 00736 if (nextlen == 0) { 00737 max_count = 138, min_count = 3; 00738 } else if (curlen == nextlen) { 00739 max_count = 6, min_count = 3; 00740 } else { 00741 max_count = 7, min_count = 4; 00742 } 00743 } 00744 } 00745 00746 /* =========================================================================== 00747 * Send a literal or distance tree in compressed form, using the codes in 00748 * bl_tree. 00749 */ 00750 local void send_tree (s, tree, max_code) 00751 deflate_state *s; 00752 ct_data *tree; /* the tree to be scanned */ 00753 int max_code; /* and its largest code of non zero frequency */ 00754 { 00755 int n; /* iterates over all tree elements */ 00756 int prevlen = -1; /* last emitted length */ 00757 int curlen; /* length of current code */ 00758 int nextlen = tree[0].Len; /* length of next code */ 00759 int count = 0; /* repeat count of the current code */ 00760 int max_count = 7; /* max repeat count */ 00761 int min_count = 4; /* min repeat count */ 00762 00763 /* tree[max_code+1].Len = -1; */ /* guard already set */ 00764 if (nextlen == 0) max_count = 138, min_count = 3; 00765 00766 for (n = 0; n <= max_code; n++) { 00767 curlen = nextlen; nextlen = tree[n+1].Len; 00768 if (++count < max_count && curlen == nextlen) { 00769 continue; 00770 } else if (count < min_count) { 00771 do { send_code(s, curlen, s->bl_tree); } while (--count != 0); 00772 00773 } else if (curlen != 0) { 00774 if (curlen != prevlen) { 00775 send_code(s, curlen, s->bl_tree); count--; 00776 } 00777 Assert(count >= 3 && count <= 6, " 3_6?"); 00778 send_code(s, REP_3_6, s->bl_tree); send_bits(s, count-3, 2); 00779 00780 } else if (count <= 10) { 00781 send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count-3, 3); 00782 00783 } else { 00784 send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count-11, 7); 00785 } 00786 count = 0; prevlen = curlen; 00787 if (nextlen == 0) { 00788 max_count = 138, min_count = 3; 00789 } else if (curlen == nextlen) { 00790 max_count = 6, min_count = 3; 00791 } else { 00792 max_count = 7, min_count = 4; 00793 } 00794 } 00795 } 00796 00797 /* =========================================================================== 00798 * Construct the Huffman tree for the bit lengths and return the index in 00799 * bl_order of the last bit length code to send. 00800 */ 00801 local int build_bl_tree(s) 00802 deflate_state *s; 00803 { 00804 int max_blindex; /* index of last bit length code of non zero freq */ 00805 00806 /* Determine the bit length frequencies for literal and distance trees */ 00807 scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code); 00808 scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code); 00809 00810 /* Build the bit length tree: */ 00811 build_tree(s, (tree_desc *)(&(s->bl_desc))); 00812 /* opt_len now includes the length of the tree representations, except 00813 * the lengths of the bit lengths codes and the 5+5+4 bits for the counts. 00814 */ 00815 00816 /* Determine the number of bit length codes to send. The pkzip format 00817 * requires that at least 4 bit length codes be sent. (appnote.txt says 00818 * 3 but the actual value used is 4.) 00819 */ 00820 for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) { 00821 if (s->bl_tree[bl_order[max_blindex]].Len != 0) break; 00822 } 00823 /* Update opt_len to include the bit length tree and counts */ 00824 s->opt_len += 3*(max_blindex+1) + 5+5+4; 00825 Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld", 00826 s->opt_len, s->static_len)); 00827 00828 return max_blindex; 00829 } 00830 00831 /* =========================================================================== 00832 * Send the header for a block using dynamic Huffman trees: the counts, the 00833 * lengths of the bit length codes, the literal tree and the distance tree. 00834 * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4. 00835 */ 00836 local void send_all_trees(s, lcodes, dcodes, blcodes) 00837 deflate_state *s; 00838 int lcodes, dcodes, blcodes; /* number of codes for each tree */ 00839 { 00840 int rank; /* index in bl_order */ 00841 00842 Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes"); 00843 Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES, 00844 "too many codes"); 00845 Tracev((stderr, "\nbl counts: ")); 00846 send_bits(s, lcodes-257, 5); /* not +255 as stated in appnote.txt */ 00847 send_bits(s, dcodes-1, 5); 00848 send_bits(s, blcodes-4, 4); /* not -3 as stated in appnote.txt */ 00849 for (rank = 0; rank < blcodes; rank++) { 00850 Tracev((stderr, "\nbl code %2d ", bl_order[rank])); 00851 send_bits(s, s->bl_tree[bl_order[rank]].Len, 3); 00852 } 00853 Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent)); 00854 00855 send_tree(s, (ct_data *)s->dyn_ltree, lcodes-1); /* literal tree */ 00856 Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent)); 00857 00858 send_tree(s, (ct_data *)s->dyn_dtree, dcodes-1); /* distance tree */ 00859 Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent)); 00860 } 00861 00862 /* =========================================================================== 00863 * Send a stored block 00864 */ 00865 void ZLIB_INTERNAL _tr_stored_block(s, buf, stored_len, last) 00866 deflate_state *s; 00867 charf *buf; /* input block */ 00868 ulg stored_len; /* length of input block */ 00869 int last; /* one if this is the last block for a file */ 00870 { 00871 send_bits(s, (STORED_BLOCK<<1)+last, 3); /* send block type */ 00872 #ifdef DEBUG 00873 s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L; 00874 s->compressed_len += (stored_len + 4) << 3; 00875 #endif 00876 copy_block(s, buf, (unsigned)stored_len, 1); /* with header */ 00877 } 00878 00879 /* =========================================================================== 00880 * Flush the bits in the bit buffer to pending output (leaves at most 7 bits) 00881 */ 00882 void ZLIB_INTERNAL _tr_flush_bits(s) 00883 deflate_state *s; 00884 { 00885 bi_flush(s); 00886 } 00887 00888 /* =========================================================================== 00889 * Send one empty static block to give enough lookahead for inflate. 00890 * This takes 10 bits, of which 7 may remain in the bit buffer. 00891 */ 00892 void ZLIB_INTERNAL _tr_align(s) 00893 deflate_state *s; 00894 { 00895 send_bits(s, STATIC_TREES<<1, 3); 00896 send_code(s, END_BLOCK, static_ltree); 00897 #ifdef DEBUG 00898 s->compressed_len += 10L; /* 3 for block type, 7 for EOB */ 00899 #endif 00900 bi_flush(s); 00901 } 00902 00903 /* =========================================================================== 00904 * Determine the best encoding for the current block: dynamic trees, static 00905 * trees or store, and output the encoded block to the zip file. 00906 */ 00907 void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last) 00908 deflate_state *s; 00909 charf *buf; /* input block, or NULL if too old */ 00910 ulg stored_len; /* length of input block */ 00911 int last; /* one if this is the last block for a file */ 00912 { 00913 ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */ 00914 int max_blindex = 0; /* index of last bit length code of non zero freq */ 00915 00916 /* Build the Huffman trees unless a stored block is forced */ 00917 if (s->level > 0) { 00918 00919 /* Check if the file is binary or text */ 00920 if (s->strm->data_type == Z_UNKNOWN) 00921 s->strm->data_type = detect_data_type(s); 00922 00923 /* Construct the literal and distance trees */ 00924 build_tree(s, (tree_desc *)(&(s->l_desc))); 00925 Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len, 00926 s->static_len)); 00927 00928 build_tree(s, (tree_desc *)(&(s->d_desc))); 00929 Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len, 00930 s->static_len)); 00931 /* At this point, opt_len and static_len are the total bit lengths of 00932 * the compressed block data, excluding the tree representations. 00933 */ 00934 00935 /* Build the bit length tree for the above two trees, and get the index 00936 * in bl_order of the last bit length code to send. 00937 */ 00938 max_blindex = build_bl_tree(s); 00939 00940 /* Determine the best encoding. Compute the block lengths in bytes. */ 00941 opt_lenb = (s->opt_len+3+7)>>3; 00942 static_lenb = (s->static_len+3+7)>>3; 00943 00944 Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ", 00945 opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len, 00946 s->last_lit)); 00947 00948 if (static_lenb <= opt_lenb) opt_lenb = static_lenb; 00949 00950 } else { 00951 Assert(buf != (char*)0, "lost buf"); 00952 opt_lenb = static_lenb = stored_len + 5; /* force a stored block */ 00953 } 00954 00955 #ifdef FORCE_STORED 00956 if (buf != (char*)0) { /* force stored block */ 00957 #else 00958 if (stored_len+4 <= opt_lenb && buf != (char*)0) { 00959 /* 4: two words for the lengths */ 00960 #endif 00961 /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE. 00962 * Otherwise we can't have processed more than WSIZE input bytes since 00963 * the last block flush, because compression would have been 00964 * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to 00965 * transform a block into a stored block. 00966 */ 00967 _tr_stored_block(s, buf, stored_len, last); 00968 00969 #ifdef FORCE_STATIC 00970 } else if (static_lenb >= 0) { /* force static trees */ 00971 #else 00972 } else if (s->strategy == Z_FIXED || static_lenb == opt_lenb) { 00973 #endif 00974 send_bits(s, (STATIC_TREES<<1)+last, 3); 00975 compress_block(s, (ct_data *)static_ltree, (ct_data *)static_dtree); 00976 #ifdef DEBUG 00977 s->compressed_len += 3 + s->static_len; 00978 #endif 00979 } else { 00980 send_bits(s, (DYN_TREES<<1)+last, 3); 00981 send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1, 00982 max_blindex+1); 00983 compress_block(s, (ct_data *)s->dyn_ltree, (ct_data *)s->dyn_dtree); 00984 #ifdef DEBUG 00985 s->compressed_len += 3 + s->opt_len; 00986 #endif 00987 } 00988 Assert (s->compressed_len == s->bits_sent, "bad compressed size"); 00989 /* The above check is made mod 2^32, for files larger than 512 MB 00990 * and uLong implemented on 32 bits. 00991 */ 00992 init_block(s); 00993 00994 if (last) { 00995 bi_windup(s); 00996 #ifdef DEBUG 00997 s->compressed_len += 7; /* align on byte boundary */ 00998 #endif 00999 } 01000 Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3, 01001 s->compressed_len-7*last)); 01002 } 01003 01004 /* =========================================================================== 01005 * Save the match info and tally the frequency counts. Return true if 01006 * the current block must be flushed. 01007 */ 01008 int ZLIB_INTERNAL _tr_tally (s, dist, lc) 01009 deflate_state *s; 01010 unsigned dist; /* distance of matched string */ 01011 unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */ 01012 { 01013 s->d_buf[s->last_lit] = (ush)dist; 01014 s->l_buf[s->last_lit++] = (uch)lc; 01015 if (dist == 0) { 01016 /* lc is the unmatched char */ 01017 s->dyn_ltree[lc].Freq++; 01018 } else { 01019 s->matches++; 01020 /* Here, lc is the match length - MIN_MATCH */ 01021 dist--; /* dist = match distance - 1 */ 01022 Assert((ush)dist < (ush)MAX_DIST(s) && 01023 (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) && 01024 (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match"); 01025 01026 s->dyn_ltree[_length_code[lc]+LITERALS+1].Freq++; 01027 s->dyn_dtree[d_code(dist)].Freq++; 01028 } 01029 01030 #ifdef TRUNCATE_BLOCK 01031 /* Try to guess if it is profitable to stop the current block here */ 01032 if ((s->last_lit & 0x1fff) == 0 && s->level > 2) { 01033 /* Compute an upper bound for the compressed length */ 01034 ulg out_length = (ulg)s->last_lit*8L; 01035 ulg in_length = (ulg)((long)s->strstart - s->block_start); 01036 int dcode; 01037 for (dcode = 0; dcode < D_CODES; dcode++) { 01038 out_length += (ulg)s->dyn_dtree[dcode].Freq * 01039 (5L+extra_dbits[dcode]); 01040 } 01041 out_length >>= 3; 01042 Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ", 01043 s->last_lit, in_length, out_length, 01044 100L - out_length*100L/in_length)); 01045 if (s->matches < s->last_lit/2 && out_length < in_length/2) return 1; 01046 } 01047 #endif 01048 return (s->last_lit == s->lit_bufsize-1); 01049 /* We avoid equality with lit_bufsize because of wraparound at 64K 01050 * on 16 bit machines and because stored blocks are restricted to 01051 * 64K-1 bytes. 01052 */ 01053 } 01054 01055 /* =========================================================================== 01056 * Send the block data compressed using the given Huffman trees 01057 */ 01058 local void compress_block(s, ltree, dtree) 01059 deflate_state *s; 01060 ct_data *ltree; /* literal tree */ 01061 ct_data *dtree; /* distance tree */ 01062 { 01063 unsigned dist; /* distance of matched string */ 01064 int lc; /* match length or unmatched char (if dist == 0) */ 01065 unsigned lx = 0; /* running index in l_buf */ 01066 unsigned code; /* the code to send */ 01067 int extra; /* number of extra bits to send */ 01068 01069 if (s->last_lit != 0) do { 01070 dist = s->d_buf[lx]; 01071 lc = s->l_buf[lx++]; 01072 if (dist == 0) { 01073 send_code(s, lc, ltree); /* send a literal byte */ 01074 Tracecv(isgraph(lc), (stderr," '%c' ", lc)); 01075 } else { 01076 /* Here, lc is the match length - MIN_MATCH */ 01077 code = _length_code[lc]; 01078 send_code(s, code+LITERALS+1, ltree); /* send the length code */ 01079 extra = extra_lbits[code]; 01080 if (extra != 0) { 01081 lc -= base_length[code]; 01082 send_bits(s, lc, extra); /* send the extra length bits */ 01083 } 01084 dist--; /* dist is now the match distance - 1 */ 01085 code = d_code(dist); 01086 Assert (code < D_CODES, "bad d_code"); 01087 01088 send_code(s, code, dtree); /* send the distance code */ 01089 extra = extra_dbits[code]; 01090 if (extra != 0) { 01091 dist -= base_dist[code]; 01092 send_bits(s, dist, extra); /* send the extra distance bits */ 01093 } 01094 } /* literal or match pair ? */ 01095 01096 /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */ 01097 Assert((uInt)(s->pending) < s->lit_bufsize + 2*lx, 01098 "pendingBuf overflow"); 01099 01100 } while (lx < s->last_lit); 01101 01102 send_code(s, END_BLOCK, ltree); 01103 } 01104 01105 /* =========================================================================== 01106 * Check if the data type is TEXT or BINARY, using the following algorithm: 01107 * - TEXT if the two conditions below are satisfied: 01108 * a) There are no non-portable control characters belonging to the 01109 * "black list" (0..6, 14..25, 28..31). 01110 * b) There is at least one printable character belonging to the 01111 * "white list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255). 01112 * - BINARY otherwise. 01113 * - The following partially-portable control characters form a 01114 * "gray list" that is ignored in this detection algorithm: 01115 * (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}). 01116 * IN assertion: the fields Freq of dyn_ltree are set. 01117 */ 01118 local int detect_data_type(s) 01119 deflate_state *s; 01120 { 01121 /* black_mask is the bit mask of black-listed bytes 01122 * set bits 0..6, 14..25, and 28..31 01123 * 0xf3ffc07f = binary 11110011111111111100000001111111 01124 */ 01125 unsigned long black_mask = 0xf3ffc07fUL; 01126 int n; 01127 01128 /* Check for non-textual ("black-listed") bytes. */ 01129 for (n = 0; n <= 31; n++, black_mask >>= 1) 01130 if ((black_mask & 1) && (s->dyn_ltree[n].Freq != 0)) 01131 return Z_BINARY; 01132 01133 /* Check for textual ("white-listed") bytes. */ 01134 if (s->dyn_ltree[9].Freq != 0 || s->dyn_ltree[10].Freq != 0 01135 || s->dyn_ltree[13].Freq != 0) 01136 return Z_TEXT; 01137 for (n = 32; n < LITERALS; n++) 01138 if (s->dyn_ltree[n].Freq != 0) 01139 return Z_TEXT; 01140 01141 /* There are no "black-listed" or "white-listed" bytes: 01142 * this stream either is empty or has tolerated ("gray-listed") bytes only. 01143 */ 01144 return Z_BINARY; 01145 } 01146 01147 /* =========================================================================== 01148 * Reverse the first len bits of a code, using straightforward code (a faster 01149 * method would use a table) 01150 * IN assertion: 1 <= len <= 15 01151 */ 01152 local unsigned bi_reverse(code, len) 01153 unsigned code; /* the value to invert */ 01154 int len; /* its bit length */ 01155 { 01156 register unsigned res = 0; 01157 do { 01158 res |= code & 1; 01159 code >>= 1, res <<= 1; 01160 } while (--len > 0); 01161 return res >> 1; 01162 } 01163 01164 /* =========================================================================== 01165 * Flush the bit buffer, keeping at most 7 bits in it. 01166 */ 01167 local void bi_flush(s) 01168 deflate_state *s; 01169 { 01170 if (s->bi_valid == 16) { 01171 put_short(s, s->bi_buf); 01172 s->bi_buf = 0; 01173 s->bi_valid = 0; 01174 } else if (s->bi_valid >= 8) { 01175 put_byte(s, (Byte)s->bi_buf); 01176 s->bi_buf >>= 8; 01177 s->bi_valid -= 8; 01178 } 01179 } 01180 01181 /* =========================================================================== 01182 * Flush the bit buffer and align the output on a byte boundary 01183 */ 01184 local void bi_windup(s) 01185 deflate_state *s; 01186 { 01187 if (s->bi_valid > 8) { 01188 put_short(s, s->bi_buf); 01189 } else if (s->bi_valid > 0) { 01190 put_byte(s, (Byte)s->bi_buf); 01191 } 01192 s->bi_buf = 0; 01193 s->bi_valid = 0; 01194 #ifdef DEBUG 01195 s->bits_sent = (s->bits_sent+7) & ~7; 01196 #endif 01197 } 01198 01199 /* =========================================================================== 01200 * Copy a stored block, storing first the length and its 01201 * one's complement if requested. 01202 */ 01203 local void copy_block(s, buf, len, header) 01204 deflate_state *s; 01205 charf *buf; /* the input data */ 01206 unsigned len; /* its length */ 01207 int header; /* true if block header must be written */ 01208 { 01209 bi_windup(s); /* align on byte boundary */ 01210 01211 if (header) { 01212 put_short(s, (ush)len); 01213 put_short(s, (ush)~len); 01214 #ifdef DEBUG 01215 s->bits_sent += 2*16; 01216 #endif 01217 } 01218 #ifdef DEBUG 01219 s->bits_sent += (ulg)len<<3; 01220 #endif 01221 while (len--) { 01222 put_byte(s, *buf++); 01223 } 01224 }
Generated on Wed Jul 13 2022 09:05:31 by
1.7.2