random small shrinkage and elimination of statics
[oweals/busybox.git] / archival / libunarchive / decompress_uncompress.c
index a3fcf64e22c76b53cf187d37b666459a39b81cc1..8ce3cba748c8322d6632d77dbe0fb221eb1a87c4 100644 (file)
@@ -1,4 +1,4 @@
-#include "config.h"
+/* vi: set sw=4 ts=4: */
 #include "libbb.h"
 
 /* uncompress for busybox -- (c) 2002 Robert Griebl
 #undef MAXSEG_64K
 #define MAXCODE(n)     (1L << (n))
 
-/* Block compress mode -C compatible with 2.0 */
-static int block_mode = BLOCK_MODE;
-
-/* user settable max # bits/code */
-static int maxbits = BITS;
-
-/* Exitcode of compress (-1 no file compressed) */
-static int exit_code = -1;
-
-/* Input buffer */
-static unsigned char inbuf[IBUFSIZ + 64];
-
-/* Output buffer */
-static unsigned char outbuf[OBUFSIZ + 2048];
-
-
-static long int htab[HSIZE];
-static unsigned short codetab[HSIZE];
-
 #define        htabof(i)                               htab[i]
 #define        codetabof(i)                    codetab[i]
 #define        tab_prefixof(i)                 codetabof(i)
 #define        tab_suffixof(i)                 ((unsigned char *)(htab))[i]
 #define        de_stack                                ((unsigned char *)&(htab[HSIZE-1]))
-#define        clear_htab()                    memset(htab, -1, sizeof(htab))
+#define        clear_htab()                    memset(htab, -1, HSIZE)
 #define        clear_tab_prefixof()    memset(codetab, 0, 256);
 
 
@@ -99,8 +80,10 @@ static unsigned short codetab[HSIZE];
  * with those of the compress() routine.  See the definitions above.
  */
 
-extern int uncompress(int fd_in, int fd_out)
+USE_DESKTOP(long long) int
+uncompress(int fd_in, int fd_out)
 {
+       USE_DESKTOP(long long total_written = 0;)
        unsigned char *stackp;
        long int code;
        int finchar;
@@ -116,23 +99,42 @@ extern int uncompress(int fd_in, int fd_out)
        long int maxmaxcode;
        int n_bits;
        int rsize = 0;
+       RESERVE_CONFIG_UBUFFER(inbuf, IBUFSIZ + 64);
+       RESERVE_CONFIG_UBUFFER(outbuf, OBUFSIZ + 2048);
+       unsigned char htab[HSIZE];
+       unsigned short codetab[HSIZE];
+
+       /* Hmm, these were statics - why?! */
+       /* user settable max # bits/code */
+       int maxbits; /* = BITS; */
+       /* block compress mode -C compatible with 2.0 */
+       int block_mode; /* = BLOCK_MODE; */
+
+       memset(inbuf, 0, IBUFSIZ + 64);
+       memset(outbuf, 0, OBUFSIZ + 2048);
 
        insize = 0;
 
-       inbuf[0] = bb_xread_char(fd_in);
+       /* xread isn't good here, we have to return - caller may want
+        * to do some cleanup (e.g. delete incomplete unpacked file etc) */
+       if (full_read(fd_in, inbuf, 1) != 1) {
+               bb_error_msg("short read");
+               return -1;
+       }
 
        maxbits = inbuf[0] & BIT_MASK;
        block_mode = inbuf[0] & BLOCK_MODE;
        maxmaxcode = MAXCODE(maxbits);
 
        if (maxbits > BITS) {
-               bb_error_msg("compressed with %d bits, can only handle %d bits", maxbits,
-                                 BITS);
+               bb_error_msg("compressed with %d bits, can only handle "
+                               "%d bits", maxbits, BITS);
                return -1;
        }
 
-       maxcode = MAXCODE(n_bits = INIT_BITS) - 1;
-       bitmask = (1 << n_bits) - 1;
+       n_bits = INIT_BITS;
+       maxcode = MAXCODE(INIT_BITS) - 1;
+       bitmask = (1 << INIT_BITS) - 1;
        oldcode = -1;
        finchar = 0;
        outpos = 0;
@@ -148,13 +150,14 @@ extern int uncompress(int fd_in, int fd_out)
        }
 
        do {
-         resetbuf:;
+ resetbuf:
                {
                        int i;
                        int e;
                        int o;
 
-                       e = insize - (o = (posbits >> 3));
+                       o = posbits >> 3;
+                       e = insize - o;
 
                        for (i = 0; i < e; ++i)
                                inbuf[i] = inbuf[i + o];
@@ -163,8 +166,9 @@ extern int uncompress(int fd_in, int fd_out)
                        posbits = 0;
                }
 
-               if (insize < (int) sizeof(inbuf) - IBUFSIZ) {
+               if (insize < (int) (IBUFSIZ + 64) - IBUFSIZ) {
                        rsize = safe_read(fd_in, inbuf + insize, IBUFSIZ);
+//error check??
                        insize += rsize;
                }
 
@@ -189,16 +193,16 @@ extern int uncompress(int fd_in, int fd_out)
                        {
                                unsigned char *p = &inbuf[posbits >> 3];
 
-                               code =
-                                       ((((long) (p[0])) | ((long) (p[1]) << 8) |
-                                         ((long) (p[2]) << 16)) >> (posbits & 0x7)) & bitmask;
+                               code = ((((long) (p[0])) | ((long) (p[1]) << 8) |
+                                        ((long) (p[2]) << 16)) >> (posbits & 0x7)) & bitmask;
                        }
                        posbits += n_bits;
 
 
                        if (oldcode == -1) {
-                               outbuf[outpos++] = (unsigned char) (finchar =
-                                                                                               (int) (oldcode = code));
+                               oldcode = code;
+                               finchar = (int) oldcode;
+                               outbuf[outpos++] = (unsigned char) finchar;
                                continue;
                        }
 
@@ -209,8 +213,9 @@ extern int uncompress(int fd_in, int fd_out)
                                        ((posbits - 1) +
                                         ((n_bits << 3) -
                                          (posbits - 1 + (n_bits << 3)) % (n_bits << 3)));
-                               maxcode = MAXCODE(n_bits = INIT_BITS) - 1;
-                               bitmask = (1 << n_bits) - 1;
+                               n_bits = INIT_BITS;
+                               maxcode = MAXCODE(INIT_BITS) - 1;
+                               bitmask = (1 << INIT_BITS) - 1;
                                goto resetbuf;
                        }
 
@@ -243,13 +248,15 @@ extern int uncompress(int fd_in, int fd_out)
                                code = tab_prefixof(code);
                        }
 
