home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / unzip512.zip / explode.c < prev    next >
C/C++ Source or Header  |  1994-08-25  |  29KB  |  826 lines

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