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