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