blob: 0f8173d0ad982277217526998f6f812e36bc4bc9 [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 {
Denys Vlasenkofb110352019-10-20 19:07:06 +020042 unsigned n; /* literal, length base, or distance base */
43 /* ^^^^^ was "unsigned short", but that results in larger code */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +000044 struct huft_t *t; /* pointer to next level of table */
Glenn L McGrath7fd92942001-04-11 03:11:33 +000045 } v;
46} huft_t;
47
Denis Vlasenkod1a19af2007-01-05 23:58:45 +000048enum {
49 /* gunzip_window size--must be a power of two, and
Denis Vlasenko5dd8a032007-10-05 15:26:08 +000050 * at least 32K for zip's deflate method */
Denis Vlasenkod1a19af2007-01-05 23:58:45 +000051 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
Denis Vlasenko5dd8a032007-10-05 15:26:08 +000085 /* input (compressed) data */
86 unsigned char *bytebuffer; /* buffer itself */
Denis Vlasenkoe8ef7ec2008-02-04 12:12:48 +000087 off_t to_read; /* compressed bytes to read (unzip only, -1 for gunzip) */
88// unsigned bytebuffer_max; /* buffer size */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +000089 unsigned bytebuffer_offset; /* buffer position */
90 unsigned bytebuffer_size; /* how much data is there (size <= max) */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +000091
92 /* private data of inflate_codes() */
93 unsigned inflate_codes_ml; /* masks for bl and bd bits */
94 unsigned inflate_codes_md; /* masks for bl and bd bits */
95 unsigned inflate_codes_bb; /* bit buffer */
96 unsigned inflate_codes_k; /* number of bits in bit buffer */
97 unsigned inflate_codes_w; /* current gunzip_window position */
98 huft_t *inflate_codes_tl;
99 huft_t *inflate_codes_td;
100 unsigned inflate_codes_bl;
101 unsigned inflate_codes_bd;
Denis Vlasenko75735412007-01-10 20:50:04 +0000102 unsigned inflate_codes_nn; /* length and index for copy */
103 unsigned inflate_codes_dd;
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000104
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000105 smallint resume_copy;
106
107 /* private data of inflate_get_next_window() */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000108 smallint method; /* method == -1 for stored, -2 for codes */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000109 smallint need_another_block;
110 smallint end_reached;
111
112 /* private data of inflate_stored() */
113 unsigned inflate_stored_n;
114 unsigned inflate_stored_b;
115 unsigned inflate_stored_k;
116 unsigned inflate_stored_w;
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000117
118 const char *error_msg;
119 jmp_buf error_jmp;
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000120} state_t;
121#define gunzip_bytes_out (S()gunzip_bytes_out )
122#define gunzip_crc (S()gunzip_crc )
123#define gunzip_src_fd (S()gunzip_src_fd )
124#define gunzip_outbuf_count (S()gunzip_outbuf_count)
125#define gunzip_window (S()gunzip_window )
126#define gunzip_crc_table (S()gunzip_crc_table )
127#define gunzip_bb (S()gunzip_bb )
128#define gunzip_bk (S()gunzip_bk )
Denis Vlasenkoe8ef7ec2008-02-04 12:12:48 +0000129#define to_read (S()to_read )
130// #define bytebuffer_max (S()bytebuffer_max )
131// Both gunzip and unzip can use constant buffer size now (16k):
132#define bytebuffer_max 0x4000
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000133#define bytebuffer (S()bytebuffer )
134#define bytebuffer_offset (S()bytebuffer_offset )
135#define bytebuffer_size (S()bytebuffer_size )
136#define inflate_codes_ml (S()inflate_codes_ml )
137#define inflate_codes_md (S()inflate_codes_md )
138#define inflate_codes_bb (S()inflate_codes_bb )
139#define inflate_codes_k (S()inflate_codes_k )
140#define inflate_codes_w (S()inflate_codes_w )
141#define inflate_codes_tl (S()inflate_codes_tl )
142#define inflate_codes_td (S()inflate_codes_td )
143#define inflate_codes_bl (S()inflate_codes_bl )
144#define inflate_codes_bd (S()inflate_codes_bd )
Denis Vlasenko75735412007-01-10 20:50:04 +0000145#define inflate_codes_nn (S()inflate_codes_nn )
146#define inflate_codes_dd (S()inflate_codes_dd )
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000147#define resume_copy (S()resume_copy )
148#define method (S()method )
149#define need_another_block (S()need_another_block )
150#define end_reached (S()end_reached )
151#define inflate_stored_n (S()inflate_stored_n )
152#define inflate_stored_b (S()inflate_stored_b )
153#define inflate_stored_k (S()inflate_stored_k )
154#define inflate_stored_w (S()inflate_stored_w )
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000155#define error_msg (S()error_msg )
156#define error_jmp (S()error_jmp )
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000157
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000158/* This is a generic part */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000159#if STATE_IN_BSS /* Use global data segment */
160#define DECLARE_STATE /*nothing*/
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000161#define ALLOC_STATE /*nothing*/
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000162#define DEALLOC_STATE ((void)0)
163#define S() state.
164#define PASS_STATE /*nothing*/
165#define PASS_STATE_ONLY /*nothing*/
166#define STATE_PARAM /*nothing*/
167#define STATE_PARAM_ONLY void
168static state_t state;
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000169#endif
170
171#if STATE_IN_MALLOC /* Use malloc space */
172#define DECLARE_STATE state_t *state
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000173#define ALLOC_STATE (state = xzalloc(sizeof(*state)))
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000174#define DEALLOC_STATE free(state)
175#define S() state->
176#define PASS_STATE state,
177#define PASS_STATE_ONLY state
178#define STATE_PARAM state_t *state,
179#define STATE_PARAM_ONLY state_t *state
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000180#endif
181
Glenn L McGrathcc616922003-02-09 04:46:34 +0000182
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000183static const uint16_t mask_bits[] ALIGN2 = {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000184 0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000185 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
186};
187
Denys Vlasenko6572ef62019-10-19 18:52:41 +0200188/* Put lengths/offsets and extra bits in a struct of arrays
189 * to make calls to huft_build() have one fewer parameter.
190 */
191struct cp_ext {
192 uint16_t cp[31];
193 uint8_t ext[31];
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000194};
Denys Vlasenko6572ef62019-10-19 18:52:41 +0200195/* Copy lengths and extra bits for literal codes 257..285 */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000196/* note: see note #13 above about the 258 in this list. */
Denys Vlasenko6572ef62019-10-19 18:52:41 +0200197static const struct cp_ext lit = {
198 /*257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 */
199 /*0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 */
200 { 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 },
201 { 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, 5, 5, 5, 0, 99, 99 } /* 99 == invalid */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000202};
Denys Vlasenko6572ef62019-10-19 18:52:41 +0200203/* Copy offsets and extra bits for distance codes 0..29 */
204static const struct cp_ext dist = {
205 /*0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 */
206 { 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577 },
207 { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000208};
209
210/* Tables for deflate from PKZIP's appnote.txt. */
211/* Order of the bit length code lengths */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000212static const uint8_t border[] ALIGN1 = {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000213 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
214};
215
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000216
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000217/*
218 * Free the malloc'ed tables built by huft_build(), which makes a linked
219 * list of the tables it made, with the links in a dummy first entry of
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000220 * each table.
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000221 * t: table to free
222 */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000223static void huft_free(huft_t *p)
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000224{
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000225 huft_t *q;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000226
227 /* Go through linked list, freeing from the malloced (t[-1]) address. */
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000228 while (p) {
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000229 q = (--p)->v.t;
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000230 free(p);
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000231 p = q;
232 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000233}
234
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000235static void huft_free_all(STATE_PARAM_ONLY)
236{
237 huft_free(inflate_codes_tl);
238 huft_free(inflate_codes_td);
239 inflate_codes_tl = NULL;
240 inflate_codes_td = NULL;
241}
242
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000243static void abort_unzip(STATE_PARAM_ONLY) NORETURN;
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000244static void abort_unzip(STATE_PARAM_ONLY)
245{
246 huft_free_all(PASS_STATE_ONLY);
247 longjmp(error_jmp, 1);
248}
249
250static unsigned fill_bitbuffer(STATE_PARAM unsigned bitbuffer, unsigned *current, const unsigned required)
251{
252 while (*current < required) {
253 if (bytebuffer_offset >= bytebuffer_size) {
Denis Vlasenkoe8ef7ec2008-02-04 12:12:48 +0000254 unsigned sz = bytebuffer_max - 4;
255 if (to_read >= 0 && to_read < sz) /* unzip only */
256 sz = to_read;
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000257 /* Leave the first 4 bytes empty so we can always unwind the bitbuffer
258 * to the front of the bytebuffer */
Denis Vlasenkoe8ef7ec2008-02-04 12:12:48 +0000259 bytebuffer_size = safe_read(gunzip_src_fd, &bytebuffer[4], sz);
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000260 if ((int)bytebuffer_size < 1) {
261 error_msg = "unexpected end of file";
262 abort_unzip(PASS_STATE_ONLY);
263 }
Denis Vlasenkoe8ef7ec2008-02-04 12:12:48 +0000264 if (to_read >= 0) /* unzip only */
265 to_read -= bytebuffer_size;
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000266 bytebuffer_size += 4;
267 bytebuffer_offset = 4;
268 }
269 bitbuffer |= ((unsigned) bytebuffer[bytebuffer_offset]) << *current;
270 bytebuffer_offset++;
271 *current += 8;
272 }
273 return bitbuffer;
274}
275
276
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000277/* Given a list of code lengths and a maximum table size, make a set of
Denys Vlasenkoc7633922019-10-19 18:33:49 +0200278 * tables to decode that set of codes.
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000279 *
280 * b: code lengths in bits (all assumed <= BMAX)
281 * n: number of codes (assumed <= N_MAX)
282 * s: number of simple-valued codes (0..s-1)
283 * d: list of base values for non-simple codes
284 * e: list of extra bits for non-simple codes
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000285 * m: maximum lookup bits, returns actual
Denys Vlasenkoc7633922019-10-19 18:33:49 +0200286 * result: starting table
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000287 */
Denys Vlasenkoc7633922019-10-19 18:33:49 +0200288static huft_t* huft_build(const unsigned *b, const unsigned n,
Denys Vlasenko6572ef62019-10-19 18:52:41 +0200289 const unsigned s, const struct cp_ext *cp_ext,
290 unsigned *m)
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000291{
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000292 unsigned a; /* counter for codes of length k */
293 unsigned c[BMAX + 1]; /* bit length count table */
294 unsigned eob_len; /* length of end-of-block code (value 256) */
295 unsigned f; /* i repeats in table every f entries */
296 int g; /* maximum code length */
297 int htl; /* table level */
298 unsigned i; /* counter, current code */
299 unsigned j; /* counter */
300 int k; /* number of bits in current code */
Denys Vlasenko1de25a62015-10-26 19:33:05 +0100301 const unsigned *p; /* pointer into c[], b[], or v[] */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000302 huft_t *q; /* points to current table */
303 huft_t r; /* table entry for structure assignment */
304 huft_t *u[BMAX]; /* table stack */
Denys Vlasenko6bd3fff2015-10-30 23:41:53 +0100305 unsigned v[N_MAX + 1]; /* values in order of bit length. last v[] is never used */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000306 int ws[BMAX + 1]; /* bits decoded stack */
307 int w; /* bits decoded */
308 unsigned x[BMAX + 1]; /* bit offsets, then code stack */
309 unsigned *xp; /* pointer into x */
310 int y; /* number of dummy codes added */
311 unsigned z; /* number of entries in current table */
Denys Vlasenkoc7633922019-10-19 18:33:49 +0200312 huft_t *result;
313 huft_t **t;
Paul Fox0840b762005-07-20 20:26:49 +0000314
315 /* Length of EOB code, if any */
316 eob_len = n > 256 ? b[256] : BMAX;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000317
318 /* Generate counts for each bit length */
Denis Vlasenko447b5432007-01-05 19:49:02 +0000319 memset(c, 0, sizeof(c));
Denys Vlasenko1de25a62015-10-26 19:33:05 +0100320 p = b;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000321 i = n;
322 do {
Paul Fox0840b762005-07-20 20:26:49 +0000323 c[*p]++; /* assume all entries <= BMAX */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000324 p++; /* can't combine with above line (Solaris bug) */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000325 } while (--i);
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000326 if (c[0] == n) { /* null input - all zero length codes */
Denys Vlasenko25989152018-02-04 00:15:29 +0100327 q = xzalloc(3 * sizeof(*q));
328 //q[0].v.t = NULL;
329 q[1].e = 99; /* invalid code marker */
330 q[1].b = 1;
331 q[2].e = 99; /* invalid code marker */
332 q[2].b = 1;
Denys Vlasenko25989152018-02-04 00:15:29 +0100333 *m = 1;
Denys Vlasenkoc7633922019-10-19 18:33:49 +0200334 return q + 1;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000335 }
336
337 /* Find minimum and maximum length, bound *m by those */
Denys Vlasenko9b2a9f02013-11-29 16:43:33 +0100338 for (j = 1; (j <= BMAX) && (c[j] == 0); j++)
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000339 continue;
Paul Fox0840b762005-07-20 20:26:49 +0000340 k = j; /* minimum code length */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000341 for (i = BMAX; (c[i] == 0) && i; i--)
342 continue;
Paul Fox0840b762005-07-20 20:26:49 +0000343 g = i; /* maximum code length */
344 *m = (*m < j) ? j : ((*m > i) ? i : *m);
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000345
346 /* Adjust last length count to fill out codes, if needed */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000347 for (y = 1 << j; j < i; j++, y <<= 1) {
Denis Vlasenko447b5432007-01-05 19:49:02 +0000348 y -= c[j];
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000349 if (y < 0)
Denys Vlasenkoc7633922019-10-19 18:33:49 +0200350 return NULL; /* bad input: more codes than bits */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000351 }
Denis Vlasenko447b5432007-01-05 19:49:02 +0000352 y -= c[i];
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000353 if (y < 0)
Denys Vlasenkoc7633922019-10-19 18:33:49 +0200354 return NULL;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000355 c[i] += y;
356
357 /* Generate starting offsets into the value table for each length */
358 x[1] = j = 0;
359 p = c + 1;
360 xp = x + 2;
Paul Fox0840b762005-07-20 20:26:49 +0000361 while (--i) { /* note that i == g from above */
Denis Vlasenko447b5432007-01-05 19:49:02 +0000362 j += *p++;
363 *xp++ = j;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000364 }
365
Denys Vlasenko6bd3fff2015-10-30 23:41:53 +0100366 /* Make a table of values in order of bit lengths.
367 * To detect bad input, unused v[i]'s are set to invalid value UINT_MAX.
368 * In particular, last v[i] is never filled and must not be accessed.
369 */
370 memset(v, 0xff, sizeof(v));
Denys Vlasenko1de25a62015-10-26 19:33:05 +0100371 p = b;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000372 i = 0;
373 do {
Denis Vlasenko447b5432007-01-05 19:49:02 +0000374 j = *p++;
375 if (j != 0) {
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000376 v[x[j]++] = i;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000377 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000378 } while (++i < n);
379
380 /* Generate the Huffman codes and for each, make the table entries */
Denys Vlasenkoc7633922019-10-19 18:33:49 +0200381 result = NULL;
382 t = &result;
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000383 x[0] = i = 0; /* first Huffman code is zero */
384 p = v; /* grab values in bit order */
385 htl = -1; /* no tables yet--level -1 */
386 w = ws[0] = 0; /* bits decoded */
387 u[0] = NULL; /* just to keep compilers happy */
388 q = NULL; /* ditto */
389 z = 0; /* ditto */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000390
391 /* go through the bit lengths (k already is bits in shortest code) */
392 for (; k <= g; k++) {
393 a = c[k];
394 while (a--) {
395 /* here i is the Huffman code of length k bits for value *p */
396 /* make tables up to required level */
Rob Landley11c7a7b2006-06-25 22:39:24 +0000397 while (k > ws[htl + 1]) {
398 w = ws[++htl];
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000399
Paul Fox0840b762005-07-20 20:26:49 +0000400 /* compute minimum size table less than or equal to *m bits */
Denis Vlasenko447b5432007-01-05 19:49:02 +0000401 z = g - w;
402 z = z > *m ? *m : z; /* upper limit on table size */
403 j = k - w;
404 f = 1 << j;
405 if (f > a + 1) { /* try a k-w bit table */
Paul Fox0840b762005-07-20 20:26:49 +0000406 /* too few codes for k-w bit table */
407 f -= a + 1; /* deduct codes from patterns left */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000408 xp = c + k;
Paul Fox0840b762005-07-20 20:26:49 +0000409 while (++j < z) { /* try smaller tables up to z bits */
Denis Vlasenko447b5432007-01-05 19:49:02 +0000410 f <<= 1;
411 if (f <= *++xp) {
Paul Fox0840b762005-07-20 20:26:49 +0000412 break; /* enough codes to use up j bits */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000413 }
Paul Fox0840b762005-07-20 20:26:49 +0000414 f -= *xp; /* else deduct codes from patterns */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000415 }
416 }
Paul Fox0840b762005-07-20 20:26:49 +0000417 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 +0000418 z = 1 << j; /* table entries for j-bit table */
Rob Landley11c7a7b2006-06-25 22:39:24 +0000419 ws[htl+1] = w + j; /* set bits decoded in stack */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000420
421 /* allocate and link in new table */
Denis Vlasenko4cccc032006-12-22 18:37:07 +0000422 q = xzalloc((z + 1) * sizeof(huft_t));
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000423 *t = q + 1; /* link to list for huft_free() */
Rob Landley11c7a7b2006-06-25 22:39:24 +0000424 t = &(q->v.t);
425 u[htl] = ++q; /* table starts after link */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000426
427 /* connect to last table, if there is one */
Rob Landley11c7a7b2006-06-25 22:39:24 +0000428 if (htl) {
429 x[htl] = i; /* save pattern for backing up */
430 r.b = (unsigned char) (w - ws[htl - 1]); /* bits to dump before this table */
Paul Fox0840b762005-07-20 20:26:49 +0000431 r.e = (unsigned char) (16 + j); /* bits in this table */
432 r.v.t = q; /* pointer to this table */
Rob Landley11c7a7b2006-06-25 22:39:24 +0000433 j = (i & ((1 << w) - 1)) >> ws[htl - 1];
434 u[htl - 1][j] = r; /* connect to last table */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000435 }
436 }
437
438 /* set up table entry in r */
439 r.b = (unsigned char) (k - w);
Denys Vlasenko6bd3fff2015-10-30 23:41:53 +0100440 if (/*p >= v + n || -- redundant, caught by the second check: */
441 *p == UINT_MAX /* do we access uninited v[i]? (see memset(v))*/
442 ) {
Paul Fox0840b762005-07-20 20:26:49 +0000443 r.e = 99; /* out of values--invalid code */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000444 } else if (*p < s) {
Paul Fox0840b762005-07-20 20:26:49 +0000445 r.e = (unsigned char) (*p < 256 ? 16 : 15); /* 256 is EOB code */
446 r.v.n = (unsigned short) (*p++); /* simple code is just the value */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000447 } else {
Denys Vlasenko6572ef62019-10-19 18:52:41 +0200448 r.e = (unsigned char) cp_ext->ext[*p - s]; /* non-simple--look up in lists */
449 r.v.n = cp_ext->cp[*p++ - s];
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000450 }
451
452 /* fill code-like entries with r */
453 f = 1 << (k - w);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000454 for (j = i >> w; j < z; j += f) {
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000455 q[j] = r;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000456 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000457
458 /* backwards increment the k-bit code i */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000459 for (j = 1 << (k - 1); i & j; j >>= 1) {
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000460 i ^= j;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000461 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000462 i ^= j;
463
464 /* backup over finished tables */
Rob Landley11c7a7b2006-06-25 22:39:24 +0000465 while ((i & ((1 << w) - 1)) != x[htl]) {
466 w = ws[--htl];
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000467 }
468 }
469 }
Paul Fox0840b762005-07-20 20:26:49 +0000470
471 /* return actual size of base table */
472 *m = ws[1];
473
Denys Vlasenkoc7633922019-10-19 18:33:49 +0200474 if (y != 0 && g != 1) /* we were given an incomplete table */
475 return NULL;
476
477 return result;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000478}
479
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000480
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000481/*
482 * inflate (decompress) the codes in a deflated (compressed) block.
483 * Return an error code or zero if it all goes ok.
484 *
485 * tl, td: literal/length and distance decoder tables
486 * bl, bd: number of bits decoded by tl[] and td[]
487 */
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000488/* called once from inflate_block */
Denis Vlasenko07766bb2007-03-14 00:06:51 +0000489
490/* map formerly local static variables to globals */
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000491#define ml inflate_codes_ml
492#define md inflate_codes_md
493#define bb inflate_codes_bb
494#define k inflate_codes_k
495#define w inflate_codes_w
496#define tl inflate_codes_tl
497#define td inflate_codes_td
498#define bl inflate_codes_bl
499#define bd inflate_codes_bd
Denis Vlasenko75735412007-01-10 20:50:04 +0000500#define nn inflate_codes_nn
501#define dd inflate_codes_dd
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000502static void inflate_codes_setup(STATE_PARAM unsigned my_bl, unsigned my_bd)
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000503{
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000504 bl = my_bl;
505 bd = my_bd;
506 /* make local copies of globals */
507 bb = gunzip_bb; /* initialize bit buffer */
508 k = gunzip_bk;
509 w = gunzip_outbuf_count; /* initialize gunzip_window position */
510 /* inflate the coded data */
511 ml = mask_bits[bl]; /* precompute masks for speed */
512 md = mask_bits[bd];
513}
514/* called once from inflate_get_next_window */
Denys Vlasenkoadf922e2009-10-08 14:35:37 +0200515static NOINLINE int inflate_codes(STATE_PARAM_ONLY)
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000516{
517 unsigned e; /* table entry flag/number of extra bits */
518 huft_t *t; /* pointer to table entry */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000519
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000520 if (resume_copy)
521 goto do_copy;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000522
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000523 while (1) { /* do until end of block */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000524 bb = fill_bitbuffer(PASS_STATE bb, &k, bl);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000525 t = tl + ((unsigned) bb & ml);
Denis Vlasenko447b5432007-01-05 19:49:02 +0000526 e = t->e;
527 if (e > 16)
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000528 do {
Denys Vlasenko6bd3fff2015-10-30 23:41:53 +0100529 if (e == 99) {
530 abort_unzip(PASS_STATE_ONLY);
531 }
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000532 bb >>= t->b;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000533 k -= t->b;
534 e -= 16;
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000535 bb = fill_bitbuffer(PASS_STATE bb, &k, e);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000536 t = t->v.t + ((unsigned) bb & mask_bits[e]);
Denis Vlasenko447b5432007-01-05 19:49:02 +0000537 e = t->e;
538 } while (e > 16);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000539 bb >>= t->b;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000540 k -= t->b;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000541 if (e == 16) { /* then it's a literal */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000542 gunzip_window[w++] = (unsigned char) t->v.n;
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000543 if (w == GUNZIP_WSIZE) {
544 gunzip_outbuf_count = w;
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000545 //flush_gunzip_window();
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000546 w = 0;
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000547 return 1; // We have a block to read
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000548 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000549 } else { /* it's an EOB or a length */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000550 /* exit if end of block */
551 if (e == 15) {
552 break;
553 }
554
555 /* get length of block to copy */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000556 bb = fill_bitbuffer(PASS_STATE bb, &k, e);
Denis Vlasenko75735412007-01-10 20:50:04 +0000557 nn = t->v.n + ((unsigned) bb & mask_bits[e]);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000558 bb >>= e;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000559 k -= e;
560
561 /* decode distance of block to copy */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000562 bb = fill_bitbuffer(PASS_STATE bb, &k, bd);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000563 t = td + ((unsigned) bb & md);
Denis Vlasenko447b5432007-01-05 19:49:02 +0000564 e = t->e;
565 if (e > 16)
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000566 do {
Denys Vlasenko6bd3fff2015-10-30 23:41:53 +0100567 if (e == 99) {
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000568 abort_unzip(PASS_STATE_ONLY);
Denys Vlasenko6bd3fff2015-10-30 23:41:53 +0100569 }
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000570 bb >>= t->b;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000571 k -= t->b;
572 e -= 16;
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000573 bb = fill_bitbuffer(PASS_STATE bb, &k, e);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000574 t = t->v.t + ((unsigned) bb & mask_bits[e]);
Denis Vlasenko447b5432007-01-05 19:49:02 +0000575 e = t->e;
576 } while (e > 16);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000577 bb >>= t->b;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000578 k -= t->b;
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000579 bb = fill_bitbuffer(PASS_STATE bb, &k, e);
Denis Vlasenko75735412007-01-10 20:50:04 +0000580 dd = w - t->v.n - ((unsigned) bb & mask_bits[e]);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000581 bb >>= e;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000582 k -= e;
583
584 /* do the copy */
Denis Vlasenko447b5432007-01-05 19:49:02 +0000585 do_copy:
586 do {
Denis Vlasenko75735412007-01-10 20:50:04 +0000587 /* Was: nn -= (e = (e = GUNZIP_WSIZE - ((dd &= GUNZIP_WSIZE - 1) > w ? dd : w)) > nn ? nn : e); */
Denis Vlasenko447b5432007-01-05 19:49:02 +0000588 /* Who wrote THAT?? rewritten as: */
Joakim Tjernlund0866b362010-02-08 18:55:15 +0100589 unsigned delta;
590
Denis Vlasenko75735412007-01-10 20:50:04 +0000591 dd &= GUNZIP_WSIZE - 1;
592 e = GUNZIP_WSIZE - (dd > w ? dd : w);
Joakim Tjernlund0866b362010-02-08 18:55:15 +0100593 delta = w > dd ? w - dd : dd - w;
Denis Vlasenko75735412007-01-10 20:50:04 +0000594 if (e > nn) e = nn;
595 nn -= e;
Denis Vlasenko447b5432007-01-05 19:49:02 +0000596
597 /* copy to new buffer to prevent possible overwrite */
Joakim Tjernlund0866b362010-02-08 18:55:15 +0100598 if (delta >= e) {
Denis Vlasenko75735412007-01-10 20:50:04 +0000599 memcpy(gunzip_window + w, gunzip_window + dd, e);
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000600 w += e;
Denis Vlasenko75735412007-01-10 20:50:04 +0000601 dd += e;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000602 } else {
Denis Vlasenko447b5432007-01-05 19:49:02 +0000603 /* do it slow to avoid memcpy() overlap */
604 /* !NOMEMCPY */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000605 do {
Denis Vlasenko75735412007-01-10 20:50:04 +0000606 gunzip_window[w++] = gunzip_window[dd++];
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000607 } while (--e);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000608 }
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000609 if (w == GUNZIP_WSIZE) {
610 gunzip_outbuf_count = w;
Denis Vlasenko75735412007-01-10 20:50:04 +0000611 resume_copy = (nn != 0);
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000612 //flush_gunzip_window();
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000613 w = 0;
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000614 return 1;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000615 }
Denis Vlasenko75735412007-01-10 20:50:04 +0000616 } while (nn);
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000617 resume_copy = 0;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000618 }
619 }
620
621 /* restore the globals from the locals */
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000622 gunzip_outbuf_count = w; /* restore global gunzip_window pointer */
623 gunzip_bb = bb; /* restore global bit buffer */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000624 gunzip_bk = k;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000625
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000626 /* normally just after call to inflate_codes, but save code by putting it here */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000627 /* free the decoding tables (tl and td), return */
628 huft_free_all(PASS_STATE_ONLY);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000629
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000630 /* done */
631 return 0;
632}
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000633#undef ml
634#undef md
635#undef bb
636#undef k
637#undef w
638#undef tl
639#undef td
640#undef bl
641#undef bd
Denis Vlasenko1a8bf7f2007-01-10 20:57:03 +0000642#undef nn
643#undef dd
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000644
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000645
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000646/* called once from inflate_block */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000647static void inflate_stored_setup(STATE_PARAM int my_n, int my_b, int my_k)
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000648{
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000649 inflate_stored_n = my_n;
650 inflate_stored_b = my_b;
651 inflate_stored_k = my_k;
652 /* initialize gunzip_window position */
653 inflate_stored_w = gunzip_outbuf_count;
654}
655/* called once from inflate_get_next_window */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000656static int inflate_stored(STATE_PARAM_ONLY)
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000657{
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000658 /* read and output the compressed data */
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000659 while (inflate_stored_n--) {
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000660 inflate_stored_b = fill_bitbuffer(PASS_STATE inflate_stored_b, &inflate_stored_k, 8);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000661 gunzip_window[inflate_stored_w++] = (unsigned char) inflate_stored_b;
662 if (inflate_stored_w == GUNZIP_WSIZE) {
663 gunzip_outbuf_count = inflate_stored_w;
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000664 //flush_gunzip_window();
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000665 inflate_stored_w = 0;
666 inflate_stored_b >>= 8;
667 inflate_stored_k -= 8;
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000668 return 1; /* We have a block */
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000669 }
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000670 inflate_stored_b >>= 8;
671 inflate_stored_k -= 8;
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000672 }
673
674 /* restore the globals from the locals */
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000675 gunzip_outbuf_count = inflate_stored_w; /* restore global gunzip_window pointer */
676 gunzip_bb = inflate_stored_b; /* restore global bit buffer */
677 gunzip_bk = inflate_stored_k;
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000678 return 0; /* Finished */
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000679}
680
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000681
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000682/*
683 * decompress an inflated block
684 * e: last block flag
685 *
686 * GLOBAL VARIABLES: bb, kk,
687 */
Denis Vlasenko447b5432007-01-05 19:49:02 +0000688/* Return values: -1 = inflate_stored, -2 = inflate_codes */
689/* One callsite in inflate_get_next_window */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000690static int inflate_block(STATE_PARAM smallint *e)
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000691{
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000692 unsigned ll[286 + 30]; /* literal/length and distance code lengths */
693 unsigned t; /* block type */
694 unsigned b; /* bit buffer */
695 unsigned k; /* number of bits in bit buffer */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000696
697 /* make local bit buffer */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000698
699 b = gunzip_bb;
700 k = gunzip_bk;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000701
702 /* read in last block bit */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000703 b = fill_bitbuffer(PASS_STATE b, &k, 1);
704 *e = b & 1;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000705 b >>= 1;
706 k -= 1;
707
708 /* read in block type */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000709 b = fill_bitbuffer(PASS_STATE b, &k, 2);
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000710 t = (unsigned) b & 3;
711 b >>= 2;
712 k -= 2;
713
714 /* restore the global bit buffer */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000715 gunzip_bb = b;
716 gunzip_bk = k;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000717
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000718 /* Do we see block type 1 often? Yes!
719 * TODO: fix performance problem (see below) */
720 //bb_error_msg("blktype %d", t);
721
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000722 /* inflate that block type */
723 switch (t) {
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000724 case 0: /* Inflate stored */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000725 {
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000726 unsigned n; /* number of bytes in block */
727 unsigned b_stored; /* bit buffer */
728 unsigned k_stored; /* number of bits in bit buffer */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000729
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000730 /* make local copies of globals */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000731 b_stored = gunzip_bb; /* initialize bit buffer */
732 k_stored = gunzip_bk;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000733
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000734 /* go to byte boundary */
735 n = k_stored & 7;
736 b_stored >>= n;
737 k_stored -= n;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000738
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000739 /* get the length and its complement */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000740 b_stored = fill_bitbuffer(PASS_STATE b_stored, &k_stored, 16);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000741 n = ((unsigned) b_stored & 0xffff);
742 b_stored >>= 16;
743 k_stored -= 16;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000744
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000745 b_stored = fill_bitbuffer(PASS_STATE b_stored, &k_stored, 16);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000746 if (n != (unsigned) ((~b_stored) & 0xffff)) {
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000747 abort_unzip(PASS_STATE_ONLY); /* error in compressed data */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000748 }
749 b_stored >>= 16;
750 k_stored -= 16;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000751
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000752 inflate_stored_setup(PASS_STATE n, b_stored, k_stored);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000753
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000754 return -1;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000755 }
Denis Vlasenkocd42cb82007-01-05 23:56:53 +0000756 case 1:
757 /* Inflate fixed
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000758 * decompress an inflated type 1 (fixed Huffman codes) block. We should
Denis Vlasenkocd42cb82007-01-05 23:56:53 +0000759 * either replace this with a custom decoder, or at least precompute the
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000760 * Huffman tables. TODO */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000761 {
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000762 int i; /* temporary variable */
763 unsigned bl; /* lookup bits for tl */
764 unsigned bd; /* lookup bits for td */
765 /* gcc 4.2.1 is too dumb to reuse stackspace. Moved up... */
766 //unsigned ll[288]; /* length list for huft_build */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000767
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000768 /* set up literal table */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000769 for (i = 0; i < 144; i++)
770 ll[i] = 8;
771 for (; i < 256; i++)
772 ll[i] = 9;
773 for (; i < 280; i++)
774 ll[i] = 7;
775 for (; i < 288; i++) /* make a complete, but wrong code set */
776 ll[i] = 8;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000777 bl = 7;
Denys Vlasenko6572ef62019-10-19 18:52:41 +0200778 inflate_codes_tl = huft_build(ll, 288, 257, &lit, &bl);
Denys Vlasenkoc7633922019-10-19 18:33:49 +0200779 /* huft_build() never returns error here - we use known data */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000780
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000781 /* set up distance table */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000782 for (i = 0; i < 30; i++) /* make an incomplete code set */
783 ll[i] = 5;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000784 bd = 5;
Denys Vlasenko6572ef62019-10-19 18:52:41 +0200785 inflate_codes_td = huft_build(ll, 30, 0, &dist, &bd);
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000786
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000787 /* set up data for inflate_codes() */
788 inflate_codes_setup(PASS_STATE bl, bd);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000789
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000790 /* huft_free code moved into inflate_codes */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000791
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000792 return -2;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000793 }
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000794 case 2: /* Inflate dynamic */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000795 {
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000796 enum { dbits = 6 }; /* bits in base distance lookup table */
797 enum { lbits = 9 }; /* bits in base literal/length lookup table */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000798
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000799 huft_t *td; /* distance code table */
800 unsigned i; /* temporary variables */
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000801 unsigned j;
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000802 unsigned l; /* last length */
803 unsigned m; /* mask for bit lengths table */
804 unsigned n; /* number of lengths to get */
805 unsigned bl; /* lookup bits for tl */
806 unsigned bd; /* lookup bits for td */
807 unsigned nb; /* number of bit length codes */
808 unsigned nl; /* number of literal/length codes */
809 unsigned nd; /* number of distance codes */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000810
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000811 //unsigned ll[286 + 30];/* literal/length and distance code lengths */
812 unsigned b_dynamic; /* bit buffer */
813 unsigned k_dynamic; /* number of bits in bit buffer */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000814
815 /* make local bit buffer */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000816 b_dynamic = gunzip_bb;
817 k_dynamic = gunzip_bk;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000818
819 /* read in table lengths */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000820 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 5);
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000821 nl = 257 + ((unsigned) b_dynamic & 0x1f); /* number of literal/length codes */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000822
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000823 b_dynamic >>= 5;
824 k_dynamic -= 5;
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000825 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 5);
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000826 nd = 1 + ((unsigned) b_dynamic & 0x1f); /* number of distance codes */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000827
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000828 b_dynamic >>= 5;
829 k_dynamic -= 5;
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000830 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 4);
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000831 nb = 4 + ((unsigned) b_dynamic & 0xf); /* number of bit length codes */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000832
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000833 b_dynamic >>= 4;
834 k_dynamic -= 4;
Denys Vlasenko6bd3fff2015-10-30 23:41:53 +0100835 if (nl > 286 || nd > 30) {
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000836 abort_unzip(PASS_STATE_ONLY); /* bad lengths */
Denys Vlasenko6bd3fff2015-10-30 23:41:53 +0100837 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000838
839 /* read in bit-length-code lengths */
840 for (j = 0; j < nb; j++) {
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000841 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 3);
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000842 ll[border[j]] = (unsigned) b_dynamic & 7;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000843 b_dynamic >>= 3;
844 k_dynamic -= 3;
845 }
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000846 for (; j < 19; j++)
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000847 ll[border[j]] = 0;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000848
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000849 /* build decoding table for trees - single level, 7 bit lookup */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000850 bl = 7;
Denys Vlasenko6572ef62019-10-19 18:52:41 +0200851 inflate_codes_tl = huft_build(ll, 19, 19, NULL, &bl);
Denys Vlasenkoc7633922019-10-19 18:33:49 +0200852 if (!inflate_codes_tl) {
853 abort_unzip(PASS_STATE_ONLY); /* incomplete code set */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000854 }
855
856 /* read in literal and distance code lengths */
857 n = nl + nd;
858 m = mask_bits[bl];
859 i = l = 0;
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000860 while ((unsigned) i < n) {
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000861 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, (unsigned)bl);
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000862 td = inflate_codes_tl + ((unsigned) b_dynamic & m);
863 j = td->b;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000864 b_dynamic >>= j;
865 k_dynamic -= j;
866 j = td->v.n;
867 if (j < 16) { /* length of code in bits (0..15) */
868 ll[i++] = l = j; /* save last length in l */
869 } else if (j == 16) { /* repeat last length 3 to 6 times */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000870 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 2);
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000871 j = 3 + ((unsigned) b_dynamic & 3);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000872 b_dynamic >>= 2;
873 k_dynamic -= 2;
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000874 if ((unsigned) i + j > n) {
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000875 abort_unzip(PASS_STATE_ONLY); //return 1;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000876 }
877 while (j--) {
878 ll[i++] = l;
879 }
880 } else if (j == 17) { /* 3 to 10 zero length codes */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000881 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 3);
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000882 j = 3 + ((unsigned) b_dynamic & 7);
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000883 b_dynamic >>= 3;
884 k_dynamic -= 3;
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000885 if ((unsigned) i + j > n) {
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000886 abort_unzip(PASS_STATE_ONLY); //return 1;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000887 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000888 while (j--) {
889 ll[i++] = 0;
890 }
891 l = 0;
892 } else { /* j == 18: 11 to 138 zero length codes */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000893 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 7);
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000894 j = 11 + ((unsigned) b_dynamic & 0x7f);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000895 b_dynamic >>= 7;
896 k_dynamic -= 7;
Denis Vlasenkocc33ef12007-01-05 19:46:04 +0000897 if ((unsigned) i + j > n) {
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000898 abort_unzip(PASS_STATE_ONLY); //return 1;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000899 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000900 while (j--) {
901 ll[i++] = 0;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000902 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000903 l = 0;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000904 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000905 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000906
907 /* free decoding table for trees */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000908 huft_free(inflate_codes_tl);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000909
910 /* restore the global bit buffer */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000911 gunzip_bb = b_dynamic;
912 gunzip_bk = k_dynamic;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000913
914 /* build the decoding tables for literal/length and distance codes */
915 bl = lbits;
Denys Vlasenko6572ef62019-10-19 18:52:41 +0200916 inflate_codes_tl = huft_build(ll, nl, 257, &lit, &bl);
Denys Vlasenkoc7633922019-10-19 18:33:49 +0200917 if (!inflate_codes_tl) {
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000918 abort_unzip(PASS_STATE_ONLY);
Denys Vlasenko6bd3fff2015-10-30 23:41:53 +0100919 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000920 bd = dbits;
Denys Vlasenko6572ef62019-10-19 18:52:41 +0200921 inflate_codes_td = huft_build(ll + nl, nd, 0, &dist, &bd);
Denys Vlasenkoc7633922019-10-19 18:33:49 +0200922 if (!inflate_codes_td) {
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000923 abort_unzip(PASS_STATE_ONLY);
Denys Vlasenko6bd3fff2015-10-30 23:41:53 +0100924 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000925
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000926 /* set up data for inflate_codes() */
927 inflate_codes_setup(PASS_STATE bl, bd);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000928
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000929 /* huft_free code moved into inflate_codes */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000930
Glenn L McGrath0126fda2002-11-20 06:46:46 +0000931 return -2;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000932 }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000933 default:
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000934 abort_unzip(PASS_STATE_ONLY);
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000935 }
936}
937
Denis Vlasenko447b5432007-01-05 19:49:02 +0000938/* Two callsites, both in inflate_get_next_window */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000939static void calculate_gunzip_crc(STATE_PARAM_ONLY)
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000940{
Denys Vlasenko9ce642f2010-10-27 15:26:45 +0200941 gunzip_crc = crc32_block_endian0(gunzip_crc, gunzip_window, gunzip_outbuf_count, gunzip_crc_table);
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000942 gunzip_bytes_out += gunzip_outbuf_count;
943}
944
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000945/* One callsite in inflate_unzip_internal */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000946static int inflate_get_next_window(STATE_PARAM_ONLY)
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000947{
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000948 gunzip_outbuf_count = 0;
949
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000950 while (1) {
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000951 int ret;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000952
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000953 if (need_another_block) {
954 if (end_reached) {
955 calculate_gunzip_crc(PASS_STATE_ONLY);
956 end_reached = 0;
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000957 /* NB: need_another_block is still set */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000958 return 0; /* Last block */
959 }
960 method = inflate_block(PASS_STATE &end_reached);
961 need_another_block = 0;
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000962 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000963
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000964 switch (method) {
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000965 case -1:
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000966 ret = inflate_stored(PASS_STATE_ONLY);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000967 break;
968 case -2:
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000969 ret = inflate_codes(PASS_STATE_ONLY);
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000970 break;
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000971 default: /* cannot happen */
972 abort_unzip(PASS_STATE_ONLY);
Glenn L McGrath83bf47c2002-11-20 22:00:31 +0000973 }
974
975 if (ret == 1) {
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000976 calculate_gunzip_crc(PASS_STATE_ONLY);
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000977 return 1; /* more data left */
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +0000978 }
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000979 need_another_block = 1; /* end of that block */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000980 }
Glenn L McGratheda4f532002-11-24 06:01:20 +0000981 /* Doesnt get here */
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000982}
983
Paul Fox0840b762005-07-20 20:26:49 +0000984
Denis Vlasenkoc14d39e2007-06-08 13:05:39 +0000985/* Called from unpack_gz_stream() and inflate_unzip() */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000986static IF_DESKTOP(long long) int
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100987inflate_unzip_internal(STATE_PARAM transformer_state_t *xstate)
Glenn L McGrathfd73b8c2002-11-17 21:33:30 +0000988{
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000989 IF_DESKTOP(long long) int n = 0;
Glenn L McGrath5699b852003-11-15 23:19:05 +0000990 ssize_t nwrote;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000991
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000992 /* Allocate all global buffers (for DYN_ALLOC option) */
Denis Vlasenkod1a19af2007-01-05 23:58:45 +0000993 gunzip_window = xmalloc(GUNZIP_WSIZE);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000994 gunzip_outbuf_count = 0;
995 gunzip_bytes_out = 0;
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100996 gunzip_src_fd = xstate->src_fd;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000997
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000998 /* (re) initialize state */
999 method = -1;
1000 need_another_block = 1;
1001 resume_copy = 0;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +00001002 gunzip_bk = 0;
1003 gunzip_bb = 0;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +00001004
Glenn L McGrath7ca04f32002-09-25 02:47:48 +00001005 /* Create the crc table */
Denys Vlasenkoddacb032018-02-01 10:56:19 +01001006 gunzip_crc_table = crc32_new_table_le();
Rob Landleyc57ec372006-04-10 17:07:15 +00001007 gunzip_crc = ~0;
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00001008
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001009 error_msg = "corrupted data";
1010 if (setjmp(error_jmp)) {
1011 /* Error from deep inside zip machinery */
James Byrne69374872019-07-02 11:35:03 +02001012 bb_simple_error_msg(error_msg);
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001013 n = -1;
1014 goto ret;
1015 }
Glenn L McGrath5699b852003-11-15 23:19:05 +00001016
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001017 while (1) {
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +00001018 int r = inflate_get_next_window(PASS_STATE_ONLY);
Denys Vlasenkob4c11c12014-12-07 00:44:00 +01001019 nwrote = transformer_write(xstate, gunzip_window, gunzip_outbuf_count);
1020 if (nwrote == (ssize_t)-1) {
Denis Vlasenkod1a19af2007-01-05 23:58:45 +00001021 n = -1;
1022 goto ret;
Glenn L McGrath5699b852003-11-15 23:19:05 +00001023 }
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001024 IF_DESKTOP(n += nwrote;)
Denis Vlasenkod1a19af2007-01-05 23:58:45 +00001025 if (r == 0) break;
Glenn L McGrath5699b852003-11-15 23:19:05 +00001026 }
1027
Glenn L McGrath7ca04f32002-09-25 02:47:48 +00001028 /* Store unused bytes in a global buffer so calling applets can access it */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +00001029 if (gunzip_bk >= 8) {
1030 /* Undo too much lookahead. The next read will be byte aligned
1031 * so we can discard unused bits in the last meaningful byte. */
Glenn L McGratheda4f532002-11-24 06:01:20 +00001032 bytebuffer_offset--;
1033 bytebuffer[bytebuffer_offset] = gunzip_bb & 0xff;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +00001034 gunzip_bb >>= 8;
1035 gunzip_bk -= 8;
Glenn L McGrath7fd92942001-04-11 03:11:33 +00001036 }
Denis Vlasenkod1a19af2007-01-05 23:58:45 +00001037 ret:
1038 /* Cleanup */
1039 free(gunzip_window);
1040 free(gunzip_crc_table);
1041 return n;
Glenn L McGrath7fd92942001-04-11 03:11:33 +00001042}
Glenn L McGrathcc616922003-02-09 04:46:34 +00001043
Denis Vlasenkocd42cb82007-01-05 23:56:53 +00001044
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001045/* External entry points */
1046
1047/* For unzip */
1048
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001049IF_DESKTOP(long long) int FAST_FUNC
Denys Vlasenkob4c11c12014-12-07 00:44:00 +01001050inflate_unzip(transformer_state_t *xstate)
Denis Vlasenkocd42cb82007-01-05 23:56:53 +00001051{
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001052 IF_DESKTOP(long long) int n;
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +00001053 DECLARE_STATE;
1054
1055 ALLOC_STATE;
Denis Vlasenkocd42cb82007-01-05 23:56:53 +00001056
Denys Vlasenkoe7800f32014-12-07 00:42:49 +01001057 to_read = xstate->bytes_in;
Denis Vlasenkoe8ef7ec2008-02-04 12:12:48 +00001058// bytebuffer_max = 0x8000;
Denis Vlasenkocd42cb82007-01-05 23:56:53 +00001059 bytebuffer_offset = 4;
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001060 bytebuffer = xmalloc(bytebuffer_max);
Denys Vlasenkob4c11c12014-12-07 00:44:00 +01001061 n = inflate_unzip_internal(PASS_STATE xstate);
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001062 free(bytebuffer);
Denis Vlasenkocd42cb82007-01-05 23:56:53 +00001063
Denys Vlasenkoe7800f32014-12-07 00:42:49 +01001064 xstate->crc32 = gunzip_crc;
1065 xstate->bytes_out = gunzip_bytes_out;
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +00001066 DEALLOC_STATE;
Denis Vlasenkocd42cb82007-01-05 23:56:53 +00001067 return n;
1068}
1069
1070
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001071/* For gunzip */
1072
1073/* helpers first */
1074
1075/* Top up the input buffer with at least n bytes. */
1076static int top_up(STATE_PARAM unsigned n)
1077{
1078 int count = bytebuffer_size - bytebuffer_offset;
1079
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001080 if (count < (int)n) {
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001081 memmove(bytebuffer, &bytebuffer[bytebuffer_offset], count);
1082 bytebuffer_offset = 0;
1083 bytebuffer_size = full_read(gunzip_src_fd, &bytebuffer[count], bytebuffer_max - count);
1084 if ((int)bytebuffer_size < 0) {
James Byrne69374872019-07-02 11:35:03 +02001085 bb_simple_error_msg(bb_msg_read_error);
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001086 return 0;
1087 }
1088 bytebuffer_size += count;
1089 if (bytebuffer_size < n)
1090 return 0;
1091 }
1092 return 1;
1093}
1094
1095static uint16_t buffer_read_le_u16(STATE_PARAM_ONLY)
1096{
1097 uint16_t res;
1098#if BB_LITTLE_ENDIAN
Denis Vlasenkoefb545b2008-12-08 22:56:18 +00001099 move_from_unaligned16(res, &bytebuffer[bytebuffer_offset]);
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001100#else
1101 res = bytebuffer[bytebuffer_offset];
1102 res |= bytebuffer[bytebuffer_offset + 1] << 8;
1103#endif
1104 bytebuffer_offset += 2;
1105 return res;
1106}
1107
1108static uint32_t buffer_read_le_u32(STATE_PARAM_ONLY)
1109{
1110 uint32_t res;
1111#if BB_LITTLE_ENDIAN
Denis Vlasenkoefb545b2008-12-08 22:56:18 +00001112 move_from_unaligned32(res, &bytebuffer[bytebuffer_offset]);
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001113#else
1114 res = bytebuffer[bytebuffer_offset];
1115 res |= bytebuffer[bytebuffer_offset + 1] << 8;
1116 res |= bytebuffer[bytebuffer_offset + 2] << 16;
1117 res |= bytebuffer[bytebuffer_offset + 3] << 24;
1118#endif
1119 bytebuffer_offset += 4;
1120 return res;
1121}
1122
Denys Vlasenkoe7800f32014-12-07 00:42:49 +01001123static int check_header_gzip(STATE_PARAM transformer_state_t *xstate)
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001124{
1125 union {
1126 unsigned char raw[8];
1127 struct {
1128 uint8_t gz_method;
1129 uint8_t flags;
Denis Vlasenko1a9e9bd2008-11-01 12:54:56 +00001130 uint32_t mtime;
1131 uint8_t xtra_flags_UNUSED;
1132 uint8_t os_flags_UNUSED;
Denys Vlasenko8dc0e192009-09-16 00:58:11 +02001133 } PACKED formatted;
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001134 } header;
Denys Vlasenko7b85ec32015-10-13 17:17:34 +02001135
1136 BUILD_BUG_ON(sizeof(header) != 8);
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001137
1138 /*
1139 * Rewind bytebuffer. We use the beginning because the header has 8
1140 * bytes, leaving enough for unwinding afterwards.
1141 */
1142 bytebuffer_size -= bytebuffer_offset;
1143 memmove(bytebuffer, &bytebuffer[bytebuffer_offset], bytebuffer_size);
1144 bytebuffer_offset = 0;
1145
1146 if (!top_up(PASS_STATE 8))
1147 return 0;
1148 memcpy(header.raw, &bytebuffer[bytebuffer_offset], 8);
1149 bytebuffer_offset += 8;
1150
1151 /* Check the compression method */
1152 if (header.formatted.gz_method != 8) {
1153 return 0;
1154 }
1155
1156 if (header.formatted.flags & 0x04) {
1157 /* bit 2 set: extra field present */
1158 unsigned extra_short;
1159
1160 if (!top_up(PASS_STATE 2))
1161 return 0;
1162 extra_short = buffer_read_le_u16(PASS_STATE_ONLY);
1163 if (!top_up(PASS_STATE extra_short))
1164 return 0;
1165 /* Ignore extra field */
1166 bytebuffer_offset += extra_short;
1167 }
1168
1169 /* Discard original name and file comment if any */
1170 /* bit 3 set: original file name present */
1171 /* bit 4 set: file comment present */
1172 if (header.formatted.flags & 0x18) {
1173 while (1) {
1174 do {
1175 if (!top_up(PASS_STATE 1))
1176 return 0;
1177 } while (bytebuffer[bytebuffer_offset++] != 0);
1178 if ((header.formatted.flags & 0x18) != 0x18)
1179 break;
1180 header.formatted.flags &= ~0x18;
1181 }
1182 }
1183
Denys Vlasenkob4c11c12014-12-07 00:44:00 +01001184 xstate->mtime = SWAP_LE32(header.formatted.mtime);
Denis Vlasenko1a9e9bd2008-11-01 12:54:56 +00001185
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001186 /* Read the header checksum */
1187 if (header.formatted.flags & 0x02) {
1188 if (!top_up(PASS_STATE 2))
1189 return 0;
1190 bytebuffer_offset += 2;
1191 }
1192 return 1;
1193}
1194
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001195IF_DESKTOP(long long) int FAST_FUNC
Denys Vlasenkob4c11c12014-12-07 00:44:00 +01001196unpack_gz_stream(transformer_state_t *xstate)
Glenn L McGrathcc616922003-02-09 04:46:34 +00001197{
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001198 uint32_t v32;
Denys Vlasenko59655072012-03-06 16:23:50 +01001199 IF_DESKTOP(long long) int total, n;
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +00001200 DECLARE_STATE;
Glenn L McGrathcc616922003-02-09 04:46:34 +00001201
Denys Vlasenko02c3c382012-03-06 16:32:06 +01001202#if !ENABLE_FEATURE_SEAMLESS_Z
Denys Vlasenkob4c11c12014-12-07 00:44:00 +01001203 if (check_signature16(xstate, GZIP_MAGIC))
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +01001204 return -1;
Denys Vlasenko02c3c382012-03-06 16:32:06 +01001205#else
Denys Vlasenko984b0a62016-06-20 11:06:42 +02001206 if (!xstate->signature_skipped) {
Denys Vlasenko02c3c382012-03-06 16:32:06 +01001207 uint16_t magic2;
1208
Denys Vlasenkob4c11c12014-12-07 00:44:00 +01001209 if (full_read(xstate->src_fd, &magic2, 2) != 2) {
Denys Vlasenko02c3c382012-03-06 16:32:06 +01001210 bad_magic:
James Byrne69374872019-07-02 11:35:03 +02001211 bb_simple_error_msg("invalid magic");
Denys Vlasenko02c3c382012-03-06 16:32:06 +01001212 return -1;
1213 }
1214 if (magic2 == COMPRESS_MAGIC) {
Denys Vlasenko984b0a62016-06-20 11:06:42 +02001215 xstate->signature_skipped = 2;
Denys Vlasenkob4c11c12014-12-07 00:44:00 +01001216 return unpack_Z_stream(xstate);
Denys Vlasenko02c3c382012-03-06 16:32:06 +01001217 }
1218 if (magic2 != GZIP_MAGIC)
1219 goto bad_magic;
1220 }
1221#endif
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +01001222
Denys Vlasenko59655072012-03-06 16:23:50 +01001223 total = 0;
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001224
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +00001225 ALLOC_STATE;
Denis Vlasenkoe8ef7ec2008-02-04 12:12:48 +00001226 to_read = -1;
1227// bytebuffer_max = 0x8000;
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001228 bytebuffer = xmalloc(bytebuffer_max);
Denys Vlasenkob4c11c12014-12-07 00:44:00 +01001229 gunzip_src_fd = xstate->src_fd;
Denis Vlasenkocd42cb82007-01-05 23:56:53 +00001230
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001231 again:
Denys Vlasenkoe7800f32014-12-07 00:42:49 +01001232 if (!check_header_gzip(PASS_STATE xstate)) {
James Byrne69374872019-07-02 11:35:03 +02001233 bb_simple_error_msg("corrupted data");
Denys Vlasenko59655072012-03-06 16:23:50 +01001234 total = -1;
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001235 goto ret;
Glenn L McGrathcc616922003-02-09 04:46:34 +00001236 }
Denys Vlasenko59655072012-03-06 16:23:50 +01001237
Denys Vlasenkob4c11c12014-12-07 00:44:00 +01001238 n = inflate_unzip_internal(PASS_STATE xstate);
Denys Vlasenko59655072012-03-06 16:23:50 +01001239 if (n < 0) {
1240 total = -1;
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001241 goto ret;
Denys Vlasenko59655072012-03-06 16:23:50 +01001242 }
1243 total += n;
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001244
1245 if (!top_up(PASS_STATE 8)) {
James Byrne69374872019-07-02 11:35:03 +02001246 bb_simple_error_msg("corrupted data");
Denys Vlasenko59655072012-03-06 16:23:50 +01001247 total = -1;
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001248 goto ret;
Glenn L McGrathcc616922003-02-09 04:46:34 +00001249 }
1250
1251 /* Validate decompression - crc */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001252 v32 = buffer_read_le_u32(PASS_STATE_ONLY);
1253 if ((~gunzip_crc) != v32) {
James Byrne69374872019-07-02 11:35:03 +02001254 bb_simple_error_msg("crc error");
Denys Vlasenko59655072012-03-06 16:23:50 +01001255 total = -1;
Denis Vlasenkocd42cb82007-01-05 23:56:53 +00001256 goto ret;
Glenn L McGrathcc616922003-02-09 04:46:34 +00001257 }
1258
1259 /* Validate decompression - size */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001260 v32 = buffer_read_le_u32(PASS_STATE_ONLY);
1261 if ((uint32_t)gunzip_bytes_out != v32) {
James Byrne69374872019-07-02 11:35:03 +02001262 bb_simple_error_msg("incorrect length");
Denys Vlasenko59655072012-03-06 16:23:50 +01001263 total = -1;
Glenn L McGrathcc616922003-02-09 04:46:34 +00001264 }
Denis Vlasenko5dd8a032007-10-05 15:26:08 +00001265
1266 if (!top_up(PASS_STATE 2))
1267 goto ret; /* EOF */
1268
1269 if (bytebuffer[bytebuffer_offset] == 0x1f
1270 && bytebuffer[bytebuffer_offset + 1] == 0x8b
1271 ) {
1272 bytebuffer_offset += 2;
1273 goto again;
1274 }
1275 /* GNU gzip says: */
1276 /*bb_error_msg("decompression OK, trailing garbage ignored");*/
1277
Denis Vlasenkocd42cb82007-01-05 23:56:53 +00001278 ret:
1279 free(bytebuffer);
Denis Vlasenkoc7a4aa52007-01-06 00:03:11 +00001280 DEALLOC_STATE;
Denys Vlasenko59655072012-03-06 16:23:50 +01001281 return total;
Glenn L McGrathcc616922003-02-09 04:46:34 +00001282}