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