home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 October / Chip_2001-10_cd1.bin / zkuste / delphi / nastroje / d23456 / NSIS.EXE / Source / zlib / INFTREES.C < prev    next >
C/C++ Source or Header  |  2001-05-19  |  13KB  |  378 lines

  1. /* inftrees.c -- generate Huffman trees for efficient decoding
  2.  * Copyright (C) 1995-1998 Mark Adler
  3.  * For conditions of distribution and use, see copyright notice in zlib.h 
  4.  */
  5.  
  6. #include "zutil.h"
  7. #include "inftrees.h"
  8.  
  9.  
  10. /* non ANSI compilers may not accept inffixed.h - it is also smaller :) */
  11.  
  12. //const char inflate_copyright[] =
  13. //   " inflate 1.1.3 Copyright 1995-1998 Mark Adler ";
  14. /*
  15.   If you use the zlib library in a product, an acknowledgment is welcome
  16.   in the documentation of your product. If for some reason you cannot
  17.   include such an acknowledgment, I would appreciate that you keep this
  18.   copyright string in the executable of your product.
  19.   - we have lots of ack's, and want to keep it small.
  20.  */
  21. struct internal_state  {int dummy;}; /* for buggy compilers */
  22.  
  23. /* simplify the use of the inflate_huft type with some defines */
  24. #define exop word.what.Exop
  25. #define bits word.what.Bits
  26.  
  27. /* Tables for deflate from PKZIP's appnote.txt. */
  28. local const unsigned short  cplens[31] = { /* Copy lengths for literal codes 257..285 */
  29.         3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
  30.         35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
  31.         /* see note #13 above about 258 */
  32. local const unsigned short  cplext[31] = { /* Extra bits for literal codes 257..285 */
  33.         0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
  34.         3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112}; /* 112==invalid */
  35. local const unsigned short  cpdist[30] = { /* Copy offsets for distance codes 0..29 */
  36.         1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
  37.         257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
  38.         8193, 12289, 16385, 24577};
  39. local const unsigned short  cpdext[30] = { /* Extra bits for distance codes */
  40.         0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
  41.         7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
  42.         12, 12, 13, 13};
  43.  
  44. #define BMAX 15         /* maximum bit length of any code */
  45.  
  46. local int huft_build(
  47. uIntf *b,               /* code lengths in bits (all assumed <= BMAX) */
  48. uInt n,                 /* number of codes (assumed <= 288) */
  49. uInt s,                 /* number of simple-valued codes (0..s-1) */
  50. const unsigned short *d,         /* list of base values for non-simple codes */
  51. const unsigned short *e,         /* list of extra bits for non-simple codes */
  52. inflate_huft * FAR *t,  /* result: starting table */
  53. uIntf *m,               /* maximum lookup bits, returns actual */
  54. inflate_huft *hp,       /* space for trees */
  55. uInt *hn,               /* hufts used in space */
  56. uIntf *v)               /* working area: values in order of bit length */
  57. /* Given a list of code lengths and a maximum table size, make a set of
  58.    tables to decode that set of codes.  Return Z_OK on success, Z_BUF_ERROR
  59.    if the given code set is incomplete (the tables are still built in this
  60.    case), Z_DATA_ERROR if the input is invalid (an over-subscribed set of
  61.    lengths), or Z_MEM_ERROR if not enough memory. */
  62. {
  63.  
  64.   uInt a;                       /* counter for codes of length k */
  65.   uInt c[BMAX+1];               /* bit length count table */
  66.   uInt f;                       /* i repeats in table every f entries */
  67.   int g;                        /* maximum code length */
  68.   int h;                        /* table level */
  69.   register uInt i;              /* counter, current code */
  70.   register uInt j;              /* counter */
  71.   register int k;               /* number of bits in current code */
  72.   int l;                        /* bits per table (returned in m) */
  73.   uInt mask;                    /* (1 << w) - 1, to avoid cc -O bug on HP */
  74.   register uIntf *p;            /* pointer into c[], b[], or v[] */
  75.   inflate_huft *q;              /* points to current table */
  76.   struct inflate_huft_s r;      /* table entry for structure assignment */
  77.   inflate_huft *u[BMAX];        /* table stack */
  78.   register int w;               /* bits before this table == (l * h) */
  79.   uInt x[BMAX+1];               /* bit offsets, then code stack */
  80.   uIntf *xp;                    /* pointer into x */
  81.   int y;                        /* number of dummy codes added */
  82.   uInt z;                       /* number of entries in current table */
  83.  
  84.  
  85.   /* Generate counts for each bit length */
  86.   p=c;
  87.   y=16; while (y--) *p++ = 0;
  88.   p = b;  
  89.   i = n;
  90.   do {
  91.     c[*p++]++;                  /* assume all entries <= BMAX */
  92.   } while (--i);
  93.   if (c[0] == n)                /* null input--all zero length codes */
  94.   {
  95.     *t = (inflate_huft *)Z_NULL;
  96.     *m = 0;
  97.     return Z_OK;
  98.   }
  99.  
  100.  
  101.   /* Find minimum and maximum length, bound *m by those */
  102.   l = *m;
  103.   for (j = 1; j <= BMAX; j++)
  104.     if (c[j])
  105.       break;
  106.   k = j;                        /* minimum code length */
  107.   if ((uInt)l < j)
  108.     l = j;
  109.   for (i = BMAX; i; i--)
  110.     if (c[i])
  111.       break;
  112.   g = i;                        /* maximum code length */
  113.   if ((uInt)l > i)
  114.     l = i;
  115.   *m = l;
  116.  
  117.  
  118.   /* Adjust last length count to fill out codes, if needed */
  119.   for (y = 1 << j; j < i; j++, y <<= 1)
  120.     if ((y -= c[j]) < 0)
  121.       return Z_DATA_ERROR;
  122.   if ((y -= c[i]) < 0)
  123.     return Z_DATA_ERROR;
  124.   c[i] += y;
  125.  
  126.  
  127.   /* Generate starting offsets into the value table for each length */
  128.   x[1] = j = 0;
  129.   p = c + 1;  xp = x + 2;
  130.   while (--i) {                 /* note that i == g from above */
  131.     *xp++ = (j += *p++);
  132.   }
  133.  
  134.  
  135.   /* Make a table of values in order of bit lengths */
  136.   p = b;  i = 0;
  137.   do {
  138.     if ((j = *p++) != 0)
  139.       v[x[j]++] = i;
  140.   } while (++i < n);
  141.   n = x[g];                     /* set n to length of v */
  142.  
  143.  
  144.   /* Generate the Huffman codes and for each, make the table entries */
  145.   x[0] = i = 0;                 /* first Huffman code is zero */
  146.   p = v;                        /* grab values in bit order */
  147.   h = -1;                       /* no tables yet--level -1 */
  148.   w = -l;                       /* bits decoded == (l * h) */
  149.   u[0] = (inflate_huft *)Z_NULL;        /* just to keep compilers happy */
  150.   q = (inflate_huft *)Z_NULL;   /* ditto */
  151.   z = 0;                        /* ditto */
  152.  
  153.   /* go through the bit lengths (k already is bits in shortest code) */
  154.   for (; k <= g; k++)
  155.   {
  156.     a = c[k];
  157.     while (a--)
  158.     {
  159.       /* here i is the Huffman code of length k bits for value *p */
  160.       /* make tables up to required level */
  161.       while (k > w + l)
  162.       {
  163.         h++;
  164.         w += l;                 /* previous table always l bits */
  165.  
  166.         /* compute minimum size table less than or equal to l bits */
  167.         z = g - w;
  168.         z = z > (uInt)l ? l : z;        /* table size upper limit */
  169.         if ((f = 1 << (j = k - w)) > a + 1)     /* try a k-w bit table */
  170.         {                       /* too few codes for k-w bit table */
  171.           f -= a + 1;           /* deduct codes from patterns left */
  172.           xp = c + k;
  173.           if (j < z)
  174.             while (++j < z)     /* try smaller tables up to z bits */
  175.             {
  176.               if ((f <<= 1) <= *++xp)
  177.                 break;          /* enough codes to use up j bits */
  178.               f -= *xp;         /* else deduct codes from patterns */
  179.             }
  180.         }
  181.         z = 1 << j;             /* table entries for j-bit table */
  182.  
  183.         /* allocate new table */
  184.         if (*hn + z > MANY)     /* (note: doesn't matter for fixed) */
  185.           return Z_MEM_ERROR;   /* not enough memory */
  186.         u[h] = q = hp + *hn;
  187.         *hn += z;
  188.  
  189.         /* connect to last table, if there is one */
  190.         if (h)
  191.         {
  192.           x[h] = i;             /* save pattern for backing up */
  193.           r.bits = (Byte)l;     /* bits to dump before this table */
  194.           r.exop = (Byte)j;     /* bits in this table */
  195.           j = i >> (w - l);
  196.           r.base = (uInt)(q - u[h-1] - j);   /* offset to this table */
  197.           u[h-1][j] = r;        /* connect to last table */
  198.         }
  199.         else
  200.           *t = q;               /* first table is returned result */
  201.       }
  202.  
  203.       /* set up table entry in r */
  204.       r.bits = (Byte)(k - w);
  205.       if (p >= v + n)
  206.         r.exop = 128 + 64;      /* out of values--invalid code */
  207.       else if (*p < s)
  208.       {
  209.         r.exop = (Byte)(*p < 256 ? 0 : 32 + 64);     /* 256 is end-of-block */
  210.         r.base = *p++;          /* simple code is just the value */
  211.       }
  212.       else
  213.       {
  214.         r.exop = (Byte)(e[*p - s] + 16 + 64);/* non-simple--look up in lists */
  215.         r.base = d[*p++ - s];
  216.       }
  217.  
  218.       /* fill code-like entries with r */
  219.       f = 1 << (k - w);
  220.       for (j = i >> w; j < z; j += f)
  221.         q[j] = r;
  222.  
  223.       /* backwards increment the k-bit code i */
  224.       for (j = 1 << (k - 1); i & j; j >>= 1)
  225.         i ^= j;
  226.       i ^= j;
  227.  
  228.       /* backup over finished tables */
  229.       mask = (1 << w) - 1;      /* needed on HP, cc -O bug */
  230.       while ((i & mask) != x[h])
  231.       {
  232.         h--;                    /* don't need to update q */
  233.         w -= l;
  234.         mask = (1 << w) - 1;
  235.       }
  236.     }
  237.   }
  238.  
  239.  
  240.   /* Return Z_BUF_ERROR if we were given an incomplete table */
  241.   return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
  242. }
  243.  
  244.  
  245. int inflate_trees_bits(c, bb, tb, hp, z)
  246. uIntf *c;               /* 19 code lengths */
  247. uIntf *bb;              /* bits tree desired/actual depth */
  248. inflate_huft * FAR *tb; /* bits tree result */
  249. inflate_huft *hp;       /* space for trees */
  250. z_streamp z;            /* for messages */
  251. {
  252.   int r;
  253.   uInt hn = 0;          /* hufts used in space */
  254.   uIntf *v;             /* work area for huft_build */
  255.  
  256.   if ((v = (uIntf*)ZALLOC(z, 19, sizeof(uInt))) == Z_NULL)
  257.     return Z_MEM_ERROR;
  258.   r = huft_build(c, 19, 19, (short *)Z_NULL, (short*)Z_NULL,
  259.                  tb, bb, hp, &hn, v);
  260.   if (r == Z_BUF_ERROR || *bb == 0)
  261.   {
  262. //    z->msg = (char*)"incomplete dynamic bit lengths tree";
  263.     r = Z_DATA_ERROR;
  264.   }
  265.   ZFREE(z, v);
  266.   return r;
  267. }
  268.  
  269.  
  270. int inflate_trees_dynamic(nl, nd, c, bl, bd, tl, td, hp, z)
  271. uInt nl;                /* number of literal/length codes */
  272. uInt nd;                /* number of distance codes */
  273. uIntf *c;               /* that many (total) code lengths */
  274. uIntf *bl;              /* literal desired/actual bit depth */
  275. uIntf *bd;              /* distance desired/actual bit depth */
  276. inflate_huft * FAR *tl; /* literal/length tree result */
  277. inflate_huft * FAR *td; /* distance tree result */
  278. inflate_huft *hp;       /* space for trees */
  279. z_streamp z;            /* for messages */
  280. {
  281.   int r;
  282.   uInt hn = 0;          /* hufts used in space */
  283.   uIntf *v;             /* work area for huft_build */
  284.  
  285.   /* allocate work area */
  286.   if ((v = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
  287.     return Z_MEM_ERROR;
  288.  
  289.   /* build literal/length tree */
  290.   r = huft_build(c, nl, 257, cplens, cplext, tl, bl, hp, &hn, v);
  291.   if (r != Z_OK || *bl == 0)
  292.   {
  293.     if (r != Z_MEM_ERROR)
  294.     {
  295. //      z->msg = (char*)"incomplete literal/length tree";
  296.       r = Z_DATA_ERROR;
  297.     }
  298.     ZFREE(z, v);
  299.     return r;
  300.   }
  301.  
  302.   /* build distance tree */
  303.   r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, hp, &hn, v);
  304.   if (r != Z_OK || (*bd == 0 && nl > 257))
  305.   {
  306.     if (r != Z_MEM_ERROR)
  307.     {
  308.       r = Z_DATA_ERROR;
  309.     }
  310.     ZFREE(z, v);
  311.     return r;
  312.   }
  313.  
  314.   /* done */
  315.   ZFREE(z, v);
  316.   return Z_OK;
  317. }
  318.  
  319.  
  320. /* build fixed tables only once--keep them here */
  321. local int fixed_built = 0;
  322. #define FIXEDH 544      /* number of hufts used by fixed tables */
  323. local inflate_huft fixed_mem[FIXEDH];
  324. local uInt fixed_bl;
  325. local uInt fixed_bd;
  326. local inflate_huft *fixed_tl;
  327. local inflate_huft *fixed_td;
  328.  
  329.  
  330. int inflate_trees_fixed(bl, bd, tl, td, z)
  331. uIntf *bl;               /* literal desired/actual bit depth */
  332. uIntf *bd;               /* distance desired/actual bit depth */
  333. inflate_huft * FAR *tl;  /* literal/length tree result */
  334. inflate_huft * FAR *td;  /* distance tree result */
  335. z_streamp z;             /* for memory allocation */
  336. {
  337.   /* build fixed tables if not already */
  338.   if (!fixed_built)
  339.   {
  340.     int k;              /* temporary variable */
  341.     uInt f = 0;         /* number of hufts used in fixed_mem */
  342.     uIntf *c;           /* length list for huft_build */
  343.     uIntf *v;           /* work area for huft_build */
  344.  
  345.     /* allocate memory */
  346.     if ((c = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
  347.       return Z_MEM_ERROR;
  348.     if ((v = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
  349.     {
  350.       ZFREE(z, c);
  351.       return Z_MEM_ERROR;
  352.     }
  353.  
  354.     /* literal table */
  355.     for (k = 0; k < 144; k++) c[k] = 8;
  356.     for (; k < 256; k++) c[k] = 9;
  357.     for (; k < 280; k++) c[k] = 7;
  358.     for (; k < 288; k++) c[k] = 8;
  359.     fixed_bl = 9;
  360.     huft_build(c, 288, 257, cplens, cplext, &fixed_tl, &fixed_bl, fixed_mem, &f, v);
  361.  
  362.     /* distance table */
  363.     for (k = 0; k < 30; k++) c[k] = 5;
  364.     fixed_bd = 5;
  365.     huft_build(c, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd, fixed_mem, &f, v);
  366.  
  367.     /* done */
  368.     ZFREE(z, v);
  369.     ZFREE(z, c);
  370.     fixed_built = 1;
  371.   }
  372.   *bl = fixed_bl;
  373.   *bd = fixed_bd;
  374.   *tl = fixed_tl;
  375.   *td = fixed_td;
  376.   return Z_OK;
  377. }
  378.