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