xref: /netbsd-src/external/gpl3/binutils.old/dist/zlib/contrib/puff/puff.c (revision ede781334f5dc56e6b74c3945d364b5b98850996)
116dce513Schristos /*
216dce513Schristos  * puff.c
316dce513Schristos  * Copyright (C) 2002-2013 Mark Adler
416dce513Schristos  * For conditions of distribution and use, see copyright notice in puff.h
516dce513Schristos  * version 2.3, 21 Jan 2013
616dce513Schristos  *
716dce513Schristos  * puff.c is a simple inflate written to be an unambiguous way to specify the
816dce513Schristos  * deflate format.  It is not written for speed but rather simplicity.  As a
916dce513Schristos  * side benefit, this code might actually be useful when small code is more
1016dce513Schristos  * important than speed, such as bootstrap applications.  For typical deflate
1116dce513Schristos  * data, zlib's inflate() is about four times as fast as puff().  zlib's
1216dce513Schristos  * inflate compiles to around 20K on my machine, whereas puff.c compiles to
1316dce513Schristos  * around 4K on my machine (a PowerPC using GNU cc).  If the faster decode()
1416dce513Schristos  * function here is used, then puff() is only twice as slow as zlib's
1516dce513Schristos  * inflate().
1616dce513Schristos  *
1716dce513Schristos  * All dynamically allocated memory comes from the stack.  The stack required
1816dce513Schristos  * is less than 2K bytes.  This code is compatible with 16-bit int's and
1916dce513Schristos  * assumes that long's are at least 32 bits.  puff.c uses the short data type,
20*ede78133Schristos  * assumed to be 16 bits, for arrays in order to conserve memory.  The code
2116dce513Schristos  * works whether integers are stored big endian or little endian.
2216dce513Schristos  *
2316dce513Schristos  * In the comments below are "Format notes" that describe the inflate process
2416dce513Schristos  * and document some of the less obvious aspects of the format.  This source
2516dce513Schristos  * code is meant to supplement RFC 1951, which formally describes the deflate
2616dce513Schristos  * format:
2716dce513Schristos  *
2816dce513Schristos  *    http://www.zlib.org/rfc-deflate.html
2916dce513Schristos  */
3016dce513Schristos 
3116dce513Schristos /*
3216dce513Schristos  * Change history:
3316dce513Schristos  *
3416dce513Schristos  * 1.0  10 Feb 2002     - First version
3516dce513Schristos  * 1.1  17 Feb 2002     - Clarifications of some comments and notes
3616dce513Schristos  *                      - Update puff() dest and source pointers on negative
3716dce513Schristos  *                        errors to facilitate debugging deflators
3816dce513Schristos  *                      - Remove longest from struct huffman -- not needed
3916dce513Schristos  *                      - Simplify offs[] index in construct()
4016dce513Schristos  *                      - Add input size and checking, using longjmp() to
4116dce513Schristos  *                        maintain easy readability
4216dce513Schristos  *                      - Use short data type for large arrays
4316dce513Schristos  *                      - Use pointers instead of long to specify source and
4416dce513Schristos  *                        destination sizes to avoid arbitrary 4 GB limits
4516dce513Schristos  * 1.2  17 Mar 2002     - Add faster version of decode(), doubles speed (!),
4616dce513Schristos  *                        but leave simple version for readabilty
4716dce513Schristos  *                      - Make sure invalid distances detected if pointers
4816dce513Schristos  *                        are 16 bits
4916dce513Schristos  *                      - Fix fixed codes table error
5016dce513Schristos  *                      - Provide a scanning mode for determining size of
5116dce513Schristos  *                        uncompressed data
5216dce513Schristos  * 1.3  20 Mar 2002     - Go back to lengths for puff() parameters [Gailly]
5316dce513Schristos  *                      - Add a puff.h file for the interface
5416dce513Schristos  *                      - Add braces in puff() for else do [Gailly]
5516dce513Schristos  *                      - Use indexes instead of pointers for readability
5616dce513Schristos  * 1.4  31 Mar 2002     - Simplify construct() code set check
5716dce513Schristos  *                      - Fix some comments
5816dce513Schristos  *                      - Add FIXLCODES #define
5916dce513Schristos  * 1.5   6 Apr 2002     - Minor comment fixes
6016dce513Schristos  * 1.6   7 Aug 2002     - Minor format changes
6116dce513Schristos  * 1.7   3 Mar 2003     - Added test code for distribution
6216dce513Schristos  *                      - Added zlib-like license
6316dce513Schristos  * 1.8   9 Jan 2004     - Added some comments on no distance codes case
6416dce513Schristos  * 1.9  21 Feb 2008     - Fix bug on 16-bit integer architectures [Pohland]
6516dce513Schristos  *                      - Catch missing end-of-block symbol error
6616dce513Schristos  * 2.0  25 Jul 2008     - Add #define to permit distance too far back
6716dce513Schristos  *                      - Add option in TEST code for puff to write the data
6816dce513Schristos  *                      - Add option in TEST code to skip input bytes
6916dce513Schristos  *                      - Allow TEST code to read from piped stdin
7016dce513Schristos  * 2.1   4 Apr 2010     - Avoid variable initialization for happier compilers
7116dce513Schristos  *                      - Avoid unsigned comparisons for even happier compilers
7216dce513Schristos  * 2.2  25 Apr 2010     - Fix bug in variable initializations [Oberhumer]
7316dce513Schristos  *                      - Add const where appropriate [Oberhumer]
7416dce513Schristos  *                      - Split if's and ?'s for coverage testing
7516dce513Schristos  *                      - Break out test code to separate file
7616dce513Schristos  *                      - Move NIL to puff.h
7716dce513Schristos  *                      - Allow incomplete code only if single code length is 1
7816dce513Schristos  *                      - Add full code coverage test to Makefile
7916dce513Schristos  * 2.3  21 Jan 2013     - Check for invalid code length codes in dynamic blocks
8016dce513Schristos  */
8116dce513Schristos 
8216dce513Schristos #include <setjmp.h>             /* for setjmp(), longjmp(), and jmp_buf */
8316dce513Schristos #include "puff.h"               /* prototype for puff() */
8416dce513Schristos 
8516dce513Schristos #define local static            /* for local function definitions */
8616dce513Schristos 
8716dce513Schristos /*
8816dce513Schristos  * Maximums for allocations and loops.  It is not useful to change these --
8916dce513Schristos  * they are fixed by the deflate format.
9016dce513Schristos  */
9116dce513Schristos #define MAXBITS 15              /* maximum bits in a code */
9216dce513Schristos #define MAXLCODES 286           /* maximum number of literal/length codes */
9316dce513Schristos #define MAXDCODES 30            /* maximum number of distance codes */
9416dce513Schristos #define MAXCODES (MAXLCODES+MAXDCODES)  /* maximum codes lengths to read */
9516dce513Schristos #define FIXLCODES 288           /* number of fixed literal/length codes */
9616dce513Schristos 
9716dce513Schristos /* input and output state */
9816dce513Schristos struct state {
9916dce513Schristos     /* output state */
10016dce513Schristos     unsigned char *out;         /* output buffer */
10116dce513Schristos     unsigned long outlen;       /* available space at out */
10216dce513Schristos     unsigned long outcnt;       /* bytes written to out so far */
10316dce513Schristos 
10416dce513Schristos     /* input state */
10516dce513Schristos     const unsigned char *in;    /* input buffer */
10616dce513Schristos     unsigned long inlen;        /* available input at in */
10716dce513Schristos     unsigned long incnt;        /* bytes read so far */
10816dce513Schristos     int bitbuf;                 /* bit buffer */
10916dce513Schristos     int bitcnt;                 /* number of bits in bit buffer */
11016dce513Schristos 
11116dce513Schristos     /* input limit error return state for bits() and decode() */
11216dce513Schristos     jmp_buf env;
11316dce513Schristos };
11416dce513Schristos 
11516dce513Schristos /*
11616dce513Schristos  * Return need bits from the input stream.  This always leaves less than
11716dce513Schristos  * eight bits in the buffer.  bits() works properly for need == 0.
11816dce513Schristos  *
11916dce513Schristos  * Format notes:
12016dce513Schristos  *
12116dce513Schristos  * - Bits are stored in bytes from the least significant bit to the most
12216dce513Schristos  *   significant bit.  Therefore bits are dropped from the bottom of the bit
12316dce513Schristos  *   buffer, using shift right, and new bytes are appended to the top of the
12416dce513Schristos  *   bit buffer, using shift left.
12516dce513Schristos  */
bits(struct state * s,int need)12616dce513Schristos local int bits(struct state *s, int need)
12716dce513Schristos {
12816dce513Schristos     long val;           /* bit accumulator (can use up to 20 bits) */
12916dce513Schristos 
13016dce513Schristos     /* load at least need bits into val */
13116dce513Schristos     val = s->bitbuf;
13216dce513Schristos     while (s->bitcnt < need) {
13316dce513Schristos         if (s->incnt == s->inlen)
13416dce513Schristos             longjmp(s->env, 1);         /* out of input */
13516dce513Schristos         val |= (long)(s->in[s->incnt++]) << s->bitcnt;  /* load eight bits */
13616dce513Schristos         s->bitcnt += 8;
13716dce513Schristos     }
13816dce513Schristos 
13916dce513Schristos     /* drop need bits and update buffer, always zero to seven bits left */
14016dce513Schristos     s->bitbuf = (int)(val >> need);
14116dce513Schristos     s->bitcnt -= need;
14216dce513Schristos 
14316dce513Schristos     /* return need bits, zeroing the bits above that */
14416dce513Schristos     return (int)(val & ((1L << need) - 1));
14516dce513Schristos }
14616dce513Schristos 
14716dce513Schristos /*
14816dce513Schristos  * Process a stored block.
14916dce513Schristos  *
15016dce513Schristos  * Format notes:
15116dce513Schristos  *
15216dce513Schristos  * - After the two-bit stored block type (00), the stored block length and
15316dce513Schristos  *   stored bytes are byte-aligned for fast copying.  Therefore any leftover
15416dce513Schristos  *   bits in the byte that has the last bit of the type, as many as seven, are
15516dce513Schristos  *   discarded.  The value of the discarded bits are not defined and should not
15616dce513Schristos  *   be checked against any expectation.
15716dce513Schristos  *
15816dce513Schristos  * - The second inverted copy of the stored block length does not have to be
15916dce513Schristos  *   checked, but it's probably a good idea to do so anyway.
16016dce513Schristos  *
16116dce513Schristos  * - A stored block can have zero length.  This is sometimes used to byte-align
16216dce513Schristos  *   subsets of the compressed data for random access or partial recovery.
16316dce513Schristos  */
stored(struct state * s)16416dce513Schristos local int stored(struct state *s)
16516dce513Schristos {
16616dce513Schristos     unsigned len;       /* length of stored block */
16716dce513Schristos 
16816dce513Schristos     /* discard leftover bits from current byte (assumes s->bitcnt < 8) */
16916dce513Schristos     s->bitbuf = 0;
17016dce513Schristos     s->bitcnt = 0;
17116dce513Schristos 
17216dce513Schristos     /* get length and check against its one's complement */
17316dce513Schristos     if (s->incnt + 4 > s->inlen)
17416dce513Schristos         return 2;                               /* not enough input */
17516dce513Schristos     len = s->in[s->incnt++];
17616dce513Schristos     len |= s->in[s->incnt++] << 8;
17716dce513Schristos     if (s->in[s->incnt++] != (~len & 0xff) ||
17816dce513Schristos         s->in[s->incnt++] != ((~len >> 8) & 0xff))
17916dce513Schristos         return -2;                              /* didn't match complement! */
18016dce513Schristos 
18116dce513Schristos     /* copy len bytes from in to out */
18216dce513Schristos     if (s->incnt + len > s->inlen)
18316dce513Schristos         return 2;                               /* not enough input */
18416dce513Schristos     if (s->out != NIL) {
18516dce513Schristos         if (s->outcnt + len > s->outlen)
18616dce513Schristos             return 1;                           /* not enough output space */
18716dce513Schristos         while (len--)
18816dce513Schristos             s->out[s->outcnt++] = s->in[s->incnt++];
18916dce513Schristos     }
19016dce513Schristos     else {                                      /* just scanning */
19116dce513Schristos         s->outcnt += len;
19216dce513Schristos         s->incnt += len;
19316dce513Schristos     }
19416dce513Schristos 
19516dce513Schristos     /* done with a valid stored block */
19616dce513Schristos     return 0;
19716dce513Schristos }
19816dce513Schristos 
19916dce513Schristos /*
20016dce513Schristos  * Huffman code decoding tables.  count[1..MAXBITS] is the number of symbols of
20116dce513Schristos  * each length, which for a canonical code are stepped through in order.
20216dce513Schristos  * symbol[] are the symbol values in canonical order, where the number of
20316dce513Schristos  * entries is the sum of the counts in count[].  The decoding process can be
20416dce513Schristos  * seen in the function decode() below.
20516dce513Schristos  */
20616dce513Schristos struct huffman {
20716dce513Schristos     short *count;       /* number of symbols of each length */
20816dce513Schristos     short *symbol;      /* canonically ordered symbols */
20916dce513Schristos };
21016dce513Schristos 
21116dce513Schristos /*
21216dce513Schristos  * Decode a code from the stream s using huffman table h.  Return the symbol or
21316dce513Schristos  * a negative value if there is an error.  If all of the lengths are zero, i.e.
21416dce513Schristos  * an empty code, or if the code is incomplete and an invalid code is received,
21516dce513Schristos  * then -10 is returned after reading MAXBITS bits.
21616dce513Schristos  *
21716dce513Schristos  * Format notes:
21816dce513Schristos  *
21916dce513Schristos  * - The codes as stored in the compressed data are bit-reversed relative to
22016dce513Schristos  *   a simple integer ordering of codes of the same lengths.  Hence below the
22116dce513Schristos  *   bits are pulled from the compressed data one at a time and used to
22216dce513Schristos  *   build the code value reversed from what is in the stream in order to
22316dce513Schristos  *   permit simple integer comparisons for decoding.  A table-based decoding
22416dce513Schristos  *   scheme (as used in zlib) does not need to do this reversal.
22516dce513Schristos  *
22616dce513Schristos  * - The first code for the shortest length is all zeros.  Subsequent codes of
22716dce513Schristos  *   the same length are simply integer increments of the previous code.  When
22816dce513Schristos  *   moving up a length, a zero bit is appended to the code.  For a complete
22916dce513Schristos  *   code, the last code of the longest length will be all ones.
23016dce513Schristos  *
23116dce513Schristos  * - Incomplete codes are handled by this decoder, since they are permitted
23216dce513Schristos  *   in the deflate format.  See the format notes for fixed() and dynamic().
23316dce513Schristos  */
23416dce513Schristos #ifdef SLOW
decode(struct state * s,const struct huffman * h)23516dce513Schristos local int decode(struct state *s, const struct huffman *h)
23616dce513Schristos {
23716dce513Schristos     int len;            /* current number of bits in code */
23816dce513Schristos     int code;           /* len bits being decoded */
23916dce513Schristos     int first;          /* first code of length len */
24016dce513Schristos     int count;          /* number of codes of length len */
24116dce513Schristos     int index;          /* index of first code of length len in symbol table */
24216dce513Schristos 
24316dce513Schristos     code = first = index = 0;
24416dce513Schristos     for (len = 1; len <= MAXBITS; len++) {
24516dce513Schristos         code |= bits(s, 1);             /* get next bit */
24616dce513Schristos         count = h->count[len];
24716dce513Schristos         if (code - count < first)       /* if length len, return symbol */
24816dce513Schristos             return h->symbol[index + (code - first)];
24916dce513Schristos         index += count;                 /* else update for next length */
25016dce513Schristos         first += count;
25116dce513Schristos         first <<= 1;
25216dce513Schristos         code <<= 1;
25316dce513Schristos     }
25416dce513Schristos     return -10;                         /* ran out of codes */
25516dce513Schristos }
25616dce513Schristos 
25716dce513Schristos /*
25816dce513Schristos  * A faster version of decode() for real applications of this code.   It's not
25916dce513Schristos  * as readable, but it makes puff() twice as fast.  And it only makes the code
26016dce513Schristos  * a few percent larger.
26116dce513Schristos  */
26216dce513Schristos #else /* !SLOW */
decode(struct state * s,const struct huffman * h)26316dce513Schristos local int decode(struct state *s, const struct huffman *h)
26416dce513Schristos {
26516dce513Schristos     int len;            /* current number of bits in code */
26616dce513Schristos     int code;           /* len bits being decoded */
26716dce513Schristos     int first;          /* first code of length len */
26816dce513Schristos     int count;          /* number of codes of length len */
26916dce513Schristos     int index;          /* index of first code of length len in symbol table */
27016dce513Schristos     int bitbuf;         /* bits from stream */
27116dce513Schristos     int left;           /* bits left in next or left to process */
27216dce513Schristos     short *next;        /* next number of codes */
27316dce513Schristos 
27416dce513Schristos     bitbuf = s->bitbuf;
27516dce513Schristos     left = s->bitcnt;
27616dce513Schristos     code = first = index = 0;
27716dce513Schristos     len = 1;
27816dce513Schristos     next = h->count + 1;
27916dce513Schristos     while (1) {
28016dce513Schristos         while (left--) {
28116dce513Schristos             code |= bitbuf & 1;
28216dce513Schristos             bitbuf >>= 1;
28316dce513Schristos             count = *next++;
28416dce513Schristos             if (code - count < first) { /* if length len, return symbol */
28516dce513Schristos                 s->bitbuf = bitbuf;
28616dce513Schristos                 s->bitcnt = (s->bitcnt - len) & 7;
28716dce513Schristos                 return h->symbol[index + (code - first)];
28816dce513Schristos             }
28916dce513Schristos             index += count;             /* else update for next length */
29016dce513Schristos             first += count;
29116dce513Schristos             first <<= 1;
29216dce513Schristos             code <<= 1;
29316dce513Schristos             len++;
29416dce513Schristos         }
29516dce513Schristos         left = (MAXBITS+1) - len;
29616dce513Schristos         if (left == 0)
29716dce513Schristos             break;
29816dce513Schristos         if (s->incnt == s->inlen)
29916dce513Schristos             longjmp(s->env, 1);         /* out of input */
30016dce513Schristos         bitbuf = s->in[s->incnt++];
30116dce513Schristos         if (left > 8)
30216dce513Schristos             left = 8;
30316dce513Schristos     }
30416dce513Schristos     return -10;                         /* ran out of codes */
30516dce513Schristos }
30616dce513Schristos #endif /* SLOW */
30716dce513Schristos 
30816dce513Schristos /*
30916dce513Schristos  * Given the list of code lengths length[0..n-1] representing a canonical
31016dce513Schristos  * Huffman code for n symbols, construct the tables required to decode those
31116dce513Schristos  * codes.  Those tables are the number of codes of each length, and the symbols
31216dce513Schristos  * sorted by length, retaining their original order within each length.  The
31316dce513Schristos  * return value is zero for a complete code set, negative for an over-
31416dce513Schristos  * subscribed code set, and positive for an incomplete code set.  The tables
31516dce513Schristos  * can be used if the return value is zero or positive, but they cannot be used
31616dce513Schristos  * if the return value is negative.  If the return value is zero, it is not
31716dce513Schristos  * possible for decode() using that table to return an error--any stream of
31816dce513Schristos  * enough bits will resolve to a symbol.  If the return value is positive, then
31916dce513Schristos  * it is possible for decode() using that table to return an error for received
32016dce513Schristos  * codes past the end of the incomplete lengths.
32116dce513Schristos  *
32216dce513Schristos  * Not used by decode(), but used for error checking, h->count[0] is the number
32316dce513Schristos  * of the n symbols not in the code.  So n - h->count[0] is the number of
32416dce513Schristos  * codes.  This is useful for checking for incomplete codes that have more than
32516dce513Schristos  * one symbol, which is an error in a dynamic block.
32616dce513Schristos  *
32716dce513Schristos  * Assumption: for all i in 0..n-1, 0 <= length[i] <= MAXBITS
32816dce513Schristos  * This is assured by the construction of the length arrays in dynamic() and
32916dce513Schristos  * fixed() and is not verified by construct().
33016dce513Schristos  *
33116dce513Schristos  * Format notes:
33216dce513Schristos  *
33316dce513Schristos  * - Permitted and expected examples of incomplete codes are one of the fixed
33416dce513Schristos  *   codes and any code with a single symbol which in deflate is coded as one
33516dce513Schristos  *   bit instead of zero bits.  See the format notes for fixed() and dynamic().
33616dce513Schristos  *
33716dce513Schristos  * - Within a given code length, the symbols are kept in ascending order for
33816dce513Schristos  *   the code bits definition.
33916dce513Schristos  */
construct(struct huffman * h,const short * length,int n)34016dce513Schristos local int construct(struct huffman *h, const short *length, int n)
34116dce513Schristos {
34216dce513Schristos     int symbol;         /* current symbol when stepping through length[] */
34316dce513Schristos     int len;            /* current length when stepping through h->count[] */
34416dce513Schristos     int left;           /* number of possible codes left of current length */
34516dce513Schristos     short offs[MAXBITS+1];      /* offsets in symbol table for each length */
34616dce513Schristos 
34716dce513Schristos     /* count number of codes of each length */
34816dce513Schristos     for (len = 0; len <= MAXBITS; len++)
34916dce513Schristos         h->count[len] = 0;
35016dce513Schristos     for (symbol = 0; symbol < n; symbol++)
35116dce513Schristos         (h->count[length[symbol]])++;   /* assumes lengths are within bounds */
35216dce513Schristos     if (h->count[0] == n)               /* no codes! */
35316dce513Schristos         return 0;                       /* complete, but decode() will fail */
35416dce513Schristos 
35516dce513Schristos     /* check for an over-subscribed or incomplete set of lengths */
35616dce513Schristos     left = 1;                           /* one possible code of zero length */
35716dce513Schristos     for (len = 1; len <= MAXBITS; len++) {
35816dce513Schristos         left <<= 1;                     /* one more bit, double codes left */
35916dce513Schristos         left -= h->count[len];          /* deduct count from possible codes */
36016dce513Schristos         if (left < 0)
36116dce513Schristos             return left;                /* over-subscribed--return negative */
36216dce513Schristos     }                                   /* left > 0 means incomplete */
36316dce513Schristos 
36416dce513Schristos     /* generate offsets into symbol table for each length for sorting */
36516dce513Schristos     offs[1] = 0;
36616dce513Schristos     for (len = 1; len < MAXBITS; len++)
36716dce513Schristos         offs[len + 1] = offs[len] + h->count[len];
36816dce513Schristos 
36916dce513Schristos     /*
37016dce513Schristos      * put symbols in table sorted by length, by symbol order within each
37116dce513Schristos      * length
37216dce513Schristos      */
37316dce513Schristos     for (symbol = 0; symbol < n; symbol++)
37416dce513Schristos         if (length[symbol] != 0)
37516dce513Schristos             h->symbol[offs[length[symbol]]++] = symbol;
37616dce513Schristos 
37716dce513Schristos     /* return zero for complete set, positive for incomplete set */
37816dce513Schristos     return left;
37916dce513Schristos }
38016dce513Schristos 
38116dce513Schristos /*
38216dce513Schristos  * Decode literal/length and distance codes until an end-of-block code.
38316dce513Schristos  *
38416dce513Schristos  * Format notes:
38516dce513Schristos  *
38616dce513Schristos  * - Compressed data that is after the block type if fixed or after the code
38716dce513Schristos  *   description if dynamic is a combination of literals and length/distance
38816dce513Schristos  *   pairs terminated by and end-of-block code.  Literals are simply Huffman
38916dce513Schristos  *   coded bytes.  A length/distance pair is a coded length followed by a
39016dce513Schristos  *   coded distance to represent a string that occurs earlier in the
39116dce513Schristos  *   uncompressed data that occurs again at the current location.
39216dce513Schristos  *
39316dce513Schristos  * - Literals, lengths, and the end-of-block code are combined into a single
39416dce513Schristos  *   code of up to 286 symbols.  They are 256 literals (0..255), 29 length
39516dce513Schristos  *   symbols (257..285), and the end-of-block symbol (256).
39616dce513Schristos  *
39716dce513Schristos  * - There are 256 possible lengths (3..258), and so 29 symbols are not enough
39816dce513Schristos  *   to represent all of those.  Lengths 3..10 and 258 are in fact represented
39916dce513Schristos  *   by just a length symbol.  Lengths 11..257 are represented as a symbol and
40016dce513Schristos  *   some number of extra bits that are added as an integer to the base length
40116dce513Schristos  *   of the length symbol.  The number of extra bits is determined by the base
40216dce513Schristos  *   length symbol.  These are in the static arrays below, lens[] for the base
40316dce513Schristos  *   lengths and lext[] for the corresponding number of extra bits.
40416dce513Schristos  *
40516dce513Schristos  * - The reason that 258 gets its own symbol is that the longest length is used
40616dce513Schristos  *   often in highly redundant files.  Note that 258 can also be coded as the
40716dce513Schristos  *   base value 227 plus the maximum extra value of 31.  While a good deflate
40816dce513Schristos  *   should never do this, it is not an error, and should be decoded properly.
40916dce513Schristos  *
41016dce513Schristos  * - If a length is decoded, including its extra bits if any, then it is
41116dce513Schristos  *   followed a distance code.  There are up to 30 distance symbols.  Again
41216dce513Schristos  *   there are many more possible distances (1..32768), so extra bits are added
41316dce513Schristos  *   to a base value represented by the symbol.  The distances 1..4 get their
41416dce513Schristos  *   own symbol, but the rest require extra bits.  The base distances and
41516dce513Schristos  *   corresponding number of extra bits are below in the static arrays dist[]
41616dce513Schristos  *   and dext[].
41716dce513Schristos  *
41816dce513Schristos  * - Literal bytes are simply written to the output.  A length/distance pair is
41916dce513Schristos  *   an instruction to copy previously uncompressed bytes to the output.  The
42016dce513Schristos  *   copy is from distance bytes back in the output stream, copying for length
42116dce513Schristos  *   bytes.
42216dce513Schristos  *
42316dce513Schristos  * - Distances pointing before the beginning of the output data are not
42416dce513Schristos  *   permitted.
42516dce513Schristos  *
42616dce513Schristos  * - Overlapped copies, where the length is greater than the distance, are
42716dce513Schristos  *   allowed and common.  For example, a distance of one and a length of 258
42816dce513Schristos  *   simply copies the last byte 258 times.  A distance of four and a length of
42916dce513Schristos  *   twelve copies the last four bytes three times.  A simple forward copy
43016dce513Schristos  *   ignoring whether the length is greater than the distance or not implements
43116dce513Schristos  *   this correctly.  You should not use memcpy() since its behavior is not
43216dce513Schristos  *   defined for overlapped arrays.  You should not use memmove() or bcopy()
43316dce513Schristos  *   since though their behavior -is- defined for overlapping arrays, it is
43416dce513Schristos  *   defined to do the wrong thing in this case.
43516dce513Schristos  */
codes(struct state * s,const struct huffman * lencode,const struct huffman * distcode)43616dce513Schristos local int codes(struct state *s,
43716dce513Schristos                 const struct huffman *lencode,
43816dce513Schristos                 const struct huffman *distcode)
43916dce513Schristos {
44016dce513Schristos     int symbol;         /* decoded symbol */
44116dce513Schristos     int len;            /* length for copy */
44216dce513Schristos     unsigned dist;      /* distance for copy */
44316dce513Schristos     static const short lens[29] = { /* Size base for length codes 257..285 */
44416dce513Schristos         3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
44516dce513Schristos         35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258};
44616dce513Schristos     static const short lext[29] = { /* Extra bits for length codes 257..285 */
44716dce513Schristos         0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
44816dce513Schristos         3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0};
44916dce513Schristos     static const short dists[30] = { /* Offset base for distance codes 0..29 */
45016dce513Schristos         1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
45116dce513Schristos         257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
45216dce513Schristos         8193, 12289, 16385, 24577};
45316dce513Schristos     static const short dext[30] = { /* Extra bits for distance codes 0..29 */
45416dce513Schristos         0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
45516dce513Schristos         7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
45616dce513Schristos         12, 12, 13, 13};
45716dce513Schristos 
45816dce513Schristos     /* decode literals and length/distance pairs */
45916dce513Schristos     do {
46016dce513Schristos         symbol = decode(s, lencode);
46116dce513Schristos         if (symbol < 0)
46216dce513Schristos             return symbol;              /* invalid symbol */
46316dce513Schristos         if (symbol < 256) {             /* literal: symbol is the byte */
46416dce513Schristos             /* write out the literal */
46516dce513Schristos             if (s->out != NIL) {
46616dce513Schristos                 if (s->outcnt == s->outlen)
46716dce513Schristos                     return 1;
46816dce513Schristos                 s->out[s->outcnt] = symbol;
46916dce513Schristos             }
47016dce513Schristos             s->outcnt++;
47116dce513Schristos         }
47216dce513Schristos         else if (symbol > 256) {        /* length */
47316dce513Schristos             /* get and compute length */
47416dce513Schristos             symbol -= 257;
47516dce513Schristos             if (symbol >= 29)
47616dce513Schristos                 return -10;             /* invalid fixed code */
47716dce513Schristos             len = lens[symbol] + bits(s, lext[symbol]);
47816dce513Schristos 
47916dce513Schristos             /* get and check distance */
48016dce513Schristos             symbol = decode(s, distcode);
48116dce513Schristos             if (symbol < 0)
48216dce513Schristos                 return symbol;          /* invalid symbol */
48316dce513Schristos             dist = dists[symbol] + bits(s, dext[symbol]);
48416dce513Schristos #ifndef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
48516dce513Schristos             if (dist > s->outcnt)
48616dce513Schristos                 return -11;     /* distance too far back */
48716dce513Schristos #endif
48816dce513Schristos 
48916dce513Schristos             /* copy length bytes from distance bytes back */
49016dce513Schristos             if (s->out != NIL) {
49116dce513Schristos                 if (s->outcnt + len > s->outlen)
49216dce513Schristos                     return 1;
49316dce513Schristos                 while (len--) {
49416dce513Schristos                     s->out[s->outcnt] =
49516dce513Schristos #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
49616dce513Schristos                         dist > s->outcnt ?
49716dce513Schristos                             0 :
49816dce513Schristos #endif
49916dce513Schristos                             s->out[s->outcnt - dist];
50016dce513Schristos                     s->outcnt++;
50116dce513Schristos                 }
50216dce513Schristos             }
50316dce513Schristos             else
50416dce513Schristos                 s->outcnt += len;
50516dce513Schristos         }
50616dce513Schristos     } while (symbol != 256);            /* end of block symbol */
50716dce513Schristos 
50816dce513Schristos     /* done with a valid fixed or dynamic block */
50916dce513Schristos     return 0;
51016dce513Schristos }
51116dce513Schristos 
51216dce513Schristos /*
51316dce513Schristos  * Process a fixed codes block.
51416dce513Schristos  *
51516dce513Schristos  * Format notes:
51616dce513Schristos  *
51716dce513Schristos  * - This block type can be useful for compressing small amounts of data for
51816dce513Schristos  *   which the size of the code descriptions in a dynamic block exceeds the
51916dce513Schristos  *   benefit of custom codes for that block.  For fixed codes, no bits are
52016dce513Schristos  *   spent on code descriptions.  Instead the code lengths for literal/length
52116dce513Schristos  *   codes and distance codes are fixed.  The specific lengths for each symbol
52216dce513Schristos  *   can be seen in the "for" loops below.
52316dce513Schristos  *
52416dce513Schristos  * - The literal/length code is complete, but has two symbols that are invalid
52516dce513Schristos  *   and should result in an error if received.  This cannot be implemented
52616dce513Schristos  *   simply as an incomplete code since those two symbols are in the "middle"
52716dce513Schristos  *   of the code.  They are eight bits long and the longest literal/length\
52816dce513Schristos  *   code is nine bits.  Therefore the code must be constructed with those
52916dce513Schristos  *   symbols, and the invalid symbols must be detected after decoding.
53016dce513Schristos  *
53116dce513Schristos  * - The fixed distance codes also have two invalid symbols that should result
53216dce513Schristos  *   in an error if received.  Since all of the distance codes are the same
53316dce513Schristos  *   length, this can be implemented as an incomplete code.  Then the invalid
53416dce513Schristos  *   codes are detected while decoding.
53516dce513Schristos  */
fixed(struct state * s)53616dce513Schristos local int fixed(struct state *s)
53716dce513Schristos {
53816dce513Schristos     static int virgin = 1;
53916dce513Schristos     static short lencnt[MAXBITS+1], lensym[FIXLCODES];
54016dce513Schristos     static short distcnt[MAXBITS+1], distsym[MAXDCODES];
54116dce513Schristos     static struct huffman lencode, distcode;
54216dce513Schristos 
54316dce513Schristos     /* build fixed huffman tables if first call (may not be thread safe) */
54416dce513Schristos     if (virgin) {
54516dce513Schristos         int symbol;
54616dce513Schristos         short lengths[FIXLCODES];
54716dce513Schristos 
54816dce513Schristos         /* construct lencode and distcode */
54916dce513Schristos         lencode.count = lencnt;
55016dce513Schristos         lencode.symbol = lensym;
55116dce513Schristos         distcode.count = distcnt;
55216dce513Schristos         distcode.symbol = distsym;
55316dce513Schristos 
55416dce513Schristos         /* literal/length table */
55516dce513Schristos         for (symbol = 0; symbol < 144; symbol++)
55616dce513Schristos             lengths[symbol] = 8;
55716dce513Schristos         for (; symbol < 256; symbol++)
55816dce513Schristos             lengths[symbol] = 9;
55916dce513Schristos         for (; symbol < 280; symbol++)
56016dce513Schristos             lengths[symbol] = 7;
56116dce513Schristos         for (; symbol < FIXLCODES; symbol++)
56216dce513Schristos             lengths[symbol] = 8;
56316dce513Schristos         construct(&lencode, lengths, FIXLCODES);
56416dce513Schristos 
56516dce513Schristos         /* distance table */
56616dce513Schristos         for (symbol = 0; symbol < MAXDCODES; symbol++)
56716dce513Schristos             lengths[symbol] = 5;
56816dce513Schristos         construct(&distcode, lengths, MAXDCODES);
56916dce513Schristos 
57016dce513Schristos         /* do this just once */
57116dce513Schristos         virgin = 0;
57216dce513Schristos     }
57316dce513Schristos 
57416dce513Schristos     /* decode data until end-of-block code */
57516dce513Schristos     return codes(s, &lencode, &distcode);
57616dce513Schristos }
57716dce513Schristos 
57816dce513Schristos /*
57916dce513Schristos  * Process a dynamic codes block.
58016dce513Schristos  *
58116dce513Schristos  * Format notes:
58216dce513Schristos  *
58316dce513Schristos  * - A dynamic block starts with a description of the literal/length and
58416dce513Schristos  *   distance codes for that block.  New dynamic blocks allow the compressor to
58516dce513Schristos  *   rapidly adapt to changing data with new codes optimized for that data.
58616dce513Schristos  *
58716dce513Schristos  * - The codes used by the deflate format are "canonical", which means that
58816dce513Schristos  *   the actual bits of the codes are generated in an unambiguous way simply
58916dce513Schristos  *   from the number of bits in each code.  Therefore the code descriptions
59016dce513Schristos  *   are simply a list of code lengths for each symbol.
59116dce513Schristos  *
59216dce513Schristos  * - The code lengths are stored in order for the symbols, so lengths are
59316dce513Schristos  *   provided for each of the literal/length symbols, and for each of the
59416dce513Schristos  *   distance symbols.
59516dce513Schristos  *
59616dce513Schristos  * - If a symbol is not used in the block, this is represented by a zero as
59716dce513Schristos  *   as the code length.  This does not mean a zero-length code, but rather
59816dce513Schristos  *   that no code should be created for this symbol.  There is no way in the
59916dce513Schristos  *   deflate format to represent a zero-length code.
60016dce513Schristos  *
60116dce513Schristos  * - The maximum number of bits in a code is 15, so the possible lengths for
60216dce513Schristos  *   any code are 1..15.
60316dce513Schristos  *
60416dce513Schristos  * - The fact that a length of zero is not permitted for a code has an
60516dce513Schristos  *   interesting consequence.  Normally if only one symbol is used for a given
60616dce513Schristos  *   code, then in fact that code could be represented with zero bits.  However
60716dce513Schristos  *   in deflate, that code has to be at least one bit.  So for example, if
60816dce513Schristos  *   only a single distance base symbol appears in a block, then it will be
60916dce513Schristos  *   represented by a single code of length one, in particular one 0 bit.  This
61016dce513Schristos  *   is an incomplete code, since if a 1 bit is received, it has no meaning,
61116dce513Schristos  *   and should result in an error.  So incomplete distance codes of one symbol
61216dce513Schristos  *   should be permitted, and the receipt of invalid codes should be handled.
61316dce513Schristos  *
61416dce513Schristos  * - It is also possible to have a single literal/length code, but that code
61516dce513Schristos  *   must be the end-of-block code, since every dynamic block has one.  This
61616dce513Schristos  *   is not the most efficient way to create an empty block (an empty fixed
61716dce513Schristos  *   block is fewer bits), but it is allowed by the format.  So incomplete
61816dce513Schristos  *   literal/length codes of one symbol should also be permitted.
61916dce513Schristos  *
62016dce513Schristos  * - If there are only literal codes and no lengths, then there are no distance
62116dce513Schristos  *   codes.  This is represented by one distance code with zero bits.
62216dce513Schristos  *
62316dce513Schristos  * - The list of up to 286 length/literal lengths and up to 30 distance lengths
62416dce513Schristos  *   are themselves compressed using Huffman codes and run-length encoding.  In
62516dce513Schristos  *   the list of code lengths, a 0 symbol means no code, a 1..15 symbol means
62616dce513Schristos  *   that length, and the symbols 16, 17, and 18 are run-length instructions.
62716dce513Schristos  *   Each of 16, 17, and 18 are follwed by extra bits to define the length of
62816dce513Schristos  *   the run.  16 copies the last length 3 to 6 times.  17 represents 3 to 10
62916dce513Schristos  *   zero lengths, and 18 represents 11 to 138 zero lengths.  Unused symbols
63016dce513Schristos  *   are common, hence the special coding for zero lengths.
63116dce513Schristos  *
63216dce513Schristos  * - The symbols for 0..18 are Huffman coded, and so that code must be
63316dce513Schristos  *   described first.  This is simply a sequence of up to 19 three-bit values
63416dce513Schristos  *   representing no code (0) or the code length for that symbol (1..7).
63516dce513Schristos  *
63616dce513Schristos  * - A dynamic block starts with three fixed-size counts from which is computed
63716dce513Schristos  *   the number of literal/length code lengths, the number of distance code
63816dce513Schristos  *   lengths, and the number of code length code lengths (ok, you come up with
63916dce513Schristos  *   a better name!) in the code descriptions.  For the literal/length and
64016dce513Schristos  *   distance codes, lengths after those provided are considered zero, i.e. no
64116dce513Schristos  *   code.  The code length code lengths are received in a permuted order (see
64216dce513Schristos  *   the order[] array below) to make a short code length code length list more
64316dce513Schristos  *   likely.  As it turns out, very short and very long codes are less likely
64416dce513Schristos  *   to be seen in a dynamic code description, hence what may appear initially
64516dce513Schristos  *   to be a peculiar ordering.
64616dce513Schristos  *
64716dce513Schristos  * - Given the number of literal/length code lengths (nlen) and distance code
64816dce513Schristos  *   lengths (ndist), then they are treated as one long list of nlen + ndist
64916dce513Schristos  *   code lengths.  Therefore run-length coding can and often does cross the
65016dce513Schristos  *   boundary between the two sets of lengths.
65116dce513Schristos  *
65216dce513Schristos  * - So to summarize, the code description at the start of a dynamic block is
65316dce513Schristos  *   three counts for the number of code lengths for the literal/length codes,
65416dce513Schristos  *   the distance codes, and the code length codes.  This is followed by the
65516dce513Schristos  *   code length code lengths, three bits each.  This is used to construct the
65616dce513Schristos  *   code length code which is used to read the remainder of the lengths.  Then
65716dce513Schristos  *   the literal/length code lengths and distance lengths are read as a single
65816dce513Schristos  *   set of lengths using the code length codes.  Codes are constructed from
65916dce513Schristos  *   the resulting two sets of lengths, and then finally you can start
66016dce513Schristos  *   decoding actual compressed data in the block.
66116dce513Schristos  *
66216dce513Schristos  * - For reference, a "typical" size for the code description in a dynamic
66316dce513Schristos  *   block is around 80 bytes.
66416dce513Schristos  */
dynamic(struct state * s)66516dce513Schristos local int dynamic(struct state *s)
66616dce513Schristos {
66716dce513Schristos     int nlen, ndist, ncode;             /* number of lengths in descriptor */
66816dce513Schristos     int index;                          /* index of lengths[] */
66916dce513Schristos     int err;                            /* construct() return value */
67016dce513Schristos     short lengths[MAXCODES];            /* descriptor code lengths */
67116dce513Schristos     short lencnt[MAXBITS+1], lensym[MAXLCODES];         /* lencode memory */
67216dce513Schristos     short distcnt[MAXBITS+1], distsym[MAXDCODES];       /* distcode memory */
67316dce513Schristos     struct huffman lencode, distcode;   /* length and distance codes */
67416dce513Schristos     static const short order[19] =      /* permutation of code length codes */
67516dce513Schristos         {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
67616dce513Schristos 
67716dce513Schristos     /* construct lencode and distcode */
67816dce513Schristos     lencode.count = lencnt;
67916dce513Schristos     lencode.symbol = lensym;
68016dce513Schristos     distcode.count = distcnt;
68116dce513Schristos     distcode.symbol = distsym;
68216dce513Schristos 
68316dce513Schristos     /* get number of lengths in each table, check lengths */
68416dce513Schristos     nlen = bits(s, 5) + 257;
68516dce513Schristos     ndist = bits(s, 5) + 1;
68616dce513Schristos     ncode = bits(s, 4) + 4;
68716dce513Schristos     if (nlen > MAXLCODES || ndist > MAXDCODES)
68816dce513Schristos         return -3;                      /* bad counts */
68916dce513Schristos 
69016dce513Schristos     /* read code length code lengths (really), missing lengths are zero */
69116dce513Schristos     for (index = 0; index < ncode; index++)
69216dce513Schristos         lengths[order[index]] = bits(s, 3);
69316dce513Schristos     for (; index < 19; index++)
69416dce513Schristos         lengths[order[index]] = 0;
69516dce513Schristos 
69616dce513Schristos     /* build huffman table for code lengths codes (use lencode temporarily) */
69716dce513Schristos     err = construct(&lencode, lengths, 19);
69816dce513Schristos     if (err != 0)               /* require complete code set here */
69916dce513Schristos         return -4;
70016dce513Schristos 
70116dce513Schristos     /* read length/literal and distance code length tables */
70216dce513Schristos     index = 0;
70316dce513Schristos     while (index < nlen + ndist) {
70416dce513Schristos         int symbol;             /* decoded value */
70516dce513Schristos         int len;                /* last length to repeat */
70616dce513Schristos 
70716dce513Schristos         symbol = decode(s, &lencode);
70816dce513Schristos         if (symbol < 0)
70916dce513Schristos             return symbol;          /* invalid symbol */
71016dce513Schristos         if (symbol < 16)                /* length in 0..15 */
71116dce513Schristos             lengths[index++] = symbol;
71216dce513Schristos         else {                          /* repeat instruction */
71316dce513Schristos             len = 0;                    /* assume repeating zeros */
71416dce513Schristos             if (symbol == 16) {         /* repeat last length 3..6 times */
71516dce513Schristos                 if (index == 0)
71616dce513Schristos                     return -5;          /* no last length! */
71716dce513Schristos                 len = lengths[index - 1];       /* last length */
71816dce513Schristos                 symbol = 3 + bits(s, 2);
71916dce513Schristos             }
72016dce513Schristos             else if (symbol == 17)      /* repeat zero 3..10 times */
72116dce513Schristos                 symbol = 3 + bits(s, 3);
72216dce513Schristos             else                        /* == 18, repeat zero 11..138 times */
72316dce513Schristos                 symbol = 11 + bits(s, 7);
72416dce513Schristos             if (index + symbol > nlen + ndist)
72516dce513Schristos                 return -6;              /* too many lengths! */
72616dce513Schristos             while (symbol--)            /* repeat last or zero symbol times */
72716dce513Schristos                 lengths[index++] = len;
72816dce513Schristos         }
72916dce513Schristos     }
73016dce513Schristos 
73116dce513Schristos     /* check for end-of-block code -- there better be one! */
73216dce513Schristos     if (lengths[256] == 0)
73316dce513Schristos         return -9;
73416dce513Schristos 
73516dce513Schristos     /* build huffman table for literal/length codes */
73616dce513Schristos     err = construct(&lencode, lengths, nlen);
73716dce513Schristos     if (err && (err < 0 || nlen != lencode.count[0] + lencode.count[1]))
73816dce513Schristos         return -7;      /* incomplete code ok only for single length 1 code */
73916dce513Schristos 
74016dce513Schristos     /* build huffman table for distance codes */
74116dce513Schristos     err = construct(&distcode, lengths + nlen, ndist);
74216dce513Schristos     if (err && (err < 0 || ndist != distcode.count[0] + distcode.count[1]))
74316dce513Schristos         return -8;      /* incomplete code ok only for single length 1 code */
74416dce513Schristos 
74516dce513Schristos     /* decode data until end-of-block code */
74616dce513Schristos     return codes(s, &lencode, &distcode);
74716dce513Schristos }
74816dce513Schristos 
74916dce513Schristos /*
75016dce513Schristos  * Inflate source to dest.  On return, destlen and sourcelen are updated to the
75116dce513Schristos  * size of the uncompressed data and the size of the deflate data respectively.
75216dce513Schristos  * On success, the return value of puff() is zero.  If there is an error in the
75316dce513Schristos  * source data, i.e. it is not in the deflate format, then a negative value is
75416dce513Schristos  * returned.  If there is not enough input available or there is not enough
75516dce513Schristos  * output space, then a positive error is returned.  In that case, destlen and
75616dce513Schristos  * sourcelen are not updated to facilitate retrying from the beginning with the
75716dce513Schristos  * provision of more input data or more output space.  In the case of invalid
75816dce513Schristos  * inflate data (a negative error), the dest and source pointers are updated to
75916dce513Schristos  * facilitate the debugging of deflators.
76016dce513Schristos  *
76116dce513Schristos  * puff() also has a mode to determine the size of the uncompressed output with
76216dce513Schristos  * no output written.  For this dest must be (unsigned char *)0.  In this case,
76316dce513Schristos  * the input value of *destlen is ignored, and on return *destlen is set to the
76416dce513Schristos  * size of the uncompressed output.
76516dce513Schristos  *
76616dce513Schristos  * The return codes are:
76716dce513Schristos  *
76816dce513Schristos  *   2:  available inflate data did not terminate
76916dce513Schristos  *   1:  output space exhausted before completing inflate
77016dce513Schristos  *   0:  successful inflate
77116dce513Schristos  *  -1:  invalid block type (type == 3)
77216dce513Schristos  *  -2:  stored block length did not match one's complement
77316dce513Schristos  *  -3:  dynamic block code description: too many length or distance codes
77416dce513Schristos  *  -4:  dynamic block code description: code lengths codes incomplete
77516dce513Schristos  *  -5:  dynamic block code description: repeat lengths with no first length
77616dce513Schristos  *  -6:  dynamic block code description: repeat more than specified lengths
77716dce513Schristos  *  -7:  dynamic block code description: invalid literal/length code lengths
77816dce513Schristos  *  -8:  dynamic block code description: invalid distance code lengths
77916dce513Schristos  *  -9:  dynamic block code description: missing end-of-block code
78016dce513Schristos  * -10:  invalid literal/length or distance code in fixed or dynamic block
78116dce513Schristos  * -11:  distance is too far back in fixed or dynamic block
78216dce513Schristos  *
78316dce513Schristos  * Format notes:
78416dce513Schristos  *
78516dce513Schristos  * - Three bits are read for each block to determine the kind of block and
78616dce513Schristos  *   whether or not it is the last block.  Then the block is decoded and the
78716dce513Schristos  *   process repeated if it was not the last block.
78816dce513Schristos  *
78916dce513Schristos  * - The leftover bits in the last byte of the deflate data after the last
79016dce513Schristos  *   block (if it was a fixed or dynamic block) are undefined and have no
79116dce513Schristos  *   expected values to check.
79216dce513Schristos  */
puff(unsigned char * dest,unsigned long * destlen,const unsigned char * source,unsigned long * sourcelen)79316dce513Schristos int puff(unsigned char *dest,           /* pointer to destination pointer */
79416dce513Schristos          unsigned long *destlen,        /* amount of output space */
79516dce513Schristos          const unsigned char *source,   /* pointer to source data pointer */
79616dce513Schristos          unsigned long *sourcelen)      /* amount of input available */
79716dce513Schristos {
79816dce513Schristos     struct state s;             /* input/output state */
79916dce513Schristos     int last, type;             /* block information */
80016dce513Schristos     int err;                    /* return value */
80116dce513Schristos 
80216dce513Schristos     /* initialize output state */
80316dce513Schristos     s.out = dest;
80416dce513Schristos     s.outlen = *destlen;                /* ignored if dest is NIL */
80516dce513Schristos     s.outcnt = 0;
80616dce513Schristos 
80716dce513Schristos     /* initialize input state */
80816dce513Schristos     s.in = source;
80916dce513Schristos     s.inlen = *sourcelen;
81016dce513Schristos     s.incnt = 0;
81116dce513Schristos     s.bitbuf = 0;
81216dce513Schristos     s.bitcnt = 0;
81316dce513Schristos 
81416dce513Schristos     /* return if bits() or decode() tries to read past available input */
81516dce513Schristos     if (setjmp(s.env) != 0)             /* if came back here via longjmp() */
81616dce513Schristos         err = 2;                        /* then skip do-loop, return error */
81716dce513Schristos     else {
81816dce513Schristos         /* process blocks until last block or error */
81916dce513Schristos         do {
82016dce513Schristos             last = bits(&s, 1);         /* one if last block */
82116dce513Schristos             type = bits(&s, 2);         /* block type 0..3 */
82216dce513Schristos             err = type == 0 ?
82316dce513Schristos                     stored(&s) :
82416dce513Schristos                     (type == 1 ?
82516dce513Schristos                         fixed(&s) :
82616dce513Schristos                         (type == 2 ?
82716dce513Schristos                             dynamic(&s) :
82816dce513Schristos                             -1));       /* type == 3, invalid */
82916dce513Schristos             if (err != 0)
83016dce513Schristos                 break;                  /* return with error */
83116dce513Schristos         } while (!last);
83216dce513Schristos     }
83316dce513Schristos 
83416dce513Schristos     /* update the lengths and return */
83516dce513Schristos     if (err <= 0) {
83616dce513Schristos         *destlen = s.outcnt;
83716dce513Schristos         *sourcelen = s.incnt;
83816dce513Schristos     }
83916dce513Schristos     return err;
84016dce513Schristos }
841