1 /* inffast.c -- fast decoding
2 * Copyright (C) 1995-2004 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
6 #include <linux/zutil.h>
13 /* Allow machine dependent optimization for post-increment or pre-increment.
14 Based on testing to date,
15 Pre-increment preferred for:
17 - MIPS R5000 (Randers-Pehrson)
18 Post-increment preferred for:
20 No measurable difference:
21 - Pentium III (Anderson)
29 /* Endian independed version */
30 static inline unsigned short
31 get_unaligned16(const unsigned short *p
)
34 unsigned char *b
= (unsigned char *)p
;
43 # define PUP(a) *(a)++
44 # define UP_UNALIGNED(a) get_unaligned16((a)++)
47 # define PUP(a) *++(a)
48 # define UP_UNALIGNED(a) get_unaligned16(++(a))
52 Decode literal, length, and distance codes and write out the resulting
53 literal and match bytes until either not enough input or output is
54 available, an end-of-block is encountered, or a data error is encountered.
55 When large enough input and output buffers are supplied to inflate(), for
56 example, a 16K input buffer and a 64K output buffer, more than 95% of the
57 inflate execution time is spent in this routine.
63 strm->avail_out >= 258
64 start >= strm->avail_out
67 On return, state->mode is one of:
69 LEN -- ran out of enough output space or enough available input
70 TYPE -- reached end of block code, inflate() to interpret next block
71 BAD -- error in block data
75 - The maximum input bits used by a length/distance pair is 15 bits for the
76 length code, 5 bits for the length extra, 15 bits for the distance code,
77 and 13 bits for the distance extra. This totals 48 bits, or six bytes.
78 Therefore if strm->avail_in >= 6, then there is enough input to avoid
79 checking for available input while decoding.
81 - The maximum bytes that a single length/distance pair can output is 258
82 bytes, which is the maximum length that can be coded. inflate_fast()
83 requires strm->avail_out >= 258 for each loop to avoid checking for
86 - @start: inflate()'s starting value for strm->avail_out
88 void inflate_fast(z_streamp strm
, unsigned start
)
90 struct inflate_state
*state
;
91 const unsigned char *in
; /* local strm->next_in */
92 const unsigned char *last
; /* while in < last, enough input available */
93 unsigned char *out
; /* local strm->next_out */
94 unsigned char *beg
; /* inflate()'s initial strm->next_out */
95 unsigned char *end
; /* while out < end, enough space available */
97 unsigned dmax
; /* maximum distance from zlib header */
99 unsigned wsize
; /* window size or zero if not using window */
100 unsigned whave
; /* valid bytes in the window */
101 unsigned write
; /* window write index */
102 unsigned char *window
; /* allocated sliding window, if wsize != 0 */
103 unsigned long hold
; /* local strm->hold */
104 unsigned bits
; /* local strm->bits */
105 code
const *lcode
; /* local strm->lencode */
106 code
const *dcode
; /* local strm->distcode */
107 unsigned lmask
; /* mask for first level of length codes */
108 unsigned dmask
; /* mask for first level of distance codes */
109 code
this; /* retrieved table entry */
110 unsigned op
; /* code bits, operation, extra bits, or */
111 /* window position, window bytes to copy */
112 unsigned len
; /* match length, unused bytes */
113 unsigned dist
; /* match distance */
114 unsigned char *from
; /* where to copy match from */
116 /* copy state to local variables */
117 state
= (struct inflate_state
*)strm
->state
;
118 in
= strm
->next_in
- OFF
;
119 last
= in
+ (strm
->avail_in
- 5);
120 out
= strm
->next_out
- OFF
;
121 beg
= out
- (start
- strm
->avail_out
);
122 end
= out
+ (strm
->avail_out
- 257);
123 #ifdef INFLATE_STRICT
126 wsize
= state
->wsize
;
127 whave
= state
->whave
;
128 write
= state
->write
;
129 window
= state
->window
;
132 lcode
= state
->lencode
;
133 dcode
= state
->distcode
;
134 lmask
= (1U << state
->lenbits
) - 1;
135 dmask
= (1U << state
->distbits
) - 1;
137 /* decode literals and length/distances until end-of-block or not enough
138 input data or output space */
141 hold
+= (unsigned long)(PUP(in
)) << bits
;
143 hold
+= (unsigned long)(PUP(in
)) << bits
;
146 this = lcode
[hold
& lmask
];
148 op
= (unsigned)(this.bits
);
151 op
= (unsigned)(this.op
);
152 if (op
== 0) { /* literal */
153 PUP(out
) = (unsigned char)(this.val
);
155 else if (op
& 16) { /* length base */
156 len
= (unsigned)(this.val
);
157 op
&= 15; /* number of extra bits */
160 hold
+= (unsigned long)(PUP(in
)) << bits
;
163 len
+= (unsigned)hold
& ((1U << op
) - 1);
168 hold
+= (unsigned long)(PUP(in
)) << bits
;
170 hold
+= (unsigned long)(PUP(in
)) << bits
;
173 this = dcode
[hold
& dmask
];
175 op
= (unsigned)(this.bits
);
178 op
= (unsigned)(this.op
);
179 if (op
& 16) { /* distance base */
180 dist
= (unsigned)(this.val
);
181 op
&= 15; /* number of extra bits */
183 hold
+= (unsigned long)(PUP(in
)) << bits
;
186 hold
+= (unsigned long)(PUP(in
)) << bits
;
190 dist
+= (unsigned)hold
& ((1U << op
) - 1);
191 #ifdef INFLATE_STRICT
193 strm
->msg
= (char *)"invalid distance too far back";
200 op
= (unsigned)(out
- beg
); /* max distance in output */
201 if (dist
> op
) { /* see if copy from window */
202 op
= dist
- op
; /* distance back in window */
204 strm
->msg
= (char *)"invalid distance too far back";
209 if (write
== 0) { /* very common case */
211 if (op
< len
) { /* some from window */
214 PUP(out
) = PUP(from
);
216 from
= out
- dist
; /* rest from output */
219 else if (write
< op
) { /* wrap around window */
220 from
+= wsize
+ write
- op
;
222 if (op
< len
) { /* some from end of window */
225 PUP(out
) = PUP(from
);
228 if (write
< len
) { /* some from start of window */
232 PUP(out
) = PUP(from
);
234 from
= out
- dist
; /* rest from output */
238 else { /* contiguous in window */
240 if (op
< len
) { /* some from window */
243 PUP(out
) = PUP(from
);
245 from
= out
- dist
; /* rest from output */
249 PUP(out
) = PUP(from
);
250 PUP(out
) = PUP(from
);
251 PUP(out
) = PUP(from
);
255 PUP(out
) = PUP(from
);
257 PUP(out
) = PUP(from
);
261 unsigned short *sout
;
264 from
= out
- dist
; /* copy direct from output */
265 /* minimum length is three */
267 if (!((long)(out
- 1 + OFF
) & 1)) {
268 PUP(out
) = PUP(from
);
271 sout
= (unsigned short *)(out
- OFF
);
273 unsigned short *sfrom
;
275 sfrom
= (unsigned short *)(from
- OFF
);
278 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
279 PUP(sout
) = PUP(sfrom
);
281 PUP(sout
) = UP_UNALIGNED(sfrom
);
284 out
= (unsigned char *)sout
+ OFF
;
285 from
= (unsigned char *)sfrom
+ OFF
;
286 } else { /* dist == 1 or dist == 2 */
287 unsigned short pat16
;
289 pat16
= *(sout
-1+OFF
);
292 /* copy one char pattern to both bytes */
301 out
= (unsigned char *)sout
+ OFF
;
304 PUP(out
) = PUP(from
);
307 else if ((op
& 64) == 0) { /* 2nd level distance code */
308 this = dcode
[this.val
+ (hold
& ((1U << op
) - 1))];
312 strm
->msg
= (char *)"invalid distance code";
317 else if ((op
& 64) == 0) { /* 2nd level length code */
318 this = lcode
[this.val
+ (hold
& ((1U << op
) - 1))];
321 else if (op
& 32) { /* end-of-block */
326 strm
->msg
= (char *)"invalid literal/length code";
330 } while (in
< last
&& out
< end
);
332 /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
336 hold
&= (1U << bits
) - 1;
338 /* update state and return */
339 strm
->next_in
= in
+ OFF
;
340 strm
->next_out
= out
+ OFF
;
341 strm
->avail_in
= (unsigned)(in
< last
? 5 + (last
- in
) : 5 - (in
- last
));
342 strm
->avail_out
= (unsigned)(out
< end
?
343 257 + (end
- out
) : 257 - (out
- end
));
350 inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
351 - Using bit fields for code structure
352 - Different op definition to avoid & for extra bits (do & for table bits)
353 - Three separate decoding do-loops for direct, window, and write == 0
354 - Special case for distance > 1 copies to do overlapped load and store copy
355 - Explicit branch predictions (based on measured branch probabilities)
356 - Deferring match copy and interspersed it with decoding subsequent codes
357 - Swapping literal/length else
358 - Swapping window/direct else
359 - Larger unrolled copy loops (three is about right)
360 - Moving len -= 3 statement into middle of loop