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>
18 /* Endian independent version */
19 static inline unsigned short
20 get_unaligned16(const unsigned short *p)
23 unsigned char *b = (unsigned char *)p;
31 Decode literal, length, and distance codes and write out the resulting
32 literal and match bytes until either not enough input or output is
33 available, an end-of-block is encountered, or a data error is encountered.
34 When large enough input and output buffers are supplied to inflate(), for
35 example, a 16K input buffer and a 64K output buffer, more than 95% of the
36 inflate execution time is spent in this routine.
42 strm->avail_out >= 258
43 start >= strm->avail_out
46 On return, state->mode is one of:
48 LEN -- ran out of enough output space or enough available input
49 TYPE -- reached end of block code, inflate() to interpret next block
50 BAD -- error in block data
54 - The maximum input bits used by a length/distance pair is 15 bits for the
55 length code, 5 bits for the length extra, 15 bits for the distance code,
56 and 13 bits for the distance extra. This totals 48 bits, or six bytes.
57 Therefore if strm->avail_in >= 6, then there is enough input to avoid
58 checking for available input while decoding.
60 - The maximum bytes that a single length/distance pair can output is 258
61 bytes, which is the maximum length that can be coded. inflate_fast()
62 requires strm->avail_out >= 258 for each loop to avoid checking for
65 - @start: inflate()'s starting value for strm->avail_out
67 void inflate_fast(z_streamp strm, unsigned start)
69 struct inflate_state *state;
70 const unsigned char *in; /* local strm->next_in */
71 const unsigned char *last; /* while in < last, enough input available */
72 unsigned char *out; /* local strm->next_out */
73 unsigned char *beg; /* inflate()'s initial strm->next_out */
74 unsigned char *end; /* while out < end, enough space available */
76 unsigned dmax; /* maximum distance from zlib header */
78 unsigned wsize; /* window size or zero if not using window */
79 unsigned whave; /* valid bytes in the window */
80 unsigned write; /* window write index */
81 unsigned char *window; /* allocated sliding window, if wsize != 0 */
82 unsigned long hold; /* local strm->hold */
83 unsigned bits; /* local strm->bits */
84 code const *lcode; /* local strm->lencode */
85 code const *dcode; /* local strm->distcode */
86 unsigned lmask; /* mask for first level of length codes */
87 unsigned dmask; /* mask for first level of distance codes */
88 code this; /* retrieved table entry */
89 unsigned op; /* code bits, operation, extra bits, or */
90 /* window position, window bytes to copy */
91 unsigned len; /* match length, unused bytes */
92 unsigned dist; /* match distance */
93 unsigned char *from; /* where to copy match from */
95 /* copy state to local variables */
96 state = (struct inflate_state *)strm->state;
98 last = in + (strm->avail_in - 5);
100 beg = out - (start - strm->avail_out);
101 end = out + (strm->avail_out - 257);
102 #ifdef INFLATE_STRICT
105 wsize = state->wsize;
106 whave = state->whave;
107 write = state->write;
108 window = state->window;
111 lcode = state->lencode;
112 dcode = state->distcode;
113 lmask = (1U << state->lenbits) - 1;
114 dmask = (1U << state->distbits) - 1;
116 /* decode literals and length/distances until end-of-block or not enough
117 input data or output space */
120 hold += (unsigned long)(*in++) << bits;
122 hold += (unsigned long)(*in++) << bits;
125 this = lcode[hold & lmask];
127 op = (unsigned)(this.bits);
130 op = (unsigned)(this.op);
131 if (op == 0) { /* literal */
132 *out++ = (unsigned char)(this.val);
134 else if (op & 16) { /* length base */
135 len = (unsigned)(this.val);
136 op &= 15; /* number of extra bits */
139 hold += (unsigned long)(*in++) << bits;
142 len += (unsigned)hold & ((1U << op) - 1);
147 hold += (unsigned long)(*in++) << bits;
149 hold += (unsigned long)(*in++) << bits;
152 this = dcode[hold & dmask];
154 op = (unsigned)(this.bits);
157 op = (unsigned)(this.op);
158 if (op & 16) { /* distance base */
159 dist = (unsigned)(this.val);
160 op &= 15; /* number of extra bits */
162 hold += (unsigned long)(*in++) << bits;
165 hold += (unsigned long)(*in++) << bits;
169 dist += (unsigned)hold & ((1U << op) - 1);
170 #ifdef INFLATE_STRICT
172 strm->msg = (char *)"invalid distance too far back";
179 op = (unsigned)(out - beg); /* max distance in output */
180 if (dist > op) { /* see if copy from window */
181 op = dist - op; /* distance back in window */
183 strm->msg = (char *)"invalid distance too far back";
188 if (write == 0) { /* very common case */
190 if (op < len) { /* some from window */
195 from = out - dist; /* rest from output */
198 else if (write < op) { /* wrap around window */
199 from += wsize + write - op;
201 if (op < len) { /* some from end of window */
207 if (write < len) { /* some from start of window */
213 from = out - dist; /* rest from output */
217 else { /* contiguous in window */
219 if (op < len) { /* some from window */
224 from = out - dist; /* rest from output */
240 unsigned short *sout;
243 from = out - dist; /* copy direct from output */
244 /* minimum length is three */
246 if (!((long)(out - 1) & 1)) {
250 sout = (unsigned short *)(out);
252 unsigned short *sfrom;
254 sfrom = (unsigned short *)(from);
257 if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
260 *sout++ = get_unaligned16(sfrom++);
262 out = (unsigned char *)sout;
263 from = (unsigned char *)sfrom;
264 } else { /* dist == 1 or dist == 2 */
265 unsigned short pat16;
270 /* copy one char pattern to both bytes */
279 out = (unsigned char *)sout;
285 else if ((op & 64) == 0) { /* 2nd level distance code */
286 this = dcode[this.val + (hold & ((1U << op) - 1))];
290 strm->msg = (char *)"invalid distance code";
295 else if ((op & 64) == 0) { /* 2nd level length code */
296 this = lcode[this.val + (hold & ((1U << op) - 1))];
299 else if (op & 32) { /* end-of-block */
304 strm->msg = (char *)"invalid literal/length code";
308 } while (in < last && out < end);
310 /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
314 hold &= (1U << bits) - 1;
316 /* update state and return */
318 strm->next_out = out;
319 strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
320 strm->avail_out = (unsigned)(out < end ?
321 257 + (end - out) : 257 - (out - end));
328 inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
329 - Using bit fields for code structure
330 - Different op definition to avoid & for extra bits (do & for table bits)
331 - Three separate decoding do-loops for direct, window, and write == 0
332 - Special case for distance > 1 copies to do overlapped load and store copy
333 - Explicit branch predictions (based on measured branch probabilities)
334 - Deferring match copy and interspersed it with decoding subsequent codes
335 - Swapping literal/length else
336 - Swapping window/direct else
337 - Larger unrolled copy loops (three is about right)
338 - Moving len -= 3 statement into middle of loop