blob: 4b6e3449d82402286ce4172afc61162da136b9e4 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * This code is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 only, as
6 * published by the Free Software Foundation. Sun designates this
7 * particular file as subject to the "Classpath" exception as provided
8 * by Sun in the LICENSE file that accompanied this code.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
22 * have any questions.
23 *
24 * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
25 */
26
27/*
28 * This file is available under and governed by the GNU General Public
29 * License version 2 only, as published by the Free Software Foundation.
30 * However, the following notice accompanied the original version of this
31 * file and, per its terms, should not be removed:
32 *
33 * inflate.c -- zlib interface to inflate modules
34 * Copyright (C) 1995-1998 Mark Adler
35 * For conditions of distribution and use, see copyright notice in zlib.h
36 */
37
38#include "zutil.h"
39#include "infblock.h"
40
41struct inflate_blocks_state {int dummy;}; /* for buggy compilers */
42
43typedef enum {
44 METHOD, /* waiting for method byte */
45 FLAG, /* waiting for flag byte */
46 DICT4, /* four dictionary check bytes to go */
47 DICT3, /* three dictionary check bytes to go */
48 DICT2, /* two dictionary check bytes to go */
49 DICT1, /* one dictionary check byte to go */
50 DICT0, /* waiting for inflateSetDictionary */
51 BLOCKS, /* decompressing blocks */
52 CHECK4, /* four check bytes to go */
53 CHECK3, /* three check bytes to go */
54 CHECK2, /* two check bytes to go */
55 CHECK1, /* one check byte to go */
56 DONE, /* finished check, done */
57 BAD} /* got an error--stay here */
58inflate_mode;
59
60/* inflate private state */
61struct internal_state {
62
63 /* mode */
64 inflate_mode mode; /* current inflate mode */
65
66 /* mode dependent information */
67 union {
68 uInt method; /* if FLAGS, method byte */
69 struct {
70 uLong was; /* computed check value */
71 uLong need; /* stream check value */
72 } check; /* if CHECK, check values to compare */
73 uInt marker; /* if BAD, inflateSync's marker bytes count */
74 } sub; /* submode */
75
76 /* mode independent information */
77 int nowrap; /* flag for no wrapper */
78 uInt wbits; /* log2(window size) (8..15, defaults to 15) */
79 inflate_blocks_statef
80 *blocks; /* current inflate_blocks state */
81
82};
83
84
85int ZEXPORT inflateReset(z)
86z_streamp z;
87{
88 if (z == Z_NULL || z->state == Z_NULL)
89 return Z_STREAM_ERROR;
90 z->total_in = z->total_out = 0;
91 z->msg = Z_NULL;
92 z->state->mode = z->state->nowrap ? BLOCKS : METHOD;
93 inflate_blocks_reset(z->state->blocks, z, Z_NULL);
94 Tracev((stderr, "inflate: reset\n"));
95 return Z_OK;
96}
97
98
99int ZEXPORT inflateEnd(z)
100z_streamp z;
101{
102 if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL)
103 return Z_STREAM_ERROR;
104 if (z->state->blocks != Z_NULL)
105 inflate_blocks_free(z->state->blocks, z);
106 ZFREE(z, z->state);
107 z->state = Z_NULL;
108 Tracev((stderr, "inflate: end\n"));
109 return Z_OK;
110}
111
112
113int ZEXPORT inflateInit2_(z, w, version, stream_size)
114z_streamp z;
115int w;
116const char *version;
117int stream_size;
118{
119 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
120 stream_size != sizeof(z_stream))
121 return Z_VERSION_ERROR;
122
123 /* initialize state */
124 if (z == Z_NULL)
125 return Z_STREAM_ERROR;
126 z->msg = Z_NULL;
127 if (z->zalloc == Z_NULL)
128 {
129 z->zalloc = zcalloc;
130 z->opaque = (voidpf)0;
131 }
132 if (z->zfree == Z_NULL) z->zfree = zcfree;
133 if ((z->state = (struct internal_state FAR *)
134 ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL)
135 return Z_MEM_ERROR;
136 z->state->blocks = Z_NULL;
137
138 /* handle undocumented nowrap option (no zlib header or check) */
139 z->state->nowrap = 0;
140 if (w < 0)
141 {
142 w = - w;
143 z->state->nowrap = 1;
144 }
145
146 /* set window size */
147 if (w < 8 || w > 15)
148 {
149 inflateEnd(z);
150 return Z_STREAM_ERROR;
151 }
152 z->state->wbits = (uInt)w;
153
154 /* create inflate_blocks state */
155 if ((z->state->blocks =
156 inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, (uInt)1 << w))
157 == Z_NULL)
158 {
159 inflateEnd(z);
160 return Z_MEM_ERROR;
161 }
162 Tracev((stderr, "inflate: allocated\n"));
163
164 /* reset state */
165 inflateReset(z);
166 return Z_OK;
167}
168
169
170int ZEXPORT inflateInit_(z, version, stream_size)
171z_streamp z;
172const char *version;
173int stream_size;
174{
175 return inflateInit2_(z, DEF_WBITS, version, stream_size);
176}
177
178
179#define NEEDBYTE {if(z->avail_in==0)return r;r=f;}
180#define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
181
182int ZEXPORT inflate(z, f)
183z_streamp z;
184int f;
185{
186 int r;
187 uInt b;
188
189 if (z == Z_NULL || z->state == Z_NULL || z->next_in == Z_NULL)
190 return Z_STREAM_ERROR;
191 f = f == Z_FINISH ? Z_BUF_ERROR : Z_OK;
192 r = Z_BUF_ERROR;
193 while (1) switch (z->state->mode)
194 {
195 case METHOD:
196 NEEDBYTE
197 if (((z->state->sub.method = NEXTBYTE) & 0xf) != Z_DEFLATED)
198 {
199 z->state->mode = BAD;
200 z->msg = (char*)"unknown compression method";
201 z->state->sub.marker = 5; /* can't try inflateSync */
202 break;
203 }
204 if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
205 {
206 z->state->mode = BAD;
207 z->msg = (char*)"invalid window size";
208 z->state->sub.marker = 5; /* can't try inflateSync */
209 break;
210 }
211 z->state->mode = FLAG;
212 case FLAG:
213 NEEDBYTE
214 b = NEXTBYTE;
215 if (((z->state->sub.method << 8) + b) % 31)
216 {
217 z->state->mode = BAD;
218 z->msg = (char*)"incorrect header check";
219 z->state->sub.marker = 5; /* can't try inflateSync */
220 break;
221 }
222 Tracev((stderr, "inflate: zlib header ok\n"));
223 if (!(b & PRESET_DICT))
224 {
225 z->state->mode = BLOCKS;
226 break;
227 }
228 z->state->mode = DICT4;
229 case DICT4:
230 NEEDBYTE
231 z->state->sub.check.need = (uLong)NEXTBYTE << 24;
232 z->state->mode = DICT3;
233 case DICT3:
234 NEEDBYTE
235 z->state->sub.check.need += (uLong)NEXTBYTE << 16;
236 z->state->mode = DICT2;
237 case DICT2:
238 NEEDBYTE
239 z->state->sub.check.need += (uLong)NEXTBYTE << 8;
240 z->state->mode = DICT1;
241 case DICT1:
242 NEEDBYTE
243 z->state->sub.check.need += (uLong)NEXTBYTE;
244 z->adler = z->state->sub.check.need;
245 z->state->mode = DICT0;
246 return Z_NEED_DICT;
247 case DICT0:
248 z->state->mode = BAD;
249 z->msg = (char*)"need dictionary";
250 z->state->sub.marker = 0; /* can try inflateSync */
251 return Z_STREAM_ERROR;
252 case BLOCKS:
253 r = inflate_blocks(z->state->blocks, z, r);
254 if (r == Z_DATA_ERROR)
255 {
256 z->state->mode = BAD;
257 z->state->sub.marker = 0; /* can try inflateSync */
258 break;
259 }
260 if (r == Z_OK)
261 r = f;
262 if (r != Z_STREAM_END)
263 return r;
264 r = f;
265 inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was);
266
267 /* zlib.h inflate() doc states that z->adler contains a checksum
268 of all uncompressed output even when returning Z_STREAM_END. */
269 z->adler = z->state->sub.check.was;
270
271 if (z->state->nowrap)
272 {
273 z->state->mode = DONE;
274 break;
275 }
276 z->state->mode = CHECK4;
277 case CHECK4:
278 NEEDBYTE
279 z->state->sub.check.need = (uLong)NEXTBYTE << 24;
280 z->state->mode = CHECK3;
281 case CHECK3:
282 NEEDBYTE
283 z->state->sub.check.need += (uLong)NEXTBYTE << 16;
284 z->state->mode = CHECK2;
285 case CHECK2:
286 NEEDBYTE
287 z->state->sub.check.need += (uLong)NEXTBYTE << 8;
288 z->state->mode = CHECK1;
289 case CHECK1:
290 NEEDBYTE
291 z->state->sub.check.need += (uLong)NEXTBYTE;
292
293 if (z->state->sub.check.was != z->state->sub.check.need)
294 {
295 z->state->mode = BAD;
296 z->msg = (char*)"incorrect data check";
297 z->state->sub.marker = 5; /* can't try inflateSync */
298 break;
299 }
300 Tracev((stderr, "inflate: zlib check ok\n"));
301 z->state->mode = DONE;
302 case DONE:
303 return Z_STREAM_END;
304 case BAD:
305 return Z_DATA_ERROR;
306 default:
307 return Z_STREAM_ERROR;
308 }
309#ifdef NEED_DUMMY_RETURN
310 return Z_STREAM_ERROR; /* Some dumb compilers complain without this */
311#endif
312}
313
314
315int ZEXPORT inflateSetDictionary(z, dictionary, dictLength)
316z_streamp z;
317const Bytef *dictionary;
318uInt dictLength;
319{
320 uInt length = dictLength;
321
322 if (z == Z_NULL || z->state == Z_NULL || z->state->mode != DICT0)
323 return Z_STREAM_ERROR;
324
325 if (adler32(1L, dictionary, dictLength) != z->adler) return Z_DATA_ERROR;
326 z->adler = 1L;
327
328 if (length >= ((uInt)1<<z->state->wbits))
329 {
330 length = (1<<z->state->wbits)-1;
331 dictionary += dictLength - length;
332 }
333 inflate_set_dictionary(z->state->blocks, dictionary, length);
334 z->state->mode = BLOCKS;
335 return Z_OK;
336}
337
338
339int ZEXPORT inflateSync(z)
340z_streamp z;
341{
342 uInt n; /* number of bytes to look at */
343 Bytef *p; /* pointer to bytes */
344 uInt m; /* number of marker bytes found in a row */
345 uLong r, w; /* temporaries to save total_in and total_out */
346
347 /* set up */
348 if (z == Z_NULL || z->state == Z_NULL)
349 return Z_STREAM_ERROR;
350 if (z->state->mode != BAD)
351 {
352 z->state->mode = BAD;
353 z->state->sub.marker = 0;
354 }
355 if ((n = z->avail_in) == 0)
356 return Z_BUF_ERROR;
357 p = z->next_in;
358 m = z->state->sub.marker;
359
360 /* search */
361 while (n && m < 4)
362 {
363 static const Byte mark[4] = {0, 0, 0xff, 0xff};
364 if (*p == mark[m])
365 m++;
366 else if (*p)
367 m = 0;
368 else
369 m = 4 - m;
370 p++, n--;
371 }
372
373 /* restore */
374 z->total_in += p - z->next_in;
375 z->next_in = p;
376 z->avail_in = n;
377 z->state->sub.marker = m;
378
379 /* return no joy or set up to restart on a new block */
380 if (m != 4)
381 return Z_DATA_ERROR;
382 r = z->total_in; w = z->total_out;
383 inflateReset(z);
384 z->total_in = r; z->total_out = w;
385 z->state->mode = BLOCKS;
386 return Z_OK;
387}
388
389
390/* Returns true if inflate is currently at the end of a block generated
391 * by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
392 * implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH
393 * but removes the length bytes of the resulting empty stored block. When
394 * decompressing, PPP checks that at the end of input packet, inflate is
395 * waiting for these length bytes.
396 */
397int ZEXPORT inflateSyncPoint(z)
398z_streamp z;
399{
400 if (z == Z_NULL || z->state == Z_NULL || z->state->blocks == Z_NULL)
401 return Z_STREAM_ERROR;
402 return inflate_blocks_sync_point(z->state->blocks);
403}