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
9 struct inflate_blocks_state {int dummy;}; /* for buggy compilers */
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 */
28 /* inflate private state */
29 struct internal_state {
32 inflate_mode mode; /* current inflate mode */
34 /* mode dependent information */
36 uInt method; /* if FLAGS, method byte */
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 */
44 /* mode independent information */
45 int nowrap; /* flag for no wrapper */
46 uInt wbits; /* log2(window size) (8..15, defaults to 15) */
48 *blocks; /* current inflate_blocks state */
53 int EXPORT inflateReset(z)
56 if (z == Z_NULL || z->state == Z_NULL)
57 return Z_STREAM_ERROR;
58 z->total_in = z->total_out = 0;
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"));
67 int EXPORT inflateEnd(z)
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);
76 Tracev((stderr, "inflate: end\n"));
81 int EXPORT inflateInit2_(z, w, version, stream_size)
87 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
88 stream_size != sizeof(z_stream))
89 return Z_VERSION_ERROR;
91 /* initialize state */
93 return Z_STREAM_ERROR;
95 if (z->zalloc == Z_NULL)
98 z->opaque = (voidpf)0;
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)
104 z->state->blocks = Z_NULL;
106 /* handle undocumented nowrap option (no zlib header or check) */
107 z->state->nowrap = 0;
111 z->state->nowrap = 1;
114 /* set window size */
118 return Z_STREAM_ERROR;
120 z->state->wbits = (uInt)w;
122 /* create inflate_blocks state */
123 if ((z->state->blocks =
124 inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, (uInt)1 << w))
130 Tracev((stderr, "inflate: allocated\n"));
138 int EXPORT inflateInit_(z, version, stream_size)
143 return inflateInit2_(z, DEF_WBITS, version, stream_size);
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++)
150 int EXPORT inflate(z, f)
157 if (z == Z_NULL || z->state == Z_NULL || z->next_in == Z_NULL || f < 0)
158 return Z_STREAM_ERROR;
160 while (1) switch (z->state->mode)
164 if (((z->state->sub.method = NEXTBYTE) & 0xf) != Z_DEFLATED)
166 z->state->mode = BAD;
167 z->msg = (char*)"unknown compression method";
168 z->state->sub.marker = 5; /* can't try inflateSync */
171 if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
173 z->state->mode = BAD;
174 z->msg = (char*)"invalid window size";
175 z->state->sub.marker = 5; /* can't try inflateSync */
178 z->state->mode = FLAG;
182 if (((z->state->sub.method << 8) + b) % 31)
184 z->state->mode = BAD;
185 z->msg = (char*)"incorrect header check";
186 z->state->sub.marker = 5; /* can't try inflateSync */
189 Tracev((stderr, "inflate: zlib header ok\n"));
190 if (!(b & PRESET_DICT))
192 z->state->mode = BLOCKS;
195 z->state->mode = DICT4;
198 z->state->sub.check.need = (uLong)NEXTBYTE << 24;
199 z->state->mode = DICT3;
202 z->state->sub.check.need += (uLong)NEXTBYTE << 16;
203 z->state->mode = DICT2;
206 z->state->sub.check.need += (uLong)NEXTBYTE << 8;
207 z->state->mode = DICT1;
210 z->state->sub.check.need += (uLong)NEXTBYTE;
211 z->adler = z->state->sub.check.need;
212 z->state->mode = 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;
220 r = inflate_blocks(z->state->blocks, z, r);
221 if (r == Z_DATA_ERROR)
223 z->state->mode = BAD;
224 z->state->sub.marker = 0; /* can try inflateSync */
227 if (r != Z_STREAM_END)
228 return f == Z_FINISH && r == Z_OK ? Z_BUF_ERROR : r;
230 inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was);
231 if (z->state->nowrap)
233 z->state->mode = DONE;
236 z->state->mode = CHECK4;
237 if (f == Z_FINISH) r = Z_BUF_ERROR;
240 z->state->sub.check.need = (uLong)NEXTBYTE << 24;
241 z->state->mode = CHECK3;
244 z->state->sub.check.need += (uLong)NEXTBYTE << 16;
245 z->state->mode = CHECK2;
248 z->state->sub.check.need += (uLong)NEXTBYTE << 8;
249 z->state->mode = CHECK1;
252 z->state->sub.check.need += (uLong)NEXTBYTE;
254 if (z->state->sub.check.was != z->state->sub.check.need)
256 z->state->mode = BAD;
257 z->msg = (char*)"incorrect data check";
258 z->state->sub.marker = 5; /* can't try inflateSync */
261 Tracev((stderr, "inflate: zlib check ok\n"));
262 z->state->mode = DONE;
268 return Z_STREAM_ERROR;
270 #ifdef NEED_DUMMY_RETURN
271 return Z_STREAM_ERROR; /* Some dumb compilers complain without this */
276 int EXPORT inflateSetDictionary(z, dictionary, dictLength)
278 const Bytef *dictionary;
281 uInt length = dictLength;
283 if (z == Z_NULL || z->state == Z_NULL || z->state->mode != DICT0)
284 return Z_STREAM_ERROR;
286 if (adler32(1L, dictionary, dictLength) != z->adler) return Z_DATA_ERROR;
289 if (length >= ((uInt)1<<z->state->wbits))
291 length = (1<<z->state->wbits)-1;
292 dictionary += dictLength - length;
294 inflate_set_dictionary(z->state->blocks, dictionary, length);
295 z->state->mode = BLOCKS;
300 int EXPORT inflateSync(z)
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 */
309 if (z == Z_NULL || z->state == Z_NULL)
310 return Z_STREAM_ERROR;
311 if (z->state->mode != BAD)
313 z->state->mode = BAD;
314 z->state->sub.marker = 0;
316 if ((n = z->avail_in) == 0)
319 m = z->state->sub.marker;
324 static const Byte mark[4] = {0, 0, 0xff, 0xff};
335 z->total_in += p - z->next_in;
338 z->state->sub.marker = m;
340 /* return no joy or set up to restart on a new block */
343 r = z->total_in; w = z->total_out;
345 z->total_in = r; z->total_out = w;
346 z->state->mode = BLOCKS;
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.
358 int EXPORT inflateSyncPoint(z)
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);