Mercurial > ~darius > hgwebdir.cgi > paradise_server
comparison zlib/infcodes.c @ 10:1040ca591f2e
First entry of Paradise Server 2.9 patch 10 Beta
author | darius |
---|---|
date | Sat, 06 Dec 1997 04:37:18 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
9:331055a97a9d | 10:1040ca591f2e |
---|---|
1 /* infcodes.c -- process literals and length/distance pairs | |
2 * Copyright (C) 1995 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 base more.Base | |
15 #define next more.Next | |
16 #define exop word.what.Exop | |
17 #define bits word.what.Bits | |
18 | |
19 /* inflate codes private state */ | |
20 struct inflate_codes_state { | |
21 | |
22 /* mode */ | |
23 enum { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */ | |
24 START, /* x: set up for LEN */ | |
25 LEN, /* i: get length/literal/eob next */ | |
26 LENEXT, /* i: getting length extra (have base) */ | |
27 DIST, /* i: get distance next */ | |
28 DISTEXT, /* i: getting distance extra */ | |
29 COPY, /* o: copying bytes in window, waiting for space */ | |
30 LIT, /* o: got literal, waiting for output space */ | |
31 WASH, /* o: got eob, possibly still output waiting */ | |
32 END, /* x: got eob and all data flushed */ | |
33 BADCODE} /* x: got error */ | |
34 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(bl, bd, tl, td, z) | |
60 uInt bl, bd; | |
61 inflate_huft *tl, *td; | |
62 z_stream *z; | |
63 { | |
64 inflate_codes_statef *c; | |
65 | |
66 if ((c = (inflate_codes_statef *) | |
67 ZALLOC(z,1,sizeof(struct inflate_codes_state))) != Z_NULL) | |
68 { | |
69 c->mode = START; | |
70 c->lbits = (Byte)bl; | |
71 c->dbits = (Byte)bd; | |
72 c->ltree = tl; | |
73 c->dtree = td; | |
74 Tracev((stderr, "inflate: codes new\n")); | |
75 } | |
76 return c; | |
77 } | |
78 | |
79 | |
80 int inflate_codes(s, z, r) | |
81 inflate_blocks_statef *s; | |
82 z_stream *z; | |
83 int r; | |
84 { | |
85 uInt j; /* temporary storage */ | |
86 inflate_huft *t; /* temporary pointer */ | |
87 uInt e; /* extra bits or operation */ | |
88 uLong b; /* bit buffer */ | |
89 uInt k; /* bits in bit buffer */ | |
90 Bytef *p; /* input data pointer */ | |
91 uInt n; /* bytes available there */ | |
92 Bytef *q; /* output window write pointer */ | |
93 uInt m; /* bytes to end of window or read pointer */ | |
94 Bytef *f; /* pointer to copy strings from */ | |
95 inflate_codes_statef *c = s->sub.decode.codes; /* codes state */ | |
96 | |
97 /* copy input/output information to locals (UPDATE macro restores) */ | |
98 LOAD | |
99 | |
100 /* process input and output based on current state */ | |
101 while (1) switch (c->mode) | |
102 { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */ | |
103 case START: /* x: set up for LEN */ | |
104 #ifndef SLOW | |
105 if (m >= 258 && n >= 10) | |
106 { | |
107 UPDATE | |
108 r = inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z); | |
109 LOAD | |
110 if (r != Z_OK) | |
111 { | |
112 c->mode = r == Z_STREAM_END ? WASH : BADCODE; | |
113 break; | |
114 } | |
115 } | |
116 #endif /* !SLOW */ | |
117 c->sub.code.need = c->lbits; | |
118 c->sub.code.tree = c->ltree; | |
119 c->mode = LEN; | |
120 case LEN: /* i: get length/literal/eob next */ | |
121 j = c->sub.code.need; | |
122 NEEDBITS(j) | |
123 t = c->sub.code.tree + ((uInt)b & inflate_mask[j]); | |
124 DUMPBITS(t->bits) | |
125 e = (uInt)(t->exop); | |
126 if (e == 0) /* literal */ | |
127 { | |
128 c->sub.lit = t->base; | |
129 Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ? | |
130 "inflate: literal '%c'\n" : | |
131 "inflate: literal 0x%02x\n", t->base)); | |
132 c->mode = LIT; | |
133 break; | |
134 } | |
135 if (e & 16) /* length */ | |
136 { | |
137 c->sub.copy.get = e & 15; | |
138 c->len = t->base; | |
139 c->mode = LENEXT; | |
140 break; | |
141 } | |
142 if ((e & 64) == 0) /* next table */ | |
143 { | |
144 c->sub.code.need = e; | |
145 c->sub.code.tree = t->next; | |
146 break; | |
147 } | |
148 if (e & 32) /* end of block */ | |
149 { | |
150 Tracevv((stderr, "inflate: end of block\n")); | |
151 c->mode = WASH; | |
152 break; | |
153 } | |
154 c->mode = BADCODE; /* invalid code */ | |
155 z->msg = "invalid literal/length code"; | |
156 r = Z_DATA_ERROR; | |
157 LEAVE | |
158 case LENEXT: /* i: getting length extra (have base) */ | |
159 j = c->sub.copy.get; | |
160 NEEDBITS(j) | |
161 c->len += (uInt)b & inflate_mask[j]; | |
162 DUMPBITS(j) | |
163 c->sub.code.need = c->dbits; | |
164 c->sub.code.tree = c->dtree; | |
165 Tracevv((stderr, "inflate: length %u\n", c->len)); | |
166 c->mode = DIST; | |
167 case DIST: /* i: get distance next */ | |
168 j = c->sub.code.need; | |
169 NEEDBITS(j) | |
170 t = c->sub.code.tree + ((uInt)b & inflate_mask[j]); | |
171 DUMPBITS(t->bits) | |
172 e = (uInt)(t->exop); | |
173 if (e & 16) /* distance */ | |
174 { | |
175 c->sub.copy.get = e & 15; | |
176 c->sub.copy.dist = t->base; | |
177 c->mode = DISTEXT; | |
178 break; | |
179 } | |
180 if ((e & 64) == 0) /* next table */ | |
181 { | |
182 c->sub.code.need = e; | |
183 c->sub.code.tree = t->next; | |
184 break; | |
185 } | |
186 c->mode = BADCODE; /* invalid code */ | |
187 z->msg = "invalid distance code"; | |
188 r = Z_DATA_ERROR; | |
189 LEAVE | |
190 case DISTEXT: /* i: getting distance extra */ | |
191 j = c->sub.copy.get; | |
192 NEEDBITS(j) | |
193 c->sub.copy.dist += (uInt)b & inflate_mask[j]; | |
194 DUMPBITS(j) | |
195 Tracevv((stderr, "inflate: distance %u\n", c->sub.copy.dist)); | |
196 c->mode = COPY; | |
197 case COPY: /* o: copying bytes in window, waiting for space */ | |
198 #ifndef __TURBOC__ /* Turbo C bug for following expression */ | |
199 f = (uInt)(q - s->window) < c->sub.copy.dist ? | |
200 s->end - (c->sub.copy.dist - (q - s->window)) : | |
201 q - c->sub.copy.dist; | |
202 #else | |
203 f = q - c->sub.copy.dist; | |
204 if ((uInt)(q - s->window) < c->sub.copy.dist) | |
205 f = s->end - (c->sub.copy.dist - (q - s->window)); | |
206 #endif | |
207 while (c->len) | |
208 { | |
209 NEEDOUT | |
210 OUTBYTE(*f++) | |
211 if (f == s->end) | |
212 f = s->window; | |
213 c->len--; | |
214 } | |
215 c->mode = START; | |
216 break; | |
217 case LIT: /* o: got literal, waiting for output space */ | |
218 NEEDOUT | |
219 OUTBYTE(c->sub.lit) | |
220 c->mode = START; | |
221 break; | |
222 case WASH: /* o: got eob, possibly more output */ | |
223 FLUSH | |
224 if (s->read != s->write) | |
225 LEAVE | |
226 c->mode = END; | |
227 case END: | |
228 r = Z_STREAM_END; | |
229 LEAVE | |
230 case BADCODE: /* x: got error */ | |
231 r = Z_DATA_ERROR; | |
232 LEAVE | |
233 default: | |
234 r = Z_STREAM_ERROR; | |
235 LEAVE | |
236 } | |
237 } | |
238 | |
239 | |
240 void inflate_codes_free(c, z) | |
241 inflate_codes_statef *c; | |
242 z_stream *z; | |
243 { | |
244 ZFREE(z, c); | |
245 Tracev((stderr, "inflate: codes free\n")); | |
246 } |