Coverage Report

Created: 2026-11-23 06:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/zlib-ng/inffast_tpl.h
Line
Count
Source
1
/* inffast.c -- fast decoding
2
 * Copyright (C) 1995-2017 Mark Adler
3
 * For conditions of distribution and use, see copyright notice in zlib.h
4
 */
5
6
#include "zbuild.h"
7
#include "zutil.h"
8
#include "inftrees.h"
9
#include "inflate.h"
10
#include "inflate_p.h"
11
#include "functable.h"
12
13
/*
14
   Decode literal, length, and distance codes and write out the resulting
15
   literal and match bytes until either not enough input or output is
16
   available, an end-of-block is encountered, or a data error is encountered.
17
   When large enough input and output buffers are supplied to inflate(), for
18
   example, a 16K input buffer and a 64K output buffer, more than 95% of the
19
   inflate execution time is spent in this routine.
20
21
   Entry assumptions:
22
23
        state->mode == LEN
24
        strm->avail_in >= INFLATE_FAST_MIN_HAVE
25
        strm->avail_out >= INFLATE_FAST_MIN_LEFT
26
        start >= strm->avail_out
27
        state->bits < 8
28
29
   On return, state->mode is one of:
30
31
        LEN -- ran out of enough output space or enough available input
32
        TYPE -- reached end of block code, inflate() to interpret next block
33
        BAD -- error in block data
34
35
   Notes:
36
37
    - The maximum input bits used by a length/distance pair is 15 bits for the
38
      length code, 5 bits for the length extra, 15 bits for the distance code,
39
      and 13 bits for the distance extra.  This totals 48 bits, or six bytes.
40
      Therefore if strm->avail_in >= 6, then there is enough input to avoid
41
      checking for available input while decoding.
42
43
    - On some architectures, it can be significantly faster (e.g. up to 1.2x
44
      faster on x86_64) to load from strm->next_in 64 bits, or 8 bytes, at a
45
      time, so INFLATE_FAST_MIN_HAVE == 8.
46
47
    - The maximum bytes that a single length/distance pair can output is 258
48
      bytes, which is the maximum length that can be coded.  inflate_fast()
49
      requires strm->avail_out >= 258 for each loop to avoid checking for
50
      output space.
51
 */
