1275793eaSopenharmony_ci/* inffast.c -- fast decoding
2275793eaSopenharmony_ci * Copyright (C) 1995-2017 Mark Adler
3275793eaSopenharmony_ci * For conditions of distribution and use, see copyright notice in zlib.h
4275793eaSopenharmony_ci */
5275793eaSopenharmony_ci
6275793eaSopenharmony_ci#include "zutil.h"
7275793eaSopenharmony_ci#include "inftrees.h"
8275793eaSopenharmony_ci#include "inflate.h"
9275793eaSopenharmony_ci#include "inffast.h"
10275793eaSopenharmony_ci
11275793eaSopenharmony_ci#ifdef ASMINF
12275793eaSopenharmony_ci#  pragma message("Assembler code may have bugs -- use at your own risk")
13275793eaSopenharmony_ci#else
14275793eaSopenharmony_ci
15275793eaSopenharmony_ci/*
16275793eaSopenharmony_ci   Decode literal, length, and distance codes and write out the resulting
17275793eaSopenharmony_ci   literal and match bytes until either not enough input or output is
18275793eaSopenharmony_ci   available, an end-of-block is encountered, or a data error is encountered.
19275793eaSopenharmony_ci   When large enough input and output buffers are supplied to inflate(), for
20275793eaSopenharmony_ci   example, a 16K input buffer and a 64K output buffer, more than 95% of the
21275793eaSopenharmony_ci   inflate execution time is spent in this routine.
22275793eaSopenharmony_ci
23275793eaSopenharmony_ci   Entry assumptions:
24275793eaSopenharmony_ci
25275793eaSopenharmony_ci        state->mode == LEN
26275793eaSopenharmony_ci        strm->avail_in >= 6
27275793eaSopenharmony_ci        strm->avail_out >= 258
28275793eaSopenharmony_ci        start >= strm->avail_out
29275793eaSopenharmony_ci        state->bits < 8
30275793eaSopenharmony_ci
31275793eaSopenharmony_ci   On return, state->mode is one of:
32275793eaSopenharmony_ci
33275793eaSopenharmony_ci        LEN -- ran out of enough output space or enough available input
34275793eaSopenharmony_ci        TYPE -- reached end of block code, inflate() to interpret next block
35275793eaSopenharmony_ci        BAD -- error in block data
36275793eaSopenharmony_ci
37275793eaSopenharmony_ci   Notes:
38275793eaSopenharmony_ci
39275793eaSopenharmony_ci    - The maximum input bits used by a length/distance pair is 15 bits for the
40275793eaSopenharmony_ci      length code, 5 bits for the length extra, 15 bits for the distance code,
41275793eaSopenharmony_ci      and 13 bits for the distance extra.  This totals 48 bits, or six bytes.
42275793eaSopenharmony_ci      Therefore if strm->avail_in >= 6, then there is enough input to avoid
43275793eaSopenharmony_ci      checking for available input while decoding.
44275793eaSopenharmony_ci
45275793eaSopenharmony_ci    - The maximum bytes that a single length/distance pair can output is 258
46275793eaSopenharmony_ci      bytes, which is the maximum length that can be coded.  inflate_fast()
47275793eaSopenharmony_ci      requires strm->avail_out >= 258 for each loop to avoid checking for
48275793eaSopenharmony_ci      output space.
49275793eaSopenharmony_ci */
50275793eaSopenharmony_civoid ZLIB_INTERNAL inflate_fast(z_streamp strm, unsigned start) {
51275793eaSopenharmony_ci    struct inflate_state FAR *state;
52275793eaSopenharmony_ci    z_const unsigned char FAR *in;      /* local strm->next_in */
53275793eaSopenharmony_ci    z_const unsigned char FAR *last;    /* have enough input while in < last */
54275793eaSopenharmony_ci    unsigned char FAR *out;     /* local strm->next_out */
55275793eaSopenharmony_ci    unsigned char FAR *beg;     /* inflate()'s initial strm->next_out */
56275793eaSopenharmony_ci    unsigned char FAR *end;     /* while out < end, enough space available */
57275793eaSopenharmony_ci#ifdef INFLATE_STRICT
58275793eaSopenharmony_ci    unsigned dmax;              /* maximum distance from zlib header */
59275793eaSopenharmony_ci#endif
60275793eaSopenharmony_ci    unsigned wsize;             /* window size or zero if not using window */
61275793eaSopenharmony_ci    unsigned whave;             /* valid bytes in the window */
62275793eaSopenharmony_ci    unsigned wnext;             /* window write index */
63275793eaSopenharmony_ci    unsigned char FAR *window;  /* allocated sliding window, if wsize != 0 */
64275793eaSopenharmony_ci    unsigned long hold;         /* local strm->hold */
65275793eaSopenharmony_ci    unsigned bits;              /* local strm->bits */
66275793eaSopenharmony_ci    code const FAR *lcode;      /* local strm->lencode */
67275793eaSopenharmony_ci    code const FAR *dcode;      /* local strm->distcode */
68275793eaSopenharmony_ci    unsigned lmask;             /* mask for first level of length codes */
69275793eaSopenharmony_ci    unsigned dmask;             /* mask for first level of distance codes */
70275793eaSopenharmony_ci    code const *here;           /* retrieved table entry */
71275793eaSopenharmony_ci    unsigned op;                /* code bits, operation, extra bits, or */
72275793eaSopenharmony_ci                                /*  window position, window bytes to copy */
73275793eaSopenharmony_ci    unsigned len;               /* match length, unused bytes */
74275793eaSopenharmony_ci    unsigned dist;              /* match distance */
75275793eaSopenharmony_ci    unsigned char FAR *from;    /* where to copy match from */
76275793eaSopenharmony_ci
77275793eaSopenharmony_ci    /* copy state to local variables */
78275793eaSopenharmony_ci    state = (struct inflate_state FAR *)strm->state;
79275793eaSopenharmony_ci    in = strm->next_in;
80275793eaSopenharmony_ci    last = in + (strm->avail_in - 5);
81275793eaSopenharmony_ci    out = strm->next_out;
82275793eaSopenharmony_ci    beg = out - (start - strm->avail_out);
83275793eaSopenharmony_ci    end = out + (strm->avail_out - 257);
84275793eaSopenharmony_ci#ifdef INFLATE_STRICT
85275793eaSopenharmony_ci    dmax = state->dmax;
86275793eaSopenharmony_ci#endif
87275793eaSopenharmony_ci    wsize = state->wsize;
88275793eaSopenharmony_ci    whave = state->whave;
89275793eaSopenharmony_ci    wnext = state->wnext;
90275793eaSopenharmony_ci    window = state->window;
91275793eaSopenharmony_ci    hold = state->hold;
92275793eaSopenharmony_ci    bits = state->bits;
93275793eaSopenharmony_ci    lcode = state->lencode;
94275793eaSopenharmony_ci    dcode = state->distcode;
95275793eaSopenharmony_ci    lmask = (1U << state->lenbits) - 1;
96275793eaSopenharmony_ci    dmask = (1U << state->distbits) - 1;
97275793eaSopenharmony_ci
98275793eaSopenharmony_ci    /* decode literals and length/distances until end-of-block or not enough
99275793eaSopenharmony_ci       input data or output space */
100275793eaSopenharmony_ci    do {
101275793eaSopenharmony_ci        if (bits < 15) {
102275793eaSopenharmony_ci            hold += (unsigned long)(*in++) << bits;
103275793eaSopenharmony_ci            bits += 8;
104275793eaSopenharmony_ci            hold += (unsigned long)(*in++) << bits;
105275793eaSopenharmony_ci            bits += 8;
106275793eaSopenharmony_ci        }
107275793eaSopenharmony_ci        here = lcode + (hold & lmask);
108275793eaSopenharmony_ci      dolen:
109275793eaSopenharmony_ci        op = (unsigned)(here->bits);
110275793eaSopenharmony_ci        hold >>= op;
111275793eaSopenharmony_ci        bits -= op;
112275793eaSopenharmony_ci        op = (unsigned)(here->op);
113275793eaSopenharmony_ci        if (op == 0) {                          /* literal */
114275793eaSopenharmony_ci            Tracevv((stderr, here->val >= 0x20 && here->val < 0x7f ?
115275793eaSopenharmony_ci                    "inflate:         literal '%c'\n" :
116275793eaSopenharmony_ci                    "inflate:         literal 0x%02x\n", here->val));
117275793eaSopenharmony_ci            *out++ = (unsigned char)(here->val);
118275793eaSopenharmony_ci        }
119275793eaSopenharmony_ci        else if (op & 16) {                     /* length base */
120275793eaSopenharmony_ci            len = (unsigned)(here->val);
121275793eaSopenharmony_ci            op &= 15;                           /* number of extra bits */
122275793eaSopenharmony_ci            if (op) {
123275793eaSopenharmony_ci                if (bits < op) {
124275793eaSopenharmony_ci                    hold += (unsigned long)(*in++) << bits;
125275793eaSopenharmony_ci                    bits += 8;
126275793eaSopenharmony_ci                }
127275793eaSopenharmony_ci                len += (unsigned)hold & ((1U << op) - 1);
128275793eaSopenharmony_ci                hold >>= op;
129275793eaSopenharmony_ci                bits -= op;
130275793eaSopenharmony_ci            }
131275793eaSopenharmony_ci            Tracevv((stderr, "inflate:         length %u\n", len));
132275793eaSopenharmony_ci            if (bits < 15) {
133275793eaSopenharmony_ci                hold += (unsigned long)(*in++) << bits;
134275793eaSopenharmony_ci                bits += 8;
135275793eaSopenharmony_ci                hold += (unsigned long)(*in++) << bits;
136275793eaSopenharmony_ci                bits += 8;
137275793eaSopenharmony_ci            }
138275793eaSopenharmony_ci            here = dcode + (hold & dmask);
139275793eaSopenharmony_ci          dodist:
140275793eaSopenharmony_ci            op = (unsigned)(here->bits);
141275793eaSopenharmony_ci            hold >>= op;
142275793eaSopenharmony_ci            bits -= op;
143275793eaSopenharmony_ci            op = (unsigned)(here->op);
144275793eaSopenharmony_ci            if (op & 16) {                      /* distance base */
145275793eaSopenharmony_ci                dist = (unsigned)(here->val);
146275793eaSopenharmony_ci                op &= 15;                       /* number of extra bits */
147275793eaSopenharmony_ci                if (bits < op) {
148275793eaSopenharmony_ci                    hold += (unsigned long)(*in++) << bits;
149275793eaSopenharmony_ci                    bits += 8;
150275793eaSopenharmony_ci                    if (bits < op) {
151275793eaSopenharmony_ci                        hold += (unsigned long)(*in++) << bits;
152275793eaSopenharmony_ci                        bits += 8;
153275793eaSopenharmony_ci                    }
154275793eaSopenharmony_ci                }
155275793eaSopenharmony_ci                dist += (unsigned)hold & ((1U << op) - 1);
156275793eaSopenharmony_ci#ifdef INFLATE_STRICT
157275793eaSopenharmony_ci                if (dist > dmax) {
158275793eaSopenharmony_ci                    strm->msg = (char *)"invalid distance too far back";
159275793eaSopenharmony_ci                    state->mode = BAD;
160275793eaSopenharmony_ci                    break;
161275793eaSopenharmony_ci                }
162275793eaSopenharmony_ci#endif
163275793eaSopenharmony_ci                hold >>= op;
164275793eaSopenharmony_ci                bits -= op;
165275793eaSopenharmony_ci                Tracevv((stderr, "inflate:         distance %u\n", dist));
166275793eaSopenharmony_ci                op = (unsigned)(out - beg);     /* max distance in output */
167275793eaSopenharmony_ci                if (dist > op) {                /* see if copy from window */
168275793eaSopenharmony_ci                    op = dist - op;             /* distance back in window */
169275793eaSopenharmony_ci                    if (op > whave) {
170275793eaSopenharmony_ci                        if (state->sane) {
171275793eaSopenharmony_ci                            strm->msg =
172275793eaSopenharmony_ci                                (char *)"invalid distance too far back";
173275793eaSopenharmony_ci                            state->mode = BAD;
174275793eaSopenharmony_ci                            break;
175275793eaSopenharmony_ci                        }
176275793eaSopenharmony_ci#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
177275793eaSopenharmony_ci                        if (len <= op - whave) {
178275793eaSopenharmony_ci                            do {
179275793eaSopenharmony_ci                                *out++ = 0;
180275793eaSopenharmony_ci                            } while (--len);
181275793eaSopenharmony_ci                            continue;
182275793eaSopenharmony_ci                        }
183275793eaSopenharmony_ci                        len -= op - whave;
184275793eaSopenharmony_ci                        do {
185275793eaSopenharmony_ci                            *out++ = 0;
186275793eaSopenharmony_ci                        } while (--op > whave);
187275793eaSopenharmony_ci                        if (op == 0) {
188275793eaSopenharmony_ci                            from = out - dist;
189275793eaSopenharmony_ci                            do {
190275793eaSopenharmony_ci                                *out++ = *from++;
191275793eaSopenharmony_ci                            } while (--len);
192275793eaSopenharmony_ci                            continue;
193275793eaSopenharmony_ci                        }
194275793eaSopenharmony_ci#endif
195275793eaSopenharmony_ci                    }
196275793eaSopenharmony_ci                    from = window;
197275793eaSopenharmony_ci                    if (wnext == 0) {           /* very common case */
198275793eaSopenharmony_ci                        from += wsize - op;
199275793eaSopenharmony_ci                        if (op < len) {         /* some from window */
200275793eaSopenharmony_ci                            len -= op;
201275793eaSopenharmony_ci                            do {
202275793eaSopenharmony_ci                                *out++ = *from++;
203275793eaSopenharmony_ci                            } while (--op);
204275793eaSopenharmony_ci                            from = out - dist;  /* rest from output */
205275793eaSopenharmony_ci                        }
206275793eaSopenharmony_ci                    }
207275793eaSopenharmony_ci                    else if (wnext < op) {      /* wrap around window */
208275793eaSopenharmony_ci                        from += wsize + wnext - op;
209275793eaSopenharmony_ci                        op -= wnext;
210275793eaSopenharmony_ci                        if (op < len) {         /* some from end of window */
211275793eaSopenharmony_ci                            len -= op;
212275793eaSopenharmony_ci                            do {
213275793eaSopenharmony_ci                                *out++ = *from++;
214275793eaSopenharmony_ci                            } while (--op);
215275793eaSopenharmony_ci                            from = window;
216275793eaSopenharmony_ci                            if (wnext < len) {  /* some from start of window */
217275793eaSopenharmony_ci                                op = wnext;
218275793eaSopenharmony_ci                                len -= op;
219275793eaSopenharmony_ci                                do {
220275793eaSopenharmony_ci                                    *out++ = *from++;
221275793eaSopenharmony_ci                                } while (--op);
222275793eaSopenharmony_ci                                from = out - dist;      /* rest from output */
223275793eaSopenharmony_ci                            }
224275793eaSopenharmony_ci                        }
225275793eaSopenharmony_ci                    }
226275793eaSopenharmony_ci                    else {                      /* contiguous in window */
227275793eaSopenharmony_ci                        from += wnext - op;
228275793eaSopenharmony_ci                        if (op < len) {         /* some from window */
229275793eaSopenharmony_ci                            len -= op;
230275793eaSopenharmony_ci                            do {
231275793eaSopenharmony_ci                                *out++ = *from++;
232275793eaSopenharmony_ci                            } while (--op);
233275793eaSopenharmony_ci                            from = out - dist;  /* rest from output */
234275793eaSopenharmony_ci                        }
235275793eaSopenharmony_ci                    }
236275793eaSopenharmony_ci                    while (len > 2) {
237275793eaSopenharmony_ci                        *out++ = *from++;
238275793eaSopenharmony_ci                        *out++ = *from++;
239275793eaSopenharmony_ci                        *out++ = *from++;
240275793eaSopenharmony_ci                        len -= 3;
241275793eaSopenharmony_ci                    }
242275793eaSopenharmony_ci                    if (len) {
243275793eaSopenharmony_ci                        *out++ = *from++;
244275793eaSopenharmony_ci                        if (len > 1)
245275793eaSopenharmony_ci                            *out++ = *from++;
246275793eaSopenharmony_ci                    }
247275793eaSopenharmony_ci                }
248275793eaSopenharmony_ci                else {
249275793eaSopenharmony_ci                    from = out - dist;          /* copy direct from output */
250275793eaSopenharmony_ci                    do {                        /* minimum length is three */
251275793eaSopenharmony_ci                        *out++ = *from++;
252275793eaSopenharmony_ci                        *out++ = *from++;
253275793eaSopenharmony_ci                        *out++ = *from++;
254275793eaSopenharmony_ci                        len -= 3;
255275793eaSopenharmony_ci                    } while (len > 2);
256275793eaSopenharmony_ci                    if (len) {
257275793eaSopenharmony_ci                        *out++ = *from++;
258275793eaSopenharmony_ci                        if (len > 1)
259275793eaSopenharmony_ci                            *out++ = *from++;
260275793eaSopenharmony_ci                    }
261275793eaSopenharmony_ci                }
262275793eaSopenharmony_ci            }
263275793eaSopenharmony_ci            else if ((op & 64) == 0) {          /* 2nd level distance code */
264275793eaSopenharmony_ci                here = dcode + here->val + (hold & ((1U << op) - 1));
265275793eaSopenharmony_ci                goto dodist;
266275793eaSopenharmony_ci            }
267275793eaSopenharmony_ci            else {
268275793eaSopenharmony_ci                strm->msg = (char *)"invalid distance code";
269275793eaSopenharmony_ci                state->mode = BAD;
270275793eaSopenharmony_ci                break;
271275793eaSopenharmony_ci            }
272275793eaSopenharmony_ci        }
273275793eaSopenharmony_ci        else if ((op & 64) == 0) {              /* 2nd level length code */
274275793eaSopenharmony_ci            here = lcode + here->val + (hold & ((1U << op) - 1));
275275793eaSopenharmony_ci            goto dolen;
276275793eaSopenharmony_ci        }
277275793eaSopenharmony_ci        else if (op & 32) {                     /* end-of-block */
278275793eaSopenharmony_ci            Tracevv((stderr, "inflate:         end of block\n"));
279275793eaSopenharmony_ci            state->mode = TYPE;
280275793eaSopenharmony_ci            break;
281275793eaSopenharmony_ci        }
282275793eaSopenharmony_ci        else {
283275793eaSopenharmony_ci            strm->msg = (char *)"invalid literal/length code";
284275793eaSopenharmony_ci            state->mode = BAD;
285275793eaSopenharmony_ci            break;
286275793eaSopenharmony_ci        }
287275793eaSopenharmony_ci    } while (in < last && out < end);
288275793eaSopenharmony_ci
289275793eaSopenharmony_ci    /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
290275793eaSopenharmony_ci    len = bits >> 3;
291275793eaSopenharmony_ci    in -= len;
292275793eaSopenharmony_ci    bits -= len << 3;
293275793eaSopenharmony_ci    hold &= (1U << bits) - 1;
294275793eaSopenharmony_ci
295275793eaSopenharmony_ci    /* update state and return */
296275793eaSopenharmony_ci    strm->next_in = in;
297275793eaSopenharmony_ci    strm->next_out = out;
298275793eaSopenharmony_ci    strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
299275793eaSopenharmony_ci    strm->avail_out = (unsigned)(out < end ?
300275793eaSopenharmony_ci                                 257 + (end - out) : 257 - (out - end));
301275793eaSopenharmony_ci    state->hold = hold;
302275793eaSopenharmony_ci    state->bits = bits;
303275793eaSopenharmony_ci    return;
304275793eaSopenharmony_ci}
305275793eaSopenharmony_ci
306275793eaSopenharmony_ci/*
307275793eaSopenharmony_ci   inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
308275793eaSopenharmony_ci   - Using bit fields for code structure
309275793eaSopenharmony_ci   - Different op definition to avoid & for extra bits (do & for table bits)
310275793eaSopenharmony_ci   - Three separate decoding do-loops for direct, window, and wnext == 0
311275793eaSopenharmony_ci   - Special case for distance > 1 copies to do overlapped load and store copy
312275793eaSopenharmony_ci   - Explicit branch predictions (based on measured branch probabilities)
313275793eaSopenharmony_ci   - Deferring match copy and interspersed it with decoding subsequent codes
314275793eaSopenharmony_ci   - Swapping literal/length else
315275793eaSopenharmony_ci   - Swapping window/direct else
316275793eaSopenharmony_ci   - Larger unrolled copy loops (three is about right)
317275793eaSopenharmony_ci   - Moving len -= 3 statement into middle of loop
318275793eaSopenharmony_ci */
319275793eaSopenharmony_ci
320275793eaSopenharmony_ci#endif /* !ASMINF */
321