annotate lib/zlib/inflate.c @ 117:a64b244da76c

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