]>
git.saurik.com Git - wxWidgets.git/blob - utils/Install/packzip/explode.c
   1 /* explode.c -- put in the public domain by Mark Adler 
   2    version c15, 6 July 1996 */ 
   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. 
  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 
  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     c16   8 Feb 98  C. Spieler      added ZCONST modifiers to const tables 
  40                                     and #ifdef DEBUG around debugging code. 
  41     c16b 25 Mar 98  C. Spieler      modified DLL code for slide redirection. 
  46    Explode imploded (PKZIP method 6 compressed) data.  This compression 
  47    method searches for as much of the current string of bytes (up to a length 
  48    of ~320) in the previous 4K or 8K bytes.  If it doesn't find any matches 
  49    (of at least length 2 or 3), it codes the next byte.  Otherwise, it codes 
  50    the length of the matched string and its distance backwards from the 
  51    current position.  Single bytes ("literals") are preceded by a one (a 
  52    single bit) and are either uncoded (the eight bits go directly into the 
  53    compressed stream for a total of nine bits) or Huffman coded with a 
  54    supplied literal code tree.  If literals are coded, then the minimum match 
  55    length is three, otherwise it is two. 
  57    There are therefore four kinds of imploded streams: 8K search with coded 
  58    literals (min match = 3), 4K search with coded literals (min match = 3), 
  59    8K with uncoded literals (min match = 2), and 4K with uncoded literals 
  60    (min match = 2).  The kind of stream is identified in two bits of a 
  61    general purpose bit flag that is outside of the compressed stream. 
  63    Distance-length pairs for matched strings are preceded by a zero bit (to 
  64    distinguish them from literals) and are always coded.  The distance comes 
  65    first and is either the low six (4K) or low seven (8K) bits of the 
  66    distance (uncoded), followed by the high six bits of the distance coded. 
  67    Then the length is six bits coded (0..63 + min match length), and if the 
  68    maximum such length is coded, then it's followed by another eight bits 
  69    (uncoded) to be added to the coded length.  This gives a match length 
  70    range of 2..320 or 3..321 bytes. 
  72    The literal, length, and distance codes are all represented in a slightly 
  73    compressed form themselves.  What is sent are the lengths of the codes for 
  74    each value, which is sufficient to construct the codes.  Each byte of the 
  75    code representation is the code length (the low four bits representing 
  76    1..16), and the number of values sequentially with that length (the high 
  77    four bits also representing 1..16).  There are 256 literal code values (if 
  78    literals are coded), 64 length code values, and 64 distance code values, 
  79    in that order at the beginning of the compressed stream.  Each set of code 
  80    values is preceded (redundantly) with a byte indicating how many bytes are 
  81    in the code description that follows, in the range 1..256. 
  83    The codes themselves are decoded using tables made by huft_build() from 
  84    the bit lengths.  That routine and its comments are in the inflate.c 
  88 #define UNZIP_INTERNAL 
  89 #include "unzip.h"      /* must supply slide[] (uch) array and NEXTBYTE macro */ 
  92 #  define WSIZE 0x8000  /* window size--must be a power of two, and */ 
  93 #endif                  /* at least 8K for zip's implode method */ 
  95 #if (defined(DLL) && !defined(NO_SLIDE_REDIR)) 
  96 #  define wsize G._wsize 
 102 static int get_tree 
OF((__GPRO__ 
unsigned *l
, unsigned n
)); 
 103 static int explode_lit8 
OF((__GPRO__ 
struct huft 
*tb
, struct huft 
*tl
, 
 104                             struct huft 
*td
, int bb
, int bl
, int bd
)); 
 105 static int explode_lit4 
OF((__GPRO__ 
struct huft 
*tb
, struct huft 
*tl
, 
 106                             struct huft 
*td
, int bb
, int bl
, int bd
)); 
 107 static int explode_nolit8 
