debuggers.hg

annotate xen/common/inflate.c @ 22848:6341fe0f4e5a

Added tag 4.1.0-rc2 for changeset 9dca60d88c63
author Keir Fraser <keir@xen.org>
date Tue Jan 25 14:06:55 2011 +0000 (2011-01-25)
parents 9b0289a165eb
children
rev   line source
keir@19113 1 #define DEBG(x)
keir@19113 2 #define DEBG1(x)
keir@19113 3 /* inflate.c -- Not copyrighted 1992 by Mark Adler
keir@19113 4 version c10p1, 10 January 1993 */
keir@19113 5
keir@19113 6 /*
keir@19113 7 * Adapted for booting Linux by Hannu Savolainen 1993
keir@19113 8 * based on gzip-1.0.3
keir@19113 9 *
keir@19113 10 * Nicolas Pitre <nico@cam.org>, 1999/04/14 :
keir@19113 11 * Little mods for all variable to reside either into rodata or bss segments
keir@19113 12 * by marking constant variables with 'const' and initializing all the others
keir@19113 13 * at run-time only. This allows for the kernel uncompressor to run
keir@19113 14 * directly from Flash or ROM memory on embedded systems.
keir@19113 15 */
keir@19113 16
keir@19113 17 /*
keir@19113 18 Inflate deflated (PKZIP's method 8 compressed) data. The compression
keir@19113 19 method searches for as much of the current string of bytes (up to a
keir@19113 20 length of 258) in the previous 32 K bytes. If it doesn't find any
keir@19113 21 matches (of at least length 3), it codes the next byte. Otherwise, it
keir@19113 22 codes the length of the matched string and its distance backwards from
keir@19113 23 the current position. There is a single Huffman code that codes both
keir@19113 24 single bytes (called "literals") and match lengths. A second Huffman
keir@19113 25 code codes the distance information, which follows a length code. Each
keir@19113 26 length or distance code actually represents a base value and a number
keir@19113 27 of "extra" (sometimes zero) bits to get to add to the base value. At
keir@19113 28 the end of each deflated block is a special end-of-block (EOB) literal/
keir@19113 29 length code. The decoding process is basically: get a literal/length
keir@19113 30 code; if EOB then done; if a literal, emit the decoded byte; if a
keir@19113 31 length then get the distance and emit the referred-to bytes from the
keir@19113 32 sliding window of previously emitted data.
keir@19113 33
keir@19113 34 There are (currently) three kinds of inflate blocks: stored, fixed, and
keir@19113 35 dynamic. The compressor deals with some chunk of data at a time, and
keir@19113 36 decides which method to use on a chunk-by-chunk basis. A chunk might
keir@19113 37 typically be 32 K or 64 K. If the chunk is incompressible, then the
keir@19113 38 "stored" method is used. In this case, the bytes are simply stored as
keir@19113 39 is, eight bits per byte, with none of the above coding. The bytes are
keir@19113 40 preceded by a count, since there is no longer an EOB code.
keir@19113 41
keir@19113 42 If the data is compressible, then either the fixed or dynamic methods
keir@19113 43 are used. In the dynamic method, the compressed data is preceded by
keir@19113 44 an encoding of the literal/length and distance Huffman codes that are
keir@19113 45 to be used to decode this block. The representation is itself Huffman
keir@19113 46 coded, and so is preceded by a description of that code. These code
keir@19113 47 descriptions take up a little space, and so for small blocks, there is
keir@19113 48 a predefined set of codes, called the fixed codes. The fixed method is
keir@19113 49 used if the block codes up smaller that way (usually for quite small
keir@19113 50 chunks), otherwise the dynamic method is used. In the latter case, the
keir@19113 51 codes are customized to the probabilities in the current block, and so
keir@19113 52 can code it much better than the pre-determined fixed codes.
keir@19113 53
keir@19113 54 The Huffman codes themselves are decoded using a multi-level table
keir@19113 55 lookup, in order to maximize the speed of decoding plus the speed of
keir@19113 56 building the decoding tables. See the comments below that precede the
keir@19113 57 lbits and dbits tuning parameters.
keir@19113 58 */
keir@19113 59
keir@19113 60
keir@19113 61 /*
keir@19113 62 Notes beyond the 1.93a appnote.txt:
keir@19113 63
keir@19113 64 1. Distance pointers never point before the beginning of the output
keir@19113 65 stream.
keir@19113 66 2. Distance pointers can point back across blocks, up to 32k away.
keir@19113 67 3. There is an implied maximum of 7 bits for the bit length table and
keir@19113 68 15 bits for the actual data.
keir@19113 69 4. If only one code exists, then it is encoded using one bit. (Zero
keir@19113 70 would be more efficient, but perhaps a little confusing.) If two
keir@19113 71 codes exist, they are coded using one bit each (0 and 1).
keir@19113 72 5. There is no way of sending zero distance codes--a dummy must be
keir@19113 73 sent if there are none. (History: a pre 2.0 version of PKZIP would
keir@19113 74 store blocks with no distance codes, but this was discovered to be
keir@19113 75 too harsh a criterion.) Valid only for 1.93a. 2.04c does allow
keir@19113 76 zero distance codes, which is sent as one code of zero bits in
keir@19113 77 length.
keir@19113 78 6. There are up to 286 literal/length codes. Code 256 represents the
keir@19113 79 end-of-block. Note however that the static length tree defines
keir@19113 80 288 codes just to fill out the Huffman codes. Codes 286 and 287
keir@19113 81 cannot be used though, since there is no length base or extra bits
keir@19113 82 defined for them. Similarly, there are up to 30 distance codes.
keir@19113 83 However, static trees define 32 codes (all 5 bits) to fill out the
keir@19113 84 Huffman codes, but the last two had better not show up in the data.
keir@19113 85 7. Unzip can check dynamic Huffman blocks for complete code sets.
keir@19113 86 The exception is that a single code would not be complete (see #4).
keir@19113 87 8. The five bits following the block type is really the number of
keir@19113 88 literal codes sent minus 257.
keir@19113 89 9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
keir@19113 90 (1+6+6). Therefore, to output three times the length, you output
keir@19113 91 three codes (1+1+1), whereas to output four times the same length,
keir@19113 92 you only need two codes (1+3). Hmm.
keir@19113 93 10. In the tree reconstruction algorithm, Code = Code + Increment
keir@19113 94 only if BitLength(i) is not zero. (Pretty obvious.)
keir@19113 95 11. Correction: 4 Bits: # of Bit Length codes - 4 (4 - 19)
keir@19113 96 12. Note: length code 284 can represent 227-258, but length code 285
keir@19113 97 really is 258. The last length deserves its own, short code
keir@19113 98 since it gets used a lot in very redundant files. The length
keir@19113 99 258 is special since 258 - 3 (the min match length) is 255.
keir@19113 100 13. The literal/length and distance code bit lengths are read as a
keir@19113 101 single stream of lengths. It is possible (and advantageous) for
keir@19113 102 a repeat code (16, 17, or 18) to go across the boundary between
keir@19113 103 the two sets of lengths.
keir@19113 104 */
keir@19113 105
keir@19113 106 #ifdef RCSID
keir@19113 107 static char rcsid[] = "#Id: inflate.c,v 0.14 1993/06/10 13:27:04 jloup Exp #";
keir@19113 108 #endif
keir@19113 109
keir@19113 110 #ifndef STATIC
keir@19113 111
keir@19113 112 #if defined(STDC_HEADERS) || defined(HAVE_STDLIB_H)
keir@19113 113 # include <sys/types.h>
keir@19113 114 # include <stdlib.h>
keir@19113 115 #endif
keir@19113 116
keir@19113 117 #include "gzip.h"
keir@19113 118 #define STATIC
keir@19113 119 #endif /* !STATIC */
keir@19113 120
keir@19113 121 #ifndef INIT
keir@19113 122 #define INIT
keir@19113 123 #endif
keir@19113 124
keir@19113 125 #define slide window
keir@19113 126
keir@19113 127 /* Huffman code lookup table entry--this entry is four bytes for machines
keir@19113 128 that have 16-bit pointers (e.g. PC's in the small or medium model).
keir@19113 129 Valid extra bits are 0..13. e == 15 is EOB (end of block), e == 16
keir@19113 130 means that v is a literal, 16 < e < 32 means that v is a pointer to
keir@19113 131 the next table, which codes e - 16 bits, and lastly e == 99 indicates
keir@19113 132 an unused code. If a code with e == 99 is looked up, this implies an
keir@19113 133 error in the data. */
keir@19113 134 struct huft {
keir@19113 135 uch e; /* number of extra bits or operation */
keir@19113 136 uch b; /* number of bits in this code or subcode */
keir@19113 137 union {
keir@19113 138 ush n; /* literal, length base, or distance base */
keir@19113 139 struct huft *t; /* pointer to next level of table */
keir@19113 140 } v;
keir@19113 141 };
keir@19113 142
keir@19113 143
keir@19113 144 /* Function prototypes */
keir@19113 145 STATIC int INIT huft_build OF((unsigned *, unsigned, unsigned,
keir@19113 146 const ush *, const ush *, struct huft **, int *));
keir@19113 147 STATIC int INIT huft_free OF((struct huft *));
keir@19113 148 STATIC int INIT inflate_codes OF((struct huft *, struct huft *, int, int));
keir@19113 149 STATIC int INIT inflate_stored OF((void));
keir@19113 150 STATIC int INIT inflate_fixed OF((void));
keir@19113 151 STATIC int INIT inflate_dynamic OF((void));
keir@19113 152 STATIC int INIT inflate_block OF((int *));
keir@19113 153 STATIC int INIT inflate OF((void));
keir@19113 154
keir@19113 155
keir@19113 156 /* The inflate algorithm uses a sliding 32 K byte window on the uncompressed
keir@19113 157 stream to find repeated byte strings. This is implemented here as a
keir@19113 158 circular buffer. The index is updated simply by incrementing and then
keir@19113 159 ANDing with 0x7fff (32K-1). */
keir@19113 160 /* It is left to other modules to supply the 32 K area. It is assumed
keir@19113 161 to be usable as if it were declared "uch slide[32768];" or as just
keir@19113 162 "uch *slide;" and then malloc'ed in the latter case. The definition
keir@19113 163 must be in unzip.h, included above. */
keir@19113 164 /* unsigned wp; current position in slide */
keir@19113 165 #define wp outcnt
keir@19113 166 #define flush_output(w) (wp=(w),flush_window())
keir@19113 167
keir@19113 168 /* Tables for deflate from PKZIP's appnote.txt. */
keir@19113 169 static const unsigned border[] = { /* Order of the bit length code lengths */
keir@19113 170 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
keir@19113 171 static const ush cplens[] = { /* Copy lengths for literal codes 257..285 */
keir@19113 172 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
keir@19113 173 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
keir@19113 174 /* note: see note #13 above about the 258 in this list. */
keir@19113 175 static const ush cplext[] = { /* Extra bits for literal codes 257..285 */
keir@19113 176 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
keir@19113 177 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */
keir@19113 178 static const ush cpdist[] = { /* Copy offsets for distance codes 0..29 */
keir@19113 179 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
keir@19113 180 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
keir@19113 181 8193, 12289, 16385, 24577};
keir@19113 182 static const ush cpdext[] = { /* Extra bits for distance codes */
keir@19113 183 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
keir@19113 184 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
keir@19113 185 12, 12, 13, 13};
keir@19113 186
keir@19113 187
keir@19113 188
keir@19113 189 /* Macros for inflate() bit peeking and grabbing.
keir@19113 190 The usage is:
keir@19113 191
keir@19113 192 NEEDBITS(j)
keir@19113 193 x = b & mask_bits[j];
keir@19113 194 DUMPBITS(j)
keir@19113 195
keir@19113 196 where NEEDBITS makes sure that b has at least j bits in it, and
keir@19113 197 DUMPBITS removes the bits from b. The macros use the variable k
keir@19113 198 for the number of bits in b. Normally, b and k are register
keir@19113 199 variables for speed, and are initialized at the beginning of a
keir@19113 200 routine that uses these macros from a global bit buffer and count.
keir@19113 201
keir@19113 202 If we assume that EOB will be the longest code, then we will never
keir@19113 203 ask for bits with NEEDBITS that are beyond the end of the stream.
keir@19113 204 So, NEEDBITS should not read any more bytes than are needed to
keir@19113 205 meet the request. Then no bytes need to be "returned" to the buffer
keir@19113 206 at the end of the last block.
keir@19113 207
keir@19113 208 However, this assumption is not true for fixed blocks--the EOB code
keir@19113 209 is 7 bits, but the other literal/length codes can be 8 or 9 bits.
keir@19113 210 (The EOB code is shorter than other codes because fixed blocks are
keir@19113 211 generally short. So, while a block always has an EOB, many other
keir@19113 212 literal/length codes have a significantly lower probability of
keir@19113 213 showing up at all.) However, by making the first table have a
keir@19113 214 lookup of seven bits, the EOB code will be found in that first
keir@19113 215 lookup, and so will not require that too many bits be pulled from
keir@19113 216 the stream.
keir@19113 217 */
keir@19113 218
keir@19113 219 STATIC ulg bb; /* bit buffer */
keir@19113 220 STATIC unsigned bk; /* bits in bit buffer */
keir@19113 221
keir@19113 222 STATIC const ush mask_bits[] = {
keir@19113 223 0x0000,
keir@19113 224 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
keir@19113 225 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
keir@19113 226 };
keir@19113 227
keir@19113 228 #define NEXTBYTE() ({ int v = get_byte(); if (v < 0) goto underrun; (uch)v; })
keir@19113 229 #define NEEDBITS(n) {while(k<(n)){b|=((ulg)NEXTBYTE())<<k;k+=8;}}
keir@19113 230 #define DUMPBITS(n) {b>>=(n);k-=(n);}
keir@19113 231
keir@19113 232 #ifndef NO_INFLATE_MALLOC
keir@19113 233 /* A trivial malloc implementation, adapted from
keir@19113 234 * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
keir@19113 235 */
keir@19113 236
keir@19113 237 static unsigned long malloc_ptr;
keir@19113 238 static int malloc_count;
keir@19113 239
keir@19113 240 static void *malloc(int size)
keir@19113 241 {
keir@19113 242 void *p;
keir@19113 243
keir@19113 244 if (size < 0)
keir@19113 245 error("Malloc error");
keir@19113 246 if (!malloc_ptr)
keir@19113 247 malloc_ptr = free_mem_ptr;
keir@19113 248
keir@19113 249 malloc_ptr = (malloc_ptr + 3) & ~3; /* Align */
keir@19113 250
keir@19113 251 p = (void *)malloc_ptr;
keir@19113 252 malloc_ptr += size;
keir@19113 253
keir@19113 254 if (free_mem_end_ptr && malloc_ptr >= free_mem_end_ptr)
keir@19113 255 error("Out of memory");
keir@19113 256
keir@19113 257 malloc_count++;
keir@19113 258 return p;
keir@19113 259 }
keir@19113 260
keir@19113 261 static void free(void *where)
keir@19113 262 {
keir@19113 263 malloc_count--;
keir@19113 264 if (!malloc_count)
keir@19113 265 malloc_ptr = free_mem_ptr;
keir@19113 266 }
keir@19113 267 #else
keir@19113 268 #define malloc(a) kmalloc(a, GFP_KERNEL)
keir@19113 269 #define free(a) kfree(a)
keir@19113 270 #endif
keir@19113 271
keir@19113 272 /*
keir@19113 273 Huffman code decoding is performed using a multi-level table lookup.
keir@19113 274 The fastest way to decode is to simply build a lookup table whose
keir@19113 275 size is determined by the longest code. However, the time it takes
keir@19113 276 to build this table can also be a factor if the data being decoded
keir@19113 277 is not very long. The most common codes are necessarily the
keir@19113 278 shortest codes, so those codes dominate the decoding time, and hence
keir@19113 279 the speed. The idea is you can have a shorter table that decodes the
keir@19113 280 shorter, more probable codes, and then point to subsidiary tables for
keir@19113 281 the longer codes. The time it costs to decode the longer codes is
keir@19113 282 then traded against the time it takes to make longer tables.
keir@19113 283
keir@19113 284 This results of this trade are in the variables lbits and dbits
keir@19113 285 below. lbits is the number of bits the first level table for literal/
keir@19113 286 length codes can decode in one step, and dbits is the same thing for
keir@19113 287 the distance codes. Subsequent tables are also less than or equal to
keir@19113 288 those sizes. These values may be adjusted either when all of the
keir@19113 289 codes are shorter than that, in which case the longest code length in
keir@19113 290 bits is used, or when the shortest code is *longer* than the requested
keir@19113 291 table size, in which case the length of the shortest code in bits is
keir@19113 292 used.
keir@19113 293
keir@19113 294 There are two different values for the two tables, since they code a
keir@19113 295 different number of possibilities each. The literal/length table
keir@19113 296 codes 286 possible values, or in a flat code, a little over eight
keir@19113 297 bits. The distance table codes 30 possible values, or a little less
keir@19113 298 than five bits, flat. The optimum values for speed end up being
keir@19113 299 about one bit more than those, so lbits is 8+1 and dbits is 5+1.
keir@19113 300 The optimum values may differ though from machine to machine, and
keir@19113 301 possibly even between compilers. Your mileage may vary.
keir@19113 302 */
keir@19113 303
keir@19113 304
keir@19113 305 STATIC const int lbits = 9; /* bits in base literal/length lookup table */
keir@19113 306 STATIC const int dbits = 6; /* bits in base distance lookup table */
keir@19113 307
keir@19113 308
keir@19113 309 /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
keir@19113 310 #define BMAX 16 /* maximum bit length of any code (16 for explode) */
keir@19113 311 #define N_MAX 288 /* maximum number of codes in any set */
keir@19113 312
keir@19113 313
keir@19113 314 STATIC unsigned hufts; /* track memory usage */
keir@19113 315
keir@19113 316
keir@19113 317 STATIC int INIT huft_build(
keir@19113 318 unsigned *b, /* code lengths in bits (all assumed <= BMAX) */
keir@19113 319 unsigned n, /* number of codes (assumed <= N_MAX) */
keir@19113 320 unsigned s, /* number of simple-valued codes (0..s-1) */
keir@19113 321 const ush *d, /* list of base values for non-simple codes */
keir@19113 322 const ush *e, /* list of extra bits for non-simple codes */
keir@19113 323 struct huft **t, /* result: starting table */
keir@19113 324 int *m /* maximum lookup bits, returns actual */
keir@19113 325 )
keir@19113 326 /* Given a list of code lengths and a maximum table size, make a set of
keir@19113 327 tables to decode that set of codes. Return zero on success, one if
keir@19113 328 the given code set is incomplete (the tables are still built in this
keir@19113 329 case), two if the input is invalid (all zero length codes or an
keir@19113 330 oversubscribed set of lengths), and three if not enough memory. */
keir@19113 331 {
keir@19113 332 unsigned a; /* counter for codes of length k */
keir@19113 333 unsigned f; /* i repeats in table every f entries */
keir@19113 334 int g; /* maximum code length */
keir@19113 335 int h; /* table level */
keir@19113 336 register unsigned i; /* counter, current code */
keir@19113 337 register unsigned j; /* counter */
keir@19113 338 register int k; /* number of bits in current code */
keir@19113 339 int l; /* bits per table (returned in m) */
keir@19113 340 register unsigned *p; /* pointer into c[], b[], or v[] */
keir@19113 341 register struct huft *q; /* points to current table */
keir@19113 342 struct huft r; /* table entry for structure assignment */
keir@19113 343 register int w; /* bits before this table == (l * h) */
keir@19113 344 unsigned *xp; /* pointer into x */
keir@19113 345 int y; /* number of dummy codes added */
keir@19113 346 unsigned z; /* number of entries in current table */
keir@19113 347 struct {
keir@19113 348 unsigned c[BMAX+1]; /* bit length count table */
keir@19113 349 struct huft *u[BMAX]; /* table stack */
keir@19113 350 unsigned v[N_MAX]; /* values in order of bit length */
keir@19113 351 unsigned x[BMAX+1]; /* bit offsets, then code stack */
keir@19113 352 } *stk;
keir@19113 353 unsigned *c, *v, *x;
keir@19113 354 struct huft **u;
keir@19113 355 int ret;
keir@19113 356
keir@19113 357 DEBG("huft1 ");
keir@19113 358
keir@19113 359 stk = malloc(sizeof(*stk));
keir@19113 360 if (stk == NULL)
keir@19113 361 return 3; /* out of memory */
keir@19113 362
keir@19113 363 c = stk->c;
keir@19113 364 v = stk->v;
keir@19113 365 x = stk->x;
keir@19113 366 u = stk->u;
keir@19113 367
keir@19113 368 /* Generate counts for each bit length */
keir@19113 369 memzero(stk->c, sizeof(stk->c));
keir@19113 370 p = b; i = n;
keir@19113 371 do {
keir@19113 372 Tracecv(*p, (stderr, (n-i >= ' ' && n-i <= '~' ? "%c %d\n" : "0x%x %d\n"),
keir@19113 373 n-i, *p));
keir@19113 374 c[*p]++; /* assume all entries <= BMAX */
keir@19113 375 p++; /* Can't combine with above line (Solaris bug) */
keir@19113 376 } while (--i);
keir@19113 377 if (c[0] == n) /* null input--all zero length codes */
keir@19113 378 {
keir@19113 379 *t = (struct huft *)NULL;
keir@19113 380 *m = 0;
keir@19113 381 ret = 2;
keir@19113 382 goto out;
keir@19113 383 }
keir@19113 384
keir@19113 385 DEBG("huft2 ");
keir@19113 386
keir@19113 387 /* Find minimum and maximum length, bound *m by those */
keir@19113 388 l = *m;
keir@19113 389 for (j = 1; j <= BMAX; j++)
keir@19113 390 if (c[j])
keir@19113 391 break;
keir@19113 392 k = j; /* minimum code length */
keir@19113 393 if ((unsigned)l < j)
keir@19113 394 l = j;
keir@19113 395 for (i = BMAX; i; i--)
keir@19113 396 if (c[i])
keir@19113 397 break;
keir@19113 398 g = i; /* maximum code length */
keir@19113 399 if ((unsigned)l > i)
keir@19113 400 l = i;
keir@19113 401 *m = l;
keir@19113 402
keir@19113 403 DEBG("huft3 ");
keir@19113 404
keir@19113 405 /* Adjust last length count to fill out codes, if needed */
keir@19113 406 for (y = 1 << j; j < i; j++, y <<= 1)
keir@19113 407 if ((y -= c[j]) < 0) {
keir@19113 408 ret = 2; /* bad input: more codes than bits */
keir@19113 409 goto out;
keir@19113 410 }
keir@19113 411 if ((y -= c[i]) < 0) {
keir@19113 412 ret = 2;
keir@19113 413 goto out;
keir@19113 414 }
keir@19113 415 c[i] += y;
keir@19113 416
keir@19113 417 DEBG("huft4 ");
keir@19113 418
keir@19113 419 /* Generate starting offsets into the value table for each length */
keir@19113 420 x[1] = j = 0;
keir@19113 421 p = c + 1; xp = x + 2;
keir@19113 422 while (--i) { /* note that i == g from above */
keir@19113 423 *xp++ = (j += *p++);
keir@19113 424 }
keir@19113 425
keir@19113 426 DEBG("huft5 ");
keir@19113 427
keir@19113 428 /* Make a table of values in order of bit lengths */
keir@19113 429 p = b; i = 0;
keir@19113 430 do {
keir@19113 431 if ((j = *p++) != 0)
keir@19113 432 v[x[j]++] = i;
keir@19113 433 } while (++i < n);
keir@19113 434 n = x[g]; /* set n to length of v */
keir@19113 435
keir@19113 436 DEBG("h6 ");
keir@19113 437
keir@19113 438 /* Generate the Huffman codes and for each, make the table entries */
keir@19113 439 x[0] = i = 0; /* first Huffman code is zero */
keir@19113 440 p = v; /* grab values in bit order */
keir@19113 441 h = -1; /* no tables yet--level -1 */
keir@19113 442 w = -l; /* bits decoded == (l * h) */
keir@19113 443 u[0] = (struct huft *)NULL; /* just to keep compilers happy */
keir@19113 444 q = (struct huft *)NULL; /* ditto */
keir@19113 445 z = 0; /* ditto */
keir@19113 446 DEBG("h6a ");
keir@19113 447
keir@19113 448 /* go through the bit lengths (k already is bits in shortest code) */
keir@19113 449 for (; k <= g; k++)
keir@19113 450 {
keir@19113 451 DEBG("h6b ");
keir@19113 452 a = c[k];
keir@19113 453 while (a--)
keir@19113 454 {
keir@19113 455 DEBG("h6b1 ");
keir@19113 456 /* here i is the Huffman code of length k bits for value *p */
keir@19113 457 /* make tables up to required level */
keir@19113 458 while (k > w + l)
keir@19113 459 {
keir@19113 460 DEBG1("1 ");
keir@19113 461 h++;
keir@19113 462 w += l; /* previous table always l bits */
keir@19113 463
keir@19113 464 /* compute minimum size table less than or equal to l bits */
keir@19113 465 z = (z = g - w) > (unsigned)l ? l : z; /* upper limit on table size */
keir@19113 466 if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */
keir@19113 467 { /* too few codes for k-w bit table */
keir@19113 468 DEBG1("2 ");
keir@19113 469 f -= a + 1; /* deduct codes from patterns left */
keir@19113 470 xp = c + k;
keir@19113 471 if (j < z)
keir@19113 472 while (++j < z) /* try smaller tables up to z bits */
keir@19113 473 {
keir@19113 474 if ((f <<= 1) <= *++xp)
keir@19113 475 break; /* enough codes to use up j bits */
keir@19113 476 f -= *xp; /* else deduct codes from patterns */
keir@19113 477 }
keir@19113 478 }
keir@19113 479 DEBG1("3 ");
keir@19113 480 z = 1 << j; /* table entries for j-bit table */
keir@19113 481
keir@19113 482 /* allocate and link in new table */
keir@19113 483 if ((q = (struct huft *)malloc((z + 1)*sizeof(struct huft))) ==
keir@19113 484 (struct huft *)NULL)
keir@19113 485 {
keir@19113 486 if (h)
keir@19113 487 huft_free(u[0]);
keir@19113 488 ret = 3; /* not enough memory */
keir@19113 489 goto out;
keir@19113 490 }
keir@19113 491 DEBG1("4 ");
keir@19113 492 hufts += z + 1; /* track memory usage */
keir@19113 493 *t = q + 1; /* link to list for huft_free() */
keir@19113 494 *(t = &(q->v.t)) = (struct huft *)NULL;
keir@19113 495 u[h] = ++q; /* table starts after link */
keir@19113 496
keir@19113 497 DEBG1("5 ");
keir@19113 498 /* connect to last table, if there is one */
keir@19113 499 if (h)
keir@19113 500 {
keir@19113 501 x[h] = i; /* save pattern for backing up */
keir@19113 502 r.b = (uch)l; /* bits to dump before this table */
keir@19113 503 r.e = (uch)(16 + j); /* bits in this table */
keir@19113 504 r.v.t = q; /* pointer to this table */
keir@19113 505 j = i >> (w - l); /* (get around Turbo C bug) */
keir@19113 506 u[h-1][j] = r; /* connect to last table */
keir@19113 507 }
keir@19113 508 DEBG1("6 ");
keir@19113 509 }
keir@19113 510 DEBG("h6c ");
keir@19113 511
keir@19113 512 /* set up table entry in r */
keir@19113 513 r.b = (uch)(k - w);
keir@19113 514 if (p >= v + n)
keir@19113 515 r.e = 99; /* out of values--invalid code */
keir@19113 516 else if (*p < s)
keir@19113 517 {
keir@19113 518 r.e = (uch)(*p < 256 ? 16 : 15); /* 256 is end-of-block code */
keir@19113 519 r.v.n = (ush)(*p); /* simple code is just the value */
keir@19113 520 p++; /* one compiler does not like *p++ */
keir@19113 521 }
keir@19113 522 else
keir@19113 523 {
keir@19113 524 r.e = (uch)e[*p - s]; /* non-simple--look up in lists */
keir@19113 525 r.v.n = d[*p++ - s];
keir@19113 526 }
keir@19113 527 DEBG("h6d ");
keir@19113 528
keir@19113 529 /* fill code-like entries with r */
keir@19113 530 f = 1 << (k - w);
keir@19113 531 for (j = i >> w; j < z; j += f)
keir@19113 532 q[j] = r;
keir@19113 533
keir@19113 534 /* backwards increment the k-bit code i */
keir@19113 535 for (j = 1 << (k - 1); i & j; j >>= 1)
keir@19113 536 i ^= j;
keir@19113 537 i ^= j;
keir@19113 538
keir@19113 539 /* backup over finished tables */
keir@19113 540 while ((i & ((1 << w) - 1)) != x[h])
keir@19113 541 {
keir@19113 542 h--; /* don't need to update q */
keir@19113 543 w -= l;
keir@19113 544 }
keir@19113 545 DEBG("h6e ");
keir@19113 546 }
keir@19113 547 DEBG("h6f ");
keir@19113 548 }
keir@19113 549
keir@19113 550 DEBG("huft7 ");
keir@19113 551
keir@19113 552 /* Return true (1) if we were given an incomplete table */
keir@19113 553 ret = y != 0 && g != 1;
keir@19113 554
keir@19113 555 out:
keir@19113 556 free(stk);
keir@19113 557 return ret;
keir@19113 558 }
keir@19113 559
keir@19113 560
keir@19113 561
keir@19113 562 STATIC int INIT huft_free(
keir@19113 563 struct huft *t /* table to free */
keir@19113 564 )
keir@19113 565 /* Free the malloc'ed tables built by huft_build(), which makes a linked
keir@19113 566 list of the tables it made, with the links in a dummy first entry of
keir@19113 567 each table. */
keir@19113 568 {
keir@19113 569 register struct huft *p, *q;
keir@19113 570
keir@19113 571
keir@19113 572 /* Go through linked list, freeing from the malloced (t[-1]) address. */
keir@19113 573 p = t;
keir@19113 574 while (p != (struct huft *)NULL)
keir@19113 575 {
keir@19113 576 q = (--p)->v.t;
keir@19113 577 free((char*)p);
keir@19113 578 p = q;
keir@19113 579 }
keir@19113 580 return 0;
keir@19113 581 }
keir@19113 582
keir@19113 583
keir@19113 584 STATIC int INIT inflate_codes(
keir@19113 585 struct huft *tl, /* literal/length decoder tables */
keir@19113 586 struct huft *td, /* distance decoder tables */
keir@19113 587 int bl, /* number of bits decoded by tl[] */
keir@19113 588 int bd /* number of bits decoded by td[] */
keir@19113 589 )
keir@19113 590 /* inflate (decompress) the codes in a deflated (compressed) block.
keir@19113 591 Return an error code or zero if it all goes ok. */
keir@19113 592 {
keir@19113 593 register unsigned e; /* table entry flag/number of extra bits */
keir@19113 594 unsigned n, d; /* length and index for copy */
keir@19113 595 unsigned w; /* current window position */
keir@19113 596 struct huft *t; /* pointer to table entry */
keir@19113 597 unsigned ml, md; /* masks for bl and bd bits */
keir@19113 598 register ulg b; /* bit buffer */
keir@19113 599 register unsigned k; /* number of bits in bit buffer */
keir@19113 600
keir@19113 601
keir@19113 602 /* make local copies of globals */
keir@19113 603 b = bb; /* initialize bit buffer */
keir@19113 604 k = bk;
keir@19113 605 w = wp; /* initialize window position */
keir@19113 606
keir@19113 607 /* inflate the coded data */
keir@19113 608 ml = mask_bits[bl]; /* precompute masks for speed */
keir@19113 609 md = mask_bits[bd];
keir@19113 610 for (;;) /* do until end of block */
keir@19113 611 {
keir@19113 612 NEEDBITS((unsigned)bl)
keir@19113 613 if ((e = (t = tl + ((unsigned)b & ml))->e) > 16)
keir@19113 614 do {
keir@19113 615 if (e == 99)
keir@19113 616 return 1;
keir@19113 617 DUMPBITS(t->b)
keir@19113 618 e -= 16;
keir@19113 619 NEEDBITS(e)
keir@19113 620 } while ((e = (t = t->v.t + ((unsigned)b & mask_bits[e]))->e) > 16);
keir@19113 621 DUMPBITS(t->b)
keir@19113 622 if (e == 16) /* then it's a literal */
keir@19113 623 {
keir@19113 624 slide[w++] = (uch)t->v.n;
keir@19113 625 Tracevv((stderr, "%c", slide[w-1]));
keir@19113 626 if (w == WSIZE)
keir@19113 627 {
keir@19113 628 flush_output(w);
keir@19113 629 w = 0;
keir@19113 630 }
keir@19113 631 }
keir@19113 632 else /* it's an EOB or a length */
keir@19113 633 {
keir@19113 634 /* exit if end of block */
keir@19113 635 if (e == 15)
keir@19113 636 break;
keir@19113 637
keir@19113 638 /* get length of block to copy */
keir@19113 639 NEEDBITS(e)
keir@19113 640 n = t->v.n + ((unsigned)b & mask_bits[e]);
keir@19113 641 DUMPBITS(e);
keir@19113 642
keir@19113 643 /* decode distance of block to copy */
keir@19113 644 NEEDBITS((unsigned)bd)
keir@19113 645 if ((e = (t = td + ((unsigned)b & md))->e) > 16)
keir@19113 646 do {
keir@19113 647 if (e == 99)
keir@19113 648 return 1;
keir@19113 649 DUMPBITS(t->b)
keir@19113 650 e -= 16;
keir@19113 651 NEEDBITS(e)
keir@19113 652 } while ((e = (t = t->v.t + ((unsigned)b & mask_bits[e]))->e) > 16);
keir@19113 653 DUMPBITS(t->b)
keir@19113 654 NEEDBITS(e)
keir@19113 655 d = w - t->v.n - ((unsigned)b & mask_bits[e]);
keir@19113 656 DUMPBITS(e)
keir@19113 657 Tracevv((stderr,"\\[%d,%d]", w-d, n));
keir@19113 658
keir@19113 659 /* do the copy */
keir@19113 660 do {
keir@19113 661 n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e);
keir@19113 662 #if !defined(NOMEMCPY) && !defined(DEBUG)
keir@19113 663 if (w - d >= e) /* (this test assumes unsigned comparison) */
keir@19113 664 {
keir@19113 665 memcpy(slide + w, slide + d, e);
keir@19113 666 w += e;
keir@19113 667 d += e;
keir@19113 668 }
keir@19113 669 else /* do it slow to avoid memcpy() overlap */
keir@19113 670 #endif /* !NOMEMCPY */
keir@19113 671 do {
keir@19113 672 slide[w++] = slide[d++];
keir@19113 673 Tracevv((stderr, "%c", slide[w-1]));
keir@19113 674 } while (--e);
keir@19113 675 if (w == WSIZE)
keir@19113 676 {
keir@19113 677 flush_output(w);
keir@19113 678 w = 0;
keir@19113 679 }
keir@19113 680 } while (n);
keir@19113 681 }
keir@19113 682 }
keir@19113 683
keir@19113 684
keir@19113 685 /* restore the globals from the locals */
keir@19113 686 wp = w; /* restore global window pointer */
keir@19113 687 bb = b; /* restore global bit buffer */
keir@19113 688 bk = k;
keir@19113 689
keir@19113 690 /* done */
keir@19113 691 return 0;
keir@19113 692
keir@19113 693 underrun:
keir@19113 694 return 4; /* Input underrun */
keir@19113 695 }
keir@19113 696
keir@19113 697
keir@19113 698
keir@19113 699 STATIC int INIT inflate_stored(void)
keir@19113 700 /* "decompress" an inflated type 0 (stored) block. */
keir@19113 701 {
keir@19113 702 unsigned n; /* number of bytes in block */
keir@19113 703 unsigned w; /* current window position */
keir@19113 704 register ulg b; /* bit buffer */
keir@19113 705 register unsigned k; /* number of bits in bit buffer */
keir@19113 706
keir@19113 707 DEBG("<stor");
keir@19113 708
keir@19113 709 /* make local copies of globals */
keir@19113 710 b = bb; /* initialize bit buffer */
keir@19113 711 k = bk;
keir@19113 712 w = wp; /* initialize window position */
keir@19113 713
keir@19113 714
keir@19113 715 /* go to byte boundary */
keir@19113 716 n = k & 7;
keir@19113 717 DUMPBITS(n);
keir@19113 718
keir@19113 719
keir@19113 720 /* get the length and its complement */
keir@19113 721 NEEDBITS(16)
keir@19113 722 n = ((unsigned)b & 0xffff);
keir@19113 723 DUMPBITS(16)
keir@19113 724 NEEDBITS(16)
keir@19113 725 if (n != (unsigned)((~b) & 0xffff))
keir@19113 726 return 1; /* error in compressed data */
keir@19113 727 DUMPBITS(16)
keir@19113 728
keir@19113 729
keir@19113 730 /* read and output the compressed data */
keir@19113 731 while (n--)
keir@19113 732 {
keir@19113 733 NEEDBITS(8)
keir@19113 734 slide[w++] = (uch)b;
keir@19113 735 if (w == WSIZE)
keir@19113 736 {
keir@19113 737 flush_output(w);
keir@19113 738 w = 0;
keir@19113 739 }
keir@19113 740 DUMPBITS(8)
keir@19113 741 }
keir@19113 742
keir@19113 743
keir@19113 744 /* restore the globals from the locals */
keir@19113 745 wp = w; /* restore global window pointer */
keir@19113 746 bb = b; /* restore global bit buffer */
keir@19113 747 bk = k;
keir@19113 748
keir@19113 749 DEBG(">");
keir@19113 750 return 0;
keir@19113 751
keir@19113 752 underrun:
keir@19113 753 return 4; /* Input underrun */
keir@19113 754 }
keir@19113 755
keir@19113 756
keir@19113 757 /*
keir@19113 758 * We use `noinline' here to prevent gcc-3.5 from using too much stack space
keir@19113 759 */
keir@19113 760 STATIC int noinline INIT inflate_fixed(void)
keir@19113 761 /* decompress an inflated type 1 (fixed Huffman codes) block. We should
keir@19113 762 either replace this with a custom decoder, or at least precompute the
keir@19113 763 Huffman tables. */
keir@19113 764 {
keir@19113 765 int i; /* temporary variable */
keir@19113 766 struct huft *tl; /* literal/length code table */
keir@19113 767 struct huft *td; /* distance code table */
keir@19113 768 int bl; /* lookup bits for tl */
keir@19113 769 int bd; /* lookup bits for td */
keir@19113 770 unsigned *l; /* length list for huft_build */
keir@19113 771
keir@19113 772 DEBG("<fix");
keir@19113 773
keir@19113 774 l = malloc(sizeof(*l) * 288);
keir@19113 775 if (l == NULL)
keir@19113 776 return 3; /* out of memory */
keir@19113 777
keir@19113 778 /* set up literal table */
keir@19113 779 for (i = 0; i < 144; i++)
keir@19113 780 l[i] = 8;
keir@19113 781 for (; i < 256; i++)
keir@19113 782 l[i] = 9;
keir@19113 783 for (; i < 280; i++)
keir@19113 784 l[i] = 7;
keir@19113 785 for (; i < 288; i++) /* make a complete, but wrong code set */
keir@19113 786 l[i] = 8;
keir@19113 787 bl = 7;
keir@19113 788 if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0) {
keir@19113 789 free(l);
keir@19113 790 return i;
keir@19113 791 }
keir@19113 792
keir@19113 793 /* set up distance table */
keir@19113 794 for (i = 0; i < 30; i++) /* make an incomplete code set */
keir@19113 795 l[i] = 5;
keir@19113 796 bd = 5;
keir@19113 797 if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1)
keir@19113 798 {
keir@19113 799 huft_free(tl);
keir@19113 800 free(l);
keir@19113 801
keir@19113 802 DEBG(">");
keir@19113 803 return i;
keir@19113 804 }
keir@19113 805
keir@19113 806
keir@19113 807 /* decompress until an end-of-block code */
keir@19113 808 if (inflate_codes(tl, td, bl, bd)) {
keir@19113 809 free(l);
keir@19113 810 return 1;
keir@19113 811 }
keir@19113 812
keir@19113 813 /* free the decoding tables, return */
keir@19113 814 free(l);
keir@19113 815 huft_free(tl);
keir@19113 816 huft_free(td);
keir@19113 817 return 0;
keir@19113 818 }
keir@19113 819
keir@19113 820
keir@19113 821 /*
keir@19113 822 * We use `noinline' here to prevent gcc-3.5 from using too much stack space
keir@19113 823 */
keir@19113 824 STATIC int noinline INIT inflate_dynamic(void)
keir@19113 825 /* decompress an inflated type 2 (dynamic Huffman codes) block. */
keir@19113 826 {
keir@19113 827 int i; /* temporary variables */
keir@19113 828 unsigned j;
keir@19113 829 unsigned l; /* last length */
keir@19113 830 unsigned m; /* mask for bit lengths table */
keir@19113 831 unsigned n; /* number of lengths to get */
keir@19113 832 struct huft *tl; /* literal/length code table */
keir@19113 833 struct huft *td; /* distance code table */
keir@19113 834 int bl; /* lookup bits for tl */
keir@19113 835 int bd; /* lookup bits for td */
keir@19113 836 unsigned nb; /* number of bit length codes */
keir@19113 837 unsigned nl; /* number of literal/length codes */
keir@19113 838 unsigned nd; /* number of distance codes */
keir@19113 839 unsigned *ll; /* literal/length and distance code lengths */
keir@19113 840 register ulg b; /* bit buffer */
keir@19113 841 register unsigned k; /* number of bits in bit buffer */
keir@19113 842 int ret;
keir@19113 843
keir@19113 844 DEBG("<dyn");
keir@19113 845
keir@19113 846 #ifdef PKZIP_BUG_WORKAROUND
keir@19113 847 ll = malloc(sizeof(*ll) * (288+32)); /* literal/length and distance code lengths */
keir@19113 848 #else
keir@19113 849 ll = malloc(sizeof(*ll) * (286+30)); /* literal/length and distance code lengths */
keir@19113 850 #endif
keir@19113 851
keir@19113 852 if (ll == NULL)
keir@19113 853 return 1;
keir@19113 854
keir@19113 855 /* make local bit buffer */
keir@19113 856 b = bb;
keir@19113 857 k = bk;
keir@19113 858
keir@19113 859
keir@19113 860 /* read in table lengths */
keir@19113 861 NEEDBITS(5)
keir@19113 862 nl = 257 + ((unsigned)b & 0x1f); /* number of literal/length codes */
keir@19113 863 DUMPBITS(5)
keir@19113 864 NEEDBITS(5)
keir@19113 865 nd = 1 + ((unsigned)b & 0x1f); /* number of distance codes */
keir@19113 866 DUMPBITS(5)
keir@19113 867 NEEDBITS(4)
keir@19113 868 nb = 4 + ((unsigned)b & 0xf); /* number of bit length codes */
keir@19113 869 DUMPBITS(4)
keir@19113 870 #ifdef PKZIP_BUG_WORKAROUND
keir@19113 871 if (nl > 288 || nd > 32)
keir@19113 872 #else
keir@19113 873 if (nl > 286 || nd > 30)
keir@19113 874 #endif
keir@19113 875 {
keir@19113 876 ret = 1; /* bad lengths */
keir@19113 877 goto out;
keir@19113 878 }
keir@19113 879
keir@19113 880 DEBG("dyn1 ");
keir@19113 881
keir@19113 882 /* read in bit-length-code lengths */
keir@19113 883 for (j = 0; j < nb; j++)
keir@19113 884 {
keir@19113 885 NEEDBITS(3)
keir@19113 886 ll[border[j]] = (unsigned)b & 7;
keir@19113 887 DUMPBITS(3)
keir@19113 888 }
keir@19113 889 for (; j < 19; j++)
keir@19113 890 ll[border[j]] = 0;
keir@19113 891
keir@19113 892 DEBG("dyn2 ");
keir@19113 893
keir@19113 894 /* build decoding table for trees--single level, 7 bit lookup */
keir@19113 895 bl = 7;
keir@19113 896 if ((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl)) != 0)
keir@19113 897 {
keir@19113 898 if (i == 1)
keir@19113 899 huft_free(tl);
keir@19113 900 ret = i; /* incomplete code set */
keir@19113 901 goto out;
keir@19113 902 }
keir@19113 903
keir@19113 904 DEBG("dyn3 ");
keir@19113 905
keir@19113 906 /* read in literal and distance code lengths */
keir@19113 907 n = nl + nd;
keir@19113 908 m = mask_bits[bl];
keir@19113 909 i = l = 0;
keir@19113 910 while ((unsigned)i < n)
keir@19113 911 {
keir@19113 912 NEEDBITS((unsigned)bl)
keir@19113 913 j = (td = tl + ((unsigned)b & m))->b;
keir@19113 914 DUMPBITS(j)
keir@19113 915 j = td->v.n;
keir@19113 916 if (j < 16) /* length of code in bits (0..15) */
keir@19113 917 ll[i++] = l = j; /* save last length in l */
keir@19113 918 else if (j == 16) /* repeat last length 3 to 6 times */
keir@19113 919 {
keir@19113 920 NEEDBITS(2)
keir@19113 921 j = 3 + ((unsigned)b & 3);
keir@19113 922 DUMPBITS(2)
keir@19113 923 if ((unsigned)i + j > n) {
keir@19113 924 ret = 1;
keir@19113 925 goto out;
keir@19113 926 }
keir@19113 927 while (j--)
keir@19113 928 ll[i++] = l;
keir@19113 929 }
keir@19113 930 else if (j == 17) /* 3 to 10 zero length codes */
keir@19113 931 {
keir@19113 932 NEEDBITS(3)
keir@19113 933 j = 3 + ((unsigned)b & 7);
keir@19113 934 DUMPBITS(3)
keir@19113 935 if ((unsigned)i + j > n) {
keir@19113 936 ret = 1;
keir@19113 937 goto out;
keir@19113 938 }
keir@19113 939 while (j--)
keir@19113 940 ll[i++] = 0;
keir@19113 941 l = 0;
keir@19113 942 }
keir@19113 943 else /* j == 18: 11 to 138 zero length codes */
keir@19113 944 {
keir@19113 945 NEEDBITS(7)
keir@19113 946 j = 11 + ((unsigned)b & 0x7f);
keir@19113 947 DUMPBITS(7)
keir@19113 948 if ((unsigned)i + j > n) {
keir@19113 949 ret = 1;
keir@19113 950 goto out;
keir@19113 951 }
keir@19113 952 while (j--)
keir@19113 953 ll[i++] = 0;
keir@19113 954 l = 0;
keir@19113 955 }
keir@19113 956 }
keir@19113 957
keir@19113 958 DEBG("dyn4 ");
keir@19113 959
keir@19113 960 /* free decoding table for trees */
keir@19113 961 huft_free(tl);
keir@19113 962
keir@19113 963 DEBG("dyn5 ");
keir@19113 964
keir@19113 965 /* restore the global bit buffer */
keir@19113 966 bb = b;
keir@19113 967 bk = k;
keir@19113 968
keir@19113 969 DEBG("dyn5a ");
keir@19113 970
keir@19113 971 /* build the decoding tables for literal/length and distance codes */
keir@19113 972 bl = lbits;
keir@19113 973 if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0)
keir@19113 974 {
keir@19113 975 DEBG("dyn5b ");
keir@19113 976 if (i == 1) {
keir@19113 977 error("incomplete literal tree");
keir@19113 978 huft_free(tl);
keir@19113 979 }
keir@19113 980 ret = i; /* incomplete code set */
keir@19113 981 goto out;
keir@19113 982 }
keir@19113 983 DEBG("dyn5c ");
keir@19113 984 bd = dbits;
keir@19113 985 if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0)
keir@19113 986 {
keir@19113 987 DEBG("dyn5d ");
keir@19113 988 if (i == 1) {
keir@19113 989 error("incomplete distance tree");
keir@19113 990 #ifdef PKZIP_BUG_WORKAROUND
keir@19113 991 i = 0;
keir@19113 992 }
keir@19113 993 #else
keir@19113 994 huft_free(td);
keir@19113 995 }
keir@19113 996 huft_free(tl);
keir@19113 997 ret = i; /* incomplete code set */
keir@19113 998 goto out;
keir@19113 999 #endif
keir@19113 1000 }
keir@19113 1001
keir@19113 1002 DEBG("dyn6 ");
keir@19113 1003
keir@19113 1004 /* decompress until an end-of-block code */
keir@19113 1005 if (inflate_codes(tl, td, bl, bd)) {
keir@19113 1006 ret = 1;
keir@19113 1007 goto out;
keir@19113 1008 }
keir@19113 1009
keir@19113 1010 DEBG("dyn7 ");
keir@19113 1011
keir@19113 1012 /* free the decoding tables, return */
keir@19113 1013 huft_free(tl);
keir@19113 1014 huft_free(td);
keir@19113 1015
keir@19113 1016 DEBG(">");
keir@19113 1017 ret = 0;
keir@19113 1018 out:
keir@19113 1019 free(ll);
keir@19113 1020 return ret;
keir@19113 1021
keir@19113 1022 underrun:
keir@19113 1023 ret = 4; /* Input underrun */
keir@19113 1024 goto out;
keir@19113 1025 }
keir@19113 1026
keir@19113 1027
keir@19113 1028
keir@19113 1029 STATIC int INIT inflate_block(
keir@19113 1030 int *e /* last block flag */
keir@19113 1031 )
keir@19113 1032 /* decompress an inflated block */
keir@19113 1033 {
keir@19113 1034 unsigned t; /* block type */
keir@19113 1035 register ulg b; /* bit buffer */
keir@19113 1036 register unsigned k; /* number of bits in bit buffer */
keir@19113 1037
keir@19113 1038 DEBG("<blk");
keir@19113 1039
keir@19113 1040 /* make local bit buffer */
keir@19113 1041 b = bb;
keir@19113 1042 k = bk;
keir@19113 1043
keir@19113 1044
keir@19113 1045 /* read in last block bit */
keir@19113 1046 NEEDBITS(1)
keir@19113 1047 *e = (int)b & 1;
keir@19113 1048 DUMPBITS(1)
keir@19113 1049
keir@19113 1050
keir@19113 1051 /* read in block type */
keir@19113 1052 NEEDBITS(2)
keir@19113 1053 t = (unsigned)b & 3;
keir@19113 1054 DUMPBITS(2)
keir@19113 1055
keir@19113 1056
keir@19113 1057 /* restore the global bit buffer */
keir@19113 1058 bb = b;
keir@19113 1059 bk = k;
keir@19113 1060
keir@19113 1061 /* inflate that block type */
keir@19113 1062 if (t == 2)
keir@19113 1063 return inflate_dynamic();
keir@19113 1064 if (t == 0)
keir@19113 1065 return inflate_stored();
keir@19113 1066 if (t == 1)
keir@19113 1067 return inflate_fixed();
keir@19113 1068
keir@19113 1069 DEBG(">");
keir@19113 1070
keir@19113 1071 /* bad block type */
keir@19113 1072 return 2;
keir@19113 1073
keir@19113 1074 underrun:
keir@19113 1075 return 4; /* Input underrun */
keir@19113 1076 }
keir@19113 1077
keir@19113 1078
keir@19113 1079
keir@19113 1080 STATIC int INIT inflate(void)
keir@19113 1081 /* decompress an inflated entry */
keir@19113 1082 {
keir@19113 1083 int e; /* last block flag */
keir@19113 1084 int r; /* result code */
keir@19113 1085 unsigned h; /* maximum struct huft's malloc'ed */
keir@19113 1086
keir@19113 1087 /* initialize window, bit buffer */
keir@19113 1088 wp = 0;
keir@19113 1089 bk = 0;
keir@19113 1090 bb = 0;
keir@19113 1091
keir@19113 1092
keir@19113 1093 /* decompress until the last block */
keir@19113 1094 h = 0;
keir@19113 1095 do {
keir@19113 1096 hufts = 0;
keir@19113 1097 #ifdef ARCH_HAS_DECOMP_WDOG
keir@19113 1098 arch_decomp_wdog();
keir@19113 1099 #endif
keir@19113 1100 r = inflate_block(&e);
keir@19113 1101 if (r)
keir@19113 1102 return r;
keir@19113 1103 if (hufts > h)
keir@19113 1104 h = hufts;
keir@19113 1105 } while (!e);
keir@19113 1106
keir@19113 1107 /* Undo too much lookahead. The next read will be byte aligned so we
keir@19113 1108 * can discard unused bits in the last meaningful byte.
keir@19113 1109 */
keir@19113 1110 while (bk >= 8) {
keir@19113 1111 bk -= 8;
keir@19113 1112 inptr--;
keir@19113 1113 }
keir@19113 1114
keir@19113 1115 /* flush out slide */
keir@19113 1116 flush_output(wp);
keir@19113 1117
keir@19113 1118
keir@19113 1119 /* return success */
keir@19113 1120 #ifdef DEBUG
keir@19113 1121 fprintf(stderr, "<%u> ", h);
keir@19113 1122 #endif /* DEBUG */
keir@19113 1123 return 0;
keir@19113 1124 }
keir@19113 1125
keir@19113 1126 /**********************************************************************
keir@19113 1127 *
keir@19113 1128 * The following are support routines for inflate.c
keir@19113 1129 *
keir@19113 1130 **********************************************************************/
keir@19113 1131
keir@19113 1132 static ulg crc_32_tab[256];
keir@19113 1133 static ulg crc; /* initialized in makecrc() so it'll reside in bss */
keir@19113 1134 #define CRC_VALUE (crc ^ 0xffffffffUL)
keir@19113 1135
keir@19113 1136 /*
keir@19113 1137 * Code to compute the CRC-32 table. Borrowed from
keir@19113 1138 * gzip-1.0.3/makecrc.c.
keir@19113 1139 */
keir@19113 1140
keir@19113 1141 static void INIT
keir@19113 1142 makecrc(void)
keir@19113 1143 {
keir@19113 1144 /* Not copyrighted 1990 Mark Adler */
keir@19113 1145
keir@19113 1146 unsigned long c; /* crc shift register */
keir@19113 1147 unsigned long e; /* polynomial exclusive-or pattern */
keir@19113 1148 int i; /* counter for all possible eight bit values */
keir@19113 1149 int k; /* byte being shifted into crc apparatus */
keir@19113 1150
keir@19113 1151 /* terms of polynomial defining this crc (except x^32): */
keir@19113 1152 static const int p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
keir@19113 1153
keir@19113 1154 /* Make exclusive-or pattern from polynomial */
keir@19113 1155 e = 0;
keir@19113 1156 for (i = 0; i < sizeof(p)/sizeof(int); i++)
keir@19113 1157 e |= 1L << (31 - p[i]);
keir@19113 1158
keir@19113 1159 crc_32_tab[0] = 0;
keir@19113 1160
keir@19113 1161 for (i = 1; i < 256; i++)
keir@19113 1162 {
keir@19113 1163 c = 0;
keir@19113 1164 for (k = i | 256; k != 1; k >>= 1)
keir@19113 1165 {
keir@19113 1166 c = c & 1 ? (c >> 1) ^ e : c >> 1;
keir@19113 1167 if (k & 1)
keir@19113 1168 c ^= e;
keir@19113 1169 }
keir@19113 1170 crc_32_tab[i] = c;
keir@19113 1171 }
keir@19113 1172
keir@19113 1173 /* this is initialized here so this code could reside in ROM */
keir@19113 1174 crc = (ulg)0xffffffffUL; /* shift register contents */
keir@19113 1175 }
keir@19113 1176
keir@19113 1177 /* gzip flag byte */
keir@19113 1178 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ASCII text */
keir@19113 1179 #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
keir@19113 1180 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
keir@19113 1181 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
keir@19113 1182 #define COMMENT 0x10 /* bit 4 set: file comment present */
keir@19113 1183 #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
keir@19113 1184 #define RESERVED 0xC0 /* bit 6,7: reserved */
keir@19113 1185
keir@19113 1186 /*
keir@19113 1187 * Do the uncompression!
keir@19113 1188 */
keir@19113 1189 static int INIT gunzip(void)
keir@19113 1190 {
keir@19113 1191 uch flags;
keir@19113 1192 unsigned char magic[2]; /* magic header */
keir@19113 1193 char method;
keir@19113 1194 ulg orig_crc = 0; /* original crc */
keir@19113 1195 ulg orig_len = 0; /* original uncompressed length */
keir@19113 1196 int res;
keir@19113 1197
keir@19113 1198 magic[0] = NEXTBYTE();
keir@19113 1199 magic[1] = NEXTBYTE();
keir@19113 1200 method = NEXTBYTE();
keir@19113 1201
keir@19113 1202 if (magic[0] != 037 ||
keir@19113 1203 ((magic[1] != 0213) && (magic[1] != 0236))) {
keir@19113 1204 error("bad gzip magic numbers");
keir@19113 1205 return -1;
keir@19113 1206 }
keir@19113 1207
keir@19113 1208 /* We only support method #8, DEFLATED */
keir@19113 1209 if (method != 8) {
keir@19113 1210 error("internal error, invalid method");
keir@19113 1211 return -1;
keir@19113 1212 }
keir@19113 1213
keir@19113 1214 flags = (uch)get_byte();
keir@19113 1215 if ((flags & ENCRYPTED) != 0) {
keir@19113 1216 error("Input is encrypted");
keir@19113 1217 return -1;
keir@19113 1218 }
keir@19113 1219 if ((flags & CONTINUATION) != 0) {
keir@19113 1220 error("Multi part input");
keir@19113 1221 return -1;
keir@19113 1222 }
keir@19113 1223 if ((flags & RESERVED) != 0) {
keir@19113 1224 error("Input has invalid flags");
keir@19113 1225 return -1;
keir@19113 1226 }
keir@19113 1227 NEXTBYTE(); /* Get timestamp */
keir@19113 1228 NEXTBYTE();
keir@19113 1229 NEXTBYTE();
keir@19113 1230 NEXTBYTE();
keir@19113 1231
keir@19113 1232 (void)NEXTBYTE(); /* Ignore extra flags for the moment */
keir@19113 1233 (void)NEXTBYTE(); /* Ignore OS type for the moment */
keir@19113 1234
keir@19113 1235 if ((flags & EXTRA_FIELD) != 0) {
keir@19113 1236 unsigned len = (unsigned)NEXTBYTE();
keir@19113 1237 len |= ((unsigned)NEXTBYTE())<<8;
keir@19113 1238 while (len--) (void)NEXTBYTE();
keir@19113 1239 }
keir@19113 1240
keir@19113 1241 /* Get original file name if it was truncated */
keir@19113 1242 if ((flags & ORIG_NAME) != 0) {
keir@19113 1243 /* Discard the old name */
keir@19113 1244 while (NEXTBYTE() != 0) /* null */ ;
keir@19113 1245 }
keir@19113 1246
keir@19113 1247 /* Discard file comment if any */
keir@19113 1248 if ((flags & COMMENT) != 0) {
keir@19113 1249 while (NEXTBYTE() != 0) /* null */ ;
keir@19113 1250 }
keir@19113 1251
keir@19113 1252 /* Decompress */
keir@19113 1253 if ((res = inflate())) {
keir@19113 1254 switch (res) {
keir@19113 1255 case 0:
keir@19113 1256 break;
keir@19113 1257 case 1:
keir@19113 1258 error("invalid compressed format (err=1)");
keir@19113 1259 break;
keir@19113 1260 case 2:
keir@19113 1261 error("invalid compressed format (err=2)");
keir@19113 1262 break;
keir@19113 1263 case 3:
keir@19113 1264 error("out of memory");
keir@19113 1265 break;
keir@19113 1266 case 4:
keir@19113 1267 error("out of input data");
keir@19113 1268 break;
keir@19113 1269 default:
keir@19113 1270 error("invalid compressed format (other)");
keir@19113 1271 }
keir@19113 1272 return -1;
keir@19113 1273 }
keir@19113 1274
keir@19113 1275 /* Get the crc and original length */
keir@19113 1276 /* crc32 (see algorithm.doc)
keir@19113 1277 * uncompressed input size modulo 2^32
keir@19113 1278 */
keir@19113 1279 orig_crc = (ulg) NEXTBYTE();
keir@19113 1280 orig_crc |= (ulg) NEXTBYTE() << 8;
keir@19113 1281 orig_crc |= (ulg) NEXTBYTE() << 16;
keir@19113 1282 orig_crc |= (ulg) NEXTBYTE() << 24;
keir@19113 1283
keir@19113 1284 orig_len = (ulg) NEXTBYTE();
keir@19113 1285 orig_len |= (ulg) NEXTBYTE() << 8;
keir@19113 1286 orig_len |= (ulg) NEXTBYTE() << 16;
keir@19113 1287 orig_len |= (ulg) NEXTBYTE() << 24;
keir@19113 1288
keir@19113 1289 /* Validate decompression */
keir@19113 1290 if (orig_crc != CRC_VALUE) {
keir@19113 1291 error("crc error");
keir@19113 1292 return -1;
keir@19113 1293 }
keir@19113 1294 if (orig_len != bytes_out) {
keir@19113 1295 error("length error");
keir@19113 1296 return -1;
keir@19113 1297 }
keir@19113 1298 return 0;
keir@19113 1299
keir@19113 1300 underrun: /* NEXTBYTE() goto's here if needed */
keir@19113 1301 error("out of input data");
keir@19113 1302 return -1;
keir@19113 1303 }