ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

inftrees.c
Go to the documentation of this file.
00001 /* inftrees.c -- generate Huffman trees for efficient decoding
00002  * Copyright (C) 1995-2002 Mark Adler
00003  * For conditions of distribution and use, see copyright notice in zlib.h
00004  */
00005 
00006 #include "zutil.h"
00007 #include "inftrees.h"
00008 
00009 #if !defined(BUILDFIXED) && !defined(STDC)
00010 #  define BUILDFIXED   /* non ANSI compilers may not accept inffixed.h */
00011 #endif
00012 
00013 
00014 #if 0
00015 local const char inflate_copyright[] =
00016    " inflate 1.1.4 Copyright 1995-2002 Mark Adler ";
00017 #endif
00018 /*
00019   If you use the zlib library in a product, an acknowledgment is welcome
00020   in the documentation of your product. If for some reason you cannot
00021   include such an acknowledgment, I would appreciate that you keep this
00022   copyright string in the executable of your product.
00023  */
00024 
00025 /* simplify the use of the inflate_huft type with some defines */
00026 #define exop word.what.Exop
00027 #define bits word.what.Bits
00028 
00029 
00030 local int huft_build OF((
00031     uIntf *,            /* code lengths in bits */
00032     uInt,               /* number of codes */
00033     uInt,               /* number of "simple" codes */
00034     const uIntf *,      /* list of base values for non-simple codes */
00035     const uIntf *,      /* list of extra bits for non-simple codes */
00036     inflate_huft * FAR*,/* result: starting table */
00037     uIntf *,            /* maximum lookup bits (returns actual) */
00038     inflate_huft *,     /* space for trees */
00039     uInt *,             /* hufts used in space */
00040     uIntf * ));         /* space for values */
00041 
00042 /* Tables for deflate from PKZIP's appnote.txt. */
00043 local const uInt cplens[31] = { /* Copy lengths for literal codes 257..285 */
00044         3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
00045         35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
00046         /* see note #13 above about 258 */
00047 local const uInt cplext[31] = { /* Extra bits for literal codes 257..285 */
00048         0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
00049         3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112}; /* 112==invalid */
00050 local const uInt cpdist[30] = { /* Copy offsets for distance codes 0..29 */
00051         1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
00052         257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
00053         8193, 12289, 16385, 24577};
00054 local const uInt cpdext[30] = { /* Extra bits for distance codes */
00055         0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
00056         7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
00057         12, 12, 13, 13};
00058 
00059 /*
00060    Huffman code decoding is performed using a multi-level table lookup.
00061    The fastest way to decode is to simply build a lookup table whose
00062    size is determined by the longest code.  However, the time it takes
00063    to build this table can also be a factor if the data being decoded
00064    is not very long.  The most common codes are necessarily the
00065    shortest codes, so those codes dominate the decoding time, and hence
00066    the speed.  The idea is you can have a shorter table that decodes the
00067    shorter, more probable codes, and then point to subsidiary tables for
00068    the longer codes.  The time it costs to decode the longer codes is
00069    then traded against the time it takes to make longer tables.
00070 
00071    This results of this trade are in the variables lbits and dbits
00072    below.  lbits is the number of bits the first level table for literal/
00073    length codes can decode in one step, and dbits is the same thing for
00074    the distance codes.  Subsequent tables are also less than or equal to
00075    those sizes.  These values may be adjusted either when all of the
00076    codes are shorter than that, in which case the longest code length in
00077    bits is used, or when the shortest code is *longer* than the requested
00078    table size, in which case the length of the shortest code in bits is
00079    used.
00080 
00081    There are two different values for the two tables, since they code a
00082    different number of possibilities each.  The literal/length table
00083    codes 286 possible values, or in a flat code, a little over eight
00084    bits.  The distance table codes 30 possible values, or a little less
00085    than five bits, flat.  The optimum values for speed end up being
00086    about one bit more than those, so lbits is 8+1 and dbits is 5+1.
00087    The optimum values may differ though from machine to machine, and
00088    possibly even between compilers.  Your mileage may vary.
00089  */
00090 
00091 
00092 /* If BMAX needs to be larger than 16, then h and x[] should be uLong. */
00093 #define BMAX 15         /* maximum bit length of any code */
00094 
00095 local int huft_build( /* b, n, s, d, e, t, m, hp, hn, v) */
00096 uIntf *b,               /* code lengths in bits (all assumed <= BMAX) */
00097 uInt n,                 /* number of codes (assumed <= 288) */
00098 uInt s,                 /* number of simple-valued codes (0..s-1) */
00099 const uIntf *d,         /* list of base values for non-simple codes */
00100 const uIntf *e,         /* list of extra bits for non-simple codes */
00101 inflate_huft * FAR *t,  /* result: starting table */
00102 uIntf *m,               /* maximum lookup bits, returns actual */
00103 inflate_huft *hp,       /* space for trees */
00104 uInt *hn,               /* hufts used in space */
00105 uIntf *v                /* working area: values in order of bit length */
00106 /* Given a list of code lengths and a maximum table size, make a set of
00107    tables to decode that set of codes.  Return Z_OK on success, Z_BUF_ERROR
00108    if the given code set is incomplete (the tables are still built in this
00109    case), or Z_DATA_ERROR if the input is invalid. */
00110 )
00111 {
00112 
00113   uInt a;                       /* counter for codes of length k */
00114   uInt c[BMAX+1];               /* bit length count table */
00115   uInt f;                       /* i repeats in table every f entries */
00116   int g;                        /* maximum code length */
00117   int h;                        /* table level */
00118   register uInt i;              /* counter, current code */
00119   register uInt j;              /* counter */
00120   register int k;               /* number of bits in current code */
00121   int l;                        /* bits per table (returned in m) */
00122   uInt mask;                    /* (1 << w) - 1, to avoid cc -O bug on HP */
00123   register uIntf *p;            /* pointer into c[], b[], or v[] */
00124   inflate_huft *q;              /* points to current table */
00125   struct inflate_huft_s r;      /* table entry for structure assignment */
00126   inflate_huft *u[BMAX];        /* table stack */
00127   register int w;               /* bits before this table == (l * h) */
00128   uInt x[BMAX+1];               /* bit offsets, then code stack */
00129   uIntf *xp;                    /* pointer into x */
00130   int y;                        /* number of dummy codes added */
00131   uInt z;                       /* number of entries in current table */
00132 
00133 
00134   /* Make compiler happy */
00135   r.base = 0;
00136 
00137   /* Generate counts for each bit length */
00138   p = c;
00139 #define C0 *p++ = 0;
00140 #define C2 C0 C0 C0 C0
00141 #define C4 C2 C2 C2 C2
00142   C4                            /* clear c[]--assume BMAX+1 is 16 */
00143   p = b;  i = n;
00144   do {
00145     c[*p++]++;                  /* assume all entries <= BMAX */
00146   } while (--i);
00147   if (c[0] == n)                /* null input--all zero length codes */
00148   {
00149     *t = (inflate_huft *)Z_NULL;
00150     *m = 0;
00151     return Z_OK;
00152   }
00153 
00154 
00155   /* Find minimum and maximum length, bound *m by those */
00156   l = *m;
00157   for (j = 1; j <= BMAX; j++)
00158     if (c[j])
00159       break;
00160   k = j;                        /* minimum code length */
00161   if ((uInt)l < j)
00162     l = j;
00163   for (i = BMAX; i; i--)
00164     if (c[i])
00165       break;
00166   g = i;                        /* maximum code length */
00167   if ((uInt)l > i)
00168     l = i;
00169   *m = l;
00170 
00171 
00172   /* Adjust last length count to fill out codes, if needed */
00173   for (y = 1 << j; j < i; j++, y <<= 1)
00174     if ((y -= c[j]) < 0)
00175       return Z_DATA_ERROR;
00176   if ((y -= c[i]) < 0)
00177     return Z_DATA_ERROR;
00178   c[i] += y;
00179 
00180 
00181   /* Generate starting offsets into the value table for each length */
00182   x[1] = j = 0;
00183   p = c + 1;  xp = x + 2;
00184   while (--i) {                 /* note that i == g from above */
00185     *xp++ = (j += *p++);
00186   }
00187 
00188 
00189   /* Make a table of values in order of bit lengths */
00190   p = b;  i = 0;
00191   do {
00192     if ((j = *p++) != 0)
00193       v[x[j]++] = i;
00194   } while (++i < n);
00195   n = x[g];                     /* set n to length of v */
00196 
00197 
00198   /* Generate the Huffman codes and for each, make the table entries */
00199   x[0] = i = 0;                 /* first Huffman code is zero */
00200   p = v;                        /* grab values in bit order */
00201   h = -1;                       /* no tables yet--level -1 */
00202   w = -l;                       /* bits decoded == (l * h) */
00203   u[0] = (inflate_huft *)Z_NULL;        /* just to keep compilers happy */
00204   q = (inflate_huft *)Z_NULL;   /* ditto */
00205   z = 0;                        /* ditto */
00206 
00207   /* go through the bit lengths (k already is bits in shortest code) */
00208   for (; k <= g; k++)
00209   {
00210     a = c[k];
00211     while (a--)
00212     {
00213       /* here i is the Huffman code of length k bits for value *p */
00214       /* make tables up to required level */
00215       while (k > w + l)
00216       {
00217         h++;
00218         w += l;                 /* previous table always l bits */
00219 
00220         /* compute minimum size table less than or equal to l bits */
00221         z = g - w;
00222         z = z > (uInt)l ? (uInt)l : z;        /* table size upper limit */
00223         if ((f = 1 << (j = k - w)) > a + 1)     /* try a k-w bit table */
00224         {                       /* too few codes for k-w bit table */
00225           f -= a + 1;           /* deduct codes from patterns left */
00226           xp = c + k;
00227           if (j < z)
00228             while (++j < z)     /* try smaller tables up to z bits */
00229             {
00230               if ((f <<= 1) <= *++xp)
00231                 break;          /* enough codes to use up j bits */
00232               f -= *xp;         /* else deduct codes from patterns */
00233             }
00234         }
00235         z = 1 << j;             /* table entries for j-bit table */
00236 
00237         /* allocate new table */
00238         if (*hn + z > MANY)     /* (note: doesn't matter for fixed) */
00239           return Z_DATA_ERROR;  /* overflow of MANY */
00240         u[h] = q = hp + *hn;
00241         *hn += z;
00242 
00243         /* connect to last table, if there is one */
00244         if (h)
00245         {
00246           x[h] = i;             /* save pattern for backing up */
00247           r.bits = (Byte)l;     /* bits to dump before this table */
00248           r.exop = (Byte)j;     /* bits in this table */
00249           j = i >> (w - l);
00250           r.base = (uInt)(q - u[h-1] - j);   /* offset to this table */
00251           u[h-1][j] = r;        /* connect to last table */
00252         }
00253         else
00254           *t = q;               /* first table is returned result */
00255       }
00256 
00257       /* set up table entry in r */
00258       r.bits = (Byte)(k - w);
00259       if (p >= v + n)
00260         r.exop = 128 + 64;      /* out of values--invalid code */
00261       else if (*p < s)
00262       {
00263         r.exop = (Byte)(*p < 256 ? 0 : 32 + 64);     /* 256 is end-of-block */
00264         r.base = *p++;          /* simple code is just the value */
00265       }
00266       else
00267       {
00268         r.exop = (Byte)(e[*p - s] + 16 + 64);/* non-simple--look up in lists */
00269         r.base = d[*p++ - s];
00270       }
00271 
00272       /* fill code-like entries with r */
00273       f = 1 << (k - w);
00274       for (j = i >> w; j < z; j += f)
00275         q[j] = r;
00276 
00277       /* backwards increment the k-bit code i */
00278       for (j = 1 << (k - 1); i & j; j >>= 1)
00279         i ^= j;
00280       i ^= j;
00281 
00282       /* backup over finished tables */
00283       mask = (1 << w) - 1;      /* needed on HP, cc -O bug */
00284       while ((i & mask) != x[h])
00285       {
00286         h--;                    /* don't need to update q */
00287         w -= l;
00288         mask = (1 << w) - 1;
00289       }
00290     }
00291   }
00292 
00293 
00294   /* Return Z_BUF_ERROR if we were given an incomplete table */
00295   return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
00296 }
00297 
00298 
00299 local int inflate_trees_bits( /* c, bb, tb, hp, z) */
00300 uIntf *c,               /* 19 code lengths */
00301 uIntf *bb,              /* bits tree desired/actual depth */
00302 inflate_huft * FAR *tb, /* bits tree result */
00303 inflate_huft *hp,       /* space for trees */
00304 z_streamp z             /* for messages */
00305 )
00306 {
00307   int r;
00308   uInt hn = 0;          /* hufts used in space */
00309   uIntf *v;             /* work area for huft_build */
00310 
00311   if ((v = (uIntf*)ZALLOC(z, 19, sizeof(uInt))) == Z_NULL)
00312     return Z_MEM_ERROR;
00313   r = huft_build(c, 19, 19, (uIntf*)Z_NULL, (uIntf*)Z_NULL,
00314                  tb, bb, hp, &hn, v);
00315   if (r == Z_DATA_ERROR)
00316     z->msg = (char*)"oversubscribed dynamic bit lengths tree";
00317   else if (r == Z_BUF_ERROR || *bb == 0)
00318   {
00319     z->msg = (char*)"incomplete dynamic bit lengths tree";
00320     r = Z_DATA_ERROR;
00321   }
00322   ZFREE(z, v);
00323   return r;
00324 }
00325 
00326 
00327 local int inflate_trees_dynamic( /* nl, nd, c, bl, bd, tl, td, hp, z) */
00328 uInt nl,                /* number of literal/length codes */
00329 uInt nd,                /* number of distance codes */
00330 uIntf *c,               /* that many (total) code lengths */
00331 uIntf *bl,              /* literal desired/actual bit depth */
00332 uIntf *bd,              /* distance desired/actual bit depth */
00333 inflate_huft * FAR *tl, /* literal/length tree result */
00334 inflate_huft * FAR *td, /* distance tree result */
00335 inflate_huft *hp,       /* space for trees */
00336 z_streamp z             /* for messages */
00337 )
00338 {
00339   int r;
00340   uInt hn = 0;          /* hufts used in space */
00341   uIntf *v;             /* work area for huft_build */
00342 
00343   /* allocate work area */
00344   if ((v = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
00345     return Z_MEM_ERROR;
00346 
00347   /* build literal/length tree */
00348   r = huft_build(c, nl, 257, cplens, cplext, tl, bl, hp, &hn, v);
00349   if (r != Z_OK || *bl == 0)
00350   {
00351     if (r == Z_DATA_ERROR)
00352       z->msg = (char*)"oversubscribed literal/length tree";
00353     else if (r != Z_MEM_ERROR)
00354     {
00355       z->msg = (char*)"incomplete literal/length tree";
00356       r = Z_DATA_ERROR;
00357     }
00358     ZFREE(z, v);
00359     return r;
00360   }
00361 
00362   /* build distance tree */
00363   r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, hp, &hn, v);
00364   if (r != Z_OK || (*bd == 0 && nl > 257))
00365   {
00366     if (r == Z_DATA_ERROR)
00367       z->msg = (char*)"oversubscribed distance tree";
00368     else if (r == Z_BUF_ERROR) {
00369 #if 0
00370     {
00371 #endif
00372 #ifdef PKZIP_BUG_WORKAROUND
00373       r = Z_OK;
00374     }
00375 #else
00376       z->msg = (char*)"incomplete distance tree";
00377       r = Z_DATA_ERROR;
00378     }
00379     else if (r != Z_MEM_ERROR)
00380     {
00381       z->msg = (char*)"empty distance tree with lengths";
00382       r = Z_DATA_ERROR;
00383     }
00384     ZFREE(z, v);
00385     return r;
00386 #endif
00387   }
00388 
00389   /* done */
00390   ZFREE(z, v);
00391   return Z_OK;
00392 }
00393 
00394 
00395 /* build fixed tables only once--keep them here */
00396 #ifdef BUILDFIXED
00397 local int fixed_built = 0;
00398 #define FIXEDH 544      /* number of hufts used by fixed tables */
00399 local inflate_huft fixed_mem[FIXEDH];
00400 local uInt fixed_bl;
00401 local uInt fixed_bd;
00402 local inflate_huft *fixed_tl;
00403 local inflate_huft *fixed_td;
00404 #else
00405 #include "inffixed.h"
00406 #endif
00407 
00408 
00409 local int inflate_trees_fixed( /* bl, bd, tl, td, z) */
00410 uIntf *bl,                      /* literal desired/actual bit depth */
00411 uIntf *bd,                      /* distance desired/actual bit depth */
00412 const inflate_huft * FAR *tl,   /* literal/length tree result */
00413 const inflate_huft * FAR *td,   /* distance tree result */
00414 z_streamp z                     /* for memory allocation */
00415 )
00416 {
00417 #ifdef BUILDFIXED
00418   /* build fixed tables if not already */
00419   if (!fixed_built)
00420   {
00421     int k;              /* temporary variable */
00422     uInt f = 0;         /* number of hufts used in fixed_mem */
00423     uIntf *c;           /* length list for huft_build */
00424     uIntf *v;           /* work area for huft_build */
00425 
00426     /* allocate memory */
00427     if ((c = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
00428       return Z_MEM_ERROR;
00429     if ((v = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
00430     {
00431       ZFREE(z, c);
00432       return Z_MEM_ERROR;
00433     }
00434 
00435     /* literal table */
00436     for (k = 0; k < 144; k++)
00437       c[k] = 8;
00438     for (; k < 256; k++)
00439       c[k] = 9;
00440     for (; k < 280; k++)
00441       c[k] = 7;
00442     for (; k < 288; k++)
00443       c[k] = 8;
00444     fixed_bl = 9;
00445     huft_build(c, 288, 257, cplens, cplext, &fixed_tl, &fixed_bl,
00446                fixed_mem, &f, v);
00447 
00448     /* distance table */
00449     for (k = 0; k < 30; k++)
00450       c[k] = 5;
00451     fixed_bd = 5;
00452     huft_build(c, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd,
00453                fixed_mem, &f, v);
00454 
00455     /* done */
00456     ZFREE(z, v);
00457     ZFREE(z, c);
00458     fixed_built = 1;
00459   }
00460 #else
00461   FT_UNUSED(z);
00462 #endif
00463   *bl = fixed_bl;
00464   *bd = fixed_bd;
00465   *tl = fixed_tl;
00466   *td = fixed_td;
00467   return Z_OK;
00468 }

Generated on Sat May 26 2012 04:32:45 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.