kusano fc6ab3
/* blast.c
kusano fc6ab3
 * Copyright (C) 2003, 2012 Mark Adler
kusano fc6ab3
 * For conditions of distribution and use, see copyright notice in blast.h
kusano fc6ab3
 * version 1.2, 24 Oct 2012
kusano fc6ab3
 *
kusano fc6ab3
 * blast.c decompresses data compressed by the PKWare Compression Library.
kusano fc6ab3
 * This function provides functionality similar to the explode() function of
kusano fc6ab3
 * the PKWare library, hence the name "blast".
kusano fc6ab3
 *
kusano fc6ab3
 * This decompressor is based on the excellent format description provided by
kusano fc6ab3
 * Ben Rudiak-Gould in comp.compression on August 13, 2001.  Interestingly, the
kusano fc6ab3
 * example Ben provided in the post is incorrect.  The distance 110001 should
kusano fc6ab3
 * instead be 111000.  When corrected, the example byte stream becomes:
kusano fc6ab3
 *
kusano fc6ab3
 *    00 04 82 24 25 8f 80 7f
kusano fc6ab3
 *
kusano fc6ab3
 * which decompresses to "AIAIAIAIAIAIA" (without the quotes).
kusano fc6ab3
 */
kusano fc6ab3
kusano fc6ab3
/*
kusano fc6ab3
 * Change history:
kusano fc6ab3
 *
kusano fc6ab3
 * 1.0  12 Feb 2003     - First version
kusano fc6ab3
 * 1.1  16 Feb 2003     - Fixed distance check for > 4 GB uncompressed data
kusano fc6ab3
 * 1.2  24 Oct 2012     - Add note about using binary mode in stdio
kusano fc6ab3
 *                      - Fix comparisons of differently signed integers
kusano fc6ab3
 */
kusano fc6ab3
kusano fc6ab3
#include <setjmp.h>             /* for setjmp(), longjmp(), and jmp_buf */</setjmp.h>
kusano fc6ab3
#include "blast.h"              /* prototype for blast() */
kusano fc6ab3
kusano fc6ab3
#define local static            /* for local function definitions */
kusano fc6ab3
#define MAXBITS 13              /* maximum code length */
kusano fc6ab3
#define MAXWIN 4096             /* maximum window size */
kusano fc6ab3
kusano fc6ab3
/* input and output state */
kusano fc6ab3
struct state {
kusano fc6ab3
    /* input state */
kusano fc6ab3
    blast_in infun;             /* input function provided by user */
kusano fc6ab3
    void *inhow;                /* opaque information passed to infun() */
kusano fc6ab3
    unsigned char *in;          /* next input location */
kusano fc6ab3
    unsigned left;              /* available input at in */
kusano fc6ab3
    int bitbuf;                 /* bit buffer */
kusano fc6ab3
    int bitcnt;                 /* number of bits in bit buffer */
kusano fc6ab3
kusano fc6ab3
    /* input limit error return state for bits() and decode() */
kusano fc6ab3
    jmp_buf env;
kusano fc6ab3
kusano fc6ab3
    /* output state */
kusano fc6ab3
    blast_out outfun;           /* output function provided by user */
kusano fc6ab3
    void *outhow;               /* opaque information passed to outfun() */
kusano fc6ab3
    unsigned next;              /* index of next write location in out[] */
kusano fc6ab3
    int first;                  /* true to check distances (for first 4K) */
kusano fc6ab3
    unsigned char out[MAXWIN];  /* output buffer and sliding window */
kusano fc6ab3
};
kusano fc6ab3
kusano fc6ab3
/*
kusano fc6ab3
 * Return need bits from the input stream.  This always leaves less than
kusano fc6ab3
 * eight bits in the buffer.  bits() works properly for need == 0.
kusano fc6ab3
 *
kusano fc6ab3
 * Format notes:
kusano fc6ab3
 *
kusano fc6ab3
 * - Bits are stored in bytes from the least significant bit to the most
kusano fc6ab3
 *   significant bit.  Therefore bits are dropped from the bottom of the bit
kusano fc6ab3
 *   buffer, using shift right, and new bytes are appended to the top of the
kusano fc6ab3
 *   bit buffer, using shift left.
kusano fc6ab3
 */
