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