aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277>2007-03-14 00:08:28 +0000
committervda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277>2007-03-14 00:08:28 +0000
commitb414ca57b18913fbe60c85a1b2bc6d91e2b0cf97 (patch)
tree694f03c8320b5cf9eb5145186ab236fd402a403d
parent6e95b2cd2e0a9cbe10bbe3aad5803b099799e0f4 (diff)
downloadbusybox-w32-b414ca57b18913fbe60c85a1b2bc6d91e2b0cf97.tar.gz
busybox-w32-b414ca57b18913fbe60c85a1b2bc6d91e2b0cf97.tar.bz2
busybox-w32-b414ca57b18913fbe60c85a1b2bc6d91e2b0cf97.zip
gzip: bbunzip integration fixes
git-svn-id: svn://busybox.net/trunk/busybox@18094 69ca8d6d-28ef-0310-b511-8ec308f3f277
-rw-r--r--archival/gzip.c191
-rw-r--r--include/libbb.h4
2 files changed, 23 insertions, 172 deletions
diff --git a/archival/gzip.c b/archival/gzip.c
index 90075272d..9411e17a9 100644
--- a/archival/gzip.c
+++ b/archival/gzip.c
@@ -67,12 +67,6 @@ aa: 85.1% -- replaced with aa.gz
67 */ 67 */
68#define SMALL_MEM 68#define SMALL_MEM
69 69
70//// /* Compression methods (see algorithm.doc) */
71//// /* Only STORED and DEFLATED are supported by this BusyBox module */
72//// #define STORED 0
73//// /* methods 4 to 7 reserved */
74//// #define DEFLATED 8
75
76#ifndef INBUFSIZ 70#ifndef INBUFSIZ
77# ifdef SMALL_MEM 71# ifdef SMALL_MEM
78# define INBUFSIZ 0x2000 /* input buffer size */ 72# define INBUFSIZ 0x2000 /* input buffer size */
@@ -307,25 +301,19 @@ struct global1 {
307 /* DECLARE(Pos, head, 1<<HASH_BITS); */ 301 /* DECLARE(Pos, head, 1<<HASH_BITS); */
308#define head (G1.prev + WSIZE) /* hash head (see deflate.c) */ 302#define head (G1.prev + WSIZE) /* hash head (see deflate.c) */
309 303
310
311/* number of input bytes */ 304/* number of input bytes */
312 ulg isize; /* only 32 bits stored in .gz file */ 305 ulg isize; /* only 32 bits stored in .gz file */
313 306
314//// int method = DEFLATED; /* compression method */ 307/* bbox always use stdin/stdout */
315//## int exit_code; /* program exit code */ 308#define ifd STDIN_FILENO /* input file descriptor */
316 309#define ofd STDOUT_FILENO /* output file descriptor */
317/* original time stamp (modification time) */
318 ulg time_stamp; /* only 32 bits stored in .gz file */
319 310
320//TODO: get rid of this
321 int ifd; /* input file descriptor */
322 int ofd; /* output file descriptor */
323#ifdef DEBUG 311#ifdef DEBUG
324 unsigned insize; /* valid bytes in l_buf */ 312 unsigned insize; /* valid bytes in l_buf */
325#endif 313#endif
326 unsigned outcnt; /* bytes in output buffer */ 314 unsigned outcnt; /* bytes in output buffer */
327 315
328 smallint eofile; /* flag set at end of input file */ 316 smallint eofile; /* flag set at end of input file */
329 317
330/* =========================================================================== 318/* ===========================================================================
331 * Local data used by the "bit string" routines. 319 * Local data used by the "bit string" routines.
@@ -368,7 +356,7 @@ static void flush_outbuf(void)
368 if (G1.outcnt == 0) 356 if (G1.outcnt == 0)
369 return; 357 return;
370 358
371 xwrite(G1.ofd, (char *) G1.outbuf, G1.outcnt); 359 xwrite(ofd, (char *) G1.outbuf, G1.outcnt);
372 G1.outcnt = 0; 360 G1.outcnt = 0;
373} 361}
374 362
@@ -441,7 +429,7 @@ static unsigned file_read(void *buf, unsigned size)
441 429
442 Assert(G1.insize == 0, "l_buf not empty"); 430 Assert(G1.insize == 0, "l_buf not empty");
443 431
444 len = safe_read(G1.ifd, buf, size); 432 len = safe_read(ifd, buf, size);
445 if (len == (unsigned)(-1) || len == 0) 433 if (len == (unsigned)(-1) || len == 0)
446 return len; 434 return len;
447 435
@@ -743,10 +731,10 @@ static void check_match(IPos start, IPos match, int length)
743 * Addison-Wesley, 1983. ISBN 0-201-06672-6. 731 * Addison-Wesley, 1983. ISBN 0-201-06672-6.
744 * 732 *
745 * INTERFACE 733 * INTERFACE
746 * void ct_init() //// ush *attr, int *methodp) 734 * void ct_init()
747 * Allocate the match buffer, initialize the various tables and save 735 * Allocate the match buffer, initialize the various tables [and save
748 * the location of the internal file attribute (ascii/binary) and 736 * the location of the internal file attribute (ascii/binary) and
749 * method (DEFLATE/STORE) 737 * method (DEFLATE/STORE) -- deleted in bbox]
750 * 738 *
751 * void ct_tally(int dist, int lc); 739 * void ct_tally(int dist, int lc);
752 * Save the match info and tally the frequency counts. 740 * Save the match info and tally the frequency counts.
@@ -966,10 +954,6 @@ struct global2 {
966 ulg static_len; /* bit length of current block with static trees */ 954 ulg static_len; /* bit length of current block with static trees */
967 955
968 ulg compressed_len; /* total bit length of compressed file */ 956 ulg compressed_len; /* total bit length of compressed file */
969
970//// ush *file_type; /* pointer to UNKNOWN, BINARY or ASCII */
971//// int *file_method; /* pointer to DEFLATE or STORE */
972
973}; 957};
974 958
975#define G2ptr ((struct global2*)(ptr_to_globals)) 959#define G2ptr ((struct global2*)(ptr_to_globals))
@@ -1495,31 +1479,6 @@ static void send_all_trees(int lcodes, int dcodes, int blcodes)
1495} 1479}
1496 1480
1497 1481
1498/////* ===========================================================================
1499//// * Set the file type to ASCII or BINARY, using a crude approximation:
1500//// * binary if more than 20% of the bytes are <= 6 or >= 128, ascii otherwise.
1501//// * IN assertion: the fields freq of dyn_ltree are set and the total of all
1502//// * frequencies does not exceed 64K (to fit in an int on 16 bit machines).
1503//// */
1504////static void set_file_type(void)
1505////{
1506//// int n = 0;
1507//// unsigned ascii_freq = 0;
1508//// unsigned bin_freq = 0;
1509////
1510//// while (n < 7)
1511//// bin_freq += G2.dyn_ltree[n++].Freq;
1512//// while (n < 128)
1513//// ascii_freq += G2.dyn_ltree[n++].Freq;
1514//// while (n < LITERALS)
1515//// bin_freq += G2.dyn_ltree[n++].Freq;
1516//// *G2.file_type = (bin_freq > (ascii_freq >> 2)) ? BINARY : ASCII;
1517//// if (*G2.file_type == BINARY && translate_eol) {
1518//// bb_error_msg("-l used on binary file");
1519//// }
1520////}
1521
1522
1523/* =========================================================================== 1482/* ===========================================================================
1524 * Save the match info and tally the frequency counts. Return true if 1483 * Save the match info and tally the frequency counts. Return true if
1525 * the current block must be flushed. 1484 * the current block must be flushed.
@@ -1638,10 +1597,6 @@ static ulg flush_block(char *buf, ulg stored_len, int eof)
1638 1597
1639 G2.flag_buf[G2.last_flags] = G2.flags; /* Save the flags for the last 8 items */ 1598 G2.flag_buf[G2.last_flags] = G2.flags; /* Save the flags for the last 8 items */
1640 1599
1641//// /* Check if the file is ascii or binary */
1642//// if (*G2.file_type == (ush) UNKNOWN)
1643//// set_file_type();
1644
1645 /* Construct the literal and distance trees */ 1600 /* Construct the literal and distance trees */
1646 build_tree(&G2.l_desc); 1601 build_tree(&G2.l_desc);
1647 Tracev((stderr, "\nlit data: dyn %ld, stat %ld", G2.opt_len, G2.static_len)); 1602 Tracev((stderr, "\nlit data: dyn %ld, stat %ld", G2.opt_len, G2.static_len));
@@ -1680,7 +1635,6 @@ static ulg flush_block(char *buf, ulg stored_len, int eof)
1680 1635
1681 copy_block(buf, (unsigned) stored_len, 0); /* without header */ 1636 copy_block(buf, (unsigned) stored_len, 0); /* without header */
1682 G2.compressed_len = stored_len << 3; 1637 G2.compressed_len = stored_len << 3;
1683//// *file_method = STORED;
1684 1638
1685 } else if (stored_len + 4 <= opt_lenb && buf != NULL) { 1639 } else if (stored_len + 4 <= opt_lenb && buf != NULL) {
1686 /* 4: two words for the lengths */ 1640 /* 4: two words for the lengths */
@@ -1931,15 +1885,13 @@ static void lm_init(ush * flagsp)
1931 * (DEFLATE/STORE). 1885 * (DEFLATE/STORE).
1932 * One callsite in zip() 1886 * One callsite in zip()
1933 */ 1887 */
1934static void ct_init(void) ////ush * attr, int *methodp) 1888static void ct_init(void)
1935{ 1889{
1936 int n; /* iterates over tree elements */ 1890 int n; /* iterates over tree elements */
1937 int length; /* length value */ 1891 int length; /* length value */
1938 int code; /* code value */ 1892 int code; /* code value */
1939 int dist; /* distance index */ 1893 int dist; /* distance index */
1940 1894
1941//// file_type = attr;
1942//// file_method = methodp;
1943 G2.compressed_len = 0L; 1895 G2.compressed_len = 0L;
1944 1896
1945#ifdef NOT_NEEDED 1897#ifdef NOT_NEEDED
@@ -2022,42 +1974,26 @@ static void ct_init(void) ////ush * attr, int *methodp)
2022/* =========================================================================== 1974/* ===========================================================================
2023 * Deflate in to out. 1975 * Deflate in to out.
2024 * IN assertions: the input and output buffers are cleared. 1976 * IN assertions: the input and output buffers are cleared.
2025 * The variables time_stamp and save_orig_name are initialized.
2026 */ 1977 */
2027 1978
2028/* put_header_byte is used for the compressed output 1979static void zip(ulg time_stamp)
2029 * - for the initial 4 bytes that can't overflow the buffer. */
2030#define put_header_byte(c) G1.outbuf[G1.outcnt++] = (c)
2031
2032static void zip(int in, int out)
2033{ 1980{
2034//// uch my_flags = 0; /* general purpose bit flags */
2035//// ush attr = 0; /* ascii/binary flag */
2036 ush deflate_flags = 0; /* pkzip -es, -en or -ex equivalent */ 1981 ush deflate_flags = 0; /* pkzip -es, -en or -ex equivalent */
2037//// int method = DEFLATED; /* compression method */
2038 1982
2039 G1.ifd = in;
2040 G1.ofd = out;
2041 G1.outcnt = 0; 1983 G1.outcnt = 0;
2042 1984
2043 /* Write the header to the gzip file. See algorithm.doc for the format */ 1985 /* Write the header to the gzip file. See algorithm.doc for the format */
2044
2045 //put_header_byte(0x1f); /* magic header for gzip files, 1F 8B */
2046 //put_header_byte(0x8b);
2047 //////put_header_byte(DEFLATED); /* compression method */
2048 //put_header_byte(8); /* compression method */
2049 //put_header_byte(0); /* general flags */
2050 /* magic header for gzip files: 1F 8B */ 1986 /* magic header for gzip files: 1F 8B */
2051 /* compression method: 8 */ 1987 /* compression method: 8 (DEFLATED) */
2052 /* general flags: 0 */ 1988 /* general flags: 0 */
2053 put_32bit(0x00088b1f); 1989 put_32bit(0x00088b1f);
2054 put_32bit(G1.time_stamp); 1990 put_32bit(time_stamp);
2055 1991
2056 /* Write deflated file to zip file */ 1992 /* Write deflated file to zip file */
2057 G1.crc = ~0; 1993 G1.crc = ~0;
2058 1994
2059 bi_init(); 1995 bi_init();
2060 ct_init(); //// &attr, &method); 1996 ct_init();
2061 lm_init(&deflate_flags); 1997 lm_init(&deflate_flags);
2062 1998
2063 put_8bit(deflate_flags); /* extra flags */ 1999 put_8bit(deflate_flags); /* extra flags */
@@ -2074,94 +2010,6 @@ static void zip(int in, int out)
2074 2010
2075 2011
2076/* ======================================================================== */ 2012/* ======================================================================== */
2077#if 0
2078static void abort_gzip(int ATTRIBUTE_UNUSED ignored)
2079{
2080 exit(1);
2081}
2082
2083int gzip_main(int argc, char **argv);
2084int gzip_main(int argc, char **argv)
2085{
2086
2087 if (optind == argc) {
2088 G1.time_stamp = 0;
2089 zip(STDIN_FILENO, STDOUT_FILENO);
2090 return 0; //## G1.exit_code;
2091 }
2092
2093 for (i = optind; i < argc; i++) {
2094 char *path = NULL;
2095
2096 clear_bufs();
2097 if (LONE_DASH(argv[i])) {
2098 G1.time_stamp = 0;
2099 inFileNum = STDIN_FILENO;
2100 outFileNum = STDOUT_FILENO;
2101 } else {
2102 inFileNum = xopen(argv[i], O_RDONLY);
2103 if (fstat(inFileNum, &statBuf) < 0)
2104 bb_perror_msg_and_die("%s", argv[i]);
2105 G1.time_stamp = statBuf.st_ctime;
2106
2107 if (!(opt & OPT_tostdout)) {
2108 path = xasprintf("%s.gz", argv[i]);
2109
2110 /* Open output file */
2111#if defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1 && defined(O_NOFOLLOW)
2112 outFileNum = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW);
2113#else
2114 outFileNum = open(path, O_RDWR | O_CREAT | O_EXCL);
2115#endif
2116 if (outFileNum < 0) {
2117 bb_perror_msg("%s", path);
2118 free(path);
2119 continue;
2120 }
2121
2122 /* Set permissions on the file */
2123 fchmod(outFileNum, statBuf.st_mode);
2124 } else
2125 outFileNum = STDOUT_FILENO;
2126 }
2127
2128 if (path == NULL && isatty(outFileNum) && !(opt & OPT_force)) {
2129 bb_error_msg("compressed data not written "
2130 "to a terminal. Use -f to force compression.");
2131 free(path);
2132 continue;
2133 }
2134
2135 zip(inFileNum, outFileNum);
2136
2137 if (path != NULL) {
2138 char *delFileName;
2139
2140 close(inFileNum);
2141 close(outFileNum);
2142
2143 /* Delete the original file */
2144 // Pity we don't propagate zip failures to this place...
2145 //if (zip_is_ok)
2146 delFileName = argv[i];
2147 //else
2148 // delFileName = path;
2149 if (unlink(delFileName) < 0)
2150 bb_perror_msg("%s", delFileName);
2151 }
2152
2153 free(path);
2154 }
2155
2156 return 0; //##G1.exit_code;
2157}
2158#endif
2159
2160int bbunpack(char **argv,
2161 char* (*make_new_name)(char *filename),
2162 USE_DESKTOP(long long) int (*unpacker)(void)
2163);
2164
2165static 2013static
2166char* make_new_name_gzip(char *filename) 2014char* make_new_name_gzip(char *filename)
2167{ 2015{
@@ -2173,10 +2021,10 @@ USE_DESKTOP(long long) int pack_gzip(void)
2173{ 2021{
2174 struct stat s; 2022 struct stat s;
2175 2023
2176 G1.time_stamp = 0; 2024 clear_bufs();
2177 if (!fstat(STDIN_FILENO, &s)) 2025 s.st_ctime = 0;
2178 G1.time_stamp = s.st_ctime; 2026 fstat(STDIN_FILENO, &s);
2179 zip(STDIN_FILENO, STDOUT_FILENO); 2027 zip(s.st_ctime);
2180 return 0; 2028 return 0;
2181} 2029}
2182 2030
@@ -2187,6 +2035,7 @@ int gzip_main(int argc, char **argv)
2187 2035
2188 /* Must match bbunzip's constants OPT_STDOUT, OPT_FORCE! */ 2036 /* Must match bbunzip's constants OPT_STDOUT, OPT_FORCE! */
2189 opt = getopt32(argc, argv, "cfv" USE_GUNZIP("d") "q123456789" ); 2037 opt = getopt32(argc, argv, "cfv" USE_GUNZIP("d") "q123456789" );
2038 argv += optind;
2190 option_mask32 &= 0x7; /* Clear -d, ignore -q, -0..9 */ 2039 option_mask32 &= 0x7; /* Clear -d, ignore -q, -0..9 */
2191 //if (opt & 0x1) // -c 2040 //if (opt & 0x1) // -c
2192 //if (opt & 0x2) // -f 2041 //if (opt & 0x2) // -f
@@ -2235,7 +2084,5 @@ int gzip_main(int argc, char **argv)
2235 /* Initialise the CRC32 table */ 2084 /* Initialise the CRC32 table */
2236 G1.crc_32_tab = crc32_filltable(0); 2085 G1.crc_32_tab = crc32_filltable(0);
2237 2086
2238 clear_bufs();
2239
2240 return bbunpack(argv, make_new_name_gzip, pack_gzip); 2087 return bbunpack(argv, make_new_name_gzip, pack_gzip);
2241} 2088}
diff --git a/include/libbb.h b/include/libbb.h
index 401fce4cf..759eb8d15 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -519,6 +519,10 @@ int chown_main(int argc, char **argv);
519#if ENABLE_GUNZIP 519#if ENABLE_GUNZIP
520int gunzip_main(int argc, char **argv); 520int gunzip_main(int argc, char **argv);
521#endif 521#endif
522int bbunpack(char **argv,
523 char* (*make_new_name)(char *filename),
524 USE_DESKTOP(long long) int (*unpacker)(void)
525);
522 526
523 527
524/* Networking */ 528/* Networking */