1aaf4ece6Schristos /* 2aaf4ece6Schristos * puff.c 3c3423655Schristos * Copyright (C) 2002-2013 Mark Adler 4aaf4ece6Schristos * For conditions of distribution and use, see copyright notice in puff.h 5c3423655Schristos * version 2.3, 21 Jan 2013 6aaf4ece6Schristos * 7aaf4ece6Schristos * puff.c is a simple inflate written to be an unambiguous way to specify the 8aaf4ece6Schristos * deflate format. It is not written for speed but rather simplicity. As a 9aaf4ece6Schristos * side benefit, this code might actually be useful when small code is more 10aaf4ece6Schristos * important than speed, such as bootstrap applications. For typical deflate 11aaf4ece6Schristos * data, zlib's inflate() is about four times as fast as puff(). zlib's 12aaf4ece6Schristos * inflate compiles to around 20K on my machine, whereas puff.c compiles to 13aaf4ece6Schristos * around 4K on my machine (a PowerPC using GNU cc). If the faster decode() 14aaf4ece6Schristos * function here is used, then puff() is only twice as slow as zlib's 15aaf4ece6Schristos * inflate(). 16aaf4ece6Schristos * 17aaf4ece6Schristos * All dynamically allocated memory comes from the stack. The stack required 18aaf4ece6Schristos * is less than 2K bytes. This code is compatible with 16-bit int's and 19aaf4ece6Schristos * assumes that long's are at least 32 bits. puff.c uses the short data type, 20c3423655Schristos * assumed to be 16 bits, for arrays in order to conserve memory. The code 21aaf4ece6Schristos * works whether integers are stored big endian or little endian. 22aaf4ece6Schristos * 23aaf4ece6Schristos * In the comments below are "Format notes" that describe the inflate process 24aaf4ece6Schristos * and document some of the less obvious aspects of the format. This source 25aaf4ece6Schristos * code is meant to supplement RFC 1951, which formally describes the deflate 26aaf4ece6Schristos * format: 27aaf4ece6Schristos * 28aaf4ece6Schristos * http://www.zlib.org/rfc-deflate.html 29aaf4ece6Schristos */ 30aaf4ece6Schristos 31aaf4ece6Schristos /* 32aaf4ece6Schristos * Change history: 33aaf4ece6Schristos * 34aaf4ece6Schristos * 1.0 10 Feb 2002 - First version 35aaf4ece6Schristos * 1.1 17 Feb 2002 - Clarifications of some comments and notes 36aaf4ece6Schristos * - Update puff() dest and source pointers on negative 37aaf4ece6Schristos * errors to facilitate debugging deflators 38aaf4ece6Schristos * - Remove longest from struct huffman -- not needed 39aaf4ece6Schristos * - Simplify offs[] index in construct() 40aaf4ece6Schristos * - Add input size and checking, using longjmp() to 41aaf4ece6Schristos * maintain easy readability 42aaf4ece6Schristos * - Use short data type for large arrays 43aaf4ece6Schristos * - Use pointers instead of long to specify source and 44aaf4ece6Schristos * destination sizes to avoid arbitrary 4 GB limits 45aaf4ece6Schristos * 1.2 17 Mar 2002 - Add faster version of decode(), doubles speed (!), 46ec47cc4bSchristos * but leave simple version for readability 47aaf4ece6Schristos * - Make sure invalid distances detected if pointers 48aaf4ece6Schristos * are 16 bits 49aaf4ece6Schristos * - Fix fixed codes table error 50aaf4ece6Schristos * - Provide a scanning mode for determining size of 51aaf4ece6Schristos * uncompressed data 52c3423655Schristos * 1.3 20 Mar 2002 - Go back to lengths for puff() parameters [Gailly] 53aaf4ece6Schristos * - Add a puff.h file for the interface 54c3423655Schristos * - Add braces in puff() for else do [Gailly] 55aaf4ece6Schristos * - Use indexes instead of pointers for readability 56aaf4ece6Schristos * 1.4 31 Mar 2002 - Simplify construct() code set check 57aaf4ece6Schristos * - Fix some comments 58aaf4ece6Schristos * - Add FIXLCODES #define 59aaf4ece6Schristos * 1.5 6 Apr 2002 - Minor comment fixes 60aaf4ece6Schristos * 1.6 7 Aug 2002 - Minor format changes 61aaf4ece6Schristos * 1.7 3 Mar 2003 - Added test code for distribution 62aaf4ece6Schristos * - Added zlib-like license 63aaf4ece6Schristos * 1.8 9 Jan 2004 - Added some comments on no distance codes case 64c3423655Schristos * 1.9 21 Feb 2008 - Fix bug on 16-bit integer architectures [Pohland] 65c3423655Schristos * - Catch missing end-of-block symbol error 66c3423655Schristos * 2.0 25 Jul 2008 - Add #define to permit distance too far back 67c3423655Schristos * - Add option in TEST code for puff to write the data 68c3423655Schristos * - Add option in TEST code to skip input bytes 69c3423655Schristos * - Allow TEST code to read from piped stdin 70c3423655Schristos * 2.1 4 Apr 2010 - Avoid variable initialization for happier compilers 71c3423655Schristos * - Avoid unsigned comparisons for even happier compilers 72c3423655Schristos * 2.2 25 Apr 2010 - Fix bug in variable initializations [Oberhumer] 73c3423655Schristos * - Add const where appropriate [Oberhumer] 74c3423655Schristos * - Split if's and ?'s for coverage testing 75c3423655Schristos * - Break out test code to separate file 76c3423655Schristos * - Move NIL to puff.h 77c3423655Schristos * - Allow incomplete code only if single code length is 1 78c3423655Schristos * - Add full code coverage test to Makefile 79c3423655Schristos * 2.3 21 Jan 2013 - Check for invalid code length codes in dynamic blocks 80aaf4ece6Schristos */ 81aaf4ece6Schristos 82aaf4ece6Schristos #include <setjmp.h> /* for setjmp(), longjmp(), and jmp_buf */ 83aaf4ece6Schristos #include "puff.h" /* prototype for puff() */ 84aaf4ece6Schristos 85aaf4ece6Schristos #define local static /* for local function definitions */ 86aaf4ece6Schristos 87aaf4ece6Schristos /* 88aaf4ece6Schristos * Maximums for allocations and loops. It is not useful to change these -- 89aaf4ece6Schristos * they are fixed by the deflate format. 90aaf4ece6Schristos */ 91aaf4ece6Schristos #define MAXBITS 15 /* maximum bits in a code */ 92aaf4ece6Schristos #define MAXLCODES 286 /* maximum number of literal/length codes */ 93aaf4ece6Schristos #define MAXDCODES 30 /* maximum number of distance codes */ 94aaf4ece6Schristos #define MAXCODES (MAXLCODES+MAXDCODES) /* maximum codes lengths to read */ 95aaf4ece6Schristos #define FIXLCODES 288 /* number of fixed literal/length codes */ 96aaf4ece6Schristos 97aaf4ece6Schristos /* input and output state */ 98aaf4ece6Schristos struct state { 99aaf4ece6Schristos /* output state */ 100aaf4ece6Schristos unsigned char *out; /* output buffer */ 101aaf4ece6Schristos unsigned long outlen; /* available space at out */ 102aaf4ece6Schristos unsigned long outcnt; /* bytes written to out so far */ 103aaf4ece6Schristos 104aaf4ece6Schristos /* input state */ 105c3423655Schristos const unsigned char *in; /* input buffer */ 106aaf4ece6Schristos unsigned long inlen; /* available input at in */ 107aaf4ece6Schristos unsigned long incnt; /* bytes read so far */ 108aaf4ece6Schristos int bitbuf; /* bit buffer */ 109aaf4ece6Schristos int bitcnt; /* number of bits in bit buffer */ 110aaf4ece6Schristos 111aaf4ece6Schristos /* input limit error return state for bits() and decode() */ 112aaf4ece6Schristos jmp_buf env; 113aaf4ece6Schristos }; 114aaf4ece6Schristos 115aaf4ece6Schristos /* 116aaf4ece6Schristos * Return need bits from the input stream. This always leaves less than 117aaf4ece6Schristos * eight bits in the buffer. bits() works properly for need == 0. 118aaf4ece6Schristos * 119aaf4ece6Schristos * Format notes: 120aaf4ece6Schristos * 121aaf4ece6Schristos * - Bits are stored in bytes from the least significant bit to the most 122aaf4ece6Schristos * significant bit. Therefore bits are dropped from the bottom of the bit 123aaf4ece6Schristos * buffer, using shift right, and new bytes are appended to the top of the 124aaf4ece6Schristos * bit buffer, using shift left. 125aaf4ece6Schristos */ 126aaf4ece6Schristos local int bits(struct state *s, int need) 127aaf4ece6Schristos { 128aaf4ece6Schristos long val; /* bit accumulator (can use up to 20 bits) */ 129aaf4ece6Schristos 130aaf4ece6Schristos /* load at least need bits into val */ 131aaf4ece6Schristos val = s->bitbuf; 132aaf4ece6Schristos while (s->bitcnt < need) { 133c3423655Schristos if (s->incnt == s->inlen) 134c3423655Schristos longjmp(s->env, 1); /* out of input */ 135aaf4ece6Schristos val |= (long)(s->in[s->incnt++]) << s->bitcnt; /* load eight bits */ 136aaf4ece6Schristos s->bitcnt += 8; 137aaf4ece6Schristos } 138aaf4ece6Schristos 139aaf4ece6Schristos /* drop need bits and update buffer, always zero to seven bits left */ 140aaf4ece6Schristos s->bitbuf = (int)(val >> need); 141aaf4ece6Schristos s->bitcnt -= need; 142aaf4ece6Schristos 143aaf4ece6Schristos /* return need bits, zeroing the bits above that */ 144aaf4ece6Schristos return (int)(val & ((1L << need) - 1)); 145aaf4ece6Schristos } 146aaf4ece6Schristos 147aaf4ece6Schristos /* 148aaf4ece6Schristos * Process a stored block. 149aaf4ece6Schristos * 150aaf4ece6Schristos * Format notes: 151aaf4ece6Schristos * 152aaf4ece6Schristos * - After the two-bit stored block type (00), the stored block length and 153aaf4ece6Schristos * stored bytes are byte-aligned for fast copying. Therefore any leftover 154aaf4ece6Schristos * bits in the byte that has the last bit of the type, as many as seven, are 155aaf4ece6Schristos * discarded. The value of the discarded bits are not defined and should not 156aaf4ece6Schristos * be checked against any expectation. 157aaf4ece6Schristos * 158aaf4ece6Schristos * - The second inverted copy of the stored block length does not have to be 159aaf4ece6Schristos * checked, but it's probably a good idea to do so anyway. 160aaf4ece6Schristos * 161aaf4ece6Schristos * - A stored block can have zero length. This is sometimes used to byte-align 162aaf4ece6Schristos * subsets of the compressed data for random access or partial recovery. 163aaf4ece6Schristos */ 164aaf4ece6Schristos local int stored(struct state *s) 165aaf4ece6Schristos { 166aaf4ece6Schristos unsigned len; /* length of stored block */ 167aaf4ece6Schristos 168aaf4ece6Schristos /* discard leftover bits from current byte (assumes s->bitcnt < 8) */ 169aaf4ece6Schristos s->bitbuf = 0; 170aaf4ece6Schristos s->bitcnt = 0; 171aaf4ece6Schristos 172aaf4ece6Schristos /* get length and check against its one's complement */ 173c3423655Schristos if (s->incnt + 4 > s->inlen) 174c3423655Schristos return 2; /* not enough input */ 175aaf4ece6Schristos len = s->in[s->incnt++]; 176aaf4ece6Schristos len |= s->in[s->incnt++] << 8; 177aaf4ece6Schristos if (s->in[s->incnt++] != (~len & 0xff) || 178aaf4ece6Schristos s->in[s->incnt++] != ((~len >> 8) & 0xff)) 179aaf4ece6Schristos return -2; /* didn't match complement! */ 180aaf4ece6Schristos 181aaf4ece6Schristos /* copy len bytes from in to out */ 182c3423655Schristos if (s->incnt + len > s->inlen) 183c3423655Schristos return 2; /* not enough input */ 184aaf4ece6Schristos if (s->out != NIL) { 185aaf4ece6Schristos if (s->outcnt + len > s->outlen) 186aaf4ece6Schristos return 1; /* not enough output space */ 187aaf4ece6Schristos while (len--) 188aaf4ece6Schristos s->out[s->outcnt++] = s->in[s->incnt++]; 189aaf4ece6Schristos } 190aaf4ece6Schristos else { /* just scanning */ 191aaf4ece6Schristos s->outcnt += len; 192aaf4ece6Schristos s->incnt += len; 193aaf4ece6Schristos } 194aaf4ece6Schristos 195aaf4ece6Schristos /* done with a valid stored block */ 196aaf4ece6Schristos return 0; 197aaf4ece6Schristos } 198aaf4ece6Schristos 199aaf4ece6Schristos /* 200aaf4ece6Schristos * Huffman code decoding tables. count[1..MAXBITS] is the number of symbols of 201aaf4ece6Schristos * each length, which for a canonical code are stepped through in order. 202aaf4ece6Schristos * symbol[] are the symbol values in canonical order, where the number of 203aaf4ece6Schristos * entries is the sum of the counts in count[]. The decoding process can be 204aaf4ece6Schristos * seen in the function decode() below. 205aaf4ece6Schristos */ 206aaf4ece6Schristos struct huffman { 207aaf4ece6Schristos short *count; /* number of symbols of each length */ 208aaf4ece6Schristos short *symbol; /* canonically ordered symbols */ 209aaf4ece6Schristos }; 210aaf4ece6Schristos 211aaf4ece6Schristos /* 212aaf4ece6Schristos * Decode a code from the stream s using huffman table h. Return the symbol or 213aaf4ece6Schristos * a negative value if there is an error. If all of the lengths are zero, i.e. 214aaf4ece6Schristos * an empty code, or if the code is incomplete and an invalid code is received, 215c3423655Schristos * then -10 is returned after reading MAXBITS bits. 216aaf4ece6Schristos * 217aaf4ece6Schristos * Format notes: 218aaf4ece6Schristos * 219aaf4ece6Schristos * - The codes as stored in the compressed data are bit-reversed relative to 220aaf4ece6Schristos * a simple integer ordering of codes of the same lengths. Hence below the 221aaf4ece6Schristos * bits are pulled from the compressed data one at a time and used to 222aaf4ece6Schristos * build the code value reversed from what is in the stream in order to 223aaf4ece6Schristos * permit simple integer comparisons for decoding. A table-based decoding 224aaf4ece6Schristos * scheme (as used in zlib) does not need to do this reversal. 225aaf4ece6Schristos * 226aaf4ece6Schristos * - The first code for the shortest length is all zeros. Subsequent codes of 227aaf4ece6Schristos * the same length are simply integer increments of the previous code. When 228aaf4ece6Schristos * moving up a length, a zero bit is appended to the code. For a complete 229aaf4ece6Schristos * code, the last code of the longest length will be all ones. 230aaf4ece6Schristos * 231aaf4ece6Schristos * - Incomplete codes are handled by this decoder, since they are permitted 232aaf4ece6Schristos * in the deflate format. See the format notes for fixed() and dynamic(). 233aaf4ece6Schristos */ 234aaf4ece6Schristos #ifdef SLOW 235c3423655Schristos local int decode(struct state *s, const struct huffman *h) 236aaf4ece6Schristos { 237aaf4ece6Schristos int len; /* current number of bits in code */ 238aaf4ece6Schristos int code; /* len bits being decoded */ 239aaf4ece6Schristos int first; /* first code of length len */ 240aaf4ece6Schristos int count; /* number of codes of length len */ 241aaf4ece6Schristos int index; /* index of first code of length len in symbol table */ 242aaf4ece6Schristos 243aaf4ece6Schristos code = first = index = 0; 244aaf4ece6Schristos for (len = 1; len <= MAXBITS; len++) { 245aaf4ece6Schristos code |= bits(s, 1); /* get next bit */ 246aaf4ece6Schristos count = h->count[len]; 247c3423655Schristos if (code - count < first) /* if length len, return symbol */ 248aaf4ece6Schristos return h->symbol[index + (code - first)]; 249aaf4ece6Schristos index += count; /* else update for next length */ 250aaf4ece6Schristos first += count; 251aaf4ece6Schristos first <<= 1; 252aaf4ece6Schristos code <<= 1; 253aaf4ece6Schristos } 254c3423655Schristos return -10; /* ran out of codes */ 255aaf4ece6Schristos } 256aaf4ece6Schristos 257aaf4ece6Schristos /* 258aaf4ece6Schristos * A faster version of decode() for real applications of this code. It's not 259aaf4ece6Schristos * as readable, but it makes puff() twice as fast. And it only makes the code 260aaf4ece6Schristos * a few percent larger. 261aaf4ece6Schristos */ 262aaf4ece6Schristos #else /* !SLOW */ 263c3423655Schristos local int decode(struct state *s, const struct huffman *h) 264aaf4ece6Schristos { 265aaf4ece6Schristos int len; /* current number of bits in code */ 266aaf4ece6Schristos int code; /* len bits being decoded */ 267aaf4ece6Schristos int first; /* first code of length len */ 268aaf4ece6Schristos int count; /* number of codes of length len */ 269aaf4ece6Schristos int index; /* index of first code of length len in symbol table */ 270aaf4ece6Schristos int bitbuf; /* bits from stream */ 271aaf4ece6Schristos int left; /* bits left in next or left to process */ 272aaf4ece6Schristos short *next; /* next number of codes */ 273aaf4ece6Schristos 274aaf4ece6Schristos bitbuf = s->bitbuf; 275aaf4ece6Schristos left = s->bitcnt; 276aaf4ece6Schristos code = first = index = 0; 277aaf4ece6Schristos len = 1; 278aaf4ece6Schristos next = h->count + 1; 279aaf4ece6Schristos while (1) { 280aaf4ece6Schristos while (left--) { 281aaf4ece6Schristos code |= bitbuf & 1; 282aaf4ece6Schristos bitbuf >>= 1; 283aaf4ece6Schristos count = *next++; 284c3423655Schristos if (code - count < first) { /* if length len, return symbol */ 285aaf4ece6Schristos s->bitbuf = bitbuf; 286aaf4ece6Schristos s->bitcnt = (s->bitcnt - len) & 7; 287aaf4ece6Schristos return h->symbol[index + (code - first)]; 288aaf4ece6Schristos } 289aaf4ece6Schristos index += count; /* else update for next length */ 290aaf4ece6Schristos first += count; 291aaf4ece6Schristos first <<= 1; 292aaf4ece6Schristos code <<= 1; 293aaf4ece6Schristos len++; 294aaf4ece6Schristos } 295aaf4ece6Schristos left = (MAXBITS+1) - len; 296c3423655Schristos if (left == 0) 297c3423655Schristos break; 298c3423655Schristos if (s->incnt == s->inlen) 299c3423655Schristos longjmp(s->env, 1); /* out of input */ 300aaf4ece6Schristos bitbuf = s->in[s->incnt++]; 301c3423655Schristos if (left > 8) 302c3423655Schristos left = 8; 303aaf4ece6Schristos } 304c3423655Schristos return -10; /* ran out of codes */ 305aaf4ece6Schristos } 306aaf4ece6Schristos #endif /* SLOW */ 307aaf4ece6Schristos 308aaf4ece6Schristos /* 309aaf4ece6Schristos * Given the list of code lengths length[0..n-1] representing a canonical 310aaf4ece6Schristos * Huffman code for n symbols, construct the tables required to decode those 311aaf4ece6Schristos * codes. Those tables are the number of codes of each length, and the symbols 312aaf4ece6Schristos * sorted by length, retaining their original order within each length. The 313aaf4ece6Schristos * return value is zero for a complete code set, negative for an over- 314aaf4ece6Schristos * subscribed code set, and positive for an incomplete code set. The tables 315aaf4ece6Schristos * can be used if the return value is zero or positive, but they cannot be used 316aaf4ece6Schristos * if the return value is negative. If the return value is zero, it is not 317aaf4ece6Schristos * possible for decode() using that table to return an error--any stream of 318aaf4ece6Schristos * enough bits will resolve to a symbol. If the return value is positive, then 319aaf4ece6Schristos * it is possible for decode() using that table to return an error for received 320aaf4ece6Schristos * codes past the end of the incomplete lengths. 321aaf4ece6Schristos * 322aaf4ece6Schristos * Not used by decode(), but used for error checking, h->count[0] is the number 323aaf4ece6Schristos * of the n symbols not in the code. So n - h->count[0] is the number of 324aaf4ece6Schristos * codes. This is useful for checking for incomplete codes that have more than 325aaf4ece6Schristos * one symbol, which is an error in a dynamic block. 326aaf4ece6Schristos * 327aaf4ece6Schristos * Assumption: for all i in 0..n-1, 0 <= length[i] <= MAXBITS 328aaf4ece6Schristos * This is assured by the construction of the length arrays in dynamic() and 329aaf4ece6Schristos * fixed() and is not verified by construct(). 330aaf4ece6Schristos * 331aaf4ece6Schristos * Format notes: 332aaf4ece6Schristos * 333aaf4ece6Schristos * - Permitted and expected examples of incomplete codes are one of the fixed 334aaf4ece6Schristos * codes and any code with a single symbol which in deflate is coded as one 335aaf4ece6Schristos * bit instead of zero bits. See the format notes for fixed() and dynamic(). 336aaf4ece6Schristos * 337aaf4ece6Schristos * - Within a given code length, the symbols are kept in ascending order for 338aaf4ece6Schristos * the code bits definition. 339aaf4ece6Schristos */ 340c3423655Schristos local int construct(struct huffman *h, const short *length, int n) 341aaf4ece6Schristos { 342aaf4ece6Schristos int symbol; /* current symbol when stepping through length[] */ 343aaf4ece6Schristos int len; /* current length when stepping through h->count[] */ 344aaf4ece6Schristos int left; /* number of possible codes left of current length */ 345aaf4ece6Schristos short offs[MAXBITS+1]; /* offsets in symbol table for each length */ 346aaf4ece6Schristos 347aaf4ece6Schristos /* count number of codes of each length */ 348aaf4ece6Schristos for (len = 0; len <= MAXBITS; len++) 349aaf4ece6Schristos h->count[len] = 0; 350aaf4ece6Schristos for (symbol = 0; symbol < n; symbol++) 351aaf4ece6Schristos (h->count[length[symbol]])++; /* assumes lengths are within bounds */ 352aaf4ece6Schristos if (h->count[0] == n) /* no codes! */ 353aaf4ece6Schristos return 0; /* complete, but decode() will fail */ 354aaf4ece6Schristos 355aaf4ece6Schristos /* check for an over-subscribed or incomplete set of lengths */ 356aaf4ece6Schristos left = 1; /* one possible code of zero length */ 357aaf4ece6Schristos for (len = 1; len <= MAXBITS; len++) { 358aaf4ece6Schristos left <<= 1; /* one more bit, double codes left */ 359aaf4ece6Schristos left -= h->count[len]; /* deduct count from possible codes */ 360c3423655Schristos if (left < 0) 361c3423655Schristos return left; /* over-subscribed--return negative */ 362aaf4ece6Schristos } /* left > 0 means incomplete */ 363aaf4ece6Schristos 364aaf4ece6Schristos /* generate offsets into symbol table for each length for sorting */ 365aaf4ece6Schristos offs[1] = 0; 366aaf4ece6Schristos for (len = 1; len < MAXBITS; len++) 367aaf4ece6Schristos offs[len + 1] = offs[len] + h->count[len]; 368aaf4ece6Schristos 369aaf4ece6Schristos /* 370aaf4ece6Schristos * put symbols in table sorted by length, by symbol order within each 371aaf4ece6Schristos * length 372aaf4ece6Schristos */ 373aaf4ece6Schristos for (symbol = 0; symbol < n; symbol++) 374aaf4ece6Schristos if (length[symbol] != 0) 375aaf4ece6Schristos h->symbol[offs[length[symbol]]++] = symbol; 376aaf4ece6Schristos 377aaf4ece6Schristos /* return zero for complete set, positive for incomplete set */ 378aaf4ece6Schristos return left; 379aaf4ece6Schristos } 380aaf4ece6Schristos 381aaf4ece6Schristos /* 382aaf4ece6Schristos * Decode literal/length and distance codes until an end-of-block code. 383aaf4ece6Schristos * 384aaf4ece6Schristos * Format notes: 385aaf4ece6Schristos * 386aaf4ece6Schristos * - Compressed data that is after the block type if fixed or after the code 387aaf4ece6Schristos * description if dynamic is a combination of literals and length/distance 388aaf4ece6Schristos * pairs terminated by and end-of-block code. Literals are simply Huffman 389aaf4ece6Schristos * coded bytes. A length/distance pair is a coded length followed by a 390aaf4ece6Schristos * coded distance to represent a string that occurs earlier in the 391aaf4ece6Schristos * uncompressed data that occurs again at the current location. 392aaf4ece6Schristos * 393aaf4ece6Schristos * - Literals, lengths, and the end-of-block code are combined into a single 394aaf4ece6Schristos * code of up to 286 symbols. They are 256 literals (0..255), 29 length 395aaf4ece6Schristos * symbols (257..285), and the end-of-block symbol (256). 396aaf4ece6Schristos * 397aaf4ece6Schristos * - There are 256 possible lengths (3..258), and so 29 symbols are not enough 398aaf4ece6Schristos * to represent all of those. Lengths 3..10 and 258 are in fact represented 399aaf4ece6Schristos * by just a length symbol. Lengths 11..257 are represented as a symbol and 400aaf4ece6Schristos * some number of extra bits that are added as an integer to the base length 401aaf4ece6Schristos * of the length symbol. The number of extra bits is determined by the base 402aaf4ece6Schristos * length symbol. These are in the static arrays below, lens[] for the base 403aaf4ece6Schristos * lengths and lext[] for the corresponding number of extra bits. 404aaf4ece6Schristos * 405aaf4ece6Schristos * - The reason that 258 gets its own symbol is that the longest length is used 406aaf4ece6Schristos * often in highly redundant files. Note that 258 can also be coded as the 407aaf4ece6Schristos * base value 227 plus the maximum extra value of 31. While a good deflate 408aaf4ece6Schristos * should never do this, it is not an error, and should be decoded properly. 409aaf4ece6Schristos * 410aaf4ece6Schristos * - If a length is decoded, including its extra bits if any, then it is 411aaf4ece6Schristos * followed a distance code. There are up to 30 distance symbols. Again 412aaf4ece6Schristos * there are many more possible distances (1..32768), so extra bits are added 413aaf4ece6Schristos * to a base value represented by the symbol. The distances 1..4 get their 414aaf4ece6Schristos * own symbol, but the rest require extra bits. The base distances and 415aaf4ece6Schristos * corresponding number of extra bits are below in the static arrays dist[] 416aaf4ece6Schristos * and dext[]. 417aaf4ece6Schristos * 418aaf4ece6Schristos * - Literal bytes are simply written to the output. A length/distance pair is 419aaf4ece6Schristos * an instruction to copy previously uncompressed bytes to the output. The 420aaf4ece6Schristos * copy is from distance bytes back in the output stream, copying for length 421aaf4ece6Schristos * bytes. 422aaf4ece6Schristos * 423aaf4ece6Schristos * - Distances pointing before the beginning of the output data are not 424aaf4ece6Schristos * permitted. 425aaf4ece6Schristos * 426aaf4ece6Schristos * - Overlapped copies, where the length is greater than the distance, are 427aaf4ece6Schristos * allowed and common. For example, a distance of one and a length of 258 428aaf4ece6Schristos * simply copies the last byte 258 times. A distance of four and a length of 429aaf4ece6Schristos * twelve copies the last four bytes three times. A simple forward copy 430aaf4ece6Schristos * ignoring whether the length is greater than the distance or not implements 431aaf4ece6Schristos * this correctly. You should not use memcpy() since its behavior is not 432aaf4ece6Schristos * defined for overlapped arrays. You should not use memmove() or bcopy() 433aaf4ece6Schristos * since though their behavior -is- defined for overlapping arrays, it is 434aaf4ece6Schristos * defined to do the wrong thing in this case. 435aaf4ece6Schristos */ 436aaf4ece6Schristos local int codes(struct state *s, 437c3423655Schristos const struct huffman *lencode, 438c3423655Schristos const struct huffman *distcode) 439aaf4ece6Schristos { 440aaf4ece6Schristos int symbol; /* decoded symbol */ 441aaf4ece6Schristos int len; /* length for copy */ 442aaf4ece6Schristos unsigned dist; /* distance for copy */ 443aaf4ece6Schristos static const short lens[29] = { /* Size base for length codes 257..285 */ 444aaf4ece6Schristos 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 445aaf4ece6Schristos 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258}; 446aaf4ece6Schristos static const short lext[29] = { /* Extra bits for length codes 257..285 */ 447aaf4ece6Schristos 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 448aaf4ece6Schristos 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0}; 449aaf4ece6Schristos static const short dists[30] = { /* Offset base for distance codes 0..29 */ 450aaf4ece6Schristos 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 451aaf4ece6Schristos 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 452aaf4ece6Schristos 8193, 12289, 16385, 24577}; 453aaf4ece6Schristos static const short dext[30] = { /* Extra bits for distance codes 0..29 */ 454aaf4ece6Schristos 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 455aaf4ece6Schristos 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 456aaf4ece6Schristos 12, 12, 13, 13}; 457aaf4ece6Schristos 458aaf4ece6Schristos /* decode literals and length/distance pairs */ 459aaf4ece6Schristos do { 460aaf4ece6Schristos symbol = decode(s, lencode); 461c3423655Schristos if (symbol < 0) 462c3423655Schristos return symbol; /* invalid symbol */ 463aaf4ece6Schristos if (symbol < 256) { /* literal: symbol is the byte */ 464aaf4ece6Schristos /* write out the literal */ 465aaf4ece6Schristos if (s->out != NIL) { 466c3423655Schristos if (s->outcnt == s->outlen) 467c3423655Schristos return 1; 468aaf4ece6Schristos s->out[s->outcnt] = symbol; 469aaf4ece6Schristos } 470aaf4ece6Schristos s->outcnt++; 471aaf4ece6Schristos } 472aaf4ece6Schristos else if (symbol > 256) { /* length */ 473aaf4ece6Schristos /* get and compute length */ 474aaf4ece6Schristos symbol -= 257; 475c3423655Schristos if (symbol >= 29) 476c3423655Schristos return -10; /* invalid fixed code */ 477aaf4ece6Schristos len = lens[symbol] + bits(s, lext[symbol]); 478aaf4ece6Schristos 479aaf4ece6Schristos /* get and check distance */ 480aaf4ece6Schristos symbol = decode(s, distcode); 481c3423655Schristos if (symbol < 0) 482c3423655Schristos return symbol; /* invalid symbol */ 483aaf4ece6Schristos dist = dists[symbol] + bits(s, dext[symbol]); 484c3423655Schristos #ifndef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR 485aaf4ece6Schristos if (dist > s->outcnt) 486c3423655Schristos return -11; /* distance too far back */ 487c3423655Schristos #endif 488aaf4ece6Schristos 489aaf4ece6Schristos /* copy length bytes from distance bytes back */ 490aaf4ece6Schristos if (s->out != NIL) { 491c3423655Schristos if (s->outcnt + len > s->outlen) 492c3423655Schristos return 1; 493aaf4ece6Schristos while (len--) { 494c3423655Schristos s->out[s->outcnt] = 495c3423655Schristos #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR 496c3423655Schristos dist > s->outcnt ? 497c3423655Schristos 0 : 498c3423655Schristos #endif 499c3423655Schristos s->out[s->outcnt - dist]; 500aaf4ece6Schristos s->outcnt++; 501aaf4ece6Schristos } 502aaf4ece6Schristos } 503aaf4ece6Schristos else 504aaf4ece6Schristos s->outcnt += len; 505aaf4ece6Schristos } 506aaf4ece6Schristos } while (symbol != 256); /* end of block symbol */ 507aaf4ece6Schristos 508aaf4ece6Schristos /* done with a valid fixed or dynamic block */ 509aaf4ece6Schristos return 0; 510aaf4ece6Schristos } 511aaf4ece6Schristos 512aaf4ece6Schristos /* 513aaf4ece6Schristos * Process a fixed codes block. 514aaf4ece6Schristos * 515aaf4ece6Schristos * Format notes: 516aaf4ece6Schristos * 517aaf4ece6Schristos * - This block type can be useful for compressing small amounts of data for 518aaf4ece6Schristos * which the size of the code descriptions in a dynamic block exceeds the 519aaf4ece6Schristos * benefit of custom codes for that block. For fixed codes, no bits are 520aaf4ece6Schristos * spent on code descriptions. Instead the code lengths for literal/length 521aaf4ece6Schristos * codes and distance codes are fixed. The specific lengths for each symbol 522aaf4ece6Schristos * can be seen in the "for" loops below. 523aaf4ece6Schristos * 524aaf4ece6Schristos * - The literal/length code is complete, but has two symbols that are invalid 525aaf4ece6Schristos * and should result in an error if received. This cannot be implemented 526aaf4ece6Schristos * simply as an incomplete code since those two symbols are in the "middle" 527aaf4ece6Schristos * of the code. They are eight bits long and the longest literal/length\ 528aaf4ece6Schristos * code is nine bits. Therefore the code must be constructed with those 529aaf4ece6Schristos * symbols, and the invalid symbols must be detected after decoding. 530aaf4ece6Schristos * 531aaf4ece6Schristos * - The fixed distance codes also have two invalid symbols that should result 532aaf4ece6Schristos * in an error if received. Since all of the distance codes are the same 533aaf4ece6Schristos * length, this can be implemented as an incomplete code. Then the invalid 534aaf4ece6Schristos * codes are detected while decoding. 535aaf4ece6Schristos */ 536aaf4ece6Schristos local int fixed(struct state *s) 537aaf4ece6Schristos { 538aaf4ece6Schristos static int virgin = 1; 539aaf4ece6Schristos static short lencnt[MAXBITS+1], lensym[FIXLCODES]; 540aaf4ece6Schristos static short distcnt[MAXBITS+1], distsym[MAXDCODES]; 541c3423655Schristos static struct huffman lencode, distcode; 542aaf4ece6Schristos 543aaf4ece6Schristos /* build fixed huffman tables if first call (may not be thread safe) */ 544aaf4ece6Schristos if (virgin) { 545aaf4ece6Schristos int symbol; 546aaf4ece6Schristos short lengths[FIXLCODES]; 547aaf4ece6Schristos 548c3423655Schristos /* construct lencode and distcode */ 549c3423655Schristos lencode.count = lencnt; 550c3423655Schristos lencode.symbol = lensym; 551c3423655Schristos distcode.count = distcnt; 552c3423655Schristos distcode.symbol = distsym; 553c3423655Schristos 554aaf4ece6Schristos /* literal/length table */ 555aaf4ece6Schristos for (symbol = 0; symbol < 144; symbol++) 556aaf4ece6Schristos lengths[symbol] = 8; 557aaf4ece6Schristos for (; symbol < 256; symbol++) 558aaf4ece6Schristos lengths[symbol] = 9; 559aaf4ece6Schristos for (; symbol < 280; symbol++) 560aaf4ece6Schristos lengths[symbol] = 7; 561aaf4ece6Schristos for (; symbol < FIXLCODES; symbol++) 562aaf4ece6Schristos lengths[symbol] = 8; 563aaf4ece6Schristos construct(&lencode, lengths, FIXLCODES); 564aaf4ece6Schristos 565aaf4ece6Schristos /* distance table */ 566aaf4ece6Schristos for (symbol = 0; symbol < MAXDCODES; symbol++) 567aaf4ece6Schristos lengths[symbol] = 5; 568aaf4ece6Schristos construct(&distcode, lengths, MAXDCODES); 569aaf4ece6Schristos 570aaf4ece6Schristos /* do this just once */ 571aaf4ece6Schristos virgin = 0; 572aaf4ece6Schristos } 573aaf4ece6Schristos 574aaf4ece6Schristos /* decode data until end-of-block code */ 575aaf4ece6Schristos return codes(s, &lencode, &distcode); 576aaf4ece6Schristos } 577aaf4ece6Schristos 578aaf4ece6Schristos /* 579aaf4ece6Schristos * Process a dynamic codes block. 580aaf4ece6Schristos * 581aaf4ece6Schristos * Format notes: 582aaf4ece6Schristos * 583aaf4ece6Schristos * - A dynamic block starts with a description of the literal/length and 584aaf4ece6Schristos * distance codes for that block. New dynamic blocks allow the compressor to 585aaf4ece6Schristos * rapidly adapt to changing data with new codes optimized for that data. 586aaf4ece6Schristos * 587aaf4ece6Schristos * - The codes used by the deflate format are "canonical", which means that 588aaf4ece6Schristos * the actual bits of the codes are generated in an unambiguous way simply 589aaf4ece6Schristos * from the number of bits in each code. Therefore the code descriptions 590aaf4ece6Schristos * are simply a list of code lengths for each symbol. 591aaf4ece6Schristos * 592aaf4ece6Schristos * - The code lengths are stored in order for the symbols, so lengths are 593aaf4ece6Schristos * provided for each of the literal/length symbols, and for each of the 594aaf4ece6Schristos * distance symbols. 595aaf4ece6Schristos * 596*b175d1c2Schristos * - If a symbol is not used in the block, this is represented by a zero as the 597*b175d1c2Schristos * code length. This does not mean a zero-length code, but rather that no 598*b175d1c2Schristos * code should be created for this symbol. There is no way in the deflate 599*b175d1c2Schristos * format to represent a zero-length code. 600aaf4ece6Schristos * 601aaf4ece6Schristos * - The maximum number of bits in a code is 15, so the possible lengths for 602aaf4ece6Schristos * any code are 1..15. 603aaf4ece6Schristos * 604aaf4ece6Schristos * - The fact that a length of zero is not permitted for a code has an 605aaf4ece6Schristos * interesting consequence. Normally if only one symbol is used for a given 606aaf4ece6Schristos * code, then in fact that code could be represented with zero bits. However 607aaf4ece6Schristos * in deflate, that code has to be at least one bit. So for example, if 608aaf4ece6Schristos * only a single distance base symbol appears in a block, then it will be 609aaf4ece6Schristos * represented by a single code of length one, in particular one 0 bit. This 610aaf4ece6Schristos * is an incomplete code, since if a 1 bit is received, it has no meaning, 611aaf4ece6Schristos * and should result in an error. So incomplete distance codes of one symbol 612aaf4ece6Schristos * should be permitted, and the receipt of invalid codes should be handled. 613aaf4ece6Schristos * 614aaf4ece6Schristos * - It is also possible to have a single literal/length code, but that code 615aaf4ece6Schristos * must be the end-of-block code, since every dynamic block has one. This 616aaf4ece6Schristos * is not the most efficient way to create an empty block (an empty fixed 617aaf4ece6Schristos * block is fewer bits), but it is allowed by the format. So incomplete 618aaf4ece6Schristos * literal/length codes of one symbol should also be permitted. 619aaf4ece6Schristos * 620aaf4ece6Schristos * - If there are only literal codes and no lengths, then there are no distance 621aaf4ece6Schristos * codes. This is represented by one distance code with zero bits. 622aaf4ece6Schristos * 623aaf4ece6Schristos * - The list of up to 286 length/literal lengths and up to 30 distance lengths 624aaf4ece6Schristos * are themselves compressed using Huffman codes and run-length encoding. In 625aaf4ece6Schristos * the list of code lengths, a 0 symbol means no code, a 1..15 symbol means 626aaf4ece6Schristos * that length, and the symbols 16, 17, and 18 are run-length instructions. 627ec47cc4bSchristos * Each of 16, 17, and 18 are followed by extra bits to define the length of 628aaf4ece6Schristos * the run. 16 copies the last length 3 to 6 times. 17 represents 3 to 10 629aaf4ece6Schristos * zero lengths, and 18 represents 11 to 138 zero lengths. Unused symbols 630aaf4ece6Schristos * are common, hence the special coding for zero lengths. 631aaf4ece6Schristos * 632aaf4ece6Schristos * - The symbols for 0..18 are Huffman coded, and so that code must be 633aaf4ece6Schristos * described first. This is simply a sequence of up to 19 three-bit values 634aaf4ece6Schristos * representing no code (0) or the code length for that symbol (1..7). 635aaf4ece6Schristos * 636aaf4ece6Schristos * - A dynamic block starts with three fixed-size counts from which is computed 637aaf4ece6Schristos * the number of literal/length code lengths, the number of distance code 638aaf4ece6Schristos * lengths, and the number of code length code lengths (ok, you come up with 639aaf4ece6Schristos * a better name!) in the code descriptions. For the literal/length and 640aaf4ece6Schristos * distance codes, lengths after those provided are considered zero, i.e. no 641aaf4ece6Schristos * code. The code length code lengths are received in a permuted order (see 642aaf4ece6Schristos * the order[] array below) to make a short code length code length list more 643aaf4ece6Schristos * likely. As it turns out, very short and very long codes are less likely 644aaf4ece6Schristos * to be seen in a dynamic code description, hence what may appear initially 645aaf4ece6Schristos * to be a peculiar ordering. 646aaf4ece6Schristos * 647aaf4ece6Schristos * - Given the number of literal/length code lengths (nlen) and distance code 648aaf4ece6Schristos * lengths (ndist), then they are treated as one long list of nlen + ndist 649aaf4ece6Schristos * code lengths. Therefore run-length coding can and often does cross the 650aaf4ece6Schristos * boundary between the two sets of lengths. 651aaf4ece6Schristos * 652aaf4ece6Schristos * - So to summarize, the code description at the start of a dynamic block is 653aaf4ece6Schristos * three counts for the number of code lengths for the literal/length codes, 654aaf4ece6Schristos * the distance codes, and the code length codes. This is followed by the 655aaf4ece6Schristos * code length code lengths, three bits each. This is used to construct the 656aaf4ece6Schristos * code length code which is used to read the remainder of the lengths. Then 657aaf4ece6Schristos * the literal/length code lengths and distance lengths are read as a single 658aaf4ece6Schristos * set of lengths using the code length codes. Codes are constructed from 659aaf4ece6Schristos * the resulting two sets of lengths, and then finally you can start 660aaf4ece6Schristos * decoding actual compressed data in the block. 661aaf4ece6Schristos * 662aaf4ece6Schristos * - For reference, a "typical" size for the code description in a dynamic 663aaf4ece6Schristos * block is around 80 bytes. 664aaf4ece6Schristos */ 665aaf4ece6Schristos local int dynamic(struct state *s) 666aaf4ece6Schristos { 667aaf4ece6Schristos int nlen, ndist, ncode; /* number of lengths in descriptor */ 668aaf4ece6Schristos int index; /* index of lengths[] */ 669aaf4ece6Schristos int err; /* construct() return value */ 670aaf4ece6Schristos short lengths[MAXCODES]; /* descriptor code lengths */ 671aaf4ece6Schristos short lencnt[MAXBITS+1], lensym[MAXLCODES]; /* lencode memory */ 672aaf4ece6Schristos short distcnt[MAXBITS+1], distsym[MAXDCODES]; /* distcode memory */ 673c3423655Schristos struct huffman lencode, distcode; /* length and distance codes */ 674aaf4ece6Schristos static const short order[19] = /* permutation of code length codes */ 675aaf4ece6Schristos {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; 676aaf4ece6Schristos 677c3423655Schristos /* construct lencode and distcode */ 678c3423655Schristos lencode.count = lencnt; 679c3423655Schristos lencode.symbol = lensym; 680c3423655Schristos distcode.count = distcnt; 681c3423655Schristos distcode.symbol = distsym; 682c3423655Schristos 683aaf4ece6Schristos /* get number of lengths in each table, check lengths */ 684aaf4ece6Schristos nlen = bits(s, 5) + 257; 685aaf4ece6Schristos ndist = bits(s, 5) + 1; 686aaf4ece6Schristos ncode = bits(s, 4) + 4; 687aaf4ece6Schristos if (nlen > MAXLCODES || ndist > MAXDCODES) 688aaf4ece6Schristos return -3; /* bad counts */ 689aaf4ece6Schristos 690aaf4ece6Schristos /* read code length code lengths (really), missing lengths are zero */ 691aaf4ece6Schristos for (index = 0; index < ncode; index++) 692aaf4ece6Schristos lengths[order[index]] = bits(s, 3); 693aaf4ece6Schristos for (; index < 19; index++) 694aaf4ece6Schristos lengths[order[index]] = 0; 695aaf4ece6Schristos 696aaf4ece6Schristos /* build huffman table for code lengths codes (use lencode temporarily) */ 697aaf4ece6Schristos err = construct(&lencode, lengths, 19); 698c3423655Schristos if (err != 0) /* require complete code set here */ 699c3423655Schristos return -4; 700aaf4ece6Schristos 701aaf4ece6Schristos /* read length/literal and distance code length tables */ 702aaf4ece6Schristos index = 0; 703aaf4ece6Schristos while (index < nlen + ndist) { 704aaf4ece6Schristos int symbol; /* decoded value */ 705aaf4ece6Schristos int len; /* last length to repeat */ 706aaf4ece6Schristos 707aaf4ece6Schristos symbol = decode(s, &lencode); 708c3423655Schristos if (symbol < 0) 709c3423655Schristos return symbol; /* invalid symbol */ 710aaf4ece6Schristos if (symbol < 16) /* length in 0..15 */ 711aaf4ece6Schristos lengths[index++] = symbol; 712aaf4ece6Schristos else { /* repeat instruction */ 713aaf4ece6Schristos len = 0; /* assume repeating zeros */ 714aaf4ece6Schristos if (symbol == 16) { /* repeat last length 3..6 times */ 715c3423655Schristos if (index == 0) 716c3423655Schristos return -5; /* no last length! */ 717aaf4ece6Schristos len = lengths[index - 1]; /* last length */ 718aaf4ece6Schristos symbol = 3 + bits(s, 2); 719aaf4ece6Schristos } 720aaf4ece6Schristos else if (symbol == 17) /* repeat zero 3..10 times */ 721aaf4ece6Schristos symbol = 3 + bits(s, 3); 722aaf4ece6Schristos else /* == 18, repeat zero 11..138 times */ 723aaf4ece6Schristos symbol = 11 + bits(s, 7); 724aaf4ece6Schristos if (index + symbol > nlen + ndist) 725aaf4ece6Schristos return -6; /* too many lengths! */ 726aaf4ece6Schristos while (symbol--) /* repeat last or zero symbol times */ 727aaf4ece6Schristos lengths[index++] = len; 728aaf4ece6Schristos } 729aaf4ece6Schristos } 730aaf4ece6Schristos 731c3423655Schristos /* check for end-of-block code -- there better be one! */ 732c3423655Schristos if (lengths[256] == 0) 733c3423655Schristos return -9; 734c3423655Schristos 735aaf4ece6Schristos /* build huffman table for literal/length codes */ 736aaf4ece6Schristos err = construct(&lencode, lengths, nlen); 737c3423655Schristos if (err && (err < 0 || nlen != lencode.count[0] + lencode.count[1])) 738c3423655Schristos return -7; /* incomplete code ok only for single length 1 code */ 739aaf4ece6Schristos 740aaf4ece6Schristos /* build huffman table for distance codes */ 741aaf4ece6Schristos err = construct(&distcode, lengths + nlen, ndist); 742c3423655Schristos if (err && (err < 0 || ndist != distcode.count[0] + distcode.count[1])) 743c3423655Schristos return -8; /* incomplete code ok only for single length 1 code */ 744aaf4ece6Schristos 745aaf4ece6Schristos /* decode data until end-of-block code */ 746aaf4ece6Schristos return codes(s, &lencode, &distcode); 747aaf4ece6Schristos } 748aaf4ece6Schristos 749aaf4ece6Schristos /* 750aaf4ece6Schristos * Inflate source to dest. On return, destlen and sourcelen are updated to the 751aaf4ece6Schristos * size of the uncompressed data and the size of the deflate data respectively. 752aaf4ece6Schristos * On success, the return value of puff() is zero. If there is an error in the 753aaf4ece6Schristos * source data, i.e. it is not in the deflate format, then a negative value is 754aaf4ece6Schristos * returned. If there is not enough input available or there is not enough 755aaf4ece6Schristos * output space, then a positive error is returned. In that case, destlen and 756aaf4ece6Schristos * sourcelen are not updated to facilitate retrying from the beginning with the 757aaf4ece6Schristos * provision of more input data or more output space. In the case of invalid 758aaf4ece6Schristos * inflate data (a negative error), the dest and source pointers are updated to 759aaf4ece6Schristos * facilitate the debugging of deflators. 760aaf4ece6Schristos * 761aaf4ece6Schristos * puff() also has a mode to determine the size of the uncompressed output with 762aaf4ece6Schristos * no output written. For this dest must be (unsigned char *)0. In this case, 763aaf4ece6Schristos * the input value of *destlen is ignored, and on return *destlen is set to the 764aaf4ece6Schristos * size of the uncompressed output. 765aaf4ece6Schristos * 766aaf4ece6Schristos * The return codes are: 767aaf4ece6Schristos * 768aaf4ece6Schristos * 2: available inflate data did not terminate 769aaf4ece6Schristos * 1: output space exhausted before completing inflate 770aaf4ece6Schristos * 0: successful inflate 771aaf4ece6Schristos * -1: invalid block type (type == 3) 772aaf4ece6Schristos * -2: stored block length did not match one's complement 773aaf4ece6Schristos * -3: dynamic block code description: too many length or distance codes 774aaf4ece6Schristos * -4: dynamic block code description: code lengths codes incomplete 775aaf4ece6Schristos * -5: dynamic block code description: repeat lengths with no first length 776aaf4ece6Schristos * -6: dynamic block code description: repeat more than specified lengths 777aaf4ece6Schristos * -7: dynamic block code description: invalid literal/length code lengths 778aaf4ece6Schristos * -8: dynamic block code description: invalid distance code lengths 779c3423655Schristos * -9: dynamic block code description: missing end-of-block code 780c3423655Schristos * -10: invalid literal/length or distance code in fixed or dynamic block 781c3423655Schristos * -11: distance is too far back in fixed or dynamic block 782aaf4ece6Schristos * 783aaf4ece6Schristos * Format notes: 784aaf4ece6Schristos * 785aaf4ece6Schristos * - Three bits are read for each block to determine the kind of block and 786aaf4ece6Schristos * whether or not it is the last block. Then the block is decoded and the 787aaf4ece6Schristos * process repeated if it was not the last block. 788aaf4ece6Schristos * 789aaf4ece6Schristos * - The leftover bits in the last byte of the deflate data after the last 790aaf4ece6Schristos * block (if it was a fixed or dynamic block) are undefined and have no 791aaf4ece6Schristos * expected values to check. 792aaf4ece6Schristos */ 793aaf4ece6Schristos int puff(unsigned char *dest, /* pointer to destination pointer */ 794aaf4ece6Schristos unsigned long *destlen, /* amount of output space */ 795c3423655Schristos const unsigned char *source, /* pointer to source data pointer */ 796aaf4ece6Schristos unsigned long *sourcelen) /* amount of input available */ 797aaf4ece6Schristos { 798aaf4ece6Schristos struct state s; /* input/output state */ 799aaf4ece6Schristos int last, type; /* block information */ 800aaf4ece6Schristos int err; /* return value */ 801aaf4ece6Schristos 802aaf4ece6Schristos /* initialize output state */ 803aaf4ece6Schristos s.out = dest; 804aaf4ece6Schristos s.outlen = *destlen; /* ignored if dest is NIL */ 805aaf4ece6Schristos s.outcnt = 0; 806aaf4ece6Schristos 807aaf4ece6Schristos /* initialize input state */ 808aaf4ece6Schristos s.in = source; 809aaf4ece6Schristos s.inlen = *sourcelen; 810aaf4ece6Schristos s.incnt = 0; 811aaf4ece6Schristos s.bitbuf = 0; 812aaf4ece6Schristos s.bitcnt = 0; 813aaf4ece6Schristos 814aaf4ece6Schristos /* return if bits() or decode() tries to read past available input */ 815aaf4ece6Schristos if (setjmp(s.env) != 0) /* if came back here via longjmp() */ 816aaf4ece6Schristos err = 2; /* then skip do-loop, return error */ 817aaf4ece6Schristos else { 818aaf4ece6Schristos /* process blocks until last block or error */ 819aaf4ece6Schristos do { 820aaf4ece6Schristos last = bits(&s, 1); /* one if last block */ 821aaf4ece6Schristos type = bits(&s, 2); /* block type 0..3 */ 822c3423655Schristos err = type == 0 ? 823c3423655Schristos stored(&s) : 824c3423655Schristos (type == 1 ? 825c3423655Schristos fixed(&s) : 826c3423655Schristos (type == 2 ? 827c3423655Schristos dynamic(&s) : 828aaf4ece6Schristos -1)); /* type == 3, invalid */ 829c3423655Schristos if (err != 0) 830c3423655Schristos break; /* return with error */ 831aaf4ece6Schristos } while (!last); 832aaf4ece6Schristos } 833aaf4ece6Schristos 834aaf4ece6Schristos /* update the lengths and return */ 835aaf4ece6Schristos if (err <= 0) { 836aaf4ece6Schristos *destlen = s.outcnt; 837aaf4ece6Schristos *sourcelen = s.incnt; 838aaf4ece6Schristos } 839aaf4ece6Schristos return err; 840aaf4ece6Schristos } 841