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

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