]> git.saurik.com Git - apple/xnu.git/blob - libkern/zlib/inffast.c
54f0ee81505d6dac8468eeda3772c08c80cdc8e8
[apple/xnu.git] / libkern / zlib / inffast.c
1 /*
2 * Copyright (c) 2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /* inffast.c -- fast decoding
29 * Copyright (C) 1995-2004 Mark Adler
30 * For conditions of distribution and use, see copyright notice in zlib.h
31 */
32
33
34 #if defined _ARM_ARCH_6
35
36 // dummy definition, for armv6 or above, compile code from inffastS.s
37 typedef char DummyDefinition;
38
39 #else // architecture
40
41 #include "zutil.h"
42 #include "inftrees.h"
43 #include "inflate.h"
44 #include "inffast.h"
45
46 #ifndef ASMINF
47
48 /* Allow machine dependent optimization for post-increment or pre-increment.
49 Based on testing to date,
50 Pre-increment preferred for:
51 - PowerPC G3 (Adler)
52 - MIPS R5000 (Randers-Pehrson)
53 Post-increment preferred for:
54 - none
55 No measurable difference:
56 - Pentium III (Anderson)
57 - M68060 (Nikl)
58 */
59 #ifdef POSTINC
60 # define OFF 0
61 # define PUP(a) *(a)++
62 #else
63 # define OFF 1
64 # define PUP(a) *++(a)
65 #endif
66
67 /*
68 Decode literal, length, and distance codes and write out the resulting
69 literal and match bytes until either not enough input or output is
70 available, an end-of-block is encountered, or a data error is encountered.
71 When large enough input and output buffers are supplied to inflate(), for
72 example, a 16K input buffer and a 64K output buffer, more than 95% of the
73 inflate execution time is spent in this routine.
74
75 Entry assumptions:
76
77 state->mode == LEN
78 strm->avail_in >= 6
79 strm->avail_out >= 258
80 start >= strm->avail_out
81 state->bits < 8
82
83 On return, state->mode is one of:
84
85 LEN -- ran out of enough output space or enough available input
86 TYPE -- reached end of block code, inflate() to interpret next block
87 BAD -- error in block data
88
89 Notes:
90
91 - The maximum input bits used by a length/distance pair is 15 bits for the
92 length code, 5 bits for the length extra, 15 bits for the distance code,
93 and 13 bits for the distance extra. This totals 48 bits, or six bytes.
94 Therefore if strm->avail_in >= 6, then there is enough input to avoid
95 checking for available input while decoding.
96
97 - The maximum bytes that a single length/distance pair can output is 258
98 bytes, which is the maximum length that can be coded. inflate_fast()
99 requires strm->avail_out >= 258 for each loop to avoid checking for
100 output space.
101 */
102 void inflate_fast(strm, start)
103 z_streamp strm;
104 unsigned start; /* inflate()'s starting value for strm->avail_out */
105 {
106 struct inflate_state FAR *state;
107 unsigned char FAR *in; /* local strm->next_in */
108 unsigned char FAR *last; /* while in < last, enough input available */
109 unsigned char FAR *out; /* local strm->next_out */
110 unsigned char FAR *beg; /* inflate()'s initial strm->next_out */
111 unsigned char FAR *end; /* while out < end, enough space available */
112 #ifdef INFLATE_STRICT
113 unsigned dmax; /* maximum distance from zlib header */
114 #endif
115 unsigned wsize; /* window size or zero if not using window */
116 unsigned whave; /* valid bytes in the window */
117 unsigned write; /* window write index */
118 unsigned char FAR *window; /* allocated sliding window, if wsize != 0 */
119 unsigned long hold; /* local strm->hold */
120 unsigned bits; /* local strm->bits */
121 code const FAR *lcode; /* local strm->lencode */
122 code const FAR *dcode; /* local strm->distcode */
123 unsigned lmask; /* mask for first level of length codes */
124 unsigned dmask; /* mask for first level of distance codes */
125 code this; /* retrieved table entry */
126 unsigned op; /* code bits, operation, extra bits, or */
127 /* window position, window bytes to copy */
128 unsigned len; /* match length, unused bytes */
129 unsigned dist; /* match distance */
130 unsigned char FAR *from; /* where to copy match from */
131
132 /* copy state to local variables */
133 state = (struct inflate_state FAR *)strm->state;
134 in = strm->next_in - OFF;
135 last = in + (strm->avail_in - 5);
136 out = strm->next_out - OFF;
137 beg = out - (start - strm->avail_out);
138 end = out + (strm->avail_out - 257);
139 #ifdef INFLATE_STRICT
140 dmax = state->dmax;
141 #endif
142 wsize = state->wsize;
143 whave = state->whave;
144 write = state->write;
145 window = state->window;
146 hold = state->hold;
147 bits = state->bits;
148 lcode = state->lencode;
149 dcode = state->distcode;
150 lmask = (1U << state->lenbits) - 1;
151 dmask = (1U << state->distbits) - 1;
152
153 /* decode literals and length/distances until end-of-block or not enough
154 input data or output space */
155 do {
156 if (bits < 15) {
157 hold += (unsigned long)(PUP(in)) << bits;
158 bits += 8;
159 hold += (unsigned long)(PUP(in)) << bits;
160 bits += 8;
161 }
162 this = lcode[hold & lmask];
163 dolen:
164 op = (unsigned)(this.bits);
165 hold >>= op;
166 bits -= op;
167 op = (unsigned)(this.op);
168 if (op == 0) { /* literal */
169 Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
170 "inflate: literal '%c'\n" :
171 "inflate: literal 0x%02x\n", this.val));
172 PUP(out) = (unsigned char)(this.val);
173 }
174 else if (op & 16) { /* length base */
175 len = (unsigned)(this.val);
176 op &= 15; /* number of extra bits */
177 if (op) {
178 if (bits < op) {
179 hold += (unsigned long)(PUP(in)) << bits;
180 bits += 8;
181 }
182 len += (unsigned)hold & ((1U << op) - 1);
183 hold >>= op;
184 bits -= op;
185 }
186 Tracevv((stderr, "inflate: length %u\n", len));
187 if (bits < 15) {
188 hold += (unsigned long)(PUP(in)) << bits;
189 bits += 8;
190 hold += (unsigned long)(PUP(in)) << bits;
191 bits += 8;
192 }
193 this = dcode[hold & dmask];
194 dodist:
195 op = (unsigned)(this.bits);
196 hold >>= op;
197 bits -= op;
198 op = (unsigned)(this.op);
199 if (op & 16) { /* distance base */
200 dist = (unsigned)(this.val);
201 op &= 15; /* number of extra bits */
202 if (bits < op) {
203 hold += (unsigned long)(PUP(in)) << bits;
204 bits += 8;
205 if (bits < op) {
206 hold += (unsigned long)(PUP(in)) << bits;
207 bits += 8;
208 }
209 }
210 dist += (unsigned)hold & ((1U << op) - 1);
211 #ifdef INFLATE_STRICT
212 if (dist > dmax) {
213 strm->msg = (char *)"invalid distance too far back";
214 state->mode = BAD;
215 break;
216 }
217 #endif
218 hold >>= op;
219 bits -= op;
220 Tracevv((stderr, "inflate: distance %u\n", dist));
221 op = (unsigned)(out - beg); /* max distance in output */
222 if (dist > op) { /* see if copy from window */
223 op = dist - op; /* distance back in window */
224 if (op > whave) {
225 strm->msg = (char *)"invalid distance too far back";
226 state->mode = BAD;
227 break;
228 }
229 from = window - OFF;
230 if (write == 0) { /* very common case */
231 from += wsize - 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 else if (write < op) { /* wrap around window */
241 from += wsize + write - op;
242 op -= write;
243 if (op < len) { /* some from end of window */
244 len -= op;
245 do {
246 PUP(out) = PUP(from);
247 } while (--op);
248 from = window - OFF;
249 if (write < len) { /* some from start of window */
250 op = write;
251 len -= op;
252 do {
253 PUP(out) = PUP(from);
254 } while (--op);
255 from = out - dist; /* rest from output */
256 }
257 }
258 }
259 else { /* contiguous in window */
260 from += write - op;
261 if (op < len) { /* some from window */
262 len -= op;
263 do {
264 PUP(out) = PUP(from);
265 } while (--op);
266 from = out - dist; /* rest from output */
267 }
268 }
269 while (len > 2) {
270 PUP(out) = PUP(from);
271 PUP(out) = PUP(from);
272 PUP(out) = PUP(from);
273 len -= 3;
274 }
275 if (len) {
276 PUP(out) = PUP(from);
277 if (len > 1)
278 PUP(out) = PUP(from);
279 }
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 }
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 */
354
355 #endif // architecture