blob: fb87fe88da5faef5493360a8b22cbfe9dd6cd635 [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;
Denis Vlasenko75735412007-01-10 20:50:04 +0000101 unsigned inflate_codes_nn; /* length and index for copy */
102 unsigned inflate_codes_dd;
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000103 smallint resume_copy;
104
105 /* private data of inflate_get_next_window() */
106 smallint method; /* Method == -1 for stored, -2 for codes */
107 smallint need_another_block;
108 smallint end_reached;
109
110 /* private data of inflate_stored() */
111 unsigned inflate_stored_n;
112 unsigned inflate_stored_b;
113 unsigned inflate_stored_k;
114 unsigned inflate_stored_w;
115} state_t;
116#define gunzip_bytes_out (S()gunzip_bytes_out )
117#define gunzip_crc (S()gunzip_crc )
118#define gunzip_src_fd (S()gunzip_src_fd )
119#define gunzip_outbuf_count (S()gunzip_outbuf_count)
120#define gunzip_window (S()gunzip_window )
121#define gunzip_crc_table (S()gunzip_crc_table )
122#define gunzip_bb (S()gunzip_bb )
123#define gunzip_bk (S()gunzip_bk )
124#define bytebuffer_max (S()bytebuffer_max )
125#define bytebuffer (S()bytebuffer )
126#define bytebuffer_offset (S()bytebuffer_offset )
127#define bytebuffer_size (S()bytebuffer_size )
128#define inflate_codes_ml (S()inflate_codes_ml )
129#define inflate_codes_md (S()inflate_codes_md )
130#define inflate_codes_bb (S()inflate_codes_bb )
131#define inflate_codes_k (S()inflate_codes_k )
132#define inflate_codes_w (S()inflate_codes_w )
133#define inflate_codes_tl (S()inflate_codes_tl )
134#define inflate_codes_td (S()inflate_codes_td )
135#define inflate_codes_bl (S()inflate_codes_bl )
136#define inflate_codes_bd (S()inflate_codes_bd )
Denis Vlasenko75735412007-01-10 20:50:04 +0000137#define inflate_codes_nn (S()inflate_codes_nn )
138#define inflate_codes_dd (S()inflate_codes_dd )
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000139#define resume_copy (S()resume_copy )
140#define method (S()method )
141#define need_another_block (S()need_another_block )
142#define end_reached (S()end_reached )
143#define inflate_stored_n (S()inflate_stored_n )
144#define inflate_stored_b (S()inflate_stored_b )
145#define inflate_stored_k (S()inflate_stored_k )
146#define inflate_stored_w (S()inflate_stored_w )
147#define INIT_STATE ({ bytebuffer_size = 0; method = -1; need_another_block = 1; })
148
149
150/* This is generic part */
151#if STATE_IN_BSS /* Use global data segment */
152#define DECLARE_STATE /*nothing*/
153#define ALLOC_STATE (init_state())
154#define DEALLOC_STATE ((void)0)
155#define S() state.
156#define PASS_STATE /*nothing*/
157#define PASS_STATE_ONLY /*nothing*/
158#define STATE_PARAM /*nothing*/
159#define STATE_PARAM_ONLY void
160static state_t state;
161static void init_state(void)
162{
163 INIT_STATE;
164}
165#endif
166
167#if STATE_IN_MALLOC /* Use malloc space */
168#define DECLARE_STATE state_t *state
169#define ALLOC_STATE (state = alloc_state())
170#define DEALLOC_STATE free(state)
171#define S() state->
172#define PASS_STATE state,
173#define PASS_STATE_ONLY state
174#define STATE_PARAM state_t *state,
175#define STATE_PARAM_ONLY state_t *state
176static state_t* alloc_state(void)
177{
178 state_t* state = xzalloc(sizeof(*state));
179 INIT_STATE;
180 return state;
181}
182#endif
183
Glenn L McGrathcc616922003-02-09 04:46:34 +0000184
Eric Andersen044228d2001-07-17 01:12:36 +0000185static const unsigned short mask_bits[] = {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000186 0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000187 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
188};
189
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000190/* Copy lengths for literal codes 257..285 */
191static const unsigned short cplens[] = {
192 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 +0000193 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000194};
195
196/* note: see note #13 above about the 258 in this list. */
197/* Extra bits for literal codes 257..285 */
198static const unsigned char cplext[] = {
199 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 +0000200 5, 5, 5, 0, 99, 99
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000201}; /* 99 == invalid */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000202
203/* Copy offsets for distance codes 0..29 */
204static const unsigned short cpdist[] = {
205 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 +0000206 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000207};
208
209/* Extra bits for distance codes */
210static const unsigned char cpdext[] = {
211 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 +0000212 11, 11, 12, 12, 13, 13
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000213};
214
215/* Tables for deflate from PKZIP's appnote.txt. */
216/* Order of the bit length code lengths */
217static const unsigned char border[] = {
218 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
219};
220
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000221static unsigned fill_bitbuffer(STATE_PARAM unsigned bitbuffer, unsigned *current, const unsigned required)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000222{
223 while (*current < required) {
Glenn L McGrath5699b852003-11-15 23:19:05 +0000224 if (bytebuffer_offset >= bytebuffer_size) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000225 /* Leave the first 4 bytes empty so we can always unwind the bitbuffer
Glenn L McGrath5699b852003-11-15 23:19:05 +0000226 * to the front of the bytebuffer, leave 4 bytes free at end of tail
227 * so we can easily top up buffer in check_trailer_gzip() */
Denis Vlasenko447b5432007-01-05 19:49:02 +0000228 bytebuffer_size = safe_read(gunzip_src_fd, &bytebuffer[4], bytebuffer_max - 8);
229 if (1 > bytebuffer_size)
230//shouldn't we propagate error?
Manuel Novoa III 0d8c6522005-03-01 19:29:29 +0000231 bb_error_msg_and_die("unexpected end of file");
Manuel Novoa III 0d8c6522005-03-01 19:29:29 +0000232 bytebuffer_size += 4;
Glenn L McGrath5699b852003-11-15 23:19:05 +0000233 bytebuffer_offset = 4;
234 }
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000235 bitbuffer |= ((unsigned) bytebuffer[bytebuffer_offset]) << *current;
Glenn L McGratheda4f532002-11-24 06:01:20 +0000236 bytebuffer_offset++;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000237 *current += 8;
238 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000239 return bitbuffer;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000240}
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000241
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000242/*
243 * Free the malloc'ed tables built by huft_build(), which makes a linked
244 * list of the tables it made, with the links in a dummy first entry of
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000245 * each table.
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000246 * t: table to free
247 */
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000248static void huft_free(huft_t * p)
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000249{
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000250 huft_t *q;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000251
252 /* Go through linked list, freeing from the malloced (t[-1]) address. */
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000253 while (p) {
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000254 q = (--p)->v.t;
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000255 free(p);
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000256 p = q;
257 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000258}
259
260/* Given a list of code lengths and a maximum table size, make a set of
261 * tables to decode that set of codes. Return zero on success, one if
262 * the given code set is incomplete (the tables are still built in this
263 * case), two if the input is invalid (all zero length codes or an
264 * oversubscribed set of lengths), and three if not enough memory.
265 *
266 * b: code lengths in bits (all assumed <= BMAX)
267 * n: number of codes (assumed <= N_MAX)
268 * s: number of simple-valued codes (0..s-1)
269 * d: list of base values for non-simple codes
270 * e: list of extra bits for non-simple codes
271 * t: result: starting table
272 * m: maximum lookup bits, returns actual
273 */
Denis Vlasenkocd42cb82007-01-05 23:56:53 +0000274static int huft_build(unsigned *b, const unsigned n,
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000275 const unsigned s, const unsigned short *d,
276 const unsigned char *e, huft_t ** t, unsigned *m)
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000277{
Paul Fox0840b762005-07-20 20:26:49 +0000278 unsigned a; /* counter for codes of length k */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000279 unsigned c[BMAX + 1]; /* bit length count table */
Paul Fox0840b762005-07-20 20:26:49 +0000280 unsigned eob_len; /* length of end-of-block code (value 256) */
281 unsigned f; /* i repeats in table every f entries */
282 int g; /* maximum code length */
Rob Landley11c7a7b2006-06-25 22:39:24 +0000283 int htl; /* table level */
284 unsigned i; /* counter, current code */
285 unsigned j; /* counter */
286 int k; /* number of bits in current code */
287 unsigned *p; /* pointer into c[], b[], or v[] */
288 huft_t *q; /* points to current table */
Paul Fox0840b762005-07-20 20:26:49 +0000289 huft_t r; /* table entry for structure assignment */
290 huft_t *u[BMAX]; /* table stack */
291 unsigned v[N_MAX]; /* values in order of bit length */
292 int ws[BMAX+1]; /* bits decoded stack */
Rob Landley11c7a7b2006-06-25 22:39:24 +0000293 int w; /* bits decoded */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000294 unsigned x[BMAX + 1]; /* bit offsets, then code stack */
Paul Fox0840b762005-07-20 20:26:49 +0000295 unsigned *xp; /* pointer into x */
296 int y; /* number of dummy codes added */
297 unsigned z; /* number of entries in current table */
298
299 /* Length of EOB code, if any */
300 eob_len = n > 256 ? b[256] : BMAX;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000301
302 /* Generate counts for each bit length */
Denis Vlasenko447b5432007-01-05 19:49:02 +0000303 memset(c, 0, sizeof(c));
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000304 p = b;
305 i = n;
306 do {
Paul Fox0840b762005-07-20 20:26:49 +0000307 c[*p]++; /* assume all entries <= BMAX */
308 p++; /* Can't combine with above line (Solaris bug) */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000309 } while (--i);
Paul Fox0840b762005-07-20 20:26:49 +0000310 if (c[0] == n) { /* null input--all zero length codes */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000311 *t = NULL;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000312 *m = 0;
Rob Landleyeb00afb2006-02-20 02:18:03 +0000313 return 2;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000314 }
315
316 /* Find minimum and maximum length, bound *m by those */
Paul Fox0840b762005-07-20 20:26:49 +0000317 for (j = 1; (c[j] == 0) && (j <= BMAX); j++);
318 k = j; /* minimum code length */
319 for (i = BMAX; (c[i] == 0) && i; i--);
320 g = i; /* maximum code length */
321 *m = (*m < j) ? j : ((*m > i) ? i : *m);
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000322
323 /* Adjust last length count to fill out codes, if needed */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000324 for (y = 1 << j; j < i; j++, y <<= 1) {
Denis Vlasenko447b5432007-01-05 19:49:02 +0000325 y -= c[j];
326 if (y < 0) {
Paul Fox0840b762005-07-20 20:26:49 +0000327 return 2; /* bad input: more codes than bits */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000328 }
329 }
Denis Vlasenko447b5432007-01-05 19:49:02 +0000330 y -= c[i];
331 if (y < 0) {
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000332 return 2;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000333 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000334 c[i] += y;
335
336 /* Generate starting offsets into the value table for each length */
337 x[1] = j = 0;
338 p = c + 1;
339 xp = x + 2;
Paul Fox0840b762005-07-20 20:26:49 +0000340 while (--i) { /* note that i == g from above */
Denis Vlasenko447b5432007-01-05 19:49:02 +0000341 j += *p++;
342 *xp++ = j;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000343 }
344
345 /* Make a table of values in order of bit lengths */
346 p = b;
347 i = 0;
348 do {
Denis Vlasenko447b5432007-01-05 19:49:02 +0000349 j = *p++;
350 if (j != 0) {
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000351 v[x[j]++] = i;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000352 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000353 } while (++i < n);
354
355 /* Generate the Huffman codes and for each, make the table entries */
Paul Fox0840b762005-07-20 20:26:49 +0000356 x[0] = i = 0; /* first Huffman code is zero */
357 p = v; /* grab values in bit order */
Rob Landley11c7a7b2006-06-25 22:39:24 +0000358 htl = -1; /* no tables yet--level -1 */
Paul Fox0840b762005-07-20 20:26:49 +0000359 w = ws[0] = 0; /* bits decoded */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000360 u[0] = NULL; /* just to keep compilers happy */
361 q = NULL; /* ditto */
Paul Fox0840b762005-07-20 20:26:49 +0000362 z = 0; /* ditto */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000363
364 /* go through the bit lengths (k already is bits in shortest code) */
365 for (; k <= g; k++) {
366 a = c[k];
367 while (a--) {
368 /* here i is the Huffman code of length k bits for value *p */
369 /* make tables up to required level */
Rob Landley11c7a7b2006-06-25 22:39:24 +0000370 while (k > ws[htl + 1]) {
371 w = ws[++htl];
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000372
Paul Fox0840b762005-07-20 20:26:49 +0000373 /* compute minimum size table less than or equal to *m bits */
Denis Vlasenko447b5432007-01-05 19:49:02 +0000374 z = g - w;
375 z = z > *m ? *m : z; /* upper limit on table size */
376 j = k - w;
377 f = 1 << j;
378 if (f > a + 1) { /* try a k-w bit table */
Paul Fox0840b762005-07-20 20:26:49 +0000379 /* too few codes for k-w bit table */
380 f -= a + 1; /* deduct codes from patterns left */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000381 xp = c + k;
Paul Fox0840b762005-07-20 20:26:49 +0000382 while (++j < z) { /* try smaller tables up to z bits */
Denis Vlasenko447b5432007-01-05 19:49:02 +0000383 f <<= 1;
384 if (f <= *++xp) {
Paul Fox0840b762005-07-20 20:26:49 +0000385 break; /* enough codes to use up j bits */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000386 }
Paul Fox0840b762005-07-20 20:26:49 +0000387 f -= *xp; /* else deduct codes from patterns */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000388 }
389 }
Paul Fox0840b762005-07-20 20:26:49 +0000390 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 +0000391 z = 1 << j; /* table entries for j-bit table */
Rob Landley11c7a7b2006-06-25 22:39:24 +0000392 ws[htl+1] = w + j; /* set bits decoded in stack */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000393
394 /* allocate and link in new table */
Denis Vlasenko4cccc032006-12-22 18:37:07 +0000395 q = xzalloc((z + 1) * sizeof(huft_t));
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000396 *t = q + 1; /* link to list for huft_free() */
Rob Landley11c7a7b2006-06-25 22:39:24 +0000397 t = &(q->v.t);
398 u[htl] = ++q; /* table starts after link */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000399
400 /* connect to last table, if there is one */
Rob Landley11c7a7b2006-06-25 22:39:24 +0000401 if (htl) {
402 x[htl] = i; /* save pattern for backing up */
403 r.b = (unsigned char) (w - ws[htl - 1]); /* bits to dump before this table */
Paul Fox0840b762005-07-20 20:26:49 +0000404 r.e = (unsigned char) (16 + j); /* bits in this table */
405 r.v.t = q; /* pointer to this table */
Rob Landley11c7a7b2006-06-25 22:39:24 +0000406 j = (i & ((1 << w) - 1)) >> ws[htl - 1];
407 u[htl - 1][j] = r; /* connect to last table */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000408 }
409 }
410
411 /* set up table entry in r */
412 r.b = (unsigned char) (k - w);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000413 if (p >= v + n) {
Paul Fox0840b762005-07-20 20:26:49 +0000414 r.e = 99; /* out of values--invalid code */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000415 } else if (*p < s) {
Paul Fox0840b762005-07-20 20:26:49 +0000416 r.e = (unsigned char) (*p < 256 ? 16 : 15); /* 256 is EOB code */
417 r.v.n = (unsigned short) (*p++); /* simple code is just the value */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000418 } else {
Paul Fox0840b762005-07-20 20:26:49 +0000419 r.e = (unsigned char) e[*p - s]; /* non-simple--look up in lists */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000420 r.v.n = d[*p++ - s];
421 }
422
423 /* fill code-like entries with r */
424 f = 1 << (k - w);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000425 for (j = i >> w; j < z; j += f) {
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000426 q[j] = r;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000427 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000428
429 /* backwards increment the k-bit code i */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000430 for (j = 1 << (k - 1); i & j; j >>= 1) {
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000431 i ^= j;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000432 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000433 i ^= j;
434
435 /* backup over finished tables */
Rob Landley11c7a7b2006-06-25 22:39:24 +0000436 while ((i & ((1 << w) - 1)) != x[htl]) {
437 w = ws[--htl];
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000438 }
439 }
440 }
Paul Fox0840b762005-07-20 20:26:49 +0000441
442 /* return actual size of base table */
443 *m = ws[1];
444
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000445 /* Return true (1) if we were given an incomplete table */
446 return y != 0 && g != 1;
447}
448
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000449
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000450/*
451 * inflate (decompress) the codes in a deflated (compressed) block.
452 * Return an error code or zero if it all goes ok.
453 *
454 * tl, td: literal/length and distance decoder tables
455 * bl, bd: number of bits decoded by tl[] and td[]
456 */
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000457/* called once from inflate_block */
458#define ml inflate_codes_ml
459#define md inflate_codes_md
460#define bb inflate_codes_bb
461#define k inflate_codes_k
462#define w inflate_codes_w
463#define tl inflate_codes_tl
464#define td inflate_codes_td
465#define bl inflate_codes_bl
466#define bd inflate_codes_bd
Denis Vlasenko75735412007-01-10 20:50:04 +0000467#define nn inflate_codes_nn
468#define dd inflate_codes_dd
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000469static 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 +0000470{
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000471 tl = my_tl;
472 td = my_td;
473 bl = my_bl;
474 bd = my_bd;
475 /* make local copies of globals */
476 bb = gunzip_bb; /* initialize bit buffer */
477 k = gunzip_bk;
478 w = gunzip_outbuf_count; /* initialize gunzip_window position */
479 /* inflate the coded data */
480 ml = mask_bits[bl]; /* precompute masks for speed */
481 md = mask_bits[bd];
482}
483/* called once from inflate_get_next_window */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000484static int inflate_codes(STATE_PARAM_ONLY)
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000485{
486 unsigned e; /* table entry flag/number of extra bits */
487 huft_t *t; /* pointer to table entry */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000488
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000489 if (resume_copy) goto do_copy;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000490
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000491 while (1) { /* do until end of block */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000492 bb = fill_bitbuffer(PASS_STATE bb, &k, bl);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000493 t = tl + ((unsigned) bb & ml);
Denis Vlasenko447b5432007-01-05 19:49:02 +0000494 e = t->e;
495 if (e > 16)
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000496 do {
497 if (e == 99) {
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000498//shouldn't we propagate error?
Bernhard Reutner-Fischer0b42a6a2005-10-07 11:34:50 +0000499 bb_error_msg_and_die("inflate_codes error 1");
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000500 }
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000501 bb >>= t->b;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000502 k -= t->b;
503 e -= 16;
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000504 bb = fill_bitbuffer(PASS_STATE bb, &k, e);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000505 t = t->v.t + ((unsigned) bb & mask_bits[e]);
Denis Vlasenko447b5432007-01-05 19:49:02 +0000506 e = t->e;
507 } while (e > 16);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000508 bb >>= t->b;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000509 k -= t->b;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000510 if (e == 16) { /* then it's a literal */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000511 gunzip_window[w++] = (unsigned char) t->v.n;
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000512 if (w == GUNZIP_WSIZE) {
513 gunzip_outbuf_count = w;
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000514 //flush_gunzip_window();
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000515 w = 0;
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000516 return 1; // We have a block to read
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000517 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000518 } else { /* it's an EOB or a length */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000519 /* exit if end of block */
520 if (e == 15) {
521 break;
522 }
523
524 /* get length of block to copy */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000525 bb = fill_bitbuffer(PASS_STATE bb, &k, e);
Denis Vlasenko75735412007-01-10 20:50:04 +0000526 nn = t->v.n + ((unsigned) bb & mask_bits[e]);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000527 bb >>= e;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000528 k -= e;
529
530 /* decode distance of block to copy */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000531 bb = fill_bitbuffer(PASS_STATE bb, &k, bd);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000532 t = td + ((unsigned) bb & md);
Denis Vlasenko447b5432007-01-05 19:49:02 +0000533 e = t->e;
534 if (e > 16)
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000535 do {
536 if (e == 99)
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000537//shouldn't we propagate error?
Bernhard Reutner-Fischer0b42a6a2005-10-07 11:34:50 +0000538 bb_error_msg_and_die("inflate_codes error 2");
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000539 bb >>= t->b;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000540 k -= t->b;
541 e -= 16;
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000542 bb = fill_bitbuffer(PASS_STATE bb, &k, e);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000543 t = t->v.t + ((unsigned) bb & mask_bits[e]);
Denis Vlasenko447b5432007-01-05 19:49:02 +0000544 e = t->e;
545 } while (e > 16);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000546 bb >>= t->b;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000547 k -= t->b;
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000548 bb = fill_bitbuffer(PASS_STATE bb, &k, e);
Denis Vlasenko75735412007-01-10 20:50:04 +0000549 dd = w - t->v.n - ((unsigned) bb & mask_bits[e]);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000550 bb >>= e;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000551 k -= e;
552
553 /* do the copy */
Denis Vlasenko447b5432007-01-05 19:49:02 +0000554 do_copy:
555 do {
Denis Vlasenko75735412007-01-10 20:50:04 +0000556 /* Was: nn -= (e = (e = GUNZIP_WSIZE - ((dd &= GUNZIP_WSIZE - 1) > w ? dd : w)) > nn ? nn : e); */
Denis Vlasenko447b5432007-01-05 19:49:02 +0000557 /* Who wrote THAT?? rewritten as: */
Denis Vlasenko75735412007-01-10 20:50:04 +0000558 dd &= GUNZIP_WSIZE - 1;
559 e = GUNZIP_WSIZE - (dd > w ? dd : w);
560 if (e > nn) e = nn;
561 nn -= e;
Denis Vlasenko447b5432007-01-05 19:49:02 +0000562
563 /* copy to new buffer to prevent possible overwrite */
Denis Vlasenko75735412007-01-10 20:50:04 +0000564 if (w - dd >= e) { /* (this test assumes unsigned comparison) */
565 memcpy(gunzip_window + w, gunzip_window + dd, e);
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000566 w += e;
Denis Vlasenko75735412007-01-10 20:50:04 +0000567 dd += e;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000568 } else {
Denis Vlasenko447b5432007-01-05 19:49:02 +0000569 /* do it slow to avoid memcpy() overlap */
570 /* !NOMEMCPY */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000571 do {
Denis Vlasenko75735412007-01-10 20:50:04 +0000572 gunzip_window[w++] = gunzip_window[dd++];
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000573 } while (--e);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000574 }
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000575 if (w == GUNZIP_WSIZE) {
576 gunzip_outbuf_count = w;
Denis Vlasenko75735412007-01-10 20:50:04 +0000577 resume_copy = (nn != 0);
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000578 //flush_gunzip_window();
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000579 w = 0;
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000580 return 1;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000581 }
Denis Vlasenko75735412007-01-10 20:50:04 +0000582 } while (nn);
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000583 resume_copy = 0;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000584 }
585 }
586
587 /* restore the globals from the locals */
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000588 gunzip_outbuf_count = w; /* restore global gunzip_window pointer */
589 gunzip_bb = bb; /* restore global bit buffer */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000590 gunzip_bk = k;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000591
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000592 /* normally just after call to inflate_codes, but save code by putting it here */
593 /* free the decoding tables, return */
594 huft_free(tl);
595 huft_free(td);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000596
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000597 /* done */
598 return 0;
599}
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000600#undef ml
601#undef md
602#undef bb
603#undef k
604#undef w
605#undef tl
606#undef td
607#undef bl
608#undef bd
Denis Vlasenko1a8bf7f2007-01-10 20:57:03 +0000609#undef nn
610#undef dd
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000611
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000612
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000613/* called once from inflate_block */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000614static void inflate_stored_setup(STATE_PARAM int my_n, int my_b, int my_k)
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000615{
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000616 inflate_stored_n = my_n;
617 inflate_stored_b = my_b;
618 inflate_stored_k = my_k;
619 /* initialize gunzip_window position */
620 inflate_stored_w = gunzip_outbuf_count;
621}
622/* called once from inflate_get_next_window */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000623static int inflate_stored(STATE_PARAM_ONLY)
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000624{
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000625 /* read and output the compressed data */
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000626 while (inflate_stored_n--) {
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000627 inflate_stored_b = fill_bitbuffer(PASS_STATE inflate_stored_b, &inflate_stored_k, 8);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000628 gunzip_window[inflate_stored_w++] = (unsigned char) inflate_stored_b;
629 if (inflate_stored_w == GUNZIP_WSIZE) {
630 gunzip_outbuf_count = inflate_stored_w;
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000631 //flush_gunzip_window();
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000632 inflate_stored_w = 0;
633 inflate_stored_b >>= 8;
634 inflate_stored_k -= 8;
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000635 return 1; // We have a block
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000636 }
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000637 inflate_stored_b >>= 8;
638 inflate_stored_k -= 8;
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000639 }
640
641 /* restore the globals from the locals */
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000642 gunzip_outbuf_count = inflate_stored_w; /* restore global gunzip_window pointer */
643 gunzip_bb = inflate_stored_b; /* restore global bit buffer */
644 gunzip_bk = inflate_stored_k;
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000645 return 0; // Finished
646}
647
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000648
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000649/*
650 * decompress an inflated block
651 * e: last block flag
652 *
653 * GLOBAL VARIABLES: bb, kk,
654 */
Denis Vlasenko447b5432007-01-05 19:49:02 +0000655/* Return values: -1 = inflate_stored, -2 = inflate_codes */
656/* One callsite in inflate_get_next_window */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000657static int inflate_block(STATE_PARAM smallint *e)
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000658{
659 unsigned t; /* block type */
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000660 unsigned b; /* bit buffer */
661 unsigned k; /* number of bits in bit buffer */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000662
663 /* make local bit buffer */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000664
665 b = gunzip_bb;
666 k = gunzip_bk;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000667
668 /* read in last block bit */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000669 b = fill_bitbuffer(PASS_STATE b, &k, 1);
670 *e = b & 1;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000671 b >>= 1;
672 k -= 1;
673
674 /* read in block type */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000675 b = fill_bitbuffer(PASS_STATE b, &k, 2);
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000676 t = (unsigned) b & 3;
677 b >>= 2;
678 k -= 2;
679
680 /* restore the global bit buffer */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000681 gunzip_bb = b;
682 gunzip_bk = k;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000683
684 /* inflate that block type */
685 switch (t) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000686 case 0: /* Inflate stored */
687 {
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000688 unsigned n; /* number of bytes in block */
689 unsigned b_stored; /* bit buffer */
690 unsigned k_stored; /* number of bits in bit buffer */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000691
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000692 /* make local copies of globals */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000693 b_stored = gunzip_bb; /* initialize bit buffer */
694 k_stored = gunzip_bk;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000695
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000696 /* go to byte boundary */
697 n = k_stored & 7;
698 b_stored >>= n;
699 k_stored -= n;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000700
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000701 /* get the length and its complement */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000702 b_stored = fill_bitbuffer(PASS_STATE b_stored, &k_stored, 16);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000703 n = ((unsigned) b_stored & 0xffff);
704 b_stored >>= 16;
705 k_stored -= 16;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000706
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000707 b_stored = fill_bitbuffer(PASS_STATE b_stored, &k_stored, 16);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000708 if (n != (unsigned) ((~b_stored) & 0xffff)) {
709 return 1; /* error in compressed data */
710 }
711 b_stored >>= 16;
712 k_stored -= 16;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000713
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000714 inflate_stored_setup(PASS_STATE n, b_stored, k_stored); // Setup inflate_stored
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000715
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000716 return -1;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000717 }
Denis Vlasenkocd42cb82007-01-05 23:56:53 +0000718 case 1:
719 /* Inflate fixed
720 * decompress an inflated type 1 (fixed Huffman codes) block. We should
721 * either replace this with a custom decoder, or at least precompute the
722 * Huffman tables. */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000723 {
724 int i; /* temporary variable */
725 huft_t *tl; /* literal/length code table */
726 huft_t *td; /* distance code table */
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000727 unsigned bl; /* lookup bits for tl */
728 unsigned bd; /* lookup bits for td */
729 unsigned l[288]; /* length list for huft_build */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000730
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000731 /* set up literal table */
732 for (i = 0; i < 144; i++) {
733 l[i] = 8;
734 }
735 for (; i < 256; i++) {
736 l[i] = 9;
737 }
738 for (; i < 280; i++) {
739 l[i] = 7;
740 }
741 for (; i < 288; i++) { /* make a complete, but wrong code set */
742 l[i] = 8;
743 }
744 bl = 7;
Denis Vlasenko447b5432007-01-05 19:49:02 +0000745 i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl);
746 if (i != 0) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000747 return i;
748 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000749
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000750 /* set up distance table */
751 for (i = 0; i < 30; i++) { /* make an incomplete code set */
752 l[i] = 5;
753 }
754 bd = 5;
Denis Vlasenko447b5432007-01-05 19:49:02 +0000755 i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd);
756 if (i > 1) {
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000757 huft_free(tl);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000758 return i;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000759 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000760
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000761 /* decompress until an end-of-block code */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000762 inflate_codes_setup(PASS_STATE tl, td, bl, bd); // Setup inflate_codes
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000763
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000764 /* huft_free code moved into inflate_codes */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000765
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000766 return -2;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000767 }
768 case 2: /* Inflate dynamic */
769 {
770 const int dbits = 6; /* bits in base distance lookup table */
771 const int lbits = 9; /* bits in base literal/length lookup table */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000772
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000773 huft_t *tl; /* literal/length code table */
774 huft_t *td; /* distance code table */
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000775 unsigned i; /* temporary variables */
776 unsigned j;
777 unsigned l; /* last length */
778 unsigned m; /* mask for bit lengths table */
779 unsigned n; /* number of lengths to get */
780 unsigned bl; /* lookup bits for tl */
781 unsigned bd; /* lookup bits for td */
782 unsigned nb; /* number of bit length codes */
783 unsigned nl; /* number of literal/length codes */
784 unsigned nd; /* number of distance codes */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000785
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000786 unsigned ll[286 + 30]; /* literal/length and distance code lengths */
787 unsigned b_dynamic; /* bit buffer */
788 unsigned k_dynamic; /* number of bits in bit buffer */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000789
790 /* make local bit buffer */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000791 b_dynamic = gunzip_bb;
792 k_dynamic = gunzip_bk;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000793
794 /* read in table lengths */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000795 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 5);
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000796 nl = 257 + ((unsigned) b_dynamic & 0x1f); /* number of literal/length codes */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000797
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000798 b_dynamic >>= 5;
799 k_dynamic -= 5;
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000800 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 5);
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000801 nd = 1 + ((unsigned) b_dynamic & 0x1f); /* number of distance codes */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000802
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000803 b_dynamic >>= 5;
804 k_dynamic -= 5;
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000805 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 4);
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000806 nb = 4 + ((unsigned) b_dynamic & 0xf); /* number of bit length codes */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000807
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000808 b_dynamic >>= 4;
809 k_dynamic -= 4;
810 if (nl > 286 || nd > 30) {
811 return 1; /* bad lengths */
812 }
813
814 /* read in bit-length-code lengths */
815 for (j = 0; j < nb; j++) {
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000816 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 3);
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000817 ll[border[j]] = (unsigned) b_dynamic & 7;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000818 b_dynamic >>= 3;
819 k_dynamic -= 3;
820 }
821 for (; j < 19; j++) {
822 ll[border[j]] = 0;
823 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000824
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000825 /* build decoding table for trees--single level, 7 bit lookup */
826 bl = 7;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000827 i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl);
828 if (i != 0) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000829 if (i == 1) {
830 huft_free(tl);
831 }
832 return i; /* incomplete code set */
833 }
834
835 /* read in literal and distance code lengths */
836 n = nl + nd;
837 m = mask_bits[bl];
838 i = l = 0;
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000839 while ((unsigned) i < n) {
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000840 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, (unsigned)bl);
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000841 j = (td = tl + ((unsigned) b_dynamic & m))->b;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000842 b_dynamic >>= j;
843 k_dynamic -= j;
844 j = td->v.n;
845 if (j < 16) { /* length of code in bits (0..15) */
846 ll[i++] = l = j; /* save last length in l */
847 } else if (j == 16) { /* repeat last length 3 to 6 times */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000848 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 2);
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000849 j = 3 + ((unsigned) b_dynamic & 3);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000850 b_dynamic >>= 2;
851 k_dynamic -= 2;
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000852 if ((unsigned) i + j > n) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000853 return 1;
854 }
855 while (j--) {
856 ll[i++] = l;
857 }
858 } else if (j == 17) { /* 3 to 10 zero length codes */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000859 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 3);
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000860 j = 3 + ((unsigned) b_dynamic & 7);
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000861 b_dynamic >>= 3;
862 k_dynamic -= 3;
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000863 if ((unsigned) i + j > n) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000864 return 1;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000865 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000866 while (j--) {
867 ll[i++] = 0;
868 }
869 l = 0;
870 } else { /* j == 18: 11 to 138 zero length codes */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000871 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 7);
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000872 j = 11 + ((unsigned) b_dynamic & 0x7f);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000873 b_dynamic >>= 7;
874 k_dynamic -= 7;
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000875 if ((unsigned) i + j > n) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000876 return 1;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000877 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000878 while (j--) {
879 ll[i++] = 0;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000880 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000881 l = 0;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000882 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000883 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000884
885 /* free decoding table for trees */
886 huft_free(tl);
887
888 /* restore the global bit buffer */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000889 gunzip_bb = b_dynamic;
890 gunzip_bk = k_dynamic;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000891
892 /* build the decoding tables for literal/length and distance codes */
893 bl = lbits;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000894
Denis Vlasenko447b5432007-01-05 19:49:02 +0000895 i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl);
896 if (i != 0) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000897 if (i == 1) {
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000898//shouldn't we propagate error?
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000899 bb_error_msg_and_die("incomplete literal tree");
Denis Vlasenko447b5432007-01-05 19:49:02 +0000900 /* huft_free(tl); */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000901 }
902 return i; /* incomplete code set */
903 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000904
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000905 bd = dbits;
Denis Vlasenko447b5432007-01-05 19:49:02 +0000906 i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd);
907 if (i != 0) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000908 if (i == 1) {
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000909//shouldn't we propagate error?
Manuel Novoa III cad53642003-03-19 09:13:01 +0000910 bb_error_msg_and_die("incomplete distance tree");
Denis Vlasenko447b5432007-01-05 19:49:02 +0000911 /* huft_free(td); */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000912 }
913 huft_free(tl);
914 return i; /* incomplete code set */
915 }
916
917 /* decompress until an end-of-block code */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000918 inflate_codes_setup(PASS_STATE tl, td, bl, bd); // Setup inflate_codes
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000919
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000920 /* huft_free code moved into inflate_codes */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000921
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000922 return -2;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000923 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000924 default:
925 /* bad block type */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000926//shouldn't we propagate error?
Denis Vlasenko6d655be2006-09-06 19:02:46 +0000927 bb_error_msg_and_die("bad block type %d", t);
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000928 }
929}
930
Denis Vlasenko447b5432007-01-05 19:49:02 +0000931/* Two callsites, both in inflate_get_next_window */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000932static void calculate_gunzip_crc(STATE_PARAM_ONLY)
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000933{
934 int n;
935 for (n = 0; n < gunzip_outbuf_count; n++) {
936 gunzip_crc = gunzip_crc_table[((int) gunzip_crc ^ (gunzip_window[n])) & 0xff] ^ (gunzip_crc >> 8);
937 }
938 gunzip_bytes_out += gunzip_outbuf_count;
939}
940
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000941/* One callsite in inflate_unzip_internal */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000942static int inflate_get_next_window(STATE_PARAM_ONLY)
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000943{
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000944 gunzip_outbuf_count = 0;
945
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000946 while (1) {
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000947 int ret;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000948
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000949 if (need_another_block) {
950 if (end_reached) {
951 calculate_gunzip_crc(PASS_STATE_ONLY);
952 end_reached = 0;
953 need_another_block = 1;
954 return 0; /* Last block */
955 }
956 method = inflate_block(PASS_STATE &end_reached);
957 need_another_block = 0;
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000958 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000959
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000960 switch (method) {
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000961 case -1:
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000962 ret = inflate_stored(PASS_STATE_ONLY);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000963 break;
964 case -2:
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000965 ret = inflate_codes(PASS_STATE_ONLY);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000966 break;
967 default:
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000968//shouldn't we propagate error?
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000969 bb_error_msg_and_die("inflate error %d", method);
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000970 }
971
972 if (ret == 1) {
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000973 calculate_gunzip_crc(PASS_STATE_ONLY);
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000974 return 1; // More data left
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000975 }
976 need_another_block = 1; // End of that block
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000977 }
Glenn L McGratheda4f532002-11-24 06:01:20 +0000978 /* Doesnt get here */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000979}
980
Paul Fox0840b762005-07-20 20:26:49 +0000981
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000982/* Called from inflate_gunzip() and inflate_unzip() */
983/* NB: bytebuffer is allocated here but freeing it is left to the caller! */
Denis Vlasenkocd42cb82007-01-05 23:56:53 +0000984static USE_DESKTOP(long long) int
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000985inflate_unzip_internal(STATE_PARAM int in, int out)
Glenn L McGrathfd73b8c2002-11-17 21:33:30 +0000986{
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000987 USE_DESKTOP(long long) int n = 0;
Glenn L McGrath5699b852003-11-15 23:19:05 +0000988 ssize_t nwrote;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000989
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000990 /* Allocate all global buffers (for DYN_ALLOC option) */
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000991 gunzip_window = xmalloc(GUNZIP_WSIZE);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000992 gunzip_outbuf_count = 0;
993 gunzip_bytes_out = 0;
Glenn L McGrath5699b852003-11-15 23:19:05 +0000994 gunzip_src_fd = in;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000995
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000996 /* initialize gunzip_window, bit buffer */
997 gunzip_bk = 0;
998 gunzip_bb = 0;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000999
Glenn L McGrath7ca04f32002-09-25 02:47:48 +00001000 /* Create the crc table */
Rob Landleyd921b2e2006-08-03 15:41:12 +00001001 gunzip_crc_table = crc32_filltable(0);
Rob Landleyc57ec372006-04-10 17:07:15 +00001002 gunzip_crc = ~0;
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00001003
Glenn L McGrath5699b852003-11-15 23:19:05 +00001004 /* Allocate space for buffer */
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001005 bytebuffer = xmalloc(bytebuffer_max);
Glenn L McGrath5699b852003-11-15 23:19:05 +00001006
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001007 while (1) {
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +00001008 int r = inflate_get_next_window(PASS_STATE_ONLY);
Rob Landley53437472006-07-16 08:14:35 +00001009 nwrote = full_write(out, gunzip_window, gunzip_outbuf_count);
Denis Vlasenko447b5432007-01-05 19:49:02 +00001010 if (nwrote != gunzip_outbuf_count) {
Glenn L McGrath5699b852003-11-15 23:19:05 +00001011 bb_perror_msg("write");
Denis Vlasenkod1a19af2007-01-05 23:58:45 +00001012 n = -1;
1013 goto ret;
Glenn L McGrath5699b852003-11-15 23:19:05 +00001014 }
Denis Vlasenkod1a19af2007-01-05 23:58:45 +00001015 USE_DESKTOP(n += nwrote;)
1016 if (r == 0) break;
Glenn L McGrath5699b852003-11-15 23:19:05 +00001017 }
1018
Glenn L McGrath7ca04f32002-09-25 02:47:48 +00001019 /* Store unused bytes in a global buffer so calling applets can access it */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +00001020 if (gunzip_bk >= 8) {
1021 /* Undo too much lookahead. The next read will be byte aligned
1022 * so we can discard unused bits in the last meaningful byte. */
Glenn L McGratheda4f532002-11-24 06:01:20 +00001023 bytebuffer_offset--;
1024 bytebuffer[bytebuffer_offset] = gunzip_bb & 0xff;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +00001025 gunzip_bb >>= 8;
1026 gunzip_bk -= 8;
Glenn L McGrath7fd92942001-04-11 03:11:33 +00001027 }
Denis Vlasenkod1a19af2007-01-05 23:58:45 +00001028 ret:
1029 /* Cleanup */
1030 free(gunzip_window);
1031 free(gunzip_crc_table);
1032 return n;
Glenn L McGrath7fd92942001-04-11 03:11:33 +00001033}
Glenn L McGrathcc616922003-02-09 04:46:34 +00001034
Denis Vlasenkocd42cb82007-01-05 23:56:53 +00001035
1036USE_DESKTOP(long long) int
1037inflate_unzip(inflate_unzip_result *res, unsigned bufsize, int in, int out)
1038{
1039 USE_DESKTOP(long long) int n;
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +00001040 DECLARE_STATE;
1041
1042 ALLOC_STATE;
Denis Vlasenkocd42cb82007-01-05 23:56:53 +00001043
1044 bytebuffer_max = bufsize + 8;
1045 bytebuffer_offset = 4;
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +00001046 n = inflate_unzip_internal(PASS_STATE in, out);
Denis Vlasenkocd42cb82007-01-05 23:56:53 +00001047
1048 res->crc = gunzip_crc;
1049 res->bytes_out = gunzip_bytes_out;
1050 free(bytebuffer);
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +00001051 DEALLOC_STATE;
Denis Vlasenkocd42cb82007-01-05 23:56:53 +00001052 return n;
1053}
1054
1055
Denis Vlasenko97a8dd32006-10-01 15:55:11 +00001056USE_DESKTOP(long long) int
1057inflate_gunzip(int in, int out)
Glenn L McGrathcc616922003-02-09 04:46:34 +00001058{
Rob Landleyc57ec372006-04-10 17:07:15 +00001059 uint32_t stored_crc = 0;
Denis Vlasenkocc33ef12007-01-05 19:46:04 +00001060 unsigned count;
Denis Vlasenkocd42cb82007-01-05 23:56:53 +00001061 USE_DESKTOP(long long) int n;
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +00001062 DECLARE_STATE;
Glenn L McGrathcc616922003-02-09 04:46:34 +00001063
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +00001064 ALLOC_STATE;
1065
1066 bytebuffer_max = 0x8000;
1067 n = inflate_unzip_internal(PASS_STATE in, out);
Denis Vlasenkocd42cb82007-01-05 23:56:53 +00001068
1069 if (n < 0) goto ret;
Glenn L McGrath5699b852003-11-15 23:19:05 +00001070
Glenn L McGrathcc616922003-02-09 04:46:34 +00001071 /* top up the input buffer with the rest of the trailer */
1072 count = bytebuffer_size - bytebuffer_offset;
1073 if (count < 8) {
Rob Landley53437472006-07-16 08:14:35 +00001074 xread(in, &bytebuffer[bytebuffer_size], 8 - count);
Denis Vlasenko447b5432007-01-05 19:49:02 +00001075//shouldn't we propagate error?
Glenn L McGrathcc616922003-02-09 04:46:34 +00001076 bytebuffer_size += 8 - count;
1077 }
1078 for (count = 0; count != 4; count++) {
1079 stored_crc |= (bytebuffer[bytebuffer_offset] << (count * 8));
1080 bytebuffer_offset++;
1081 }
1082
1083 /* Validate decompression - crc */
Rob Landleyc57ec372006-04-10 17:07:15 +00001084 if (stored_crc != (~gunzip_crc)) {
Glenn L McGrath5699b852003-11-15 23:19:05 +00001085 bb_error_msg("crc error");
Denis Vlasenkocd42cb82007-01-05 23:56:53 +00001086 n = -1;
1087 goto ret;
Glenn L McGrathcc616922003-02-09 04:46:34 +00001088 }
1089
1090 /* Validate decompression - size */
1091 if (gunzip_bytes_out !=
1092 (bytebuffer[bytebuffer_offset] | (bytebuffer[bytebuffer_offset+1] << 8) |
Denis Vlasenko447b5432007-01-05 19:49:02 +00001093 (bytebuffer[bytebuffer_offset+2] << 16) | (bytebuffer[bytebuffer_offset+3] << 24))
1094 ) {
Denis Vlasenko3038ac92006-09-30 19:37:25 +00001095 bb_error_msg("incorrect length");
Denis Vlasenkocd42cb82007-01-05 23:56:53 +00001096 n = -1;
Glenn L McGrathcc616922003-02-09 04:46:34 +00001097 }
Denis Vlasenkocd42cb82007-01-05 23:56:53 +00001098 ret:
1099 free(bytebuffer);
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +00001100 DEALLOC_STATE;
Denis Vlasenkocd42cb82007-01-05 23:56:53 +00001101 return n;
Glenn L McGrathcc616922003-02-09 04:46:34 +00001102}