1 /* inflate.c -- zlib interface to inflate modules
2 * Copyright (C) 1995-2002 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
9 #ifdef BAD /* For AIX */
13 struct inflate_blocks_state {int dummy;}; /* for buggy compilers */
16 METHOD, /* waiting for method byte */
17 FLAG, /* waiting for flag byte */
18 DICT4, /* four dictionary check bytes to go */
19 DICT3, /* three dictionary check bytes to go */
20 DICT2, /* two dictionary check bytes to go */
21 DICT1, /* one dictionary check byte to go */
22 DICT0, /* waiting for inflateSetDictionary */
23 BLOCKS, /* decompressing blocks */
24 CHECK4, /* four check bytes to go */
25 CHECK3, /* three check bytes to go */
26 CHECK2, /* two check bytes to go */
27 CHECK1, /* one check byte to go */
28 DONE, /* finished check, done */
29 BAD} /* got an error--stay here */
32 /* inflate private state */
33 struct internal_state {
36 inflate_mode mode; /* current inflate mode */
38 /* mode dependent information */
40 uInt method; /* if FLAGS, method byte */
42 uLong was; /* computed check value */
43 uLong need; /* stream check value */
44 } check; /* if CHECK, check values to compare */
45 uInt marker; /* if BAD, inflateSync's marker bytes count */
48 /* mode independent information */
49 int nowrap; /* flag for no wrapper */
50 uInt wbits; /* log2(window size) (8..15, defaults to 15) */
52 *blocks; /* current inflate_blocks state */
57 int ZEXPORT inflateReset(z)
60 if (z == Z_NULL || z->state == Z_NULL)
61 return Z_STREAM_ERROR;
62 z->total_in = z->total_out = 0;
64 z->state->mode = z->state->nowrap ? BLOCKS : METHOD;
65 inflate_blocks_reset(z->state->blocks, z, Z_NULL);
66 Tracev((stderr, "inflate: reset\n"));
71 int ZEXPORT inflateEnd(z)
74 if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL)
75 return Z_STREAM_ERROR;
76 if (z->state->blocks != Z_NULL)
77 inflate_blocks_free(z->state->blocks, z);
80 Tracev((stderr, "inflate: end\n"));
85 int ZEXPORT inflateInit2_(z, w, version, stream_size)
91 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
92 stream_size != sizeof(z_stream))
93 return Z_VERSION_ERROR;
95 /* initialize state */
97 return Z_STREAM_ERROR;
99 if (z->zalloc == Z_NULL)
102 z->opaque = (voidpf)0;
104 if (z->zfree == Z_NULL) z->zfree = zcfree;
105 if ((z->state = (struct internal_state FAR *)
106 ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL)
108 z->state->blocks = Z_NULL;
110 /* handle undocumented nowrap option (no zlib header or check) */
111 z->state->nowrap = 0;
115 z->state->nowrap = 1;
118 /* set window size */
122 return Z_STREAM_ERROR;
124 z->state->wbits = (uInt)w;
126 /* create inflate_blocks state */
127 if ((z->state->blocks =
128 inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, (uInt)1 << w))
134 Tracev((stderr, "inflate: allocated\n"));
142 int ZEXPORT inflateInit_(z, version, stream_size)
147 return inflateInit2_(z, DEF_WBITS, version, stream_size);
151 #define NEEDBYTE {if(z->avail_in==0)return r;r=f;}
152 #define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
154 int ZEXPORT inflate(z, f)
161 if (z == Z_NULL || z->state == Z_NULL || z->next_in == Z_NULL)
162 return Z_STREAM_ERROR;
163 f = f == Z_FINISH ? Z_BUF_ERROR : Z_OK;
165 while (1) switch (z->state->mode)
169 if (((z->state->sub.method = NEXTBYTE) & 0xf) != Z_DEFLATED)
171 z->state->mode = BAD;
172 z->msg = (char*)"unknown compression method";
173 z->state->sub.marker = 5; /* can't try inflateSync */
176 if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
178 z->state->mode = BAD;
179 z->msg = (char*)"invalid window size";
180 z->state->sub.marker = 5; /* can't try inflateSync */
183 z->state->mode = FLAG;
187 if (((z->state->sub.method << 8) + b) % 31)
189 z->state->mode = BAD;
190 z->msg = (char*)"incorrect header check";
191 z->state->sub.marker = 5; /* can't try inflateSync */
194 Tracev((stderr, "inflate: zlib header ok\n"));
195 if (!(b & PRESET_DICT))
197 z->state->mode = BLOCKS;
200 z->state->mode = DICT4;
203 z->state->sub.check.need = (uLong)NEXTBYTE << 24;
204 z->state->mode = DICT3;
207 z->state->sub.check.need += (uLong)NEXTBYTE << 16;
208 z->state->mode = DICT2;
211 z->state->sub.check.need += (uLong)NEXTBYTE << 8;
212 z->state->mode = DICT1;
215 z->state->sub.check.need += (uLong)NEXTBYTE;
216 z->adler = z->state->sub.check.need;
217 z->state->mode = DICT0;
220 z->state->mode = BAD;
221 z->msg = (char*)"need dictionary";
222 z->state->sub.marker = 0; /* can try inflateSync */
223 return Z_STREAM_ERROR;
225 r = inflate_blocks(z->state->blocks, z, r);
226 if (r == Z_DATA_ERROR)
228 z->state->mode = BAD;
229 z->state->sub.marker = 0; /* can try inflateSync */
234 if (r != Z_STREAM_END)
237 inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was);
238 if (z->state->nowrap)
240 z->state->mode = DONE;
243 z->state->mode = CHECK4;
246 z->state->sub.check.need = (uLong)NEXTBYTE << 24;
247 z->state->mode = CHECK3;
250 z->state->sub.check.need += (uLong)NEXTBYTE << 16;
251 z->state->mode = CHECK2;
254 z->state->sub.check.need += (uLong)NEXTBYTE << 8;
255 z->state->mode = CHECK1;
258 z->state->sub.check.need += (uLong)NEXTBYTE;
260 if (z->state->sub.check.was != z->state->sub.check.need)
262 z->state->mode = BAD;
263 z->msg = (char*)"incorrect data check";
264 z->state->sub.marker = 5; /* can't try inflateSync */
267 Tracev((stderr, "inflate: zlib check ok\n"));
268 z->state->mode = DONE;
274 return Z_STREAM_ERROR;
276 #ifdef NEED_DUMMY_RETURN
277 return Z_STREAM_ERROR; /* Some dumb compilers complain without this */
282 int ZEXPORT inflateSetDictionary(z, dictionary, dictLength)
284 const Bytef *dictionary;
287 uInt length = dictLength;
289 if (z == Z_NULL || z->state == Z_NULL || z->state->mode != DICT0)
290 return Z_STREAM_ERROR;
292 if (adler32(1L, dictionary, dictLength) != z->adler) return Z_DATA_ERROR;
295 if (length >= ((uInt)1<<z->state->wbits))
297 length = (1<<z->state->wbits)-1;
298 dictionary += dictLength - length;
300 inflate_set_dictionary(z->state->blocks, dictionary, length);
301 z->state->mode = BLOCKS;
306 int ZEXPORT inflateSync(z)
309 uInt n; /* number of bytes to look at */
310 Bytef *p; /* pointer to bytes */
311 uInt m; /* number of marker bytes found in a row */
312 uLong r, w; /* temporaries to save total_in and total_out */
315 if (z == Z_NULL || z->state == Z_NULL)
316 return Z_STREAM_ERROR;
317 if (z->state->mode != BAD)
319 z->state->mode = BAD;
320 z->state->sub.marker = 0;
322 if ((n = z->avail_in) == 0)
325 m = z->state->sub.marker;
330 static const Byte mark[4] = {0, 0, 0xff, 0xff};
341 z->total_in += p - z->next_in;
344 z->state->sub.marker = m;
346 /* return no joy or set up to restart on a new block */
349 r = z->total_in; w = z->total_out;
351 z->total_in = r; z->total_out = w;
352 z->state->mode = BLOCKS;
357 /* Returns true if inflate is currently at the end of a block generated
358 * by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
359 * implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH
360 * but removes the length bytes of the resulting empty stored block. When
361 * decompressing, PPP checks that at the end of input packet, inflate is
362 * waiting for these length bytes.
364 int ZEXPORT inflateSyncPoint(z)
367 if (z == Z_NULL || z->state == Z_NULL || z->state->blocks == Z_NULL)
368 return Z_STREAM_ERROR;
369 return inflate_blocks_sync_point(z->state->blocks);