add tests for gunzip
[oweals/busybox.git] / archival / gzip.c
index c2333e3b13d2200ca249190fd792abc2ac7abd0d..cabcc5ecb4c819baeef85a1037aa4463211df6cd 100644 (file)
@@ -39,7 +39,7 @@ gzip: bogus: No such file or directory
 aa:      85.1% -- replaced with aa.gz
 */
 
-#include "busybox.h"
+#include "libbb.h"
 
 
 /* ===========================================================================
@@ -47,12 +47,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)
@@ -686,7 +686,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 +768,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 +859,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 +1062,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;
@@ -2019,7 +2017,7 @@ static
 USE_DESKTOP(long long) int pack_gzip(void)
 {
        struct stat s;
-       
+
        clear_bufs();
        s.st_ctime = 0;
        fstat(STDIN_FILENO, &s);
@@ -2033,8 +2031,7 @@ int gzip_main(int argc, char **argv)
        unsigned opt;
 
        /* Must match bbunzip's constants OPT_STDOUT, OPT_FORCE! */
-       opt = getopt32(argc, argv, "cfv" USE_GUNZIP("d") "q123456789" );
-       argv += optind;
+       opt = getopt32(argv, "cfv" USE_GUNZIP("d") "q123456789" );
        option_mask32 &= 0x7; /* Clear -d, ignore -q, -0..9 */
        //if (opt & 0x1) // -c
        //if (opt & 0x2) // -f
@@ -2044,6 +2041,7 @@ int gzip_main(int argc, char **argv)
                return gunzip_main(argc, argv);
        }
 #endif
+       argv += optind;
 
        PTR_TO_GLOBALS = xzalloc(sizeof(struct globals) + sizeof(struct globals2))
                        + sizeof(struct globals);
@@ -2079,7 +2077,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);
 }