Subversion Repositories autosfx

Rev

Blame | Last modification | View Log | RSS feed

  1. #include "stdafx.h"
  2. #pragma hdrstop
  3.  
  4. #include <stdio.h>
  5. #include "UnzInf.h"
  6. #include "dz_errs.h"
  7.  
  8. #undef _DZ_FILE_
  9. #define _DZ_FILE_ DZ_UINFLATE_CPP
  10. /*
  11.   Inflate.c -
  12.  
  13.   Copyright (c) 1990-2007 Info-ZIP.  All rights reserved.
  14.  
  15.   See the accompanying file LICENSE, version 2007-Mar-4 or later
  16.   (the contents of which are also included in zip.h) for terms of use.
  17.   If, for some reason, all these files are missing, the Info-ZIP license
  18.   also may be found at:  ftp://ftp.info-zip.org/pub/infozip/license.html
  19.  
  20.   parts Copyright (C) 1997 Mike White, Eric W. Engler
  21. ************************************************************************
  22.  Copyright (C) 2009, 2010  by Russell J. Peters, Roger Aelbrecht
  23.  
  24.    This file is part of TZipMaster Version 1.9.
  25.  
  26.     TZipMaster is free software: you can redistribute it and/or modify
  27.     it under the terms of the GNU Lesser General Public License as published by
  28.     the Free Software Foundation, either version 3 of the License, or
  29.     (at your option) any later version.
  30.  
  31.     TZipMaster is distributed in the hope that it will be useful,
  32.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  33.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  34.     GNU Lesser General Public License for more details.
  35.  
  36.     You should have received a copy of the GNU Lesser General Public License
  37.     along with TZipMaster.  If not, see <http://www.gnu.org/licenses/>.
  38.  
  39.     contact: problems@delphizip.org (include ZipMaster in the subject).
  40.     updates: http://www.delphizip.org
  41.     DelphiZip maillist subscribe at http://www.freelists.org/list/delphizip
  42. ************************************************************************/
  43.  
  44. /* inflate.c -- put in the public domain by Mark Adler
  45.  * This version modified by Chris Vleghert and Eric W. Engler
  46.  * for BCB/Delphi Zip, Jun 18, 2000.
  47.  */
  48.  
  49. /* Inflate deflated (PKZIP's method 8 compressed) data.  The compression
  50.  * method searches for as much of the current string of bytes (up to a
  51.  * length of 258) in the previous 32K bytes.  If it doesn't find any
  52.  * matches (of at least length 3), it codes the next byte.  Otherwise, it
  53.  * codes the length of the matched string and its distance backwards from
  54.  * the current position.  There is a single Huffman code that codes both
  55.  * single bytes (called "literals") and match lengths.  A second Huffman
  56.  * code codes the distance information, which follows a length code.  Each
  57.  * length or distance code actually represents a base value and a number
  58.  * of "extra" (sometimes zero) bits to get to add to the base value.  At
  59.  * the end of each deflated block is a special end-of-block (EOB) literal/
  60.  * length code.  The decoding process is basically: get a literal/length
  61.  * code; if EOB then done; if a literal, emit the decoded byte; if a
  62.  * length then get the distance and emit the referred-to bytes from the
  63.  * sliding window of previously emitted data.
  64.  *
  65.  * There are (currently) three kinds of inflate blocks: stored, fixed, and
  66.  * dynamic.  The compressor outputs a chunk of data at a time and decides
  67.  * which method to use on a chunk-by-chunk basis.  A chunk might typically
  68.  * be 32K to 64K, uncompressed.  If the chunk is uncompressible, then the
  69.  * "stored" method is used.  In this case, the bytes are simply stored as
  70.  * is, eight bits per byte, with none of the above coding.  The bytes are
  71.  * preceded by a count, since there is no longer an EOB code.
  72.  *
  73.  * If the data are compressible, then either the fixed or dynamic methods
  74.  * are used.  In the dynamic method, the compressed data are preceded by
  75.  * an encoding of the literal/length and distance Huffman codes that are
  76.  * to be used to decode this block.  The representation is itself Huffman
  77.  * coded, and so is preceded by a description of that code.  These code
  78.  * descriptions take up a little space, and so for small blocks, there is
  79.  * a predefined set of codes, called the fixed codes.  The fixed method is
  80.  * used if the block ends up smaller that way (usually for quite small
  81.  * chunks); otherwise the dynamic method is used.  In the latter case, the
  82.  * codes are customized to the probabilities in the current block and so
  83.  * can code it much better than the pre-determined fixed codes can.
  84.  *
  85.  * The Huffman codes themselves are decoded using a multi-level table
  86.  * lookup, in order to maximize the speed of decoding plus the speed of
  87.  * building the decoding tables.  See the comments below that precede the
  88.  * lbits and dbits tuning parameters.
  89.  * GRR:  return values(?)
  90.  *         0  OK
  91.  *         1  incomplete table
  92.  *         2  bad input
  93.  *         3  not enough memory
  94.  */
  95.  
  96. /*
  97.  * Notes beyond the 1.93a appnote.txt:
  98.  *  1. Distance pointers never point before the beginning of the output
  99.  *     stream.
  100.  *  2. Distance pointers can point back across blocks, up to 32k away.
  101.  *  3. There is an implied maximum of 7 bits for the bit length table and
  102.  *     15 bits for the actual data.
  103.  *  4. If only one code exists, then it is encoded using one bit.  (Zero
  104.  *     would be more efficient, but perhaps a little confusing.)  If two
  105.  *     codes exist, they are coded using one bit each (0 and 1).
  106.  *  5. There is no way of sending zero distance codes--a dummy must be
  107.  *     sent if there are none.  (History: a pre 2.0 version of PKZIP would
  108.  *     store blocks with no distance codes, but this was discovered to be
  109.  *     too harsh a criterion.)  Valid only for 1.93a.  2.04c does allow
  110.  *     zero distance codes, which is sent as one code of zero bits in
  111.  *     length.
  112.  *  6. There are up to 286 literal/length codes.  Code 256 represents the
  113.  *     end-of-block.  Note however that the static length tree defines
  114.  *     288 codes just to fill out the Huffman codes.  Codes 286 and 287
  115.  *     cannot be used though, since there is no length base or extra bits
  116.  *     defined for them.  Similarily, there are up to 30 distance codes.
  117.  *     However, static trees define 32 codes (all 5 bits) to fill out the
  118.  *     Huffman codes, but the last two had better not show up in the data.
  119.  *  7. Unzip can check dynamic Huffman blocks for complete code sets.
  120.  *     The exception is that a single code would not be complete (see #4).
  121.  *  8. The five bits following the block type is really the number of
  122.  *     literal codes sent minus 257.
  123.  *  9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
  124.  *     (1+6+6).  Therefore, to output three times the length, you output
  125.  *     three codes (1+1+1), whereas to output four times the same length,
  126.  *     you only need two codes (1+3).  Hmm.
  127.  * 10. In the tree reconstruction algorithm, Code = Code + Increment
  128.  *     only if BitLength(i) is not zero.  (Pretty obvious.)
  129.  * 11. Correction: 4 Bits: # of Bit Length codes - 4     (4 - 19)
  130.  * 12. Note: length code 284 can represent 227-258, but length code 285
  131.  *     really is 258.  The last length deserves its own, short code
  132.  *     since it gets used a lot in very redundant files.  The length
  133.  *     258 is special since 258 - 3 (the min match length) is 255.
  134.  * 13. The literal/length and distance code bit lengths are read as a
  135.  *     single stream of lengths.  It is possible (and advantageous) for
  136.  *     a repeat code (16, 17, or 18) to go across the boundary between
  137.  *     the two sets of lengths.
  138.  * 14. The Deflate64 (PKZIP method 9) variant of the compression algorithm
  139.  *     differs from "classic" deflate in the following 3 aspect:
  140.  *     a) The size of the sliding history window is expanded to 64 kByte.
  141.  *     b) The previously unused distance codes #30 and #31 code distances
  142.  *        from 32769 to 49152 and 49153 to 65536.  Both codes take 14 bits
  143.  *        of extra data to determine the exact position in their 16 kByte
  144.  *        range.
  145.  *     c) The last lit/length code #285 gets a different meaning. Instead
  146.  *        of coding a fixed maximum match length of 258, it is used as a
  147.  *        "generic" match length code, capable of coding any length from
  148.  *        3 (min match length + 0) to 65538 (min match length + 65535).
  149.  *        This means that the length code #285 takes 16 bits (!) of uncoded
  150.  *        extra data, added to a fixed min length of 3.
  151.  *     Changes a) and b) would have been transparent for valid deflated
  152.  *     data, but change c) requires to switch decoder configurations between
  153.  *     Deflate and Deflate64 modes.
  154.  */
  155.  
  156. #define PKZIP_BUG_WORKAROUND    /* PKZIP 1.93a problem--live with it */
  157.  
  158. /*  inflate.h must supply the uch slide[UWSIZE] array, the void typedef
  159.  *  (void if (void *) is accepted, else char) and the NEXTBYTE,
  160.  *  FLUSH() and memzero macros.  If the window size is not 32K, it
  161.  *  should also define UWSIZE.  If INFMOD is defined, it can include
  162.  *  compiled functions to support the NEXTBYTE and/or FLUSH() macros.
  163.  *  There are defaults for NEXTBYTE and FLUSH() below for use as
  164.  *  examples of what those functions need to do.  Normally, you would
  165.  *  also want FLUSH() to compute a crc on the data.  inflate.h also
  166.  *  needs to provide these typedefs:
  167.  *      typedef unsigned char  uch;
  168.  *      typedef unsigned short ush;
  169.  *      typedef unsigned long  ulg;
  170.  */
  171.  
  172. /*
  173. #ifdef USING_MEM_STRMS
  174. #define FLUSH(w)  ((fmem_mode)? MemFlush(Slide, (ulg)(w)) \
  175.                       : flush(Slide, (ulg)(w), 0))
  176. #else */
  177. //#define FLUSH(w)  (flush(Slide, (ulg)(w), 0))
  178. //#endif
  179. //#define NEXTBYTE  (--fincnt >= 0 ? (int)(*finptr++) : readbyte(pG))
  180.  
  181. #define READBITS(nbits, zdest) { if(nbits>fbits_left) {int temp; fzipeof = 1; \
  182.   while (fbits_left <= 8 *(sizeof(fbitbuf)- 1) && (temp = NEXTBYTE) != EOF) { \
  183.   fbitbuf |= (ulg)temp << fbits_left; fbits_left += 8; fzipeof = 0;}} \
  184.   zdest = (shrint)((ush)fbitbuf & mask_bits[nbits]); fbitbuf >>= nbits; \
  185.   fbits_left -= nbits; }
  186.  
  187. //#ifndef NEXTBYTE                /* default is to simply get a byte from stdin */
  188. //error NEXTBYTE not defined
  189. ////#  define NEXTBYTE getchar()
  190. //#endif
  191.  
  192. //#ifndef FLUSH                   /* default is to simply write the buffer to stdout */
  193. //error FLUSH not defined
  194. //#  define FLUSH(n) fwrite(Slide, 1, n, stdout) /* return value not used */
  195. //#endif
  196.  
  197. /* The inflate algorithm uses a sliding 32K byte window on the uncompressed
  198.  * stream to find repeated byte strings.  This is implemented here as a
  199.  * circular buffer.  The index is updated simply by incrementing and then
  200.  * and'ing with 0x7fff (32K-1).
  201.  * It is left to other modules to supply the 32K area.  It is assumed
  202.  * to be usable as if it were declared "uch slide[32768];" or as just
  203.  * "uch *slide;" and then malloc'ed in the latter case.  The definition
  204.  * must be in unzip.h, included above.
  205.  */
  206. #define INVALID_CODE 99
  207. #define IS_INVALID_CODE(c)  ((c) == INVALID_CODE)
  208. /* Tables for deflate from PKZIP's appnote.txt. */
  209. static const unsigned border[]  =
  210.   {      /* Order of the bit length code lengths */
  211.     16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
  212.   };
  213. static const ush cplens32[]  =
  214.   {   /* Copy lengths for literal codes 257..285 */
  215.     3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
  216.     35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
  217.   };
  218. /* note: see note #13 above about the 258 in this list. */
  219. static const ush cplens64[] =
  220.   {
  221.     3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
  222.     35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 3, 0, 0
  223.   };
  224. /* For Deflate64, the code 285 is defined differently. */
  225. static const uch cplext32[]  =
  226.   {   /* Extra bits for literal codes 257..285 */
  227.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
  228.     3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, INVALID_CODE, INVALID_CODE
  229.   }
  230.   ;                              /* 99==invalid */
  231. static const uch cplext64[] =
  232.   {
  233.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
  234.     3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 16, INVALID_CODE, INVALID_CODE
  235.   };
  236.  
  237. static const ush cpdist[]  =
  238.   {   /* Copy offsets for distance codes 0..29 */
  239.     1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
  240.     257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
  241.     8193, 12289, 16385, 24577, 32769, 49153
  242.   };
  243. static const uch cpdext32[]  =
  244.   {   /* Extra bits for distance codes */
  245.     0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
  246.     7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
  247.     12, 12, 13, 13, INVALID_CODE, INVALID_CODE
  248.   };
  249. static const uch cpdext64[] =
  250.   {
  251.     0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
  252.     7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
  253.     12, 12, 13, 13, 14, 14
  254.   };
  255. #define MAXLITLENS 288
  256. #define MAXDISTS 32
  257.  
  258. /* Macros for inflate() bit peeking and grabbing.
  259.  * The usage is:
  260.  *      NEEDBITS(j)
  261.  *      x = b & mask_bits[j];
  262.  *      DUMPBITS(j)
  263.  * where NEEDBITS makes sure that b has at least j bits in it, and
  264.  * DUMPBITS removes the bits from b.  The macros use the variable k
  265.  * for the number of bits in b.  Normally, b and k are register
  266.  * variables for speed and are initialized at the begining of a
  267.  * routine that uses these macros from a global bit buffer and count.
  268.  *
  269.  * In order to not ask for more bits than there are in the compressed
  270.  * stream, the Huffman tables are constructed to only ask for just
  271.  * enough bits to make up the end-of-block code (value 256).  Then no
  272.  * bytes need to be "returned" to the buffer at the end of the last
  273.  * block.  See the huft_build() routine.
  274.  */
  275.                                                    
  276. //#  define NEXTBYTE  (--fincnt >= 0 ? (int)(*finptr++) : readbyte())
  277. #ifndef CHECK_EOF
  278. #  define CHECK_EOF             /* default as of 5.13/5.2 */
  279. #endif
  280.  
  281. #ifndef CHECK_EOF
  282. #  define NEEDBITS(n) {while(k < (n)) {b |= ((ulg)NEXTBYTE) << k;k += 8;} }
  283. #else
  284. //#  define NEEDBITS(n) {while(k < (n)) {int c = NEXTBYTE; if (c == EOF) return 1;
  285. //    b |= ((ulg)c) << k; k += 8;} }
  286. #ifdef TRACE_INFLATE
  287. #  define NEEDBITS(n) {while((int)k < (int)(n)) {int c = NEXTBYTE; if (c == EOF){\
  288.      if ((int)k>=0)break;retval=1; \
  289.      if (Verbose < 0) Notify(ITRACE, "eos %s", __LINE__); goto fini;} \
  290.     b |= ((ulg)c) << k; k += 8;} }
  291. #else
  292. #  define NEEDBITS(n) {while((int)k < (int)(n)) {int c = NEXTBYTE; if (c == EOF){\
  293.      if ((int)k>=0)break;retval=1;goto fini;} \
  294.     b |= ((ulg)c) << k; k += 8;} }
  295. #endif
  296. #endif /* Piet Plomp:  change "return 1" to "break" */
  297.  
  298. #define DUMPBITS(n) {b >>= (n); k -= (n);}
  299.  
  300. /* Huffman code decoding is performed using a multi-level table lookup.
  301.  * The fastest way to decode is to simply build a lookup table whose
  302.  * size is determined by the longest code.  However, the time it takes
  303.  * to build this table can also be a factor if the data being decoded
  304.  * are not very long.  The most common codes are necessarily the
  305.  * shortest codes, so those codes dominate the decoding time, and hence
  306.  * the speed.  The idea is you can have a shorter table that decodes the
  307.  * shorter, more probable codes, and then point to subsidiary tables for
  308.  * the longer codes.  The time it costs to decode the longer codes is
  309.  * then traded against the time it takes to make longer tables.
  310.  *
  311.  * This results of this trade are in the variables lbits and dbits
  312.  * below.  lbits is the number of bits the first level table for literal/
  313.  * length codes can decode in one step, and dbits is the same thing for
  314.  * the distance codes.  Subsequent tables are also less than or equal to
  315.  * those sizes.  These values may be adjusted either when all of the
  316.  * codes are shorter than that, in which case the longest code length in
  317.  * bits is used, or when the shortest code is *longer* than the requested
  318.  * table size, in which case the length of the shortest code in bits is
  319.  * used.
  320.  *
  321.  * There are two different values for the two tables, since they code a
  322.  * different number of possibilities each.  The literal/length table
  323.  * codes 286 possible values, or in a flat code, a little over eight
  324.  * bits.  The distance table codes 30 possible values, or a little less
  325.  * than five bits, flat.  The optimum values for speed end up being
  326.  * about one bit more than those, so lbits is 8+1 and dbits is 5+1.
  327.  * The optimum values may differ though from machine to machine, and
  328.  * possibly even between compilers.  Your mileage may vary.
  329.  */
  330.  
  331. static const int lbits = 9;     /* bits in base literal/length lookup table */
  332. static const int dbits = 6;     /* bits in base distance lookup table       */
  333.                                                          
  334. //#  define NEXTBYTE  (--fincnt >= 0 ? (int)(*finptr++) : readbyte())
  335.  
  336. int huft_free(struct huft *t);
  337.  
  338. /* ===========================================================================
  339.  * inflate (decompress) the codes in a deflated (compressed) block.
  340.  * Return an error code or zero if it all goes ok.
  341.         *tl, *td :: Literal/length and distance decoder tables.
  342.          bl, bd  :: Number of bits decoded by tl[] and td[].
  343.  */
  344. int UnzInf::inflate_codes(struct huft *tl, struct huft *td, int bl, int bd)
  345. {
  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 ml, md;              /* masks for bl and bd bits */
  351.   register ulg b;               /* bit buffer   */
  352.   register unsigned k;          /* number of bits in bit buffer   */
  353.   int retval = 0;
  354.  
  355. #ifdef TRACE_INFLATE
  356.   if (Verbose < 0)
  357.     Notify(ITRACE, _T("inflate codes"));
  358. #endif
  359.   /* make local copies of globals */
  360.   b = fbb;                   /* initialize bit buffer    */
  361.   k = fbk;
  362.   w = fwp;                   /* initialize window position */
  363.  
  364.   /* inflate the coded data */
  365.   ml = mask_bits[bl];           /* precompute masks for speed */
  366.   md = mask_bits[bd];
  367.  
  368.   while (1)
  369.   {                   /* do until end of block   */
  370.     NEEDBITS((unsigned)  bl)
  371.     t = tl + ((unsigned)  b & ml);
  372.     while (1)
  373.     {
  374.       DUMPBITS(t->b)
  375.  
  376.       if ((e = t->e) == 32)     /* then it's a literal */
  377.       {
  378.         Slide[w++] = (uch)t->v.n;
  379. //#ifdef USE_STRM_OUTPUT
  380. //              if (fredirect_data && !(w % 0x8000))    // RCV1.6019
  381. //              {
  382. //                // bump up progress bar
  383. //                UserProgress(0x8000);
  384. //              }
  385. //#endif
  386.                 if (w == wsize)
  387.                 {
  388. //                if ((retval = FLUSH(w)) != 0)
  389.                   if ((retval = flush(Slide, (ulg)(w), 0)) != 0)
  390.                         goto fini;
  391.           w = 0;
  392.         }
  393.         break;
  394.       }
  395.  
  396.       if (e < 31)               /* then it's a length */
  397.       {
  398.         /* get length of block to copy */
  399.         NEEDBITS(e)
  400.         n = t->v.n + ((unsigned)b & mask_bits[e]);
  401.         DUMPBITS(e)
  402.  
  403.         /* decode distance of block to copy */
  404.         NEEDBITS(bd)
  405.         t = td + ((unsigned)b & md);
  406.         while (1)
  407.         {
  408.           DUMPBITS(t->b)
  409.           if ((e = t->e) < 32)
  410.             break;
  411.           if (IS_INVALID_CODE(e))
  412.             return 1;
  413.           e &= 31;
  414.           NEEDBITS(e)
  415.           t = t->v.t + ((unsigned)b & mask_bits[e]);
  416.         }
  417.         NEEDBITS(e)
  418.         d = (unsigned)w - t->v.n - ((unsigned)b & mask_bits[e]);
  419.         DUMPBITS(e)
  420.  
  421.         /* do the copy */
  422.         do
  423.         {
  424. //#ifdef USE_STRM_OUTPUT
  425. //                if (fredirect_data && !fUseInStream)
  426. //                {       /* &= w/ wsize unnecessary & wrong if redirect      */
  427. //                      if (d >= wsize)
  428. //                        return 1;   // invalid compression data
  429. //                      e = (unsigned)(wsize - (d > (unsigned)w ? (ulg)d : w));
  430. //                }
  431. //                else
  432. //#endif
  433.             e = (unsigned)(wsize -
  434.                            ((d &= (unsigned)(wsize-1)) > (unsigned)w ?
  435.                             (ulg)d : w));
  436.           if ((ulg)e > n)
  437.             e = (unsigned)n;
  438.           n -= e;
  439. //#ifndef NOMEMCPY
  440.                   if (w - d >= e)
  441.           {       /* (this test assumes unsigned comparison)              */
  442.             memcpy(Slide + w, Slide + d, e);
  443. //#ifdef USE_STRM_OUTPUT
  444. //                      if (fredirect_data && ((w + e)  / 0x8000 - w / 0x8000))      // RCV1.6022
  445. //                      {
  446. //                        // bump up progress bar
  447. //                        UserProgress(0x8000);
  448. //                      }
  449. //#endif
  450.             w += e;
  451.             d += e;
  452.           }
  453.           else                    /* do it slowly to avoid memcpy() overlap */
  454. //#  endif /* !NOMEMCPY */
  455.                         do
  456.             {
  457.               Slide[w++]  = Slide[d++];
  458. //# ifdef USE_STRM_OUTPUT
  459. //                        if (fredirect_data && !(w % 0x8000))      // RCV1.6019 1.6022
  460. //                        {
  461. //                              // bump up progress bar
  462. //                              UserProgress(0x8000);
  463. //                        }
  464. //#  endif
  465.             }
  466.             while (--e);
  467.  
  468.  
  469.           if (w == wsize)
  470.                   {
  471. //                      if ((retval = FLUSH(w)) != 0)
  472.                         if ((retval = flush(Slide, (ulg)(w), 0)) != 0)
  473.               goto fini;
  474.             w = 0;
  475.           }
  476.         }
  477.         while (n);
  478.         break;
  479.       }
  480.  
  481.       if (e == 31)              /* it's the EOB signal */
  482.       {
  483.         /* sorry for this goto, but we have to exit two loops at once */
  484.         goto clean1;
  485.       }
  486.  
  487.       if (IS_INVALID_CODE(e))
  488.         return 1;
  489.  
  490.       e &= 31;
  491.       NEEDBITS(e)
  492.       t = t->v.t + ((unsigned)b & mask_bits[e]);
  493.     }
  494.   }
  495. clean1:
  496.   /* restore the globals from the locals */
  497.   fwp = w;                   /* restore global window pointer */
  498.   fbb = b;                   /* restore global bit buffer */
  499.   fbk = k;
  500. fini:
  501. #ifdef TRACE_INFLATE
  502.   if (Verbose < 0)
  503.     Notify(ITRACE, _T("inflate_codes returning %d"), retval);
  504. #endif
  505.   return retval;
  506. }
  507.  
  508.  
  509. /* ===========================================================================
  510.  * "decompress" an inflated type 0 (stored) block.
  511.  */
  512. int UnzInf::inflate_stored(void)
  513. {
  514.   unsigned n;                   /* number of bytes in block    */
  515.   unsigned w;                   /* current window position   */
  516.   register ulg b;               /* bit buffer      */
  517.   register unsigned k;          /* number of bits in bit buffer   */
  518.   int retval = 0;
  519.  
  520.   /* make local copies of globals */
  521. #ifdef TRACE_INFLATE
  522.   if (Verbose < 0)
  523.     Notify(ITRACE, _T("extracting files from stored block"));
  524. #endif
  525.   b = fbb;                   /* initialize bit buffer      */
  526.   k = fbk;
  527.   w = fwp;                   /* initialize window position */
  528.  
  529.   /* go to byte boundary */
  530.   n = k & 7;
  531.   DUMPBITS(n);
  532.  
  533.   /* get the length and its complement */
  534.   NEEDBITS(16)
  535.   n = ((unsigned)  b & 0xffff);
  536.   DUMPBITS(16)
  537.   NEEDBITS(16)
  538.   if (n != (unsigned)((~b)  & 0xffff))
  539.   {
  540. #ifdef TRACE_INFLATE
  541.   if (Verbose < 0)
  542.     Notify(ITRACE, _T("error in compressed stored data"));
  543. #endif
  544.     return 1;                   /* error in compressed data */
  545.   }
  546.   DUMPBITS(16)
  547.  
  548.   /* read and output the compressed data */
  549.   while (n--)
  550.   {
  551.     NEEDBITS(8)
  552.     Slide[w++]  = (uch)  b;
  553. //# ifdef USE_STRM_OUTPUT
  554. //      if (fredirect_data && !(w % 0x8000))      // RCV1.6019
  555. //      {
  556. //        // bump up progress bar
  557. //        UserProgress(0x8000);
  558. //      }
  559. //#  endif
  560.     if (w == wsize)
  561.         {
  562. //        if ((retval = FLUSH(w)) != 0)
  563.           if ((retval = flush(Slide, (ulg)(w), 0)) != 0)
  564.         goto fini;
  565.       w = 0;
  566.     }
  567.     DUMPBITS(8)
  568.   }
  569.   /* restore the globals from the locals */
  570.   fwp = w;                   /* restore global window pointer */
  571.   fbb = b;                   /* restore global bit buffer */
  572.   fbk = k;
  573. fini:
  574.   return retval;
  575. }
  576.  
  577.  
  578. /* ===========================================================================
  579.  * decompress an inflated type 1 (fixed Huffman codes) block.  We should
  580.  * either replace this with a custom decoder, or at least precompute the
  581.  * Huffman tables.
  582.  */
  583. int UnzInf::inflate_fixed(void)
  584. {
  585.   /* if first time, set up tables for fixed blocks */
  586. #ifdef TRACE_INFLATE
  587.   if (Verbose < 0)
  588.     Notify(ITRACE, _T("literal block"));
  589. #endif
  590.   if (ffixed_tl == (struct huft *)  NULL)
  591.   {
  592.     int i;                      /* temporary variable                      */
  593.     unsigned l[288];            /* length list for huft_build */
  594.  
  595.     /* literal table */
  596.     for (i = 0; i < 144; i++)
  597.       l[i]  = 8;
  598.     for (; i < 256; i++)
  599.       l[i]  = 9;
  600.     for (; i < 280; i++)
  601.       l[i]  = 7;
  602.     for (; i < 288; i++)
  603.       l[i]  = 8;                 /* make a complete, but wrong code set */
  604.  
  605.     ffixed_bl = 7;
  606.     if ((i =
  607.            huft_build(l, 288, 257, fcplens, fcplext, &ffixed_tl,
  608.                       &ffixed_bl))  != 0)
  609.     {
  610.       ffixed_tl = (struct huft *)  NULL;
  611.       return i;
  612.     }
  613.     /* distance table */
  614.     for (i = 0; i < MAXDISTS; i++)
  615.       l[i]  = 5;                 /* make an incomplete code set */
  616.  
  617.     ffixed_bd = 5;
  618.     if ((i =
  619.            huft_build(l, MAXDISTS, 0, cpdist, fcpdext, &ffixed_td,
  620.                       &ffixed_bd))  > 1)
  621.     {
  622.       huft_free(ffixed_tl);
  623.       ffixed_tl = (struct huft *)  NULL;
  624.       return i;
  625.     }
  626.   }
  627.   /* Decompress until an end-of-block code. */
  628.   return inflate_codes(ffixed_tl, ffixed_td, ffixed_bl,
  629.                        ffixed_bd)  != 0;
  630. }
  631.  
  632.  
  633. /* ===========================================================================
  634.  * decompress an inflated type 2 (dynamic Huffman codes) block.
  635.  */
  636. int UnzInf::inflate_dynamic(void)
  637. {
  638.   int i;                        /* temporary variables    */
  639.   unsigned j;
  640.   unsigned l;                   /* last length     */
  641.   unsigned m;                   /* mask for bit lengths table   */
  642.   unsigned n;                   /* number of lengths to get   */
  643.   int bl;                       /* lookup bits for tl      */
  644.   int bd;                       /* lookup bits for td      */
  645.   unsigned nb;                  /* number of bit length codes     */
  646.   unsigned nl;                  /* number of literal/length codes   */
  647.   unsigned nd;                  /* number of distance codes    */
  648.   int retval;// =0;
  649.   struct huft *tl = NULL;              /* literal/length code table     */
  650.   struct huft *td = NULL;              /* distance code table         */
  651. //#ifdef PKZIP_BUG_WORKAROUND
  652.   unsigned ll[288 + MAXDISTS]; /* literal/length and distance code lengths */
  653. //#else
  654. //  unsigned ll[286 + 30];        /* literal/length and distance code lengths */
  655. //#endif
  656.   register ulg b;               /* bit buffer */
  657.   register unsigned k;          /* number of bits in bit buffer */
  658.  
  659.   /* make local bit buffer */
  660. #ifdef TRACE_INFLATE
  661.   if (Verbose < 0)
  662.     Notify(ITRACE, _T("in inflate_dynamic"));
  663. #endif
  664.   b = fbb;
  665.   k = fbk;
  666.  
  667.   /* read in table lengths */
  668.   NEEDBITS(5)
  669.   nl = 257 + ((unsigned)  b & 0x1f);   /* number of literal/length codes */
  670.   DUMPBITS(5)
  671.   NEEDBITS(5)
  672.   nd = 1 + ((unsigned)  b & 0x1f);     /* number of distance codes */
  673.   DUMPBITS(5)
  674.   NEEDBITS(4)
  675.   nb = 4 + ((unsigned)  b & 0xf);      /* number of bit length codes */
  676.   DUMPBITS(4)
  677. //#ifdef PKZIP_BUG_WORKAROUND
  678.   if (nl > MAXLITLENS || nd > MAXDISTS)
  679. //#else
  680. //  if (nl > 286 || nd > 30)
  681. //#endif
  682.     return 1;                   /* bad lengths */
  683.  
  684.   /* read in bit-length-code lengths */
  685.   for (j = 0; j < nb; j++)
  686.   {
  687.     NEEDBITS(3)
  688.     ll[border[j]]  = (unsigned)  b & 7;
  689.     DUMPBITS(3)
  690.   }
  691.   for (; j < 19; j++)
  692.     ll[border[j]]  = 0;
  693.  
  694.   /* build decoding table for trees--single level, 7 bit lookup */
  695.   bl = 7;
  696.   retval = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl);
  697.   if (bl == 0)
  698.     retval = 1;
  699.   if (retval)
  700.   {
  701.     if (retval == 1)
  702.       huft_free(tl);
  703.     return retval;                   /* incomplete code set */
  704.   }
  705.  
  706.   /* read in literal and distance code lengths */
  707.   n = nl + nd;
  708.   m = mask_bits[bl];
  709.   i = l = 0;
  710.  
  711.   while ((unsigned)  i < n)
  712.   {
  713.     NEEDBITS((unsigned)  bl)
  714.     j = (td = tl + ((unsigned)  b & m)) ->b;
  715.     DUMPBITS(j)
  716.     j = td->v.n;
  717.  
  718.     if (j < 16)                  /* length of code in bits (0..15)       */
  719.       ll[i++]  = l = j;          /* save last length in l         */
  720.     else
  721.       if (j == 16)
  722.       {         /* repeat last length 3 to 6 times */
  723.         NEEDBITS(2)
  724.         j = 3 + ((unsigned)  b & 3);
  725.         DUMPBITS(2)
  726.         if ((unsigned)  i + j > n)
  727.         {
  728.           huft_free(tl);
  729.           return 1;
  730.         }
  731.         while (j--)
  732.           ll[i++]  = l;
  733.       }
  734.       else
  735.         if (j == 17)
  736.         {         /* 3 to 10 zero length codes */
  737.           NEEDBITS(3)
  738.           j = 3 + ((unsigned)  b & 7);
  739.           DUMPBITS(3)
  740.           if ((unsigned)  i + j > n)
  741.           {
  742.             huft_free(tl);
  743.             return 1;
  744.           }
  745.           while (j--)
  746.             ll[i++]  = 0;
  747.           l = 0;
  748.         }
  749.         else
  750.         {                      /* j == 18: 11 to 138 zero length codes */
  751.           NEEDBITS(7)
  752.           j = 11 + ((unsigned)  b & 0x7f);
  753.           DUMPBITS(7)
  754.           if ((unsigned)  i + j > n)
  755.           {
  756.             huft_free(tl);
  757.             return 1;
  758.           }
  759.           while (j--)
  760.             ll[i++]  = 0;
  761.           l = 0;
  762.         }
  763.   }
  764.  
  765.   /* free decoding table for trees */
  766.   huft_free(tl);
  767.  
  768.   /* restore the global bit buffer */
  769.   fbb = b;
  770.   fbk = k;
  771.  
  772.   /* build the decoding tables for literal/length and distance codes */
  773.   bl = lbits;
  774.   retval = huft_build(ll, nl, 257, fcplens, fcplext, &tl, &bl);
  775.   if (bl == 0)
  776.     retval = 1;
  777.   if (retval)
  778.   {
  779.     if (retval == 1 && !fqflag)
  780.     {
  781.       Notify(0, _T("Fatal error: incomplete l - tree"));
  782.       huft_free(tl);
  783.     }
  784.     return retval;                   /* incomplete code set */
  785.   }
  786.  
  787.   bd = dbits;
  788.   retval = huft_build(ll + nl, nd, 0, cpdist, fcpdext, &td, &bd);
  789.   if (retval == 1)
  790.     retval =0;
  791.   if (bd == 0 && nl > 257)   // lengths but no distances
  792.     retval = 1;
  793.   if (retval)
  794.   {
  795.     if (retval == 1)
  796.     {
  797.       if (!fqflag)
  798.         Notify(0, _T("Fatal Error: incomplete d-tree"));
  799. //#ifdef PKZIP_BUG_WORKAROUND
  800. //                            i = 0;  RCV not used later on why???
  801. //#else
  802.       huft_free(td);
  803. //#endif
  804.     }
  805. //#ifndef PKZIP_BUG_WORKAROUND
  806.     huft_free(tl);
  807.     return retval;                   /* incomplete code set */
  808. //#endif
  809.  
  810.   }
  811.   /* decompress until an end-of-block code */
  812.   retval = inflate_codes(tl, td, bl, bd);
  813. fini:
  814.   /* free the decoding tables, return */
  815.   huft_free(tl);
  816.   huft_free(td);
  817. #ifdef TRACE_INFLATE
  818.   if (Verbose < 0)
  819.     Notify(ITRACE, _T("inflate_dynamic returning %d"), retval);
  820. #endif
  821.   return retval;
  822. }
  823.  
  824.  
  825. /* ===========================================================================
  826.  * decompress an inflated block
  827.         *e :: Last block flag.
  828.  */
  829. int UnzInf::inflate_block(int *e)
  830. {
  831.   unsigned t;                   /* block type */
  832.   register ulg b;               /* bit buffer */
  833.   register unsigned k;          /* number of bits in bit buffer */
  834.   int retval = 2;               /* bad block type */
  835.  
  836.   /* make local bit buffer */
  837.   b = fbb;
  838.   k = fbk;
  839.  
  840.   /* read in last block bit */
  841.   NEEDBITS(1)
  842.   * e = (int)  b & 1;
  843.   DUMPBITS(1)
  844.  
  845.   /* read in block type */
  846.   NEEDBITS(2)
  847.   t = (unsigned)  b & 3;
  848.   DUMPBITS(2)
  849.  
  850.   /* restore the global bit buffer */
  851.   fbb = b;
  852.   fbk = k;
  853.  
  854.   /* inflate that block type */
  855.   if (t == 2)
  856.     return inflate_dynamic();
  857.   if (t == 0)
  858.     return inflate_stored();
  859.   if (t == 1)
  860.     return inflate_fixed();
  861.  
  862. fini:
  863.   return retval;
  864. }
  865.  
  866.  
  867. /* ===========================================================================
  868.  * Main entry to inflate a compressed file
  869.  * decompress an inflated entry
  870.  */
  871. int UnzInf::inflate(bool defl64)
  872. {
  873.   int e;                        /* last block flag */
  874.   int retval;                   /* result code     */
  875.   unsigned h;                   /* maximum struct huft's malloc'ed */
  876.  
  877. //#ifdef USE_STRM_OUTPUT
  878. //  if (fredirect_data)
  879. //  {
  880. //    wsize = fredirect_size;
  881. //    Slide = fredirect_pointer;
  882. //  }
  883. //  else
  884. //  {
  885. //    wsize = UWSIZE;
  886. //    Slide = Slide;
  887. //  }
  888. //#else
  889. ////    wsize = UWSIZE;
  890. ////    Slide = Slide;
  891. //#endif
  892.  
  893.   if (Verbose < 0)
  894.     Notify(ITRACE, defl64? _T("starting inflate64") : _T("starting inflate"));
  895.   if (defl64)
  896.   {
  897.     fcplens = cplens64;
  898.     fcplext = cplext64;
  899.     fcpdext = cpdext64;
  900.     ffixed_tl = ffixed_tl64;
  901.     ffixed_bl = ffixed_bl64;
  902.     ffixed_td = ffixed_td64;
  903.     ffixed_bd = ffixed_bd64;
  904.   }
  905.   else
  906.   {
  907.     fcplens = cplens32;
  908.     fcplext = cplext32;
  909.     fcpdext = cpdext32;
  910.     ffixed_tl = ffixed_tl32;
  911.     ffixed_bl = ffixed_bl32;
  912.     ffixed_td = ffixed_td32;
  913.     ffixed_bd = ffixed_bd32;
  914.   }
  915.   /* initialize window, bit buffer */
  916.   fwp = 0;
  917.   fbk = 0;
  918.   fbb = 0;
  919.  
  920.   /* decompress until the last block */
  921.   h = 0;
  922.   do
  923.   {
  924.     fhufts = 0;
  925.     if ((retval = inflate_block(&e))  != 0)
  926.     {
  927.       e = 888;  // break loop
  928.       if (Verbose < 0)
  929.         Notify(ITRACE,
  930.                _T("inflate_block returned poss error=%d, inflate will also return it"),  retval);
  931.       break;
  932.     }
  933.     if (fhufts > h)
  934.       h = fhufts;
  935.     if (Abort_Flag)
  936.     {
  937.       retval = DZ_ERM_ABORT;//UEN_ABORT03;
  938.       e = 999;  //break loop
  939.       break;
  940.     }
  941.   }
  942.   while (!e);
  943.  
  944.   if (defl64)
  945.   {
  946.     ffixed_tl64 = ffixed_tl;
  947.     ffixed_bl64 = ffixed_bl;
  948.     ffixed_td64 = ffixed_td;
  949.     ffixed_bd64 = ffixed_bd;
  950.   }
  951.   else
  952.   {
  953.     ffixed_tl32 = ffixed_tl;
  954.     ffixed_bl32 = ffixed_bl;
  955.     ffixed_td32 = ffixed_td;
  956.     ffixed_bd32 = ffixed_bd;
  957.   }
  958.  
  959.   /* flush out Slide */
  960.   if (!retval)
  961.         retval = flush(Slide, (ulg)(fwp), 0);
  962. //      retval = FLUSH(fwp);
  963.  
  964.   /* return success */
  965.   if (!retval && Verbose < 0)
  966.     Notify(ITRACE, _T("NO ERROR - %u bytes in Huffman tables (%d/entry)"),
  967.            h * sizeof(struct huft), sizeof(struct huft));
  968.   return retval;
  969. }
  970.  
  971.  
  972. /* ===========================================================================
  973.  */
  974. int UnzInf::inflate_free(void)
  975. {
  976.   if (ffixed_tl != (struct huft *)  NULL)
  977.   {
  978.     huft_free(ffixed_td);
  979.     huft_free(ffixed_tl);
  980.     ffixed_td = ffixed_tl = (struct huft *)  NULL;
  981.   }
  982.   return 0;
  983. }
  984.  
  985.  
  986. /*
  987.  * GRR:  moved huft_build() and huft_free() down here; used by explode()
  988.  */
  989.  
  990. /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
  991. #define BMAX 16                 /* maximum bit length of any code (16 for explode) */
  992. #define N_MAX 288               /* maximum number of codes in any set */
  993.  
  994. /* ===========================================================================
  995.  * Given a list of code lengths and a maximum table size, make a set of
  996.  * tables to decode that set of codes.  Return zero on success, one if
  997.  * the given code set is incomplete (the tables are still built in this
  998.  * case), two if the input is invalid (all zero length codes or an
  999.  * oversubscribed set of lengths), and three if not enough memory.
  1000.  * The code with value 256 is special, and the tables are constructed
  1001.  * so that no bits beyond that code are fetched when that code is
  1002.  * decoded.
  1003.          *b :: Code lengths in bits (all assumed <= BMAX).
  1004.           n :: Number of codes (assumed <= N_MAX).
  1005.           s :: Number of simple-valued codes (0..s-1).
  1006.          *d :: List of base values for non-simple codes.
  1007.          *e :: List of extra bits for non-simple codes.
  1008.         **t :: Result: starting table.
  1009.          *m :: Maximum lookup bits, returns actual.
  1010.  */
  1011. int UnzInf::huft_build(unsigned *b, unsigned n, unsigned s,
  1012.            const ush * d, const uch * e, struct huft **t, int *m)
  1013. {
  1014.   unsigned a;                   /* counter for codes of length k   */
  1015.   unsigned c[BMAX + 1];         /* bit length count table       */
  1016.   unsigned el;                  /* length of EOB code (value 256)   */
  1017.   unsigned f;                   /* i repeats in table every f entries    */
  1018.   int g;                        /* maximum code length     */
  1019.   int h;                        /* table level         */
  1020.   register unsigned i;          /* counter, current code      */
  1021.   register unsigned j;          /* counter        */
  1022.   register int k;               /* number of bits in current code    */
  1023.   int lx[BMAX + 1];             /* memory for l[-1..BMAX-1]  */
  1024.   int *l = lx + 1;              /* stack of bits per table       */
  1025.   register unsigned *p;         /* pointer into c[], b[], or v[]     */
  1026.   register struct huft *q;      /* points to current table          */
  1027.   struct huft r;                /* table entry for structure assignment   */
  1028.   struct huft *u[BMAX];         /* table stack       */
  1029.   unsigned v[N_MAX];            /* values in order of bit length       */
  1030.   register int w;               /* bits before this table == (l * h)     */
  1031.   unsigned x[BMAX + 1];         /* bit offsets, then code stack      */
  1032.   unsigned *xp;                 /* pointer into x         */
  1033.   int y;                        /* number of dummy codes added      */
  1034.   unsigned z;                   /* number of entries in current table    */
  1035.  
  1036.   /* Generate counts for each bit length */
  1037.   el = n > 256 ? b[256]  : BMAX; /* set length of EOB code, if any */
  1038.   ZeroMemory((char *)  c, sizeof(c));
  1039.   p = b;
  1040.   i = n;
  1041.  
  1042.   do
  1043.   {
  1044.     c[*p] ++;
  1045.     p++;                        /* assume all entries <= BMAX */
  1046.   }
  1047.   while (--i);
  1048.  
  1049.   if (c[0]  == n)
  1050.   {              /* null input--all zero length codes */
  1051.     *t = (struct huft *)  NULL;
  1052.     *m = 0;
  1053.     return 0;
  1054.   }
  1055.  
  1056.   /* Find minimum and maximum length, bound *m by those */
  1057.   for (j = 1; j <= BMAX; j++)
  1058.     if (c[j])
  1059.       break;
  1060.  
  1061.   k = j;                        /* minimum code length */
  1062.   if ((unsigned)  *m < j)
  1063.     *m = j;
  1064.  
  1065.   for (i = BMAX; i; i--)
  1066.     if (c[i])
  1067.       break;
  1068.  
  1069.   g = i;                        /* maximum code length */
  1070.   if ((unsigned)  *m > i)
  1071.     *m = i;
  1072.  
  1073.   /* Adjust last length count to fill out codes, if needed */
  1074.   for (y = 1 << j; j < i; j++, y <<= 1)
  1075.     if ((y -= c[j])  < 0)
  1076.       return 2;                 /* bad input: more codes than bits */
  1077.  
  1078.   if ((y -= c[i])  < 0)
  1079.     return 2;
  1080.   c[i]  += y;
  1081.  
  1082.   /* Generate starting offsets into the value table for each length */
  1083.   x[1]  = j = 0;
  1084.   p = c + 1;
  1085.   xp = x + 2;
  1086.  
  1087.   while (--i)
  1088.   {                 /* note that i == g from above */
  1089.     *xp++ = (j += *p++);
  1090.   }
  1091.  
  1092.   /* Make a table of values in order of bit lengths */
  1093.   ZeroMemory((char *)v, sizeof(v));
  1094.   p = b;
  1095.   i = 0;
  1096.  
  1097.   do
  1098.   {
  1099.     if ((j = *p++)  != 0)
  1100.       v[x[j] ++]  = i;
  1101.   }
  1102.   while (++i < n);
  1103.   n = x[g];                     /* set n to length of v */
  1104.  
  1105.   /* Generate the Huffman codes and for each, make the table entries */
  1106.   x[0]  = i = 0;                 /* first Huffman code is zero     */
  1107.   p = v;                        /* grab values in bit order     */
  1108.   h = -1;                       /* no tables yet--level -1  */
  1109.   w = l[-1]  = 0;                /* no bits decoded yet     */
  1110.   u[0]  = (struct huft *)  NULL;  /* just to keep compilers happy   */
  1111.   q = (struct huft *)  NULL;     /* ditto */
  1112.   z = 0;                        /* ditto */
  1113.  
  1114.   /* go through the bit lengths (k already is bits in shortest code) */
  1115.   for (; k <= g; k++)
  1116.   {
  1117.     a = c[k];
  1118.     while (a--)
  1119.     {
  1120.       /* here i is the Huffman code of length k bits for value *p */
  1121.       /* make tables up to required level */
  1122.       while (k > w + l[h])
  1123.       {
  1124.         w += l[h++];            /* add bits already decoded */
  1125.  
  1126.         /* compute minimum size table less than or equal to *m bits  */
  1127.         z = (z = g - w)  > (unsigned)  *m ? *m : z;       /* upper limit         */
  1128.         if ((f = 1 << (j = k - w))  > a + 1)
  1129.         {   /* try a k-w bit table *//* too few codes for k-w bit table  */
  1130.           f -= a + 1;           /* deduct codes from patterns left  */
  1131.           xp = c + k;
  1132.           while (++j < z)
  1133.           {     /* try smaller tables up to z bits  */
  1134.             if ((f <<= 1)  <= *++xp)
  1135.               break;            /* enough codes to use up j bits */
  1136.             f -= *xp;           /* else deduct codes from patterns */
  1137.           }
  1138.         }
  1139.         if ((unsigned)  w + j > el && (unsigned)  w < el)
  1140.           j = el - w;           /* make EOB code end at table    */
  1141.         z = 1 << j;             /* table entries for j-bit table */
  1142.         l[h]  = j;               /* set table size in stack       */
  1143.  
  1144.         /* allocate and link in new table */
  1145. //        if ((q =
  1146. //               (struct huft *)  MALLOC((z + 1)  * sizeof(struct huft)))  ==
  1147. //            (struct huft *)  NULL)
  1148.         q = new huft[z + 1];
  1149. //        if ((q = new huft[z + 1]) == NULL)
  1150. //        {
  1151. //          if (h)
  1152. //            huft_free(u[0]);
  1153. //          return 3;             /* not enough memory */
  1154. //        }
  1155.         fhufts += z + 1;     /* track memory usage               */
  1156.         *t = q + 1;             /* link to list for huft_free()     */
  1157.         *(t = &(q->v.t))  = (struct huft *)  NULL;
  1158.         u[h]  = ++q;             /* table starts after link          */
  1159.  
  1160.         /* connect to last table, if there is one */
  1161.         if (h)
  1162.         {
  1163.           x[h]  = i;             /* save pattern for backing up      */
  1164.           r.b = (uch)  l[h - 1]; /* bits to dump before this table   */
  1165.           r.e = (uch)(32 + j); /* bits in this table               */
  1166.           r.v.t = q;            /* pointer to this table            */
  1167.           j = (i & ((1 << w)  - 1))  >> (w - l[h - 1]);
  1168.           u[h - 1][j]  = r;      /* connect to last table            */
  1169.         }
  1170.       }
  1171.  
  1172.       /* set up table entry in r */
  1173.       r.b = (uch)(k - w);
  1174.       if (p >= v + n)
  1175.         r.e = INVALID_CODE;         /* out of values--invalid code     */
  1176.       else
  1177.         if (*p < s)
  1178.         {
  1179.           r.e = (uch)(*p < 256 ? 32 : 31); /* 256 is end-of-block code */
  1180.           r.v.n = (ush)  * p++;    /* simple code is just the value   */
  1181.         }
  1182.         else
  1183.         {
  1184.           if (!e)
  1185.             return 1;             /* RCV v1.6015 added               */
  1186.           r.e = (uch)  e[*p - s];  /* non-simple--look up in lists    */
  1187.           r.v.n = d[*p++ - s];
  1188.         }
  1189.  
  1190.       /* fill code-like entries with r */
  1191.       f = 1 << (k - w);
  1192.       for (j = i >> w; j < z; j += f)
  1193.         q[j]  = r;
  1194.  
  1195.       /* backwards increment the k-bit code i */
  1196.       for (j = 1 << (k - 1); i & j; j >>= 1)
  1197.         i ^= j;
  1198.       i ^= j;
  1199.  
  1200.       /* backup over finished tables */
  1201.       while ((i & ((1 << w)  - 1))  != x[h])
  1202.         w -= l[--h];            /* don't need to update q */
  1203.     }
  1204.   }
  1205.   /* return actual size of base table */
  1206.   *m = l[0];
  1207.  
  1208.   /* Return true (1) if we were given an incomplete table */
  1209.   return y != 0 && g != 1;
  1210. }
  1211.  
  1212.  
  1213. /* ===========================================================================
  1214.  * Free the malloc'ed tables built by huft_build(), which makes a linked
  1215.  * list of the tables it made, with the links in a dummy first entry of
  1216.  * each table.
  1217.         *t :: Table to free.
  1218.  */
  1219. int huft_free(struct huft *t)
  1220. {
  1221.   register struct huft *p, *q;
  1222.  
  1223.   /* Go through linked list, freeing from the malloced (t[-1]) address. */
  1224.   p = t;
  1225.   while (p != (struct huft *)  NULL)
  1226.   {
  1227.     q = (--p) ->v.t;
  1228. //    FREE(p);
  1229.     delete[] p;
  1230.     p = q;
  1231.   }
  1232.   return 0;
  1233. }
  1234. /* 30/1/07 */
  1235.