udhcpc: fix a problem with binary-encoded options #2
[oweals/busybox.git] / archival / gzip.c
index f9c8e4d4ba2ff68dc474626416494270f3d00081..31ccab3cd7a5013b3e34ed027c9b7f050676702e 100644 (file)
@@ -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,8 +39,23 @@ gzip: bogus: No such file or directory
 aa:      85.1% -- replaced with aa.gz
 */
 
+//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 "unarchive.h"
+#include "bb_archive.h"
 
 
 /* ===========================================================================
@@ -66,9 +81,17 @@ aa:      85.1% -- replaced with aa.gz
 
 /* ===========================================================================
  */
-#define SMALL_MEM
+#if   CONFIG_GZIP_FAST == 0
+# define SMALL_MEM
+#elif CONFIG_GZIP_FAST == 1
+# define MEDIUM_MEM
+#elif CONFIG_GZIP_FAST == 2
+# define BIG_MEM
+#else
+# error "Invalid CONFIG_GZIP_FAST value"
+#endif
 
-#ifndef        INBUFSIZ
+#ifndef INBUFSIZ
 #  ifdef SMALL_MEM
 #    define INBUFSIZ  0x2000   /* input buffer size */
 #  else
@@ -76,7 +99,7 @@ aa:      85.1% -- replaced with aa.gz
 #  endif
 #endif
 
-#ifndef        OUTBUFSIZ
+#ifndef OUTBUFSIZ
 #  ifdef SMALL_MEM
 #    define OUTBUFSIZ   8192   /* output buffer size */
 #  else
@@ -340,7 +363,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 */
 };
 
@@ -393,15 +416,9 @@ static void put_32bit(ulg n)
  * 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*/);
 }
 
 
@@ -676,7 +693,7 @@ static void check_match(IPos start, IPos match, int length)
        if (verbose > 1) {
                bb_error_msg("\\[%d,%d]", start - match, length);
                do {
-                       fputc(G1.window[start++], stderr);
+                       bb_putchar_stderr(G1.window[start++]);
                } while (--length != 0);
        }
 }
@@ -964,7 +981,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
@@ -1162,7 +1179,7 @@ static void gen_codes(ct_data * tree, int max_code)
         * must be all ones.
         */
        Assert(code + G2.bl_count[MAX_BITS] - 1 == (1 << MAX_BITS) - 1,
-                  "inconsistent bit counts");
+                       "inconsistent bit counts");
        Tracev((stderr, "\ngen_codes: max_code %d ", max_code));
 
        for (n = 0; n <= max_code; n++) {
@@ -1510,9 +1527,9 @@ static int ct_tally(int dist, int lc)
                }
                out_length >>= 3;
                Trace((stderr,
-                          "\nlast_lit %u, last_dist %u, in %ld, out ~%ld(%ld%%) ",
-                          G2.last_lit, G2.last_dist, in_length, out_length,
-                          100L - out_length * 100L / in_length));
+                               "\nlast_lit %u, last_dist %u, in %ld, out ~%ld(%ld%%) ",
+                               G2.last_lit, G2.last_dist, in_length, out_length,
+                               100L - out_length * 100L / in_length));
                if (G2.last_dist < G2.last_lit / 2 && out_length < in_length / 2)
                        return 1;
        }
@@ -1604,9 +1621,9 @@ static ulg flush_block(char *buf, ulg stored_len, int eof)
        static_lenb = (G2.static_len + 3 + 7) >> 3;
 
        Trace((stderr,
-                  "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u dist %u ",
-                  opt_lenb, G2.opt_len, static_lenb, G2.static_len, stored_len,
-                  G2.last_lit, G2.last_dist));
+                       "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u dist %u ",
+                       opt_lenb, G2.opt_len, static_lenb, G2.static_len, stored_len,
+                       G2.last_lit, G2.last_dist));
 
        if (static_lenb <= opt_lenb)
                opt_lenb = static_lenb;
@@ -1644,7 +1661,7 @@ static ulg flush_block(char *buf, ulg stored_len, int eof)
        } else {
                send_bits((DYN_TREES << 1) + eof, 3);
                send_all_trees(G2.l_desc.max_code + 1, G2.d_desc.max_code + 1,
-                                          max_blindex + 1);
+                                       max_blindex + 1);
                compress_block((ct_data *) G2.dyn_ltree, (ct_data *) G2.dyn_dtree);
                G2.compressed_len += 3 + G2.opt_len;
        }
@@ -1664,7 +1681,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.
  */
@@ -1695,7 +1712,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) \
@@ -1998,13 +2015,7 @@ static void zip(ulg time_stamp)
 
 /* ======================================================================== */
 static
-char* make_new_name_gzip(char *filename)
-{
-       return xasprintf("%s.gz", filename);
-}
-
-static
-IF_DESKTOP(long long) int pack_gzip(unpack_info_t *info UNUSED_PARAM)
+IF_DESKTOP(long long) int FAST_FUNC pack_gzip(transformer_aux_data_t *aux UNUSED_PARAM)
 {
        struct stat s;
 
@@ -2063,7 +2074,7 @@ static const char gzip_longopts[] ALIGN1 =
 #endif
 
 /*
- * Linux kernel build uses gzip -d -n. We accept and ignore it.
+ * 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.
@@ -2110,8 +2121,8 @@ int gzip_main(int argc UNUSED_PARAM, char **argv)
        ALLOC(uch, G1.window, 2L * WSIZE);
        ALLOC(ush, G1.prev, 1L << BITS);
 
-       /* Initialise the CRC32 table */
-       G1.crc_32_tab = crc32_filltable(NULL, 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");
 }