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