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