typo fix in a comment in a testcase. oh well...
[oweals/busybox.git] / archival / gzip.c
index b8f5765005312b289cc3aedbbe3ddf84f9a5aa5d..1e56c9dec453dd51c7af76a646e7b5d240d448ff 100644 (file)
@@ -39,7 +39,8 @@ gzip: bogus: No such file or directory
 aa:      85.1% -- replaced with aa.gz
 */
 
-#include "busybox.h"
+#include "libbb.h"
+#include "unarchive.h"
 
 
 /* ===========================================================================
@@ -47,12 +48,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)
@@ -266,7 +267,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)
 
@@ -387,19 +388,6 @@ 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.
@@ -686,7 +674,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);
+                       fputc(G1.window[start++], stderr);
                } while (--length != 0);
        }
 }
@@ -768,26 +756,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 +847,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 */
@@ -1064,7 +1050,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;
@@ -2016,60 +2002,85 @@ char* make_new_name_gzip(char *filename)
 }
 
 static
-USE_DESKTOP(long long) int pack_gzip(void)
+IF_DESKTOP(long long) int 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);
+/*
+ * Linux kernel build uses gzip -d -n. We accept and ignore it.
+ * 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;
 
        /* 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
+       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))
-                       + sizeof(struct 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));
+       barrier();
 
        /* Allocate all global buffers (for DYN_ALLOC option) */
        ALLOC(uch, G1.l_buf, INBUFSIZ);
@@ -2079,7 +2090,7 @@ int gzip_main(int argc, char **argv)
        ALLOC(ush, G1.prev, 1L << BITS);
 
        /* Initialise the CRC32 table */
-       G1.crc_32_tab = crc32_filltable(0);
+       G1.crc_32_tab = crc32_filltable(NULL, 0);
 
        return bbunpack(argv, make_new_name_gzip, pack_gzip);
 }