[BusyBox] Patch: libbb/unzip.c size reduction

rbrown64 at csc.com.au rbrown64 at csc.com.au
Sat Dec 1 01:06:31 UTC 2001


   text      data       bss       dec        hex   filename
   6356         4        36      6396       18fc   libbb/unzip.o       -- i?86 linux
   6200       428        36      6664       1a08   libbb/unzip.o.0

2001-12-01  Rodney Brown  <rbrown64 at csc.com.au>
     * libbb/unzip.c: Space reductions.
     * archival/bunzip2.c: Declare BZ2_crc32Table const.


--- TODO  2001/12/01 01:50:37 1.1
+++ TODO  2001/12/01 02:00:38
@@ -56,3 +56,8 @@ and then start with the biggest things a
 xargs could use a -l option

 ------------------------------------------------------------------
+
+libbb/unzip.c and archival/gzip.c have common constant static arrays and
+code for initializing the CRC array. Both use CRC-32 and could use
+common code for CRC calculation. Within archival/gzip.c, the CRC
+array should be malloc-ed as it is in libbb/unzip.c .
--- Changelog  2001/12/01 01:50:37 1.1
+++ Changelog  2001/12/01 01:51:05
@@ -15,7 +15,7 @@
         -- a whole bunch of ash size optimizations
         -- Fix for ash leading redirections (i.e. '2>/dev/null ls rubbish')
     * Rodney Brown  <RDBrown at mira.net>
-        -- Optimized gzip.c, shrinking it be ~1.5k
+        -- Optimized gzip.c, shrinking it by ~1.5k
     * Matt Kraai
         -- Fix sed s/[/]// handling (closes: #1208).
         -- Fix `-/bin/sh' invocation (closes: #1209).
--- libbb/unzip.c   2001/11/30 11:06:14  1.2
+++ libbb/unzip.c   2001/12/01 02:06:49
@@ -124,19 +124,12 @@ static void abort_gzip(void)
 static void make_crc_table(void)
 {
     unsigned long table_entry;      /* crc shift register */
-    unsigned long poly = 0;      /* polynomial exclusive-or pattern */
+    const unsigned long poly = 0xedb88320L;      /* polynomial exclusive-or pattern */
     int i;                /* counter for all possible eight bit values */
     int k;                /* byte being shifted into crc apparatus */

-    /* terms of polynomial defining this crc (except x^32): */
-    static int p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
-
     crc_table = (unsigned long *) malloc(256 * sizeof(unsigned long));

-    /* Make exclusive-or pattern from polynomial (0xedb88320) */
-    for (i = 0; i < sizeof(p)/sizeof(int); i++)
-         poly |= 1L << (31 - p[i]);
-
     /* Compute and print table of CRC's, five per line */
     for (i = 0; i < 256; i++) {
          table_entry = i;
@@ -192,6 +185,8 @@ static int huft_free(huft_t *t)
     return 0;
 }

+typedef unsigned char extra_bits_t;
+
 /* Given a list of code lengths and a maximum table size, make a set of
  * tables to decode that set of codes.  Return zero on success, one if
  * the given code set is incomplete (the tables are still built in this
@@ -207,7 +202,7 @@ static int huft_free(huft_t *t)
  * m:    maximum lookup bits, returns actual
  */
 static int huft_build(unsigned int *b, const unsigned int n, const unsigned int s,
-    const unsigned short *d, const unsigned short *e, huft_t **t, int *m)
+    const unsigned short *d, const extra_bits_t *e, huft_t **t, int *m)
 {
     unsigned a;         /* counter for codes of length k */
     unsigned c[BMAX + 1];    /* bit length count table */
@@ -499,6 +494,30 @@ static int inflate_codes(huft_t *tl, huf
     return 0;
 }

+static const unsigned short cplens[] = {     /* Copy lengths for literal codes 257..285 */
+    3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
+    35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
+};
+/* note: see note #13 above about the 258 in this list. */
+static const extra_bits_t cplext[] = {  /* Extra bits for literal codes 257..285 */
+    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, 99, 99
+};                 /* 99==invalid */
+static const unsigned short cpdist[] = {     /* Copy offsets for distance codes 0..29 */
+    1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
+    257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
+    8193, 12289, 16385, 24577
+};
+static const extra_bits_t cpdext[] = {  /* Extra bits for distance codes */
+    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
+};
+/* Tables for deflate from PKZIP's appnote.txt. */
+static const extra_bits_t border[] = {  /* Order of the bit length code lengths */
+    16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
+};
+
 /*
  * decompress an inflated block
  * e: last block flag
@@ -510,25 +529,6 @@ static int inflate_block(int *e)
     unsigned t;              /* block type */
     register unsigned long b;                /* bit buffer */
     register unsigned k;          /* number of bits in bit buffer */
-    static unsigned short cplens[] = {       /* Copy lengths for literal codes 257..285 */
-         3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
-         35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
-    };
-    /* note: see note #13 above about the 258 in this list. */
-    static unsigned short cplext[] = {       /* Extra bits for literal codes 257..285 */
-         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, 99, 99
-    };                  /* 99==invalid */
-    static unsigned short cpdist[] = {       /* Copy offsets for distance codes 0..29 */
-         1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
-         257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
-         8193, 12289, 16385, 24577
-    };
-    static unsigned short cpdext[] = {       /* Extra bits for distance codes */
-         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
-    };

     /* make local bit buffer */
     b = bb;
@@ -667,12 +667,8 @@ static int inflate_block(int *e)
          }
     case 2:   /* Inflate dynamic */
          {
-              /* Tables for deflate from PKZIP's appnote.txt. */
-              static unsigned border[] = {   /* Order of the bit length code lengths */
-                   16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
-              };
-              int dbits = 6;                      /* bits in base distance lookup table */
-              int lbits = 9;                      /* bits in base literal/length lookup table */
+              const int dbits = 6;                     /* bits in base distance lookup table */
+              const int lbits = 9;                     /* bits in base literal/length lookup table */

               int i;                              /* temporary variables */
               unsigned j;
--- archival/bunzip2.c   2001/11/30 10:43:23  1.1
+++ archival/bunzip2.c   2001/11/30 10:44:33
@@ -276,7 +276,7 @@ int numFileNames;
 int numFilesProcessed;
 int exitValue;

-unsigned int BZ2_crc32Table[256] = {
+const unsigned int BZ2_crc32Table[256] = {

    /*-- Ugly, innit? --*/







More information about the busybox mailing list