Annotation of pgp/src/zinflate.c, revision 1.1.1.2

1.1.1.2 ! root        1: /* inflate.c -- Not copyrighted 1992 by Mark Adler
        !             2:    version c4, 15 April 1992 */
        !             3: 
        !             4: 
        !             5: /* You can do whatever you like with this source file, though I would
        !             6:    prefer that if you modify it and redistribute it that you include
        !             7:    comments to that effect with your name and the date.  Thank you.
        !             8: 
        !             9:    History:
        !            10:    vers    date          who           what
        !            11:    ----  ---------  --------------  ------------------------------------
        !            12:     a    ~~ Feb 92  M. Adler        used full (large, one-step) lookup table
        !            13:     b1   21 Mar 92  M. Adler        first version with partial lookup tables
        !            14:     b2   21 Mar 92  M. Adler        fixed bug in fixed-code blocks
        !            15:     b3   22 Mar 92  M. Adler        sped up match copies, cleaned up some
        !            16:     b4   25 Mar 92  M. Adler        added prototypes; removed window[] (now
        !            17:                                     is the responsibility of unzip.h--also
        !            18:                                     changed name to slide[]), so needs diffs
        !            19:                                     for unzip.c and unzip.h (this allows
        !            20:                                     compiling in the small model on MSDOS);
        !            21:                                     fixed cast of q in huft_build();
        !            22:     b5   26 Mar 92  M. Adler        got rid of unintended macro recursion.
        !            23:     b6   27 Mar 92  M. Adler        got rid of nextbyte() routine.  fixed
        !            24:                                     bug in inflate_fixed().
        !            25:     c1   30 Mar 92  M. Adler        removed lbits, dbits environment variables.
        !            26:                                     changed BMAX to 16 for explode.  Removed
        !            27:                                     OUTB usage, and replaced it with flush()--
        !            28:                                     this was a 20% speed improvement!  Added
        !            29:                                     an explode.c (to replace unimplode.c) that
        !            30:                                     uses the huft routines here.  Removed
        !            31:                                     register union.
        !            32:     c2    4 Apr 92  M. Adler        fixed bug for file sizes a multiple of 32k.
        !            33:     c3   10 Apr 92  M. Adler        reduced memory of code tables made by
        !            34:                                     huft_build significantly (factor of two to
        !            35:                                     three).
        !            36:     c4   15 Apr 92  M. Adler        added NOMEMCPY do kill use of memcpy().
        !            37:                                     worked around a Turbo C optimization bug.
        !            38:     c5   21 Apr 92  M. Adler        added the WSIZE #define to allow reducing
        !            39:                                     the 32K window size for specialized
        !            40:                                     applications.
        !            41:     c6   27 May 92  J.loup Gailly   Adapted for pgp
        !            42:  */
        !            43: 
        !            44: 
        !            45: /*
        !            46:    Inflate deflated (PKZIP's method 8 compressed) data.  The compression
        !            47:    method searches for as much of the current string of bytes (up to a
        !            48:    length of 258) in the previous 32K bytes.  If it doesn't find any
        !            49:    matches (of at least length 3), it codes the next byte.  Otherwise, it
        !            50:    codes the length of the matched string and its distance backwards from
        !            51:    the current position.  There is a single Huffman code that codes both
        !            52:    single bytes (called "literals") and match lengths.  A second Huffman
        !            53:    code codes the distance information, which follows a length code.  Each
        !            54:    length or distance code actually represents a base value and a number
        !            55:    of "extra" (sometimes zero) bits to get to add to the base value.  At
        !            56:    the end of each deflated block is a special end-of-block (EOB) literal/
        !            57:    length code.  The decoding process is basically: get a literal/length
        !            58:    code; if EOB then done; if a literal, emit the decoded byte; if a
        !            59:    length then get the distance and emit the referred-to bytes from the
        !            60:    sliding window of previously emitted data.
        !            61: 
        !            62:    There are (currently) three kinds of inflate blocks: stored, fixed, and
        !            63:    dynamic.  The compressor deals with some chunk of data at a time, and
        !            64:    decides which method to use on a chunk-by-chunk basis.  A chunk might
        !            65:    typically be 32K or 64K.  If the chunk is uncompressible, then the
        !            66:    "stored" method is used.  In this case, the bytes are simply stored as
        !            67:    is, eight bits per byte, with none of the above coding.  The bytes are
        !            68:    preceded by a count, since there is no longer an EOB code.
        !            69: 
        !            70:    If the data is compressible, then either the fixed or dynamic methods
        !            71:    are used.  In the dynamic method, the compressed data is preceded by
        !            72:    an encoding of the literal/length and distance Huffman codes that are
        !            73:    to be used to decode this block.  The representation is itself Huffman
        !            74:    coded, and so is preceded by a description of that code.  These code
        !            75:    descriptions take up a little space, and so for small blocks, there is
        !            76:    a predefined set of codes, called the fixed codes.  The fixed method is
        !            77:    used if the block codes up smaller that way (usually for quite small
        !            78:    chunks), otherwise the dynamic method is used.  In the latter case, the
        !            79:    codes are customized to the probabilities in the current block, and so
        !            80:    can code it much better than the pre-determined fixed codes.
        !            81:  
        !            82:    The Huffman codes themselves are decoded using a mutli-level table
        !            83:    lookup, in order to maximize the speed of decoding plus the speed of
        !            84:    building the decoding tables.  See the comments below that precede the
        !            85:    lbits and dbits tuning parameters.
        !            86:  */
        !            87: 
        !            88: 
        !            89: /*
        !            90:    Notes beyond the 1.93a appnote.txt:
        !            91: 
        !            92:    1. Distance pointers never point before the beginning of the output
        !            93:       stream.
        !            94:    2. Distance pointers can point back across blocks, up to 32k away.
        !            95:    3. There is an implied maximum of 7 bits for the bit length table and
        !            96:       15 bits for the actual data.
        !            97:    4. If only one code exists, then it is encoded using one bit.  (Zero
        !            98:       would be more efficient, but perhaps a little confusing.)  If two
        !            99:       codes exist, they are coded using one bit each (0 and 1).
        !           100:    5. There is no way of sending zero distance codes--a dummy must be
        !           101:       sent if there are none.  (History: a pre 2.0 version of PKZIP would
        !           102:       store blocks with no distance codes, but this was discovered to be
        !           103:       too harsh a criterion.)
        !           104:    6. There are up to 286 literal/length codes.  Code 256 represents the
        !           105:       end-of-block.  Note however that the static length tree defines
        !           106:       288 codes just to fill out the Huffman codes.  Codes 286 and 287
        !           107:       cannot be used though, since there is no length base or extra bits
        !           108:       defined for them.  Similarily, there are up to 30 distance codes.
        !           109:       However, static trees define 32 codes (all 5 bits) to fill out the
        !           110:       Huffman codes, but the last two had better not show up in the data.
        !           111:    7. Unzip can check dynamic huffman blocks for complete code sets.
        !           112:       The exception is that a single code would not be complete (see #4).
        !           113:    8. The five bits following the block type is really the number of
        !           114:       literal codes sent minus 257.
        !           115:    9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
        !           116:       (1+6+6).  Therefore, to output three times the length, you output
        !           117:       three codes (1+1+1), whereas to output four times the same length,
        !           118:       you only need two codes (1+3).  Hmm.
        !           119:   10. In the tree reconstruction algorithm, Code = Code + Increment
        !           120:       only if BitLength(i) is not zero.  (Pretty obvious.)
        !           121:   11. Correction: 4 Bits: # of Bit Length codes - 4     (4 - 19)
        !           122:   12. Note: length code 284 can represent 227-258, but length code 285
        !           123:       really is 258.  The last length deserves its own, short code
        !           124:       since it gets used a lot in very redundant files.  The length
        !           125:       258 is special since 258 - 3 (the min match length) is 255.
        !           126:   13. The literal/length and distance code bit lengths are read as a
        !           127:       single stream of lengths.  It is possible (and advantageous) for
        !           128:       a repeat code (16, 17, or 18) to go across the boundary between
        !           129:       the two sets of lengths.
        !           130:  */
        !           131: 
        !           132: #include "zunzip.h"
        !           133: #define OF __
        !           134: 
        !           135: #ifndef WSIZE
        !           136: #  define WSIZE 8192
        !           137: /* window size--must be a power of two, <= 32K, and equal to that of zip.
        !           138:  * On 16 bit machines (MSDOS), WSIZE must be <= 16K (32K is possible
        !           139:  * with a few hacks, see the zip archiver.
        !           140:  */
        !           141: #endif
        !           142: 
        !           143: #ifdef DYN_ALLOC
        !           144:   extern char *window;
        !           145: #else
        !           146:   extern char window[];
        !           147: #endif
        !           148: #define slide window
        !           149: 
        !           150: /* Huffman code lookup table entry--this entry is four bytes for machines
        !           151:    that have 16-bit pointers (e.g. PC's in the small or medium model).
        !           152:    Valid extra bits are 0..13.  e == 15 is EOB (end of block), e == 16
        !           153:    means that v is a literal, 16 < e < 32 means that v is a pointer to
        !           154:    the next table, which codes e - 16 bits, and lastly e == 99 indicates
        !           155:    an unused code.  If a code with e == 99 is looked up, this implies an
        !           156:    error in the data. */
        !           157: struct huft {
        !           158:   byte e;               /* number of extra bits or operation */
        !           159:   byte b;               /* number of bits in this code or subcode */
        !           160:   union {
        !           161:     UWORD n;            /* literal, length base, or distance base */
        !           162:     struct huft *t;     /* pointer to next level of table */
        !           163:   } v;
        !           164: };
        !           165: 
        !           166: 
        !           167: /* Function prototypes */
        !           168: int huft_build OF((unsigned *, unsigned, unsigned, UWORD *, UWORD *,
        !           169:                    struct huft **, int *));
        !           170: int huft_free OF((struct huft *));
        !           171: void flush OF((unsigned));
        !           172: int inflate_codes OF((struct huft *, struct huft *, int, int));
        !           173: int inflate_stored OF((void));
        !           174: int inflate_fixed OF((void));
        !           175: int inflate_dynamic OF((void));
        !           176: int inflate_block OF((int *));
        !           177: int inflate_entry OF((void));
        !           178: void inflate OF((void));
        !           179: 
        !           180: 
        !           181: /* The inflate algorithm uses a sliding 32K byte window on the uncompressed
        !           182:    stream to find repeated byte strings.  This is implemented here as a
        !           183:    circular buffer.  The index is updated simply by incrementing and then
        !           184:    and'ing with 0x7fff (32K-1). */
        !           185: /* It is left to other modules to supply the 32K area.  It is assumed
        !           186:    to be usable as if it were declared "byte slide[32768];" or as just
        !           187:    "byte *slide;" and then malloc'ed in the latter case.  The definition
        !           188:    must be in unzip.h, included above. */
        !           189: unsigned wp;            /* current position in slide */
        !           190: 
        !           191: 
        !           192: /* Tables for deflate from PKZIP's appnote.txt. */
        !           193: static unsigned border[] = {    /* Order of the bit length code lengths */
        !           194:         16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
        !           195: static UWORD cplens[] = {       /* Copy lengths for literal codes 257..285 */
        !           196:         3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
        !           197:         35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
        !           198:         /* note: see note #13 above about the 258 in this list. */
        !           199: static UWORD cplext[] = {       /* Extra bits for literal codes 257..285 */
        !           200:         0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
        !           201:         3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */
        !           202: static UWORD cpdist[] = {       /* Copy offsets for distance codes 0..29 */
        !           203:         1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
        !           204:         257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
        !           205:         8193, 12289, 16385, 24577};
        !           206: static UWORD cpdext[] = {       /* Extra bits for distance codes */
        !           207:         0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
        !           208:         7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
        !           209:         12, 12, 13, 13};
        !           210: 
        !           211: 
        !           212: 
        !           213: /* Macros for inflate() bit peeking and grabbing.
        !           214:    The usage is:
        !           215:    
        !           216:         NEEDBITS(j)
        !           217:         x = b & mask_bits[j];
        !           218:         DUMPBITS(j)
        !           219: 
        !           220:    where NEEDBITS makes sure that b has at least j bits in it, and
        !           221:    DUMPBITS removes the bits from b.  The macros use the variable k
        !           222:    for the number of bits in b.  Normally, b and k are register
        !           223:    variables for speed, and are initialized at the begining of a
        !           224:    routine that uses these macros from a global bit buffer and count.
        !           225: 
        !           226:    If we assume that EOB will be the longest code, then we will never
        !           227:    ask for bits with NEEDBITS that are beyond the end of the stream.
        !           228:    So, NEEDBITS should not read any more bytes than are needed to
        !           229:    meet the request.  Then no bytes need to be "returned" to the buffer
        !           230:    at the end of the last block.
        !           231: 
        !           232:    However, this assumption is not true for fixed blocks--the EOB code
        !           233:    is 7 bits, but the other literal/length codes can be 8 or 9 bits.
        !           234:    (Why PK made the EOB code, which can only occur once in a block,
        !           235:    the *shortest* code in the set, I'll never know.)  However, by
        !           236:    making the first table have a lookup of seven bits, the EOB code
        !           237:    will be found in that first lookup, and so will not require that too
        !           238:    many bits be pulled from the stream.
        !           239:  */
        !           240: 
        !           241: ULONG bb;                       /* bit buffer */
        !           242: unsigned bk;                    /* bits in bit buffer */
        !           243: 
        !           244: UWORD bytebuf;
        !           245: #define NEXTBYTE    (ReadByte(&bytebuf), bytebuf)
        !           246: #define NEEDBITS(n) {while(k<(n)){b|=((ULONG)NEXTBYTE)<<k;k+=8;}}
        !           247: #define DUMPBITS(n) {b>>=(n);k-=(n);}
        !           248: 
        !           249: 
        !           250: /*
        !           251:    Huffman code decoding is performed using a multi-level table lookup.
        !           252:    The fastest way to decode is to simply build a lookup table whose
        !           253:    size is determined by the longest code.  However, the time it takes
        !           254:    to build this table can also be a factor if the data being decoded
        !           255:    is not very long.  The most common codes are necessarily the
        !           256:    shortest codes, so those codes dominate the decoding time, and hence
        !           257:    the speed.  The idea is you can have a shorter table that decodes the
        !           258:    shorter, more probable codes, and then point to subsidiary tables for
        !           259:    the longer codes.  The time it costs to decode the longer codes is
        !           260:    then traded against the time it takes to make longer tables.
        !           261: 
        !           262:    This results of this trade are in the variables lbits and dbits
        !           263:    below.  lbits is the number of bits the first level table for literal/
        !           264:    length codes can decode in one step, and dbits is the same thing for
        !           265:    the distance codes.  Subsequent tables are also less than or equal to
        !           266:    those sizes.  These values may be adjusted either when all of the
        !           267:    codes are shorter than that, in which case the longest code length in
        !           268:    bits is used, or when the shortest code is *longer* than the requested
        !           269:    table size, in which case the length of the shortest code in bits is
        !           270:    used.
        !           271: 
        !           272:    There are two different values for the two tables, since they code a
        !           273:    different number of possibilities each.  The literal/length table
        !           274:    codes 286 possible values, or in a flat code, a little over eight
        !           275:    bits.  The distance table codes 30 possible values, or a little less
        !           276:    than five bits, flat.  The optimum values for speed end up being
        !           277:    about one bit more than those, so lbits is 8+1 and dbits is 5+1.
        !           278:    The optimum values may differ though from machine to machine, and
        !           279:    possibly even between compilers.  Your mileage may vary.
        !           280:  */
        !           281: 
        !           282: 
        !           283: int lbits = 9;          /* bits in base literal/length lookup table */
        !           284: int dbits = 6;          /* bits in base distance lookup table */
        !           285: 
        !           286: 
        !           287: /* If BMAX needs to be larger than 16, then h and x[] should be ULONG. */
        !           288: #define BMAX 16         /* maximum bit length of any code (16 for explode) */
        !           289: #define NMAX 288        /* maximum number of codes in any set */
        !           290: 
        !           291: 
        !           292: unsigned hufts;         /* track memory usage */
        !           293: 
        !           294: 
        !           295: int huft_build(b, n, s, d, e, t, m)
        !           296: unsigned *b;            /* code lengths in bits (all assumed <= BMAX) */
        !           297: unsigned n;             /* number of codes (assumed <= NMAX) */
        !           298: unsigned s;             /* number of simple-valued codes (0..s-1) */
        !           299: UWORD *d;               /* list of base values for non-simple codes */
        !           300: UWORD *e;               /* list of extra bits for non-simple codes */
        !           301: struct huft **t;        /* result: starting table */
        !           302: int *m;                 /* maximum lookup bits, returns actual */
        !           303: /* Given a list of code lengths and a maximum table size, make a set of
        !           304:    tables to decode that set of codes.  Return zero on success, one if
        !           305:    the given code set is incomplete (the tables are still built in this
        !           306:    case), two if the input is invalid (all zero length codes or an
        !           307:    oversubscribed set of lengths), and three if not enough memory. */
        !           308: {
        !           309:   unsigned a;                   /* counter for codes of length k */
        !           310:   unsigned c[BMAX+1];           /* bit length count table */
        !           311:   unsigned f;                   /* i repeats in table every f entries */
        !           312:   int g;                        /* maximum code length */
        !           313:   int h;                        /* table level */
        !           314:   register unsigned i;          /* counter, current code */
        !           315:   register unsigned j;          /* counter */
        !           316:   register int k;               /* number of bits in current code */
        !           317:   int l;                        /* bits per table (returned in m) */
        !           318:   register unsigned *p;         /* pointer into c[], b[], or v[] */
        !           319:   register struct huft *q;      /* points to current table */
        !           320:   struct huft r;                /* table entry for structure assignment */
        !           321:   struct huft *u[BMAX];         /* table stack */
        !           322:   unsigned v[NMAX];             /* values in order of bit length */
        !           323:   register int w;               /* bits before this table == (l * h) */
        !           324:   unsigned x[BMAX+1];           /* bit offsets, then code stack */
        !           325:   unsigned *xp;                 /* pointer into x */
        !           326:   int y;                        /* number of dummy codes added */
        !           327:   unsigned z;                   /* number of entries in current table */
        !           328: 
        !           329: 
        !           330:   /* Generate counts for each bit length */
        !           331:   memset(c, 0, sizeof(c));
        !           332:   p = b;  i = n;
        !           333:   do {
        !           334:     c[*p++]++;                  /* assume all entries <= BMAX */
        !           335:   } while (--i);
        !           336:   if (c[0] == n)
        !           337:     return 2;                   /* bad input--all zero length codes */
        !           338: 
        !           339: 
        !           340:   /* Find minimum and maximum length, bound *m by those */
        !           341:   l = *m;
        !           342:   for (j = 1; j <= BMAX; j++)
        !           343:     if (c[j])
        !           344:       break;
        !           345:   k = j;                        /* minimum code length */
        !           346:   if (l < j)
        !           347:     l = j;
        !           348:   for (i = BMAX; i; i--)
        !           349:     if (c[i])
        !           350:       break;
        !           351:   g = i;                        /* maximum code length */
        !           352:   if (l > i)
        !           353:     l = i;
        !           354:   *m = l;
        !           355: 
        !           356: 
        !           357:   /* Adjust last length count to fill out codes, if needed */
        !           358:   for (y = 1 << j; j < i; j++, y <<= 1)
        !           359:     if ((y -= c[j]) < 0)
        !           360:       return 2;                 /* bad input: more codes than bits */
        !           361:   if ((y -= c[i]) < 0)
        !           362:     return 2;
        !           363:   c[i] += y;
        !           364: 
        !           365: 
        !           366:   /* Generate starting offsets into the value table for each length */
        !           367:   x[1] = j = 0;
        !           368:   p = c + 1;  xp = x + 2;
        !           369:   while (--i) {                 /* note that i == g from above */
        !           370:     *xp++ = (j += *p++);
        !           371:   }
        !           372: 
        !           373: 
        !           374:   /* Make a table of values in order of bit lengths */
        !           375:   p = b;  i = 0;
        !           376:   do {
        !           377:     if ((j = *p++) != 0)
        !           378:       v[x[j]++] = i;
        !           379:   } while (++i < n);
        !           380: 
        !           381: 
        !           382:   /* Generate the Huffman codes and for each, make the table entries */
        !           383:   x[0] = i = 0;                 /* first Huffman code is zero */
        !           384:   p = v;                        /* grab values in bit order */
        !           385:   h = -1;                       /* no tables yet--level -1 */
        !           386:   w = -l;                       /* bits decoded == (l * h) */
        !           387:   u[0] = NULL;  q = NULL;  z = 0;       /* just to keep compilers happy */
        !           388: 
        !           389:   /* go through the bit lengths (k already is bits in shortest code) */
        !           390:   for (; k <= g; k++)
        !           391:   {
        !           392:     a = c[k];
        !           393:     while (a--)
        !           394:     {
        !           395:       /* here i is the Huffman code of length k bits for value *p */
        !           396:       /* make tables up to required level */
        !           397:       while (k > w + l)
        !           398:       {
        !           399:         h++;
        !           400:         w += l;                 /* previous table always l bits */
        !           401: 
        !           402:         /* compute minimum size table less than or equal to l bits */
        !           403:         z = (z = g - w) > l ? l : z;    /* upper limit on table size */
        !           404:         if ((f = 1 << (j = k - w)) > a + 1)     /* try a k-w bit table */
        !           405:         {                       /* too few codes for k-w bit table */
        !           406:           f -= a + 1;           /* deduct codes from patterns left */
        !           407:           xp = c + k;
        !           408:           while (++j < z)       /* try smaller tables up to z bits */
        !           409:           {
        !           410:             if ((f <<= 1) <= *++xp)
        !           411:               break;            /* enough codes to use up j bits */
        !           412:             f -= *xp;           /* else deduct codes from patterns */
        !           413:           }
        !           414:         }
        !           415:         z = 1 << j;             /* table entries for j-bit table */
        !           416: 
        !           417:         /* allocate and link in new table */
        !           418:         if ((q = (struct huft *)malloc((z + 1)*sizeof(struct huft))) == NULL)
        !           419:         {
        !           420:           if (h)
        !           421:             huft_free(u[0]);
        !           422:           fprintf(stderr, "\n*** inflate out of memory *** ");
        !           423:           return 3;             /* not enough memory */
        !           424:         }
        !           425:         hufts += z + 1;         /* track memory usage */
        !           426:         *t = q + 1;             /* link to list for huft_free() */
        !           427:         *(t = &(q->v.t)) = NULL;
        !           428:         u[h] = ++q;             /* table starts after link */
        !           429: 
        !           430:         /* connect to last table, if there is one */
        !           431:         if (h)
        !           432:         {
        !           433:           x[h] = i;             /* save pattern for backing up */
        !           434:           r.b = l;              /* bits to dump before this table */
        !           435:           r.e = 16 + j;         /* bits in this table */
        !           436:           r.v.t = q;            /* pointer to this table */
        !           437:           j = i >> (w - l);     /* (get around Turbo C bug) */
        !           438:           u[h-1][j] = r;        /* connect to last table */
        !           439:         }
        !           440:       }
        !           441: 
        !           442:       /* set up table entry in r */
        !           443:       r.b = k - w;
        !           444:       if (p >= v + n)
        !           445:         r.e = 99;               /* out of values--invalid code */
        !           446:       else if (*p < s)
        !           447:       {
        !           448:         r.e = *p < 256 ? 16 : 15;       /* 256 is end-of-block code */
        !           449:         r.v.n = *p++;           /* simple code is just the value */
        !           450:       }
        !           451:       else
        !           452:       {
        !           453:         r.e = e[*p - s];        /* non-simple--look up in lists */
        !           454:         r.v.n = d[*p++ - s];
        !           455:       }
        !           456: 
        !           457:       /* fill code-like entries with r */
        !           458:       f = 1 << (k - w);
        !           459:       for (j = i >> w; j < z; j += f)
        !           460:         q[j] = r;
        !           461: 
        !           462:       /* backwards increment the k-bit code i */
        !           463:       for (j = 1 << (k - 1); i & j; j >>= 1)
        !           464:         i ^= j;
        !           465:       i ^= j;
        !           466: 
        !           467:       /* backup over finished tables */
        !           468:       while ((i & ((1 << w) - 1)) != x[h])
        !           469:       {
        !           470:         h--;                    /* don't need to update q */
        !           471:         w -= l;
        !           472:       }
        !           473:     }
        !           474:   }
        !           475: 
        !           476: 
        !           477:   /* Return true (1) if we were given an incomplete table */
        !           478:   return y != 0 && n != 1;
        !           479: }
        !           480: 
        !           481: 
        !           482: 
        !           483: int huft_free(t)
        !           484: struct huft *t;         /* table to free */
        !           485: /* Free the malloc'ed tables built by huft_build(), which makes a linked
        !           486:    list of the tables it made, with the links in a dummy first entry of
        !           487:    each table. */
        !           488: {
        !           489:   register struct huft *p, *q;
        !           490: 
        !           491: 
        !           492:   /* Go through linked list, freeing from the malloced (t[-1]) address. */
        !           493:   p = t;
        !           494:   while (p != NULL)
        !           495:   {
        !           496:     q = (--p)->v.t;
        !           497:     free(p);
        !           498:     p = q;
        !           499:   } 
        !           500:   return 0;
        !           501: }
        !           502: 
        !           503: 
        !           504: 
        !           505: void flush(w)
        !           506: unsigned w;             /* number of bytes to flush */
        !           507: /* Do the equivalent of OUTB for the bytes slide[0..w-1]. */
        !           508: {
        !           509:   unsigned n;
        !           510:   byte *p;
        !           511: 
        !           512:   p = (byte*)slide;
        !           513:   while (w)
        !           514:   {
        !           515:     n = (n = OUTBUFSIZ - outcnt) < w ? n : w;
        !           516:     memcpy(outptr, p, n);       /* try to fill up buffer */
        !           517:     outptr += n;
        !           518:     if ((outcnt += n) == OUTBUFSIZ)
        !           519:       if (FlushOutput())            /* if full, empty */
        !           520:          {
        !           521:                fprintf(stderr, "\nWrite error.\n");
        !           522:                exitPGP(1);
        !           523:          }
        !           524:     p += n;
        !           525:     w -= n;
        !           526:   }
        !           527: }
        !           528: 
        !           529: 
        !           530: 
        !           531: int inflate_codes(tl, td, bl, bd)
        !           532: struct huft *tl, *td;   /* literal/length and distance decoder tables */
        !           533: int bl, bd;             /* number of bits decoded by tl[] and td[] */
        !           534: /* inflate (decompress) the codes in a deflated (compressed) block.
        !           535:    Return an error code or zero if it all goes ok. */
        !           536: {
        !           537:   register unsigned e;  /* table entry flag/number of extra bits */
        !           538:   unsigned n, d;        /* length and index for copy */
        !           539:   unsigned w;           /* current window position */
        !           540:   struct huft *t;       /* pointer to table entry */
        !           541:   ULONG ml, md;         /* masks for bl and bd bits */
        !           542:   register ULONG b;     /* bit buffer */
        !           543:   register unsigned k;  /* number of bits in bit buffer */
        !           544: 
        !           545: 
        !           546:   /* make local copies of globals */
        !           547:   b = bb;                       /* initialize bit buffer */
        !           548:   k = bk;
        !           549:   w = wp;                       /* initialize window position */
        !           550: 
        !           551: 
        !           552:   /* inflate the coded data */
        !           553:   ml = mask_bits[bl];           /* precompute masks for speed */
        !           554:   md = mask_bits[bd];
        !           555:   while (1)                     /* do until end of block */
        !           556:   {
        !           557:     NEEDBITS(bl)
        !           558:     if ((e = (t = tl + (b & ml))->e) > 16)
        !           559:       do {
        !           560:         if (e == 99)
        !           561:           return 1;
        !           562:         DUMPBITS(t->b)
        !           563:         e -= 16;
        !           564:         NEEDBITS(e)
        !           565:       } while ((e = (t = t->v.t + (b & mask_bits[e]))->e) > 16);
        !           566:     DUMPBITS(t->b)
        !           567:     if (e == 16)                /* then it's a literal */
        !           568:     {
        !           569:       slide[w++] = t->v.n;
        !           570:       if (w == WSIZE)
        !           571:       {
        !           572:         flush(w);
        !           573:         w = 0;
        !           574:       }
        !           575:     }
        !           576:     else                        /* it's an EOB or a length */
        !           577:     {
        !           578:       /* exit if end of block */
        !           579:       if (e == 15)
        !           580:         break;
        !           581: 
        !           582:       /* get length of block to copy */
        !           583:       NEEDBITS(e)
        !           584:       n = t->v.n + (b & mask_bits[e]);
        !           585:       DUMPBITS(e);
        !           586: 
        !           587:       /* decode distance of block to copy */
        !           588:       NEEDBITS(bd)
        !           589:       if ((e = (t = td + (b & md))->e) > 16)
        !           590:         do {
        !           591:           if (e == 99)
        !           592:             return 1;
        !           593:           DUMPBITS(t->b)
        !           594:           e -= 16;
        !           595:           NEEDBITS(e)
        !           596:         } while ((e = (t = t->v.t + (b & mask_bits[e]))->e) > 16);
        !           597:       DUMPBITS(t->b)
        !           598:       NEEDBITS(e)
        !           599:       d = w - t->v.n - (b & mask_bits[e]);
        !           600:       DUMPBITS(e)
        !           601: 
        !           602:       /* do the copy */
        !           603:       do {
        !           604:         n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e);
        !           605: #ifndef NOMEMCPY
        !           606:         if (w - d >= e)         /* (this test assumes unsigned comparison) */
        !           607:         {
        !           608:           memcpy(slide + w, slide + d, e);
        !           609:           w += e;
        !           610:           d += e;
        !           611:         }
        !           612:         else                      /* do it slow to avoid memcpy() overlap */
        !           613: #endif /* !NOMEMCPY */
        !           614:           do {
        !           615:             slide[w++] = slide[d++];
        !           616:           } while (--e);
        !           617:         if (w == WSIZE)
        !           618:         {
        !           619:           flush(w);
        !           620:           w = 0;
        !           621:         }
        !           622:       } while (n);
        !           623:     }
        !           624:   }
        !           625: 
        !           626: 
        !           627:   /* restore the globals from the locals */
        !           628:   wp = w;                       /* restore global window pointer */
        !           629:   bb = b;                       /* restore global bit buffer */
        !           630:   bk = k;
        !           631: 
        !           632: 
        !           633:   /* done */
        !           634:   return 0;
        !           635: }
        !           636: 
        !           637: 
        !           638: 
        !           639: int inflate_stored()
        !           640: /* "decompress" an inflated type 0 (stored) block. */
        !           641: {
        !           642:   unsigned n;           /* number of bytes in block */
        !           643:   unsigned w;           /* current window position */
        !           644:   register ULONG b;     /* bit buffer */
        !           645:   register unsigned k;  /* number of bits in bit buffer */
        !           646: 
        !           647: 
        !           648:   /* make local copies of globals */
        !           649:   b = bb;                       /* initialize bit buffer */
        !           650:   k = bk;
        !           651:   w = wp;                       /* initialize window position */
        !           652: 
        !           653: 
        !           654:   /* go to byte boundary */
        !           655:   n = k & 7;
        !           656:   DUMPBITS(n);
        !           657: 
        !           658: 
        !           659:   /* get the length and its complement */
        !           660:   NEEDBITS(16)
        !           661:   n = b & 0xffff;
        !           662:   DUMPBITS(16)
        !           663:   NEEDBITS(16)
        !           664:   if (n != ((~b) & 0xffff))
        !           665:     return 1;                   /* error in compressed data */
        !           666:   DUMPBITS(16)
        !           667: 
        !           668: 
        !           669:   /* read and output the compressed data */
        !           670:   while (n--)
        !           671:   {
        !           672:     NEEDBITS(8)
        !           673:     slide[w++] = b;
        !           674:     if (w == WSIZE)
        !           675:     {
        !           676:       flush(w);
        !           677:       w = 0;
        !           678:     }
        !           679:     DUMPBITS(8)
        !           680:   }
        !           681: 
        !           682: 
        !           683:   /* restore the globals from the locals */
        !           684:   wp = w;                       /* restore global window pointer */
        !           685:   bb = b;                       /* restore global bit buffer */
        !           686:   bk = k;
        !           687:   return 0;
        !           688: }
        !           689: 
        !           690: 
        !           691: 
        !           692: int inflate_fixed()
        !           693: /* decompress an inflated type 1 (fixed Huffman codes) block.  We should
        !           694:    either replace this with a custom decoder, or at least precompute the
        !           695:    Huffman tables. */
        !           696: {
        !           697:   int i;                /* temporary variable */
        !           698:   struct huft *tl;      /* literal/length code table */
        !           699:   struct huft *td;      /* distance code table */
        !           700:   int bl;               /* lookup bits for tl */
        !           701:   int bd;               /* lookup bits for td */
        !           702:   unsigned l[288];      /* length list for huft_build */
        !           703: 
        !           704: 
        !           705:   /* set up literal table */
        !           706:   for (i = 0; i < 144; i++)
        !           707:     l[i] = 8;
        !           708:   for (; i < 256; i++)
        !           709:     l[i] = 9;
        !           710:   for (; i < 280; i++)
        !           711:     l[i] = 7;
        !           712:   for (; i < 288; i++)          /* make a complete, but wrong code set */
        !           713:     l[i] = 8;
        !           714:   bl = 7;
        !           715:   if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0)
        !           716:     return i;
        !           717: 
        !           718: 
        !           719:   /* set up distance table */
        !           720:   for (i = 0; i < 30; i++)      /* make an incomplete code set */
        !           721:     l[i] = 5;
        !           722:   bd = 5;
        !           723:   if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1)
        !           724:   {
        !           725:     huft_free(tl);
        !           726:     return i;
        !           727:   }
        !           728: 
        !           729: 
        !           730:   /* decompress until an end-of-block code */
        !           731:   if (inflate_codes(tl, td, bl, bd))
        !           732:     return 1;
        !           733: 
        !           734: 
        !           735:   /* free the decoding tables, return */
        !           736:   huft_free(tl);
        !           737:   huft_free(td);
        !           738:   return 0;
        !           739: }
        !           740: 
        !           741: 
        !           742: 
        !           743: int inflate_dynamic()
        !           744: /* decompress an inflated type 2 (dynamic Huffman codes) block. */
        !           745: {
        !           746:   int i;                /* temporary variables */
        !           747:   unsigned j;
        !           748:   unsigned l;           /* last length */
        !           749:   unsigned m;           /* mask for bit lengths table */
        !           750:   unsigned n;           /* number of lengths to get */
        !           751:   struct huft *tl;      /* literal/length code table */
        !           752:   struct huft *td;      /* distance code table */
        !           753:   int bl;               /* lookup bits for tl */
        !           754:   int bd;               /* lookup bits for td */
        !           755:   unsigned nb;          /* number of bit length codes */
        !           756:   unsigned nl;          /* number of literal/length codes */
        !           757:   unsigned nd;          /* number of distance codes */
        !           758:   unsigned ll[286+30];  /* literal/length and distance code lengths */
        !           759:   register ULONG b;     /* bit buffer */
        !           760:   register unsigned k;  /* number of bits in bit buffer */
        !           761: 
        !           762: 
        !           763:   /* make local bit buffer */
        !           764:   b = bb;
        !           765:   k = bk;
        !           766: 
        !           767: 
        !           768:   /* read in table lengths */
        !           769:   NEEDBITS(5)
        !           770:   nl = 257 + (b & 0x1f);        /* number of literal/length codes */
        !           771:   DUMPBITS(5)
        !           772:   NEEDBITS(5)
        !           773:   nd = 1 + (b & 0x1f);          /* number of distance codes */
        !           774:   DUMPBITS(5)
        !           775:   NEEDBITS(4)
        !           776:   nb = 4 + (b & 0xf);           /* number of bit length codes */
        !           777:   DUMPBITS(4)
        !           778:   if (nl > 286 || nd > 30)
        !           779:     return 1;                   /* bad lengths */
        !           780: 
        !           781: 
        !           782:   /* read in bit-length-code lengths */
        !           783:   for (i = 0; i < nb; i++)
        !           784:   {
        !           785:     NEEDBITS(3)
        !           786:     ll[border[i]] = b & 7;
        !           787:     DUMPBITS(3)
        !           788:   }
        !           789:   for (; i < 19; i++)
        !           790:     ll[border[i]] = 0;
        !           791: 
        !           792: 
        !           793:   /* build decoding table for trees--single level, 7 bit lookup */
        !           794:   bl = 7;
        !           795:   if ((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl)) != 0)
        !           796:   {
        !           797:     if (i == 1)
        !           798:       huft_free(tl);
        !           799:     return i;                   /* incomplete code set */
        !           800:   }
        !           801: 
        !           802: 
        !           803:   /* read in literal and distance code lengths */
        !           804:   n = nl + nd;
        !           805:   m = mask_bits[bl];
        !           806:   i = l = 0;
        !           807:   while (i < n)
        !           808:   {
        !           809:     NEEDBITS(bl)
        !           810:     j = (td = tl + (b & m))->b;
        !           811:     DUMPBITS(j)
        !           812:     j = td->v.n;
        !           813:     if (j < 16)                 /* length of code in bits (0..15) */
        !           814:       ll[i++] = l = j;          /* save last length in l */
        !           815:     else if (j == 16)           /* repeat last length 3 to 6 times */
        !           816:     {
        !           817:       NEEDBITS(2)
        !           818:       j = 3 + (b & 3);
        !           819:       DUMPBITS(2)
        !           820:       if (i + j > n)
        !           821:         return 1;
        !           822:       while (j--)
        !           823:         ll[i++] = l;
        !           824:     }
        !           825:     else if (j == 17)           /* 3 to 10 zero length codes */
        !           826:     {
        !           827:       NEEDBITS(3)
        !           828:       j = 3 + (b & 7);
        !           829:       DUMPBITS(3)
        !           830:       if (i + j > n)
        !           831:         return 1;
        !           832:       while (j--)
        !           833:         ll[i++] = 0;
        !           834:       l = 0;
        !           835:     }
        !           836:     else                        /* j == 18: 11 to 138 zero length codes */
        !           837:     {
        !           838:       NEEDBITS(7)
        !           839:       j = 11 + (b & 0x7f);
        !           840:       DUMPBITS(7)
        !           841:       if (i + j > n)
        !           842:         return 1;
        !           843:       while (j--)
        !           844:         ll[i++] = 0;
        !           845:       l = 0;
        !           846:     }
        !           847:   }
        !           848: 
        !           849: 
        !           850:   /* free decoding table for trees */
        !           851:   huft_free(tl);
        !           852: 
        !           853: 
        !           854:   /* restore the global bit buffer */
        !           855:   bb = b;
        !           856:   bk = k;
        !           857: 
        !           858: 
        !           859:   /* build the decoding tables for literal/length and distance codes */
        !           860:   bl = lbits;
        !           861:   if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0)
        !           862:   {
        !           863:     if (i == 1)
        !           864:       huft_free(tl);
        !           865:     return i;                   /* incomplete code set */
        !           866:   }
        !           867:   bd = dbits;
        !           868:   if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0)
        !           869:   {
        !           870:     if (i == 1)
        !           871:       huft_free(td);
        !           872:     huft_free(tl);
        !           873:     return i;                   /* incomplete code set */
        !           874:   }
        !           875: 
        !           876: 
        !           877:   /* decompress until an end-of-block code */
        !           878:   if (inflate_codes(tl, td, bl, bd))
        !           879:     return 1;
        !           880: 
        !           881: 
        !           882:   /* free the decoding tables, return */
        !           883:   huft_free(tl);
        !           884:   huft_free(td);
        !           885:   return 0;
        !           886: }
        !           887: 
        !           888: 
        !           889: 
        !           890: int inflate_block(e)
        !           891: int *e;                 /* last block flag */
        !           892: /* decompress an inflated block */
        !           893: {
        !           894:   unsigned t;           /* block type */
        !           895:   register ULONG b;     /* bit buffer */
        !           896:   register unsigned k;  /* number of bits in bit buffer */
        !           897: 
        !           898: 
        !           899:   /* make local bit buffer */
        !           900:   b = bb;
        !           901:   k = bk;
        !           902: 
        !           903: 
        !           904:   /* read in last block bit */
        !           905:   NEEDBITS(1)
        !           906:   *e = b & 1;
        !           907:   DUMPBITS(1)
        !           908: 
        !           909: 
        !           910:   /* read in block type */
        !           911:   NEEDBITS(2)
        !           912:   t = b & 3;
        !           913:   DUMPBITS(2)
        !           914: 
        !           915: 
        !           916:   /* restore the global bit buffer */
        !           917:   bb = b;
        !           918:   bk = k;
        !           919: 
        !           920: 
        !           921:   /* inflate that block type */
        !           922:   if (t == 2)
        !           923:     return inflate_dynamic();
        !           924:   if (t == 0)
        !           925:     return inflate_stored();
        !           926:   if (t == 1)
        !           927:     return inflate_fixed();
        !           928: 
        !           929: 
        !           930:   /* bad block type */
        !           931:   return 2;
        !           932: }
        !           933: 
        !           934: 
        !           935: 
        !           936: int inflate_entry()
        !           937: /* decompress an inflated entry */
        !           938: {
        !           939:   int e;                /* last block flag */
        !           940:   int r;                /* result code */
        !           941:   unsigned h;           /* maximum struct huft's malloc'ed */
        !           942: 
        !           943: 
        !           944:   /* initialize window, bit buffer */
        !           945:   wp = 0;
        !           946:   bk = 0;
        !           947:   bb = 0;
        !           948: 
        !           949: 
        !           950:   /* decompress until the last block */
        !           951:   h = 0;
        !           952:   do {
        !           953:     hufts = 0;
        !           954:     if ((r = inflate_block(&e)) != 0)
        !           955:       return r;
        !           956:     if (hufts > h)
        !           957:       h = hufts;
        !           958:   } while (!e);
        !           959: 
        !           960: 
        !           961:   /* flush out slide */
        !           962:   flush(wp);
        !           963: 
        !           964: 
        !           965:   /* return success */
        !           966: #ifdef DEBUG
        !           967:   fprintf(stderr, "<%u> ", h);
        !           968: #endif /* DEBUG */
        !           969:   return 0;
        !           970: }
        !           971: 
        !           972: 
        !           973: void inflate()
        !           974: /* ignore the return code for now ... */
        !           975: {
        !           976: #ifdef DYN_ALLOC
        !           977:   if (window == NULL) {
        !           978:     window = (char*)  calloc((unsigned)WSIZE, 2*sizeof(char));
        !           979:     /* Note that inflate only needs WSIZE bytes, but the window
        !           980:      * array is shared with deflate, which needs 2*WISZE bytes.
        !           981:      */
        !           982:     if (window==NULL) err(4, "");
        !           983:   }
        !           984: #endif
        !           985: 
        !           986:   inflate_entry();
        !           987: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.