ReactOS 0.4.17-dev-301-g9127a53
inflate.c
Go to the documentation of this file.
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
4 */
5
6#include "zutil.h"
7#include "infblock.h"
8
9#define DONE INFLATE_DONE
10#define BAD INFLATE_BAD
11
12typedef enum {
13 METHOD, /* waiting for method byte */
14 FLAG, /* waiting for flag byte */
15 DICT4, /* four dictionary check bytes to go */
16 DICT3, /* three dictionary check bytes to go */
17 DICT2, /* two dictionary check bytes to go */
18 DICT1, /* one dictionary check byte to go */
19 DICT0, /* waiting for inflateSetDictionary */
20 BLOCKS, /* decompressing blocks */
21 CHECK4, /* four check bytes to go */
22 CHECK3, /* three check bytes to go */
23 CHECK2, /* two check bytes to go */
24 CHECK1, /* one check byte to go */
25 DONE, /* finished check, done */
26 BAD} /* got an error--stay here */
28
29/* inflate private state */
31
32 /* mode */
33 inflate_mode mode; /* current inflate mode */
34
35 /* mode dependent information */
36 union {
37 uInt method; /* if FLAGS, method byte */
38 struct {
39 uLong was; /* computed check value */
40 uLong need; /* stream check value */
41 } check; /* if CHECK, check values to compare */
42 uInt marker; /* if BAD, inflateSync's marker bytes count */
43 } sub; /* submode */
44
45 /* mode independent information */
46 int nowrap; /* flag for no wrapper */
47 uInt wbits; /* log2(window size) (8..15, defaults to 15) */
49 *blocks; /* current inflate_blocks state */
50
51};
52
53
54ZEXPORT(int) inflateReset( /* z) */
56{
57 if (z == Z_NULL || z->state == Z_NULL)
58 return Z_STREAM_ERROR;
59 z->total_in = z->total_out = 0;
60 z->msg = Z_NULL;
61 z->state->mode = z->state->nowrap ? BLOCKS : METHOD;
62 inflate_blocks_reset(z->state->blocks, z, Z_NULL);
63 Tracev((stderr, "inflate: reset\n"));
64 return Z_OK;
65}
66
67
68ZEXPORT(int) inflateEnd( /* z) */
70{
71 if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL)
72 return Z_STREAM_ERROR;
73 if (z->state->blocks != Z_NULL)
74 inflate_blocks_free(z->state->blocks, z);
75 ZFREE(z, z->state);
76 z->state = Z_NULL;
77 Tracev((stderr, "inflate: end\n"));
78 return Z_OK;
79}
80
81
82ZEXPORT(int) inflateInit2_( /* z, w, version, stream_size) */
84int w,
85const char *version,
86int stream_size )
87{
88 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
89 stream_size != sizeof(z_stream))
90 return Z_VERSION_ERROR;
91
92 /* initialize state */
93 if (z == Z_NULL)
94 return Z_STREAM_ERROR;
95 z->msg = Z_NULL;
96 if (z->zalloc == Z_NULL)
97 {
98 z->zalloc = zcalloc;
99 z->opaque = (voidpf)0;
100 }
101 if (z->zfree == Z_NULL) z->zfree = zcfree;
102 if ((z->state = (struct internal_state FAR *)
103 ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL)
104 return Z_MEM_ERROR;
105 z->state->blocks = Z_NULL;
106
107 /* handle undocumented nowrap option (no zlib header or check) */
108 z->state->nowrap = 0;
109 if (w < 0)
110 {
111 w = - w;
112 z->state->nowrap = 1;
113 }
114
115 /* set window size */
116 if (w < 8 || w > 15)
117 {
118 inflateEnd(z);
119 return Z_STREAM_ERROR;
120 }
121 z->state->wbits = (uInt)w;
122
123 /* create inflate_blocks state */
124 if ((z->state->blocks =
125 inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, (uInt)1 << w))
126 == Z_NULL)
127 {
128 inflateEnd(z);
129 return Z_MEM_ERROR;
130 }
131 Tracev((stderr, "inflate: allocated\n"));
132
133 /* reset state */
135 return Z_OK;
136}
137
138
139
140#undef NEEDBYTE
141#define NEEDBYTE {if(z->avail_in==0)return r;r=f;}
142
143#undef NEXTBYTE
144#define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
145
146
147ZEXPORT(int) inflate( /* z, f) */
149int f )
150{
151 int r;
152 uInt b;
153
154 if (z == Z_NULL || z->state == Z_NULL || z->next_in == Z_NULL)
155 return Z_STREAM_ERROR;
156 f = f == Z_FINISH ? Z_BUF_ERROR : Z_OK;
157 r = Z_BUF_ERROR;
158 while (1) switch (z->state->mode)
159 {
160 case METHOD:
162 if (((z->state->sub.method = NEXTBYTE) & 0xf) != Z_DEFLATED)
163 {
164 z->state->mode = BAD;
165 z->msg = (char*)"unknown compression method";
166 z->state->sub.marker = 5; /* can't try inflateSync */
167 break;
168 }
169 if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
170 {
171 z->state->mode = BAD;
172 z->msg = (char*)"invalid window size";
173 z->state->sub.marker = 5; /* can't try inflateSync */
174 break;
175 }
176 z->state->mode = FLAG;
177 /* fall through */
178 case FLAG:
180 b = NEXTBYTE;
181 if (((z->state->sub.method << 8) + b) % 31)
182 {
183 z->state->mode = BAD;
184 z->msg = (char*)"incorrect header check";
185 z->state->sub.marker = 5; /* can't try inflateSync */
186 break;
187 }
188 Tracev((stderr, "inflate: zlib header ok\n"));
189 if (!(b & PRESET_DICT))
190 {
191 z->state->mode = BLOCKS;
192 break;
193 }
194 z->state->mode = DICT4;
195 /* fall through */
196 case DICT4:
198 z->state->sub.check.need = (uLong)NEXTBYTE << 24;
199 z->state->mode = DICT3;
200 /* fall through */
201 case DICT3:
203 z->state->sub.check.need += (uLong)NEXTBYTE << 16;
204 z->state->mode = DICT2;
205 /* fall through */
206 case DICT2:
208 z->state->sub.check.need += (uLong)NEXTBYTE << 8;
209 z->state->mode = DICT1;
210 /* fall through */
211 case DICT1:
213 z->state->sub.check.need += (uLong)NEXTBYTE;
214 z->adler = z->state->sub.check.need;
215 z->state->mode = DICT0;
216 return Z_NEED_DICT;
217 case DICT0:
218 z->state->mode = BAD;
219 z->msg = (char*)"need dictionary";
220 z->state->sub.marker = 0; /* can try inflateSync */
221 return Z_STREAM_ERROR;
222 case BLOCKS:
223 r = inflate_blocks(z->state->blocks, z, r);
224 if (r == Z_DATA_ERROR)
225 {
226 z->state->mode = BAD;
227 z->state->sub.marker = 0; /* can try inflateSync */
228 break;
229 }
230 if (r == Z_OK)
231 r = f;
232 if (r != Z_STREAM_END)
233 return r;
234 r = f;
235 inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was);
236 if (z->state->nowrap)
237 {
238 z->state->mode = DONE;
239 break;
240 }
241 z->state->mode = CHECK4;
242 /* fall through */
243 case CHECK4:
245 z->state->sub.check.need = (uLong)NEXTBYTE << 24;
246 z->state->mode = CHECK3;
247 /* fall through */
248 case CHECK3:
250 z->state->sub.check.need += (uLong)NEXTBYTE << 16;
251 z->state->mode = CHECK2;
252 /* fall through */
253 case CHECK2:
255 z->state->sub.check.need += (uLong)NEXTBYTE << 8;
256 z->state->mode = CHECK1;
257 /* fall through */
258 case CHECK1:
260 z->state->sub.check.need += (uLong)NEXTBYTE;
261
262 if (z->state->sub.check.was != z->state->sub.check.need)
263 {
264 z->state->mode = BAD;
265 z->msg = (char*)"incorrect data check";
266 z->state->sub.marker = 5; /* can't try inflateSync */
267 break;
268 }
269 Tracev((stderr, "inflate: zlib check ok\n"));
270 z->state->mode = DONE;
271 /* fall through */
272 case DONE:
273 return Z_STREAM_END;
274 case BAD:
275 return Z_DATA_ERROR;
276 default:
277 return Z_STREAM_ERROR;
278 }
279#ifdef NEED_DUMMY_RETURN
280 return Z_STREAM_ERROR; /* Some dumb compilers complain without this */
281#endif
282}
283
inflate_mode
Definition: inflate.c:157
@ DONE
Definition: inflate.c:186
@ BAD
Definition: inflate.c:187
#define ZALLOC(strm, items, size)
Definition: inflate.c:49
static int inflateReset(z_streamp strm)
Definition: inflate.c:839
#define Tracev(x)
Definition: inflate.c:43
#define ZFREE(strm, addr)
Definition: inflate.c:51
int inflate(z_streamp strm, int flush)
Definition: inflate.c:1257
static uLong adler32(uLong adler, const Bytef *buf, uInt len)
Definition: inflate.c:72
int inflateEnd(z_streamp strm)
Definition: inflate.c:1910
unsigned long uLong
Definition: zlib.h:39
#define Z_DEFLATED
Definition: zlib.h:146
#define Z_NEED_DICT
Definition: zlib.h:116
#define Z_BUF_ERROR
Definition: zlib.h:121
z_stream FAR * z_streamp
Definition: zlib.h:80
#define Z_VERSION_ERROR
Definition: zlib.h:122
void FAR * voidpf
Definition: zlib.h:42
#define Z_STREAM_END
Definition: zlib.h:115
#define Z_FINISH
Definition: zlib.h:109
unsigned int uInt
Definition: zlib.h:38
#define Z_OK
Definition: zlib.h:114
#define Z_DATA_ERROR
Definition: zlib.h:119
#define Z_STREAM_ERROR
Definition: zlib.h:118
#define Z_NULL
Definition: zlib.h:149
#define Z_MEM_ERROR
Definition: zlib.h:120
#define FAR
Definition: zlib.h:34
static const WCHAR version[]
Definition: asmname.c:66
#define stderr
void zcfree(voidpf opaque, voidpf ptr)
Definition: zutil.c:173
voidpf zcalloc(voidpf opaque, unsigned items, unsigned size)
Definition: zutil.c:164
#define PRESET_DICT
Definition: zutil.h:68
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
GLfloat f
Definition: glext.h:7540
GLboolean GLboolean GLboolean b
Definition: glext.h:6204
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:6102
GLdouble GLdouble z
Definition: glext.h:5874
int inflate_blocks_free(inflate_blocks_statef *s, z_streamp z)
Definition: infblock.c:380
void inflate_blocks_reset(inflate_blocks_statef *s, z_streamp z, uLongf *c)
Definition: infblock.c:67
inflate_blocks_statef * inflate_blocks_new(z_streamp z, check_func c, uInt w)
Definition: infblock.c:88
int inflate_blocks(inflate_blocks_statef *s, z_streamp z, int r)
Definition: infblock.c:119
struct inflate_blocks_state FAR inflate_blocks_statef
Definition: infblock.h:15
#define f
Definition: ke_i.h:83
#define b
Definition: ke_i.h:79
#define ZLIB_VERSION
Definition: zlib.h:40
@ FLAG
Definition: inflate.c:14
@ DICT2
Definition: inflate.c:17
@ BLOCKS
Definition: inflate.c:20
@ CHECK1
Definition: inflate.c:24
@ CHECK2
Definition: inflate.c:23
@ CHECK4
Definition: inflate.c:21
@ DICT0
Definition: inflate.c:19
@ DICT4
Definition: inflate.c:15
@ DICT3
Definition: inflate.c:16
@ METHOD
Definition: inflate.c:13
@ DICT1
Definition: inflate.c:18
@ CHECK3
Definition: inflate.c:22
#define NEEDBYTE
Definition: inflate.c:141
#define NEXTBYTE
Definition: inflate.c:144
int const char int stream_size
Definition: zlib.h:814
int ZEXPORT inflateInit2_(z_streamp strm, int windowBits, const char *version, int stream_size)
Definition: inflate.c:198
uInt method
Definition: inflate.c:37
uInt wbits
Definition: inflate.c:47
inflate_blocks_statef * blocks
Definition: inflate.c:49
union internal_state::@4733 sub
uLong need
Definition: inflate.c:40
uInt marker
Definition: inflate.c:42
uLong was
Definition: inflate.c:39
inflate_mode mode
Definition: inflate.c:33
struct internal_state::@4733::@4734 check
#define ZEXPORT
Definition: zconf.h:386