blast.c revision 1.1.1.1.2.2 1 1.1.1.1.2.2 pgoyette /* blast.c
2 1.1.1.1.2.2 pgoyette * Copyright (C) 2003 Mark Adler
3 1.1.1.1.2.2 pgoyette * For conditions of distribution and use, see copyright notice in blast.h
4 1.1.1.1.2.2 pgoyette * version 1.1, 16 Feb 2003
5 1.1.1.1.2.2 pgoyette *
6 1.1.1.1.2.2 pgoyette * blast.c decompresses data compressed by the PKWare Compression Library.
7 1.1.1.1.2.2 pgoyette * This function provides functionality similar to the explode() function of
8 1.1.1.1.2.2 pgoyette * the PKWare library, hence the name "blast".
9 1.1.1.1.2.2 pgoyette *
10 1.1.1.1.2.2 pgoyette * This decompressor is based on the excellent format description provided by
11 1.1.1.1.2.2 pgoyette * Ben Rudiak-Gould in comp.compression on August 13, 2001. Interestingly, the
12 1.1.1.1.2.2 pgoyette * example Ben provided in the post is incorrect. The distance 110001 should
13 1.1.1.1.2.2 pgoyette * instead be 111000. When corrected, the example byte stream becomes:
14 1.1.1.1.2.2 pgoyette *
15 1.1.1.1.2.2 pgoyette * 00 04 82 24 25 8f 80 7f
16 1.1.1.1.2.2 pgoyette *
17 1.1.1.1.2.2 pgoyette * which decompresses to "AIAIAIAIAIAIA" (without the quotes).
18 1.1.1.1.2.2 pgoyette */
19 1.1.1.1.2.2 pgoyette
20 1.1.1.1.2.2 pgoyette /*
21 1.1.1.1.2.2 pgoyette * Change history:
22 1.1.1.1.2.2 pgoyette *
23 1.1.1.1.2.2 pgoyette * 1.0 12 Feb 2003 - First version
24 1.1.1.1.2.2 pgoyette * 1.1 16 Feb 2003 - Fixed distance check for > 4 GB uncompressed data
25 1.1.1.1.2.2 pgoyette */
26 1.1.1.1.2.2 pgoyette
27 1.1.1.1.2.2 pgoyette #include <setjmp.h> /* for setjmp(), longjmp(), and jmp_buf */
28 1.1.1.1.2.2 pgoyette #include "blast.h" /* prototype for blast() */
29 1.1.1.1.2.2 pgoyette
30 1.1.1.1.2.2 pgoyette #define local static /* for local function definitions */
31 1.1.1.1.2.2 pgoyette #define MAXBITS 13 /* maximum code length */
32 1.1.1.1.2.2 pgoyette #define MAXWIN 4096 /* maximum window size */
33 1.1.1.1.2.2 pgoyette
34 1.1.1.1.2.2 pgoyette /* input and output state */
35 1.1.1.1.2.2 pgoyette struct state {
36 1.1.1.1.2.2 pgoyette /* input state */
37 1.1.1.1.2.2 pgoyette blast_in infun; /* input function provided by user */
38 1.1.1.1.2.2 pgoyette void *inhow; /* opaque information passed to infun() */
39 1.1.1.1.2.2 pgoyette unsigned char *in; /* next input location */
40 1.1.1.1.2.2 pgoyette unsigned left; /* available input at in */
41 1.1.1.1.2.2 pgoyette int bitbuf; /* bit buffer */
42 1.1.1.1.2.2 pgoyette int bitcnt; /* number of bits in bit buffer */
43 1.1.1.1.2.2 pgoyette
44 1.1.1.1.2.2 pgoyette /* input limit error return state for bits() and decode() */
45 1.1.1.1.2.2 pgoyette jmp_buf env;
46 1.1.1.1.2.2 pgoyette
47 1.1.1.1.2.2 pgoyette /* output state */
48 1.1.1.1.2.2 pgoyette blast_out outfun; /* output function provided by user */
49 1.1.1.1.2.2 pgoyette void *outhow; /* opaque information passed to outfun() */
50 1.1.1.1.2.2 pgoyette unsigned next; /* index of next write location in out[] */
51 1.1.1.1.2.2 pgoyette int first; /* true to check distances (for first 4K) */
52 1.1.1.1.2.2 pgoyette unsigned char out[MAXWIN]; /* output buffer and sliding window */
53 1.1.1.1.2.2 pgoyette };
54 1.1.1.1.2.2 pgoyette
55 1.1.1.1.2.2 pgoyette /*
56 1.1.1.1.2.2 pgoyette * Return need bits from the input stream. This always leaves less than
57 1.1.1.1.2.2 pgoyette * eight bits in the buffer. bits() works properly for need == 0.
58 1.1.1.1.2.2 pgoyette *
59 1.1.1.1.2.2 pgoyette * Format notes:
60 1.1.1.1.2.2 pgoyette *
61 1.1.1.1.2.2 pgoyette * - Bits are stored in bytes from the least significant bit to the most
62 1.1.1.1.2.2 pgoyette * significant bit. Therefore bits are dropped from the bottom of the bit
63 1.1.1.1.2.2 pgoyette * buffer, using shift right, and new bytes are appended to the top of the
64 1.1.1.1.2.2 pgoyette * bit buffer, using shift left.
65 1.1.1.1.2.2 pgoyette */
66 1.1.1.1.2.2 pgoyette local int bits(struct state *s, int need)
67 1.1.1.1.2.2 pgoyette {
68 1.1.1.1.2.2 pgoyette int val; /* bit accumulator */
69 1.1.1.1.2.2 pgoyette
70 1.1.1.1.2.2 pgoyette /* load at least need bits into val */
71 1.1.1.1.2.2 pgoyette val = s->bitbuf;
72 1.1.1.1.2.2 pgoyette while (s->bitcnt < need) {
73 1.1.1.1.2.2 pgoyette if (s->left == 0) {
74 1.1.1.1.2.2 pgoyette s->left = s->infun(s->inhow, &(s->in));
75 1.1.1.1.2.2 pgoyette if (s->left == 0) longjmp(s->env, 1); /* out of input */
76 1.1.1.1.2.2 pgoyette }
77 1.1.1.1.2.2 pgoyette val |= (int)(*(s->in)++) << s->bitcnt; /* load eight bits */
78 1.1.1.1.2.2 pgoyette s->left--;
79 1.1.1.1.2.2 pgoyette s->bitcnt += 8;
80 1.1.1.1.2.2 pgoyette }
81 1.1.1.1.2.2 pgoyette
82 1.1.1.1.2.2 pgoyette /* drop need bits and update buffer, always zero to seven bits left */
83 1.1.1.1.2.2 pgoyette s->bitbuf = val >> need;
84 1.1.1.1.2.2 pgoyette s->bitcnt -= need;
85 1.1.1.1.2.2 pgoyette
86 1.1.1.1.2.2 pgoyette /* return need bits, zeroing the bits above that */
87 1.1.1.1.2.2 pgoyette return val & ((1 << need) - 1);
88 1.1.1.1.2.2 pgoyette }
89 1.1.1.1.2.2 pgoyette
90 1.1.1.1.2.2 pgoyette /*
91 1.1.1.1.2.2 pgoyette * Huffman code decoding tables. count[1..MAXBITS] is the number of symbols of
92 1.1.1.1.2.2 pgoyette * each length, which for a canonical code are stepped through in order.
93 1.1.1.1.2.2 pgoyette * symbol[] are the symbol values in canonical order, where the number of
94 1.1.1.1.2.2 pgoyette * entries is the sum of the counts in count[]. The decoding process can be
95 1.1.1.1.2.2 pgoyette * seen in the function decode() below.
96 1.1.1.1.2.2 pgoyette */
97 1.1.1.1.2.2 pgoyette struct huffman {
98 1.1.1.1.2.2 pgoyette short *count; /* number of symbols of each length */
99 1.1.1.1.2.2 pgoyette short *symbol; /* canonically ordered symbols */
100 1.1.1.1.2.2 pgoyette };
101 1.1.1.1.2.2 pgoyette
102 1.1.1.1.2.2 pgoyette /*
103 1.1.1.1.2.2 pgoyette * Decode a code from the stream s using huffman table h. Return the symbol or
104 1.1.1.1.2.2 pgoyette * a negative value if there is an error. If all of the lengths are zero, i.e.
105 1.1.1.1.2.2 pgoyette * an empty code, or if the code is incomplete and an invalid code is received,
106 1.1.1.1.2.2 pgoyette * then -9 is returned after reading MAXBITS bits.
107 1.1.1.1.2.2 pgoyette *
108 1.1.1.1.2.2 pgoyette * Format notes:
109 1.1.1.1.2.2 pgoyette *
110 1.1.1.1.2.2 pgoyette * - The codes as stored in the compressed data are bit-reversed relative to
111 1.1.1.1.2.2 pgoyette * a simple integer ordering of codes of the same lengths. Hence below the
112 1.1.1.1.2.2 pgoyette * bits are pulled from the compressed data one at a time and used to
113 1.1.1.1.2.2 pgoyette * build the code value reversed from what is in the stream in order to
114 1.1.1.1.2.2 pgoyette * permit simple integer comparisons for decoding.
115 1.1.1.1.2.2 pgoyette *
116 1.1.1.1.2.2 pgoyette * - The first code for the shortest length is all ones. Subsequent codes of
117 1.1.1.1.2.2 pgoyette * the same length are simply integer decrements of the previous code. When
118 1.1.1.1.2.2 pgoyette * moving up a length, a one bit is appended to the code. For a complete
119 1.1.1.1.2.2 pgoyette * code, the last code of the longest length will be all zeros. To support
120 1.1.1.1.2.2 pgoyette * this ordering, the bits pulled during decoding are inverted to apply the
121 1.1.1.1.2.2 pgoyette * more "natural" ordering starting with all zeros and incrementing.
122 1.1.1.1.2.2 pgoyette */
123 1.1.1.1.2.2 pgoyette local int decode(struct state *s, struct huffman *h)
124 1.1.1.1.2.2 pgoyette {
125 1.1.1.1.2.2 pgoyette int len; /* current number of bits in code */
126 1.1.1.1.2.2 pgoyette int code; /* len bits being decoded */
127 1.1.1.1.2.2 pgoyette int first; /* first code of length len */
128 1.1.1.1.2.2 pgoyette int count; /* number of codes of length len */
129 1.1.1.1.2.2 pgoyette int index; /* index of first code of length len in symbol table */
130 1.1.1.1.2.2 pgoyette int bitbuf; /* bits from stream */
131 1.1.1.1.2.2 pgoyette int left; /* bits left in next or left to process */
132 1.1.1.1.2.2 pgoyette short *next; /* next number of codes */
133 1.1.1.1.2.2 pgoyette
134 1.1.1.1.2.2 pgoyette bitbuf = s->bitbuf;
135 1.1.1.1.2.2 pgoyette left = s->bitcnt;
136 1.1.1.1.2.2 pgoyette code = first = index = 0;
137 1.1.1.1.2.2 pgoyette len = 1;
138 1.1.1.1.2.2 pgoyette next = h->count + 1;
139 1.1.1.1.2.2 pgoyette while (1) {
140 1.1.1.1.2.2 pgoyette while (left--) {
141 1.1.1.1.2.2 pgoyette code |= (bitbuf & 1) ^ 1; /* invert code */
142 1.1.1.1.2.2 pgoyette bitbuf >>= 1;
143 1.1.1.1.2.2 pgoyette count = *next++;
144 1.1.1.1.2.2 pgoyette if (code < first + count) { /* if length len, return symbol */
145 1.1.1.1.2.2 pgoyette s->bitbuf = bitbuf;
146 1.1.1.1.2.2 pgoyette s->bitcnt = (s->bitcnt - len) & 7;
147 1.1.1.1.2.2 pgoyette return h->symbol[index + (code - first)];
148 1.1.1.1.2.2 pgoyette }
149 1.1.1.1.2.2 pgoyette index += count; /* else update for next length */
150 1.1.1.1.2.2 pgoyette first += count;
151 1.1.1.1.2.2 pgoyette first <<= 1;
152 1.1.1.1.2.2 pgoyette code <<= 1;
153 1.1.1.1.2.2 pgoyette len++;
154 1.1.1.1.2.2 pgoyette }
155 1.1.1.1.2.2 pgoyette left = (MAXBITS+1) - len;
156 1.1.1.1.2.2 pgoyette if (left == 0) break;
157 1.1.1.1.2.2 pgoyette if (s->left == 0) {
158 1.1.1.1.2.2 pgoyette s->left = s->infun(s->inhow, &(s->in));
159 1.1.1.1.2.2 pgoyette if (s->left == 0) longjmp(s->env, 1); /* out of input */
160 1.1.1.1.2.2 pgoyette }
161 1.1.1.1.2.2 pgoyette bitbuf = *(s->in)++;
162 1.1.1.1.2.2 pgoyette s->left--;
163 1.1.1.1.2.2 pgoyette if (left > 8) left = 8;
164 1.1.1.1.2.2 pgoyette }
165 1.1.1.1.2.2 pgoyette return -9; /* ran out of codes */
166 1.1.1.1.2.2 pgoyette }
167 1.1.1.1.2.2 pgoyette
168 1.1.1.1.2.2 pgoyette /*
169 1.1.1.1.2.2 pgoyette * Given a list of repeated code lengths rep[0..n-1], where each byte is a
170 1.1.1.1.2.2 pgoyette * count (high four bits + 1) and a code length (low four bits), generate the
171 1.1.1.1.2.2 pgoyette * list of code lengths. This compaction reduces the size of the object code.
172 1.1.1.1.2.2 pgoyette * Then given the list of code lengths length[0..n-1] representing a canonical
173 1.1.1.1.2.2 pgoyette * Huffman code for n symbols, construct the tables required to decode those
174 1.1.1.1.2.2 pgoyette * codes. Those tables are the number of codes of each length, and the symbols
175 1.1.1.1.2.2 pgoyette * sorted by length, retaining their original order within each length. The
176 1.1.1.1.2.2 pgoyette * return value is zero for a complete code set, negative for an over-
177 1.1.1.1.2.2 pgoyette * subscribed code set, and positive for an incomplete code set. The tables
178 1.1.1.1.2.2 pgoyette * can be used if the return value is zero or positive, but they cannot be used
179 1.1.1.1.2.2 pgoyette * if the return value is negative. If the return value is zero, it is not
180 1.1.1.1.2.2 pgoyette * possible for decode() using that table to return an error--any stream of
181 1.1.1.1.2.2 pgoyette * enough bits will resolve to a symbol. If the return value is positive, then
182 1.1.1.1.2.2 pgoyette * it is possible for decode() using that table to return an error for received
183 1.1.1.1.2.2 pgoyette * codes past the end of the incomplete lengths.
184 1.1.1.1.2.2 pgoyette */
185 1.1.1.1.2.2 pgoyette local int construct(struct huffman *h, const unsigned char *rep, int n)
186 1.1.1.1.2.2 pgoyette {
187 1.1.1.1.2.2 pgoyette int symbol; /* current symbol when stepping through length[] */
188 1.1.1.1.2.2 pgoyette int len; /* current length when stepping through h->count[] */
189 1.1.1.1.2.2 pgoyette int left; /* number of possible codes left of current length */
190 1.1.1.1.2.2 pgoyette short offs[MAXBITS+1]; /* offsets in symbol table for each length */
191 1.1.1.1.2.2 pgoyette short length[256]; /* code lengths */
192 1.1.1.1.2.2 pgoyette
193 1.1.1.1.2.2 pgoyette /* convert compact repeat counts into symbol bit length list */
194 1.1.1.1.2.2 pgoyette symbol = 0;
195 1.1.1.1.2.2 pgoyette do {
196 1.1.1.1.2.2 pgoyette len = *rep++;
197 1.1.1.1.2.2 pgoyette left = (len >> 4) + 1;
198 1.1.1.1.2.2 pgoyette len &= 15;
199 1.1.1.1.2.2 pgoyette do {
200 1.1.1.1.2.2 pgoyette length[symbol++] = len;
201 1.1.1.1.2.2 pgoyette } while (--left);
202 1.1.1.1.2.2 pgoyette } while (--n);
203 1.1.1.1.2.2 pgoyette n = symbol;
204 1.1.1.1.2.2 pgoyette
205 1.1.1.1.2.2 pgoyette /* count number of codes of each length */
206 1.1.1.1.2.2 pgoyette for (len = 0; len <= MAXBITS; len++)
207 1.1.1.1.2.2 pgoyette h->count[len] = 0;
208 1.1.1.1.2.2 pgoyette for (symbol = 0; symbol < n; symbol++)
209 1.1.1.1.2.2 pgoyette (h->count[length[symbol]])++; /* assumes lengths are within bounds */
210 1.1.1.1.2.2 pgoyette if (h->count[0] == n) /* no codes! */
211 1.1.1.1.2.2 pgoyette return 0; /* complete, but decode() will fail */
212 1.1.1.1.2.2 pgoyette
213 1.1.1.1.2.2 pgoyette /* check for an over-subscribed or incomplete set of lengths */
214 1.1.1.1.2.2 pgoyette left = 1; /* one possible code of zero length */
215 1.1.1.1.2.2 pgoyette for (len = 1; len <= MAXBITS; len++) {
216 1.1.1.1.2.2 pgoyette left <<= 1; /* one more bit, double codes left */
217 1.1.1.1.2.2 pgoyette left -= h->count[len]; /* deduct count from possible codes */
218 1.1.1.1.2.2 pgoyette if (left < 0) return left; /* over-subscribed--return negative */
219 1.1.1.1.2.2 pgoyette } /* left > 0 means incomplete */
220 1.1.1.1.2.2 pgoyette
221 1.1.1.1.2.2 pgoyette /* generate offsets into symbol table for each length for sorting */
222 1.1.1.1.2.2 pgoyette offs[1] = 0;
223 1.1.1.1.2.2 pgoyette for (len = 1; len < MAXBITS; len++)
224 1.1.1.1.2.2 pgoyette offs[len + 1] = offs[len] + h->count[len];
225 1.1.1.1.2.2 pgoyette
226 1.1.1.1.2.2 pgoyette /*
227 1.1.1.1.2.2 pgoyette * put symbols in table sorted by length, by symbol order within each
228 1.1.1.1.2.2 pgoyette * length
229 1.1.1.1.2.2 pgoyette */
230 1.1.1.1.2.2 pgoyette for (symbol = 0; symbol < n; symbol++)
231 1.1.1.1.2.2 pgoyette if (length[symbol] != 0)
232 1.1.1.1.2.2 pgoyette h->symbol[offs[length[symbol]]++] = symbol;
233 1.1.1.1.2.2 pgoyette
234 1.1.1.1.2.2 pgoyette /* return zero for complete set, positive for incomplete set */
235 1.1.1.1.2.2 pgoyette return left;
236 1.1.1.1.2.2 pgoyette }
237 1.1.1.1.2.2 pgoyette
238 1.1.1.1.2.2 pgoyette /*
239 1.1.1.1.2.2 pgoyette * Decode PKWare Compression Library stream.
240 1.1.1.1.2.2 pgoyette *
241 1.1.1.1.2.2 pgoyette * Format notes:
242 1.1.1.1.2.2 pgoyette *
243 1.1.1.1.2.2 pgoyette * - First byte is 0 if literals are uncoded or 1 if they are coded. Second
244 1.1.1.1.2.2 pgoyette * byte is 4, 5, or 6 for the number of extra bits in the distance code.
245 1.1.1.1.2.2 pgoyette * This is the base-2 logarithm of the dictionary size minus six.
246 1.1.1.1.2.2 pgoyette *
247 1.1.1.1.2.2 pgoyette * - Compressed data is a combination of literals and length/distance pairs
248 1.1.1.1.2.2 pgoyette * terminated by an end code. Literals are either Huffman coded or
249 1.1.1.1.2.2 pgoyette * uncoded bytes. A length/distance pair is a coded length followed by a
250 1.1.1.1.2.2 pgoyette * coded distance to represent a string that occurs earlier in the
251 1.1.1.1.2.2 pgoyette * uncompressed data that occurs again at the current location.
252 1.1.1.1.2.2 pgoyette *
253 1.1.1.1.2.2 pgoyette * - A bit preceding a literal or length/distance pair indicates which comes
254 1.1.1.1.2.2 pgoyette * next, 0 for literals, 1 for length/distance.
255 1.1.1.1.2.2 pgoyette *
256 1.1.1.1.2.2 pgoyette * - If literals are uncoded, then the next eight bits are the literal, in the
257 1.1.1.1.2.2 pgoyette * normal bit order in th stream, i.e. no bit-reversal is needed. Similarly,
258 1.1.1.1.2.2 pgoyette * no bit reversal is needed for either the length extra bits or the distance
259 1.1.1.1.2.2 pgoyette * extra bits.
260 1.1.1.1.2.2 pgoyette *
261 1.1.1.1.2.2 pgoyette * - Literal bytes are simply written to the output. A length/distance pair is
262 1.1.1.1.2.2 pgoyette * an instruction to copy previously uncompressed bytes to the output. The
263 1.1.1.1.2.2 pgoyette * copy is from distance bytes back in the output stream, copying for length
264 1.1.1.1.2.2 pgoyette * bytes.
265 1.1.1.1.2.2 pgoyette *
266 1.1.1.1.2.2 pgoyette * - Distances pointing before the beginning of the output data are not
267 1.1.1.1.2.2 pgoyette * permitted.
268 1.1.1.1.2.2 pgoyette *
269 1.1.1.1.2.2 pgoyette * - Overlapped copies, where the length is greater than the distance, are
270 1.1.1.1.2.2 pgoyette * allowed and common. For example, a distance of one and a length of 518
271 1.1.1.1.2.2 pgoyette * simply copies the last byte 518 times. A distance of four and a length of
272 1.1.1.1.2.2 pgoyette * twelve copies the last four bytes three times. A simple forward copy
273 1.1.1.1.2.2 pgoyette * ignoring whether the length is greater than the distance or not implements
274 1.1.1.1.2.2 pgoyette * this correctly.
275 1.1.1.1.2.2 pgoyette */
276 1.1.1.1.2.2 pgoyette local int decomp(struct state *s)
277 1.1.1.1.2.2 pgoyette {
278 1.1.1.1.2.2 pgoyette int lit; /* true if literals are coded */
279 1.1.1.1.2.2 pgoyette int dict; /* log2(dictionary size) - 6 */
280 1.1.1.1.2.2 pgoyette int symbol; /* decoded symbol, extra bits for distance */
281 1.1.1.1.2.2 pgoyette int len; /* length for copy */
282 1.1.1.1.2.2 pgoyette int dist; /* distance for copy */
283 1.1.1.1.2.2 pgoyette int copy; /* copy counter */
284 1.1.1.1.2.2 pgoyette unsigned char *from, *to; /* copy pointers */
285 1.1.1.1.2.2 pgoyette static int virgin = 1; /* build tables once */
286 1.1.1.1.2.2 pgoyette static short litcnt[MAXBITS+1], litsym[256]; /* litcode memory */
287 1.1.1.1.2.2 pgoyette static short lencnt[MAXBITS+1], lensym[16]; /* lencode memory */
288 1.1.1.1.2.2 pgoyette static short distcnt[MAXBITS+1], distsym[64]; /* distcode memory */
289 1.1.1.1.2.2 pgoyette static struct huffman litcode = {litcnt, litsym}; /* length code */
290 1.1.1.1.2.2 pgoyette static struct huffman lencode = {lencnt, lensym}; /* length code */
291 1.1.1.1.2.2 pgoyette static struct huffman distcode = {distcnt, distsym};/* distance code */
292 1.1.1.1.2.2 pgoyette /* bit lengths of literal codes */
293 1.1.1.1.2.2 pgoyette static const unsigned char litlen[] = {
294 1.1.1.1.2.2 pgoyette 11, 124, 8, 7, 28, 7, 188, 13, 76, 4, 10, 8, 12, 10, 12, 10, 8, 23, 8,
295 1.1.1.1.2.2 pgoyette 9, 7, 6, 7, 8, 7, 6, 55, 8, 23, 24, 12, 11, 7, 9, 11, 12, 6, 7, 22, 5,
296 1.1.1.1.2.2 pgoyette 7, 24, 6, 11, 9, 6, 7, 22, 7, 11, 38, 7, 9, 8, 25, 11, 8, 11, 9, 12,
297 1.1.1.1.2.2 pgoyette 8, 12, 5, 38, 5, 38, 5, 11, 7, 5, 6, 21, 6, 10, 53, 8, 7, 24, 10, 27,
298 1.1.1.1.2.2 pgoyette 44, 253, 253, 253, 252, 252, 252, 13, 12, 45, 12, 45, 12, 61, 12, 45,
299 1.1.1.1.2.2 pgoyette 44, 173};
300 1.1.1.1.2.2 pgoyette /* bit lengths of length codes 0..15 */
301 1.1.1.1.2.2 pgoyette static const unsigned char lenlen[] = {2, 35, 36, 53, 38, 23};
302 1.1.1.1.2.2 pgoyette /* bit lengths of distance codes 0..63 */
303 1.1.1.1.2.2 pgoyette static const unsigned char distlen[] = {2, 20, 53, 230, 247, 151, 248};
304 1.1.1.1.2.2 pgoyette static const short base[16] = { /* base for length codes */
305 1.1.1.1.2.2 pgoyette 3, 2, 4, 5, 6, 7, 8, 9, 10, 12, 16, 24, 40, 72, 136, 264};
306 1.1.1.1.2.2 pgoyette static const char extra[16] = { /* extra bits for length codes */
307 1.1.1.1.2.2 pgoyette 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8};
308 1.1.1.1.2.2 pgoyette
309 1.1.1.1.2.2 pgoyette /* set up decoding tables (once--might not be thread-safe) */
310 1.1.1.1.2.2 pgoyette if (virgin) {
311 1.1.1.1.2.2 pgoyette construct(&litcode, litlen, sizeof(litlen));
312 1.1.1.1.2.2 pgoyette construct(&lencode, lenlen, sizeof(lenlen));
313 1.1.1.1.2.2 pgoyette construct(&distcode, distlen, sizeof(distlen));
314 1.1.1.1.2.2 pgoyette virgin = 0;
315 1.1.1.1.2.2 pgoyette }
316 1.1.1.1.2.2 pgoyette
317 1.1.1.1.2.2 pgoyette /* read header */
318 1.1.1.1.2.2 pgoyette lit = bits(s, 8);
319 1.1.1.1.2.2 pgoyette if (lit > 1) return -1;
320 1.1.1.1.2.2 pgoyette dict = bits(s, 8);
321 1.1.1.1.2.2 pgoyette if (dict < 4 || dict > 6) return -2;
322 1.1.1.1.2.2 pgoyette
323 1.1.1.1.2.2 pgoyette /* decode literals and length/distance pairs */
324 1.1.1.1.2.2 pgoyette do {
325 1.1.1.1.2.2 pgoyette if (bits(s, 1)) {
326 1.1.1.1.2.2 pgoyette /* get length */
327 1.1.1.1.2.2 pgoyette symbol = decode(s, &lencode);
328 1.1.1.1.2.2 pgoyette len = base[symbol] + bits(s, extra[symbol]);
329 1.1.1.1.2.2 pgoyette if (len == 519) break; /* end code */
330 1.1.1.1.2.2 pgoyette
331 1.1.1.1.2.2 pgoyette /* get distance */
332 1.1.1.1.2.2 pgoyette symbol = len == 2 ? 2 : dict;
333 1.1.1.1.2.2 pgoyette dist = decode(s, &distcode) << symbol;
334 1.1.1.1.2.2 pgoyette dist += bits(s, symbol);
335 1.1.1.1.2.2 pgoyette dist++;
336 1.1.1.1.2.2 pgoyette if (s->first && dist > s->next)
337 1.1.1.1.2.2 pgoyette return -3; /* distance too far back */
338 1.1.1.1.2.2 pgoyette
339 1.1.1.1.2.2 pgoyette /* copy length bytes from distance bytes back */
340 1.1.1.1.2.2 pgoyette do {
341 1.1.1.1.2.2 pgoyette to = s->out + s->next;
342 1.1.1.1.2.2 pgoyette from = to - dist;
343 1.1.1.1.2.2 pgoyette copy = MAXWIN;
344 1.1.1.1.2.2 pgoyette if (s->next < dist) {
345 1.1.1.1.2.2 pgoyette from += copy;
346 1.1.1.1.2.2 pgoyette copy = dist;
347 1.1.1.1.2.2 pgoyette }
348 1.1.1.1.2.2 pgoyette copy -= s->next;
349 1.1.1.1.2.2 pgoyette if (copy > len) copy = len;
350 1.1.1.1.2.2 pgoyette len -= copy;
351 1.1.1.1.2.2 pgoyette s->next += copy;
352 1.1.1.1.2.2 pgoyette do {
353 1.1.1.1.2.2 pgoyette *to++ = *from++;
354 1.1.1.1.2.2 pgoyette } while (--copy);
355 1.1.1.1.2.2 pgoyette if (s->next == MAXWIN) {
356 1.1.1.1.2.2 pgoyette if (s->outfun(s->outhow, s->out, s->next)) return 1;
357 1.1.1.1.2.2 pgoyette s->next = 0;
358 1.1.1.1.2.2 pgoyette s->first = 0;
359 1.1.1.1.2.2 pgoyette }
360 1.1.1.1.2.2 pgoyette } while (len != 0);
361 1.1.1.1.2.2 pgoyette }
362 1.1.1.1.2.2 pgoyette else {
363 1.1.1.1.2.2 pgoyette /* get literal and write it */
364 1.1.1.1.2.2 pgoyette symbol = lit ? decode(s, &litcode) : bits(s, 8);
365 1.1.1.1.2.2 pgoyette s->out[s->next++] = symbol;
366 1.1.1.1.2.2 pgoyette if (s->next == MAXWIN) {
367 1.1.1.1.2.2 pgoyette if (s->outfun(s->outhow, s->out, s->next)) return 1;
368 1.1.1.1.2.2 pgoyette s->next = 0;
369 1.1.1.1.2.2 pgoyette s->first = 0;
370 1.1.1.1.2.2 pgoyette }
371 1.1.1.1.2.2 pgoyette }
372 1.1.1.1.2.2 pgoyette } while (1);
373 1.1.1.1.2.2 pgoyette return 0;
374 1.1.1.1.2.2 pgoyette }
375 1.1.1.1.2.2 pgoyette
376 1.1.1.1.2.2 pgoyette /* See comments in blast.h */
377 1.1.1.1.2.2 pgoyette int blast(blast_in infun, void *inhow, blast_out outfun, void *outhow)
378 1.1.1.1.2.2 pgoyette {
379 1.1.1.1.2.2 pgoyette struct state s; /* input/output state */
380 1.1.1.1.2.2 pgoyette int err; /* return value */
381 1.1.1.1.2.2 pgoyette
382 1.1.1.1.2.2 pgoyette /* initialize input state */
383 1.1.1.1.2.2 pgoyette s.infun = infun;
384 1.1.1.1.2.2 pgoyette s.inhow = inhow;
385 1.1.1.1.2.2 pgoyette s.left = 0;
386 1.1.1.1.2.2 pgoyette s.bitbuf = 0;
387 1.1.1.1.2.2 pgoyette s.bitcnt = 0;
388 1.1.1.1.2.2 pgoyette
389 1.1.1.1.2.2 pgoyette /* initialize output state */
390 1.1.1.1.2.2 pgoyette s.outfun = outfun;
391 1.1.1.1.2.2 pgoyette s.outhow = outhow;
392 1.1.1.1.2.2 pgoyette s.next = 0;
393 1.1.1.1.2.2 pgoyette s.first = 1;
394 1.1.1.1.2.2 pgoyette
395 1.1.1.1.2.2 pgoyette /* return if bits() or decode() tries to read past available input */
396 1.1.1.1.2.2 pgoyette if (setjmp(s.env) != 0) /* if came back here via longjmp(), */
397 1.1.1.1.2.2 pgoyette err = 2; /* then skip decomp(), return error */
398 1.1.1.1.2.2 pgoyette else
399 1.1.1.1.2.2 pgoyette err = decomp(&s); /* decompress */
400 1.1.1.1.2.2 pgoyette
401 1.1.1.1.2.2 pgoyette /* write any leftover output and update the error code if needed */
402 1.1.1.1.2.2 pgoyette if (err != 1 && s.next && s.outfun(s.outhow, s.out, s.next) && err == 0)
403 1.1.1.1.2.2 pgoyette err = 1;
404 1.1.1.1.2.2 pgoyette return err;
405 1.1.1.1.2.2 pgoyette }
406 1.1.1.1.2.2 pgoyette
407 1.1.1.1.2.2 pgoyette #ifdef TEST
408 1.1.1.1.2.2 pgoyette /* Example of how to use blast() */
409 1.1.1.1.2.2 pgoyette #include <stdio.h>
410 1.1.1.1.2.2 pgoyette #include <stdlib.h>
411 1.1.1.1.2.2 pgoyette
412 1.1.1.1.2.2 pgoyette #define CHUNK 16384
413 1.1.1.1.2.2 pgoyette
414 1.1.1.1.2.2 pgoyette local unsigned inf(void *how, unsigned char **buf)
415 1.1.1.1.2.2 pgoyette {
416 1.1.1.1.2.2 pgoyette static unsigned char hold[CHUNK];
417 1.1.1.1.2.2 pgoyette
418 1.1.1.1.2.2 pgoyette *buf = hold;
419 1.1.1.1.2.2 pgoyette return fread(hold, 1, CHUNK, (FILE *)how);
420 1.1.1.1.2.2 pgoyette }
421 1.1.1.1.2.2 pgoyette
422 1.1.1.1.2.2 pgoyette local int outf(void *how, unsigned char *buf, unsigned len)
423 1.1.1.1.2.2 pgoyette {
424 1.1.1.1.2.2 pgoyette return fwrite(buf, 1, len, (FILE *)how) != len;
425 1.1.1.1.2.2 pgoyette }
426 1.1.1.1.2.2 pgoyette
427 1.1.1.1.2.2 pgoyette /* Decompress a PKWare Compression Library stream from stdin to stdout */
428 1.1.1.1.2.2 pgoyette int main(void)
429 1.1.1.1.2.2 pgoyette {
430 1.1.1.1.2.2 pgoyette int ret, n;
431 1.1.1.1.2.2 pgoyette
432 1.1.1.1.2.2 pgoyette /* decompress to stdout */
433 1.1.1.1.2.2 pgoyette ret = blast(inf, stdin, outf, stdout);
434 1.1.1.1.2.2 pgoyette if (ret != 0) fprintf(stderr, "blast error: %d\n", ret);
435 1.1.1.1.2.2 pgoyette
436 1.1.1.1.2.2 pgoyette /* see if there are any leftover bytes */
437 1.1.1.1.2.2 pgoyette n = 0;
438 1.1.1.1.2.2 pgoyette while (getchar() != EOF) n++;
439 1.1.1.1.2.2 pgoyette if (n) fprintf(stderr, "blast warning: %d unused bytes of input\n", n);
440 1.1.1.1.2.2 pgoyette
441 1.1.1.1.2.2 pgoyette /* return blast() error code */
442 1.1.1.1.2.2 pgoyette return ret;
443 1.1.1.1.2.2 pgoyette }
444 1.1.1.1.2.2 pgoyette #endif
445