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