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