trees.c (46c3d7fc) | trees.c (a04ea15d) |
---|---|
1/* trees.c -- output deflated data using Huffman coding 2 * Copyright (C) 1995-2021 Jean-loup Gailly 3 * detect_data_type() function provided freely by Cosmin Truta, 2006 4 * For conditions of distribution and use, see copyright notice in zlib.h 5 */ 6 7/* 8 * ALGORITHM --- 106 unchanged lines hidden (view full) --- 115struct static_tree_desc_s { 116 const ct_data *static_tree; /* static tree or NULL */ 117 const intf *extra_bits; /* extra bits for each code or NULL */ 118 int extra_base; /* base index for extra_bits */ 119 int elems; /* max number of elements in the tree */ 120 int max_length; /* max bit length for the codes */ 121}; 122 | 1/* trees.c -- output deflated data using Huffman coding 2 * Copyright (C) 1995-2021 Jean-loup Gailly 3 * detect_data_type() function provided freely by Cosmin Truta, 2006 4 * For conditions of distribution and use, see copyright notice in zlib.h 5 */ 6 7/* 8 * ALGORITHM --- 106 unchanged lines hidden (view full) --- 115struct static_tree_desc_s { 116 const ct_data *static_tree; /* static tree or NULL */ 117 const intf *extra_bits; /* extra bits for each code or NULL */ 118 int extra_base; /* base index for extra_bits */ 119 int elems; /* max number of elements in the tree */ 120 int max_length; /* max bit length for the codes */ 121}; 122 |
123local const static_tree_desc static_l_desc = | 123#ifdef NO_INIT_GLOBAL_POINTERS 124# define TCONST 125#else 126# define TCONST const 127#endif 128 129local TCONST static_tree_desc static_l_desc = |
124{static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS}; 125 | 130{static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS}; 131 |
126local const static_tree_desc static_d_desc = | 132local TCONST static_tree_desc static_d_desc = |
127{static_dtree, extra_dbits, 0, D_CODES, MAX_BITS}; 128 | 133{static_dtree, extra_dbits, 0, D_CODES, MAX_BITS}; 134 |
129local const static_tree_desc static_bl_desc = | 135local TCONST static_tree_desc static_bl_desc = |
130{(const ct_data *)0, extra_blbits, 0, BL_CODES, MAX_BL_BITS}; 131 132/* =========================================================================== | 136{(const ct_data *)0, extra_blbits, 0, BL_CODES, MAX_BL_BITS}; 137 138/* =========================================================================== |
133 * Local (static) routines in this file. | 139 * Output a short LSB first on the stream. 140 * IN assertion: there is enough room in pendingBuf. |
134 */ | 141 */ |
142#define put_short(s, w) { \ 143 put_byte(s, (uch)((w) & 0xff)); \ 144 put_byte(s, (uch)((ush)(w) >> 8)); \ 145} |
|
135 | 146 |
136local void tr_static_init OF((void)); 137local void init_block OF((deflate_state *s)); 138local void pqdownheap OF((deflate_state *s, ct_data *tree, int k)); 139local void gen_bitlen OF((deflate_state *s, tree_desc *desc)); 140local void gen_codes OF((ct_data *tree, int max_code, ushf *bl_count)); 141local void build_tree OF((deflate_state *s, tree_desc *desc)); 142local void scan_tree OF((deflate_state *s, ct_data *tree, int max_code)); 143local void send_tree OF((deflate_state *s, ct_data *tree, int max_code)); 144local int build_bl_tree OF((deflate_state *s)); 145local void send_all_trees OF((deflate_state *s, int lcodes, int dcodes, 146 int blcodes)); 147local void compress_block OF((deflate_state *s, const ct_data *ltree, 148 const ct_data *dtree)); 149local int detect_data_type OF((deflate_state *s)); 150local unsigned bi_reverse OF((unsigned code, int len)); 151local void bi_windup OF((deflate_state *s)); 152local void bi_flush OF((deflate_state *s)); | 147/* =========================================================================== 148 * Reverse the first len bits of a code, using straightforward code (a faster 149 * method would use a table) 150 * IN assertion: 1 <= len <= 15 151 */ 152local unsigned bi_reverse(unsigned code, int len) { 153 register unsigned res = 0; 154 do { 155 res |= code & 1; 156 code >>= 1, res <<= 1; 157 } while (--len > 0); 158 return res >> 1; 159} |
153 | 160 |
161/* =========================================================================== 162 * Flush the bit buffer, keeping at most 7 bits in it. 163 */ 164local void bi_flush(deflate_state *s) { 165 if (s->bi_valid == 16) { 166 put_short(s, s->bi_buf); 167 s->bi_buf = 0; 168 s->bi_valid = 0; 169 } else if (s->bi_valid >= 8) { 170 put_byte(s, (Byte)s->bi_buf); 171 s->bi_buf >>= 8; 172 s->bi_valid -= 8; 173 } 174} 175 176/* =========================================================================== 177 * Flush the bit buffer and align the output on a byte boundary 178 */ 179local void bi_windup(deflate_state *s) { 180 if (s->bi_valid > 8) { 181 put_short(s, s->bi_buf); 182 } else if (s->bi_valid > 0) { 183 put_byte(s, (Byte)s->bi_buf); 184 } 185 s->bi_buf = 0; 186 s->bi_valid = 0; 187#ifdef ZLIB_DEBUG 188 s->bits_sent = (s->bits_sent + 7) & ~7; 189#endif 190} 191 192/* =========================================================================== 193 * Generate the codes for a given tree and bit counts (which need not be 194 * optimal). 195 * IN assertion: the array bl_count contains the bit length statistics for 196 * the given tree and the field len is set for all tree elements. 197 * OUT assertion: the field code is set for all tree elements of non 198 * zero code length. 199 */ 200local void gen_codes(ct_data *tree, int max_code, ushf *bl_count) { 201 ush next_code[MAX_BITS+1]; /* next code value for each bit length */ 202 unsigned code = 0; /* running code value */ 203 int bits; /* bit index */ 204 int n; /* code index */ 205 206 /* The distribution counts are first used to generate the code values 207 * without bit reversal. 208 */ 209 for (bits = 1; bits <= MAX_BITS; bits++) { 210 code = (code + bl_count[bits - 1]) << 1; 211 next_code[bits] = (ush)code; 212 } 213 /* Check that the bit counts in bl_count are consistent. The last code 214 * must be all ones. 215 */ 216 Assert (code + bl_count[MAX_BITS] - 1 == (1 << MAX_BITS) - 1, 217 "inconsistent bit counts"); 218 Tracev((stderr,"\ngen_codes: max_code %d ", max_code)); 219 220 for (n = 0; n <= max_code; n++) { 221 int len = tree[n].Len; 222 if (len == 0) continue; 223 /* Now reverse the bits */ 224 tree[n].Code = (ush)bi_reverse(next_code[len]++, len); 225 226 Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ", 227 n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len] - 1)); 228 } 229} 230 |
|
154#ifdef GEN_TREES_H | 231#ifdef GEN_TREES_H |
155local void gen_trees_header OF((void)); | 232local void gen_trees_header(void); |
156#endif 157 158#ifndef ZLIB_DEBUG 159# define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len) 160 /* Send a code of the given tree. c and tree must not have side effects */ 161 162#else /* !ZLIB_DEBUG */ 163# define send_code(s, c, tree) \ 164 { if (z_verbose>2) fprintf(stderr,"\ncd %3d ",(c)); \ 165 send_bits(s, tree[c].Code, tree[c].Len); } 166#endif 167 168/* =========================================================================== | 233#endif 234 235#ifndef ZLIB_DEBUG 236# define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len) 237 /* Send a code of the given tree. c and tree must not have side effects */ 238 239#else /* !ZLIB_DEBUG */ 240# define send_code(s, c, tree) \ 241 { if (z_verbose>2) fprintf(stderr,"\ncd %3d ",(c)); \ 242 send_bits(s, tree[c].Code, tree[c].Len); } 243#endif 244 245/* =========================================================================== |
169 * Output a short LSB first on the stream. 170 * IN assertion: there is enough room in pendingBuf. 171 */ 172#define put_short(s, w) { \ 173 put_byte(s, (uch)((w) & 0xff)); \ 174 put_byte(s, (uch)((ush)(w) >> 8)); \ 175} 176 177/* =========================================================================== | |
178 * Send a value on a given number of bits. 179 * IN assertion: length <= 16 and value fits in length bits. 180 */ 181#ifdef ZLIB_DEBUG | 246 * Send a value on a given number of bits. 247 * IN assertion: length <= 16 and value fits in length bits. 248 */ 249#ifdef ZLIB_DEBUG |
182local void send_bits OF((deflate_state *s, int value, int length)); 183 184local void send_bits(s, value, length) 185 deflate_state *s; 186 int value; /* value to send */ 187 int length; /* number of bits */ 188{ | 250local void send_bits(deflate_state *s, int value, int length) { |
189 Tracevv((stderr," l %2d v %4x ", length, value)); 190 Assert(length > 0 && length <= 15, "invalid length"); 191 s->bits_sent += (ulg)length; 192 193 /* If not enough room in bi_buf, use (valid) bits from bi_buf and 194 * (16 - bi_valid) bits from value, leaving (width - (16 - bi_valid)) 195 * unused bits in value. 196 */ --- 25 unchanged lines hidden (view full) --- 222#endif /* ZLIB_DEBUG */ 223 224 225/* the arguments must not have side effects */ 226 227/* =========================================================================== 228 * Initialize the various 'constant' tables. 229 */ | 251 Tracevv((stderr," l %2d v %4x ", length, value)); 252 Assert(length > 0 && length <= 15, "invalid length"); 253 s->bits_sent += (ulg)length; 254 255 /* If not enough room in bi_buf, use (valid) bits from bi_buf and 256 * (16 - bi_valid) bits from value, leaving (width - (16 - bi_valid)) 257 * unused bits in value. 258 */ --- 25 unchanged lines hidden (view full) --- 284#endif /* ZLIB_DEBUG */ 285 286 287/* the arguments must not have side effects */ 288 289/* =========================================================================== 290 * Initialize the various 'constant' tables. 291 */ |
230local void tr_static_init() 231{ | 292local void tr_static_init(void) { |
232#if defined(GEN_TREES_H) || !defined(STDC) 233 static int static_init_done = 0; 234 int n; /* iterates over tree elements */ 235 int bits; /* bit counter */ 236 int length; /* length value */ 237 int code; /* code value */ 238 int dist; /* distance index */ 239 ush bl_count[MAX_BITS+1]; --- 76 unchanged lines hidden (view full) --- 316# ifndef ZLIB_DEBUG 317# include <stdio.h> 318# endif 319 320# define SEPARATOR(i, last, width) \ 321 ((i) == (last)? "\n};\n\n" : \ 322 ((i) % (width) == (width) - 1 ? ",\n" : ", ")) 323 | 293#if defined(GEN_TREES_H) || !defined(STDC) 294 static int static_init_done = 0; 295 int n; /* iterates over tree elements */ 296 int bits; /* bit counter */ 297 int length; /* length value */ 298 int code; /* code value */ 299 int dist; /* distance index */ 300 ush bl_count[MAX_BITS+1]; --- 76 unchanged lines hidden (view full) --- 377# ifndef ZLIB_DEBUG 378# include <stdio.h> 379# endif 380 381# define SEPARATOR(i, last, width) \ 382 ((i) == (last)? "\n};\n\n" : \ 383 ((i) % (width) == (width) - 1 ? ",\n" : ", ")) 384 |
324void gen_trees_header() 325{ | 385void gen_trees_header(void) { |
326 FILE *header = fopen("trees.h", "w"); 327 int i; 328 329 Assert (header != NULL, "Can't open trees.h"); 330 fprintf(header, 331 "/* header created automatically with -DGEN_TREES_H */\n\n"); 332 333 fprintf(header, "local const ct_data static_ltree[L_CODES+2] = {\n"); --- 33 unchanged lines hidden (view full) --- 367 SEPARATOR(i, D_CODES-1, 10)); 368 } 369 370 fclose(header); 371} 372#endif /* GEN_TREES_H */ 373 374/* =========================================================================== | 386 FILE *header = fopen("trees.h", "w"); 387 int i; 388 389 Assert (header != NULL, "Can't open trees.h"); 390 fprintf(header, 391 "/* header created automatically with -DGEN_TREES_H */\n\n"); 392 393 fprintf(header, "local const ct_data static_ltree[L_CODES+2] = {\n"); --- 33 unchanged lines hidden (view full) --- 427 SEPARATOR(i, D_CODES-1, 10)); 428 } 429 430 fclose(header); 431} 432#endif /* GEN_TREES_H */ 433 434/* =========================================================================== |
435 * Initialize a new block. 436 */ 437local void init_block(deflate_state *s) { 438 int n; /* iterates over tree elements */ 439 440 /* Initialize the trees. */ 441 for (n = 0; n < L_CODES; n++) s->dyn_ltree[n].Freq = 0; 442 for (n = 0; n < D_CODES; n++) s->dyn_dtree[n].Freq = 0; 443 for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0; 444 445 s->dyn_ltree[END_BLOCK].Freq = 1; 446 s->opt_len = s->static_len = 0L; 447 s->sym_next = s->matches = 0; 448} 449 450/* =========================================================================== |
|
375 * Initialize the tree data structures for a new zlib stream. 376 */ | 451 * Initialize the tree data structures for a new zlib stream. 452 */ |
377void ZLIB_INTERNAL _tr_init(s) 378 deflate_state *s; 379{ | 453void ZLIB_INTERNAL _tr_init(deflate_state *s) { |
380 tr_static_init(); 381 382 s->l_desc.dyn_tree = s->dyn_ltree; 383 s->l_desc.stat_desc = &static_l_desc; 384 385 s->d_desc.dyn_tree = s->dyn_dtree; 386 s->d_desc.stat_desc = &static_d_desc; 387 --- 6 unchanged lines hidden (view full) --- 394 s->compressed_len = 0L; 395 s->bits_sent = 0L; 396#endif 397 398 /* Initialize the first block of the first file: */ 399 init_block(s); 400} 401 | 454 tr_static_init(); 455 456 s->l_desc.dyn_tree = s->dyn_ltree; 457 s->l_desc.stat_desc = &static_l_desc; 458 459 s->d_desc.dyn_tree = s->dyn_dtree; 460 s->d_desc.stat_desc = &static_d_desc; 461 --- 6 unchanged lines hidden (view full) --- 468 s->compressed_len = 0L; 469 s->bits_sent = 0L; 470#endif 471 472 /* Initialize the first block of the first file: */ 473 init_block(s); 474} 475 |
402/* =========================================================================== 403 * Initialize a new block. 404 */ 405local void init_block(s) 406 deflate_state *s; 407{ 408 int n; /* iterates over tree elements */ 409 410 /* Initialize the trees. */ 411 for (n = 0; n < L_CODES; n++) s->dyn_ltree[n].Freq = 0; 412 for (n = 0; n < D_CODES; n++) s->dyn_dtree[n].Freq = 0; 413 for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0; 414 415 s->dyn_ltree[END_BLOCK].Freq = 1; 416 s->opt_len = s->static_len = 0L; 417 s->sym_next = s->matches = 0; 418} 419 | |
420#define SMALLEST 1 421/* Index within the heap array of least frequent node in the Huffman tree */ 422 423 424/* =========================================================================== 425 * Remove the smallest element from the heap and recreate the heap with 426 * one less element. Updates heap and heap_len. 427 */ --- 13 unchanged lines hidden (view full) --- 441 (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m])) 442 443/* =========================================================================== 444 * Restore the heap property by moving down the tree starting at node k, 445 * exchanging a node with the smallest of its two sons if necessary, stopping 446 * when the heap property is re-established (each father smaller than its 447 * two sons). 448 */ | 476#define SMALLEST 1 477/* Index within the heap array of least frequent node in the Huffman tree */ 478 479 480/* =========================================================================== 481 * Remove the smallest element from the heap and recreate the heap with 482 * one less element. Updates heap and heap_len. 483 */ --- 13 unchanged lines hidden (view full) --- 497 (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m])) 498 499/* =========================================================================== 500 * Restore the heap property by moving down the tree starting at node k, 501 * exchanging a node with the smallest of its two sons if necessary, stopping 502 * when the heap property is re-established (each father smaller than its 503 * two sons). 504 */ |
449local void pqdownheap(s, tree, k) 450 deflate_state *s; 451 ct_data *tree; /* the tree to restore */ 452 int k; /* node to move down */ 453{ | 505local void pqdownheap(deflate_state *s, ct_data *tree, int k) { |
454 int v = s->heap[k]; 455 int j = k << 1; /* left son of k */ 456 while (j <= s->heap_len) { 457 /* Set j to the smallest of the two sons: */ 458 if (j < s->heap_len && 459 smaller(tree, s->heap[j + 1], s->heap[j], s->depth)) { 460 j++; 461 } --- 14 unchanged lines hidden (view full) --- 476 * for the current block. 477 * IN assertion: the fields freq and dad are set, heap[heap_max] and 478 * above are the tree nodes sorted by increasing frequency. 479 * OUT assertions: the field len is set to the optimal bit length, the 480 * array bl_count contains the frequencies for each bit length. 481 * The length opt_len is updated; static_len is also updated if stree is 482 * not null. 483 */ | 506 int v = s->heap[k]; 507 int j = k << 1; /* left son of k */ 508 while (j <= s->heap_len) { 509 /* Set j to the smallest of the two sons: */ 510 if (j < s->heap_len && 511 smaller(tree, s->heap[j + 1], s->heap[j], s->depth)) { 512 j++; 513 } --- 14 unchanged lines hidden (view full) --- 528 * for the current block. 529 * IN assertion: the fields freq and dad are set, heap[heap_max] and 530 * above are the tree nodes sorted by increasing frequency. 531 * OUT assertions: the field len is set to the optimal bit length, the 532 * array bl_count contains the frequencies for each bit length. 533 * The length opt_len is updated; static_len is also updated if stree is 534 * not null. 535 */ |
484local void gen_bitlen(s, desc) 485 deflate_state *s; 486 tree_desc *desc; /* the tree descriptor */ 487{ | 536local void gen_bitlen(deflate_state *s, tree_desc *desc) { |
488 ct_data *tree = desc->dyn_tree; 489 int max_code = desc->max_code; 490 const ct_data *stree = desc->stat_desc->static_tree; 491 const intf *extra = desc->stat_desc->extra_bits; 492 int base = desc->stat_desc->extra_base; 493 int max_length = desc->stat_desc->max_length; 494 int h; /* heap index */ 495 int n, m; /* iterate over the tree elements */ --- 58 unchanged lines hidden (view full) --- 554 s->opt_len += ((ulg)bits - tree[m].Len) * tree[m].Freq; 555 tree[m].Len = (ush)bits; 556 } 557 n--; 558 } 559 } 560} 561 | 537 ct_data *tree = desc->dyn_tree; 538 int max_code = desc->max_code; 539 const ct_data *stree = desc->stat_desc->static_tree; 540 const intf *extra = desc->stat_desc->extra_bits; 541 int base = desc->stat_desc->extra_base; 542 int max_length = desc->stat_desc->max_length; 543 int h; /* heap index */ 544 int n, m; /* iterate over the tree elements */ --- 58 unchanged lines hidden (view full) --- 603 s->opt_len += ((ulg)bits - tree[m].Len) * tree[m].Freq; 604 tree[m].Len = (ush)bits; 605 } 606 n--; 607 } 608 } 609} 610 |
562/* =========================================================================== 563 * Generate the codes for a given tree and bit counts (which need not be 564 * optimal). 565 * IN assertion: the array bl_count contains the bit length statistics for 566 * the given tree and the field len is set for all tree elements. 567 * OUT assertion: the field code is set for all tree elements of non 568 * zero code length. 569 */ 570local void gen_codes(tree, max_code, bl_count) 571 ct_data *tree; /* the tree to decorate */ 572 int max_code; /* largest code with non zero frequency */ 573 ushf *bl_count; /* number of codes at each bit length */ 574{ 575 ush next_code[MAX_BITS+1]; /* next code value for each bit length */ 576 unsigned code = 0; /* running code value */ 577 int bits; /* bit index */ 578 int n; /* code index */ | 611#ifdef DUMP_BL_TREE 612# include <stdio.h> 613#endif |
579 | 614 |
580 /* The distribution counts are first used to generate the code values 581 * without bit reversal. 582 */ 583 for (bits = 1; bits <= MAX_BITS; bits++) { 584 code = (code + bl_count[bits - 1]) << 1; 585 next_code[bits] = (ush)code; 586 } 587 /* Check that the bit counts in bl_count are consistent. The last code 588 * must be all ones. 589 */ 590 Assert (code + bl_count[MAX_BITS] - 1 == (1 << MAX_BITS) - 1, 591 "inconsistent bit counts"); 592 Tracev((stderr,"\ngen_codes: max_code %d ", max_code)); 593 594 for (n = 0; n <= max_code; n++) { 595 int len = tree[n].Len; 596 if (len == 0) continue; 597 /* Now reverse the bits */ 598 tree[n].Code = (ush)bi_reverse(next_code[len]++, len); 599 600 Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ", 601 n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len] - 1)); 602 } 603} 604 | |
605/* =========================================================================== 606 * Construct one Huffman tree and assigns the code bit strings and lengths. 607 * Update the total bit length for the current block. 608 * IN assertion: the field freq is set for all tree elements. 609 * OUT assertions: the fields len and code are set to the optimal bit length 610 * and corresponding code. The length opt_len is updated; static_len is 611 * also updated if stree is not null. The field max_code is set. 612 */ | 615/* =========================================================================== 616 * Construct one Huffman tree and assigns the code bit strings and lengths. 617 * Update the total bit length for the current block. 618 * IN assertion: the field freq is set for all tree elements. 619 * OUT assertions: the fields len and code are set to the optimal bit length 620 * and corresponding code. The length opt_len is updated; static_len is 621 * also updated if stree is not null. The field max_code is set. 622 */ |
613local void build_tree(s, desc) 614 deflate_state *s; 615 tree_desc *desc; /* the tree descriptor */ 616{ | 623local void build_tree(deflate_state *s, tree_desc *desc) { |
617 ct_data *tree = desc->dyn_tree; 618 const ct_data *stree = desc->stat_desc->static_tree; 619 int elems = desc->stat_desc->elems; 620 int n, m; /* iterate over heap elements */ 621 int max_code = -1; /* largest code with non zero frequency */ 622 int node; /* new node being created */ 623 624 /* Construct the initial heap, with least frequent element in --- 68 unchanged lines hidden (view full) --- 693 /* The field len is now set, we can generate the bit codes */ 694 gen_codes ((ct_data *)tree, max_code, s->bl_count); 695} 696 697/* =========================================================================== 698 * Scan a literal or distance tree to determine the frequencies of the codes 699 * in the bit length tree. 700 */ | 624 ct_data *tree = desc->dyn_tree; 625 const ct_data *stree = desc->stat_desc->static_tree; 626 int elems = desc->stat_desc->elems; 627 int n, m; /* iterate over heap elements */ 628 int max_code = -1; /* largest code with non zero frequency */ 629 int node; /* new node being created */ 630 631 /* Construct the initial heap, with least frequent element in --- 68 unchanged lines hidden (view full) --- 700 /* The field len is now set, we can generate the bit codes */ 701 gen_codes ((ct_data *)tree, max_code, s->bl_count); 702} 703 704/* =========================================================================== 705 * Scan a literal or distance tree to determine the frequencies of the codes 706 * in the bit length tree. 707 */ |
701local void scan_tree(s, tree, max_code) 702 deflate_state *s; 703 ct_data *tree; /* the tree to be scanned */ 704 int max_code; /* and its largest code of non zero frequency */ 705{ | 708local void scan_tree(deflate_state *s, ct_data *tree, int max_code) { |
706 int n; /* iterates over all tree elements */ 707 int prevlen = -1; /* last emitted length */ 708 int curlen; /* length of current code */ 709 int nextlen = tree[0].Len; /* length of next code */ 710 int count = 0; /* repeat count of the current code */ 711 int max_count = 7; /* max repeat count */ 712 int min_count = 4; /* min repeat count */ 713 --- 24 unchanged lines hidden (view full) --- 738 } 739 } 740} 741 742/* =========================================================================== 743 * Send a literal or distance tree in compressed form, using the codes in 744 * bl_tree. 745 */ | 709 int n; /* iterates over all tree elements */ 710 int prevlen = -1; /* last emitted length */ 711 int curlen; /* length of current code */ 712 int nextlen = tree[0].Len; /* length of next code */ 713 int count = 0; /* repeat count of the current code */ 714 int max_count = 7; /* max repeat count */ 715 int min_count = 4; /* min repeat count */ 716 --- 24 unchanged lines hidden (view full) --- 741 } 742 } 743} 744 745/* =========================================================================== 746 * Send a literal or distance tree in compressed form, using the codes in 747 * bl_tree. 748 */ |
746local void send_tree(s, tree, max_code) 747 deflate_state *s; 748 ct_data *tree; /* the tree to be scanned */ 749 int max_code; /* and its largest code of non zero frequency */ 750{ | 749local void send_tree(deflate_state *s, ct_data *tree, int max_code) { |
751 int n; /* iterates over all tree elements */ 752 int prevlen = -1; /* last emitted length */ 753 int curlen; /* length of current code */ 754 int nextlen = tree[0].Len; /* length of next code */ 755 int count = 0; /* repeat count of the current code */ 756 int max_count = 7; /* max repeat count */ 757 int min_count = 4; /* min repeat count */ 758 --- 30 unchanged lines hidden (view full) --- 789 } 790 } 791} 792 793/* =========================================================================== 794 * Construct the Huffman tree for the bit lengths and return the index in 795 * bl_order of the last bit length code to send. 796 */ | 750 int n; /* iterates over all tree elements */ 751 int prevlen = -1; /* last emitted length */ 752 int curlen; /* length of current code */ 753 int nextlen = tree[0].Len; /* length of next code */ 754 int count = 0; /* repeat count of the current code */ 755 int max_count = 7; /* max repeat count */ 756 int min_count = 4; /* min repeat count */ 757 --- 30 unchanged lines hidden (view full) --- 788 } 789 } 790} 791 792/* =========================================================================== 793 * Construct the Huffman tree for the bit lengths and return the index in 794 * bl_order of the last bit length code to send. 795 */ |
797local int build_bl_tree(s) 798 deflate_state *s; 799{ | 796local int build_bl_tree(deflate_state *s) { |
800 int max_blindex; /* index of last bit length code of non zero freq */ 801 802 /* Determine the bit length frequencies for literal and distance trees */ 803 scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code); 804 scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code); 805 806 /* Build the bit length tree: */ 807 build_tree(s, (tree_desc *)(&(s->bl_desc))); --- 16 unchanged lines hidden (view full) --- 824 return max_blindex; 825} 826 827/* =========================================================================== 828 * Send the header for a block using dynamic Huffman trees: the counts, the 829 * lengths of the bit length codes, the literal tree and the distance tree. 830 * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4. 831 */ | 797 int max_blindex; /* index of last bit length code of non zero freq */ 798 799 /* Determine the bit length frequencies for literal and distance trees */ 800 scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code); 801 scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code); 802 803 /* Build the bit length tree: */ 804 build_tree(s, (tree_desc *)(&(s->bl_desc))); --- 16 unchanged lines hidden (view full) --- 821 return max_blindex; 822} 823 824/* =========================================================================== 825 * Send the header for a block using dynamic Huffman trees: the counts, the 826 * lengths of the bit length codes, the literal tree and the distance tree. 827 * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4. 828 */ |
832local void send_all_trees(s, lcodes, dcodes, blcodes) 833 deflate_state *s; 834 int lcodes, dcodes, blcodes; /* number of codes for each tree */ 835{ | 829local void send_all_trees(deflate_state *s, int lcodes, int dcodes, 830 int blcodes) { |
836 int rank; /* index in bl_order */ 837 838 Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes"); 839 Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES, 840 "too many codes"); 841 Tracev((stderr, "\nbl counts: ")); 842 send_bits(s, lcodes - 257, 5); /* not +255 as stated in appnote.txt */ 843 send_bits(s, dcodes - 1, 5); --- 9 unchanged lines hidden (view full) --- 853 854 send_tree(s, (ct_data *)s->dyn_dtree, dcodes - 1); /* distance tree */ 855 Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent)); 856} 857 858/* =========================================================================== 859 * Send a stored block 860 */ | 831 int rank; /* index in bl_order */ 832 833 Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes"); 834 Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES, 835 "too many codes"); 836 Tracev((stderr, "\nbl counts: ")); 837 send_bits(s, lcodes - 257, 5); /* not +255 as stated in appnote.txt */ 838 send_bits(s, dcodes - 1, 5); --- 9 unchanged lines hidden (view full) --- 848 849 send_tree(s, (ct_data *)s->dyn_dtree, dcodes - 1); /* distance tree */ 850 Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent)); 851} 852 853/* =========================================================================== 854 * Send a stored block 855 */ |
861void ZLIB_INTERNAL _tr_stored_block(s, buf, stored_len, last) 862 deflate_state *s; 863 charf *buf; /* input block */ 864 ulg stored_len; /* length of input block */ 865 int last; /* one if this is the last block for a file */ 866{ | 856void ZLIB_INTERNAL _tr_stored_block(deflate_state *s, charf *buf, 857 ulg stored_len, int last) { |
867 send_bits(s, (STORED_BLOCK<<1) + last, 3); /* send block type */ 868 bi_windup(s); /* align on byte boundary */ 869 put_short(s, (ush)stored_len); 870 put_short(s, (ush)~stored_len); 871 if (stored_len) 872 zmemcpy(s->pending_buf + s->pending, (Bytef *)buf, stored_len); 873 s->pending += stored_len; 874#ifdef ZLIB_DEBUG 875 s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L; 876 s->compressed_len += (stored_len + 4) << 3; 877 s->bits_sent += 2*16; 878 s->bits_sent += stored_len << 3; 879#endif 880} 881 882/* =========================================================================== 883 * Flush the bits in the bit buffer to pending output (leaves at most 7 bits) 884 */ | 858 send_bits(s, (STORED_BLOCK<<1) + last, 3); /* send block type */ 859 bi_windup(s); /* align on byte boundary */ 860 put_short(s, (ush)stored_len); 861 put_short(s, (ush)~stored_len); 862 if (stored_len) 863 zmemcpy(s->pending_buf + s->pending, (Bytef *)buf, stored_len); 864 s->pending += stored_len; 865#ifdef ZLIB_DEBUG 866 s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L; 867 s->compressed_len += (stored_len + 4) << 3; 868 s->bits_sent += 2*16; 869 s->bits_sent += stored_len << 3; 870#endif 871} 872 873/* =========================================================================== 874 * Flush the bits in the bit buffer to pending output (leaves at most 7 bits) 875 */ |
885void ZLIB_INTERNAL _tr_flush_bits(s) 886 deflate_state *s; 887{ | 876void ZLIB_INTERNAL _tr_flush_bits(deflate_state *s) { |
888 bi_flush(s); 889} 890 891/* =========================================================================== 892 * Send one empty static block to give enough lookahead for inflate. 893 * This takes 10 bits, of which 7 may remain in the bit buffer. 894 */ | 877 bi_flush(s); 878} 879 880/* =========================================================================== 881 * Send one empty static block to give enough lookahead for inflate. 882 * This takes 10 bits, of which 7 may remain in the bit buffer. 883 */ |
895void ZLIB_INTERNAL _tr_align(s) 896 deflate_state *s; 897{ | 884void ZLIB_INTERNAL _tr_align(deflate_state *s) { |
898 send_bits(s, STATIC_TREES<<1, 3); 899 send_code(s, END_BLOCK, static_ltree); 900#ifdef ZLIB_DEBUG 901 s->compressed_len += 10L; /* 3 for block type, 7 for EOB */ 902#endif 903 bi_flush(s); 904} 905 906/* =========================================================================== | 885 send_bits(s, STATIC_TREES<<1, 3); 886 send_code(s, END_BLOCK, static_ltree); 887#ifdef ZLIB_DEBUG 888 s->compressed_len += 10L; /* 3 for block type, 7 for EOB */ 889#endif 890 bi_flush(s); 891} 892 893/* =========================================================================== |
894 * Send the block data compressed using the given Huffman trees 895 */ 896local void compress_block(deflate_state *s, const ct_data *ltree, 897 const ct_data *dtree) { 898 unsigned dist; /* distance of matched string */ 899 int lc; /* match length or unmatched char (if dist == 0) */ 900 unsigned sx = 0; /* running index in sym_buf */ 901 unsigned code; /* the code to send */ 902 int extra; /* number of extra bits to send */ 903 904 if (s->sym_next != 0) do { 905 dist = s->sym_buf[sx++] & 0xff; 906 dist += (unsigned)(s->sym_buf[sx++] & 0xff) << 8; 907 lc = s->sym_buf[sx++]; 908 if (dist == 0) { 909 send_code(s, lc, ltree); /* send a literal byte */ 910 Tracecv(isgraph(lc), (stderr," '%c' ", lc)); 911 } else { 912 /* Here, lc is the match length - MIN_MATCH */ 913 code = _length_code[lc]; 914 send_code(s, code + LITERALS + 1, ltree); /* send length code */ 915 extra = extra_lbits[code]; 916 if (extra != 0) { 917 lc -= base_length[code]; 918 send_bits(s, lc, extra); /* send the extra length bits */ 919 } 920 dist--; /* dist is now the match distance - 1 */ 921 code = d_code(dist); 922 Assert (code < D_CODES, "bad d_code"); 923 924 send_code(s, code, dtree); /* send the distance code */ 925 extra = extra_dbits[code]; 926 if (extra != 0) { 927 dist -= (unsigned)base_dist[code]; 928 send_bits(s, dist, extra); /* send the extra distance bits */ 929 } 930 } /* literal or match pair ? */ 931 932 /* Check that the overlay between pending_buf and sym_buf is ok: */ 933 Assert(s->pending < s->lit_bufsize + sx, "pendingBuf overflow"); 934 935 } while (sx < s->sym_next); 936 937 send_code(s, END_BLOCK, ltree); 938} 939 940/* =========================================================================== 941 * Check if the data type is TEXT or BINARY, using the following algorithm: 942 * - TEXT if the two conditions below are satisfied: 943 * a) There are no non-portable control characters belonging to the 944 * "block list" (0..6, 14..25, 28..31). 945 * b) There is at least one printable character belonging to the 946 * "allow list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255). 947 * - BINARY otherwise. 948 * - The following partially-portable control characters form a 949 * "gray list" that is ignored in this detection algorithm: 950 * (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}). 951 * IN assertion: the fields Freq of dyn_ltree are set. 952 */ 953local int detect_data_type(deflate_state *s) { 954 /* block_mask is the bit mask of block-listed bytes 955 * set bits 0..6, 14..25, and 28..31 956 * 0xf3ffc07f = binary 11110011111111111100000001111111 957 */ 958 unsigned long block_mask = 0xf3ffc07fUL; 959 int n; 960 961 /* Check for non-textual ("block-listed") bytes. */ 962 for (n = 0; n <= 31; n++, block_mask >>= 1) 963 if ((block_mask & 1) && (s->dyn_ltree[n].Freq != 0)) 964 return Z_BINARY; 965 966 /* Check for textual ("allow-listed") bytes. */ 967 if (s->dyn_ltree[9].Freq != 0 || s->dyn_ltree[10].Freq != 0 968 || s->dyn_ltree[13].Freq != 0) 969 return Z_TEXT; 970 for (n = 32; n < LITERALS; n++) 971 if (s->dyn_ltree[n].Freq != 0) 972 return Z_TEXT; 973 974 /* There are no "block-listed" or "allow-listed" bytes: 975 * this stream either is empty or has tolerated ("gray-listed") bytes only. 976 */ 977 return Z_BINARY; 978} 979 980/* =========================================================================== |
|
907 * Determine the best encoding for the current block: dynamic trees, static 908 * trees or store, and write out the encoded block. 909 */ | 981 * Determine the best encoding for the current block: dynamic trees, static 982 * trees or store, and write out the encoded block. 983 */ |
910void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last) 911 deflate_state *s; 912 charf *buf; /* input block, or NULL if too old */ 913 ulg stored_len; /* length of input block */ 914 int last; /* one if this is the last block for a file */ 915{ | 984void ZLIB_INTERNAL _tr_flush_block(deflate_state *s, charf *buf, 985 ulg stored_len, int last) { |
916 ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */ 917 int max_blindex = 0; /* index of last bit length code of non zero freq */ 918 919 /* Build the Huffman trees unless a stored block is forced */ 920 if (s->level > 0) { 921 922 /* Check if the file is binary or text */ 923 if (s->strm->data_type == Z_UNKNOWN) --- 80 unchanged lines hidden (view full) --- 1004 Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len >> 3, 1005 s->compressed_len - 7*last)); 1006} 1007 1008/* =========================================================================== 1009 * Save the match info and tally the frequency counts. Return true if 1010 * the current block must be flushed. 1011 */ | 986 ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */ 987 int max_blindex = 0; /* index of last bit length code of non zero freq */ 988 989 /* Build the Huffman trees unless a stored block is forced */ 990 if (s->level > 0) { 991 992 /* Check if the file is binary or text */ 993 if (s->strm->data_type == Z_UNKNOWN) --- 80 unchanged lines hidden (view full) --- 1074 Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len >> 3, 1075 s->compressed_len - 7*last)); 1076} 1077 1078/* =========================================================================== 1079 * Save the match info and tally the frequency counts. Return true if 1080 * the current block must be flushed. 1081 */ |
1012int ZLIB_INTERNAL _tr_tally(s, dist, lc) 1013 deflate_state *s; 1014 unsigned dist; /* distance of matched string */ 1015 unsigned lc; /* match length - MIN_MATCH or unmatched char (dist==0) */ 1016{ | 1082int ZLIB_INTERNAL _tr_tally(deflate_state *s, unsigned dist, unsigned lc) { |
1017 s->sym_buf[s->sym_next++] = (uch)dist; 1018 s->sym_buf[s->sym_next++] = (uch)(dist >> 8); 1019 s->sym_buf[s->sym_next++] = (uch)lc; 1020 if (dist == 0) { 1021 /* lc is the unmatched char */ 1022 s->dyn_ltree[lc].Freq++; 1023 } else { 1024 s->matches++; 1025 /* Here, lc is the match length - MIN_MATCH */ 1026 dist--; /* dist = match distance - 1 */ 1027 Assert((ush)dist < (ush)MAX_DIST(s) && 1028 (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) && 1029 (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match"); 1030 1031 s->dyn_ltree[_length_code[lc] + LITERALS + 1].Freq++; 1032 s->dyn_dtree[d_code(dist)].Freq++; 1033 } 1034 return (s->sym_next == s->sym_end); 1035} | 1083 s->sym_buf[s->sym_next++] = (uch)dist; 1084 s->sym_buf[s->sym_next++] = (uch)(dist >> 8); 1085 s->sym_buf[s->sym_next++] = (uch)lc; 1086 if (dist == 0) { 1087 /* lc is the unmatched char */ 1088 s->dyn_ltree[lc].Freq++; 1089 } else { 1090 s->matches++; 1091 /* Here, lc is the match length - MIN_MATCH */ 1092 dist--; /* dist = match distance - 1 */ 1093 Assert((ush)dist < (ush)MAX_DIST(s) && 1094 (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) && 1095 (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match"); 1096 1097 s->dyn_ltree[_length_code[lc] + LITERALS + 1].Freq++; 1098 s->dyn_dtree[d_code(dist)].Freq++; 1099 } 1100 return (s->sym_next == s->sym_end); 1101} |
1036 1037/* =========================================================================== 1038 * Send the block data compressed using the given Huffman trees 1039 */ 1040local void compress_block(s, ltree, dtree) 1041 deflate_state *s; 1042 const ct_data *ltree; /* literal tree */ 1043 const ct_data *dtree; /* distance tree */ 1044{ 1045 unsigned dist; /* distance of matched string */ 1046 int lc; /* match length or unmatched char (if dist == 0) */ 1047 unsigned sx = 0; /* running index in sym_buf */ 1048 unsigned code; /* the code to send */ 1049 int extra; /* number of extra bits to send */ 1050 1051 if (s->sym_next != 0) do { 1052 dist = s->sym_buf[sx++] & 0xff; 1053 dist += (unsigned)(s->sym_buf[sx++] & 0xff) << 8; 1054 lc = s->sym_buf[sx++]; 1055 if (dist == 0) { 1056 send_code(s, lc, ltree); /* send a literal byte */ 1057 Tracecv(isgraph(lc), (stderr," '%c' ", lc)); 1058 } else { 1059 /* Here, lc is the match length - MIN_MATCH */ 1060 code = _length_code[lc]; 1061 send_code(s, code + LITERALS + 1, ltree); /* send length code */ 1062 extra = extra_lbits[code]; 1063 if (extra != 0) { 1064 lc -= base_length[code]; 1065 send_bits(s, lc, extra); /* send the extra length bits */ 1066 } 1067 dist--; /* dist is now the match distance - 1 */ 1068 code = d_code(dist); 1069 Assert (code < D_CODES, "bad d_code"); 1070 1071 send_code(s, code, dtree); /* send the distance code */ 1072 extra = extra_dbits[code]; 1073 if (extra != 0) { 1074 dist -= (unsigned)base_dist[code]; 1075 send_bits(s, dist, extra); /* send the extra distance bits */ 1076 } 1077 } /* literal or match pair ? */ 1078 1079 /* Check that the overlay between pending_buf and sym_buf is ok: */ 1080 Assert(s->pending < s->lit_bufsize + sx, "pendingBuf overflow"); 1081 1082 } while (sx < s->sym_next); 1083 1084 send_code(s, END_BLOCK, ltree); 1085} 1086 1087/* =========================================================================== 1088 * Check if the data type is TEXT or BINARY, using the following algorithm: 1089 * - TEXT if the two conditions below are satisfied: 1090 * a) There are no non-portable control characters belonging to the 1091 * "block list" (0..6, 14..25, 28..31). 1092 * b) There is at least one printable character belonging to the 1093 * "allow list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255). 1094 * - BINARY otherwise. 1095 * - The following partially-portable control characters form a 1096 * "gray list" that is ignored in this detection algorithm: 1097 * (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}). 1098 * IN assertion: the fields Freq of dyn_ltree are set. 1099 */ 1100local int detect_data_type(s) 1101 deflate_state *s; 1102{ 1103 /* block_mask is the bit mask of block-listed bytes 1104 * set bits 0..6, 14..25, and 28..31 1105 * 0xf3ffc07f = binary 11110011111111111100000001111111 1106 */ 1107 unsigned long block_mask = 0xf3ffc07fUL; 1108 int n; 1109 1110 /* Check for non-textual ("block-listed") bytes. */ 1111 for (n = 0; n <= 31; n++, block_mask >>= 1) 1112 if ((block_mask & 1) && (s->dyn_ltree[n].Freq != 0)) 1113 return Z_BINARY; 1114 1115 /* Check for textual ("allow-listed") bytes. */ 1116 if (s->dyn_ltree[9].Freq != 0 || s->dyn_ltree[10].Freq != 0 1117 || s->dyn_ltree[13].Freq != 0) 1118 return Z_TEXT; 1119 for (n = 32; n < LITERALS; n++) 1120 if (s->dyn_ltree[n].Freq != 0) 1121 return Z_TEXT; 1122 1123 /* There are no "block-listed" or "allow-listed" bytes: 1124 * this stream either is empty or has tolerated ("gray-listed") bytes only. 1125 */ 1126 return Z_BINARY; 1127} 1128 1129/* =========================================================================== 1130 * Reverse the first len bits of a code, using straightforward code (a faster 1131 * method would use a table) 1132 * IN assertion: 1 <= len <= 15 1133 */ 1134local unsigned bi_reverse(code, len) 1135 unsigned code; /* the value to invert */ 1136 int len; /* its bit length */ 1137{ 1138 register unsigned res = 0; 1139 do { 1140 res |= code & 1; 1141 code >>= 1, res <<= 1; 1142 } while (--len > 0); 1143 return res >> 1; 1144} 1145 1146/* =========================================================================== 1147 * Flush the bit buffer, keeping at most 7 bits in it. 1148 */ 1149local void bi_flush(s) 1150 deflate_state *s; 1151{ 1152 if (s->bi_valid == 16) { 1153 put_short(s, s->bi_buf); 1154 s->bi_buf = 0; 1155 s->bi_valid = 0; 1156 } else if (s->bi_valid >= 8) { 1157 put_byte(s, (Byte)s->bi_buf); 1158 s->bi_buf >>= 8; 1159 s->bi_valid -= 8; 1160 } 1161} 1162 1163/* =========================================================================== 1164 * Flush the bit buffer and align the output on a byte boundary 1165 */ 1166local void bi_windup(s) 1167 deflate_state *s; 1168{ 1169 if (s->bi_valid > 8) { 1170 put_short(s, s->bi_buf); 1171 } else if (s->bi_valid > 0) { 1172 put_byte(s, (Byte)s->bi_buf); 1173 } 1174 s->bi_buf = 0; 1175 s->bi_valid = 0; 1176#ifdef ZLIB_DEBUG 1177 s->bits_sent = (s->bits_sent + 7) & ~7; 1178#endif 1179} | |