rev |
line source |
nuclear@0
|
1 /* inffast.c -- fast decoding
|
nuclear@0
|
2 * Copyright (C) 1995-2004 Mark Adler
|
nuclear@0
|
3 * For conditions of distribution and use, see copyright notice in zlib.h
|
nuclear@0
|
4 */
|
nuclear@0
|
5
|
nuclear@0
|
6 #include "zutil.h"
|
nuclear@0
|
7 #include "inftrees.h"
|
nuclear@0
|
8 #include "inflate.h"
|
nuclear@0
|
9 #include "inffast.h"
|
nuclear@0
|
10
|
nuclear@0
|
11 #ifndef ASMINF
|
nuclear@0
|
12
|
nuclear@0
|
13 /* Allow machine dependent optimization for post-increment or pre-increment.
|
nuclear@0
|
14 Based on testing to date,
|
nuclear@0
|
15 Pre-increment preferred for:
|
nuclear@0
|
16 - PowerPC G3 (Adler)
|
nuclear@0
|
17 - MIPS R5000 (Randers-Pehrson)
|
nuclear@0
|
18 Post-increment preferred for:
|
nuclear@0
|
19 - none
|
nuclear@0
|
20 No measurable difference:
|
nuclear@0
|
21 - Pentium III (Anderson)
|
nuclear@0
|
22 - M68060 (Nikl)
|
nuclear@0
|
23 */
|
nuclear@0
|
24 #ifdef POSTINC
|
nuclear@0
|
25 # define OFF 0
|
nuclear@0
|
26 # define PUP(a) *(a)++
|
nuclear@0
|
27 #else
|
nuclear@0
|
28 # define OFF 1
|
nuclear@0
|
29 # define PUP(a) *++(a)
|
nuclear@0
|
30 #endif
|
nuclear@0
|
31
|
nuclear@0
|
32 /*
|
nuclear@0
|
33 Decode literal, length, and distance codes and write out the resulting
|
nuclear@0
|
34 literal and match bytes until either not enough input or output is
|
nuclear@0
|
35 available, an end-of-block is encountered, or a data error is encountered.
|
nuclear@0
|
36 When large enough input and output buffers are supplied to inflate(), for
|
nuclear@0
|
37 example, a 16K input buffer and a 64K output buffer, more than 95% of the
|
nuclear@0
|
38 inflate execution time is spent in this routine.
|
nuclear@0
|
39
|
nuclear@0
|
40 Entry assumptions:
|
nuclear@0
|
41
|
nuclear@0
|
42 state->mode == LEN
|
nuclear@0
|
43 strm->avail_in >= 6
|
nuclear@0
|
44 strm->avail_out >= 258
|
nuclear@0
|
45 start >= strm->avail_out
|
nuclear@0
|
46 state->bits < 8
|
nuclear@0
|
47
|
nuclear@0
|
48 On return, state->mode is one of:
|
nuclear@0
|
49
|
nuclear@0
|
50 LEN -- ran out of enough output space or enough available input
|
nuclear@0
|
51 TYPE -- reached end of block code, inflate() to interpret next block
|
nuclear@0
|
52 BAD -- error in block data
|
nuclear@0
|
53
|
nuclear@0
|
54 Notes:
|
nuclear@0
|
55
|
nuclear@0
|
56 - The maximum input bits used by a length/distance pair is 15 bits for the
|
nuclear@0
|
57 length code, 5 bits for the length extra, 15 bits for the distance code,
|
nuclear@0
|
58 and 13 bits for the distance extra. This totals 48 bits, or six bytes.
|
nuclear@0
|
59 Therefore if strm->avail_in >= 6, then there is enough input to avoid
|
nuclear@0
|
60 checking for available input while decoding.
|
nuclear@0
|
61
|
nuclear@0
|
62 - The maximum bytes that a single length/distance pair can output is 258
|
nuclear@0
|
63 bytes, which is the maximum length that can be coded. inflate_fast()
|
nuclear@0
|
64 requires strm->avail_out >= 258 for each loop to avoid checking for
|
nuclear@0
|
65 output space.
|
nuclear@0
|
66 */
|
nuclear@0
|
67 void inflate_fast(strm, start)
|
nuclear@0
|
68 z_streamp strm;
|
nuclear@0
|
69 unsigned start; /* inflate()'s starting value for strm->avail_out */
|
nuclear@0
|
70 {
|
nuclear@0
|
71 struct inflate_state FAR *state;
|
nuclear@0
|
72 unsigned char FAR *in; /* local strm->next_in */
|
nuclear@0
|
73 unsigned char FAR *last; /* while in < last, enough input available */
|
nuclear@0
|
74 unsigned char FAR *out; /* local strm->next_out */
|
nuclear@0
|
75 unsigned char FAR *beg; /* inflate()'s initial strm->next_out */
|
nuclear@0
|
76 unsigned char FAR *end; /* while out < end, enough space available */
|
nuclear@0
|
77 #ifdef INFLATE_STRICT
|
nuclear@0
|
78 unsigned dmax; /* maximum distance from zlib header */
|
nuclear@0
|
79 #endif
|
nuclear@0
|
80 unsigned wsize; /* window size or zero if not using window */
|
nuclear@0
|
81 unsigned whave; /* valid bytes in the window */
|
nuclear@0
|
82 unsigned write; /* window write index */
|
nuclear@0
|
83 unsigned char FAR *window; /* allocated sliding window, if wsize != 0 */
|
nuclear@0
|
84 unsigned long hold; /* local strm->hold */
|
nuclear@0
|
85 unsigned bits; /* local strm->bits */
|
nuclear@0
|
86 code const FAR *lcode; /* local strm->lencode */
|
nuclear@0
|
87 code const FAR *dcode; /* local strm->distcode */
|
nuclear@0
|
88 unsigned lmask; /* mask for first level of length codes */
|
nuclear@0
|
89 unsigned dmask; /* mask for first level of distance codes */
|
nuclear@0
|
90 code this; /* retrieved table entry */
|
nuclear@0
|
91 unsigned op; /* code bits, operation, extra bits, or */
|
nuclear@0
|
92 /* window position, window bytes to copy */
|
nuclear@0
|
93 unsigned len; /* match length, unused bytes */
|
nuclear@0
|
94 unsigned dist; /* match distance */
|
nuclear@0
|
95 unsigned char FAR *from; /* where to copy match from */
|
nuclear@0
|
96
|
nuclear@0
|
97 /* copy state to local variables */
|
nuclear@0
|
98 state = (struct inflate_state FAR *)strm->state;
|
nuclear@0
|
99 in = strm->next_in - OFF;
|
nuclear@0
|
100 last = in + (strm->avail_in - 5);
|
nuclear@0
|
101 out = strm->next_out - OFF;
|
nuclear@0
|
102 beg = out - (start - strm->avail_out);
|
nuclear@0
|
103 end = out + (strm->avail_out - 257);
|
nuclear@0
|
104 #ifdef INFLATE_STRICT
|
nuclear@0
|
105 dmax = state->dmax;
|
nuclear@0
|
106 #endif
|
nuclear@0
|
107 wsize = state->wsize;
|
nuclear@0
|
108 whave = state->whave;
|
nuclear@0
|
109 write = state->write;
|
nuclear@0
|
110 window = state->window;
|
nuclear@0
|
111 hold = state->hold;
|
nuclear@0
|
112 bits = state->bits;
|
nuclear@0
|
113 lcode = state->lencode;
|
nuclear@0
|
114 dcode = state->distcode;
|
nuclear@0
|
115 lmask = (1U << state->lenbits) - 1;
|
nuclear@0
|
116 dmask = (1U << state->distbits) - 1;
|
nuclear@0
|
117
|
nuclear@0
|
118 /* decode literals and length/distances until end-of-block or not enough
|
nuclear@0
|
119 input data or output space */
|
nuclear@0
|
120 do {
|
nuclear@0
|
121 if (bits < 15) {
|
nuclear@0
|
122 hold += (unsigned long)(PUP(in)) << bits;
|
nuclear@0
|
123 bits += 8;
|
nuclear@0
|
124 hold += (unsigned long)(PUP(in)) << bits;
|
nuclear@0
|
125 bits += 8;
|
nuclear@0
|
126 }
|
nuclear@0
|
127 this = lcode[hold & lmask];
|
nuclear@0
|
128 dolen:
|
nuclear@0
|
129 op = (unsigned)(this.bits);
|
nuclear@0
|
130 hold >>= op;
|
nuclear@0
|
131 bits -= op;
|
nuclear@0
|
132 op = (unsigned)(this.op);
|
nuclear@0
|
133 if (op == 0) { /* literal */
|
nuclear@0
|
134 Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
|
nuclear@0
|
135 "inflate: literal '%c'\n" :
|
nuclear@0
|
136 "inflate: literal 0x%02x\n", this.val));
|
nuclear@0
|
137 PUP(out) = (unsigned char)(this.val);
|
nuclear@0
|
138 }
|
nuclear@0
|
139 else if (op & 16) { /* length base */
|
nuclear@0
|
140 len = (unsigned)(this.val);
|
nuclear@0
|
141 op &= 15; /* number of extra bits */
|
nuclear@0
|
142 if (op) {
|
nuclear@0
|
143 if (bits < op) {
|
nuclear@0
|
144 hold += (unsigned long)(PUP(in)) << bits;
|
nuclear@0
|
145 bits += 8;
|
nuclear@0
|
146 }
|
nuclear@0
|
147 len += (unsigned)hold & ((1U << op) - 1);
|
nuclear@0
|
148 hold >>= op;
|
nuclear@0
|
149 bits -= op;
|
nuclear@0
|
150 }
|
nuclear@0
|
151 Tracevv((stderr, "inflate: length %u\n", len));
|
nuclear@0
|
152 if (bits < 15) {
|
nuclear@0
|
153 hold += (unsigned long)(PUP(in)) << bits;
|
nuclear@0
|
154 bits += 8;
|
nuclear@0
|
155 hold += (unsigned long)(PUP(in)) << bits;
|
nuclear@0
|
156 bits += 8;
|
nuclear@0
|
157 }
|
nuclear@0
|
158 this = dcode[hold & dmask];
|
nuclear@0
|
159 dodist:
|
nuclear@0
|
160 op = (unsigned)(this.bits);
|
nuclear@0
|
161 hold >>= op;
|
nuclear@0
|
162 bits -= op;
|
nuclear@0
|
163 op = (unsigned)(this.op);
|
nuclear@0
|
164 if (op & 16) { /* distance base */
|
nuclear@0
|
165 dist = (unsigned)(this.val);
|
nuclear@0
|
166 op &= 15; /* number of extra bits */
|
nuclear@0
|
167 if (bits < op) {
|
nuclear@0
|
168 hold += (unsigned long)(PUP(in)) << bits;
|
nuclear@0
|
169 bits += 8;
|
nuclear@0
|
170 if (bits < op) {
|
nuclear@0
|
171 hold += (unsigned long)(PUP(in)) << bits;
|
nuclear@0
|
172 bits += 8;
|
nuclear@0
|
173 }
|
nuclear@0
|
174 }
|
nuclear@0
|
175 dist += (unsigned)hold & ((1U << op) - 1);
|
nuclear@0
|
176 #ifdef INFLATE_STRICT
|
nuclear@0
|
177 if (dist > dmax) {
|
nuclear@0
|
178 strm->msg = (char *)"invalid distance too far back";
|
nuclear@0
|
179 state->mode = BAD;
|
nuclear@0
|
180 break;
|
nuclear@0
|
181 }
|
nuclear@0
|
182 #endif
|
nuclear@0
|
183 hold >>= op;
|
nuclear@0
|
184 bits -= op;
|
nuclear@0
|
185 Tracevv((stderr, "inflate: distance %u\n", dist));
|
nuclear@0
|
186 op = (unsigned)(out - beg); /* max distance in output */
|
nuclear@0
|
187 if (dist > op) { /* see if copy from window */
|
nuclear@0
|
188 op = dist - op; /* distance back in window */
|
nuclear@0
|
189 if (op > whave) {
|
nuclear@0
|
190 strm->msg = (char *)"invalid distance too far back";
|
nuclear@0
|
191 state->mode = BAD;
|
nuclear@0
|
192 break;
|
nuclear@0
|
193 }
|
nuclear@0
|
194 from = window - OFF;
|
nuclear@0
|
195 if (write == 0) { /* very common case */
|
nuclear@0
|
196 from += wsize - op;
|
nuclear@0
|
197 if (op < len) { /* some from window */
|
nuclear@0
|
198 len -= op;
|
nuclear@0
|
199 do {
|
nuclear@0
|
200 PUP(out) = PUP(from);
|
nuclear@0
|
201 } while (--op);
|
nuclear@0
|
202 from = out - dist; /* rest from output */
|
nuclear@0
|
203 }
|
nuclear@0
|
204 }
|
nuclear@0
|
205 else if (write < op) { /* wrap around window */
|
nuclear@0
|
206 from += wsize + write - op;
|
nuclear@0
|
207 op -= write;
|
nuclear@0
|
208 if (op < len) { /* some from end of window */
|
nuclear@0
|
209 len -= op;
|
nuclear@0
|
210 do {
|
nuclear@0
|
211 PUP(out) = PUP(from);
|
nuclear@0
|
212 } while (--op);
|
nuclear@0
|
213 from = window - OFF;
|
nuclear@0
|
214 if (write < len) { /* some from start of window */
|
nuclear@0
|
215 op = write;
|
nuclear@0
|
216 len -= op;
|
nuclear@0
|
217 do {
|
nuclear@0
|
218 PUP(out) = PUP(from);
|
nuclear@0
|
219 } while (--op);
|
nuclear@0
|
220 from = out - dist; /* rest from output */
|
nuclear@0
|
221 }
|
nuclear@0
|
222 }
|
nuclear@0
|
223 }
|
nuclear@0
|
224 else { /* contiguous in window */
|
nuclear@0
|
225 from += write - op;
|
nuclear@0
|
226 if (op < len) { /* some from window */
|
nuclear@0
|
227 len -= op;
|
nuclear@0
|
228 do {
|
nuclear@0
|
229 PUP(out) = PUP(from);
|
nuclear@0
|
230 } while (--op);
|
nuclear@0
|
231 from = out - dist; /* rest from output */
|
nuclear@0
|
232 }
|
nuclear@0
|
233 }
|
nuclear@0
|
234 while (len > 2) {
|
nuclear@0
|
235 PUP(out) = PUP(from);
|
nuclear@0
|
236 PUP(out) = PUP(from);
|
nuclear@0
|
237 PUP(out) = PUP(from);
|
nuclear@0
|
238 len -= 3;
|
nuclear@0
|
239 }
|
nuclear@0
|
240 if (len) {
|
nuclear@0
|
241 PUP(out) = PUP(from);
|
nuclear@0
|
242 if (len > 1)
|
nuclear@0
|
243 PUP(out) = PUP(from);
|
nuclear@0
|
244 }
|
nuclear@0
|
245 }
|
nuclear@0
|
246 else {
|
nuclear@0
|
247 from = out - dist; /* copy direct from output */
|
nuclear@0
|
248 do { /* minimum length is three */
|
nuclear@0
|
249 PUP(out) = PUP(from);
|
nuclear@0
|
250 PUP(out) = PUP(from);
|
nuclear@0
|
251 PUP(out) = PUP(from);
|
nuclear@0
|
252 len -= 3;
|
nuclear@0
|
253 } while (len > 2);
|
nuclear@0
|
254 if (len) {
|
nuclear@0
|
255 PUP(out) = PUP(from);
|
nuclear@0
|
256 if (len > 1)
|
nuclear@0
|
257 PUP(out) = PUP(from);
|
nuclear@0
|
258 }
|
nuclear@0
|
259 }
|
nuclear@0
|
260 }
|
nuclear@0
|
261 else if ((op & 64) == 0) { /* 2nd level distance code */
|
nuclear@0
|
262 this = dcode[this.val + (hold & ((1U << op) - 1))];
|
nuclear@0
|
263 goto dodist;
|
nuclear@0
|
264 }
|
nuclear@0
|
265 else {
|
nuclear@0
|
266 strm->msg = (char *)"invalid distance code";
|
nuclear@0
|
267 state->mode = BAD;
|
nuclear@0
|
268 break;
|
nuclear@0
|
269 }
|
nuclear@0
|
270 }
|
nuclear@0
|
271 else if ((op & 64) == 0) { /* 2nd level length code */
|
nuclear@0
|
272 this = lcode[this.val + (hold & ((1U << op) - 1))];
|
nuclear@0
|
273 goto dolen;
|
nuclear@0
|
274 }
|
nuclear@0
|
275 else if (op & 32) { /* end-of-block */
|
nuclear@0
|
276 Tracevv((stderr, "inflate: end of block\n"));
|
nuclear@0
|
277 state->mode = TYPE;
|
nuclear@0
|
278 break;
|
nuclear@0
|
279 }
|
nuclear@0
|
280 else {
|
nuclear@0
|
281 strm->msg = (char *)"invalid literal/length code";
|
nuclear@0
|
282 state->mode = BAD;
|
nuclear@0
|
283 break;
|
nuclear@0
|
284 }
|
nuclear@0
|
285 } while (in < last && out < end);
|
nuclear@0
|
286
|
nuclear@0
|
287 /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
|
nuclear@0
|
288 len = bits >> 3;
|
nuclear@0
|
289 in -= len;
|
nuclear@0
|
290 bits -= len << 3;
|
nuclear@0
|
291 hold &= (1U << bits) - 1;
|
nuclear@0
|
292
|
nuclear@0
|
293 /* update state and return */
|
nuclear@0
|
294 strm->next_in = in + OFF;
|
nuclear@0
|
295 strm->next_out = out + OFF;
|
nuclear@0
|
296 strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
|
nuclear@0
|
297 strm->avail_out = (unsigned)(out < end ?
|
nuclear@0
|
298 257 + (end - out) : 257 - (out - end));
|
nuclear@0
|
299 state->hold = hold;
|
nuclear@0
|
300 state->bits = bits;
|
nuclear@0
|
301 return;
|
nuclear@0
|
302 }
|
nuclear@0
|
303
|
nuclear@0
|
304 /*
|
nuclear@0
|
305 inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
|
nuclear@0
|
306 - Using bit fields for code structure
|
nuclear@0
|
307 - Different op definition to avoid & for extra bits (do & for table bits)
|
nuclear@0
|
308 - Three separate decoding do-loops for direct, window, and write == 0
|
nuclear@0
|
309 - Special case for distance > 1 copies to do overlapped load and store copy
|
nuclear@0
|
310 - Explicit branch predictions (based on measured branch probabilities)
|
nuclear@0
|
311 - Deferring match copy and interspersed it with decoding subsequent codes
|
nuclear@0
|
312 - Swapping literal/length else
|
nuclear@0
|
313 - Swapping window/direct else
|
nuclear@0
|
314 - Larger unrolled copy loops (three is about right)
|
nuclear@0
|
315 - Moving len -= 3 statement into middle of loop
|
nuclear@0
|
316 */
|
nuclear@0
|
317
|
nuclear@0
|
318 #endif /* !ASMINF */
|