X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=archival%2Fgzip.c;h=403eb4dcb4eeabbd982cc0f44e4e7c28274ea3bd;hb=0d0260fd1e55c39525660370094d090bc5412fe5;hp=68f1cc87d93436b94c8a47d5c5b1e57c513692b4;hpb=972288e62fa0798f59caf209ab58c911d289a8b2;p=oweals%2Fbusybox.git diff --git a/archival/gzip.c b/archival/gzip.c index 68f1cc87d..403eb4dcb 100644 --- a/archival/gzip.c +++ b/archival/gzip.c @@ -13,7 +13,7 @@ * files as well as stdin/stdout, and to generally behave itself wrt * command line handling. * - * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. + * Licensed under GPLv2 or later, see file LICENSE in this source tree. */ /* big objects in bss: @@ -39,7 +39,23 @@ gzip: bogus: No such file or directory aa: 85.1% -- replaced with aa.gz */ -#include "busybox.h" +//usage:#define gzip_trivial_usage +//usage: "[-cfd] [FILE]..." +//usage:#define gzip_full_usage "\n\n" +//usage: "Compress FILEs (or stdin)\n" +//usage: "\n -d Decompress" +//usage: "\n -c Write to stdout" +//usage: "\n -f Force" +//usage: +//usage:#define gzip_example_usage +//usage: "$ ls -la /tmp/busybox*\n" +//usage: "-rw-rw-r-- 1 andersen andersen 1761280 Apr 14 17:47 /tmp/busybox.tar\n" +//usage: "$ gzip /tmp/busybox.tar\n" +//usage: "$ ls -la /tmp/busybox*\n" +//usage: "-rw-rw-r-- 1 andersen andersen 554058 Apr 14 17:49 /tmp/busybox.tar.gz\n" + +#include "libbb.h" +#include "archive.h" /* =========================================================================== @@ -47,12 +63,12 @@ aa: 85.1% -- replaced with aa.gz //#define DEBUG 1 /* Diagnostic functions */ #ifdef DEBUG -# define Assert(cond,msg) {if(!(cond)) bb_error_msg(msg);} +# define Assert(cond,msg) { if (!(cond)) bb_error_msg(msg); } # define Trace(x) fprintf x -# define Tracev(x) {if (verbose) fprintf x ;} -# define Tracevv(x) {if (verbose > 1) fprintf x ;} -# define Tracec(c,x) {if (verbose && (c)) fprintf x ;} -# define Tracecv(c,x) {if (verbose > 1 && (c)) fprintf x ;} +# define Tracev(x) {if (verbose) fprintf x; } +# define Tracevv(x) {if (verbose > 1) fprintf x; } +# define Tracec(c,x) {if (verbose && (c)) fprintf x; } +# define Tracecv(c,x) {if (verbose > 1 && (c)) fprintf x; } #else # define Assert(cond,msg) # define Trace(x) @@ -67,7 +83,7 @@ aa: 85.1% -- replaced with aa.gz */ #define SMALL_MEM -#ifndef INBUFSIZ +#ifndef INBUFSIZ # ifdef SMALL_MEM # define INBUFSIZ 0x2000 /* input buffer size */ # else @@ -75,7 +91,7 @@ aa: 85.1% -- replaced with aa.gz # endif #endif -#ifndef OUTBUFSIZ +#ifndef OUTBUFSIZ # ifdef SMALL_MEM # define OUTBUFSIZ 8192 /* output buffer size */ # else @@ -266,7 +282,7 @@ struct globals { #define DECLARE(type, array, size) \ type * array #define ALLOC(type, array, size) \ - array = xzalloc((size_t)(((size)+1L)/2) * 2*sizeof(type)); + array = xzalloc((size_t)(((size)+1L)/2) * 2*sizeof(type)) #define FREE(array) \ do { free(array); array = NULL; } while (0) @@ -339,7 +355,7 @@ struct globals { ulg bits_sent; /* bit length of the compressed data */ #endif - uint32_t *crc_32_tab; + /*uint32_t *crc_32_tab;*/ uint32_t crc; /* shift register contents */ }; @@ -387,33 +403,14 @@ static void put_32bit(ulg n) put_16bit(n >> 16); } -/* =========================================================================== - * Clear input and output buffers - */ -static void clear_bufs(void) -{ - G1.outcnt = 0; -#ifdef DEBUG - G1.insize = 0; -#endif - G1.isize = 0; -} - - /* =========================================================================== * Run a set of bytes through the crc shift register. If s is a NULL * pointer, then initialize the crc shift register contents instead. * Return the current crc in either case. */ -static uint32_t updcrc(uch * s, unsigned n) +static void updcrc(uch * s, unsigned n) { - uint32_t c = G1.crc; - while (n) { - c = G1.crc_32_tab[(uch)(c ^ *s++)] ^ (c >> 8); - n--; - } - G1.crc = c; - return c; + G1.crc = crc32_block_endian0(G1.crc, s, n, global_crc32_table /*G1.crc_32_tab*/); } @@ -632,10 +629,12 @@ static int longest_match(IPos cur_match) /* Skip to next match if the match length cannot increase * or if the match length is less than 2: */ - if (match[best_len] != scan_end || - match[best_len - 1] != scan_end1 || - *match != *scan || *++match != scan[1]) + if (match[best_len] != scan_end + || match[best_len - 1] != scan_end1 + || *match != *scan || *++match != scan[1] + ) { continue; + } /* The check at best_len-1 can be removed because it will be made * again later. (This heuristic is not always a win.) @@ -686,7 +685,7 @@ static void check_match(IPos start, IPos match, int length) if (verbose > 1) { bb_error_msg("\\[%d,%d]", start - match, length); do { - putc(G1.window[start++], stderr); + bb_putchar_stderr(G1.window[start++]); } while (--length != 0); } } @@ -768,26 +767,24 @@ static void check_match(IPos start, IPos match, int length) #define BL_CODES 19 /* number of codes used to transfer the bit lengths */ -typedef uch extra_bits_t; - /* extra bits for each length code */ -static const extra_bits_t extra_lbits[LENGTH_CODES]= { +static const uint8_t extra_lbits[LENGTH_CODES] ALIGN1 = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0 }; /* extra bits for each distance code */ -static const extra_bits_t extra_dbits[D_CODES] = { +static const uint8_t extra_dbits[D_CODES] ALIGN1 = { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13 }; /* extra bits for each bit length code */ -static const extra_bits_t extra_blbits[BL_CODES] = { +static const uint8_t extra_blbits[BL_CODES] ALIGN1 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 7 }; /* number of codes at each bit length for an optimal tree */ -static const uch bl_order[BL_CODES] = { +static const uint8_t bl_order[BL_CODES] ALIGN1 = { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 }; #define STORED_BLOCK 0 @@ -861,7 +858,7 @@ typedef struct ct_data { typedef struct tree_desc { ct_data *dyn_tree; /* the dynamic tree */ ct_data *static_tree; /* corresponding static tree or NULL */ - const extra_bits_t *extra_bits; /* extra bits for each code or NULL */ + const uint8_t *extra_bits; /* extra bits for each code or NULL */ int extra_base; /* base index for extra_bits */ int elems; /* max number of elements in the tree */ int max_length; /* max bit length for the codes */ @@ -976,7 +973,7 @@ static void compress_block(ct_data * ltree, ct_data * dtree); #else # define SEND_CODE(c, tree) \ { \ - if (verbose > 1) bb_error_msg("\ncd %3d ",(c)); \ + if (verbose > 1) bb_error_msg("\ncd %3d ", (c)); \ send_bits(tree[c].Code, tree[c].Len); \ } #endif @@ -1064,7 +1061,7 @@ static void pqdownheap(ct_data * tree, int k) static void gen_bitlen(tree_desc * desc) { ct_data *tree = desc->dyn_tree; - const extra_bits_t *extra = desc->extra_bits; + const uint8_t *extra = desc->extra_bits; int base = desc->extra_base; int max_code = desc->max_code; int max_length = desc->max_length; @@ -1187,7 +1184,7 @@ static void gen_codes(ct_data * tree, int max_code) Tracec(tree != G2.static_ltree, (stderr, "\nn %3d %c l %2d c %4x (%x) ", n, - (isgraph(n) ? n : ' '), len, tree[n].Code, + (n > ' ' ? n : ' '), len, tree[n].Code, next_code[len] - 1)); } } @@ -1555,7 +1552,7 @@ static void compress_block(ct_data * ltree, ct_data * dtree) lc = G1.l_buf[lx++]; if ((flag & 1) == 0) { SEND_CODE(lc, ltree); /* send a literal byte */ - Tracecv(isgraph(lc), (stderr, " '%c' ", lc)); + Tracecv(lc > ' ', (stderr, " '%c' ", lc)); } else { /* Here, lc is the match length - MIN_MATCH */ code = G2.length_code[lc]; @@ -1676,7 +1673,7 @@ static ulg flush_block(char *buf, ulg stored_len, int eof) /* =========================================================================== * Update a hash value with the given input byte - * IN assertion: all calls to to UPDATE_HASH are made with consecutive + * IN assertion: all calls to UPDATE_HASH are made with consecutive * input characters, so that a running hash key can be computed from the * previous key instead of complete recalculation each time. */ @@ -1707,7 +1704,7 @@ static ulg flush_block(char *buf, ulg stored_len, int eof) /* Insert string s in the dictionary and set match_head to the previous head * of the hash chain (the most recent string with same hash key). Return * the previous length of the hash chain. - * IN assertion: all calls to to INSERT_STRING are made with consecutive + * IN assertion: all calls to INSERT_STRING are made with consecutive * input characters and the first MIN_MATCH bytes of s are valid * (except for the last MIN_MATCH-1 bytes of the input file). */ #define INSERT_STRING(s, match_head) \ @@ -2010,68 +2007,104 @@ static void zip(ulg time_stamp) /* ======================================================================== */ static -char* make_new_name_gzip(char *filename) -{ - return xasprintf("%s.gz", filename); -} - -static -USE_DESKTOP(long long) int pack_gzip(void) +IF_DESKTOP(long long) int FAST_FUNC pack_gzip(unpack_info_t *info UNUSED_PARAM) { struct stat s; - - clear_bufs(); + + /* Clear input and output buffers */ + G1.outcnt = 0; +#ifdef DEBUG + G1.insize = 0; +#endif + G1.isize = 0; + + /* Reinit G2.xxx */ + memset(&G2, 0, sizeof(G2)); + G2.l_desc.dyn_tree = G2.dyn_ltree; + G2.l_desc.static_tree = G2.static_ltree; + G2.l_desc.extra_bits = extra_lbits; + G2.l_desc.extra_base = LITERALS + 1; + G2.l_desc.elems = L_CODES; + G2.l_desc.max_length = MAX_BITS; + //G2.l_desc.max_code = 0; + G2.d_desc.dyn_tree = G2.dyn_dtree; + G2.d_desc.static_tree = G2.static_dtree; + G2.d_desc.extra_bits = extra_dbits; + //G2.d_desc.extra_base = 0; + G2.d_desc.elems = D_CODES; + G2.d_desc.max_length = MAX_BITS; + //G2.d_desc.max_code = 0; + G2.bl_desc.dyn_tree = G2.bl_tree; + //G2.bl_desc.static_tree = NULL; + G2.bl_desc.extra_bits = extra_blbits, + //G2.bl_desc.extra_base = 0; + G2.bl_desc.elems = BL_CODES; + G2.bl_desc.max_length = MAX_BL_BITS; + //G2.bl_desc.max_code = 0; + s.st_ctime = 0; fstat(STDIN_FILENO, &s); zip(s.st_ctime); return 0; } -int gzip_main(int argc, char **argv); +#if ENABLE_FEATURE_GZIP_LONG_OPTIONS +static const char gzip_longopts[] ALIGN1 = + "stdout\0" No_argument "c" + "to-stdout\0" No_argument "c" + "force\0" No_argument "f" + "verbose\0" No_argument "v" +#if ENABLE_GUNZIP + "decompress\0" No_argument "d" + "uncompress\0" No_argument "d" + "test\0" No_argument "t" +#endif + "quiet\0" No_argument "q" + "fast\0" No_argument "1" + "best\0" No_argument "9" + ; +#endif + +/* + * Linux kernel build uses gzip -d -n. We accept and ignore -n. + * Man page says: + * -n --no-name + * gzip: do not save the original file name and time stamp. + * (The original name is always saved if the name had to be truncated.) + * gunzip: do not restore the original file name/time even if present + * (remove only the gzip suffix from the compressed file name). + * This option is the default when decompressing. + * -N --name + * gzip: always save the original file name and time stamp (this is the default) + * gunzip: restore the original file name and time stamp if present. + */ + +int gzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; +#if ENABLE_GUNZIP int gzip_main(int argc, char **argv) +#else +int gzip_main(int argc UNUSED_PARAM, char **argv) +#endif { unsigned opt; +#if ENABLE_FEATURE_GZIP_LONG_OPTIONS + applet_long_options = gzip_longopts; +#endif /* Must match bbunzip's constants OPT_STDOUT, OPT_FORCE! */ - opt = getopt32(argc, argv, "cfv" USE_GUNZIP("d") "q123456789" ); - argv += optind; - option_mask32 &= 0x7; /* Clear -d, ignore -q, -0..9 */ - //if (opt & 0x1) // -c - //if (opt & 0x2) // -f - //if (opt & 0x4) // -v + opt = getopt32(argv, "cfv" IF_GUNZIP("dt") "q123456789n"); #if ENABLE_GUNZIP /* gunzip_main may not be visible... */ - if (opt & 0x8) { // -d - /* FIXME: getopt32 should not depend on optind */ - optind = 1; + if (opt & 0x18) // -d and/or -t return gunzip_main(argc, argv); - } #endif + option_mask32 &= 0x7; /* ignore -q, -0..9 */ + //if (opt & 0x1) // -c + //if (opt & 0x2) // -f + //if (opt & 0x4) // -v + argv += optind; - ptr_to_globals = xzalloc(sizeof(struct globals) + sizeof(struct globals2)); - ptr_to_globals++; - G2.l_desc.dyn_tree = G2.dyn_ltree; - G2.l_desc.static_tree = G2.static_ltree; - G2.l_desc.extra_bits = extra_lbits; - G2.l_desc.extra_base = LITERALS + 1; - G2.l_desc.elems = L_CODES; - G2.l_desc.max_length = MAX_BITS; - //G2.l_desc.max_code = 0; - - G2.d_desc.dyn_tree = G2.dyn_dtree; - G2.d_desc.static_tree = G2.static_dtree; - G2.d_desc.extra_bits = extra_dbits; - //G2.d_desc.extra_base = 0; - G2.d_desc.elems = D_CODES; - G2.d_desc.max_length = MAX_BITS; - //G2.d_desc.max_code = 0; - - G2.bl_desc.dyn_tree = G2.bl_tree; - //G2.bl_desc.static_tree = NULL; - G2.bl_desc.extra_bits = extra_blbits, - //G2.bl_desc.extra_base = 0; - G2.bl_desc.elems = BL_CODES; - G2.bl_desc.max_length = MAX_BL_BITS; - //G2.bl_desc.max_code = 0; + SET_PTR_TO_GLOBALS((char *)xzalloc(sizeof(struct globals)+sizeof(struct globals2)) + + sizeof(struct globals)); /* Allocate all global buffers (for DYN_ALLOC option) */ ALLOC(uch, G1.l_buf, INBUFSIZ); @@ -2080,8 +2113,8 @@ int gzip_main(int argc, char **argv) ALLOC(uch, G1.window, 2L * WSIZE); ALLOC(ush, G1.prev, 1L << BITS); - /* Initialise the CRC32 table */ - G1.crc_32_tab = crc32_filltable(0); + /* Initialize the CRC32 table */ + global_crc32_table = crc32_filltable(NULL, 0); - return bbunpack(argv, make_new_name_gzip, pack_gzip); + return bbunpack(argv, pack_gzip, append_ext, "gz"); }