162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci#define DEBG(x) 362306a36Sopenharmony_ci#define DEBG1(x) 462306a36Sopenharmony_ci/* inflate.c -- Not copyrighted 1992 by Mark Adler 562306a36Sopenharmony_ci version c10p1, 10 January 1993 */ 662306a36Sopenharmony_ci 762306a36Sopenharmony_ci/* 862306a36Sopenharmony_ci * Adapted for booting Linux by Hannu Savolainen 1993 962306a36Sopenharmony_ci * based on gzip-1.0.3 1062306a36Sopenharmony_ci * 1162306a36Sopenharmony_ci * Nicolas Pitre <nico@fluxnic.net>, 1999/04/14 : 1262306a36Sopenharmony_ci * Little mods for all variable to reside either into rodata or bss segments 1362306a36Sopenharmony_ci * by marking constant variables with 'const' and initializing all the others 1462306a36Sopenharmony_ci * at run-time only. This allows for the kernel uncompressor to run 1562306a36Sopenharmony_ci * directly from Flash or ROM memory on embedded systems. 1662306a36Sopenharmony_ci */ 1762306a36Sopenharmony_ci 1862306a36Sopenharmony_ci/* 1962306a36Sopenharmony_ci Inflate deflated (PKZIP's method 8 compressed) data. The compression 2062306a36Sopenharmony_ci method searches for as much of the current string of bytes (up to a 2162306a36Sopenharmony_ci length of 258) in the previous 32 K bytes. If it doesn't find any 2262306a36Sopenharmony_ci matches (of at least length 3), it codes the next byte. Otherwise, it 2362306a36Sopenharmony_ci codes the length of the matched string and its distance backwards from 2462306a36Sopenharmony_ci the current position. There is a single Huffman code that codes both 2562306a36Sopenharmony_ci single bytes (called "literals") and match lengths. A second Huffman 2662306a36Sopenharmony_ci code codes the distance information, which follows a length code. Each 2762306a36Sopenharmony_ci length or distance code actually represents a base value and a number 2862306a36Sopenharmony_ci of "extra" (sometimes zero) bits to get to add to the base value. At 2962306a36Sopenharmony_ci the end of each deflated block is a special end-of-block (EOB) literal/ 3062306a36Sopenharmony_ci length code. The decoding process is basically: get a literal/length 3162306a36Sopenharmony_ci code; if EOB then done; if a literal, emit the decoded byte; if a 3262306a36Sopenharmony_ci length then get the distance and emit the referred-to bytes from the 3362306a36Sopenharmony_ci sliding window of previously emitted data. 3462306a36Sopenharmony_ci 3562306a36Sopenharmony_ci There are (currently) three kinds of inflate blocks: stored, fixed, and 3662306a36Sopenharmony_ci dynamic. The compressor deals with some chunk of data at a time, and 3762306a36Sopenharmony_ci decides which method to use on a chunk-by-chunk basis. A chunk might 3862306a36Sopenharmony_ci typically be 32 K or 64 K. If the chunk is incompressible, then the 3962306a36Sopenharmony_ci "stored" method is used. In this case, the bytes are simply stored as 4062306a36Sopenharmony_ci is, eight bits per byte, with none of the above coding. The bytes are 4162306a36Sopenharmony_ci preceded by a count, since there is no longer an EOB code. 4262306a36Sopenharmony_ci 4362306a36Sopenharmony_ci If the data is compressible, then either the fixed or dynamic methods 4462306a36Sopenharmony_ci are used. In the dynamic method, the compressed data is preceded by 4562306a36Sopenharmony_ci an encoding of the literal/length and distance Huffman codes that are 4662306a36Sopenharmony_ci to be used to decode this block. The representation is itself Huffman 4762306a36Sopenharmony_ci coded, and so is preceded by a description of that code. These code 4862306a36Sopenharmony_ci descriptions take up a little space, and so for small blocks, there is 4962306a36Sopenharmony_ci a predefined set of codes, called the fixed codes. The fixed method is 5062306a36Sopenharmony_ci used if the block codes up smaller that way (usually for quite small 5162306a36Sopenharmony_ci chunks), otherwise the dynamic method is used. In the latter case, the 5262306a36Sopenharmony_ci codes are customized to the probabilities in the current block, and so 5362306a36Sopenharmony_ci can code it much better than the pre-determined fixed codes. 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_ci The Huffman codes themselves are decoded using a multi-level table 5662306a36Sopenharmony_ci lookup, in order to maximize the speed of decoding plus the speed of 5762306a36Sopenharmony_ci building the decoding tables. See the comments below that precede the 5862306a36Sopenharmony_ci lbits and dbits tuning parameters. 5962306a36Sopenharmony_ci */ 6062306a36Sopenharmony_ci 6162306a36Sopenharmony_ci 6262306a36Sopenharmony_ci/* 6362306a36Sopenharmony_ci Notes beyond the 1.93a appnote.txt: 6462306a36Sopenharmony_ci 6562306a36Sopenharmony_ci 1. Distance pointers never point before the beginning of the output 6662306a36Sopenharmony_ci stream. 6762306a36Sopenharmony_ci 2. Distance pointers can point back across blocks, up to 32k away. 6862306a36Sopenharmony_ci 3. There is an implied maximum of 7 bits for the bit length table and 6962306a36Sopenharmony_ci 15 bits for the actual data. 7062306a36Sopenharmony_ci 4. If only one code exists, then it is encoded using one bit. (Zero 7162306a36Sopenharmony_ci would be more efficient, but perhaps a little confusing.) If two 7262306a36Sopenharmony_ci codes exist, they are coded using one bit each (0 and 1). 7362306a36Sopenharmony_ci 5. There is no way of sending zero distance codes--a dummy must be 7462306a36Sopenharmony_ci sent if there are none. (History: a pre 2.0 version of PKZIP would 7562306a36Sopenharmony_ci store blocks with no distance codes, but this was discovered to be 7662306a36Sopenharmony_ci too harsh a criterion.) Valid only for 1.93a. 2.04c does allow 7762306a36Sopenharmony_ci zero distance codes, which is sent as one code of zero bits in 7862306a36Sopenharmony_ci length. 7962306a36Sopenharmony_ci 6. There are up to 286 literal/length codes. Code 256 represents the 8062306a36Sopenharmony_ci end-of-block. Note however that the static length tree defines 8162306a36Sopenharmony_ci 288 codes just to fill out the Huffman codes. Codes 286 and 287 8262306a36Sopenharmony_ci cannot be used though, since there is no length base or extra bits 8362306a36Sopenharmony_ci defined for them. Similarly, there are up to 30 distance codes. 8462306a36Sopenharmony_ci However, static trees define 32 codes (all 5 bits) to fill out the 8562306a36Sopenharmony_ci Huffman codes, but the last two had better not show up in the data. 8662306a36Sopenharmony_ci 7. Unzip can check dynamic Huffman blocks for complete code sets. 8762306a36Sopenharmony_ci The exception is that a single code would not be complete (see #4). 8862306a36Sopenharmony_ci 8. The five bits following the block type is really the number of 8962306a36Sopenharmony_ci literal codes sent minus 257. 9062306a36Sopenharmony_ci 9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits 9162306a36Sopenharmony_ci (1+6+6). Therefore, to output three times the length, you output 9262306a36Sopenharmony_ci three codes (1+1+1), whereas to output four times the same length, 9362306a36Sopenharmony_ci you only need two codes (1+3). Hmm. 9462306a36Sopenharmony_ci 10. In the tree reconstruction algorithm, Code = Code + Increment 9562306a36Sopenharmony_ci only if BitLength(i) is not zero. (Pretty obvious.) 9662306a36Sopenharmony_ci 11. Correction: 4 Bits: # of Bit Length codes - 4 (4 - 19) 9762306a36Sopenharmony_ci 12. Note: length code 284 can represent 227-258, but length code 285 9862306a36Sopenharmony_ci really is 258. The last length deserves its own, short code 9962306a36Sopenharmony_ci since it gets used a lot in very redundant files. The length 10062306a36Sopenharmony_ci 258 is special since 258 - 3 (the min match length) is 255. 10162306a36Sopenharmony_ci 13. The literal/length and distance code bit lengths are read as a 10262306a36Sopenharmony_ci single stream of lengths. It is possible (and advantageous) for 10362306a36Sopenharmony_ci a repeat code (16, 17, or 18) to go across the boundary between 10462306a36Sopenharmony_ci the two sets of lengths. 10562306a36Sopenharmony_ci */ 10662306a36Sopenharmony_ci#include <linux/compiler.h> 10762306a36Sopenharmony_ci#ifdef NO_INFLATE_MALLOC 10862306a36Sopenharmony_ci#include <linux/slab.h> 10962306a36Sopenharmony_ci#endif 11062306a36Sopenharmony_ci 11162306a36Sopenharmony_ci#ifdef RCSID 11262306a36Sopenharmony_cistatic char rcsid[] = "#Id: inflate.c,v 0.14 1993/06/10 13:27:04 jloup Exp #"; 11362306a36Sopenharmony_ci#endif 11462306a36Sopenharmony_ci 11562306a36Sopenharmony_ci#ifndef STATIC 11662306a36Sopenharmony_ci 11762306a36Sopenharmony_ci#if defined(STDC_HEADERS) || defined(HAVE_STDLIB_H) 11862306a36Sopenharmony_ci# include <sys/types.h> 11962306a36Sopenharmony_ci# include <stdlib.h> 12062306a36Sopenharmony_ci#endif 12162306a36Sopenharmony_ci 12262306a36Sopenharmony_ci#include "gzip.h" 12362306a36Sopenharmony_ci#define STATIC 12462306a36Sopenharmony_ci#endif /* !STATIC */ 12562306a36Sopenharmony_ci 12662306a36Sopenharmony_ci#ifndef INIT 12762306a36Sopenharmony_ci#define INIT 12862306a36Sopenharmony_ci#endif 12962306a36Sopenharmony_ci 13062306a36Sopenharmony_ci#define slide window 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_ci/* Huffman code lookup table entry--this entry is four bytes for machines 13362306a36Sopenharmony_ci that have 16-bit pointers (e.g. PC's in the small or medium model). 13462306a36Sopenharmony_ci Valid extra bits are 0..13. e == 15 is EOB (end of block), e == 16 13562306a36Sopenharmony_ci means that v is a literal, 16 < e < 32 means that v is a pointer to 13662306a36Sopenharmony_ci the next table, which codes e - 16 bits, and lastly e == 99 indicates 13762306a36Sopenharmony_ci an unused code. If a code with e == 99 is looked up, this implies an 13862306a36Sopenharmony_ci error in the data. */ 13962306a36Sopenharmony_cistruct huft { 14062306a36Sopenharmony_ci uch e; /* number of extra bits or operation */ 14162306a36Sopenharmony_ci uch b; /* number of bits in this code or subcode */ 14262306a36Sopenharmony_ci union { 14362306a36Sopenharmony_ci ush n; /* literal, length base, or distance base */ 14462306a36Sopenharmony_ci struct huft *t; /* pointer to next level of table */ 14562306a36Sopenharmony_ci } v; 14662306a36Sopenharmony_ci}; 14762306a36Sopenharmony_ci 14862306a36Sopenharmony_ci 14962306a36Sopenharmony_ci/* Function prototypes */ 15062306a36Sopenharmony_ciSTATIC int INIT huft_build OF((unsigned *, unsigned, unsigned, 15162306a36Sopenharmony_ci const ush *, const ush *, struct huft **, int *)); 15262306a36Sopenharmony_ciSTATIC int INIT huft_free OF((struct huft *)); 15362306a36Sopenharmony_ciSTATIC int INIT inflate_codes OF((struct huft *, struct huft *, int, int)); 15462306a36Sopenharmony_ciSTATIC int INIT inflate_stored OF((void)); 15562306a36Sopenharmony_ciSTATIC int INIT inflate_fixed OF((void)); 15662306a36Sopenharmony_ciSTATIC int INIT inflate_dynamic OF((void)); 15762306a36Sopenharmony_ciSTATIC int INIT inflate_block OF((int *)); 15862306a36Sopenharmony_ciSTATIC int INIT inflate OF((void)); 15962306a36Sopenharmony_ci 16062306a36Sopenharmony_ci 16162306a36Sopenharmony_ci/* The inflate algorithm uses a sliding 32 K byte window on the uncompressed 16262306a36Sopenharmony_ci stream to find repeated byte strings. This is implemented here as a 16362306a36Sopenharmony_ci circular buffer. The index is updated simply by incrementing and then 16462306a36Sopenharmony_ci ANDing with 0x7fff (32K-1). */ 16562306a36Sopenharmony_ci/* It is left to other modules to supply the 32 K area. It is assumed 16662306a36Sopenharmony_ci to be usable as if it were declared "uch slide[32768];" or as just 16762306a36Sopenharmony_ci "uch *slide;" and then malloc'ed in the latter case. The definition 16862306a36Sopenharmony_ci must be in unzip.h, included above. */ 16962306a36Sopenharmony_ci/* unsigned wp; current position in slide */ 17062306a36Sopenharmony_ci#define wp outcnt 17162306a36Sopenharmony_ci#define flush_output(w) (wp=(w),flush_window()) 17262306a36Sopenharmony_ci 17362306a36Sopenharmony_ci/* Tables for deflate from PKZIP's appnote.txt. */ 17462306a36Sopenharmony_cistatic const unsigned border[] = { /* Order of the bit length code lengths */ 17562306a36Sopenharmony_ci 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; 17662306a36Sopenharmony_cistatic const ush cplens[] = { /* Copy lengths for literal codes 257..285 */ 17762306a36Sopenharmony_ci 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 17862306a36Sopenharmony_ci 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; 17962306a36Sopenharmony_ci /* note: see note #13 above about the 258 in this list. */ 18062306a36Sopenharmony_cistatic const ush cplext[] = { /* Extra bits for literal codes 257..285 */ 18162306a36Sopenharmony_ci 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 18262306a36Sopenharmony_ci 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */ 18362306a36Sopenharmony_cistatic const ush cpdist[] = { /* Copy offsets for distance codes 0..29 */ 18462306a36Sopenharmony_ci 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 18562306a36Sopenharmony_ci 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 18662306a36Sopenharmony_ci 8193, 12289, 16385, 24577}; 18762306a36Sopenharmony_cistatic const ush cpdext[] = { /* Extra bits for distance codes */ 18862306a36Sopenharmony_ci 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 18962306a36Sopenharmony_ci 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 19062306a36Sopenharmony_ci 12, 12, 13, 13}; 19162306a36Sopenharmony_ci 19262306a36Sopenharmony_ci 19362306a36Sopenharmony_ci 19462306a36Sopenharmony_ci/* Macros for inflate() bit peeking and grabbing. 19562306a36Sopenharmony_ci The usage is: 19662306a36Sopenharmony_ci 19762306a36Sopenharmony_ci NEEDBITS(j) 19862306a36Sopenharmony_ci x = b & mask_bits[j]; 19962306a36Sopenharmony_ci DUMPBITS(j) 20062306a36Sopenharmony_ci 20162306a36Sopenharmony_ci where NEEDBITS makes sure that b has at least j bits in it, and 20262306a36Sopenharmony_ci DUMPBITS removes the bits from b. The macros use the variable k 20362306a36Sopenharmony_ci for the number of bits in b. Normally, b and k are register 20462306a36Sopenharmony_ci variables for speed, and are initialized at the beginning of a 20562306a36Sopenharmony_ci routine that uses these macros from a global bit buffer and count. 20662306a36Sopenharmony_ci 20762306a36Sopenharmony_ci If we assume that EOB will be the longest code, then we will never 20862306a36Sopenharmony_ci ask for bits with NEEDBITS that are beyond the end of the stream. 20962306a36Sopenharmony_ci So, NEEDBITS should not read any more bytes than are needed to 21062306a36Sopenharmony_ci meet the request. Then no bytes need to be "returned" to the buffer 21162306a36Sopenharmony_ci at the end of the last block. 21262306a36Sopenharmony_ci 21362306a36Sopenharmony_ci However, this assumption is not true for fixed blocks--the EOB code 21462306a36Sopenharmony_ci is 7 bits, but the other literal/length codes can be 8 or 9 bits. 21562306a36Sopenharmony_ci (The EOB code is shorter than other codes because fixed blocks are 21662306a36Sopenharmony_ci generally short. So, while a block always has an EOB, many other 21762306a36Sopenharmony_ci literal/length codes have a significantly lower probability of 21862306a36Sopenharmony_ci showing up at all.) However, by making the first table have a 21962306a36Sopenharmony_ci lookup of seven bits, the EOB code will be found in that first 22062306a36Sopenharmony_ci lookup, and so will not require that too many bits be pulled from 22162306a36Sopenharmony_ci the stream. 22262306a36Sopenharmony_ci */ 22362306a36Sopenharmony_ci 22462306a36Sopenharmony_ciSTATIC ulg bb; /* bit buffer */ 22562306a36Sopenharmony_ciSTATIC unsigned bk; /* bits in bit buffer */ 22662306a36Sopenharmony_ci 22762306a36Sopenharmony_ciSTATIC const ush mask_bits[] = { 22862306a36Sopenharmony_ci 0x0000, 22962306a36Sopenharmony_ci 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff, 23062306a36Sopenharmony_ci 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff 23162306a36Sopenharmony_ci}; 23262306a36Sopenharmony_ci 23362306a36Sopenharmony_ci#define NEXTBYTE() ({ int v = get_byte(); if (v < 0) goto underrun; (uch)v; }) 23462306a36Sopenharmony_ci#define NEEDBITS(n) {while(k<(n)){b|=((ulg)NEXTBYTE())<<k;k+=8;}} 23562306a36Sopenharmony_ci#define DUMPBITS(n) {b>>=(n);k-=(n);} 23662306a36Sopenharmony_ci 23762306a36Sopenharmony_ci#ifndef NO_INFLATE_MALLOC 23862306a36Sopenharmony_ci/* A trivial malloc implementation, adapted from 23962306a36Sopenharmony_ci * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994 24062306a36Sopenharmony_ci */ 24162306a36Sopenharmony_ci 24262306a36Sopenharmony_cistatic unsigned long malloc_ptr; 24362306a36Sopenharmony_cistatic int malloc_count; 24462306a36Sopenharmony_ci 24562306a36Sopenharmony_cistatic void *malloc(int size) 24662306a36Sopenharmony_ci{ 24762306a36Sopenharmony_ci void *p; 24862306a36Sopenharmony_ci 24962306a36Sopenharmony_ci if (size < 0) 25062306a36Sopenharmony_ci error("Malloc error"); 25162306a36Sopenharmony_ci if (!malloc_ptr) 25262306a36Sopenharmony_ci malloc_ptr = free_mem_ptr; 25362306a36Sopenharmony_ci 25462306a36Sopenharmony_ci malloc_ptr = (malloc_ptr + 3) & ~3; /* Align */ 25562306a36Sopenharmony_ci 25662306a36Sopenharmony_ci p = (void *)malloc_ptr; 25762306a36Sopenharmony_ci malloc_ptr += size; 25862306a36Sopenharmony_ci 25962306a36Sopenharmony_ci if (free_mem_end_ptr && malloc_ptr >= free_mem_end_ptr) 26062306a36Sopenharmony_ci error("Out of memory"); 26162306a36Sopenharmony_ci 26262306a36Sopenharmony_ci malloc_count++; 26362306a36Sopenharmony_ci return p; 26462306a36Sopenharmony_ci} 26562306a36Sopenharmony_ci 26662306a36Sopenharmony_cistatic void free(void *where) 26762306a36Sopenharmony_ci{ 26862306a36Sopenharmony_ci malloc_count--; 26962306a36Sopenharmony_ci if (!malloc_count) 27062306a36Sopenharmony_ci malloc_ptr = free_mem_ptr; 27162306a36Sopenharmony_ci} 27262306a36Sopenharmony_ci#else 27362306a36Sopenharmony_ci#define malloc(a) kmalloc(a, GFP_KERNEL) 27462306a36Sopenharmony_ci#define free(a) kfree(a) 27562306a36Sopenharmony_ci#endif 27662306a36Sopenharmony_ci 27762306a36Sopenharmony_ci/* 27862306a36Sopenharmony_ci Huffman code decoding is performed using a multi-level table lookup. 27962306a36Sopenharmony_ci The fastest way to decode is to simply build a lookup table whose 28062306a36Sopenharmony_ci size is determined by the longest code. However, the time it takes 28162306a36Sopenharmony_ci to build this table can also be a factor if the data being decoded 28262306a36Sopenharmony_ci is not very long. The most common codes are necessarily the 28362306a36Sopenharmony_ci shortest codes, so those codes dominate the decoding time, and hence 28462306a36Sopenharmony_ci the speed. The idea is you can have a shorter table that decodes the 28562306a36Sopenharmony_ci shorter, more probable codes, and then point to subsidiary tables for 28662306a36Sopenharmony_ci the longer codes. The time it costs to decode the longer codes is 28762306a36Sopenharmony_ci then traded against the time it takes to make longer tables. 28862306a36Sopenharmony_ci 28962306a36Sopenharmony_ci This results of this trade are in the variables lbits and dbits 29062306a36Sopenharmony_ci below. lbits is the number of bits the first level table for literal/ 29162306a36Sopenharmony_ci length codes can decode in one step, and dbits is the same thing for 29262306a36Sopenharmony_ci the distance codes. Subsequent tables are also less than or equal to 29362306a36Sopenharmony_ci those sizes. These values may be adjusted either when all of the 29462306a36Sopenharmony_ci codes are shorter than that, in which case the longest code length in 29562306a36Sopenharmony_ci bits is used, or when the shortest code is *longer* than the requested 29662306a36Sopenharmony_ci table size, in which case the length of the shortest code in bits is 29762306a36Sopenharmony_ci used. 29862306a36Sopenharmony_ci 29962306a36Sopenharmony_ci There are two different values for the two tables, since they code a 30062306a36Sopenharmony_ci different number of possibilities each. The literal/length table 30162306a36Sopenharmony_ci codes 286 possible values, or in a flat code, a little over eight 30262306a36Sopenharmony_ci bits. The distance table codes 30 possible values, or a little less 30362306a36Sopenharmony_ci than five bits, flat. The optimum values for speed end up being 30462306a36Sopenharmony_ci about one bit more than those, so lbits is 8+1 and dbits is 5+1. 30562306a36Sopenharmony_ci The optimum values may differ though from machine to machine, and 30662306a36Sopenharmony_ci possibly even between compilers. Your mileage may vary. 30762306a36Sopenharmony_ci */ 30862306a36Sopenharmony_ci 30962306a36Sopenharmony_ci 31062306a36Sopenharmony_ciSTATIC const int lbits = 9; /* bits in base literal/length lookup table */ 31162306a36Sopenharmony_ciSTATIC const int dbits = 6; /* bits in base distance lookup table */ 31262306a36Sopenharmony_ci 31362306a36Sopenharmony_ci 31462306a36Sopenharmony_ci/* If BMAX needs to be larger than 16, then h and x[] should be ulg. */ 31562306a36Sopenharmony_ci#define BMAX 16 /* maximum bit length of any code (16 for explode) */ 31662306a36Sopenharmony_ci#define N_MAX 288 /* maximum number of codes in any set */ 31762306a36Sopenharmony_ci 31862306a36Sopenharmony_ci 31962306a36Sopenharmony_ciSTATIC unsigned hufts; /* track memory usage */ 32062306a36Sopenharmony_ci 32162306a36Sopenharmony_ci 32262306a36Sopenharmony_ciSTATIC int INIT huft_build( 32362306a36Sopenharmony_ci unsigned *b, /* code lengths in bits (all assumed <= BMAX) */ 32462306a36Sopenharmony_ci unsigned n, /* number of codes (assumed <= N_MAX) */ 32562306a36Sopenharmony_ci unsigned s, /* number of simple-valued codes (0..s-1) */ 32662306a36Sopenharmony_ci const ush *d, /* list of base values for non-simple codes */ 32762306a36Sopenharmony_ci const ush *e, /* list of extra bits for non-simple codes */ 32862306a36Sopenharmony_ci struct huft **t, /* result: starting table */ 32962306a36Sopenharmony_ci int *m /* maximum lookup bits, returns actual */ 33062306a36Sopenharmony_ci ) 33162306a36Sopenharmony_ci/* Given a list of code lengths and a maximum table size, make a set of 33262306a36Sopenharmony_ci tables to decode that set of codes. Return zero on success, one if 33362306a36Sopenharmony_ci the given code set is incomplete (the tables are still built in this 33462306a36Sopenharmony_ci case), two if the input is invalid (all zero length codes or an 33562306a36Sopenharmony_ci oversubscribed set of lengths), and three if not enough memory. */ 33662306a36Sopenharmony_ci{ 33762306a36Sopenharmony_ci unsigned a; /* counter for codes of length k */ 33862306a36Sopenharmony_ci unsigned f; /* i repeats in table every f entries */ 33962306a36Sopenharmony_ci int g; /* maximum code length */ 34062306a36Sopenharmony_ci int h; /* table level */ 34162306a36Sopenharmony_ci register unsigned i; /* counter, current code */ 34262306a36Sopenharmony_ci register unsigned j; /* counter */ 34362306a36Sopenharmony_ci register int k; /* number of bits in current code */ 34462306a36Sopenharmony_ci int l; /* bits per table (returned in m) */ 34562306a36Sopenharmony_ci register unsigned *p; /* pointer into c[], b[], or v[] */ 34662306a36Sopenharmony_ci register struct huft *q; /* points to current table */ 34762306a36Sopenharmony_ci struct huft r; /* table entry for structure assignment */ 34862306a36Sopenharmony_ci register int w; /* bits before this table == (l * h) */ 34962306a36Sopenharmony_ci unsigned *xp; /* pointer into x */ 35062306a36Sopenharmony_ci int y; /* number of dummy codes added */ 35162306a36Sopenharmony_ci unsigned z; /* number of entries in current table */ 35262306a36Sopenharmony_ci struct { 35362306a36Sopenharmony_ci unsigned c[BMAX+1]; /* bit length count table */ 35462306a36Sopenharmony_ci struct huft *u[BMAX]; /* table stack */ 35562306a36Sopenharmony_ci unsigned v[N_MAX]; /* values in order of bit length */ 35662306a36Sopenharmony_ci unsigned x[BMAX+1]; /* bit offsets, then code stack */ 35762306a36Sopenharmony_ci } *stk; 35862306a36Sopenharmony_ci unsigned *c, *v, *x; 35962306a36Sopenharmony_ci struct huft **u; 36062306a36Sopenharmony_ci int ret; 36162306a36Sopenharmony_ci 36262306a36Sopenharmony_ciDEBG("huft1 "); 36362306a36Sopenharmony_ci 36462306a36Sopenharmony_ci stk = malloc(sizeof(*stk)); 36562306a36Sopenharmony_ci if (stk == NULL) 36662306a36Sopenharmony_ci return 3; /* out of memory */ 36762306a36Sopenharmony_ci 36862306a36Sopenharmony_ci c = stk->c; 36962306a36Sopenharmony_ci v = stk->v; 37062306a36Sopenharmony_ci x = stk->x; 37162306a36Sopenharmony_ci u = stk->u; 37262306a36Sopenharmony_ci 37362306a36Sopenharmony_ci /* Generate counts for each bit length */ 37462306a36Sopenharmony_ci memzero(stk->c, sizeof(stk->c)); 37562306a36Sopenharmony_ci p = b; i = n; 37662306a36Sopenharmony_ci do { 37762306a36Sopenharmony_ci Tracecv(*p, (stderr, (n-i >= ' ' && n-i <= '~' ? "%c %d\n" : "0x%x %d\n"), 37862306a36Sopenharmony_ci n-i, *p)); 37962306a36Sopenharmony_ci c[*p]++; /* assume all entries <= BMAX */ 38062306a36Sopenharmony_ci p++; /* Can't combine with above line (Solaris bug) */ 38162306a36Sopenharmony_ci } while (--i); 38262306a36Sopenharmony_ci if (c[0] == n) /* null input--all zero length codes */ 38362306a36Sopenharmony_ci { 38462306a36Sopenharmony_ci *t = (struct huft *)NULL; 38562306a36Sopenharmony_ci *m = 0; 38662306a36Sopenharmony_ci ret = 2; 38762306a36Sopenharmony_ci goto out; 38862306a36Sopenharmony_ci } 38962306a36Sopenharmony_ci 39062306a36Sopenharmony_ciDEBG("huft2 "); 39162306a36Sopenharmony_ci 39262306a36Sopenharmony_ci /* Find minimum and maximum length, bound *m by those */ 39362306a36Sopenharmony_ci l = *m; 39462306a36Sopenharmony_ci for (j = 1; j <= BMAX; j++) 39562306a36Sopenharmony_ci if (c[j]) 39662306a36Sopenharmony_ci break; 39762306a36Sopenharmony_ci k = j; /* minimum code length */ 39862306a36Sopenharmony_ci if ((unsigned)l < j) 39962306a36Sopenharmony_ci l = j; 40062306a36Sopenharmony_ci for (i = BMAX; i; i--) 40162306a36Sopenharmony_ci if (c[i]) 40262306a36Sopenharmony_ci break; 40362306a36Sopenharmony_ci g = i; /* maximum code length */ 40462306a36Sopenharmony_ci if ((unsigned)l > i) 40562306a36Sopenharmony_ci l = i; 40662306a36Sopenharmony_ci *m = l; 40762306a36Sopenharmony_ci 40862306a36Sopenharmony_ciDEBG("huft3 "); 40962306a36Sopenharmony_ci 41062306a36Sopenharmony_ci /* Adjust last length count to fill out codes, if needed */ 41162306a36Sopenharmony_ci for (y = 1 << j; j < i; j++, y <<= 1) 41262306a36Sopenharmony_ci if ((y -= c[j]) < 0) { 41362306a36Sopenharmony_ci ret = 2; /* bad input: more codes than bits */ 41462306a36Sopenharmony_ci goto out; 41562306a36Sopenharmony_ci } 41662306a36Sopenharmony_ci if ((y -= c[i]) < 0) { 41762306a36Sopenharmony_ci ret = 2; 41862306a36Sopenharmony_ci goto out; 41962306a36Sopenharmony_ci } 42062306a36Sopenharmony_ci c[i] += y; 42162306a36Sopenharmony_ci 42262306a36Sopenharmony_ciDEBG("huft4 "); 42362306a36Sopenharmony_ci 42462306a36Sopenharmony_ci /* Generate starting offsets into the value table for each length */ 42562306a36Sopenharmony_ci x[1] = j = 0; 42662306a36Sopenharmony_ci p = c + 1; xp = x + 2; 42762306a36Sopenharmony_ci while (--i) { /* note that i == g from above */ 42862306a36Sopenharmony_ci *xp++ = (j += *p++); 42962306a36Sopenharmony_ci } 43062306a36Sopenharmony_ci 43162306a36Sopenharmony_ciDEBG("huft5 "); 43262306a36Sopenharmony_ci 43362306a36Sopenharmony_ci /* Make a table of values in order of bit lengths */ 43462306a36Sopenharmony_ci p = b; i = 0; 43562306a36Sopenharmony_ci do { 43662306a36Sopenharmony_ci if ((j = *p++) != 0) 43762306a36Sopenharmony_ci v[x[j]++] = i; 43862306a36Sopenharmony_ci } while (++i < n); 43962306a36Sopenharmony_ci n = x[g]; /* set n to length of v */ 44062306a36Sopenharmony_ci 44162306a36Sopenharmony_ciDEBG("h6 "); 44262306a36Sopenharmony_ci 44362306a36Sopenharmony_ci /* Generate the Huffman codes and for each, make the table entries */ 44462306a36Sopenharmony_ci x[0] = i = 0; /* first Huffman code is zero */ 44562306a36Sopenharmony_ci p = v; /* grab values in bit order */ 44662306a36Sopenharmony_ci h = -1; /* no tables yet--level -1 */ 44762306a36Sopenharmony_ci w = -l; /* bits decoded == (l * h) */ 44862306a36Sopenharmony_ci u[0] = (struct huft *)NULL; /* just to keep compilers happy */ 44962306a36Sopenharmony_ci q = (struct huft *)NULL; /* ditto */ 45062306a36Sopenharmony_ci z = 0; /* ditto */ 45162306a36Sopenharmony_ciDEBG("h6a "); 45262306a36Sopenharmony_ci 45362306a36Sopenharmony_ci /* go through the bit lengths (k already is bits in shortest code) */ 45462306a36Sopenharmony_ci for (; k <= g; k++) 45562306a36Sopenharmony_ci { 45662306a36Sopenharmony_ciDEBG("h6b "); 45762306a36Sopenharmony_ci a = c[k]; 45862306a36Sopenharmony_ci while (a--) 45962306a36Sopenharmony_ci { 46062306a36Sopenharmony_ciDEBG("h6b1 "); 46162306a36Sopenharmony_ci /* here i is the Huffman code of length k bits for value *p */ 46262306a36Sopenharmony_ci /* make tables up to required level */ 46362306a36Sopenharmony_ci while (k > w + l) 46462306a36Sopenharmony_ci { 46562306a36Sopenharmony_ciDEBG1("1 "); 46662306a36Sopenharmony_ci h++; 46762306a36Sopenharmony_ci w += l; /* previous table always l bits */ 46862306a36Sopenharmony_ci 46962306a36Sopenharmony_ci /* compute minimum size table less than or equal to l bits */ 47062306a36Sopenharmony_ci z = (z = g - w) > (unsigned)l ? l : z; /* upper limit on table size */ 47162306a36Sopenharmony_ci if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */ 47262306a36Sopenharmony_ci { /* too few codes for k-w bit table */ 47362306a36Sopenharmony_ciDEBG1("2 "); 47462306a36Sopenharmony_ci f -= a + 1; /* deduct codes from patterns left */ 47562306a36Sopenharmony_ci xp = c + k; 47662306a36Sopenharmony_ci if (j < z) 47762306a36Sopenharmony_ci while (++j < z) /* try smaller tables up to z bits */ 47862306a36Sopenharmony_ci { 47962306a36Sopenharmony_ci if ((f <<= 1) <= *++xp) 48062306a36Sopenharmony_ci break; /* enough codes to use up j bits */ 48162306a36Sopenharmony_ci f -= *xp; /* else deduct codes from patterns */ 48262306a36Sopenharmony_ci } 48362306a36Sopenharmony_ci } 48462306a36Sopenharmony_ciDEBG1("3 "); 48562306a36Sopenharmony_ci z = 1 << j; /* table entries for j-bit table */ 48662306a36Sopenharmony_ci 48762306a36Sopenharmony_ci /* allocate and link in new table */ 48862306a36Sopenharmony_ci if ((q = (struct huft *)malloc((z + 1)*sizeof(struct huft))) == 48962306a36Sopenharmony_ci (struct huft *)NULL) 49062306a36Sopenharmony_ci { 49162306a36Sopenharmony_ci if (h) 49262306a36Sopenharmony_ci huft_free(u[0]); 49362306a36Sopenharmony_ci ret = 3; /* not enough memory */ 49462306a36Sopenharmony_ci goto out; 49562306a36Sopenharmony_ci } 49662306a36Sopenharmony_ciDEBG1("4 "); 49762306a36Sopenharmony_ci hufts += z + 1; /* track memory usage */ 49862306a36Sopenharmony_ci *t = q + 1; /* link to list for huft_free() */ 49962306a36Sopenharmony_ci *(t = &(q->v.t)) = (struct huft *)NULL; 50062306a36Sopenharmony_ci u[h] = ++q; /* table starts after link */ 50162306a36Sopenharmony_ci 50262306a36Sopenharmony_ciDEBG1("5 "); 50362306a36Sopenharmony_ci /* connect to last table, if there is one */ 50462306a36Sopenharmony_ci if (h) 50562306a36Sopenharmony_ci { 50662306a36Sopenharmony_ci x[h] = i; /* save pattern for backing up */ 50762306a36Sopenharmony_ci r.b = (uch)l; /* bits to dump before this table */ 50862306a36Sopenharmony_ci r.e = (uch)(16 + j); /* bits in this table */ 50962306a36Sopenharmony_ci r.v.t = q; /* pointer to this table */ 51062306a36Sopenharmony_ci j = i >> (w - l); /* (get around Turbo C bug) */ 51162306a36Sopenharmony_ci u[h-1][j] = r; /* connect to last table */ 51262306a36Sopenharmony_ci } 51362306a36Sopenharmony_ciDEBG1("6 "); 51462306a36Sopenharmony_ci } 51562306a36Sopenharmony_ciDEBG("h6c "); 51662306a36Sopenharmony_ci 51762306a36Sopenharmony_ci /* set up table entry in r */ 51862306a36Sopenharmony_ci r.b = (uch)(k - w); 51962306a36Sopenharmony_ci if (p >= v + n) 52062306a36Sopenharmony_ci r.e = 99; /* out of values--invalid code */ 52162306a36Sopenharmony_ci else if (*p < s) 52262306a36Sopenharmony_ci { 52362306a36Sopenharmony_ci r.e = (uch)(*p < 256 ? 16 : 15); /* 256 is end-of-block code */ 52462306a36Sopenharmony_ci r.v.n = (ush)(*p); /* simple code is just the value */ 52562306a36Sopenharmony_ci p++; /* one compiler does not like *p++ */ 52662306a36Sopenharmony_ci } 52762306a36Sopenharmony_ci else 52862306a36Sopenharmony_ci { 52962306a36Sopenharmony_ci r.e = (uch)e[*p - s]; /* non-simple--look up in lists */ 53062306a36Sopenharmony_ci r.v.n = d[*p++ - s]; 53162306a36Sopenharmony_ci } 53262306a36Sopenharmony_ciDEBG("h6d "); 53362306a36Sopenharmony_ci 53462306a36Sopenharmony_ci /* fill code-like entries with r */ 53562306a36Sopenharmony_ci f = 1 << (k - w); 53662306a36Sopenharmony_ci for (j = i >> w; j < z; j += f) 53762306a36Sopenharmony_ci q[j] = r; 53862306a36Sopenharmony_ci 53962306a36Sopenharmony_ci /* backwards increment the k-bit code i */ 54062306a36Sopenharmony_ci for (j = 1 << (k - 1); i & j; j >>= 1) 54162306a36Sopenharmony_ci i ^= j; 54262306a36Sopenharmony_ci i ^= j; 54362306a36Sopenharmony_ci 54462306a36Sopenharmony_ci /* backup over finished tables */ 54562306a36Sopenharmony_ci while ((i & ((1 << w) - 1)) != x[h]) 54662306a36Sopenharmony_ci { 54762306a36Sopenharmony_ci h--; /* don't need to update q */ 54862306a36Sopenharmony_ci w -= l; 54962306a36Sopenharmony_ci } 55062306a36Sopenharmony_ciDEBG("h6e "); 55162306a36Sopenharmony_ci } 55262306a36Sopenharmony_ciDEBG("h6f "); 55362306a36Sopenharmony_ci } 55462306a36Sopenharmony_ci 55562306a36Sopenharmony_ciDEBG("huft7 "); 55662306a36Sopenharmony_ci 55762306a36Sopenharmony_ci /* Return true (1) if we were given an incomplete table */ 55862306a36Sopenharmony_ci ret = y != 0 && g != 1; 55962306a36Sopenharmony_ci 56062306a36Sopenharmony_ci out: 56162306a36Sopenharmony_ci free(stk); 56262306a36Sopenharmony_ci return ret; 56362306a36Sopenharmony_ci} 56462306a36Sopenharmony_ci 56562306a36Sopenharmony_ci 56662306a36Sopenharmony_ci 56762306a36Sopenharmony_ciSTATIC int INIT huft_free( 56862306a36Sopenharmony_ci struct huft *t /* table to free */ 56962306a36Sopenharmony_ci ) 57062306a36Sopenharmony_ci/* Free the malloc'ed tables built by huft_build(), which makes a linked 57162306a36Sopenharmony_ci list of the tables it made, with the links in a dummy first entry of 57262306a36Sopenharmony_ci each table. */ 57362306a36Sopenharmony_ci{ 57462306a36Sopenharmony_ci register struct huft *p, *q; 57562306a36Sopenharmony_ci 57662306a36Sopenharmony_ci 57762306a36Sopenharmony_ci /* Go through linked list, freeing from the malloced (t[-1]) address. */ 57862306a36Sopenharmony_ci p = t; 57962306a36Sopenharmony_ci while (p != (struct huft *)NULL) 58062306a36Sopenharmony_ci { 58162306a36Sopenharmony_ci q = (--p)->v.t; 58262306a36Sopenharmony_ci free((char*)p); 58362306a36Sopenharmony_ci p = q; 58462306a36Sopenharmony_ci } 58562306a36Sopenharmony_ci return 0; 58662306a36Sopenharmony_ci} 58762306a36Sopenharmony_ci 58862306a36Sopenharmony_ci 58962306a36Sopenharmony_ciSTATIC int INIT inflate_codes( 59062306a36Sopenharmony_ci struct huft *tl, /* literal/length decoder tables */ 59162306a36Sopenharmony_ci struct huft *td, /* distance decoder tables */ 59262306a36Sopenharmony_ci int bl, /* number of bits decoded by tl[] */ 59362306a36Sopenharmony_ci int bd /* number of bits decoded by td[] */ 59462306a36Sopenharmony_ci ) 59562306a36Sopenharmony_ci/* inflate (decompress) the codes in a deflated (compressed) block. 59662306a36Sopenharmony_ci Return an error code or zero if it all goes ok. */ 59762306a36Sopenharmony_ci{ 59862306a36Sopenharmony_ci register unsigned e; /* table entry flag/number of extra bits */ 59962306a36Sopenharmony_ci unsigned n, d; /* length and index for copy */ 60062306a36Sopenharmony_ci unsigned w; /* current window position */ 60162306a36Sopenharmony_ci struct huft *t; /* pointer to table entry */ 60262306a36Sopenharmony_ci unsigned ml, md; /* masks for bl and bd bits */ 60362306a36Sopenharmony_ci register ulg b; /* bit buffer */ 60462306a36Sopenharmony_ci register unsigned k; /* number of bits in bit buffer */ 60562306a36Sopenharmony_ci 60662306a36Sopenharmony_ci 60762306a36Sopenharmony_ci /* make local copies of globals */ 60862306a36Sopenharmony_ci b = bb; /* initialize bit buffer */ 60962306a36Sopenharmony_ci k = bk; 61062306a36Sopenharmony_ci w = wp; /* initialize window position */ 61162306a36Sopenharmony_ci 61262306a36Sopenharmony_ci /* inflate the coded data */ 61362306a36Sopenharmony_ci ml = mask_bits[bl]; /* precompute masks for speed */ 61462306a36Sopenharmony_ci md = mask_bits[bd]; 61562306a36Sopenharmony_ci for (;;) /* do until end of block */ 61662306a36Sopenharmony_ci { 61762306a36Sopenharmony_ci NEEDBITS((unsigned)bl) 61862306a36Sopenharmony_ci if ((e = (t = tl + ((unsigned)b & ml))->e) > 16) 61962306a36Sopenharmony_ci do { 62062306a36Sopenharmony_ci if (e == 99) 62162306a36Sopenharmony_ci return 1; 62262306a36Sopenharmony_ci DUMPBITS(t->b) 62362306a36Sopenharmony_ci e -= 16; 62462306a36Sopenharmony_ci NEEDBITS(e) 62562306a36Sopenharmony_ci } while ((e = (t = t->v.t + ((unsigned)b & mask_bits[e]))->e) > 16); 62662306a36Sopenharmony_ci DUMPBITS(t->b) 62762306a36Sopenharmony_ci if (e == 16) /* then it's a literal */ 62862306a36Sopenharmony_ci { 62962306a36Sopenharmony_ci slide[w++] = (uch)t->v.n; 63062306a36Sopenharmony_ci Tracevv((stderr, "%c", slide[w-1])); 63162306a36Sopenharmony_ci if (w == WSIZE) 63262306a36Sopenharmony_ci { 63362306a36Sopenharmony_ci flush_output(w); 63462306a36Sopenharmony_ci w = 0; 63562306a36Sopenharmony_ci } 63662306a36Sopenharmony_ci } 63762306a36Sopenharmony_ci else /* it's an EOB or a length */ 63862306a36Sopenharmony_ci { 63962306a36Sopenharmony_ci /* exit if end of block */ 64062306a36Sopenharmony_ci if (e == 15) 64162306a36Sopenharmony_ci break; 64262306a36Sopenharmony_ci 64362306a36Sopenharmony_ci /* get length of block to copy */ 64462306a36Sopenharmony_ci NEEDBITS(e) 64562306a36Sopenharmony_ci n = t->v.n + ((unsigned)b & mask_bits[e]); 64662306a36Sopenharmony_ci DUMPBITS(e); 64762306a36Sopenharmony_ci 64862306a36Sopenharmony_ci /* decode distance of block to copy */ 64962306a36Sopenharmony_ci NEEDBITS((unsigned)bd) 65062306a36Sopenharmony_ci if ((e = (t = td + ((unsigned)b & md))->e) > 16) 65162306a36Sopenharmony_ci do { 65262306a36Sopenharmony_ci if (e == 99) 65362306a36Sopenharmony_ci return 1; 65462306a36Sopenharmony_ci DUMPBITS(t->b) 65562306a36Sopenharmony_ci e -= 16; 65662306a36Sopenharmony_ci NEEDBITS(e) 65762306a36Sopenharmony_ci } while ((e = (t = t->v.t + ((unsigned)b & mask_bits[e]))->e) > 16); 65862306a36Sopenharmony_ci DUMPBITS(t->b) 65962306a36Sopenharmony_ci NEEDBITS(e) 66062306a36Sopenharmony_ci d = w - t->v.n - ((unsigned)b & mask_bits[e]); 66162306a36Sopenharmony_ci DUMPBITS(e) 66262306a36Sopenharmony_ci Tracevv((stderr,"\\[%d,%d]", w-d, n)); 66362306a36Sopenharmony_ci 66462306a36Sopenharmony_ci /* do the copy */ 66562306a36Sopenharmony_ci do { 66662306a36Sopenharmony_ci n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e); 66762306a36Sopenharmony_ci#if !defined(NOMEMCPY) && !defined(DEBUG) 66862306a36Sopenharmony_ci if (w - d >= e) /* (this test assumes unsigned comparison) */ 66962306a36Sopenharmony_ci { 67062306a36Sopenharmony_ci memcpy(slide + w, slide + d, e); 67162306a36Sopenharmony_ci w += e; 67262306a36Sopenharmony_ci d += e; 67362306a36Sopenharmony_ci } 67462306a36Sopenharmony_ci else /* do it slow to avoid memcpy() overlap */ 67562306a36Sopenharmony_ci#endif /* !NOMEMCPY */ 67662306a36Sopenharmony_ci do { 67762306a36Sopenharmony_ci slide[w++] = slide[d++]; 67862306a36Sopenharmony_ci Tracevv((stderr, "%c", slide[w-1])); 67962306a36Sopenharmony_ci } while (--e); 68062306a36Sopenharmony_ci if (w == WSIZE) 68162306a36Sopenharmony_ci { 68262306a36Sopenharmony_ci flush_output(w); 68362306a36Sopenharmony_ci w = 0; 68462306a36Sopenharmony_ci } 68562306a36Sopenharmony_ci } while (n); 68662306a36Sopenharmony_ci } 68762306a36Sopenharmony_ci } 68862306a36Sopenharmony_ci 68962306a36Sopenharmony_ci 69062306a36Sopenharmony_ci /* restore the globals from the locals */ 69162306a36Sopenharmony_ci wp = w; /* restore global window pointer */ 69262306a36Sopenharmony_ci bb = b; /* restore global bit buffer */ 69362306a36Sopenharmony_ci bk = k; 69462306a36Sopenharmony_ci 69562306a36Sopenharmony_ci /* done */ 69662306a36Sopenharmony_ci return 0; 69762306a36Sopenharmony_ci 69862306a36Sopenharmony_ci underrun: 69962306a36Sopenharmony_ci return 4; /* Input underrun */ 70062306a36Sopenharmony_ci} 70162306a36Sopenharmony_ci 70262306a36Sopenharmony_ci 70362306a36Sopenharmony_ci 70462306a36Sopenharmony_ciSTATIC int INIT inflate_stored(void) 70562306a36Sopenharmony_ci/* "decompress" an inflated type 0 (stored) block. */ 70662306a36Sopenharmony_ci{ 70762306a36Sopenharmony_ci unsigned n; /* number of bytes in block */ 70862306a36Sopenharmony_ci unsigned w; /* current window position */ 70962306a36Sopenharmony_ci register ulg b; /* bit buffer */ 71062306a36Sopenharmony_ci register unsigned k; /* number of bits in bit buffer */ 71162306a36Sopenharmony_ci 71262306a36Sopenharmony_ciDEBG("<stor"); 71362306a36Sopenharmony_ci 71462306a36Sopenharmony_ci /* make local copies of globals */ 71562306a36Sopenharmony_ci b = bb; /* initialize bit buffer */ 71662306a36Sopenharmony_ci k = bk; 71762306a36Sopenharmony_ci w = wp; /* initialize window position */ 71862306a36Sopenharmony_ci 71962306a36Sopenharmony_ci 72062306a36Sopenharmony_ci /* go to byte boundary */ 72162306a36Sopenharmony_ci n = k & 7; 72262306a36Sopenharmony_ci DUMPBITS(n); 72362306a36Sopenharmony_ci 72462306a36Sopenharmony_ci 72562306a36Sopenharmony_ci /* get the length and its complement */ 72662306a36Sopenharmony_ci NEEDBITS(16) 72762306a36Sopenharmony_ci n = ((unsigned)b & 0xffff); 72862306a36Sopenharmony_ci DUMPBITS(16) 72962306a36Sopenharmony_ci NEEDBITS(16) 73062306a36Sopenharmony_ci if (n != (unsigned)((~b) & 0xffff)) 73162306a36Sopenharmony_ci return 1; /* error in compressed data */ 73262306a36Sopenharmony_ci DUMPBITS(16) 73362306a36Sopenharmony_ci 73462306a36Sopenharmony_ci 73562306a36Sopenharmony_ci /* read and output the compressed data */ 73662306a36Sopenharmony_ci while (n--) 73762306a36Sopenharmony_ci { 73862306a36Sopenharmony_ci NEEDBITS(8) 73962306a36Sopenharmony_ci slide[w++] = (uch)b; 74062306a36Sopenharmony_ci if (w == WSIZE) 74162306a36Sopenharmony_ci { 74262306a36Sopenharmony_ci flush_output(w); 74362306a36Sopenharmony_ci w = 0; 74462306a36Sopenharmony_ci } 74562306a36Sopenharmony_ci DUMPBITS(8) 74662306a36Sopenharmony_ci } 74762306a36Sopenharmony_ci 74862306a36Sopenharmony_ci 74962306a36Sopenharmony_ci /* restore the globals from the locals */ 75062306a36Sopenharmony_ci wp = w; /* restore global window pointer */ 75162306a36Sopenharmony_ci bb = b; /* restore global bit buffer */ 75262306a36Sopenharmony_ci bk = k; 75362306a36Sopenharmony_ci 75462306a36Sopenharmony_ci DEBG(">"); 75562306a36Sopenharmony_ci return 0; 75662306a36Sopenharmony_ci 75762306a36Sopenharmony_ci underrun: 75862306a36Sopenharmony_ci return 4; /* Input underrun */ 75962306a36Sopenharmony_ci} 76062306a36Sopenharmony_ci 76162306a36Sopenharmony_ci 76262306a36Sopenharmony_ci/* 76362306a36Sopenharmony_ci * We use `noinline' here to prevent gcc-3.5 from using too much stack space 76462306a36Sopenharmony_ci */ 76562306a36Sopenharmony_ciSTATIC int noinline INIT inflate_fixed(void) 76662306a36Sopenharmony_ci/* decompress an inflated type 1 (fixed Huffman codes) block. We should 76762306a36Sopenharmony_ci either replace this with a custom decoder, or at least precompute the 76862306a36Sopenharmony_ci Huffman tables. */ 76962306a36Sopenharmony_ci{ 77062306a36Sopenharmony_ci int i; /* temporary variable */ 77162306a36Sopenharmony_ci struct huft *tl; /* literal/length code table */ 77262306a36Sopenharmony_ci struct huft *td; /* distance code table */ 77362306a36Sopenharmony_ci int bl; /* lookup bits for tl */ 77462306a36Sopenharmony_ci int bd; /* lookup bits for td */ 77562306a36Sopenharmony_ci unsigned *l; /* length list for huft_build */ 77662306a36Sopenharmony_ci 77762306a36Sopenharmony_ciDEBG("<fix"); 77862306a36Sopenharmony_ci 77962306a36Sopenharmony_ci l = malloc(sizeof(*l) * 288); 78062306a36Sopenharmony_ci if (l == NULL) 78162306a36Sopenharmony_ci return 3; /* out of memory */ 78262306a36Sopenharmony_ci 78362306a36Sopenharmony_ci /* set up literal table */ 78462306a36Sopenharmony_ci for (i = 0; i < 144; i++) 78562306a36Sopenharmony_ci l[i] = 8; 78662306a36Sopenharmony_ci for (; i < 256; i++) 78762306a36Sopenharmony_ci l[i] = 9; 78862306a36Sopenharmony_ci for (; i < 280; i++) 78962306a36Sopenharmony_ci l[i] = 7; 79062306a36Sopenharmony_ci for (; i < 288; i++) /* make a complete, but wrong code set */ 79162306a36Sopenharmony_ci l[i] = 8; 79262306a36Sopenharmony_ci bl = 7; 79362306a36Sopenharmony_ci if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0) { 79462306a36Sopenharmony_ci free(l); 79562306a36Sopenharmony_ci return i; 79662306a36Sopenharmony_ci } 79762306a36Sopenharmony_ci 79862306a36Sopenharmony_ci /* set up distance table */ 79962306a36Sopenharmony_ci for (i = 0; i < 30; i++) /* make an incomplete code set */ 80062306a36Sopenharmony_ci l[i] = 5; 80162306a36Sopenharmony_ci bd = 5; 80262306a36Sopenharmony_ci if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1) 80362306a36Sopenharmony_ci { 80462306a36Sopenharmony_ci huft_free(tl); 80562306a36Sopenharmony_ci free(l); 80662306a36Sopenharmony_ci 80762306a36Sopenharmony_ci DEBG(">"); 80862306a36Sopenharmony_ci return i; 80962306a36Sopenharmony_ci } 81062306a36Sopenharmony_ci 81162306a36Sopenharmony_ci 81262306a36Sopenharmony_ci /* decompress until an end-of-block code */ 81362306a36Sopenharmony_ci if (inflate_codes(tl, td, bl, bd)) { 81462306a36Sopenharmony_ci free(l); 81562306a36Sopenharmony_ci return 1; 81662306a36Sopenharmony_ci } 81762306a36Sopenharmony_ci 81862306a36Sopenharmony_ci /* free the decoding tables, return */ 81962306a36Sopenharmony_ci free(l); 82062306a36Sopenharmony_ci huft_free(tl); 82162306a36Sopenharmony_ci huft_free(td); 82262306a36Sopenharmony_ci return 0; 82362306a36Sopenharmony_ci} 82462306a36Sopenharmony_ci 82562306a36Sopenharmony_ci 82662306a36Sopenharmony_ci/* 82762306a36Sopenharmony_ci * We use `noinline' here to prevent gcc-3.5 from using too much stack space 82862306a36Sopenharmony_ci */ 82962306a36Sopenharmony_ciSTATIC int noinline INIT inflate_dynamic(void) 83062306a36Sopenharmony_ci/* decompress an inflated type 2 (dynamic Huffman codes) block. */ 83162306a36Sopenharmony_ci{ 83262306a36Sopenharmony_ci int i; /* temporary variables */ 83362306a36Sopenharmony_ci unsigned j; 83462306a36Sopenharmony_ci unsigned l; /* last length */ 83562306a36Sopenharmony_ci unsigned m; /* mask for bit lengths table */ 83662306a36Sopenharmony_ci unsigned n; /* number of lengths to get */ 83762306a36Sopenharmony_ci struct huft *tl; /* literal/length code table */ 83862306a36Sopenharmony_ci struct huft *td; /* distance code table */ 83962306a36Sopenharmony_ci int bl; /* lookup bits for tl */ 84062306a36Sopenharmony_ci int bd; /* lookup bits for td */ 84162306a36Sopenharmony_ci unsigned nb; /* number of bit length codes */ 84262306a36Sopenharmony_ci unsigned nl; /* number of literal/length codes */ 84362306a36Sopenharmony_ci unsigned nd; /* number of distance codes */ 84462306a36Sopenharmony_ci unsigned *ll; /* literal/length and distance code lengths */ 84562306a36Sopenharmony_ci register ulg b; /* bit buffer */ 84662306a36Sopenharmony_ci register unsigned k; /* number of bits in bit buffer */ 84762306a36Sopenharmony_ci int ret; 84862306a36Sopenharmony_ci 84962306a36Sopenharmony_ciDEBG("<dyn"); 85062306a36Sopenharmony_ci 85162306a36Sopenharmony_ci#ifdef PKZIP_BUG_WORKAROUND 85262306a36Sopenharmony_ci ll = malloc(sizeof(*ll) * (288+32)); /* literal/length and distance code lengths */ 85362306a36Sopenharmony_ci#else 85462306a36Sopenharmony_ci ll = malloc(sizeof(*ll) * (286+30)); /* literal/length and distance code lengths */ 85562306a36Sopenharmony_ci#endif 85662306a36Sopenharmony_ci 85762306a36Sopenharmony_ci if (ll == NULL) 85862306a36Sopenharmony_ci return 1; 85962306a36Sopenharmony_ci 86062306a36Sopenharmony_ci /* make local bit buffer */ 86162306a36Sopenharmony_ci b = bb; 86262306a36Sopenharmony_ci k = bk; 86362306a36Sopenharmony_ci 86462306a36Sopenharmony_ci 86562306a36Sopenharmony_ci /* read in table lengths */ 86662306a36Sopenharmony_ci NEEDBITS(5) 86762306a36Sopenharmony_ci nl = 257 + ((unsigned)b & 0x1f); /* number of literal/length codes */ 86862306a36Sopenharmony_ci DUMPBITS(5) 86962306a36Sopenharmony_ci NEEDBITS(5) 87062306a36Sopenharmony_ci nd = 1 + ((unsigned)b & 0x1f); /* number of distance codes */ 87162306a36Sopenharmony_ci DUMPBITS(5) 87262306a36Sopenharmony_ci NEEDBITS(4) 87362306a36Sopenharmony_ci nb = 4 + ((unsigned)b & 0xf); /* number of bit length codes */ 87462306a36Sopenharmony_ci DUMPBITS(4) 87562306a36Sopenharmony_ci#ifdef PKZIP_BUG_WORKAROUND 87662306a36Sopenharmony_ci if (nl > 288 || nd > 32) 87762306a36Sopenharmony_ci#else 87862306a36Sopenharmony_ci if (nl > 286 || nd > 30) 87962306a36Sopenharmony_ci#endif 88062306a36Sopenharmony_ci { 88162306a36Sopenharmony_ci ret = 1; /* bad lengths */ 88262306a36Sopenharmony_ci goto out; 88362306a36Sopenharmony_ci } 88462306a36Sopenharmony_ci 88562306a36Sopenharmony_ciDEBG("dyn1 "); 88662306a36Sopenharmony_ci 88762306a36Sopenharmony_ci /* read in bit-length-code lengths */ 88862306a36Sopenharmony_ci for (j = 0; j < nb; j++) 88962306a36Sopenharmony_ci { 89062306a36Sopenharmony_ci NEEDBITS(3) 89162306a36Sopenharmony_ci ll[border[j]] = (unsigned)b & 7; 89262306a36Sopenharmony_ci DUMPBITS(3) 89362306a36Sopenharmony_ci } 89462306a36Sopenharmony_ci for (; j < 19; j++) 89562306a36Sopenharmony_ci ll[border[j]] = 0; 89662306a36Sopenharmony_ci 89762306a36Sopenharmony_ciDEBG("dyn2 "); 89862306a36Sopenharmony_ci 89962306a36Sopenharmony_ci /* build decoding table for trees--single level, 7 bit lookup */ 90062306a36Sopenharmony_ci bl = 7; 90162306a36Sopenharmony_ci if ((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl)) != 0) 90262306a36Sopenharmony_ci { 90362306a36Sopenharmony_ci if (i == 1) 90462306a36Sopenharmony_ci huft_free(tl); 90562306a36Sopenharmony_ci ret = i; /* incomplete code set */ 90662306a36Sopenharmony_ci goto out; 90762306a36Sopenharmony_ci } 90862306a36Sopenharmony_ci 90962306a36Sopenharmony_ciDEBG("dyn3 "); 91062306a36Sopenharmony_ci 91162306a36Sopenharmony_ci /* read in literal and distance code lengths */ 91262306a36Sopenharmony_ci n = nl + nd; 91362306a36Sopenharmony_ci m = mask_bits[bl]; 91462306a36Sopenharmony_ci i = l = 0; 91562306a36Sopenharmony_ci while ((unsigned)i < n) 91662306a36Sopenharmony_ci { 91762306a36Sopenharmony_ci NEEDBITS((unsigned)bl) 91862306a36Sopenharmony_ci j = (td = tl + ((unsigned)b & m))->b; 91962306a36Sopenharmony_ci DUMPBITS(j) 92062306a36Sopenharmony_ci j = td->v.n; 92162306a36Sopenharmony_ci if (j < 16) /* length of code in bits (0..15) */ 92262306a36Sopenharmony_ci ll[i++] = l = j; /* save last length in l */ 92362306a36Sopenharmony_ci else if (j == 16) /* repeat last length 3 to 6 times */ 92462306a36Sopenharmony_ci { 92562306a36Sopenharmony_ci NEEDBITS(2) 92662306a36Sopenharmony_ci j = 3 + ((unsigned)b & 3); 92762306a36Sopenharmony_ci DUMPBITS(2) 92862306a36Sopenharmony_ci if ((unsigned)i + j > n) { 92962306a36Sopenharmony_ci ret = 1; 93062306a36Sopenharmony_ci goto out; 93162306a36Sopenharmony_ci } 93262306a36Sopenharmony_ci while (j--) 93362306a36Sopenharmony_ci ll[i++] = l; 93462306a36Sopenharmony_ci } 93562306a36Sopenharmony_ci else if (j == 17) /* 3 to 10 zero length codes */ 93662306a36Sopenharmony_ci { 93762306a36Sopenharmony_ci NEEDBITS(3) 93862306a36Sopenharmony_ci j = 3 + ((unsigned)b & 7); 93962306a36Sopenharmony_ci DUMPBITS(3) 94062306a36Sopenharmony_ci if ((unsigned)i + j > n) { 94162306a36Sopenharmony_ci ret = 1; 94262306a36Sopenharmony_ci goto out; 94362306a36Sopenharmony_ci } 94462306a36Sopenharmony_ci while (j--) 94562306a36Sopenharmony_ci ll[i++] = 0; 94662306a36Sopenharmony_ci l = 0; 94762306a36Sopenharmony_ci } 94862306a36Sopenharmony_ci else /* j == 18: 11 to 138 zero length codes */ 94962306a36Sopenharmony_ci { 95062306a36Sopenharmony_ci NEEDBITS(7) 95162306a36Sopenharmony_ci j = 11 + ((unsigned)b & 0x7f); 95262306a36Sopenharmony_ci DUMPBITS(7) 95362306a36Sopenharmony_ci if ((unsigned)i + j > n) { 95462306a36Sopenharmony_ci ret = 1; 95562306a36Sopenharmony_ci goto out; 95662306a36Sopenharmony_ci } 95762306a36Sopenharmony_ci while (j--) 95862306a36Sopenharmony_ci ll[i++] = 0; 95962306a36Sopenharmony_ci l = 0; 96062306a36Sopenharmony_ci } 96162306a36Sopenharmony_ci } 96262306a36Sopenharmony_ci 96362306a36Sopenharmony_ciDEBG("dyn4 "); 96462306a36Sopenharmony_ci 96562306a36Sopenharmony_ci /* free decoding table for trees */ 96662306a36Sopenharmony_ci huft_free(tl); 96762306a36Sopenharmony_ci 96862306a36Sopenharmony_ciDEBG("dyn5 "); 96962306a36Sopenharmony_ci 97062306a36Sopenharmony_ci /* restore the global bit buffer */ 97162306a36Sopenharmony_ci bb = b; 97262306a36Sopenharmony_ci bk = k; 97362306a36Sopenharmony_ci 97462306a36Sopenharmony_ciDEBG("dyn5a "); 97562306a36Sopenharmony_ci 97662306a36Sopenharmony_ci /* build the decoding tables for literal/length and distance codes */ 97762306a36Sopenharmony_ci bl = lbits; 97862306a36Sopenharmony_ci if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0) 97962306a36Sopenharmony_ci { 98062306a36Sopenharmony_ciDEBG("dyn5b "); 98162306a36Sopenharmony_ci if (i == 1) { 98262306a36Sopenharmony_ci error("incomplete literal tree"); 98362306a36Sopenharmony_ci huft_free(tl); 98462306a36Sopenharmony_ci } 98562306a36Sopenharmony_ci ret = i; /* incomplete code set */ 98662306a36Sopenharmony_ci goto out; 98762306a36Sopenharmony_ci } 98862306a36Sopenharmony_ciDEBG("dyn5c "); 98962306a36Sopenharmony_ci bd = dbits; 99062306a36Sopenharmony_ci if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0) 99162306a36Sopenharmony_ci { 99262306a36Sopenharmony_ciDEBG("dyn5d "); 99362306a36Sopenharmony_ci if (i == 1) { 99462306a36Sopenharmony_ci error("incomplete distance tree"); 99562306a36Sopenharmony_ci#ifdef PKZIP_BUG_WORKAROUND 99662306a36Sopenharmony_ci i = 0; 99762306a36Sopenharmony_ci } 99862306a36Sopenharmony_ci#else 99962306a36Sopenharmony_ci huft_free(td); 100062306a36Sopenharmony_ci } 100162306a36Sopenharmony_ci huft_free(tl); 100262306a36Sopenharmony_ci ret = i; /* incomplete code set */ 100362306a36Sopenharmony_ci goto out; 100462306a36Sopenharmony_ci#endif 100562306a36Sopenharmony_ci } 100662306a36Sopenharmony_ci 100762306a36Sopenharmony_ciDEBG("dyn6 "); 100862306a36Sopenharmony_ci 100962306a36Sopenharmony_ci /* decompress until an end-of-block code */ 101062306a36Sopenharmony_ci if (inflate_codes(tl, td, bl, bd)) { 101162306a36Sopenharmony_ci ret = 1; 101262306a36Sopenharmony_ci goto out; 101362306a36Sopenharmony_ci } 101462306a36Sopenharmony_ci 101562306a36Sopenharmony_ciDEBG("dyn7 "); 101662306a36Sopenharmony_ci 101762306a36Sopenharmony_ci /* free the decoding tables, return */ 101862306a36Sopenharmony_ci huft_free(tl); 101962306a36Sopenharmony_ci huft_free(td); 102062306a36Sopenharmony_ci 102162306a36Sopenharmony_ci DEBG(">"); 102262306a36Sopenharmony_ci ret = 0; 102362306a36Sopenharmony_ciout: 102462306a36Sopenharmony_ci free(ll); 102562306a36Sopenharmony_ci return ret; 102662306a36Sopenharmony_ci 102762306a36Sopenharmony_ciunderrun: 102862306a36Sopenharmony_ci ret = 4; /* Input underrun */ 102962306a36Sopenharmony_ci goto out; 103062306a36Sopenharmony_ci} 103162306a36Sopenharmony_ci 103262306a36Sopenharmony_ci 103362306a36Sopenharmony_ci 103462306a36Sopenharmony_ciSTATIC int INIT inflate_block( 103562306a36Sopenharmony_ci int *e /* last block flag */ 103662306a36Sopenharmony_ci ) 103762306a36Sopenharmony_ci/* decompress an inflated block */ 103862306a36Sopenharmony_ci{ 103962306a36Sopenharmony_ci unsigned t; /* block type */ 104062306a36Sopenharmony_ci register ulg b; /* bit buffer */ 104162306a36Sopenharmony_ci register unsigned k; /* number of bits in bit buffer */ 104262306a36Sopenharmony_ci 104362306a36Sopenharmony_ci DEBG("<blk"); 104462306a36Sopenharmony_ci 104562306a36Sopenharmony_ci /* make local bit buffer */ 104662306a36Sopenharmony_ci b = bb; 104762306a36Sopenharmony_ci k = bk; 104862306a36Sopenharmony_ci 104962306a36Sopenharmony_ci 105062306a36Sopenharmony_ci /* read in last block bit */ 105162306a36Sopenharmony_ci NEEDBITS(1) 105262306a36Sopenharmony_ci *e = (int)b & 1; 105362306a36Sopenharmony_ci DUMPBITS(1) 105462306a36Sopenharmony_ci 105562306a36Sopenharmony_ci 105662306a36Sopenharmony_ci /* read in block type */ 105762306a36Sopenharmony_ci NEEDBITS(2) 105862306a36Sopenharmony_ci t = (unsigned)b & 3; 105962306a36Sopenharmony_ci DUMPBITS(2) 106062306a36Sopenharmony_ci 106162306a36Sopenharmony_ci 106262306a36Sopenharmony_ci /* restore the global bit buffer */ 106362306a36Sopenharmony_ci bb = b; 106462306a36Sopenharmony_ci bk = k; 106562306a36Sopenharmony_ci 106662306a36Sopenharmony_ci /* inflate that block type */ 106762306a36Sopenharmony_ci if (t == 2) 106862306a36Sopenharmony_ci return inflate_dynamic(); 106962306a36Sopenharmony_ci if (t == 0) 107062306a36Sopenharmony_ci return inflate_stored(); 107162306a36Sopenharmony_ci if (t == 1) 107262306a36Sopenharmony_ci return inflate_fixed(); 107362306a36Sopenharmony_ci 107462306a36Sopenharmony_ci DEBG(">"); 107562306a36Sopenharmony_ci 107662306a36Sopenharmony_ci /* bad block type */ 107762306a36Sopenharmony_ci return 2; 107862306a36Sopenharmony_ci 107962306a36Sopenharmony_ci underrun: 108062306a36Sopenharmony_ci return 4; /* Input underrun */ 108162306a36Sopenharmony_ci} 108262306a36Sopenharmony_ci 108362306a36Sopenharmony_ci 108462306a36Sopenharmony_ci 108562306a36Sopenharmony_ciSTATIC int INIT inflate(void) 108662306a36Sopenharmony_ci/* decompress an inflated entry */ 108762306a36Sopenharmony_ci{ 108862306a36Sopenharmony_ci int e; /* last block flag */ 108962306a36Sopenharmony_ci int r; /* result code */ 109062306a36Sopenharmony_ci unsigned h; /* maximum struct huft's malloc'ed */ 109162306a36Sopenharmony_ci 109262306a36Sopenharmony_ci /* initialize window, bit buffer */ 109362306a36Sopenharmony_ci wp = 0; 109462306a36Sopenharmony_ci bk = 0; 109562306a36Sopenharmony_ci bb = 0; 109662306a36Sopenharmony_ci 109762306a36Sopenharmony_ci 109862306a36Sopenharmony_ci /* decompress until the last block */ 109962306a36Sopenharmony_ci h = 0; 110062306a36Sopenharmony_ci do { 110162306a36Sopenharmony_ci hufts = 0; 110262306a36Sopenharmony_ci#ifdef ARCH_HAS_DECOMP_WDOG 110362306a36Sopenharmony_ci arch_decomp_wdog(); 110462306a36Sopenharmony_ci#endif 110562306a36Sopenharmony_ci r = inflate_block(&e); 110662306a36Sopenharmony_ci if (r) 110762306a36Sopenharmony_ci return r; 110862306a36Sopenharmony_ci if (hufts > h) 110962306a36Sopenharmony_ci h = hufts; 111062306a36Sopenharmony_ci } while (!e); 111162306a36Sopenharmony_ci 111262306a36Sopenharmony_ci /* Undo too much lookahead. The next read will be byte aligned so we 111362306a36Sopenharmony_ci * can discard unused bits in the last meaningful byte. 111462306a36Sopenharmony_ci */ 111562306a36Sopenharmony_ci while (bk >= 8) { 111662306a36Sopenharmony_ci bk -= 8; 111762306a36Sopenharmony_ci inptr--; 111862306a36Sopenharmony_ci } 111962306a36Sopenharmony_ci 112062306a36Sopenharmony_ci /* flush out slide */ 112162306a36Sopenharmony_ci flush_output(wp); 112262306a36Sopenharmony_ci 112362306a36Sopenharmony_ci 112462306a36Sopenharmony_ci /* return success */ 112562306a36Sopenharmony_ci#ifdef DEBUG 112662306a36Sopenharmony_ci fprintf(stderr, "<%u> ", h); 112762306a36Sopenharmony_ci#endif /* DEBUG */ 112862306a36Sopenharmony_ci return 0; 112962306a36Sopenharmony_ci} 113062306a36Sopenharmony_ci 113162306a36Sopenharmony_ci/********************************************************************** 113262306a36Sopenharmony_ci * 113362306a36Sopenharmony_ci * The following are support routines for inflate.c 113462306a36Sopenharmony_ci * 113562306a36Sopenharmony_ci **********************************************************************/ 113662306a36Sopenharmony_ci 113762306a36Sopenharmony_cistatic ulg crc_32_tab[256]; 113862306a36Sopenharmony_cistatic ulg crc; /* initialized in makecrc() so it'll reside in bss */ 113962306a36Sopenharmony_ci#define CRC_VALUE (crc ^ 0xffffffffUL) 114062306a36Sopenharmony_ci 114162306a36Sopenharmony_ci/* 114262306a36Sopenharmony_ci * Code to compute the CRC-32 table. Borrowed from 114362306a36Sopenharmony_ci * gzip-1.0.3/makecrc.c. 114462306a36Sopenharmony_ci */ 114562306a36Sopenharmony_ci 114662306a36Sopenharmony_cistatic void INIT 114762306a36Sopenharmony_cimakecrc(void) 114862306a36Sopenharmony_ci{ 114962306a36Sopenharmony_ci/* Not copyrighted 1990 Mark Adler */ 115062306a36Sopenharmony_ci 115162306a36Sopenharmony_ci unsigned long c; /* crc shift register */ 115262306a36Sopenharmony_ci unsigned long e; /* polynomial exclusive-or pattern */ 115362306a36Sopenharmony_ci int i; /* counter for all possible eight bit values */ 115462306a36Sopenharmony_ci int k; /* byte being shifted into crc apparatus */ 115562306a36Sopenharmony_ci 115662306a36Sopenharmony_ci /* terms of polynomial defining this crc (except x^32): */ 115762306a36Sopenharmony_ci static const int p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26}; 115862306a36Sopenharmony_ci 115962306a36Sopenharmony_ci /* Make exclusive-or pattern from polynomial */ 116062306a36Sopenharmony_ci e = 0; 116162306a36Sopenharmony_ci for (i = 0; i < sizeof(p)/sizeof(int); i++) 116262306a36Sopenharmony_ci e |= 1L << (31 - p[i]); 116362306a36Sopenharmony_ci 116462306a36Sopenharmony_ci crc_32_tab[0] = 0; 116562306a36Sopenharmony_ci 116662306a36Sopenharmony_ci for (i = 1; i < 256; i++) 116762306a36Sopenharmony_ci { 116862306a36Sopenharmony_ci c = 0; 116962306a36Sopenharmony_ci for (k = i | 256; k != 1; k >>= 1) 117062306a36Sopenharmony_ci { 117162306a36Sopenharmony_ci c = c & 1 ? (c >> 1) ^ e : c >> 1; 117262306a36Sopenharmony_ci if (k & 1) 117362306a36Sopenharmony_ci c ^= e; 117462306a36Sopenharmony_ci } 117562306a36Sopenharmony_ci crc_32_tab[i] = c; 117662306a36Sopenharmony_ci } 117762306a36Sopenharmony_ci 117862306a36Sopenharmony_ci /* this is initialized here so this code could reside in ROM */ 117962306a36Sopenharmony_ci crc = (ulg)0xffffffffUL; /* shift register contents */ 118062306a36Sopenharmony_ci} 118162306a36Sopenharmony_ci 118262306a36Sopenharmony_ci/* gzip flag byte */ 118362306a36Sopenharmony_ci#define ASCII_FLAG 0x01 /* bit 0 set: file probably ASCII text */ 118462306a36Sopenharmony_ci#define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */ 118562306a36Sopenharmony_ci#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */ 118662306a36Sopenharmony_ci#define ORIG_NAME 0x08 /* bit 3 set: original file name present */ 118762306a36Sopenharmony_ci#define COMMENT 0x10 /* bit 4 set: file comment present */ 118862306a36Sopenharmony_ci#define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */ 118962306a36Sopenharmony_ci#define RESERVED 0xC0 /* bit 6,7: reserved */ 119062306a36Sopenharmony_ci 119162306a36Sopenharmony_ci/* 119262306a36Sopenharmony_ci * Do the uncompression! 119362306a36Sopenharmony_ci */ 119462306a36Sopenharmony_cistatic int INIT gunzip(void) 119562306a36Sopenharmony_ci{ 119662306a36Sopenharmony_ci uch flags; 119762306a36Sopenharmony_ci unsigned char magic[2]; /* magic header */ 119862306a36Sopenharmony_ci char method; 119962306a36Sopenharmony_ci ulg orig_crc = 0; /* original crc */ 120062306a36Sopenharmony_ci ulg orig_len = 0; /* original uncompressed length */ 120162306a36Sopenharmony_ci int res; 120262306a36Sopenharmony_ci 120362306a36Sopenharmony_ci magic[0] = NEXTBYTE(); 120462306a36Sopenharmony_ci magic[1] = NEXTBYTE(); 120562306a36Sopenharmony_ci method = NEXTBYTE(); 120662306a36Sopenharmony_ci 120762306a36Sopenharmony_ci if (magic[0] != 037 || 120862306a36Sopenharmony_ci ((magic[1] != 0213) && (magic[1] != 0236))) { 120962306a36Sopenharmony_ci error("bad gzip magic numbers"); 121062306a36Sopenharmony_ci return -1; 121162306a36Sopenharmony_ci } 121262306a36Sopenharmony_ci 121362306a36Sopenharmony_ci /* We only support method #8, DEFLATED */ 121462306a36Sopenharmony_ci if (method != 8) { 121562306a36Sopenharmony_ci error("internal error, invalid method"); 121662306a36Sopenharmony_ci return -1; 121762306a36Sopenharmony_ci } 121862306a36Sopenharmony_ci 121962306a36Sopenharmony_ci flags = (uch)get_byte(); 122062306a36Sopenharmony_ci if ((flags & ENCRYPTED) != 0) { 122162306a36Sopenharmony_ci error("Input is encrypted"); 122262306a36Sopenharmony_ci return -1; 122362306a36Sopenharmony_ci } 122462306a36Sopenharmony_ci if ((flags & CONTINUATION) != 0) { 122562306a36Sopenharmony_ci error("Multi part input"); 122662306a36Sopenharmony_ci return -1; 122762306a36Sopenharmony_ci } 122862306a36Sopenharmony_ci if ((flags & RESERVED) != 0) { 122962306a36Sopenharmony_ci error("Input has invalid flags"); 123062306a36Sopenharmony_ci return -1; 123162306a36Sopenharmony_ci } 123262306a36Sopenharmony_ci NEXTBYTE(); /* Get timestamp */ 123362306a36Sopenharmony_ci NEXTBYTE(); 123462306a36Sopenharmony_ci NEXTBYTE(); 123562306a36Sopenharmony_ci NEXTBYTE(); 123662306a36Sopenharmony_ci 123762306a36Sopenharmony_ci (void)NEXTBYTE(); /* Ignore extra flags for the moment */ 123862306a36Sopenharmony_ci (void)NEXTBYTE(); /* Ignore OS type for the moment */ 123962306a36Sopenharmony_ci 124062306a36Sopenharmony_ci if ((flags & EXTRA_FIELD) != 0) { 124162306a36Sopenharmony_ci unsigned len = (unsigned)NEXTBYTE(); 124262306a36Sopenharmony_ci len |= ((unsigned)NEXTBYTE())<<8; 124362306a36Sopenharmony_ci while (len--) (void)NEXTBYTE(); 124462306a36Sopenharmony_ci } 124562306a36Sopenharmony_ci 124662306a36Sopenharmony_ci /* Get original file name if it was truncated */ 124762306a36Sopenharmony_ci if ((flags & ORIG_NAME) != 0) { 124862306a36Sopenharmony_ci /* Discard the old name */ 124962306a36Sopenharmony_ci while (NEXTBYTE() != 0) /* null */ ; 125062306a36Sopenharmony_ci } 125162306a36Sopenharmony_ci 125262306a36Sopenharmony_ci /* Discard file comment if any */ 125362306a36Sopenharmony_ci if ((flags & COMMENT) != 0) { 125462306a36Sopenharmony_ci while (NEXTBYTE() != 0) /* null */ ; 125562306a36Sopenharmony_ci } 125662306a36Sopenharmony_ci 125762306a36Sopenharmony_ci /* Decompress */ 125862306a36Sopenharmony_ci if ((res = inflate())) { 125962306a36Sopenharmony_ci switch (res) { 126062306a36Sopenharmony_ci case 0: 126162306a36Sopenharmony_ci break; 126262306a36Sopenharmony_ci case 1: 126362306a36Sopenharmony_ci error("invalid compressed format (err=1)"); 126462306a36Sopenharmony_ci break; 126562306a36Sopenharmony_ci case 2: 126662306a36Sopenharmony_ci error("invalid compressed format (err=2)"); 126762306a36Sopenharmony_ci break; 126862306a36Sopenharmony_ci case 3: 126962306a36Sopenharmony_ci error("out of memory"); 127062306a36Sopenharmony_ci break; 127162306a36Sopenharmony_ci case 4: 127262306a36Sopenharmony_ci error("out of input data"); 127362306a36Sopenharmony_ci break; 127462306a36Sopenharmony_ci default: 127562306a36Sopenharmony_ci error("invalid compressed format (other)"); 127662306a36Sopenharmony_ci } 127762306a36Sopenharmony_ci return -1; 127862306a36Sopenharmony_ci } 127962306a36Sopenharmony_ci 128062306a36Sopenharmony_ci /* Get the crc and original length */ 128162306a36Sopenharmony_ci /* crc32 (see algorithm.doc) 128262306a36Sopenharmony_ci * uncompressed input size modulo 2^32 128362306a36Sopenharmony_ci */ 128462306a36Sopenharmony_ci orig_crc = (ulg) NEXTBYTE(); 128562306a36Sopenharmony_ci orig_crc |= (ulg) NEXTBYTE() << 8; 128662306a36Sopenharmony_ci orig_crc |= (ulg) NEXTBYTE() << 16; 128762306a36Sopenharmony_ci orig_crc |= (ulg) NEXTBYTE() << 24; 128862306a36Sopenharmony_ci 128962306a36Sopenharmony_ci orig_len = (ulg) NEXTBYTE(); 129062306a36Sopenharmony_ci orig_len |= (ulg) NEXTBYTE() << 8; 129162306a36Sopenharmony_ci orig_len |= (ulg) NEXTBYTE() << 16; 129262306a36Sopenharmony_ci orig_len |= (ulg) NEXTBYTE() << 24; 129362306a36Sopenharmony_ci 129462306a36Sopenharmony_ci /* Validate decompression */ 129562306a36Sopenharmony_ci if (orig_crc != CRC_VALUE) { 129662306a36Sopenharmony_ci error("crc error"); 129762306a36Sopenharmony_ci return -1; 129862306a36Sopenharmony_ci } 129962306a36Sopenharmony_ci if (orig_len != bytes_out) { 130062306a36Sopenharmony_ci error("length error"); 130162306a36Sopenharmony_ci return -1; 130262306a36Sopenharmony_ci } 130362306a36Sopenharmony_ci return 0; 130462306a36Sopenharmony_ci 130562306a36Sopenharmony_ci underrun: /* NEXTBYTE() goto's here if needed */ 130662306a36Sopenharmony_ci error("out of input data"); 130762306a36Sopenharmony_ci return -1; 130862306a36Sopenharmony_ci} 130962306a36Sopenharmony_ci 131062306a36Sopenharmony_ci 1311