source: svn/newcon3bcm2_21bu/dst/dlib/src/ZLIB/infblock.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: 12.7 KB
Line 
1/*
2 * $Id: //suprahd/releases/suprahd_163/suprahd_ztvapp640_163/drivers/graphics/ZLIB/infblock.c#1 $
3 * $Revision: #1 $
4 * $DateTime: 2006/02/24 17:51:46 $
5 * $Change: 42566 $
6 * $Author: pryush.sharma $
7 */
8
9/* infblock.c -- interpret and process block types to last block
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 "infblock.h"
16#include "inftrees.h"
17#include "infcodes.h"
18#include "infutil.h"
19
20struct inflate_codes_state {int dummy;}; /* for buggy compilers */
21
22/* simplify the use of the inflate_huft type with some defines */
23#define exop word.what.Exop
24#define bits word.what.Bits
25
26/* Table for deflate from PKZIP's appnote.txt. */
27local const uInt border[] = { /* Order of the bit length code lengths */
28        16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
29
30/*
31   Notes beyond the 1.93a appnote.txt:
32
33   1. Distance pointers never point before the beginning of the output
34      stream.
35   2. Distance pointers can point back across blocks, up to 32k away.
36   3. There is an implied maximum of 7 bits for the bit length table and
37      15 bits for the actual data.
38   4. If only one code exists, then it is encoded using one bit.  (Zero
39      would be more efficient, but perhaps a little confusing.)  If two
40      codes exist, they are coded using one bit each (0 and 1).
41   5. There is no way of sending zero distance codes--a dummy must be
42      sent if there are none.  (History: a pre 2.0 version of PKZIP would
43      store blocks with no distance codes, but this was discovered to be
44      too harsh a criterion.)  Valid only for 1.93a.  2.04c does allow
45      zero distance codes, which is sent as one code of zero bits in
46      length.
47   6. There are up to 286 literal/length codes.  Code 256 represents the
48      end-of-block.  Note however that the static length tree defines
49      288 codes just to fill out the Huffman codes.  Codes 286 and 287
50      cannot be used though, since there is no length base or extra bits
51      defined for them.  Similarily, there are up to 30 distance codes.
52      However, static trees define 32 codes (all 5 bits) to fill out the
53      Huffman codes, but the last two had better not show up in the data.
54   7. Unzip can check dynamic Huffman blocks for complete code sets.
55      The exception is that a single code would not be complete (see #4).
56   8. The five bits following the block type is really the number of
57      literal codes sent minus 257.
58   9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
59      (1+6+6).  Therefore, to output three times the length, you output
60      three codes (1+1+1), whereas to output four times the same length,
61      you only need two codes (1+3).  Hmm.
62  10. In the tree reconstruction algorithm, Code = Code + Increment
63      only if BitLength(i) is not zero.  (Pretty obvious.)
64  11. Correction: 4 Bits: # of Bit Length codes - 4     (4 - 19)
65  12. Note: length code 284 can represent 227-258, but length code 285
66      really is 258.  The last length deserves its own, short code
67      since it gets used a lot in very redundant files.  The length
68      258 is special since 258 - 3 (the min match length) is 255.
69  13. The literal/length and distance code bit lengths are read as a
70      single stream of lengths.  It is possible (and advantageous) for
71      a repeat code (16, 17, or 18) to go across the boundary between
72      the two sets of lengths.
73 */
74
75
76void inflate_blocks_reset(
77    inflate_blocks_statef *s,
78    z_streamp z,
79    uLongf *c)
80{
81  if (c != Z_NULL)
82    *c = s->check;
83  if (s->mode == BTREE || s->mode == DTREE)
84    ZFREE(z, s->sub.trees.blens);
85  if (s->mode == CODES)
86    inflate_codes_free(s->sub.decode.codes, z);
87  s->mode = TYPE;
88  s->bitk = 0;
89  s->bitb = 0;
90  s->read = s->write = s->window;
91  if (s->checkfn != Z_NULL)
92    z->adler = s->check = (*s->checkfn)(0L, (const Bytef *)Z_NULL, 0);
93  Tracev((stderr, "inflate:   blocks reset\n"));
94}
95
96
97inflate_blocks_statef *inflate_blocks_new(
98    z_streamp z,
99    check_func c,
100    uInt w)
101{
102  inflate_blocks_statef *s;
103
104  if ((s = (inflate_blocks_statef *)ZALLOC
105       (z,1,sizeof(struct inflate_blocks_state))) == Z_NULL)
106    return s;
107  if ((s->hufts =
108       (inflate_huft *)ZALLOC(z, sizeof(inflate_huft), MANY)) == Z_NULL)
109  {
110    ZFREE(z, s);
111    return Z_NULL;
112  }
113  if ((s->window = (Bytef *)ZALLOC(z, 1, w)) == Z_NULL)
114  {
115    ZFREE(z, s->hufts);
116    ZFREE(z, s);
117    return Z_NULL;
118  }
119  s->end = s->window + w;
120  s->checkfn = c;
121  s->mode = TYPE;
122  Tracev((stderr, "inflate:   blocks allocated\n"));
123  inflate_blocks_reset(s, z, Z_NULL);
124  return s;
125}
126
127
128int inflate_blocks(
129    inflate_blocks_statef *s,
130    z_streamp z,
131    int r)
132{
133  uInt t;               /* temporary storage */
134  uLong b;              /* bit buffer */
135  uInt k;               /* bits in bit buffer */
136  Bytef *p;             /* input data pointer */
137  uInt n;               /* bytes available there */
138  Bytef *q;             /* output window write pointer */
139  uInt m;               /* bytes to end of window or read pointer */
140
141  /* copy input/output information to locals (UPDATE_ZLIB macro restores) */
142  LOAD
143
144  /* process input based on current state */
145  while (1) switch (s->mode)
146  {
147    case TYPE:
148      NEEDBITS(3)
149      t = (uInt)b & 7;
150      s->last = t & 1;
151      switch (t >> 1)
152      {
153        case 0:                         /* stored */
154          Tracev((stderr, "inflate:     stored block%s\n",
155                 s->last ? " (last)" : ""));
156          DUMPBITS(3)
157          t = k & 7;                    /* go to byte boundary */
158          DUMPBITS(t)
159          s->mode = LENS;               /* get length of stored block */
160          break;
161        case 1:                         /* fixed */
162          Tracev((stderr, "inflate:     fixed codes block%s\n",
163                 s->last ? " (last)" : ""));
164          {
165            uInt bl, bd;
166            inflate_huft *tl, *td;
167
168            inflate_trees_fixed(&bl, &bd, &tl, &td, z);
169            s->sub.decode.codes = inflate_codes_new(bl, bd, tl, td, z);
170            if (s->sub.decode.codes == Z_NULL)
171            {
172              r = Z_MEM_ERROR;
173              LEAVE
174            }
175          }
176          DUMPBITS(3)
177          s->mode = CODES;
178          break;
179        case 2:                         /* dynamic */
180          Tracev((stderr, "inflate:     dynamic codes block%s\n",
181                 s->last ? " (last)" : ""));
182          DUMPBITS(3)
183          s->mode = TABLE;
184          break;
185        case 3:                         /* illegal */
186          DUMPBITS(3)
187          s->mode = BAD;
188          z->msg = (char*)"invalid block type";
189          r = Z_DATA_ERROR;
190          LEAVE
191      }
192      break;
193    case LENS:
194      NEEDBITS(32)
195      if ((((~b) >> 16) & 0xffff) != (b & 0xffff))
196      {
197        s->mode = BAD;
198        z->msg = (char*)"invalid stored block lengths";
199        r = Z_DATA_ERROR;
200        LEAVE
201      }
202      s->sub.left = (uInt)b & 0xffff;
203      b = k = 0;                      /* dump bits */
204      Tracev((stderr, "inflate:       stored length %u\n", s->sub.left));
205      s->mode = s->sub.left ? STORED : (s->last ? DRY : TYPE);
206      break;
207    case STORED:
208      if (n == 0)
209        LEAVE
210      NEEDOUT
211      t = s->sub.left;
212      if (t > n) t = n;
213      if (t > m) t = m;
214      zmemcpy(q, p, t);
215      p += t;  n -= t;
216      q += t;  m -= t;
217      if ((s->sub.left -= t) != 0)
218        break;
219      Tracev((stderr, "inflate:       stored end, %lu total out\n",
220              z->total_out + (q >= s->read ? q - s->read :
221              (s->end - s->read) + (q - s->window))));
222      s->mode = s->last ? DRY : TYPE;
223      break;
224    case TABLE:
225      NEEDBITS(14)
226      s->sub.trees.table = t = (uInt)b & 0x3fff;
227#ifndef PKZIP_BUG_WORKAROUND
228      if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
229      {
230        s->mode = BAD;
231        z->msg = (char*)"too many length or distance symbols";
232        r = Z_DATA_ERROR;
233        LEAVE
234      }
235#endif
236      t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
237      if ((s->sub.trees.blens = (uIntf*)ZALLOC(z, t, sizeof(uInt))) == Z_NULL)
238      {
239        r = Z_MEM_ERROR;
240        LEAVE
241      }
242      DUMPBITS(14)
243      s->sub.trees.index = 0;
244      Tracev((stderr, "inflate:       table sizes ok\n"));
245      s->mode = BTREE;
246    case BTREE:
247      while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10))
248      {
249        NEEDBITS(3)
250        s->sub.trees.blens[border[s->sub.trees.index++]] = (uInt)b & 7;
251        DUMPBITS(3)
252      }
253      while (s->sub.trees.index < 19)
254        s->sub.trees.blens[border[s->sub.trees.index++]] = 0;
255      s->sub.trees.bb = 7;
256      t = inflate_trees_bits(s->sub.trees.blens, &s->sub.trees.bb,
257                             &s->sub.trees.tb, s->hufts, z);
258      if (t != Z_OK)
259      {
260        ZFREE(z, s->sub.trees.blens);
261        r = t;
262        if (r == Z_DATA_ERROR)
263          s->mode = BAD;
264        LEAVE
265      }
266      s->sub.trees.index = 0;
267      Tracev((stderr, "inflate:       bits tree ok\n"));
268      s->mode = DTREE;
269    case DTREE:
270      while (t = s->sub.trees.table,
271             s->sub.trees.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))
272      {
273        inflate_huft *h;
274        uInt i, j, c;
275
276        t = s->sub.trees.bb;
277        NEEDBITS(t)
278        h = s->sub.trees.tb + ((uInt)b & inflate_mask[t]);
279        t = h->bits;
280        c = h->base;
281        if (c < 16)
282        {
283          DUMPBITS(t)
284          s->sub.trees.blens[s->sub.trees.index++] = c;
285        }
286        else /* c == 16..18 */
287        {
288          i = c == 18 ? 7 : c - 14;
289          j = c == 18 ? 11 : 3;
290          NEEDBITS(t + i)
291          DUMPBITS(t)
292          j += (uInt)b & inflate_mask[i];
293          DUMPBITS(i)
294          i = s->sub.trees.index;
295          t = s->sub.trees.table;
296          if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
297              (c == 16 && i < 1))
298          {
299            ZFREE(z, s->sub.trees.blens);
300            s->mode = BAD;
301            z->msg = (char*)"invalid bit length repeat";
302            r = Z_DATA_ERROR;
303            LEAVE
304          }
305          c = c == 16 ? s->sub.trees.blens[i - 1] : 0;
306          do {
307            s->sub.trees.blens[i++] = c;
308          } while (--j);
309          s->sub.trees.index = i;
310        }
311      }
312      s->sub.trees.tb = Z_NULL;
313      {
314        uInt bl, bd;
315        inflate_huft *tl, *td;
316        inflate_codes_statef *c;
317
318        bl = 9;         /* must be <= 9 for lookahead assumptions */
319        bd = 6;         /* must be <= 9 for lookahead assumptions */
320        t = s->sub.trees.table;
321        t = inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f),
322                                  s->sub.trees.blens, &bl, &bd, &tl, &td,
323                                  s->hufts, z);
324        ZFREE(z, s->sub.trees.blens);
325        if (t != Z_OK)
326        {
327          if (t == (uInt)Z_DATA_ERROR)
328            s->mode = BAD;
329          r = t;
330          LEAVE
331        }
332        Tracev((stderr, "inflate:       trees ok\n"));
333        if ((c = inflate_codes_new(bl, bd, tl, td, z)) == Z_NULL)
334        {
335          r = Z_MEM_ERROR;
336          LEAVE
337        }
338        s->sub.decode.codes = c;
339      }
340      s->mode = CODES;
341    case CODES:
342      UPDATE_ZLIB
343      if ((r = inflate_codes(s, z, r)) != Z_STREAM_END)
344        return inflate_flush(s, z, r);
345      r = Z_OK;
346      inflate_codes_free(s->sub.decode.codes, z);
347      LOAD
348      Tracev((stderr, "inflate:       codes end, %lu total out\n",
349              z->total_out + (q >= s->read ? q - s->read :
350              (s->end - s->read) + (q - s->window))));
351      if (!s->last)
352      {
353        s->mode = TYPE;
354        break;
355      }
356      s->mode = DRY;
357    case DRY:
358      FLUSH
359      if (s->read != s->write)
360        LEAVE
361      s->mode = DONE;
362    case DONE:
363      r = Z_STREAM_END;
364      LEAVE
365    case BAD:
366      r = Z_DATA_ERROR;
367      LEAVE
368    default:
369      r = Z_STREAM_ERROR;
370      LEAVE
371  }
372}
373
374
375int inflate_blocks_free(
376    inflate_blocks_statef *s,
377    z_streamp z)
378{
379  inflate_blocks_reset(s, z, Z_NULL);
380  ZFREE(z, s->window);
381  ZFREE(z, s->hufts);
382  ZFREE(z, s);
383  Tracev((stderr, "inflate:   blocks freed\n"));
384  return Z_OK;
385}
386
387
388void inflate_set_dictionary(
389    inflate_blocks_statef *s,
390    const Bytef *d,
391    uInt  n)
392{
393  zmemcpy(s->window, d, n);
394  s->read = s->write = s->window + n;
395}
396
397
398/* Returns true if inflate is currently at the end of a block generated
399 * by Z_SYNC_FLUSH or Z_FULL_FLUSH.
400 * IN assertion: s != Z_NULL
401 */
402int inflate_blocks_sync_point(
403    inflate_blocks_statef *s)
404{
405  return s->mode == LENS;
406}
Note: See TracBrowser for help on using the repository browser.