Ignore:
Timestamp:
Nov 4, 2007, 3:16:40 AM (17 years ago)
Author:
Bruno Cornec
Message:

Update to busybox 1.7.2

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.5/mindi-busybox/archival/libunarchive/decompress_unzip.c

    r821 r1765  
    3030 *
    3131 * See the file algorithm.doc for the compression algorithms and file formats.
    32  * 
     32 *
    3333 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
    3434 */
    3535
    3636#include "libbb.h"
    37 #include <sys/wait.h>
    38 #include <signal.h>
    3937#include "unarchive.h"
    4038
     
    4846} huft_t;
    4947
    50 static int gunzip_src_fd;
    51 unsigned int gunzip_bytes_out;  /* number of output bytes */
    52 static unsigned int gunzip_outbuf_count;    /* bytes in output buffer */
    53 
    54 /* gunzip_window size--must be a power of two, and
    55  *  at least 32K for zip's deflate method */
    56 enum { gunzip_wsize = 0x8000 };
    57 static unsigned char *gunzip_window;
    58 
    59 static uint32_t *gunzip_crc_table;
    60 uint32_t gunzip_crc;
    61 
    62 /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
    63 #define BMAX 16 /* maximum bit length of any code (16 for explode) */
    64 #define N_MAX 288   /* maximum number of codes in any set */
    65 
    66 /* bitbuffer */
    67 static unsigned int gunzip_bb;  /* bit buffer */
    68 static unsigned char gunzip_bk; /* bits in bit buffer */
    69 
    70 /* These control the size of the bytebuffer */
    71 static unsigned int bytebuffer_max = 0x8000;
    72 static unsigned char *bytebuffer = NULL;
    73 static unsigned int bytebuffer_offset = 0;
    74 static unsigned int bytebuffer_size = 0;
    75 
    76 static const unsigned short mask_bits[] = {
     48enum {
     49    /* gunzip_window size--must be a power of two, and
     50     *  at least 32K for zip's deflate method */
     51    GUNZIP_WSIZE = 0x8000,
     52    /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
     53    BMAX = 16,  /* maximum bit length of any code (16 for explode) */
     54    N_MAX = 288,    /* maximum number of codes in any set */
     55};
     56
     57
     58/* This is somewhat complex-looking arrangement, but it allows
     59 * to place decompressor state either in bss or in
     60 * malloc'ed space simply by changing #defines below.
     61 * Sizes on i386:
     62 * text    data     bss     dec     hex
     63 * 5256       0     108    5364    14f4 - bss
     64 * 4915       0       0    4915    1333 - malloc
     65 */
     66#define STATE_IN_BSS 0
     67#define STATE_IN_MALLOC 1
     68
     69
     70typedef struct state_t {
     71    off_t gunzip_bytes_out; /* number of output bytes */
     72    uint32_t gunzip_crc;
     73
     74    int gunzip_src_fd;
     75    unsigned gunzip_outbuf_count; /* bytes in output buffer */
     76
     77    unsigned char *gunzip_window;
     78
     79    uint32_t *gunzip_crc_table;
     80
     81    /* bitbuffer */
     82    unsigned gunzip_bb; /* bit buffer */
     83    unsigned char gunzip_bk; /* bits in bit buffer */
     84
     85    /* These control the size of the STATE()bytebuffer */
     86    unsigned bytebuffer_max;
     87    unsigned char *bytebuffer;
     88    unsigned bytebuffer_offset;
     89    unsigned bytebuffer_size;
     90
     91    /* private data of inflate_codes() */
     92    unsigned inflate_codes_ml; /* masks for bl and bd bits */
     93    unsigned inflate_codes_md; /* masks for bl and bd bits */
     94    unsigned inflate_codes_bb; /* bit buffer */
     95    unsigned inflate_codes_k; /* number of bits in bit buffer */
     96    unsigned inflate_codes_w; /* current gunzip_window position */
     97    huft_t *inflate_codes_tl;
     98    huft_t *inflate_codes_td;
     99    unsigned inflate_codes_bl;
     100    unsigned inflate_codes_bd;
     101    unsigned inflate_codes_nn; /* length and index for copy */
     102    unsigned inflate_codes_dd;
     103    smallint resume_copy;
     104
     105    /* private data of inflate_get_next_window() */
     106    smallint method; /* Method == -1 for stored, -2 for codes */
     107    smallint need_another_block;
     108    smallint end_reached;
     109
     110    /* private data of inflate_stored() */
     111    unsigned inflate_stored_n;
     112    unsigned inflate_stored_b;
     113    unsigned inflate_stored_k;
     114    unsigned inflate_stored_w;
     115} state_t;
     116#define gunzip_bytes_out    (S()gunzip_bytes_out   )
     117#define gunzip_crc          (S()gunzip_crc         )
     118#define gunzip_src_fd       (S()gunzip_src_fd      )
     119#define gunzip_outbuf_count (S()gunzip_outbuf_count)
     120#define gunzip_window       (S()gunzip_window      )
     121#define gunzip_crc_table    (S()gunzip_crc_table   )
     122#define gunzip_bb           (S()gunzip_bb          )
     123#define gunzip_bk           (S()gunzip_bk          )
     124#define bytebuffer_max      (S()bytebuffer_max     )
     125#define bytebuffer          (S()bytebuffer         )
     126#define bytebuffer_offset   (S()bytebuffer_offset  )
     127#define bytebuffer_size     (S()bytebuffer_size    )
     128#define inflate_codes_ml    (S()inflate_codes_ml   )
     129#define inflate_codes_md    (S()inflate_codes_md   )
     130#define inflate_codes_bb    (S()inflate_codes_bb   )
     131#define inflate_codes_k     (S()inflate_codes_k    )
     132#define inflate_codes_w     (S()inflate_codes_w    )
     133#define inflate_codes_tl    (S()inflate_codes_tl   )
     134#define inflate_codes_td    (S()inflate_codes_td   )
     135#define inflate_codes_bl    (S()inflate_codes_bl   )
     136#define inflate_codes_bd    (S()inflate_codes_bd   )
     137#define inflate_codes_nn    (S()inflate_codes_nn   )
     138#define inflate_codes_dd    (S()inflate_codes_dd   )
     139#define resume_copy         (S()resume_copy        )
     140#define method              (S()method             )
     141#define need_another_block  (S()need_another_block )
     142#define end_reached         (S()end_reached        )
     143#define inflate_stored_n    (S()inflate_stored_n   )
     144#define inflate_stored_b    (S()inflate_stored_b   )
     145#define inflate_stored_k    (S()inflate_stored_k   )
     146#define inflate_stored_w    (S()inflate_stored_w   )
     147#define INIT_STATE ({ bytebuffer_size = 0; method = -1; need_another_block = 1; })
     148
     149
     150/* This is generic part */
     151#if STATE_IN_BSS /* Use global data segment */
     152#define DECLARE_STATE /*nothing*/
     153#define ALLOC_STATE (init_state())
     154#define DEALLOC_STATE ((void)0)
     155#define S() state.
     156#define PASS_STATE /*nothing*/
     157#define PASS_STATE_ONLY /*nothing*/
     158#define STATE_PARAM /*nothing*/
     159#define STATE_PARAM_ONLY void
     160static state_t state;
     161static void init_state(void)
     162{
     163    INIT_STATE;
     164}
     165#endif
     166
     167#if STATE_IN_MALLOC /* Use malloc space */
     168#define DECLARE_STATE state_t *state
     169#define ALLOC_STATE (state = alloc_state())
     170#define DEALLOC_STATE free(state)
     171#define S() state->
     172#define PASS_STATE state,
     173#define PASS_STATE_ONLY state
     174#define STATE_PARAM state_t *state,
     175#define STATE_PARAM_ONLY state_t *state
     176static state_t* alloc_state(void)
     177{
     178    state_t* state = xzalloc(sizeof(*state));
     179    INIT_STATE;
     180    return state;
     181}
     182#endif
     183
     184
     185static const unsigned short mask_bits[] ALIGN2 = {
    77186    0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
    78187    0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
     
    80189
    81190/* Copy lengths for literal codes 257..285 */
    82 static const unsigned short cplens[] = {
     191static const unsigned short cplens[] ALIGN2 = {
    83192    3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59,
    84193    67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
     
    87196/* note: see note #13 above about the 258 in this list. */
    88197/* Extra bits for literal codes 257..285 */
    89 static const unsigned char cplext[] = {
     198static const unsigned char cplext[] ALIGN1 = {
    90199    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,
    91200    5, 5, 5, 0, 99, 99
    92 };                      /* 99==invalid */
     201}; /* 99 == invalid */
    93202
    94203/* Copy offsets for distance codes 0..29 */
    95 static const unsigned short cpdist[] = {
     204static const unsigned short cpdist[] ALIGN2 = {
    96205    1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513,
    97206    769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577
     
    99208
    100209/* Extra bits for distance codes */
    101 static const unsigned char cpdext[] = {
     210static const unsigned char cpdext[] ALIGN1 = {
    102211    0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10,
    103212    11, 11, 12, 12, 13, 13
     
    106215/* Tables for deflate from PKZIP's appnote.txt. */
    107216/* Order of the bit length code lengths */
    108 static const unsigned char border[] = {
     217static const unsigned char border[] ALIGN1 = {
    109218    16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
    110219};
    111220
    112 static unsigned int fill_bitbuffer(unsigned int bitbuffer, unsigned int *current, const unsigned int required)
     221static unsigned fill_bitbuffer(STATE_PARAM unsigned bitbuffer, unsigned *current, const unsigned required)
    113222{
    114223    while (*current < required) {
     
    117226             * to the front of the bytebuffer, leave 4 bytes free at end of tail
    118227             * so we can easily top up buffer in check_trailer_gzip() */
    119             if (!(bytebuffer_size = bb_xread(gunzip_src_fd, &bytebuffer[4], bytebuffer_max - 8))) {
     228            bytebuffer_size = safe_read(gunzip_src_fd, &bytebuffer[4], bytebuffer_max - 8);
     229            if (1 > bytebuffer_size)
     230//shouldn't we propagate error?
    120231                bb_error_msg_and_die("unexpected end of file");
    121             }
    122232            bytebuffer_size += 4;
    123233            bytebuffer_offset = 4;
    124234        }
    125         bitbuffer |= ((unsigned int) bytebuffer[bytebuffer_offset]) << *current;
     235        bitbuffer |= ((unsigned) bytebuffer[bytebuffer_offset]) << *current;
    126236        bytebuffer_offset++;
    127237        *current += 8;
    128238    }
    129     return(bitbuffer);
     239    return bitbuffer;
    130240}
    131241
     
    136246 * t: table to free
    137247 */
    138 static int huft_free(huft_t * t)
    139 {
    140     huft_t *p;
     248static void huft_free(huft_t * p)
     249{
    141250    huft_t *q;
    142251
    143252    /* Go through linked list, freeing from the malloced (t[-1]) address. */
    144     p = t;
    145     while (p != (huft_t *) NULL) {
     253    while (p) {
    146254        q = (--p)->v.t;
    147         free((char *) p);
     255        free(p);
    148256        p = q;
    149257    }
    150     return 0;
    151258}
    152259
     
    165272 * m:   maximum lookup bits, returns actual
    166273 */
    167 static
    168 int huft_build(unsigned int *b, const unsigned int n,
    169                const unsigned int s, const unsigned short *d,
    170                const unsigned char *e, huft_t ** t, unsigned int *m)
     274static int huft_build(unsigned *b, const unsigned n,
     275               const unsigned s, const unsigned short *d,
     276               const unsigned char *e, huft_t ** t, unsigned *m)
    171277{
    172278    unsigned a;             /* counter for codes of length k */
     
    195301
    196302    /* Generate counts for each bit length */
    197     memset((void *)c, 0, sizeof(c));
     303    memset(c, 0, sizeof(c));
    198304    p = b;
    199305    i = n;
     
    203309    } while (--i);
    204310    if (c[0] == n) { /* null input--all zero length codes */
    205         *t = (huft_t *) NULL;
     311        *t = NULL;
    206312        *m = 0;
    207313        return 2;
     
    217323    /* Adjust last length count to fill out codes, if needed */
    218324    for (y = 1 << j; j < i; j++, y <<= 1) {
    219         if ((y -= c[j]) < 0) {
     325        y -= c[j];
     326        if (y < 0) {
    220327            return 2; /* bad input: more codes than bits */
    221328        }
    222329    }
    223     if ((y -= c[i]) < 0) {
     330    y -= c[i];
     331    if (y < 0) {
    224332        return 2;
    225333    }
     
    231339    xp = x + 2;
    232340    while (--i) { /* note that i == g from above */
    233         *xp++ = (j += *p++);
     341        j += *p++;
     342        *xp++ = j;
    234343    }
    235344
     
    238347    i = 0;
    239348    do {
    240         if ((j = *p++) != 0) {
     349        j = *p++;
     350        if (j != 0) {
    241351            v[x[j]++] = i;
    242352        }
     
    248358    htl = -1;               /* no tables yet--level -1 */
    249359    w = ws[0] = 0;          /* bits decoded */
    250     u[0] = (huft_t *) NULL; /* just to keep compilers happy */
    251     q = (huft_t *) NULL;    /* ditto */
     360    u[0] = NULL;    /* just to keep compilers happy */
     361    q = NULL;   /* ditto */
    252362    z = 0;                  /* ditto */
    253363
     
    262372
    263373                /* compute minimum size table less than or equal to *m bits */
    264                 z = (z = g - w) > *m ? *m : z; /* upper limit on table size */
    265                 if ((f = 1 << (j = k - w)) > a + 1) { /* try a k-w bit table */
     374                z = g - w;
     375                z = z > *m ? *m : z; /* upper limit on table size */
     376                j = k - w;
     377                f = 1 << j;
     378                if (f > a + 1) { /* try a k-w bit table */
    266379                    /* too few codes for k-w bit table */
    267380                    f -= a + 1; /* deduct codes from patterns left */
    268381                    xp = c + k;
    269382                    while (++j < z) { /* try smaller tables up to z bits */
    270                         if ((f <<= 1) <= *++xp) {
     383                        f <<= 1;
     384                        if (f <= *++xp) {
    271385                            break; /* enough codes to use up j bits */
    272386                        }
     
    279393
    280394                /* allocate and link in new table */
    281                 q = (huft_t *) xzalloc((z + 1) * sizeof(huft_t));
     395                q = xzalloc((z + 1) * sizeof(huft_t));
    282396                *t = q + 1; /* link to list for huft_free() */
    283397                t = &(q->v.t);
     
    333447}
    334448
     449
    335450/*
    336451 * inflate (decompress) the codes in a deflated (compressed) block.
     
    340455 * bl, bd: number of bits decoded by tl[] and td[]
    341456 */
    342 static int inflate_codes(huft_t * my_tl, huft_t * my_td, const unsigned int my_bl, const unsigned int my_bd, int setup)
    343 {
    344     static unsigned int e;  /* table entry flag/number of extra bits */
    345     static unsigned int n, d;   /* length and index for copy */
    346     static unsigned int w;  /* current gunzip_window position */
    347     static huft_t *t;           /* pointer to table entry */
    348     static unsigned int ml, md; /* masks for bl and bd bits */
    349     static unsigned int b;  /* bit buffer */
    350     static unsigned int k;          /* number of bits in bit buffer */
    351     static huft_t *tl, *td;
    352     static unsigned int bl, bd;
    353     static int resumeCopy = 0;
    354 
    355     if (setup) { // 1st time we are called, copy in variables
    356         tl = my_tl;
    357         td = my_td;
    358         bl = my_bl;
    359         bd = my_bd;
    360         /* make local copies of globals */
    361         b = gunzip_bb;              /* initialize bit buffer */
    362         k = gunzip_bk;
    363         w = gunzip_outbuf_count;            /* initialize gunzip_window position */
    364 
    365         /* inflate the coded data */
    366         ml = mask_bits[bl]; /* precompute masks for speed */
    367         md = mask_bits[bd];
    368         return 0; // Don't actually do anything the first time
    369     }
    370 
    371     if (resumeCopy) goto do_copy;
     457/* called once from inflate_block */
     458
     459/* map formerly local static variables to globals */
     460#define ml inflate_codes_ml
     461#define md inflate_codes_md
     462#define bb inflate_codes_bb
     463#define k  inflate_codes_k
     464#define w  inflate_codes_w
     465#define tl inflate_codes_tl
     466#define td inflate_codes_td
     467#define bl inflate_codes_bl
     468#define bd inflate_codes_bd
     469#define nn inflate_codes_nn
     470#define dd inflate_codes_dd
     471static void inflate_codes_setup(STATE_PARAM huft_t * my_tl, huft_t * my_td, const unsigned my_bl, const unsigned my_bd)
     472{
     473    tl = my_tl;
     474    td = my_td;
     475    bl = my_bl;
     476    bd = my_bd;
     477    /* make local copies of globals */
     478    bb = gunzip_bb;         /* initialize bit buffer */
     479    k = gunzip_bk;
     480    w = gunzip_outbuf_count;    /* initialize gunzip_window position */
     481    /* inflate the coded data */
     482    ml = mask_bits[bl];     /* precompute masks for speed */
     483    md = mask_bits[bd];
     484}
     485/* called once from inflate_get_next_window */
     486static int inflate_codes(STATE_PARAM_ONLY)
     487{
     488    unsigned e; /* table entry flag/number of extra bits */
     489    huft_t *t;  /* pointer to table entry */
     490
     491    if (resume_copy) goto do_copy;
    372492
    373493    while (1) {         /* do until end of block */
    374         b = fill_bitbuffer(b, &k, bl);
    375         if ((e = (t = tl + ((unsigned) b & ml))->e) > 16)
     494        bb = fill_bitbuffer(PASS_STATE bb, &k, bl);
     495        t = tl + ((unsigned) bb & ml);
     496        e = t->e;
     497        if (e > 16)
    376498            do {
    377499                if (e == 99) {
     500//shouldn't we propagate error?
    378501                    bb_error_msg_and_die("inflate_codes error 1");
    379502                }
    380                 b >>= t->b;
     503                bb >>= t->b;
    381504                k -= t->b;
    382505                e -= 16;
    383                 b = fill_bitbuffer(b, &k, e);
    384             } while ((e =
    385                       (t = t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16);
    386         b >>= t->b;
     506                bb = fill_bitbuffer(PASS_STATE bb, &k, e);
     507                t = t->v.t + ((unsigned) bb & mask_bits[e]);
     508                e = t->e;
     509            } while (e > 16);
     510        bb >>= t->b;
    387511        k -= t->b;
    388512        if (e == 16) {  /* then it's a literal */
    389513            gunzip_window[w++] = (unsigned char) t->v.n;
    390             if (w == gunzip_wsize) {
    391                 gunzip_outbuf_count = (w);
     514            if (w == GUNZIP_WSIZE) {
     515                gunzip_outbuf_count = w;
    392516                //flush_gunzip_window();
    393517                w = 0;
     
    395519            }
    396520        } else {        /* it's an EOB or a length */
    397 
    398521            /* exit if end of block */
    399522            if (e == 15) {
     
    402525
    403526            /* get length of block to copy */
    404             b = fill_bitbuffer(b, &k, e);
    405             n = t->v.n + ((unsigned) b & mask_bits[e]);
    406             b >>= e;
     527            bb = fill_bitbuffer(PASS_STATE bb, &k, e);
     528            nn = t->v.n + ((unsigned) bb & mask_bits[e]);
     529            bb >>= e;
    407530            k -= e;
    408531
    409532            /* decode distance of block to copy */
    410             b = fill_bitbuffer(b, &k, bd);
    411             if ((e = (t = td + ((unsigned) b & md))->e) > 16)
     533            bb = fill_bitbuffer(PASS_STATE bb, &k, bd);
     534            t = td + ((unsigned) bb & md);
     535            e = t->e;
     536            if (e > 16)
    412537                do {
    413538                    if (e == 99)
     539//shouldn't we propagate error?
    414540                        bb_error_msg_and_die("inflate_codes error 2");
    415                     b >>= t->b;
     541                    bb >>= t->b;
    416542                    k -= t->b;
    417543                    e -= 16;
    418                     b = fill_bitbuffer(b, &k, e);
    419                 } while ((e =
    420                           (t =
    421                            t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16);
    422             b >>= t->b;
     544                    bb = fill_bitbuffer(PASS_STATE bb, &k, e);
     545                    t = t->v.t + ((unsigned) bb & mask_bits[e]);
     546                    e = t->e;
     547                } while (e > 16);
     548            bb >>= t->b;
    423549            k -= t->b;
    424             b = fill_bitbuffer(b, &k, e);
    425             d = w - t->v.n - ((unsigned) b & mask_bits[e]);
    426             b >>= e;
     550            bb = fill_bitbuffer(PASS_STATE bb, &k, e);
     551            dd = w - t->v.n - ((unsigned) bb & mask_bits[e]);
     552            bb >>= e;
    427553            k -= e;
    428554
    429555            /* do the copy */
    430 do_copy:        do {
    431                 n -= (e =
    432                       (e =
    433                        gunzip_wsize - ((d &= gunzip_wsize - 1) > w ? d : w)) > n ? n : e);
    434                /* copy to new buffer to prevent possible overwrite */
    435                 if (w - d >= e) {   /* (this test assumes unsigned comparison) */
    436                     memcpy(gunzip_window + w, gunzip_window + d, e);
     556 do_copy:
     557            do {
     558                /* Was: nn -= (e = (e = GUNZIP_WSIZE - ((dd &= GUNZIP_WSIZE - 1) > w ? dd : w)) > nn ? nn : e); */
     559                /* Who wrote THAT?? rewritten as: */
     560                dd &= GUNZIP_WSIZE - 1;
     561                e = GUNZIP_WSIZE - (dd > w ? dd : w);
     562                if (e > nn) e = nn;
     563                nn -= e;
     564
     565                /* copy to new buffer to prevent possible overwrite */
     566                if (w - dd >= e) {  /* (this test assumes unsigned comparison) */
     567                    memcpy(gunzip_window + w, gunzip_window + dd, e);
    437568                    w += e;
    438                     d += e;
     569                    dd += e;
    439570                } else {
    440                    /* do it slow to avoid memcpy() overlap */
    441                    /* !NOMEMCPY */
     571                    /* do it slow to avoid memcpy() overlap */
     572                    /* !NOMEMCPY */
    442573                    do {
    443                         gunzip_window[w++] = gunzip_window[d++];
     574                        gunzip_window[w++] = gunzip_window[dd++];
    444575                    } while (--e);
    445576                }
    446                 if (w == gunzip_wsize) {
    447                     gunzip_outbuf_count = (w);
    448                     if (n) resumeCopy = 1;
    449                     else resumeCopy = 0;
     577                if (w == GUNZIP_WSIZE) {
     578                    gunzip_outbuf_count = w;
     579                    resume_copy = (nn != 0);
    450580                    //flush_gunzip_window();
    451581                    w = 0;
    452582                    return 1;
    453583                }
    454             } while (n);
    455             resumeCopy = 0;
     584            } while (nn);
     585            resume_copy = 0;
    456586        }
    457587    }
    458588
    459589    /* restore the globals from the locals */
    460     gunzip_outbuf_count = w;            /* restore global gunzip_window pointer */
    461     gunzip_bb = b            /* restore global bit buffer */
     590    gunzip_outbuf_count = w;    /* restore global gunzip_window pointer */
     591    gunzip_bb = bb;         /* restore global bit buffer */
    462592    gunzip_bk = k;
    463593
     
    470600    return 0;
    471601}
    472 
    473 static int inflate_stored(int my_n, int my_b_stored, int my_k_stored, int setup)
    474 {
    475     static unsigned int n, b_stored, k_stored, w;
    476     if (setup) {
    477         n = my_n;
    478         b_stored = my_b_stored;
    479         k_stored = my_k_stored;
    480         w = gunzip_outbuf_count;        /* initialize gunzip_window position */
    481         return 0; // Don't do anything first time
    482     }
    483 
     602#undef ml
     603#undef md
     604#undef bb
     605#undef k
     606#undef w
     607#undef tl
     608#undef td
     609#undef bl
     610#undef bd
     611#undef nn
     612#undef dd
     613
     614
     615/* called once from inflate_block */
     616static void inflate_stored_setup(STATE_PARAM int my_n, int my_b, int my_k)
     617{
     618    inflate_stored_n = my_n;
     619    inflate_stored_b = my_b;
     620    inflate_stored_k = my_k;
     621    /* initialize gunzip_window position */
     622    inflate_stored_w = gunzip_outbuf_count;
     623}
     624/* called once from inflate_get_next_window */
     625static int inflate_stored(STATE_PARAM_ONLY)
     626{
    484627    /* read and output the compressed data */
    485     while (n--) {
    486         b_stored = fill_bitbuffer(b_stored, &k_stored, 8);
    487         gunzip_window[w++] = (unsigned char) b_stored;
    488         if (w == gunzip_wsize) {
    489             gunzip_outbuf_count = (w);
     628    while (inflate_stored_n--) {
     629        inflate_stored_b = fill_bitbuffer(PASS_STATE inflate_stored_b, &inflate_stored_k, 8);
     630        gunzip_window[inflate_stored_w++] = (unsigned char) inflate_stored_b;
     631        if (inflate_stored_w == GUNZIP_WSIZE) {
     632            gunzip_outbuf_count = inflate_stored_w;
    490633            //flush_gunzip_window();
    491             w = 0;
    492             b_stored >>= 8;
    493             k_stored -= 8;
     634            inflate_stored_w = 0;
     635            inflate_stored_b >>= 8;
     636            inflate_stored_k -= 8;
    494637            return 1; // We have a block
    495638        }
    496         b_stored >>= 8;
    497         k_stored -= 8;
     639        inflate_stored_b >>= 8;
     640        inflate_stored_k -= 8;
    498641    }
    499642
    500643    /* restore the globals from the locals */
    501     gunzip_outbuf_count = w;        /* restore global gunzip_window pointer */
    502     gunzip_bb = b_stored;   /* restore global bit buffer */
    503     gunzip_bk = k_stored;
     644    gunzip_outbuf_count = inflate_stored_w;     /* restore global gunzip_window pointer */
     645    gunzip_bb = inflate_stored_b;   /* restore global bit buffer */
     646    gunzip_bk = inflate_stored_k;
    504647    return 0; // Finished
    505648}
     649
    506650
    507651/*
     
    511655 * GLOBAL VARIABLES: bb, kk,
    512656 */
    513  // Return values: -1 = inflate_stored, -2 = inflate_codes
    514 static int inflate_block(int *e)
     657/* Return values: -1 = inflate_stored, -2 = inflate_codes */
     658/* One callsite in inflate_get_next_window */
     659static int inflate_block(STATE_PARAM smallint *e)
    515660{
    516661    unsigned t;         /* block type */
    517     register unsigned int b;    /* bit buffer */
    518     unsigned int k; /* number of bits in bit buffer */
     662    unsigned b; /* bit buffer */
     663    unsigned k; /* number of bits in bit buffer */
    519664
    520665    /* make local bit buffer */
     
    524669
    525670    /* read in last block bit */
    526     b = fill_bitbuffer(b, &k, 1);
    527     *e = (int) b & 1;
     671    b = fill_bitbuffer(PASS_STATE b, &k, 1);
     672    *e = b & 1;
    528673    b >>= 1;
    529674    k -= 1;
    530675
    531676    /* read in block type */
    532     b = fill_bitbuffer(b, &k, 2);
     677    b = fill_bitbuffer(PASS_STATE b, &k, 2);
    533678    t = (unsigned) b & 3;
    534679    b >>= 2;
     
    543688    case 0:         /* Inflate stored */
    544689    {
    545         unsigned int n; /* number of bytes in block */
    546         unsigned int b_stored;  /* bit buffer */
    547         unsigned int k_stored;  /* number of bits in bit buffer */
     690        unsigned n; /* number of bytes in block */
     691        unsigned b_stored;  /* bit buffer */
     692        unsigned k_stored;  /* number of bits in bit buffer */
    548693
    549694        /* make local copies of globals */
     
    557702
    558703        /* get the length and its complement */
    559         b_stored = fill_bitbuffer(b_stored, &k_stored, 16);
     704        b_stored = fill_bitbuffer(PASS_STATE b_stored, &k_stored, 16);
    560705        n = ((unsigned) b_stored & 0xffff);
    561706        b_stored >>= 16;
    562707        k_stored -= 16;
    563708
    564         b_stored = fill_bitbuffer(b_stored, &k_stored, 16);
     709        b_stored = fill_bitbuffer(PASS_STATE b_stored, &k_stored, 16);
    565710        if (n != (unsigned) ((~b_stored) & 0xffff)) {
    566711            return 1;   /* error in compressed data */
     
    569714        k_stored -= 16;
    570715
    571         inflate_stored(n, b_stored, k_stored, 1); // Setup inflate_stored
     716        inflate_stored_setup(PASS_STATE n, b_stored, k_stored); // Setup inflate_stored
     717
    572718        return -1;
    573719    }
    574     case 1:         /* Inflate fixed
    575                            * decompress an inflated type 1 (fixed Huffman codes) block.  We should
    576                            * either replace this with a custom decoder, or at least precompute the
    577                            * Huffman tables.
    578                         */
     720    case 1:
     721    /* Inflate fixed
     722     * decompress an inflated type 1 (fixed Huffman codes) block.  We should
     723     * either replace this with a custom decoder, or at least precompute the
     724     * Huffman tables. */
    579725    {
    580726        int i;          /* temporary variable */
    581727        huft_t *tl;     /* literal/length code table */
    582728        huft_t *td;     /* distance code table */
    583         unsigned int bl;            /* lookup bits for tl */
    584         unsigned int bd;            /* lookup bits for td */
    585         unsigned int l[288];    /* length list for huft_build */
     729        unsigned bl;            /* lookup bits for tl */
     730        unsigned bd;            /* lookup bits for td */
     731        unsigned l[288];    /* length list for huft_build */
    586732
    587733        /* set up literal table */
     
    599745        }
    600746        bl = 7;
    601         if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0) {
     747        i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl);
     748        if (i != 0) {
    602749            return i;
    603750        }
     
    608755        }
    609756        bd = 5;
    610         if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1) {
     757        i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd);
     758        if (i > 1) {
    611759            huft_free(tl);
    612760            return i;
     
    614762
    615763        /* decompress until an end-of-block code */
    616         inflate_codes(tl, td, bl, bd, 1); // Setup inflate_codes
     764        inflate_codes_setup(PASS_STATE tl, td, bl, bd); // Setup inflate_codes
    617765
    618766        /* huft_free code moved into inflate_codes */
     
    627775        huft_t *tl;     /* literal/length code table */
    628776        huft_t *td;     /* distance code table */
    629         unsigned int i;         /* temporary variables */
    630         unsigned int j;
    631         unsigned int l;     /* last length */
    632         unsigned int m;     /* mask for bit lengths table */
    633         unsigned int n;     /* number of lengths to get */
    634         unsigned int bl;            /* lookup bits for tl */
    635         unsigned int bd;            /* lookup bits for td */
    636         unsigned int nb;    /* number of bit length codes */
    637         unsigned int nl;    /* number of literal/length codes */
    638         unsigned int nd;    /* number of distance codes */
    639 
    640         unsigned int ll[286 + 30];  /* literal/length and distance code lengths */
    641         unsigned int b_dynamic; /* bit buffer */
    642         unsigned int k_dynamic; /* number of bits in bit buffer */
     777        unsigned i;         /* temporary variables */
     778        unsigned j;
     779        unsigned l;     /* last length */
     780        unsigned m;     /* mask for bit lengths table */
     781        unsigned n;     /* number of lengths to get */
     782        unsigned bl;            /* lookup bits for tl */
     783        unsigned bd;            /* lookup bits for td */
     784        unsigned nb;    /* number of bit length codes */
     785        unsigned nl;    /* number of literal/length codes */
     786        unsigned nd;    /* number of distance codes */
     787
     788        unsigned ll[286 + 30];  /* literal/length and distance code lengths */
     789        unsigned b_dynamic; /* bit buffer */
     790        unsigned k_dynamic; /* number of bits in bit buffer */
    643791
    644792        /* make local bit buffer */
     
    647795
    648796        /* read in table lengths */
    649         b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 5);
    650         nl = 257 + ((unsigned int) b_dynamic & 0x1f);   /* number of literal/length codes */
     797        b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 5);
     798        nl = 257 + ((unsigned) b_dynamic & 0x1f);   /* number of literal/length codes */
    651799
    652800        b_dynamic >>= 5;
    653801        k_dynamic -= 5;
    654         b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 5);
    655         nd = 1 + ((unsigned int) b_dynamic & 0x1f); /* number of distance codes */
     802        b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 5);
     803        nd = 1 + ((unsigned) b_dynamic & 0x1f); /* number of distance codes */
    656804
    657805        b_dynamic >>= 5;
    658806        k_dynamic -= 5;
    659         b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 4);
    660         nb = 4 + ((unsigned int) b_dynamic & 0xf);  /* number of bit length codes */
     807        b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 4);
     808        nb = 4 + ((unsigned) b_dynamic & 0xf);  /* number of bit length codes */
    661809
    662810        b_dynamic >>= 4;
     
    668816        /* read in bit-length-code lengths */
    669817        for (j = 0; j < nb; j++) {
    670             b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 3);
    671             ll[border[j]] = (unsigned int) b_dynamic & 7;
     818            b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 3);
     819            ll[border[j]] = (unsigned) b_dynamic & 7;
    672820            b_dynamic >>= 3;
    673821            k_dynamic -= 3;
     
    691839        m = mask_bits[bl];
    692840        i = l = 0;
    693         while ((unsigned int) i < n) {
    694             b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, (unsigned int)bl);
    695             j = (td = tl + ((unsigned int) b_dynamic & m))->b;
     841        while ((unsigned) i < n) {
     842            b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, (unsigned)bl);
     843            j = (td = tl + ((unsigned) b_dynamic & m))->b;
    696844            b_dynamic >>= j;
    697845            k_dynamic -= j;
     
    700848                ll[i++] = l = j;    /* save last length in l */
    701849            } else if (j == 16) {   /* repeat last length 3 to 6 times */
    702                 b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 2);
    703                 j = 3 + ((unsigned int) b_dynamic & 3);
     850                b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 2);
     851                j = 3 + ((unsigned) b_dynamic & 3);
    704852                b_dynamic >>= 2;
    705853                k_dynamic -= 2;
    706                 if ((unsigned int) i + j > n) {
     854                if ((unsigned) i + j > n) {
    707855                    return 1;
    708856                }
     
    711859                }
    712860            } else if (j == 17) {   /* 3 to 10 zero length codes */
    713                 b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 3);
    714                 j = 3 + ((unsigned int) b_dynamic & 7);
     861                b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 3);
     862                j = 3 + ((unsigned) b_dynamic & 7);
    715863                b_dynamic >>= 3;
    716864                k_dynamic -= 3;
    717                 if ((unsigned int) i + j > n) {
     865                if ((unsigned) i + j > n) {
    718866                    return 1;
    719867                }
     
    723871                l = 0;
    724872            } else {    /* j == 18: 11 to 138 zero length codes */
    725                 b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 7);
    726                 j = 11 + ((unsigned int) b_dynamic & 0x7f);
     873                b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 7);
     874                j = 11 + ((unsigned) b_dynamic & 0x7f);
    727875                b_dynamic >>= 7;
    728876                k_dynamic -= 7;
    729                 if ((unsigned int) i + j > n) {
     877                if ((unsigned) i + j > n) {
    730878                    return 1;
    731879                }
     
    747895        bl = lbits;
    748896
    749         if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0) {
     897        i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl);
     898        if (i != 0) {
    750899            if (i == 1) {
    751                 bb_error_msg_and_die("Incomplete literal tree");
    752                 huft_free(tl);
     900//shouldn't we propagate error?
     901                bb_error_msg_and_die("incomplete literal tree");
     902                /* huft_free(tl); */
    753903            }
    754904            return i;   /* incomplete code set */
     
    756906
    757907        bd = dbits;
    758         if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0) {
     908        i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd);
     909        if (i != 0) {
    759910            if (i == 1) {
     911//shouldn't we propagate error?
    760912                bb_error_msg_and_die("incomplete distance tree");
    761                 huft_free(td);
     913                /* huft_free(td); */
    762914            }
    763915            huft_free(tl);
     
    766918
    767919        /* decompress until an end-of-block code */
    768         inflate_codes(tl, td, bl, bd, 1); // Setup inflate_codes
     920        inflate_codes_setup(PASS_STATE tl, td, bl, bd); // Setup inflate_codes
    769921
    770922        /* huft_free code moved into inflate_codes */
     
    774926    default:
    775927        /* bad block type */
    776         bb_error_msg_and_die("bad block type %d\n", t);
    777     }
    778 }
    779 
    780 static void calculate_gunzip_crc(void)
     928//shouldn't we propagate error?
     929        bb_error_msg_and_die("bad block type %d", t);
     930    }
     931}
     932
     933/* Two callsites, both in inflate_get_next_window */
     934static void calculate_gunzip_crc(STATE_PARAM_ONLY)
    781935{
    782936    int n;
     
    787941}
    788942
    789 static int inflate_get_next_window(void)
    790 {
    791     static int method = -1; // Method == -1 for stored, -2 for codes
    792     static int e = 0;
    793     static int needAnotherBlock = 1;
    794 
     943/* One callsite in inflate_unzip_internal */
     944static int inflate_get_next_window(STATE_PARAM_ONLY)
     945{
    795946    gunzip_outbuf_count = 0;
    796947
    797     while(1) {
     948    while (1) {
    798949        int ret;
    799950
    800         if (needAnotherBlock) {
    801             if(e) {
    802                 calculate_gunzip_crc();
    803                 e = 0;
    804                 needAnotherBlock = 1;
    805                 return 0;
    806             } // Last block
    807             method = inflate_block(&e);
    808             needAnotherBlock = 0;
     951        if (need_another_block) {
     952            if (end_reached) {
     953                calculate_gunzip_crc(PASS_STATE_ONLY);
     954                end_reached = 0;
     955                need_another_block = 1;
     956                return 0; /* Last block */
     957            }
     958            method = inflate_block(PASS_STATE &end_reached);
     959            need_another_block = 0;
    809960        }
    810961
    811962        switch (method) {
    812             case -1:    ret = inflate_stored(0,0,0,0);
    813                     break;
    814             case -2:    ret = inflate_codes(0,0,0,0,0);
    815                     break;
    816             default:    bb_error_msg_and_die("inflate error %d", method);
     963        case -1:
     964            ret = inflate_stored(PASS_STATE_ONLY);
     965            break;
     966        case -2:
     967            ret = inflate_codes(PASS_STATE_ONLY);
     968            break;
     969        default:
     970//shouldn't we propagate error?
     971            bb_error_msg_and_die("inflate error %d", method);
    817972        }
    818973
    819974        if (ret == 1) {
    820             calculate_gunzip_crc();
     975            calculate_gunzip_crc(PASS_STATE_ONLY);
    821976            return 1; // More data left
    822         } else needAnotherBlock = 1; // End of that block
     977        }
     978        need_another_block = 1; // End of that block
    823979    }
    824980    /* Doesnt get here */
    825981}
    826982
    827 /* Initialise bytebuffer, be careful not to overfill the buffer */
    828 void inflate_init(unsigned int bufsize)
    829 {
    830     /* Set the bytebuffer size, default is same as gunzip_wsize */
    831     bytebuffer_max = bufsize + 8;
    832     bytebuffer_offset = 4;
    833     bytebuffer_size = 0;
    834 }
    835 
    836 void inflate_cleanup(void)
    837 {
    838     free(bytebuffer);
    839 }
    840 
    841 int inflate_unzip(int in, int out)
    842 {
     983
     984/* Called from unpack_gz_stream() and inflate_unzip() */
     985/* NB: bytebuffer is allocated here but freeing it is left to the caller! */
     986static USE_DESKTOP(long long) int
     987inflate_unzip_internal(STATE_PARAM int in, int out)
     988{
     989    USE_DESKTOP(long long) int n = 0;
    843990    ssize_t nwrote;
    844     typedef void (*sig_type) (int);
    845991
    846992    /* Allocate all global buffers (for DYN_ALLOC option) */
    847     gunzip_window = xmalloc(gunzip_wsize);
     993    gunzip_window = xmalloc(GUNZIP_WSIZE);
    848994    gunzip_outbuf_count = 0;
    849995    gunzip_bytes_out = 0;
     
    8551001
    8561002    /* Create the crc table */
    857     gunzip_crc_table = bb_crc32_filltable(0);
     1003    gunzip_crc_table = crc32_filltable(NULL, 0);
    8581004    gunzip_crc = ~0;
    859    
     1005
    8601006    /* Allocate space for buffer */
    8611007    bytebuffer = xmalloc(bytebuffer_max);
    8621008
    863     while(1) {
    864         int ret = inflate_get_next_window();
    865         nwrote = bb_full_write(out, gunzip_window, gunzip_outbuf_count);
    866         if (nwrote == -1) {
     1009    while (1) {
     1010        int r = inflate_get_next_window(PASS_STATE_ONLY);
     1011        nwrote = full_write(out, gunzip_window, gunzip_outbuf_count);
     1012        if (nwrote != gunzip_outbuf_count) {
    8671013            bb_perror_msg("write");
    868             return -1;
    869         }
    870         if (ret == 0) break;
    871     }
    872 
    873     /* Cleanup */
    874     free(gunzip_window);
    875     free(gunzip_crc_table);
     1014            n = -1;
     1015            goto ret;
     1016        }
     1017        USE_DESKTOP(n += nwrote;)
     1018        if (r == 0) break;
     1019    }
    8761020
    8771021    /* Store unused bytes in a global buffer so calling applets can access it */
     
    8841028        gunzip_bk -= 8;
    8851029    }
    886     return 0;
    887 }
    888 
    889 int inflate_gunzip(int in, int out)
     1030 ret:
     1031    /* Cleanup */
     1032    free(gunzip_window);
     1033    free(gunzip_crc_table);
     1034    return n;
     1035}
     1036
     1037
     1038USE_DESKTOP(long long) int
     1039inflate_unzip(inflate_unzip_result *res, unsigned bufsize, int in, int out)
     1040{
     1041    USE_DESKTOP(long long) int n;
     1042    DECLARE_STATE;
     1043
     1044    ALLOC_STATE;
     1045
     1046    bytebuffer_max = bufsize + 8;
     1047    bytebuffer_offset = 4;
     1048    n = inflate_unzip_internal(PASS_STATE in, out);
     1049
     1050    res->crc = gunzip_crc;
     1051    res->bytes_out = gunzip_bytes_out;
     1052    free(bytebuffer);
     1053    DEALLOC_STATE;
     1054    return n;
     1055}
     1056
     1057
     1058USE_DESKTOP(long long) int
     1059unpack_gz_stream(int in, int out)
    8901060{
    8911061    uint32_t stored_crc = 0;
    892     unsigned int count;
    893 
    894     inflate_unzip(in, out);
     1062    unsigned count;
     1063    USE_DESKTOP(long long) int n;
     1064    DECLARE_STATE;
     1065
     1066    ALLOC_STATE;
     1067
     1068    bytebuffer_max = 0x8000;
     1069    n = inflate_unzip_internal(PASS_STATE in, out);
     1070
     1071    if (n < 0) goto ret;
    8951072
    8961073    /* top up the input buffer with the rest of the trailer */
    8971074    count = bytebuffer_size - bytebuffer_offset;
    8981075    if (count < 8) {
    899         bb_xread_all(in, &bytebuffer[bytebuffer_size], 8 - count);
     1076        xread(in, &bytebuffer[bytebuffer_size], 8 - count);
     1077//shouldn't we propagate error?
    9001078        bytebuffer_size += 8 - count;
    9011079    }
     
    9081086    if (stored_crc != (~gunzip_crc)) {
    9091087        bb_error_msg("crc error");
    910         return -1;
     1088        n = -1;
     1089        goto ret;
    9111090    }
    9121091
     
    9141093    if (gunzip_bytes_out !=
    9151094        (bytebuffer[bytebuffer_offset] | (bytebuffer[bytebuffer_offset+1] << 8) |
    916         (bytebuffer[bytebuffer_offset+2] << 16) | (bytebuffer[bytebuffer_offset+3] << 24))) {
    917         bb_error_msg("Incorrect length");
    918         return -1;
    919     }
    920 
    921     return 0;
    922 }
     1095        (bytebuffer[bytebuffer_offset+2] << 16) | (bytebuffer[bytebuffer_offset+3] << 24))
     1096    ) {
     1097        bb_error_msg("incorrect length");
     1098        n = -1;
     1099    }
     1100 ret:
     1101    free(bytebuffer);
     1102    DEALLOC_STATE;
     1103    return n;
     1104}
Note: See TracChangeset for help on using the changeset viewer.