aboutsummaryrefslogtreecommitdiff
path: root/trees.c
diff options
context:
space:
mode:
Diffstat (limited to 'trees.c')
-rw-r--r--trees.c134
1 files changed, 74 insertions, 60 deletions
diff --git a/trees.c b/trees.c
index 4cb5454..dd91b73 100644
--- a/trees.c
+++ b/trees.c
@@ -1,5 +1,5 @@
1/* trees.c -- output deflated data using Huffman coding 1/* trees.c -- output deflated data using Huffman coding
2 * Copyright (C) 1995 Jean-loup Gailly 2 * Copyright (C) 1995-1996 Jean-loup Gailly
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
@@ -78,13 +78,12 @@ local uch bl_order[BL_CODES]
78 78
79/* =========================================================================== 79/* ===========================================================================
80 * Local data. These are initialized only once. 80 * Local data. These are initialized only once.
81 * To do: initialize at compile time to be completely reentrant. ???
82 */ 81 */
83 82
84local ct_data static_ltree[L_CODES+2]; 83local ct_data static_ltree[L_CODES+2];
85/* The static literal tree. Since the bit lengths are imposed, there is no 84/* The static literal tree. Since the bit lengths are imposed, there is no
86 * need for the L_CODES extra codes used during heap construction. However 85 * need for the L_CODES extra codes used during heap construction. However
87 * The codes 286 and 287 are needed to build a canonical tree (see ct_init 86 * The codes 286 and 287 are needed to build a canonical tree (see _tr_init
88 * below). 87 * below).
89 */ 88 */
90 89
@@ -129,7 +128,7 @@ local static_tree_desc static_bl_desc =
129 * Local (static) routines in this file. 128 * Local (static) routines in this file.
130 */ 129 */
131 130
132local void ct_static_init OF((void)); 131local void tr_static_init OF((void));
133local void init_block OF((deflate_state *s)); 132local void init_block OF((deflate_state *s));
134local void pqdownheap OF((deflate_state *s, ct_data *tree, int k)); 133local void pqdownheap OF((deflate_state *s, ct_data *tree, int k));
135local void gen_bitlen OF((deflate_state *s, tree_desc *desc)); 134local void gen_bitlen OF((deflate_state *s, tree_desc *desc));
@@ -187,7 +186,7 @@ local void send_bits(s, value, length)
187 int value; /* value to send */ 186 int value; /* value to send */
188 int length; /* number of bits */ 187 int length; /* number of bits */
189{ 188{
190 Tracev((stderr," l %2d v %4x ", length, value)); 189 Tracevv((stderr," l %2d v %4x ", length, value));
191 Assert(length > 0 && length <= 15, "invalid length"); 190 Assert(length > 0 && length <= 15, "invalid length");
192 s->bits_sent += (ulg)length; 191 s->bits_sent += (ulg)length;
193 192
@@ -227,11 +226,13 @@ local void send_bits(s, value, length)
227/* the arguments must not have side effects */ 226/* the arguments must not have side effects */
228 227
229/* =========================================================================== 228/* ===========================================================================
230 * Initialize the various 'constant' tables. 229 * Initialize the various 'constant' tables. In a multi-threaded environment,
231 * To do: do this at compile time. 230 * this function may be called by two threads concurrently, but this is
231 * harmless since both invocations do exactly the same thing.
232 */ 232 */
233local void ct_static_init() 233local void tr_static_init()
234{ 234{
235 static static_init_done = 0;
235 int n; /* iterates over tree elements */ 236 int n; /* iterates over tree elements */
236 int bits; /* bit counter */ 237 int bits; /* bit counter */
237 int length; /* length value */ 238 int length; /* length value */
@@ -240,6 +241,8 @@ local void ct_static_init()
240 ush bl_count[MAX_BITS+1]; 241 ush bl_count[MAX_BITS+1];
241 /* number of codes at each bit length for an optimal tree */ 242 /* number of codes at each bit length for an optimal tree */
242 243
244 if (static_init_done) return;
245
243 /* Initialize the mapping length (0..255) -> length code (0..28) */ 246 /* Initialize the mapping length (0..255) -> length code (0..28) */
244 length = 0; 247 length = 0;
245 for (code = 0; code < LENGTH_CODES-1; code++) { 248 for (code = 0; code < LENGTH_CODES-1; code++) {
@@ -248,7 +251,7 @@ local void ct_static_init()
248 length_code[length++] = (uch)code; 251 length_code[length++] = (uch)code;
249 } 252 }
250 } 253 }
251 Assert (length == 256, "ct_static_init: length != 256"); 254 Assert (length == 256, "tr_static_init: length != 256");
252 /* Note that the length 255 (match length 258) can be represented 255 /* Note that the length 255 (match length 258) can be represented
253 * in two different ways: code 284 + 5 bits or code 285, so we 256 * in two different ways: code 284 + 5 bits or code 285, so we
254 * overwrite length_code[255] to use the best encoding: 257 * overwrite length_code[255] to use the best encoding:
@@ -263,7 +266,7 @@ local void ct_static_init()
263 dist_code[dist++] = (uch)code; 266 dist_code[dist++] = (uch)code;
264 } 267 }
265 } 268 }
266 Assert (dist == 256, "ct_static_init: dist != 256"); 269 Assert (dist == 256, "tr_static_init: dist != 256");
267 dist >>= 7; /* from now on, all distances are divided by 128 */ 270 dist >>= 7; /* from now on, all distances are divided by 128 */
268 for ( ; code < D_CODES; code++) { 271 for ( ; code < D_CODES; code++) {
269 base_dist[code] = dist << 7; 272 base_dist[code] = dist << 7;
@@ -271,7 +274,7 @@ local void ct_static_init()
271 dist_code[256 + dist++] = (uch)code; 274 dist_code[256 + dist++] = (uch)code;
272 } 275 }
273 } 276 }
274 Assert (dist == 256, "ct_static_init: 256+dist != 512"); 277 Assert (dist == 256, "tr_static_init: 256+dist != 512");
275 278
276 /* Construct the codes of the static literal tree */ 279 /* Construct the codes of the static literal tree */
277 for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0; 280 for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;
@@ -289,19 +292,18 @@ local void ct_static_init()
289 /* The static distance tree is trivial: */ 292 /* The static distance tree is trivial: */
290 for (n = 0; n < D_CODES; n++) { 293 for (n = 0; n < D_CODES; n++) {
291 static_dtree[n].Len = 5; 294 static_dtree[n].Len = 5;
292 static_dtree[n].Code = bi_reverse(n, 5); 295 static_dtree[n].Code = bi_reverse((unsigned)n, 5);
293 } 296 }
297 static_init_done = 1;
294} 298}
295 299
296/* =========================================================================== 300/* ===========================================================================
297 * Initialize the tree data structures for a new zlib stream. 301 * Initialize the tree data structures for a new zlib stream.
298 */ 302 */
299void ct_init(s) 303void _tr_init(s)
300 deflate_state *s; 304 deflate_state *s;
301{ 305{
302 if (static_dtree[0].Len == 0) { 306 tr_static_init();
303 ct_static_init(); /* To do: at compile time */
304 }
305 307
306 s->compressed_len = 0L; 308 s->compressed_len = 0L;
307 309
@@ -523,7 +525,7 @@ local void gen_codes (tree, max_code, bl_count)
523 /* Now reverse the bits */ 525 /* Now reverse the bits */
524 tree[n].Code = bi_reverse(next_code[len]++, len); 526 tree[n].Code = bi_reverse(next_code[len]++, len);
525 527
526 Tracec(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ", 528 Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
527 n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1)); 529 n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
528 } 530 }
529} 531}
@@ -783,14 +785,14 @@ local void send_all_trees(s, lcodes, dcodes, blcodes)
783/* =========================================================================== 785/* ===========================================================================
784 * Send a stored block 786 * Send a stored block
785 */ 787 */
786void ct_stored_block(s, buf, stored_len, eof) 788void _tr_stored_block(s, buf, stored_len, eof)
787 deflate_state *s; 789 deflate_state *s;
788 charf *buf; /* input block */ 790 charf *buf; /* input block */
789 ulg stored_len; /* length of input block */ 791 ulg stored_len; /* length of input block */
790 int eof; /* true if this is the last block for a file */ 792 int eof; /* true if this is the last block for a file */
791{ 793{
792 send_bits(s, (STORED_BLOCK<<1)+eof, 3); /* send block type */ 794 send_bits(s, (STORED_BLOCK<<1)+eof, 3); /* send block type */
793 s->compressed_len = (s->compressed_len + 3 + 7) & ~7L; 795 s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L;
794 s->compressed_len += (stored_len + 4) << 3; 796 s->compressed_len += (stored_len + 4) << 3;
795 797
796 copy_block(s, buf, (unsigned)stored_len, 1); /* with header */ 798 copy_block(s, buf, (unsigned)stored_len, 1); /* with header */
@@ -799,12 +801,15 @@ void ct_stored_block(s, buf, stored_len, eof)
799/* =========================================================================== 801/* ===========================================================================
800 * Send one empty static block to give enough lookahead for inflate. 802 * Send one empty static block to give enough lookahead for inflate.
801 * This takes 10 bits, of which 7 may remain in the bit buffer. 803 * This takes 10 bits, of which 7 may remain in the bit buffer.
802 * The current inflate code requires 9 bits of lookahead. If the EOB 804 * The current inflate code requires 9 bits of lookahead. If the
803 * code for the previous block was coded on 5 bits or less, inflate 805 * last two codes for the previous block (real code plus EOB) were coded
804 * may have only 5+3 bits of lookahead to decode this EOB. 806 * on 5 bits or less, inflate may have only 5+3 bits of lookahead to decode
805 * (There are no problems if the previous block is stored or fixed.) 807 * the last real code. In this case we send two empty static blocks instead
808 * of one. (There are no problems if the previous block is stored or fixed.)
809 * To simplify the code, we assume the worst case of last real code encoded
810 * on one bit only.
806 */ 811 */
807void ct_align(s) 812void _tr_align(s)
808 deflate_state *s; 813 deflate_state *s;
809{ 814{
810 send_bits(s, STATIC_TREES<<1, 3); 815 send_bits(s, STATIC_TREES<<1, 3);
@@ -812,10 +817,11 @@ void ct_align(s)
812 s->compressed_len += 10L; /* 3 for block type, 7 for EOB */ 817 s->compressed_len += 10L; /* 3 for block type, 7 for EOB */
813 bi_flush(s); 818 bi_flush(s);
814 /* Of the 10 bits for the empty block, we have already sent 819 /* Of the 10 bits for the empty block, we have already sent
815 * (10 - bi_valid) bits. The lookahead for the EOB of the previous 820 * (10 - bi_valid) bits. The lookahead for the last real code (before
816 * block was thus its length plus what we have just sent. 821 * the EOB of the previous block) was thus at least one plus the length
822 * of the EOB plus what we have just sent of the empty static block.
817 */ 823 */
818 if (s->last_eob_len + 10 - s->bi_valid < 9) { 824 if (1 + s->last_eob_len + 10 - s->bi_valid < 9) {
819 send_bits(s, STATIC_TREES<<1, 3); 825 send_bits(s, STATIC_TREES<<1, 3);
820 send_code(s, END_BLOCK, static_ltree); 826 send_code(s, END_BLOCK, static_ltree);
821 s->compressed_len += 10L; 827 s->compressed_len += 10L;
@@ -829,44 +835,52 @@ void ct_align(s)
829 * trees or store, and output the encoded block to the zip file. This function 835 * trees or store, and output the encoded block to the zip file. This function
830 * returns the total compressed length for the file so far. 836 * returns the total compressed length for the file so far.
831 */ 837 */
832ulg ct_flush_block(s, buf, stored_len, eof) 838ulg _tr_flush_block(s, buf, stored_len, eof)
833 deflate_state *s; 839 deflate_state *s;
834 charf *buf; /* input block, or NULL if too old */ 840 charf *buf; /* input block, or NULL if too old */
835 ulg stored_len; /* length of input block */ 841 ulg stored_len; /* length of input block */
836 int eof; /* true if this is the last block for a file */ 842 int eof; /* true if this is the last block for a file */
837{ 843{
838 ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */ 844 ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
839 int max_blindex; /* index of last bit length code of non zero freq */ 845 int max_blindex = 0; /* index of last bit length code of non zero freq */
840 846
841 /* Check if the file is ascii or binary */ 847 /* Build the Huffman trees unless a stored block is forced */
842 if (s->data_type == UNKNOWN) set_data_type(s); 848 if (s->level > 0) {
843 849
844 /* Construct the literal and distance trees */ 850 /* Check if the file is ascii or binary */
845 build_tree(s, (tree_desc *)(&(s->l_desc))); 851 if (s->data_type == Z_UNKNOWN) set_data_type(s);
846 Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
847 s->static_len));
848 852
849 build_tree(s, (tree_desc *)(&(s->d_desc))); 853 /* Construct the literal and distance trees */
850 Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len, 854 build_tree(s, (tree_desc *)(&(s->l_desc)));
851 s->static_len)); 855 Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
852 /* At this point, opt_len and static_len are the total bit lengths of 856 s->static_len));
853 * the compressed block data, excluding the tree representations.
854 */
855 857
856 /* Build the bit length tree for the above two trees, and get the index 858 build_tree(s, (tree_desc *)(&(s->d_desc)));
857 * in bl_order of the last bit length code to send. 859 Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
858 */ 860 s->static_len));
859 max_blindex = build_bl_tree(s); 861 /* At this point, opt_len and static_len are the total bit lengths of
862 * the compressed block data, excluding the tree representations.
863 */
860 864
861 /* Determine the best encoding. Compute first the block length in bytes */ 865 /* Build the bit length tree for the above two trees, and get the index
862 opt_lenb = (s->opt_len+3+7)>>3; 866 * in bl_order of the last bit length code to send.
863 static_lenb = (s->static_len+3+7)>>3; 867 */
868 max_blindex = build_bl_tree(s);
864 869
865 Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ", 870 /* Determine the best encoding. Compute first the block length in bytes*/
866 opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len, 871 opt_lenb = (s->opt_len+3+7)>>3;
867 s->last_lit)); 872 static_lenb = (s->static_len+3+7)>>3;
868 873
869 if (static_lenb <= opt_lenb) opt_lenb = static_lenb; 874 Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
875 opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
876 s->last_lit));
877
878 if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
879
880 } else {
881 Assert(buf != (char*)0, "lost buf");
882 opt_lenb = static_lenb = stored_len + 5; /* force a stored block */
883 }
870 884
871 /* If compression failed and this is the first and last block, 885 /* If compression failed and this is the first and last block,
872 * and if the .zip file can be seeked (to rewrite the local header), 886 * and if the .zip file can be seeked (to rewrite the local header),
@@ -874,7 +888,7 @@ ulg ct_flush_block(s, buf, stored_len, eof)
874 */ 888 */
875#ifdef STORED_FILE_OK 889#ifdef STORED_FILE_OK
876# ifdef FORCE_STORED_FILE 890# ifdef FORCE_STORED_FILE
877 if (eof && compressed_len == 0L) { /* force stored file */ 891 if (eof && s->compressed_len == 0L) { /* force stored file */
878# else 892# else
879 if (stored_len <= opt_lenb && eof && s->compressed_len==0L && seekable()) { 893 if (stored_len <= opt_lenb && eof && s->compressed_len==0L && seekable()) {
880# endif 894# endif
@@ -899,7 +913,7 @@ ulg ct_flush_block(s, buf, stored_len, eof)
899 * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to 913 * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
900 * transform a block into a stored block. 914 * transform a block into a stored block.
901 */ 915 */
902 ct_stored_block(s, buf, stored_len, eof); 916 _tr_stored_block(s, buf, stored_len, eof);
903 917
904#ifdef FORCE_STATIC 918#ifdef FORCE_STATIC
905 } else if (static_lenb >= 0) { /* force static trees */ 919 } else if (static_lenb >= 0) { /* force static trees */
@@ -933,10 +947,10 @@ ulg ct_flush_block(s, buf, stored_len, eof)
933 * Save the match info and tally the frequency counts. Return true if 947 * Save the match info and tally the frequency counts. Return true if
934 * the current block must be flushed. 948 * the current block must be flushed.
935 */ 949 */
936int ct_tally (s, dist, lc) 950int _tr_tally (s, dist, lc)
937 deflate_state *s; 951 deflate_state *s;
938 int dist; /* distance of matched string */ 952 unsigned dist; /* distance of matched string */
939 int lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */ 953 unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */
940{ 954{
941 s->d_buf[s->last_lit] = (ush)dist; 955 s->d_buf[s->last_lit] = (ush)dist;
942 s->l_buf[s->last_lit++] = (uch)lc; 956 s->l_buf[s->last_lit++] = (uch)lc;
@@ -949,7 +963,7 @@ int ct_tally (s, dist, lc)
949 dist--; /* dist = match distance - 1 */ 963 dist--; /* dist = match distance - 1 */
950 Assert((ush)dist < (ush)MAX_DIST(s) && 964 Assert((ush)dist < (ush)MAX_DIST(s) &&
951 (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) && 965 (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
952 (ush)d_code(dist) < (ush)D_CODES, "ct_tally: bad match"); 966 (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match");
953 967
954 s->dyn_ltree[length_code[lc]+LITERALS+1].Freq++; 968 s->dyn_ltree[length_code[lc]+LITERALS+1].Freq++;
955 s->dyn_dtree[d_code(dist)].Freq++; 969 s->dyn_dtree[d_code(dist)].Freq++;
@@ -959,7 +973,7 @@ int ct_tally (s, dist, lc)
959 if (s->level > 2 && (s->last_lit & 0xfff) == 0) { 973 if (s->level > 2 && (s->last_lit & 0xfff) == 0) {
960 /* Compute an upper bound for the compressed length */ 974 /* Compute an upper bound for the compressed length */
961 ulg out_length = (ulg)s->last_lit*8L; 975 ulg out_length = (ulg)s->last_lit*8L;
962 ulg in_length = (ulg)s->strstart - s->block_start; 976 ulg in_length = (ulg)((long)s->strstart - s->block_start);
963 int dcode; 977 int dcode;
964 for (dcode = 0; dcode < D_CODES; dcode++) { 978 for (dcode = 0; dcode < D_CODES; dcode++) {
965 out_length += (ulg)s->dyn_dtree[dcode].Freq * 979 out_length += (ulg)s->dyn_dtree[dcode].Freq *
@@ -1043,7 +1057,7 @@ local void set_data_type(s)
1043 while (n < 7) bin_freq += s->dyn_ltree[n++].Freq; 1057 while (n < 7) bin_freq += s->dyn_ltree[n++].Freq;
1044 while (n < 128) ascii_freq += s->dyn_ltree[n++].Freq; 1058 while (n < 128) ascii_freq += s->dyn_ltree[n++].Freq;
1045 while (n < LITERALS) bin_freq += s->dyn_ltree[n++].Freq; 1059 while (n < LITERALS) bin_freq += s->dyn_ltree[n++].Freq;
1046 s->data_type = (Byte)(bin_freq > (ascii_freq >> 2) ? BINARY : ASCII); 1060 s->data_type = (Byte)(bin_freq > (ascii_freq >> 2) ? Z_BINARY : Z_ASCII);
1047} 1061}
1048 1062
1049/* =========================================================================== 1063/* ===========================================================================