• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

contrib/zlib/contrib/puff/puff.c

00001 /*
00002  * puff.c
00003  * Copyright (C) 2002-2010 Mark Adler
00004  * For conditions of distribution and use, see copyright notice in puff.h
00005  * version 2.1, 4 Apr 2010
00006  *
00007  * puff.c is a simple inflate written to be an unambiguous way to specify the
00008  * deflate format.  It is not written for speed but rather simplicity.  As a
00009  * side benefit, this code might actually be useful when small code is more
00010  * important than speed, such as bootstrap applications.  For typical deflate
00011  * data, zlib's inflate() is about four times as fast as puff().  zlib's
00012  * inflate compiles to around 20K on my machine, whereas puff.c compiles to
00013  * around 4K on my machine (a PowerPC using GNU cc).  If the faster decode()
00014  * function here is used, then puff() is only twice as slow as zlib's
00015  * inflate().
00016  *
00017  * All dynamically allocated memory comes from the stack.  The stack required
00018  * is less than 2K bytes.  This code is compatible with 16-bit int's and
00019  * assumes that long's are at least 32 bits.  puff.c uses the short data type,
00020  * assumed to be 16 bits, for arrays in order to to conserve memory.  The code
00021  * works whether integers are stored big endian or little endian.
00022  *
00023  * In the comments below are "Format notes" that describe the inflate process
00024  * and document some of the less obvious aspects of the format.  This source
00025  * code is meant to supplement RFC 1951, which formally describes the deflate
00026  * format:
00027  *
00028  *    http://www.zlib.org/rfc-deflate.html
00029  */
00030 
00031 /*
00032  * Change history:
00033  *
00034  * 1.0  10 Feb 2002     - First version
00035  * 1.1  17 Feb 2002     - Clarifications of some comments and notes
00036  *                      - Update puff() dest and source pointers on negative
00037  *                        errors to facilitate debugging deflators
00038  *                      - Remove longest from struct huffman -- not needed
00039  *                      - Simplify offs[] index in construct()
00040  *                      - Add input size and checking, using longjmp() to
00041  *                        maintain easy readability
00042  *                      - Use short data type for large arrays
00043  *                      - Use pointers instead of long to specify source and
00044  *                        destination sizes to avoid arbitrary 4 GB limits
00045  * 1.2  17 Mar 2002     - Add faster version of decode(), doubles speed (!),
00046  *                        but leave simple version for readabilty
00047  *                      - Make sure invalid distances detected if pointers
00048  *                        are 16 bits
00049  *                      - Fix fixed codes table error
00050  *                      - Provide a scanning mode for determining size of
00051  *                        uncompressed data
00052  * 1.3  20 Mar 2002     - Go back to lengths for puff() parameters [Jean-loup]
00053  *                      - Add a puff.h file for the interface
00054  *                      - Add braces in puff() for else do [Jean-loup]
00055  *                      - Use indexes instead of pointers for readability
00056  * 1.4  31 Mar 2002     - Simplify construct() code set check
00057  *                      - Fix some comments
00058  *                      - Add FIXLCODES #define
00059  * 1.5   6 Apr 2002     - Minor comment fixes
00060  * 1.6   7 Aug 2002     - Minor format changes
00061  * 1.7   3 Mar 2003     - Added test code for distribution
00062  *                      - Added zlib-like license
00063  * 1.8   9 Jan 2004     - Added some comments on no distance codes case
00064  * 1.9  21 Feb 2008     - Fix bug on 16-bit integer architectures [Pohland]
00065  *                      - Catch missing end-of-block symbol error
00066  * 2.0  25 Jul 2008     - Add #define to permit distance too far back
00067  *                      - Add option in TEST code for puff to write the data
00068  *                      - Add option in TEST code to skip input bytes
00069  *                      - Allow TEST code to read from piped stdin
00070  * 2.1   4 Apr 2010     - Avoid variable initialization for happier compilers
00071  *                      - Avoid unsigned comparisons for even happier compilers
00072  */
00073 
00074 #include <setjmp.h>             /* for setjmp(), longjmp(), and jmp_buf */
00075 #include "puff.h"               /* prototype for puff() */
00076 
00077 #define local static            /* for local function definitions */
00078 #define NIL ((unsigned char *)0)        /* for no output option */
00079 
00080 /*
00081  * Maximums for allocations and loops.  It is not useful to change these --
00082  * they are fixed by the deflate format.
00083  */
00084 #define MAXBITS 15              /* maximum bits in a code */
00085 #define MAXLCODES 286           /* maximum number of literal/length codes */
00086 #define MAXDCODES 30            /* maximum number of distance codes */
00087 #define MAXCODES (MAXLCODES+MAXDCODES)  /* maximum codes lengths to read */
00088 #define FIXLCODES 288           /* number of fixed literal/length codes */
00089 
00090 /* input and output state */
00091 struct state {
00092     /* output state */
00093     unsigned char *out;         /* output buffer */
00094     unsigned long outlen;       /* available space at out */
00095     unsigned long outcnt;       /* bytes written to out so far */
00096 
00097     /* input state */
00098     unsigned char *in;          /* input buffer */
00099     unsigned long inlen;        /* available input at in */
00100     unsigned long incnt;        /* bytes read so far */
00101     int bitbuf;                 /* bit buffer */
00102     int bitcnt;                 /* number of bits in bit buffer */
00103 
00104     /* input limit error return state for bits() and decode() */
00105     jmp_buf env;
00106 };
00107 
00108 /*
00109  * Return need bits from the input stream.  This always leaves less than
00110  * eight bits in the buffer.  bits() works properly for need == 0.
00111  *
00112  * Format notes:
00113  *
00114  * - Bits are stored in bytes from the least significant bit to the most
00115  *   significant bit.  Therefore bits are dropped from the bottom of the bit
00116  *   buffer, using shift right, and new bytes are appended to the top of the
00117  *   bit buffer, using shift left.
00118  */
00119 local int bits(struct state *s, int need)
00120 {
00121     long val;           /* bit accumulator (can use up to 20 bits) */
00122 
00123     /* load at least need bits into val */
00124     val = s->bitbuf;
00125     while (s->bitcnt < need) {
00126         if (s->incnt == s->inlen) longjmp(s->env, 1);   /* out of input */
00127         val |= (long)(s->in[s->incnt++]) << s->bitcnt;  /* load eight bits */
00128         s->bitcnt += 8;
00129     }
00130 
00131     /* drop need bits and update buffer, always zero to seven bits left */
00132     s->bitbuf = (int)(val >> need);
00133     s->bitcnt -= need;
00134 
00135     /* return need bits, zeroing the bits above that */
00136     return (int)(val & ((1L << need) - 1));
00137 }
00138 
00139 /*
00140  * Process a stored block.
00141  *
00142  * Format notes:
00143  *
00144  * - After the two-bit stored block type (00), the stored block length and
00145  *   stored bytes are byte-aligned for fast copying.  Therefore any leftover
00146  *   bits in the byte that has the last bit of the type, as many as seven, are
00147  *   discarded.  The value of the discarded bits are not defined and should not
00148  *   be checked against any expectation.
00149  *
00150  * - The second inverted copy of the stored block length does not have to be
00151  *   checked, but it's probably a good idea to do so anyway.
00152  *
00153  * - A stored block can have zero length.  This is sometimes used to byte-align
00154  *   subsets of the compressed data for random access or partial recovery.
00155  */
00156 local int stored(struct state *s)
00157 {
00158     unsigned len;       /* length of stored block */
00159 
00160     /* discard leftover bits from current byte (assumes s->bitcnt < 8) */
00161     s->bitbuf = 0;
00162     s->bitcnt = 0;
00163 
00164     /* get length and check against its one's complement */
00165     if (s->incnt + 4 > s->inlen) return 2;      /* not enough input */
00166     len = s->in[s->incnt++];
00167     len |= s->in[s->incnt++] << 8;
00168     if (s->in[s->incnt++] != (~len & 0xff) ||
00169         s->in[s->incnt++] != ((~len >> 8) & 0xff))
00170         return -2;                              /* didn't match complement! */
00171 
00172     /* copy len bytes from in to out */
00173     if (s->incnt + len > s->inlen) return 2;    /* not enough input */
00174     if (s->out != NIL) {
00175         if (s->outcnt + len > s->outlen)
00176             return 1;                           /* not enough output space */
00177         while (len--)
00178             s->out[s->outcnt++] = s->in[s->incnt++];
00179     }
00180     else {                                      /* just scanning */
00181         s->outcnt += len;
00182         s->incnt += len;
00183     }
00184 
00185     /* done with a valid stored block */
00186     return 0;
00187 }
00188 
00189 /*
00190  * Huffman code decoding tables.  count[1..MAXBITS] is the number of symbols of
00191  * each length, which for a canonical code are stepped through in order.
00192  * symbol[] are the symbol values in canonical order, where the number of
00193  * entries is the sum of the counts in count[].  The decoding process can be
00194  * seen in the function decode() below.
00195  */
00196 struct huffman {
00197     short *count;       /* number of symbols of each length */
00198     short *symbol;      /* canonically ordered symbols */
00199 };
00200 
00201 /*
00202  * Decode a code from the stream s using huffman table h.  Return the symbol or
00203  * a negative value if there is an error.  If all of the lengths are zero, i.e.
00204  * an empty code, or if the code is incomplete and an invalid code is received,
00205  * then -10 is returned after reading MAXBITS bits.
00206  *
00207  * Format notes:
00208  *
00209  * - The codes as stored in the compressed data are bit-reversed relative to
00210  *   a simple integer ordering of codes of the same lengths.  Hence below the
00211  *   bits are pulled from the compressed data one at a time and used to
00212  *   build the code value reversed from what is in the stream in order to
00213  *   permit simple integer comparisons for decoding.  A table-based decoding
00214  *   scheme (as used in zlib) does not need to do this reversal.
00215  *
00216  * - The first code for the shortest length is all zeros.  Subsequent codes of
00217  *   the same length are simply integer increments of the previous code.  When
00218  *   moving up a length, a zero bit is appended to the code.  For a complete
00219  *   code, the last code of the longest length will be all ones.
00220  *
00221  * - Incomplete codes are handled by this decoder, since they are permitted
00222  *   in the deflate format.  See the format notes for fixed() and dynamic().
00223  */
00224 #ifdef SLOW
00225 local int decode(struct state *s, struct huffman *h)
00226 {
00227     int len;            /* current number of bits in code */
00228     int code;           /* len bits being decoded */
00229     int first;          /* first code of length len */
00230     int count;          /* number of codes of length len */
00231     int index;          /* index of first code of length len in symbol table */
00232 
00233     code = first = index = 0;
00234     for (len = 1; len <= MAXBITS; len++) {
00235         code |= bits(s, 1);             /* get next bit */
00236         count = h->count[len];
00237         if (code - count < first)       /* if length len, return symbol */
00238             return h->symbol[index + (code - first)];
00239         index += count;                 /* else update for next length */
00240         first += count;
00241         first <<= 1;
00242         code <<= 1;
00243     }
00244     return -10;                         /* ran out of codes */
00245 }
00246 
00247 /*
00248  * A faster version of decode() for real applications of this code.   It's not
00249  * as readable, but it makes puff() twice as fast.  And it only makes the code
00250  * a few percent larger.
00251  */
00252 #else /* !SLOW */
00253 local int decode(struct state *s, struct huffman *h)
00254 {
00255     int len;            /* current number of bits in code */
00256     int code;           /* len bits being decoded */
00257     int first;          /* first code of length len */
00258     int count;          /* number of codes of length len */
00259     int index;          /* index of first code of length len in symbol table */
00260     int bitbuf;         /* bits from stream */
00261     int left;           /* bits left in next or left to process */
00262     short *next;        /* next number of codes */
00263 
00264     bitbuf = s->bitbuf;
00265     left = s->bitcnt;
00266     code = first = index = 0;
00267     len = 1;
00268     next = h->count + 1;
00269     while (1) {
00270         while (left--) {
00271             code |= bitbuf & 1;
00272             bitbuf >>= 1;
00273             count = *next++;
00274             if (code - count < first) { /* if length len, return symbol */
00275                 s->bitbuf = bitbuf;
00276                 s->bitcnt = (s->bitcnt - len) & 7;
00277                 return h->symbol[index + (code - first)];
00278             }
00279             index += count;             /* else update for next length */
00280             first += count;
00281             first <<= 1;
00282             code <<= 1;
00283             len++;
00284         }
00285         left = (MAXBITS+1) - len;
00286         if (left == 0) break;
00287         if (s->incnt == s->inlen) longjmp(s->env, 1);   /* out of input */
00288         bitbuf = s->in[s->incnt++];
00289         if (left > 8) left = 8;
00290     }
00291     return -10;                         /* ran out of codes */
00292 }
00293 #endif /* SLOW */
00294 
00295 /*
00296  * Given the list of code lengths length[0..n-1] representing a canonical
00297  * Huffman code for n symbols, construct the tables required to decode those
00298  * codes.  Those tables are the number of codes of each length, and the symbols
00299  * sorted by length, retaining their original order within each length.  The
00300  * return value is zero for a complete code set, negative for an over-
00301  * subscribed code set, and positive for an incomplete code set.  The tables
00302  * can be used if the return value is zero or positive, but they cannot be used
00303  * if the return value is negative.  If the return value is zero, it is not
00304  * possible for decode() using that table to return an error--any stream of
00305  * enough bits will resolve to a symbol.  If the return value is positive, then
00306  * it is possible for decode() using that table to return an error for received
00307  * codes past the end of the incomplete lengths.
00308  *
00309  * Not used by decode(), but used for error checking, h->count[0] is the number
00310  * of the n symbols not in the code.  So n - h->count[0] is the number of
00311  * codes.  This is useful for checking for incomplete codes that have more than
00312  * one symbol, which is an error in a dynamic block.
00313  *
00314  * Assumption: for all i in 0..n-1, 0 <= length[i] <= MAXBITS
00315  * This is assured by the construction of the length arrays in dynamic() and
00316  * fixed() and is not verified by construct().
00317  *
00318  * Format notes:
00319  *
00320  * - Permitted and expected examples of incomplete codes are one of the fixed
00321  *   codes and any code with a single symbol which in deflate is coded as one
00322  *   bit instead of zero bits.  See the format notes for fixed() and dynamic().
00323  *
00324  * - Within a given code length, the symbols are kept in ascending order for
00325  *   the code bits definition.
00326  */
00327 local int construct(struct huffman *h, short *length, int n)
00328 {
00329     int symbol;         /* current symbol when stepping through length[] */
00330     int len;            /* current length when stepping through h->count[] */
00331     int left;           /* number of possible codes left of current length */
00332     short offs[MAXBITS+1];      /* offsets in symbol table for each length */
00333 
00334     /* count number of codes of each length */
00335     for (len = 0; len <= MAXBITS; len++)
00336         h->count[len] = 0;
00337     for (symbol = 0; symbol < n; symbol++)
00338         (h->count[length[symbol]])++;   /* assumes lengths are within bounds */
00339     if (h->count[0] == n)               /* no codes! */
00340         return 0;                       /* complete, but decode() will fail */
00341 
00342     /* check for an over-subscribed or incomplete set of lengths */
00343     left = 1;                           /* one possible code of zero length */
00344     for (len = 1; len <= MAXBITS; len++) {
00345         left <<= 1;                     /* one more bit, double codes left */
00346         left -= h->count[len];          /* deduct count from possible codes */
00347         if (left < 0) return left;      /* over-subscribed--return negative */
00348     }                                   /* left > 0 means incomplete */
00349 
00350     /* generate offsets into symbol table for each length for sorting */
00351     offs[1] = 0;
00352     for (len = 1; len < MAXBITS; len++)
00353         offs[len + 1] = offs[len] + h->count[len];
00354 
00355     /*
00356      * put symbols in table sorted by length, by symbol order within each
00357      * length
00358      */
00359     for (symbol = 0; symbol < n; symbol++)
00360         if (length[symbol] != 0)
00361             h->symbol[offs[length[symbol]]++] = symbol;
00362 
00363     /* return zero for complete set, positive for incomplete set */
00364     return left;
00365 }
00366 
00367 /*
00368  * Decode literal/length and distance codes until an end-of-block code.
00369  *
00370  * Format notes:
00371  *
00372  * - Compressed data that is after the block type if fixed or after the code
00373  *   description if dynamic is a combination of literals and length/distance
00374  *   pairs terminated by and end-of-block code.  Literals are simply Huffman
00375  *   coded bytes.  A length/distance pair is a coded length followed by a
00376  *   coded distance to represent a string that occurs earlier in the
00377  *   uncompressed data that occurs again at the current location.
00378  *
00379  * - Literals, lengths, and the end-of-block code are combined into a single
00380  *   code of up to 286 symbols.  They are 256 literals (0..255), 29 length
00381  *   symbols (257..285), and the end-of-block symbol (256).
00382  *
00383  * - There are 256 possible lengths (3..258), and so 29 symbols are not enough
00384  *   to represent all of those.  Lengths 3..10 and 258 are in fact represented
00385  *   by just a length symbol.  Lengths 11..257 are represented as a symbol and
00386  *   some number of extra bits that are added as an integer to the base length
00387  *   of the length symbol.  The number of extra bits is determined by the base
00388  *   length symbol.  These are in the static arrays below, lens[] for the base
00389  *   lengths and lext[] for the corresponding number of extra bits.
00390  *
00391  * - The reason that 258 gets its own symbol is that the longest length is used
00392  *   often in highly redundant files.  Note that 258 can also be coded as the
00393  *   base value 227 plus the maximum extra value of 31.  While a good deflate
00394  *   should never do this, it is not an error, and should be decoded properly.
00395  *
00396  * - If a length is decoded, including its extra bits if any, then it is
00397  *   followed a distance code.  There are up to 30 distance symbols.  Again
00398  *   there are many more possible distances (1..32768), so extra bits are added
00399  *   to a base value represented by the symbol.  The distances 1..4 get their
00400  *   own symbol, but the rest require extra bits.  The base distances and
00401  *   corresponding number of extra bits are below in the static arrays dist[]
00402  *   and dext[].
00403  *
00404  * - Literal bytes are simply written to the output.  A length/distance pair is
00405  *   an instruction to copy previously uncompressed bytes to the output.  The
00406  *   copy is from distance bytes back in the output stream, copying for length
00407  *   bytes.
00408  *
00409  * - Distances pointing before the beginning of the output data are not
00410  *   permitted.
00411  *
00412  * - Overlapped copies, where the length is greater than the distance, are
00413  *   allowed and common.  For example, a distance of one and a length of 258
00414  *   simply copies the last byte 258 times.  A distance of four and a length of
00415  *   twelve copies the last four bytes three times.  A simple forward copy
00416  *   ignoring whether the length is greater than the distance or not implements
00417  *   this correctly.  You should not use memcpy() since its behavior is not
00418  *   defined for overlapped arrays.  You should not use memmove() or bcopy()
00419  *   since though their behavior -is- defined for overlapping arrays, it is
00420  *   defined to do the wrong thing in this case.
00421  */
00422 local int codes(struct state *s,
00423                 struct huffman *lencode,
00424                 struct huffman *distcode)
00425 {
00426     int symbol;         /* decoded symbol */
00427     int len;            /* length for copy */
00428     unsigned dist;      /* distance for copy */
00429     static const short lens[29] = { /* Size base for length codes 257..285 */
00430         3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
00431         35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258};
00432     static const short lext[29] = { /* Extra bits for length codes 257..285 */
00433         0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
00434         3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0};
00435     static const short dists[30] = { /* Offset base for distance codes 0..29 */
00436         1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
00437         257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
00438         8193, 12289, 16385, 24577};
00439     static const short dext[30] = { /* Extra bits for distance codes 0..29 */
00440         0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
00441         7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
00442         12, 12, 13, 13};
00443 
00444     /* decode literals and length/distance pairs */
00445     do {
00446         symbol = decode(s, lencode);
00447         if (symbol < 0) return symbol;  /* invalid symbol */
00448         if (symbol < 256) {             /* literal: symbol is the byte */
00449             /* write out the literal */
00450             if (s->out != NIL) {
00451                 if (s->outcnt == s->outlen) return 1;
00452                 s->out[s->outcnt] = symbol;
00453             }
00454             s->outcnt++;
00455         }
00456         else if (symbol > 256) {        /* length */
00457             /* get and compute length */
00458             symbol -= 257;
00459             if (symbol >= 29) return -10;       /* invalid fixed code */
00460             len = lens[symbol] + bits(s, lext[symbol]);
00461 
00462             /* get and check distance */
00463             symbol = decode(s, distcode);
00464             if (symbol < 0) return symbol;      /* invalid symbol */
00465             dist = dists[symbol] + bits(s, dext[symbol]);
00466 #ifndef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
00467             if (dist > s->outcnt)
00468                 return -11;     /* distance too far back */
00469 #endif
00470 
00471             /* copy length bytes from distance bytes back */
00472             if (s->out != NIL) {
00473                 if (s->outcnt + len > s->outlen) return 1;
00474                 while (len--) {
00475                     s->out[s->outcnt] =
00476 #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
00477                         dist > s->outcnt ? 0 :
00478 #endif
00479                         s->out[s->outcnt - dist];
00480                     s->outcnt++;
00481                 }
00482             }
00483             else
00484                 s->outcnt += len;
00485         }
00486     } while (symbol != 256);            /* end of block symbol */
00487 
00488     /* done with a valid fixed or dynamic block */
00489     return 0;
00490 }
00491 
00492 /*
00493  * Process a fixed codes block.
00494  *
00495  * Format notes:
00496  *
00497  * - This block type can be useful for compressing small amounts of data for
00498  *   which the size of the code descriptions in a dynamic block exceeds the
00499  *   benefit of custom codes for that block.  For fixed codes, no bits are
00500  *   spent on code descriptions.  Instead the code lengths for literal/length
00501  *   codes and distance codes are fixed.  The specific lengths for each symbol
00502  *   can be seen in the "for" loops below.
00503  *
00504  * - The literal/length code is complete, but has two symbols that are invalid
00505  *   and should result in an error if received.  This cannot be implemented
00506  *   simply as an incomplete code since those two symbols are in the "middle"
00507  *   of the code.  They are eight bits long and the longest literal/length\
00508  *   code is nine bits.  Therefore the code must be constructed with those
00509  *   symbols, and the invalid symbols must be detected after decoding.
00510  *
00511  * - The fixed distance codes also have two invalid symbols that should result
00512  *   in an error if received.  Since all of the distance codes are the same
00513  *   length, this can be implemented as an incomplete code.  Then the invalid
00514  *   codes are detected while decoding.
00515  */
00516 local int fixed(struct state *s)
00517 {
00518     static int virgin = 1;
00519     static short lencnt[MAXBITS+1], lensym[FIXLCODES];
00520     static short distcnt[MAXBITS+1], distsym[MAXDCODES];
00521     static struct huffman lencode, distcode;
00522 
00523     /* build fixed huffman tables if first call (may not be thread safe) */
00524     if (virgin) {
00525         int symbol;
00526         short lengths[FIXLCODES];
00527 
00528         /* literal/length table */
00529         for (symbol = 0; symbol < 144; symbol++)
00530             lengths[symbol] = 8;
00531         for (; symbol < 256; symbol++)
00532             lengths[symbol] = 9;
00533         for (; symbol < 280; symbol++)
00534             lengths[symbol] = 7;
00535         for (; symbol < FIXLCODES; symbol++)
00536             lengths[symbol] = 8;
00537         construct(&lencode, lengths, FIXLCODES);
00538 
00539         /* distance table */
00540         for (symbol = 0; symbol < MAXDCODES; symbol++)
00541             lengths[symbol] = 5;
00542         construct(&distcode, lengths, MAXDCODES);
00543 
00544         /* construct lencode and distcode */
00545         lencode.count = lencnt;
00546         lencode.symbol = lensym;
00547         distcode.count = distcnt;
00548         distcode.symbol = distsym;
00549 
00550         /* do this just once */
00551         virgin = 0;
00552     }
00553 
00554     /* decode data until end-of-block code */
00555     return codes(s, &lencode, &distcode);
00556 }
00557 
00558 /*
00559  * Process a dynamic codes block.
00560  *
00561  * Format notes:
00562  *
00563  * - A dynamic block starts with a description of the literal/length and
00564  *   distance codes for that block.  New dynamic blocks allow the compressor to
00565  *   rapidly adapt to changing data with new codes optimized for that data.
00566  *
00567  * - The codes used by the deflate format are "canonical", which means that
00568  *   the actual bits of the codes are generated in an unambiguous way simply
00569  *   from the number of bits in each code.  Therefore the code descriptions
00570  *   are simply a list of code lengths for each symbol.
00571  *
00572  * - The code lengths are stored in order for the symbols, so lengths are
00573  *   provided for each of the literal/length symbols, and for each of the
00574  *   distance symbols.
00575  *
00576  * - If a symbol is not used in the block, this is represented by a zero as
00577  *   as the code length.  This does not mean a zero-length code, but rather
00578  *   that no code should be created for this symbol.  There is no way in the
00579  *   deflate format to represent a zero-length code.
00580  *
00581  * - The maximum number of bits in a code is 15, so the possible lengths for
00582  *   any code are 1..15.
00583  *
00584  * - The fact that a length of zero is not permitted for a code has an
00585  *   interesting consequence.  Normally if only one symbol is used for a given
00586  *   code, then in fact that code could be represented with zero bits.  However
00587  *   in deflate, that code has to be at least one bit.  So for example, if
00588  *   only a single distance base symbol appears in a block, then it will be
00589  *   represented by a single code of length one, in particular one 0 bit.  This
00590  *   is an incomplete code, since if a 1 bit is received, it has no meaning,
00591  *   and should result in an error.  So incomplete distance codes of one symbol
00592  *   should be permitted, and the receipt of invalid codes should be handled.
00593  *
00594  * - It is also possible to have a single literal/length code, but that code
00595  *   must be the end-of-block code, since every dynamic block has one.  This
00596  *   is not the most efficient way to create an empty block (an empty fixed
00597  *   block is fewer bits), but it is allowed by the format.  So incomplete
00598  *   literal/length codes of one symbol should also be permitted.
00599  *
00600  * - If there are only literal codes and no lengths, then there are no distance
00601  *   codes.  This is represented by one distance code with zero bits.
00602  *
00603  * - The list of up to 286 length/literal lengths and up to 30 distance lengths
00604  *   are themselves compressed using Huffman codes and run-length encoding.  In
00605  *   the list of code lengths, a 0 symbol means no code, a 1..15 symbol means
00606  *   that length, and the symbols 16, 17, and 18 are run-length instructions.
00607  *   Each of 16, 17, and 18 are follwed by extra bits to define the length of
00608  *   the run.  16 copies the last length 3 to 6 times.  17 represents 3 to 10
00609  *   zero lengths, and 18 represents 11 to 138 zero lengths.  Unused symbols
00610  *   are common, hence the special coding for zero lengths.
00611  *
00612  * - The symbols for 0..18 are Huffman coded, and so that code must be
00613  *   described first.  This is simply a sequence of up to 19 three-bit values
00614  *   representing no code (0) or the code length for that symbol (1..7).
00615  *
00616  * - A dynamic block starts with three fixed-size counts from which is computed
00617  *   the number of literal/length code lengths, the number of distance code
00618  *   lengths, and the number of code length code lengths (ok, you come up with
00619  *   a better name!) in the code descriptions.  For the literal/length and
00620  *   distance codes, lengths after those provided are considered zero, i.e. no
00621  *   code.  The code length code lengths are received in a permuted order (see
00622  *   the order[] array below) to make a short code length code length list more
00623  *   likely.  As it turns out, very short and very long codes are less likely
00624  *   to be seen in a dynamic code description, hence what may appear initially
00625  *   to be a peculiar ordering.
00626  *
00627  * - Given the number of literal/length code lengths (nlen) and distance code
00628  *   lengths (ndist), then they are treated as one long list of nlen + ndist
00629  *   code lengths.  Therefore run-length coding can and often does cross the
00630  *   boundary between the two sets of lengths.
00631  *
00632  * - So to summarize, the code description at the start of a dynamic block is
00633  *   three counts for the number of code lengths for the literal/length codes,
00634  *   the distance codes, and the code length codes.  This is followed by the
00635  *   code length code lengths, three bits each.  This is used to construct the
00636  *   code length code which is used to read the remainder of the lengths.  Then
00637  *   the literal/length code lengths and distance lengths are read as a single
00638  *   set of lengths using the code length codes.  Codes are constructed from
00639  *   the resulting two sets of lengths, and then finally you can start
00640  *   decoding actual compressed data in the block.
00641  *
00642  * - For reference, a "typical" size for the code description in a dynamic
00643  *   block is around 80 bytes.
00644  */
00645 local int dynamic(struct state *s)
00646 {
00647     int nlen, ndist, ncode;             /* number of lengths in descriptor */
00648     int index;                          /* index of lengths[] */
00649     int err;                            /* construct() return value */
00650     short lengths[MAXCODES];            /* descriptor code lengths */
00651     short lencnt[MAXBITS+1], lensym[MAXLCODES];         /* lencode memory */
00652     short distcnt[MAXBITS+1], distsym[MAXDCODES];       /* distcode memory */
00653     struct huffman lencode, distcode;   /* length and distance codes */
00654     static const short order[19] =      /* permutation of code length codes */
00655         {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
00656 
00657     /* construct lencode and distcode */
00658     lencode.count = lencnt;
00659     lencode.symbol = lensym;
00660     distcode.count = distcnt;
00661     distcode.symbol = distsym;
00662 
00663     /* get number of lengths in each table, check lengths */
00664     nlen = bits(s, 5) + 257;
00665     ndist = bits(s, 5) + 1;
00666     ncode = bits(s, 4) + 4;
00667     if (nlen > MAXLCODES || ndist > MAXDCODES)
00668         return -3;                      /* bad counts */
00669 
00670     /* read code length code lengths (really), missing lengths are zero */
00671     for (index = 0; index < ncode; index++)
00672         lengths[order[index]] = bits(s, 3);
00673     for (; index < 19; index++)
00674         lengths[order[index]] = 0;
00675 
00676     /* build huffman table for code lengths codes (use lencode temporarily) */
00677     err = construct(&lencode, lengths, 19);
00678     if (err != 0) return -4;            /* require complete code set here */
00679 
00680     /* read length/literal and distance code length tables */
00681     index = 0;
00682     while (index < nlen + ndist) {
00683         int symbol;             /* decoded value */
00684         int len;                /* last length to repeat */
00685 
00686         symbol = decode(s, &lencode);
00687         if (symbol < 16)                /* length in 0..15 */
00688             lengths[index++] = symbol;
00689         else {                          /* repeat instruction */
00690             len = 0;                    /* assume repeating zeros */
00691             if (symbol == 16) {         /* repeat last length 3..6 times */
00692                 if (index == 0) return -5;      /* no last length! */
00693                 len = lengths[index - 1];       /* last length */
00694                 symbol = 3 + bits(s, 2);
00695             }
00696             else if (symbol == 17)      /* repeat zero 3..10 times */
00697                 symbol = 3 + bits(s, 3);
00698             else                        /* == 18, repeat zero 11..138 times */
00699                 symbol = 11 + bits(s, 7);
00700             if (index + symbol > nlen + ndist)
00701                 return -6;              /* too many lengths! */
00702             while (symbol--)            /* repeat last or zero symbol times */
00703                 lengths[index++] = len;
00704         }
00705     }
00706 
00707     /* check for end-of-block code -- there better be one! */
00708     if (lengths[256] == 0)
00709         return -9;
00710 
00711     /* build huffman table for literal/length codes */
00712     err = construct(&lencode, lengths, nlen);
00713     if (err < 0 || (err > 0 && nlen - lencode.count[0] != 1))
00714         return -7;      /* only allow incomplete codes if just one code */
00715 
00716     /* build huffman table for distance codes */
00717     err = construct(&distcode, lengths + nlen, ndist);
00718     if (err < 0 || (err > 0 && ndist - distcode.count[0] != 1))
00719         return -8;      /* only allow incomplete codes if just one code */
00720 
00721     /* decode data until end-of-block code */
00722     return codes(s, &lencode, &distcode);
00723 }
00724 
00725 /*
00726  * Inflate source to dest.  On return, destlen and sourcelen are updated to the
00727  * size of the uncompressed data and the size of the deflate data respectively.
00728  * On success, the return value of puff() is zero.  If there is an error in the
00729  * source data, i.e. it is not in the deflate format, then a negative value is
00730  * returned.  If there is not enough input available or there is not enough
00731  * output space, then a positive error is returned.  In that case, destlen and
00732  * sourcelen are not updated to facilitate retrying from the beginning with the
00733  * provision of more input data or more output space.  In the case of invalid
00734  * inflate data (a negative error), the dest and source pointers are updated to
00735  * facilitate the debugging of deflators.
00736  *
00737  * puff() also has a mode to determine the size of the uncompressed output with
00738  * no output written.  For this dest must be (unsigned char *)0.  In this case,
00739  * the input value of *destlen is ignored, and on return *destlen is set to the
00740  * size of the uncompressed output.
00741  *
00742  * The return codes are:
00743  *
00744  *   2:  available inflate data did not terminate
00745  *   1:  output space exhausted before completing inflate
00746  *   0:  successful inflate
00747  *  -1:  invalid block type (type == 3)
00748  *  -2:  stored block length did not match one's complement
00749  *  -3:  dynamic block code description: too many length or distance codes
00750  *  -4:  dynamic block code description: code lengths codes incomplete
00751  *  -5:  dynamic block code description: repeat lengths with no first length
00752  *  -6:  dynamic block code description: repeat more than specified lengths
00753  *  -7:  dynamic block code description: invalid literal/length code lengths
00754  *  -8:  dynamic block code description: invalid distance code lengths
00755  *  -9:  dynamic block code description: missing end-of-block code
00756  * -10:  invalid literal/length or distance code in fixed or dynamic block
00757  * -11:  distance is too far back in fixed or dynamic block
00758  *
00759  * Format notes:
00760  *
00761  * - Three bits are read for each block to determine the kind of block and
00762  *   whether or not it is the last block.  Then the block is decoded and the
00763  *   process repeated if it was not the last block.
00764  *
00765  * - The leftover bits in the last byte of the deflate data after the last
00766  *   block (if it was a fixed or dynamic block) are undefined and have no
00767  *   expected values to check.
00768  */
00769 int puff(unsigned char *dest,           /* pointer to destination pointer */
00770          unsigned long *destlen,        /* amount of output space */
00771          unsigned char *source,         /* pointer to source data pointer */
00772          unsigned long *sourcelen)      /* amount of input available */
00773 {
00774     struct state s;             /* input/output state */
00775     int last, type;             /* block information */
00776     int err;                    /* return value */
00777 
00778     /* initialize output state */
00779     s.out = dest;
00780     s.outlen = *destlen;                /* ignored if dest is NIL */
00781     s.outcnt = 0;
00782 
00783     /* initialize input state */
00784     s.in = source;
00785     s.inlen = *sourcelen;
00786     s.incnt = 0;
00787     s.bitbuf = 0;
00788     s.bitcnt = 0;
00789 
00790     /* return if bits() or decode() tries to read past available input */
00791     if (setjmp(s.env) != 0)             /* if came back here via longjmp() */
00792         err = 2;                        /* then skip do-loop, return error */
00793     else {
00794         /* process blocks until last block or error */
00795         do {
00796             last = bits(&s, 1);         /* one if last block */
00797             type = bits(&s, 2);         /* block type 0..3 */
00798             err = type == 0 ? stored(&s) :
00799                   (type == 1 ? fixed(&s) :
00800                    (type == 2 ? dynamic(&s) :
00801                     -1));               /* type == 3, invalid */
00802             if (err != 0) break;        /* return with error */
00803         } while (!last);
00804     }
00805 
00806     /* update the lengths and return */
00807     if (err <= 0) {
00808         *destlen = s.outcnt;
00809         *sourcelen = s.incnt;
00810     }
00811     return err;
00812 }
00813 
00814 #ifdef TEST
00815 /* Examples of how to use puff().
00816 
00817    Usage: puff [-w] [-nnn] file
00818           ... | puff [-w] [-nnn]
00819 
00820    where file is the input file with deflate data, nnn is the number of bytes
00821    of input to skip before inflating (e.g. to skip a zlib or gzip header), and
00822    -w is used to write the decompressed data to stdout */
00823 
00824 #include <stdio.h>
00825 #include <stdlib.h>
00826 
00827 /* Return size times approximately the cube root of 2, keeping the result as 1,
00828    3, or 5 times a power of 2 -- the result is always > size, until the result
00829    is the maximum value of an unsigned long, where it remains.  This is useful
00830    to keep reallocations less than ~33% over the actual data. */
00831 local size_t bythirds(size_t size)
00832 {
00833     int n;
00834     size_t m;
00835 
00836     m = size;
00837     for (n = 0; m; n++)
00838         m >>= 1;
00839     if (n < 3)
00840         return size + 1;
00841     n -= 3;
00842     m = size >> n;
00843     m += m == 6 ? 2 : 1;
00844     m <<= n;
00845     return m > size ? m : (size_t)(-1);
00846 }
00847 
00848 /* Read the input file *name, or stdin if name is NULL, into allocated memory.
00849    Reallocate to larger buffers until the entire file is read in.  Return a
00850    pointer to the allocated data, or NULL if there was a memory allocation
00851    failure.  *len is the number of bytes of data read from the input file (even
00852    if load() returns NULL).  If the input file was empty or could not be opened
00853    or read, *len is zero. */
00854 local void *load(char *name, size_t *len)
00855 {
00856     size_t size;
00857     void *buf, *swap;
00858     FILE *in;
00859 
00860     *len = 0;
00861     buf = malloc(size = 4096);
00862     if (buf == NULL)
00863         return NULL;
00864     in = name == NULL ? stdin : fopen(name, "rb");
00865     if (in != NULL) {
00866         for (;;) {
00867             *len += fread((char *)buf + *len, 1, size - *len, in);
00868             if (*len < size) break;
00869             size = bythirds(size);
00870             if (size == *len || (swap = realloc(buf, size)) == NULL) {
00871                 free(buf);
00872                 buf = NULL;
00873                 break;
00874             }
00875             buf = swap;
00876         }
00877         fclose(in);
00878     }
00879     return buf;
00880 }
00881 
00882 int main(int argc, char **argv)
00883 {
00884     int ret, put = 0;
00885     unsigned skip = 0;
00886     char *arg, *name = NULL;
00887     unsigned char *source = NULL, *dest;
00888     size_t len = 0;
00889     unsigned long sourcelen, destlen;
00890 
00891     /* process arguments */
00892     while (arg = *++argv, --argc)
00893         if (arg[0] == '-') {
00894             if (arg[1] == 'w' && arg[2] == 0)
00895                 put = 1;
00896             else if (arg[1] >= '0' && arg[1] <= '9')
00897                 skip = (unsigned)atoi(arg + 1);
00898             else {
00899                 fprintf(stderr, "invalid option %s\n", arg);
00900                 return 3;
00901             }
00902         }
00903         else if (name != NULL) {
00904             fprintf(stderr, "only one file name allowed\n");
00905             return 3;
00906         }
00907         else
00908             name = arg;
00909     source = load(name, &len);
00910     if (source == NULL) {
00911         fprintf(stderr, "memory allocation failure\n");
00912         return 4;
00913     }
00914     if (len == 0) {
00915         fprintf(stderr, "could not read %s, or it was empty\n",
00916                 name == NULL ? "<stdin>" : name);
00917         free(source);
00918         return 3;
00919     }
00920     if (skip >= len) {
00921         fprintf(stderr, "skip request of %d leaves no input\n", skip);
00922         free(source);
00923         return 3;
00924     }
00925 
00926     /* test inflate data with offset skip */
00927     len -= skip;
00928     sourcelen = (unsigned long)len;
00929     ret = puff(NIL, &destlen, source + skip, &sourcelen);
00930     if (ret)
00931         fprintf(stderr, "puff() failed with return code %d\n", ret);
00932     else {
00933         fprintf(stderr, "puff() succeeded uncompressing %lu bytes\n", destlen);
00934         if (sourcelen < len) fprintf(stderr, "%lu compressed bytes unused\n",
00935                                      len - sourcelen);
00936     }
00937 
00938     /* if requested, inflate again and write decompressd data to stdout */
00939     if (put) {
00940         dest = malloc(destlen);
00941         if (dest == NULL) {
00942             fprintf(stderr, "memory allocation failure\n");
00943             free(source);
00944             return 4;
00945         }
00946         puff(dest, &destlen, source + skip, &sourcelen);
00947         fwrite(dest, 1, destlen, stdout);
00948         free(dest);
00949     }
00950 
00951     /* clean up */
00952     free(source);
00953     return ret;
00954 }
00955 #endif

Generated on Wed Oct 20 2010 11:12:17 for APBS by  doxygen 1.7.2