summaryrefslogtreecommitdiff
path: root/inflate.c
diff options
context:
space:
mode:
Diffstat (limited to 'inflate.c')
-rw-r--r--inflate.c94
1 files changed, 18 insertions, 76 deletions
diff --git a/inflate.c b/inflate.c
index 1f576e4..b71f74f 100644
--- a/inflate.c
+++ b/inflate.c
@@ -1,5 +1,5 @@
1/* inflate.c -- zlib interface to inflate modules 1/* inflate.c -- zlib interface to inflate modules
2 * Copyright (C) 1995-1996 Mark Adler 2 * Copyright (C) 1995 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
@@ -15,11 +15,6 @@ struct internal_state {
15 enum { 15 enum {
16 METHOD, /* waiting for method byte */ 16 METHOD, /* waiting for method byte */
17 FLAG, /* waiting for flag byte */ 17 FLAG, /* waiting for flag byte */
18 DICT4, /* four dictionary check bytes to go */
19 DICT3, /* three dictionary check bytes to go */
20 DICT2, /* two dictionary check bytes to go */
21 DICT1, /* one dictionary check byte to go */
22 DICT0, /* waiting for inflateSetDictionary */
23 BLOCKS, /* decompressing blocks */ 18 BLOCKS, /* decompressing blocks */
24 CHECK4, /* four check bytes to go */ 19 CHECK4, /* four check bytes to go */
25 CHECK3, /* three check bytes to go */ 20 CHECK3, /* three check bytes to go */
@@ -80,16 +75,10 @@ z_stream *z;
80} 75}
81 76
82 77
83int inflateInit2_(z, w, version, stream_size) 78int inflateInit2(z, w)
84z_stream *z; 79z_stream *z;
85int w; 80int w;
86const char *version;
87int stream_size;
88{ 81{
89 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
90 stream_size != sizeof(z_stream))
91 return Z_VERSION_ERROR;
92
93 /* initialize state */ 82 /* initialize state */
94 if (z == Z_NULL) 83 if (z == Z_NULL)
95 return Z_STREAM_ERROR; 84 return Z_STREAM_ERROR;
@@ -123,7 +112,7 @@ int stream_size;
123 112
124 /* create inflate_blocks state */ 113 /* create inflate_blocks state */
125 if ((z->state->blocks = 114 if ((z->state->blocks =
126 inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, (uInt)1 << w)) 115 inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, 1 << w))
127 == Z_NULL) 116 == Z_NULL)
128 { 117 {
129 inflateEnd(z); 118 inflateEnd(z);
@@ -137,12 +126,10 @@ int stream_size;
137} 126}
138 127
139 128
140int inflateInit_(z, version, stream_size) 129int inflateInit(z)
141z_stream *z; 130z_stream *z;
142const char *version;
143int stream_size;
144{ 131{
145 return inflateInit2_(z, DEF_WBITS, version, stream_size); 132 return inflateInit2(z, DEF_WBITS);
146} 133}
147 134
148 135
@@ -156,7 +143,7 @@ int f;
156 int r = f; /* to avoid warning about unused f */ 143 int r = f; /* to avoid warning about unused f */
157 uInt b; 144 uInt b;
158 145
159 if (z == Z_NULL || z->state == Z_NULL || z->next_in == Z_NULL) 146 if (z == Z_NULL || z->next_in == Z_NULL)
160 return Z_STREAM_ERROR; 147 return Z_STREAM_ERROR;
161 r = Z_BUF_ERROR; 148 r = Z_BUF_ERROR;
162 while (1) switch (z->state->mode) 149 while (1) switch (z->state->mode)
@@ -166,58 +153,36 @@ int f;
166 if (((z->state->sub.method = NEXTBYTE) & 0xf) != Z_DEFLATED) 153 if (((z->state->sub.method = NEXTBYTE) & 0xf) != Z_DEFLATED)
167 { 154 {
168 z->state->mode = BAD; 155 z->state->mode = BAD;
169 z->msg = (char*)"unknown compression method"; 156 z->msg = "unknown compression method";
170 z->state->sub.marker = 5; /* can't try inflateSync */ 157 z->state->sub.marker = 5; /* can't try inflateSync */
171 break; 158 break;
172 } 159 }
173 if ((z->state->sub.method >> 4) + 8 > z->state->wbits) 160 if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
174 { 161 {
175 z->state->mode = BAD; 162 z->state->mode = BAD;
176 z->msg = (char*)"invalid window size"; 163 z->msg = "invalid window size";
177 z->state->sub.marker = 5; /* can't try inflateSync */ 164 z->state->sub.marker = 5; /* can't try inflateSync */
178 break; 165 break;
179 } 166 }
180 z->state->mode = FLAG; 167 z->state->mode = FLAG;
181 case FLAG: 168 case FLAG:
182 NEEDBYTE 169 NEEDBYTE
183 b = NEXTBYTE; 170 if ((b = NEXTBYTE) & 0x20)
184 if (((z->state->sub.method << 8) + b) % 31)
185 { 171 {
186 z->state->mode = BAD; 172 z->state->mode = BAD;
187 z->msg = (char*)"incorrect header check"; 173 z->msg = "invalid reserved bit";
188 z->state->sub.marker = 5; /* can't try inflateSync */ 174 z->state->sub.marker = 5; /* can't try inflateSync */
189 break; 175 break;
190 } 176 }
191 Trace((stderr, "inflate: zlib header ok\n")); 177 if (((z->state->sub.method << 8) + b) % 31)
192 if (!(b & PRESET_DICT))
193 { 178 {
194 z->state->mode = BLOCKS; 179 z->state->mode = BAD;
195 break; 180 z->msg = "incorrect header check";
181 z->state->sub.marker = 5; /* can't try inflateSync */
182 break;
196 } 183 }
197 z->state->mode = DICT4; 184 Trace((stderr, "inflate: zlib header ok\n"));
198 case DICT4: 185 z->state->mode = BLOCKS;
199 NEEDBYTE
200 z->state->sub.check.need = (uLong)NEXTBYTE << 24;
201 z->state->mode = DICT3;
202 case DICT3:
203 NEEDBYTE
204 z->state->sub.check.need += (uLong)NEXTBYTE << 16;
205 z->state->mode = DICT2;
206 case DICT2:
207 NEEDBYTE
208 z->state->sub.check.need += (uLong)NEXTBYTE << 8;
209 z->state->mode = DICT1;
210 case DICT1:
211 NEEDBYTE
212 z->state->sub.check.need += (uLong)NEXTBYTE;
213 z->adler = z->state->sub.check.need;
214 z->state->mode = DICT0;
215 return Z_NEED_DICT;
216 case DICT0:
217 z->state->mode = BAD;
218 z->msg = (char*)"need dictionary";
219 z->state->sub.marker = 0; /* can try inflateSync */
220 return Z_STREAM_ERROR;
221 case BLOCKS: 186 case BLOCKS:
222 r = inflate_blocks(z->state->blocks, z, r); 187 r = inflate_blocks(z->state->blocks, z, r);
223 if (r == Z_DATA_ERROR) 188 if (r == Z_DATA_ERROR)
@@ -255,7 +220,7 @@ int f;
255 if (z->state->sub.check.was != z->state->sub.check.need) 220 if (z->state->sub.check.was != z->state->sub.check.need)
256 { 221 {
257 z->state->mode = BAD; 222 z->state->mode = BAD;
258 z->msg = (char*)"incorrect data check"; 223 z->msg = "incorrect data check";
259 z->state->sub.marker = 5; /* can't try inflateSync */ 224 z->state->sub.marker = 5; /* can't try inflateSync */
260 break; 225 break;
261 } 226 }
@@ -271,29 +236,6 @@ int f;
271} 236}
272 237
273 238
274int inflateSetDictionary(z, dictionary, dictLength)
275z_stream *z;
276const Bytef *dictionary;
277uInt dictLength;
278{
279 uInt length = dictLength;
280
281 if (z == Z_NULL || z->state == Z_NULL || z->state->mode != DICT0)
282 return Z_STREAM_ERROR;
283 if (adler32(1L, dictionary, dictLength) != z->adler) return Z_DATA_ERROR;
284 z->adler = 1L;
285
286 if (length >= (1<<z->state->wbits))
287 {
288 length = (1<<z->state->wbits)-1;
289 dictionary += dictLength - length;
290 }
291 inflate_set_dictionary(z->state->blocks, z, dictionary, length);
292 z->state->mode = BLOCKS;
293 return Z_OK;
294}
295
296
297int inflateSync(z) 239int inflateSync(z)
298z_stream *z; 240z_stream *z;
299{ 241{