52
#ifdef USE_WIDE_COPY
53
34.4k
static void INFLATE_FAST(PREFIX3(stream) *strm, uint32_t start) {
54
#else
55
52.3k
void Z_INTERNAL INFLATE_FAST(PREFIX3(stream) *strm, uint32_t start) {
56
52.3k
#endif
57
#ifdef DISPATCH_WIDE
58
    /* Blocks whose length codes allow matches over 34 bytes run the wide-copy
59
       instantiation, chosen once per call so the hot loop stays branch-free. */
60
50.2k
    if (((struct inflate_state *)strm->state)->wide_len) {
61
34.4k
        DISPATCH_WIDE(strm, start);
62
34.4k
        return;
63
34.4k
    }
64
15.8k
#endif
65
    /* start: inflate()'s starting value for strm->avail_out */
66
15.8k
    struct inflate_state *state;
67
52.3k
    z_const unsigned char *in;  /* local strm->next_in */
68
15.8k
    const unsigned char *last;  /* have enough input while in < last */
69
15.8k
    unsigned char *out;         /* local strm->next_out */
70
15.8k
    unsigned char *beg;         /* inflate()'s initial strm->next_out */
71
15.8k
    unsigned char *end;         /* while out < end, enough space available */
72
15.8k
    unsigned char *safe;        /* can use chunkcopy provided out < safe */
73
15.8k
    unsigned char *window;      /* allocated sliding window, if wsize != 0 */
74
15.8k
    unsigned wsize;             /* window size or zero if not using window */
75
15.8k
    unsigned whave;             /* valid bytes in the window */
76
15.8k
    unsigned wnext;             /* window write index */
77
78
    /* hold is a local copy of strm->hold. By default, hold satisfies the same
79
       invariants that strm->hold does, namely that (hold >> bits) == 0. This
80
       invariant is kept by loading bits into hold one byte at a time, like:
81
82
       hold |= next_byte_of_input << bits; in++; bits += 8;
83
84
       If we need to ensure that bits >= 15 then this code snippet is simply
85
       repeated. Over one iteration of the outermost do/while loop, this
86
       happens up to six times (48 bits of input), as described in the NOTES
87
       above.
88
89
       However, on some little endian architectures, it can be significantly
90
       faster to load 64 bits once instead of 8 bits six times:
91
92
       if (bits <= 16) {
93
         hold |= next_8_bytes_of_input << bits; in += 6; bits += 48;
94
       }
95
96
       Unlike the simpler one byte load, shifting the next_8_bytes_of_input
97
       by bits will overflow and lose those high bits, up to 2 bytes' worth.
98
       The conservative estimate is therefore that we have read only 6 bytes
99
       (48 bits). Again, as per the NOTES above, 48 bits is sufficient for the
100
       rest of the iteration, and we will not need to load another 8 bytes.
101
102
       Inside this function, we no longer satisfy (hold >> bits) == 0, but
103
       this is not problematic, even if that overflow does not land on an 8 bit
104
       byte boundary. Those excess bits will eventually shift down lower as the
105
       Huffman decoder consumes input, and when new input bits need to be loaded
106
       into the bits variable, the same input bits will be or'ed over those
107
       existing bits. A bitwise or is idempotent: (a | b | b) equals (a | b).
108
       Note that we therefore write that load operation as "hold |= etc" and not
109
       "hold += etc".
110
111
       Outside that loop, at the end of the function, hold is bitwise and'ed
112
       with (1<<bits)-1 to drop those excess bits so that, on function exit, we
113
       keep the invariant that (state->hold >> state->bits) == 0.
114
    */
115
15.8k
    bits_t bits;                /* local strm->bits */
116
15.8k
    uint64_t hold;              /* local strm->hold */
117
15.8k
    unsigned lmask;             /* mask for first level of length codes */
118
15.8k
    unsigned dmask;             /* mask for first level of distance codes */
119
15.8k
    code const *lcode;          /* local strm->lencode */
120
15.8k
    code const *dcode;          /* local strm->distcode */
121
15.8k
    code here;                  /* retrieved table entry */
122
15.8k
    unsigned op;                /* code bits, operation, extra bits, or */
123
                                /*  window position, window bytes to copy */
124
15.8k
    unsigned len;               /* match length, unused bytes */
125
15.8k
    unsigned char *from;        /* where to copy match from */
126
15.8k
    unsigned dist;              /* match distance */
127
15.8k
    uint64_t old;               /* look-behind buffer for extra bits */
128
129
    /* copy state to local variables */
130
15.8k
    state = (struct inflate_state *)strm->state;
131
15.8k
    in = strm->next_in;
132
52.3k
    last = in + (strm->avail_in - (INFLATE_FAST_MIN_HAVE - 1));
133
15.8k
    out = strm->next_out;
134
15.8k
    beg = out - (start - strm->avail_out);
135
15.8k
    safe = out + strm->avail_out;
136
#ifdef USE_SAFE_MODE
137
2.05k
    end = safe - INFLATE_FAST_MIN_SAFE + 1;
138
#else
139
50.2k
    end = safe - INFLATE_FAST_MIN_LEFT + 1;
140
#endif
141
15.8k
    wsize = state->wsize;
142
15.8k
    whave = state->whave;
143
15.8k
    wnext = state->wnext;
144
15.8k
    window = state->window;
145
15.8k
    hold = state->hold;
146
15.8k
    bits = (bits_t)state->bits;
147
15.8k
    lcode = state->lencode;
148
15.8k
    dcode = state->distcode;
149
15.8k
    lmask = (1U << state->lenbits) - 1;
150
15.8k
    dmask = (1U << state->distbits) - 1;
151
152
    /* Ensure enough bits for the first length table lookup. Every refill after
153
       this one is issued behind a table lookup that only needs bits already in
154
       hold, so the refill's load latency overlaps with the table load instead
155
       of feeding it. */
156
52.3k
    REFILL();
157
158
    /* decode literals and length/distances until end-of-block or not enough
159
       input data or output space */
160
106M
    do {
161
        /* This lookup needs bits >= MAX_LEN_ROOT_BITS, which every path here
162
           guarantees: each refill leaves bits >= 56, the literal-only paths
163
           loop after consuming at most 2*MAX_LEN_ROOT_BITS + MAX_BITS bits, and
164
           the distance path refills to at least MAX_BITS + MAX_DIST_EXTRA_BITS
165
           + MAX_LEN_ROOT_BITS before consuming at most MAX_BITS +
166
           MAX_DIST_EXTRA_BITS. */
167
106M
        here = lcode[hold & lmask];
168
106M
        Z_TOUCH(here);
169
        /* No-op on the first iteration: the pre-loop refill already filled hold
170
           and the lookup above consumes nothing, so bits is still >= 56. Tops
171
           hold back up on every later iteration. */
172
106M
        REFILL();
173
106M
        old = hold;
174
106M
        DROPBITS(here.bits);
175
106M
        if (LIKELY(here.op == 0)) {
176
99.0M
            TRACE_LITERAL(here.val);
177
99.0M
            *out++ = (unsigned char)(here.val);
178
99.0M
            here = lcode[hold & lmask];
179
99.0M
            Z_TOUCH(here);
180
99.0M
            old = hold;
181
99.0M
            DROPBITS(here.bits);
182
99.0M
            if (LIKELY(here.op == 0)) {
183
93.2M
                TRACE_LITERAL(here.val);
184
93.2M
                *out++ = (unsigned char)(here.val);
185
93.2M
                here = lcode[hold & lmask];
186
93.2M
                Z_TOUCH(here);
187
93.8M
            dolen:
188
93.8M
                old = hold;
189
93.8M
                DROPBITS(here.bits);
190
93.8M
                if (LIKELY(here.op == 0)) {
191
91.3M
                    TRACE_LITERAL(here.val);
192
91.3M
                    *out++ = (unsigned char)(here.val);
193
91.3M
                    continue;
194
91.3M
                }
195
93.8M
            }
196
99.0M
        }
197
16.1M
        op = here.op;
198
16.1M
        if (LIKELY(op & 16)) {                  /* length base */
199
15.5M
            len = here.val + EXTRA_BITS(old, here, op);
200
15.5M
            TRACE_LENGTH(len);
201
15.5M
            here = dcode[hold & dmask];
202
15.5M
            Z_TOUCH(here);
203
            /* Reserve bits for the distance code and its extra bits, plus the
204
               next iteration's length table lookup, which happens before the
205
               next refill. */
206
15.5M
            if (UNLIKELY(bits < MAX_BITS + MAX_DIST_EXTRA_BITS + MAX_LEN_ROOT_BITS)) {
207
866k
                REFILL();
208
866k
            }
209
15.5M
          dodist:
210
15.5M
            old = hold;
211
15.5M
            DROPBITS(here.bits);
212
15.5M
            op = here.op;
213
15.5M
            if (LIKELY(op & 16)) {              /* distance base */
214
15.5M
                dist = here.val + EXTRA_BITS(old, here, op);
215
#ifdef INFLATE_STRICT
216
                if (UNLIKELY(dist > state->dmax)) {
217
                    SET_BAD("invalid distance too far back");
218
                    break;
219
                }
220
#endif
221
15.5M
                TRACE_DISTANCE(dist);
222
223
#ifdef USE_SAFE_MODE
224
                /* If there isn't enough output space for the full copy, bail to the
225
                   slow path's MATCH state which handles partial copies. */
226
13.0k
                if (UNLIKELY(len > (unsigned)(safe - out))) {
227
290
                    state->mode = MATCH;
228
290
                    state->length = len;
229
290
                    state->was = len;
230
290
                    state->offset = dist;
231
290
                    break;
232
290
                }
233
12.8k
#endif
234
235
12.8k
                op = (unsigned)(out - beg);     /* max distance in output */
236
15.5M
                if (UNLIKELY(dist > op)) {      /* see if copy from window */
237
441k
                    op = dist - op;             /* distance back in window */
238
441k
                    if (UNLIKELY(op > whave)) {
239
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
240
                        if (LIKELY(state->sane)) {
241
                            SET_BAD("invalid distance too far back");
242
                            break;
243
                        }
244
                        unsigned gap = op - whave;
245
                        unsigned zeros = MIN(len, gap);
246
                        memset(out, 0, zeros);  /* fill missing bytes with zeros */
247
                        out += zeros;
248
                        len -= zeros;
249
                        if (UNLIKELY(len == 0))
250
                            continue;
251
                        op = whave;
252
                        if (UNLIKELY(op == 0)) {/* copy from already-decoded output */
253
                            out = chunkcopy_safe(out, out - dist, len, safe);
254
                            continue;
255
                        }
256
#else
257
0
                        SET_BAD("invalid distance too far back");
258
0
                        break;
259
0
#endif
260
0
                    }
261
441k
                    from = window;
262
441k
                    if (LIKELY(wnext == 0)) {           /* very common case */
263
363k
                        from += wsize - op;
264
363k
                    } else if (LIKELY(wnext >= op)) {   /* contiguous in window */
265
35.8k
                        from += wnext - op;
266
42.6k
                    } else {                            /* wrap around window */
267
42.6k
                        op -= wnext;
268
42.6k
                        from += wsize - op;
269
42.6k
                        if (UNLIKELY(op < len)) {       /* some from end of window */
270
97
                            len -= op;
271
97
                            out = CHUNKCOPY_SAFE(out, from, op, safe);
272
97
                            from = window;              /* more from start of window */
273
97
                            op = wnext;
274
                            /* This (rare) case can create a situation where
275
                               the first chunkcopy below must be checked.
276
                             */
277
97
                        }
278
42.6k
                    }
279
441k
                    if (UNLIKELY(op < len)) {           /* still need some from output */
280
741
                        len -= op;
281
#ifndef USE_SAFE_MODE
282
                        out = CHUNKCOPY_SAFE(out, from, op, safe);
283
685
                        out = CHUNKUNROLL(out, &dist, &len);
284
                        out = CHUNKCOPY_SAFE(out, out - dist, len, safe);
285
#elif defined(HAVE_MASKED_READWRITE)
286
                        out = CHUNKCOPY_SAFE(out, from, op, safe);
287
                        out = CHUNKCOPY_SAFE(out, out - dist, len, safe);
288
#else
289
                        out = chunkcopy_safe(out, from, op, safe);
290
                        out = chunkcopy_safe(out, out - dist, len, safe);
291
#endif
292
440k
                    } else {
293
#if !defined(USE_SAFE_MODE) || defined(HAVE_MASKED_READWRITE)
294
                        out = CHUNKCOPY_SAFE(out, from, len, safe);
295
#else
296
                        out = chunkcopy_safe(out, from, len, safe);
297
#endif
298
440k
                    }
299
15.0M
                } else {
300
#ifndef USE_SAFE_MODE
301
                    /* Whole reference is in range of current output.  No range checks are
302
                       necessary because we start with room for at least 258 bytes of output,
303
                       so unroll and roundoff operations can write beyond `out+len` so long
304
                       as they stay within 258 bytes of `out`.
305
                    */
306
#ifdef USE_WIDE_COPY
307
13.4M
                    if (LIKELY(dist >= len || dist >= 2 * CHUNKSIZE()))
308
10.6M
                        out = DOUBLECHUNKCOPY(out, out - dist, len);
309
#else
310
1.55M
                    if (LIKELY(dist >= len || dist > CHUNKSIZE()))
311
1.17M
                        out = CHUNKCOPY(out, out - dist, len);
312
381k
#endif
313
381k
                    else
314
3.20M
                        out = CHUNKMEMSET(out, out - dist, len);
315
#elif defined(HAVE_MASKED_READWRITE)
316
                    out = CHUNKCOPY_SAFE(out, out - dist, len, safe);
317
#else
318
                    out = chunkcopy_safe(out, out - dist, len, safe);
319
#endif
320
15.0M
                }
321
24.2k
            } else if (UNLIKELY((op & 64) == 0)) {          /* 2nd level distance code */
322
24.2k
                here = dcode[here.val + BITS(op)];
323
24.2k
                Z_TOUCH(here);
324
24.2k
                goto dodist;
325
24.2k
            } else {
326
0
                SET_BAD("invalid distance code");
327
0
                break;
328
0
            }
329
15.5M
        } else if (UNLIKELY((op & 64) == 0)) {              /* 2nd level length code */
330
611k
            here = lcode[here.val + BITS(op)];
331
611k
            Z_TOUCH(here);
332
611k
            goto dolen;
333
611k
        } else if (UNLIKELY(op & 32)) {                     /* end-of-block */
334
14.2k
            TRACE_END_OF_BLOCK();
335
14.2k
            state->mode = TYPE;
336
14.2k
            break;
337
14.2k
        } else {
338
0
            SET_BAD("invalid literal/length code");
339
0
            break;
340
0
        }
341
106M
    } while (in < last && out < end);
342
343
    /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
344
52.3k
    len = bits >> 3;
345
52.3k
    in -= len;
346
52.3k
    bits -= (bits_t)(len << 3);
347
52.3k
    hold &= (UINT64_C(1) << bits) - 1;
348
349
    /* update state and return */
350
52.3k
    strm->next_in = in;
351
52.3k
    strm->next_out = out;
352
52.3k
    strm->avail_in = (unsigned)(in < last ? (INFLATE_FAST_MIN_HAVE - 1) + (last - in)
353
52.3k
                                          : (INFLATE_FAST_MIN_HAVE - 1) - (in - last));
354
52.3k
    strm->avail_out = (unsigned)(safe - out);
355
356
52.3k
    Assert(bits <= 32, "Remaining bits greater than 32");
357
52.3k
    state->hold = (uint32_t)hold;
358
52.3k
    state->bits = bits;
359
52.3k
    return;
360
15.8k
}
Unexecuted instantiation: chunkset_sse2.c:inflate_fast_wide_sse2
Unexecuted instantiation: chunkset_ssse3.c:inflate_fast_wide_ssse3
chunkset_avx2.c:inflate_fast_wide_avx2
Line
Count
Source
53
34.4k
static void INFLATE_FAST(PREFIX3(stream) *strm, uint32_t start) {
54
#else
55
void Z_INTERNAL INFLATE_FAST(PREFIX3(stream) *strm, uint32_t start) {
56
#endif
57
#ifdef DISPATCH_WIDE
58
    /* Blocks whose length codes allow matches over 34 bytes run the wide-copy
59
       instantiation, chosen once per call so the hot loop stays branch-free. */
60
    if (((struct inflate_state *)strm->state)->wide_len) {
61
        DISPATCH_WIDE(strm, start);
62
        return;
63
    }
64
#endif
65
    /* start: inflate()'s starting value for strm->avail_out */
66
34.4k
    struct inflate_state *state;
67
34.4k
    z_const unsigned char *in;  /* local strm->next_in */
68
34.4k
    const unsigned char *last;  /* have enough input while in < last */
69
34.4k
    unsigned char *out;         /* local strm->next_out */
70
34.4k
    unsigned char *beg;         /* inflate()'s initial strm->next_out */
71
34.4k
    unsigned char *end;         /* while out < end, enough space available */
72
34.4k
    unsigned char *safe;        /* can use chunkcopy provided out < safe */
73
34.4k
    unsigned char *window;      /* allocated sliding window, if wsize != 0 */
74
34.4k
    unsigned wsize;             /* window size or zero if not using window */
75
34.4k
    unsigned whave;             /* valid bytes in the window */
76
34.4k
    unsigned wnext;             /* window write index */
77
78
    /* hold is a local copy of strm->hold. By default, hold satisfies the same
79
       invariants that strm->hold does, namely that (hold >> bits) == 0. This
80
       invariant is kept by loading bits into hold one byte at a time, like:
81
82
       hold |= next_byte_of_input << bits; in++; bits += 8;
83
84
       If we need to ensure that bits >= 15 then this code snippet is simply
85
       repeated. Over one iteration of the outermost do/while loop, this
86
       happens up to six times (48 bits of input), as described in the NOTES
87
       above.
88
89
       However, on some little endian architectures, it can be significantly
90
       faster to load 64 bits once instead of 8 bits six times:
91
92
       if (bits <= 16) {
93
         hold |= next_8_bytes_of_input << bits; in += 6; bits += 48;
94
       }
95
96
       Unlike the simpler one byte load, shifting the next_8_bytes_of_input
97
       by bits will overflow and lose those high bits, up to 2 bytes' worth.
98
       The conservative estimate is therefore that we have read only 6 bytes
99
       (48 bits). Again, as per the NOTES above, 48 bits is sufficient for the
100
       rest of the iteration, and we will not need to load another 8 bytes.
101
102
       Inside this function, we no longer satisfy (hold >> bits) == 0, but
103
       this is not problematic, even if that overflow does not land on an 8 bit
104
       byte boundary. Those excess bits will eventually shift down lower as the
105
       Huffman decoder consumes input, and when new input bits need to be loaded
106
       into the bits variable, the same input bits will be or'ed over those
107
       existing bits. A bitwise or is idempotent: (a | b | b) equals (a | b).
108
       Note that we therefore write that load operation as "hold |= etc" and not
109
       "hold += etc".
110
111
       Outside that loop, at the end of the function, hold is bitwise and'ed
112
       with (1<<bits)-1 to drop those excess bits so that, on function exit, we
113
       keep the invariant that (state->hold >> state->bits) == 0.
114
    */
115
34.4k
    bits_t bits;                /* local strm->bits */
116
34.4k
    uint64_t hold;              /* local strm->hold */
117
34.4k
    unsigned lmask;             /* mask for first level of length codes */
118
34.4k
    unsigned dmask;             /* mask for first level of distance codes */
119
34.4k
    code const *lcode;          /* local strm->lencode */
120
34.4k
    code const *dcode;          /* local strm->distcode */
121
34.4k
    code here;                  /* retrieved table entry */
122
34.4k
    unsigned op;                /* code bits, operation, extra bits, or */
123
                                /*  window position, window bytes to copy */
124
34.4k
    unsigned len;               /* match length, unused bytes */
125
34.4k
    unsigned char *from;        /* where to copy match from */
126
34.4k
    unsigned dist;              /* match distance */
127
34.4k
    uint64_t old;               /* look-behind buffer for extra bits */
128
129
    /* copy state to local variables */
130
34.4k
    state = (struct inflate_state *)strm->state;
131
34.4k
    in = strm->next_in;
132
34.4k
    last = in + (strm->avail_in - (INFLATE_FAST_MIN_HAVE - 1));
133
34.4k
    out = strm->next_out;
134
34.4k
    beg = out - (start - strm->avail_out);
135
34.4k
    safe = out + strm->avail_out;
136
#ifdef USE_SAFE_MODE
137
    end = safe - INFLATE_FAST_MIN_SAFE + 1;
138
#else
139
34.4k
    end = safe - INFLATE_FAST_MIN_LEFT + 1;
140
34.4k
#endif
141
34.4k
    wsize = state->wsize;
142
34.4k
    whave = state->whave;
143
34.4k
    wnext = state->wnext;
144
34.4k
    window = state->window;
145
34.4k
    hold = state->hold;
146
34.4k
    bits = (bits_t)state->bits;
147
34.4k
    lcode = state->lencode;
148
34.4k
    dcode = state->distcode;
149
34.4k
    lmask = (1U << state->lenbits) - 1;
150
34.4k
    dmask = (1U << state->distbits) - 1;
151
152
    /* Ensure enough bits for the first length table lookup. Every refill after
153
       this one is issued behind a table lookup that only needs bits already in
154
       hold, so the refill's load latency overlaps with the table load instead
155
       of feeding it. */
156
34.4k
    REFILL();
157
158
    /* decode literals and length/distances until end-of-block or not enough
159
       input data or output space */
160
65.9M
    do {
161
        /* This lookup needs bits >= MAX_LEN_ROOT_BITS, which every path here
162
           guarantees: each refill leaves bits >= 56, the literal-only paths
163
           loop after consuming at most 2*MAX_LEN_ROOT_BITS + MAX_BITS bits, and
164
           the distance path refills to at least MAX_BITS + MAX_DIST_EXTRA_BITS
165
           + MAX_LEN_ROOT_BITS before consuming at most MAX_BITS +
166
           MAX_DIST_EXTRA_BITS. */
167
65.9M
        here = lcode[hold & lmask];
168
65.9M
        Z_TOUCH(here);
169
        /* No-op on the first iteration: the pre-loop refill already filled hold
170
           and the lookup above consumes nothing, so bits is still >= 56. Tops
171
           hold back up on every later iteration. */
172
65.9M
        REFILL();
173
65.9M
        old = hold;
174
65.9M
        DROPBITS(here.bits);
175
65.9M
        if (LIKELY(here.op == 0)) {
176
59.1M
            TRACE_LITERAL(here.val);
177
59.1M
            *out++ = (unsigned char)(here.val);
178
59.1M
            here = lcode[hold & lmask];
179
59.1M
            Z_TOUCH(here);
180
59.1M
            old = hold;
181
59.1M
            DROPBITS(here.bits);
182
59.1M
            if (LIKELY(here.op == 0)) {
183
53.7M
                TRACE_LITERAL(here.val);
184
53.7M
                *out++ = (unsigned char)(here.val);
185
53.7M
                here = lcode[hold & lmask];
186
53.7M
                Z_TOUCH(here);
187
54.0M
            dolen:
188
54.0M
                old = hold;
189
54.0M
                DROPBITS(here.bits);
190
54.0M
                if (LIKELY(here.op == 0)) {
191
52.1M
                    TRACE_LITERAL(here.val);
192
52.1M
                    *out++ = (unsigned char)(here.val);
193
52.1M
                    continue;
194
52.1M
                }
195
54.0M
            }
196
59.1M
        }
197
14.1M
        op = here.op;
198
14.1M
        if (LIKELY(op & 16)) {                  /* length base */
199
13.8M
            len = here.val + EXTRA_BITS(old, here, op);
200
13.8M
            TRACE_LENGTH(len);
201
13.8M
            here = dcode[hold & dmask];
202
13.8M
            Z_TOUCH(here);
203
            /* Reserve bits for the distance code and its extra bits, plus the
204
               next iteration's length table lookup, which happens before the
205
               next refill. */
206
13.8M
            if (UNLIKELY(bits < MAX_BITS + MAX_DIST_EXTRA_BITS + MAX_LEN_ROOT_BITS)) {
207
804k
                REFILL();
208
804k
            }
209
13.8M
          dodist:
210
13.8M
            old = hold;
211
13.8M
            DROPBITS(here.bits);
212
13.8M
            op = here.op;
213
13.8M
            if (LIKELY(op & 16)) {              /* distance base */
214
13.8M
                dist = here.val + EXTRA_BITS(old, here, op);
215
#ifdef INFLATE_STRICT
216
                if (UNLIKELY(dist > state->dmax)) {
217
                    SET_BAD("invalid distance too far back");
218
                    break;
219
                }
220
#endif
221
13.8M
                TRACE_DISTANCE(dist);
222
223
#ifdef USE_SAFE_MODE
224
                /* If there isn't enough output space for the full copy, bail to the
225
                   slow path's MATCH state which handles partial copies. */
226
                if (UNLIKELY(len > (unsigned)(safe - out))) {
227
                    state->mode = MATCH;
228
                    state->length = len;
229
                    state->was = len;
230
                    state->offset = dist;
231
                    break;
232
                }
233
#endif
234
235
13.8M
                op = (unsigned)(out - beg);     /* max distance in output */
236
13.8M
                if (UNLIKELY(dist > op)) {      /* see if copy from window */
237
330k
                    op = dist - op;             /* distance back in window */
238
330k
                    if (UNLIKELY(op > whave)) {
239
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
240
                        if (LIKELY(state->sane)) {
241
                            SET_BAD("invalid distance too far back");
242
                            break;
243
                        }
244
                        unsigned gap = op - whave;
245
                        unsigned zeros = MIN(len, gap);
246
                        memset(out, 0, zeros);  /* fill missing bytes with zeros */
247
                        out += zeros;
248
                        len -= zeros;
249
                        if (UNLIKELY(len == 0))
250
                            continue;
251
                        op = whave;
252
                        if (UNLIKELY(op == 0)) {/* copy from already-decoded output */
253
                            out = chunkcopy_safe(out, out - dist, len, safe);
254
                            continue;
255
                        }
256
#else
257
0
                        SET_BAD("invalid distance too far back");
258
0
                        break;
259
0
#endif
260
0
                    }
261
330k
                    from = window;
262
330k
                    if (LIKELY(wnext == 0)) {           /* very common case */
263
273k
                        from += wsize - op;
264
273k
                    } else if (LIKELY(wnext >= op)) {   /* contiguous in window */
265
22.2k
                        from += wnext - op;
266
34.6k
                    } else {                            /* wrap around window */
267
34.6k
                        op -= wnext;
268
34.6k
                        from += wsize - op;
269
34.6k
                        if (UNLIKELY(op < len)) {       /* some from end of window */
270
84
                            len -= op;
271
84
                            out = CHUNKCOPY_SAFE(out, from, op, safe);
272
84
                            from = window;              /* more from start of window */
273
84
                            op = wnext;
274
                            /* This (rare) case can create a situation where
275
                               the first chunkcopy below must be checked.
276
                             */
277
84
                        }
278
34.6k
                    }
279
330k
                    if (UNLIKELY(op < len)) {           /* still need some from output */
280
625
                        len -= op;
281
625
#ifndef USE_SAFE_MODE
282
625
                        out = CHUNKCOPY_SAFE(out, from, op, safe);
283
625
                        out = CHUNKUNROLL(out, &dist, &len);
284
625
                        out = CHUNKCOPY_SAFE(out, out - dist, len, safe);
285
#elif defined(HAVE_MASKED_READWRITE)
286
                        out = CHUNKCOPY_SAFE(out, from, op, safe);
287
                        out = CHUNKCOPY_SAFE(out, out - dist, len, safe);
288
#else
289
                        out = chunkcopy_safe(out, from, op, safe);
290
                        out = chunkcopy_safe(out, out - dist, len, safe);
291
#endif
292
329k
                    } else {
293
329k
#if !defined(USE_SAFE_MODE) || defined(HAVE_MASKED_READWRITE)
294
329k
                        out = CHUNKCOPY_SAFE(out, from, len, safe);
295
#else
296
                        out = chunkcopy_safe(out, from, len, safe);
297
#endif
298
329k
                    }
299
13.4M
                } else {
300
13.4M
#ifndef USE_SAFE_MODE
301
                    /* Whole reference is in range of current output.  No range checks are
302
                       necessary because we start with room for at least 258 bytes of output,
303
                       so unroll and roundoff operations can write beyond `out+len` so long
304
                       as they stay within 258 bytes of `out`.
305
                    */
306
13.4M
#ifdef USE_WIDE_COPY
307
13.4M
                    if (LIKELY(dist >= len || dist >= 2 * CHUNKSIZE()))
308
10.6M
                        out = DOUBLECHUNKCOPY(out, out - dist, len);
309
#else
310
                    if (LIKELY(dist >= len || dist > CHUNKSIZE()))
311
                        out = CHUNKCOPY(out, out - dist, len);
312
#endif
313
2.82M
                    else
314
2.82M
                        out = CHUNKMEMSET(out, out - dist, len);
315
#elif defined(HAVE_MASKED_READWRITE)
316
                    out = CHUNKCOPY_SAFE(out, out - dist, len, safe);
317
#else
318
                    out = chunkcopy_safe(out, out - dist, len, safe);
319
#endif
320
13.4M
                }
321
13.8M
            } else if (UNLIKELY((op & 64) == 0)) {          /* 2nd level distance code */
322
19.4k
                here = dcode[here.val + BITS(op)];
323
19.4k
                Z_TOUCH(here);
324
19.4k
                goto dodist;
325
19.4k
            } else {
326
0
                SET_BAD("invalid distance code");
327
0
                break;
328
0
            }
329
13.8M
        } else if (UNLIKELY((op & 64) == 0)) {              /* 2nd level length code */
330
333k
            here = lcode[here.val + BITS(op)];
331
333k
            Z_TOUCH(here);
332
333k
            goto dolen;
333
333k
        } else if (UNLIKELY(op & 32)) {                     /* end-of-block */
334
7.12k
            TRACE_END_OF_BLOCK();
335
7.12k
            state->mode = TYPE;
336
7.12k
            break;
337
7.12k
        } else {
338
0
            SET_BAD("invalid literal/length code");
339
0
            break;
340
0
        }
341
65.9M
    } while (in < last && out < end);
342
343
    /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
344
34.4k
    len = bits >> 3;
345
34.4k
    in -= len;
346
34.4k
    bits -= (bits_t)(len << 3);
347
34.4k
    hold &= (UINT64_C(1) << bits) - 1;
348
349
    /* update state and return */
350
34.4k
    strm->next_in = in;
351
34.4k
    strm->next_out = out;
352
34.4k
    strm->avail_in = (unsigned)(in < last ? (INFLATE_FAST_MIN_HAVE - 1) + (last - in)
353
34.4k
                                          : (INFLATE_FAST_MIN_HAVE - 1) - (in - last));
354
34.4k
    strm->avail_out = (unsigned)(safe - out);
355
356
34.4k
    Assert(bits <= 32, "Remaining bits greater than 32");
357
34.4k
    state->hold = (uint32_t)hold;
358
34.4k
    state->bits = bits;
359
34.4k
    return;
360
34.4k
}
Unexecuted instantiation: chunkset_avx512.c:inflate_fast_wide_avx512
Unexecuted instantiation: inflate_fast_sse2
Unexecuted instantiation: inflate_fast_safe_sse2
Unexecuted instantiation: inflate_fast_ssse3
Unexecuted instantiation: inflate_fast_safe_ssse3
inflate_fast_avx2
Line
Count
Source
55
50.2k
void Z_INTERNAL INFLATE_FAST(PREFIX3(stream) *strm, uint32_t start) {
56
50.2k
#endif
57
50.2k
#ifdef DISPATCH_WIDE
58
    /* Blocks whose length codes allow matches over 34 bytes run the wide-copy
59
       instantiation, chosen once per call so the hot loop stays branch-free. */
60
50.2k
    if (((struct inflate_state *)strm->state)->wide_len) {
61
34.4k
        DISPATCH_WIDE(strm, start);
62
34.4k
        return;
63
34.4k
    }
64
15.8k
#endif
65
    /* start: inflate()'s starting value for strm->avail_out */
66
15.8k
    struct inflate_state *state;
67
15.8k
    z_const unsigned char *in;  /* local strm->next_in */
68
15.8k
    const unsigned char *last;  /* have enough input while in < last */
69
15.8k
    unsigned char *out;         /* local strm->next_out */
70
15.8k
    unsigned char *beg;         /* inflate()'s initial strm->next_out */
71
15.8k
    unsigned char *end;         /* while out < end, enough space available */
72
15.8k
    unsigned char *safe;        /* can use chunkcopy provided out < safe */
73
15.8k
    unsigned char *window;      /* allocated sliding window, if wsize != 0 */
74
15.8k
    unsigned wsize;             /* window size or zero if not using window */
75
15.8k
    unsigned whave;             /* valid bytes in the window */
76
15.8k
    unsigned wnext;             /* window write index */
77
78
    /* hold is a local copy of strm->hold. By default, hold satisfies the same
79
       invariants that strm->hold does, namely that (hold >> bits) == 0. This
80
       invariant is kept by loading bits into hold one byte at a time, like:
81
82
       hold |= next_byte_of_input << bits; in++; bits += 8;
83
84
       If we need to ensure that bits >= 15 then this code snippet is simply
85
       repeated. Over one iteration of the outermost do/while loop, this
86
       happens up to six times (48 bits of input), as described in the NOTES
87
       above.
88
89
       However, on some little endian architectures, it can be significantly
90
       faster to load 64 bits once instead of 8 bits six times:
91
92
       if (bits <= 16) {
93
         hold |= next_8_bytes_of_input << bits; in += 6; bits += 48;
94
       }
95
96
       Unlike the simpler one byte load, shifting the next_8_bytes_of_input
97
       by bits will overflow and lose those high bits, up to 2 bytes' worth.
98
       The conservative estimate is therefore that we have read only 6 bytes
99
       (48 bits). Again, as per the NOTES above, 48 bits is sufficient for the
100
       rest of the iteration, and we will not need to load another 8 bytes.
101
102
       Inside this function, we no longer satisfy (hold >> bits) == 0, but
103
       this is not problematic, even if that overflow does not land on an 8 bit
104
       byte boundary. Those excess bits will eventually shift down lower as the
105
       Huffman decoder consumes input, and when new input bits need to be loaded
106
       into the bits variable, the same input bits will be or'ed over those
107
       existing bits. A bitwise or is idempotent: (a | b | b) equals (a | b).
108
       Note that we therefore write that load operation as "hold |= etc" and not
109
       "hold += etc".
110
111
       Outside that loop, at the end of the function, hold is bitwise and'ed
112
       with (1<<bits)-1 to drop those excess bits so that, on function exit, we
113
       keep the invariant that (state->hold >> state->bits) == 0.
114
    */
115
15.8k
    bits_t bits;                /* local strm->bits */
116
15.8k
    uint64_t hold;              /* local strm->hold */
117
15.8k
    unsigned lmask;             /* mask for first level of length codes */
118
15.8k
    unsigned dmask;             /* mask for first level of distance codes */
119
15.8k
    code const *lcode;          /* local strm->lencode */
120
15.8k
    code const *dcode;          /* local strm->distcode */
121
15.8k
    code here;                  /* retrieved table entry */
122
15.8k
    unsigned op;                /* code bits, operation, extra bits, or */
123
                                /*  window position, window bytes to copy */
124
15.8k
    unsigned len;               /* match length, unused bytes */
125
15.8k
    unsigned char *from;        /* where to copy match from */
126
15.8k
    unsigned dist;              /* match distance */
127
15.8k
    uint64_t old;               /* look-behind buffer for extra bits */
128
129
    /* copy state to local variables */
130
15.8k
    state = (struct inflate_state *)strm->state;
131
15.8k
    in = strm->next_in;
132
15.8k
    last = in + (strm->avail_in - (INFLATE_FAST_MIN_HAVE - 1));
133
15.8k
    out = strm->next_out;
134
15.8k
    beg = out - (start - strm->avail_out);
135
15.8k
    safe = out + strm->avail_out;
136
#ifdef USE_SAFE_MODE
137
    end = safe - INFLATE_FAST_MIN_SAFE + 1;
138
#else
139
15.8k
    end = safe - INFLATE_FAST_MIN_LEFT + 1;
140
15.8k
#endif
141
15.8k
    wsize = state->wsize;
142
15.8k
    whave = state->whave;
143
15.8k
    wnext = state->wnext;
144
15.8k
    window = state->window;
145
15.8k
    hold = state->hold;
146
15.8k
    bits = (bits_t)state->bits;
147
15.8k
    lcode = state->lencode;
148
15.8k
    dcode = state->distcode;
149
15.8k
    lmask = (1U << state->lenbits) - 1;
150
15.8k
    dmask = (1U << state->distbits) - 1;
151
152
    /* Ensure enough bits for the first length table lookup. Every refill after
153
       this one is issued behind a table lookup that only needs bits already in
154
       hold, so the refill's load latency overlaps with the table load instead
155
       of feeding it. */
156
15.8k
    REFILL();
157
158
    /* decode literals and length/distances until end-of-block or not enough
159
       input data or output space */
160
40.8M
    do {
161
        /* This lookup needs bits >= MAX_LEN_ROOT_BITS, which every path here
162
           guarantees: each refill leaves bits >= 56, the literal-only paths
163
           loop after consuming at most 2*MAX_LEN_ROOT_BITS + MAX_BITS bits, and
164
           the distance path refills to at least MAX_BITS + MAX_DIST_EXTRA_BITS
165
           + MAX_LEN_ROOT_BITS before consuming at most MAX_BITS +
166
           MAX_DIST_EXTRA_BITS. */
167
40.8M
        here = lcode[hold & lmask];
168
40.8M
        Z_TOUCH(here);
169
        /* No-op on the first iteration: the pre-loop refill already filled hold
170
           and the lookup above consumes nothing, so bits is still >= 56. Tops
171
           hold back up on every later iteration. */
172
40.8M
        REFILL();
173
40.8M
        old = hold;
174
40.8M
        DROPBITS(here.bits);
175
40.8M
        if (LIKELY(here.op == 0)) {
176
39.8M
            TRACE_LITERAL(here.val);
177
39.8M
            *out++ = (unsigned char)(here.val);
178
39.8M
            here = lcode[hold & lmask];
179
39.8M
            Z_TOUCH(here);
180
39.8M
            old = hold;
181
39.8M
            DROPBITS(here.bits);
182
39.8M
            if (LIKELY(here.op == 0)) {
183
39.5M
                TRACE_LITERAL(here.val);
184
39.5M
                *out++ = (unsigned char)(here.val);
185
39.5M
                here = lcode[hold & lmask];
186
39.5M
                Z_TOUCH(here);
187
39.7M
            dolen:
188
39.7M
                old = hold;
189
39.7M
                DROPBITS(here.bits);
190
39.7M
                if (LIKELY(here.op == 0)) {
191
39.1M
                    TRACE_LITERAL(here.val);
192
39.1M
                    *out++ = (unsigned char)(here.val);
193
39.1M
                    continue;
194
39.1M
                }
195
39.7M
            }
196
39.8M
        }
197
1.94M
        op = here.op;
198
1.94M
        if (LIKELY(op & 16)) {                  /* length base */
199
1.66M
            len = here.val + EXTRA_BITS(old, here, op);
200
1.66M
            TRACE_LENGTH(len);
201
1.66M
            here = dcode[hold & dmask];
202
1.66M
            Z_TOUCH(here);
203
            /* Reserve bits for the distance code and its extra bits, plus the
204
               next iteration's length table lookup, which happens before the
205
               next refill. */
206
1.66M
            if (UNLIKELY(bits < MAX_BITS + MAX_DIST_EXTRA_BITS + MAX_LEN_ROOT_BITS)) {
207
61.2k
                REFILL();
208
61.2k
            }
209
1.67M
          dodist:
210
1.67M
            old = hold;
211
1.67M
            DROPBITS(here.bits);
212
1.67M
            op = here.op;
213
1.67M
            if (LIKELY(op & 16)) {              /* distance base */
214
1.66M
                dist = here.val + EXTRA_BITS(old, here, op);
215
#ifdef INFLATE_STRICT
216
                if (UNLIKELY(dist > state->dmax)) {
217
                    SET_BAD("invalid distance too far back");
218
                    break;
219
                }
220
#endif
221
1.66M
                TRACE_DISTANCE(dist);
222
223
#ifdef USE_SAFE_MODE
224
                /* If there isn't enough output space for the full copy, bail to the
225
                   slow path's MATCH state which handles partial copies. */
226
                if (UNLIKELY(len > (unsigned)(safe - out))) {
227
                    state->mode = MATCH;
228
                    state->length = len;
229
                    state->was = len;
230
                    state->offset = dist;
231
                    break;
232
                }
233
#endif
234
235
1.66M
                op = (unsigned)(out - beg);     /* max distance in output */
236
1.66M
                if (UNLIKELY(dist > op)) {      /* see if copy from window */
237
109k
                    op = dist - op;             /* distance back in window */
238
109k
                    if (UNLIKELY(op > whave)) {
239
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
240
                        if (LIKELY(state->sane)) {
241
                            SET_BAD("invalid distance too far back");
242
                            break;
243
                        }
244
                        unsigned gap = op - whave;
245
                        unsigned zeros = MIN(len, gap);
246
                        memset(out, 0, zeros);  /* fill missing bytes with zeros */
247
                        out += zeros;
248
                        len -= zeros;
249
                        if (UNLIKELY(len == 0))
250
                            continue;
251
                        op = whave;
252
                        if (UNLIKELY(op == 0)) {/* copy from already-decoded output */
253
                            out = chunkcopy_safe(out, out - dist, len, safe);
254
                            continue;
255
                        }
256
#else
257
0
                        SET_BAD("invalid distance too far back");
258
0
                        break;
259
0
#endif
260
0
                    }
261
109k
                    from = window;
262
109k
                    if (LIKELY(wnext == 0)) {           /* very common case */
263
87.8k
                        from += wsize - op;
264
87.8k
                    } else if (LIKELY(wnext >= op)) {   /* contiguous in window */
265
13.5k
                        from += wnext - op;
266
13.5k
                    } else {                            /* wrap around window */
267
8.01k
                        op -= wnext;
268
8.01k
                        from += wsize - op;
269
8.01k
                        if (UNLIKELY(op < len)) {       /* some from end of window */
270
13
                            len -= op;
271
13
                            out = CHUNKCOPY_SAFE(out, from, op, safe);
272
13
                            from = window;              /* more from start of window */
273
13
                            op = wnext;
274
                            /* This (rare) case can create a situation where
275
                               the first chunkcopy below must be checked.
276
                             */
277
13
                        }
278
8.01k
                    }
279
109k
                    if (UNLIKELY(op < len)) {           /* still need some from output */
280
60
                        len -= op;
281
60
#ifndef USE_SAFE_MODE
282
60
                        out = CHUNKCOPY_SAFE(out, from, op, safe);
283
60
                        out = CHUNKUNROLL(out, &dist, &len);
284
60
                        out = CHUNKCOPY_SAFE(out, out - dist, len, safe);
285
#elif defined(HAVE_MASKED_READWRITE)
286
                        out = CHUNKCOPY_SAFE(out, from, op, safe);
287
                        out = CHUNKCOPY_SAFE(out, out - dist, len, safe);
288
#else
289
                        out = chunkcopy_safe(out, from, op, safe);
290
                        out = chunkcopy_safe(out, out - dist, len, safe);
291
#endif
292
109k
                    } else {
293
109k
#if !defined(USE_SAFE_MODE) || defined(HAVE_MASKED_READWRITE)
294
109k
                        out = CHUNKCOPY_SAFE(out, from, len, safe);
295
#else
296
                        out = chunkcopy_safe(out, from, len, safe);
297
#endif
298
109k
                    }
299
1.55M
                } else {
300
1.55M
#ifndef USE_SAFE_MODE
301
                    /* Whole reference is in range of current output.  No range checks are
302
                       necessary because we start with room for at least 258 bytes of output,
303
                       so unroll and roundoff operations can write beyond `out+len` so long
304
                       as they stay within 258 bytes of `out`.
305
                    */
306
#ifdef USE_WIDE_COPY
307
                    if (LIKELY(dist >= len || dist >= 2 * CHUNKSIZE()))
308
                        out = DOUBLECHUNKCOPY(out, out - dist, len);
309
#else
310
1.55M
                    if (LIKELY(dist >= len || dist > CHUNKSIZE()))
311
1.17M
                        out = CHUNKCOPY(out, out - dist, len);
312
381k
#endif
313
381k
                    else
314
381k
                        out = CHUNKMEMSET(out, out - dist, len);
315
#elif defined(HAVE_MASKED_READWRITE)
316
                    out = CHUNKCOPY_SAFE(out, out - dist, len, safe);
317
#else
318
                    out = chunkcopy_safe(out, out - dist, len, safe);
319
#endif
320
1.55M
                }
321
1.66M
            } else if (UNLIKELY((op & 64) == 0)) {          /* 2nd level distance code */
322
4.75k
                here = dcode[here.val + BITS(op)];
323
4.75k
                Z_TOUCH(here);
324
4.75k
                goto dodist;
325
4.75k
            } else {
326
0
                SET_BAD("invalid distance code");
327
0
                break;
328
0
            }
329
1.67M
        } else if (UNLIKELY((op & 64) == 0)) {              /* 2nd level length code */
330
275k
            here = lcode[here.val + BITS(op)];
331
275k
            Z_TOUCH(here);
332
275k
            goto dolen;
333
275k
        } else if (UNLIKELY(op & 32)) {                     /* end-of-block */
334
6.87k
            TRACE_END_OF_BLOCK();
335
6.87k
            state->mode = TYPE;
336
6.87k
            break;
337
6.87k
        } else {
338
0
            SET_BAD("invalid literal/length code");
339
0
            break;
340
0
        }
341
40.8M
    } while (in < last && out < end);
342
343
    /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
344
15.8k
    len = bits >> 3;
345
15.8k
    in -= len;
346
15.8k
    bits -= (bits_t)(len << 3);
347
15.8k
    hold &= (UINT64_C(1) << bits) - 1;
348
349
    /* update state and return */
350
15.8k
    strm->next_in = in;
351
15.8k
    strm->next_out = out;
352
15.8k
    strm->avail_in = (unsigned)(in < last ? (INFLATE_FAST_MIN_HAVE - 1) + (last - in)
353
15.8k
                                          : (INFLATE_FAST_MIN_HAVE - 1) - (in - last));
354
15.8k
    strm->avail_out = (unsigned)(safe - out);
355
356
15.8k
    Assert(bits <= 32, "Remaining bits greater than 32");
357
15.8k
    state->hold = (uint32_t)hold;
358
15.8k
    state->bits = bits;
359
15.8k
    return;
360
15.8k
}
inflate_fast_safe_avx2
Line
Count
Source
55
2.05k
void Z_INTERNAL INFLATE_FAST(PREFIX3(stream) *strm, uint32_t start) {
56
2.05k
#endif
57
#ifdef DISPATCH_WIDE
58
    /* Blocks whose length codes allow matches over 34 bytes run the wide-copy
59
       instantiation, chosen once per call so the hot loop stays branch-free. */
60
    if (((struct inflate_state *)strm->state)->wide_len) {
61
        DISPATCH_WIDE(strm, start);
62
        return;
63
    }
64
#endif
65
    /* start: inflate()'s starting value for strm->avail_out */
66
2.05k
    struct inflate_state *state;
67
2.05k
    z_const unsigned char *in;  /* local strm->next_in */
68
2.05k
    const unsigned char *last;  /* have enough input while in < last */
69
2.05k
    unsigned char *out;         /* local strm->next_out */
70
2.05k
    unsigned char *beg;         /* inflate()'s initial strm->next_out */
71
2.05k
    unsigned char *end;         /* while out < end, enough space available */
72
2.05k
    unsigned char *safe;        /* can use chunkcopy provided out < safe */
73
2.05k
    unsigned char *window;      /* allocated sliding window, if wsize != 0 */
74
2.05k
    unsigned wsize;             /* window size or zero if not using window */
75
2.05k
    unsigned whave;             /* valid bytes in the window */
76
2.05k
    unsigned wnext;             /* window write index */
77
78
    /* hold is a local copy of strm->hold. By default, hold satisfies the same
79
       invariants that strm->hold does, namely that (hold >> bits) == 0. This
80
       invariant is kept by loading bits into hold one byte at a time, like:
81
82
       hold |= next_byte_of_input << bits; in++; bits += 8;
83
84
       If we need to ensure that bits >= 15 then this code snippet is simply
85
       repeated. Over one iteration of the outermost do/while loop, this
86
       happens up to six times (48 bits of input), as described in the NOTES
87
       above.
88
89
       However, on some little endian architectures, it can be significantly
90
       faster to load 64 bits once instead of 8 bits six times:
91
92
       if (bits <= 16) {
93
         hold |= next_8_bytes_of_input << bits; in += 6; bits += 48;
94
       }
95
96
       Unlike the simpler one byte load, shifting the next_8_bytes_of_input
97
       by bits will overflow and lose those high bits, up to 2 bytes' worth.
98
       The conservative estimate is therefore that we have read only 6 bytes
99
       (48 bits). Again, as per the NOTES above, 48 bits is sufficient for the
100
       rest of the iteration, and we will not need to load another 8 bytes.
101
102
       Inside this function, we no longer satisfy (hold >> bits) == 0, but
103
       this is not problematic, even if that overflow does not land on an 8 bit
104
       byte boundary. Those excess bits will eventually shift down lower as the
105
       Huffman decoder consumes input, and when new input bits need to be loaded
106
       into the bits variable, the same input bits will be or'ed over those
107
       existing bits. A bitwise or is idempotent: (a | b | b) equals (a | b).
108
       Note that we therefore write that load operation as "hold |= etc" and not
109
       "hold += etc".
110
111
       Outside that loop, at the end of the function, hold is bitwise and'ed
112
       with (1<<bits)-1 to drop those excess bits so that, on function exit, we
113
       keep the invariant that (state->hold >> state->bits) == 0.
114
    */
115
2.05k
    bits_t bits;                /* local strm->bits */
116
2.05k
    uint64_t hold;              /* local strm->hold */
117
2.05k
    unsigned lmask;             /* mask for first level of length codes */
118
2.05k
    unsigned dmask;             /* mask for first level of distance codes */
119
2.05k
    code const *lcode;          /* local strm->lencode */
120
2.05k
    code const *dcode;          /* local strm->distcode */
121
2.05k
    code here;                  /* retrieved table entry */
122
2.05k
    unsigned op;                /* code bits, operation, extra bits, or */
123
                                /*  window position, window bytes to copy */
124
2.05k
    unsigned len;               /* match length, unused bytes */
125
2.05k
    unsigned char *from;        /* where to copy match from */
126
2.05k
    unsigned dist;              /* match distance */
127
2.05k
    uint64_t old;               /* look-behind buffer for extra bits */
128
129
    /* copy state to local variables */
130
2.05k
    state = (struct inflate_state *)strm->state;
131
2.05k
    in = strm->next_in;
132
2.05k
    last = in + (strm->avail_in - (INFLATE_FAST_MIN_HAVE - 1));
133
2.05k
    out = strm->next_out;
134
2.05k
    beg = out - (start - strm->avail_out);
135
2.05k
    safe = out + strm->avail_out;
136
2.05k
#ifdef USE_SAFE_MODE
137
2.05k
    end = safe - INFLATE_FAST_MIN_SAFE + 1;
138
#else
139
    end = safe - INFLATE_FAST_MIN_LEFT + 1;
140
#endif
141
2.05k
    wsize = state->wsize;
142
2.05k
    whave = state->whave;
143
2.05k
    wnext = state->wnext;
144
2.05k
    window = state->window;
145
2.05k
    hold = state->hold;
146
2.05k
    bits = (bits_t)state->bits;
147
2.05k
    lcode = state->lencode;
148
2.05k
    dcode = state->distcode;
149
2.05k
    lmask = (1U << state->lenbits) - 1;
150
2.05k
    dmask = (1U << state->distbits) - 1;
151
152
    /* Ensure enough bits for the first length table lookup. Every refill after
153
       this one is issued behind a table lookup that only needs bits already in
154
       hold, so the refill's load latency overlaps with the table load instead
155
       of feeding it. */
156
2.05k
    REFILL();
157
158
    /* decode literals and length/distances until end-of-block or not enough
159
       input data or output space */
160
54.7k
    do {
161
        /* This lookup needs bits >= MAX_LEN_ROOT_BITS, which every path here
162
           guarantees: each refill leaves bits >= 56, the literal-only paths
163
           loop after consuming at most 2*MAX_LEN_ROOT_BITS + MAX_BITS bits, and
164
           the distance path refills to at least MAX_BITS + MAX_DIST_EXTRA_BITS
165
           + MAX_LEN_ROOT_BITS before consuming at most MAX_BITS +
166
           MAX_DIST_EXTRA_BITS. */
167
54.7k
        here = lcode[hold & lmask];
168
54.7k
        Z_TOUCH(here);
169
        /* No-op on the first iteration: the pre-loop refill already filled hold
170
           and the lookup above consumes nothing, so bits is still >= 56. Tops
171
           hold back up on every later iteration. */
172
54.7k
        REFILL();
173
54.7k
        old = hold;
174
54.7k
        DROPBITS(here.bits);
175
54.7k
        if (LIKELY(here.op == 0)) {
176
47.4k
            TRACE_LITERAL(here.val);
177
47.4k
            *out++ = (unsigned char)(here.val);
178
47.4k
            here = lcode[hold & lmask];
179
47.4k
            Z_TOUCH(here);
180
47.4k
            old = hold;
181
47.4k
            DROPBITS(here.bits);
182
47.4k
            if (LIKELY(here.op == 0)) {
183
42.2k
                TRACE_LITERAL(here.val);
184
42.2k
                *out++ = (unsigned char)(here.val);
185
42.2k
                here = lcode[hold & lmask];
186
42.2k
                Z_TOUCH(here);
187
43.9k
            dolen:
188
43.9k
                old = hold;
189
43.9k
                DROPBITS(here.bits);
190
43.9k
                if (LIKELY(here.op == 0)) {
191
41.4k
                    TRACE_LITERAL(here.val);
192
41.4k
                    *out++ = (unsigned char)(here.val);
193
41.4k
                    continue;
194
41.4k
                }
195
43.9k
            }
196
47.4k
        }
197
14.9k
        op = here.op;
198
14.9k
        if (LIKELY(op & 16)) {                  /* length base */
199
13.0k
            len = here.val + EXTRA_BITS(old, here, op);
200
13.0k
            TRACE_LENGTH(len);
201
13.0k
            here = dcode[hold & dmask];
202
13.0k
            Z_TOUCH(here);
203
            /* Reserve bits for the distance code and its extra bits, plus the
204
               next iteration's length table lookup, which happens before the
205
               next refill. */
206
13.0k
            if (UNLIKELY(bits < MAX_BITS + MAX_DIST_EXTRA_BITS + MAX_LEN_ROOT_BITS)) {
207
857
                REFILL();
208
857
            }
209
13.2k
          dodist:
210
13.2k
            old = hold;
211
13.2k
            DROPBITS(here.bits);
212
13.2k
            op = here.op;
213
13.2k
            if (LIKELY(op & 16)) {              /* distance base */
214
13.0k
                dist = here.val + EXTRA_BITS(old, here, op);
215
#ifdef INFLATE_STRICT
216
                if (UNLIKELY(dist > state->dmax)) {
217
                    SET_BAD("invalid distance too far back");
218
                    break;
219
                }
220
#endif
221
13.0k
                TRACE_DISTANCE(dist);
222
223
13.0k
#ifdef USE_SAFE_MODE
224
                /* If there isn't enough output space for the full copy, bail to the
225
                   slow path's MATCH state which handles partial copies. */
226
13.0k
                if (UNLIKELY(len > (unsigned)(safe - out))) {
227
290
                    state->mode = MATCH;
228
290
                    state->length = len;
229
290
                    state->was = len;
230
290
                    state->offset = dist;
231
290
                    break;
232
290
                }
233
12.8k
#endif
234
235
12.8k
                op = (unsigned)(out - beg);     /* max distance in output */
236
12.8k
                if (UNLIKELY(dist > op)) {      /* see if copy from window */
237
1.95k
                    op = dist - op;             /* distance back in window */
238
1.95k
                    if (UNLIKELY(op > whave)) {
239
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
240
                        if (LIKELY(state->sane)) {
241
                            SET_BAD("invalid distance too far back");
242
                            break;
243
                        }
244
                        unsigned gap = op - whave;
245
                        unsigned zeros = MIN(len, gap);
246
                        memset(out, 0, zeros);  /* fill missing bytes with zeros */
247
                        out += zeros;
248
                        len -= zeros;
249
                        if (UNLIKELY(len == 0))
250
                            continue;
251
                        op = whave;
252
                        if (UNLIKELY(op == 0)) {/* copy from already-decoded output */
253
                            out = chunkcopy_safe(out, out - dist, len, safe);
254
                            continue;
255
                        }
256
#else
257
0
                        SET_BAD("invalid distance too far back");
258
0
                        break;
259
0
#endif
260
0
                    }
261
1.95k
                    from = window;
262
1.95k
                    if (LIKELY(wnext == 0)) {           /* very common case */
263
1.95k
                        from += wsize - op;
264
1.95k
                    } else if (LIKELY(wnext >= op)) {   /* contiguous in window */
265
0
                        from += wnext - op;
266
0
                    } else {                            /* wrap around window */
267
0
                        op -= wnext;
268
0
                        from += wsize - op;
269
0
                        if (UNLIKELY(op < len)) {       /* some from end of window */
270
0
                            len -= op;
271
0
                            out = CHUNKCOPY_SAFE(out, from, op, safe);
272
0
                            from = window;              /* more from start of window */
273
0
                            op = wnext;
274
                            /* This (rare) case can create a situation where
275
                               the first chunkcopy below must be checked.
276
                             */
277
0
                        }
278
0
                    }
279
1.95k
                    if (UNLIKELY(op < len)) {           /* still need some from output */
280
56
                        len -= op;
281
#ifndef USE_SAFE_MODE
282
                        out = CHUNKCOPY_SAFE(out, from, op, safe);
283
                        out = CHUNKUNROLL(out, &dist, &len);
284
                        out = CHUNKCOPY_SAFE(out, out - dist, len, safe);
285
#elif defined(HAVE_MASKED_READWRITE)
286
                        out = CHUNKCOPY_SAFE(out, from, op, safe);
287
                        out = CHUNKCOPY_SAFE(out, out - dist, len, safe);
288
#else
289
56
                        out = chunkcopy_safe(out, from, op, safe);
290
56
                        out = chunkcopy_safe(out, out - dist, len, safe);
291
56
#endif
292
1.89k
                    } else {
293
#if !defined(USE_SAFE_MODE) || defined(HAVE_MASKED_READWRITE)
294
                        out = CHUNKCOPY_SAFE(out, from, len, safe);
295
#else
296
1.89k
                        out = chunkcopy_safe(out, from, len, safe);
297
1.89k
#endif
298
1.89k
                    }
299
10.8k
                } else {
300
#ifndef USE_SAFE_MODE
301
                    /* Whole reference is in range of current output.  No range checks are
302
                       necessary because we start with room for at least 258 bytes of output,
303
                       so unroll and roundoff operations can write beyond `out+len` so long
304
                       as they stay within 258 bytes of `out`.
305
                    */
306
#ifdef USE_WIDE_COPY
307
                    if (LIKELY(dist >= len || dist >= 2 * CHUNKSIZE()))
308
                        out = DOUBLECHUNKCOPY(out, out - dist, len);
309
#else
310
                    if (LIKELY(dist >= len || dist > CHUNKSIZE()))
311
                        out = CHUNKCOPY(out, out - dist, len);
312
#endif
313
                    else
314
                        out = CHUNKMEMSET(out, out - dist, len);
315
#elif defined(HAVE_MASKED_READWRITE)
316
                    out = CHUNKCOPY_SAFE(out, out - dist, len, safe);
317
#else
318
10.8k
                    out = chunkcopy_safe(out, out - dist, len, safe);
319
10.8k
#endif
320
10.8k
                }
321
12.8k
            } else if (UNLIKELY((op & 64) == 0)) {          /* 2nd level distance code */
322
121
                here = dcode[here.val + BITS(op)];
323
121
                Z_TOUCH(here);
324
121
                goto dodist;
325
121
            } else {
326
0
                SET_BAD("invalid distance code");
327
0
                break;
328
0
            }
329
13.2k
        } else if (UNLIKELY((op & 64) == 0)) {              /* 2nd level length code */
330
1.68k
            here = lcode[here.val + BITS(op)];
331
1.68k
            Z_TOUCH(here);
332
1.68k
            goto dolen;
333
1.68k
        } else if (UNLIKELY(op & 32)) {                     /* end-of-block */
334
217
            TRACE_END_OF_BLOCK();
335
217
            state->mode = TYPE;
336
217
            break;
337
217
        } else {
338
0
            SET_BAD("invalid literal/length code");
339
0
            break;
340
0
        }
341
54.2k
    } while (in < last && out < end);
342
343
    /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
344
2.05k
    len = bits >> 3;
345
2.05k
    in -= len;
346
2.05k
    bits -= (bits_t)(len << 3);
347
2.05k
    hold &= (UINT64_C(1) << bits) - 1;
348
349
    /* update state and return */
350
2.05k
    strm->next_in = in;
351
2.05k
    strm->next_out = out;
352
2.05k
    strm->avail_in = (unsigned)(in < last ? (INFLATE_FAST_MIN_HAVE - 1) + (last - in)
353
2.05k
                                          : (INFLATE_FAST_MIN_HAVE - 1) - (in - last));
354
2.05k
    strm->avail_out = (unsigned)(safe - out);
355
356
2.05k
    Assert(bits <= 32, "Remaining bits greater than 32");
357
2.05k
    state->hold = (uint32_t)hold;
358
2.05k
    state->bits = bits;
359
2.05k
    return;
360
2.05k
}
Unexecuted instantiation: inflate_fast_avx512
Unexecuted instantiation: inflate_fast_safe_avx512
361
362
/*
363
   inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
364
   - Using bit fields for code structure
365
   - Different op definition to avoid & for extra bits (do & for table bits)
366
   - Three separate decoding do-loops for direct, window, and wnext == 0
367
   - Special case for distance > 1 copies to do overlapped load and store copy
368
   - Explicit branch predictions (based on measured branch probabilities)
369
   - Deferring match copy and interspersed it with decoding subsequent codes
370
   - Swapping literal/length else
371
   - Swapping window/direct else
372
   - Larger unrolled copy loops (three is about right)
373
   - Moving len -= 3 statement into middle of loop
374
 */
375
376
 // Cleanup
377
#undef INFLATE_FAST
378
#undef DISPATCH_WIDE
379
#undef USE_WIDE_COPY
380
381
/* The chunkset macros are shared by every instantiation, so they can only be
382
   released once the safe one, which is emitted last, has been emitted. */
383
#ifdef USE_SAFE_MODE
384
#  undef CHUNKCOPY
385
#  undef CHUNKMEMSET
386
#  undef CHUNKMEMSET_SAFE
387
#  undef CHUNKSIZE
388
#  undef CHUNKUNROLL
389
#  undef HAVE_CHUNKCOPY
390
#  undef HAVE_CHUNKMEMSET_2
391
#  undef HAVE_CHUNKMEMSET_4
392
#  undef HAVE_CHUNKMEMSET_8
393
#  undef HAVE_CHUNKMEMSET_16
394
#  undef HAVE_CHUNK_MAG
395
#  undef HAVE_HALFCHUNKCOPY
396
#  undef HAVE_HALF_CHUNK
397
#  undef HAVE_MASKED_READWRITE
398
#  undef USE_SAFE_MODE
399
#endif