Move get_unaligned_le32() macros to platform.h
[oweals/busybox.git] / archival / libarchive / decompress_gunzip.c
index 7b6f459347a879094f3cb6c444ba312dc70e32fd..c7fa5b526396481571c1c2b0d44a1dc9a7355512 100644 (file)
@@ -305,11 +305,11 @@ static int huft_build(const unsigned *b, const unsigned n,
        unsigned i;             /* counter, current code */
        unsigned j;             /* counter */
        int k;                  /* number of bits in current code */
-       unsigned *p;            /* pointer into c[], b[], or v[] */
+       const unsigned *p;      /* pointer into c[], b[], or v[] */
        huft_t *q;              /* points to current table */
        huft_t r;               /* table entry for structure assignment */
        huft_t *u[BMAX];        /* table stack */
-       unsigned v[N_MAX];      /* values in order of bit length */
+       unsigned v[N_MAX + 1];  /* values in order of bit length. last v[] is never used */
        int ws[BMAX + 1];       /* bits decoded stack */
        int w;                  /* bits decoded */
        unsigned x[BMAX + 1];   /* bit offsets, then code stack */
@@ -324,7 +324,7 @@ static int huft_build(const unsigned *b, const unsigned n,
 
        /* Generate counts for each bit length */
        memset(c, 0, sizeof(c));
-       p = (unsigned *) b; /* cast allows us to reuse p for pointing to b */
+       p = b;
        i = n;
        do {
                c[*p]++; /* assume all entries <= BMAX */
@@ -364,8 +364,12 @@ static int huft_build(const unsigned *b, const unsigned n,
                *xp++ = j;
        }
 
-       /* Make a table of values in order of bit lengths */
-       p = (unsigned *) b;
+       /* Make a table of values in order of bit lengths.
+        * To detect bad input, unused v[i]'s are set to invalid value UINT_MAX.
+        * In particular, last v[i] is never filled and must not be accessed.
+        */
+       memset(v, 0xff, sizeof(v));
+       p = b;
        i = 0;
        do {
                j = *p++;
@@ -432,7 +436,9 @@ static int huft_build(const unsigned *b, const unsigned n,
 
                        /* set up table entry in r */
                        r.b = (unsigned char) (k - w);
-                       if (p >= v + n) {
+                       if (/*p >= v + n || -- redundant, caught by the second check: */
+                           *p == UINT_MAX /* do we access uninited v[i]? (see memset(v))*/
+                       ) {
                                r.e = 99; /* out of values--invalid code */
                        } else if (*p < s) {
                                r.e = (unsigned char) (*p < 256 ? 16 : 15);     /* 256 is EOB code */
@@ -517,8 +523,9 @@ static NOINLINE int inflate_codes(STATE_PARAM_ONLY)
                e = t->e;
                if (e > 16)
                        do {
-                               if (e == 99)
-                                       abort_unzip(PASS_STATE_ONLY);;
+                               if (e == 99) {
+                                       abort_unzip(PASS_STATE_ONLY);
+                               }
                                bb >>= t->b;
                                k -= t->b;
                                e -= 16;
@@ -554,8 +561,9 @@ static NOINLINE int inflate_codes(STATE_PARAM_ONLY)
                        e = t->e;
                        if (e > 16)
                                do {
-                                       if (e == 99)
+                                       if (e == 99) {
                                                abort_unzip(PASS_STATE_ONLY);
+                                       }
                                        bb >>= t->b;
                                        k -= t->b;
                                        e -= 16;
@@ -821,8 +829,9 @@ static int inflate_block(STATE_PARAM smallint *e)
 
                b_dynamic >>= 4;
                k_dynamic -= 4;
-               if (nl > 286 || nd > 30)
+               if (nl > 286 || nd > 30) {
                        abort_unzip(PASS_STATE_ONLY);   /* bad lengths */
+               }
 
                /* read in bit-length-code lengths */
                for (j = 0; j < nb; j++) {
@@ -903,12 +912,14 @@ static int inflate_block(STATE_PARAM smallint *e)
                bl = lbits;
 
                i = huft_build(ll, nl, 257, cplens, cplext, &inflate_codes_tl, &bl);
-               if (i != 0)
+               if (i != 0) {
                        abort_unzip(PASS_STATE_ONLY);
+               }
                bd = dbits;
                i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &inflate_codes_td, &bd);
-               if (i != 0)
+               if (i != 0) {
                        abort_unzip(PASS_STATE_ONLY);
+               }
 
                /* set up data for inflate_codes() */
                inflate_codes_setup(PASS_STATE bl, bd);
@@ -996,6 +1007,7 @@ inflate_unzip_internal(STATE_PARAM transformer_state_t *xstate)
        error_msg = "corrupted data";
        if (setjmp(error_jmp)) {
                /* Error from deep inside zip machinery */
+               bb_error_msg(error_msg);
                n = -1;
                goto ret;
        }
@@ -1189,7 +1201,7 @@ unpack_gz_stream(transformer_state_t *xstate)
        if (check_signature16(xstate, GZIP_MAGIC))
                return -1;
 #else
-       if (xstate->check_signature) {
+       if (!xstate->signature_skipped) {
                uint16_t magic2;
 
                if (full_read(xstate->src_fd, &magic2, 2) != 2) {
@@ -1198,7 +1210,7 @@ unpack_gz_stream(transformer_state_t *xstate)
                        return -1;
                }
                if (magic2 == COMPRESS_MAGIC) {
-                       xstate->check_signature = 0;
+                       xstate->signature_skipped = 2;
                        return unpack_Z_stream(xstate);
                }
                if (magic2 != GZIP_MAGIC)