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