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