]>
git.saurik.com Git - apple/xnu.git/blob - libkern/zlib/inffast.c
54f0ee81505d6dac8468eeda3772c08c80cdc8e8
2 * Copyright (c) 2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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
34 #if defined _ARM_ARCH_6
36 // dummy definition, for armv6 or above, compile code from inffastS.s
37 typedef char DummyDefinition
;
48 /* Allow machine dependent optimization for post-increment or pre-increment.
49 Based on testing to date,
50 Pre-increment preferred for:
52 - MIPS R5000 (Randers-Pehrson)
53 Post-increment preferred for:
55 No measurable difference:
56 - Pentium III (Anderson)
61 # define PUP(a) *(a)++
64 # define PUP(a) *++(a)
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.
79 strm->avail_out >= 258
80 start >= strm->avail_out
83 On return, state->mode is one of:
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
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.
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
102 void inflate_fast(strm
, start
)
104 unsigned start
; /* inflate()'s starting value for strm->avail_out */
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 */
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 */
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
142 wsize
= state
->wsize
;
143 whave
= state
->whave
;
144 write
= state
->write
;
145 window
= state
->window
;
148 lcode
= state
->lencode
;
149 dcode
= state
->distcode
;
150 lmask
= (1U << state
->lenbits
) - 1;
151 dmask
= (1U << state
->distbits
) - 1;
153 /* decode literals and length/distances until end-of-block or not enough
154 input data or output space */
157 hold
+= (unsigned long)(PUP(in
)) << bits
;
159 hold
+= (unsigned long)(PUP(in
)) << bits
;
162 this = lcode
[hold
& lmask
];
164 op
= (unsigned)(this.bits
);
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
);
174 else if (op
& 16) { /* length base */
175 len
= (unsigned)(this.val
);
176 op
&= 15; /* number of extra bits */
179 hold
+= (unsigned long)(PUP(in
)) << bits
;
182 len
+= (unsigned)hold
& ((1U << op
) - 1);
186 Tracevv((stderr
, "inflate: length %u\n", len
));
188 hold
+= (unsigned long)(PUP(in
)) << bits
;
190 hold
+= (unsigned long)(PUP(in
)) << bits
;
193 this = dcode
[hold
& dmask
];
195 op
= (unsigned)(this.bits
);
198 op
= (unsigned)(this.op
);
199 if (op
& 16) { /* distance base */
200 dist
= (unsigned)(this.val
);
201 op
&= 15; /* number of extra bits */
203 hold
+= (unsigned long)(PUP(in
)) << bits
;
206 hold
+= (unsigned long)(PUP(in
)) << bits
;
210 dist
+= (unsigned)hold
& ((1U << op
) - 1);
211 #ifdef INFLATE_STRICT
213 strm
->msg
= (char *)"invalid distance too far back";
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 */
225 strm
->msg
= (char *)"invalid distance too far back";
230 if (write
== 0) { /* very common case */
232 if (op
< len
) { /* some from window */
235 PUP(out
) = PUP(from
);
237 from
= out
- dist
; /* rest from output */
240 else if (write
< op
) { /* wrap around window */
241 from
+= wsize
+ write
- op
;
243 if (op
< len
) { /* some from end of window */
246 PUP(out
) = PUP(from
);
249 if (write
< len
) { /* some from start of window */
253 PUP(out
) = PUP(from
);
255 from
= out
- dist
; /* rest from output */
259 else { /* contiguous in window */
261 if (op
< len
) { /* some from window */
264 PUP(out
) = PUP(from
);
266 from
= out
- dist
; /* rest from output */
270 PUP(out
) = PUP(from
);
271 PUP(out
) = PUP(from
);
272 PUP(out
) = PUP(from
);
276 PUP(out
) = PUP(from
);
278 PUP(out
) = PUP(from
);
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
);
290 PUP(out
) = PUP(from
);
292 PUP(out
) = PUP(from
);
296 else if ((op
& 64) == 0) { /* 2nd level distance code */
297 this = dcode
[this.val
+ (hold
& ((1U << op
) - 1))];
301 strm
->msg
= (char *)"invalid distance code";
306 else if ((op
& 64) == 0) { /* 2nd level length code */
307 this = lcode
[this.val
+ (hold
& ((1U << op
) - 1))];
310 else if (op
& 32) { /* end-of-block */
311 Tracevv((stderr
, "inflate: end of block\n"));
316 strm
->msg
= (char *)"invalid literal/length code";
320 } while (in
< last
&& out
< end
);
322 /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
326 hold
&= (1U << bits
) - 1;
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
));
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
355 #endif // architecture