Magellan Linux

Diff of /trunk/mkinitrd-magellan/busybox/archival/libunarchive/decompress_unzip.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 532 by niro, Sat Sep 1 22:45:15 2007 UTC revision 816 by niro, Fri Apr 24 18:33:46 2009 UTC
# Line 12  Line 12 
12   * command line handling.   * command line handling.
13   *   *
14   * General cleanup to better adhere to the style guide and make use of standard   * General cleanup to better adhere to the style guide and make use of standard
15   * busybox functions by Glenn McGrath <bug1@iinet.net.au>   * busybox functions by Glenn McGrath
16   *   *
17   * read_gz interface + associated hacking by Laurence Anderson   * read_gz interface + associated hacking by Laurence Anderson
18   *   *
# Line 33  Line 33 
33   * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.   * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
34   */   */
35    
36    #include <setjmp.h>
37  #include "libbb.h"  #include "libbb.h"
38  #include "unarchive.h"  #include "unarchive.h"
39    
40  typedef struct huft_s {  typedef struct huft_t {
41   unsigned char e; /* number of extra bits or operation */   unsigned char e; /* number of extra bits or operation */
42   unsigned char b; /* number of bits in this code or subcode */   unsigned char b; /* number of bits in this code or subcode */
43   union {   union {
44   unsigned short n; /* literal, length base, or distance base */   unsigned short n; /* literal, length base, or distance base */
45   struct huft_s *t; /* pointer to next level of table */   struct huft_t *t; /* pointer to next level of table */
46   } v;   } v;
47  } huft_t;  } huft_t;
48    
49  enum {  enum {
50   /* gunzip_window size--must be a power of two, and   /* gunzip_window size--must be a power of two, and
51   *  at least 32K for zip's deflate method */   * at least 32K for zip's deflate method */
52   GUNZIP_WSIZE = 0x8000,   GUNZIP_WSIZE = 0x8000,
53   /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */   /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
54   BMAX = 16, /* maximum bit length of any code (16 for explode) */   BMAX = 16, /* maximum bit length of any code (16 for explode) */
# Line 82  typedef struct state_t { Line 83  typedef struct state_t {
83   unsigned gunzip_bb; /* bit buffer */   unsigned gunzip_bb; /* bit buffer */
84   unsigned char gunzip_bk; /* bits in bit buffer */   unsigned char gunzip_bk; /* bits in bit buffer */
85    
86   /* These control the size of the STATE()bytebuffer */   /* input (compressed) data */
87   unsigned bytebuffer_max;   unsigned char *bytebuffer;      /* buffer itself */
88   unsigned char *bytebuffer;   off_t to_read; /* compressed bytes to read (unzip only, -1 for gunzip) */
89   unsigned bytebuffer_offset;  // unsigned bytebuffer_max;        /* buffer size */
90   unsigned bytebuffer_size;   unsigned bytebuffer_offset;     /* buffer position */
91     unsigned bytebuffer_size;       /* how much data is there (size <= max) */
92    
93   /* private data of inflate_codes() */   /* private data of inflate_codes() */
94   unsigned inflate_codes_ml; /* masks for bl and bd bits */   unsigned inflate_codes_ml; /* masks for bl and bd bits */
# Line 100  typedef struct state_t { Line 102  typedef struct state_t {
102   unsigned inflate_codes_bd;   unsigned inflate_codes_bd;
103   unsigned inflate_codes_nn; /* length and index for copy */   unsigned inflate_codes_nn; /* length and index for copy */
104   unsigned inflate_codes_dd;   unsigned inflate_codes_dd;
105    
106   smallint resume_copy;   smallint resume_copy;
107    
108   /* private data of inflate_get_next_window() */   /* private data of inflate_get_next_window() */
109   smallint method; /* Method == -1 for stored, -2 for codes */   smallint method; /* method == -1 for stored, -2 for codes */
110   smallint need_another_block;   smallint need_another_block;
111   smallint end_reached;   smallint end_reached;
112    
# Line 112  typedef struct state_t { Line 115  typedef struct state_t {
115   unsigned inflate_stored_b;   unsigned inflate_stored_b;
116   unsigned inflate_stored_k;   unsigned inflate_stored_k;
117   unsigned inflate_stored_w;   unsigned inflate_stored_w;
118    
119     const char *error_msg;
120     jmp_buf error_jmp;
121  } state_t;  } state_t;
122  #define gunzip_bytes_out    (S()gunzip_bytes_out   )  #define gunzip_bytes_out    (S()gunzip_bytes_out   )
123  #define gunzip_crc          (S()gunzip_crc         )  #define gunzip_crc          (S()gunzip_crc         )
# Line 121  typedef struct state_t { Line 127  typedef struct state_t {
127  #define gunzip_crc_table    (S()gunzip_crc_table   )  #define gunzip_crc_table    (S()gunzip_crc_table   )
128  #define gunzip_bb           (S()gunzip_bb          )  #define gunzip_bb           (S()gunzip_bb          )
129  #define gunzip_bk           (S()gunzip_bk          )  #define gunzip_bk           (S()gunzip_bk          )
130  #define bytebuffer_max      (S()bytebuffer_max     )  #define to_read             (S()to_read            )
131    // #define bytebuffer_max   (S()bytebuffer_max     )
132    // Both gunzip and unzip can use constant buffer size now (16k):
133    #define bytebuffer_max      0x4000
134  #define bytebuffer          (S()bytebuffer         )  #define bytebuffer          (S()bytebuffer         )
135  #define bytebuffer_offset   (S()bytebuffer_offset  )  #define bytebuffer_offset   (S()bytebuffer_offset  )
136  #define bytebuffer_size     (S()bytebuffer_size    )  #define bytebuffer_size     (S()bytebuffer_size    )
# Line 144  typedef struct state_t { Line 153  typedef struct state_t {
153  #define inflate_stored_b    (S()inflate_stored_b   )  #define inflate_stored_b    (S()inflate_stored_b   )
154  #define inflate_stored_k    (S()inflate_stored_k   )  #define inflate_stored_k    (S()inflate_stored_k   )
155  #define inflate_stored_w    (S()inflate_stored_w   )  #define inflate_stored_w    (S()inflate_stored_w   )
156  #define INIT_STATE ({ bytebuffer_size = 0; method = -1; need_another_block = 1; })  #define error_msg           (S()error_msg          )
157    #define error_jmp           (S()error_jmp          )
158    
159    /* This is a generic part */
 /* This is generic part */  
