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 |
Glenn L McGrath | c6992fe | 2004-04-25 05:11:19 +0000 | [diff] [blame] | 15 | * busybox functions by Glenn McGrath <bug1@iinet.net.au> |
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 | |
Rob Landley | 11c7a7b | 2006-06-25 22:39:24 +0000 | [diff] [blame] | 36 | #include "libbb.h" |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 37 | #include "unarchive.h" |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 38 | |
| 39 | typedef struct huft_s { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 40 | unsigned char e; /* number of extra bits or operation */ |
| 41 | unsigned char b; /* number of bits in this code or subcode */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 42 | union { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 43 | unsigned short n; /* literal, length base, or distance base */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 44 | struct huft_s *t; /* pointer to next level of table */ |
| 45 | } v; |
| 46 | } huft_t; |
| 47 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 48 | static int gunzip_src_fd; |
Glenn L McGrath | 826b48b | 2003-02-09 12:00:17 +0000 | [diff] [blame] | 49 | unsigned int gunzip_bytes_out; /* number of output bytes */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 50 | static unsigned int gunzip_outbuf_count; /* bytes in output buffer */ |
| 51 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 52 | /* gunzip_window size--must be a power of two, and |
| 53 | * at least 32K for zip's deflate method */ |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 54 | enum { gunzip_wsize = 0x8000 }; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 55 | static unsigned char *gunzip_window; |
Glenn L McGrath | cc61692 | 2003-02-09 04:46:34 +0000 | [diff] [blame] | 56 | |
Rob Landley | c57ec37 | 2006-04-10 17:07:15 +0000 | [diff] [blame] | 57 | static uint32_t *gunzip_crc_table; |
| 58 | uint32_t gunzip_crc; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 59 | |
| 60 | /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */ |
| 61 | #define BMAX 16 /* maximum bit length of any code (16 for explode) */ |
| 62 | #define N_MAX 288 /* maximum number of codes in any set */ |
| 63 | |
Glenn L McGrath | cc61692 | 2003-02-09 04:46:34 +0000 | [diff] [blame] | 64 | /* bitbuffer */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 65 | static unsigned int gunzip_bb; /* bit buffer */ |
| 66 | static unsigned char gunzip_bk; /* bits in bit buffer */ |
| 67 | |
Glenn L McGrath | cc61692 | 2003-02-09 04:46:34 +0000 | [diff] [blame] | 68 | /* These control the size of the bytebuffer */ |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 69 | static unsigned int bytebuffer_max = 0x8000; |
Glenn L McGrath | cc61692 | 2003-02-09 04:46:34 +0000 | [diff] [blame] | 70 | static unsigned char *bytebuffer = NULL; |
| 71 | static unsigned int bytebuffer_offset = 0; |
| 72 | static unsigned int bytebuffer_size = 0; |
| 73 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame] | 74 | static const unsigned short mask_bits[] = { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 75 | 0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff, |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 76 | 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff |
| 77 | }; |
| 78 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 79 | /* Copy lengths for literal codes 257..285 */ |
| 80 | static const unsigned short cplens[] = { |
| 81 | 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] | 82 | 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | /* note: see note #13 above about the 258 in this list. */ |
| 86 | /* Extra bits for literal codes 257..285 */ |
| 87 | static const unsigned char cplext[] = { |
| 88 | 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] | 89 | 5, 5, 5, 0, 99, 99 |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 90 | }; /* 99==invalid */ |
| 91 | |
| 92 | /* Copy offsets for distance codes 0..29 */ |
| 93 | static const unsigned short cpdist[] = { |
| 94 | 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] | 95 | 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577 |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | /* Extra bits for distance codes */ |
| 99 | static const unsigned char cpdext[] = { |
| 100 | 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] | 101 | 11, 11, 12, 12, 13, 13 |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | /* Tables for deflate from PKZIP's appnote.txt. */ |
| 105 | /* Order of the bit length code lengths */ |
| 106 | static const unsigned char border[] = { |
| 107 | 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 |
| 108 | }; |
| 109 | |
| 110 | static unsigned int fill_bitbuffer(unsigned int bitbuffer, unsigned int *current, const unsigned int required) |
| 111 | { |
| 112 | while (*current < required) { |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 113 | if (bytebuffer_offset >= bytebuffer_size) { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 114 | /* Leave the first 4 bytes empty so we can always unwind the bitbuffer |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 115 | * to the front of the bytebuffer, leave 4 bytes free at end of tail |
| 116 | * so we can easily top up buffer in check_trailer_gzip() */ |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 117 | if (1 > (bytebuffer_size = safe_read(gunzip_src_fd, &bytebuffer[4], bytebuffer_max - 8))) |
Manuel Novoa III | 0d8c652 | 2005-03-01 19:29:29 +0000 | [diff] [blame] | 118 | bb_error_msg_and_die("unexpected end of file"); |
Manuel Novoa III | 0d8c652 | 2005-03-01 19:29:29 +0000 | [diff] [blame] | 119 | bytebuffer_size += 4; |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 120 | bytebuffer_offset = 4; |
| 121 | } |
Glenn L McGrath | eda4f53 | 2002-11-24 06:01:20 +0000 | [diff] [blame] | 122 | bitbuffer |= ((unsigned int) bytebuffer[bytebuffer_offset]) << *current; |
| 123 | bytebuffer_offset++; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 124 | *current += 8; |
| 125 | } |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 126 | return(bitbuffer); |
| 127 | } |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 128 | |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 129 | /* |
| 130 | * Free the malloc'ed tables built by huft_build(), which makes a linked |
| 131 | * 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] | 132 | * each table. |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 133 | * t: table to free |
| 134 | */ |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 135 | static int huft_free(huft_t * t) |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 136 | { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 137 | huft_t *p; |
| 138 | huft_t *q; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 139 | |
| 140 | /* Go through linked list, freeing from the malloced (t[-1]) address. */ |
| 141 | p = t; |
| 142 | while (p != (huft_t *) NULL) { |
| 143 | q = (--p)->v.t; |
| 144 | free((char *) p); |
| 145 | p = q; |
| 146 | } |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | /* Given a list of code lengths and a maximum table size, make a set of |
| 151 | * tables to decode that set of codes. Return zero on success, one if |
| 152 | * the given code set is incomplete (the tables are still built in this |
| 153 | * case), two if the input is invalid (all zero length codes or an |
| 154 | * oversubscribed set of lengths), and three if not enough memory. |
| 155 | * |
| 156 | * b: code lengths in bits (all assumed <= BMAX) |
| 157 | * n: number of codes (assumed <= N_MAX) |
| 158 | * s: number of simple-valued codes (0..s-1) |
| 159 | * d: list of base values for non-simple codes |
| 160 | * e: list of extra bits for non-simple codes |
| 161 | * t: result: starting table |
| 162 | * m: maximum lookup bits, returns actual |
| 163 | */ |
Bernhard Reutner-Fischer | 7ab5f4d | 2006-04-02 21:23:40 +0000 | [diff] [blame] | 164 | static |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 165 | int huft_build(unsigned int *b, const unsigned int n, |
| 166 | const unsigned int s, const unsigned short *d, |
Eric Andersen | f55289f | 2006-01-30 17:27:00 +0000 | [diff] [blame] | 167 | const unsigned char *e, huft_t ** t, unsigned int *m) |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 168 | { |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 169 | unsigned a; /* counter for codes of length k */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 170 | unsigned c[BMAX + 1]; /* bit length count table */ |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 171 | unsigned eob_len; /* length of end-of-block code (value 256) */ |
| 172 | unsigned f; /* i repeats in table every f entries */ |
| 173 | int g; /* maximum code length */ |
Rob Landley | 11c7a7b | 2006-06-25 22:39:24 +0000 | [diff] [blame] | 174 | int htl; /* table level */ |
| 175 | unsigned i; /* counter, current code */ |
| 176 | unsigned j; /* counter */ |
| 177 | int k; /* number of bits in current code */ |
| 178 | unsigned *p; /* pointer into c[], b[], or v[] */ |
| 179 | huft_t *q; /* points to current table */ |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 180 | huft_t r; /* table entry for structure assignment */ |
| 181 | huft_t *u[BMAX]; /* table stack */ |
| 182 | unsigned v[N_MAX]; /* values in order of bit length */ |
| 183 | int ws[BMAX+1]; /* bits decoded stack */ |
Rob Landley | 11c7a7b | 2006-06-25 22:39:24 +0000 | [diff] [blame] | 184 | int w; /* bits decoded */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 185 | unsigned x[BMAX + 1]; /* bit offsets, then code stack */ |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 186 | unsigned *xp; /* pointer into x */ |
| 187 | int y; /* number of dummy codes added */ |
| 188 | unsigned z; /* number of entries in current table */ |
| 189 | |
| 190 | /* Length of EOB code, if any */ |
| 191 | eob_len = n > 256 ? b[256] : BMAX; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 192 | |
| 193 | /* Generate counts for each bit length */ |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 194 | memset((void *)c, 0, sizeof(c)); |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 195 | p = b; |
| 196 | i = n; |
| 197 | do { |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 198 | c[*p]++; /* assume all entries <= BMAX */ |
| 199 | p++; /* Can't combine with above line (Solaris bug) */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 200 | } while (--i); |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 201 | if (c[0] == n) { /* null input--all zero length codes */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 202 | *t = (huft_t *) NULL; |
| 203 | *m = 0; |
Rob Landley | eb00afb | 2006-02-20 02:18:03 +0000 | [diff] [blame] | 204 | return 2; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /* Find minimum and maximum length, bound *m by those */ |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 208 | for (j = 1; (c[j] == 0) && (j <= BMAX); j++); |
| 209 | k = j; /* minimum code length */ |
| 210 | for (i = BMAX; (c[i] == 0) && i; i--); |
| 211 | g = i; /* maximum code length */ |
| 212 | *m = (*m < j) ? j : ((*m > i) ? i : *m); |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 213 | |
| 214 | /* Adjust last length count to fill out codes, if needed */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 215 | for (y = 1 << j; j < i; j++, y <<= 1) { |
| 216 | if ((y -= c[j]) < 0) { |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 217 | return 2; /* bad input: more codes than bits */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | if ((y -= c[i]) < 0) { |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 221 | return 2; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 222 | } |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 223 | c[i] += y; |
| 224 | |
| 225 | /* Generate starting offsets into the value table for each length */ |
| 226 | x[1] = j = 0; |
| 227 | p = c + 1; |
| 228 | xp = x + 2; |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 229 | while (--i) { /* note that i == g from above */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 230 | *xp++ = (j += *p++); |
| 231 | } |
| 232 | |
| 233 | /* Make a table of values in order of bit lengths */ |
| 234 | p = b; |
| 235 | i = 0; |
| 236 | do { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 237 | if ((j = *p++) != 0) { |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 238 | v[x[j]++] = i; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 239 | } |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 240 | } while (++i < n); |
| 241 | |
| 242 | /* Generate the Huffman codes and for each, make the table entries */ |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 243 | x[0] = i = 0; /* first Huffman code is zero */ |
| 244 | p = v; /* grab values in bit order */ |
Rob Landley | 11c7a7b | 2006-06-25 22:39:24 +0000 | [diff] [blame] | 245 | htl = -1; /* no tables yet--level -1 */ |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 246 | w = ws[0] = 0; /* bits decoded */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 247 | u[0] = (huft_t *) NULL; /* just to keep compilers happy */ |
| 248 | q = (huft_t *) NULL; /* ditto */ |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 249 | z = 0; /* ditto */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 250 | |
| 251 | /* go through the bit lengths (k already is bits in shortest code) */ |
| 252 | for (; k <= g; k++) { |
| 253 | a = c[k]; |
| 254 | while (a--) { |
| 255 | /* here i is the Huffman code of length k bits for value *p */ |
| 256 | /* make tables up to required level */ |
Rob Landley | 11c7a7b | 2006-06-25 22:39:24 +0000 | [diff] [blame] | 257 | while (k > ws[htl + 1]) { |
| 258 | w = ws[++htl]; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 259 | |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 260 | /* compute minimum size table less than or equal to *m bits */ |
| 261 | z = (z = g - w) > *m ? *m : z; /* upper limit on table size */ |
| 262 | if ((f = 1 << (j = k - w)) > a + 1) { /* try a k-w bit table */ |
| 263 | /* too few codes for k-w bit table */ |
| 264 | f -= a + 1; /* deduct codes from patterns left */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 265 | xp = c + k; |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 266 | while (++j < z) { /* try smaller tables up to z bits */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 267 | if ((f <<= 1) <= *++xp) { |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 268 | break; /* enough codes to use up j bits */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 269 | } |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 270 | f -= *xp; /* else deduct codes from patterns */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 271 | } |
| 272 | } |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 273 | 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] | 274 | z = 1 << j; /* table entries for j-bit table */ |
Rob Landley | 11c7a7b | 2006-06-25 22:39:24 +0000 | [diff] [blame] | 275 | ws[htl+1] = w + j; /* set bits decoded in stack */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 276 | |
| 277 | /* allocate and link in new table */ |
Rob Landley | 11c7a7b | 2006-06-25 22:39:24 +0000 | [diff] [blame] | 278 | q = (huft_t *) xzalloc((z + 1) * sizeof(huft_t)); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 279 | *t = q + 1; /* link to list for huft_free() */ |
Rob Landley | 11c7a7b | 2006-06-25 22:39:24 +0000 | [diff] [blame] | 280 | t = &(q->v.t); |
| 281 | u[htl] = ++q; /* table starts after link */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 282 | |
| 283 | /* connect to last table, if there is one */ |
Rob Landley | 11c7a7b | 2006-06-25 22:39:24 +0000 | [diff] [blame] | 284 | if (htl) { |
| 285 | x[htl] = i; /* save pattern for backing up */ |
| 286 | 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] | 287 | r.e = (unsigned char) (16 + j); /* bits in this table */ |
| 288 | r.v.t = q; /* pointer to this table */ |
Rob Landley | 11c7a7b | 2006-06-25 22:39:24 +0000 | [diff] [blame] | 289 | j = (i & ((1 << w) - 1)) >> ws[htl - 1]; |
| 290 | u[htl - 1][j] = r; /* connect to last table */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
| 294 | /* set up table entry in r */ |
| 295 | r.b = (unsigned char) (k - w); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 296 | if (p >= v + n) { |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 297 | r.e = 99; /* out of values--invalid code */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 298 | } else if (*p < s) { |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 299 | r.e = (unsigned char) (*p < 256 ? 16 : 15); /* 256 is EOB code */ |
| 300 | 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] | 301 | } else { |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 302 | 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] | 303 | r.v.n = d[*p++ - s]; |
| 304 | } |
| 305 | |
| 306 | /* fill code-like entries with r */ |
| 307 | f = 1 << (k - w); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 308 | for (j = i >> w; j < z; j += f) { |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 309 | q[j] = r; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 310 | } |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 311 | |
| 312 | /* backwards increment the k-bit code i */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 313 | for (j = 1 << (k - 1); i & j; j >>= 1) { |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 314 | i ^= j; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 315 | } |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 316 | i ^= j; |
| 317 | |
| 318 | /* backup over finished tables */ |
Rob Landley | 11c7a7b | 2006-06-25 22:39:24 +0000 | [diff] [blame] | 319 | while ((i & ((1 << w) - 1)) != x[htl]) { |
| 320 | w = ws[--htl]; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | } |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 324 | |
| 325 | /* return actual size of base table */ |
| 326 | *m = ws[1]; |
| 327 | |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 328 | /* Return true (1) if we were given an incomplete table */ |
| 329 | return y != 0 && g != 1; |
| 330 | } |
| 331 | |
| 332 | /* |
| 333 | * inflate (decompress) the codes in a deflated (compressed) block. |
| 334 | * Return an error code or zero if it all goes ok. |
| 335 | * |
| 336 | * tl, td: literal/length and distance decoder tables |
| 337 | * bl, bd: number of bits decoded by tl[] and td[] |
| 338 | */ |
Glenn L McGrath | 0126fda | 2002-11-20 06:46:46 +0000 | [diff] [blame] | 339 | static int inflate_codes(huft_t * my_tl, huft_t * my_td, const unsigned int my_bl, const unsigned int my_bd, int setup) |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 340 | { |
Glenn L McGrath | 0126fda | 2002-11-20 06:46:46 +0000 | [diff] [blame] | 341 | static unsigned int e; /* table entry flag/number of extra bits */ |
| 342 | static unsigned int n, d; /* length and index for copy */ |
| 343 | static unsigned int w; /* current gunzip_window position */ |
| 344 | static huft_t *t; /* pointer to table entry */ |
| 345 | static unsigned int ml, md; /* masks for bl and bd bits */ |
| 346 | static unsigned int b; /* bit buffer */ |
| 347 | static unsigned int k; /* number of bits in bit buffer */ |
| 348 | static huft_t *tl, *td; |
| 349 | static unsigned int bl, bd; |
| 350 | static int resumeCopy = 0; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 351 | |
Glenn L McGrath | 0126fda | 2002-11-20 06:46:46 +0000 | [diff] [blame] | 352 | if (setup) { // 1st time we are called, copy in variables |
| 353 | tl = my_tl; |
| 354 | td = my_td; |
| 355 | bl = my_bl; |
| 356 | bd = my_bd; |
| 357 | /* make local copies of globals */ |
| 358 | b = gunzip_bb; /* initialize bit buffer */ |
| 359 | k = gunzip_bk; |
| 360 | w = gunzip_outbuf_count; /* initialize gunzip_window position */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 361 | |
Glenn L McGrath | 0126fda | 2002-11-20 06:46:46 +0000 | [diff] [blame] | 362 | /* inflate the coded data */ |
| 363 | ml = mask_bits[bl]; /* precompute masks for speed */ |
| 364 | md = mask_bits[bd]; |
| 365 | return 0; // Don't actually do anything the first time |
| 366 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 367 | |
Glenn L McGrath | 0126fda | 2002-11-20 06:46:46 +0000 | [diff] [blame] | 368 | if (resumeCopy) goto do_copy; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 369 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 370 | while (1) { /* do until end of block */ |
| 371 | b = fill_bitbuffer(b, &k, bl); |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 372 | if ((e = (t = tl + ((unsigned) b & ml))->e) > 16) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 373 | do { |
| 374 | if (e == 99) { |
Bernhard Reutner-Fischer | 0b42a6a | 2005-10-07 11:34:50 +0000 | [diff] [blame] | 375 | bb_error_msg_and_die("inflate_codes error 1"); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 376 | } |
| 377 | b >>= t->b; |
| 378 | k -= t->b; |
| 379 | e -= 16; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 380 | b = fill_bitbuffer(b, &k, e); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 381 | } while ((e = |
| 382 | (t = t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16); |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 383 | b >>= t->b; |
| 384 | k -= t->b; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 385 | if (e == 16) { /* then it's a literal */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 386 | gunzip_window[w++] = (unsigned char) t->v.n; |
| 387 | if (w == gunzip_wsize) { |
| 388 | gunzip_outbuf_count = (w); |
Glenn L McGrath | 0126fda | 2002-11-20 06:46:46 +0000 | [diff] [blame] | 389 | //flush_gunzip_window(); |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 390 | w = 0; |
Glenn L McGrath | 83bf47c | 2002-11-20 22:00:31 +0000 | [diff] [blame] | 391 | return 1; // We have a block to read |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 392 | } |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 393 | } else { /* it's an EOB or a length */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 394 | |
| 395 | /* exit if end of block */ |
| 396 | if (e == 15) { |
| 397 | break; |
| 398 | } |
| 399 | |
| 400 | /* get length of block to copy */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 401 | b = fill_bitbuffer(b, &k, e); |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 402 | n = t->v.n + ((unsigned) b & mask_bits[e]); |
| 403 | b >>= e; |
| 404 | k -= e; |
| 405 | |
| 406 | /* decode distance of block to copy */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 407 | b = fill_bitbuffer(b, &k, bd); |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 408 | if ((e = (t = td + ((unsigned) b & md))->e) > 16) |
| 409 | do { |
| 410 | if (e == 99) |
Bernhard Reutner-Fischer | 0b42a6a | 2005-10-07 11:34:50 +0000 | [diff] [blame] | 411 | bb_error_msg_and_die("inflate_codes error 2"); |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 412 | b >>= t->b; |
| 413 | k -= t->b; |
| 414 | e -= 16; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 415 | b = fill_bitbuffer(b, &k, e); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 416 | } while ((e = |
| 417 | (t = |
| 418 | t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16); |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 419 | b >>= t->b; |
| 420 | k -= t->b; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 421 | b = fill_bitbuffer(b, &k, e); |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 422 | d = w - t->v.n - ((unsigned) b & mask_bits[e]); |
| 423 | b >>= e; |
| 424 | k -= e; |
| 425 | |
| 426 | /* do the copy */ |
Glenn L McGrath | 0126fda | 2002-11-20 06:46:46 +0000 | [diff] [blame] | 427 | do_copy: do { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 428 | n -= (e = |
| 429 | (e = |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 430 | gunzip_wsize - ((d &= gunzip_wsize - 1) > w ? d : w)) > n ? n : e); |
| 431 | /* copy to new buffer to prevent possible overwrite */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 432 | if (w - d >= e) { /* (this test assumes unsigned comparison) */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 433 | memcpy(gunzip_window + w, gunzip_window + d, e); |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 434 | w += e; |
| 435 | d += e; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 436 | } else { |
| 437 | /* do it slow to avoid memcpy() overlap */ |
| 438 | /* !NOMEMCPY */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 439 | do { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 440 | gunzip_window[w++] = gunzip_window[d++]; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 441 | } while (--e); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 442 | } |
| 443 | if (w == gunzip_wsize) { |
| 444 | gunzip_outbuf_count = (w); |
Glenn L McGrath | 0126fda | 2002-11-20 06:46:46 +0000 | [diff] [blame] | 445 | if (n) resumeCopy = 1; |
| 446 | else resumeCopy = 0; |
| 447 | //flush_gunzip_window(); |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 448 | w = 0; |
Glenn L McGrath | 83bf47c | 2002-11-20 22:00:31 +0000 | [diff] [blame] | 449 | return 1; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 450 | } |
| 451 | } while (n); |
Glenn L McGrath | 0126fda | 2002-11-20 06:46:46 +0000 | [diff] [blame] | 452 | resumeCopy = 0; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | |
| 456 | /* restore the globals from the locals */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 457 | gunzip_outbuf_count = w; /* restore global gunzip_window pointer */ |
| 458 | gunzip_bb = b; /* restore global bit buffer */ |
| 459 | gunzip_bk = k; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 460 | |
Glenn L McGrath | 0126fda | 2002-11-20 06:46:46 +0000 | [diff] [blame] | 461 | /* normally just after call to inflate_codes, but save code by putting it here */ |
| 462 | /* free the decoding tables, return */ |
| 463 | huft_free(tl); |
| 464 | huft_free(td); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 465 | |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 466 | /* done */ |
| 467 | return 0; |
| 468 | } |
| 469 | |
Glenn L McGrath | 83bf47c | 2002-11-20 22:00:31 +0000 | [diff] [blame] | 470 | static int inflate_stored(int my_n, int my_b_stored, int my_k_stored, int setup) |
Glenn L McGrath | 0126fda | 2002-11-20 06:46:46 +0000 | [diff] [blame] | 471 | { |
Eric Andersen | f55289f | 2006-01-30 17:27:00 +0000 | [diff] [blame] | 472 | static unsigned int n, b_stored, k_stored, w; |
Glenn L McGrath | 0126fda | 2002-11-20 06:46:46 +0000 | [diff] [blame] | 473 | if (setup) { |
| 474 | n = my_n; |
| 475 | b_stored = my_b_stored; |
| 476 | k_stored = my_k_stored; |
| 477 | w = gunzip_outbuf_count; /* initialize gunzip_window position */ |
| 478 | return 0; // Don't do anything first time |
| 479 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 480 | |
Glenn L McGrath | 0126fda | 2002-11-20 06:46:46 +0000 | [diff] [blame] | 481 | /* read and output the compressed data */ |
| 482 | while (n--) { |
| 483 | b_stored = fill_bitbuffer(b_stored, &k_stored, 8); |
| 484 | gunzip_window[w++] = (unsigned char) b_stored; |
Eric Andersen | f55289f | 2006-01-30 17:27:00 +0000 | [diff] [blame] | 485 | if (w == gunzip_wsize) { |
Glenn L McGrath | 0126fda | 2002-11-20 06:46:46 +0000 | [diff] [blame] | 486 | gunzip_outbuf_count = (w); |
| 487 | //flush_gunzip_window(); |
| 488 | w = 0; |
| 489 | b_stored >>= 8; |
| 490 | k_stored -= 8; |
Glenn L McGrath | 83bf47c | 2002-11-20 22:00:31 +0000 | [diff] [blame] | 491 | return 1; // We have a block |
Glenn L McGrath | 0126fda | 2002-11-20 06:46:46 +0000 | [diff] [blame] | 492 | } |
| 493 | b_stored >>= 8; |
| 494 | k_stored -= 8; |
| 495 | } |
| 496 | |
| 497 | /* restore the globals from the locals */ |
| 498 | gunzip_outbuf_count = w; /* restore global gunzip_window pointer */ |
| 499 | gunzip_bb = b_stored; /* restore global bit buffer */ |
| 500 | gunzip_bk = k_stored; |
| 501 | return 0; // Finished |
| 502 | } |
| 503 | |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 504 | /* |
| 505 | * decompress an inflated block |
| 506 | * e: last block flag |
| 507 | * |
| 508 | * GLOBAL VARIABLES: bb, kk, |
| 509 | */ |
Glenn L McGrath | 0126fda | 2002-11-20 06:46:46 +0000 | [diff] [blame] | 510 | // Return values: -1 = inflate_stored, -2 = inflate_codes |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 511 | static int inflate_block(int *e) |
| 512 | { |
| 513 | unsigned t; /* block type */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 514 | unsigned int b; /* bit buffer */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 515 | unsigned int k; /* number of bits in bit buffer */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 516 | |
| 517 | /* make local bit buffer */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 518 | |
| 519 | b = gunzip_bb; |
| 520 | k = gunzip_bk; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 521 | |
| 522 | /* read in last block bit */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 523 | b = fill_bitbuffer(b, &k, 1); |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 524 | *e = (int) b & 1; |
| 525 | b >>= 1; |
| 526 | k -= 1; |
| 527 | |
| 528 | /* read in block type */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 529 | b = fill_bitbuffer(b, &k, 2); |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 530 | t = (unsigned) b & 3; |
| 531 | b >>= 2; |
| 532 | k -= 2; |
| 533 | |
| 534 | /* restore the global bit buffer */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 535 | gunzip_bb = b; |
| 536 | gunzip_bk = k; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 537 | |
| 538 | /* inflate that block type */ |
| 539 | switch (t) { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 540 | case 0: /* Inflate stored */ |
| 541 | { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 542 | unsigned int n; /* number of bytes in block */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 543 | unsigned int b_stored; /* bit buffer */ |
| 544 | unsigned int k_stored; /* number of bits in bit buffer */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 545 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 546 | /* make local copies of globals */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 547 | b_stored = gunzip_bb; /* initialize bit buffer */ |
| 548 | k_stored = gunzip_bk; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 549 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 550 | /* go to byte boundary */ |
| 551 | n = k_stored & 7; |
| 552 | b_stored >>= n; |
| 553 | k_stored -= n; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 554 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 555 | /* get the length and its complement */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 556 | b_stored = fill_bitbuffer(b_stored, &k_stored, 16); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 557 | n = ((unsigned) b_stored & 0xffff); |
| 558 | b_stored >>= 16; |
| 559 | k_stored -= 16; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 560 | |
| 561 | b_stored = fill_bitbuffer(b_stored, &k_stored, 16); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 562 | if (n != (unsigned) ((~b_stored) & 0xffff)) { |
| 563 | return 1; /* error in compressed data */ |
| 564 | } |
| 565 | b_stored >>= 16; |
| 566 | k_stored -= 16; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 567 | |
Glenn L McGrath | 0126fda | 2002-11-20 06:46:46 +0000 | [diff] [blame] | 568 | inflate_stored(n, b_stored, k_stored, 1); // Setup inflate_stored |
| 569 | return -1; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 570 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 571 | case 1: /* Inflate fixed |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 572 | * decompress an inflated type 1 (fixed Huffman codes) block. We should |
| 573 | * either replace this with a custom decoder, or at least precompute the |
| 574 | * Huffman tables. |
| 575 | */ |
| 576 | { |
| 577 | int i; /* temporary variable */ |
| 578 | huft_t *tl; /* literal/length code table */ |
| 579 | huft_t *td; /* distance code table */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 580 | unsigned int bl; /* lookup bits for tl */ |
| 581 | unsigned int bd; /* lookup bits for td */ |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 582 | unsigned int l[288]; /* length list for huft_build */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 583 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 584 | /* set up literal table */ |
| 585 | for (i = 0; i < 144; i++) { |
| 586 | l[i] = 8; |
| 587 | } |
| 588 | for (; i < 256; i++) { |
| 589 | l[i] = 9; |
| 590 | } |
| 591 | for (; i < 280; i++) { |
| 592 | l[i] = 7; |
| 593 | } |
| 594 | for (; i < 288; i++) { /* make a complete, but wrong code set */ |
| 595 | l[i] = 8; |
| 596 | } |
| 597 | bl = 7; |
| 598 | if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0) { |
| 599 | return i; |
| 600 | } |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 601 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 602 | /* set up distance table */ |
| 603 | for (i = 0; i < 30; i++) { /* make an incomplete code set */ |
| 604 | l[i] = 5; |
| 605 | } |
| 606 | bd = 5; |
| 607 | if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1) { |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 608 | huft_free(tl); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 609 | return i; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 610 | } |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 611 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 612 | /* decompress until an end-of-block code */ |
Glenn L McGrath | 0126fda | 2002-11-20 06:46:46 +0000 | [diff] [blame] | 613 | inflate_codes(tl, td, bl, bd, 1); // Setup inflate_codes |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 614 | |
Glenn L McGrath | 0126fda | 2002-11-20 06:46:46 +0000 | [diff] [blame] | 615 | /* huft_free code moved into inflate_codes */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 616 | |
Glenn L McGrath | 0126fda | 2002-11-20 06:46:46 +0000 | [diff] [blame] | 617 | return -2; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 618 | } |
| 619 | case 2: /* Inflate dynamic */ |
| 620 | { |
| 621 | const int dbits = 6; /* bits in base distance lookup table */ |
| 622 | const int lbits = 9; /* bits in base literal/length lookup table */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 623 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 624 | huft_t *tl; /* literal/length code table */ |
| 625 | huft_t *td; /* distance code table */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 626 | unsigned int i; /* temporary variables */ |
| 627 | unsigned int j; |
| 628 | unsigned int l; /* last length */ |
| 629 | unsigned int m; /* mask for bit lengths table */ |
| 630 | unsigned int n; /* number of lengths to get */ |
| 631 | unsigned int bl; /* lookup bits for tl */ |
| 632 | unsigned int bd; /* lookup bits for td */ |
| 633 | unsigned int nb; /* number of bit length codes */ |
| 634 | unsigned int nl; /* number of literal/length codes */ |
| 635 | unsigned int nd; /* number of distance codes */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 636 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 637 | unsigned int ll[286 + 30]; /* literal/length and distance code lengths */ |
| 638 | unsigned int b_dynamic; /* bit buffer */ |
| 639 | unsigned int k_dynamic; /* number of bits in bit buffer */ |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 640 | |
| 641 | /* make local bit buffer */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 642 | b_dynamic = gunzip_bb; |
| 643 | k_dynamic = gunzip_bk; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 644 | |
| 645 | /* read in table lengths */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 646 | b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 5); |
| 647 | nl = 257 + ((unsigned int) b_dynamic & 0x1f); /* number of literal/length codes */ |
| 648 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 649 | b_dynamic >>= 5; |
| 650 | k_dynamic -= 5; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 651 | b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 5); |
| 652 | nd = 1 + ((unsigned int) b_dynamic & 0x1f); /* number of distance codes */ |
| 653 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 654 | b_dynamic >>= 5; |
| 655 | k_dynamic -= 5; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 656 | b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 4); |
| 657 | nb = 4 + ((unsigned int) b_dynamic & 0xf); /* number of bit length codes */ |
| 658 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 659 | b_dynamic >>= 4; |
| 660 | k_dynamic -= 4; |
| 661 | if (nl > 286 || nd > 30) { |
| 662 | return 1; /* bad lengths */ |
| 663 | } |
| 664 | |
| 665 | /* read in bit-length-code lengths */ |
| 666 | for (j = 0; j < nb; j++) { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 667 | b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 3); |
| 668 | ll[border[j]] = (unsigned int) b_dynamic & 7; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 669 | b_dynamic >>= 3; |
| 670 | k_dynamic -= 3; |
| 671 | } |
| 672 | for (; j < 19; j++) { |
| 673 | ll[border[j]] = 0; |
| 674 | } |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 675 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 676 | /* build decoding table for trees--single level, 7 bit lookup */ |
| 677 | bl = 7; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 678 | i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl); |
| 679 | if (i != 0) { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 680 | if (i == 1) { |
| 681 | huft_free(tl); |
| 682 | } |
| 683 | return i; /* incomplete code set */ |
| 684 | } |
| 685 | |
| 686 | /* read in literal and distance code lengths */ |
| 687 | n = nl + nd; |
| 688 | m = mask_bits[bl]; |
| 689 | i = l = 0; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 690 | while ((unsigned int) i < n) { |
| 691 | b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, (unsigned int)bl); |
| 692 | j = (td = tl + ((unsigned int) b_dynamic & m))->b; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 693 | b_dynamic >>= j; |
| 694 | k_dynamic -= j; |
| 695 | j = td->v.n; |
| 696 | if (j < 16) { /* length of code in bits (0..15) */ |
| 697 | ll[i++] = l = j; /* save last length in l */ |
| 698 | } else if (j == 16) { /* repeat last length 3 to 6 times */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 699 | b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 2); |
| 700 | j = 3 + ((unsigned int) b_dynamic & 3); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 701 | b_dynamic >>= 2; |
| 702 | k_dynamic -= 2; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 703 | if ((unsigned int) i + j > n) { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 704 | return 1; |
| 705 | } |
| 706 | while (j--) { |
| 707 | ll[i++] = l; |
| 708 | } |
| 709 | } else if (j == 17) { /* 3 to 10 zero length codes */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 710 | b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 3); |
| 711 | j = 3 + ((unsigned int) b_dynamic & 7); |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 712 | b_dynamic >>= 3; |
| 713 | k_dynamic -= 3; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 714 | if ((unsigned int) i + j > n) { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 715 | return 1; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 716 | } |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 717 | while (j--) { |
| 718 | ll[i++] = 0; |
| 719 | } |
| 720 | l = 0; |
| 721 | } else { /* j == 18: 11 to 138 zero length codes */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 722 | b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 7); |
| 723 | j = 11 + ((unsigned int) b_dynamic & 0x7f); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 724 | b_dynamic >>= 7; |
| 725 | k_dynamic -= 7; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 726 | if ((unsigned int) i + j > n) { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 727 | return 1; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 728 | } |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 729 | while (j--) { |
| 730 | ll[i++] = 0; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 731 | } |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 732 | l = 0; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 733 | } |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 734 | } |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 735 | |
| 736 | /* free decoding table for trees */ |
| 737 | huft_free(tl); |
| 738 | |
| 739 | /* restore the global bit buffer */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 740 | gunzip_bb = b_dynamic; |
| 741 | gunzip_bk = k_dynamic; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 742 | |
| 743 | /* build the decoding tables for literal/length and distance codes */ |
| 744 | bl = lbits; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 745 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 746 | if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0) { |
| 747 | if (i == 1) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 748 | bb_error_msg_and_die("Incomplete literal tree"); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 749 | huft_free(tl); |
| 750 | } |
| 751 | return i; /* incomplete code set */ |
| 752 | } |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 753 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 754 | bd = dbits; |
| 755 | if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0) { |
| 756 | if (i == 1) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 757 | bb_error_msg_and_die("incomplete distance tree"); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 758 | huft_free(td); |
| 759 | } |
| 760 | huft_free(tl); |
| 761 | return i; /* incomplete code set */ |
| 762 | } |
| 763 | |
| 764 | /* decompress until an end-of-block code */ |
Glenn L McGrath | 0126fda | 2002-11-20 06:46:46 +0000 | [diff] [blame] | 765 | inflate_codes(tl, td, bl, bd, 1); // Setup inflate_codes |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 766 | |
Glenn L McGrath | 0126fda | 2002-11-20 06:46:46 +0000 | [diff] [blame] | 767 | /* huft_free code moved into inflate_codes */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 768 | |
Glenn L McGrath | 0126fda | 2002-11-20 06:46:46 +0000 | [diff] [blame] | 769 | return -2; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 770 | } |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 771 | default: |
| 772 | /* bad block type */ |
Denis Vlasenko | 6d655be | 2006-09-06 19:02:46 +0000 | [diff] [blame] | 773 | bb_error_msg_and_die("bad block type %d", t); |
Glenn L McGrath | 83bf47c | 2002-11-20 22:00:31 +0000 | [diff] [blame] | 774 | } |
| 775 | } |
| 776 | |
| 777 | static void calculate_gunzip_crc(void) |
| 778 | { |
| 779 | int n; |
| 780 | for (n = 0; n < gunzip_outbuf_count; n++) { |
| 781 | gunzip_crc = gunzip_crc_table[((int) gunzip_crc ^ (gunzip_window[n])) & 0xff] ^ (gunzip_crc >> 8); |
| 782 | } |
| 783 | gunzip_bytes_out += gunzip_outbuf_count; |
| 784 | } |
| 785 | |
| 786 | static int inflate_get_next_window(void) |
| 787 | { |
Glenn L McGrath | 83bf47c | 2002-11-20 22:00:31 +0000 | [diff] [blame] | 788 | static int method = -1; // Method == -1 for stored, -2 for codes |
| 789 | static int e = 0; |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 790 | static int needAnotherBlock = 1; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 791 | |
Glenn L McGrath | 83bf47c | 2002-11-20 22:00:31 +0000 | [diff] [blame] | 792 | gunzip_outbuf_count = 0; |
| 793 | |
| 794 | while(1) { |
| 795 | int ret; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 796 | |
Glenn L McGrath | 83bf47c | 2002-11-20 22:00:31 +0000 | [diff] [blame] | 797 | if (needAnotherBlock) { |
Glenn L McGrath | eda4f53 | 2002-11-24 06:01:20 +0000 | [diff] [blame] | 798 | if(e) { |
| 799 | calculate_gunzip_crc(); |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 800 | e = 0; |
| 801 | needAnotherBlock = 1; |
Glenn L McGrath | eda4f53 | 2002-11-24 06:01:20 +0000 | [diff] [blame] | 802 | return 0; |
| 803 | } // Last block |
Glenn L McGrath | 83bf47c | 2002-11-20 22:00:31 +0000 | [diff] [blame] | 804 | method = inflate_block(&e); |
| 805 | needAnotherBlock = 0; |
| 806 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 807 | |
Glenn L McGrath | 83bf47c | 2002-11-20 22:00:31 +0000 | [diff] [blame] | 808 | switch (method) { |
Denis Vlasenko | 3038ac9 | 2006-09-30 19:37:25 +0000 | [diff] [blame] | 809 | case -1: ret = inflate_stored(0,0,0,0); |
| 810 | break; |
| 811 | case -2: ret = inflate_codes(0,0,0,0,0); |
| 812 | break; |
| 813 | default: bb_error_msg_and_die("inflate error %d", method); |
Glenn L McGrath | 83bf47c | 2002-11-20 22:00:31 +0000 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | if (ret == 1) { |
| 817 | calculate_gunzip_crc(); |
| 818 | return 1; // More data left |
| 819 | } else needAnotherBlock = 1; // End of that block |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 820 | } |
Glenn L McGrath | eda4f53 | 2002-11-24 06:01:20 +0000 | [diff] [blame] | 821 | /* Doesnt get here */ |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 822 | } |
| 823 | |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 824 | /* Initialise bytebuffer, be careful not to overfill the buffer */ |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 825 | void inflate_init(unsigned int bufsize) |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 826 | { |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 827 | /* Set the bytebuffer size, default is same as gunzip_wsize */ |
| 828 | bytebuffer_max = bufsize + 8; |
| 829 | bytebuffer_offset = 4; |
| 830 | bytebuffer_size = 0; |
Glenn L McGrath | fd73b8c | 2002-11-17 21:33:30 +0000 | [diff] [blame] | 831 | } |
| 832 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 833 | void inflate_cleanup(void) |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 834 | { |
| 835 | free(bytebuffer); |
| 836 | } |
| 837 | |
Denis Vlasenko | 97a8dd3 | 2006-10-01 15:55:11 +0000 | [diff] [blame^] | 838 | USE_DESKTOP(long long) int |
| 839 | inflate_unzip(int in, int out) |
Glenn L McGrath | fd73b8c | 2002-11-17 21:33:30 +0000 | [diff] [blame] | 840 | { |
Denis Vlasenko | 97a8dd3 | 2006-10-01 15:55:11 +0000 | [diff] [blame^] | 841 | USE_DESKTOP(long long total = 0;) |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 842 | ssize_t nwrote; |
Glenn L McGrath | fd73b8c | 2002-11-17 21:33:30 +0000 | [diff] [blame] | 843 | typedef void (*sig_type) (int); |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 844 | |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 845 | /* Allocate all global buffers (for DYN_ALLOC option) */ |
Glenn L McGrath | fd73b8c | 2002-11-17 21:33:30 +0000 | [diff] [blame] | 846 | gunzip_window = xmalloc(gunzip_wsize); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 847 | gunzip_outbuf_count = 0; |
| 848 | gunzip_bytes_out = 0; |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 849 | gunzip_src_fd = in; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 850 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 851 | /* initialize gunzip_window, bit buffer */ |
| 852 | gunzip_bk = 0; |
| 853 | gunzip_bb = 0; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 854 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 855 | /* Create the crc table */ |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 856 | gunzip_crc_table = crc32_filltable(0); |
Rob Landley | c57ec37 | 2006-04-10 17:07:15 +0000 | [diff] [blame] | 857 | gunzip_crc = ~0; |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 858 | |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 859 | /* Allocate space for buffer */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 860 | bytebuffer = xmalloc(bytebuffer_max); |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 861 | |
| 862 | while(1) { |
| 863 | int ret = inflate_get_next_window(); |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 864 | nwrote = full_write(out, gunzip_window, gunzip_outbuf_count); |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 865 | if (nwrote == -1) { |
| 866 | bb_perror_msg("write"); |
| 867 | return -1; |
| 868 | } |
Denis Vlasenko | 97a8dd3 | 2006-10-01 15:55:11 +0000 | [diff] [blame^] | 869 | USE_DESKTOP(total += nwrote;) |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 870 | if (ret == 0) break; |
| 871 | } |
| 872 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 873 | /* Cleanup */ |
Glenn L McGrath | fd73b8c | 2002-11-17 21:33:30 +0000 | [diff] [blame] | 874 | free(gunzip_window); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 875 | free(gunzip_crc_table); |
| 876 | |
| 877 | /* 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] | 878 | if (gunzip_bk >= 8) { |
| 879 | /* Undo too much lookahead. The next read will be byte aligned |
| 880 | * so we can discard unused bits in the last meaningful byte. */ |
Glenn L McGrath | eda4f53 | 2002-11-24 06:01:20 +0000 | [diff] [blame] | 881 | bytebuffer_offset--; |
| 882 | bytebuffer[bytebuffer_offset] = gunzip_bb & 0xff; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 883 | gunzip_bb >>= 8; |
| 884 | gunzip_bk -= 8; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 885 | } |
Denis Vlasenko | 97a8dd3 | 2006-10-01 15:55:11 +0000 | [diff] [blame^] | 886 | return USE_DESKTOP(total) + 0; |
Glenn L McGrath | 7fd9294 | 2001-04-11 03:11:33 +0000 | [diff] [blame] | 887 | } |
Glenn L McGrath | cc61692 | 2003-02-09 04:46:34 +0000 | [diff] [blame] | 888 | |
Denis Vlasenko | 97a8dd3 | 2006-10-01 15:55:11 +0000 | [diff] [blame^] | 889 | USE_DESKTOP(long long) int |
| 890 | inflate_gunzip(int in, int out) |
Glenn L McGrath | cc61692 | 2003-02-09 04:46:34 +0000 | [diff] [blame] | 891 | { |
Rob Landley | c57ec37 | 2006-04-10 17:07:15 +0000 | [diff] [blame] | 892 | uint32_t stored_crc = 0; |
Rob Landley | 29ee76c | 2005-08-31 22:03:15 +0000 | [diff] [blame] | 893 | unsigned int count; |
Denis Vlasenko | 97a8dd3 | 2006-10-01 15:55:11 +0000 | [diff] [blame^] | 894 | USE_DESKTOP(long long total = )inflate_unzip(in, out); |
Glenn L McGrath | cc61692 | 2003-02-09 04:46:34 +0000 | [diff] [blame] | 895 | |
Denis Vlasenko | 97a8dd3 | 2006-10-01 15:55:11 +0000 | [diff] [blame^] | 896 | USE_DESKTOP(if (total < 0) return total;) |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 897 | |
Glenn L McGrath | cc61692 | 2003-02-09 04:46:34 +0000 | [diff] [blame] | 898 | /* top up the input buffer with the rest of the trailer */ |
| 899 | count = bytebuffer_size - bytebuffer_offset; |
| 900 | if (count < 8) { |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 901 | xread(in, &bytebuffer[bytebuffer_size], 8 - count); |
Glenn L McGrath | cc61692 | 2003-02-09 04:46:34 +0000 | [diff] [blame] | 902 | bytebuffer_size += 8 - count; |
| 903 | } |
| 904 | for (count = 0; count != 4; count++) { |
| 905 | stored_crc |= (bytebuffer[bytebuffer_offset] << (count * 8)); |
| 906 | bytebuffer_offset++; |
| 907 | } |
| 908 | |
| 909 | /* Validate decompression - crc */ |
Rob Landley | c57ec37 | 2006-04-10 17:07:15 +0000 | [diff] [blame] | 910 | if (stored_crc != (~gunzip_crc)) { |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 911 | bb_error_msg("crc error"); |
Rob Landley | 9e4100b | 2006-01-10 06:19:56 +0000 | [diff] [blame] | 912 | return -1; |
Glenn L McGrath | cc61692 | 2003-02-09 04:46:34 +0000 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | /* Validate decompression - size */ |
| 916 | if (gunzip_bytes_out != |
| 917 | (bytebuffer[bytebuffer_offset] | (bytebuffer[bytebuffer_offset+1] << 8) | |
| 918 | (bytebuffer[bytebuffer_offset+2] << 16) | (bytebuffer[bytebuffer_offset+3] << 24))) { |
Denis Vlasenko | 3038ac9 | 2006-09-30 19:37:25 +0000 | [diff] [blame] | 919 | bb_error_msg("incorrect length"); |
Rob Landley | 9e4100b | 2006-01-10 06:19:56 +0000 | [diff] [blame] | 920 | return -1; |
Glenn L McGrath | cc61692 | 2003-02-09 04:46:34 +0000 | [diff] [blame] | 921 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 922 | |
Denis Vlasenko | 97a8dd3 | 2006-10-01 15:55:11 +0000 | [diff] [blame^] | 923 | return USE_DESKTOP(total) + 0; |
Glenn L McGrath | cc61692 | 2003-02-09 04:46:34 +0000 | [diff] [blame] | 924 | } |