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