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 12115
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 12115
    state = (struct inflate_state FAR *)strm->state;
78 12115
    in = strm->next_in;
79 12115
    last = in + (strm->avail_in - 5);
80 12115
    out = strm->next_out;
81 12115
    beg = out - (start - strm->avail_out);
82 12115
    end = out + (strm->avail_out - 257);
83
#ifdef INFLATE_STRICT
84
    dmax = state->dmax;
85
#endif
86 12115
    wsize = state->wsize;
87 12115
    whave = state->whave;
88 12115
    wnext = state->wnext;
89 12115
    window = state->window;
90 12115
    hold = state->hold;
91 12115
    bits = state->bits;
92 12115
    lcode = state->lencode;
93 12115
    dcode = state->distcode;
94 12115
    lmask = (1U << state->lenbits) - 1;
95 12115
    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 12115
    do {
100 381244
        if (bits < 15) {
101 195038
            hold += (unsigned long)(*in++) << bits;
102 195038
            bits += 8;
103 195038
            hold += (unsigned long)(*in++) << bits;
104 195038
            bits += 8;
105 195038
        }
106 381244
        here = lcode + (hold & lmask);
107
      dolen:
108 381244
        op = (unsigned)(here->bits);
109 381244
        hold >>= op;
110 381244
        bits -= op;
111 381244
        op = (unsigned)(here->op);
112 381244
        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 369836
            *out++ = (unsigned char)(here->val);
117 369836
        }
118 11408
        else if (op & 16) {                     /* length base */
119 4834
            len = (unsigned)(here->val);
120 4834
            op &= 15;                           /* number of extra bits */
121 4834
            if (op) {
122 1000
                if (bits < op) {
123 0
                    hold += (unsigned long)(*in++) << bits;
124 0
                    bits += 8;
125 0
                }
126 1000
                len += (unsigned)hold & ((1U << op) - 1);
127 1000
                hold >>= op;
128 1000
                bits -= op;
129 1000
            }
130
            Tracevv((stderr, "inflate:         length %u\n", len));
131 4834
            if (bits < 15) {
132 2161
                hold += (unsigned long)(*in++) << bits;
133 2161
                bits += 8;
134 2161
                hold += (unsigned long)(*in++) << bits;
135 2161
                bits += 8;
136 2161
            }
137 4834
            here = dcode + (hold & dmask);
138
          dodist:
139 4834
            op = (unsigned)(here->bits);
140 4834
            hold >>= op;
141 4834
            bits -= op;
142 4834
            op = (unsigned)(here->op);
143 4834
            if (op & 16) {                      /* distance base */
144 4834
                dist = (unsigned)(here->val);
145 4834
                op &= 15;                       /* number of extra bits */
146 4834
                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 4834
                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 4834
                hold >>= op;
163 4834
                bits -= op;
164
                Tracevv((stderr, "inflate:         distance %u\n", dist));
165 4834
                op = (unsigned)(out - beg);     /* max distance in output */
166 4834
                if (dist > op) {                /* see if copy from window */
167 186
                    op = dist - op;             /* distance back in window */
168 186
                    if (op > whave) {
169 50
                        if (state->sane) {
170 50
                            strm->msg =
171
                                "invalid distance too far back";
172 50
                            state->mode = BAD;
173 50
                            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 136
                    from = window;
196 136
                    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 136
                    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 136
                        from += wnext - op;
227 136
                        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 272
                    while (len > 2) {
236 136
                        *out++ = *from++;
237 136
                        *out++ = *from++;
238 136
                        *out++ = *from++;
239 136
                        len -= 3;
240
                    }
241 136
                    if (len) {
242 0
                        *out++ = *from++;
243 0
                        if (len > 1)
244 0
                            *out++ = *from++;
245 0
                    }
246 136
                }
247
                else {
248 4648
                    from = out - dist;          /* copy direct from output */
249 4648
                    do {                        /* minimum length is three */
250 20798
                        *out++ = *from++;
251 20798
                        *out++ = *from++;
252 20798
                        *out++ = *from++;
253 20798
                        len -= 3;
254 20798
                    } while (len > 2);
255 4648
                    if (len) {
256 1875
                        *out++ = *from++;
257 1875
                        if (len > 1)
258 875
                            *out++ = *from++;
259 1875
                    }
260
                }
261 4784
            }
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 4784
        }
272 6574
        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 6574
        else if (op & 32) {                     /* end-of-block */
277
            Tracevv((stderr, "inflate:         end of block\n"));
278 6574
            state->mode = TYPE;
279 6574
            break;
280
        }
281
        else {
282 0
            strm->msg = "invalid literal/length code";
283 0
            state->mode = BAD;
284 0
            break;
285
        }
286 374620
    } while (in < last && out < end);
287
288
    /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
289 12115
    len = bits >> 3;
290 12115
    in -= len;
291 12115
    bits -= len << 3;
292 12115
    hold &= (1U << bits) - 1;
293
294
    /* update state and return */
295 12115
    strm->next_in = in;
296 12115
    strm->next_out = out;
297 12115
    strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
298 12115
    strm->avail_out = (unsigned)(out < end ?
299 12115
                                 257 + (end - out) : 257 - (out - end));
300 12115
    state->hold = hold;
301 12115
    state->bits = bits;
302 12115
    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 */