blob: 424fe51902420892f41094e78a0992f89ac79949 [file] [log] [blame]
Glenn L McGrath7fd92942001-04-11 03:11:33 +00001/* vi: set sw=4 ts=4: */
2/*
3 * gunzip implementation for busybox
4 *
5 * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
6 *
7 * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
8 * based on gzip sources
9 *
Eric Andersencb81e642003-07-14 21:21:08 +000010 * Adjusted further by Erik Andersen <andersen@codepoet.org> to support
Eric Andersendb930942001-12-06 08:20:14 +000011 * files as well as stdin/stdout, and to generally behave itself wrt
Glenn L McGrath7fd92942001-04-11 03:11:33 +000012 * command line handling.
13 *
14 * General cleanup to better adhere to the style guide and make use of standard
Glenn L McGrathc6992fe2004-04-25 05:11:19 +000015 * busybox functions by Glenn McGrath <bug1@iinet.net.au>
Eric Andersenc7bda1c2004-03-15 08:29:22 +000016 *
Glenn L McGrath83bf47c2002-11-20 22:00:31 +000017 * read_gz interface + associated hacking by Laurence Anderson
18 *
Paul Fox0840b762005-07-20 20:26:49 +000019 * Fixed huft_build() so decoding end-of-block code does not grab more bits
20 * than necessary (this is required by unzip applet), added inflate_cleanup()
21 * to free leaked bytebuffer memory (used in unzip.c), and some minor style
22 * guide cleanups by Ed Clark
23 *
Glenn L McGrath7fd92942001-04-11 03:11:33 +000024 * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
25 * Copyright (C) 1992-1993 Jean-loup Gailly
26 * The unzip code was written and put in the public domain by Mark Adler.
27 * Portions of the lzw code are derived from the public domain 'compress'
28 * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
29 * Ken Turkowski, Dave Mack and Peter Jannesen.
30 *
Glenn L McGrath7fd92942001-04-11 03:11:33 +000031 * See the file algorithm.doc for the compression algorithms and file formats.
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000032 *
Rob Landley11c7a7b2006-06-25 22:39:24 +000033 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrath7fd92942001-04-11 03:11:33 +000034 */
35
Rob Landley11c7a7b2006-06-25 22:39:24 +000036#include "libbb.h"
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000037#include "unarchive.h"
Glenn L McGrath7fd92942001-04-11 03:11:33 +000038
39typedef struct huft_s {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +000040 unsigned char e; /* number of extra bits or operation */
41 unsigned char b; /* number of bits in this code or subcode */
Glenn L McGrath7fd92942001-04-11 03:11:33 +000042 union {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +000043 unsigned short n; /* literal, length base, or distance base */
Glenn L McGrath7fd92942001-04-11 03:11:33 +000044 struct huft_s *t; /* pointer to next level of table */
45 } v;
46} huft_t;
47
Denis Vlasenkod1a19af2007-01-05 23:58:45 +000048enum {
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
Denis Vlasenkobb3d0fa2007-01-03 01:57:25 +000057
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +000058/* 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
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000068
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000069
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +000070typedef struct state_t {
71 off_t gunzip_bytes_out; /* number of output bytes */
72 uint32_t gunzip_crc;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000073
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +000074 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 smallint resume_copy;
102
103 /* private data of inflate_get_next_window() */
104 smallint method; /* Method == -1 for stored, -2 for codes */
105 smallint need_another_block;
106 smallint end_reached;
107
108 /* private data of inflate_stored() */
109 unsigned inflate_stored_n;
110 unsigned inflate_stored_b;
111 unsigned inflate_stored_k;
112 unsigned inflate_stored_w;
113} state_t;
114#define gunzip_bytes_out (S()gunzip_bytes_out )
115#define gunzip_crc (S()gunzip_crc )
116#define gunzip_src_fd (S()gunzip_src_fd )
117#define gunzip_outbuf_count (S()gunzip_outbuf_count)
118#define gunzip_window (S()gunzip_window )
119#define gunzip_crc_table (S()gunzip_crc_table )
120#define gunzip_bb (S()gunzip_bb )
121#define gunzip_bk (S()gunzip_bk )
122#define bytebuffer_max (S()bytebuffer_max )
123#define bytebuffer (S()bytebuffer )
124#define bytebuffer_offset (S()bytebuffer_offset )
125#define bytebuffer_size (S()bytebuffer_size )
126#define inflate_codes_ml (S()inflate_codes_ml )
127#define inflate_codes_md (S()inflate_codes_md )
128#define inflate_codes_bb (S()inflate_codes_bb )
129#define inflate_codes_k (S()inflate_codes_k )
130#define inflate_codes_w (S()inflate_codes_w )
131#define inflate_codes_tl (S()inflate_codes_tl )
132#define inflate_codes_td (S()inflate_codes_td )
133#define inflate_codes_bl (S()inflate_codes_bl )
134#define inflate_codes_bd (S()inflate_codes_bd )
135#define resume_copy (S()resume_copy )
136#define method (S()method )
137#define need_another_block (S()need_another_block )
138#define end_reached (S()end_reached )
139#define inflate_stored_n (S()inflate_stored_n )
140#define inflate_stored_b (S()inflate_stored_b )
141#define inflate_stored_k (S()inflate_stored_k )
142#define inflate_stored_w (S()inflate_stored_w )
143#define INIT_STATE ({ bytebuffer_size = 0; method = -1; need_another_block = 1; })
144
145
146/* This is generic part */
147#if STATE_IN_BSS /* Use global data segment */
148#define DECLARE_STATE /*nothing*/
149#define ALLOC_STATE (init_state())
150#define DEALLOC_STATE ((void)0)
151#define S() state.
152#define PASS_STATE /*nothing*/
153#define PASS_STATE_ONLY /*nothing*/
154#define STATE_PARAM /*nothing*/
155#define STATE_PARAM_ONLY void
156static state_t state;
157static void init_state(void)
158{
159 INIT_STATE;
160}
161#endif
162
163#if STATE_IN_MALLOC /* Use malloc space */
164#define DECLARE_STATE state_t *state
165#define ALLOC_STATE (state = alloc_state())
166#define DEALLOC_STATE free(state)
167#define S() state->
168#define PASS_STATE state,
169#define PASS_STATE_ONLY state
170#define STATE_PARAM state_t *state,
171#define STATE_PARAM_ONLY state_t *state
172static state_t* alloc_state(void)
173{
174 state_t* state = xzalloc(sizeof(*state));
175 INIT_STATE;
176 return state;
177}
178#endif
179
Glenn L McGrathcc616922003-02-09 04:46:34 +0000180
Eric Andersen044228d2001-07-17 01:12:36 +0000181static const unsigned short mask_bits[] = {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000182 0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000183 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
184};
185
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000186/* Copy lengths for literal codes 257..285 */
187static const unsigned short cplens[] = {
188 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59,
Paul Fox0840b762005-07-20 20:26:49 +0000189 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000190};
191
192/* note: see note #13 above about the 258 in this list. */
193/* Extra bits for literal codes 257..285 */
194static const unsigned char cplext[] = {
195 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,
Paul Fox0840b762005-07-20 20:26:49 +0000196 5, 5, 5, 0, 99, 99
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000197}; /* 99 == invalid */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000198
199/* Copy offsets for distance codes 0..29 */
200static const unsigned short cpdist[] = {
201 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513,
Paul Fox0840b762005-07-20 20:26:49 +0000202 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000203};
204
205/* Extra bits for distance codes */
206static const unsigned char cpdext[] = {
207 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10,
Paul Fox0840b762005-07-20 20:26:49 +0000208 11, 11, 12, 12, 13, 13
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000209};
210
211/* Tables for deflate from PKZIP's appnote.txt. */
212/* Order of the bit length code lengths */
213static const unsigned char border[] = {
214 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
215};
216
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000217static unsigned fill_bitbuffer(STATE_PARAM unsigned bitbuffer, unsigned *current, const unsigned required)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000218{
219 while (*current < required) {
Glenn L McGrath5699b852003-11-15 23:19:05 +0000220 if (bytebuffer_offset >= bytebuffer_size) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000221 /* Leave the first 4 bytes empty so we can always unwind the bitbuffer
Glenn L McGrath5699b852003-11-15 23:19:05 +0000222 * to the front of the bytebuffer, leave 4 bytes free at end of tail
223 * so we can easily top up buffer in check_trailer_gzip() */
Denis Vlasenko447b5432007-01-05 19:49:02 +0000224 bytebuffer_size = safe_read(gunzip_src_fd, &bytebuffer[4], bytebuffer_max - 8);
225 if (1 > bytebuffer_size)
226//shouldn't we propagate error?
Manuel Novoa III 0d8c6522005-03-01 19:29:29 +0000227 bb_error_msg_and_die("unexpected end of file");
Manuel Novoa III 0d8c6522005-03-01 19:29:29 +0000228 bytebuffer_size += 4;
Glenn L McGrath5699b852003-11-15 23:19:05 +0000229 bytebuffer_offset = 4;
230 }
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000231 bitbuffer |= ((unsigned) bytebuffer[bytebuffer_offset]) << *current;
Glenn L McGratheda4f532002-11-24 06:01:20 +0000232 bytebuffer_offset++;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000233 *current += 8;
234 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000235 return bitbuffer;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000236}
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000237
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000238/*
239 * Free the malloc'ed tables built by huft_build(), which makes a linked
240 * list of the tables it made, with the links in a dummy first entry of
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000241 * each table.
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000242 * t: table to free
243 */
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000244static void huft_free(huft_t * p)
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000245{
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000246 huft_t *q;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000247
248 /* Go through linked list, freeing from the malloced (t[-1]) address. */
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000249 while (p) {
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000250 q = (--p)->v.t;
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000251 free(p);
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000252 p = q;
253 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000254}
255
256/* Given a list of code lengths and a maximum table size, make a set of
257 * tables to decode that set of codes. Return zero on success, one if
258 * the given code set is incomplete (the tables are still built in this
259 * case), two if the input is invalid (all zero length codes or an
260 * oversubscribed set of lengths), and three if not enough memory.
261 *
262 * b: code lengths in bits (all assumed <= BMAX)
263 * n: number of codes (assumed <= N_MAX)
264 * s: number of simple-valued codes (0..s-1)
265 * d: list of base values for non-simple codes
266 * e: list of extra bits for non-simple codes
267 * t: result: starting table
268 * m: maximum lookup bits, returns actual
269 */
Denis Vlasenkocd42cb82007-01-05 23:56:53 +0000270static int huft_build(unsigned *b, const unsigned n,
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000271 const unsigned s, const unsigned short *d,
272 const unsigned char *e, huft_t ** t, unsigned *m)
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000273{
Paul Fox0840b762005-07-20 20:26:49 +0000274 unsigned a; /* counter for codes of length k */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000275 unsigned c[BMAX + 1]; /* bit length count table */
Paul Fox0840b762005-07-20 20:26:49 +0000276 unsigned eob_len; /* length of end-of-block code (value 256) */
277 unsigned f; /* i repeats in table every f entries */
278 int g; /* maximum code length */
Rob Landley11c7a7b2006-06-25 22:39:24 +0000279 int htl; /* table level */
280 unsigned i; /* counter, current code */
281 unsigned j; /* counter */
282 int k; /* number of bits in current code */
283 unsigned *p; /* pointer into c[], b[], or v[] */
284 huft_t *q; /* points to current table */
Paul Fox0840b762005-07-20 20:26:49 +0000285 huft_t r; /* table entry for structure assignment */
286 huft_t *u[BMAX]; /* table stack */
287 unsigned v[N_MAX]; /* values in order of bit length */
288 int ws[BMAX+1]; /* bits decoded stack */
Rob Landley11c7a7b2006-06-25 22:39:24 +0000289 int w; /* bits decoded */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000290 unsigned x[BMAX + 1]; /* bit offsets, then code stack */
Paul Fox0840b762005-07-20 20:26:49 +0000291 unsigned *xp; /* pointer into x */
292 int y; /* number of dummy codes added */
293 unsigned z; /* number of entries in current table */
294
295 /* Length of EOB code, if any */
296 eob_len = n > 256 ? b[256] : BMAX;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000297
298 /* Generate counts for each bit length */
Denis Vlasenko447b5432007-01-05 19:49:02 +0000299 memset(c, 0, sizeof(c));
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000300 p = b;
301 i = n;
302 do {
Paul Fox0840b762005-07-20 20:26:49 +0000303 c[*p]++; /* assume all entries <= BMAX */
304 p++; /* Can't combine with above line (Solaris bug) */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000305 } while (--i);
Paul Fox0840b762005-07-20 20:26:49 +0000306 if (c[0] == n) { /* null input--all zero length codes */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000307 *t = NULL;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000308 *m = 0;
Rob Landleyeb00afb2006-02-20 02:18:03 +0000309 return 2;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000310 }
311
312 /* Find minimum and maximum length, bound *m by those */
Paul Fox0840b762005-07-20 20:26:49 +0000313 for (j = 1; (c[j] == 0) && (j <= BMAX); j++);
314 k = j; /* minimum code length */
315 for (i = BMAX; (c[i] == 0) && i; i--);
316 g = i; /* maximum code length */
317 *m = (*m < j) ? j : ((*m > i) ? i : *m);
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000318
319 /* Adjust last length count to fill out codes, if needed */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000320 for (y = 1 << j; j < i; j++, y <<= 1) {
Denis Vlasenko447b5432007-01-05 19:49:02 +0000321 y -= c[j];
322 if (y < 0) {
Paul Fox0840b762005-07-20 20:26:49 +0000323 return 2; /* bad input: more codes than bits */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000324 }
325 }
Denis Vlasenko447b5432007-01-05 19:49:02 +0000326 y -= c[i];
327 if (y < 0) {
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000328 return 2;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000329 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000330 c[i] += y;
331
332 /* Generate starting offsets into the value table for each length */
333 x[1] = j = 0;
334 p = c + 1;
335 xp = x + 2;
Paul Fox0840b762005-07-20 20:26:49 +0000336 while (--i) { /* note that i == g from above */
Denis Vlasenko447b5432007-01-05 19:49:02 +0000337 j += *p++;
338 *xp++ = j;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000339 }
340
341 /* Make a table of values in order of bit lengths */
342 p = b;
343 i = 0;
344 do {
Denis Vlasenko447b5432007-01-05 19:49:02 +0000345 j = *p++;
346 if (j != 0) {
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000347 v[x[j]++] = i;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000348 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000349 } while (++i < n);
350
351 /* Generate the Huffman codes and for each, make the table entries */
Paul Fox0840b762005-07-20 20:26:49 +0000352 x[0] = i = 0; /* first Huffman code is zero */
353 p = v; /* grab values in bit order */
Rob Landley11c7a7b2006-06-25 22:39:24 +0000354 htl = -1; /* no tables yet--level -1 */
Paul Fox0840b762005-07-20 20:26:49 +0000355 w = ws[0] = 0; /* bits decoded */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000356 u[0] = NULL; /* just to keep compilers happy */
357 q = NULL; /* ditto */
Paul Fox0840b762005-07-20 20:26:49 +0000358 z = 0; /* ditto */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000359
360 /* go through the bit lengths (k already is bits in shortest code) */
361 for (; k <= g; k++) {
362 a = c[k];
363 while (a--) {
364 /* here i is the Huffman code of length k bits for value *p */
365 /* make tables up to required level */
Rob Landley11c7a7b2006-06-25 22:39:24 +0000366 while (k > ws[htl + 1]) {
367 w = ws[++htl];
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000368
Paul Fox0840b762005-07-20 20:26:49 +0000369 /* compute minimum size table less than or equal to *m bits */
Denis Vlasenko447b5432007-01-05 19:49:02 +0000370 z = g - w;
371 z = z > *m ? *m : z; /* upper limit on table size */
372 j = k - w;
373 f = 1 << j;
374 if (f > a + 1) { /* try a k-w bit table */
Paul Fox0840b762005-07-20 20:26:49 +0000375 /* too few codes for k-w bit table */
376 f -= a + 1; /* deduct codes from patterns left */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000377 xp = c + k;
Paul Fox0840b762005-07-20 20:26:49 +0000378 while (++j < z) { /* try smaller tables up to z bits */
Denis Vlasenko447b5432007-01-05 19:49:02 +0000379 f <<= 1;
380 if (f <= *++xp) {
Paul Fox0840b762005-07-20 20:26:49 +0000381 break; /* enough codes to use up j bits */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000382 }
Paul Fox0840b762005-07-20 20:26:49 +0000383 f -= *xp; /* else deduct codes from patterns */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000384 }
385 }
Paul Fox0840b762005-07-20 20:26:49 +0000386 j = (w + j > eob_len && w < eob_len) ? eob_len - w : j; /* make EOB code end at table */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000387 z = 1 << j; /* table entries for j-bit table */
Rob Landley11c7a7b2006-06-25 22:39:24 +0000388 ws[htl+1] = w + j; /* set bits decoded in stack */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000389
390 /* allocate and link in new table */
Denis Vlasenko4cccc032006-12-22 18:37:07 +0000391 q = xzalloc((z + 1) * sizeof(huft_t));
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000392 *t = q + 1; /* link to list for huft_free() */
Rob Landley11c7a7b2006-06-25 22:39:24 +0000393 t = &(q->v.t);
394 u[htl] = ++q; /* table starts after link */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000395
396 /* connect to last table, if there is one */
Rob Landley11c7a7b2006-06-25 22:39:24 +0000397 if (htl) {
398 x[htl] = i; /* save pattern for backing up */
399 r.b = (unsigned char) (w - ws[htl - 1]); /* bits to dump before this table */
Paul Fox0840b762005-07-20 20:26:49 +0000400 r.e = (unsigned char) (16 + j); /* bits in this table */
401 r.v.t = q; /* pointer to this table */
Rob Landley11c7a7b2006-06-25 22:39:24 +0000402 j = (i & ((1 << w) - 1)) >> ws[htl - 1];
403 u[htl - 1][j] = r; /* connect to last table */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000404 }
405 }
406
407 /* set up table entry in r */
408 r.b = (unsigned char) (k - w);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000409 if (p >= v + n) {
Paul Fox0840b762005-07-20 20:26:49 +0000410 r.e = 99; /* out of values--invalid code */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000411 } else if (*p < s) {
Paul Fox0840b762005-07-20 20:26:49 +0000412 r.e = (unsigned char) (*p < 256 ? 16 : 15); /* 256 is EOB code */
413 r.v.n = (unsigned short) (*p++); /* simple code is just the value */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000414 } else {
Paul Fox0840b762005-07-20 20:26:49 +0000415 r.e = (unsigned char) e[*p - s]; /* non-simple--look up in lists */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000416 r.v.n = d[*p++ - s];
417 }
418
419 /* fill code-like entries with r */
420 f = 1 << (k - w);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000421 for (j = i >> w; j < z; j += f) {
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000422 q[j] = r;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000423 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000424
425 /* backwards increment the k-bit code i */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000426 for (j = 1 << (k - 1); i & j; j >>= 1) {
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000427 i ^= j;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000428 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000429 i ^= j;
430
431 /* backup over finished tables */
Rob Landley11c7a7b2006-06-25 22:39:24 +0000432 while ((i & ((1 << w) - 1)) != x[htl]) {
433 w = ws[--htl];
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000434 }
435 }
436 }
Paul Fox0840b762005-07-20 20:26:49 +0000437
438 /* return actual size of base table */
439 *m = ws[1];
440
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000441 /* Return true (1) if we were given an incomplete table */
442 return y != 0 && g != 1;
443}
444
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000445
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000446/*
447 * inflate (decompress) the codes in a deflated (compressed) block.
448 * Return an error code or zero if it all goes ok.
449 *
450 * tl, td: literal/length and distance decoder tables
451 * bl, bd: number of bits decoded by tl[] and td[]
452 */
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000453/* called once from inflate_block */
454#define ml inflate_codes_ml
455#define md inflate_codes_md
456#define bb inflate_codes_bb
457#define k inflate_codes_k
458#define w inflate_codes_w
459#define tl inflate_codes_tl
460#define td inflate_codes_td
461#define bl inflate_codes_bl
462#define bd inflate_codes_bd
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000463static void inflate_codes_setup(STATE_PARAM huft_t * my_tl, huft_t * my_td, const unsigned my_bl, const unsigned my_bd)
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000464{
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000465 tl = my_tl;
466 td = my_td;
467 bl = my_bl;
468 bd = my_bd;
469 /* make local copies of globals */
470 bb = gunzip_bb; /* initialize bit buffer */
471 k = gunzip_bk;
472 w = gunzip_outbuf_count; /* initialize gunzip_window position */
473 /* inflate the coded data */
474 ml = mask_bits[bl]; /* precompute masks for speed */
475 md = mask_bits[bd];
476}
477/* called once from inflate_get_next_window */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000478static int inflate_codes(STATE_PARAM_ONLY)
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000479{
480 unsigned e; /* table entry flag/number of extra bits */
481 huft_t *t; /* pointer to table entry */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000482
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000483 if (resume_copy) goto do_copy;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000484
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000485 while (1) { /* do until end of block */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000486 bb = fill_bitbuffer(PASS_STATE bb, &k, bl);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000487 t = tl + ((unsigned) bb & ml);
Denis Vlasenko447b5432007-01-05 19:49:02 +0000488 e = t->e;
489 if (e > 16)
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000490 do {
491 if (e == 99) {
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000492//shouldn't we propagate error?
Bernhard Reutner-Fischer0b42a6a2005-10-07 11:34:50 +0000493 bb_error_msg_and_die("inflate_codes error 1");
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000494 }
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000495 bb >>= t->b;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000496 k -= t->b;
497 e -= 16;
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000498 bb = fill_bitbuffer(PASS_STATE bb, &k, e);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000499 t = t->v.t + ((unsigned) bb & mask_bits[e]);
Denis Vlasenko447b5432007-01-05 19:49:02 +0000500 e = t->e;
501 } while (e > 16);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000502 bb >>= t->b;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000503 k -= t->b;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000504 if (e == 16) { /* then it's a literal */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000505 gunzip_window[w++] = (unsigned char) t->v.n;
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000506 if (w == GUNZIP_WSIZE) {
507 gunzip_outbuf_count = w;
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000508 //flush_gunzip_window();
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000509 w = 0;
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000510 return 1; // We have a block to read
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000511 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000512 } else { /* it's an EOB or a length */
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000513 /* length and index for copy */
514 unsigned n = n; /* for gcc */
515 unsigned d = d; /* for gcc */
516
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000517 /* exit if end of block */
518 if (e == 15) {
519 break;
520 }
521
522 /* get length of block to copy */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000523 bb = fill_bitbuffer(PASS_STATE bb, &k, e);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000524 n = t->v.n + ((unsigned) bb & mask_bits[e]);
525 bb >>= e;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000526 k -= e;
527
528 /* decode distance of block to copy */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000529 bb = fill_bitbuffer(PASS_STATE bb, &k, bd);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000530 t = td + ((unsigned) bb & md);
Denis Vlasenko447b5432007-01-05 19:49:02 +0000531 e = t->e;
532 if (e > 16)
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000533 do {
534 if (e == 99)
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000535//shouldn't we propagate error?
Bernhard Reutner-Fischer0b42a6a2005-10-07 11:34:50 +0000536 bb_error_msg_and_die("inflate_codes error 2");
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000537 bb >>= t->b;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000538 k -= t->b;
539 e -= 16;
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000540 bb = fill_bitbuffer(PASS_STATE bb, &k, e);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000541 t = t->v.t + ((unsigned) bb & mask_bits[e]);
Denis Vlasenko447b5432007-01-05 19:49:02 +0000542 e = t->e;
543 } while (e > 16);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000544 bb >>= t->b;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000545 k -= t->b;
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000546 bb = fill_bitbuffer(PASS_STATE bb, &k, e);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000547 d = w - t->v.n - ((unsigned) bb & mask_bits[e]);
548 bb >>= e;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000549 k -= e;
550
551 /* do the copy */
Denis Vlasenko447b5432007-01-05 19:49:02 +0000552 do_copy:
553 do {
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000554 /* Was: n -= (e = (e = GUNZIP_WSIZE - ((d &= GUNZIP_WSIZE - 1) > w ? d : w)) > n ? n : e); */
Denis Vlasenko447b5432007-01-05 19:49:02 +0000555 /* Who wrote THAT?? rewritten as: */
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000556 d &= GUNZIP_WSIZE - 1;
557 e = GUNZIP_WSIZE - (d > w ? d : w);
Denis Vlasenko447b5432007-01-05 19:49:02 +0000558 if (e > n) e = n;
559 n -= e;
560
561 /* copy to new buffer to prevent possible overwrite */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000562 if (w - d >= e) { /* (this test assumes unsigned comparison) */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000563 memcpy(gunzip_window + w, gunzip_window + d, e);
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000564 w += e;
565 d += e;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000566 } else {
Denis Vlasenko447b5432007-01-05 19:49:02 +0000567 /* do it slow to avoid memcpy() overlap */
568 /* !NOMEMCPY */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000569 do {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000570 gunzip_window[w++] = gunzip_window[d++];
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000571 } while (--e);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000572 }
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000573 if (w == GUNZIP_WSIZE) {
574 gunzip_outbuf_count = w;
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000575 resume_copy = (n != 0);
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000576 //flush_gunzip_window();
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000577 w = 0;
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000578 return 1;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000579 }
580 } while (n);
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000581 resume_copy = 0;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000582 }
583 }
584
585 /* restore the globals from the locals */
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000586 gunzip_outbuf_count = w; /* restore global gunzip_window pointer */
587 gunzip_bb = bb; /* restore global bit buffer */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000588 gunzip_bk = k;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000589
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000590 /* normally just after call to inflate_codes, but save code by putting it here */
591 /* free the decoding tables, return */
592 huft_free(tl);
593 huft_free(td);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000594
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000595 /* done */
596 return 0;
597}
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000598#undef ml
599#undef md
600#undef bb
601#undef k
602#undef w
603#undef tl
604#undef td
605#undef bl
606#undef bd
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000607
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000608
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000609/* called once from inflate_block */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000610static void inflate_stored_setup(STATE_PARAM int my_n, int my_b, int my_k)
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000611{
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000612 inflate_stored_n = my_n;
613 inflate_stored_b = my_b;
614 inflate_stored_k = my_k;
615 /* initialize gunzip_window position */
616 inflate_stored_w = gunzip_outbuf_count;
617}
618/* called once from inflate_get_next_window */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000619static int inflate_stored(STATE_PARAM_ONLY)
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000620{
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000621 /* read and output the compressed data */
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000622 while (inflate_stored_n--) {
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000623 inflate_stored_b = fill_bitbuffer(PASS_STATE inflate_stored_b, &inflate_stored_k, 8);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000624 gunzip_window[inflate_stored_w++] = (unsigned char) inflate_stored_b;
625 if (inflate_stored_w == GUNZIP_WSIZE) {
626 gunzip_outbuf_count = inflate_stored_w;
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000627 //flush_gunzip_window();
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000628 inflate_stored_w = 0;
629 inflate_stored_b >>= 8;
630 inflate_stored_k -= 8;
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000631 return 1; // We have a block
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000632 }
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000633 inflate_stored_b >>= 8;
634 inflate_stored_k -= 8;
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000635 }
636
637 /* restore the globals from the locals */
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000638 gunzip_outbuf_count = inflate_stored_w; /* restore global gunzip_window pointer */
639 gunzip_bb = inflate_stored_b; /* restore global bit buffer */
640 gunzip_bk = inflate_stored_k;
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000641 return 0; // Finished
642}
643
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000644
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000645/*
646 * decompress an inflated block
647 * e: last block flag
648 *
649 * GLOBAL VARIABLES: bb, kk,
650 */
Denis Vlasenko447b5432007-01-05 19:49:02 +0000651/* Return values: -1 = inflate_stored, -2 = inflate_codes */
652/* One callsite in inflate_get_next_window */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000653static int inflate_block(STATE_PARAM smallint *e)
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000654{
655 unsigned t; /* block type */
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000656 unsigned b; /* bit buffer */
657 unsigned k; /* number of bits in bit buffer */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000658
659 /* make local bit buffer */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000660
661 b = gunzip_bb;
662 k = gunzip_bk;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000663
664 /* read in last block bit */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000665 b = fill_bitbuffer(PASS_STATE b, &k, 1);
666 *e = b & 1;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000667 b >>= 1;
668 k -= 1;
669
670 /* read in block type */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000671 b = fill_bitbuffer(PASS_STATE b, &k, 2);
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000672 t = (unsigned) b & 3;
673 b >>= 2;
674 k -= 2;
675
676 /* restore the global bit buffer */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000677 gunzip_bb = b;
678 gunzip_bk = k;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000679
680 /* inflate that block type */
681 switch (t) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000682 case 0: /* Inflate stored */
683 {
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000684 unsigned n; /* number of bytes in block */
685 unsigned b_stored; /* bit buffer */
686 unsigned k_stored; /* number of bits in bit buffer */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000687
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000688 /* make local copies of globals */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000689 b_stored = gunzip_bb; /* initialize bit buffer */
690 k_stored = gunzip_bk;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000691
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000692 /* go to byte boundary */
693 n = k_stored & 7;
694 b_stored >>= n;
695 k_stored -= n;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000696
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000697 /* get the length and its complement */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000698 b_stored = fill_bitbuffer(PASS_STATE b_stored, &k_stored, 16);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000699 n = ((unsigned) b_stored & 0xffff);
700 b_stored >>= 16;
701 k_stored -= 16;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000702
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000703 b_stored = fill_bitbuffer(PASS_STATE b_stored, &k_stored, 16);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000704 if (n != (unsigned) ((~b_stored) & 0xffff)) {
705 return 1; /* error in compressed data */
706 }
707 b_stored >>= 16;
708 k_stored -= 16;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000709
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000710 inflate_stored_setup(PASS_STATE n, b_stored, k_stored); // Setup inflate_stored
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000711
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000712 return -1;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000713 }
Denis Vlasenkocd42cb82007-01-05 23:56:53 +0000714 case 1:
715 /* Inflate fixed
716 * decompress an inflated type 1 (fixed Huffman codes) block. We should
717 * either replace this with a custom decoder, or at least precompute the
718 * Huffman tables. */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000719 {
720 int i; /* temporary variable */
721 huft_t *tl; /* literal/length code table */
722 huft_t *td; /* distance code table */
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000723 unsigned bl; /* lookup bits for tl */
724 unsigned bd; /* lookup bits for td */
725 unsigned l[288]; /* length list for huft_build */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000726
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000727 /* set up literal table */
728 for (i = 0; i < 144; i++) {
729 l[i] = 8;
730 }
731 for (; i < 256; i++) {
732 l[i] = 9;
733 }
734 for (; i < 280; i++) {
735 l[i] = 7;
736 }
737 for (; i < 288; i++) { /* make a complete, but wrong code set */
738 l[i] = 8;
739 }
740 bl = 7;
Denis Vlasenko447b5432007-01-05 19:49:02 +0000741 i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl);
742 if (i != 0) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000743 return i;
744 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000745
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000746 /* set up distance table */
747 for (i = 0; i < 30; i++) { /* make an incomplete code set */
748 l[i] = 5;
749 }
750 bd = 5;
Denis Vlasenko447b5432007-01-05 19:49:02 +0000751 i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd);
752 if (i > 1) {
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000753 huft_free(tl);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000754 return i;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000755 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000756
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000757 /* decompress until an end-of-block code */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000758 inflate_codes_setup(PASS_STATE tl, td, bl, bd); // Setup inflate_codes
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000759
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000760 /* huft_free code moved into inflate_codes */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000761
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000762 return -2;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000763 }
764 case 2: /* Inflate dynamic */
765 {
766 const int dbits = 6; /* bits in base distance lookup table */
767 const int lbits = 9; /* bits in base literal/length lookup table */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000768
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000769 huft_t *tl; /* literal/length code table */
770 huft_t *td; /* distance code table */
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000771 unsigned i; /* temporary variables */
772 unsigned j;
773 unsigned l; /* last length */
774 unsigned m; /* mask for bit lengths table */
775 unsigned n; /* number of lengths to get */
776 unsigned bl; /* lookup bits for tl */
777 unsigned bd; /* lookup bits for td */
778 unsigned nb; /* number of bit length codes */
779 unsigned nl; /* number of literal/length codes */
780 unsigned nd; /* number of distance codes */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000781
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000782 unsigned ll[286 + 30]; /* literal/length and distance code lengths */
783 unsigned b_dynamic; /* bit buffer */
784 unsigned k_dynamic; /* number of bits in bit buffer */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000785
786 /* make local bit buffer */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000787 b_dynamic = gunzip_bb;
788 k_dynamic = gunzip_bk;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000789
790 /* read in table lengths */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000791 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 5);
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000792 nl = 257 + ((unsigned) b_dynamic & 0x1f); /* number of literal/length codes */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000793
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000794 b_dynamic >>= 5;
795 k_dynamic -= 5;
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000796 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 5);
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000797 nd = 1 + ((unsigned) b_dynamic & 0x1f); /* number of distance codes */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000798
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000799 b_dynamic >>= 5;
800 k_dynamic -= 5;
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000801 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 4);
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000802 nb = 4 + ((unsigned) b_dynamic & 0xf); /* number of bit length codes */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000803
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000804 b_dynamic >>= 4;
805 k_dynamic -= 4;
806 if (nl > 286 || nd > 30) {
807 return 1; /* bad lengths */
808 }
809
810 /* read in bit-length-code lengths */
811 for (j = 0; j < nb; j++) {
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000812 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 3);
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000813 ll[border[j]] = (unsigned) b_dynamic & 7;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000814 b_dynamic >>= 3;
815 k_dynamic -= 3;
816 }
817 for (; j < 19; j++) {
818 ll[border[j]] = 0;
819 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000820
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000821 /* build decoding table for trees--single level, 7 bit lookup */
822 bl = 7;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000823 i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl);
824 if (i != 0) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000825 if (i == 1) {
826 huft_free(tl);
827 }
828 return i; /* incomplete code set */
829 }
830
831 /* read in literal and distance code lengths */
832 n = nl + nd;
833 m = mask_bits[bl];
834 i = l = 0;
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000835 while ((unsigned) i < n) {
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000836 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, (unsigned)bl);
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000837 j = (td = tl + ((unsigned) b_dynamic & m))->b;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000838 b_dynamic >>= j;
839 k_dynamic -= j;
840 j = td->v.n;
841 if (j < 16) { /* length of code in bits (0..15) */
842 ll[i++] = l = j; /* save last length in l */
843 } else if (j == 16) { /* repeat last length 3 to 6 times */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000844 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 2);
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000845 j = 3 + ((unsigned) b_dynamic & 3);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000846 b_dynamic >>= 2;
847 k_dynamic -= 2;
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000848 if ((unsigned) i + j > n) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000849 return 1;
850 }
851 while (j--) {
852 ll[i++] = l;
853 }
854 } else if (j == 17) { /* 3 to 10 zero length codes */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000855 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 3);
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000856 j = 3 + ((unsigned) b_dynamic & 7);
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000857 b_dynamic >>= 3;
858 k_dynamic -= 3;
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000859 if ((unsigned) i + j > n) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000860 return 1;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000861 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000862 while (j--) {
863 ll[i++] = 0;
864 }
865 l = 0;
866 } else { /* j == 18: 11 to 138 zero length codes */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000867 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 7);
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000868 j = 11 + ((unsigned) b_dynamic & 0x7f);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000869 b_dynamic >>= 7;
870 k_dynamic -= 7;
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000871 if ((unsigned) i + j > n) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000872 return 1;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000873 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000874 while (j--) {
875 ll[i++] = 0;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000876 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000877 l = 0;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000878 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000879 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000880
881 /* free decoding table for trees */
882 huft_free(tl);
883
884 /* restore the global bit buffer */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000885 gunzip_bb = b_dynamic;
886 gunzip_bk = k_dynamic;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000887
888 /* build the decoding tables for literal/length and distance codes */
889 bl = lbits;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000890
Denis Vlasenko447b5432007-01-05 19:49:02 +0000891 i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl);
892 if (i != 0) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000893 if (i == 1) {
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000894//shouldn't we propagate error?
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000895 bb_error_msg_and_die("incomplete literal tree");
Denis Vlasenko447b5432007-01-05 19:49:02 +0000896 /* huft_free(tl); */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000897 }
898 return i; /* incomplete code set */
899 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000900
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000901 bd = dbits;
Denis Vlasenko447b5432007-01-05 19:49:02 +0000902 i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd);
903 if (i != 0) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000904 if (i == 1) {
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000905//shouldn't we propagate error?
Manuel Novoa III cad53642003-03-19 09:13:01 +0000906 bb_error_msg_and_die("incomplete distance tree");
Denis Vlasenko447b5432007-01-05 19:49:02 +0000907 /* huft_free(td); */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000908 }
909 huft_free(tl);
910 return i; /* incomplete code set */
911 }
912
913 /* decompress until an end-of-block code */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000914 inflate_codes_setup(PASS_STATE tl, td, bl, bd); // Setup inflate_codes
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000915
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000916 /* huft_free code moved into inflate_codes */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000917
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000918 return -2;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000919 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000920 default:
921 /* bad block type */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000922//shouldn't we propagate error?
Denis Vlasenko6d655be2006-09-06 19:02:46 +0000923 bb_error_msg_and_die("bad block type %d", t);
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000924 }
925}
926
Denis Vlasenko447b5432007-01-05 19:49:02 +0000927/* Two callsites, both in inflate_get_next_window */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000928static void calculate_gunzip_crc(STATE_PARAM_ONLY)
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000929{
930 int n;
931 for (n = 0; n < gunzip_outbuf_count; n++) {
932 gunzip_crc = gunzip_crc_table[((int) gunzip_crc ^ (gunzip_window[n])) & 0xff] ^ (gunzip_crc >> 8);
933 }
934 gunzip_bytes_out += gunzip_outbuf_count;
935}
936
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000937/* One callsite in inflate_unzip_internal */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000938static int inflate_get_next_window(STATE_PARAM_ONLY)
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000939{
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000940 gunzip_outbuf_count = 0;
941
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000942 while (1) {
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000943 int ret;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000944
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000945 if (need_another_block) {
946 if (end_reached) {
947 calculate_gunzip_crc(PASS_STATE_ONLY);
948 end_reached = 0;
949 need_another_block = 1;
950 return 0; /* Last block */
951 }
952 method = inflate_block(PASS_STATE &end_reached);
953 need_another_block = 0;
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000954 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000955
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000956 switch (method) {
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000957 case -1:
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000958 ret = inflate_stored(PASS_STATE_ONLY);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000959 break;
960 case -2:
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000961 ret = inflate_codes(PASS_STATE_ONLY);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000962 break;
963 default:
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000964//shouldn't we propagate error?
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000965 bb_error_msg_and_die("inflate error %d", method);
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000966 }
967
968 if (ret == 1) {
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000969 calculate_gunzip_crc(PASS_STATE_ONLY);
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000970 return 1; // More data left
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000971 }
972 need_another_block = 1; // End of that block
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000973 }
Glenn L McGratheda4f532002-11-24 06:01:20 +0000974 /* Doesnt get here */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000975}
976
Paul Fox0840b762005-07-20 20:26:49 +0000977
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000978/* Called from inflate_gunzip() and inflate_unzip() */
979/* NB: bytebuffer is allocated here but freeing it is left to the caller! */
Denis Vlasenkocd42cb82007-01-05 23:56:53 +0000980static USE_DESKTOP(long long) int
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000981inflate_unzip_internal(STATE_PARAM int in, int out)
Glenn L McGrathfd73b8c2002-11-17 21:33:30 +0000982{
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000983 USE_DESKTOP(long long) int n = 0;
Glenn L McGrath5699b852003-11-15 23:19:05 +0000984 ssize_t nwrote;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000985
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000986 /* Allocate all global buffers (for DYN_ALLOC option) */
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000987 gunzip_window = xmalloc(GUNZIP_WSIZE);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000988 gunzip_outbuf_count = 0;
989 gunzip_bytes_out = 0;
Glenn L McGrath5699b852003-11-15 23:19:05 +0000990 gunzip_src_fd = in;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000991
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000992 /* initialize gunzip_window, bit buffer */
993 gunzip_bk = 0;
994 gunzip_bb = 0;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000995
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000996 /* Create the crc table */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000997 gunzip_crc_table = crc32_filltable(0);
Rob Landleyc57ec372006-04-10 17:07:15 +0000998 gunzip_crc = ~0;
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000999
Glenn L McGrath5699b852003-11-15 23:19:05 +00001000 /* Allocate space for buffer */
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001001 bytebuffer = xmalloc(bytebuffer_max);
Glenn L McGrath5699b852003-11-15 23:19:05 +00001002
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001003 while (1) {
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +00001004 int r = inflate_get_next_window(PASS_STATE_ONLY);
Rob Landley53437472006-07-16 08:14:35 +00001005 nwrote = full_write(out, gunzip_window, gunzip_outbuf_count);
Denis Vlasenko447b5432007-01-05 19:49:02 +00001006 if (nwrote != gunzip_outbuf_count) {
Glenn L McGrath5699b852003-11-15 23:19:05 +00001007 bb_perror_msg("write");
Denis Vlasenkod1a19af2007-01-05 23:58:45 +00001008 n = -1;
1009 goto ret;
Glenn L McGrath5699b852003-11-15 23:19:05 +00001010 }
Denis Vlasenkod1a19af2007-01-05 23:58:45 +00001011 USE_DESKTOP(n += nwrote;)
1012 if (r == 0) break;
Glenn L McGrath5699b852003-11-15 23:19:05 +00001013 }
1014
Glenn L McGrath7ca04f32002-09-25 02:47:48 +00001015 /* Store unused bytes in a global buffer so calling applets can access it */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +00001016 if (gunzip_bk >= 8) {
1017 /* Undo too much lookahead. The next read will be byte aligned
1018 * so we can discard unused bits in the last meaningful byte. */
Glenn L McGratheda4f532002-11-24 06:01:20 +00001019 bytebuffer_offset--;
1020 bytebuffer[bytebuffer_offset] = gunzip_bb & 0xff;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +00001021 gunzip_bb >>= 8;
1022 gunzip_bk -= 8;
Glenn L McGrath7fd92942001-04-11 03:11:33 +00001023 }
Denis Vlasenkod1a19af2007-01-05 23:58:45 +00001024 ret:
1025 /* Cleanup */
1026 free(gunzip_window);
1027 free(gunzip_crc_table);
1028 return n;
Glenn L McGrath7fd92942001-04-11 03:11:33 +00001029}
Glenn L McGrathcc616922003-02-09 04:46:34 +00001030
Denis Vlasenkocd42cb82007-01-05 23:56:53 +00001031
1032USE_DESKTOP(long long) int
1033inflate_unzip(inflate_unzip_result *res, unsigned bufsize, int in, int out)
1034{
1035 USE_DESKTOP(long long) int n;
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +00001036 DECLARE_STATE;
1037
1038 ALLOC_STATE;
Denis Vlasenkocd42cb82007-01-05 23:56:53 +00001039
1040 bytebuffer_max = bufsize + 8;
1041 bytebuffer_offset = 4;
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +00001042 n = inflate_unzip_internal(PASS_STATE in, out);
Denis Vlasenkocd42cb82007-01-05 23:56:53 +00001043
1044 res->crc = gunzip_crc;
1045 res->bytes_out = gunzip_bytes_out;
1046 free(bytebuffer);
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +00001047 DEALLOC_STATE;
Denis Vlasenkocd42cb82007-01-05 23:56:53 +00001048 return n;
1049}
1050
1051
Denis Vlasenko97a8dd32006-10-01 15:55:11 +00001052USE_DESKTOP(long long) int
1053inflate_gunzip(int in, int out)
Glenn L McGrathcc616922003-02-09 04:46:34 +00001054{
Rob Landleyc57ec372006-04-10 17:07:15 +00001055 uint32_t stored_crc = 0;
Denis Vlasenkocc33ef12007-01-05 19:46:04 +00001056 unsigned count;
Denis Vlasenkocd42cb82007-01-05 23:56:53 +00001057 USE_DESKTOP(long long) int n;
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +00001058 DECLARE_STATE;
Glenn L McGrathcc616922003-02-09 04:46:34 +00001059
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +00001060 ALLOC_STATE;
1061
1062 bytebuffer_max = 0x8000;
1063 n = inflate_unzip_internal(PASS_STATE in, out);
Denis Vlasenkocd42cb82007-01-05 23:56:53 +00001064
1065 if (n < 0) goto ret;
Glenn L McGrath5699b852003-11-15 23:19:05 +00001066
Glenn L McGrathcc616922003-02-09 04:46:34 +00001067 /* top up the input buffer with the rest of the trailer */
1068 count = bytebuffer_size - bytebuffer_offset;
1069 if (count < 8) {
Rob Landley53437472006-07-16 08:14:35 +00001070 xread(in, &bytebuffer[bytebuffer_size], 8 - count);
Denis Vlasenko447b5432007-01-05 19:49:02 +00001071//shouldn't we propagate error?
Glenn L McGrathcc616922003-02-09 04:46:34 +00001072 bytebuffer_size += 8 - count;
1073 }
1074 for (count = 0; count != 4; count++) {
1075 stored_crc |= (bytebuffer[bytebuffer_offset] << (count * 8));
1076 bytebuffer_offset++;
1077 }
1078
1079 /* Validate decompression - crc */
Rob Landleyc57ec372006-04-10 17:07:15 +00001080 if (stored_crc != (~gunzip_crc)) {
Glenn L McGrath5699b852003-11-15 23:19:05 +00001081 bb_error_msg("crc error");
Denis Vlasenkocd42cb82007-01-05 23:56:53 +00001082 n = -1;
1083 goto ret;
Glenn L McGrathcc616922003-02-09 04:46:34 +00001084 }
1085
1086 /* Validate decompression - size */
1087 if (gunzip_bytes_out !=
1088 (bytebuffer[bytebuffer_offset] | (bytebuffer[bytebuffer_offset+1] << 8) |
Denis Vlasenko447b5432007-01-05 19:49:02 +00001089 (bytebuffer[bytebuffer_offset+2] << 16) | (bytebuffer[bytebuffer_offset+3] << 24))
1090 ) {
Denis Vlasenko3038ac92006-09-30 19:37:25 +00001091 bb_error_msg("incorrect length");
Denis Vlasenkocd42cb82007-01-05 23:56:53 +00001092 n = -1;
Glenn L McGrathcc616922003-02-09 04:46:34 +00001093 }
Denis Vlasenkocd42cb82007-01-05 23:56:53 +00001094 ret:
1095 free(bytebuffer);
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +00001096 DEALLOC_STATE;
Denis Vlasenkocd42cb82007-01-05 23:56:53 +00001097 return n;
Glenn L McGrathcc616922003-02-09 04:46:34 +00001098}