1 /* inflate.c -- zlib interface to inflate modules
2 * Copyright (C) 1995 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 */
11 /* inflate private state */
12 struct internal_state {
16 METHOD, /* waiting for method byte */
17 FLAG, /* waiting for flag byte */
18 BLOCKS, /* decompressing blocks */
19 CHECK4, /* four check bytes to go */
20 CHECK3, /* three check bytes to go */
21 CHECK2, /* two check bytes to go */
22 CHECK1, /* one check byte to go */
23 DONE, /* finished check, done */
24 BAD} /* got an error--stay here */
25 mode; /* current inflate mode */
27 /* mode dependent information */
29 uInt method; /* if FLAGS, method byte */
31 uLong was; /* computed check value */
32 uLong need; /* stream check value */
33 } check; /* if CHECK, check values to compare */
34 uInt marker; /* if BAD, inflateSync's marker bytes count */
37 /* mode independent information */
38 int nowrap; /* flag for no wrapper */
39 uInt wbits; /* log2(window size) (8..15, defaults to 15) */
41 *blocks; /* current inflate_blocks state */
51 if (z == Z_NULL || z->state == Z_NULL)
52 return Z_STREAM_ERROR;
53 z->total_in = z->total_out = 0;
55 z->state->mode = z->state->nowrap ? BLOCKS : METHOD;
56 inflate_blocks_reset(z->state->blocks, z, &c);
57 Trace((stderr, "inflate: reset\n"));
67 if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL)
68 return Z_STREAM_ERROR;
69 if (z->state->blocks != Z_NULL)
70 inflate_blocks_free(z->state->blocks, z, &c);
73 Trace((stderr, "inflate: end\n"));
78 int inflateInit2(z, w)
82 /* initialize state */
84 return Z_STREAM_ERROR;
85 if (z->zalloc == Z_NULL) z->zalloc = zcalloc;
86 if (z->zfree == Z_NULL) z->zfree = zcfree;
87 if ((z->state = (struct internal_state FAR *)
88 ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL)
90 z->state->blocks = Z_NULL;
92 /* handle undocumented nowrap option (no zlib header or check) */
100 /* set window size */
104 return Z_STREAM_ERROR;
106 z->state->wbits = (uInt)w;
108 /* create inflate_blocks state */
109 if ((z->state->blocks =
110 inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, 1 << w))
116 Trace((stderr, "inflate: allocated\n"));
127 return inflateInit2(z, DEF_WBITS);
131 #define NEEDBYTE {if(z->avail_in==0)return r;r=Z_OK;}
132 #define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
138 int r = f; /* to avoid warning about unused f */
141 if (z == Z_NULL || z->next_in == Z_NULL)
142 return Z_STREAM_ERROR;
144 while (1) switch (z->state->mode)
148 if (((z->state->sub.method = NEXTBYTE) & 0xf) != DEFLATED)
150 z->state->mode = BAD;
151 z->msg = "unknown compression method";
152 z->state->sub.marker = 5; /* can't try inflateSync */
155 if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
157 z->state->mode = BAD;
158 z->msg = "invalid window size";
159 z->state->sub.marker = 5; /* can't try inflateSync */
162 z->state->mode = FLAG;
165 if ((b = NEXTBYTE) & 0x20)
167 z->state->mode = BAD;
168 z->msg = "invalid reserved bit";
169 z->state->sub.marker = 5; /* can't try inflateSync */
172 if (((z->state->sub.method << 8) + b) % 31)
174 z->state->mode = BAD;
175 z->msg = "incorrect header check";
176 z->state->sub.marker = 5; /* can't try inflateSync */
179 Trace((stderr, "inflate: zlib header ok\n"));
180 z->state->mode = BLOCKS;
182 r = inflate_blocks(z->state->blocks, z, r);
183 if (r == Z_DATA_ERROR)
185 z->state->mode = BAD;
186 z->state->sub.marker = 0; /* can try inflateSync */
189 if (r != Z_STREAM_END)
192 inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was);
193 if (z->state->nowrap)
195 z->state->mode = DONE;
198 z->state->mode = CHECK4;
201 z->state->sub.check.need = (uLong)NEXTBYTE << 24;
202 z->state->mode = CHECK3;
205 z->state->sub.check.need += (uLong)NEXTBYTE << 16;
206 z->state->mode = CHECK2;
209 z->state->sub.check.need += (uLong)NEXTBYTE << 8;
210 z->state->mode = CHECK1;
213 z->state->sub.check.need += (uLong)NEXTBYTE;
215 if (z->state->sub.check.was != z->state->sub.check.need)
217 z->state->mode = BAD;
218 z->msg = "incorrect data check";
219 z->state->sub.marker = 5; /* can't try inflateSync */
222 Trace((stderr, "inflate: zlib check ok\n"));
223 z->state->mode = DONE;
229 return Z_STREAM_ERROR;
237 uInt n; /* number of bytes to look at */
238 Bytef *p; /* pointer to bytes */
239 uInt m; /* number of marker bytes found in a row */
240 uLong r, w; /* temporaries to save total_in and total_out */
243 if (z == Z_NULL || z->state == Z_NULL)
244 return Z_STREAM_ERROR;
245 if (z->state->mode != BAD)
247 z->state->mode = BAD;
248 z->state->sub.marker = 0;
250 if ((n = z->avail_in) == 0)
253 m = z->state->sub.marker;
258 if (*p == (Byte)(m < 2 ? 0 : 0xff))
268 z->total_in += p - z->next_in;
271 z->state->sub.marker = m;
273 /* return no joy or set up to restart on a new block */
276 r = z->total_in; w = z->total_out;
278 z->total_in = r; z->total_out = w;
279 z->state->mode = BLOCKS;