-                       *--stackp = (unsigned char) (finchar = tab_suffixof(code));
+                       finchar = tab_suffixof(code);
+                       *--stackp = (unsigned char) finchar;
 
                        /* And put them out in forward order */
                        {
                                int i;
 
-                               if (outpos + (i = (de_stack - stackp)) >= OBUFSIZ) {
+                               i = de_stack - stackp;
+                               if (outpos + i >= OBUFSIZ) {
                                        do {
                                                if (i > OBUFSIZ - outpos) {
                                                        i = OBUFSIZ - outpos;
@@ -261,11 +268,14 @@ extern int uncompress(int fd_in, int fd_out)
                                                }
 
                                                if (outpos >= OBUFSIZ) {
-                                                       write(fd_out, outbuf, outpos);
+                                                       full_write(fd_out, outbuf, outpos);
+//error check??
+                                                       USE_DESKTOP(total_written += outpos;)
                                                        outpos = 0;
                                                }
                                                stackp += i;
-                                       } while ((i = (de_stack - stackp)) > 0);
+                                               i = de_stack - stackp;
+                                       } while (i > 0);
                                } else {
                                        memcpy(outbuf + outpos, stackp, i);
                                        outpos += i;
@@ -273,7 +283,8 @@ extern int uncompress(int fd_in, int fd_out)
                        }
 
                        /* Generate the new entry. */
-                       if ((code = free_ent) < maxmaxcode) {
+                       code = free_ent;
+                       if (code < maxmaxcode) {
                                tab_prefixof(code) = (unsigned short) oldcode;
                                tab_suffixof(code) = (unsigned char) finchar;
                                free_ent = code + 1;
@@ -286,8 +297,12 @@ extern int uncompress(int fd_in, int fd_out)
        } while (rsize > 0);
 
        if (outpos > 0) {
-               write(fd_out, outbuf, outpos);
+               full_write(fd_out, outbuf, outpos);
+//error check??
+               USE_DESKTOP(total_written += outpos;)
        }
 
-       return 0;
+       RELEASE_CONFIG_BUFFER(inbuf);
+       RELEASE_CONFIG_BUFFER(outbuf);
+       return USE_DESKTOP(total_written) + 0;
 }