160  #if STATE_IN_BSS /* Use global data segment */  #if STATE_IN_BSS /* Use global data segment */
161  #define DECLARE_STATE /*nothing*/  #define DECLARE_STATE /*nothing*/
162  #define ALLOC_STATE (init_state())  #define ALLOC_STATE /*nothing*/
163  #define DEALLOC_STATE ((void)0)  #define DEALLOC_STATE ((void)0)
164  #define S() state.  #define S() state.
165  #define PASS_STATE /*nothing*/  #define PASS_STATE /*nothing*/
# Line 158  typedef struct state_t { Line 167  typedef struct state_t {
167  #define STATE_PARAM /*nothing*/  #define STATE_PARAM /*nothing*/
168  #define STATE_PARAM_ONLY void  #define STATE_PARAM_ONLY void
169  static state_t state;  static state_t state;
 static void init_state(void)  
 {  
  INIT_STATE;  
 }  
170  #endif  #endif
171    
172  #if STATE_IN_MALLOC /* Use malloc space */  #if STATE_IN_MALLOC /* Use malloc space */
173  #define DECLARE_STATE state_t *state  #define DECLARE_STATE state_t *state
174  #define ALLOC_STATE (state = alloc_state())  #define ALLOC_STATE (state = xzalloc(sizeof(*state)))
175  #define DEALLOC_STATE free(state)  #define DEALLOC_STATE free(state)
176  #define S() state->  #define S() state->
177  #define PASS_STATE state,  #define PASS_STATE state,
178  #define PASS_STATE_ONLY state  #define PASS_STATE_ONLY state
179  #define STATE_PARAM state_t *state,  #define STATE_PARAM state_t *state,
180  #define STATE_PARAM_ONLY state_t *state  #define STATE_PARAM_ONLY state_t *state
 static state_t* alloc_state(void)  
 {  
  state_t* state = xzalloc(sizeof(*state));  
  INIT_STATE;  
  return state;  
 }  
181  #endif  #endif
182    
183    
184  static const unsigned short mask_bits[] = {  static const uint16_t mask_bits[] ALIGN2 = {
185   0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,   0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
186   0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff   0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
187  };  };
188    
189  /* Copy lengths for literal codes 257..285 */  /* Copy lengths for literal codes 257..285 */
190  static const unsigned short cplens[] = {  static const uint16_t cplens[] ALIGN2 = {
191   3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59,   3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59,
192   67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0   67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
193  };  };
194    
195  /* note: see note #13 above about the 258 in this list. */  /* note: see note #13 above about the 258 in this list. */
196  /* Extra bits for literal codes 257..285 */  /* Extra bits for literal codes 257..285 */
197  static const unsigned char cplext[] = {  static const uint8_t cplext[] ALIGN1 = {
198   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,   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,
199   5, 5, 5, 0, 99, 99   5, 5, 5, 0, 99, 99
200  }; /* 99 == invalid */  }; /* 99 == invalid */
201    
202  /* Copy offsets for distance codes 0..29 */  /* Copy offsets for distance codes 0..29 */
203  static const unsigned short cpdist[] = {  static const uint16_t cpdist[] ALIGN2 = {
204   1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513,   1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513,
205   769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577   769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577
206  };  };
207    
208  /* Extra bits for distance codes */  /* Extra bits for distance codes */
209  static const unsigned char cpdext[] = {  static const uint8_t cpdext[] ALIGN1 = {
210   0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10,   0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10,
211   11, 11, 12, 12, 13, 13   11, 11, 12, 12, 13, 13
212  };  };
213    
214  /* Tables for deflate from PKZIP's appnote.txt. */  /* Tables for deflate from PKZIP's appnote.txt. */
215  /* Order of the bit length code lengths */  /* Order of the bit length code lengths */
216  static const unsigned char border[] = {  static const uint8_t border[] ALIGN1 = {
217   16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15   16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
218  };  };
219    
 static unsigned fill_bitbuffer(STATE_PARAM unsigned bitbuffer, unsigned *current, const unsigned required)  
 {  
  while (*current < required) {  
  if (bytebuffer_offset >= bytebuffer_size) {  
  /* Leave the first 4 bytes empty so we can always unwind the bitbuffer  
  * to the front of the bytebuffer, leave 4 bytes free at end of tail  
  * so we can easily top up buffer in check_trailer_gzip() */  
  bytebuffer_size = safe_read(gunzip_src_fd, &bytebuffer[4], bytebuffer_max - 8);  
  if (1 > bytebuffer_size)  
 //shouldn't we propagate error?  
  bb_error_msg_and_die("unexpected end of file");  
  bytebuffer_size += 4;  
  bytebuffer_offset = 4;  
  }  
  bitbuffer |= ((unsigned) bytebuffer[bytebuffer_offset]) << *current;  
  bytebuffer_offset++;  
  *current += 8;  
  }  
  return bitbuffer;  
 }  
