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