summaryrefslogtreecommitdiff
path: root/trees.c
diff options
context:
space:
mode:
Diffstat (limited to 'trees.c')
-rw-r--r--trees.c52
1 files changed, 25 insertions, 27 deletions
diff --git a/trees.c b/trees.c
index ef31043..f01fb30 100644
--- a/trees.c
+++ b/trees.c
@@ -250,6 +250,13 @@ local void tr_static_init()
250 250
251 if (static_init_done) return; 251 if (static_init_done) return;
252 252
253 /* For some embedded targets, global variables are not initialized: */
254 static_l_desc.static_tree = static_ltree;
255 static_l_desc.extra_bits = extra_lbits;
256 static_d_desc.static_tree = static_dtree;
257 static_d_desc.extra_bits = extra_dbits;
258 static_bl_desc.extra_bits = extra_blbits;
259
253 /* Initialize the mapping length (0..255) -> length code (0..28) */ 260 /* Initialize the mapping length (0..255) -> length code (0..28) */
254 length = 0; 261 length = 0;
255 for (code = 0; code < LENGTH_CODES-1; code++) { 262 for (code = 0; code < LENGTH_CODES-1; code++) {
@@ -378,8 +385,6 @@ void _tr_init(s)
378{ 385{
379 tr_static_init(); 386 tr_static_init();
380 387
381 s->compressed_len = 0L;
382
383 s->l_desc.dyn_tree = s->dyn_ltree; 388 s->l_desc.dyn_tree = s->dyn_ltree;
384 s->l_desc.stat_desc = &static_l_desc; 389 s->l_desc.stat_desc = &static_l_desc;
385 390
@@ -393,6 +398,7 @@ void _tr_init(s)
393 s->bi_valid = 0; 398 s->bi_valid = 0;
394 s->last_eob_len = 8; /* enough lookahead for inflate */ 399 s->last_eob_len = 8; /* enough lookahead for inflate */
395#ifdef DEBUG 400#ifdef DEBUG
401 s->compressed_len = 0L;
396 s->bits_sent = 0L; 402 s->bits_sent = 0L;
397#endif 403#endif
398 404
@@ -865,9 +871,10 @@ void _tr_stored_block(s, buf, stored_len, eof)
865 int eof; /* true if this is the last block for a file */ 871 int eof; /* true if this is the last block for a file */
866{ 872{
867 send_bits(s, (STORED_BLOCK<<1)+eof, 3); /* send block type */ 873 send_bits(s, (STORED_BLOCK<<1)+eof, 3); /* send block type */
874#ifdef DEBUG
868 s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L; 875 s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L;
869 s->compressed_len += (stored_len + 4) << 3; 876 s->compressed_len += (stored_len + 4) << 3;
870 877#endif
871 copy_block(s, buf, (unsigned)stored_len, 1); /* with header */ 878 copy_block(s, buf, (unsigned)stored_len, 1); /* with header */
872} 879}
873 880
@@ -887,7 +894,9 @@ void _tr_align(s)
887{ 894{
888 send_bits(s, STATIC_TREES<<1, 3); 895 send_bits(s, STATIC_TREES<<1, 3);
889 send_code(s, END_BLOCK, static_ltree); 896 send_code(s, END_BLOCK, static_ltree);
897#ifdef DEBUG
890 s->compressed_len += 10L; /* 3 for block type, 7 for EOB */ 898 s->compressed_len += 10L; /* 3 for block type, 7 for EOB */
899#endif
891 bi_flush(s); 900 bi_flush(s);
892 /* Of the 10 bits for the empty block, we have already sent 901 /* Of the 10 bits for the empty block, we have already sent
893 * (10 - bi_valid) bits. The lookahead for the last real code (before 902 * (10 - bi_valid) bits. The lookahead for the last real code (before
@@ -897,7 +906,9 @@ void _tr_align(s)
897 if (1 + s->last_eob_len + 10 - s->bi_valid < 9) { 906 if (1 + s->last_eob_len + 10 - s->bi_valid < 9) {
898 send_bits(s, STATIC_TREES<<1, 3); 907 send_bits(s, STATIC_TREES<<1, 3);
899 send_code(s, END_BLOCK, static_ltree); 908 send_code(s, END_BLOCK, static_ltree);
909#ifdef DEBUG
900 s->compressed_len += 10L; 910 s->compressed_len += 10L;
911#endif
901 bi_flush(s); 912 bi_flush(s);
902 } 913 }
903 s->last_eob_len = 7; 914 s->last_eob_len = 7;
@@ -905,10 +916,9 @@ void _tr_align(s)
905 916
906/* =========================================================================== 917/* ===========================================================================
907 * Determine the best encoding for the current block: dynamic trees, static 918 * Determine the best encoding for the current block: dynamic trees, static
908 * trees or store, and output the encoded block to the zip file. This function 919 * trees or store, and output the encoded block to the zip file.
909 * returns the total compressed length for the file so far.
910 */ 920 */
911ulg _tr_flush_block(s, buf, stored_len, eof) 921void _tr_flush_block(s, buf, stored_len, eof)
912 deflate_state *s; 922 deflate_state *s;
913 charf *buf; /* input block, or NULL if too old */ 923 charf *buf; /* input block, or NULL if too old */
914 ulg stored_len; /* length of input block */ 924 ulg stored_len; /* length of input block */
@@ -955,25 +965,6 @@ ulg _tr_flush_block(s, buf, stored_len, eof)
955 opt_lenb = static_lenb = stored_len + 5; /* force a stored block */ 965 opt_lenb = static_lenb = stored_len + 5; /* force a stored block */
956 } 966 }
957 967
958 /* If compression failed and this is the first and last block,
959 * and if the .zip file can be seeked (to rewrite the local header),
960 * the whole file is transformed into a stored file:
961 */
962#ifdef STORED_FILE_OK
963# ifdef FORCE_STORED_FILE
964 if (eof && s->compressed_len == 0L) { /* force stored file */
965# else
966 if (stored_len <= opt_lenb && eof && s->compressed_len==0L && seekable()) {
967# endif
968 /* Since LIT_BUFSIZE <= 2*WSIZE, the input data must be there: */
969 if (buf == (charf*)0) error ("block vanished");
970
971 copy_block(buf, (unsigned)stored_len, 0); /* without header */
972 s->compressed_len = stored_len << 3;
973 s->method = STORED;
974 } else
975#endif /* STORED_FILE_OK */
976
977#ifdef FORCE_STORED 968#ifdef FORCE_STORED
978 if (buf != (char*)0) { /* force stored block */ 969 if (buf != (char*)0) { /* force stored block */
979#else 970#else
@@ -995,25 +986,32 @@ ulg _tr_flush_block(s, buf, stored_len, eof)
995#endif 986#endif
996 send_bits(s, (STATIC_TREES<<1)+eof, 3); 987 send_bits(s, (STATIC_TREES<<1)+eof, 3);
997 compress_block(s, (ct_data *)static_ltree, (ct_data *)static_dtree); 988 compress_block(s, (ct_data *)static_ltree, (ct_data *)static_dtree);
989#ifdef DEBUG
998 s->compressed_len += 3 + s->static_len; 990 s->compressed_len += 3 + s->static_len;
991#endif
999 } else { 992 } else {
1000 send_bits(s, (DYN_TREES<<1)+eof, 3); 993 send_bits(s, (DYN_TREES<<1)+eof, 3);
1001 send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1, 994 send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1,
1002 max_blindex+1); 995 max_blindex+1);
1003 compress_block(s, (ct_data *)s->dyn_ltree, (ct_data *)s->dyn_dtree); 996 compress_block(s, (ct_data *)s->dyn_ltree, (ct_data *)s->dyn_dtree);
997#ifdef DEBUG
1004 s->compressed_len += 3 + s->opt_len; 998 s->compressed_len += 3 + s->opt_len;
999#endif
1005 } 1000 }
1006 Assert (s->compressed_len == s->bits_sent, "bad compressed size"); 1001 Assert (s->compressed_len == s->bits_sent, "bad compressed size");
1002 /* The above check is made mod 2^32, for files larger than 512 MB
1003 * and uLong implemented on 32 bits.
1004 */
1007 init_block(s); 1005 init_block(s);
1008 1006
1009 if (eof) { 1007 if (eof) {
1010 bi_windup(s); 1008 bi_windup(s);
1009#ifdef DEBUG
1011 s->compressed_len += 7; /* align on byte boundary */ 1010 s->compressed_len += 7; /* align on byte boundary */
1011#endif
1012 } 1012 }
1013 Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3, 1013 Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
1014 s->compressed_len-7*eof)); 1014 s->compressed_len-7*eof));
1015
1016 return s->compressed_len >> 3;
1017} 1015}
1018 1016
1019/* =========================================================================== 1017/* ===========================================================================