220    
221  /*  /*
222   * Free the malloc'ed tables built by huft_build(), which makes a linked   * Free the malloc'ed tables built by huft_build(), which makes a linked
# Line 245  static unsigned fill_bitbuffer(STATE_PAR Line 224  static unsigned fill_bitbuffer(STATE_PAR
224   * each table.   * each table.
225   * t: table to free   * t: table to free
226   */   */
227  static void huft_free(huft_t * p)  static void huft_free(huft_t *p)
228  {  {
229   huft_t *q;   huft_t *q;
230    
# Line 257  static void huft_free(huft_t * p) Line 236  static void huft_free(huft_t * p)
236   }   }
237  }  }
238    
239    static void huft_free_all(STATE_PARAM_ONLY)
240    {
241     huft_free(inflate_codes_tl);
242     huft_free(inflate_codes_td);
243     inflate_codes_tl = NULL;
244     inflate_codes_td = NULL;
245    }
246    
247    static void abort_unzip(STATE_PARAM_ONLY) NORETURN;
248    static void abort_unzip(STATE_PARAM_ONLY)
249    {
250     huft_free_all(PASS_STATE_ONLY);
251     longjmp(error_jmp, 1);
252    }
253    
254    static unsigned fill_bitbuffer(STATE_PARAM unsigned bitbuffer, unsigned *current, const unsigned required)
255    {
256     while (*current < required) {
257     if (bytebuffer_offset >= bytebuffer_size) {
258     unsigned sz = bytebuffer_max - 4;
259     if (to_read >= 0 && to_read < sz) /* unzip only */
260     sz = to_read;
261     /* Leave the first 4 bytes empty so we can always unwind the bitbuffer
262     * to the front of the bytebuffer */
263     bytebuffer_size = safe_read(gunzip_src_fd, &bytebuffer[4], sz);
264     if ((int)bytebuffer_size < 1) {
265     error_msg = "unexpected end of file";
266     abort_unzip(PASS_STATE_ONLY);
267     }
268     if (to_read >= 0) /* unzip only */
269     to_read -= bytebuffer_size;
270     bytebuffer_size += 4;
271     bytebuffer_offset = 4;
272     }
273     bitbuffer |= ((unsigned) bytebuffer[bytebuffer_offset]) << *current;
274     bytebuffer_offset++;
275     *current += 8;
276     }
277     return bitbuffer;
278    }
279    
280    
281  /* Given a list of code lengths and a maximum table size, make a set of  /* Given a list of code lengths and a maximum table size, make a set of
282   * tables to decode that set of codes.  Return zero on success, one if   * tables to decode that set of codes.  Return zero on success, one if
283   * the given code set is incomplete (the tables are still built in this   * the given code set is incomplete (the tables are still built in this
284   * case), two if the input is invalid (all zero length codes or an   * case), two if the input is invalid (all zero length codes or an
285   * oversubscribed set of lengths), and three if not enough memory.   * oversubscribed set of lengths) - in this case stores NULL in *t.
286   *   *
287   * b: code lengths in bits (all assumed <= BMAX)   * b: code lengths in bits (all assumed <= BMAX)
288   * n: number of codes (assumed <= N_MAX)   * n: number of codes (assumed <= N_MAX)
# Line 271  static void huft_free(huft_t * p) Line 292  static void huft_free(huft_t * p)
292   * t: result: starting table   * t: result: starting table
293   * m: maximum lookup bits, returns actual   * m: maximum lookup bits, returns actual
294   */   */
295  static int huft_build(unsigned *b, const unsigned n,  static int huft_build(const unsigned *b, const unsigned n,
296     const unsigned s, const unsigned short *d,     const unsigned s, const unsigned short *d,
297     const unsigned char *e, huft_t ** t, unsigned *m)     const unsigned char *e, huft_t **t, unsigned *m)
298  {  {
299   unsigned a; /* counter for codes of length k */   unsigned a;             /* counter for codes of length k */
300   unsigned c[BMAX + 1]; /* bit length count table */   unsigned c[BMAX + 1];   /* bit length count table */
301   unsigned eob_len; /* length of end-of-block code (value 256) */   unsigned eob_len;       /* length of end-of-block code (value 256) */
302   unsigned f; /* i repeats in table every f entries */   unsigned f;             /* i repeats in table every f entries */
303   int g; /* maximum code length */   int g;                  /* maximum code length */
304   int htl; /* table level */   int htl;                /* table level */
305   unsigned i; /* counter, current code */   unsigned i;             /* counter, current code */
306   unsigned j; /* counter */   unsigned j;             /* counter */
307   int k; /* number of bits in current code */   int k;                  /* number of bits in current code */
308   unsigned *p; /* pointer into c[], b[], or v[] */   unsigned *p;            /* pointer into c[], b[], or v[] */
309   huft_t *q; /* points to current table */   huft_t *q;              /* points to current table */
310   huft_t r; /* table entry for structure assignment */   huft_t r;               /* table entry for structure assignment */
311   huft_t *u[BMAX]; /* table stack */   huft_t *u[BMAX];        /* table stack */
312   unsigned v[N_MAX]; /* values in order of bit length */   unsigned v[N_MAX];      /* values in order of bit length */
313   int ws[BMAX+1]; /* bits decoded stack */   int ws[BMAX + 1];       /* bits decoded stack */
314   int w; /* bits decoded */   int w;                  /* bits decoded */
315   unsigned x[BMAX + 1]; /* bit offsets, then code stack */   unsigned x[BMAX + 1];   /* bit offsets, then code stack */
316   unsigned *xp; /* pointer into x */   unsigned *xp;           /* pointer into x */
317   int y; /* number of dummy codes added */   int y;                  /* number of dummy codes added */
318   unsigned z; /* number of entries in current table */   unsigned z;             /* number of entries in current table */
319    
320   /* Length of EOB code, if any */   /* Length of EOB code, if any */
321   eob_len = n > 256 ? b[256] : BMAX;   eob_len = n > 256 ? b[256] : BMAX;
322    
323     *t = NULL;
324    
325   /* Generate counts for each bit length */   /* Generate counts for each bit length */
326   memset(c, 0, sizeof(c));   memset(c, 0, sizeof(c));
327   p = b;   p = (unsigned *) b; /* cast allows us to reuse p for pointing to b */
328   i = n;   i = n;
329   do {   do {
330   c[*p]++; /* assume all entries <= BMAX */   c[*p]++; /* assume all entries <= BMAX */
331   p++; /* Can't combine with above line (Solaris bug) */   p++;     /* can't combine with above line (Solaris bug) */
332   } while (--i);   } while (--i);
333   if (c[0] == n) { /* null input--all zero length codes */   if (c[0] == n) {  /* null input - all zero length codes */
  *t = NULL;  
334   *m = 0;   *m = 0;
335   return 2;   return 2;
336   }   }
337    
338   /* Find minimum and maximum length, bound *m by those */   /* Find minimum and maximum length, bound *m by those */
339   for (j = 1; (c[j] == 0) && (j <= BMAX); j++);   for (j = 1; (c[j] == 0) && (j <= BMAX); j++)
340     continue;
341   k = j; /* minimum code length */   k = j; /* minimum code length */
342   for (i = BMAX; (c[i] == 0) && i; i--);   for (i = BMAX; (c[i] == 0) && i; i--)
343     continue;
344   g = i; /* maximum code length */   g = i; /* maximum code length */
345   *m = (*m < j) ? j : ((*m > i) ? i : *m);   *m = (*m < j) ? j : ((*m > i) ? i : *m);
346    
347   /* Adjust last length count to fill out codes, if needed */   /* Adjust last length count to fill out codes, if needed */
348   for (y = 1 << j; j < i; j++, y <<= 1) {   for (y = 1 << j; j < i; j++, y <<= 1) {
349   y -= c[j];   y -= c[j];
350   if (y < 0) {   if (y < 0)
351   return 2; /* bad input: more codes than bits */   return 2; /* bad input: more codes than bits */
  }  
352   }   }
353   y -= c[i];   y -= c[i];
354   if (y < 0) {   if (y < 0)
355   return 2;   return 2;
  }  
356   c[i] += y;   c[i] += y;
357    
358   /* Generate starting offsets into the value table for each length */   /* Generate starting offsets into the value table for each length */
# Line 343  static int huft_build(unsigned *b, const Line 365  static int huft_build(unsigned *b, const
365   }   }
366    
367   /* Make a table of values in order of bit lengths */   /* Make a table of values in order of bit lengths */
368   p = b;   p = (unsigned *) b;
369   i = 0;   i = 0;
370   do {   do {
371   j = *p++;   j = *p++;
# Line 353  static int huft_build(unsigned *b, const Line 375  static int huft_build(unsigned *b, const
375   } while (++i < n);   } while (++i < n);
376    
377   /* Generate the Huffman codes and for each, make the table entries */   /* Generate the Huffman codes and for each, make the table entries */
378   x[0] = i = 0; /* first Huffman code is zero */   x[0] = i = 0;   /* first Huffman code is zero */
379   p = v; /* grab values in bit order */   p = v;          /* grab values in bit order */
380   htl = -1; /* no tables yet--level -1 */   htl = -1;       /* no tables yet--level -1 */
381   w = ws[0] = 0; /* bits decoded */   w = ws[0] = 0;  /* bits decoded */
382   u[0] = NULL; /* just to keep compilers happy */   u[0] = NULL;    /* just to keep compilers happy */
383   q = NULL; /* ditto */   q = NULL;       /* ditto */
384   z = 0; /* ditto */   z = 0;          /* ditto */
385    
386   /* go through the bit lengths (k already is bits in shortest code) */   /* go through the bit lengths (k already is bits in shortest code) */
387   for (; k <= g; k++) {   for (; k <= g; k++) {
# Line 442  static int huft_build(unsigned *b, const Line 464  static int huft_build(unsigned *b, const
464   /* return actual size of base table */   /* return actual size of base table */
465   *m = ws[1];   *m = ws[1];
466    
467   /* Return true (1) if we were given an incomplete table */   /* Return 1 if we were given an incomplete table */
468   return y != 0 && g != 1;   return y != 0 && g != 1;
469  }  }
470    
# Line 455  static int huft_build(unsigned *b, const Line 477  static int huft_build(unsigned *b, const
477   * bl, bd: number of bits decoded by tl[] and td[]   * bl, bd: number of bits decoded by tl[] and td[]
478   */   */
479  /* called once from inflate_block */  /* called once from inflate_block */
480    
481    /* map formerly local static variables to globals */
482  #define ml inflate_codes_ml  #define ml inflate_codes_ml
483  #define md inflate_codes_md  #define md inflate_codes_md
484  #define bb inflate_codes_bb  #define bb inflate_codes_bb
# Line 466  static int huft_build(unsigned *b, const Line 490  static int huft_build(unsigned *b, const
490  #define bd inflate_codes_bd  #define bd inflate_codes_bd
491  #define nn inflate_codes_nn  #define nn inflate_codes_nn
492  #define dd inflate_codes_dd  #define dd inflate_codes_dd
493  static void inflate_codes_setup(STATE_PARAM huft_t * my_tl, huft_t * my_td, const unsigned my_bl, const unsigned my_bd)  static void inflate_codes_setup(STATE_PARAM unsigned my_bl, unsigned my_bd)
494  {  {
  tl = my_tl;  
  td = my_td;  
495   bl = my_bl;   bl = my_bl;
496   bd = my_bd;   bd = my_bd;
497   /* make local copies of globals */   /* make local copies of globals */
# Line 486  static int inflate_codes(STATE_PARAM_ONL Line 508  static int inflate_codes(STATE_PARAM_ONL
508   unsigned e; /* table entry flag/number of extra bits */   unsigned e; /* table entry flag/number of extra bits */
509   huft_t *t; /* pointer to table entry */   huft_t *t; /* pointer to table entry */
510    
511   if (resume_copy) goto do_copy;   if (resume_copy)
512     goto do_copy;
513    
514   while (1) { /* do until end of block */   while (1) { /* do until end of block */
515   bb = fill_bitbuffer(PASS_STATE bb, &k, bl);   bb = fill_bitbuffer(PASS_STATE bb, &k, bl);
# Line 494  static int inflate_codes(STATE_PARAM_ONL Line 517  static int inflate_codes(STATE_PARAM_ONL
517   e = t->e;   e = t->e;
518   if (e > 16)   if (e > 16)
519   do {   do {
520   if (e == 99) {   if (e == 99)
521  //shouldn't we propagate error?   abort_unzip(PASS_STATE_ONLY);;
  bb_error_msg_and_die("inflate_codes error 1");  
  }  
522   bb >>= t->b;   bb >>= t->b;
523   k -= t->b;   k -= t->b;
524   e -= 16;   e -= 16;
# Line 534  static int inflate_codes(STATE_PARAM_ONL Line 555  static int inflate_codes(STATE_PARAM_ONL
555   if (e > 16)   if (e > 16)
556   do {   do {
557   if (e == 99)   if (e == 99)
558  //shouldn't we propagate error?   abort_unzip(PASS_STATE_ONLY);
  bb_error_msg_and_die("inflate_codes error 2");  
559   bb >>= t->b;   bb >>= t->b;
560   k -= t->b;   k -= t->b;
561   e -= 16;   e -= 16;
# Line 590  static int inflate_codes(STATE_PARAM_ONL Line 610  static int inflate_codes(STATE_PARAM_ONL
610   gunzip_bk = k;   gunzip_bk = k;
611    
612   /* normally just after call to inflate_codes, but save code by putting it here */   /* normally just after call to inflate_codes, but save code by putting it here */
613   /* free the decoding tables, return */   /* free the decoding tables (tl and td), return */
614   huft_free(tl);   huft_free_all(PASS_STATE_ONLY);
  huft_free(td);  
615    
616   /* done */   /* done */
617   return 0;   return 0;
# Line 632  static int inflate_stored(STATE_PARAM_ON Line 651  static int inflate_stored(STATE_PARAM_ON
651   inflate_stored_w = 0;   inflate_stored_w = 0;
652   inflate_stored_b >>= 8;   inflate_stored_b >>= 8;
653   inflate_stored_k -= 8;   inflate_stored_k -= 8;
654   return 1; // We have a block   return 1; /* We have a block */
655   }   }
656   inflate_stored_b >>= 8;   inflate_stored_b >>= 8;
657   inflate_stored_k -= 8;   inflate_stored_k -= 8;
# Line 642  static int inflate_stored(STATE_PARAM_ON Line 661  static int inflate_stored(STATE_PARAM_ON
661   gunzip_outbuf_count = inflate_stored_w; /* restore global gunzip_window pointer */   gunzip_outbuf_count = inflate_stored_w; /* restore global gunzip_window pointer */
662   gunzip_bb = inflate_stored_b; /* restore global bit buffer */   gunzip_bb = inflate_stored_b; /* restore global bit buffer */
663   gunzip_bk = inflate_stored_k;   gunzip_bk = inflate_stored_k;
664   return 0; // Finished   return 0; /* Finished */
665  }  }
666    
667    
# Line 656  static int inflate_stored(STATE_PARAM_ON Line 675  static int inflate_stored(STATE_PARAM_ON
675  /* One callsite in inflate_get_next_window */  /* One callsite in inflate_get_next_window */
676  static int inflate_block(STATE_PARAM smallint *e)  static int inflate_block(STATE_PARAM smallint *e)
677  {  {
678   unsigned t; /* block type */   unsigned ll[286 + 30];  /* literal/length and distance code lengths */
679   unsigned b; /* bit buffer */   unsigned t;     /* block type */
680   unsigned k; /* number of bits in bit buffer */   unsigned b;     /* bit buffer */
681     unsigned k;     /* number of bits in bit buffer */
682    
683   /* make local bit buffer */   /* make local bit buffer */
684    
# Line 681  static int inflate_block(STATE_PARAM sma Line 701  static int inflate_block(STATE_PARAM sma
701   gunzip_bb = b;   gunzip_bb = b;
702   gunzip_bk = k;   gunzip_bk = k;
703    
704     /* Do we see block type 1 often? Yes!
705     * TODO: fix performance problem (see below) */
706     //bb_error_msg("blktype %d", t);
707    
708   /* inflate that block type */   /* inflate that block type */
709   switch (t) {   switch (t) {
710   case 0: /* Inflate stored */   case 0: /* Inflate stored */
711   {   {
712   unsigned n; /* number of bytes in block */   unsigned n; /* number of bytes in block */
713   unsigned b_stored; /* bit buffer */   unsigned b_stored; /* bit buffer */
# Line 706  static int inflate_block(STATE_PARAM sma Line 730  static int inflate_block(STATE_PARAM sma
730    
731   b_stored = fill_bitbuffer(PASS_STATE b_stored, &k_stored, 16);   b_stored = fill_bitbuffer(PASS_STATE b_stored, &k_stored, 16);
732   if (n != (unsigned) ((~b_stored) & 0xffff)) {   if (n != (unsigned) ((~b_stored) & 0xffff)) {
733   return 1; /* error in compressed data */   abort_unzip(PASS_STATE_ONLY); /* error in compressed data */
734   }   }
735   b_stored >>= 16;   b_stored >>= 16;
736   k_stored -= 16;   k_stored -= 16;
737    
738   inflate_stored_setup(PASS_STATE n, b_stored, k_stored); // Setup inflate_stored   inflate_stored_setup(PASS_STATE n, b_stored, k_stored);
739    
740   return -1;   return -1;
741   }   }
742   case 1:   case 1:
743   /* Inflate fixed   /* Inflate fixed
744   * decompress an inflated type 1 (fixed Huffman codes) block.  We should   * decompress an inflated type 1 (fixed Huffman codes) block. We should
745   * either replace this with a custom decoder, or at least precompute the   * either replace this with a custom decoder, or at least precompute the
746   * Huffman tables. */   * Huffman tables. TODO */
747   {   {
748   int i; /* temporary variable */   int i;                  /* temporary variable */
749   huft_t *tl; /* literal/length code table */   unsigned bl;            /* lookup bits for tl */
750   huft_t *td; /* distance code table */   unsigned bd;            /* lookup bits for td */
751   unsigned bl; /* lookup bits for tl */   /* gcc 4.2.1 is too dumb to reuse stackspace. Moved up... */
752   unsigned bd; /* lookup bits for td */   //unsigned ll[288];     /* length list for huft_build */
  unsigned l[288]; /* length list for huft_build */  
753    
754   /* set up literal table */   /* set up literal table */
755   for (i = 0; i < 144; i++) {   for (i = 0; i < 144; i++)
756   l[i] = 8;   ll[i] = 8;
757   }   for (; i < 256; i++)
758   for (; i < 256; i++) {   ll[i] = 9;
759   l[i] = 9;   for (; i < 280; i++)
760   }   ll[i] = 7;
761   for (; i < 280; i++) {   for (; i < 288; i++) /* make a complete, but wrong code set */
762   l[i] = 7;   ll[i] = 8;
  }  
  for (; i < 288; i++) { /* make a complete, but wrong code set */  
  l[i] = 8;  
  }  
763   bl = 7;   bl = 7;
764   i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl);   huft_build(ll, 288, 257, cplens, cplext, &inflate_codes_tl, &bl);
765   if (i != 0) {   /* huft_build() never return nonzero - we use known data */
  return i;  
  }  
766    
767   /* set up distance table */   /* set up distance table */
768   for (i = 0; i < 30; i++) { /* make an incomplete code set */   for (i = 0; i < 30; i++) /* make an incomplete code set */
769   l[i] = 5;   ll[i] = 5;
  }  
770   bd = 5;   bd = 5;
771   i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd);   huft_build(ll, 30, 0, cpdist, cpdext, &inflate_codes_td, &bd);
  if (i > 1) {  
  huft_free(tl);  
  return i;  
  }  
772    
773   /* decompress until an end-of-block code */   /* set up data for inflate_codes() */
774   inflate_codes_setup(PASS_STATE tl, td, bl, bd); // Setup inflate_codes   inflate_codes_setup(PASS_STATE bl, bd);
775    
776   /* huft_free code moved into inflate_codes */   /* huft_free code moved into inflate_codes */
777    
778   return -2;   return -2;
779   }   }
780   case 2: /* Inflate dynamic */   case 2: /* Inflate dynamic */
781   {   {
782   const int dbits = 6; /* bits in base distance lookup table */   enum { dbits = 6 };     /* bits in base distance lookup table */
783   const int lbits = 9; /* bits in base literal/length lookup table */   enum { lbits = 9 };     /* bits in base literal/length lookup table */
784    
785   huft_t *tl; /* literal/length code table */   huft_t *td;             /* distance code table */
786   huft_t *td; /* distance code table */   unsigned i;             /* temporary variables */
  unsigned i; /* temporary variables */  
787   unsigned j;   unsigned j;
788   unsigned l; /* last length */   unsigned l;             /* last length */
789   unsigned m; /* mask for bit lengths table */   unsigned m;             /* mask for bit lengths table */
790   unsigned n; /* number of lengths to get */   unsigned n;             /* number of lengths to get */
791   unsigned bl; /* lookup bits for tl */   unsigned bl;            /* lookup bits for tl */
792   unsigned bd; /* lookup bits for td */   unsigned bd;            /* lookup bits for td */
793   unsigned nb; /* number of bit length codes */   unsigned nb;            /* number of bit length codes */
794   unsigned nl; /* number of literal/length codes */   unsigned nl;            /* number of literal/length codes */
795   unsigned nd; /* number of distance codes */   unsigned nd;            /* number of distance codes */
796    
797   unsigned ll[286 + 30]; /* literal/length and distance code lengths */   //unsigned ll[286 + 30];/* literal/length and distance code lengths */
798   unsigned b_dynamic; /* bit buffer */   unsigned b_dynamic;     /* bit buffer */
799   unsigned k_dynamic; /* number of bits in bit buffer */   unsigned k_dynamic;     /* number of bits in bit buffer */
800    
801   /* make local bit buffer */   /* make local bit buffer */
802   b_dynamic = gunzip_bb;   b_dynamic = gunzip_bb;
# Line 807  static int inflate_block(STATE_PARAM sma Line 818  static int inflate_block(STATE_PARAM sma
818    
819   b_dynamic >>= 4;   b_dynamic >>= 4;
820   k_dynamic -= 4;   k_dynamic -= 4;
821   if (nl > 286 || nd > 30) {   if (nl > 286 || nd > 30)
822   return 1; /* bad lengths */   abort_unzip(PASS_STATE_ONLY); /* bad lengths */
  }  
823    
824   /* read in bit-length-code lengths */   /* read in bit-length-code lengths */
825   for (j = 0; j < nb; j++) {   for (j = 0; j < nb; j++) {
# Line 818  static int inflate_block(STATE_PARAM sma Line 828  static int inflate_block(STATE_PARAM sma
828   b_dynamic >>= 3;   b_dynamic >>= 3;
829   k_dynamic -= 3;   k_dynamic -= 3;
830   }   }
831   for (; j < 19; j++) {   for (; j < 19; j++)
832   ll[border[j]] = 0;   ll[border[j]] = 0;
  }  
833    
834   /* build decoding table for trees--single level, 7 bit lookup */   /* build decoding table for trees - single level, 7 bit lookup */
835   bl = 7;   bl = 7;
836   i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl);   i = huft_build(ll, 19, 19, NULL, NULL, &inflate_codes_tl, &bl);
837   if (i != 0) {   if (i != 0) {
838   if (i == 1) {   abort_unzip(PASS_STATE_ONLY); //return i; /* incomplete code set */
  huft_free(tl);  
  }  
  return i; /* incomplete code set */  
839   }   }
840    
841   /* read in literal and distance code lengths */   /* read in literal and distance code lengths */
# Line 838  static int inflate_block(STATE_PARAM sma Line 844  static int inflate_block(STATE_PARAM sma
844   i = l = 0;   i = l = 0;
845   while ((unsigned) i < n) {   while ((unsigned) i < n) {
846   b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, (unsigned)bl);   b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, (unsigned)bl);
847   j = (td = tl + ((unsigned) b_dynamic & m))->b;   td = inflate_codes_tl + ((unsigned) b_dynamic & m);
848     j = td->b;
849   b_dynamic >>= j;   b_dynamic >>= j;
850   k_dynamic -= j;   k_dynamic -= j;
851   j = td->v.n;   j = td->v.n;
# Line 850  static int inflate_block(STATE_PARAM sma Line 857  static int inflate_block(STATE_PARAM sma
857   b_dynamic >>= 2;   b_dynamic >>= 2;
858   k_dynamic -= 2;   k_dynamic -= 2;
859   if ((unsigned) i + j > n) {   if ((unsigned) i + j > n) {
860   return 1;   abort_unzip(PASS_STATE_ONLY); //return 1;
861   }   }
862   while (j--) {   while (j--) {
863   ll[i++] = l;   ll[i++] = l;
# Line 861  static int inflate_block(STATE_PARAM sma Line 868  static int inflate_block(STATE_PARAM sma
868   b_dynamic >>= 3;   b_dynamic >>= 3;
869   k_dynamic -= 3;   k_dynamic -= 3;
870   if ((unsigned) i + j > n) {   if ((unsigned) i + j > n) {
871   return 1;   abort_unzip(PASS_STATE_ONLY); //return 1;
872   }   }
873   while (j--) {   while (j--) {
874   ll[i++] = 0;   ll[i++] = 0;
# Line 873  static int inflate_block(STATE_PARAM sma Line 880  static int inflate_block(STATE_PARAM sma
880   b_dynamic >>= 7;   b_dynamic >>= 7;
881   k_dynamic -= 7;   k_dynamic -= 7;
882   if ((unsigned) i + j > n) {   if ((unsigned) i + j > n) {
883   return 1;   abort_unzip(PASS_STATE_ONLY); //return 1;
884   }   }
885   while (j--) {   while (j--) {
886   ll[i++] = 0;   ll[i++] = 0;
# Line 883  static int inflate_block(STATE_PARAM sma Line 890  static int inflate_block(STATE_PARAM sma
890   }   }
891    
892   /* free decoding table for trees */   /* free decoding table for trees */
893   huft_free(tl);   huft_free(inflate_codes_tl);
894    
895   /* restore the global bit buffer */   /* restore the global bit buffer */
896   gunzip_bb = b_dynamic;   gunzip_bb = b_dynamic;
# Line 892  static int inflate_block(STATE_PARAM sma Line 899  static int inflate_block(STATE_PARAM sma
899   /* build the decoding tables for literal/length and distance codes */   /* build the decoding tables for literal/length and distance codes */
900   bl = lbits;   bl = lbits;
901    
902   i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl);   i = huft_build(ll, nl, 257, cplens, cplext, &inflate_codes_tl, &bl);
903   if (i != 0) {   if (i != 0)
904   if (i == 1) {   abort_unzip(PASS_STATE_ONLY);
 //shouldn't we propagate error?  
  bb_error_msg_and_die("incomplete literal tree");  
  /* huft_free(tl); */  
  }  
  return i; /* incomplete code set */  
  }  
   
905   bd = dbits;   bd = dbits;
906   i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd);   i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &inflate_codes_td, &bd);
907   if (i != 0) {   if (i != 0)
908   if (i == 1) {   abort_unzip(PASS_STATE_ONLY);
 //shouldn't we propagate error?  
  bb_error_msg_and_die("incomplete distance tree");  
  /* huft_free(td); */  
  }  
  huft_free(tl);  
  return i; /* incomplete code set */  
  }  
909    
910   /* decompress until an end-of-block code */   /* set up data for inflate_codes() */
911   inflate_codes_setup(PASS_STATE tl, td, bl, bd); // Setup inflate_codes   inflate_codes_setup(PASS_STATE bl, bd);
912    
913   /* huft_free code moved into inflate_codes */   /* huft_free code moved into inflate_codes */
914    
915   return -2;   return -2;
916   }   }
917   default:   default:
918   /* bad block type */   abort_unzip(PASS_STATE_ONLY);
 //shouldn't we propagate error?  
  bb_error_msg_and_die("bad block type %d", t);  
919   }   }
920  }  }
921    
922  /* Two callsites, both in inflate_get_next_window */  /* Two callsites, both in inflate_get_next_window */
923  static void calculate_gunzip_crc(STATE_PARAM_ONLY)  static void calculate_gunzip_crc(STATE_PARAM_ONLY)
924  {  {
925   int n;   unsigned n;
926   for (n = 0; n < gunzip_outbuf_count; n++) {   for (n = 0; n < gunzip_outbuf_count; n++) {
927   gunzip_crc = gunzip_crc_table[((int) gunzip_crc ^ (gunzip_window[n])) & 0xff] ^ (gunzip_crc >> 8);   gunzip_crc = gunzip_crc_table[((int) gunzip_crc ^ (gunzip_window[n])) & 0xff] ^ (gunzip_crc >> 8);
928   }   }
# Line 950  static int inflate_get_next_window(STATE Line 941  static int inflate_get_next_window(STATE
941   if (end_reached) {   if (end_reached) {
942   calculate_gunzip_crc(PASS_STATE_ONLY);   calculate_gunzip_crc(PASS_STATE_ONLY);
943   end_reached = 0;   end_reached = 0;
944   need_another_block = 1;   /* NB: need_another_block is still set */
945   return 0; /* Last block */   return 0; /* Last block */
946   }   }
947   method = inflate_block(PASS_STATE &end_reached);   method = inflate_block(PASS_STATE &end_reached);
# Line 964  static int inflate_get_next_window(STATE Line 955  static int inflate_get_next_window(STATE
955   case -2:   case -2:
956   ret = inflate_codes(PASS_STATE_ONLY);   ret = inflate_codes(PASS_STATE_ONLY);
957   break;   break;
958   default:   default: /* cannot happen */
959  //shouldn't we propagate error?   abort_unzip(PASS_STATE_ONLY);
  bb_error_msg_and_die("inflate error %d", method);  
960   }   }
961    
962   if (ret == 1) {   if (ret == 1) {
963   calculate_gunzip_crc(PASS_STATE_ONLY);   calculate_gunzip_crc(PASS_STATE_ONLY);
964   return 1; // More data left   return 1; /* more data left */
965   }   }
966   need_another_block = 1; // End of that block   need_another_block = 1; /* end of that block */
967   }   }
968   /* Doesnt get here */   /* Doesnt get here */
969  }  }
970    
971    
972  /* Called from inflate_gunzip() and inflate_unzip() */  /* Called from unpack_gz_stream() and inflate_unzip() */
 /* NB: bytebuffer is allocated here but freeing it is left to the caller! */  
