diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-01-07 19:38:42 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-01-07 19:38:42 +0000 |
commit | f824136f6b21558ac023bc53bcb69294e0676103 (patch) | |
tree | f168544e67c3c8c5dae57c5a58ffa7e8b16e92f3 /archival/gzip.c | |
parent | ed0f6db35e02ccd0d8c22337639463a05e3efea1 (diff) | |
download | busybox-w32-f824136f6b21558ac023bc53bcb69294e0676103.tar.gz busybox-w32-f824136f6b21558ac023bc53bcb69294e0676103.tar.bz2 busybox-w32-f824136f6b21558ac023bc53bcb69294e0676103.zip |
gzip cleanup part #4
Diffstat (limited to 'archival/gzip.c')
-rw-r--r-- | archival/gzip.c | 74 |
1 files changed, 39 insertions, 35 deletions
diff --git a/archival/gzip.c b/archival/gzip.c index dcbcae0b3..4f47a2782 100644 --- a/archival/gzip.c +++ b/archival/gzip.c | |||
@@ -61,12 +61,6 @@ aa: 85.1% -- replaced with aa.gz | |||
61 | # endif | 61 | # endif |
62 | #endif | 62 | #endif |
63 | 63 | ||
64 | #define PACK_MAGIC "\037\036" /* Magic header for packed files */ | ||
65 | #define GZIP_MAGIC "\037\213" /* Magic header for gzip files, 1F 8B */ | ||
66 | #define OLD_GZIP_MAGIC "\037\236" /* Magic header for gzip 0.5 = freeze 1.x */ | ||
67 | #define LZH_MAGIC "\037\240" /* Magic header for SCO LZH Compress files */ | ||
68 | #define PKZIP_MAGIC "\120\113\003\004" /* Magic header for pkzip files */ | ||
69 | |||
70 | /* gzip flag byte */ | 64 | /* gzip flag byte */ |
71 | #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */ | 65 | #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */ |
72 | #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */ | 66 | #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */ |
@@ -142,9 +136,9 @@ aa: 85.1% -- replaced with aa.gz | |||
142 | # define Assert(cond,msg) {if(!(cond)) bb_error_msg(msg);} | 136 | # define Assert(cond,msg) {if(!(cond)) bb_error_msg(msg);} |
143 | # define Trace(x) fprintf x | 137 | # define Trace(x) fprintf x |
144 | # define Tracev(x) {if (verbose) fprintf x ;} | 138 | # define Tracev(x) {if (verbose) fprintf x ;} |
145 | # define Tracevv(x) {if (verbose>1) fprintf x ;} | 139 | # define Tracevv(x) {if (verbose > 1) fprintf x ;} |
146 | # define Tracec(c,x) {if (verbose && (c)) fprintf x ;} | 140 | # define Tracec(c,x) {if (verbose && (c)) fprintf x ;} |
147 | # define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;} | 141 | # define Tracecv(c,x) {if (verbose > 1 && (c)) fprintf x ;} |
148 | #else | 142 | #else |
149 | # define Assert(cond,msg) | 143 | # define Assert(cond,msg) |
150 | # define Trace(x) | 144 | # define Trace(x) |
@@ -313,7 +307,7 @@ static void clear_bufs(void) | |||
313 | * pointer, then initialize the crc shift register contents instead. | 307 | * pointer, then initialize the crc shift register contents instead. |
314 | * Return the current crc in either case. | 308 | * Return the current crc in either case. |
315 | */ | 309 | */ |
316 | static uint32_t crc = ~0; /* shift register contents */ | 310 | static uint32_t crc; /* shift register contents */ |
317 | static uint32_t updcrc(uch * s, unsigned n) | 311 | static uint32_t updcrc(uch * s, unsigned n) |
318 | { | 312 | { |
319 | uint32_t c; /* temporary variable */ | 313 | uint32_t c; /* temporary variable */ |
@@ -1414,17 +1408,18 @@ static void set_file_type(void); | |||
1414 | 1408 | ||
1415 | 1409 | ||
1416 | #ifndef DEBUG | 1410 | #ifndef DEBUG |
1411 | /* Send a code of the given tree. c and tree must not have side effects */ | ||
1417 | # define send_code(c, tree) send_bits(tree[c].Code, tree[c].Len) | 1412 | # define send_code(c, tree) send_bits(tree[c].Code, tree[c].Len) |
1418 | /* Send a code of the given tree. c and tree must not have side effects */ | ||
1419 | |||
1420 | #else /* DEBUG */ | 1413 | #else /* DEBUG */ |
1421 | # define send_code(c, tree) \ | 1414 | # define send_code(c, tree) \ |
1422 | { if (verbose>1) bb_error_msg("\ncd %3d ",(c)); \ | 1415 | { \ |
1423 | send_bits(tree[c].Code, tree[c].Len); } | 1416 | if (verbose > 1) bb_error_msg("\ncd %3d ",(c)); \ |
1417 | send_bits(tree[c].Code, tree[c].Len); \ | ||
1418 | } | ||
1424 | #endif | 1419 | #endif |
1425 | 1420 | ||
1426 | #define d_code(dist) \ | 1421 | #define d_code(dist) \ |
1427 | ((dist) < 256 ? dist_code[dist] : dist_code[256+((dist)>>7)]) | 1422 | ((dist) < 256 ? dist_code[dist] : dist_code[256 + ((dist)>>7)]) |
1428 | /* Mapping from a distance to a distance code. dist is the distance - 1 and | 1423 | /* Mapping from a distance to a distance code. dist is the distance - 1 and |
1429 | * must not have side effects. dist_code[256] and dist_code[257] are never | 1424 | * must not have side effects. dist_code[256] and dist_code[257] are never |
1430 | * used. | 1425 | * used. |
@@ -1432,6 +1427,7 @@ static void set_file_type(void); | |||
1432 | 1427 | ||
1433 | /* the arguments must not have side effects */ | 1428 | /* the arguments must not have side effects */ |
1434 | 1429 | ||
1430 | |||
1435 | /* =========================================================================== | 1431 | /* =========================================================================== |
1436 | * Allocate the match buffer, initialize the various tables and save the | 1432 | * Allocate the match buffer, initialize the various tables and save the |
1437 | * location of the internal file attribute (ascii/binary) and method | 1433 | * location of the internal file attribute (ascii/binary) and method |
@@ -1513,12 +1509,13 @@ static void ct_init(ush * attr, int *methodp) | |||
1513 | init_block(); | 1509 | init_block(); |
1514 | } | 1510 | } |
1515 | 1511 | ||
1512 | |||
1516 | /* =========================================================================== | 1513 | /* =========================================================================== |
1517 | * Initialize a new block. | 1514 | * Initialize a new block. |
1518 | */ | 1515 | */ |
1519 | static void init_block(void) | 1516 | static void init_block(void) |
1520 | { | 1517 | { |
1521 | int n; /* iterates over tree elements */ | 1518 | int n; /* iterates over tree elements */ |
1522 | 1519 | ||
1523 | /* Initialize the trees. */ | 1520 | /* Initialize the trees. */ |
1524 | for (n = 0; n < L_CODES; n++) | 1521 | for (n = 0; n < L_CODES; n++) |
@@ -1535,28 +1532,22 @@ static void init_block(void) | |||
1535 | flag_bit = 1; | 1532 | flag_bit = 1; |
1536 | } | 1533 | } |
1537 | 1534 | ||
1538 | #define SMALLEST 1 | ||
1539 | /* Index within the heap array of least frequent node in the Huffman tree */ | ||
1540 | |||
1541 | 1535 | ||
1542 | /* =========================================================================== | 1536 | /* =========================================================================== |
1543 | * Remove the smallest element from the heap and recreate the heap with | 1537 | * Remove the smallest element from the heap and recreate the heap with |
1544 | * one less element. Updates heap and heap_len. | 1538 | * one less element. Updates heap and heap_len. |
1545 | */ | 1539 | */ |
1540 | |||
1541 | #define SMALLEST 1 | ||
1542 | /* Index within the heap array of least frequent node in the Huffman tree */ | ||
1543 | |||
1546 | #define pqremove(tree, top) \ | 1544 | #define pqremove(tree, top) \ |
1547 | {\ | 1545 | { \ |
1548 | top = heap[SMALLEST]; \ | 1546 | top = heap[SMALLEST]; \ |
1549 | heap[SMALLEST] = heap[heap_len--]; \ | 1547 | heap[SMALLEST] = heap[heap_len--]; \ |
1550 | pqdownheap(tree, SMALLEST); \ | 1548 | pqdownheap(tree, SMALLEST); \ |
1551 | } | 1549 | } |
1552 | 1550 | ||
1553 | /* =========================================================================== | ||
1554 | * Compares to subtrees, using the tree depth as tie breaker when | ||
1555 | * the subtrees have equal frequency. This minimizes the worst case length. | ||
1556 | */ | ||
1557 | #define smaller(tree, n, m) \ | ||
1558 | (tree[n].Freq < tree[m].Freq || \ | ||
1559 | (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m])) | ||
1560 | 1551 | ||
1561 | /* =========================================================================== | 1552 | /* =========================================================================== |
1562 | * Restore the heap property by moving down the tree starting at node k, | 1553 | * Restore the heap property by moving down the tree starting at node k, |
@@ -1564,6 +1555,14 @@ static void init_block(void) | |||
1564 | * when the heap property is re-established (each father smaller than its | 1555 | * when the heap property is re-established (each father smaller than its |
1565 | * two sons). | 1556 | * two sons). |
1566 | */ | 1557 | */ |
1558 | |||
1559 | /* Compares to subtrees, using the tree depth as tie breaker when | ||
1560 | * the subtrees have equal frequency. This minimizes the worst case length. | ||
1561 | */ | ||
1562 | #define smaller(tree, n, m) \ | ||
1563 | (tree[n].Freq < tree[m].Freq \ | ||
1564 | || (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m])) | ||
1565 | |||
1567 | static void pqdownheap(ct_data * tree, int k) | 1566 | static void pqdownheap(ct_data * tree, int k) |
1568 | { | 1567 | { |
1569 | int v = heap[k]; | 1568 | int v = heap[k]; |
@@ -1588,6 +1587,7 @@ static void pqdownheap(ct_data * tree, int k) | |||
1588 | heap[k] = v; | 1587 | heap[k] = v; |
1589 | } | 1588 | } |
1590 | 1589 | ||
1590 | |||
1591 | /* =========================================================================== | 1591 | /* =========================================================================== |
1592 | * Compute the optimal bit lengths for a tree and update the total bit length | 1592 | * Compute the optimal bit lengths for a tree and update the total bit length |
1593 | * for the current block. | 1593 | * for the current block. |
@@ -1624,8 +1624,10 @@ static void gen_bitlen(tree_desc * desc) | |||
1624 | for (h = heap_max + 1; h < HEAP_SIZE; h++) { | 1624 | for (h = heap_max + 1; h < HEAP_SIZE; h++) { |
1625 | n = heap[h]; | 1625 | n = heap[h]; |
1626 | bits = tree[tree[n].Dad].Len + 1; | 1626 | bits = tree[tree[n].Dad].Len + 1; |
1627 | if (bits > max_length) | 1627 | if (bits > max_length) { |
1628 | bits = max_length, overflow++; | 1628 | bits = max_length; |
1629 | overflow++; | ||
1630 | } | ||
1629 | tree[n].Len = (ush) bits; | 1631 | tree[n].Len = (ush) bits; |
1630 | /* We overwrite tree[n].Dad which is no longer needed */ | 1632 | /* We overwrite tree[n].Dad which is no longer needed */ |
1631 | 1633 | ||
@@ -2151,7 +2153,7 @@ static void compress_block(ct_data * ltree, ct_data * dtree) | |||
2151 | unsigned code; /* the code to send */ | 2153 | unsigned code; /* the code to send */ |
2152 | int extra; /* number of extra bits to send */ | 2154 | int extra; /* number of extra bits to send */ |
2153 | 2155 | ||
2154 | if (last_lit != 0) | 2156 | if (last_lit != 0) { |
2155 | do { | 2157 | do { |
2156 | if ((lx & 7) == 0) | 2158 | if ((lx & 7) == 0) |
2157 | flag = flag_buf[fx++]; | 2159 | flag = flag_buf[fx++]; |
@@ -2182,6 +2184,7 @@ static void compress_block(ct_data * ltree, ct_data * dtree) | |||
2182 | } /* literal or match pair ? */ | 2184 | } /* literal or match pair ? */ |
2183 | flag >>= 1; | 2185 | flag >>= 1; |
2184 | } while (lx < last_lit); | 2186 | } while (lx < last_lit); |
2187 | } | ||
2185 | 2188 | ||
2186 | send_code(END_BLOCK, ltree); | 2189 | send_code(END_BLOCK, ltree); |
2187 | } | 2190 | } |
@@ -2204,7 +2207,7 @@ static void set_file_type(void) | |||
2204 | ascii_freq += dyn_ltree[n++].Freq; | 2207 | ascii_freq += dyn_ltree[n++].Freq; |
2205 | while (n < LITERALS) | 2208 | while (n < LITERALS) |
2206 | bin_freq += dyn_ltree[n++].Freq; | 2209 | bin_freq += dyn_ltree[n++].Freq; |
2207 | *file_type = bin_freq > (ascii_freq >> 2) ? BINARY : ASCII; | 2210 | *file_type = (bin_freq > (ascii_freq >> 2)) ? BINARY : ASCII; |
2208 | if (*file_type == BINARY && translate_eol) { | 2211 | if (*file_type == BINARY && translate_eol) { |
2209 | bb_error_msg("-l used on binary file"); | 2212 | bb_error_msg("-l used on binary file"); |
2210 | } | 2213 | } |
@@ -2228,8 +2231,9 @@ static int zip(int in, int out) | |||
2228 | /* Write the header to the gzip file. See algorithm.doc for the format */ | 2231 | /* Write the header to the gzip file. See algorithm.doc for the format */ |
2229 | 2232 | ||
2230 | method = DEFLATED; | 2233 | method = DEFLATED; |
2231 | put_header_byte(GZIP_MAGIC[0]); /* magic header */ | 2234 | put_header_byte(0x1f); /* magic header for gzip files, 1F 8B */ |
2232 | put_header_byte(GZIP_MAGIC[1]); | 2235 | put_header_byte(0x8b); |
2236 | |||
2233 | put_header_byte(DEFLATED); /* compression method */ | 2237 | put_header_byte(DEFLATED); /* compression method */ |
2234 | 2238 | ||
2235 | put_header_byte(my_flags); /* general flags */ | 2239 | put_header_byte(my_flags); /* general flags */ |