home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / unzip511.zip / explode.c < prev    next >
C/C++ Source or Header  |  1994-07-19  |  28KB  |  825 lines

  1. /* explode.c -- put in the public domain by Mark Adler
  2.    version c12, 9 April 1994 */
  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.     c1   30 Mar 92  M. Adler        explode that uses huft_build from inflate
  13.                                     (this gives over a 70% speed improvement
  14.                                     over the original unimplode.c, which
  15.                                     decoded a bit at a time)
  16.     c2    4 Apr 92  M. Adler        fixed bug for file sizes a multiple of 32k.
  17.     c3   10 Apr 92  M. Adler        added a little memory tracking if DEBUG
  18.     c4   11 Apr 92  M. Adler        added NOMEMCPY do kill use of memcpy()
  19.     c5   21 Apr 92  M. Adler        added the WSIZE #define to allow reducing
  20.                                     the 32K window size for specialized
  21.                                     applications.
  22.     c6   31 May 92  M. Adler        added typecasts to eliminate some warnings
  23.     c7   27 Jun 92  G. Roelofs      added more typecasts.
  24.     c8   17 Oct 92  G. Roelofs      changed ULONG/UWORD/byte to ulg/ush/uch.
  25.     c9   19 Jul 93  J. Bush         added more typecasts (to return values);
  26.                                     made l[256] array static for Amiga.
  27.     c10   8 Oct 93  G. Roelofs      added used_csize for diagnostics; added
  28.                                     buf and unshrink arguments to flush();
  29.                                     undef'd various macros at end for Turbo C;
  30.                                     removed NEXTBYTE macro (now in unzip.h)
  31.                                     and bytebuf variable (not used); changed
  32.                                     memset() to memzero().
  33.     c11   9 Jan 94  M. Adler        fixed incorrect used_csize calculation.
  34.     c12   9 Apr 94  G. Roelofs      fixed split comments on preprocessor lines
  35.                                     to avoid bug in Encore compiler.
  36.  */
  37.  
  38.  
  39. /*
  40.    Explode imploded (PKZIP method 6 compressed) data.  This compression
  41.    method searches for as much of the current string of bytes (up to a length
  42.    of ~320) in the previous 4K or 8K bytes.  If it doesn't find any matches
  43.    (of at least length 2 or 3), it codes the next byte.  Otherwise, it codes
  44.    the length of the matched string and its distance backwards from the
  45.    current position.  Single bytes ("literals") are preceded by a one (a
  46.    single bit) and are either uncoded (the eight bits go directly into the
  47.    compressed stream for a total of nine bits) or Huffman coded with a
  48.    supplied literal code tree.  If literals are coded, then the minimum match
  49.    length is three, otherwise it is two.
  50.    
  51.    There are therefore four kinds of imploded streams: 8K search with coded
  52.    literals (min match = 3), 4K search with coded literals (min match = 3),
  53.    8K with uncoded literals (min match = 2), and 4K with uncoded literals
  54.    (min match = 2).  The kind of stream is identified in two bits of a
  55.    general purpose bit flag that is outside of the compressed stream.
  56.    
  57.    Distance-length pairs are always coded.  Distance-length pairs for matched
  58.    strings are preceded by a zero bit (to distinguish them from literals) and
  59.    are always coded.  The distance comes first and is either the low six (4K)
  60.    or low seven (8K) bits of the distance (uncoded), followed by the high six
  61.    bits of the distance coded.  Then the length is six bits coded (0..63 +
  62.    min match length), and if the maximum such length is coded, then it's
  63.    followed by another eight bits (uncoded) to be added to the coded length.
  64.    This gives a match length range of 2..320 or 3..321 bytes.
  65.  
  66.    The literal, length, and distance codes are all represented in a slightly
  67.    compressed form themselves.  What is sent are the lengths of the codes for
  68.    each value, which is sufficient to construct the codes.  Each byte of the
  69.    code representation is the code length (the low four bits representing
  70.    1..16), and the number of values sequentially with that length (the high
  71.    four bits also representing 1..16).  There are 256 literal code values (if
  72.    literals are coded), 64 length code values, and 64 distance code values,
  73.    in that order at the beginning of the compressed stream.  Each set of code
  74.    values is preceded (redundantly) with a byte indicating how many bytes are
  75.    in the code description that follows, in the range 1..256.
  76.  
  77.    The codes themselves are decoded using tables made by huft_build() from
  78.    the bit lengths.  That routine and its comments are in the inflate.c
  79.    module.
  80.  */
  81.  
  82. #include "unzip.h"      /* must supply slide[] (uch) array and NEXTBYTE macro */
  83.  
  84. #ifndef WSIZE
  85. #  define WSIZE 0x8000  /* window size--must be a power of two, and */
  86. #endif                  /* at least 8K for zip's implode method */
  87.  
  88.  
  89. struct huft {
  90.   uch e;                /* number of extra bits or operation */
  91.   uch b;                /* number of bits in this code or subcode */
  92.   union {
  93.     ush n;              /* literal, length base, or distance base */
  94.     struct huft *t;     /* pointer to next level of table */
  95.   } v;
  96. };
  97.  
  98. /* Function prototypes */
  99. /* routines from inflate.c */
  100. extern unsigned hufts;
  101. int huft_build OF((unsigned *, unsigned, unsigned, ush *, ush *,
  102.                    struct huft **, int *));
  103. int huft_free OF((struct huft *));
  104.  
  105. /* routines here */
  106. int get_tree OF((unsigned *, unsigned));
  107. int explode_lit8 OF((struct huft *, struct huft *, struct huft *,
  108.                      int, int, int));
  109. int explode_lit4 OF((struct huft *, struct huft *, struct huft *,
  110.                      int, int, int));
  111. int explode_nolit8 OF((struct huft *, struct huft *, int, int));
  112. int explode_nolit4 OF((struct huft *, struct huft *, int, int));
  113. int explode OF((void));
  114.  
  115.  
  116. /* The implode algorithm uses a sliding 4K or 8K byte window on the
  117.    uncompressed stream to find repeated byte strings.  This is implemented
  118.    here as a circular buffer.  The index is updated simply by incrementing
  119.    and then and'ing with 0x0fff (4K-1) or 0x1fff (8K-1).  Here, the 32K
  120.    buffer of inflate is used, and it works just as well to always have
  121.    a 32K circular buffer, so the index is anded with 0x7fff.  This is
  122.    done to allow the window to also be used as the output buffer. */
  123. /* This must be supplied in an external module useable like "uch slide[8192];"
  124.    or "uch *slide;", where the latter would be malloc'ed.  In unzip, slide[]
  125.    is actually a 32K area for use by inflate, which uses a 32K sliding window.
  126.  */
  127.  
  128.  
  129. /* Tables for length and distance */
  130. ush cplen2[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
  131.         18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,
  132.         35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
  133.         52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65};
  134. ush cplen3[] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
  135.         19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
  136.         36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
  137.         53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66};
  138. ush extra[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  139.         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  140.         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  141.         8};
  142. ush cpdist4[] = {1, 65, 129, 193, 257, 321, 385, 449, 513, 577, 641, 705,
  143.         769, 833, 897, 961, 1025, 1089, 1153, 1217, 1281, 1345, 1409, 1473,
  144.         1537, 1601, 1665, 1729, 1793, 1857, 1921, 1985, 2049, 2113, 2177,
  145.         2241, 2305, 2369, 2433, 2497, 2561, 2625, 2689, 2753, 2817, 2881,
  146.         2945, 3009, 3073, 3137, 3201, 3265, 3329, 3393, 3457, 3521, 3585,
  147.         3649, 3713, 3777, 3841, 3905, 3969, 4033};
  148. ush cpdist8[] = {1, 129, 257, 385, 513, 641, 769, 897, 1025, 1153, 1281,
  149.         1409, 1537, 1665, 1793, 1921, 2049, 2177, 2305, 2433, 2561, 2689,
  150.         2817, 2945, 3073, 3201, 3329, 3457, 3585, 3713, 3841, 3969, 4097,
  151.         4225, 4353, 4481, 4609, 4737, 4865, 4993, 5121, 5249, 5377, 5505,
  152.         5633, 5761, 5889, 6017, 6145, 6273, 6401, 6529, 6657, 6785, 6913,
  153.         7041, 7169, 7297, 7425, 7553, 7681, 7809, 7937, 8065};
  154.  
  155.  
  156. /* Macros for inflate() bit peeking and grabbing.
  157.    The usage is:
  158.    
  159.         NEEDBITS(j)
  160.         x = b & mask_bits[j];
  161.         DUMPBITS(j)
  162.  
  163.    where NEEDBITS makes sure that b has at least j bits in it, and
  164.    DUMPBITS removes the bits from b.  The macros use the variable k
  165.    for the number of bits in b.  Normally, b and k are register
  166.    variables for speed.
  167.  */
  168.  
  169. #define NEEDBITS(n) {while(k<(n)){b|=((ulg)NEXTBYTE)<<k;k+=8;}}
  170. #define DUMPBITS(n) {b>>=(n);k-=(n);}
  171.  
  172.  
  173.  
  174. int get_tree(l, n)
  175. unsigned *l;            /* bit lengths */
  176. unsigned n;             /* number expected */
  177. /* Get the bit lengths for a code representation from the compressed
  178.    stream.  If get_tree() returns 4, then there is an error in the data.
  179.    Otherwise zero is returned. */
  180. {
  181.   unsigned i;           /* bytes remaining in list */
  182.   unsigned k;           /* lengths entered */
  183.   unsigned j;           /* number of codes */
  184.   unsigned b;           /* bit length for those codes */ 
  185.  
  186.  
  187.   /* get bit lengths */
  188.   i = NEXTBYTE + 1;                     /* length/count pairs to read */
  189.   k = 0;                                /* next code */
  190.   do {
  191.     b = ((j = NEXTBYTE) & 0xf) + 1;     /* bits in code (1..16) */
  192.     j = ((j & 0xf0) >> 4) + 1;          /* codes with those bits (1..16) */
  193.     if (k + j > n)
  194.       return 4;                         /* don't overflow l[] */
  195.     do {
  196.       l[k++] = b;
  197.     } while (--j);
  198.   } while (--i);
  199.   return k != n ? 4 : 0;                /* should have read n of them */
  200. }
  201.  
  202.  
  203.  
  204. int explode_lit8(tb, tl, td, bb, bl, bd)
  205. struct huft *tb, *tl, *td;      /* literal, length, and distance tables */
  206. int bb, bl, bd;                 /* number of bits decoded by those */
  207. /* Decompress the imploded data using coded literals and an 8K sliding
  208.    window. */
  209. {
  210.   long s;               /* bytes to decompress */
  211.   register unsigned e;  /* table entry flag/number of extra bits */
  212.   unsigned n, d;        /* length and index for copy */
  213.   unsigned w;           /* current window position */
  214.   struct huft *t;       /* pointer to table entry */
  215.   unsigned mb, ml, md;  /* masks for bb, bl, and bd bits */
  216.   register ulg b;       /* bit buffer */
  217.   register unsigned k;  /* number of bits in bit buffer */
  218.   unsigned u;           /* true if unflushed */
  219.  
  220.  
  221.   /* explode the coded data */
  222.   b = k = w = 0;                /* initialize bit buffer, window */
  223.   u = 1;                        /* buffer unflushed */
  224.   mb = mask_bits[bb];           /* precompute masks for speed */
  225.   ml = mask_bits[bl];
  226.   md = mask_bits[bd];
  227.   s = ucsize;
  228.   while (s > 0)                 /* do until ucsize bytes uncompressed */
  229.   {
  230.     NEEDBITS(1)
  231.     if (b & 1)                  /* then literal--decode it */
  232.     {
  233.       DUMPBITS(1)
  234.       s--;
  235.       NEEDBITS((unsigned)bb)    /* get coded literal */
  236.       if ((e = (t = tb + ((~(unsigned)b) & mb))->e) > 16)
  237.         do {
  238.           if (e == 99)
  239.             return 1;
  240.           DUMPBITS(t->b)
  241.           e -= 16;
  242.           NEEDBITS(e)
  243.         } while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16);
  244.       DUMPBITS(t->b)
  245.       slide[w++] = (uch)t->v.n;
  246.       if (w == WSIZE)
  247.       {
  248.         flush(slide, w, 0);
  249.         w = u = 0;
  250.       }
  251.     }
  252.     else                        /* else distance/length */
  253.     {
  254.       DUMPBITS(1)
  255.       NEEDBITS(7)               /* get distance low bits */
  256.       d = (unsigned)b & 0x7f;
  257.       DUMPBITS(7)
  258.       NEEDBITS((unsigned)bd)    /* get coded distance high bits */
  259.       if ((e = (t = td + ((~(unsigned)b) & md))->e) > 16)
  260.         do {
  261.           if (e == 99)
  262.             return 1;
  263.           DUMPBITS(t->b)
  264.           e -= 16;
  265.           NEEDBITS(e)
  266.         } while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16);
  267.       DUMPBITS(t->b)
  268.       d = w - d - t->v.n;       /* construct offset */
  269.       NEEDBITS((unsigned)bl)    /* get coded length */
  270.       if ((e = (t = tl + ((~(unsigned)b) & ml))->e) > 16)
  271.         do {
  272.           if (e == 99)
  273.             return 1;
  274.           DUMPBITS(t->b)
  275.           e -= 16;
  276.           NEEDBITS(e)
  277.         } while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16);
  278.       DUMPBITS(t->b)
  279.       n = t->v.n;
  280.       if (e)                    /* get length extra bits */
  281.       {
  282.         NEEDBITS(8)
  283.         n += (unsigned)b & 0xff;
  284.         DUMPBITS(8)
  285.       }
  286.  
  287.       /* do the copy */
  288.       s -= n;
  289.       do {
  290.         n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e);
  291.         if (u && w <= d)
  292.         {
  293.           memzero(slide + w, e);
  294.           w += e;
  295.           d += e;
  296.         }
  297.         else
  298. #ifndef NOMEMCPY
  299.           if (w - d >= e)       /* (this test assumes unsigned comparison) */
  300.           {
  301.             memcpy(slide + w, slide + d, e);
  302.             w += e;
  303.             d += e;
  304.           }
  305.           else                  /* do it slow to avoid memcpy() overlap */
  306. #endif /* !NOMEMCPY */
  307.             do {
  308.               slide[w++] = slide[d++];
  309.             } while (--e);
  310.         if (w == WSIZE)
  311.         {
  312.           flush(slide, w, 0);
  313.           w = u = 0;
  314.         }
  315.       } while (n);
  316.     }
  317.   }
  318.  
  319.   /* flush out slide */
  320.   flush(slide, w, 0);
  321.   if (csize + (k >> 3))   /* should have read csize bytes, but sometimes */
  322.   {                       /* read one too many:  k>>3 compensates */
  323.     used_csize = lrec.csize - csize - (k >> 3);
  324.     return 5;
  325.   }
  326.   return 0;
  327. }
  328.  
  329.  
  330.  
  331. int explode_lit4(tb, tl, td, bb, bl, bd)
  332. struct huft *tb, *tl, *td;      /* literal, length, and distance tables */
  333. int bb, bl, bd;                 /* number of bits decoded by those */
  334. /* Decompress the imploded data using coded literals and a 4K sliding
  335.    window. */
  336. {
  337.   long s;               /* bytes to decompress */
  338.   register unsigned e;  /* table entry flag/number of extra bits */
  339.   unsigned n, d;        /* length and index for copy */
  340.   unsigned w;           /* current window position */
  341.   struct huft *t;       /* pointer to table entry */
  342.   unsigned mb, ml, md;  /* masks for bb, bl, and bd bits */
  343.   register ulg b;       /* bit buffer */
  344.   register unsigned k;  /* number of bits in bit buffer */
  345.   unsigned u;           /* true if unflushed */
  346.  
  347.  
  348.   /* explode the coded data */
  349.   b = k = w = 0;                /* initialize bit buffer, window */
  350.   u = 1;                        /* buffer unflushed */
  351.   mb = mask_bits[bb];           /* precompute masks for speed */
  352.   ml = mask_bits[bl];
  353.   md = mask_bits[bd];
  354.   s = ucsize;
  355.   while (s > 0)                 /* do until ucsize bytes uncompressed */
  356.   {
  357.     NEEDBITS(1)
  358.     if (b & 1)                  /* then literal--decode it */
  359.     {
  360.       DUMPBITS(1)
  361.       s--;
  362.       NEEDBITS((unsigned)bb)    /* get coded literal */
  363.       if ((e = (t = tb + ((~(unsigned)b) & mb))->e) > 16)
  364.         do {
  365.           if (e == 99)
  366.             return 1;
  367.           DUMPBITS(t->b)
  368.           e -= 16;
  369.           NEEDBITS(e)
  370.         } while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16);
  371.       DUMPBITS(t->b)
  372.       slide[w++] = (uch)t->v.n;
  373.       if (w == WSIZE)
  374.       {
  375.         flush(slide, w, 0);
  376.         w = u = 0;
  377.       }
  378.     }
  379.     else                        /* else distance/length */
  380.     {
  381.       DUMPBITS(1)
  382.       NEEDBITS(6)               /* get distance low bits */
  383.       d = (unsigned)b & 0x3f;
  384.       DUMPBITS(6)
  385.       NEEDBITS((unsigned)bd)    /* get coded distance high bits */
  386.       if ((e = (t = td + ((~(unsigned)b) & md))->e) > 16)
  387.         do {
  388.           if (e == 99)
  389.             return 1;
  390.           DUMPBITS(t->b)
  391.           e -= 16;
  392.           NEEDBITS(e)
  393.         } while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16);
  394.       DUMPBITS(t->b)
  395.       d = w - d - t->v.n;       /* construct offset */
  396.       NEEDBITS((unsigned)bl)    /* get coded length */
  397.       if ((e = (t = tl + ((~(unsigned)b) & ml))->e) > 16)
  398.         do {
  399.           if (e == 99)
  400.             return 1;
  401.           DUMPBITS(t->b)
  402.           e -= 16;
  403.           NEEDBITS(e)
  404.         } while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16);
  405.       DUMPBITS(t->b)
  406.       n = t->v.n;
  407.       if (e)                    /* get length extra bits */
  408.       {
  409.         NEEDBITS(8)
  410.         n += (unsigned)b & 0xff;
  411.         DUMPBITS(8)
  412.       }
  413.  
  414.       /* do the copy */
  415.       s -= n;
  416.       do {
  417.         n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e);
  418.         if (u && w <= d)
  419.         {
  420.           memzero(slide + w, e);
  421.           w += e;
  422.           d += e;
  423.         }
  424.         else
  425. #ifndef NOMEMCPY
  426.           if (w - d >= e)       /* (this test assumes unsigned comparison) */
  427.           {
  428.             memcpy(slide + w, slide + d, e);
  429.             w += e;
  430.             d += e;
  431.           }
  432.           else                  /* do it slow to avoid memcpy() overlap */
  433. #endif /* !NOMEMCPY */
  434.             do {
  435.               slide[w++] = slide[d++];
  436.             } while (--e);
  437.         if (w == WSIZE)
  438.         {
  439.           flush(slide, w, 0);
  440.           w = u = 0;
  441.         }
  442.       } while (n);
  443.     }
  444.   }
  445.  
  446.   /* flush out slide */
  447.   flush(slide, w, 0);
  448.   if (csize + (k >> 3))   /* should have read csize bytes, but sometimes */
  449.   {                       /* read one too many:  k>>3 compensates */
  450.     used_csize = lrec.csize - csize - (k >> 3);
  451.     return 5;
  452.   }
  453.   return 0;
  454. }
  455.  
  456.  
  457.  
  458. int explode_nolit8(tl, td, bl, bd)
  459. struct huft *tl, *td;   /* length and distance decoder tables */
  460. int bl, bd;             /* number of bits decoded by tl[] and td[] */
  461. /* Decompress the imploded data using uncoded literals and an 8K sliding
  462.    window. */
  463. {
  464.   long s;               /* bytes to decompress */
  465.   register unsigned e;  /* table entry flag/number of extra bits */
  466.   unsigned n, d;        /* length and index for copy */
  467.   unsigned w;           /* current window position */
  468.   struct huft *t;       /* pointer to table entry */
  469.   unsigned ml, md;      /* masks for bl and bd bits */
  470.   register ulg b;       /* bit buffer */
  471.   register unsigned k;  /* number of bits in bit buffer */
  472.   unsigned u;           /* true if unflushed */
  473.  
  474.  
  475.   /* explode the coded data */
  476.   b = k = w = 0;                /* initialize bit buffer, window */
  477.   u = 1;                        /* buffer unflushed */
  478.   ml = mask_bits[bl];           /* precompute masks for speed */
  479.   md = mask_bits[bd];
  480.   s = ucsize;
  481.   while (s > 0)                 /* do until ucsize bytes uncompressed */
  482.   {
  483.     NEEDBITS(1)
  484.     if (b & 1)                  /* then literal--get eight bits */
  485.     {
  486.       DUMPBITS(1)
  487.       s--;
  488.       NEEDBITS(8)
  489.       slide[w++] = (uch)b;
  490.       if (w == WSIZE)
  491.       {
  492.         flush(slide, w, 0);
  493.         w = u = 0;
  494.       }
  495.       DUMPBITS(8)
  496.     }
  497.     else                        /* else distance/length */
  498.     {
  499.       DUMPBITS(1)
  500.       NEEDBITS(7)               /* get distance low bits */
  501.       d = (unsigned)b & 0x7f;
  502.       DUMPBITS(7)
  503.       NEEDBITS((unsigned)bd)    /* get coded distance high bits */
  504.       if ((e = (t = td + ((~(unsigned)b) & md))->e) > 16)
  505.         do {
  506.           if (e == 99)
  507.             return 1;
  508.           DUMPBITS(t->b)
  509.           e -= 16;
  510.           NEEDBITS(e)
  511.         } while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16);
  512.       DUMPBITS(t->b)
  513.       d = w - d - t->v.n;       /* construct offset */
  514.       NEEDBITS((unsigned)bl)    /* get coded length */
  515.       if ((e = (t = tl + ((~(unsigned)b) & ml))->e) > 16)
  516.         do {
  517.           if (e == 99)
  518.             return 1;
  519.           DUMPBITS(t->b)
  520.           e -= 16;
  521.           NEEDBITS(e)
  522.         } while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16);
  523.       DUMPBITS(t->b)
  524.       n = t->v.n;
  525.       if (e)                    /* get length extra bits */
  526.       {
  527.         NEEDBITS(8)
  528.         n += (unsigned)b & 0xff;
  529.         DUMPBITS(8)
  530.       }
  531.  
  532.       /* do the copy */
  533.       s -= n;
  534.       do {
  535.         n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e);
  536.         if (u && w <= d)
  537.         {
  538.           memzero(slide + w, e);
  539.           w += e;
  540.           d += e;
  541.         }
  542.         else
  543. #ifndef NOMEMCPY
  544.           if (w - d >= e)       /* (this test assumes unsigned comparison) */
  545.           {
  546.             memcpy(slide + w, slide + d, e);
  547.             w += e;
  548.             d += e;
  549.           }
  550.           else                  /* do it slow to avoid memcpy() overlap */
  551. #endif /* !NOMEMCPY */
  552.             do {
  553.               slide[w++] = slide[d++];
  554.             } while (--e);
  555.         if (w == WSIZE)
  556.         {
  557.           flush(slide, w, 0);
  558.           w = u = 0;
  559.         }
  560.       } while (n);
  561.     }
  562.   }
  563.  
  564.   /* flush out slide */
  565.   flush(slide, w, 0);
  566.   if (csize + (k >> 3))   /* should have read csize bytes, but sometimes */
  567.   {                       /* read one too many:  k>>3 compensates */
  568.     used_csize = lrec.csize - csize - (k >> 3);
  569.     return 5;
  570.   }
  571.   return 0;
  572. }
  573.  
  574.  
  575.  
  576. int explode_nolit4(tl, td, bl, bd)
  577. struct huft *tl, *td;   /* length and distance decoder tables */
  578. int bl, bd;             /* number of bits decoded by tl[] and td[] */
  579. /* Decompress the imploded data using uncoded literals and a 4K sliding
  580.    window. */
  581. {
  582.   long s;               /* bytes to decompress */
  583.   register unsigned e;  /* table entry flag/number of extra bits */
  584.   unsigned n, d;        /* length and index for copy */
  585.   unsigned w;           /* current window position */
  586.   struct huft *t;       /* pointer to table entry */
  587.   unsigned ml, md;      /* masks for bl and bd bits */
  588.   register ulg b;       /* bit buffer */
  589.   register unsigned k;  /* number of bits in bit buffer */
  590.   unsigned u;           /* true if unflushed */
  591.  
  592.  
  593.   /* explode the coded data */
  594.   b = k = w = 0;                /* initialize bit buffer, window */
  595.   u = 1;                        /* buffer unflushed */
  596.   ml = mask_bits[bl];           /* precompute masks for speed */
  597.   md = mask_bits[bd];
  598.   s = ucsize;
  599.   while (s > 0)                 /* do until ucsize bytes uncompressed */
  600.   {
  601.     NEEDBITS(1)
  602.     if (b & 1)                  /* then literal--get eight bits */
  603.     {
  604.       DUMPBITS(1)
  605.       s--;
  606.       NEEDBITS(8)
  607.       slide[w++] = (uch)b;
  608.       if (w == WSIZE)
  609.       {
  610.         flush(slide, w, 0);
  611.         w = u = 0;
  612.       }
  613.       DUMPBITS(8)
  614.     }
  615.     else                        /* else distance/length */
  616.     {
  617.       DUMPBITS(1)
  618.       NEEDBITS(6)               /* get distance low bits */
  619.       d = (unsigned)b & 0x3f;
  620.       DUMPBITS(6)
  621.       NEEDBITS((unsigned)bd)    /* get coded distance high bits */
  622.       if ((e = (t = td + ((~(unsigned)b) & md))->e) > 16)
  623.         do {
  624.           if (e == 99)
  625.             return 1;
  626.           DUMPBITS(t->b)
  627.           e -= 16;
  628.           NEEDBITS(e)
  629.         } while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16);
  630.       DUMPBITS(t->b)
  631.       d = w - d - t->v.n;       /* construct offset */
  632.       NEEDBITS((unsigned)bl)    /* get coded length */
  633.       if ((e = (t = tl + ((~(unsigned)b) & ml))->e) > 16)
  634.         do {
  635.           if (e == 99)
  636.             return 1;
  637.           DUMPBITS(t->b)
  638.           e -= 16;
  639.           NEEDBITS(e)
  640.         } while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16);
  641.       DUMPBITS(t->b)
  642.       n = t->v.n;
  643.       if (e)                    /* get length extra bits */
  644.       {
  645.         NEEDBITS(8)
  646.         n += (unsigned)b & 0xff;
  647.         DUMPBITS(8)
  648.       }
  649.  
  650.       /* do the copy */
  651.       s -= n;
  652.       do {
  653.         n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e);
  654.         if (u && w <= d)
  655.         {
  656.           memzero(slide + w, e);
  657.           w += e;
  658.           d += e;
  659.         }
  660.         else
  661. #ifndef NOMEMCPY
  662.           if (w - d >= e)       /* (this test assumes unsigned comparison) */
  663.           {
  664.             memcpy(slide + w, slide + d, e);
  665.             w += e;
  666.             d += e;
  667.           }
  668.           else                  /* do it slow to avoid memcpy() overlap */
  669. #endif /* !NOMEMCPY */
  670.             do {
  671.               slide[w++] = slide[d++];
  672.             } while (--e);
  673.         if (w == WSIZE)
  674.         {
  675.           flush(slide, w, 0);
  676.           w = u = 0;
  677.         }
  678.       } while (n);
  679.     }
  680.   }
  681.  
  682.   /* flush out slide */
  683.   flush(slide, w, 0);
  684.   if (csize + (k >> 3))   /* should have read csize bytes, but sometimes */
  685.   {                       /* read one too many:  k>>3 compensates */
  686.     used_csize = lrec.csize - csize - (k >> 3);
  687.     return 5;
  688.   }
  689.   return 0;
  690. }
  691.  
  692.  
  693.  
  694. int explode()
  695. /* Explode an imploded compressed stream.  Based on the general purpose
  696.    bit flag, decide on coded or uncoded literals, and an 8K or 4K sliding
  697.    window.  Construct the literal (if any), length, and distance codes and
  698.    the tables needed to decode them (using huft_build() from inflate.c),
  699.    and call the appropriate routine for the type of data in the remainder
  700.    of the stream.  The four routines are nearly identical, differing only
  701.    in whether the literal is decoded or simply read in, and in how many
  702.    bits are read in, uncoded, for the low distance bits. */
  703. {
  704.   unsigned r;           /* return codes */
  705.   struct huft *tb;      /* literal code table */
  706.   struct huft *tl;      /* length code table */
  707.   struct huft *td;      /* distance code table */
  708.   int bb;               /* bits for tb */
  709.   int bl;               /* bits for tl */
  710.   int bd;               /* bits for td */
  711.   static unsigned l[256]; /* bit lengths for codes */
  712.  
  713.  
  714.   /* Tune base table sizes.  Note: I thought that to truly optimize speed,
  715.      I would have to select different bl, bd, and bb values for different
  716.      compressed file sizes.  I was suprised to find out the the values of
  717.      7, 7, and 9 worked best over a very wide range of sizes, except that
  718.      bd = 8 worked marginally better for large compressed sizes. */
  719.   bl = 7;
  720.   bd = csize > 200000L ? 8 : 7;
  721.  
  722.  
  723.   /* With literal tree--minimum match length is 3 */
  724.   hufts = 0;                    /* initialize huft's malloc'ed */
  725.   if (lrec.general_purpose_bit_flag & 4)
  726.   {
  727.     bb = 9;                     /* base table size for literals */
  728.     if ((r = get_tree(l, 256)) != 0)
  729.       return (int)r;
  730.     if ((r = huft_build(l, 256, 256, NULL, NULL, &tb, &bb)) != 0)
  731.     {
  732.       if (r == 1)
  733.         huft_free(tb);
  734.       return (int)r;
  735.     }
  736.     if ((r = get_tree(l, 64)) != 0)
  737.       return (int)r;
  738.     if ((r = huft_build(l, 64, 0, cplen3, extra, &tl, &bl)) != 0)
  739.     {
  740.       if (r == 1)
  741.         huft_free(tl);
  742.       huft_free(tb);
  743.       return (int)r;
  744.     }
  745.     if ((r = get_tree(l, 64)) != 0)
  746.       return (int)r;
  747.     if (lrec.general_purpose_bit_flag & 2)      /* true if 8K */
  748.     {
  749.       if ((r = huft_build(l, 64, 0, cpdist8, extra, &td, &bd)) != 0)
  750.       {
  751.         if (r == 1)
  752.           huft_free(td);
  753.         huft_free(tl);
  754.         huft_free(tb);
  755.         return (int)r;
  756.       }
  757.       r = explode_lit8(tb, tl, td, bb, bl, bd);
  758.     }
  759.     else                                        /* else 4K */
  760.     {
  761.       if ((r = huft_build(l, 64, 0, cpdist4, extra, &td, &bd)) != 0)
  762.       {
  763.         if (r == 1)
  764.           huft_free(td);
  765.         huft_free(tl);
  766.         huft_free(tb);
  767.         return (int)r;
  768.       }
  769.       r = explode_lit4(tb, tl, td, bb, bl, bd);
  770.     }
  771.     huft_free(td);
  772.     huft_free(tl);
  773.     huft_free(tb);
  774.   }
  775.   else
  776.  
  777.  
  778.   /* No literal tree--minimum match length is 2 */
  779.   {
  780.     if ((r = get_tree(l, 64)) != 0)
  781.       return (int)r;
  782.     if ((r = huft_build(l, 64, 0, cplen2, extra, &tl, &bl)) != 0)
  783.     {
  784.       if (r == 1)
  785.         huft_free(tl);
  786.       return (int)r;
  787.     }
  788.     if ((r = get_tree(l, 64)) != 0)
  789.       return (int)r;
  790.     if (lrec.general_purpose_bit_flag & 2)      /* true if 8K */
  791.     {
  792.       if ((r = huft_build(l, 64, 0, cpdist8, extra, &td, &bd)) != 0)
  793.       {
  794.         if (r == 1)
  795.           huft_free(td);
  796.         huft_free(tl);
  797.         return (int)r;
  798.       }
  799.       r = explode_nolit8(tl, td, bl, bd);
  800.     }
  801.     else                                        /* else 4K */
  802.     {
  803.       if ((r = huft_build(l, 64, 0, cpdist4, extra, &td, &bd)) != 0)
  804.       {
  805.         if (r == 1)
  806.           huft_free(td);
  807.         huft_free(tl);
  808.         return (int)r;
  809.       }
  810.       r = explode_nolit4(tl, td, bl, bd);
  811.     }
  812.     huft_free(td);
  813.     huft_free(tl);
  814.   }
  815. #ifdef DEBUG
  816.   fprintf(stderr, "<%u > ", hufts);
  817. #endif /* DEBUG */
  818.   return (int)r;
  819. }
  820.  
  821. /* so explode.c and inflate.c can be compiled together into one object: */
  822. #undef NEXTBYTE
  823. #undef NEEDBITS
  824. #undef DUMPBITS
  825.