973  static USE_DESKTOP(long long) int  static USE_DESKTOP(long long) int
974  inflate_unzip_internal(STATE_PARAM int in, int out)  inflate_unzip_internal(STATE_PARAM int in, int out)
975  {  {
# Line 993  inflate_unzip_internal(STATE_PARAM int i Line 982  inflate_unzip_internal(STATE_PARAM int i
982   gunzip_bytes_out = 0;   gunzip_bytes_out = 0;
983   gunzip_src_fd = in;   gunzip_src_fd = in;
984    
985   /* initialize gunzip_window, bit buffer */   /* (re) initialize state */
986     method = -1;
987     need_another_block = 1;
988     resume_copy = 0;
989   gunzip_bk = 0;   gunzip_bk = 0;
990   gunzip_bb = 0;   gunzip_bb = 0;
991    
992   /* Create the crc table */   /* Create the crc table */
993   gunzip_crc_table = crc32_filltable(0);   gunzip_crc_table = crc32_filltable(NULL, 0);
994   gunzip_crc = ~0;   gunzip_crc = ~0;
995    
996   /* Allocate space for buffer */   error_msg = "corrupted data";
997   bytebuffer = xmalloc(bytebuffer_max);   if (setjmp(error_jmp)) {
998     /* Error from deep inside zip machinery */
999     n = -1;
1000     goto ret;
1001     }
1002    
1003   while (1) {   while (1) {
1004   int r = inflate_get_next_window(PASS_STATE_ONLY);   int r = inflate_get_next_window(PASS_STATE_ONLY);
1005   nwrote = full_write(out, gunzip_window, gunzip_outbuf_count);   nwrote = full_write(out, gunzip_window, gunzip_outbuf_count);
1006   if (nwrote != gunzip_outbuf_count) {   if (nwrote != (ssize_t)gunzip_outbuf_count) {
1007   bb_perror_msg("write");   bb_perror_msg("write");
1008   n = -1;   n = -1;
1009   goto ret;   goto ret;
# Line 1033  inflate_unzip_internal(STATE_PARAM int i Line 1029  inflate_unzip_internal(STATE_PARAM int i
1029  }  }
1030    
1031    
1032  USE_DESKTOP(long long) int  /* External entry points */
1033  inflate_unzip(inflate_unzip_result *res, unsigned bufsize, int in, int out)  
1034    /* For unzip */
1035    
1036    USE_DESKTOP(long long) int FAST_FUNC
1037    inflate_unzip(inflate_unzip_result *res, off_t compr_size, int in, int out)
1038  {  {
1039   USE_DESKTOP(long long) int n;   USE_DESKTOP(long long) int n;
1040   DECLARE_STATE;   DECLARE_STATE;
1041    
1042   ALLOC_STATE;   ALLOC_STATE;
1043    
1044   bytebuffer_max = bufsize + 8;   to_read = compr_size;
1045    // bytebuffer_max = 0x8000;
1046   bytebuffer_offset = 4;   bytebuffer_offset = 4;
1047     bytebuffer = xmalloc(bytebuffer_max);
1048   n = inflate_unzip_internal(PASS_STATE in, out);   n = inflate_unzip_internal(PASS_STATE in, out);
1049     free(bytebuffer);
1050    
1051   res->crc = gunzip_crc;   res->crc = gunzip_crc;
1052   res->bytes_out = gunzip_bytes_out;   res->bytes_out = gunzip_bytes_out;
  free(bytebuffer);  
1053   DEALLOC_STATE;   DEALLOC_STATE;
1054   return n;   return n;
1055  }  }
1056    
1057    
1058  USE_DESKTOP(long long) int  /* For gunzip */
1059  inflate_gunzip(int in, int out)  
1060    /* helpers first */
1061    
1062    /* Top up the input buffer with at least n bytes. */
1063    static int top_up(STATE_PARAM unsigned n)
1064    {
1065     int count = bytebuffer_size - bytebuffer_offset;
1066    
1067     if (count < (int)n) {
1068     memmove(bytebuffer, &bytebuffer[bytebuffer_offset], count);
1069     bytebuffer_offset = 0;
1070     bytebuffer_size = full_read(gunzip_src_fd, &bytebuffer[count], bytebuffer_max - count);
1071     if ((int)bytebuffer_size < 0) {
1072     bb_error_msg("read error");
1073     return 0;
1074     }
1075     bytebuffer_size += count;
1076     if (bytebuffer_size < n)
1077     return 0;
1078     }
1079     return 1;
1080    }
1081    
1082    static uint16_t buffer_read_le_u16(STATE_PARAM_ONLY)
1083    {
1084     uint16_t res;
1085    #if BB_LITTLE_ENDIAN
1086     /* gcc 4.2.1 is very clever */
1087     memcpy(&res, &bytebuffer[bytebuffer_offset], 2);
1088    #else
1089     res = bytebuffer[bytebuffer_offset];
1090     res |= bytebuffer[bytebuffer_offset + 1] << 8;
1091    #endif
1092     bytebuffer_offset += 2;
1093     return res;
1094    }
1095    
1096    static uint32_t buffer_read_le_u32(STATE_PARAM_ONLY)
1097    {
1098     uint32_t res;
1099    #if BB_LITTLE_ENDIAN
1100     memcpy(&res, &bytebuffer[bytebuffer_offset], 4);
1101    #else
1102     res = bytebuffer[bytebuffer_offset];
1103     res |= bytebuffer[bytebuffer_offset + 1] << 8;
1104     res |= bytebuffer[bytebuffer_offset + 2] << 16;
1105     res |= bytebuffer[bytebuffer_offset + 3] << 24;
1106    #endif
1107     bytebuffer_offset += 4;
1108     return res;
1109    }
1110    
1111    static int check_header_gzip(STATE_PARAM unpack_info_t *info)
1112    {
1113     union {
1114     unsigned char raw[8];
1115     struct {
1116     uint8_t gz_method;
1117     uint8_t flags;
1118     uint32_t mtime;
1119     uint8_t xtra_flags_UNUSED;
1120     uint8_t os_flags_UNUSED;
1121     } __attribute__((packed)) formatted;
1122     } header;
1123     struct BUG_header {
1124     char BUG_header[sizeof(header) == 8 ? 1 : -1];
1125     };
1126    
1127     /*
1128     * Rewind bytebuffer. We use the beginning because the header has 8
1129     * bytes, leaving enough for unwinding afterwards.
1130     */
1131     bytebuffer_size -= bytebuffer_offset;
1132     memmove(bytebuffer, &bytebuffer[bytebuffer_offset], bytebuffer_size);
1133     bytebuffer_offset = 0;
1134    
1135     if (!top_up(PASS_STATE 8))
1136     return 0;
1137     memcpy(header.raw, &bytebuffer[bytebuffer_offset], 8);
1138     bytebuffer_offset += 8;
1139    
1140     /* Check the compression method */
1141     if (header.formatted.gz_method != 8) {
1142     return 0;
1143     }
1144    
1145     if (header.formatted.flags & 0x04) {
1146     /* bit 2 set: extra field present */
1147     unsigned extra_short;
1148    
1149     if (!top_up(PASS_STATE 2))
1150     return 0;
1151     extra_short = buffer_read_le_u16(PASS_STATE_ONLY);
1152     if (!top_up(PASS_STATE extra_short))
1153     return 0;
1154     /* Ignore extra field */
1155     bytebuffer_offset += extra_short;
1156     }
1157    
1158     /* Discard original name and file comment if any */
1159     /* bit 3 set: original file name present */
1160     /* bit 4 set: file comment present */
1161     if (header.formatted.flags & 0x18) {
1162     while (1) {
1163     do {
1164     if (!top_up(PASS_STATE 1))
1165     return 0;
1166     } while (bytebuffer[bytebuffer_offset++] != 0);
1167     if ((header.formatted.flags & 0x18) != 0x18)
1168     break;
1169     header.formatted.flags &= ~0x18;
1170     }
1171     }
1172    
1173     if (info)
1174     info->mtime = SWAP_LE32(header.formatted.mtime);
1175    
1176     /* Read the header checksum */
1177     if (header.formatted.flags & 0x02) {
1178     if (!top_up(PASS_STATE 2))
1179     return 0;
1180     bytebuffer_offset += 2;
1181     }
1182     return 1;
1183    }
1184    
1185    USE_DESKTOP(long long) int FAST_FUNC
1186    unpack_gz_stream_with_info(int in, int out, unpack_info_t *info)
1187  {  {
1188   uint32_t stored_crc = 0;   uint32_t v32;
  unsigned count;  
1189   USE_DESKTOP(long long) int n;   USE_DESKTOP(long long) int n;
1190   DECLARE_STATE;   DECLARE_STATE;
1191    
1192   ALLOC_STATE;   n = 0;
   
  bytebuffer_max = 0x8000;  
  n = inflate_unzip_internal(PASS_STATE in, out);  
1193    
1194   if (n < 0) goto ret;   ALLOC_STATE;
1195     to_read = -1;
1196    // bytebuffer_max = 0x8000;
1197     bytebuffer = xmalloc(bytebuffer_max);
1198     gunzip_src_fd = in;
1199    
1200   /* top up the input buffer with the rest of the trailer */   again:
1201   count = bytebuffer_size - bytebuffer_offset;   if (!check_header_gzip(PASS_STATE info)) {
1202   if (count < 8) {   bb_error_msg("corrupted data");
1203   xread(in, &bytebuffer[bytebuffer_size], 8 - count);   n = -1;
1204  //shouldn't we propagate error?   goto ret;
  bytebuffer_size += 8 - count;  
1205   }   }
1206   for (count = 0; count != 4; count++) {   n += inflate_unzip_internal(PASS_STATE in, out);
1207   stored_crc |= (bytebuffer[bytebuffer_offset] << (count * 8));   if (n < 0)
1208   bytebuffer_offset++;   goto ret;
1209    
1210     if (!top_up(PASS_STATE 8)) {
1211     bb_error_msg("corrupted data");
1212     n = -1;
1213     goto ret;
1214   }   }
1215    
1216   /* Validate decompression - crc */   /* Validate decompression - crc */
1217   if (stored_crc != (~gunzip_crc)) {   v32 = buffer_read_le_u32(PASS_STATE_ONLY);
1218     if ((~gunzip_crc) != v32) {
1219   bb_error_msg("crc error");   bb_error_msg("crc error");
1220   n = -1;   n = -1;
1221   goto ret;   goto ret;
1222   }   }
1223    
1224   /* Validate decompression - size */   /* Validate decompression - size */
1225   if (gunzip_bytes_out !=   v32 = buffer_read_le_u32(PASS_STATE_ONLY);
1226   (bytebuffer[bytebuffer_offset] | (bytebuffer[bytebuffer_offset+1] << 8) |   if ((uint32_t)gunzip_bytes_out != v32) {
  (bytebuffer[bytebuffer_offset+2] << 16) | (bytebuffer[bytebuffer_offset+3] << 24))  
  ) {  
1227   bb_error_msg("incorrect length");   bb_error_msg("incorrect length");
1228   n = -1;   n = -1;
1229   }   }
1230    
1231     if (!top_up(PASS_STATE 2))
1232     goto ret; /* EOF */
1233    
1234     if (bytebuffer[bytebuffer_offset] == 0x1f
1235     && bytebuffer[bytebuffer_offset + 1] == 0x8b
1236     ) {
1237     bytebuffer_offset += 2;
1238     goto again;
1239     }
1240     /* GNU gzip says: */
1241     /*bb_error_msg("decompression OK, trailing garbage ignored");*/
1242    
1243   ret:   ret:
1244   free(bytebuffer);   free(bytebuffer);
1245   DEALLOC_STATE;   DEALLOC_STATE;
1246   return n;   return n;
1247  }  }
1248    
1249    USE_DESKTOP(long long) int FAST_FUNC
1250    unpack_gz_stream(int in, int out)
1251    {
1252     return unpack_gz_stream_with_info(in, out, NULL);
1253    }

Legend:
Removed from v.532  
changed lines
  Added in v.816