source: svn/newcon3bcm2_21bu/dst/dlib/src/ZLIB/inftrees.c

Last change on this file was 76, checked in by megakiss, 10 years ago

1W 대기전력을 만족시키기 위하여 POWEROFF시 튜너를 Standby 상태로 함

  • Property svn:executable set to *
File size: 16.4 KB
Line 
1/*
2 * $Id: //suprahd/releases/suprahd_163/suprahd_ztvapp640_163/drivers/graphics/ZLIB/inftrees.c#1 $
3 * $Revision: #1 $
4 * $DateTime: 2006/02/24 17:51:46 $
5 * $Change: 42566 $
6 * $Author: pryush.sharma $
7 */
8
9/* inftrees.c -- generate Huffman trees for efficient decoding
10 * Copyright (C) 1995-1998 Mark Adler
11 * For conditions of distribution and use, see copyright notice in zlib.h
12 */
13
14#include "zutil.h"
15#include "inftrees.h"
16
17#if !defined(BUILDFIXED) && !defined(STDC)
18#  define BUILDFIXED   /* non ANSI compilers may not accept inffixed.h */
19#endif
20
21const char inflate_copyright[] =
22   " inflate 1.1.3 Copyright 1995-1998 Mark Adler ";
23/*
24  If you use the zlib library in a product, an acknowledgment is welcome
25  in the documentation of your product. If for some reason you cannot
26  include such an acknowledgment, I would appreciate that you keep this
27  copyright string in the executable of your product.
28 */
29struct internal_state  {int dummy;}; /* for buggy compilers */
30
31/* simplify the use of the inflate_huft type with some defines */
32#define exop word.what.Exop
33#define bits word.what.Bits
34
35
36local int huft_build OF((
37    uIntf *,            /* code lengths in bits */
38    uInt,               /* number of codes */
39    uInt,               /* number of "simple" codes */
40    const uIntf *,      /* list of base values for non-simple codes */
41    const uIntf *,      /* list of extra bits for non-simple codes */
42    inflate_huft * FAR*,/* result: starting table */
43    uIntf *,            /* maximum lookup bits (returns actual) */
44    inflate_huft *,     /* space for trees */
45    uInt *,             /* hufts used in space */
46    uIntf * ));         /* space for values */
47
48/* Tables for deflate from PKZIP's appnote.txt. */
49local const uInt cplens[31] = { /* Copy lengths for literal codes 257..285 */
50        3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
51        35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
52        /* see note #13 above about 258 */
53local const uInt cplext[31] = { /* Extra bits for literal codes 257..285 */
54        0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
55        3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112}; /* 112==invalid */
56local const uInt cpdist[30] = { /* Copy offsets for distance codes 0..29 */
57        1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
58        257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
59        8193, 12289, 16385, 24577};
60local const uInt cpdext[30] = { /* Extra bits for distance codes */
61        0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
62        7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
63        12, 12, 13, 13};
64
65/*
66   Huffman code decoding is performed using a multi-level table lookup.
67   The fastest way to decode is to simply build a lookup table whose
68   size is determined by the longest code.  However, the time it takes
69   to build this table can also be a factor if the data being decoded
70   is not very long.  The most common codes are necessarily the
71   shortest codes, so those codes dominate the decoding time, and hence
72   the speed.  The idea is you can have a shorter table that decodes the
73   shorter, more probable codes, and then point to subsidiary tables for
74   the longer codes.  The time it costs to decode the longer codes is
75   then traded against the time it takes to make longer tables.
76
77   This results of this trade are in the variables lbits and dbits
78   below.  lbits is the number of bits the first level table for literal/
79   length codes can decode in one step, and dbits is the same thing for
80   the distance codes.  Subsequent tables are also less than or equal to
81   those sizes.  These values may be adjusted either when all of the
82   codes are shorter than that, in which case the longest code length in
83   bits is used, or when the shortest code is *longer* than the requested
84   table size, in which case the length of the shortest code in bits is
85   used.
86
87   There are two different values for the two tables, since they code a
88   different number of possibilities each.  The literal/length table
89   codes 286 possible values, or in a flat code, a little over eight
90   bits.  The distance table codes 30 possible values, or a little less
91   than five bits, flat.  The optimum values for speed end up being
92   about one bit more than those, so lbits is 8+1 and dbits is 5+1.
93   The optimum values may differ though from machine to machine, and
94   possibly even between compilers.  Your mileage may vary.
95 */
96
97
98/* If BMAX needs to be larger than 16, then h and x[] should be uLong. */
99#define BMAX 15         /* maximum bit length of any code */
100
101local int huft_build(
102    uIntf *b,               /* code lengths in bits (all assumed <= BMAX) */
103    uInt n,                 /* number of codes (assumed <= 288) */
104    uInt s,                 /* number of simple-valued codes (0..s-1) */
105    const uIntf *d,         /* list of base values for non-simple codes */
106    const uIntf *e,         /* list of extra bits for non-simple codes */
107    inflate_huft * FAR *t,  /* result: starting table */
108    uIntf *m,               /* maximum lookup bits, returns actual */
109    inflate_huft *hp,       /* space for trees */
110    uInt *hn,               /* hufts used in space */
111    uIntf *v)               /* working area: values in order of bit length */
112/* Given a list of code lengths and a maximum table size, make a set of
113   tables to decode that set of codes.  Return Z_OK on success, Z_BUF_ERROR
114   if the given code set is incomplete (the tables are still built in this
115   case), Z_DATA_ERROR if the input is invalid (an over-subscribed set of
116   lengths), or Z_MEM_ERROR if not enough memory. */
117{
118
119  uInt a;                       /* counter for codes of length k */
120  uInt c[BMAX+1];               /* bit length count table */
121  uInt f;                       /* i repeats in table every f entries */
122  int g;                        /* maximum code length */
123  int h;                        /* table level */
124  register uInt i;              /* counter, current code */
125  register uInt j;              /* counter */
126  register int k;               /* number of bits in current code */
127  int l;                        /* bits per table (returned in m) */
128  uInt mask;                    /* (1 << w) - 1, to avoid cc -O bug on HP */
129  register uIntf *p;            /* pointer into c[], b[], or v[] */
130  inflate_huft *q;              /* points to current table */
131  struct inflate_huft_s r;      /* table entry for structure assignment */
132  inflate_huft *u[BMAX];        /* table stack */
133  register int w;               /* bits before this table == (l * h) */
134  uInt x[BMAX+1];               /* bit offsets, then code stack */
135  uIntf *xp;                    /* pointer into x */
136  int y;                        /* number of dummy codes added */
137  uInt z;                       /* number of entries in current table */
138
139
140  /* Generate counts for each bit length */
141  p = c;
142#define C0 *p++ = 0;
143#define C2 C0 C0 C0 C0
144#define C4 C2 C2 C2 C2
145  C4                            /* clear c[]--assume BMAX+1 is 16 */
146  p = b;  i = n;
147  do {
148    c[*p++]++;                  /* assume all entries <= BMAX */
149  } while (--i);
150  if (c[0] == n)                /* null input--all zero length codes */
151  {
152    *t = (inflate_huft *)Z_NULL;
153    *m = 0;
154    return Z_OK;
155  }
156
157
158  /* Find minimum and maximum length, bound *m by those */
159  l = *m;
160  for (j = 1; j <= BMAX; j++)
161    if (c[j])
162      break;
163  k = j;                        /* minimum code length */
164  if ((uInt)l < j)
165    l = j;
166  for (i = BMAX; i; i--)
167    if (c[i])
168      break;
169  g = i;                        /* maximum code length */
170  if ((uInt)l > i)
171    l = i;
172  *m = l;
173
174
175  /* Adjust last length count to fill out codes, if needed */
176  for (y = 1 << j; j < i; j++, y <<= 1)
177    if ((y -= c[j]) < 0)
178      return Z_DATA_ERROR;
179  if ((y -= c[i]) < 0)
180    return Z_DATA_ERROR;
181  c[i] += y;
182
183
184  /* Generate starting offsets into the value table for each length */
185  x[1] = j = 0;
186  p = c + 1;  xp = x + 2;
187  while (--i) {                 /* note that i == g from above */
188    *xp++ = (j += *p++);
189  }
190
191
192  /* Make a table of values in order of bit lengths */
193  p = b;  i = 0;
194  do {
195    if ((j = *p++) != 0)
196      v[x[j]++] = i;
197  } while (++i < n);
198  n = x[g];                     /* set n to length of v */
199
200
201  /* Generate the Huffman codes and for each, make the table entries */
202  x[0] = i = 0;                 /* first Huffman code is zero */
203  p = v;                        /* grab values in bit order */
204  h = -1;                       /* no tables yet--level -1 */
205  w = -l;                       /* bits decoded == (l * h) */
206  u[0] = (inflate_huft *)Z_NULL;        /* just to keep compilers happy */
207  q = (inflate_huft *)Z_NULL;   /* ditto */
208  z = 0;                        /* ditto */
209
210  /* go through the bit lengths (k already is bits in shortest code) */
211  for (; k <= g; k++)
212  {
213    a = c[k];
214    while (a--)
215    {
216      /* here i is the Huffman code of length k bits for value *p */
217      /* make tables up to required level */
218      while (k > w + l)
219      {
220        h++;
221        w += l;                 /* previous table always l bits */
222
223        /* compute minimum size table less than or equal to l bits */
224        z = g - w;
225        z = z > (uInt)l ? l : z;        /* table size upper limit */
226        if ((f = 1 << (j = k - w)) > a + 1)     /* try a k-w bit table */
227        {                       /* too few codes for k-w bit table */
228          f -= a + 1;           /* deduct codes from patterns left */
229          xp = c + k;
230          if (j < z)
231            while (++j < z)     /* try smaller tables up to z bits */
232            {
233              if ((f <<= 1) <= *++xp)
234                break;          /* enough codes to use up j bits */
235              f -= *xp;         /* else deduct codes from patterns */
236            }
237        }
238        z = 1 << j;             /* table entries for j-bit table */
239
240        /* allocate new table */
241        if (*hn + z > MANY)     /* (note: doesn't matter for fixed) */
242          return Z_MEM_ERROR;   /* not enough memory */
243        u[h] = q = hp + *hn;
244        *hn += z;
245
246        /* connect to last table, if there is one */
247        if (h)
248        {
249          x[h] = i;             /* save pattern for backing up */
250          r.bits = (Byte)l;     /* bits to dump before this table */
251          r.exop = (Byte)j;     /* bits in this table */
252          j = i >> (w - l);
253          r.base = (uInt)(q - u[h-1] - j);   /* offset to this table */
254          u[h-1][j] = r;        /* connect to last table */
255        }
256        else
257          *t = q;               /* first table is returned result */
258      }
259
260      /* set up table entry in r */
261      r.bits = (Byte)(k - w);
262      if (p >= v + n)
263        r.exop = 128 + 64;      /* out of values--invalid code */
264      else if (*p < s)
265      {
266        r.exop = (Byte)(*p < 256 ? 0 : 32 + 64);     /* 256 is end-of-block */
267        r.base = *p++;          /* simple code is just the value */
268      }
269      else
270      {
271        r.exop = (Byte)(e[*p - s] + 16 + 64);/* non-simple--look up in lists */
272        r.base = d[*p++ - s];
273      }
274
275      /* fill code-like entries with r */
276      f = 1 << (k - w);
277      for (j = i >> w; j < z; j += f)
278        q[j] = r;
279
280      /* backwards increment the k-bit code i */
281      for (j = 1 << (k - 1); i & j; j >>= 1)
282        i ^= j;
283      i ^= j;
284
285      /* backup over finished tables */
286      mask = (1 << w) - 1;      /* needed on HP, cc -O bug */
287      while ((i & mask) != x[h])
288      {
289        h--;                    /* don't need to update q */
290        w -= l;
291        mask = (1 << w) - 1;
292      }
293    }
294  }
295
296
297  /* Return Z_BUF_ERROR if we were given an incomplete table */
298  return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
299}
300
301
302int inflate_trees_bits(
303    uIntf *c,               /* 19 code lengths */
304    uIntf *bb,              /* bits tree desired/actual depth */
305    inflate_huft * FAR *tb, /* bits tree result */
306    inflate_huft *hp,       /* space for trees */
307    z_streamp z)            /* for messages */
308{
309  int r;
310  uInt hn = 0;          /* hufts used in space */
311  uIntf *v;             /* work area for huft_build */
312
313  if ((v = (uIntf*)ZALLOC(z, 19, sizeof(uInt))) == Z_NULL)
314    return Z_MEM_ERROR;
315  r = huft_build(c, 19, 19, (uIntf*)Z_NULL, (uIntf*)Z_NULL,
316                 tb, bb, hp, &hn, v);
317  if (r == Z_DATA_ERROR)
318    z->msg = (char*)"oversubscribed dynamic bit lengths tree";
319  else if (r == Z_BUF_ERROR || *bb == 0)
320  {
321    z->msg = (char*)"incomplete dynamic bit lengths tree";
322    r = Z_DATA_ERROR;
323  }
324  ZFREE(z, v);
325  return r;
326}
327
328
329int inflate_trees_dynamic(
330    uInt nl,                /* number of literal/length codes */
331    uInt nd,                /* number of distance codes */
332    uIntf *c,               /* that many (total) code lengths */
333    uIntf *bl,              /* literal desired/actual bit depth */
334    uIntf *bd,              /* distance desired/actual bit depth */
335    inflate_huft * FAR *tl, /* literal/length tree result */
336    inflate_huft * FAR *td, /* distance tree result */
337    inflate_huft *hp,       /* space for trees */
338    z_streamp z)            /* for messages */
339{
340  int r;
341  uInt hn = 0;          /* hufts used in space */
342  uIntf *v;             /* work area for huft_build */
343
344  /* allocate work area */
345  if ((v = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
346    return Z_MEM_ERROR;
347
348  /* build literal/length tree */
349  r = huft_build(c, nl, 257, cplens, cplext, tl, bl, hp, &hn, v);
350  if (r != Z_OK || *bl == 0)
351  {
352    if (r == Z_DATA_ERROR)
353      z->msg = (char*)"oversubscribed literal/length tree";
354    else if (r != Z_MEM_ERROR)
355    {
356      z->msg = (char*)"incomplete literal/length tree";
357      r = Z_DATA_ERROR;
358    }
359    ZFREE(z, v);
360    return r;
361  }
362
363  /* build distance tree */
364  r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, hp, &hn, v);
365  if (r != Z_OK || (*bd == 0 && nl > 257))
366  {
367    if (r == Z_DATA_ERROR)
368      z->msg = (char*)"oversubscribed distance tree";
369    else if (r == Z_BUF_ERROR) {
370#ifdef PKZIP_BUG_WORKAROUND
371      r = Z_OK;
372    }
373#else
374      z->msg = (char*)"incomplete distance tree";
375      r = Z_DATA_ERROR;
376    }
377    else if (r != Z_MEM_ERROR)
378    {
379      z->msg = (char*)"empty distance tree with lengths";
380      r = Z_DATA_ERROR;
381    }
382    ZFREE(z, v);
383    return r;
384#endif
385  }
386
387  /* done */
388  ZFREE(z, v);
389  return Z_OK;
390}
391
392
393/* build fixed tables only once--keep them here */
394#ifdef BUILDFIXED
395local int fixed_built = 0;
396#define FIXEDH 544      /* number of hufts used by fixed tables */
397local inflate_huft fixed_mem[FIXEDH];
398local uInt fixed_bl;
399local uInt fixed_bd;
400local inflate_huft *fixed_tl;
401local inflate_huft *fixed_td;
402#else
403#include "inffixed.h"
404#endif
405
406
407int inflate_trees_fixed(
408    uIntf *bl,               /* literal desired/actual bit depth */
409    uIntf *bd,               /* distance desired/actual bit depth */
410    inflate_huft * FAR *tl,  /* literal/length tree result */
411    inflate_huft * FAR *td,  /* distance tree result */
412    z_streamp z)             /* for memory allocation */
413{
414#ifdef BUILDFIXED
415  /* build fixed tables if not already */
416  if (!fixed_built)
417  {
418    int k;              /* temporary variable */
419    uInt f = 0;         /* number of hufts used in fixed_mem */
420    uIntf *c;           /* length list for huft_build */
421    uIntf *v;           /* work area for huft_build */
422
423    /* allocate memory */
424    if ((c = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
425      return Z_MEM_ERROR;
426    if ((v = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
427    {
428      ZFREE(z, c);
429      return Z_MEM_ERROR;
430    }
431
432    /* literal table */
433    for (k = 0; k < 144; k++)
434      c[k] = 8;
435    for (; k < 256; k++)
436      c[k] = 9;
437    for (; k < 280; k++)
438      c[k] = 7;
439    for (; k < 288; k++)
440      c[k] = 8;
441    fixed_bl = 9;
442    huft_build(c, 288, 257, cplens, cplext, &fixed_tl, &fixed_bl,
443               fixed_mem, &f, v);
444
445    /* distance table */
446    for (k = 0; k < 30; k++)
447      c[k] = 5;
448    fixed_bd = 5;
449    huft_build(c, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd,
450               fixed_mem, &f, v);
451
452    /* done */
453    ZFREE(z, v);
454    ZFREE(z, c);
455    fixed_built = 1;
456  }
457#endif
458  *bl = fixed_bl;
459  *bd = fixed_bd;
460  *tl = fixed_tl;
461  *td = fixed_td;
462  return Z_OK;
463}
Note: See TracBrowser for help on using the repository browser.