blob: 7f9046b82b98d881b66b9537b681dc9a9e5bc691 [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
Denis Vlasenko0beaff82007-09-21 13:16:32 +000015 * busybox functions by Glenn McGrath
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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020033 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Glenn L McGrath7fd92942001-04-11 03:11:33 +000034 */
Rob Landley11c7a7b2006-06-25 22:39:24 +000035#include "libbb.h"
Denys Vlasenkod184a722011-09-22 12:45:14 +020036#include "bb_archive.h"
Glenn L McGrath7fd92942001-04-11 03:11:33 +000037
Denis Vlasenko5dd8a032007-10-05 15:26:08 +000038typedef struct huft_t {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +000039 unsigned char e; /* number of extra bits or operation */
40 unsigned char b; /* number of bits in this code or subcode */
Glenn L McGrath7fd92942001-04-11 03:11:33 +000041 union {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +000042 unsigned short n; /* literal, length base, or distance base */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +000043 struct huft_t *t; /* pointer to next level of table */
Glenn L McGrath7fd92942001-04-11 03:11:33 +000044 } v;
45} huft_t;
46
Denis Vlasenkod1a19af2007-01-05 23:58:45 +000047enum {
48 /* gunzip_window size--must be a power of two, and
Denis Vlasenko5dd8a032007-10-05 15:26:08 +000049 * at least 32K for zip's deflate method */
Denis Vlasenkod1a19af2007-01-05 23:58:45 +000050 GUNZIP_WSIZE = 0x8000,
51 /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
52 BMAX = 16, /* maximum bit length of any code (16 for explode) */
53 N_MAX = 288, /* maximum number of codes in any set */
54};
55
Denis Vlasenkobb3d0fa2007-01-03 01:57:25 +000056
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +000057/* This is somewhat complex-looking arrangement, but it allows
58 * to place decompressor state either in bss or in
59 * malloc'ed space simply by changing #defines below.
60 * Sizes on i386:
61 * text data bss dec hex
62 * 5256 0 108 5364 14f4 - bss
63 * 4915 0 0 4915 1333 - malloc
64 */
65#define STATE_IN_BSS 0
66#define STATE_IN_MALLOC 1
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000067
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000068
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +000069typedef struct state_t {
70 off_t gunzip_bytes_out; /* number of output bytes */
71 uint32_t gunzip_crc;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000072
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +000073 int gunzip_src_fd;
74 unsigned gunzip_outbuf_count; /* bytes in output buffer */
75
76 unsigned char *gunzip_window;
77
78 uint32_t *gunzip_crc_table;
79
80 /* bitbuffer */
81 unsigned gunzip_bb; /* bit buffer */
82 unsigned char gunzip_bk; /* bits in bit buffer */
83
Denis Vlasenko5dd8a032007-10-05 15:26:08 +000084 /* input (compressed) data */
85 unsigned char *bytebuffer; /* buffer itself */
Denis Vlasenkoe8ef7ec2008-02-04 12:12:48 +000086 off_t to_read; /* compressed bytes to read (unzip only, -1 for gunzip) */
87// unsigned bytebuffer_max; /* buffer size */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +000088 unsigned bytebuffer_offset; /* buffer position */
89 unsigned bytebuffer_size; /* how much data is there (size <= max) */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +000090
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 Vlasenko5dd8a032007-10-05 15:26:08 +0000103
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000104 smallint resume_copy;
105
106 /* private data of inflate_get_next_window() */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000107 smallint method; /* method == -1 for stored, -2 for codes */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000108 smallint need_another_block;
109 smallint end_reached;
110
111 /* private data of inflate_stored() */
112 unsigned inflate_stored_n;
113 unsigned inflate_stored_b;
114 unsigned inflate_stored_k;
115 unsigned inflate_stored_w;
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000116
117 const char *error_msg;
118 jmp_buf error_jmp;
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000119} state_t;
120#define gunzip_bytes_out (S()gunzip_bytes_out )
121#define gunzip_crc (S()gunzip_crc )
122#define gunzip_src_fd (S()gunzip_src_fd )
123#define gunzip_outbuf_count (S()gunzip_outbuf_count)
124#define gunzip_window (S()gunzip_window )
125#define gunzip_crc_table (S()gunzip_crc_table )
126#define gunzip_bb (S()gunzip_bb )
127#define gunzip_bk (S()gunzip_bk )
Denis Vlasenkoe8ef7ec2008-02-04 12:12:48 +0000128#define to_read (S()to_read )
129// #define bytebuffer_max (S()bytebuffer_max )
130// Both gunzip and unzip can use constant buffer size now (16k):
131#define bytebuffer_max 0x4000
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000132#define bytebuffer (S()bytebuffer )
133#define bytebuffer_offset (S()bytebuffer_offset )
134#define bytebuffer_size (S()bytebuffer_size )
135#define inflate_codes_ml (S()inflate_codes_ml )
136#define inflate_codes_md (S()inflate_codes_md )
137#define inflate_codes_bb (S()inflate_codes_bb )
138#define inflate_codes_k (S()inflate_codes_k )
139#define inflate_codes_w (S()inflate_codes_w )
140#define inflate_codes_tl (S()inflate_codes_tl )
141#define inflate_codes_td (S()inflate_codes_td )
142#define inflate_codes_bl (S()inflate_codes_bl )
143#define inflate_codes_bd (S()inflate_codes_bd )
Denis Vlasenko75735412007-01-10 20:50:04 +0000144#define inflate_codes_nn (S()inflate_codes_nn )
145#define inflate_codes_dd (S()inflate_codes_dd )
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000146#define resume_copy (S()resume_copy )
147#define method (S()method )
148#define need_another_block (S()need_another_block )
149#define end_reached (S()end_reached )
150#define inflate_stored_n (S()inflate_stored_n )
151#define inflate_stored_b (S()inflate_stored_b )
152#define inflate_stored_k (S()inflate_stored_k )
153#define inflate_stored_w (S()inflate_stored_w )
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000154#define error_msg (S()error_msg )
155#define error_jmp (S()error_jmp )
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000156
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000157/* This is a generic part */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000158#if STATE_IN_BSS /* Use global data segment */
159#define DECLARE_STATE /*nothing*/
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000160#define ALLOC_STATE /*nothing*/
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000161#define DEALLOC_STATE ((void)0)
162#define S() state.
163#define PASS_STATE /*nothing*/
164#define PASS_STATE_ONLY /*nothing*/
165#define STATE_PARAM /*nothing*/
166#define STATE_PARAM_ONLY void
167static state_t state;
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000168#endif
169
170#if STATE_IN_MALLOC /* Use malloc space */
171#define DECLARE_STATE state_t *state
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000172#define ALLOC_STATE (state = xzalloc(sizeof(*state)))
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000173#define DEALLOC_STATE free(state)
174#define S() state->
175#define PASS_STATE state,
176#define PASS_STATE_ONLY state
177#define STATE_PARAM state_t *state,
178#define STATE_PARAM_ONLY state_t *state
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000179#endif
180
Glenn L McGrathcc616922003-02-09 04:46:34 +0000181
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000182static const uint16_t mask_bits[] ALIGN2 = {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000183 0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000184 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
185};
186
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000187/* Copy lengths for literal codes 257..285 */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000188static const uint16_t cplens[] ALIGN2 = {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000189 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 +0000190 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000191};
192
193/* note: see note #13 above about the 258 in this list. */
194/* Extra bits for literal codes 257..285 */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000195static const uint8_t cplext[] ALIGN1 = {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000196 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 +0000197 5, 5, 5, 0, 99, 99
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000198}; /* 99 == invalid */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000199
200/* Copy offsets for distance codes 0..29 */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000201static const uint16_t cpdist[] ALIGN2 = {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000202 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 +0000203 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000204};
205
206/* Extra bits for distance codes */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000207static const uint8_t cpdext[] ALIGN1 = {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000208 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 +0000209 11, 11, 12, 12, 13, 13
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000210};
211
212/* Tables for deflate from PKZIP's appnote.txt. */
213/* Order of the bit length code lengths */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000214static const uint8_t border[] ALIGN1 = {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000215 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
216};
217
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000218
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000219/*
220 * Free the malloc'ed tables built by huft_build(), which makes a linked
221 * list of the tables it made, with the links in a dummy first entry of
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000222 * each table.
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000223 * t: table to free
224 */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000225static void huft_free(huft_t *p)
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000226{
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000227 huft_t *q;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000228
229 /* Go through linked list, freeing from the malloced (t[-1]) address. */
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000230 while (p) {
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000231 q = (--p)->v.t;
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000232 free(p);
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000233 p = q;
234 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000235}
236
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000237static void huft_free_all(STATE_PARAM_ONLY)
238{
239 huft_free(inflate_codes_tl);
240 huft_free(inflate_codes_td);
241 inflate_codes_tl = NULL;
242 inflate_codes_td = NULL;
243}
244
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000245static void abort_unzip(STATE_PARAM_ONLY) NORETURN;
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000246static void abort_unzip(STATE_PARAM_ONLY)
247{
248 huft_free_all(PASS_STATE_ONLY);
249 longjmp(error_jmp, 1);
250}
251
252static unsigned fill_bitbuffer(STATE_PARAM unsigned bitbuffer, unsigned *current, const unsigned required)
253{
254 while (*current < required) {
255 if (bytebuffer_offset >= bytebuffer_size) {
Denis Vlasenkoe8ef7ec2008-02-04 12:12:48 +0000256 unsigned sz = bytebuffer_max - 4;
257 if (to_read >= 0 && to_read < sz) /* unzip only */
258 sz = to_read;
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000259 /* Leave the first 4 bytes empty so we can always unwind the bitbuffer
260 * to the front of the bytebuffer */
Denis Vlasenkoe8ef7ec2008-02-04 12:12:48 +0000261 bytebuffer_size = safe_read(gunzip_src_fd, &bytebuffer[4], sz);
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000262 if ((int)bytebuffer_size < 1) {
263 error_msg = "unexpected end of file";
264 abort_unzip(PASS_STATE_ONLY);
265 }
Denis Vlasenkoe8ef7ec2008-02-04 12:12:48 +0000266 if (to_read >= 0) /* unzip only */
267 to_read -= bytebuffer_size;
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000268 bytebuffer_size += 4;
269 bytebuffer_offset = 4;
270 }
271 bitbuffer |= ((unsigned) bytebuffer[bytebuffer_offset]) << *current;
272 bytebuffer_offset++;
273 *current += 8;
274 }
275 return bitbuffer;
276}
277
278
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000279/* Given a list of code lengths and a maximum table size, make a set of
280 * tables to decode that set of codes. Return zero on success, one if
281 * the given code set is incomplete (the tables are still built in this
Denys Vlasenko25989152018-02-04 00:15:29 +0100282 * case), two if the input is invalid (an oversubscribed set of lengths)
283 * - in this case stores NULL in *t.
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000284 *
285 * b: code lengths in bits (all assumed <= BMAX)
286 * n: number of codes (assumed <= N_MAX)
287 * s: number of simple-valued codes (0..s-1)
288 * d: list of base values for non-simple codes
289 * e: list of extra bits for non-simple codes
290 * t: result: starting table
291 * m: maximum lookup bits, returns actual
292 */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000293static int huft_build(const unsigned *b, const unsigned n,
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100294 const unsigned s, const unsigned short *d,
295 const unsigned char *e, huft_t **t, unsigned *m)
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000296{
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000297 unsigned a; /* counter for codes of length k */
298 unsigned c[BMAX + 1]; /* bit length count table */
299 unsigned eob_len; /* length of end-of-block code (value 256) */
300 unsigned f; /* i repeats in table every f entries */
301 int g; /* maximum code length */
302 int htl; /* table level */
303 unsigned i; /* counter, current code */
304 unsigned j; /* counter */
305 int k; /* number of bits in current code */
Denys Vlasenko1de25a62015-10-26 19:33:05 +0100306 const unsigned *p; /* pointer into c[], b[], or v[] */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000307 huft_t *q; /* points to current table */
308 huft_t r; /* table entry for structure assignment */
309 huft_t *u[BMAX]; /* table stack */
Denys Vlasenko6bd3fff2015-10-30 23:41:53 +0100310 unsigned v[N_MAX + 1]; /* values in order of bit length. last v[] is never used */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000311 int ws[BMAX + 1]; /* bits decoded stack */
312 int w; /* bits decoded */
313 unsigned x[BMAX + 1]; /* bit offsets, then code stack */
314 unsigned *xp; /* pointer into x */
315 int y; /* number of dummy codes added */
316 unsigned z; /* number of entries in current table */
Paul Fox0840b762005-07-20 20:26:49 +0000317
318 /* Length of EOB code, if any */
319 eob_len = n > 256 ? b[256] : BMAX;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000320
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000321 *t = NULL;
322
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000323 /* Generate counts for each bit length */
Denis Vlasenko447b5432007-01-05 19:49:02 +0000324 memset(c, 0, sizeof(c));
Denys Vlasenko1de25a62015-10-26 19:33:05 +0100325 p = b;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000326 i = n;
327 do {
Paul Fox0840b762005-07-20 20:26:49 +0000328 c[*p]++; /* assume all entries <= BMAX */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000329 p++; /* can't combine with above line (Solaris bug) */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000330 } while (--i);
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000331 if (c[0] == n) { /* null input - all zero length codes */
Denys Vlasenko25989152018-02-04 00:15:29 +0100332 q = xzalloc(3 * sizeof(*q));
333 //q[0].v.t = NULL;
334 q[1].e = 99; /* invalid code marker */
335 q[1].b = 1;
336 q[2].e = 99; /* invalid code marker */
337 q[2].b = 1;
338 *t = q + 1;
339 *m = 1;
340 return 0;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000341 }
342
343 /* Find minimum and maximum length, bound *m by those */
Denys Vlasenko9b2a9f02013-11-29 16:43:33 +0100344 for (j = 1; (j <= BMAX) && (c[j] == 0); j++)
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000345 continue;
Paul Fox0840b762005-07-20 20:26:49 +0000346 k = j; /* minimum code length */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000347 for (i = BMAX; (c[i] == 0) && i; i--)
348 continue;
Paul Fox0840b762005-07-20 20:26:49 +0000349 g = i; /* maximum code length */
350 *m = (*m < j) ? j : ((*m > i) ? i : *m);
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000351
352 /* Adjust last length count to fill out codes, if needed */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000353 for (y = 1 << j; j < i; j++, y <<= 1) {
Denis Vlasenko447b5432007-01-05 19:49:02 +0000354 y -= c[j];
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000355 if (y < 0)
Paul Fox0840b762005-07-20 20:26:49 +0000356 return 2; /* bad input: more codes than bits */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000357 }
Denis Vlasenko447b5432007-01-05 19:49:02 +0000358 y -= c[i];
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000359 if (y < 0)
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000360 return 2;
361 c[i] += y;
362
363 /* Generate starting offsets into the value table for each length */
364 x[1] = j = 0;
365 p = c + 1;
366 xp = x + 2;
Paul Fox0840b762005-07-20 20:26:49 +0000367 while (--i) { /* note that i == g from above */
Denis Vlasenko447b5432007-01-05 19:49:02 +0000368 j += *p++;
369 *xp++ = j;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000370 }
371
Denys Vlasenko6bd3fff2015-10-30 23:41:53 +0100372 /* Make a table of values in order of bit lengths.
373 * To detect bad input, unused v[i]'s are set to invalid value UINT_MAX.
374 * In particular, last v[i] is never filled and must not be accessed.
375 */
376 memset(v, 0xff, sizeof(v));
Denys Vlasenko1de25a62015-10-26 19:33:05 +0100377 p = b;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000378 i = 0;
379 do {
Denis Vlasenko447b5432007-01-05 19:49:02 +0000380 j = *p++;
381 if (j != 0) {
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000382 v[x[j]++] = i;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000383 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000384 } while (++i < n);
385
386 /* Generate the Huffman codes and for each, make the table entries */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000387 x[0] = i = 0; /* first Huffman code is zero */
388 p = v; /* grab values in bit order */
389 htl = -1; /* no tables yet--level -1 */
390 w = ws[0] = 0; /* bits decoded */
391 u[0] = NULL; /* just to keep compilers happy */
392 q = NULL; /* ditto */
393 z = 0; /* ditto */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000394
395 /* go through the bit lengths (k already is bits in shortest code) */
396 for (; k <= g; k++) {
397 a = c[k];
398 while (a--) {
399 /* here i is the Huffman code of length k bits for value *p */
400 /* make tables up to required level */
Rob Landley11c7a7b2006-06-25 22:39:24 +0000401 while (k > ws[htl + 1]) {
402 w = ws[++htl];
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000403
Paul Fox0840b762005-07-20 20:26:49 +0000404 /* compute minimum size table less than or equal to *m bits */
Denis Vlasenko447b5432007-01-05 19:49:02 +0000405 z = g - w;
406 z = z > *m ? *m : z; /* upper limit on table size */
407 j = k - w;
408 f = 1 << j;
409 if (f > a + 1) { /* try a k-w bit table */
Paul Fox0840b762005-07-20 20:26:49 +0000410 /* too few codes for k-w bit table */
411 f -= a + 1; /* deduct codes from patterns left */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000412 xp = c + k;
Paul Fox0840b762005-07-20 20:26:49 +0000413 while (++j < z) { /* try smaller tables up to z bits */
Denis Vlasenko447b5432007-01-05 19:49:02 +0000414 f <<= 1;
415 if (f <= *++xp) {
Paul Fox0840b762005-07-20 20:26:49 +0000416 break; /* enough codes to use up j bits */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000417 }
Paul Fox0840b762005-07-20 20:26:49 +0000418 f -= *xp; /* else deduct codes from patterns */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000419 }
420 }
Paul Fox0840b762005-07-20 20:26:49 +0000421 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 +0000422 z = 1 << j; /* table entries for j-bit table */
Rob Landley11c7a7b2006-06-25 22:39:24 +0000423 ws[htl+1] = w + j; /* set bits decoded in stack */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000424
425 /* allocate and link in new table */
Denis Vlasenko4cccc032006-12-22 18:37:07 +0000426 q = xzalloc((z + 1) * sizeof(huft_t));
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000427 *t = q + 1; /* link to list for huft_free() */
Rob Landley11c7a7b2006-06-25 22:39:24 +0000428 t = &(q->v.t);
429 u[htl] = ++q; /* table starts after link */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000430
431 /* connect to last table, if there is one */
Rob Landley11c7a7b2006-06-25 22:39:24 +0000432 if (htl) {
433 x[htl] = i; /* save pattern for backing up */
434 r.b = (unsigned char) (w - ws[htl - 1]); /* bits to dump before this table */
Paul Fox0840b762005-07-20 20:26:49 +0000435 r.e = (unsigned char) (16 + j); /* bits in this table */
436 r.v.t = q; /* pointer to this table */
Rob Landley11c7a7b2006-06-25 22:39:24 +0000437 j = (i & ((1 << w) - 1)) >> ws[htl - 1];
438 u[htl - 1][j] = r; /* connect to last table */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000439 }
440 }
441
442 /* set up table entry in r */
443 r.b = (unsigned char) (k - w);
Denys Vlasenko6bd3fff2015-10-30 23:41:53 +0100444 if (/*p >= v + n || -- redundant, caught by the second check: */
445 *p == UINT_MAX /* do we access uninited v[i]? (see memset(v))*/
446 ) {
Paul Fox0840b762005-07-20 20:26:49 +0000447 r.e = 99; /* out of values--invalid code */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000448 } else if (*p < s) {
Paul Fox0840b762005-07-20 20:26:49 +0000449 r.e = (unsigned char) (*p < 256 ? 16 : 15); /* 256 is EOB code */
450 r.v.n = (unsigned short) (*p++); /* simple code is just the value */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000451 } else {
Paul Fox0840b762005-07-20 20:26:49 +0000452 r.e = (unsigned char) e[*p - s]; /* non-simple--look up in lists */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000453 r.v.n = d[*p++ - s];
454 }
455
456 /* fill code-like entries with r */
457 f = 1 << (k - w);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000458 for (j = i >> w; j < z; j += f) {
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000459 q[j] = r;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000460 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000461
462 /* backwards increment the k-bit code i */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000463 for (j = 1 << (k - 1); i & j; j >>= 1) {
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000464 i ^= j;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000465 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000466 i ^= j;
467
468 /* backup over finished tables */
Rob Landley11c7a7b2006-06-25 22:39:24 +0000469 while ((i & ((1 << w) - 1)) != x[htl]) {
470 w = ws[--htl];
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000471 }
472 }
473 }
Paul Fox0840b762005-07-20 20:26:49 +0000474
475 /* return actual size of base table */
476 *m = ws[1];
477
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000478 /* Return 1 if we were given an incomplete table */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000479 return y != 0 && g != 1;
480}
481
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000482
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000483/*
484 * inflate (decompress) the codes in a deflated (compressed) block.
485 * Return an error code or zero if it all goes ok.
486 *
487 * tl, td: literal/length and distance decoder tables
488 * bl, bd: number of bits decoded by tl[] and td[]
489 */
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000490/* called once from inflate_block */
Denis Vlasenko07766bb2007-03-14 00:06:51 +0000491
492/* map formerly local static variables to globals */
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000493#define ml inflate_codes_ml
494#define md inflate_codes_md
495#define bb inflate_codes_bb
496#define k inflate_codes_k
497#define w inflate_codes_w
498#define tl inflate_codes_tl
499#define td inflate_codes_td
500#define bl inflate_codes_bl
501#define bd inflate_codes_bd
Denis Vlasenko75735412007-01-10 20:50:04 +0000502#define nn inflate_codes_nn
503#define dd inflate_codes_dd
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000504static void inflate_codes_setup(STATE_PARAM unsigned my_bl, unsigned my_bd)
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000505{
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000506 bl = my_bl;
507 bd = my_bd;
508 /* make local copies of globals */
509 bb = gunzip_bb; /* initialize bit buffer */
510 k = gunzip_bk;
511 w = gunzip_outbuf_count; /* initialize gunzip_window position */
512 /* inflate the coded data */
513 ml = mask_bits[bl]; /* precompute masks for speed */
514 md = mask_bits[bd];
515}
516/* called once from inflate_get_next_window */
Denys Vlasenkoadf922e2009-10-08 14:35:37 +0200517static NOINLINE int inflate_codes(STATE_PARAM_ONLY)
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000518{
519 unsigned e; /* table entry flag/number of extra bits */
520 huft_t *t; /* pointer to table entry */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000521
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000522 if (resume_copy)
523 goto do_copy;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000524
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000525 while (1) { /* do until end of block */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000526 bb = fill_bitbuffer(PASS_STATE bb, &k, bl);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000527 t = tl + ((unsigned) bb & ml);
Denis Vlasenko447b5432007-01-05 19:49:02 +0000528 e = t->e;
529 if (e > 16)
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000530 do {
Denys Vlasenko6bd3fff2015-10-30 23:41:53 +0100531 if (e == 99) {
532 abort_unzip(PASS_STATE_ONLY);
533 }
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000534 bb >>= t->b;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000535 k -= t->b;
536 e -= 16;
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000537 bb = fill_bitbuffer(PASS_STATE bb, &k, e);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000538 t = t->v.t + ((unsigned) bb & mask_bits[e]);
Denis Vlasenko447b5432007-01-05 19:49:02 +0000539 e = t->e;
540 } while (e > 16);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000541 bb >>= t->b;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000542 k -= t->b;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000543 if (e == 16) { /* then it's a literal */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000544 gunzip_window[w++] = (unsigned char) t->v.n;
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000545 if (w == GUNZIP_WSIZE) {
546 gunzip_outbuf_count = w;
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000547 //flush_gunzip_window();
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000548 w = 0;
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000549 return 1; // We have a block to read
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000550 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000551 } else { /* it's an EOB or a length */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000552 /* exit if end of block */
553 if (e == 15) {
554 break;
555 }
556
557 /* get length of block to copy */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000558 bb = fill_bitbuffer(PASS_STATE bb, &k, e);
Denis Vlasenko75735412007-01-10 20:50:04 +0000559 nn = t->v.n + ((unsigned) bb & mask_bits[e]);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000560 bb >>= e;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000561 k -= e;
562
563 /* decode distance of block to copy */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000564 bb = fill_bitbuffer(PASS_STATE bb, &k, bd);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000565 t = td + ((unsigned) bb & md);
Denis Vlasenko447b5432007-01-05 19:49:02 +0000566 e = t->e;
567 if (e > 16)
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000568 do {
Denys Vlasenko6bd3fff2015-10-30 23:41:53 +0100569 if (e == 99) {
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000570 abort_unzip(PASS_STATE_ONLY);
Denys Vlasenko6bd3fff2015-10-30 23:41:53 +0100571 }
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000572 bb >>= t->b;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000573 k -= t->b;
574 e -= 16;
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000575 bb = fill_bitbuffer(PASS_STATE bb, &k, e);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000576 t = t->v.t + ((unsigned) bb & mask_bits[e]);
Denis Vlasenko447b5432007-01-05 19:49:02 +0000577 e = t->e;
578 } while (e > 16);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000579 bb >>= t->b;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000580 k -= t->b;
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000581 bb = fill_bitbuffer(PASS_STATE bb, &k, e);
Denis Vlasenko75735412007-01-10 20:50:04 +0000582 dd = w - t->v.n - ((unsigned) bb & mask_bits[e]);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000583 bb >>= e;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000584 k -= e;
585
586 /* do the copy */
Denis Vlasenko447b5432007-01-05 19:49:02 +0000587 do_copy:
588 do {
Denis Vlasenko75735412007-01-10 20:50:04 +0000589 /* Was: nn -= (e = (e = GUNZIP_WSIZE - ((dd &= GUNZIP_WSIZE - 1) > w ? dd : w)) > nn ? nn : e); */
Denis Vlasenko447b5432007-01-05 19:49:02 +0000590 /* Who wrote THAT?? rewritten as: */
Joakim Tjernlund0866b362010-02-08 18:55:15 +0100591 unsigned delta;
592
Denis Vlasenko75735412007-01-10 20:50:04 +0000593 dd &= GUNZIP_WSIZE - 1;
594 e = GUNZIP_WSIZE - (dd > w ? dd : w);
Joakim Tjernlund0866b362010-02-08 18:55:15 +0100595 delta = w > dd ? w - dd : dd - w;
Denis Vlasenko75735412007-01-10 20:50:04 +0000596 if (e > nn) e = nn;
597 nn -= e;
Denis Vlasenko447b5432007-01-05 19:49:02 +0000598
599 /* copy to new buffer to prevent possible overwrite */
Joakim Tjernlund0866b362010-02-08 18:55:15 +0100600 if (delta >= e) {
Denis Vlasenko75735412007-01-10 20:50:04 +0000601 memcpy(gunzip_window + w, gunzip_window + dd, e);
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000602 w += e;
Denis Vlasenko75735412007-01-10 20:50:04 +0000603 dd += e;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000604 } else {
Denis Vlasenko447b5432007-01-05 19:49:02 +0000605 /* do it slow to avoid memcpy() overlap */
606 /* !NOMEMCPY */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000607 do {
Denis Vlasenko75735412007-01-10 20:50:04 +0000608 gunzip_window[w++] = gunzip_window[dd++];
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000609 } while (--e);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000610 }
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000611 if (w == GUNZIP_WSIZE) {
612 gunzip_outbuf_count = w;
Denis Vlasenko75735412007-01-10 20:50:04 +0000613 resume_copy = (nn != 0);
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000614 //flush_gunzip_window();
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000615 w = 0;
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000616 return 1;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000617 }
Denis Vlasenko75735412007-01-10 20:50:04 +0000618 } while (nn);
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000619 resume_copy = 0;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000620 }
621 }
622
623 /* restore the globals from the locals */
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000624 gunzip_outbuf_count = w; /* restore global gunzip_window pointer */
625 gunzip_bb = bb; /* restore global bit buffer */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000626 gunzip_bk = k;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000627
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000628 /* normally just after call to inflate_codes, but save code by putting it here */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000629 /* free the decoding tables (tl and td), return */
630 huft_free_all(PASS_STATE_ONLY);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000631
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000632 /* done */
633 return 0;
634}
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000635#undef ml
636#undef md
637#undef bb
638#undef k
639#undef w
640#undef tl
641#undef td
642#undef bl
643#undef bd
Denis Vlasenko1a8bf7f2007-01-10 20:57:03 +0000644#undef nn
645#undef dd
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000646
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000647
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000648/* called once from inflate_block */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000649static void inflate_stored_setup(STATE_PARAM int my_n, int my_b, int my_k)
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000650{
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000651 inflate_stored_n = my_n;
652 inflate_stored_b = my_b;
653 inflate_stored_k = my_k;
654 /* initialize gunzip_window position */
655 inflate_stored_w = gunzip_outbuf_count;
656}
657/* called once from inflate_get_next_window */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000658static int inflate_stored(STATE_PARAM_ONLY)
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000659{
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000660 /* read and output the compressed data */
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000661 while (inflate_stored_n--) {
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000662 inflate_stored_b = fill_bitbuffer(PASS_STATE inflate_stored_b, &inflate_stored_k, 8);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000663 gunzip_window[inflate_stored_w++] = (unsigned char) inflate_stored_b;
664 if (inflate_stored_w == GUNZIP_WSIZE) {
665 gunzip_outbuf_count = inflate_stored_w;
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000666 //flush_gunzip_window();
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000667 inflate_stored_w = 0;
668 inflate_stored_b >>= 8;
669 inflate_stored_k -= 8;
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000670 return 1; /* We have a block */
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000671 }
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000672 inflate_stored_b >>= 8;
673 inflate_stored_k -= 8;
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000674 }
675
676 /* restore the globals from the locals */
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000677 gunzip_outbuf_count = inflate_stored_w; /* restore global gunzip_window pointer */
678 gunzip_bb = inflate_stored_b; /* restore global bit buffer */
679 gunzip_bk = inflate_stored_k;
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000680 return 0; /* Finished */
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000681}
682
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000683
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000684/*
685 * decompress an inflated block
686 * e: last block flag
687 *
688 * GLOBAL VARIABLES: bb, kk,
689 */
Denis Vlasenko447b5432007-01-05 19:49:02 +0000690/* Return values: -1 = inflate_stored, -2 = inflate_codes */
691/* One callsite in inflate_get_next_window */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000692static int inflate_block(STATE_PARAM smallint *e)
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000693{
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000694 unsigned ll[286 + 30]; /* literal/length and distance code lengths */
695 unsigned t; /* block type */
696 unsigned b; /* bit buffer */
697 unsigned k; /* number of bits in bit buffer */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000698
699 /* make local bit buffer */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000700
701 b = gunzip_bb;
702 k = gunzip_bk;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000703
704 /* read in last block bit */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000705 b = fill_bitbuffer(PASS_STATE b, &k, 1);
706 *e = b & 1;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000707 b >>= 1;
708 k -= 1;
709
710 /* read in block type */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000711 b = fill_bitbuffer(PASS_STATE b, &k, 2);
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000712 t = (unsigned) b & 3;
713 b >>= 2;
714 k -= 2;
715
716 /* restore the global bit buffer */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000717 gunzip_bb = b;
718 gunzip_bk = k;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000719
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000720 /* Do we see block type 1 often? Yes!
721 * TODO: fix performance problem (see below) */
722 //bb_error_msg("blktype %d", t);
723
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000724 /* inflate that block type */
725 switch (t) {
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000726 case 0: /* Inflate stored */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000727 {
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000728 unsigned n; /* number of bytes in block */
729 unsigned b_stored; /* bit buffer */
730 unsigned k_stored; /* number of bits in bit buffer */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000731
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000732 /* make local copies of globals */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000733 b_stored = gunzip_bb; /* initialize bit buffer */
734 k_stored = gunzip_bk;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000735
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000736 /* go to byte boundary */
737 n = k_stored & 7;
738 b_stored >>= n;
739 k_stored -= n;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000740
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000741 /* get the length and its complement */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000742 b_stored = fill_bitbuffer(PASS_STATE b_stored, &k_stored, 16);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000743 n = ((unsigned) b_stored & 0xffff);
744 b_stored >>= 16;
745 k_stored -= 16;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000746
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000747 b_stored = fill_bitbuffer(PASS_STATE b_stored, &k_stored, 16);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000748 if (n != (unsigned) ((~b_stored) & 0xffff)) {
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000749 abort_unzip(PASS_STATE_ONLY); /* error in compressed data */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000750 }
751 b_stored >>= 16;
752 k_stored -= 16;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000753
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000754 inflate_stored_setup(PASS_STATE n, b_stored, k_stored);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000755
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000756 return -1;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000757 }
Denis Vlasenkocd42cb82007-01-05 23:56:53 +0000758 case 1:
759 /* Inflate fixed
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000760 * decompress an inflated type 1 (fixed Huffman codes) block. We should
Denis Vlasenkocd42cb82007-01-05 23:56:53 +0000761 * either replace this with a custom decoder, or at least precompute the
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000762 * Huffman tables. TODO */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000763 {
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000764 int i; /* temporary variable */
765 unsigned bl; /* lookup bits for tl */
766 unsigned bd; /* lookup bits for td */
767 /* gcc 4.2.1 is too dumb to reuse stackspace. Moved up... */
768 //unsigned ll[288]; /* length list for huft_build */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000769
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000770 /* set up literal table */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000771 for (i = 0; i < 144; i++)
772 ll[i] = 8;
773 for (; i < 256; i++)
774 ll[i] = 9;
775 for (; i < 280; i++)
776 ll[i] = 7;
777 for (; i < 288; i++) /* make a complete, but wrong code set */
778 ll[i] = 8;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000779 bl = 7;
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000780 huft_build(ll, 288, 257, cplens, cplext, &inflate_codes_tl, &bl);
781 /* huft_build() never return nonzero - we use known data */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000782
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000783 /* set up distance table */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000784 for (i = 0; i < 30; i++) /* make an incomplete code set */
785 ll[i] = 5;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000786 bd = 5;
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000787 huft_build(ll, 30, 0, cpdist, cpdext, &inflate_codes_td, &bd);
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000788
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000789 /* set up data for inflate_codes() */
790 inflate_codes_setup(PASS_STATE bl, bd);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000791
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000792 /* huft_free code moved into inflate_codes */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000793
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000794 return -2;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000795 }
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000796 case 2: /* Inflate dynamic */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000797 {
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000798 enum { dbits = 6 }; /* bits in base distance lookup table */
799 enum { lbits = 9 }; /* bits in base literal/length lookup table */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000800
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000801 huft_t *td; /* distance code table */
802 unsigned i; /* temporary variables */
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000803 unsigned j;
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000804 unsigned l; /* last length */
805 unsigned m; /* mask for bit lengths table */
806 unsigned n; /* number of lengths to get */
807 unsigned bl; /* lookup bits for tl */
808 unsigned bd; /* lookup bits for td */
809 unsigned nb; /* number of bit length codes */
810 unsigned nl; /* number of literal/length codes */
811 unsigned nd; /* number of distance codes */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000812
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000813 //unsigned ll[286 + 30];/* literal/length and distance code lengths */
814 unsigned b_dynamic; /* bit buffer */
815 unsigned k_dynamic; /* number of bits in bit buffer */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000816
817 /* make local bit buffer */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000818 b_dynamic = gunzip_bb;
819 k_dynamic = gunzip_bk;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000820
821 /* read in table lengths */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000822 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 5);
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000823 nl = 257 + ((unsigned) b_dynamic & 0x1f); /* number of literal/length codes */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000824
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000825 b_dynamic >>= 5;
826 k_dynamic -= 5;
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000827 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 5);
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000828 nd = 1 + ((unsigned) b_dynamic & 0x1f); /* number of distance codes */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000829
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000830 b_dynamic >>= 5;
831 k_dynamic -= 5;
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000832 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 4);
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000833 nb = 4 + ((unsigned) b_dynamic & 0xf); /* number of bit length codes */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000834
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000835 b_dynamic >>= 4;
836 k_dynamic -= 4;
Denys Vlasenko6bd3fff2015-10-30 23:41:53 +0100837 if (nl > 286 || nd > 30) {
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000838 abort_unzip(PASS_STATE_ONLY); /* bad lengths */
Denys Vlasenko6bd3fff2015-10-30 23:41:53 +0100839 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000840
841 /* read in bit-length-code lengths */
842 for (j = 0; j < nb; j++) {
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000843 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 3);
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000844 ll[border[j]] = (unsigned) b_dynamic & 7;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000845 b_dynamic >>= 3;
846 k_dynamic -= 3;
847 }
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000848 for (; j < 19; j++)
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000849 ll[border[j]] = 0;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000850
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000851 /* build decoding table for trees - single level, 7 bit lookup */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000852 bl = 7;
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000853 i = huft_build(ll, 19, 19, NULL, NULL, &inflate_codes_tl, &bl);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000854 if (i != 0) {
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000855 abort_unzip(PASS_STATE_ONLY); //return i; /* incomplete code set */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000856 }
857
858 /* read in literal and distance code lengths */
859 n = nl + nd;
860 m = mask_bits[bl];
861 i = l = 0;
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000862 while ((unsigned) i < n) {
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000863 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, (unsigned)bl);
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000864 td = inflate_codes_tl + ((unsigned) b_dynamic & m);
865 j = td->b;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000866 b_dynamic >>= j;
867 k_dynamic -= j;
868 j = td->v.n;
869 if (j < 16) { /* length of code in bits (0..15) */
870 ll[i++] = l = j; /* save last length in l */
871 } else if (j == 16) { /* repeat last length 3 to 6 times */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000872 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 2);
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000873 j = 3 + ((unsigned) b_dynamic & 3);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000874 b_dynamic >>= 2;
875 k_dynamic -= 2;
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000876 if ((unsigned) i + j > n) {
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000877 abort_unzip(PASS_STATE_ONLY); //return 1;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000878 }
879 while (j--) {
880 ll[i++] = l;
881 }
882 } else if (j == 17) { /* 3 to 10 zero length codes */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000883 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 3);
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000884 j = 3 + ((unsigned) b_dynamic & 7);
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000885 b_dynamic >>= 3;
886 k_dynamic -= 3;
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000887 if ((unsigned) i + j > n) {
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000888 abort_unzip(PASS_STATE_ONLY); //return 1;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000889 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000890 while (j--) {
891 ll[i++] = 0;
892 }
893 l = 0;
894 } else { /* j == 18: 11 to 138 zero length codes */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000895 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 7);
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000896 j = 11 + ((unsigned) b_dynamic & 0x7f);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000897 b_dynamic >>= 7;
898 k_dynamic -= 7;
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000899 if ((unsigned) i + j > n) {
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000900 abort_unzip(PASS_STATE_ONLY); //return 1;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000901 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000902 while (j--) {
903 ll[i++] = 0;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000904 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000905 l = 0;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000906 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000907 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000908
909 /* free decoding table for trees */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000910 huft_free(inflate_codes_tl);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000911
912 /* restore the global bit buffer */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000913 gunzip_bb = b_dynamic;
914 gunzip_bk = k_dynamic;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000915
916 /* build the decoding tables for literal/length and distance codes */
917 bl = lbits;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000918
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000919 i = huft_build(ll, nl, 257, cplens, cplext, &inflate_codes_tl, &bl);
Denys Vlasenko6bd3fff2015-10-30 23:41:53 +0100920 if (i != 0) {
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000921 abort_unzip(PASS_STATE_ONLY);
Denys Vlasenko6bd3fff2015-10-30 23:41:53 +0100922 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000923 bd = dbits;
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000924 i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &inflate_codes_td, &bd);
Denys Vlasenko6bd3fff2015-10-30 23:41:53 +0100925 if (i != 0) {
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000926 abort_unzip(PASS_STATE_ONLY);
Denys Vlasenko6bd3fff2015-10-30 23:41:53 +0100927 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000928
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000929 /* set up data for inflate_codes() */
930 inflate_codes_setup(PASS_STATE bl, bd);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000931
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000932 /* huft_free code moved into inflate_codes */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000933
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000934 return -2;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000935 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000936 default:
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000937 abort_unzip(PASS_STATE_ONLY);
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000938 }
939}
940
Denis Vlasenko447b5432007-01-05 19:49:02 +0000941/* Two callsites, both in inflate_get_next_window */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000942static void calculate_gunzip_crc(STATE_PARAM_ONLY)
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000943{
Denys Vlasenko9ce642f2010-10-27 15:26:45 +0200944 gunzip_crc = crc32_block_endian0(gunzip_crc, gunzip_window, gunzip_outbuf_count, gunzip_crc_table);
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000945 gunzip_bytes_out += gunzip_outbuf_count;
946}
947
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000948/* One callsite in inflate_unzip_internal */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000949static int inflate_get_next_window(STATE_PARAM_ONLY)
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000950{
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000951 gunzip_outbuf_count = 0;
952
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000953 while (1) {
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000954 int ret;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000955
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000956 if (need_another_block) {
957 if (end_reached) {
958 calculate_gunzip_crc(PASS_STATE_ONLY);
959 end_reached = 0;
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000960 /* NB: need_another_block is still set */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000961 return 0; /* Last block */
962 }
963 method = inflate_block(PASS_STATE &end_reached);
964 need_another_block = 0;
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000965 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000966
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000967 switch (method) {
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000968 case -1:
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000969 ret = inflate_stored(PASS_STATE_ONLY);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000970 break;
971 case -2:
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000972 ret = inflate_codes(PASS_STATE_ONLY);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000973 break;
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000974 default: /* cannot happen */
975 abort_unzip(PASS_STATE_ONLY);
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000976 }
977
978 if (ret == 1) {
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000979 calculate_gunzip_crc(PASS_STATE_ONLY);
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000980 return 1; /* more data left */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000981 }
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000982 need_another_block = 1; /* end of that block */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000983 }
Glenn L McGratheda4f532002-11-24 06:01:20 +0000984 /* Doesnt get here */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000985}
986
Paul Fox0840b762005-07-20 20:26:49 +0000987
Denis Vlasenkoc14d39e2007-06-08 13:05:39 +0000988/* Called from unpack_gz_stream() and inflate_unzip() */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000989static IF_DESKTOP(long long) int
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100990inflate_unzip_internal(STATE_PARAM transformer_state_t *xstate)
Glenn L McGrathfd73b8c2002-11-17 21:33:30 +0000991{
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000992 IF_DESKTOP(long long) int n = 0;
Glenn L McGrath5699b852003-11-15 23:19:05 +0000993 ssize_t nwrote;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000994
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000995 /* Allocate all global buffers (for DYN_ALLOC option) */
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000996 gunzip_window = xmalloc(GUNZIP_WSIZE);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000997 gunzip_outbuf_count = 0;
998 gunzip_bytes_out = 0;
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100999 gunzip_src_fd = xstate->src_fd;
Glenn L McGrath7fd92942001-04-11 03:11:33 +00001000
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001001 /* (re) initialize state */
1002 method = -1;
1003 need_another_block = 1;
1004 resume_copy = 0;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +00001005 gunzip_bk = 0;
1006 gunzip_bb = 0;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +00001007
Glenn L McGrath7ca04f32002-09-25 02:47:48 +00001008 /* Create the crc table */
Denys Vlasenkoddacb032018-02-01 10:56:19 +01001009 gunzip_crc_table = crc32_new_table_le();
Rob Landleyc57ec372006-04-10 17:07:15 +00001010 gunzip_crc = ~0;
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00001011
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001012 error_msg = "corrupted data";
1013 if (setjmp(error_jmp)) {
1014 /* Error from deep inside zip machinery */
Denys Vlasenko6bd3fff2015-10-30 23:41:53 +01001015 bb_error_msg(error_msg);
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001016 n = -1;
1017 goto ret;
1018 }
Glenn L McGrath5699b852003-11-15 23:19:05 +00001019
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001020 while (1) {
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +00001021 int r = inflate_get_next_window(PASS_STATE_ONLY);
Denys Vlasenkob4c11c12014-12-07 00:44:00 +01001022 nwrote = transformer_write(xstate, gunzip_window, gunzip_outbuf_count);
1023 if (nwrote == (ssize_t)-1) {
Denis Vlasenkod1a19af2007-01-05 23:58:45 +00001024 n = -1;
1025 goto ret;
Glenn L McGrath5699b852003-11-15 23:19:05 +00001026 }
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001027 IF_DESKTOP(n += nwrote;)
Denis Vlasenkod1a19af2007-01-05 23:58:45 +00001028 if (r == 0) break;
Glenn L McGrath5699b852003-11-15 23:19:05 +00001029 }
1030
Glenn L McGrath7ca04f32002-09-25 02:47:48 +00001031 /* Store unused bytes in a global buffer so calling applets can access it */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +00001032 if (gunzip_bk >= 8) {
1033 /* Undo too much lookahead. The next read will be byte aligned
1034 * so we can discard unused bits in the last meaningful byte. */
Glenn L McGratheda4f532002-11-24 06:01:20 +00001035 bytebuffer_offset--;
1036 bytebuffer[bytebuffer_offset] = gunzip_bb & 0xff;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +00001037 gunzip_bb >>= 8;
1038 gunzip_bk -= 8;
Glenn L McGrath7fd92942001-04-11 03:11:33 +00001039 }
Denis Vlasenkod1a19af2007-01-05 23:58:45 +00001040 ret:
1041 /* Cleanup */
1042 free(gunzip_window);
1043 free(gunzip_crc_table);
1044 return n;
Glenn L McGrath7fd92942001-04-11 03:11:33 +00001045}
Glenn L McGrathcc616922003-02-09 04:46:34 +00001046
Denis Vlasenkocd42cb82007-01-05 23:56:53 +00001047
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001048/* External entry points */
1049
1050/* For unzip */
1051
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001052IF_DESKTOP(long long) int FAST_FUNC
Denys Vlasenkob4c11c12014-12-07 00:44:00 +01001053inflate_unzip(transformer_state_t *xstate)
Denis Vlasenkocd42cb82007-01-05 23:56:53 +00001054{
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001055 IF_DESKTOP(long long) int n;
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +00001056 DECLARE_STATE;
1057
1058 ALLOC_STATE;
Denis Vlasenkocd42cb82007-01-05 23:56:53 +00001059
Denys Vlasenkoe7800f32014-12-07 00:42:49 +01001060 to_read = xstate->bytes_in;
Denis Vlasenkoe8ef7ec2008-02-04 12:12:48 +00001061// bytebuffer_max = 0x8000;
Denis Vlasenkocd42cb82007-01-05 23:56:53 +00001062 bytebuffer_offset = 4;
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001063 bytebuffer = xmalloc(bytebuffer_max);
Denys Vlasenkob4c11c12014-12-07 00:44:00 +01001064 n = inflate_unzip_internal(PASS_STATE xstate);
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001065 free(bytebuffer);
Denis Vlasenkocd42cb82007-01-05 23:56:53 +00001066
Denys Vlasenkoe7800f32014-12-07 00:42:49 +01001067 xstate->crc32 = gunzip_crc;
1068 xstate->bytes_out = gunzip_bytes_out;
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +00001069 DEALLOC_STATE;
Denis Vlasenkocd42cb82007-01-05 23:56:53 +00001070 return n;
1071}
1072
1073
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001074/* For gunzip */
1075
1076/* helpers first */
1077
1078/* Top up the input buffer with at least n bytes. */
1079static int top_up(STATE_PARAM unsigned n)
1080{
1081 int count = bytebuffer_size - bytebuffer_offset;
1082
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001083 if (count < (int)n) {
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001084 memmove(bytebuffer, &bytebuffer[bytebuffer_offset], count);
1085 bytebuffer_offset = 0;
1086 bytebuffer_size = full_read(gunzip_src_fd, &bytebuffer[count], bytebuffer_max - count);
1087 if ((int)bytebuffer_size < 0) {
Denys Vlasenkoea8b2522010-06-02 12:57:26 +02001088 bb_error_msg(bb_msg_read_error);
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001089 return 0;
1090 }
1091 bytebuffer_size += count;
1092 if (bytebuffer_size < n)
1093 return 0;
1094 }
1095 return 1;
1096}
1097
1098static uint16_t buffer_read_le_u16(STATE_PARAM_ONLY)
1099{
1100 uint16_t res;
1101#if BB_LITTLE_ENDIAN
Denis Vlasenkoefb545b2008-12-08 22:56:18 +00001102 move_from_unaligned16(res, &bytebuffer[bytebuffer_offset]);
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001103#else
1104 res = bytebuffer[bytebuffer_offset];
1105 res |= bytebuffer[bytebuffer_offset + 1] << 8;
1106#endif
1107 bytebuffer_offset += 2;
1108 return res;
1109}
1110
1111static uint32_t buffer_read_le_u32(STATE_PARAM_ONLY)
1112{
1113 uint32_t res;
1114#if BB_LITTLE_ENDIAN
Denis Vlasenkoefb545b2008-12-08 22:56:18 +00001115 move_from_unaligned32(res, &bytebuffer[bytebuffer_offset]);
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001116#else
1117 res = bytebuffer[bytebuffer_offset];
1118 res |= bytebuffer[bytebuffer_offset + 1] << 8;
1119 res |= bytebuffer[bytebuffer_offset + 2] << 16;
1120 res |= bytebuffer[bytebuffer_offset + 3] << 24;
1121#endif
1122 bytebuffer_offset += 4;
1123 return res;
1124}
1125
Denys Vlasenkoe7800f32014-12-07 00:42:49 +01001126static int check_header_gzip(STATE_PARAM transformer_state_t *xstate)
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001127{
1128 union {
1129 unsigned char raw[8];
1130 struct {
1131 uint8_t gz_method;
1132 uint8_t flags;
Denis Vlasenko1a9e9bd2008-11-01 12:54:56 +00001133 uint32_t mtime;
1134 uint8_t xtra_flags_UNUSED;
1135 uint8_t os_flags_UNUSED;
Denys Vlasenko8dc0e192009-09-16 00:58:11 +02001136 } PACKED formatted;
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001137 } header;
Denys Vlasenko7b85ec32015-10-13 17:17:34 +02001138
1139 BUILD_BUG_ON(sizeof(header) != 8);
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001140
1141 /*
1142 * Rewind bytebuffer. We use the beginning because the header has 8
1143 * bytes, leaving enough for unwinding afterwards.
1144 */
1145 bytebuffer_size -= bytebuffer_offset;
1146 memmove(bytebuffer, &bytebuffer[bytebuffer_offset], bytebuffer_size);
1147 bytebuffer_offset = 0;
1148
1149 if (!top_up(PASS_STATE 8))
1150 return 0;
1151 memcpy(header.raw, &bytebuffer[bytebuffer_offset], 8);
1152 bytebuffer_offset += 8;
1153
1154 /* Check the compression method */
1155 if (header.formatted.gz_method != 8) {
1156 return 0;
1157 }
1158
1159 if (header.formatted.flags & 0x04) {
1160 /* bit 2 set: extra field present */
1161 unsigned extra_short;
1162
1163 if (!top_up(PASS_STATE 2))
1164 return 0;
1165 extra_short = buffer_read_le_u16(PASS_STATE_ONLY);
1166 if (!top_up(PASS_STATE extra_short))
1167 return 0;
1168 /* Ignore extra field */
1169 bytebuffer_offset += extra_short;
1170 }
1171
1172 /* Discard original name and file comment if any */
1173 /* bit 3 set: original file name present */
1174 /* bit 4 set: file comment present */
1175 if (header.formatted.flags & 0x18) {
1176 while (1) {
1177 do {
1178 if (!top_up(PASS_STATE 1))
1179 return 0;
1180 } while (bytebuffer[bytebuffer_offset++] != 0);
1181 if ((header.formatted.flags & 0x18) != 0x18)
1182 break;
1183 header.formatted.flags &= ~0x18;
1184 }
1185 }
1186
Denys Vlasenkob4c11c12014-12-07 00:44:00 +01001187 xstate->mtime = SWAP_LE32(header.formatted.mtime);
Denis Vlasenko1a9e9bd2008-11-01 12:54:56 +00001188
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001189 /* Read the header checksum */
1190 if (header.formatted.flags & 0x02) {
1191 if (!top_up(PASS_STATE 2))
1192 return 0;
1193 bytebuffer_offset += 2;
1194 }
1195 return 1;
1196}
1197
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001198IF_DESKTOP(long long) int FAST_FUNC
Denys Vlasenkob4c11c12014-12-07 00:44:00 +01001199unpack_gz_stream(transformer_state_t *xstate)
Glenn L McGrathcc616922003-02-09 04:46:34 +00001200{
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001201 uint32_t v32;
Denys Vlasenko59655072012-03-06 16:23:50 +01001202 IF_DESKTOP(long long) int total, n;
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +00001203 DECLARE_STATE;
Glenn L McGrathcc616922003-02-09 04:46:34 +00001204
Denys Vlasenko02c3c382012-03-06 16:32:06 +01001205#if !ENABLE_FEATURE_SEAMLESS_Z
Denys Vlasenkob4c11c12014-12-07 00:44:00 +01001206 if (check_signature16(xstate, GZIP_MAGIC))
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +01001207 return -1;
Denys Vlasenko02c3c382012-03-06 16:32:06 +01001208#else
Denys Vlasenko984b0a62016-06-20 11:06:42 +02001209 if (!xstate->signature_skipped) {
Denys Vlasenko02c3c382012-03-06 16:32:06 +01001210 uint16_t magic2;
1211
Denys Vlasenkob4c11c12014-12-07 00:44:00 +01001212 if (full_read(xstate->src_fd, &magic2, 2) != 2) {
Denys Vlasenko02c3c382012-03-06 16:32:06 +01001213 bad_magic:
1214 bb_error_msg("invalid magic");
1215 return -1;
1216 }
1217 if (magic2 == COMPRESS_MAGIC) {
Denys Vlasenko984b0a62016-06-20 11:06:42 +02001218 xstate->signature_skipped = 2;
Denys Vlasenkob4c11c12014-12-07 00:44:00 +01001219 return unpack_Z_stream(xstate);
Denys Vlasenko02c3c382012-03-06 16:32:06 +01001220 }
1221 if (magic2 != GZIP_MAGIC)
1222 goto bad_magic;
1223 }
1224#endif
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +01001225
Denys Vlasenko59655072012-03-06 16:23:50 +01001226 total = 0;
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001227
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +00001228 ALLOC_STATE;
Denis Vlasenkoe8ef7ec2008-02-04 12:12:48 +00001229 to_read = -1;
1230// bytebuffer_max = 0x8000;
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001231 bytebuffer = xmalloc(bytebuffer_max);
Denys Vlasenkob4c11c12014-12-07 00:44:00 +01001232 gunzip_src_fd = xstate->src_fd;
Denis Vlasenkocd42cb82007-01-05 23:56:53 +00001233
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001234 again:
Denys Vlasenkoe7800f32014-12-07 00:42:49 +01001235 if (!check_header_gzip(PASS_STATE xstate)) {
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001236 bb_error_msg("corrupted data");
Denys Vlasenko59655072012-03-06 16:23:50 +01001237 total = -1;
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001238 goto ret;
Glenn L McGrathcc616922003-02-09 04:46:34 +00001239 }
Denys Vlasenko59655072012-03-06 16:23:50 +01001240
Denys Vlasenkob4c11c12014-12-07 00:44:00 +01001241 n = inflate_unzip_internal(PASS_STATE xstate);
Denys Vlasenko59655072012-03-06 16:23:50 +01001242 if (n < 0) {
1243 total = -1;
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001244 goto ret;
Denys Vlasenko59655072012-03-06 16:23:50 +01001245 }
1246 total += n;
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001247
1248 if (!top_up(PASS_STATE 8)) {
1249 bb_error_msg("corrupted data");
Denys Vlasenko59655072012-03-06 16:23:50 +01001250 total = -1;
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001251 goto ret;
Glenn L McGrathcc616922003-02-09 04:46:34 +00001252 }
1253
1254 /* Validate decompression - crc */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001255 v32 = buffer_read_le_u32(PASS_STATE_ONLY);
1256 if ((~gunzip_crc) != v32) {
Glenn L McGrath5699b852003-11-15 23:19:05 +00001257 bb_error_msg("crc error");
Denys Vlasenko59655072012-03-06 16:23:50 +01001258 total = -1;
Denis Vlasenkocd42cb82007-01-05 23:56:53 +00001259 goto ret;
Glenn L McGrathcc616922003-02-09 04:46:34 +00001260 }
1261
1262 /* Validate decompression - size */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001263 v32 = buffer_read_le_u32(PASS_STATE_ONLY);
1264 if ((uint32_t)gunzip_bytes_out != v32) {
Denis Vlasenko3038ac92006-09-30 19:37:25 +00001265 bb_error_msg("incorrect length");
Denys Vlasenko59655072012-03-06 16:23:50 +01001266 total = -1;
Glenn L McGrathcc616922003-02-09 04:46:34 +00001267 }
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001268
1269 if (!top_up(PASS_STATE 2))
1270 goto ret; /* EOF */
1271
1272 if (bytebuffer[bytebuffer_offset] == 0x1f
1273 && bytebuffer[bytebuffer_offset + 1] == 0x8b
1274 ) {
1275 bytebuffer_offset += 2;
1276 goto again;
1277 }
1278 /* GNU gzip says: */
1279 /*bb_error_msg("decompression OK, trailing garbage ignored");*/
1280
Denis Vlasenkocd42cb82007-01-05 23:56:53 +00001281 ret:
1282 free(bytebuffer);
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +00001283 DEALLOC_STATE;
Denys Vlasenko59655072012-03-06 16:23:50 +01001284 return total;
Glenn L McGrathcc616922003-02-09 04:46:34 +00001285}