aboutsummaryrefslogtreecommitdiff
path: root/inflate.c
diff options
context:
space:
mode:
authorMark Adler <madler@alumni.caltech.edu>2011-09-09 23:11:37 -0700
committerMark Adler <madler@alumni.caltech.edu>2011-09-09 23:11:37 -0700
commit56bcb184fac036a45cb8937238d51778d0a796aa (patch)
tree7b127418b30e135f8ce27ec136038b5090540820 /inflate.c
parent25e5325501edade156e897f95afdaa2be78ad9a3 (diff)
downloadzlib-56bcb184fac036a45cb8937238d51778d0a796aa.tar.gz
zlib-56bcb184fac036a45cb8937238d51778d0a796aa.tar.bz2
zlib-56bcb184fac036a45cb8937238d51778d0a796aa.zip
zlib 0.99v0.99
Diffstat (limited to 'inflate.c')
-rw-r--r--inflate.c103
1 files changed, 83 insertions, 20 deletions
diff --git a/inflate.c b/inflate.c
index ec0f3a0..1f576e4 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 Mark Adler 2 * Copyright (C) 1995-1996 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,6 +15,11 @@ 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 */
18 BLOCKS, /* decompressing blocks */ 23 BLOCKS, /* decompressing blocks */
19 CHECK4, /* four check bytes to go */ 24 CHECK4, /* four check bytes to go */
20 CHECK3, /* three check bytes to go */ 25 CHECK3, /* three check bytes to go */
@@ -75,14 +80,25 @@ z_stream *z;
75} 80}
76 81
77 82
78int inflateInit2(z, w) 83int inflateInit2_(z, w, version, stream_size)
79z_stream *z; 84z_stream *z;
80int w; 85int w;
86const char *version;
87int stream_size;
81{ 88{
89 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
90 stream_size != sizeof(z_stream))
91 return Z_VERSION_ERROR;
92
82 /* initialize state */ 93 /* initialize state */
83 if (z == Z_NULL) 94 if (z == Z_NULL)
84 return Z_STREAM_ERROR; 95 return Z_STREAM_ERROR;
85 if (z->zalloc == Z_NULL) z->zalloc = zcalloc; 96 z->msg = Z_NULL;
97 if (z->zalloc == Z_NULL)
98 {
99 z->zalloc = zcalloc;
100 z->opaque = (voidpf)0;
101 }
86 if (z->zfree == Z_NULL) z->zfree = zcfree; 102 if (z->zfree == Z_NULL) z->zfree = zcfree;
87 if ((z->state = (struct internal_state FAR *) 103 if ((z->state = (struct internal_state FAR *)
88 ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL) 104 ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL)
@@ -107,7 +123,7 @@ int w;
107 123
108 /* create inflate_blocks state */ 124 /* create inflate_blocks state */
109 if ((z->state->blocks = 125 if ((z->state->blocks =
110 inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, 1 << w)) 126 inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, (uInt)1 << w))
111 == Z_NULL) 127 == Z_NULL)
112 { 128 {
113 inflateEnd(z); 129 inflateEnd(z);
@@ -121,10 +137,12 @@ int w;
121} 137}
122 138
123 139
124int inflateInit(z) 140int inflateInit_(z, version, stream_size)
125z_stream *z; 141z_stream *z;
142const char *version;
143int stream_size;
126{ 144{
127 return inflateInit2(z, DEF_WBITS); 145 return inflateInit2_(z, DEF_WBITS, version, stream_size);
128} 146}
129 147
130 148
@@ -138,46 +156,68 @@ int f;
138 int r = f; /* to avoid warning about unused f */ 156 int r = f; /* to avoid warning about unused f */
139 uInt b; 157 uInt b;
140 158
141 if (z == Z_NULL || z->next_in == Z_NULL) 159 if (z == Z_NULL || z->state == Z_NULL || z->next_in == Z_NULL)
142 return Z_STREAM_ERROR; 160 return Z_STREAM_ERROR;
143 r = Z_BUF_ERROR; 161 r = Z_BUF_ERROR;
144 while (1) switch (z->state->mode) 162 while (1) switch (z->state->mode)
145 { 163 {
146 case METHOD: 164 case METHOD:
147 NEEDBYTE 165 NEEDBYTE
148 if (((z->state->sub.method = NEXTBYTE) & 0xf) != DEFLATED) 166 if (((z->state->sub.method = NEXTBYTE) & 0xf) != Z_DEFLATED)
149 { 167 {
150 z->state->mode = BAD; 168 z->state->mode = BAD;
151 z->msg = "unknown compression method"; 169 z->msg = (char*)"unknown compression method";
152 z->state->sub.marker = 5; /* can't try inflateSync */ 170 z->state->sub.marker = 5; /* can't try inflateSync */
153 break; 171 break;
154 } 172 }
155 if ((z->state->sub.method >> 4) + 8 > z->state->wbits) 173 if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
156 { 174 {
157 z->state->mode = BAD; 175 z->state->mode = BAD;
158 z->msg = "invalid window size"; 176 z->msg = (char*)"invalid window size";
159 z->state->sub.marker = 5; /* can't try inflateSync */ 177 z->state->sub.marker = 5; /* can't try inflateSync */
160 break; 178 break;
161 } 179 }
162 z->state->mode = FLAG; 180 z->state->mode = FLAG;
163 case FLAG: 181 case FLAG:
164 NEEDBYTE 182 NEEDBYTE
165 if ((b = NEXTBYTE) & 0x20) 183 b = NEXTBYTE;
166 {
167 z->state->mode = BAD;
168 z->msg = "invalid reserved bit";
169 z->state->sub.marker = 5; /* can't try inflateSync */
170 break;
171 }
172 if (((z->state->sub.method << 8) + b) % 31) 184 if (((z->state->sub.method << 8) + b) % 31)
173 { 185 {
174 z->state->mode = BAD; 186 z->state->mode = BAD;
175 z->msg = "incorrect header check"; 187 z->msg = (char*)"incorrect header check";
176 z->state->sub.marker = 5; /* can't try inflateSync */ 188 z->state->sub.marker = 5; /* can't try inflateSync */
177 break; 189 break;
178 } 190 }
179 Trace((stderr, "inflate: zlib header ok\n")); 191 Trace((stderr, "inflate: zlib header ok\n"));
180 z->state->mode = BLOCKS; 192 if (!(b & PRESET_DICT))
193 {
194 z->state->mode = BLOCKS;
195 break;
196 }
197 z->state->mode = DICT4;
198 case DICT4:
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;
181 case BLOCKS: 221 case BLOCKS:
182 r = inflate_blocks(z->state->blocks, z, r); 222 r = inflate_blocks(z->state->blocks, z, r);
183 if (r == Z_DATA_ERROR) 223 if (r == Z_DATA_ERROR)
@@ -215,7 +255,7 @@ int f;
215 if (z->state->sub.check.was != z->state->sub.check.need) 255 if (z->state->sub.check.was != z->state->sub.check.need)
216 { 256 {
217 z->state->mode = BAD; 257 z->state->mode = BAD;
218 z->msg = "incorrect data check"; 258 z->msg = (char*)"incorrect data check";
219 z->state->sub.marker = 5; /* can't try inflateSync */ 259 z->state->sub.marker = 5; /* can't try inflateSync */
220 break; 260 break;
221 } 261 }
@@ -231,6 +271,29 @@ int f;
231} 271}
232 272
233 273
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
234int inflateSync(z) 297int inflateSync(z)
235z_stream *z; 298z_stream *z;
236{ 299{