annotate zlib/inflate.c @ 3:5a977ccbc7a9 default tip

Empty changelog
author darius
date Sat, 06 Dec 1997 05:41:29 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
1 /* inflate.c -- zlib interface to inflate modules
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
2 * Copyright (C) 1995 Mark Adler
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
3 * For conditions of distribution and use, see copyright notice in zlib.h
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
4 */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
5
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
6 #include "zutil.h"
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
7 #include "infblock.h"
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
8
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
9 struct inflate_blocks_state {int dummy;}; /* for buggy compilers */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
10
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
11 /* inflate private state */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
12 struct internal_state {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
13
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
14 /* mode */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
15 enum {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
16 METHOD, /* waiting for method byte */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
17 FLAG, /* waiting for flag byte */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
18 BLOCKS, /* decompressing blocks */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
19 CHECK4, /* four check bytes to go */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
20 CHECK3, /* three check bytes to go */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
21 CHECK2, /* two check bytes to go */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
22 CHECK1, /* one check byte to go */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
23 DONE, /* finished check, done */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
24 BAD} /* got an error--stay here */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
25 mode; /* current inflate mode */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
26
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
27 /* mode dependent information */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
28 union {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
29 uInt method; /* if FLAGS, method byte */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
30 struct {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
31 uLong was; /* computed check value */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
32 uLong need; /* stream check value */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
33 } check; /* if CHECK, check values to compare */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
34 uInt marker; /* if BAD, inflateSync's marker bytes count */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
35 } sub; /* submode */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
36
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
37 /* mode independent information */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
38 int nowrap; /* flag for no wrapper */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
39 uInt wbits; /* log2(window size) (8..15, defaults to 15) */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
40 struct inflate_blocks_state
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
41 *blocks; /* current inflate_blocks state */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
42
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
43 };
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
44
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
45
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
46 int inflateReset(z)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
47 z_stream *z;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
48 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
49 uLong c;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
50
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
51 if (z == Z_NULL || z->state == Z_NULL)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
52 return Z_STREAM_ERROR;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
53 z->total_in = z->total_out = 0;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
54 z->msg = Z_NULL;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
55 z->state->mode = z->state->nowrap ? BLOCKS : METHOD;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
56 inflate_blocks_reset(z->state->blocks, z, &c);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
57 Trace((stderr, "inflate: reset\n"));
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
58 return Z_OK;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
59 }
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
60
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
61
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
62 int inflateEnd(z)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
63 z_stream *z;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
64 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
65 uLong c;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
66
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
67 if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
68 return Z_STREAM_ERROR;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
69 if (z->state->blocks != Z_NULL)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
70 inflate_blocks_free(z->state->blocks, z, &c);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
71 ZFREE(z, z->state);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
72 z->state = Z_NULL;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
73 Trace((stderr, "inflate: end\n"));
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
74 return Z_OK;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
75 }
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
76
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
77
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
78 int inflateInit2(z, w)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
79 z_stream *z;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
80 int w;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
81 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
82 /* initialize state */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
83 if (z == Z_NULL)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
84 return Z_STREAM_ERROR;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
85 if (z->zalloc == Z_NULL) z->zalloc = zcalloc;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
86 if (z->zfree == Z_NULL) z->zfree = zcfree;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
87 if ((z->state = (struct internal_state *)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
88 ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
89 return Z_MEM_ERROR;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
90 z->state->blocks = Z_NULL;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
91
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
92 /* handle undocumented nowrap option (no zlib header or check) */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
93 z->state->nowrap = 0;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
94 if (w < 0)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
95 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
96 w = - w;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
97 z->state->nowrap = 1;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
98 }
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
99
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
100 /* set window size */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
101 if (w < 8 || w > 15)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
102 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
103 inflateEnd(z);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
104 return Z_STREAM_ERROR;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
105 }
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
106 z->state->wbits = (uInt)w;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
107
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
108 /* create inflate_blocks state */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
109 if ((z->state->blocks =
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
110 inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, 1 << w))
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
111 == Z_NULL)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
112 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
113 inflateEnd(z);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
114 return Z_MEM_ERROR;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
115 }
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
116 Trace((stderr, "inflate: allocated\n"));
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
117
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
118 /* reset state */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
119 inflateReset(z);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
120 return Z_OK;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
121 }
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
122
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
123
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
124 int inflateInit(z)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
125 z_stream *z;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
126 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
127 return inflateInit2(z, DEF_WBITS);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
128 }
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
129
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
130
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
131 #define NEEDBYTE {if(z->avail_in==0)return r;r=Z_OK;}
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
132 #define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
133
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
134 int inflate(z, f)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
135 z_stream *z;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
136 int f;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
137 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
138 int r = f; /* to avoid warning about unused f */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
139 uInt b;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
140
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
141 if (z == Z_NULL || z->next_in == Z_NULL)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
142 return Z_STREAM_ERROR;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
143 r = Z_BUF_ERROR;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
144 while (1) switch (z->state->mode)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
145 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
146 case METHOD:
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
147 NEEDBYTE
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
148 if (((z->state->sub.method = NEXTBYTE) & 0xf) != DEFLATED)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
149 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
150 z->state->mode = BAD;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
151 z->msg = "unknown compression method";
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
152 z->state->sub.marker = 5; /* can't try inflateSync */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
153 break;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
154 }
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
155 if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
156 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
157 z->state->mode = BAD;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
158 z->msg = "invalid window size";
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
159 z->state->sub.marker = 5; /* can't try inflateSync */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
160 break;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
161 }
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
162 z->state->mode = FLAG;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
163 case FLAG:
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
164 NEEDBYTE
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
165 if ((b = NEXTBYTE) & 0x20)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
166 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
167 z->state->mode = BAD;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
168 z->msg = "invalid reserved bit";
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
169 z->state->sub.marker = 5; /* can't try inflateSync */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
170 break;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
171 }
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
172 if (((z->state->sub.method << 8) + b) % 31)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
173 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
174 z->state->mode = BAD;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
175 z->msg = "incorrect header check";
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
176 z->state->sub.marker = 5; /* can't try inflateSync */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
177 break;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
178 }
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
179 Trace((stderr, "inflate: zlib header ok\n"));
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
180 z->state->mode = BLOCKS;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
181 case BLOCKS:
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
182 r = inflate_blocks(z->state->blocks, z, r);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
183 if (r == Z_DATA_ERROR)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
184 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
185 z->state->mode = BAD;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
186 z->state->sub.marker = 0; /* can try inflateSync */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
187 break;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
188 }
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
189 if (r != Z_STREAM_END)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
190 return r;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
191 r = Z_OK;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
192 inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
193 if (z->state->nowrap)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
194 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
195 z->state->mode = DONE;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
196 break;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
197 }
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
198 z->state->mode = CHECK4;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
199 case CHECK4:
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
200 NEEDBYTE
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
201 z->state->sub.check.need = (uLong)NEXTBYTE << 24;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
202 z->state->mode = CHECK3;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
203 case CHECK3:
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
204 NEEDBYTE
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
205 z->state->sub.check.need += (uLong)NEXTBYTE << 16;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
206 z->state->mode = CHECK2;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
207 case CHECK2:
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
208 NEEDBYTE
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
209 z->state->sub.check.need += (uLong)NEXTBYTE << 8;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
210 z->state->mode = CHECK1;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
211 case CHECK1:
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
212 NEEDBYTE
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
213 z->state->sub.check.need += (uLong)NEXTBYTE;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
214
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
215 if (z->state->sub.check.was != z->state->sub.check.need)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
216 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
217 z->state->mode = BAD;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
218 z->msg = "incorrect data check";
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
219 z->state->sub.marker = 5; /* can't try inflateSync */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
220 break;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
221 }
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
222 Trace((stderr, "inflate: zlib check ok\n"));
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
223 z->state->mode = DONE;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
224 case DONE:
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
225 return Z_STREAM_END;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
226 case BAD:
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
227 return Z_DATA_ERROR;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
228 default:
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
229 return Z_STREAM_ERROR;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
230 }
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
231 }
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
232
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
233
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
234 int inflateSync(z)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
235 z_stream *z;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
236 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
237 uInt n; /* number of bytes to look at */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
238 Byte *p; /* pointer to bytes */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
239 uInt m; /* number of marker bytes found in a row */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
240 uLong r, w; /* temporaries to save total_in and total_out */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
241
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
242 /* set up */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
243 if (z == Z_NULL || z->state == Z_NULL)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
244 return Z_STREAM_ERROR;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
245 if (z->state->mode != BAD)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
246 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
247 z->state->mode = BAD;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
248 z->state->sub.marker = 0;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
249 }
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
250 if ((n = z->avail_in) == 0)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
251 return Z_BUF_ERROR;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
252 p = z->next_in;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
253 m = z->state->sub.marker;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
254
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
255 /* search */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
256 while (n && m < 4)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
257 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
258 if (*p == (Byte)(m < 2 ? 0 : 0xff))
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
259 m++;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
260 else if (*p)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
261 m = 0;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
262 else
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
263 m = 4 - m;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
264 p++, n--;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
265 }
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
266
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
267 /* restore */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
268 z->total_in += p - z->next_in;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
269 z->next_in = p;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
270 z->avail_in = n;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
271 z->state->sub.marker = m;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
272
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
273 /* return no joy or set up to restart on a new block */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
274 if (m != 4)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
275 return Z_DATA_ERROR;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
276 r = z->total_in; w = z->total_out;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
277 inflateReset(z);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
278 z->total_in = r; z->total_out = w;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
279 z->state->mode = BLOCKS;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
280 return Z_OK;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
281 }