Lines 183-192
Link Here
|
183 |
#ifdef ZLIB_DEBUG |
183 |
#ifdef ZLIB_DEBUG |
184 |
local void send_bits OF((deflate_state *s, int value, int length)); |
184 |
local void send_bits OF((deflate_state *s, int value, int length)); |
185 |
|
185 |
|
186 |
local void send_bits(s, value, length) |
186 |
local void send_bits(deflate_state *s, |
187 |
deflate_state *s; |
187 |
int value /* value to send */, |
188 |
int value; /* value to send */ |
188 |
int length /* number of bits */) |
189 |
int length; /* number of bits */ |
|
|
190 |
{ |
189 |
{ |
191 |
Tracevv((stderr," l %2d v %4x ", length, value)); |
190 |
Tracevv((stderr," l %2d v %4x ", length, value)); |
192 |
Assert(length > 0 && length <= 15, "invalid length"); |
191 |
Assert(length > 0 && length <= 15, "invalid length"); |
Lines 376-383
Link Here
|
376 |
/* =========================================================================== |
375 |
/* =========================================================================== |
377 |
* Initialize the tree data structures for a new zlib stream. |
376 |
* Initialize the tree data structures for a new zlib stream. |
378 |
*/ |
377 |
*/ |
379 |
void ZLIB_INTERNAL _tr_init(s) |
378 |
void ZLIB_INTERNAL _tr_init(deflate_state *s) |
380 |
deflate_state *s; |
|
|
381 |
{ |
379 |
{ |
382 |
tr_static_init(); |
380 |
tr_static_init(); |
383 |
|
381 |
|
Lines 404-411
Link Here
|
404 |
/* =========================================================================== |
402 |
/* =========================================================================== |
405 |
* Initialize a new block. |
403 |
* Initialize a new block. |
406 |
*/ |
404 |
*/ |
407 |
local void init_block(s) |
405 |
local void init_block(deflate_state *s) |
408 |
deflate_state *s; |
|
|
409 |
{ |
406 |
{ |
410 |
int n; /* iterates over tree elements */ |
407 |
int n; /* iterates over tree elements */ |
411 |
|
408 |
|
Lines 448-457
Link Here
|
448 |
* when the heap property is re-established (each father smaller than its |
445 |
* when the heap property is re-established (each father smaller than its |
449 |
* two sons). |
446 |
* two sons). |
450 |
*/ |
447 |
*/ |
451 |
local void pqdownheap(s, tree, k) |
448 |
local void pqdownheap(deflate_state *s, |
452 |
deflate_state *s; |
449 |
ct_data *tree /* the tree to restore */, |
453 |
ct_data *tree; /* the tree to restore */ |
450 |
int k /* node to move down */) |
454 |
int k; /* node to move down */ |
|
|
455 |
{ |
451 |
{ |
456 |
int v = s->heap[k]; |
452 |
int v = s->heap[k]; |
457 |
int j = k << 1; /* left son of k */ |
453 |
int j = k << 1; /* left son of k */ |
Lines 483-491
Link Here
|
483 |
* The length opt_len is updated; static_len is also updated if stree is |
479 |
* The length opt_len is updated; static_len is also updated if stree is |
484 |
* not null. |
480 |
* not null. |
485 |
*/ |
481 |
*/ |
486 |
local void gen_bitlen(s, desc) |
482 |
local void gen_bitlen(deflate_state *s, |
487 |
deflate_state *s; |
483 |
tree_desc *desc /* the tree descriptor */) |
488 |
tree_desc *desc; /* the tree descriptor */ |
|
|
489 |
{ |
484 |
{ |
490 |
ct_data *tree = desc->dyn_tree; |
485 |
ct_data *tree = desc->dyn_tree; |
491 |
int max_code = desc->max_code; |
486 |
int max_code = desc->max_code; |
Lines 569-578
Link Here
|
569 |
* OUT assertion: the field code is set for all tree elements of non |
564 |
* OUT assertion: the field code is set for all tree elements of non |
570 |
* zero code length. |
565 |
* zero code length. |
571 |
*/ |
566 |
*/ |
572 |
local void gen_codes(tree, max_code, bl_count) |
567 |
local void gen_codes(ct_data *tree /* the tree to decorate */, |
573 |
ct_data *tree; /* the tree to decorate */ |
568 |
int max_code /* largest code with non zero frequency */, |
574 |
int max_code; /* largest code with non zero frequency */ |
569 |
ushf *bl_count /* number of codes at each bit length */) |
575 |
ushf *bl_count; /* number of codes at each bit length */ |
|
|
576 |
{ |
570 |
{ |
577 |
ush next_code[MAX_BITS+1]; /* next code value for each bit length */ |
571 |
ush next_code[MAX_BITS+1]; /* next code value for each bit length */ |
578 |
unsigned code = 0; /* running code value */ |
572 |
unsigned code = 0; /* running code value */ |
Lines 612-620
Link Here
|
612 |
* and corresponding code. The length opt_len is updated; static_len is |
606 |
* and corresponding code. The length opt_len is updated; static_len is |
613 |
* also updated if stree is not null. The field max_code is set. |
607 |
* also updated if stree is not null. The field max_code is set. |
614 |
*/ |
608 |
*/ |
615 |
local void build_tree(s, desc) |
609 |
local void build_tree(deflate_state *s, |
616 |
deflate_state *s; |
610 |
tree_desc *desc /* the tree descriptor */) |
617 |
tree_desc *desc; /* the tree descriptor */ |
|
|
618 |
{ |
611 |
{ |
619 |
ct_data *tree = desc->dyn_tree; |
612 |
ct_data *tree = desc->dyn_tree; |
620 |
const ct_data *stree = desc->stat_desc->static_tree; |
613 |
const ct_data *stree = desc->stat_desc->static_tree; |
Lines 700-709
Link Here
|
700 |
* Scan a literal or distance tree to determine the frequencies of the codes |
693 |
* Scan a literal or distance tree to determine the frequencies of the codes |
701 |
* in the bit length tree. |
694 |
* in the bit length tree. |
702 |
*/ |
695 |
*/ |
703 |
local void scan_tree(s, tree, max_code) |
696 |
local void scan_tree(deflate_state *s, |
704 |
deflate_state *s; |
697 |
ct_data *tree /* the tree to be scanned */, |
705 |
ct_data *tree; /* the tree to be scanned */ |
698 |
int max_code /* and its largest code of non zero frequency */) |
706 |
int max_code; /* and its largest code of non zero frequency */ |
|
|
707 |
{ |
699 |
{ |
708 |
int n; /* iterates over all tree elements */ |
700 |
int n; /* iterates over all tree elements */ |
709 |
int prevlen = -1; /* last emitted length */ |
701 |
int prevlen = -1; /* last emitted length */ |
Lines 745-754
Link Here
|
745 |
* Send a literal or distance tree in compressed form, using the codes in |
737 |
* Send a literal or distance tree in compressed form, using the codes in |
746 |
* bl_tree. |
738 |
* bl_tree. |
747 |
*/ |
739 |
*/ |
748 |
local void send_tree(s, tree, max_code) |
740 |
local void send_tree( deflate_state *s, |
749 |
deflate_state *s; |
741 |
ct_data *tree /* the tree to be scanned */, |
750 |
ct_data *tree; /* the tree to be scanned */ |
742 |
int max_code /* and its largest code of non zero frequency */) |
751 |
int max_code; /* and its largest code of non zero frequency */ |
|
|
752 |
{ |
743 |
{ |
753 |
int n; /* iterates over all tree elements */ |
744 |
int n; /* iterates over all tree elements */ |
754 |
int prevlen = -1; /* last emitted length */ |
745 |
int prevlen = -1; /* last emitted length */ |
Lines 796-803
Link Here
|
796 |
* Construct the Huffman tree for the bit lengths and return the index in |
787 |
* Construct the Huffman tree for the bit lengths and return the index in |
797 |
* bl_order of the last bit length code to send. |
788 |
* bl_order of the last bit length code to send. |
798 |
*/ |
789 |
*/ |
799 |
local int build_bl_tree(s) |
790 |
local int build_bl_tree(deflate_state *s) |
800 |
deflate_state *s; |
|
|
801 |
{ |
791 |
{ |
802 |
int max_blindex; /* index of last bit length code of non zero freq */ |
792 |
int max_blindex; /* index of last bit length code of non zero freq */ |
803 |
|
793 |
|
Lines 831-839
Link Here
|
831 |
* lengths of the bit length codes, the literal tree and the distance tree. |
821 |
* lengths of the bit length codes, the literal tree and the distance tree. |
832 |
* IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4. |
822 |
* IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4. |
833 |
*/ |
823 |
*/ |
834 |
local void send_all_trees(s, lcodes, dcodes, blcodes) |
824 |
local void send_all_trees(deflate_state *s, |
835 |
deflate_state *s; |
825 |
/* number of codes for each tree */ int lcodes, int dcodes, int blcodes) |
836 |
int lcodes, dcodes, blcodes; /* number of codes for each tree */ |
|
|
837 |
{ |
826 |
{ |
838 |
int rank; /* index in bl_order */ |
827 |
int rank; /* index in bl_order */ |
839 |
|
828 |
|
Lines 860-870
Link Here
|
860 |
/* =========================================================================== |
849 |
/* =========================================================================== |
861 |
* Send a stored block |
850 |
* Send a stored block |
862 |
*/ |
851 |
*/ |
863 |
void ZLIB_INTERNAL _tr_stored_block(s, buf, stored_len, last) |
852 |
void ZLIB_INTERNAL _tr_stored_block(deflate_state *s, |
864 |
deflate_state *s; |
853 |
charf *buf /* input block */, |
865 |
charf *buf; /* input block */ |
854 |
ulg stored_len /* length of input block */, |
866 |
ulg stored_len; /* length of input block */ |
855 |
int last /* one if this is the last block for a file */) |
867 |
int last; /* one if this is the last block for a file */ |
|
|
868 |
{ |
856 |
{ |
869 |
send_bits(s, (STORED_BLOCK<<1) + last, 3); /* send block type */ |
857 |
send_bits(s, (STORED_BLOCK<<1) + last, 3); /* send block type */ |
870 |
bi_windup(s); /* align on byte boundary */ |
858 |
bi_windup(s); /* align on byte boundary */ |
Lines 884-891
Link Here
|
884 |
/* =========================================================================== |
872 |
/* =========================================================================== |
885 |
* Flush the bits in the bit buffer to pending output (leaves at most 7 bits) |
873 |
* Flush the bits in the bit buffer to pending output (leaves at most 7 bits) |
886 |
*/ |
874 |
*/ |
887 |
void ZLIB_INTERNAL _tr_flush_bits(s) |
875 |
void ZLIB_INTERNAL _tr_flush_bits(deflate_state *s) |
888 |
deflate_state *s; |
|
|
889 |
{ |
876 |
{ |
890 |
bi_flush(s); |
877 |
bi_flush(s); |
891 |
} |
878 |
} |
Lines 894-901
Link Here
|
894 |
* Send one empty static block to give enough lookahead for inflate. |
881 |
* Send one empty static block to give enough lookahead for inflate. |
895 |
* This takes 10 bits, of which 7 may remain in the bit buffer. |
882 |
* This takes 10 bits, of which 7 may remain in the bit buffer. |
896 |
*/ |
883 |
*/ |
897 |
void ZLIB_INTERNAL _tr_align(s) |
884 |
void ZLIB_INTERNAL _tr_align(deflate_state *s) |
898 |
deflate_state *s; |
|
|
899 |
{ |
885 |
{ |
900 |
send_bits(s, STATIC_TREES<<1, 3); |
886 |
send_bits(s, STATIC_TREES<<1, 3); |
901 |
send_code(s, END_BLOCK, static_ltree); |
887 |
send_code(s, END_BLOCK, static_ltree); |
Lines 909-919
Link Here
|
909 |
* Determine the best encoding for the current block: dynamic trees, static |
895 |
* Determine the best encoding for the current block: dynamic trees, static |
910 |
* trees or store, and write out the encoded block. |
896 |
* trees or store, and write out the encoded block. |
911 |
*/ |
897 |
*/ |
912 |
void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last) |
898 |
void ZLIB_INTERNAL _tr_flush_block(deflate_state *s, |
913 |
deflate_state *s; |
899 |
charf *buf /* input block, or NULL if too old */, |
914 |
charf *buf; /* input block, or NULL if too old */ |
900 |
ulg stored_len /* length of input block */, |
915 |
ulg stored_len; /* length of input block */ |
901 |
int last /* one if this is the last block for a file */) |
916 |
int last; /* one if this is the last block for a file */ |
|
|
917 |
{ |
902 |
{ |
918 |
ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */ |
903 |
ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */ |
919 |
int max_blindex = 0; /* index of last bit length code of non zero freq */ |
904 |
int max_blindex = 0; /* index of last bit length code of non zero freq */ |
Lines 1011-1020
Link Here
|
1011 |
* Save the match info and tally the frequency counts. Return true if |
996 |
* Save the match info and tally the frequency counts. Return true if |
1012 |
* the current block must be flushed. |
997 |
* the current block must be flushed. |
1013 |
*/ |
998 |
*/ |
1014 |
int ZLIB_INTERNAL _tr_tally(s, dist, lc) |
999 |
int ZLIB_INTERNAL _tr_tally(deflate_state *s, |
1015 |
deflate_state *s; |
1000 |
unsigned dist /* distance of matched string */, |
1016 |
unsigned dist; /* distance of matched string */ |
1001 |
unsigned lc /* match length - MIN_MATCH or unmatched char (dist==0) */) |
1017 |
unsigned lc; /* match length - MIN_MATCH or unmatched char (dist==0) */ |
|
|
1018 |
{ |
1002 |
{ |
1019 |
s->sym_buf[s->sym_next++] = (uch)dist; |
1003 |
s->sym_buf[s->sym_next++] = (uch)dist; |
1020 |
s->sym_buf[s->sym_next++] = (uch)(dist >> 8); |
1004 |
s->sym_buf[s->sym_next++] = (uch)(dist >> 8); |
Lines 1039-1048
Link Here
|
1039 |
/* =========================================================================== |
1023 |
/* =========================================================================== |
1040 |
* Send the block data compressed using the given Huffman trees |
1024 |
* Send the block data compressed using the given Huffman trees |
1041 |
*/ |
1025 |
*/ |
1042 |
local void compress_block(s, ltree, dtree) |
1026 |
local void compress_block(deflate_state *s, |
1043 |
deflate_state *s; |
1027 |
const ct_data *ltree /* literal tree */, |
1044 |
const ct_data *ltree; /* literal tree */ |
1028 |
const ct_data *dtree /* distance tree */) |
1045 |
const ct_data *dtree; /* distance tree */ |
|
|
1046 |
{ |
1029 |
{ |
1047 |
unsigned dist; /* distance of matched string */ |
1030 |
unsigned dist; /* distance of matched string */ |
1048 |
int lc; /* match length or unmatched char (if dist == 0) */ |
1031 |
int lc; /* match length or unmatched char (if dist == 0) */ |
Lines 1099-1106
Link Here
|
1099 |
* (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}). |
1082 |
* (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}). |
1100 |
* IN assertion: the fields Freq of dyn_ltree are set. |
1083 |
* IN assertion: the fields Freq of dyn_ltree are set. |
1101 |
*/ |
1084 |
*/ |
1102 |
local int detect_data_type(s) |
1085 |
local int detect_data_type(deflate_state *s) |
1103 |
deflate_state *s; |
|
|
1104 |
{ |
1086 |
{ |
1105 |
/* block_mask is the bit mask of block-listed bytes |
1087 |
/* block_mask is the bit mask of block-listed bytes |
1106 |
* set bits 0..6, 14..25, and 28..31 |
1088 |
* set bits 0..6, 14..25, and 28..31 |
Lines 1133-1141
Link Here
|
1133 |
* method would use a table) |
1115 |
* method would use a table) |
1134 |
* IN assertion: 1 <= len <= 15 |
1116 |
* IN assertion: 1 <= len <= 15 |
1135 |
*/ |
1117 |
*/ |
1136 |
local unsigned bi_reverse(code, len) |
1118 |
local unsigned bi_reverse(unsigned code /* the value to invert */, |
1137 |
unsigned code; /* the value to invert */ |
1119 |
int len /* its bit length */) |
1138 |
int len; /* its bit length */ |
|
|
1139 |
{ |
1120 |
{ |
1140 |
register unsigned res = 0; |
1121 |
register unsigned res = 0; |
1141 |
do { |
1122 |
do { |
Lines 1148-1155
Link Here
|
1148 |
/* =========================================================================== |
1129 |
/* =========================================================================== |
1149 |
* Flush the bit buffer, keeping at most 7 bits in it. |
1130 |
* Flush the bit buffer, keeping at most 7 bits in it. |
1150 |
*/ |
1131 |
*/ |
1151 |
local void bi_flush(s) |
1132 |
local void bi_flush(deflate_state *s) |
1152 |
deflate_state *s; |
|
|
1153 |
{ |
1133 |
{ |
1154 |
if (s->bi_valid == 16) { |
1134 |
if (s->bi_valid == 16) { |
1155 |
put_short(s, s->bi_buf); |
1135 |
put_short(s, s->bi_buf); |
Lines 1165-1172
Link Here
|
1165 |
/* =========================================================================== |
1145 |
/* =========================================================================== |
1166 |
* Flush the bit buffer and align the output on a byte boundary |
1146 |
* Flush the bit buffer and align the output on a byte boundary |
1167 |
*/ |
1147 |
*/ |
1168 |
local void bi_windup(s) |
1148 |
local void bi_windup(deflate_state *s) |
1169 |
deflate_state *s; |
|
|
1170 |
{ |
1149 |
{ |
1171 |
if (s->bi_valid > 8) { |
1150 |
if (s->bi_valid > 8) { |
1172 |
put_short(s, s->bi_buf); |
1151 |
put_short(s, s->bi_buf); |