1212397c6Schristos /* blast.c
2*796c32c9Schristos * Copyright (C) 2003, 2012, 2013 Mark Adler
3212397c6Schristos * For conditions of distribution and use, see copyright notice in blast.h
4*796c32c9Schristos * version 1.3, 24 Aug 2013
5212397c6Schristos *
6212397c6Schristos * blast.c decompresses data compressed by the PKWare Compression Library.
7212397c6Schristos * This function provides functionality similar to the explode() function of
8212397c6Schristos * the PKWare library, hence the name "blast".
9212397c6Schristos *
10212397c6Schristos * This decompressor is based on the excellent format description provided by
11212397c6Schristos * Ben Rudiak-Gould in comp.compression on August 13, 2001. Interestingly, the
12212397c6Schristos * example Ben provided in the post is incorrect. The distance 110001 should
13212397c6Schristos * instead be 111000. When corrected, the example byte stream becomes:
14212397c6Schristos *
15212397c6Schristos * 00 04 82 24 25 8f 80 7f
16212397c6Schristos *
17212397c6Schristos * which decompresses to "AIAIAIAIAIAIA" (without the quotes).
18212397c6Schristos */
19212397c6Schristos
20212397c6Schristos /*
21212397c6Schristos * Change history:
22212397c6Schristos *
23212397c6Schristos * 1.0 12 Feb 2003 - First version
24212397c6Schristos * 1.1 16 Feb 2003 - Fixed distance check for > 4 GB uncompressed data
25ba340e45Schristos * 1.2 24 Oct 2012 - Add note about using binary mode in stdio
26ba340e45Schristos * - Fix comparisons of differently signed integers
27*796c32c9Schristos * 1.3 24 Aug 2013 - Return unused input from blast()
28*796c32c9Schristos * - Fix test code to correctly report unused input
29*796c32c9Schristos * - Enable the provision of initial input to blast()
30212397c6Schristos */
31212397c6Schristos
32*796c32c9Schristos #include <stddef.h> /* for NULL */
33212397c6Schristos #include <setjmp.h> /* for setjmp(), longjmp(), and jmp_buf */
34212397c6Schristos #include "blast.h" /* prototype for blast() */
35212397c6Schristos
36212397c6Schristos #define local static /* for local function definitions */
37212397c6Schristos #define MAXBITS 13 /* maximum code length */
38212397c6Schristos #define MAXWIN 4096 /* maximum window size */
39212397c6Schristos
40212397c6Schristos /* input and output state */
41212397c6Schristos struct state {
42212397c6Schristos /* input state */
43212397c6Schristos blast_in infun; /* input function provided by user */
44212397c6Schristos void *inhow; /* opaque information passed to infun() */
45212397c6Schristos unsigned char *in; /* next input location */
46212397c6Schristos unsigned left; /* available input at in */
47212397c6Schristos int bitbuf; /* bit buffer */
48212397c6Schristos int bitcnt; /* number of bits in bit buffer */
49212397c6Schristos
50212397c6Schristos /* input limit error return state for bits() and decode() */
51212397c6Schristos jmp_buf env;
52212397c6Schristos
53212397c6Schristos /* output state */
54212397c6Schristos blast_out outfun; /* output function provided by user */
55212397c6Schristos void *outhow; /* opaque information passed to outfun() */
56212397c6Schristos unsigned next; /* index of next write location in out[] */
57212397c6Schristos int first; /* true to check distances (for first 4K) */
58212397c6Schristos unsigned char out[MAXWIN]; /* output buffer and sliding window */
59212397c6Schristos };
60212397c6Schristos
61212397c6Schristos /*
62212397c6Schristos * Return need bits from the input stream. This always leaves less than
63212397c6Schristos * eight bits in the buffer. bits() works properly for need == 0.
64212397c6Schristos *
65212397c6Schristos * Format notes:
66212397c6Schristos *
67212397c6Schristos * - Bits are stored in bytes from the least significant bit to the most
68212397c6Schristos * significant bit. Therefore bits are dropped from the bottom of the bit
69212397c6Schristos * buffer, using shift right, and new bytes are appended to the top of the
70212397c6Schristos * bit buffer, using shift left.
71212397c6Schristos */
bits(struct state * s,int need)72212397c6Schristos local int bits(struct state *s, int need)
73212397c6Schristos {
74212397c6Schristos int val; /* bit accumulator */
75212397c6Schristos
76212397c6Schristos /* load at least need bits into val */
77212397c6Schristos val = s->bitbuf;
78212397c6Schristos while (s->bitcnt < need) {
79212397c6Schristos if (s->left == 0) {
80212397c6Schristos s->left = s->infun(s->inhow, &(s->in));
81212397c6Schristos if (s->left == 0) longjmp(s->env, 1); /* out of input */
82212397c6Schristos }
83212397c6Schristos val |= (int)(*(s->in)++) << s->bitcnt; /* load eight bits */
84212397c6Schristos s->left--;
85212397c6Schristos s->bitcnt += 8;
86212397c6Schristos }
87212397c6Schristos
88212397c6Schristos /* drop need bits and update buffer, always zero to seven bits left */
89212397c6Schristos s->bitbuf = val >> need;
90212397c6Schristos s->bitcnt -= need;
91212397c6Schristos
92212397c6Schristos /* return need bits, zeroing the bits above that */
93212397c6Schristos return val & ((1 << need) - 1);
94212397c6Schristos }
95212397c6Schristos
96212397c6Schristos /*
97212397c6Schristos * Huffman code decoding tables. count[1..MAXBITS] is the number of symbols of
98212397c6Schristos * each length, which for a canonical code are stepped through in order.
99212397c6Schristos * symbol[] are the symbol values in canonical order, where the number of
100212397c6Schristos * entries is the sum of the counts in count[]. The decoding process can be
101212397c6Schristos * seen in the function decode() below.
102212397c6Schristos */
103212397c6Schristos struct huffman {
104212397c6Schristos short *count; /* number of symbols of each length */
105212397c6Schristos short *symbol; /* canonically ordered symbols */
106212397c6Schristos };
107212397c6Schristos
108212397c6Schristos /*
109212397c6Schristos * Decode a code from the stream s using huffman table h. Return the symbol or
110212397c6Schristos * a negative value if there is an error. If all of the lengths are zero, i.e.
111212397c6Schristos * an empty code, or if the code is incomplete and an invalid code is received,
112212397c6Schristos * then -9 is returned after reading MAXBITS bits.
113212397c6Schristos *
114212397c6Schristos * Format notes:
115212397c6Schristos *
116212397c6Schristos * - The codes as stored in the compressed data are bit-reversed relative to
117212397c6Schristos * a simple integer ordering of codes of the same lengths. Hence below the
118212397c6Schristos * bits are pulled from the compressed data one at a time and used to
119212397c6Schristos * build the code value reversed from what is in the stream in order to
120212397c6Schristos * permit simple integer comparisons for decoding.
121212397c6Schristos *
122212397c6Schristos * - The first code for the shortest length is all ones. Subsequent codes of
123212397c6Schristos * the same length are simply integer decrements of the previous code. When
124212397c6Schristos * moving up a length, a one bit is appended to the code. For a complete
125212397c6Schristos * code, the last code of the longest length will be all zeros. To support
126212397c6Schristos * this ordering, the bits pulled during decoding are inverted to apply the
127212397c6Schristos * more "natural" ordering starting with all zeros and incrementing.
128212397c6Schristos */
decode(struct state * s,struct huffman * h)129212397c6Schristos local int decode(struct state *s, struct huffman *h)
130212397c6Schristos {
131212397c6Schristos int len; /* current number of bits in code */
132212397c6Schristos int code; /* len bits being decoded */
133212397c6Schristos int first; /* first code of length len */
134212397c6Schristos int count; /* number of codes of length len */
135212397c6Schristos int index; /* index of first code of length len in symbol table */
136212397c6Schristos int bitbuf; /* bits from stream */
137212397c6Schristos int left; /* bits left in next or left to process */
138212397c6Schristos short *next; /* next number of codes */
139212397c6Schristos
140212397c6Schristos bitbuf = s->bitbuf;
141212397c6Schristos left = s->bitcnt;
142212397c6Schristos code = first = index = 0;
143212397c6Schristos len = 1;
144212397c6Schristos next = h->count + 1;
145212397c6Schristos while (1) {
146212397c6Schristos while (left--) {
147212397c6Schristos code |= (bitbuf & 1) ^ 1; /* invert code */
148212397c6Schristos bitbuf >>= 1;
149212397c6Schristos count = *next++;
150212397c6Schristos if (code < first + count) { /* if length len, return symbol */
151212397c6Schristos s->bitbuf = bitbuf;
152212397c6Schristos s->bitcnt = (s->bitcnt - len) & 7;
153212397c6Schristos return h->symbol[index + (code - first)];
154212397c6Schristos }
155212397c6Schristos index += count; /* else update for next length */
156212397c6Schristos first += count;
157212397c6Schristos first <<= 1;
158212397c6Schristos code <<= 1;
159212397c6Schristos len++;
160212397c6Schristos }
161212397c6Schristos left = (MAXBITS+1) - len;
162212397c6Schristos if (left == 0) break;
163212397c6Schristos if (s->left == 0) {
164212397c6Schristos s->left = s->infun(s->inhow, &(s->in));
165212397c6Schristos if (s->left == 0) longjmp(s->env, 1); /* out of input */
166212397c6Schristos }
167212397c6Schristos bitbuf = *(s->in)++;
168212397c6Schristos s->left--;
169212397c6Schristos if (left > 8) left = 8;
170212397c6Schristos }
171212397c6Schristos return -9; /* ran out of codes */
172212397c6Schristos }
173212397c6Schristos
174212397c6Schristos /*
175212397c6Schristos * Given a list of repeated code lengths rep[0..n-1], where each byte is a
176212397c6Schristos * count (high four bits + 1) and a code length (low four bits), generate the
177212397c6Schristos * list of code lengths. This compaction reduces the size of the object code.
178212397c6Schristos * Then given the list of code lengths length[0..n-1] representing a canonical
179212397c6Schristos * Huffman code for n symbols, construct the tables required to decode those
180212397c6Schristos * codes. Those tables are the number of codes of each length, and the symbols
181212397c6Schristos * sorted by length, retaining their original order within each length. The
182212397c6Schristos * return value is zero for a complete code set, negative for an over-
183212397c6Schristos * subscribed code set, and positive for an incomplete code set. The tables
184212397c6Schristos * can be used if the return value is zero or positive, but they cannot be used
185212397c6Schristos * if the return value is negative. If the return value is zero, it is not
186212397c6Schristos * possible for decode() using that table to return an error--any stream of
187212397c6Schristos * enough bits will resolve to a symbol. If the return value is positive, then
188212397c6Schristos * it is possible for decode() using that table to return an error for received
189212397c6Schristos * codes past the end of the incomplete lengths.
190212397c6Schristos */
construct(struct huffman * h,const unsigned char * rep,int n)191212397c6Schristos local int construct(struct huffman *h, const unsigned char *rep, int n)
192212397c6Schristos {
193212397c6Schristos int symbol; /* current symbol when stepping through length[] */
194212397c6Schristos int len; /* current length when stepping through h->count[] */
195212397c6Schristos int left; /* number of possible codes left of current length */
196212397c6Schristos short offs[MAXBITS+1]; /* offsets in symbol table for each length */
197212397c6Schristos short length[256]; /* code lengths */
198212397c6Schristos
199212397c6Schristos /* convert compact repeat counts into symbol bit length list */
200212397c6Schristos symbol = 0;
201212397c6Schristos do {
202212397c6Schristos len = *rep++;
203212397c6Schristos left = (len >> 4) + 1;
204212397c6Schristos len &= 15;
205212397c6Schristos do {
206212397c6Schristos length[symbol++] = len;
207212397c6Schristos } while (--left);
208212397c6Schristos } while (--n);
209212397c6Schristos n = symbol;
210212397c6Schristos
211212397c6Schristos /* count number of codes of each length */
212212397c6Schristos for (len = 0; len <= MAXBITS; len++)
213212397c6Schristos h->count[len] = 0;
214212397c6Schristos for (symbol = 0; symbol < n; symbol++)
215212397c6Schristos (h->count[length[symbol]])++; /* assumes lengths are within bounds */
216212397c6Schristos if (h->count[0] == n) /* no codes! */
217212397c6Schristos return 0; /* complete, but decode() will fail */
218212397c6Schristos
219212397c6Schristos /* check for an over-subscribed or incomplete set of lengths */
220212397c6Schristos left = 1; /* one possible code of zero length */
221212397c6Schristos for (len = 1; len <= MAXBITS; len++) {
222212397c6Schristos left <<= 1; /* one more bit, double codes left */
223212397c6Schristos left -= h->count[len]; /* deduct count from possible codes */
224212397c6Schristos if (left < 0) return left; /* over-subscribed--return negative */
225212397c6Schristos } /* left > 0 means incomplete */
226212397c6Schristos
227212397c6Schristos /* generate offsets into symbol table for each length for sorting */
228212397c6Schristos offs[1] = 0;
229212397c6Schristos for (len = 1; len < MAXBITS; len++)
230212397c6Schristos offs[len + 1] = offs[len] + h->count[len];
231212397c6Schristos
232212397c6Schristos /*
233212397c6Schristos * put symbols in table sorted by length, by symbol order within each
234212397c6Schristos * length
235212397c6Schristos */
236212397c6Schristos for (symbol = 0; symbol < n; symbol++)
237212397c6Schristos if (length[symbol] != 0)
238212397c6Schristos h->symbol[offs[length[symbol]]++] = symbol;
239212397c6Schristos
240212397c6Schristos /* return zero for complete set, positive for incomplete set */
241212397c6Schristos return left;
242212397c6Schristos }
243212397c6Schristos
244212397c6Schristos /*
245212397c6Schristos * Decode PKWare Compression Library stream.
246212397c6Schristos *
247212397c6Schristos * Format notes:
248212397c6Schristos *
249212397c6Schristos * - First byte is 0 if literals are uncoded or 1 if they are coded. Second
250212397c6Schristos * byte is 4, 5, or 6 for the number of extra bits in the distance code.
251212397c6Schristos * This is the base-2 logarithm of the dictionary size minus six.
252212397c6Schristos *
253212397c6Schristos * - Compressed data is a combination of literals and length/distance pairs
254212397c6Schristos * terminated by an end code. Literals are either Huffman coded or
255212397c6Schristos * uncoded bytes. A length/distance pair is a coded length followed by a
256212397c6Schristos * coded distance to represent a string that occurs earlier in the
257212397c6Schristos * uncompressed data that occurs again at the current location.
258212397c6Schristos *
259212397c6Schristos * - A bit preceding a literal or length/distance pair indicates which comes
260212397c6Schristos * next, 0 for literals, 1 for length/distance.
261212397c6Schristos *
262212397c6Schristos * - If literals are uncoded, then the next eight bits are the literal, in the
263*796c32c9Schristos * normal bit order in the stream, i.e. no bit-reversal is needed. Similarly,
264212397c6Schristos * no bit reversal is needed for either the length extra bits or the distance
265212397c6Schristos * extra bits.
266212397c6Schristos *
267212397c6Schristos * - Literal bytes are simply written to the output. A length/distance pair is
268212397c6Schristos * an instruction to copy previously uncompressed bytes to the output. The
269212397c6Schristos * copy is from distance bytes back in the output stream, copying for length
270212397c6Schristos * bytes.
271212397c6Schristos *
272212397c6Schristos * - Distances pointing before the beginning of the output data are not
273212397c6Schristos * permitted.
274212397c6Schristos *
275212397c6Schristos * - Overlapped copies, where the length is greater than the distance, are
276212397c6Schristos * allowed and common. For example, a distance of one and a length of 518
277212397c6Schristos * simply copies the last byte 518 times. A distance of four and a length of
278212397c6Schristos * twelve copies the last four bytes three times. A simple forward copy
279212397c6Schristos * ignoring whether the length is greater than the distance or not implements
280212397c6Schristos * this correctly.
281212397c6Schristos */
decomp(struct state * s)282212397c6Schristos local int decomp(struct state *s)
283212397c6Schristos {
284212397c6Schristos int lit; /* true if literals are coded */
285212397c6Schristos int dict; /* log2(dictionary size) - 6 */
286212397c6Schristos int symbol; /* decoded symbol, extra bits for distance */
287212397c6Schristos int len; /* length for copy */
288ba340e45Schristos unsigned dist; /* distance for copy */
289212397c6Schristos int copy; /* copy counter */
290212397c6Schristos unsigned char *from, *to; /* copy pointers */
291212397c6Schristos static int virgin = 1; /* build tables once */
292212397c6Schristos static short litcnt[MAXBITS+1], litsym[256]; /* litcode memory */
293212397c6Schristos static short lencnt[MAXBITS+1], lensym[16]; /* lencode memory */
294212397c6Schristos static short distcnt[MAXBITS+1], distsym[64]; /* distcode memory */
295212397c6Schristos static struct huffman litcode = {litcnt, litsym}; /* length code */
296212397c6Schristos static struct huffman lencode = {lencnt, lensym}; /* length code */
297212397c6Schristos static struct huffman distcode = {distcnt, distsym};/* distance code */
298212397c6Schristos /* bit lengths of literal codes */
299212397c6Schristos static const unsigned char litlen[] = {
300212397c6Schristos 11, 124, 8, 7, 28, 7, 188, 13, 76, 4, 10, 8, 12, 10, 12, 10, 8, 23, 8,
301212397c6Schristos 9, 7, 6, 7, 8, 7, 6, 55, 8, 23, 24, 12, 11, 7, 9, 11, 12, 6, 7, 22, 5,
302212397c6Schristos 7, 24, 6, 11, 9, 6, 7, 22, 7, 11, 38, 7, 9, 8, 25, 11, 8, 11, 9, 12,
303212397c6Schristos 8, 12, 5, 38, 5, 38, 5, 11, 7, 5, 6, 21, 6, 10, 53, 8, 7, 24, 10, 27,
304212397c6Schristos 44, 253, 253, 253, 252, 252, 252, 13, 12, 45, 12, 45, 12, 61, 12, 45,
305212397c6Schristos 44, 173};
306212397c6Schristos /* bit lengths of length codes 0..15 */
307212397c6Schristos static const unsigned char lenlen[] = {2, 35, 36, 53, 38, 23};
308212397c6Schristos /* bit lengths of distance codes 0..63 */
309212397c6Schristos static const unsigned char distlen[] = {2, 20, 53, 230, 247, 151, 248};
310212397c6Schristos static const short base[16] = { /* base for length codes */
311212397c6Schristos 3, 2, 4, 5, 6, 7, 8, 9, 10, 12, 16, 24, 40, 72, 136, 264};
312212397c6Schristos static const char extra[16] = { /* extra bits for length codes */
313212397c6Schristos 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8};
314212397c6Schristos
315212397c6Schristos /* set up decoding tables (once--might not be thread-safe) */
316212397c6Schristos if (virgin) {
317212397c6Schristos construct(&litcode, litlen, sizeof(litlen));
318212397c6Schristos construct(&lencode, lenlen, sizeof(lenlen));
319212397c6Schristos construct(&distcode, distlen, sizeof(distlen));
320212397c6Schristos virgin = 0;
321212397c6Schristos }
322212397c6Schristos
323212397c6Schristos /* read header */
324212397c6Schristos lit = bits(s, 8);
325212397c6Schristos if (lit > 1) return -1;
326212397c6Schristos dict = bits(s, 8);
327212397c6Schristos if (dict < 4 || dict > 6) return -2;
328212397c6Schristos
329212397c6Schristos /* decode literals and length/distance pairs */
330212397c6Schristos do {
331212397c6Schristos if (bits(s, 1)) {
332212397c6Schristos /* get length */
333212397c6Schristos symbol = decode(s, &lencode);
334212397c6Schristos len = base[symbol] + bits(s, extra[symbol]);
335212397c6Schristos if (len == 519) break; /* end code */
336212397c6Schristos
337212397c6Schristos /* get distance */
338212397c6Schristos symbol = len == 2 ? 2 : dict;
339212397c6Schristos dist = decode(s, &distcode) << symbol;
340212397c6Schristos dist += bits(s, symbol);
341212397c6Schristos dist++;
342212397c6Schristos if (s->first && dist > s->next)
343212397c6Schristos return -3; /* distance too far back */
344212397c6Schristos
345212397c6Schristos /* copy length bytes from distance bytes back */
346212397c6Schristos do {
347212397c6Schristos to = s->out + s->next;
348212397c6Schristos from = to - dist;
349212397c6Schristos copy = MAXWIN;
350212397c6Schristos if (s->next < dist) {
351212397c6Schristos from += copy;
352212397c6Schristos copy = dist;
353212397c6Schristos }
354212397c6Schristos copy -= s->next;
355212397c6Schristos if (copy > len) copy = len;
356212397c6Schristos len -= copy;
357212397c6Schristos s->next += copy;
358212397c6Schristos do {
359212397c6Schristos *to++ = *from++;
360212397c6Schristos } while (--copy);
361212397c6Schristos if (s->next == MAXWIN) {
362212397c6Schristos if (s->outfun(s->outhow, s->out, s->next)) return 1;
363212397c6Schristos s->next = 0;
364212397c6Schristos s->first = 0;
365212397c6Schristos }
366212397c6Schristos } while (len != 0);
367212397c6Schristos }
368212397c6Schristos else {
369212397c6Schristos /* get literal and write it */
370212397c6Schristos symbol = lit ? decode(s, &litcode) : bits(s, 8);
371212397c6Schristos s->out[s->next++] = symbol;
372212397c6Schristos if (s->next == MAXWIN) {
373212397c6Schristos if (s->outfun(s->outhow, s->out, s->next)) return 1;
374212397c6Schristos s->next = 0;
375212397c6Schristos s->first = 0;
376212397c6Schristos }
377212397c6Schristos }
378212397c6Schristos } while (1);
379212397c6Schristos return 0;
380212397c6Schristos }
381212397c6Schristos
382212397c6Schristos /* See comments in blast.h */
blast(blast_in infun,void * inhow,blast_out outfun,void * outhow,unsigned * left,unsigned char ** in)383*796c32c9Schristos int blast(blast_in infun, void *inhow, blast_out outfun, void *outhow,
384*796c32c9Schristos unsigned *left, unsigned char **in)
385212397c6Schristos {
386212397c6Schristos struct state s; /* input/output state */
387212397c6Schristos int err; /* return value */
388212397c6Schristos
389212397c6Schristos /* initialize input state */
390212397c6Schristos s.infun = infun;
391212397c6Schristos s.inhow = inhow;
392*796c32c9Schristos if (left != NULL && *left) {
393*796c32c9Schristos s.left = *left;
394*796c32c9Schristos s.in = *in;
395*796c32c9Schristos }
396*796c32c9Schristos else
397212397c6Schristos s.left = 0;
398212397c6Schristos s.bitbuf = 0;
399212397c6Schristos s.bitcnt = 0;
400212397c6Schristos
401212397c6Schristos /* initialize output state */
402212397c6Schristos s.outfun = outfun;
403212397c6Schristos s.outhow = outhow;
404212397c6Schristos s.next = 0;
405212397c6Schristos s.first = 1;
406212397c6Schristos
407212397c6Schristos /* return if bits() or decode() tries to read past available input */
408212397c6Schristos if (setjmp(s.env) != 0) /* if came back here via longjmp(), */
409212397c6Schristos err = 2; /* then skip decomp(), return error */
410212397c6Schristos else
411212397c6Schristos err = decomp(&s); /* decompress */
412212397c6Schristos
413*796c32c9Schristos /* return unused input */
414*796c32c9Schristos if (left != NULL)
415*796c32c9Schristos *left = s.left;
416*796c32c9Schristos if (in != NULL)
417*796c32c9Schristos *in = s.left ? s.in : NULL;
418*796c32c9Schristos
419212397c6Schristos /* write any leftover output and update the error code if needed */
420212397c6Schristos if (err != 1 && s.next && s.outfun(s.outhow, s.out, s.next) && err == 0)
421212397c6Schristos err = 1;
422212397c6Schristos return err;
423212397c6Schristos }
424212397c6Schristos
425212397c6Schristos #ifdef TEST
426212397c6Schristos /* Example of how to use blast() */
427212397c6Schristos #include <stdio.h>
428212397c6Schristos #include <stdlib.h>
429212397c6Schristos
430212397c6Schristos #define CHUNK 16384
431212397c6Schristos
inf(void * how,unsigned char ** buf)432212397c6Schristos local unsigned inf(void *how, unsigned char **buf)
433212397c6Schristos {
434212397c6Schristos static unsigned char hold[CHUNK];
435212397c6Schristos
436212397c6Schristos *buf = hold;
437212397c6Schristos return fread(hold, 1, CHUNK, (FILE *)how);
438212397c6Schristos }
439212397c6Schristos
outf(void * how,unsigned char * buf,unsigned len)440212397c6Schristos local int outf(void *how, unsigned char *buf, unsigned len)
441212397c6Schristos {
442212397c6Schristos return fwrite(buf, 1, len, (FILE *)how) != len;
443212397c6Schristos }
444212397c6Schristos
445212397c6Schristos /* Decompress a PKWare Compression Library stream from stdin to stdout */
main(void)446212397c6Schristos int main(void)
447212397c6Schristos {
448*796c32c9Schristos int ret;
449*796c32c9Schristos unsigned left;
450212397c6Schristos
451212397c6Schristos /* decompress to stdout */
452*796c32c9Schristos left = 0;
453*796c32c9Schristos ret = blast(inf, stdin, outf, stdout, &left, NULL);
454*796c32c9Schristos if (ret != 0)
455*796c32c9Schristos fprintf(stderr, "blast error: %d\n", ret);
456212397c6Schristos
457*796c32c9Schristos /* count any leftover bytes */
458*796c32c9Schristos while (getchar() != EOF)
459*796c32c9Schristos left++;
460*796c32c9Schristos if (left)
461*796c32c9Schristos fprintf(stderr, "blast warning: %u unused bytes of input\n", left);
462212397c6Schristos
463212397c6Schristos /* return blast() error code */
464212397c6Schristos return ret;
465212397c6Schristos }
466212397c6Schristos #endif
467