kusano fc6ab3
local int bits(struct state *s, int need)
kusano fc6ab3
{
kusano fc6ab3
    int val;            /* bit accumulator */
kusano fc6ab3
kusano fc6ab3
    /* load at least need bits into val */
kusano fc6ab3
    val = s->bitbuf;
kusano fc6ab3
    while (s->bitcnt < need) {
kusano fc6ab3
        if (s->left == 0) {
kusano fc6ab3
            s->left = s->infun(s->inhow, &(s->in));
kusano fc6ab3
            if (s->left == 0) longjmp(s->env, 1);       /* out of input */
kusano fc6ab3
        }
kusano fc6ab3
        val |= (int)(*(s->in)++) << s->bitcnt;          /* load eight bits */
kusano fc6ab3
        s->left--;
kusano fc6ab3
        s->bitcnt += 8;
kusano fc6ab3
    }
kusano fc6ab3
kusano fc6ab3
    /* drop need bits and update buffer, always zero to seven bits left */
kusano fc6ab3
    s->bitbuf = val >> need;
kusano fc6ab3
    s->bitcnt -= need;
kusano fc6ab3
kusano fc6ab3
    /* return need bits, zeroing the bits above that */
kusano fc6ab3
    return val & ((1 << need) - 1);
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
/*
kusano fc6ab3
 * Huffman code decoding tables.  count[1..MAXBITS] is the number of symbols of
kusano fc6ab3
 * each length, which for a canonical code are stepped through in order.
kusano fc6ab3
 * symbol[] are the symbol values in canonical order, where the number of
kusano fc6ab3
 * entries is the sum of the counts in count[].  The decoding process can be
kusano fc6ab3
 * seen in the function decode() below.
kusano fc6ab3
 */
kusano fc6ab3
struct huffman {
kusano fc6ab3
    short *count;       /* number of symbols of each length */
kusano fc6ab3
    short *symbol;      /* canonically ordered symbols */
kusano fc6ab3
};
kusano fc6ab3
kusano fc6ab3
/*
kusano fc6ab3
 * Decode a code from the stream s using huffman table h.  Return the symbol or
kusano fc6ab3
 * a negative value if there is an error.  If all of the lengths are zero, i.e.
kusano fc6ab3
 * an empty code, or if the code is incomplete and an invalid code is received,
kusano fc6ab3
 * then -9 is returned after reading MAXBITS bits.
kusano fc6ab3
 *
kusano fc6ab3
 * Format notes:
kusano fc6ab3
 *
kusano fc6ab3
 * - The codes as stored in the compressed data are bit-reversed relative to
kusano fc6ab3
 *   a simple integer ordering of codes of the same lengths.  Hence below the
kusano fc6ab3
 *   bits are pulled from the compressed data one at a time and used to
kusano fc6ab3
 *   build the code value reversed from what is in the stream in order to
kusano fc6ab3
 *   permit simple integer comparisons for decoding.
kusano fc6ab3
 *
kusano fc6ab3
 * - The first code for the shortest length is all ones.  Subsequent codes of
kusano fc6ab3
 *   the same length are simply integer decrements of the previous code.  When
kusano fc6ab3
 *   moving up a length, a one bit is appended to the code.  For a complete
kusano fc6ab3
 *   code, the last code of the longest length will be all zeros.  To support
kusano fc6ab3
 *   this ordering, the bits pulled during decoding are inverted to apply the
kusano fc6ab3
 *   more "natural" ordering starting with all zeros and incrementing.
kusano fc6ab3
 */
kusano fc6ab3
local int decode(struct state *s, struct huffman *h)
kusano fc6ab3
{
kusano fc6ab3
    int len;            /* current number of bits in code */
kusano fc6ab3
    int code;           /* len bits being decoded */
kusano fc6ab3
    int first;          /* first code of length len */
kusano fc6ab3
    int count;          /* number of codes of length len */
kusano fc6ab3
    int index;          /* index of first code of length len in symbol table */
kusano fc6ab3
    int bitbuf;         /* bits from stream */
kusano fc6ab3
    int left;           /* bits left in next or left to process */
kusano fc6ab3
    short *next;        /* next number of codes */
kusano fc6ab3
kusano fc6ab3
    bitbuf = s->bitbuf;
kusano fc6ab3
    left = s->bitcnt;
kusano fc6ab3
    code = first = index = 0;
kusano fc6ab3
    len = 1;
kusano fc6ab3
    next = h->count + 1;
kusano fc6ab3
    while (1) {
kusano fc6ab3
        while (left--) {
kusano fc6ab3
            code |= (bitbuf & 1) ^ 1;   /* invert code */
kusano fc6ab3
            bitbuf >>= 1;
kusano fc6ab3
            count = *next++;
kusano fc6ab3
            if (code < first + count) { /* if length len, return symbol */
kusano fc6ab3
                s->bitbuf = bitbuf;
kusano fc6ab3
                s->bitcnt = (s->bitcnt - len) & 7;
kusano fc6ab3
                return h->symbol[index + (code - first)];
kusano fc6ab3
            }
kusano fc6ab3
            index += count;             /* else update for next length */
kusano fc6ab3
            first += count;
kusano fc6ab3
            first <<= 1;
kusano fc6ab3
            code <<= 1;
kusano fc6ab3
            len++;
kusano fc6ab3
        }
kusano fc6ab3
        left = (MAXBITS+1) - len;
kusano fc6ab3
        if (left == 0) break;
kusano fc6ab3
        if (s->left == 0) {
kusano fc6ab3
            s->left = s->infun(s->inhow, &(s->in));
kusano fc6ab3
            if (s->left == 0) longjmp(s->env, 1);       /* out of input */
kusano fc6ab3
        }
kusano fc6ab3
        bitbuf = *(s->in)++;
kusano fc6ab3
        s->left--;
kusano fc6ab3
        if (left > 8) left = 8;
kusano fc6ab3
    }
kusano fc6ab3
    return -9;                          /* ran out of codes */
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
/*
kusano fc6ab3
 * Given a list of repeated code lengths rep[0..n-1], where each byte is a
kusano fc6ab3
 * count (high four bits + 1) and a code length (low four bits), generate the
kusano fc6ab3
 * list of code lengths.  This compaction reduces the size of the object code.
kusano fc6ab3
 * Then given the list of code lengths length[0..n-1] representing a canonical
kusano fc6ab3
 * Huffman code for n symbols, construct the tables required to decode those
kusano fc6ab3
 * codes.  Those tables are the number of codes of each length, and the symbols
kusano fc6ab3
 * sorted by length, retaining their original order within each length.  The
kusano fc6ab3
 * return value is zero for a complete code set, negative for an over-
kusano fc6ab3
 * subscribed code set, and positive for an incomplete code set.  The tables
kusano fc6ab3
 * can be used if the return value is zero or positive, but they cannot be used
kusano fc6ab3
 * if the return value is negative.  If the return value is zero, it is not
kusano fc6ab3
 * possible for decode() using that table to return an error--any stream of
kusano fc6ab3
 * enough bits will resolve to a symbol.  If the return value is positive, then
kusano fc6ab3
 * it is possible for decode() using that table to return an error for received
kusano fc6ab3
 * codes past the end of the incomplete lengths.
kusano fc6ab3
 */
kusano fc6ab3
local int construct(struct huffman *h, const unsigned char *rep, int n)
kusano fc6ab3
{
kusano fc6ab3
    int symbol;         /* current symbol when stepping through length[] */
kusano fc6ab3
    int len;            /* current length when stepping through h->count[] */
kusano fc6ab3
    int left;           /* number of possible codes left of current length */
kusano fc6ab3
    short offs[MAXBITS+1];      /* offsets in symbol table for each length */
kusano fc6ab3
    short length[256];  /* code lengths */
kusano fc6ab3
kusano fc6ab3
    /* convert compact repeat counts into symbol bit length list */
kusano fc6ab3
    symbol = 0;
kusano fc6ab3
    do {
kusano fc6ab3
        len = *rep++;
kusano fc6ab3
        left = (len >> 4) + 1;
kusano fc6ab3
        len &= 15;
kusano fc6ab3
        do {
kusano fc6ab3
            length[symbol++] = len;
kusano fc6ab3
        } while (--left);
kusano fc6ab3
    } while (--n);
kusano fc6ab3
    n = symbol;
kusano fc6ab3
kusano fc6ab3
    /* count number of codes of each length */
kusano fc6ab3
    for (len = 0; len <= MAXBITS; len++)
kusano fc6ab3
        h->count[len] = 0;
kusano fc6ab3
    for (symbol = 0; symbol < n; symbol++)
kusano fc6ab3
        (h->count[length[symbol]])++;   /* assumes lengths are within bounds */
kusano fc6ab3
    if (h->count[0] == n)               /* no codes! */
kusano fc6ab3
        return 0;                       /* complete, but decode() will fail */
kusano fc6ab3
kusano fc6ab3
    /* check for an over-subscribed or incomplete set of lengths */
kusano fc6ab3
    left = 1;                           /* one possible code of zero length */
kusano fc6ab3
    for (len = 1; len <= MAXBITS; len++) {
kusano fc6ab3
        left <<= 1;                     /* one more bit, double codes left */
kusano fc6ab3
        left -= h->count[len];          /* deduct count from possible codes */
kusano fc6ab3
        if (left < 0) return left;      /* over-subscribed--return negative */
kusano fc6ab3
    }                                   /* left > 0 means incomplete */
kusano fc6ab3
kusano fc6ab3
    /* generate offsets into symbol table for each length for sorting */
kusano fc6ab3
    offs[1] = 0;
kusano fc6ab3
    for (len = 1; len < MAXBITS; len++)
kusano fc6ab3
        offs[len + 1] = offs[len] + h->count[len];
kusano fc6ab3
kusano fc6ab3
    /*
kusano fc6ab3
     * put symbols in table sorted by length, by symbol order within each
kusano fc6ab3
     * length
kusano fc6ab3
     */
kusano fc6ab3
    for (symbol = 0; symbol < n; symbol++)
kusano fc6ab3
        if (length[symbol] != 0)
kusano fc6ab3
            h->symbol[offs[length[symbol]]++] = symbol;
kusano fc6ab3
kusano fc6ab3
    /* return zero for complete set, positive for incomplete set */
kusano fc6ab3
    return left;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
/*
kusano fc6ab3
 * Decode PKWare Compression Library stream.
kusano fc6ab3
 *
kusano fc6ab3
 * Format notes:
kusano fc6ab3
 *
kusano fc6ab3
 * - First byte is 0 if literals are uncoded or 1 if they are coded.  Second
kusano fc6ab3
 *   byte is 4, 5, or 6 for the number of extra bits in the distance code.
kusano fc6ab3
 *   This is the base-2 logarithm of the dictionary size minus six.
kusano fc6ab3
 *
kusano fc6ab3
 * - Compressed data is a combination of literals and length/distance pairs
kusano fc6ab3
 *   terminated by an end code.  Literals are either Huffman coded or
kusano fc6ab3
 *   uncoded bytes.  A length/distance pair is a coded length followed by a
kusano fc6ab3
 *   coded distance to represent a string that occurs earlier in the
kusano fc6ab3
 *   uncompressed data that occurs again at the current location.
kusano fc6ab3
 *
kusano fc6ab3
 * - A bit preceding a literal or length/distance pair indicates which comes
kusano fc6ab3
 *   next, 0 for literals, 1 for length/distance.
kusano fc6ab3
 *
kusano fc6ab3
 * - If literals are uncoded, then the next eight bits are the literal, in the
kusano fc6ab3
 *   normal bit order in th stream, i.e. no bit-reversal is needed. Similarly,
kusano fc6ab3
 *   no bit reversal is needed for either the length extra bits or the distance
kusano fc6ab3
 *   extra bits.
kusano fc6ab3
 *
kusano fc6ab3
 * - Literal bytes are simply written to the output.  A length/distance pair is
kusano fc6ab3
 *   an instruction to copy previously uncompressed bytes to the output.  The
kusano fc6ab3
 *   copy is from distance bytes back in the output stream, copying for length
kusano fc6ab3
 *   bytes.
kusano fc6ab3
 *
kusano fc6ab3
 * - Distances pointing before the beginning of the output data are not
kusano fc6ab3
 *   permitted.
kusano fc6ab3
 *
kusano fc6ab3
 * - Overlapped copies, where the length is greater than the distance, are
kusano fc6ab3
 *   allowed and common.  For example, a distance of one and a length of 518
kusano fc6ab3
 *   simply copies the last byte 518 times.  A distance of four and a length of
kusano fc6ab3
 *   twelve copies the last four bytes three times.  A simple forward copy
kusano fc6ab3
 *   ignoring whether the length is greater than the distance or not implements
kusano fc6ab3
 *   this correctly.
kusano fc6ab3
 */
kusano fc6ab3
local int decomp(struct state *s)
kusano fc6ab3
{
kusano fc6ab3
    int lit;            /* true if literals are coded */
kusano fc6ab3
    int dict;           /* log2(dictionary size) - 6 */
kusano fc6ab3
    int symbol;         /* decoded symbol, extra bits for distance */
kusano fc6ab3
    int len;            /* length for copy */
kusano fc6ab3
    unsigned dist;      /* distance for copy */
kusano fc6ab3
    int copy;           /* copy counter */
kusano fc6ab3
    unsigned char *from, *to;   /* copy pointers */
kusano fc6ab3
    static int virgin = 1;                              /* build tables once */
kusano fc6ab3
    static short litcnt[MAXBITS+1], litsym[256];        /* litcode memory */
kusano fc6ab3
    static short lencnt[MAXBITS+1], lensym[16];         /* lencode memory */
kusano fc6ab3
    static short distcnt[MAXBITS+1], distsym[64];       /* distcode memory */
kusano fc6ab3
    static struct huffman litcode = {litcnt, litsym};   /* length code */
kusano fc6ab3
    static struct huffman lencode = {lencnt, lensym};   /* length code */
kusano fc6ab3
    static struct huffman distcode = {distcnt, distsym};/* distance code */
kusano fc6ab3
        /* bit lengths of literal codes */
kusano fc6ab3
    static const unsigned char litlen[] = {
kusano fc6ab3
        11, 124, 8, 7, 28, 7, 188, 13, 76, 4, 10, 8, 12, 10, 12, 10, 8, 23, 8,
kusano fc6ab3
        9, 7, 6, 7, 8, 7, 6, 55, 8, 23, 24, 12, 11, 7, 9, 11, 12, 6, 7, 22, 5,
kusano fc6ab3
        7, 24, 6, 11, 9, 6, 7, 22, 7, 11, 38, 7, 9, 8, 25, 11, 8, 11, 9, 12,
kusano fc6ab3
        8, 12, 5, 38, 5, 38, 5, 11, 7, 5, 6, 21, 6, 10, 53, 8, 7, 24, 10, 27,
kusano fc6ab3
        44, 253, 253, 253, 252, 252, 252, 13, 12, 45, 12, 45, 12, 61, 12, 45,
kusano fc6ab3
        44, 173};
kusano fc6ab3
        /* bit lengths of length codes 0..15 */
kusano fc6ab3
    static const unsigned char lenlen[] = {2, 35, 36, 53, 38, 23};
kusano fc6ab3
        /* bit lengths of distance codes 0..63 */
kusano fc6ab3
    static const unsigned char distlen[] = {2, 20, 53, 230, 247, 151, 248};
kusano fc6ab3
    static const short base[16] = {     /* base for length codes */
kusano fc6ab3
        3, 2, 4, 5, 6, 7, 8, 9, 10, 12, 16, 24, 40, 72, 136, 264};
kusano fc6ab3
    static const char extra[16] = {     /* extra bits for length codes */
kusano fc6ab3
        0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8};
kusano fc6ab3
kusano fc6ab3
    /* set up decoding tables (once--might not be thread-safe) */
kusano fc6ab3
    if (virgin) {
kusano fc6ab3
        construct(&litcode, litlen, sizeof(litlen));
kusano fc6ab3
        construct(&lencode, lenlen, sizeof(lenlen));
kusano fc6ab3
        construct(&distcode, distlen, sizeof(distlen));
kusano fc6ab3
        virgin = 0;
kusano fc6ab3
    }
kusano fc6ab3
kusano fc6ab3
    /* read header */
kusano fc6ab3
    lit = bits(s, 8);
kusano fc6ab3
    if (lit > 1) return -1;
kusano fc6ab3
    dict = bits(s, 8);
kusano fc6ab3
    if (dict < 4 || dict > 6) return -2;
kusano fc6ab3
kusano fc6ab3
    /* decode literals and length/distance pairs */
kusano fc6ab3
    do {
kusano fc6ab3
        if (bits(s, 1)) {
kusano fc6ab3
            /* get length */
kusano fc6ab3
            symbol = decode(s, &lencode);
kusano fc6ab3
            len = base[symbol] + bits(s, extra[symbol]);
kusano fc6ab3
            if (len == 519) break;              /* end code */
kusano fc6ab3
kusano fc6ab3
            /* get distance */
kusano fc6ab3
            symbol = len == 2 ? 2 : dict;
kusano fc6ab3
            dist = decode(s, &distcode) << symbol;
kusano fc6ab3
            dist += bits(s, symbol);
kusano fc6ab3
            dist++;
kusano fc6ab3
            if (s->first && dist > s->next)
kusano fc6ab3
                return -3;              /* distance too far back */
kusano fc6ab3
kusano fc6ab3
            /* copy length bytes from distance bytes back */
kusano fc6ab3
            do {
kusano fc6ab3
                to = s->out + s->next;
kusano fc6ab3
                from = to - dist;
kusano fc6ab3
                copy = MAXWIN;
kusano fc6ab3
                if (s->next < dist) {
kusano fc6ab3
                    from += copy;
kusano fc6ab3
                    copy = dist;
kusano fc6ab3
                }
kusano fc6ab3
                copy -= s->next;
kusano fc6ab3
                if (copy > len) copy = len;
kusano fc6ab3
                len -= copy;
kusano fc6ab3
                s->next += copy;
kusano fc6ab3
                do {
kusano fc6ab3
                    *to++ = *from++;
kusano fc6ab3
                } while (--copy);
kusano fc6ab3
                if (s->next == MAXWIN) {
kusano fc6ab3
                    if (s->outfun(s->outhow, s->out, s->next)) return 1;
kusano fc6ab3
                    s->next = 0;
kusano fc6ab3
                    s->first = 0;
kusano fc6ab3
                }
kusano fc6ab3
            } while (len != 0);
kusano fc6ab3
        }
kusano fc6ab3
        else {
kusano fc6ab3
            /* get literal and write it */
kusano fc6ab3
            symbol = lit ? decode(s, &litcode) : bits(s, 8);
kusano fc6ab3
            s->out[s->next++] = symbol;
kusano fc6ab3
            if (s->next == MAXWIN) {
kusano fc6ab3
                if (s->outfun(s->outhow, s->out, s->next)) return 1;
kusano fc6ab3
                s->next = 0;
kusano fc6ab3
                s->first = 0;
kusano fc6ab3
            }
kusano fc6ab3
        }
kusano fc6ab3
    } while (1);
kusano fc6ab3
    return 0;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
/* See comments in blast.h */
kusano fc6ab3
int blast(blast_in infun, void *inhow, blast_out outfun, void *outhow)
kusano fc6ab3
{
kusano fc6ab3
    struct state s;             /* input/output state */
kusano fc6ab3
    int err;                    /* return value */
kusano fc6ab3
kusano fc6ab3
    /* initialize input state */
kusano fc6ab3
    s.infun = infun;
kusano fc6ab3
    s.inhow = inhow;
kusano fc6ab3
    s.left = 0;
kusano fc6ab3
    s.bitbuf = 0;
kusano fc6ab3
    s.bitcnt = 0;
kusano fc6ab3
kusano fc6ab3
    /* initialize output state */
kusano fc6ab3
    s.outfun = outfun;
kusano fc6ab3
    s.outhow = outhow;
kusano fc6ab3
    s.next = 0;
kusano fc6ab3
    s.first = 1;
kusano fc6ab3
kusano fc6ab3
    /* return if bits() or decode() tries to read past available input */
kusano fc6ab3
    if (setjmp(s.env) != 0)             /* if came back here via longjmp(), */
kusano fc6ab3
        err = 2;                        /*  then skip decomp(), return error */
kusano fc6ab3
    else
kusano fc6ab3
        err = decomp(&s);               /* decompress */
kusano fc6ab3
kusano fc6ab3
    /* write any leftover output and update the error code if needed */
kusano fc6ab3
    if (err != 1 && s.next && s.outfun(s.outhow, s.out, s.next) && err == 0)
kusano fc6ab3
        err = 1;
kusano fc6ab3
    return err;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
#ifdef TEST
kusano fc6ab3
/* Example of how to use blast() */
kusano fc6ab3
#include <stdio.h></stdio.h>
kusano fc6ab3
#include <stdlib.h></stdlib.h>
kusano fc6ab3
kusano fc6ab3
#define CHUNK 16384
kusano fc6ab3
kusano fc6ab3
local unsigned inf(void *how, unsigned char **buf)
kusano fc6ab3
{
kusano fc6ab3
    static unsigned char hold[CHUNK];
kusano fc6ab3
kusano fc6ab3
    *buf = hold;
kusano fc6ab3
    return fread(hold, 1, CHUNK, (FILE *)how);
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
local int outf(void *how, unsigned char *buf, unsigned len)
kusano fc6ab3
{
kusano fc6ab3
    return fwrite(buf, 1, len, (FILE *)how) != len;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
/* Decompress a PKWare Compression Library stream from stdin to stdout */
kusano fc6ab3
int main(void)
kusano fc6ab3
{
kusano fc6ab3
    int ret, n;
kusano fc6ab3
kusano fc6ab3
    /* decompress to stdout */
kusano fc6ab3
    ret = blast(inf, stdin, outf, stdout);
kusano fc6ab3
    if (ret != 0) fprintf(stderr, "blast error: %d\n", ret);
kusano fc6ab3
kusano fc6ab3
    /* see if there are any leftover bytes */
kusano fc6ab3
    n = 0;
kusano fc6ab3
    while (getchar() != EOF) n++;
kusano fc6ab3
    if (n) fprintf(stderr, "blast warning: %d unused bytes of input\n", n);
kusano fc6ab3
kusano fc6ab3
    /* return blast() error code */
kusano fc6ab3
    return ret;
kusano fc6ab3
}
kusano fc6ab3
#endif