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

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

unix.superglobalmegacorp.com

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