diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-01-07 19:44:57 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-01-07 19:44:57 +0000 |
commit | 3ae6f34135af68818aced918f675c525d5c5a4cf (patch) | |
tree | 4e89ae167a8f1be21eeba9313f2e238ef0b19a50 | |
parent | 2f6df7fa0a70810d70e8ca0816b64c0f40b76847 (diff) | |
download | busybox-w32-3ae6f34135af68818aced918f675c525d5c5a4cf.tar.gz busybox-w32-3ae6f34135af68818aced918f675c525d5c5a4cf.tar.bz2 busybox-w32-3ae6f34135af68818aced918f675c525d5c5a4cf.zip |
gzip cleanup part #12
-rw-r--r-- | archival/gzip.c | 403 |
1 files changed, 200 insertions, 203 deletions
diff --git a/archival/gzip.c b/archival/gzip.c index 17010438a..632dd4a14 100644 --- a/archival/gzip.c +++ b/archival/gzip.c | |||
@@ -39,8 +39,6 @@ gzip: bogus: No such file or directory | |||
39 | aa: 85.1% -- replaced with aa.gz | 39 | aa: 85.1% -- replaced with aa.gz |
40 | */ | 40 | */ |
41 | 41 | ||
42 | |||
43 | //#include <dirent.h> | ||
44 | #include "busybox.h" | 42 | #include "busybox.h" |
45 | 43 | ||
46 | 44 | ||
@@ -195,10 +193,12 @@ aa: 85.1% -- replaced with aa.gz | |||
195 | 193 | ||
196 | 194 | ||
197 | /* =========================================================================== | 195 | /* =========================================================================== |
196 | * These types are not really 'char', 'short' and 'long' | ||
198 | */ | 197 | */ |
199 | typedef unsigned char uch; | 198 | typedef uint8_t uch; |
200 | typedef unsigned short ush; | 199 | typedef uint16_t ush; |
201 | typedef unsigned long ulg; | 200 | typedef uint32_t ulg; |
201 | typedef uint32_t lng; | ||
202 | 202 | ||
203 | 203 | ||
204 | /* =========================================================================== | 204 | /* =========================================================================== |
@@ -211,7 +211,7 @@ typedef unsigned IPos; | |||
211 | * save space in the various tables. IPos is used only for parameter passing. | 211 | * save space in the various tables. IPos is used only for parameter passing. |
212 | */ | 212 | */ |
213 | 213 | ||
214 | #define DECLARE(type, array, size)\ | 214 | #define DECLARE(type, array, size) \ |
215 | static type * array | 215 | static type * array |
216 | #define ALLOC(type, array, size) { \ | 216 | #define ALLOC(type, array, size) { \ |
217 | array = xzalloc((size_t)(((size)+1L)/2) * 2*sizeof(type)); \ | 217 | array = xzalloc((size_t)(((size)+1L)/2) * 2*sizeof(type)); \ |
@@ -223,7 +223,7 @@ typedef unsigned IPos; | |||
223 | array = NULL; \ | 223 | array = NULL; \ |
224 | } | 224 | } |
225 | 225 | ||
226 | static long block_start; | 226 | static lng block_start; |
227 | 227 | ||
228 | /* window position at the beginning of the current output block. Gets | 228 | /* window position at the beginning of the current output block. Gets |
229 | * negative when the window is moved backwards. | 229 | * negative when the window is moved backwards. |
@@ -339,12 +339,15 @@ DECLARE(ush, d_buf, DIST_BUFSIZE); | |||
339 | DECLARE(uch, window, 2L * WSIZE); | 339 | DECLARE(uch, window, 2L * WSIZE); |
340 | DECLARE(ush, prev, 1L << BITS); | 340 | DECLARE(ush, prev, 1L << BITS); |
341 | 341 | ||
342 | static long isize; /* number of input bytes */ | 342 | /* number of input bytes */ |
343 | static ulg isize; /* only 32 bits stored in .gz file */ | ||
343 | 344 | ||
344 | static int foreground; /* set if program run in foreground */ | 345 | static int foreground; /* set if program run in foreground */ |
345 | static int method = DEFLATED; /* compression method */ | 346 | static int method = DEFLATED; /* compression method */ |
346 | static int exit_code; /* program exit code */ | 347 | static int exit_code; /* program exit code */ |
347 | static long time_stamp; /* original time stamp (modification time) */ | 348 | |
349 | /* original time stamp (modification time) */ | ||
350 | static ulg time_stamp; /* only 32 bits stored in .gz file */ | ||
348 | ////static char z_suffix[MAX_SUFFIX + 1]; /* default suffix (can be set with --suffix) */ | 351 | ////static char z_suffix[MAX_SUFFIX + 1]; /* default suffix (can be set with --suffix) */ |
349 | 352 | ||
350 | static int ifd; /* input file descriptor */ | 353 | static int ifd; /* input file descriptor */ |
@@ -425,15 +428,6 @@ static void put_32bit(ulg n) | |||
425 | put_16bit(n >> 16); | 428 | put_16bit(n >> 16); |
426 | } | 429 | } |
427 | 430 | ||
428 | /* put_header_byte is used for the compressed output | ||
429 | * - for the initial 4 bytes that can't overflow the buffer. | ||
430 | */ | ||
431 | #define put_header_byte(c) \ | ||
432 | { \ | ||
433 | outbuf[outcnt++] = (c); \ | ||
434 | } | ||
435 | |||
436 | |||
437 | /* =========================================================================== | 431 | /* =========================================================================== |
438 | * Clear input and output buffers | 432 | * Clear input and output buffers |
439 | */ | 433 | */ |
@@ -443,7 +437,7 @@ static void clear_bufs(void) | |||
443 | #ifdef DEBUG | 437 | #ifdef DEBUG |
444 | insize = 0; | 438 | insize = 0; |
445 | #endif | 439 | #endif |
446 | isize = 0L; | 440 | isize = 0; |
447 | } | 441 | } |
448 | 442 | ||
449 | 443 | ||
@@ -487,20 +481,6 @@ static unsigned file_read(void *buf, unsigned size) | |||
487 | 481 | ||
488 | 482 | ||
489 | /* =========================================================================== | 483 | /* =========================================================================== |
490 | * Initialize the bit string routines. | ||
491 | */ | ||
492 | static void bi_init(int zipfile) | ||
493 | { | ||
494 | zfile = zipfile; | ||
495 | bi_buf = 0; | ||
496 | bi_valid = 0; | ||
497 | #ifdef DEBUG | ||
498 | bits_sent = 0L; | ||
499 | #endif | ||
500 | } | ||
501 | |||
502 | |||
503 | /* =========================================================================== | ||
504 | * Send a value on a given number of bits. | 484 | * Send a value on a given number of bits. |
505 | * IN assertion: length <= 16 and value fits in length bits. | 485 | * IN assertion: length <= 16 and value fits in length bits. |
506 | */ | 486 | */ |
@@ -647,56 +627,6 @@ static void fill_window(void) | |||
647 | 627 | ||
648 | 628 | ||
649 | /* =========================================================================== | 629 | /* =========================================================================== |
650 | * Update a hash value with the given input byte | ||
651 | * IN assertion: all calls to to UPDATE_HASH are made with consecutive | ||
652 | * input characters, so that a running hash key can be computed from the | ||
653 | * previous key instead of complete recalculation each time. | ||
654 | */ | ||
655 | #define UPDATE_HASH(h, c) (h = (((h)<<H_SHIFT) ^ (c)) & HASH_MASK) | ||
656 | |||
657 | |||
658 | /* =========================================================================== | ||
659 | * Initialize the "longest match" routines for a new file | ||
660 | */ | ||
661 | static void lm_init(ush * flags) | ||
662 | { | ||
663 | unsigned j; | ||
664 | |||
665 | /* Initialize the hash table. */ | ||
666 | memset(head, 0, HASH_SIZE * sizeof(*head)); | ||
667 | /* prev will be initialized on the fly */ | ||
668 | |||
669 | /*speed options for the general purpose bit flag */ | ||
670 | *flags |= 2; /* FAST 4, SLOW 2 */ | ||
671 | /* ??? reduce max_chain_length for binary files */ | ||
672 | |||
673 | strstart = 0; | ||
674 | block_start = 0L; | ||
675 | |||
676 | lookahead = file_read(window, | ||
677 | sizeof(int) <= 2 ? (unsigned) WSIZE : 2 * WSIZE); | ||
678 | |||
679 | if (lookahead == 0 || lookahead == (unsigned) -1) { | ||
680 | eofile = 1; | ||
681 | lookahead = 0; | ||
682 | return; | ||
683 | } | ||
684 | eofile = 0; | ||
685 | /* Make sure that we always have enough lookahead. This is important | ||
686 | * if input comes from a device such as a tty. | ||
687 | */ | ||
688 | while (lookahead < MIN_LOOKAHEAD && !eofile) | ||
689 | fill_window(); | ||
690 | |||
691 | ins_h = 0; | ||
692 | for (j = 0; j < MIN_MATCH - 1; j++) | ||
693 | UPDATE_HASH(ins_h, window[j]); | ||
694 | /* If lookahead < MIN_MATCH, ins_h is garbage, but this is | ||
695 | * not important since only literal bytes will be emitted. | ||
696 | */ | ||
697 | } | ||
698 | |||
699 | /* =========================================================================== | ||
700 | * Set match_start to the longest match starting at the given string and | 630 | * Set match_start to the longest match starting at the given string and |
701 | * return its length. Matches shorter or equal to prev_length are discarded, | 631 | * return its length. Matches shorter or equal to prev_length are discarded, |
702 | * in which case the result is equal to prev_length and match_start is | 632 | * in which case the result is equal to prev_length and match_start is |
@@ -850,7 +780,7 @@ static void check_match(IPos start, IPos match, int length) | |||
850 | * void ct_tally(int dist, int lc); | 780 | * void ct_tally(int dist, int lc); |
851 | * Save the match info and tally the frequency counts. | 781 | * Save the match info and tally the frequency counts. |
852 | * | 782 | * |
853 | * long flush_block (char *buf, ulg stored_len, int eof) | 783 | * ulg flush_block(char *buf, ulg stored_len, int eof) |
854 | * Determine the best encoding for the current block: dynamic trees, | 784 | * Determine the best encoding for the current block: dynamic trees, |
855 | * static trees or store, and output the encoded block to the zip | 785 | * static trees or store, and output the encoded block to the zip |
856 | * file. Returns the total compressed length for the file so far. | 786 | * file. Returns the total compressed length for the file so far. |
@@ -1068,25 +998,24 @@ static uch flag_buf[LIT_BUFSIZE / 8]; | |||
1068 | * l_buf, thus indicating the presence or absence of a distance. | 998 | * l_buf, thus indicating the presence or absence of a distance. |
1069 | */ | 999 | */ |
1070 | 1000 | ||
1071 | static unsigned last_lit; /* running index in l_buf */ | 1001 | static unsigned last_lit; /* running index in l_buf */ |
1072 | static unsigned last_dist; /* running index in d_buf */ | 1002 | static unsigned last_dist; /* running index in d_buf */ |
1073 | static unsigned last_flags; /* running index in flag_buf */ | 1003 | static unsigned last_flags; /* running index in flag_buf */ |
1074 | static uch flags; /* current flags not yet saved in flag_buf */ | 1004 | static uch flags; /* current flags not yet saved in flag_buf */ |
1075 | static uch flag_bit; /* current bit used in flags */ | 1005 | static uch flag_bit; /* current bit used in flags */ |
1076 | 1006 | ||
1077 | /* bits are filled in flags starting at bit 0 (least significant). | 1007 | /* bits are filled in flags starting at bit 0 (least significant). |
1078 | * Note: these flags are overkill in the current code since we don't | 1008 | * Note: these flags are overkill in the current code since we don't |
1079 | * take advantage of DIST_BUFSIZE == LIT_BUFSIZE. | 1009 | * take advantage of DIST_BUFSIZE == LIT_BUFSIZE. |
1080 | */ | 1010 | */ |
1081 | 1011 | ||
1082 | static ulg opt_len; /* bit length of current block with optimal trees */ | 1012 | static ulg opt_len; /* bit length of current block with optimal trees */ |
1083 | static ulg static_len; /* bit length of current block with static trees */ | 1013 | static ulg static_len; /* bit length of current block with static trees */ |
1084 | |||
1085 | static ulg compressed_len; /* total bit length of compressed file */ | ||
1086 | 1014 | ||
1015 | static ulg compressed_len; /* total bit length of compressed file */ | ||
1087 | 1016 | ||
1088 | static ush *file_type; /* pointer to UNKNOWN, BINARY or ASCII */ | 1017 | static ush *file_type; /* pointer to UNKNOWN, BINARY or ASCII */ |
1089 | static int *file_method; /* pointer to DEFLATE or STORE */ | 1018 | static int *file_method; /* pointer to DEFLATE or STORE */ |
1090 | 1019 | ||
1091 | /* =========================================================================== | 1020 | /* =========================================================================== |
1092 | */ | 1021 | */ |
@@ -1135,7 +1064,7 @@ static void init_block(void) | |||
1135 | bl_tree[n].Freq = 0; | 1064 | bl_tree[n].Freq = 0; |
1136 | 1065 | ||
1137 | dyn_ltree[END_BLOCK].Freq = 1; | 1066 | dyn_ltree[END_BLOCK].Freq = 1; |
1138 | opt_len = static_len = 0L; | 1067 | opt_len = static_len = 0; |
1139 | last_lit = last_dist = last_flags = 0; | 1068 | last_lit = last_dist = last_flags = 0; |
1140 | flags = 0; | 1069 | flags = 0; |
1141 | flag_bit = 1; | 1070 | flag_bit = 1; |
@@ -1143,101 +1072,6 @@ static void init_block(void) | |||
1143 | 1072 | ||
1144 | 1073 | ||
1145 | /* =========================================================================== | 1074 | /* =========================================================================== |
1146 | * Allocate the match buffer, initialize the various tables and save the | ||
1147 | * location of the internal file attribute (ascii/binary) and method | ||
1148 | * (DEFLATE/STORE). | ||
1149 | * One callsite in zip() | ||
1150 | */ | ||
1151 | static void ct_init(ush * attr, int *methodp) | ||
1152 | { | ||
1153 | int n; /* iterates over tree elements */ | ||
1154 | int bits; /* bit counter */ | ||
1155 | int length; /* length value */ | ||
1156 | int code; /* code value */ | ||
1157 | int dist; /* distance index */ | ||
1158 | |||
1159 | file_type = attr; | ||
1160 | file_method = methodp; | ||
1161 | compressed_len = 0L; | ||
1162 | |||
1163 | #ifdef NOT_NEEDED | ||
1164 | if (static_dtree[0].Len != 0) | ||
1165 | return; /* ct_init already called */ | ||
1166 | #endif | ||
1167 | |||
1168 | /* Initialize the mapping length (0..255) -> length code (0..28) */ | ||
1169 | length = 0; | ||
1170 | for (code = 0; code < LENGTH_CODES - 1; code++) { | ||
1171 | base_length[code] = length; | ||
1172 | for (n = 0; n < (1 << extra_lbits[code]); n++) { | ||
1173 | length_code[length++] = code; | ||
1174 | } | ||
1175 | } | ||
1176 | Assert(length == 256, "ct_init: length != 256"); | ||
1177 | /* Note that the length 255 (match length 258) can be represented | ||
1178 | * in two different ways: code 284 + 5 bits or code 285, so we | ||
1179 | * overwrite length_code[255] to use the best encoding: | ||
1180 | */ | ||
1181 | length_code[length - 1] = code; | ||
1182 | |||
1183 | /* Initialize the mapping dist (0..32K) -> dist code (0..29) */ | ||
1184 | dist = 0; | ||
1185 | for (code = 0; code < 16; code++) { | ||
1186 | base_dist[code] = dist; | ||
1187 | for (n = 0; n < (1 << extra_dbits[code]); n++) { | ||
1188 | dist_code[dist++] = code; | ||
1189 | } | ||
1190 | } | ||
1191 | Assert(dist == 256, "ct_init: dist != 256"); | ||
1192 | dist >>= 7; /* from now on, all distances are divided by 128 */ | ||
1193 | for (; code < D_CODES; code++) { | ||
1194 | base_dist[code] = dist << 7; | ||
1195 | for (n = 0; n < (1 << (extra_dbits[code] - 7)); n++) { | ||
1196 | dist_code[256 + dist++] = code; | ||
1197 | } | ||
1198 | } | ||
1199 | Assert(dist == 256, "ct_init: 256+dist != 512"); | ||
1200 | |||
1201 | /* Construct the codes of the static literal tree */ | ||
1202 | /* already zeroed - it's in bss | ||
1203 | for (bits = 0; bits <= MAX_BITS; bits++) | ||
1204 | bl_count[bits] = 0; */ | ||
1205 | |||
1206 | n = 0; | ||
1207 | while (n <= 143) { | ||
1208 | static_ltree[n++].Len = 8; | ||
1209 | bl_count[8]++; | ||
1210 | } | ||
1211 | while (n <= 255) { | ||
1212 | static_ltree[n++].Len = 9; | ||
1213 | bl_count[9]++; | ||
1214 | } | ||
1215 | while (n <= 279) { | ||
1216 | static_ltree[n++].Len = 7; | ||
1217 | bl_count[7]++; | ||
1218 | } | ||
1219 | while (n <= 287) { | ||
1220 | static_ltree[n++].Len = 8; | ||
1221 | bl_count[8]++; | ||
1222 | } | ||
1223 | /* Codes 286 and 287 do not exist, but we must include them in the | ||
1224 | * tree construction to get a canonical Huffman tree (longest code | ||
1225 | * all ones) | ||
1226 | */ | ||
1227 | gen_codes((ct_data *) static_ltree, L_CODES + 1); | ||
1228 | |||
1229 | /* The static distance tree is trivial: */ | ||
1230 | for (n = 0; n < D_CODES; n++) { | ||
1231 | static_dtree[n].Len = 5; | ||
1232 | static_dtree[n].Code = bi_reverse(n, 5); | ||
1233 | } | ||
1234 | |||
1235 | /* Initialize the first block of the first file: */ | ||
1236 | init_block(); | ||
1237 | } | ||
1238 | |||
1239 | |||
1240 | /* =========================================================================== | ||
1241 | * Restore the heap property by moving down the tree starting at node k, | 1075 | * Restore the heap property by moving down the tree starting at node k, |
1242 | * exchanging a node with the smallest of its two sons if necessary, stopping | 1076 | * exchanging a node with the smallest of its two sons if necessary, stopping |
1243 | * when the heap property is re-established (each father smaller than its | 1077 | * when the heap property is re-established (each father smaller than its |
@@ -1364,8 +1198,8 @@ static void gen_bitlen(tree_desc * desc) | |||
1364 | continue; | 1198 | continue; |
1365 | if (tree[m].Len != (unsigned) bits) { | 1199 | if (tree[m].Len != (unsigned) bits) { |
1366 | Trace((stderr, "code %d bits %d->%d\n", m, tree[m].Len, bits)); | 1200 | Trace((stderr, "code %d bits %d->%d\n", m, tree[m].Len, bits)); |
1367 | opt_len += ((long) bits - (long) tree[m].Len) * (long) tree[m].Freq; | 1201 | opt_len += ((int32_t) bits - tree[m].Len) * tree[m].Freq; |
1368 | tree[m].Len = (ush) bits; | 1202 | tree[m].Len = bits; |
1369 | } | 1203 | } |
1370 | n--; | 1204 | n--; |
1371 | } | 1205 | } |
@@ -1840,10 +1674,10 @@ static void compress_block(ct_data * ltree, ct_data * dtree) | |||
1840 | */ | 1674 | */ |
1841 | static ulg flush_block(char *buf, ulg stored_len, int eof) | 1675 | static ulg flush_block(char *buf, ulg stored_len, int eof) |
1842 | { | 1676 | { |
1843 | ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */ | 1677 | ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */ |
1844 | int max_blindex; /* index of last bit length code of non zero freq */ | 1678 | int max_blindex; /* index of last bit length code of non zero freq */ |
1845 | 1679 | ||
1846 | flag_buf[last_flags] = flags; /* Save the flags for the last 8 items */ | 1680 | flag_buf[last_flags] = flags; /* Save the flags for the last 8 items */ |
1847 | 1681 | ||
1848 | /* Check if the file is ascii or binary */ | 1682 | /* Check if the file is ascii or binary */ |
1849 | if (*file_type == (ush) UNKNOWN) | 1683 | if (*file_type == (ush) UNKNOWN) |
@@ -1889,7 +1723,7 @@ static ulg flush_block(char *buf, ulg stored_len, int eof) | |||
1889 | compressed_len = stored_len << 3; | 1723 | compressed_len = stored_len << 3; |
1890 | *file_method = STORED; | 1724 | *file_method = STORED; |
1891 | 1725 | ||
1892 | } else if (stored_len + 4 <= opt_lenb && buf != (char *) 0) { | 1726 | } else if (stored_len + 4 <= opt_lenb && buf != NULL) { |
1893 | /* 4: two words for the lengths */ | 1727 | /* 4: two words for the lengths */ |
1894 | /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE. | 1728 | /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE. |
1895 | * Otherwise we can't have processed more than WSIZE input bytes since | 1729 | * Otherwise we can't have processed more than WSIZE input bytes since |
@@ -1929,6 +1763,15 @@ static ulg flush_block(char *buf, ulg stored_len, int eof) | |||
1929 | 1763 | ||
1930 | 1764 | ||
1931 | /* =========================================================================== | 1765 | /* =========================================================================== |
1766 | * Update a hash value with the given input byte | ||
1767 | * IN assertion: all calls to to UPDATE_HASH are made with consecutive | ||
1768 | * input characters, so that a running hash key can be computed from the | ||
1769 | * previous key instead of complete recalculation each time. | ||
1770 | */ | ||
1771 | #define UPDATE_HASH(h, c) (h = (((h)<<H_SHIFT) ^ (c)) & HASH_MASK) | ||
1772 | |||
1773 | |||
1774 | /* =========================================================================== | ||
1932 | * Same as above, but achieves better compression. We use a lazy | 1775 | * Same as above, but achieves better compression. We use a lazy |
1933 | * evaluation for matches: a match is finally adopted only if there is | 1776 | * evaluation for matches: a match is finally adopted only if there is |
1934 | * no better match at the next window position. | 1777 | * no better match at the next window position. |
@@ -1945,7 +1788,7 @@ static ulg flush_block(char *buf, ulg stored_len, int eof) | |||
1945 | block_start >= 0L \ | 1788 | block_start >= 0L \ |
1946 | ? (char*)&window[(unsigned)block_start] \ | 1789 | ? (char*)&window[(unsigned)block_start] \ |
1947 | : (char*)NULL, \ | 1790 | : (char*)NULL, \ |
1948 | (long)strstart - block_start, \ | 1791 | (ulg)strstart - block_start, \ |
1949 | (eof) \ | 1792 | (eof) \ |
1950 | ) | 1793 | ) |
1951 | 1794 | ||
@@ -2068,10 +1911,166 @@ static ulg deflate(void) | |||
2068 | 1911 | ||
2069 | 1912 | ||
2070 | /* =========================================================================== | 1913 | /* =========================================================================== |
1914 | * Initialize the bit string routines. | ||
1915 | */ | ||
1916 | static void bi_init(int zipfile) | ||
1917 | { | ||
1918 | zfile = zipfile; | ||
1919 | bi_buf = 0; | ||
1920 | bi_valid = 0; | ||
1921 | #ifdef DEBUG | ||
1922 | bits_sent = 0L; | ||
1923 | #endif | ||
1924 | } | ||
1925 | |||
1926 | |||
1927 | /* =========================================================================== | ||
1928 | * Initialize the "longest match" routines for a new file | ||
1929 | */ | ||
1930 | static void lm_init(ush * flags) | ||
1931 | { | ||
1932 | unsigned j; | ||
1933 | |||
1934 | /* Initialize the hash table. */ | ||
1935 | memset(head, 0, HASH_SIZE * sizeof(*head)); | ||
1936 | /* prev will be initialized on the fly */ | ||
1937 | |||
1938 | /*speed options for the general purpose bit flag */ | ||
1939 | *flags |= 2; /* FAST 4, SLOW 2 */ | ||
1940 | /* ??? reduce max_chain_length for binary files */ | ||
1941 | |||
1942 | strstart = 0; | ||
1943 | block_start = 0L; | ||
1944 | |||
1945 | lookahead = file_read(window, | ||
1946 | sizeof(int) <= 2 ? (unsigned) WSIZE : 2 * WSIZE); | ||
1947 | |||
1948 | if (lookahead == 0 || lookahead == (unsigned) -1) { | ||
1949 | eofile = 1; | ||
1950 | lookahead = 0; | ||
1951 | return; | ||
1952 | } | ||
1953 | eofile = 0; | ||
1954 | /* Make sure that we always have enough lookahead. This is important | ||
1955 | * if input comes from a device such as a tty. | ||
1956 | */ | ||
1957 | while (lookahead < MIN_LOOKAHEAD && !eofile) | ||
1958 | fill_window(); | ||
1959 | |||
1960 | ins_h = 0; | ||
1961 | for (j = 0; j < MIN_MATCH - 1; j++) | ||
1962 | UPDATE_HASH(ins_h, window[j]); | ||
1963 | /* If lookahead < MIN_MATCH, ins_h is garbage, but this is | ||
1964 | * not important since only literal bytes will be emitted. | ||
1965 | */ | ||
1966 | } | ||
1967 | |||
1968 | |||
1969 | /* =========================================================================== | ||
1970 | * Allocate the match buffer, initialize the various tables and save the | ||
1971 | * location of the internal file attribute (ascii/binary) and method | ||
1972 | * (DEFLATE/STORE). | ||
1973 | * One callsite in zip() | ||
1974 | */ | ||
1975 | static void ct_init(ush * attr, int *methodp) | ||
1976 | { | ||
1977 | int n; /* iterates over tree elements */ | ||
1978 | int bits; /* bit counter */ | ||
1979 | int length; /* length value */ | ||
1980 | int code; /* code value */ | ||
1981 | int dist; /* distance index */ | ||
1982 | |||
1983 | file_type = attr; | ||
1984 | file_method = methodp; | ||
1985 | compressed_len = 0L; | ||
1986 | |||
1987 | #ifdef NOT_NEEDED | ||
1988 | if (static_dtree[0].Len != 0) | ||
1989 | return; /* ct_init already called */ | ||
1990 | #endif | ||
1991 | |||
1992 | /* Initialize the mapping length (0..255) -> length code (0..28) */ | ||
1993 | length = 0; | ||
1994 | for (code = 0; code < LENGTH_CODES - 1; code++) { | ||
1995 | base_length[code] = length; | ||
1996 | for (n = 0; n < (1 << extra_lbits[code]); n++) { | ||
1997 | length_code[length++] = code; | ||
1998 | } | ||
1999 | } | ||
2000 | Assert(length == 256, "ct_init: length != 256"); | ||
2001 | /* Note that the length 255 (match length 258) can be represented | ||
2002 | * in two different ways: code 284 + 5 bits or code 285, so we | ||
2003 | * overwrite length_code[255] to use the best encoding: | ||
2004 | */ | ||
2005 | length_code[length - 1] = code; | ||
2006 | |||
2007 | /* Initialize the mapping dist (0..32K) -> dist code (0..29) */ | ||
2008 | dist = 0; | ||
2009 | for (code = 0; code < 16; code++) { | ||
2010 | base_dist[code] = dist; | ||
2011 | for (n = 0; n < (1 << extra_dbits[code]); n++) { | ||
2012 | dist_code[dist++] = code; | ||
2013 | } | ||
2014 | } | ||
2015 | Assert(dist == 256, "ct_init: dist != 256"); | ||
2016 | dist >>= 7; /* from now on, all distances are divided by 128 */ | ||
2017 | for (; code < D_CODES; code++) { | ||
2018 | base_dist[code] = dist << 7; | ||
2019 | for (n = 0; n < (1 << (extra_dbits[code] - 7)); n++) { | ||
2020 | dist_code[256 + dist++] = code; | ||
2021 | } | ||
2022 | } | ||
2023 | Assert(dist == 256, "ct_init: 256+dist != 512"); | ||
2024 | |||
2025 | /* Construct the codes of the static literal tree */ | ||
2026 | /* already zeroed - it's in bss | ||
2027 | for (bits = 0; bits <= MAX_BITS; bits++) | ||
2028 | bl_count[bits] = 0; */ | ||
2029 | |||
2030 | n = 0; | ||
2031 | while (n <= 143) { | ||
2032 | static_ltree[n++].Len = 8; | ||
2033 | bl_count[8]++; | ||
2034 | } | ||
2035 | while (n <= 255) { | ||
2036 | static_ltree[n++].Len = 9; | ||
2037 | bl_count[9]++; | ||
2038 | } | ||
2039 | while (n <= 279) { | ||
2040 | static_ltree[n++].Len = 7; | ||
2041 | bl_count[7]++; | ||
2042 | } | ||
2043 | while (n <= 287) { | ||
2044 | static_ltree[n++].Len = 8; | ||
2045 | bl_count[8]++; | ||
2046 | } | ||
2047 | /* Codes 286 and 287 do not exist, but we must include them in the | ||
2048 | * tree construction to get a canonical Huffman tree (longest code | ||
2049 | * all ones) | ||
2050 | */ | ||
2051 | gen_codes((ct_data *) static_ltree, L_CODES + 1); | ||
2052 | |||
2053 | /* The static distance tree is trivial: */ | ||
2054 | for (n = 0; n < D_CODES; n++) { | ||
2055 | static_dtree[n].Len = 5; | ||
2056 | static_dtree[n].Code = bi_reverse(n, 5); | ||
2057 | } | ||
2058 | |||
2059 | /* Initialize the first block of the first file: */ | ||
2060 | init_block(); | ||
2061 | } | ||
2062 | |||
2063 | |||
2064 | /* =========================================================================== | ||
2071 | * Deflate in to out. | 2065 | * Deflate in to out. |
2072 | * IN assertions: the input and output buffers are cleared. | 2066 | * IN assertions: the input and output buffers are cleared. |
2073 | * The variables time_stamp and save_orig_name are initialized. | 2067 | * The variables time_stamp and save_orig_name are initialized. |
2074 | */ | 2068 | */ |
2069 | |||
2070 | /* put_header_byte is used for the compressed output | ||
2071 | * - for the initial 4 bytes that can't overflow the buffer. */ | ||
2072 | #define put_header_byte(c) outbuf[outcnt++] = (c) | ||
2073 | |||
2075 | static int zip(int in, int out) | 2074 | static int zip(int in, int out) |
2076 | { | 2075 | { |
2077 | uch my_flags = 0; /* general purpose bit flags */ | 2076 | uch my_flags = 0; /* general purpose bit flags */ |
@@ -2085,12 +2084,10 @@ static int zip(int in, int out) | |||
2085 | /* Write the header to the gzip file. See algorithm.doc for the format */ | 2084 | /* Write the header to the gzip file. See algorithm.doc for the format */ |
2086 | 2085 | ||
2087 | method = DEFLATED; | 2086 | method = DEFLATED; |
2088 | put_header_byte(0x1f); /* magic header for gzip files, 1F 8B */ | 2087 | put_header_byte(0x1f); /* magic header for gzip files, 1F 8B */ |
2089 | put_header_byte(0x8b); | 2088 | put_header_byte(0x8b); |
2090 | 2089 | put_header_byte(DEFLATED); /* compression method */ | |
2091 | put_header_byte(DEFLATED); /* compression method */ | 2090 | put_header_byte(my_flags); /* general flags */ |
2092 | |||
2093 | put_header_byte(my_flags); /* general flags */ | ||
2094 | put_32bit(time_stamp); | 2091 | put_32bit(time_stamp); |
2095 | 2092 | ||
2096 | /* Write deflated file to zip file */ | 2093 | /* Write deflated file to zip file */ |