116dce513Schristos /* blast.c
2*ede78133Schristos * Copyright (C) 2003, 2012, 2013 Mark Adler
316dce513Schristos * For conditions of distribution and use, see copyright notice in blast.h
4*ede78133Schristos * version 1.3, 24 Aug 2013
516dce513Schristos *
616dce513Schristos * blast.c decompresses data compressed by the PKWare Compression Library.
716dce513Schristos * This function provides functionality similar to the explode() function of
816dce513Schristos * the PKWare library, hence the name "blast".
916dce513Schristos *
1016dce513Schristos * This decompressor is based on the excellent format description provided by
1116dce513Schristos * Ben Rudiak-Gould in comp.compression on August 13, 2001. Interestingly, the
1216dce513Schristos * example Ben provided in the post is incorrect. The distance 110001 should
1316dce513Schristos * instead be 111000. When corrected, the example byte stream becomes:
1416dce513Schristos *
1516dce513Schristos * 00 04 82 24 25 8f 80 7f
1616dce513Schristos *
1716dce513Schristos * which decompresses to "AIAIAIAIAIAIA" (without the quotes).
1816dce513Schristos */
1916dce513Schristos
2016dce513Schristos /*
2116dce513Schristos * Change history:
2216dce513Schristos *
2316dce513Schristos * 1.0 12 Feb 2003 - First version
2416dce513Schristos * 1.1 16 Feb 2003 - Fixed distance check for > 4 GB uncompressed data
2516dce513Schristos * 1.2 24 Oct 2012 - Add note about using binary mode in stdio
2616dce513Schristos * - Fix comparisons of differently signed integers
27*ede78133Schristos * 1.3 24 Aug 2013 - Return unused input from blast()
28*ede78133Schristos * - Fix test code to correctly report unused input
29*ede78133Schristos * - Enable the provision of initial input to blast()
3016dce513Schristos */
3116dce513Schristos
32*ede78133Schristos #include <stddef.h> /* for NULL */
3316dce513Schristos #include <setjmp.h> /* for setjmp(), longjmp(), and jmp_buf */
3416dce513Schristos #include "blast.h" /* prototype for blast() */
3516dce513Schristos
3616dce513Schristos #define local static /* for local function definitions */
3716dce513Schristos #define MAXBITS 13 /* maximum code length */
3816dce513Schristos #define MAXWIN 4096 /* maximum window size */
3916dce513Schristos
4016dce513Schristos /* input and output state */
4116dce513Schristos struct state {
4216dce513Schristos /* input state */
4316dce513Schristos blast_in infun; /* input function provided by user */
4416dce513Schristos void *inhow; /* opaque information passed to infun() */
4516dce513Schristos unsigned char *in; /* next input location */
4616dce513Schristos unsigned left; /* available input at in */
4716dce513Schristos int bitbuf; /* bit buffer */
4816dce513Schristos int bitcnt; /* number of bits in bit buffer */
4916dce513Schristos
5016dce513Schristos /* input limit error return state for bits() and decode() */
5116dce513Schristos jmp_buf env;
5216dce513Schristos
5316dce513Schristos /* output state */
5416dce513Schristos blast_out outfun; /* output function provided by user */
5516dce513Schristos void *outhow; /* opaque information passed to outfun() */
5616dce513Schristos unsigned next; /* index of next write location in out[] */
5716dce513Schristos int first; /* true to check distances (for first 4K) */
5816dce513Schristos unsigned char out[MAXWIN]; /* output buffer and sliding window */
5916dce513Schristos };
6016dce513Schristos
6116dce513Schristos /*
6216dce513Schristos * Return need bits from the input stream. This always leaves less than
6316dce513Schristos * eight bits in the buffer. bits() works properly for need == 0.
6416dce513Schristos *
6516dce513Schristos * Format notes:
6616dce513Schristos *
6716dce513Schristos * - Bits are stored in bytes from the least significant bit to the most
6816dce513Schristos * significant bit. Therefore bits are dropped from the bottom of the bit
6916dce513Schristos * buffer, using shift right, and new bytes are appended to the top of the
7016dce513Schristos * bit buffer, using shift left.
7116dce513Schristos */
bits(struct state * s,int need)7216dce513Schristos local int bits(struct state *s, int need)
7316dce513Schristos {
7416dce513Schristos int val; /* bit accumulator */
7516dce513Schristos
7616dce513Schristos /* load at least need bits into val */
7716dce513Schristos val = s->bitbuf;
7816dce513Schristos while (s->bitcnt < need) {
7916dce513Schristos if (s->left == 0) {
8016dce513Schristos s->left = s->infun(s->inhow, &(s->in));
8116dce513Schristos if (s->left == 0) longjmp(s->env, 1); /* out of input */
8216dce513Schristos }
8316dce513Schristos val |= (int)(*(s->in)++) << s->bitcnt; /* load eight bits */
8416dce513Schristos s->left--;
8516dce513Schristos s->bitcnt += 8;
8616dce513Schristos }
8716dce513Schristos
8816dce513Schristos /* drop need bits and update buffer, always zero to seven bits left */
8916dce513Schristos s->bitbuf = val >> need;
9016dce513Schristos s->bitcnt -= need;
9116dce513Schristos
9216dce513Schristos /* return need bits, zeroing the bits above that */
9316dce513Schristos return val & ((1 << need) - 1);
9416dce513Schristos }
9516dce513Schristos
9616dce513Schristos /*
9716dce513Schristos * Huffman code decoding tables. count[1..MAXBITS] is the number of symbols of
9816dce513Schristos * each length, which for a canonical code are stepped through in order.
9916dce513Schristos * symbol[] are the symbol values in canonical order, where the number of
10016dce513Schristos * entries is the sum of the counts in count[]. The decoding process can be
10116dce513Schristos * seen in the function decode() below.
10216dce513Schristos */
10316dce513Schristos struct huffman {
10416dce513Schristos short *count; /* number of symbols of each length */
10516dce513Schristos short *symbol; /* canonically ordered symbols */
10616dce513Schristos };
10716dce513Schristos
10816dce513Schristos /*
10916dce513Schristos * Decode a code from the stream s using huffman table h. Return the symbol or
11016dce513Schristos * a negative value if there is an error. If all of the lengths are zero, i.e.
11116dce513Schristos * an empty code, or if the code is incomplete and an invalid code is received,
11216dce513Schristos * then -9 is returned after reading MAXBITS bits.
11316dce513Schristos *
11416dce513Schristos * Format notes:
11516dce513Schristos *
11616dce513Schristos * - The codes as stored in the compressed data are bit-reversed relative to
11716dce513Schristos * a simple integer ordering of codes of the same lengths. Hence below the
11816dce513Schristos * bits are pulled from the compressed data one at a time and used to
11916dce513Schristos * build the code value reversed from what is in the stream in order to
12016dce513Schristos * permit simple integer comparisons for decoding.
12116dce513Schristos *
12216dce513Schristos * - The first code for the shortest length is all ones. Subsequent codes of
12316dce513Schristos * the same length are simply integer decrements of the previous code. When
12416dce513Schristos * moving up a length, a one bit is appended to the code. For a complete
12516dce513Schristos * code, the last code of the longest length will be all zeros. To support
12616dce513Schristos * this ordering, the bits pulled during decoding are inverted to apply the
12716dce513Schristos * more "natural" ordering starting with all zeros and incrementing.
12816dce513Schristos */
decode(struct state * s,struct huffman * h)12916dce513Schristos local int decode(struct state *s, struct huffman *h)
13016dce513Schristos {
13116dce513Schristos int len; /* current number of bits in code */
13216dce513Schristos int code; /* len bits being decoded */
13316dce513Schristos int first; /* first code of length len */
13416dce513Schristos int count; /* number of codes of length len */
13516dce513Schristos int index; /* index of first code of length len in symbol table */
13616dce513Schristos int bitbuf; /* bits from stream */
13716dce513Schristos int left; /* bits left in next or left to process */
13816dce513Schristos short *next; /* next number of codes */
13916dce513Schristos
14016dce513Schristos bitbuf = s->bitbuf;
14116dce513Schristos left = s->bitcnt;
14216dce513Schristos code = first = index = 0;
14316dce513Schristos len = 1;
14416dce513Schristos next = h->count + 1;
14516dce513Schristos while (1) {
14616dce513Schristos while (left--) {
14716dce513Schristos code |= (bitbuf & 1) ^ 1; /* invert code */
14816dce513Schristos bitbuf >>= 1;
14916dce513Schristos count = *next++;
15016dce513Schristos if (code < first + count) { /* if length len, return symbol */
15116dce513Schristos s->bitbuf = bitbuf;
15216dce513Schristos s->bitcnt = (s->bitcnt - len) & 7;
15316dce513Schristos return h->symbol[index + (code - first)];
15416dce513Schristos }
15516dce513Schristos index += count; /* else update for next length */
15616dce513Schristos first += count;
15716dce513Schristos first <<= 1;
15816dce513Schristos code <<= 1;
15916dce513Schristos len++;
16016dce513Schristos }
16116dce513Schristos left = (MAXBITS+1) - len;
16216dce513Schristos if (left == 0) break;
16316dce513Schristos if (s->left == 0) {
16416dce513Schristos s->left = s->infun(s->inhow, &(s->in));
16516dce513Schristos if (s->left == 0) longjmp(s->env, 1); /* out of input */
16616dce513Schristos }
16716dce513Schristos bitbuf = *(s->in)++;
16816dce513Schristos s->left--;
16916dce513Schristos if (left > 8) left = 8;
17016dce513Schristos }
17116dce513Schristos return -9; /* ran out of codes */
17216dce513Schristos }
17316dce513Schristos
17416dce513Schristos /*
17516dce513Schristos * Given a list of repeated code lengths rep[0..n-1], where each byte is a
17616dce513Schristos * count (high four bits + 1) and a code length (low four bits), generate the
17716dce513Schristos * list of code lengths. This compaction reduces the size of the object code.
17816dce513Schristos * Then given the list of code lengths length[0..n-1] representing a canonical
17916dce513Schristos * Huffman code for n symbols, construct the tables required to decode those
18016dce513Schristos * codes. Those tables are the number of codes of each length, and the symbols
18116dce513Schristos * sorted by length, retaining their original order within each length. The
18216dce513Schristos * return value is zero for a complete code set, negative for an over-
18316dce513Schristos * subscribed code set, and positive for an incomplete code set. The tables
18416dce513Schristos * can be used if the return value is zero or positive, but they cannot be used
18516dce513Schristos * if the return value is negative. If the return value is zero, it is not
18616dce513Schristos * possible for decode() using that table to return an error--any stream of
18716dce513Schristos * enough bits will resolve to a symbol. If the return value is positive, then
18816dce513Schristos * it is possible for decode() using that table to return an error for received
18916dce513Schristos * codes past the end of the incomplete lengths.
19016dce513Schristos */
construct(struct huffman * h,const unsigned char * rep,int n)19116dce513Schristos local int construct(struct huffman *h, const unsigned char *rep, int n)
19216dce513Schristos {
19316dce513Schristos int symbol; /* current symbol when stepping through length[] */
19416dce513Schristos int len; /* current length when stepping through h->count[] */
19516dce513Schristos int left; /* number of possible codes left of current length */
19616dce513Schristos short offs[MAXBITS+1]; /* offsets in symbol table for each length */
19716dce513Schristos short length[256]; /* code lengths */
19816dce513Schristos
19916dce513Schristos /* convert compact repeat counts into symbol bit length list */
20016dce513Schristos symbol = 0;
20116dce513Schristos do {
20216dce513Schristos len = *rep++;
20316dce513Schristos left = (len >> 4) + 1;
20416dce513Schristos len &= 15;
20516dce513Schristos do {
20616dce513Schristos length[symbol++] = len;
20716dce513Schristos } while (--left);
20816dce513Schristos } while (--n);
20916dce513Schristos n = symbol;
21016dce513Schristos
21116dce513Schristos /* count number of codes of each length */
21216dce513Schristos for (len = 0; len <= MAXBITS; len++)
21316dce513Schristos h->count[len] = 0;
21416dce513Schristos for (symbol = 0; symbol < n; symbol++)
21516dce513Schristos (h->count[length[symbol]])++; /* assumes lengths are within bounds */
21616dce513Schristos if (h->count[0] == n) /* no codes! */
21716dce513Schristos return 0; /* complete, but decode() will fail */
21816dce513Schristos
21916dce513Schristos /* check for an over-subscribed or incomplete set of lengths */
22016dce513Schristos left = 1; /* one possible code of zero length */
22116dce513Schristos for (len = 1; len <= MAXBITS; len++) {
22216dce513Schristos left <<= 1; /* one more bit, double codes left */
22316dce513Schristos left -= h->count[len]; /* deduct count from possible codes */
22416dce513Schristos if (left < 0) return left; /* over-subscribed--return negative */
22516dce513Schristos } /* left > 0 means incomplete */
22616dce513Schristos
22716dce513Schristos /* generate offsets into symbol table for each length for sorting */
22816dce513Schristos offs[1] = 0;
22916dce513Schristos for (len = 1; len < MAXBITS; len++)
23016dce513Schristos offs[len + 1] = offs[len] + h->count[len];
23116dce513Schristos
23216dce513Schristos /*
23316dce513Schristos * put symbols in table sorted by length, by symbol order within each
23416dce513Schristos * length
23516dce513Schristos */
23616dce513Schristos for (symbol = 0; symbol < n; symbol++)
23716dce513Schristos if (length[symbol] != 0)
23816dce513Schristos h->symbol[offs[length[symbol]]++] = symbol;
23916dce513Schristos
24016dce513Schristos /* return zero for complete set, positive for incomplete set */
24116dce513Schristos return left;
24216dce513Schristos }
24316dce513Schristos
24416dce513Schristos /*
24516dce513Schristos * Decode PKWare Compression Library stream.
24616dce513Schristos *
24716dce513Schristos * Format notes:
24816dce513Schristos *
24916dce513Schristos * - First byte is 0 if literals are uncoded or 1 if they are coded. Second
25016dce513Schristos * byte is 4, 5, or 6 for the number of extra bits in the distance code.
25116dce513Schristos * This is the base-2 logarithm of the dictionary size minus six.
25216dce513Schristos *
25316dce513Schristos * - Compressed data is a combination of literals and length/distance pairs
25416dce513Schristos * terminated by an end code. Literals are either Huffman coded or
25516dce513Schristos * uncoded bytes. A length/distance pair is a coded length followed by a
25616dce513Schristos * coded distance to represent a string that occurs earlier in the
25716dce513Schristos * uncompressed data that occurs again at the current location.
25816dce513Schristos *
25916dce513Schristos * - A bit preceding a literal or length/distance pair indicates which comes
26016dce513Schristos * next, 0 for literals, 1 for length/distance.
26116dce513Schristos *
26216dce513Schristos * - If literals are uncoded, then the next eight bits are the literal, in the
263*ede78133Schristos * normal bit order in the stream, i.e. no bit-reversal is needed. Similarly,
26416dce513Schristos * no bit reversal is needed for either the length extra bits or the distance
26516dce513Schristos * extra bits.
26616dce513Schristos *
26716dce513Schristos * - Literal bytes are simply written to the output. A length/distance pair is
26816dce513Schristos * an instruction to copy previously uncompressed bytes to the output. The
26916dce513Schristos * copy is from distance bytes back in the output stream, copying for length
27016dce513Schristos * bytes.
27116dce513Schristos *
27216dce513Schristos * - Distances pointing before the beginning of the output data are not
27316dce513Schristos * permitted.
27416dce513Schristos *
27516dce513Schristos * - Overlapped copies, where the length is greater than the distance, are
27616dce513Schristos * allowed and common. For example, a distance of one and a length of 518
27716dce513Schristos * simply copies the last byte 518 times. A distance of four and a length of
27816dce513Schristos * twelve copies the last four bytes three times. A simple forward copy
27916dce513Schristos * ignoring whether the length is greater than the distance or not implements
28016dce513Schristos * this correctly.
28116dce513Schristos */
decomp(struct state * s)28216dce513Schristos local int decomp(struct state *s)
28316dce513Schristos {
28416dce513Schristos int lit; /* true if literals are coded */
28516dce513Schristos int dict; /* log2(dictionary size) - 6 */
28616dce513Schristos int symbol; /* decoded symbol, extra bits for distance */
28716dce513Schristos int len; /* length for copy */
28816dce513Schristos unsigned dist; /* distance for copy */
28916dce513Schristos int copy; /* copy counter */
29016dce513Schristos unsigned char *from, *to; /* copy pointers */
29116dce513Schristos static int virgin = 1; /* build tables once */
29216dce513Schristos static short litcnt[MAXBITS+1], litsym[256]; /* litcode memory */
29316dce513Schristos static short lencnt[MAXBITS+1], lensym[16]; /* lencode memory */
29416dce513Schristos static short distcnt[MAXBITS+1], distsym[64]; /* distcode memory */
29516dce513Schristos static struct huffman litcode = {litcnt, litsym}; /* length code */
29616dce513Schristos static struct huffman lencode = {lencnt, lensym}; /* length code */
29716dce513Schristos static struct huffman distcode = {distcnt, distsym};/* distance code */
29816dce513Schristos /* bit lengths of literal codes */
29916dce513Schristos static const unsigned char litlen[] = {
30016dce513Schristos 11, 124, 8, 7, 28, 7, 188, 13, 76, 4, 10, 8, 12, 10, 12, 10, 8, 23, 8,
30116dce513Schristos 9, 7, 6, 7, 8, 7, 6, 55, 8, 23, 24, 12, 11, 7, 9, 11, 12, 6, 7, 22, 5,
30216dce513Schristos 7, 24, 6, 11, 9, 6, 7, 22, 7, 11, 38, 7, 9, 8, 25, 11, 8, 11, 9, 12,
30316dce513Schristos 8, 12, 5, 38, 5, 38, 5, 11, 7, 5, 6, 21, 6, 10, 53, 8, 7, 24, 10, 27,
30416dce513Schristos 44, 253, 253, 253, 252, 252, 252, 13, 12, 45, 12, 45, 12, 61, 12, 45,
30516dce513Schristos 44, 173};
30616dce513Schristos /* bit lengths of length codes 0..15 */
30716dce513Schristos static const unsigned char lenlen[] = {2, 35, 36, 53, 38, 23};
30816dce513Schristos /* bit lengths of distance codes 0..63 */
30916dce513Schristos static const unsigned char distlen[] = {2, 20, 53, 230, 247, 151, 248};
31016dce513Schristos static const short base[16] = { /* base for length codes */
31116dce513Schristos 3, 2, 4, 5, 6, 7, 8, 9, 10, 12, 16, 24, 40, 72, 136, 264};
31216dce513Schristos static const char extra[16] = { /* extra bits for length codes */
31316dce513Schristos 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8};
31416dce513Schristos
31516dce513Schristos /* set up decoding tables (once--might not be thread-safe) */
31616dce513Schristos if (virgin) {
31716dce513Schristos construct(&litcode, litlen, sizeof(litlen));
31816dce513Schristos construct(&lencode, lenlen, sizeof(lenlen));
31916dce513Schristos construct(&distcode, distlen, sizeof(distlen));
32016dce513Schristos virgin = 0;
32116dce513Schristos }
32216dce513Schristos
32316dce513Schristos /* read header */
32416dce513Schristos lit = bits(s, 8);
32516dce513Schristos if (lit > 1) return -1;
32616dce513Schristos dict = bits(s, 8);
32716dce513Schristos if (dict < 4 || dict > 6) return -2;
32816dce513Schristos
32916dce513Schristos /* decode literals and length/distance pairs */
33016dce513Schristos do {
33116dce513Schristos if (bits(s, 1)) {
33216dce513Schristos /* get length */
33316dce513Schristos symbol = decode(s, &lencode);
33416dce513Schristos len = base[symbol] + bits(s, extra[symbol]);
33516dce513Schristos if (len == 519) break; /* end code */
33616dce513Schristos
33716dce513Schristos /* get distance */
33816dce513Schristos symbol = len == 2 ? 2 : dict;
33916dce513Schristos dist = decode(s, &distcode) << symbol;
34016dce513Schristos dist += bits(s, symbol);
34116dce513Schristos dist++;
34216dce513Schristos if (s->first && dist > s->next)
34316dce513Schristos return -3; /* distance too far back */
34416dce513Schristos
34516dce513Schristos /* copy length bytes from distance bytes back */
34616dce513Schristos do {
34716dce513Schristos to = s->out + s->next;
34816dce513Schristos from = to - dist;
34916dce513Schristos copy = MAXWIN;
35016dce513Schristos if (s->next < dist) {
35116dce513Schristos from += copy;
35216dce513Schristos copy = dist;
35316dce513Schristos }
35416dce513Schristos copy -= s->next;
35516dce513Schristos if (copy > len) copy = len;
35616dce513Schristos len -= copy;
35716dce513Schristos s->next += copy;
35816dce513Schristos do {
35916dce513Schristos *to++ = *from++;
36016dce513Schristos } while (--copy);
36116dce513Schristos if (s->next == MAXWIN) {
36216dce513Schristos if (s->outfun(s->outhow, s->out, s->next)) return 1;
36316dce513Schristos s->next = 0;
36416dce513Schristos s->first = 0;
36516dce513Schristos }
36616dce513Schristos } while (len != 0);
36716dce513Schristos }
36816dce513Schristos else {
36916dce513Schristos /* get literal and write it */
37016dce513Schristos symbol = lit ? decode(s, &litcode) : bits(s, 8);
37116dce513Schristos s->out[s->next++] = symbol;
37216dce513Schristos if (s->next == MAXWIN) {
37316dce513Schristos if (s->outfun(s->outhow, s->out, s->next)) return 1;
37416dce513Schristos s->next = 0;
37516dce513Schristos s->first = 0;
37616dce513Schristos }
37716dce513Schristos }
37816dce513Schristos } while (1);
37916dce513Schristos return 0;
38016dce513Schristos }
38116dce513Schristos
38216dce513Schristos /* See comments in blast.h */
blast(blast_in infun,void * inhow,blast_out outfun,void * outhow,unsigned * left,unsigned char ** in)383*ede78133Schristos int blast(blast_in infun, void *inhow, blast_out outfun, void *outhow,
384*ede78133Schristos unsigned *left, unsigned char **in)
38516dce513Schristos {
38616dce513Schristos struct state s; /* input/output state */
38716dce513Schristos int err; /* return value */
38816dce513Schristos
38916dce513Schristos /* initialize input state */
39016dce513Schristos s.infun = infun;
39116dce513Schristos s.inhow = inhow;
392*ede78133Schristos if (left != NULL && *left) {
393*ede78133Schristos s.left = *left;
394*ede78133Schristos s.in = *in;
395*ede78133Schristos }
396*ede78133Schristos else
39716dce513Schristos s.left = 0;
39816dce513Schristos s.bitbuf = 0;
39916dce513Schristos s.bitcnt = 0;
40016dce513Schristos
40116dce513Schristos /* initialize output state */
40216dce513Schristos s.outfun = outfun;
40316dce513Schristos s.outhow = outhow;
40416dce513Schristos s.next = 0;
40516dce513Schristos s.first = 1;
40616dce513Schristos
40716dce513Schristos /* return if bits() or decode() tries to read past available input */
40816dce513Schristos if (setjmp(s.env) != 0) /* if came back here via longjmp(), */
40916dce513Schristos err = 2; /* then skip decomp(), return error */
41016dce513Schristos else
41116dce513Schristos err = decomp(&s); /* decompress */
41216dce513Schristos
413*ede78133Schristos /* return unused input */
414*ede78133Schristos if (left != NULL)
415*ede78133Schristos *left = s.left;
416*ede78133Schristos if (in != NULL)
417*ede78133Schristos *in = s.left ? s.in : NULL;
418*ede78133Schristos
41916dce513Schristos /* write any leftover output and update the error code if needed */
42016dce513Schristos if (err != 1 && s.next && s.outfun(s.outhow, s.out, s.next) && err == 0)
42116dce513Schristos err = 1;
42216dce513Schristos return err;
42316dce513Schristos }
42416dce513Schristos
42516dce513Schristos #ifdef TEST
42616dce513Schristos /* Example of how to use blast() */
42716dce513Schristos #include <stdio.h>
42816dce513Schristos #include <stdlib.h>
42916dce513Schristos
43016dce513Schristos #define CHUNK 16384
43116dce513Schristos
inf(void * how,unsigned char ** buf)43216dce513Schristos local unsigned inf(void *how, unsigned char **buf)
43316dce513Schristos {
43416dce513Schristos static unsigned char hold[CHUNK];
43516dce513Schristos
43616dce513Schristos *buf = hold;
43716dce513Schristos return fread(hold, 1, CHUNK, (FILE *)how);
43816dce513Schristos }
43916dce513Schristos
outf(void * how,unsigned char * buf,unsigned len)44016dce513Schristos local int outf(void *how, unsigned char *buf, unsigned len)
44116dce513Schristos {
44216dce513Schristos return fwrite(buf, 1, len, (FILE *)how) != len;
44316dce513Schristos }
44416dce513Schristos
44516dce513Schristos /* Decompress a PKWare Compression Library stream from stdin to stdout */
main(void)44616dce513Schristos int main(void)
44716dce513Schristos {
448*ede78133Schristos int ret;
449*ede78133Schristos unsigned left;
45016dce513Schristos
45116dce513Schristos /* decompress to stdout */
452*ede78133Schristos left = 0;
453*ede78133Schristos ret = blast(inf, stdin, outf, stdout, &left, NULL);
454*ede78133Schristos if (ret != 0)
455*ede78133Schristos fprintf(stderr, "blast error: %d\n", ret);
45616dce513Schristos
457*ede78133Schristos /* count any leftover bytes */
458*ede78133Schristos while (getchar() != EOF)
459*ede78133Schristos left++;
460*ede78133Schristos if (left)
461*ede78133Schristos fprintf(stderr, "blast warning: %u unused bytes of input\n", left);
46216dce513Schristos
46316dce513Schristos /* return blast() error code */
46416dce513Schristos return ret;
46516dce513Schristos }
46616dce513Schristos #endif
467