19573673dSchristos /* blast.c
2*fc4f4269Schristos * Copyright (C) 2003, 2012, 2013 Mark Adler
39573673dSchristos * For conditions of distribution and use, see copyright notice in blast.h
4*fc4f4269Schristos * version 1.3, 24 Aug 2013
59573673dSchristos *
69573673dSchristos * blast.c decompresses data compressed by the PKWare Compression Library.
79573673dSchristos * This function provides functionality similar to the explode() function of
89573673dSchristos * the PKWare library, hence the name "blast".
99573673dSchristos *
109573673dSchristos * This decompressor is based on the excellent format description provided by
119573673dSchristos * Ben Rudiak-Gould in comp.compression on August 13, 2001. Interestingly, the
129573673dSchristos * example Ben provided in the post is incorrect. The distance 110001 should
139573673dSchristos * instead be 111000. When corrected, the example byte stream becomes:
149573673dSchristos *
159573673dSchristos * 00 04 82 24 25 8f 80 7f
169573673dSchristos *
179573673dSchristos * which decompresses to "AIAIAIAIAIAIA" (without the quotes).
189573673dSchristos */
199573673dSchristos
209573673dSchristos /*
219573673dSchristos * Change history:
229573673dSchristos *
239573673dSchristos * 1.0 12 Feb 2003 - First version
249573673dSchristos * 1.1 16 Feb 2003 - Fixed distance check for > 4 GB uncompressed data
258cbf5cb7Schristos * 1.2 24 Oct 2012 - Add note about using binary mode in stdio
268cbf5cb7Schristos * - Fix comparisons of differently signed integers
27*fc4f4269Schristos * 1.3 24 Aug 2013 - Return unused input from blast()
28*fc4f4269Schristos * - Fix test code to correctly report unused input
29*fc4f4269Schristos * - Enable the provision of initial input to blast()
309573673dSchristos */
319573673dSchristos
32*fc4f4269Schristos #include <stddef.h> /* for NULL */
339573673dSchristos #include <setjmp.h> /* for setjmp(), longjmp(), and jmp_buf */
349573673dSchristos #include "blast.h" /* prototype for blast() */
359573673dSchristos
369573673dSchristos #define local static /* for local function definitions */
379573673dSchristos #define MAXBITS 13 /* maximum code length */
389573673dSchristos #define MAXWIN 4096 /* maximum window size */
399573673dSchristos
409573673dSchristos /* input and output state */
419573673dSchristos struct state {
429573673dSchristos /* input state */
439573673dSchristos blast_in infun; /* input function provided by user */
449573673dSchristos void *inhow; /* opaque information passed to infun() */
459573673dSchristos unsigned char *in; /* next input location */
469573673dSchristos unsigned left; /* available input at in */
479573673dSchristos int bitbuf; /* bit buffer */
489573673dSchristos int bitcnt; /* number of bits in bit buffer */
499573673dSchristos
509573673dSchristos /* input limit error return state for bits() and decode() */
519573673dSchristos jmp_buf env;
529573673dSchristos
539573673dSchristos /* output state */
549573673dSchristos blast_out outfun; /* output function provided by user */
559573673dSchristos void *outhow; /* opaque information passed to outfun() */
569573673dSchristos unsigned next; /* index of next write location in out[] */
579573673dSchristos int first; /* true to check distances (for first 4K) */
589573673dSchristos unsigned char out[MAXWIN]; /* output buffer and sliding window */
599573673dSchristos };
609573673dSchristos
619573673dSchristos /*
629573673dSchristos * Return need bits from the input stream. This always leaves less than
639573673dSchristos * eight bits in the buffer. bits() works properly for need == 0.
649573673dSchristos *
659573673dSchristos * Format notes:
669573673dSchristos *
679573673dSchristos * - Bits are stored in bytes from the least significant bit to the most
689573673dSchristos * significant bit. Therefore bits are dropped from the bottom of the bit
699573673dSchristos * buffer, using shift right, and new bytes are appended to the top of the
709573673dSchristos * bit buffer, using shift left.
719573673dSchristos */
bits(struct state * s,int need)729573673dSchristos local int bits(struct state *s, int need)
739573673dSchristos {
749573673dSchristos int val; /* bit accumulator */
759573673dSchristos
769573673dSchristos /* load at least need bits into val */
779573673dSchristos val = s->bitbuf;
789573673dSchristos while (s->bitcnt < need) {
799573673dSchristos if (s->left == 0) {
809573673dSchristos s->left = s->infun(s->inhow, &(s->in));
819573673dSchristos if (s->left == 0) longjmp(s->env, 1); /* out of input */
829573673dSchristos }
839573673dSchristos val |= (int)(*(s->in)++) << s->bitcnt; /* load eight bits */
849573673dSchristos s->left--;
859573673dSchristos s->bitcnt += 8;
869573673dSchristos }
879573673dSchristos
889573673dSchristos /* drop need bits and update buffer, always zero to seven bits left */
899573673dSchristos s->bitbuf = val >> need;
909573673dSchristos s->bitcnt -= need;
919573673dSchristos
929573673dSchristos /* return need bits, zeroing the bits above that */
939573673dSchristos return val & ((1 << need) - 1);
949573673dSchristos }
959573673dSchristos
969573673dSchristos /*
979573673dSchristos * Huffman code decoding tables. count[1..MAXBITS] is the number of symbols of
989573673dSchristos * each length, which for a canonical code are stepped through in order.
999573673dSchristos * symbol[] are the symbol values in canonical order, where the number of
1009573673dSchristos * entries is the sum of the counts in count[]. The decoding process can be
1019573673dSchristos * seen in the function decode() below.
1029573673dSchristos */
1039573673dSchristos struct huffman {
1049573673dSchristos short *count; /* number of symbols of each length */
1059573673dSchristos short *symbol; /* canonically ordered symbols */
1069573673dSchristos };
1079573673dSchristos
1089573673dSchristos /*
1099573673dSchristos * Decode a code from the stream s using huffman table h. Return the symbol or
1109573673dSchristos * a negative value if there is an error. If all of the lengths are zero, i.e.
1119573673dSchristos * an empty code, or if the code is incomplete and an invalid code is received,
1129573673dSchristos * then -9 is returned after reading MAXBITS bits.
1139573673dSchristos *
1149573673dSchristos * Format notes:
1159573673dSchristos *
1169573673dSchristos * - The codes as stored in the compressed data are bit-reversed relative to
1179573673dSchristos * a simple integer ordering of codes of the same lengths. Hence below the
1189573673dSchristos * bits are pulled from the compressed data one at a time and used to
1199573673dSchristos * build the code value reversed from what is in the stream in order to
1209573673dSchristos * permit simple integer comparisons for decoding.
1219573673dSchristos *
1229573673dSchristos * - The first code for the shortest length is all ones. Subsequent codes of
1239573673dSchristos * the same length are simply integer decrements of the previous code. When
1249573673dSchristos * moving up a length, a one bit is appended to the code. For a complete
1259573673dSchristos * code, the last code of the longest length will be all zeros. To support
1269573673dSchristos * this ordering, the bits pulled during decoding are inverted to apply the
1279573673dSchristos * more "natural" ordering starting with all zeros and incrementing.
1289573673dSchristos */
decode(struct state * s,struct huffman * h)1299573673dSchristos local int decode(struct state *s, struct huffman *h)
1309573673dSchristos {
1319573673dSchristos int len; /* current number of bits in code */
1329573673dSchristos int code; /* len bits being decoded */
1339573673dSchristos int first; /* first code of length len */
1349573673dSchristos int count; /* number of codes of length len */
1359573673dSchristos int index; /* index of first code of length len in symbol table */
1369573673dSchristos int bitbuf; /* bits from stream */
1379573673dSchristos int left; /* bits left in next or left to process */
1389573673dSchristos short *next; /* next number of codes */
1399573673dSchristos
1409573673dSchristos bitbuf = s->bitbuf;
1419573673dSchristos left = s->bitcnt;
1429573673dSchristos code = first = index = 0;
1439573673dSchristos len = 1;
1449573673dSchristos next = h->count + 1;
1459573673dSchristos while (1) {
1469573673dSchristos while (left--) {
1479573673dSchristos code |= (bitbuf & 1) ^ 1; /* invert code */
1489573673dSchristos bitbuf >>= 1;
1499573673dSchristos count = *next++;
1509573673dSchristos if (code < first + count) { /* if length len, return symbol */
1519573673dSchristos s->bitbuf = bitbuf;
1529573673dSchristos s->bitcnt = (s->bitcnt - len) & 7;
1539573673dSchristos return h->symbol[index + (code - first)];
1549573673dSchristos }
1559573673dSchristos index += count; /* else update for next length */
1569573673dSchristos first += count;
1579573673dSchristos first <<= 1;
1589573673dSchristos code <<= 1;
1599573673dSchristos len++;
1609573673dSchristos }
1619573673dSchristos left = (MAXBITS+1) - len;
1629573673dSchristos if (left == 0) break;
1639573673dSchristos if (s->left == 0) {
1649573673dSchristos s->left = s->infun(s->inhow, &(s->in));
1659573673dSchristos if (s->left == 0) longjmp(s->env, 1); /* out of input */
1669573673dSchristos }
1679573673dSchristos bitbuf = *(s->in)++;
1689573673dSchristos s->left--;
1699573673dSchristos if (left > 8) left = 8;
1709573673dSchristos }
1719573673dSchristos return -9; /* ran out of codes */
1729573673dSchristos }
1739573673dSchristos
1749573673dSchristos /*
1759573673dSchristos * Given a list of repeated code lengths rep[0..n-1], where each byte is a
1769573673dSchristos * count (high four bits + 1) and a code length (low four bits), generate the
1779573673dSchristos * list of code lengths. This compaction reduces the size of the object code.
1789573673dSchristos * Then given the list of code lengths length[0..n-1] representing a canonical
1799573673dSchristos * Huffman code for n symbols, construct the tables required to decode those
1809573673dSchristos * codes. Those tables are the number of codes of each length, and the symbols
1819573673dSchristos * sorted by length, retaining their original order within each length. The
1829573673dSchristos * return value is zero for a complete code set, negative for an over-
1839573673dSchristos * subscribed code set, and positive for an incomplete code set. The tables
1849573673dSchristos * can be used if the return value is zero or positive, but they cannot be used
1859573673dSchristos * if the return value is negative. If the return value is zero, it is not
1869573673dSchristos * possible for decode() using that table to return an error--any stream of
1879573673dSchristos * enough bits will resolve to a symbol. If the return value is positive, then
1889573673dSchristos * it is possible for decode() using that table to return an error for received
1899573673dSchristos * codes past the end of the incomplete lengths.
1909573673dSchristos */
construct(struct huffman * h,const unsigned char * rep,int n)1919573673dSchristos local int construct(struct huffman *h, const unsigned char *rep, int n)
1929573673dSchristos {
1939573673dSchristos int symbol; /* current symbol when stepping through length[] */
1949573673dSchristos int len; /* current length when stepping through h->count[] */
1959573673dSchristos int left; /* number of possible codes left of current length */
1969573673dSchristos short offs[MAXBITS+1]; /* offsets in symbol table for each length */
1979573673dSchristos short length[256]; /* code lengths */
1989573673dSchristos
1999573673dSchristos /* convert compact repeat counts into symbol bit length list */
2009573673dSchristos symbol = 0;
2019573673dSchristos do {
2029573673dSchristos len = *rep++;
2039573673dSchristos left = (len >> 4) + 1;
2049573673dSchristos len &= 15;
2059573673dSchristos do {
2069573673dSchristos length[symbol++] = len;
2079573673dSchristos } while (--left);
2089573673dSchristos } while (--n);
2099573673dSchristos n = symbol;
2109573673dSchristos
2119573673dSchristos /* count number of codes of each length */
2129573673dSchristos for (len = 0; len <= MAXBITS; len++)
2139573673dSchristos h->count[len] = 0;
2149573673dSchristos for (symbol = 0; symbol < n; symbol++)
2159573673dSchristos (h->count[length[symbol]])++; /* assumes lengths are within bounds */
2169573673dSchristos if (h->count[0] == n) /* no codes! */
2179573673dSchristos return 0; /* complete, but decode() will fail */
2189573673dSchristos
2199573673dSchristos /* check for an over-subscribed or incomplete set of lengths */
2209573673dSchristos left = 1; /* one possible code of zero length */
2219573673dSchristos for (len = 1; len <= MAXBITS; len++) {
2229573673dSchristos left <<= 1; /* one more bit, double codes left */
2239573673dSchristos left -= h->count[len]; /* deduct count from possible codes */
2249573673dSchristos if (left < 0) return left; /* over-subscribed--return negative */
2259573673dSchristos } /* left > 0 means incomplete */
2269573673dSchristos
2279573673dSchristos /* generate offsets into symbol table for each length for sorting */
2289573673dSchristos offs[1] = 0;
2299573673dSchristos for (len = 1; len < MAXBITS; len++)
2309573673dSchristos offs[len + 1] = offs[len] + h->count[len];
2319573673dSchristos
2329573673dSchristos /*
2339573673dSchristos * put symbols in table sorted by length, by symbol order within each
2349573673dSchristos * length
2359573673dSchristos */
2369573673dSchristos for (symbol = 0; symbol < n; symbol++)
2379573673dSchristos if (length[symbol] != 0)
2389573673dSchristos h->symbol[offs[length[symbol]]++] = symbol;
2399573673dSchristos
2409573673dSchristos /* return zero for complete set, positive for incomplete set */
2419573673dSchristos return left;
2429573673dSchristos }
2439573673dSchristos
2449573673dSchristos /*
2459573673dSchristos * Decode PKWare Compression Library stream.
2469573673dSchristos *
2479573673dSchristos * Format notes:
2489573673dSchristos *
2499573673dSchristos * - First byte is 0 if literals are uncoded or 1 if they are coded. Second
2509573673dSchristos * byte is 4, 5, or 6 for the number of extra bits in the distance code.
2519573673dSchristos * This is the base-2 logarithm of the dictionary size minus six.
2529573673dSchristos *
2539573673dSchristos * - Compressed data is a combination of literals and length/distance pairs
2549573673dSchristos * terminated by an end code. Literals are either Huffman coded or
2559573673dSchristos * uncoded bytes. A length/distance pair is a coded length followed by a
2569573673dSchristos * coded distance to represent a string that occurs earlier in the
2579573673dSchristos * uncompressed data that occurs again at the current location.
2589573673dSchristos *
2599573673dSchristos * - A bit preceding a literal or length/distance pair indicates which comes
2609573673dSchristos * next, 0 for literals, 1 for length/distance.
2619573673dSchristos *
2629573673dSchristos * - If literals are uncoded, then the next eight bits are the literal, in the
263*fc4f4269Schristos * normal bit order in the stream, i.e. no bit-reversal is needed. Similarly,
2649573673dSchristos * no bit reversal is needed for either the length extra bits or the distance
2659573673dSchristos * extra bits.
2669573673dSchristos *
2679573673dSchristos * - Literal bytes are simply written to the output. A length/distance pair is
2689573673dSchristos * an instruction to copy previously uncompressed bytes to the output. The
2699573673dSchristos * copy is from distance bytes back in the output stream, copying for length
2709573673dSchristos * bytes.
2719573673dSchristos *
2729573673dSchristos * - Distances pointing before the beginning of the output data are not
2739573673dSchristos * permitted.
2749573673dSchristos *
2759573673dSchristos * - Overlapped copies, where the length is greater than the distance, are
2769573673dSchristos * allowed and common. For example, a distance of one and a length of 518
2779573673dSchristos * simply copies the last byte 518 times. A distance of four and a length of
2789573673dSchristos * twelve copies the last four bytes three times. A simple forward copy
2799573673dSchristos * ignoring whether the length is greater than the distance or not implements
2809573673dSchristos * this correctly.
2819573673dSchristos */
decomp(struct state * s)2829573673dSchristos local int decomp(struct state *s)
2839573673dSchristos {
2849573673dSchristos int lit; /* true if literals are coded */
2859573673dSchristos int dict; /* log2(dictionary size) - 6 */
2869573673dSchristos int symbol; /* decoded symbol, extra bits for distance */
2879573673dSchristos int len; /* length for copy */
2888cbf5cb7Schristos unsigned dist; /* distance for copy */
2899573673dSchristos int copy; /* copy counter */
2909573673dSchristos unsigned char *from, *to; /* copy pointers */
2919573673dSchristos static int virgin = 1; /* build tables once */
2929573673dSchristos static short litcnt[MAXBITS+1], litsym[256]; /* litcode memory */
2939573673dSchristos static short lencnt[MAXBITS+1], lensym[16]; /* lencode memory */
2949573673dSchristos static short distcnt[MAXBITS+1], distsym[64]; /* distcode memory */
2959573673dSchristos static struct huffman litcode = {litcnt, litsym}; /* length code */
2969573673dSchristos static struct huffman lencode = {lencnt, lensym}; /* length code */
2979573673dSchristos static struct huffman distcode = {distcnt, distsym};/* distance code */
2989573673dSchristos /* bit lengths of literal codes */
2999573673dSchristos static const unsigned char litlen[] = {
3009573673dSchristos 11, 124, 8, 7, 28, 7, 188, 13, 76, 4, 10, 8, 12, 10, 12, 10, 8, 23, 8,
3019573673dSchristos 9, 7, 6, 7, 8, 7, 6, 55, 8, 23, 24, 12, 11, 7, 9, 11, 12, 6, 7, 22, 5,
3029573673dSchristos 7, 24, 6, 11, 9, 6, 7, 22, 7, 11, 38, 7, 9, 8, 25, 11, 8, 11, 9, 12,
3039573673dSchristos 8, 12, 5, 38, 5, 38, 5, 11, 7, 5, 6, 21, 6, 10, 53, 8, 7, 24, 10, 27,
3049573673dSchristos 44, 253, 253, 253, 252, 252, 252, 13, 12, 45, 12, 45, 12, 61, 12, 45,
3059573673dSchristos 44, 173};
3069573673dSchristos /* bit lengths of length codes 0..15 */
3079573673dSchristos static const unsigned char lenlen[] = {2, 35, 36, 53, 38, 23};
3089573673dSchristos /* bit lengths of distance codes 0..63 */
3099573673dSchristos static const unsigned char distlen[] = {2, 20, 53, 230, 247, 151, 248};
3109573673dSchristos static const short base[16] = { /* base for length codes */
3119573673dSchristos 3, 2, 4, 5, 6, 7, 8, 9, 10, 12, 16, 24, 40, 72, 136, 264};
3129573673dSchristos static const char extra[16] = { /* extra bits for length codes */
3139573673dSchristos 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8};
3149573673dSchristos
3159573673dSchristos /* set up decoding tables (once--might not be thread-safe) */
3169573673dSchristos if (virgin) {
3179573673dSchristos construct(&litcode, litlen, sizeof(litlen));
3189573673dSchristos construct(&lencode, lenlen, sizeof(lenlen));
3199573673dSchristos construct(&distcode, distlen, sizeof(distlen));
3209573673dSchristos virgin = 0;
3219573673dSchristos }
3229573673dSchristos
3239573673dSchristos /* read header */
3249573673dSchristos lit = bits(s, 8);
3259573673dSchristos if (lit > 1) return -1;
3269573673dSchristos dict = bits(s, 8);
3279573673dSchristos if (dict < 4 || dict > 6) return -2;
3289573673dSchristos
3299573673dSchristos /* decode literals and length/distance pairs */
3309573673dSchristos do {
3319573673dSchristos if (bits(s, 1)) {
3329573673dSchristos /* get length */
3339573673dSchristos symbol = decode(s, &lencode);
3349573673dSchristos len = base[symbol] + bits(s, extra[symbol]);
3359573673dSchristos if (len == 519) break; /* end code */
3369573673dSchristos
3379573673dSchristos /* get distance */
3389573673dSchristos symbol = len == 2 ? 2 : dict;
3399573673dSchristos dist = decode(s, &distcode) << symbol;
3409573673dSchristos dist += bits(s, symbol);
3419573673dSchristos dist++;
3429573673dSchristos if (s->first && dist > s->next)
3439573673dSchristos return -3; /* distance too far back */
3449573673dSchristos
3459573673dSchristos /* copy length bytes from distance bytes back */
3469573673dSchristos do {
3479573673dSchristos to = s->out + s->next;
3489573673dSchristos from = to - dist;
3499573673dSchristos copy = MAXWIN;
3509573673dSchristos if (s->next < dist) {
3519573673dSchristos from += copy;
3529573673dSchristos copy = dist;
3539573673dSchristos }
3549573673dSchristos copy -= s->next;
3559573673dSchristos if (copy > len) copy = len;
3569573673dSchristos len -= copy;
3579573673dSchristos s->next += copy;
3589573673dSchristos do {
3599573673dSchristos *to++ = *from++;
3609573673dSchristos } while (--copy);
3619573673dSchristos if (s->next == MAXWIN) {
3629573673dSchristos if (s->outfun(s->outhow, s->out, s->next)) return 1;
3639573673dSchristos s->next = 0;
3649573673dSchristos s->first = 0;
3659573673dSchristos }
3669573673dSchristos } while (len != 0);
3679573673dSchristos }
3689573673dSchristos else {
3699573673dSchristos /* get literal and write it */
3709573673dSchristos symbol = lit ? decode(s, &litcode) : bits(s, 8);
3719573673dSchristos s->out[s->next++] = symbol;
3729573673dSchristos if (s->next == MAXWIN) {
3739573673dSchristos if (s->outfun(s->outhow, s->out, s->next)) return 1;
3749573673dSchristos s->next = 0;
3759573673dSchristos s->first = 0;
3769573673dSchristos }
3779573673dSchristos }
3789573673dSchristos } while (1);
3799573673dSchristos return 0;
3809573673dSchristos }
3819573673dSchristos
3829573673dSchristos /* See comments in blast.h */
blast(blast_in infun,void * inhow,blast_out outfun,void * outhow,unsigned * left,unsigned char ** in)383*fc4f4269Schristos int blast(blast_in infun, void *inhow, blast_out outfun, void *outhow,
384*fc4f4269Schristos unsigned *left, unsigned char **in)
3859573673dSchristos {
3869573673dSchristos struct state s; /* input/output state */
3879573673dSchristos int err; /* return value */
3889573673dSchristos
3899573673dSchristos /* initialize input state */
3909573673dSchristos s.infun = infun;
3919573673dSchristos s.inhow = inhow;
392*fc4f4269Schristos if (left != NULL && *left) {
393*fc4f4269Schristos s.left = *left;
394*fc4f4269Schristos s.in = *in;
395*fc4f4269Schristos }
396*fc4f4269Schristos else
3979573673dSchristos s.left = 0;
3989573673dSchristos s.bitbuf = 0;
3999573673dSchristos s.bitcnt = 0;
4009573673dSchristos
4019573673dSchristos /* initialize output state */
4029573673dSchristos s.outfun = outfun;
4039573673dSchristos s.outhow = outhow;
4049573673dSchristos s.next = 0;
4059573673dSchristos s.first = 1;
4069573673dSchristos
4079573673dSchristos /* return if bits() or decode() tries to read past available input */
4089573673dSchristos if (setjmp(s.env) != 0) /* if came back here via longjmp(), */
4099573673dSchristos err = 2; /* then skip decomp(), return error */
4109573673dSchristos else
4119573673dSchristos err = decomp(&s); /* decompress */
4129573673dSchristos
413*fc4f4269Schristos /* return unused input */
414*fc4f4269Schristos if (left != NULL)
415*fc4f4269Schristos *left = s.left;
416*fc4f4269Schristos if (in != NULL)
417*fc4f4269Schristos *in = s.left ? s.in : NULL;
418*fc4f4269Schristos
4199573673dSchristos /* write any leftover output and update the error code if needed */
4209573673dSchristos if (err != 1 && s.next && s.outfun(s.outhow, s.out, s.next) && err == 0)
4219573673dSchristos err = 1;
4229573673dSchristos return err;
4239573673dSchristos }
4249573673dSchristos
4259573673dSchristos #ifdef TEST
4269573673dSchristos /* Example of how to use blast() */
4279573673dSchristos #include <stdio.h>
4289573673dSchristos #include <stdlib.h>
4299573673dSchristos
4309573673dSchristos #define CHUNK 16384
4319573673dSchristos
inf(void * how,unsigned char ** buf)4329573673dSchristos local unsigned inf(void *how, unsigned char **buf)
4339573673dSchristos {
4349573673dSchristos static unsigned char hold[CHUNK];
4359573673dSchristos
4369573673dSchristos *buf = hold;
4379573673dSchristos return fread(hold, 1, CHUNK, (FILE *)how);
4389573673dSchristos }
4399573673dSchristos
outf(void * how,unsigned char * buf,unsigned len)4409573673dSchristos local int outf(void *how, unsigned char *buf, unsigned len)
4419573673dSchristos {
4429573673dSchristos return fwrite(buf, 1, len, (FILE *)how) != len;
4439573673dSchristos }
4449573673dSchristos
4459573673dSchristos /* Decompress a PKWare Compression Library stream from stdin to stdout */
main(void)4469573673dSchristos int main(void)
4479573673dSchristos {
448*fc4f4269Schristos int ret;
449*fc4f4269Schristos unsigned left;
4509573673dSchristos
4519573673dSchristos /* decompress to stdout */
452*fc4f4269Schristos left = 0;
453*fc4f4269Schristos ret = blast(inf, stdin, outf, stdout, &left, NULL);
454*fc4f4269Schristos if (ret != 0)
455*fc4f4269Schristos fprintf(stderr, "blast error: %d\n", ret);
4569573673dSchristos
457*fc4f4269Schristos /* count any leftover bytes */
458*fc4f4269Schristos while (getchar() != EOF)
459*fc4f4269Schristos left++;
460*fc4f4269Schristos if (left)
461*fc4f4269Schristos fprintf(stderr, "blast warning: %u unused bytes of input\n", left);
4629573673dSchristos
4639573673dSchristos /* return blast() error code */
4649573673dSchristos return ret;
4659573673dSchristos }
4669573673dSchristos #endif
467