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