|
1 /* infcodes.c -- process literals and length/distance pairs |
|
2 * Copyright (C) 1995-1998 Mark Adler |
|
3 * For conditions of distribution and use, see copyright notice in zlib.h |
|
4 */ |
|
5 |
|
6 #include "zutil.h" |
|
7 #include "inftrees.h" |
|
8 #include "infblock.h" |
|
9 #include "infcodes.h" |
|
10 #include "infutil.h" |
|
11 #include "inffast.h" |
|
12 |
|
13 /* simplify the use of the inflate_huft type with some defines */ |
|
14 #define exop word.what.Exop |
|
15 #define bits word.what.Bits |
|
16 |
|
17 typedef enum { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */ |
|
18 START, /* x: set up for LEN */ |
|
19 LEN, /* i: get length/literal/eob next */ |
|
20 LENEXT, /* i: getting length extra (have base) */ |
|
21 DIST, /* i: get distance next */ |
|
22 DISTEXT, /* i: getting distance extra */ |
|
23 COPY, /* o: copying bytes in window, waiting for space */ |
|
24 LIT, /* o: got literal, waiting for output space */ |
|
25 WASH, /* o: got eob, possibly still output waiting */ |
|
26 END, /* x: got eob and all data flushed */ |
|
27 BADCODE} /* x: got error */ |
|
28 inflate_codes_mode; |
|
29 |
|
30 /* inflate codes private state */ |
|
31 struct inflate_codes_state { |
|
32 |
|
33 /* mode */ |
|
34 inflate_codes_mode mode; /* current inflate_codes mode */ |
|
35 |
|
36 /* mode dependent information */ |
|
37 uInt len; |
|
38 union { |
|
39 struct { |
|
40 inflate_huft *tree; /* pointer into tree */ |
|
41 uInt need; /* bits needed */ |
|
42 } code; /* if LEN or DIST, where in tree */ |
|
43 uInt lit; /* if LIT, literal */ |
|
44 struct { |
|
45 uInt get; /* bits to get for extra */ |
|
46 uInt dist; /* distance back to copy from */ |
|
47 } copy; /* if EXT or COPY, where and how much */ |
|
48 } sub; /* submode */ |
|
49 |
|
50 /* mode independent information */ |
|
51 Byte lbits; /* ltree bits decoded per branch */ |
|
52 Byte dbits; /* dtree bits decoder per branch */ |
|
53 inflate_huft *ltree; /* literal/length/eob tree */ |
|
54 inflate_huft *dtree; /* distance tree */ |
|
55 |
|
56 }; |
|
57 |
|
58 |
|
59 inflate_codes_statef *inflate_codes_new(uInt bl, uInt bd, inflate_huft *tl, inflate_huft *td, z_streamp z) |
|
60 /* inflate_huft *td need separate declaration for Borland C++ */ |
|
61 { |
|
62 inflate_codes_statef *c; |
|
63 |
|
64 if ((c = (inflate_codes_statef *) |
|
65 ZALLOC(z,1,sizeof(struct inflate_codes_state))) != Z_NULL) |
|
66 { |
|
67 c->mode = START; |
|
68 c->lbits = (Byte)bl; |
|
69 c->dbits = (Byte)bd; |
|
70 c->ltree = tl; |
|
71 c->dtree = td; |
|
72 Tracev((stderr, "inflate: codes new\n")); |
|
73 } |
|
74 return c; |
|
75 } |
|
76 |
|
77 |
|
78 int inflate_codes(inflate_blocks_statef *s,z_streamp z, int r) |
|
79 { |
|
80 uInt j; /* temporary storage */ |
|
81 inflate_huft *t; /* temporary pointer */ |
|
82 uInt e; /* extra bits or operation */ |
|
83 uLong b; /* bit buffer */ |
|
84 uInt k; /* bits in bit buffer */ |
|
85 Bytef *p; /* input data pointer */ |
|
86 uInt n; /* bytes available there */ |
|
87 Bytef *q; /* output window write pointer */ |
|
88 uInt m; /* bytes to end of window or read pointer */ |
|
89 Bytef *f; /* pointer to copy strings from */ |
|
90 inflate_codes_statef *c = s->sub.decode.codes; /* codes state */ |
|
91 |
|
92 /* copy input/output information to locals (UPDATE macro restores) */ |
|
93 LOAD |
|
94 |
|
95 /* process input and output based on current state */ |
|
96 while (1) switch (c->mode) |
|
97 { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */ |
|
98 case START: /* x: set up for LEN */ |
|
99 #ifndef SLOW |
|
100 if (m >= 258 && n >= 10) |
|
101 { |
|
102 UPDATE |
|
103 r = inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z); |
|
104 LOAD |
|
105 if (r != Z_OK) |
|
106 { |
|
107 c->mode = r == Z_STREAM_END ? WASH : BADCODE; |
|
108 break; |
|
109 } |
|
110 } |
|
111 #endif /* !SLOW */ |
|
112 c->sub.code.need = c->lbits; |
|
113 c->sub.code.tree = c->ltree; |
|
114 c->mode = LEN; |
|
115 case LEN: /* i: get length/literal/eob next */ |
|
116 j = c->sub.code.need; |
|
117 NEEDBITS(j) |
|
118 t = c->sub.code.tree + ((uInt)b & inflate_mask[j]); |
|
119 DUMPBITS(t->bits) |
|
120 e = (uInt)(t->exop); |
|
121 if (e == 0) /* literal */ |
|
122 { |
|
123 c->sub.lit = t->base; |
|
124 Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ? |
|
125 "inflate: literal '%c'\n" : |
|
126 "inflate: literal 0x%02x\n", t->base)); |
|
127 c->mode = LIT; |
|
128 break; |
|
129 } |
|
130 if (e & 16) /* length */ |
|
131 { |
|
132 c->sub.copy.get = e & 15; |
|
133 c->len = t->base; |
|
134 c->mode = LENEXT; |
|
135 break; |
|
136 } |
|
137 if ((e & 64) == 0) /* next table */ |
|
138 { |
|
139 c->sub.code.need = e; |
|
140 c->sub.code.tree = t + t->base; |
|
141 break; |
|
142 } |
|
143 if (e & 32) /* end of block */ |
|
144 { |
|
145 Tracevv((stderr, "inflate: end of block\n")); |
|
146 c->mode = WASH; |
|
147 break; |
|
148 } |
|
149 c->mode = BADCODE; /* invalid code */ |
|
150 z->msg = (char*)"invalid literal/length code"; |
|
151 r = Z_DATA_ERROR; |
|
152 LEAVE |
|
153 case LENEXT: /* i: getting length extra (have base) */ |
|
154 j = c->sub.copy.get; |
|
155 NEEDBITS(j) |
|
156 c->len += (uInt)b & inflate_mask[j]; |
|
157 DUMPBITS(j) |
|
158 c->sub.code.need = c->dbits; |
|
159 c->sub.code.tree = c->dtree; |
|
160 Tracevv((stderr, "inflate: length %u\n", c->len)); |
|
161 c->mode = DIST; |
|
162 case DIST: /* i: get distance next */ |
|
163 j = c->sub.code.need; |
|
164 NEEDBITS(j) |
|
165 t = c->sub.code.tree + ((uInt)b & inflate_mask[j]); |
|
166 DUMPBITS(t->bits) |
|
167 e = (uInt)(t->exop); |
|
168 if (e & 16) /* distance */ |
|
169 { |
|
170 c->sub.copy.get = e & 15; |
|
171 c->sub.copy.dist = t->base; |
|
172 c->mode = DISTEXT; |
|
173 break; |
|
174 } |
|
175 if ((e & 64) == 0) /* next table */ |
|
176 { |
|
177 c->sub.code.need = e; |
|
178 c->sub.code.tree = t + t->base; |
|
179 break; |
|
180 } |
|
181 c->mode = BADCODE; /* invalid code */ |
|
182 z->msg = (char*)"invalid distance code"; |
|
183 r = Z_DATA_ERROR; |
|
184 LEAVE |
|
185 case DISTEXT: /* i: getting distance extra */ |
|
186 j = c->sub.copy.get; |
|
187 NEEDBITS(j) |
|
188 c->sub.copy.dist += (uInt)b & inflate_mask[j]; |
|
189 DUMPBITS(j) |
|
190 Tracevv((stderr, "inflate: distance %u\n", c->sub.copy.dist)); |
|
191 c->mode = COPY; |
|
192 case COPY: /* o: copying bytes in window, waiting for space */ |
|
193 #ifndef __TURBOC__ /* Turbo C bug for following expression */ |
|
194 f = (uInt)(q - s->window) < c->sub.copy.dist ? |
|
195 s->end - (c->sub.copy.dist - (q - s->window)) : |
|
196 q - c->sub.copy.dist; |
|
197 #else |
|
198 f = q - c->sub.copy.dist; |
|
199 if ((uInt)(q - s->window) < c->sub.copy.dist) |
|
200 f = s->end - (c->sub.copy.dist - (uInt)(q - s->window)); |
|
201 #endif |
|
202 while (c->len) |
|
203 { |
|
204 NEEDOUT |
|
205 OUTBYTE(*f++) |
|
206 if (f == s->end) |
|
207 f = s->window; |
|
208 c->len--; |
|
209 } |
|
210 c->mode = START; |
|
211 break; |
|
212 case LIT: /* o: got literal, waiting for output space */ |
|
213 NEEDOUT |
|
214 OUTBYTE(c->sub.lit) |
|
215 c->mode = START; |
|
216 break; |
|
217 case WASH: /* o: got eob, possibly more output */ |
|
218 if (k > 7) /* return unused byte, if any */ |
|
219 { |
|
220 Assert(k < 16, "inflate_codes grabbed too many bytes") |
|
221 k -= 8; |
|
222 n++; |
|
223 p--; /* can always return one */ |
|
224 } |
|
225 FLUSH |
|
226 if (s->read != s->write) |
|
227 LEAVE |
|
228 c->mode = END; |
|
229 case END: |
|
230 r = Z_STREAM_END; |
|
231 LEAVE |
|
232 case BADCODE: /* x: got error */ |
|
233 r = Z_DATA_ERROR; |
|
234 LEAVE |
|
235 default: |
|
236 r = Z_STREAM_ERROR; |
|
237 LEAVE |
|
238 } |
|
239 #ifdef NEED_DUMMY_RETURN |
|
240 return Z_STREAM_ERROR; /* Some dumb compilers complain without this */ |
|
241 #endif |
|
242 } |
|
243 |
|
244 |
|
245 void inflate_codes_free(inflate_codes_statef *c, z_streamp z) |
|
246 { |
|
247 ZFREE(z, c); |
|
248 Tracev((stderr, "inflate: codes free\n")); |
|
249 } |