source: svn/newcon3bcm2_21bu/dst/dlib/src/ZLIB/inflate.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: 10.0 KB
Line 
1/*
2 * $Id: //suprahd/releases/suprahd_163/suprahd_ztvapp640_163/drivers/graphics/ZLIB/inflate.c#1 $
3 * $Revision: #1 $
4 * $DateTime: 2006/02/24 17:51:46 $
5 * $Change: 42566 $
6 * $Author: pryush.sharma $
7 */
8
9/* inflate.c -- zlib interface to inflate modules
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
17struct inflate_blocks_state {int dummy;}; /* for buggy compilers */
18
19typedef enum {
20      METHOD,   /* waiting for method byte */
21      FLAG,     /* waiting for flag byte */
22      DICT4,    /* four dictionary check bytes to go */
23      DICT3,    /* three dictionary check bytes to go */
24      DICT2,    /* two dictionary check bytes to go */
25      DICT1,    /* one dictionary check byte to go */
26      DICT0,    /* waiting for inflateSetDictionary */
27      BLOCKS,   /* decompressing blocks */
28      CHECK4,   /* four check bytes to go */
29      CHECK3,   /* three check bytes to go */
30      CHECK2,   /* two check bytes to go */
31      CHECK1,   /* one check byte to go */
32      DONE,     /* finished check, done */
33      BAD}      /* got an error--stay here */
34inflate_mode;
35
36/* inflate private state */
37struct internal_state {
38
39  /* mode */
40  inflate_mode  mode;   /* current inflate mode */
41
42  /* mode dependent information */
43  union {
44    uInt method;        /* if FLAGS, method byte */
45    struct {
46      uLong was;                /* computed check value */
47      uLong need;               /* stream check value */
48    } check;            /* if CHECK, check values to compare */
49    uInt marker;        /* if BAD, inflateSync's marker bytes count */
50  } sub;        /* submode */
51
52  /* mode independent information */
53  int  nowrap;          /* flag for no wrapper */
54  uInt wbits;           /* log2(window size)  (8..15, defaults to 15) */
55  inflate_blocks_statef
56    *blocks;            /* current inflate_blocks state */
57
58};
59
60
61int ZEXPORT inflateReset(
62    z_streamp z)
63{
64  if (z == Z_NULL || z->state == Z_NULL)
65    return Z_STREAM_ERROR;
66  z->total_in = z->total_out = 0;
67  z->msg = Z_NULL;
68  z->state->mode = z->state->nowrap ? BLOCKS : METHOD;
69  inflate_blocks_reset(z->state->blocks, z, Z_NULL);
70  Tracev((stderr, "inflate: reset\n"));
71  return Z_OK;
72}
73
74
75int ZEXPORT inflateEnd(
76    z_streamp z)
77{
78  if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL)
79    return Z_STREAM_ERROR;
80  if (z->state->blocks != Z_NULL)
81    inflate_blocks_free(z->state->blocks, z);
82  ZFREE(z, z->state);
83  z->state = Z_NULL;
84  Tracev((stderr, "inflate: end\n"));
85  return Z_OK;
86}
87
88
89int ZEXPORT inflateInit2_(
90    z_streamp z,
91    int w,
92    const char *version,
93    int stream_size)
94{
95  if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
96      stream_size != sizeof(z_stream))
97      return Z_VERSION_ERROR;
98
99  /* initialize state */
100  if (z == Z_NULL)
101    return Z_STREAM_ERROR;
102  z->msg = Z_NULL;
103  if (z->zalloc == Z_NULL)
104  {
105    z->zalloc = zcalloc;
106    z->opaque = (voidpf)0;
107  }
108  if (z->zfree == Z_NULL) z->zfree = zcfree;
109  if ((z->state = (struct internal_state FAR *)
110       ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL)
111    return Z_MEM_ERROR;
112  z->state->blocks = Z_NULL;
113
114  /* handle undocumented nowrap option (no zlib header or check) */
115  z->state->nowrap = 0;
116  if (w < 0)
117  {
118    w = - w;
119    z->state->nowrap = 1;
120  }
121
122  /* set window size */
123  if (w < 8 || w > 15)
124  {
125    inflateEnd(z);
126    return Z_STREAM_ERROR;
127  }
128  z->state->wbits = (uInt)w;
129
130  /* create inflate_blocks state */
131  if ((z->state->blocks =
132      inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, (uInt)1 << w))
133      == Z_NULL)
134  {
135    inflateEnd(z);
136    return Z_MEM_ERROR;
137  }
138  Tracev((stderr, "inflate: allocated\n"));
139
140  /* reset state */
141  inflateReset(z);
142  return Z_OK;
143}
144
145
146int ZEXPORT inflateInit_(
147    z_streamp z,
148    const char *version,
149    int stream_size)
150{
151  return inflateInit2_(z, DEF_WBITS, version, stream_size);
152}
153
154
155#define NEEDBYTE {if(z->avail_in==0)return r;r=f;}
156#define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
157
158int ZEXPORT inflate(
159    z_streamp z,
160    int f)
161{
162  int r;
163  uInt b;
164
165  if (z == Z_NULL || z->state == Z_NULL || z->next_in == Z_NULL)
166    return Z_STREAM_ERROR;
167  f = f == Z_FINISH ? Z_BUF_ERROR : Z_OK;
168  r = Z_BUF_ERROR;
169  while (1) switch (z->state->mode)
170  {
171    case METHOD:
172      NEEDBYTE
173      if (((z->state->sub.method = NEXTBYTE) & 0xf) != Z_DEFLATED)
174      {
175        z->state->mode = BAD;
176        z->msg = (char*)"unknown compression method";
177        z->state->sub.marker = 5;       /* can't try inflateSync */
178        break;
179      }
180      if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
181      {
182        z->state->mode = BAD;
183        z->msg = (char*)"invalid window size";
184        z->state->sub.marker = 5;       /* can't try inflateSync */
185        break;
186      }
187      z->state->mode = FLAG;
188    case FLAG:
189      NEEDBYTE
190      b = NEXTBYTE;
191      if (((z->state->sub.method << 8) + b) % 31)
192      {
193        z->state->mode = BAD;
194        z->msg = (char*)"incorrect header check";
195        z->state->sub.marker = 5;       /* can't try inflateSync */
196        break;
197      }
198      Tracev((stderr, "inflate: zlib header ok\n"));
199      if (!(b & PRESET_DICT))
200      {
201        z->state->mode = BLOCKS;
202        break;
203      }
204      z->state->mode = DICT4;
205    case DICT4:
206      NEEDBYTE
207      z->state->sub.check.need = (uLong)NEXTBYTE << 24;
208      z->state->mode = DICT3;
209    case DICT3:
210      NEEDBYTE
211      z->state->sub.check.need += (uLong)NEXTBYTE << 16;
212      z->state->mode = DICT2;
213    case DICT2:
214      NEEDBYTE
215      z->state->sub.check.need += (uLong)NEXTBYTE << 8;
216      z->state->mode = DICT1;
217    case DICT1:
218      NEEDBYTE
219      z->state->sub.check.need += (uLong)NEXTBYTE;
220      z->adler = z->state->sub.check.need;
221      z->state->mode = DICT0;
222      return Z_NEED_DICT;
223    case DICT0:
224      z->state->mode = BAD;
225      z->msg = (char*)"need dictionary";
226      z->state->sub.marker = 0;       /* can try inflateSync */
227      return Z_STREAM_ERROR;
228    case BLOCKS:
229      r = inflate_blocks(z->state->blocks, z, r);
230      if (r == Z_DATA_ERROR)
231      {
232        z->state->mode = BAD;
233        z->state->sub.marker = 0;       /* can try inflateSync */
234        break;
235      }
236      if (r == Z_OK)
237        r = f;
238      if (r != Z_STREAM_END)
239        return r;
240      r = f;
241      inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was);
242      if (z->state->nowrap)
243      {
244        z->state->mode = DONE;
245        break;
246      }
247      z->state->mode = CHECK4;
248    case CHECK4:
249      NEEDBYTE
250      z->state->sub.check.need = (uLong)NEXTBYTE << 24;
251      z->state->mode = CHECK3;
252    case CHECK3:
253      NEEDBYTE
254      z->state->sub.check.need += (uLong)NEXTBYTE << 16;
255      z->state->mode = CHECK2;
256    case CHECK2:
257      NEEDBYTE
258      z->state->sub.check.need += (uLong)NEXTBYTE << 8;
259      z->state->mode = CHECK1;
260    case CHECK1:
261      NEEDBYTE
262      z->state->sub.check.need += (uLong)NEXTBYTE;
263
264      if (z->state->sub.check.was != z->state->sub.check.need)
265      {
266        z->state->mode = BAD;
267        z->msg = (char*)"incorrect data check";
268        z->state->sub.marker = 5;       /* can't try inflateSync */
269        break;
270      }
271      Tracev((stderr, "inflate: zlib check ok\n"));
272      z->state->mode = DONE;
273    case DONE:
274      return Z_STREAM_END;
275    case BAD:
276      return Z_DATA_ERROR;
277    default:
278      return Z_STREAM_ERROR;
279  }
280#ifdef NEED_DUMMY_RETURN
281  return Z_STREAM_ERROR;  /* Some dumb compilers complain without this */
282#endif
283}
284
285
286int ZEXPORT inflateSetDictionary(
287    z_streamp z,
288    const Bytef *dictionary,
289    uInt  dictLength)
290{
291  uInt length = dictLength;
292
293  if (z == Z_NULL || z->state == Z_NULL || z->state->mode != DICT0)
294    return Z_STREAM_ERROR;
295
296  if (adler32(1L, dictionary, dictLength) != z->adler) return Z_DATA_ERROR;
297  z->adler = 1L;
298
299  if (length >= ((uInt)1<<z->state->wbits))
300  {
301    length = (1<<z->state->wbits)-1;
302    dictionary += dictLength - length;
303  }
304  inflate_set_dictionary(z->state->blocks, dictionary, length);
305  z->state->mode = BLOCKS;
306  return Z_OK;
307}
308
309
310int ZEXPORT inflateSync(
311    z_streamp z)
312{
313  uInt n;       /* number of bytes to look at */
314  Bytef *p;     /* pointer to bytes */
315  uInt m;       /* number of marker bytes found in a row */
316  uLong r, w;   /* temporaries to save total_in and total_out */
317
318  /* set up */
319  if (z == Z_NULL || z->state == Z_NULL)
320    return Z_STREAM_ERROR;
321  if (z->state->mode != BAD)
322  {
323    z->state->mode = BAD;
324    z->state->sub.marker = 0;
325  }
326  if ((n = z->avail_in) == 0)
327    return Z_BUF_ERROR;
328  p = z->next_in;
329  m = z->state->sub.marker;
330
331  /* search */
332  while (n && m < 4)
333  {
334    static const Byte mark[4] = {0, 0, 0xff, 0xff};
335    if (*p == mark[m])
336      m++;
337    else if (*p)
338      m = 0;
339    else
340      m = 4 - m;
341    p++, n--;
342  }
343
344  /* restore */
345  z->total_in += p - z->next_in;
346  z->next_in = p;
347  z->avail_in = n;
348  z->state->sub.marker = m;
349
350  /* return no joy or set up to restart on a new block */
351  if (m != 4)
352    return Z_DATA_ERROR;
353  r = z->total_in;  w = z->total_out;
354  inflateReset(z);
355  z->total_in = r;  z->total_out = w;
356  z->state->mode = BLOCKS;
357  return Z_OK;
358}
359
360
361/* Returns true if inflate is currently at the end of a block generated
362 * by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
363 * implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH
364 * but removes the length bytes of the resulting empty stored block. When
365 * decompressing, PPP checks that at the end of input packet, inflate is
366 * waiting for these length bytes.
367 */
368int ZEXPORT inflateSyncPoint(
369    z_streamp z)
370{
371  if (z == Z_NULL || z->state == Z_NULL || z->state->blocks == Z_NULL)
372    return Z_STREAM_ERROR;
373  return inflate_blocks_sync_point(z->state->blocks);
374}
Note: See TracBrowser for help on using the repository browser.