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
7 __FBSDID("$FreeBSD: src/lib/libz/inflate.c,v 1.1.1.2.6.1 2003/02/01 13:33:12 sobomax Exp $");
12 struct inflate_blocks_state {int dummy;}; /* for buggy compilers */
15 METHOD, /* waiting for method byte */
16 FLAG, /* waiting for flag byte */
17 DICT4, /* four dictionary check bytes to go */
18 DICT3, /* three dictionary check bytes to go */
19 DICT2, /* two dictionary check bytes to go */
20 DICT1, /* one dictionary check byte to go */
21 DICT0, /* waiting for inflateSetDictionary */
22 BLOCKS, /* decompressing blocks */
23 CHECK4, /* four check bytes to go */
24 CHECK3, /* three check bytes to go */
25 CHECK2, /* two check bytes to go */
26 CHECK1, /* one check byte to go */
27 DONE, /* finished check, done */
28 BAD} /* got an error--stay here */
31 /* inflate private state */
32 struct internal_state {
35 inflate_mode mode; /* current inflate mode */
37 /* mode dependent information */
39 uInt method; /* if FLAGS, method byte */
41 uLong was; /* computed check value */
42 uLong need; /* stream check value */
43 } check; /* if CHECK, check values to compare */
44 uInt marker; /* if BAD, inflateSync's marker bytes count */
47 /* mode independent information */
48 int nowrap; /* flag for no wrapper */
49 uInt wbits; /* log2(window size) (8..15, defaults to 15) */
51 *blocks; /* current inflate_blocks state */
56 int ZEXPORT inflateReset(z)
59 if (z == Z_NULL || z->state == Z_NULL)
60 return Z_STREAM_ERROR;
61 z->total_in = z->total_out = 0;
63 z->state->mode = z->state->nowrap ? BLOCKS : METHOD;
64 inflate_blocks_reset(z->state->blocks, z, Z_NULL);
65 Tracev((stderr, "inflate: reset\n"));
70 int ZEXPORT inflateEnd(z)
73 if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL)
74 return Z_STREAM_ERROR;
75 if (z->state->blocks != Z_NULL)
76 inflate_blocks_free(z->state->blocks, z);
79 Tracev((stderr, "inflate: end\n"));
84 int ZEXPORT inflateInit2_(z, w, version, stream_size)
90 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
91 stream_size != sizeof(z_stream))
92 return Z_VERSION_ERROR;
94 /* initialize state */
96 return Z_STREAM_ERROR;
98 if (z->zalloc == Z_NULL)
101 z->opaque = (voidpf)0;
103 if (z->zfree == Z_NULL) z->zfree = zcfree;
104 if ((z->state = (struct internal_state FAR *)
105 ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL)
107 z->state->blocks = Z_NULL;
109 /* handle undocumented nowrap option (no zlib header or check) */
110 z->state->nowrap = 0;
114 z->state->nowrap = 1;
117 /* set window size */
121 return Z_STREAM_ERROR;
123 z->state->wbits = (uInt)w;
125 /* create inflate_blocks state */
126 if ((z->state->blocks =
127 inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, (uInt)1 << w))
133 Tracev((stderr, "inflate: allocated\n"));
141 int ZEXPORT inflateInit_(z, version, stream_size)
146 return inflateInit2_(z, DEF_WBITS, version, stream_size);
150 #define NEEDBYTE {if(z->avail_in==0)return r;r=f;}
151 #define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
153 int ZEXPORT inflate(z, f)
160 if (z == Z_NULL || z->state == Z_NULL || z->next_in == Z_NULL)
161 return Z_STREAM_ERROR;
162 f = f == Z_FINISH ? Z_BUF_ERROR : Z_OK;
164 while (1) switch (z->state->mode)
168 if (((z->state->sub.method = NEXTBYTE) & 0xf) != Z_DEFLATED)
170 z->state->mode = BAD;
171 z->msg = (char*)"unknown compression method";
172 z->state->sub.marker = 5; /* can't try inflateSync */
175 if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
177 z->state->mode = BAD;
178 z->msg = (char*)"invalid window size";
179 z->state->sub.marker = 5; /* can't try inflateSync */
182 z->state->mode = FLAG;
186 if (((z->state->sub.method << 8) + b) % 31)
188 z->state->mode = BAD;
189 z->msg = (char*)"incorrect header check";
190 z->state->sub.marker = 5; /* can't try inflateSync */
193 Tracev((stderr, "inflate: zlib header ok\n"));
194 if (!(b & PRESET_DICT))
196 z->state->mode = BLOCKS;
199 z->state->mode = DICT4;
202 z->state->sub.check.need = (uLong)NEXTBYTE << 24;
203 z->state->mode = DICT3;
206 z->state->sub.check.need += (uLong)NEXTBYTE << 16;
207 z->state->mode = DICT2;
210 z->state->sub.check.need += (uLong)NEXTBYTE << 8;
211 z->state->mode = DICT1;
214 z->state->sub.check.need += (uLong)NEXTBYTE;
215 z->adler = z->state->sub.check.need;
216 z->state->mode = DICT0;
219 z->state->mode = BAD;
220 z->msg = (char*)"need dictionary";
221 z->state->sub.marker = 0; /* can try inflateSync */
222 return Z_STREAM_ERROR;
224 r = inflate_blocks(z->state->blocks, z, r);
225 if (r == Z_DATA_ERROR)
227 z->state->mode = BAD;
228 z->state->sub.marker = 0; /* can try inflateSync */
233 if (r != Z_STREAM_END)
236 inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was);
237 if (z->state->nowrap)
239 z->state->mode = DONE;
242 z->state->mode = CHECK4;
245 z->state->sub.check.need = (uLong)NEXTBYTE << 24;
246 z->state->mode = CHECK3;
249 z->state->sub.check.need += (uLong)NEXTBYTE << 16;
250 z->state->mode = CHECK2;
253 z->state->sub.check.need += (uLong)NEXTBYTE << 8;
254 z->state->mode = CHECK1;
257 z->state->sub.check.need += (uLong)NEXTBYTE;
259 if (z->state->sub.check.was != z->state->sub.check.need)
261 z->state->mode = BAD;
262 z->msg = (char*)"incorrect data check";
263 z->state->sub.marker = 5; /* can't try inflateSync */
266 Tracev((stderr, "inflate: zlib check ok\n"));
267 z->state->mode = DONE;
273 return Z_STREAM_ERROR;
275 #ifdef NEED_DUMMY_RETURN
276 return Z_STREAM_ERROR; /* Some dumb compilers complain without this */
281 int ZEXPORT inflateSetDictionary(z, dictionary, dictLength)
283 const Bytef *dictionary;
286 uInt length = dictLength;
288 if (z == Z_NULL || z->state == Z_NULL || z->state->mode != DICT0)
289 return Z_STREAM_ERROR;
291 if (adler32(1L, dictionary, dictLength) != z->adler) return Z_DATA_ERROR;
294 if (length >= ((uInt)1<<z->state->wbits))
296 length = (1<<z->state->wbits)-1;
297 dictionary += dictLength - length;
299 inflate_set_dictionary(z->state->blocks, dictionary, length);
300 z->state->mode = BLOCKS;
305 int ZEXPORT inflateSync(z)
308 uInt n; /* number of bytes to look at */
309 Bytef *p; /* pointer to bytes */
310 uInt m; /* number of marker bytes found in a row */
311 uLong r, w; /* temporaries to save total_in and total_out */
314 if (z == Z_NULL || z->state == Z_NULL)
315 return Z_STREAM_ERROR;
316 if (z->state->mode != BAD)
318 z->state->mode = BAD;
319 z->state->sub.marker = 0;
321 if ((n = z->avail_in) == 0)
324 m = z->state->sub.marker;
329 static const Byte mark[4] = {0, 0, 0xff, 0xff};
340 z->total_in += p - z->next_in;
343 z->state->sub.marker = m;
345 /* return no joy or set up to restart on a new block */
348 r = z->total_in; w = z->total_out;
350 z->total_in = r; z->total_out = w;
351 z->state->mode = BLOCKS;
356 /* Returns true if inflate is currently at the end of a block generated
357 * by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
358 * implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH
359 * but removes the length bytes of the resulting empty stored block. When
360 * decompressing, PPP checks that at the end of input packet, inflate is
361 * waiting for these length bytes.
363 int ZEXPORT inflateSyncPoint(z)
366 if (z == Z_NULL || z->state == Z_NULL || z->state->blocks == Z_NULL)
367 return Z_STREAM_ERROR;
368 return inflate_blocks_sync_point(z->state->blocks);