Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 1 | /* 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 Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 10 | * Adjusted further by Erik Andersen <andersen@codepoet.org> to support |
Eric Andersen | db93094 | 2001-12-06 08:20:14 +0000 | [diff] [blame] | 11 | * files as well as stdin/stdout, and to generally behave itself wrt |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 12 | * command line handling. |
| 13 | * |
| 14 | * General cleanup to better adhere to the style guide and make use of standard |
Denis Vlasenko | 0beaff8 | 2007-09-21 13:16:32 +0000 | [diff] [blame] | 15 | * busybox functions by Glenn McGrath |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 16 | * |
Glenn L McGrath | 83bf47c | 2002-11-20 22:00:31 +0000 | [diff] [blame] | 17 | * read_gz interface + associated hacking by Laurence Anderson |
| 18 | * |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 19 | * 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 McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 24 | * 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 McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 31 | * See the file algorithm.doc for the compression algorithms and file formats. |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 32 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 33 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 34 | */ |
Rob Landley | 11c7a7b | 2006-06-25 22:39:24 +0000 | [diff] [blame] | 35 | #include "libbb.h" |
Denys Vlasenko | d184a72 | 2011-09-22 12:45:14 +0200 | [diff] [blame] | 36 | #include "bb_archive.h" |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 37 | |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 38 | typedef struct huft_t { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 39 | unsigned char e; /* number of extra bits or operation */ |
| 40 | unsigned char b; /* number of bits in this code or subcode */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 41 | union { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 42 | unsigned short n; /* literal, length base, or distance base */ |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 43 | struct huft_t *t; /* pointer to next level of table */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 44 | } v; |
| 45 | } huft_t; |
| 46 | |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 47 | enum { |
| 48 | /* gunzip_window size--must be a power of two, and |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 49 | * at least 32K for zip's deflate method */ |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 50 | 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 Vlasenko | bb3d0fa | 2007-01-03 01:57:25 +0000 | [diff] [blame] | 56 | |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 57 | /* 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 McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 67 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 68 | |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 69 | typedef struct state_t { |
| 70 | off_t gunzip_bytes_out; /* number of output bytes */ |
| 71 | uint32_t gunzip_crc; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 72 | |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 73 | 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 Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 84 | /* input (compressed) data */ |
| 85 | unsigned char *bytebuffer; /* buffer itself */ |
Denis Vlasenko | e8ef7ec | 2008-02-04 12:12:48 +0000 | [diff] [blame] | 86 | off_t to_read; /* compressed bytes to read (unzip only, -1 for gunzip) */ |
| 87 | // unsigned bytebuffer_max; /* buffer size */ |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 88 | unsigned bytebuffer_offset; /* buffer position */ |
| 89 | unsigned bytebuffer_size; /* how much data is there (size <= max) */ |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 90 | |
| 91 | /* private data of inflate_codes() */ |
| 92 | unsigned inflate_codes_ml; /* masks for bl and bd bits */ |
| 93 | unsigned inflate_codes_md; /* masks for bl and bd bits */ |
| 94 | unsigned inflate_codes_bb; /* bit buffer */ |
| 95 | unsigned inflate_codes_k; /* number of bits in bit buffer */ |
| 96 | unsigned inflate_codes_w; /* current gunzip_window position */ |
| 97 | huft_t *inflate_codes_tl; |
| 98 | huft_t *inflate_codes_td; |
| 99 | unsigned inflate_codes_bl; |
| 100 | unsigned inflate_codes_bd; |
Denis Vlasenko | 7573541 | 2007-01-10 20:50:04 +0000 | [diff] [blame] | 101 | unsigned inflate_codes_nn; /* length and index for copy */ |
| 102 | unsigned inflate_codes_dd; |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 103 | |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 104 | smallint resume_copy; |
| 105 | |
| 106 | /* private data of inflate_get_next_window() */ |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 107 | smallint method; /* method == -1 for stored, -2 for codes */ |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 108 | 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 Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 116 | |
| 117 | const char *error_msg; |
| 118 | jmp_buf error_jmp; |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 119 | } 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 Vlasenko | e8ef7ec | 2008-02-04 12:12:48 +0000 | [diff] [blame] | 128 | #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 Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 132 | #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 Vlasenko | 7573541 | 2007-01-10 20:50:04 +0000 | [diff] [blame] | 144 | #define inflate_codes_nn (S()inflate_codes_nn ) |
| 145 | #define inflate_codes_dd (S()inflate_codes_dd ) |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 146 | #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 Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 154 | #define error_msg (S()error_msg ) |
| 155 | #define error_jmp (S()error_jmp ) |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 156 | |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 157 | /* This is a generic part */ |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 158 | #if STATE_IN_BSS /* Use global data segment */ |
| 159 | #define DECLARE_STATE /*nothing*/ |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 160 | #define ALLOC_STATE /*nothing*/ |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 161 | #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 |
| 167 | static state_t state; |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 168 | #endif |
| 169 | |
| 170 | #if STATE_IN_MALLOC /* Use malloc space */ |
| 171 | #define DECLARE_STATE state_t *state |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 172 | #define ALLOC_STATE (state = xzalloc(sizeof(*state))) |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 173 | #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 Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 179 | #endif |
| 180 | |
Glenn L McGrath | cc61692 | 2003-02-09 04:46:34 +0000 | [diff] [blame] | 181 | |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 182 | static const uint16_t mask_bits[] ALIGN2 = { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 183 | 0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff, |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 184 | 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff |
| 185 | }; |
| 186 | |
Denys Vlasenko | 6572ef6 | 2019-10-19 18:52:41 +0200 | [diff] [blame^] | 187 | /* Put lengths/offsets and extra bits in a struct of arrays |
| 188 | * to make calls to huft_build() have one fewer parameter. |
| 189 | */ |
| 190 | struct cp_ext { |
| 191 | uint16_t cp[31]; |
| 192 | uint8_t ext[31]; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 193 | }; |
Denys Vlasenko | 6572ef6 | 2019-10-19 18:52:41 +0200 | [diff] [blame^] | 194 | /* Copy lengths and extra bits for literal codes 257..285 */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 195 | /* note: see note #13 above about the 258 in this list. */ |
Denys Vlasenko | 6572ef6 | 2019-10-19 18:52:41 +0200 | [diff] [blame^] | 196 | static const struct cp_ext lit = { |
| 197 | /*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 */ |
| 198 | /*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 */ |
| 199 | { 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 }, |
| 200 | { 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 McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 201 | }; |
Denys Vlasenko | 6572ef6 | 2019-10-19 18:52:41 +0200 | [diff] [blame^] | 202 | /* Copy offsets and extra bits for distance codes 0..29 */ |
| 203 | static const struct cp_ext dist = { |
| 204 | /*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 */ |
| 205 | { 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 }, |
| 206 | { 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 McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | /* Tables for deflate from PKZIP's appnote.txt. */ |
| 210 | /* Order of the bit length code lengths */ |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 211 | static const uint8_t border[] ALIGN1 = { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 212 | 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 |
| 213 | }; |
| 214 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 215 | |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 216 | /* |
| 217 | * Free the malloc'ed tables built by huft_build(), which makes a linked |
| 218 | * list of the tables it made, with the links in a dummy first entry of |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 219 | * each table. |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 220 | * t: table to free |
| 221 | */ |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 222 | static void huft_free(huft_t *p) |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 223 | { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 224 | huft_t *q; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 225 | |
| 226 | /* Go through linked list, freeing from the malloced (t[-1]) address. */ |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 227 | while (p) { |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 228 | q = (--p)->v.t; |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 229 | free(p); |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 230 | p = q; |
| 231 | } |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 234 | static void huft_free_all(STATE_PARAM_ONLY) |
| 235 | { |
| 236 | huft_free(inflate_codes_tl); |
| 237 | huft_free(inflate_codes_td); |
| 238 | inflate_codes_tl = NULL; |
| 239 | inflate_codes_td = NULL; |
| 240 | } |
| 241 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 242 | static void abort_unzip(STATE_PARAM_ONLY) NORETURN; |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 243 | static void abort_unzip(STATE_PARAM_ONLY) |
| 244 | { |
| 245 | huft_free_all(PASS_STATE_ONLY); |
| 246 | longjmp(error_jmp, 1); |
| 247 | } |
| 248 | |
| 249 | static unsigned fill_bitbuffer(STATE_PARAM unsigned bitbuffer, unsigned *current, const unsigned required) |
| 250 | { |
| 251 | while (*current < required) { |
| 252 | if (bytebuffer_offset >= bytebuffer_size) { |
Denis Vlasenko | e8ef7ec | 2008-02-04 12:12:48 +0000 | [diff] [blame] | 253 | unsigned sz = bytebuffer_max - 4; |
| 254 | if (to_read >= 0 && to_read < sz) /* unzip only */ |
| 255 | sz = to_read; |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 256 | /* Leave the first 4 bytes empty so we can always unwind the bitbuffer |
| 257 | * to the front of the bytebuffer */ |
Denis Vlasenko | e8ef7ec | 2008-02-04 12:12:48 +0000 | [diff] [blame] | 258 | bytebuffer_size = safe_read(gunzip_src_fd, &bytebuffer[4], sz); |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 259 | if ((int)bytebuffer_size < 1) { |
| 260 | error_msg = "unexpected end of file"; |
| 261 | abort_unzip(PASS_STATE_ONLY); |
| 262 | } |
Denis Vlasenko | e8ef7ec | 2008-02-04 12:12:48 +0000 | [diff] [blame] | 263 | if (to_read >= 0) /* unzip only */ |
| 264 | to_read -= bytebuffer_size; |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 265 | bytebuffer_size += 4; |
| 266 | bytebuffer_offset = 4; |
| 267 | } |
| 268 | bitbuffer |= ((unsigned) bytebuffer[bytebuffer_offset]) << *current; |
| 269 | bytebuffer_offset++; |
| 270 | *current += 8; |
| 271 | } |
| 272 | return bitbuffer; |
| 273 | } |
| 274 | |
| 275 | |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 276 | /* Given a list of code lengths and a maximum table size, make a set of |
Denys Vlasenko | c763392 | 2019-10-19 18:33:49 +0200 | [diff] [blame] | 277 | * tables to decode that set of codes. |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 278 | * |
| 279 | * b: code lengths in bits (all assumed <= BMAX) |
| 280 | * n: number of codes (assumed <= N_MAX) |
| 281 | * s: number of simple-valued codes (0..s-1) |
| 282 | * d: list of base values for non-simple codes |
| 283 | * e: list of extra bits for non-simple codes |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 284 | * m: maximum lookup bits, returns actual |
Denys Vlasenko | c763392 | 2019-10-19 18:33:49 +0200 | [diff] [blame] | 285 | * result: starting table |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 286 | */ |
Denys Vlasenko | c763392 | 2019-10-19 18:33:49 +0200 | [diff] [blame] | 287 | static huft_t* huft_build(const unsigned *b, const unsigned n, |
Denys Vlasenko | 6572ef6 | 2019-10-19 18:52:41 +0200 | [diff] [blame^] | 288 | const unsigned s, const struct cp_ext *cp_ext, |
| 289 | unsigned *m) |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 290 | { |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 291 | unsigned a; /* counter for codes of length k */ |
| 292 | unsigned c[BMAX + 1]; /* bit length count table */ |
| 293 | unsigned eob_len; /* length of end-of-block code (value 256) */ |
| 294 | unsigned f; /* i repeats in table every f entries */ |
| 295 | int g; /* maximum code length */ |
| 296 | int htl; /* table level */ |
| 297 | unsigned i; /* counter, current code */ |
| 298 | unsigned j; /* counter */ |
| 299 | int k; /* number of bits in current code */ |
Denys Vlasenko | 1de25a6 | 2015-10-26 19:33:05 +0100 | [diff] [blame] | 300 | const unsigned *p; /* pointer into c[], b[], or v[] */ |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 301 | huft_t *q; /* points to current table */ |
| 302 | huft_t r; /* table entry for structure assignment */ |
| 303 | huft_t *u[BMAX]; /* table stack */ |
Denys Vlasenko | 6bd3fff | 2015-10-30 23:41:53 +0100 | [diff] [blame] | 304 | unsigned v[N_MAX + 1]; /* values in order of bit length. last v[] is never used */ |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 305 | int ws[BMAX + 1]; /* bits decoded stack */ |
| 306 | int w; /* bits decoded */ |
| 307 | unsigned x[BMAX + 1]; /* bit offsets, then code stack */ |
| 308 | unsigned *xp; /* pointer into x */ |
| 309 | int y; /* number of dummy codes added */ |
| 310 | unsigned z; /* number of entries in current table */ |
Denys Vlasenko | c763392 | 2019-10-19 18:33:49 +0200 | [diff] [blame] | 311 | huft_t *result; |
| 312 | huft_t **t; |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 313 | |
| 314 | /* Length of EOB code, if any */ |
| 315 | eob_len = n > 256 ? b[256] : BMAX; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 316 | |
| 317 | /* Generate counts for each bit length */ |
Denis Vlasenko | 447b543 | 2007-01-05 19:49:02 +0000 | [diff] [blame] | 318 | memset(c, 0, sizeof(c)); |
Denys Vlasenko | 1de25a6 | 2015-10-26 19:33:05 +0100 | [diff] [blame] | 319 | p = b; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 320 | i = n; |
| 321 | do { |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 322 | c[*p]++; /* assume all entries <= BMAX */ |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 323 | p++; /* can't combine with above line (Solaris bug) */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 324 | } while (--i); |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 325 | if (c[0] == n) { /* null input - all zero length codes */ |
Denys Vlasenko | 2598915 | 2018-02-04 00:15:29 +0100 | [diff] [blame] | 326 | q = xzalloc(3 * sizeof(*q)); |
| 327 | //q[0].v.t = NULL; |
| 328 | q[1].e = 99; /* invalid code marker */ |
| 329 | q[1].b = 1; |
| 330 | q[2].e = 99; /* invalid code marker */ |
| 331 | q[2].b = 1; |
Denys Vlasenko | 2598915 | 2018-02-04 00:15:29 +0100 | [diff] [blame] | 332 | *m = 1; |
Denys Vlasenko | c763392 | 2019-10-19 18:33:49 +0200 | [diff] [blame] | 333 | return q + 1; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | /* Find minimum and maximum length, bound *m by those */ |
Denys Vlasenko | 9b2a9f0 | 2013-11-29 16:43:33 +0100 | [diff] [blame] | 337 | for (j = 1; (j <= BMAX) && (c[j] == 0); j++) |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 338 | continue; |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 339 | k = j; /* minimum code length */ |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 340 | for (i = BMAX; (c[i] == 0) && i; i--) |
| 341 | continue; |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 342 | g = i; /* maximum code length */ |
| 343 | *m = (*m < j) ? j : ((*m > i) ? i : *m); |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 344 | |
| 345 | /* Adjust last length count to fill out codes, if needed */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 346 | for (y = 1 << j; j < i; j++, y <<= 1) { |
Denis Vlasenko | 447b543 | 2007-01-05 19:49:02 +0000 | [diff] [blame] | 347 | y -= c[j]; |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 348 | if (y < 0) |
Denys Vlasenko | c763392 | 2019-10-19 18:33:49 +0200 | [diff] [blame] | 349 | return NULL; /* bad input: more codes than bits */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 350 | } |
Denis Vlasenko | 447b543 | 2007-01-05 19:49:02 +0000 | [diff] [blame] | 351 | y -= c[i]; |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 352 | if (y < 0) |
Denys Vlasenko | c763392 | 2019-10-19 18:33:49 +0200 | [diff] [blame] | 353 | return NULL; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 354 | c[i] += y; |
| 355 | |
| 356 | /* Generate starting offsets into the value table for each length */ |
| 357 | x[1] = j = 0; |
| 358 | p = c + 1; |
| 359 | xp = x + 2; |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 360 | while (--i) { /* note that i == g from above */ |
Denis Vlasenko | 447b543 | 2007-01-05 19:49:02 +0000 | [diff] [blame] | 361 | j += *p++; |
| 362 | *xp++ = j; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Denys Vlasenko | 6bd3fff | 2015-10-30 23:41:53 +0100 | [diff] [blame] | 365 | /* Make a table of values in order of bit lengths. |
| 366 | * To detect bad input, unused v[i]'s are set to invalid value UINT_MAX. |
| 367 | * In particular, last v[i] is never filled and must not be accessed. |
| 368 | */ |
| 369 | memset(v, 0xff, sizeof(v)); |
Denys Vlasenko | 1de25a6 | 2015-10-26 19:33:05 +0100 | [diff] [blame] | 370 | p = b; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 371 | i = 0; |
| 372 | do { |
Denis Vlasenko | 447b543 | 2007-01-05 19:49:02 +0000 | [diff] [blame] | 373 | j = *p++; |
| 374 | if (j != 0) { |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 375 | v[x[j]++] = i; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 376 | } |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 377 | } while (++i < n); |
| 378 | |
| 379 | /* Generate the Huffman codes and for each, make the table entries */ |
Denys Vlasenko | c763392 | 2019-10-19 18:33:49 +0200 | [diff] [blame] | 380 | result = NULL; |
| 381 | t = &result; |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 382 | x[0] = i = 0; /* first Huffman code is zero */ |
| 383 | p = v; /* grab values in bit order */ |
| 384 | htl = -1; /* no tables yet--level -1 */ |
| 385 | w = ws[0] = 0; /* bits decoded */ |
| 386 | u[0] = NULL; /* just to keep compilers happy */ |
| 387 | q = NULL; /* ditto */ |
| 388 | z = 0; /* ditto */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 389 | |
| 390 | /* go through the bit lengths (k already is bits in shortest code) */ |
| 391 | for (; k <= g; k++) { |
| 392 | a = c[k]; |
| 393 | while (a--) { |
| 394 | /* here i is the Huffman code of length k bits for value *p */ |
| 395 | /* make tables up to required level */ |
Rob Landley | 11c7a7b | 2006-06-25 22:39:24 +0000 | [diff] [blame] | 396 | while (k > ws[htl + 1]) { |
| 397 | w = ws[++htl]; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 398 | |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 399 | /* compute minimum size table less than or equal to *m bits */ |
Denis Vlasenko | 447b543 | 2007-01-05 19:49:02 +0000 | [diff] [blame] | 400 | z = g - w; |
| 401 | z = z > *m ? *m : z; /* upper limit on table size */ |
| 402 | j = k - w; |
| 403 | f = 1 << j; |
| 404 | if (f > a + 1) { /* try a k-w bit table */ |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 405 | /* too few codes for k-w bit table */ |
| 406 | f -= a + 1; /* deduct codes from patterns left */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 407 | xp = c + k; |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 408 | while (++j < z) { /* try smaller tables up to z bits */ |
Denis Vlasenko | 447b543 | 2007-01-05 19:49:02 +0000 | [diff] [blame] | 409 | f <<= 1; |
| 410 | if (f <= *++xp) { |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 411 | break; /* enough codes to use up j bits */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 412 | } |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 413 | f -= *xp; /* else deduct codes from patterns */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 414 | } |
| 415 | } |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 416 | j = (w + j > eob_len && w < eob_len) ? eob_len - w : j; /* make EOB code end at table */ |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 417 | z = 1 << j; /* table entries for j-bit table */ |
Rob Landley | 11c7a7b | 2006-06-25 22:39:24 +0000 | [diff] [blame] | 418 | ws[htl+1] = w + j; /* set bits decoded in stack */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 419 | |
| 420 | /* allocate and link in new table */ |
Denis Vlasenko | 4cccc03 | 2006-12-22 18:37:07 +0000 | [diff] [blame] | 421 | q = xzalloc((z + 1) * sizeof(huft_t)); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 422 | *t = q + 1; /* link to list for huft_free() */ |
Rob Landley | 11c7a7b | 2006-06-25 22:39:24 +0000 | [diff] [blame] | 423 | t = &(q->v.t); |
| 424 | u[htl] = ++q; /* table starts after link */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 425 | |
| 426 | /* connect to last table, if there is one */ |
Rob Landley | 11c7a7b | 2006-06-25 22:39:24 +0000 | [diff] [blame] | 427 | if (htl) { |
| 428 | x[htl] = i; /* save pattern for backing up */ |
| 429 | r.b = (unsigned char) (w - ws[htl - 1]); /* bits to dump before this table */ |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 430 | r.e = (unsigned char) (16 + j); /* bits in this table */ |
| 431 | r.v.t = q; /* pointer to this table */ |
Rob Landley | 11c7a7b | 2006-06-25 22:39:24 +0000 | [diff] [blame] | 432 | j = (i & ((1 << w) - 1)) >> ws[htl - 1]; |
| 433 | u[htl - 1][j] = r; /* connect to last table */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | |
| 437 | /* set up table entry in r */ |
| 438 | r.b = (unsigned char) (k - w); |
Denys Vlasenko | 6bd3fff | 2015-10-30 23:41:53 +0100 | [diff] [blame] | 439 | if (/*p >= v + n || -- redundant, caught by the second check: */ |
| 440 | *p == UINT_MAX /* do we access uninited v[i]? (see memset(v))*/ |
| 441 | ) { |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 442 | r.e = 99; /* out of values--invalid code */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 443 | } else if (*p < s) { |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 444 | r.e = (unsigned char) (*p < 256 ? 16 : 15); /* 256 is EOB code */ |
| 445 | r.v.n = (unsigned short) (*p++); /* simple code is just the value */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 446 | } else { |
Denys Vlasenko | 6572ef6 | 2019-10-19 18:52:41 +0200 | [diff] [blame^] | 447 | r.e = (unsigned char) cp_ext->ext[*p - s]; /* non-simple--look up in lists */ |
| 448 | r.v.n = cp_ext->cp[*p++ - s]; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | /* fill code-like entries with r */ |
| 452 | f = 1 << (k - w); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 453 | for (j = i >> w; j < z; j += f) { |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 454 | q[j] = r; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 455 | } |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 456 | |
| 457 | /* backwards increment the k-bit code i */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 458 | for (j = 1 << (k - 1); i & j; j >>= 1) { |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 459 | i ^= j; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 460 | } |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 461 | i ^= j; |
| 462 | |
| 463 | /* backup over finished tables */ |
Rob Landley | 11c7a7b | 2006-06-25 22:39:24 +0000 | [diff] [blame] | 464 | while ((i & ((1 << w) - 1)) != x[htl]) { |
| 465 | w = ws[--htl]; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | } |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 469 | |
| 470 | /* return actual size of base table */ |
| 471 | *m = ws[1]; |
| 472 | |
Denys Vlasenko | c763392 | 2019-10-19 18:33:49 +0200 | [diff] [blame] | 473 | if (y != 0 && g != 1) /* we were given an incomplete table */ |
| 474 | return NULL; |
| 475 | |
| 476 | return result; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 479 | |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 480 | /* |
| 481 | * inflate (decompress) the codes in a deflated (compressed) block. |
| 482 | * Return an error code or zero if it all goes ok. |
| 483 | * |
| 484 | * tl, td: literal/length and distance decoder tables |
| 485 | * bl, bd: number of bits decoded by tl[] and td[] |
| 486 | */ |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 487 | /* called once from inflate_block */ |
Denis Vlasenko | 07766bb | 2007-03-14 00:06:51 +0000 | [diff] [blame] | 488 | |
| 489 | /* map formerly local static variables to globals */ |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 490 | #define ml inflate_codes_ml |
| 491 | #define md inflate_codes_md |
| 492 | #define bb inflate_codes_bb |
| 493 | #define k inflate_codes_k |
| 494 | #define w inflate_codes_w |
| 495 | #define tl inflate_codes_tl |
| 496 | #define td inflate_codes_td |
| 497 | #define bl inflate_codes_bl |
| 498 | #define bd inflate_codes_bd |
Denis Vlasenko | 7573541 | 2007-01-10 20:50:04 +0000 | [diff] [blame] | 499 | #define nn inflate_codes_nn |
| 500 | #define dd inflate_codes_dd |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 501 | static void inflate_codes_setup(STATE_PARAM unsigned my_bl, unsigned my_bd) |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 502 | { |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 503 | bl = my_bl; |
| 504 | bd = my_bd; |
| 505 | /* make local copies of globals */ |
| 506 | bb = gunzip_bb; /* initialize bit buffer */ |
| 507 | k = gunzip_bk; |
| 508 | w = gunzip_outbuf_count; /* initialize gunzip_window position */ |
| 509 | /* inflate the coded data */ |
| 510 | ml = mask_bits[bl]; /* precompute masks for speed */ |
| 511 | md = mask_bits[bd]; |
| 512 | } |
| 513 | /* called once from inflate_get_next_window */ |
Denys Vlasenko | adf922e | 2009-10-08 14:35:37 +0200 | [diff] [blame] | 514 | static NOINLINE int inflate_codes(STATE_PARAM_ONLY) |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 515 | { |
| 516 | unsigned e; /* table entry flag/number of extra bits */ |
| 517 | huft_t *t; /* pointer to table entry */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 518 | |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 519 | if (resume_copy) |
| 520 | goto do_copy; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 521 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 522 | while (1) { /* do until end of block */ |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 523 | bb = fill_bitbuffer(PASS_STATE bb, &k, bl); |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 524 | t = tl + ((unsigned) bb & ml); |
Denis Vlasenko | 447b543 | 2007-01-05 19:49:02 +0000 | [diff] [blame] | 525 | e = t->e; |
| 526 | if (e > 16) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 527 | do { |
Denys Vlasenko | 6bd3fff | 2015-10-30 23:41:53 +0100 | [diff] [blame] | 528 | if (e == 99) { |
| 529 | abort_unzip(PASS_STATE_ONLY); |
| 530 | } |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 531 | bb >>= t->b; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 532 | k -= t->b; |
| 533 | e -= 16; |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 534 | bb = fill_bitbuffer(PASS_STATE bb, &k, e); |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 535 | t = t->v.t + ((unsigned) bb & mask_bits[e]); |
Denis Vlasenko | 447b543 | 2007-01-05 19:49:02 +0000 | [diff] [blame] | 536 | e = t->e; |
| 537 | } while (e > 16); |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 538 | bb >>= t->b; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 539 | k -= t->b; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 540 | if (e == 16) { /* then it's a literal */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 541 | gunzip_window[w++] = (unsigned char) t->v.n; |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 542 | if (w == GUNZIP_WSIZE) { |
| 543 | gunzip_outbuf_count = w; |
Glenn L McGrath | 0126fda | 2002-11-20 06:46:46 +0000 | [diff] [blame] | 544 | //flush_gunzip_window(); |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 545 | w = 0; |
Glenn L McGrath | 83bf47c | 2002-11-20 22:00:31 +0000 | [diff] [blame] | 546 | return 1; // We have a block to read |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 547 | } |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 548 | } else { /* it's an EOB or a length */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 549 | /* exit if end of block */ |
| 550 | if (e == 15) { |
| 551 | break; |
| 552 | } |
| 553 | |
| 554 | /* get length of block to copy */ |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 555 | bb = fill_bitbuffer(PASS_STATE bb, &k, e); |
Denis Vlasenko | 7573541 | 2007-01-10 20:50:04 +0000 | [diff] [blame] | 556 | nn = t->v.n + ((unsigned) bb & mask_bits[e]); |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 557 | bb >>= e; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 558 | k -= e; |
| 559 | |
| 560 | /* decode distance of block to copy */ |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 561 | bb = fill_bitbuffer(PASS_STATE bb, &k, bd); |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 562 | t = td + ((unsigned) bb & md); |
Denis Vlasenko | 447b543 | 2007-01-05 19:49:02 +0000 | [diff] [blame] | 563 | e = t->e; |
| 564 | if (e > 16) |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 565 | do { |
Denys Vlasenko | 6bd3fff | 2015-10-30 23:41:53 +0100 | [diff] [blame] | 566 | if (e == 99) { |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 567 | abort_unzip(PASS_STATE_ONLY); |
Denys Vlasenko | 6bd3fff | 2015-10-30 23:41:53 +0100 | [diff] [blame] | 568 | } |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 569 | bb >>= t->b; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 570 | k -= t->b; |
| 571 | e -= 16; |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 572 | bb = fill_bitbuffer(PASS_STATE bb, &k, e); |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 573 | t = t->v.t + ((unsigned) bb & mask_bits[e]); |
Denis Vlasenko | 447b543 | 2007-01-05 19:49:02 +0000 | [diff] [blame] | 574 | e = t->e; |
| 575 | } while (e > 16); |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 576 | bb >>= t->b; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 577 | k -= t->b; |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 578 | bb = fill_bitbuffer(PASS_STATE bb, &k, e); |
Denis Vlasenko | 7573541 | 2007-01-10 20:50:04 +0000 | [diff] [blame] | 579 | dd = w - t->v.n - ((unsigned) bb & mask_bits[e]); |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 580 | bb >>= e; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 581 | k -= e; |
| 582 | |
| 583 | /* do the copy */ |
Denis Vlasenko | 447b543 | 2007-01-05 19:49:02 +0000 | [diff] [blame] | 584 | do_copy: |
| 585 | do { |
Denis Vlasenko | 7573541 | 2007-01-10 20:50:04 +0000 | [diff] [blame] | 586 | /* Was: nn -= (e = (e = GUNZIP_WSIZE - ((dd &= GUNZIP_WSIZE - 1) > w ? dd : w)) > nn ? nn : e); */ |
Denis Vlasenko | 447b543 | 2007-01-05 19:49:02 +0000 | [diff] [blame] | 587 | /* Who wrote THAT?? rewritten as: */ |
Joakim Tjernlund | 0866b36 | 2010-02-08 18:55:15 +0100 | [diff] [blame] | 588 | unsigned delta; |
| 589 | |
Denis Vlasenko | 7573541 | 2007-01-10 20:50:04 +0000 | [diff] [blame] | 590 | dd &= GUNZIP_WSIZE - 1; |
| 591 | e = GUNZIP_WSIZE - (dd > w ? dd : w); |
Joakim Tjernlund | 0866b36 | 2010-02-08 18:55:15 +0100 | [diff] [blame] | 592 | delta = w > dd ? w - dd : dd - w; |
Denis Vlasenko | 7573541 | 2007-01-10 20:50:04 +0000 | [diff] [blame] | 593 | if (e > nn) e = nn; |
| 594 | nn -= e; |
Denis Vlasenko | 447b543 | 2007-01-05 19:49:02 +0000 | [diff] [blame] | 595 | |
| 596 | /* copy to new buffer to prevent possible overwrite */ |
Joakim Tjernlund | 0866b36 | 2010-02-08 18:55:15 +0100 | [diff] [blame] | 597 | if (delta >= e) { |
Denis Vlasenko | 7573541 | 2007-01-10 20:50:04 +0000 | [diff] [blame] | 598 | memcpy(gunzip_window + w, gunzip_window + dd, e); |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 599 | w += e; |
Denis Vlasenko | 7573541 | 2007-01-10 20:50:04 +0000 | [diff] [blame] | 600 | dd += e; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 601 | } else { |
Denis Vlasenko | 447b543 | 2007-01-05 19:49:02 +0000 | [diff] [blame] | 602 | /* do it slow to avoid memcpy() overlap */ |
| 603 | /* !NOMEMCPY */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 604 | do { |
Denis Vlasenko | 7573541 | 2007-01-10 20:50:04 +0000 | [diff] [blame] | 605 | gunzip_window[w++] = gunzip_window[dd++]; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 606 | } while (--e); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 607 | } |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 608 | if (w == GUNZIP_WSIZE) { |
| 609 | gunzip_outbuf_count = w; |
Denis Vlasenko | 7573541 | 2007-01-10 20:50:04 +0000 | [diff] [blame] | 610 | resume_copy = (nn != 0); |
Glenn L McGrath | 0126fda | 2002-11-20 06:46:46 +0000 | [diff] [blame] | 611 | //flush_gunzip_window(); |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 612 | w = 0; |
Glenn L McGrath | 83bf47c | 2002-11-20 22:00:31 +0000 | [diff] [blame] | 613 | return 1; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 614 | } |
Denis Vlasenko | 7573541 | 2007-01-10 20:50:04 +0000 | [diff] [blame] | 615 | } while (nn); |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 616 | resume_copy = 0; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 617 | } |
| 618 | } |
| 619 | |
| 620 | /* restore the globals from the locals */ |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 621 | gunzip_outbuf_count = w; /* restore global gunzip_window pointer */ |
| 622 | gunzip_bb = bb; /* restore global bit buffer */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 623 | gunzip_bk = k; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 624 | |
Glenn L McGrath | 0126fda | 2002-11-20 06:46:46 +0000 | [diff] [blame] | 625 | /* normally just after call to inflate_codes, but save code by putting it here */ |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 626 | /* free the decoding tables (tl and td), return */ |
| 627 | huft_free_all(PASS_STATE_ONLY); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 628 | |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 629 | /* done */ |
| 630 | return 0; |
| 631 | } |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 632 | #undef ml |
| 633 | #undef md |
| 634 | #undef bb |
| 635 | #undef k |
| 636 | #undef w |
| 637 | #undef tl |
| 638 | #undef td |
| 639 | #undef bl |
| 640 | #undef bd |
Denis Vlasenko | 1a8bf7f | 2007-01-10 20:57:03 +0000 | [diff] [blame] | 641 | #undef nn |
| 642 | #undef dd |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 643 | |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 644 | |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 645 | /* called once from inflate_block */ |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 646 | static void inflate_stored_setup(STATE_PARAM int my_n, int my_b, int my_k) |
Glenn L McGrath | 0126fda | 2002-11-20 06:46:46 +0000 | [diff] [blame] | 647 | { |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 648 | inflate_stored_n = my_n; |
| 649 | inflate_stored_b = my_b; |
| 650 | inflate_stored_k = my_k; |
| 651 | /* initialize gunzip_window position */ |
| 652 | inflate_stored_w = gunzip_outbuf_count; |
| 653 | } |
| 654 | /* called once from inflate_get_next_window */ |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 655 | static int inflate_stored(STATE_PARAM_ONLY) |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 656 | { |
Glenn L McGrath | 0126fda | 2002-11-20 06:46:46 +0000 | [diff] [blame] | 657 | /* read and output the compressed data */ |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 658 | while (inflate_stored_n--) { |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 659 | inflate_stored_b = fill_bitbuffer(PASS_STATE inflate_stored_b, &inflate_stored_k, 8); |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 660 | gunzip_window[inflate_stored_w++] = (unsigned char) inflate_stored_b; |
| 661 | if (inflate_stored_w == GUNZIP_WSIZE) { |
| 662 | gunzip_outbuf_count = inflate_stored_w; |
Glenn L McGrath | 0126fda | 2002-11-20 06:46:46 +0000 | [diff] [blame] | 663 | //flush_gunzip_window(); |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 664 | inflate_stored_w = 0; |
| 665 | inflate_stored_b >>= 8; |
| 666 | inflate_stored_k -= 8; |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 667 | return 1; /* We have a block */ |
Glenn L McGrath | 0126fda | 2002-11-20 06:46:46 +0000 | [diff] [blame] | 668 | } |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 669 | inflate_stored_b >>= 8; |
| 670 | inflate_stored_k -= 8; |
Glenn L McGrath | 0126fda | 2002-11-20 06:46:46 +0000 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | /* restore the globals from the locals */ |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 674 | gunzip_outbuf_count = inflate_stored_w; /* restore global gunzip_window pointer */ |
| 675 | gunzip_bb = inflate_stored_b; /* restore global bit buffer */ |
| 676 | gunzip_bk = inflate_stored_k; |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 677 | return 0; /* Finished */ |
Glenn L McGrath | 0126fda | 2002-11-20 06:46:46 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 680 | |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 681 | /* |
| 682 | * decompress an inflated block |
| 683 | * e: last block flag |
| 684 | * |
| 685 | * GLOBAL VARIABLES: bb, kk, |
| 686 | */ |
Denis Vlasenko | 447b543 | 2007-01-05 19:49:02 +0000 | [diff] [blame] | 687 | /* Return values: -1 = inflate_stored, -2 = inflate_codes */ |
| 688 | /* One callsite in inflate_get_next_window */ |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 689 | static int inflate_block(STATE_PARAM smallint *e) |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 690 | { |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 691 | unsigned ll[286 + 30]; /* literal/length and distance code lengths */ |
| 692 | unsigned t; /* block type */ |
| 693 | unsigned b; /* bit buffer */ |
| 694 | unsigned k; /* number of bits in bit buffer */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 695 | |
| 696 | /* make local bit buffer */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 697 | |
| 698 | b = gunzip_bb; |
| 699 | k = gunzip_bk; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 700 | |
| 701 | /* read in last block bit */ |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 702 | b = fill_bitbuffer(PASS_STATE b, &k, 1); |
| 703 | *e = b & 1; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 704 | b >>= 1; |
| 705 | k -= 1; |
| 706 | |
| 707 | /* read in block type */ |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 708 | b = fill_bitbuffer(PASS_STATE b, &k, 2); |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 709 | t = (unsigned) b & 3; |
| 710 | b >>= 2; |
| 711 | k -= 2; |
| 712 | |
| 713 | /* restore the global bit buffer */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 714 | gunzip_bb = b; |
| 715 | gunzip_bk = k; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 716 | |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 717 | /* Do we see block type 1 often? Yes! |
| 718 | * TODO: fix performance problem (see below) */ |
| 719 | //bb_error_msg("blktype %d", t); |
| 720 | |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 721 | /* inflate that block type */ |
| 722 | switch (t) { |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 723 | case 0: /* Inflate stored */ |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 724 | { |
Denis Vlasenko | cc33ef1 | 2007-01-05 19:46:04 +0000 | [diff] [blame] | 725 | unsigned n; /* number of bytes in block */ |
| 726 | unsigned b_stored; /* bit buffer */ |
| 727 | unsigned k_stored; /* number of bits in bit buffer */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 728 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 729 | /* make local copies of globals */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 730 | b_stored = gunzip_bb; /* initialize bit buffer */ |
| 731 | k_stored = gunzip_bk; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 732 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 733 | /* go to byte boundary */ |
| 734 | n = k_stored & 7; |
| 735 | b_stored >>= n; |
| 736 | k_stored -= n; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 737 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 738 | /* get the length and its complement */ |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 739 | b_stored = fill_bitbuffer(PASS_STATE b_stored, &k_stored, 16); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 740 | n = ((unsigned) b_stored & 0xffff); |
| 741 | b_stored >>= 16; |
| 742 | k_stored -= 16; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 743 | |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 744 | b_stored = fill_bitbuffer(PASS_STATE b_stored, &k_stored, 16); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 745 | if (n != (unsigned) ((~b_stored) & 0xffff)) { |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 746 | abort_unzip(PASS_STATE_ONLY); /* error in compressed data */ |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 747 | } |
| 748 | b_stored >>= 16; |
| 749 | k_stored -= 16; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 750 | |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 751 | inflate_stored_setup(PASS_STATE n, b_stored, k_stored); |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 752 | |
Glenn L McGrath | 0126fda | 2002-11-20 06:46:46 +0000 | [diff] [blame] | 753 | return -1; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 754 | } |
Denis Vlasenko | cd42cb8 | 2007-01-05 23:56:53 +0000 | [diff] [blame] | 755 | case 1: |
| 756 | /* Inflate fixed |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 757 | * decompress an inflated type 1 (fixed Huffman codes) block. We should |
Denis Vlasenko | cd42cb8 | 2007-01-05 23:56:53 +0000 | [diff] [blame] | 758 | * either replace this with a custom decoder, or at least precompute the |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 759 | * Huffman tables. TODO */ |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 760 | { |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 761 | int i; /* temporary variable */ |
| 762 | unsigned bl; /* lookup bits for tl */ |
| 763 | unsigned bd; /* lookup bits for td */ |
| 764 | /* gcc 4.2.1 is too dumb to reuse stackspace. Moved up... */ |
| 765 | //unsigned ll[288]; /* length list for huft_build */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 766 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 767 | /* set up literal table */ |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 768 | for (i = 0; i < 144; i++) |
| 769 | ll[i] = 8; |
| 770 | for (; i < 256; i++) |
| 771 | ll[i] = 9; |
| 772 | for (; i < 280; i++) |
| 773 | ll[i] = 7; |
| 774 | for (; i < 288; i++) /* make a complete, but wrong code set */ |
| 775 | ll[i] = 8; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 776 | bl = 7; |
Denys Vlasenko | 6572ef6 | 2019-10-19 18:52:41 +0200 | [diff] [blame^] | 777 | inflate_codes_tl = huft_build(ll, 288, 257, &lit, &bl); |
Denys Vlasenko | c763392 | 2019-10-19 18:33:49 +0200 | [diff] [blame] | 778 | /* huft_build() never returns error here - we use known data */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 779 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 780 | /* set up distance table */ |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 781 | for (i = 0; i < 30; i++) /* make an incomplete code set */ |
| 782 | ll[i] = 5; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 783 | bd = 5; |
Denys Vlasenko | 6572ef6 | 2019-10-19 18:52:41 +0200 | [diff] [blame^] | 784 | inflate_codes_td = huft_build(ll, 30, 0, &dist, &bd); |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 785 | |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 786 | /* set up data for inflate_codes() */ |
| 787 | inflate_codes_setup(PASS_STATE bl, bd); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 788 | |
Glenn L McGrath | 0126fda | 2002-11-20 06:46:46 +0000 | [diff] [blame] | 789 | /* huft_free code moved into inflate_codes */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 790 | |
Glenn L McGrath | 0126fda | 2002-11-20 06:46:46 +0000 | [diff] [blame] | 791 | return -2; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 792 | } |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 793 | case 2: /* Inflate dynamic */ |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 794 | { |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 795 | enum { dbits = 6 }; /* bits in base distance lookup table */ |
| 796 | enum { lbits = 9 }; /* bits in base literal/length lookup table */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 797 | |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 798 | huft_t *td; /* distance code table */ |
| 799 | unsigned i; /* temporary variables */ |
Denis Vlasenko | cc33ef1 | 2007-01-05 19:46:04 +0000 | [diff] [blame] | 800 | unsigned j; |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 801 | unsigned l; /* last length */ |
| 802 | unsigned m; /* mask for bit lengths table */ |
| 803 | unsigned n; /* number of lengths to get */ |
| 804 | unsigned bl; /* lookup bits for tl */ |
| 805 | unsigned bd; /* lookup bits for td */ |
| 806 | unsigned nb; /* number of bit length codes */ |
| 807 | unsigned nl; /* number of literal/length codes */ |
| 808 | unsigned nd; /* number of distance codes */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 809 | |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 810 | //unsigned ll[286 + 30];/* literal/length and distance code lengths */ |
| 811 | unsigned b_dynamic; /* bit buffer */ |
| 812 | unsigned k_dynamic; /* number of bits in bit buffer */ |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 813 | |
| 814 | /* make local bit buffer */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 815 | b_dynamic = gunzip_bb; |
| 816 | k_dynamic = gunzip_bk; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 817 | |
| 818 | /* read in table lengths */ |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 819 | b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 5); |
Denis Vlasenko | cc33ef1 | 2007-01-05 19:46:04 +0000 | [diff] [blame] | 820 | nl = 257 + ((unsigned) b_dynamic & 0x1f); /* number of literal/length codes */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 821 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 822 | b_dynamic >>= 5; |
| 823 | k_dynamic -= 5; |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 824 | b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 5); |
Denis Vlasenko | cc33ef1 | 2007-01-05 19:46:04 +0000 | [diff] [blame] | 825 | nd = 1 + ((unsigned) b_dynamic & 0x1f); /* number of distance codes */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 826 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 827 | b_dynamic >>= 5; |
| 828 | k_dynamic -= 5; |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 829 | b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 4); |
Denis Vlasenko | cc33ef1 | 2007-01-05 19:46:04 +0000 | [diff] [blame] | 830 | nb = 4 + ((unsigned) b_dynamic & 0xf); /* number of bit length codes */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 831 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 832 | b_dynamic >>= 4; |
| 833 | k_dynamic -= 4; |
Denys Vlasenko | 6bd3fff | 2015-10-30 23:41:53 +0100 | [diff] [blame] | 834 | if (nl > 286 || nd > 30) { |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 835 | abort_unzip(PASS_STATE_ONLY); /* bad lengths */ |
Denys Vlasenko | 6bd3fff | 2015-10-30 23:41:53 +0100 | [diff] [blame] | 836 | } |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 837 | |
| 838 | /* read in bit-length-code lengths */ |
| 839 | for (j = 0; j < nb; j++) { |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 840 | b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 3); |
Denis Vlasenko | cc33ef1 | 2007-01-05 19:46:04 +0000 | [diff] [blame] | 841 | ll[border[j]] = (unsigned) b_dynamic & 7; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 842 | b_dynamic >>= 3; |
| 843 | k_dynamic -= 3; |
| 844 | } |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 845 | for (; j < 19; j++) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 846 | ll[border[j]] = 0; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 847 | |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 848 | /* build decoding table for trees - single level, 7 bit lookup */ |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 849 | bl = 7; |
Denys Vlasenko | 6572ef6 | 2019-10-19 18:52:41 +0200 | [diff] [blame^] | 850 | inflate_codes_tl = huft_build(ll, 19, 19, NULL, &bl); |
Denys Vlasenko | c763392 | 2019-10-19 18:33:49 +0200 | [diff] [blame] | 851 | if (!inflate_codes_tl) { |
| 852 | abort_unzip(PASS_STATE_ONLY); /* incomplete code set */ |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | /* read in literal and distance code lengths */ |
| 856 | n = nl + nd; |
| 857 | m = mask_bits[bl]; |
| 858 | i = l = 0; |
Denis Vlasenko | cc33ef1 | 2007-01-05 19:46:04 +0000 | [diff] [blame] | 859 | while ((unsigned) i < n) { |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 860 | b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, (unsigned)bl); |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 861 | td = inflate_codes_tl + ((unsigned) b_dynamic & m); |
| 862 | j = td->b; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 863 | b_dynamic >>= j; |
| 864 | k_dynamic -= j; |
| 865 | j = td->v.n; |
| 866 | if (j < 16) { /* length of code in bits (0..15) */ |
| 867 | ll[i++] = l = j; /* save last length in l */ |
| 868 | } else if (j == 16) { /* repeat last length 3 to 6 times */ |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 869 | b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 2); |
Denis Vlasenko | cc33ef1 | 2007-01-05 19:46:04 +0000 | [diff] [blame] | 870 | j = 3 + ((unsigned) b_dynamic & 3); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 871 | b_dynamic >>= 2; |
| 872 | k_dynamic -= 2; |
Denis Vlasenko | cc33ef1 | 2007-01-05 19:46:04 +0000 | [diff] [blame] | 873 | if ((unsigned) i + j > n) { |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 874 | abort_unzip(PASS_STATE_ONLY); //return 1; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 875 | } |
| 876 | while (j--) { |
| 877 | ll[i++] = l; |
| 878 | } |
| 879 | } else if (j == 17) { /* 3 to 10 zero length codes */ |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 880 | b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 3); |
Denis Vlasenko | cc33ef1 | 2007-01-05 19:46:04 +0000 | [diff] [blame] | 881 | j = 3 + ((unsigned) b_dynamic & 7); |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 882 | b_dynamic >>= 3; |
| 883 | k_dynamic -= 3; |
Denis Vlasenko | cc33ef1 | 2007-01-05 19:46:04 +0000 | [diff] [blame] | 884 | if ((unsigned) i + j > n) { |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 885 | abort_unzip(PASS_STATE_ONLY); //return 1; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 886 | } |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 887 | while (j--) { |
| 888 | ll[i++] = 0; |
| 889 | } |
| 890 | l = 0; |
| 891 | } else { /* j == 18: 11 to 138 zero length codes */ |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 892 | b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 7); |
Denis Vlasenko | cc33ef1 | 2007-01-05 19:46:04 +0000 | [diff] [blame] | 893 | j = 11 + ((unsigned) b_dynamic & 0x7f); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 894 | b_dynamic >>= 7; |
| 895 | k_dynamic -= 7; |
Denis Vlasenko | cc33ef1 | 2007-01-05 19:46:04 +0000 | [diff] [blame] | 896 | if ((unsigned) i + j > n) { |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 897 | abort_unzip(PASS_STATE_ONLY); //return 1; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 898 | } |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 899 | while (j--) { |
| 900 | ll[i++] = 0; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 901 | } |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 902 | l = 0; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 903 | } |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 904 | } |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 905 | |
| 906 | /* free decoding table for trees */ |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 907 | huft_free(inflate_codes_tl); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 908 | |
| 909 | /* restore the global bit buffer */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 910 | gunzip_bb = b_dynamic; |
| 911 | gunzip_bk = k_dynamic; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 912 | |
| 913 | /* build the decoding tables for literal/length and distance codes */ |
| 914 | bl = lbits; |
Denys Vlasenko | 6572ef6 | 2019-10-19 18:52:41 +0200 | [diff] [blame^] | 915 | inflate_codes_tl = huft_build(ll, nl, 257, &lit, &bl); |
Denys Vlasenko | c763392 | 2019-10-19 18:33:49 +0200 | [diff] [blame] | 916 | if (!inflate_codes_tl) { |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 917 | abort_unzip(PASS_STATE_ONLY); |
Denys Vlasenko | 6bd3fff | 2015-10-30 23:41:53 +0100 | [diff] [blame] | 918 | } |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 919 | bd = dbits; |
Denys Vlasenko | 6572ef6 | 2019-10-19 18:52:41 +0200 | [diff] [blame^] | 920 | inflate_codes_td = huft_build(ll + nl, nd, 0, &dist, &bd); |
Denys Vlasenko | c763392 | 2019-10-19 18:33:49 +0200 | [diff] [blame] | 921 | if (!inflate_codes_td) { |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 922 | abort_unzip(PASS_STATE_ONLY); |
Denys Vlasenko | 6bd3fff | 2015-10-30 23:41:53 +0100 | [diff] [blame] | 923 | } |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 924 | |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 925 | /* set up data for inflate_codes() */ |
| 926 | inflate_codes_setup(PASS_STATE bl, bd); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 927 | |
Glenn L McGrath | 0126fda | 2002-11-20 06:46:46 +0000 | [diff] [blame] | 928 | /* huft_free code moved into inflate_codes */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 929 | |
Glenn L McGrath | 0126fda | 2002-11-20 06:46:46 +0000 | [diff] [blame] | 930 | return -2; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 931 | } |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 932 | default: |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 933 | abort_unzip(PASS_STATE_ONLY); |
Glenn L McGrath | 83bf47c | 2002-11-20 22:00:31 +0000 | [diff] [blame] | 934 | } |
| 935 | } |
| 936 | |
Denis Vlasenko | 447b543 | 2007-01-05 19:49:02 +0000 | [diff] [blame] | 937 | /* Two callsites, both in inflate_get_next_window */ |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 938 | static void calculate_gunzip_crc(STATE_PARAM_ONLY) |
Glenn L McGrath | 83bf47c | 2002-11-20 22:00:31 +0000 | [diff] [blame] | 939 | { |
Denys Vlasenko | 9ce642f | 2010-10-27 15:26:45 +0200 | [diff] [blame] | 940 | gunzip_crc = crc32_block_endian0(gunzip_crc, gunzip_window, gunzip_outbuf_count, gunzip_crc_table); |
Glenn L McGrath | 83bf47c | 2002-11-20 22:00:31 +0000 | [diff] [blame] | 941 | gunzip_bytes_out += gunzip_outbuf_count; |
| 942 | } |
| 943 | |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 944 | /* One callsite in inflate_unzip_internal */ |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 945 | static int inflate_get_next_window(STATE_PARAM_ONLY) |
Glenn L McGrath | 83bf47c | 2002-11-20 22:00:31 +0000 | [diff] [blame] | 946 | { |
Glenn L McGrath | 83bf47c | 2002-11-20 22:00:31 +0000 | [diff] [blame] | 947 | gunzip_outbuf_count = 0; |
| 948 | |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 949 | while (1) { |
Glenn L McGrath | 83bf47c | 2002-11-20 22:00:31 +0000 | [diff] [blame] | 950 | int ret; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 951 | |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 952 | if (need_another_block) { |
| 953 | if (end_reached) { |
| 954 | calculate_gunzip_crc(PASS_STATE_ONLY); |
| 955 | end_reached = 0; |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 956 | /* NB: need_another_block is still set */ |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 957 | return 0; /* Last block */ |
| 958 | } |
| 959 | method = inflate_block(PASS_STATE &end_reached); |
| 960 | need_another_block = 0; |
Glenn L McGrath | 83bf47c | 2002-11-20 22:00:31 +0000 | [diff] [blame] | 961 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 962 | |
Glenn L McGrath | 83bf47c | 2002-11-20 22:00:31 +0000 | [diff] [blame] | 963 | switch (method) { |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 964 | case -1: |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 965 | ret = inflate_stored(PASS_STATE_ONLY); |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 966 | break; |
| 967 | case -2: |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 968 | ret = inflate_codes(PASS_STATE_ONLY); |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 969 | break; |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 970 | default: /* cannot happen */ |
| 971 | abort_unzip(PASS_STATE_ONLY); |
Glenn L McGrath | 83bf47c | 2002-11-20 22:00:31 +0000 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | if (ret == 1) { |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 975 | calculate_gunzip_crc(PASS_STATE_ONLY); |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 976 | return 1; /* more data left */ |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 977 | } |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 978 | need_another_block = 1; /* end of that block */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 979 | } |
Glenn L McGrath | eda4f53 | 2002-11-24 06:01:20 +0000 | [diff] [blame] | 980 | /* Doesnt get here */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 981 | } |
| 982 | |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 983 | |
Denis Vlasenko | c14d39e | 2007-06-08 13:05:39 +0000 | [diff] [blame] | 984 | /* Called from unpack_gz_stream() and inflate_unzip() */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 985 | static IF_DESKTOP(long long) int |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 986 | inflate_unzip_internal(STATE_PARAM transformer_state_t *xstate) |
Glenn L McGrath | fd73b8c | 2002-11-17 21:33:30 +0000 | [diff] [blame] | 987 | { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 988 | IF_DESKTOP(long long) int n = 0; |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 989 | ssize_t nwrote; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 990 | |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 991 | /* Allocate all global buffers (for DYN_ALLOC option) */ |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 992 | gunzip_window = xmalloc(GUNZIP_WSIZE); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 993 | gunzip_outbuf_count = 0; |
| 994 | gunzip_bytes_out = 0; |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 995 | gunzip_src_fd = xstate->src_fd; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 996 | |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 997 | /* (re) initialize state */ |
| 998 | method = -1; |
| 999 | need_another_block = 1; |
| 1000 | resume_copy = 0; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 1001 | gunzip_bk = 0; |
| 1002 | gunzip_bb = 0; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 1003 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 1004 | /* Create the crc table */ |
Denys Vlasenko | ddacb03 | 2018-02-01 10:56:19 +0100 | [diff] [blame] | 1005 | gunzip_crc_table = crc32_new_table_le(); |
Rob Landley | c57ec37 | 2006-04-10 17:07:15 +0000 | [diff] [blame] | 1006 | gunzip_crc = ~0; |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 1007 | |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 1008 | error_msg = "corrupted data"; |
| 1009 | if (setjmp(error_jmp)) { |
| 1010 | /* Error from deep inside zip machinery */ |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1011 | bb_simple_error_msg(error_msg); |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 1012 | n = -1; |
| 1013 | goto ret; |
| 1014 | } |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 1015 | |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 1016 | while (1) { |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 1017 | int r = inflate_get_next_window(PASS_STATE_ONLY); |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 1018 | nwrote = transformer_write(xstate, gunzip_window, gunzip_outbuf_count); |
| 1019 | if (nwrote == (ssize_t)-1) { |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 1020 | n = -1; |
| 1021 | goto ret; |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 1022 | } |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1023 | IF_DESKTOP(n += nwrote;) |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 1024 | if (r == 0) break; |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 1027 | /* Store unused bytes in a global buffer so calling applets can access it */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 1028 | if (gunzip_bk >= 8) { |
| 1029 | /* Undo too much lookahead. The next read will be byte aligned |
| 1030 | * so we can discard unused bits in the last meaningful byte. */ |
Glenn L McGrath | eda4f53 | 2002-11-24 06:01:20 +0000 | [diff] [blame] | 1031 | bytebuffer_offset--; |
| 1032 | bytebuffer[bytebuffer_offset] = gunzip_bb & 0xff; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 1033 | gunzip_bb >>= 8; |
| 1034 | gunzip_bk -= 8; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 1035 | } |
Denis Vlasenko | d1a19af | 2007-01-05 23:58:45 +0000 | [diff] [blame] | 1036 | ret: |
| 1037 | /* Cleanup */ |
| 1038 | free(gunzip_window); |
| 1039 | free(gunzip_crc_table); |
| 1040 | return n; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 1041 | } |
Glenn L McGrath | cc61692 | 2003-02-09 04:46:34 +0000 | [diff] [blame] | 1042 | |
Denis Vlasenko | cd42cb8 | 2007-01-05 23:56:53 +0000 | [diff] [blame] | 1043 | |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 1044 | /* External entry points */ |
| 1045 | |
| 1046 | /* For unzip */ |
| 1047 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1048 | IF_DESKTOP(long long) int FAST_FUNC |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 1049 | inflate_unzip(transformer_state_t *xstate) |
Denis Vlasenko | cd42cb8 | 2007-01-05 23:56:53 +0000 | [diff] [blame] | 1050 | { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1051 | IF_DESKTOP(long long) int n; |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 1052 | DECLARE_STATE; |
| 1053 | |
| 1054 | ALLOC_STATE; |
Denis Vlasenko | cd42cb8 | 2007-01-05 23:56:53 +0000 | [diff] [blame] | 1055 | |
Denys Vlasenko | e7800f3 | 2014-12-07 00:42:49 +0100 | [diff] [blame] | 1056 | to_read = xstate->bytes_in; |
Denis Vlasenko | e8ef7ec | 2008-02-04 12:12:48 +0000 | [diff] [blame] | 1057 | // bytebuffer_max = 0x8000; |
Denis Vlasenko | cd42cb8 | 2007-01-05 23:56:53 +0000 | [diff] [blame] | 1058 | bytebuffer_offset = 4; |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 1059 | bytebuffer = xmalloc(bytebuffer_max); |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 1060 | n = inflate_unzip_internal(PASS_STATE xstate); |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 1061 | free(bytebuffer); |
Denis Vlasenko | cd42cb8 | 2007-01-05 23:56:53 +0000 | [diff] [blame] | 1062 | |
Denys Vlasenko | e7800f3 | 2014-12-07 00:42:49 +0100 | [diff] [blame] | 1063 | xstate->crc32 = gunzip_crc; |
| 1064 | xstate->bytes_out = gunzip_bytes_out; |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 1065 | DEALLOC_STATE; |
Denis Vlasenko | cd42cb8 | 2007-01-05 23:56:53 +0000 | [diff] [blame] | 1066 | return n; |
| 1067 | } |
| 1068 | |
| 1069 | |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 1070 | /* For gunzip */ |
| 1071 | |
| 1072 | /* helpers first */ |
| 1073 | |
| 1074 | /* Top up the input buffer with at least n bytes. */ |
| 1075 | static int top_up(STATE_PARAM unsigned n) |
| 1076 | { |
| 1077 | int count = bytebuffer_size - bytebuffer_offset; |
| 1078 | |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 1079 | if (count < (int)n) { |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 1080 | memmove(bytebuffer, &bytebuffer[bytebuffer_offset], count); |
| 1081 | bytebuffer_offset = 0; |
| 1082 | bytebuffer_size = full_read(gunzip_src_fd, &bytebuffer[count], bytebuffer_max - count); |
| 1083 | if ((int)bytebuffer_size < 0) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1084 | bb_simple_error_msg(bb_msg_read_error); |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 1085 | return 0; |
| 1086 | } |
| 1087 | bytebuffer_size += count; |
| 1088 | if (bytebuffer_size < n) |
| 1089 | return 0; |
| 1090 | } |
| 1091 | return 1; |
| 1092 | } |
| 1093 | |
| 1094 | static uint16_t buffer_read_le_u16(STATE_PARAM_ONLY) |
| 1095 | { |
| 1096 | uint16_t res; |
| 1097 | #if BB_LITTLE_ENDIAN |
Denis Vlasenko | efb545b | 2008-12-08 22:56:18 +0000 | [diff] [blame] | 1098 | move_from_unaligned16(res, &bytebuffer[bytebuffer_offset]); |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 1099 | #else |
| 1100 | res = bytebuffer[bytebuffer_offset]; |
| 1101 | res |= bytebuffer[bytebuffer_offset + 1] << 8; |
| 1102 | #endif |
| 1103 | bytebuffer_offset += 2; |
| 1104 | return res; |
| 1105 | } |
| 1106 | |
| 1107 | static uint32_t buffer_read_le_u32(STATE_PARAM_ONLY) |
| 1108 | { |
| 1109 | uint32_t res; |
| 1110 | #if BB_LITTLE_ENDIAN |
Denis Vlasenko | efb545b | 2008-12-08 22:56:18 +0000 | [diff] [blame] | 1111 | move_from_unaligned32(res, &bytebuffer[bytebuffer_offset]); |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 1112 | #else |
| 1113 | res = bytebuffer[bytebuffer_offset]; |
| 1114 | res |= bytebuffer[bytebuffer_offset + 1] << 8; |
| 1115 | res |= bytebuffer[bytebuffer_offset + 2] << 16; |
| 1116 | res |= bytebuffer[bytebuffer_offset + 3] << 24; |
| 1117 | #endif |
| 1118 | bytebuffer_offset += 4; |
| 1119 | return res; |
| 1120 | } |
| 1121 | |
Denys Vlasenko | e7800f3 | 2014-12-07 00:42:49 +0100 | [diff] [blame] | 1122 | static int check_header_gzip(STATE_PARAM transformer_state_t *xstate) |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 1123 | { |
| 1124 | union { |
| 1125 | unsigned char raw[8]; |
| 1126 | struct { |
| 1127 | uint8_t gz_method; |
| 1128 | uint8_t flags; |
Denis Vlasenko | 1a9e9bd | 2008-11-01 12:54:56 +0000 | [diff] [blame] | 1129 | uint32_t mtime; |
| 1130 | uint8_t xtra_flags_UNUSED; |
| 1131 | uint8_t os_flags_UNUSED; |
Denys Vlasenko | 8dc0e19 | 2009-09-16 00:58:11 +0200 | [diff] [blame] | 1132 | } PACKED formatted; |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 1133 | } header; |
Denys Vlasenko | 7b85ec3 | 2015-10-13 17:17:34 +0200 | [diff] [blame] | 1134 | |
| 1135 | BUILD_BUG_ON(sizeof(header) != 8); |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 1136 | |
| 1137 | /* |
| 1138 | * Rewind bytebuffer. We use the beginning because the header has 8 |
| 1139 | * bytes, leaving enough for unwinding afterwards. |
| 1140 | */ |
| 1141 | bytebuffer_size -= bytebuffer_offset; |
| 1142 | memmove(bytebuffer, &bytebuffer[bytebuffer_offset], bytebuffer_size); |
| 1143 | bytebuffer_offset = 0; |
| 1144 | |
| 1145 | if (!top_up(PASS_STATE 8)) |
| 1146 | return 0; |
| 1147 | memcpy(header.raw, &bytebuffer[bytebuffer_offset], 8); |
| 1148 | bytebuffer_offset += 8; |
| 1149 | |
| 1150 | /* Check the compression method */ |
| 1151 | if (header.formatted.gz_method != 8) { |
| 1152 | return 0; |
| 1153 | } |
| 1154 | |
| 1155 | if (header.formatted.flags & 0x04) { |
| 1156 | /* bit 2 set: extra field present */ |
| 1157 | unsigned extra_short; |
| 1158 | |
| 1159 | if (!top_up(PASS_STATE 2)) |
| 1160 | return 0; |
| 1161 | extra_short = buffer_read_le_u16(PASS_STATE_ONLY); |
| 1162 | if (!top_up(PASS_STATE extra_short)) |
| 1163 | return 0; |
| 1164 | /* Ignore extra field */ |
| 1165 | bytebuffer_offset += extra_short; |
| 1166 | } |
| 1167 | |
| 1168 | /* Discard original name and file comment if any */ |
| 1169 | /* bit 3 set: original file name present */ |
| 1170 | /* bit 4 set: file comment present */ |
| 1171 | if (header.formatted.flags & 0x18) { |
| 1172 | while (1) { |
| 1173 | do { |
| 1174 | if (!top_up(PASS_STATE 1)) |
| 1175 | return 0; |
| 1176 | } while (bytebuffer[bytebuffer_offset++] != 0); |
| 1177 | if ((header.formatted.flags & 0x18) != 0x18) |
| 1178 | break; |
| 1179 | header.formatted.flags &= ~0x18; |
| 1180 | } |
| 1181 | } |
| 1182 | |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 1183 | xstate->mtime = SWAP_LE32(header.formatted.mtime); |
Denis Vlasenko | 1a9e9bd | 2008-11-01 12:54:56 +0000 | [diff] [blame] | 1184 | |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 1185 | /* Read the header checksum */ |
| 1186 | if (header.formatted.flags & 0x02) { |
| 1187 | if (!top_up(PASS_STATE 2)) |
| 1188 | return 0; |
| 1189 | bytebuffer_offset += 2; |
| 1190 | } |
| 1191 | return 1; |
| 1192 | } |
| 1193 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1194 | IF_DESKTOP(long long) int FAST_FUNC |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 1195 | unpack_gz_stream(transformer_state_t *xstate) |
Glenn L McGrath | cc61692 | 2003-02-09 04:46:34 +0000 | [diff] [blame] | 1196 | { |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 1197 | uint32_t v32; |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 1198 | IF_DESKTOP(long long) int total, n; |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 1199 | DECLARE_STATE; |
Glenn L McGrath | cc61692 | 2003-02-09 04:46:34 +0000 | [diff] [blame] | 1200 | |
Denys Vlasenko | 02c3c38 | 2012-03-06 16:32:06 +0100 | [diff] [blame] | 1201 | #if !ENABLE_FEATURE_SEAMLESS_Z |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 1202 | if (check_signature16(xstate, GZIP_MAGIC)) |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 1203 | return -1; |
Denys Vlasenko | 02c3c38 | 2012-03-06 16:32:06 +0100 | [diff] [blame] | 1204 | #else |
Denys Vlasenko | 984b0a6 | 2016-06-20 11:06:42 +0200 | [diff] [blame] | 1205 | if (!xstate->signature_skipped) { |
Denys Vlasenko | 02c3c38 | 2012-03-06 16:32:06 +0100 | [diff] [blame] | 1206 | uint16_t magic2; |
| 1207 | |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 1208 | if (full_read(xstate->src_fd, &magic2, 2) != 2) { |
Denys Vlasenko | 02c3c38 | 2012-03-06 16:32:06 +0100 | [diff] [blame] | 1209 | bad_magic: |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1210 | bb_simple_error_msg("invalid magic"); |
Denys Vlasenko | 02c3c38 | 2012-03-06 16:32:06 +0100 | [diff] [blame] | 1211 | return -1; |
| 1212 | } |
| 1213 | if (magic2 == COMPRESS_MAGIC) { |
Denys Vlasenko | 984b0a6 | 2016-06-20 11:06:42 +0200 | [diff] [blame] | 1214 | xstate->signature_skipped = 2; |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 1215 | return unpack_Z_stream(xstate); |
Denys Vlasenko | 02c3c38 | 2012-03-06 16:32:06 +0100 | [diff] [blame] | 1216 | } |
| 1217 | if (magic2 != GZIP_MAGIC) |
| 1218 | goto bad_magic; |
| 1219 | } |
| 1220 | #endif |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 1221 | |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 1222 | total = 0; |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 1223 | |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 1224 | ALLOC_STATE; |
Denis Vlasenko | e8ef7ec | 2008-02-04 12:12:48 +0000 | [diff] [blame] | 1225 | to_read = -1; |
| 1226 | // bytebuffer_max = 0x8000; |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 1227 | bytebuffer = xmalloc(bytebuffer_max); |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 1228 | gunzip_src_fd = xstate->src_fd; |
Denis Vlasenko | cd42cb8 | 2007-01-05 23:56:53 +0000 | [diff] [blame] | 1229 | |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 1230 | again: |
Denys Vlasenko | e7800f3 | 2014-12-07 00:42:49 +0100 | [diff] [blame] | 1231 | if (!check_header_gzip(PASS_STATE xstate)) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1232 | bb_simple_error_msg("corrupted data"); |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 1233 | total = -1; |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 1234 | goto ret; |
Glenn L McGrath | cc61692 | 2003-02-09 04:46:34 +0000 | [diff] [blame] | 1235 | } |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 1236 | |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 1237 | n = inflate_unzip_internal(PASS_STATE xstate); |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 1238 | if (n < 0) { |
| 1239 | total = -1; |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 1240 | goto ret; |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 1241 | } |
| 1242 | total += n; |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 1243 | |
| 1244 | if (!top_up(PASS_STATE 8)) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1245 | bb_simple_error_msg("corrupted data"); |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 1246 | total = -1; |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 1247 | goto ret; |
Glenn L McGrath | cc61692 | 2003-02-09 04:46:34 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
| 1250 | /* Validate decompression - crc */ |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 1251 | v32 = buffer_read_le_u32(PASS_STATE_ONLY); |
| 1252 | if ((~gunzip_crc) != v32) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1253 | bb_simple_error_msg("crc error"); |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 1254 | total = -1; |
Denis Vlasenko | cd42cb8 | 2007-01-05 23:56:53 +0000 | [diff] [blame] | 1255 | goto ret; |
Glenn L McGrath | cc61692 | 2003-02-09 04:46:34 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
| 1258 | /* Validate decompression - size */ |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 1259 | v32 = buffer_read_le_u32(PASS_STATE_ONLY); |
| 1260 | if ((uint32_t)gunzip_bytes_out != v32) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1261 | bb_simple_error_msg("incorrect length"); |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 1262 | total = -1; |
Glenn L McGrath | cc61692 | 2003-02-09 04:46:34 +0000 | [diff] [blame] | 1263 | } |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 1264 | |
| 1265 | if (!top_up(PASS_STATE 2)) |
| 1266 | goto ret; /* EOF */ |
| 1267 | |
| 1268 | if (bytebuffer[bytebuffer_offset] == 0x1f |
| 1269 | && bytebuffer[bytebuffer_offset + 1] == 0x8b |
| 1270 | ) { |
| 1271 | bytebuffer_offset += 2; |
| 1272 | goto again; |
| 1273 | } |
| 1274 | /* GNU gzip says: */ |
| 1275 | /*bb_error_msg("decompression OK, trailing garbage ignored");*/ |
| 1276 | |
Denis Vlasenko | cd42cb8 | 2007-01-05 23:56:53 +0000 | [diff] [blame] | 1277 | ret: |
| 1278 | free(bytebuffer); |
Denis Vlasenko | c7a4aa5 | 2007-01-06 00:03:11 +0000 | [diff] [blame] | 1279 | DEALLOC_STATE; |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 1280 | return total; |
Glenn L McGrath | cc61692 | 2003-02-09 04:46:34 +0000 | [diff] [blame] | 1281 | } |