home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / unzip531.zip / explode.c < prev    next >
C/C++ Source or Header  |  1996-07-06  |  30KB  |  857 lines

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