diff options
| author | vda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2007-01-07 19:45:51 +0000 |
|---|---|---|
| committer | vda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2007-01-07 19:45:51 +0000 |
| commit | b84f54ce5c79fdb83bef55a67e351ef329c26256 (patch) | |
| tree | 12966e0e26335cc9108961662ea4fd949cfefb4f | |
| parent | 477c00d500ac927405fcc948f147d3ed75eb2826 (diff) | |
| download | busybox-w32-b84f54ce5c79fdb83bef55a67e351ef329c26256.tar.gz busybox-w32-b84f54ce5c79fdb83bef55a67e351ef329c26256.tar.bz2 busybox-w32-b84f54ce5c79fdb83bef55a67e351ef329c26256.zip | |
gzip cleanup part #13 - the last for today I think
git-svn-id: svn://busybox.net/trunk/busybox@17195 69ca8d6d-28ef-0310-b511-8ec308f3f277
| -rw-r--r-- | archival/gzip.c | 255 |
1 files changed, 111 insertions, 144 deletions
diff --git a/archival/gzip.c b/archival/gzip.c index 632dd4a14..bfe762128 100644 --- a/archival/gzip.c +++ b/archival/gzip.c | |||
| @@ -5,9 +5,9 @@ | |||
| 5 | * Based on GNU gzip Copyright (C) 1992-1993 Jean-loup Gailly. | 5 | * Based on GNU gzip Copyright (C) 1992-1993 Jean-loup Gailly. |
| 6 | * | 6 | * |
| 7 | * Originally adjusted for busybox by Charles P. Wright <cpw@unix.asb.com> | 7 | * Originally adjusted for busybox by Charles P. Wright <cpw@unix.asb.com> |
| 8 | * "this is a stripped down version of gzip I put into busybox, it does | 8 | * "this is a stripped down version of gzip I put into busybox, it does |
| 9 | * only standard in to standard out with -9 compression. It also requires | 9 | * only standard in to standard out with -9 compression. It also requires |
| 10 | * the zcat module for some important functions." | 10 | * the zcat module for some important functions." |
| 11 | * | 11 | * |
| 12 | * Adjusted further by Erik Andersen <andersen@codepoet.org> to support | 12 | * Adjusted further by Erik Andersen <andersen@codepoet.org> to support |
| 13 | * files as well as stdin/stdout, and to generally behave itself wrt | 13 | * files as well as stdin/stdout, and to generally behave itself wrt |
| @@ -27,13 +27,13 @@ | |||
| 27 | * 00000200 b dist_code | 27 | * 00000200 b dist_code |
| 28 | * 0000023d b depth | 28 | * 0000023d b depth |
| 29 | * 00000400 b flag_buf | 29 | * 00000400 b flag_buf |
| 30 | * 0000047a b heap | ||
| 30 | * 00000480 b static_ltree | 31 | * 00000480 b static_ltree |
| 31 | * 000008f4 b dyn_ltree | 32 | * 000008f4 b dyn_ltree |
| 32 | * 000008f4 b heap | ||
| 33 | */ | 33 | */ |
| 34 | 34 | ||
| 35 | /* TODO: full support for -v for DESKTOP | 35 | /* TODO: full support for -v for DESKTOP |
| 36 | /usr/bin/gzip -v a bogus aa | 36 | * "/usr/bin/gzip -v a bogus aa" should say: |
| 37 | a: 85.1% -- replaced with a.gz | 37 | a: 85.1% -- replaced with a.gz |
| 38 | gzip: bogus: No such file or directory | 38 | gzip: bogus: No such file or directory |
| 39 | aa: 85.1% -- replaced with aa.gz | 39 | aa: 85.1% -- replaced with aa.gz |
| @@ -81,9 +81,6 @@ aa: 85.1% -- replaced with aa.gz | |||
| 81 | # endif | 81 | # endif |
| 82 | #endif | 82 | #endif |
| 83 | 83 | ||
| 84 | //remove?? | ||
| 85 | #define INBUF_EXTRA 64 /* required by unlzw() */ | ||
| 86 | |||
| 87 | #ifndef OUTBUFSIZ | 84 | #ifndef OUTBUFSIZ |
| 88 | # ifdef SMALL_MEM | 85 | # ifdef SMALL_MEM |
| 89 | # define OUTBUFSIZ 8192 /* output buffer size */ | 86 | # define OUTBUFSIZ 8192 /* output buffer size */ |
| @@ -91,8 +88,6 @@ aa: 85.1% -- replaced with aa.gz | |||
| 91 | # define OUTBUFSIZ 16384 /* output buffer size */ | 88 | # define OUTBUFSIZ 16384 /* output buffer size */ |
| 92 | # endif | 89 | # endif |
| 93 | #endif | 90 | #endif |
| 94 | //remove?? | ||
| 95 | #define OUTBUF_EXTRA 2048 /* required by unlzw() */ | ||
| 96 | 91 | ||
| 97 | #ifndef DIST_BUFSIZE | 92 | #ifndef DIST_BUFSIZE |
| 98 | # ifdef SMALL_MEM | 93 | # ifdef SMALL_MEM |
| @@ -198,11 +193,10 @@ aa: 85.1% -- replaced with aa.gz | |||
| 198 | typedef uint8_t uch; | 193 | typedef uint8_t uch; |
| 199 | typedef uint16_t ush; | 194 | typedef uint16_t ush; |
| 200 | typedef uint32_t ulg; | 195 | typedef uint32_t ulg; |
| 201 | typedef uint32_t lng; | 196 | typedef int32_t lng; |
| 202 | 197 | ||
| 203 | 198 | ||
| 204 | /* =========================================================================== | 199 | /* =========================================================================== |
| 205 | * Local data used by the "longest match" routines. | ||
| 206 | */ | 200 | */ |
| 207 | typedef ush Pos; | 201 | typedef ush Pos; |
| 208 | typedef unsigned IPos; | 202 | typedef unsigned IPos; |
| @@ -211,18 +205,6 @@ typedef unsigned IPos; | |||
| 211 | * save space in the various tables. IPos is used only for parameter passing. | 205 | * save space in the various tables. IPos is used only for parameter passing. |
| 212 | */ | 206 | */ |
| 213 | 207 | ||
| 214 | #define DECLARE(type, array, size) \ | ||
| 215 | static type * array | ||
| 216 | #define ALLOC(type, array, size) { \ | ||
| 217 | array = xzalloc((size_t)(((size)+1L)/2) * 2*sizeof(type)); \ | ||
| 218 | } | ||
| 219 | |||
| 220 | #define FREE(array) \ | ||
| 221 | { \ | ||
| 222 | free(array); \ | ||
| 223 | array = NULL; \ | ||
| 224 | } | ||
| 225 | |||
| 226 | static lng block_start; | 208 | static lng block_start; |
| 227 | 209 | ||
| 228 | /* window position at the beginning of the current output block. Gets | 210 | /* window position at the beginning of the current output block. Gets |
| @@ -231,7 +213,7 @@ static lng block_start; | |||
| 231 | 213 | ||
| 232 | static unsigned ins_h; /* hash index of string to be inserted */ | 214 | static unsigned ins_h; /* hash index of string to be inserted */ |
| 233 | 215 | ||
| 234 | #define H_SHIFT ((HASH_BITS+MIN_MATCH-1)/MIN_MATCH) | 216 | #define H_SHIFT ((HASH_BITS+MIN_MATCH-1) / MIN_MATCH) |
| 235 | /* Number of bits by which ins_h and del_h must be shifted at each | 217 | /* Number of bits by which ins_h and del_h must be shifted at each |
| 236 | * input step. It must be such that after MIN_MATCH steps, the oldest | 218 | * input step. It must be such that after MIN_MATCH steps, the oldest |
| 237 | * byte no longer takes part in the hash key, that is: | 219 | * byte no longer takes part in the hash key, that is: |
| @@ -281,7 +263,7 @@ enum { | |||
| 281 | * found for specific files. | 263 | * found for specific files. |
| 282 | */ | 264 | */ |
| 283 | 265 | ||
| 284 | nice_match = 258 /* Stop searching when current match exceeds this */ | 266 | nice_match = 258, /* Stop searching when current match exceeds this */ |
| 285 | /* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4 | 267 | /* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4 |
| 286 | * For deflate_fast() (levels <= 3) good is ignored and lazy has a different | 268 | * For deflate_fast() (levels <= 3) good is ignored and lazy has a different |
| 287 | * meaning. | 269 | * meaning. |
| @@ -289,31 +271,31 @@ enum { | |||
| 289 | }; | 271 | }; |
| 290 | 272 | ||
| 291 | 273 | ||
| 274 | /* =========================================================================== | ||
| 275 | */ | ||
| 276 | #define DECLARE(type, array, size) \ | ||
| 277 | static type * array | ||
| 278 | |||
| 279 | #define ALLOC(type, array, size) \ | ||
| 280 | { \ | ||
| 281 | array = xzalloc((size_t)(((size)+1L)/2) * 2*sizeof(type)); \ | ||
| 282 | } | ||
| 283 | |||
| 284 | #define FREE(array) \ | ||
| 285 | { \ | ||
| 286 | free(array); \ | ||
| 287 | array = NULL; \ | ||
| 288 | } | ||
| 289 | |||
| 292 | /* global buffers */ | 290 | /* global buffers */ |
| 293 | 291 | ||
| 294 | /* To save memory for 16 bit systems, some arrays are overlaid between | 292 | /* buffer for literals or lengths */ |
| 295 | * the various modules: | 293 | /* DECLARE(uch, l_buf, LIT_BUFSIZE); */ |
| 296 | * deflate: prev+head window d_buf l_buf outbuf | 294 | DECLARE(uch, l_buf, INBUFSIZ); |
| 297 | * unlzw: tab_prefix tab_suffix stack inbuf outbuf | ||
| 298 | //remove unlzw?? | ||
| 299 | * For compression, input is done in window[]. For decompression, output | ||
| 300 | * is done in window except for unlzw. | ||
| 301 | */ | ||
| 302 | /* To save space (see unlzw.c), we overlay prev+head with tab_prefix and | ||
| 303 | * window with tab_suffix. Check that we can do this: | ||
| 304 | */ | ||
| 305 | #if (WSIZE<<1) > (1<<BITS) | ||
| 306 | # error cannot overlay window with tab_suffix and prev with tab_prefix0 | ||
| 307 | #endif | ||
| 308 | #if HASH_BITS > BITS-1 | ||
| 309 | # error cannot overlay head with tab_prefix1 | ||
| 310 | #endif | ||
| 311 | 295 | ||
| 312 | //#define tab_suffix window | 296 | DECLARE(ush, d_buf, DIST_BUFSIZE); |
| 313 | //#define tab_prefix prev /* hash link (see deflate.c) */ | 297 | DECLARE(uch, outbuf, OUTBUFSIZ); |
| 314 | #define head (prev+WSIZE) /* hash head (see deflate.c) */ | ||
| 315 | 298 | ||
| 316 | /* DECLARE(uch, window, 2L*WSIZE); */ | ||
| 317 | /* Sliding window. Input bytes are read into the second half of the window, | 299 | /* Sliding window. Input bytes are read into the second half of the window, |
| 318 | * and move to the first half later to keep a dictionary of at least WSIZE | 300 | * and move to the first half later to keep a dictionary of at least WSIZE |
| 319 | * bytes. With this organization, matches are limited to a distance of | 301 | * bytes. With this organization, matches are limited to a distance of |
| @@ -323,21 +305,19 @@ enum { | |||
| 323 | * To do: limit the window size to WSIZE+BSZ if SMALL_MEM (the code would | 305 | * To do: limit the window size to WSIZE+BSZ if SMALL_MEM (the code would |
| 324 | * be less efficient). | 306 | * be less efficient). |
| 325 | */ | 307 | */ |
| 308 | DECLARE(uch, window, 2L * WSIZE); | ||
| 326 | 309 | ||
| 327 | /* DECLARE(Pos, prev, WSIZE); */ | ||
| 328 | /* Link to older string with same hash index. To limit the size of this | 310 | /* Link to older string with same hash index. To limit the size of this |
| 329 | * array to 64K, this link is maintained only for the last 32K strings. | 311 | * array to 64K, this link is maintained only for the last 32K strings. |
| 330 | * An index in this array is thus a window index modulo 32K. | 312 | * An index in this array is thus a window index modulo 32K. |
| 331 | */ | 313 | */ |
| 314 | /* DECLARE(Pos, prev, WSIZE); */ | ||
| 315 | DECLARE(ush, prev, 1L << BITS); | ||
| 332 | 316 | ||
| 333 | /* DECLARE(Pos, head, 1<<HASH_BITS); */ | ||
| 334 | /* Heads of the hash chains or 0. */ | 317 | /* Heads of the hash chains or 0. */ |
| 318 | /* DECLARE(Pos, head, 1<<HASH_BITS); */ | ||
| 319 | #define head (prev+WSIZE) /* hash head (see deflate.c) */ | ||
| 335 | 320 | ||
| 336 | DECLARE(uch, inbuf, INBUFSIZ + INBUF_EXTRA); //remove + XX_EXTRA (unlzw)?? | ||
| 337 | DECLARE(uch, outbuf, OUTBUFSIZ + OUTBUF_EXTRA); | ||
| 338 | DECLARE(ush, d_buf, DIST_BUFSIZE); | ||
| 339 | DECLARE(uch, window, 2L * WSIZE); | ||
| 340 | DECLARE(ush, prev, 1L << BITS); | ||
| 341 | 321 | ||
| 342 | /* number of input bytes */ | 322 | /* number of input bytes */ |
| 343 | static ulg isize; /* only 32 bits stored in .gz file */ | 323 | static ulg isize; /* only 32 bits stored in .gz file */ |
| @@ -348,12 +328,11 @@ static int exit_code; /* program exit code */ | |||
| 348 | 328 | ||
| 349 | /* original time stamp (modification time) */ | 329 | /* original time stamp (modification time) */ |
| 350 | static ulg time_stamp; /* only 32 bits stored in .gz file */ | 330 | static ulg time_stamp; /* only 32 bits stored in .gz file */ |
| 351 | ////static char z_suffix[MAX_SUFFIX + 1]; /* default suffix (can be set with --suffix) */ | ||
| 352 | 331 | ||
| 353 | static int ifd; /* input file descriptor */ | 332 | static int ifd; /* input file descriptor */ |
| 354 | static int ofd; /* output file descriptor */ | 333 | static int ofd; /* output file descriptor */ |
| 355 | #ifdef DEBUG | 334 | #ifdef DEBUG |
| 356 | static unsigned insize; /* valid bytes in inbuf */ | 335 | static unsigned insize; /* valid bytes in l_buf */ |
| 357 | #endif | 336 | #endif |
| 358 | static unsigned outcnt; /* bytes in output buffer */ | 337 | static unsigned outcnt; /* bytes in output buffer */ |
| 359 | 338 | ||
| @@ -364,7 +343,7 @@ static uint32_t *crc_32_tab; | |||
| 364 | * Local data used by the "bit string" routines. | 343 | * Local data used by the "bit string" routines. |
| 365 | */ | 344 | */ |
| 366 | 345 | ||
| 367 | static int zfile; /* output gzip file */ | 346 | //// static int zfile; /* output gzip file */ |
| 368 | 347 | ||
| 369 | static unsigned short bi_buf; | 348 | static unsigned short bi_buf; |
| 370 | 349 | ||
| @@ -468,7 +447,7 @@ static unsigned file_read(void *buf, unsigned size) | |||
| 468 | { | 447 | { |
| 469 | unsigned len; | 448 | unsigned len; |
| 470 | 449 | ||
| 471 | Assert(insize == 0, "inbuf not empty"); | 450 | Assert(insize == 0, "l_buf not empty"); |
| 472 | 451 | ||
| 473 | len = safe_read(ifd, buf, size); | 452 | len = safe_read(ifd, buf, size); |
| 474 | if (len == (unsigned)(-1) || len == 0) | 453 | if (len == (unsigned)(-1) || len == 0) |
| @@ -867,9 +846,6 @@ static const extra_bits_t extra_blbits[BL_CODES] = { | |||
| 867 | * memory at the expense of compression). Some optimizations would be possible | 846 | * memory at the expense of compression). Some optimizations would be possible |
| 868 | * if we rely on DIST_BUFSIZE == LIT_BUFSIZE. | 847 | * if we rely on DIST_BUFSIZE == LIT_BUFSIZE. |
| 869 | */ | 848 | */ |
| 870 | #if LIT_BUFSIZE > INBUFSIZ | ||
| 871 | #error cannot overlay l_buf and inbuf | ||
| 872 | #endif | ||
| 873 | #define REP_3_6 16 | 849 | #define REP_3_6 16 |
| 874 | /* repeat previous bit length 3-6 times (2 bits of repeat count) */ | 850 | /* repeat previous bit length 3-6 times (2 bits of repeat count) */ |
| 875 | #define REPZ_3_10 17 | 851 | #define REPZ_3_10 17 |
| @@ -896,9 +872,19 @@ typedef struct ct_data { | |||
| 896 | #define Dad dl.dad | 872 | #define Dad dl.dad |
| 897 | #define Len dl.len | 873 | #define Len dl.len |
| 898 | 874 | ||
| 899 | #define HEAP_SIZE (2*L_CODES+1) | 875 | #define HEAP_SIZE (2*L_CODES + 1) |
| 900 | /* maximum heap size */ | 876 | /* maximum heap size */ |
| 901 | 877 | ||
| 878 | ////static int heap[HEAP_SIZE]; /* heap used to build the Huffman trees */ | ||
| 879 | ////let's try this | ||
| 880 | static ush heap[HEAP_SIZE]; /* heap used to build the Huffman trees */ | ||
| 881 | static int heap_len; /* number of elements in the heap */ | ||
| 882 | static int heap_max; /* element of largest frequency */ | ||
| 883 | |||
| 884 | /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used. | ||
| 885 | * The same heap array is used to build all trees. | ||
| 886 | */ | ||
| 887 | |||
| 902 | static ct_data dyn_ltree[HEAP_SIZE]; /* literal and length tree */ | 888 | static ct_data dyn_ltree[HEAP_SIZE]; /* literal and length tree */ |
| 903 | static ct_data dyn_dtree[2 * D_CODES + 1]; /* distance tree */ | 889 | static ct_data dyn_dtree[2 * D_CODES + 1]; /* distance tree */ |
| 904 | 890 | ||
| @@ -956,14 +942,6 @@ static const uch bl_order[BL_CODES] = { | |||
| 956 | * probability, to avoid transmitting the lengths for unused bit length codes. | 942 | * probability, to avoid transmitting the lengths for unused bit length codes. |
| 957 | */ | 943 | */ |
| 958 | 944 | ||
| 959 | static int heap[2 * L_CODES + 1]; /* heap used to build the Huffman trees */ | ||
| 960 | static int heap_len; /* number of elements in the heap */ | ||
| 961 | static int heap_max; /* element of largest frequency */ | ||
| 962 | |||
| 963 | /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used. | ||
| 964 | * The same heap array is used to build all trees. | ||
| 965 | */ | ||
| 966 | |||
| 967 | static uch depth[2 * L_CODES + 1]; | 945 | static uch depth[2 * L_CODES + 1]; |
| 968 | 946 | ||
| 969 | /* Depth of each subtree used as tie breaker for trees of equal frequency */ | 947 | /* Depth of each subtree used as tie breaker for trees of equal frequency */ |
| @@ -987,11 +965,6 @@ static int base_dist[D_CODES]; | |||
| 987 | 965 | ||
| 988 | /* First normalized distance for each code (0 = distance of 1) */ | 966 | /* First normalized distance for each code (0 = distance of 1) */ |
| 989 | 967 | ||
| 990 | #define l_buf inbuf | ||
| 991 | /* DECLARE(uch, l_buf, LIT_BUFSIZE); buffer for literals or lengths */ | ||
| 992 | |||
| 993 | /* DECLARE(ush, d_buf, DIST_BUFSIZE); buffer for distances */ | ||
| 994 | |||
| 995 | static uch flag_buf[LIT_BUFSIZE / 8]; | 968 | static uch flag_buf[LIT_BUFSIZE / 8]; |
| 996 | 969 | ||
| 997 | /* flag_buf is a bit array distinguishing literals from lengths in | 970 | /* flag_buf is a bit array distinguishing literals from lengths in |
| @@ -1484,7 +1457,7 @@ static int build_bl_tree(void) | |||
| 1484 | scan_tree((ct_data *) dyn_dtree, d_desc.max_code); | 1457 | scan_tree((ct_data *) dyn_dtree, d_desc.max_code); |
| 1485 | 1458 | ||
| 1486 | /* Build the bit length tree: */ | 1459 | /* Build the bit length tree: */ |
| 1487 | build_tree((tree_desc *) (&bl_desc)); | 1460 | build_tree((tree_desc *) &bl_desc); |
| 1488 | /* opt_len now includes the length of the tree representations, except | 1461 | /* opt_len now includes the length of the tree representations, except |
| 1489 | * the lengths of the bit lengths codes and the 5+5+4 bits for the counts. | 1462 | * the lengths of the bit lengths codes and the 5+5+4 bits for the counts. |
| 1490 | */ | 1463 | */ |
| @@ -1621,47 +1594,45 @@ static int ct_tally(int dist, int lc) | |||
| 1621 | */ | 1594 | */ |
| 1622 | static void compress_block(ct_data * ltree, ct_data * dtree) | 1595 | static void compress_block(ct_data * ltree, ct_data * dtree) |
| 1623 | { | 1596 | { |
| 1624 | unsigned dist; /* distance of matched string */ | 1597 | unsigned dist; /* distance of matched string */ |
| 1625 | int lc; /* match length or unmatched char (if dist == 0) */ | 1598 | int lc; /* match length or unmatched char (if dist == 0) */ |
| 1626 | unsigned lx = 0; /* running index in l_buf */ | 1599 | unsigned lx = 0; /* running index in l_buf */ |
| 1627 | unsigned dx = 0; /* running index in d_buf */ | 1600 | unsigned dx = 0; /* running index in d_buf */ |
| 1628 | unsigned fx = 0; /* running index in flag_buf */ | 1601 | unsigned fx = 0; /* running index in flag_buf */ |
| 1629 | uch flag = 0; /* current flags */ | 1602 | uch flag = 0; /* current flags */ |
| 1630 | unsigned code; /* the code to send */ | 1603 | unsigned code; /* the code to send */ |
| 1631 | int extra; /* number of extra bits to send */ | 1604 | int extra; /* number of extra bits to send */ |
| 1632 | 1605 | ||
| 1633 | if (last_lit != 0) { | 1606 | if (last_lit != 0) do { |
| 1634 | do { | 1607 | if ((lx & 7) == 0) |
| 1635 | if ((lx & 7) == 0) | 1608 | flag = flag_buf[fx++]; |
| 1636 | flag = flag_buf[fx++]; | 1609 | lc = l_buf[lx++]; |
| 1637 | lc = l_buf[lx++]; | 1610 | if ((flag & 1) == 0) { |
| 1638 | if ((flag & 1) == 0) { | 1611 | SEND_CODE(lc, ltree); /* send a literal byte */ |
| 1639 | SEND_CODE(lc, ltree); /* send a literal byte */ | 1612 | Tracecv(isgraph(lc), (stderr, " '%c' ", lc)); |
| 1640 | Tracecv(isgraph(lc), (stderr, " '%c' ", lc)); | 1613 | } else { |
| 1641 | } else { | 1614 | /* Here, lc is the match length - MIN_MATCH */ |
| 1642 | /* Here, lc is the match length - MIN_MATCH */ | 1615 | code = length_code[lc]; |
| 1643 | code = length_code[lc]; | 1616 | SEND_CODE(code + LITERALS + 1, ltree); /* send the length code */ |
| 1644 | SEND_CODE(code + LITERALS + 1, ltree); /* send the length code */ | 1617 | extra = extra_lbits[code]; |
| 1645 | extra = extra_lbits[code]; | 1618 | if (extra != 0) { |
| 1646 | if (extra != 0) { | 1619 | lc -= base_length[code]; |
| 1647 | lc -= base_length[code]; | 1620 | send_bits(lc, extra); /* send the extra length bits */ |
| 1648 | send_bits(lc, extra); /* send the extra length bits */ | 1621 | } |
| 1649 | } | 1622 | dist = d_buf[dx++]; |
| 1650 | dist = d_buf[dx++]; | 1623 | /* Here, dist is the match distance - 1 */ |
| 1651 | /* Here, dist is the match distance - 1 */ | 1624 | code = D_CODE(dist); |
| 1652 | code = D_CODE(dist); | 1625 | Assert(code < D_CODES, "bad d_code"); |
| 1653 | Assert(code < D_CODES, "bad d_code"); | 1626 | |
| 1654 | 1627 | SEND_CODE(code, dtree); /* send the distance code */ | |
| 1655 | SEND_CODE(code, dtree); /* send the distance code */ | 1628 | extra = extra_dbits[code]; |
| 1656 | extra = extra_dbits[code]; | 1629 | if (extra != 0) { |
| 1657 | if (extra != 0) { | 1630 | dist -= base_dist[code]; |
| 1658 | dist -= base_dist[code]; | 1631 | send_bits(dist, extra); /* send the extra distance bits */ |
| 1659 | send_bits(dist, extra); /* send the extra distance bits */ | 1632 | } |
| 1660 | } | 1633 | } /* literal or match pair ? */ |
| 1661 | } /* literal or match pair ? */ | 1634 | flag >>= 1; |
| 1662 | flag >>= 1; | 1635 | } while (lx < last_lit); |
| 1663 | } while (lx < last_lit); | ||
| 1664 | } | ||
| 1665 | 1636 | ||
| 1666 | SEND_CODE(END_BLOCK, ltree); | 1637 | SEND_CODE(END_BLOCK, ltree); |
| 1667 | } | 1638 | } |
| @@ -1684,10 +1655,10 @@ static ulg flush_block(char *buf, ulg stored_len, int eof) | |||
| 1684 | set_file_type(); | 1655 | set_file_type(); |
| 1685 | 1656 | ||
| 1686 | /* Construct the literal and distance trees */ | 1657 | /* Construct the literal and distance trees */ |
| 1687 | build_tree((tree_desc *) (&l_desc)); | 1658 | build_tree((tree_desc *) &l_desc); |
| 1688 | Tracev((stderr, "\nlit data: dyn %ld, stat %ld", opt_len, static_len)); | 1659 | Tracev((stderr, "\nlit data: dyn %ld, stat %ld", opt_len, static_len)); |
| 1689 | 1660 | ||
| 1690 | build_tree((tree_desc *) (&d_desc)); | 1661 | build_tree((tree_desc *) &d_desc); |
| 1691 | Tracev((stderr, "\ndist data: dyn %ld, stat %ld", opt_len, static_len)); | 1662 | Tracev((stderr, "\ndist data: dyn %ld, stat %ld", opt_len, static_len)); |
| 1692 | /* At this point, opt_len and static_len are the total bit lengths of | 1663 | /* At this point, opt_len and static_len are the total bit lengths of |
| 1693 | * the compressed block data, excluding the tree representations. | 1664 | * the compressed block data, excluding the tree representations. |
| @@ -1913,9 +1884,9 @@ static ulg deflate(void) | |||
| 1913 | /* =========================================================================== | 1884 | /* =========================================================================== |
| 1914 | * Initialize the bit string routines. | 1885 | * Initialize the bit string routines. |
| 1915 | */ | 1886 | */ |
| 1916 | static void bi_init(int zipfile) | 1887 | static void bi_init(void) //// int zipfile) |
| 1917 | { | 1888 | { |
| 1918 | zfile = zipfile; | 1889 | //// zfile = zipfile; |
| 1919 | bi_buf = 0; | 1890 | bi_buf = 0; |
| 1920 | bi_valid = 0; | 1891 | bi_valid = 0; |
| 1921 | #ifdef DEBUG | 1892 | #ifdef DEBUG |
| @@ -1927,7 +1898,7 @@ static void bi_init(int zipfile) | |||
| 1927 | /* =========================================================================== | 1898 | /* =========================================================================== |
| 1928 | * Initialize the "longest match" routines for a new file | 1899 | * Initialize the "longest match" routines for a new file |
| 1929 | */ | 1900 | */ |
| 1930 | static void lm_init(ush * flags) | 1901 | static void lm_init(ush * flagsp) |
| 1931 | { | 1902 | { |
| 1932 | unsigned j; | 1903 | unsigned j; |
| 1933 | 1904 | ||
| @@ -1935,8 +1906,8 @@ static void lm_init(ush * flags) | |||
| 1935 | memset(head, 0, HASH_SIZE * sizeof(*head)); | 1906 | memset(head, 0, HASH_SIZE * sizeof(*head)); |
| 1936 | /* prev will be initialized on the fly */ | 1907 | /* prev will be initialized on the fly */ |
| 1937 | 1908 | ||
| 1938 | /*speed options for the general purpose bit flag */ | 1909 | /* speed options for the general purpose bit flag */ |
| 1939 | *flags |= 2; /* FAST 4, SLOW 2 */ | 1910 | *flagsp |= 2; /* FAST 4, SLOW 2 */ |
| 1940 | /* ??? reduce max_chain_length for binary files */ | 1911 | /* ??? reduce max_chain_length for binary files */ |
| 1941 | 1912 | ||
| 1942 | strstart = 0; | 1913 | strstart = 0; |
| @@ -1975,7 +1946,6 @@ static void lm_init(ush * flags) | |||
| 1975 | static void ct_init(ush * attr, int *methodp) | 1946 | static void ct_init(ush * attr, int *methodp) |
| 1976 | { | 1947 | { |
| 1977 | int n; /* iterates over tree elements */ | 1948 | int n; /* iterates over tree elements */ |
| 1978 | int bits; /* bit counter */ | ||
| 1979 | int length; /* length value */ | 1949 | int length; /* length value */ |
| 1980 | int code; /* code value */ | 1950 | int code; /* code value */ |
| 1981 | int dist; /* distance index */ | 1951 | int dist; /* distance index */ |
| @@ -2024,8 +1994,8 @@ static void ct_init(ush * attr, int *methodp) | |||
| 2024 | 1994 | ||
| 2025 | /* Construct the codes of the static literal tree */ | 1995 | /* Construct the codes of the static literal tree */ |
| 2026 | /* already zeroed - it's in bss | 1996 | /* already zeroed - it's in bss |
| 2027 | for (bits = 0; bits <= MAX_BITS; bits++) | 1997 | for (n = 0; n <= MAX_BITS; n++) |
| 2028 | bl_count[bits] = 0; */ | 1998 | bl_count[n] = 0; */ |
| 2029 | 1999 | ||
| 2030 | n = 0; | 2000 | n = 0; |
| 2031 | while (n <= 143) { | 2001 | while (n <= 143) { |
| @@ -2071,11 +2041,11 @@ static void ct_init(ush * attr, int *methodp) | |||
| 2071 | * - for the initial 4 bytes that can't overflow the buffer. */ | 2041 | * - for the initial 4 bytes that can't overflow the buffer. */ |
| 2072 | #define put_header_byte(c) outbuf[outcnt++] = (c) | 2042 | #define put_header_byte(c) outbuf[outcnt++] = (c) |
| 2073 | 2043 | ||
| 2074 | static int zip(int in, int out) | 2044 | static void zip(int in, int out) |
| 2075 | { | 2045 | { |
| 2076 | uch my_flags = 0; /* general purpose bit flags */ | 2046 | uch my_flags = 0; /* general purpose bit flags */ |
| 2077 | ush attr = 0; /* ascii/binary flag */ | 2047 | ush attr = 0; /* ascii/binary flag */ |
| 2078 | ush deflate_flags = 0; /* pkzip -es, -en or -ex equivalent */ | 2048 | ush deflate_flags = 0; /* pkzip -es, -en or -ex equivalent */ |
| 2079 | 2049 | ||
| 2080 | ifd = in; | 2050 | ifd = in; |
| 2081 | ofd = out; | 2051 | ofd = out; |
| @@ -2093,7 +2063,7 @@ static int zip(int in, int out) | |||
| 2093 | /* Write deflated file to zip file */ | 2063 | /* Write deflated file to zip file */ |
| 2094 | crc = ~0; | 2064 | crc = ~0; |
| 2095 | 2065 | ||
| 2096 | bi_init(out); | 2066 | bi_init(); //// (out); |
| 2097 | ct_init(&attr, &method); | 2067 | ct_init(&attr, &method); |
| 2098 | lm_init(&deflate_flags); | 2068 | lm_init(&deflate_flags); |
| 2099 | 2069 | ||
| @@ -2107,7 +2077,6 @@ static int zip(int in, int out) | |||
| 2107 | put_32bit(isize); | 2077 | put_32bit(isize); |
| 2108 | 2078 | ||
| 2109 | flush_outbuf(); | 2079 | flush_outbuf(); |
| 2110 | return 0; | ||
| 2111 | } | 2080 | } |
| 2112 | 2081 | ||
| 2113 | 2082 | ||
| @@ -2125,12 +2094,10 @@ int gzip_main(int argc, char **argv) | |||
| 2125 | }; | 2094 | }; |
| 2126 | 2095 | ||
| 2127 | unsigned opt; | 2096 | unsigned opt; |
| 2128 | int result; | ||
| 2129 | int inFileNum; | 2097 | int inFileNum; |
| 2130 | int outFileNum; | 2098 | int outFileNum; |
| 2131 | int i; | 2099 | int i; |
| 2132 | struct stat statBuf; | 2100 | struct stat statBuf; |
| 2133 | char *delFileName; | ||
| 2134 | 2101 | ||
| 2135 | opt = getopt32(argc, argv, "cf123456789qv" USE_GUNZIP("d")); | 2102 | opt = getopt32(argc, argv, "cf123456789qv" USE_GUNZIP("d")); |
| 2136 | //if (opt & 0x1) // -c | 2103 | //if (opt & 0x1) // -c |
| @@ -2170,11 +2137,9 @@ int gzip_main(int argc, char **argv) | |||
| 2170 | } | 2137 | } |
| 2171 | #endif | 2138 | #endif |
| 2172 | 2139 | ||
| 2173 | //// strncpy(z_suffix, ".gz", sizeof(z_suffix) - 1); | ||
| 2174 | |||
| 2175 | /* Allocate all global buffers (for DYN_ALLOC option) */ | 2140 | /* Allocate all global buffers (for DYN_ALLOC option) */ |
| 2176 | ALLOC(uch, inbuf, INBUFSIZ + INBUF_EXTRA); | 2141 | ALLOC(uch, l_buf, INBUFSIZ); |
| 2177 | ALLOC(uch, outbuf, OUTBUFSIZ + OUTBUF_EXTRA); | 2142 | ALLOC(uch, outbuf, OUTBUFSIZ); |
| 2178 | ALLOC(ush, d_buf, DIST_BUFSIZE); | 2143 | ALLOC(ush, d_buf, DIST_BUFSIZE); |
| 2179 | ALLOC(uch, window, 2L * WSIZE); | 2144 | ALLOC(uch, window, 2L * WSIZE); |
| 2180 | ALLOC(ush, prev, 1L << BITS); | 2145 | ALLOC(ush, prev, 1L << BITS); |
| @@ -2232,18 +2197,20 @@ int gzip_main(int argc, char **argv) | |||
| 2232 | continue; | 2197 | continue; |
| 2233 | } | 2198 | } |
| 2234 | 2199 | ||
| 2235 | result = zip(inFileNum, outFileNum); | 2200 | zip(inFileNum, outFileNum); |
| 2236 | 2201 | ||
| 2237 | if (path != NULL) { | 2202 | if (path != NULL) { |
| 2203 | char *delFileName; | ||
| 2204 | |||
| 2238 | close(inFileNum); | 2205 | close(inFileNum); |
| 2239 | close(outFileNum); | 2206 | close(outFileNum); |
| 2240 | 2207 | ||
| 2241 | /* Delete the original file */ | 2208 | /* Delete the original file */ |
| 2242 | if (result == 0) | 2209 | // Pity we don't propagate zip failures to this place... |
| 2210 | //if (zip_is_ok) | ||
| 2243 | delFileName = argv[i]; | 2211 | delFileName = argv[i]; |
| 2244 | else | 2212 | //else |
| 2245 | delFileName = path; | 2213 | // delFileName = path; |
| 2246 | |||
| 2247 | if (unlink(delFileName) < 0) | 2214 | if (unlink(delFileName) < 0) |
| 2248 | bb_perror_msg("%s", delFileName); | 2215 | bb_perror_msg("%s", delFileName); |
| 2249 | } | 2216 | } |
