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