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