OF((__GPRO__ 
struct huft 
*tl
, struct huft 
*td
, 
 109 static int explode_nolit4 
OF((__GPRO__ 
struct huft 
*tl
, struct huft 
*td
, 
 111 int explode 
OF((__GPRO
)); 
 114 /* The implode algorithm uses a sliding 4K or 8K byte window on the 
 115    uncompressed stream to find repeated byte strings.  This is implemented 
 116    here as a circular buffer.  The index is updated simply by incrementing 
 117    and then and'ing with 0x0fff (4K-1) or 0x1fff (8K-1).  Here, the 32K 
 118    buffer of inflate is used, and it works just as well to always have 
 119    a 32K circular buffer, so the index is anded with 0x7fff.  This is 
 120    done to allow the window to also be used as the output buffer. */ 
 121 /* This must be supplied in an external module useable like "uch slide[8192];" 
 122    or "uch *slide;", where the latter would be malloc'ed.  In unzip, slide[] 
 123    is actually a 32K area for use by inflate, which uses a 32K sliding window. 
 127 /* Tables for length and distance */ 
 128 static ZCONST ush cplen2
[] = 
 129         {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 
 130         18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 
 131         35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 
 132         52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65}; 
 133 static ZCONST ush cplen3
[] = 
 134         {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 
 135         19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 
 136         36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 
 137         53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66}; 
 138 static ZCONST ush extra
[] = 
 139         {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, 
 143 static ZCONST ush cpdist4
[] = 
 144         {1, 65, 129, 193, 257, 321, 385, 449, 513, 577, 641, 705, 
 145         769, 833, 897, 961, 1025, 1089, 1153, 1217, 1281, 1345, 1409, 1473, 
 146         1537, 1601, 1665, 1729, 1793, 1857, 1921, 1985, 2049, 2113, 2177, 
 147         2241, 2305, 2369, 2433, 2497, 2561, 2625, 2689, 2753, 2817, 2881, 
 148         2945, 3009, 3073, 3137, 3201, 3265, 3329, 3393, 3457, 3521, 3585, 
 149         3649, 3713, 3777, 3841, 3905, 3969, 4033}; 
 150 static ZCONST ush cpdist8
[] = 
 151         {1, 129, 257, 385, 513, 641, 769, 897, 1025, 1153, 1281, 
 152         1409, 1537, 1665, 1793, 1921, 2049, 2177, 2305, 2433, 2561, 2689, 
 153         2817, 2945, 3073, 3201, 3329, 3457, 3585, 3713, 3841, 3969, 4097, 
 154         4225, 4353, 4481, 4609, 4737, 4865, 4993, 5121, 5249, 5377, 5505, 
 155         5633, 5761, 5889, 6017, 6145, 6273, 6401, 6529, 6657, 6785, 6913, 
 156         7041, 7169, 7297, 7425, 7553, 7681, 7809, 7937, 8065}; 
 159 /* Macros for inflate() bit peeking and grabbing. 
 163         x = b & mask_bits[j]; 
 166    where NEEDBITS makes sure that b has at least j bits in it, and 
 167    DUMPBITS removes the bits from b.  The macros use the variable k 
 168    for the number of bits in b.  Normally, b and k are register 
 172 #define NEEDBITS(n) {while(k<(n)){b|=((ulg)NEXTBYTE)<<k;k+=8;}} 
 173 #define DUMPBITS(n) {b>>=(n);k-=(n);} 
 177 static int get_tree(__G__ l
, n
) 
 179 unsigned *l
;            /* bit lengths */ 
 180 unsigned n
;             /* number expected */ 
 181 /* Get the bit lengths for a code representation from the compressed 
 182    stream.  If get_tree() returns 4, then there is an error in the data. 
 183    Otherwise zero is returned. */ 
 185   unsigned i
;           /* bytes remaining in list */ 
 186   unsigned k
;           /* lengths entered */ 
 187   unsigned j
;           /* number of codes */ 
 188   unsigned b
;           /* bit length for those codes */ 
 191   /* get bit lengths */ 
 192   i 
= NEXTBYTE 
+ 1;                     /* length/count pairs to read */ 
 193   k 
= 0;                                /* next code */ 
 195     b 
= ((j 
= NEXTBYTE
) & 0xf) + 1;     /* bits in code (1..16) */ 
 196     j 
= ((j 
& 0xf0) >> 4) + 1;          /* codes with those bits (1..16) */ 
 198       return 4;                         /* don't overflow l[] */ 
 203   return k 
!= n 
? 4 : 0;                /* should have read n of them */ 
 208 static int explode_lit8(__G__ tb
, tl
, td
, bb
, bl
, bd
) 
 210 struct huft 
*tb
, *tl
, *td
;      /* literal, length, and distance tables */ 
 211 int bb
, bl
, bd
;                 /* number of bits decoded by those */ 
 212 /* Decompress the imploded data using coded literals and an 8K sliding 
 215   long s
;               /* bytes to decompress */ 
 216   register unsigned e
;  /* table entry flag/number of extra bits */ 
 217   unsigned n
, d
;        /* length and index for copy */ 
 218   unsigned w
;           /* current window position */ 
 219   struct huft 
*t
;       /* pointer to table entry */ 
 220   unsigned mb
, ml
, md
;  /* masks for bb, bl, and bd bits */ 
 221   register ulg b
;       /* bit buffer */ 
 222   register unsigned k
;  /* number of bits in bit buffer */ 
 223   unsigned u
;           /* true if unflushed */ 
 226   /* explode the coded data */ 
 227   b 
= k 
= w 
= 0;                /* initialize bit buffer, window */ 
 228   u 
= 1;                        /* buffer unflushed */ 
 229   mb 
= mask_bits
[bb
];           /* precompute masks for speed */ 
 233   while (s 
> 0)                 /* do until ucsize bytes uncompressed */ 
 236     if (b 
& 1)                  /* then literal--decode it */ 
 240       NEEDBITS((unsigned)bb
)    /* get coded literal */ 
 241       if ((e 
= (t 
= tb 
+ ((~(unsigned)b
) & mb
))->e
) > 16) 
 248         } while ((e 
= (t 
= t
->v
.t 
+ ((~(unsigned)b
) & mask_bits
[e
]))->e
) > 16); 
 250       redirSlide
[w
++] = (uch
)t
->v
.n
; 
 253         flush(__G__ redirSlide
, (ulg
)w
, 0); 
 257     else                        /* else distance/length */ 
 260       NEEDBITS(7)               /* get distance low bits */ 
 261       d 
= (unsigned)b 
& 0x7f; 
 263       NEEDBITS((unsigned)bd
)    /* get coded distance high bits */ 
 264       if ((e 
= (t 
= td 
+ ((~(unsigned)b
) & md
))->e
) > 16) 
 271         } while ((e 
= (t 
= t
->v
.t 
+ ((~(unsigned)b
) & mask_bits
[e
]))->e
) > 16); 
 273       d 
= w 
- d 
- t
->v
.n
;       /* construct offset */ 
 274       NEEDBITS((unsigned)bl
)    /* get coded length */ 
 275       if ((e 
= (t 
= tl 
+ ((~(unsigned)b
) & ml
))->e
) > 16) 
 282         } while ((e 
= (t 
= t
->v
.t 
+ ((~(unsigned)b
) & mask_bits
[e
]))->e
) > 16); 
 285       if (e
)                    /* get length extra bits */ 
 288         n 
+= (unsigned)b 
& 0xff; 
 295 #if (defined(DLL) && !defined(NO_SLIDE_REDIR)) 
 296         if (G
.redirect_slide
) { 
 297           /* &= w/ wsize not needed and wrong if redirect */ 
 300           n 
-= (e 
= (e 
= wsize 
- (d 
> w 
? d 
: w
)) > n 
? n 
: e
); 
 303         n 
-= (e 
= (e 
= wsize 
- ((d 
&= wsize
-1) > w 
? d 
: w
)) > n 
? n 
: e
); 
 306           memzero(redirSlide 
+ w
, e
); 
 312           if (w 
- d 
>= e
)       /* (this test assumes unsigned comparison) */ 
 314             memcpy(redirSlide 
+ w
, redirSlide 
+ d
, e
); 
 318           else                  /* do it slow to avoid memcpy() overlap */ 
 319 #endif /* !NOMEMCPY */ 
 321               redirSlide
[w
++] = redirSlide
[d
++]; 
 325           flush(__G__ redirSlide
, (ulg
)w
, 0); 
 332   /* flush out redirSlide */ 
 333   flush(__G__ redirSlide
, (ulg
)w
, 0); 
 334   if (G
.csize 
+ G
.incnt 
+ (k 
>> 3))   /* should have read csize bytes, but */ 
 335   {                        /* sometimes read one too many:  k>>3 compensates */ 
 336     G
.used_csize 
= G
.lrec
.csize 
- G
.csize 
- G
.incnt 
- (k 
>> 3); 
 344 static int explode_lit4(__G__ tb
, tl
, td
, bb
, bl
, bd
) 
 346 struct huft 
*tb
, *tl
, *td
;      /* literal, length, and distance tables */ 
 347 int bb
, bl
, bd
;                 /* number of bits decoded by those */ 
 348 /* Decompress the imploded data using coded literals and a 4K sliding 
 351   long s
;               /* bytes to decompress */ 
 352   register unsigned e
;  /* table entry flag/number of extra bits */ 
 353   unsigned n
, d
;        /* length and index for copy */ 
 354   unsigned w
;           /* current window position */ 
 355   struct huft 
*t
;       /* pointer to table entry */ 
 356   unsigned mb
, ml
, md
;  /* masks for bb, bl, and bd bits */ 
 357   register ulg b
;       /* bit buffer */ 
 358   register unsigned k
;  /* number of bits in bit buffer */ 
 359   unsigned u
;           /* true if unflushed */ 
 362   /* explode the coded data */ 
 363   b 
= k 
= w 
= 0;                /* initialize bit buffer, window */ 
 364   u 
= 1;                        /* buffer unflushed */ 
 365   mb 
= mask_bits
[bb
];           /* precompute masks for speed */ 
 369   while (s 
> 0)                 /* do until ucsize bytes uncompressed */ 
 372     if (b 
& 1)                  /* then literal--decode it */ 
 376       NEEDBITS((unsigned)bb
)    /* get coded literal */ 
 377       if ((e 
= (t 
= tb 
+ ((~(unsigned)b
) & mb
))->e
) > 16) 
 384         } while ((e 
= (t 
= t
->v
.t 
+ ((~(unsigned)b
) & mask_bits
[e
]))->e
) > 16); 
 386       redirSlide
[w
++] = (uch
)t
->v
.n
; 
 389         flush(__G__ redirSlide
, (ulg
)w
, 0); 
 393     else                        /* else distance/length */ 
 396       NEEDBITS(6)               /* get distance low bits */ 
 397       d 
= (unsigned)b 
& 0x3f; 
 399       NEEDBITS((unsigned)bd
)    /* get coded distance high bits */ 
 400       if ((e 
= (t 
= td 
+ ((~(unsigned)b
) & md
))->e
) > 16) 
 407         } while ((e 
= (t 
= t
->v
.t 
+ ((~(unsigned)b
) & mask_bits
[e
]))->e
) > 16); 
 409       d 
= w 
- d 
- t
->v
.n
;       /* construct offset */ 
 410       NEEDBITS((unsigned)bl
)    /* get coded length */ 
 411       if ((e 
= (t 
= tl 
+ ((~(unsigned)b
) & ml
))->e
) > 16) 
 418         } while ((e 
= (t 
= t
->v
.t 
+ ((~(unsigned)b
) & mask_bits
[e
]))->e
) > 16); 
 421       if (e
)                    /* get length extra bits */ 
 424         n 
+= (unsigned)b 
& 0xff; 
 431 #if (defined(DLL) && !defined(NO_SLIDE_REDIR)) 
 432         if (G
.redirect_slide
) { 
 433           /* &= w/ wsize not needed and wrong if redirect */ 
 436           n 
-= (e 
= (e 
= wsize 
- (d 
> w 
? d 
: w
)) > n 
? n 
: e
); 
 439         n 
-= (e 
= (e 
= wsize 
- ((d 
&= wsize
-1) > w 
? d 
: w
)) > n 
? n 
: e
); 
 442           memzero(redirSlide 
+ w
, e
); 
 448           if (w 
- d 
>= e
)       /* (this test assumes unsigned comparison) */ 
 450             memcpy(redirSlide 
+ w
, redirSlide 
+ d
, e
); 
 454           else                  /* do it slow to avoid memcpy() overlap */ 
 455 #endif /* !NOMEMCPY */ 
 457               redirSlide
[w
++] = redirSlide
[d
++]; 
 461           flush(__G__ redirSlide
, (ulg
)w
, 0); 
 468   /* flush out redirSlide */ 
 469   flush(__G__ redirSlide
, (ulg
)w
, 0); 
 470   if (G
.csize 
+ G
.incnt 
+ (k 
>> 3))   /* should have read csize bytes, but */ 
 471   {                        /* sometimes read one too many:  k>>3 compensates */ 
 472     G
.used_csize 
= G
.lrec
.csize 
- G
.csize 
- G
.incnt 
- (k 
>> 3); 
 480 static int explode_nolit8(__G__ tl
, td
, bl
, bd
) 
 482 struct huft 
*tl
, *td
;   /* length and distance decoder tables */ 
 483 int bl
, bd
;             /* number of bits decoded by tl[] and td[] */ 
 484 /* Decompress the imploded data using uncoded literals and an 8K sliding 
 487   long s
;               /* bytes to decompress */ 
 488   register unsigned e
;  /* table entry flag/number of extra bits */ 
 489   unsigned n
, d
;        /* length and index for copy */ 
 490   unsigned w
;           /* current window position */ 
 491   struct huft 
*t
;       /* pointer to table entry */ 
 492   unsigned ml
, md
;      /* masks for bl and bd bits */ 
 493   register ulg b
;       /* bit buffer */ 
 494   register unsigned k
;  /* number of bits in bit buffer */ 
 495   unsigned u
;           /* true if unflushed */ 
 498   /* explode the coded data */ 
 499   b 
= k 
= w 
= 0;                /* initialize bit buffer, window */ 
 500   u 
= 1;                        /* buffer unflushed */ 
 501   ml 
= mask_bits
[bl
];           /* precompute masks for speed */ 
 504   while (s 
> 0)                 /* do until ucsize bytes uncompressed */ 
 507     if (b 
& 1)                  /* then literal--get eight bits */ 
 512       redirSlide
[w
++] = (uch
)b
; 
 515         flush(__G__ redirSlide
, (ulg
)w
, 0); 
 520     else                        /* else distance/length */ 
 523       NEEDBITS(7)               /* get distance low bits */ 
 524       d 
= (unsigned)b 
& 0x7f; 
 526       NEEDBITS((unsigned)bd
)    /* get coded distance high bits */ 
 527       if ((e 
= (t 
= td 
+ ((~(unsigned)b
) & md
))->e
) > 16) 
 534         } while ((e 
= (t 
= t
->v
.t 
+ ((~(unsigned)b
) & mask_bits
[e
]))->e
) > 16); 
 536       d 
= w 
- d 
- t
->v
.n
;       /* construct offset */ 
 537       NEEDBITS((unsigned)bl
)    /* get coded length */ 
 538       if ((e 
= (t 
= tl 
+ ((~(unsigned)b
) & ml
))->e
) > 16) 
 545         } while ((e 
= (t 
= t
->v
.t 
+ ((~(unsigned)b
) & mask_bits
[e
]))->e
) > 16); 
 548       if (e
)                    /* get length extra bits */ 
 551         n 
+= (unsigned)b 
& 0xff; 
 558 #if (defined(DLL) && !defined(NO_SLIDE_REDIR)) 
 559         if (G
.redirect_slide
) { 
 560           /* &= w/ wsize not needed and wrong if redirect */ 
 563           n 
-= (e 
= (e 
= wsize 
- (d 
> w 
? d 
: w
)) > n 
? n 
: e
); 
 566         n 
-= (e 
= (e 
= wsize 
- ((d 
&= wsize
-1) > w 
? d 
: w
)) > n 
? n 
: e
); 
 569           memzero(redirSlide 
+ w
, e
); 
 575           if (w 
- d 
>= e
)       /* (this test assumes unsigned comparison) */ 
 577             memcpy(redirSlide 
+ w
, redirSlide 
+ d
, e
); 
 581           else                  /* do it slow to avoid memcpy() overlap */ 
 582 #endif /* !NOMEMCPY */ 
 584               redirSlide
[w
++] = redirSlide
[d
++]; 
 588           flush(__G__ redirSlide
, (ulg
)w
, 0); 
 595   /* flush out redirSlide */ 
 596   flush(__G__ redirSlide
, (ulg
)w
, 0); 
 597   if (G
.csize 
+ G
.incnt 
+ (k 
>> 3))   /* should have read csize bytes, but */ 
 598   {                        /* sometimes read one too many:  k>>3 compensates */ 
 599     G
.used_csize 
= G
.lrec
.csize 
- G
.csize 
- G
.incnt 
- (k 
>> 3); 
 607 static int explode_nolit4(__G__ tl
, td
, bl
, bd
) 
 609 struct huft 
*tl
, *td
;   /* length and distance decoder tables */ 
 610 int bl
, bd
;             /* number of bits decoded by tl[] and td[] */ 
 611 /* Decompress the imploded data using uncoded literals and a 4K sliding 
 614   long s
;               /* bytes to decompress */ 
 615   register unsigned e
;  /* table entry flag/number of extra bits */ 
 616   unsigned n
, d
;        /* length and index for copy */ 
 617   unsigned w
;           /* current window position */ 
 618   struct huft 
*t
;       /* pointer to table entry */ 
 619   unsigned ml
, md
;      /* masks for bl and bd bits */ 
 620   register ulg b
;       /* bit buffer */ 
 621   register unsigned k
;  /* number of bits in bit buffer */ 
 622   unsigned u
;           /* true if unflushed */ 
 625   /* explode the coded data */ 
 626   b 
= k 
= w 
= 0;                /* initialize bit buffer, window */ 
 627   u 
= 1;                        /* buffer unflushed */ 
 628   ml 
= mask_bits
[bl
];           /* precompute masks for speed */ 
 631   while (s 
> 0)                 /* do until ucsize bytes uncompressed */ 
 634     if (b 
& 1)                  /* then literal--get eight bits */ 
 639       redirSlide
[w
++] = (uch
)b
; 
 642         flush(__G__ redirSlide
, (ulg
)w
, 0); 
 647     else                        /* else distance/length */ 
 650       NEEDBITS(6)               /* get distance low bits */ 
 651       d 
= (unsigned)b 
& 0x3f; 
 653       NEEDBITS((unsigned)bd
)    /* get coded distance high bits */ 
 654       if ((e 
= (t 
= td 
+ ((~(unsigned)b
) & md
))->e
) > 16) 
 661         } while ((e 
= (t 
= t
->v
.t 
+ ((~(unsigned)b
) & mask_bits
[e
]))->e
) > 16); 
 663       d 
= w 
- d 
- t
->v
.n
;       /* construct offset */ 
 664       NEEDBITS((unsigned)bl
)    /* get coded length */ 
 665       if ((e 
= (t 
= tl 
+ ((~(unsigned)b
) & ml
))->e
) > 16) 
 672         } while ((e 
= (t 
= t
->v
.t 
+ ((~(unsigned)b
) & mask_bits
[e
]))->e
) > 16); 
 675       if (e
)                    /* get length extra bits */ 
 678         n 
+= (unsigned)b 
& 0xff; 
 685 #if (defined(DLL) && !defined(NO_SLIDE_REDIR)) 
 686         if (G
.redirect_slide
) { 
 687           /* &= w/ wsize not needed and wrong if redirect */ 
 690           n 
-= (e 
= (e 
= wsize 
- (d 
> w 
? d 
: w
)) > n 
? n 
: e
); 
 693         n 
-= (e 
= (e 
= wsize 
- ((d 
&= wsize
-1) > w 
? d 
: w
)) > n 
? n 
: e
); 
 696           memzero(redirSlide 
+ w
, e
); 
 702           if (w 
- d 
>= e
)       /* (this test assumes unsigned comparison) */ 
 704             memcpy(redirSlide 
+ w
, redirSlide 
+ d
, e
); 
 708           else                  /* do it slow to avoid memcpy() overlap */ 
 709 #endif /* !NOMEMCPY */ 
 711               redirSlide
[w
++] = redirSlide
[d
++]; 
 715           flush(__G__ redirSlide
, (ulg
)w
, 0); 
 722   /* flush out redirSlide */ 
 723   flush(__G__ redirSlide
, (ulg
)w
, 0); 
 724   if (G
.csize 
+ G
.incnt 
+ (k 
>> 3))   /* should have read csize bytes, but */ 
 725   {                        /* sometimes read one too many:  k>>3 compensates */ 
 726     G
.used_csize 
= G
.lrec
.csize 
- G
.csize 
- G
.incnt 
- (k 
>> 3); 
 736 /* Explode an imploded compressed stream.  Based on the general purpose 
 737    bit flag, decide on coded or uncoded literals, and an 8K or 4K sliding 
 738    window.  Construct the literal (if any), length, and distance codes and 
 739    the tables needed to decode them (using huft_build() from inflate.c), 
 740    and call the appropriate routine for the type of data in the remainder 
 741    of the stream.  The four routines are nearly identical, differing only 
 742    in whether the literal is decoded or simply read in, and in how many 
 743    bits are read in, uncoded, for the low distance bits. */ 
 745   unsigned r
;           /* return codes */ 
 746   struct huft 
*tb
;      /* literal code table */ 
 747   struct huft 
*tl
;      /* length code table */ 
 748   struct huft 
*td
;      /* distance code table */ 
 749   int bb
;               /* bits for tb */ 
 750   int bl
;               /* bits for tl */ 
 751   int bd
;               /* bits for td */ 
 752   unsigned l
[256];      /* bit lengths for codes */ 
 754 #if (defined(DLL) && !defined(NO_SLIDE_REDIR)) 
 755   if (G
.redirect_slide
) 
 756     wsize 
= G
.redirect_size
, redirSlide 
= G
.redirect_buffer
; 
 758     wsize 
= WSIZE
, redirSlide 
= slide
; 
 761   /* Tune base table sizes.  Note: I thought that to truly optimize speed, 
 762      I would have to select different bl, bd, and bb values for different 
 763      compressed file sizes.  I was surprised to find out that the values of 
 764      7, 7, and 9 worked best over a very wide range of sizes, except that 
 765      bd = 8 worked marginally better for large compressed sizes. */ 
 767   bd 
= (G
.csize 
+ G
.incnt
) > 200000L ? 8 : 7; 
 770   /* With literal tree--minimum match length is 3 */ 
 772   G
.hufts 
= 0;                    /* initialize huft's malloc'ed */ 
 774   if (G
.lrec
.general_purpose_bit_flag 
& 4) 
 776     bb 
= 9;                     /* base table size for literals */ 
 777     if ((r 
= get_tree(__G__ l
, 256)) != 0) 
 779     if ((r 
= huft_build(__G__ l
, 256, 256, NULL
, NULL
, &tb
, &bb
)) != 0) 
 785     if ((r 
= get_tree(__G__ l
, 64)) != 0) 
 787     if ((r 
= huft_build(__G__ l
, 64, 0, cplen3
, extra
, &tl
, &bl
)) != 0) 
 794     if ((r 
= get_tree(__G__ l
, 64)) != 0) 
 796     if (G
.lrec
.general_purpose_bit_flag 
& 2)      /* true if 8K */ 
 798       if ((r 
= huft_build(__G__ l
, 64, 0, cpdist8
, extra
, &td
, &bd
)) != 0) 
 806       r 
= explode_lit8(__G__ tb
, tl
, td
, bb
, bl
, bd
); 
 810       if ((r 
= huft_build(__G__ l
, 64, 0, cpdist4
, extra
, &td
, &bd
)) != 0) 
 818       r 
= explode_lit4(__G__ tb
, tl
, td
, bb
, bl
, bd
); 
 827   /* No literal tree--minimum match length is 2 */ 
 829     if ((r 
= get_tree(__G__ l
, 64)) != 0) 
 831     if ((r 
= huft_build(__G__ l
, 64, 0, cplen2
, extra
, &tl
, &bl
)) != 0) 
 837     if ((r 
= get_tree(__G__ l
, 64)) != 0) 
 839     if (G
.lrec
.general_purpose_bit_flag 
& 2)      /* true if 8K */ 
 841       if ((r 
= huft_build(__G__ l
, 64, 0, cpdist8
, extra
, &td
, &bd
)) != 0) 
 848       r 
= explode_nolit8(__G__ tl
, td
, bl
, bd
); 
 852       if ((r 
= huft_build(__G__ l
, 64, 0, cpdist4
, extra
, &td
, &bd
)) != 0) 
 859       r 
= explode_nolit4(__G__ tl
, td
, bl
, bd
); 
 864   Trace((stderr
, "<%u > ", G
.hufts
)); 
 868 /* so explode.c and inflate.c can be compiled together into one object: */