Coverage Report

Created: 2026-12-01 06:39

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
9.52M
static void INFLATE_FAST(PREFIX3(stream) *strm, uint32_t start) {
54
#else
55
9.66M
void Z_INTERNAL INFLATE_FAST(PREFIX3(stream) *strm, uint32_t start) {
56
9.66M
#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
9.57M
    if (((struct inflate_state *)strm->state)->wide_len) {
61
9.52M
        DISPATCH_WIDE(strm, start);
62
9.52M
        return;
63
9.52M
    }
64
53.7k
#endif
65
    /* start: inflate()'s starting value for strm->avail_out */
66
53.7k
    struct inflate_state *state;
67
9.66M
    z_const unsigned char *in;  /* local strm->next_in */
68
53.7k
    const unsigned char *last;  /* have enough input while in < last */
69
53.7k
    unsigned char *out;         /* local strm->next_out */
70
53.7k
    unsigned char *beg;         /* inflate()'s initial strm->next_out */
71
53.7k
    unsigned char *end;         /* while out < end, enough space available */
72
53.7k
    unsigned char *safe;        /* can use chunkcopy provided out < safe */
73
53.7k
    unsigned char *window;      /* allocated sliding window, if wsize != 0 */
74
53.7k
    unsigned wsize;             /* window size or zero if not using window */
75
53.7k
    unsigned whave;             /* valid bytes in the window */
76
53.7k
    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
53.7k
    bits_t bits;                /* local strm->bits */
116
53.7k
    uint64_t hold;              /* local strm->hold */
117
53.7k
    unsigned lmask;             /* mask for first level of length codes */
118
53.7k
    unsigned dmask;             /* mask for first level of distance codes */
119
53.7k
    code const *lcode;          /* local strm->lencode */
120
53.7k
    code const *dcode;          /* local strm->distcode */
121
53.7k
    code here;                  /* retrieved table entry */
122
53.7k
    unsigned op;                /* code bits, operation, extra bits, or */
123
                                /*  window position, window bytes to copy */
124
53.7k
    unsigned len;               /* match length, unused bytes */
125
53.7k
    unsigned char *from;        /* where to copy match from */
126
53.7k
    unsigned dist;              /* match distance */
127
53.7k
    uint64_t old;               /* look-behind buffer for extra bits */
128
129
    /* copy state to local variables */
130
53.7k
    state = (struct inflate_state *)strm->state;
131
53.7k
    in = strm->next_in;
132
9.66M
    last = in + (strm->avail_in - (INFLATE_FAST_MIN_HAVE - 1));
133
53.7k
    out = strm->next_out;
134
53.7k
    beg = out - (start - strm->avail_out);
135
53.7k
    safe = out + strm->avail_out;
136
#ifdef USE_SAFE_MODE
137
85.8k
    end = safe - INFLATE_FAST_MIN_SAFE + 1;
138
#else
139
9.57M
    end = safe - INFLATE_FAST_MIN_LEFT + 1;
140
#endif
141
53.7k
    wsize = state->wsize;
142
53.7k
    whave = state->whave;
143
53.7k
    wnext = state->wnext;
144
53.7k
    window = state->window;
145
53.7k
    hold = state->hold;
146
53.7k
    bits = (bits_t)state->bits;
147
53.7k
    lcode = state->lencode;
148
53.7k
    dcode = state->distcode;
149
53.7k
    lmask = (1U << state->lenbits) - 1;
150
53.7k
    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
9.66M
    REFILL();
157
158
    /* decode literals and length/distances until end-of-block or not enough
159
       input data or output space */
160
302M
    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
302M
        here = lcode[hold & lmask];
168
302M
        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
302M
        REFILL();
173
302M
        old = hold;
174
302M
        DROPBITS(here.bits);
175
302M
        if (LIKELY(here.op == 0)) {
176
269M
            TRACE_LITERAL(here.val);
177
269M
            *out++ = (unsigned char)(here.val);
178
269M
            here = lcode[hold & lmask];
179
269M
            Z_TOUCH(here);
180
269M
            old = hold;
181
269M
            DROPBITS(here.bits);
182
269M
            if (LIKELY(here.op == 0)) {
183
259M
                TRACE_LITERAL(here.val);
184
259M
                *out++ = (unsigned char)(here.val);
185
259M
                here = lcode[hold & lmask];
186
259M
                Z_TOUCH(here);
187
261M
            dolen:
188
261M
                old = hold;
189
261M
                DROPBITS(here.bits);
190
261M
                if (LIKELY(here.op == 0)) {
191
254M
                    TRACE_LITERAL(here.val);
192
254M
                    *out++ = (unsigned char)(here.val);
193
254M
                    continue;
194
254M
                }
195
261M
            }
196
269M
        }
197
49.7M
        op = here.op;
198
49.7M
        if (LIKELY(op & 16)) {                  /* length base */
199
37.5M
            len = here.val + EXTRA_BITS(old, here, op);
200
37.5M
            TRACE_LENGTH(len);
201
37.5M
            here = dcode[hold & dmask];
202
37.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
37.5M
            if (UNLIKELY(bits < MAX_BITS + MAX_DIST_EXTRA_BITS + MAX_LEN_ROOT_BITS)) {
207
2.58M
                REFILL();
208
2.58M
            }
209
37.6M
          dodist:
210
37.6M
            old = hold;
211
37.6M
            DROPBITS(here.bits);
212
37.6M
            op = here.op;
213
37.6M
            if (LIKELY(op & 16)) {              /* distance base */
214
37.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
37.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
220k
                if (UNLIKELY(len > (unsigned)(safe - out))) {
227
1.96k
                    state->mode = MATCH;
228
1.96k
                    state->length = len;
229
1.96k
                    state->was = len;
230
1.96k
                    state->offset = dist;
231
1.96k
                    break;
232
1.96k
                }
233
218k
#endif
234
235
218k
                op = (unsigned)(out - beg);     /* max distance in output */
236
37.5M
                if (UNLIKELY(dist > op)) {      /* see if copy from window */
237
449k
                    op = dist - op;             /* distance back in window */
238
449k
                    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
956
                        SET_BAD("invalid distance too far back");
258
956
                        break;
259
956
#endif
260
956
                    }
261
448k
                    from = window;
262
448k
                    if (LIKELY(wnext == 0)) {           /* very common case */
263
346k
                        from += wsize - op;
264
346k
                    } else if (LIKELY(wnext >= op)) {   /* contiguous in window */
265
58.7k
                        from += wnext - op;
266
58.7k
                    } else {                            /* wrap around window */
267
43.1k
                        op -= wnext;
268
43.1k
                        from += wsize - op;
269
43.1k
                        if (UNLIKELY(op < len)) {       /* some from end of window */
270
117
                            len -= op;
271
117
                            out = CHUNKCOPY_SAFE(out, from, op, safe);
272
117
                            from = window;              /* more from start of window */
273
117
                            op = wnext;
274
                            /* This (rare) case can create a situation where
275
                               the first chunkcopy below must be checked.
276
                             */
277
117
                        }
278
43.1k
                    }
279
448k
                    if (UNLIKELY(op < len)) {           /* still need some from output */
280
6.63k
                        len -= op;
281
#ifndef USE_SAFE_MODE
282
                        out = CHUNKCOPY_SAFE(out, from, op, safe);
283
2.05k
                        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
441k
                    } 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
441k
                    }
299
37.1M
                } 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
32.6M
                    if (LIKELY(dist >= len || dist >= 2 * CHUNKSIZE()))
308
26.3M
                        out = DOUBLECHUNKCOPY(out, out - dist, len);
309
#else
310
4.21M
                    if (LIKELY(dist >= len || dist > CHUNKSIZE()))
311
3.73M
                        out = CHUNKCOPY(out, out - dist, len);
312
475k
#endif
313
475k
                    else
314
6.84M
                        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
37.1M
                }
321
218k
            } else if (UNLIKELY((op & 64) == 0)) {          /* 2nd level distance code */
322
61.0k
                here = dcode[here.val + BITS(op)];
323
61.0k
                Z_TOUCH(here);
324
61.0k
                goto dodist;
325
61.0k
            } else {
326
72
                SET_BAD("invalid distance code");
327
72
                break;
328
72
            }
329
37.6M
        } else if (UNLIKELY((op & 64) == 0)) {              /* 2nd level length code */
330
2.69M
            here = lcode[here.val + BITS(op)];
331
2.69M
            Z_TOUCH(here);
332
2.69M
            goto dolen;
333
9.50M
        } else if (UNLIKELY(op & 32)) {                     /* end-of-block */
334
9.50M
            TRACE_END_OF_BLOCK();
335
9.50M
            state->mode = TYPE;
336
9.50M
            break;
337
9.50M
        } else {
338
57
            SET_BAD("invalid literal/length code");
339
57
            break;
340
57
        }
341
292M
    } while (in < last && out < end);
342
343
    /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
344
9.66M
    len = bits >> 3;
345
9.66M
    in -= len;
346
9.66M
    bits -= (bits_t)(len << 3);
347
9.66M
    hold &= (UINT64_C(1) << bits) - 1;
348
349
    /* update state and return */
350
9.66M
    strm->next_in = in;
351
9.66M
    strm->next_out = out;
352
9.66M
    strm->avail_in = (unsigned)(in < last ? (INFLATE_FAST_MIN_HAVE - 1) + (last - in)
353
9.66M
                                          : (INFLATE_FAST_MIN_HAVE - 1) - (in - last));
354
9.66M
    strm->avail_out = (unsigned)(safe - out);
355
356
9.66M
    Assert(bits <= 32, "Remaining bits greater than 32");
357
9.66M
    state->hold = (uint32_t)hold;
358
9.66M
    state->bits = bits;
359
9.66M
    return;
360
53.7k
}
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
9.52M
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
9.52M
    struct inflate_state *state;
67
9.52M
    z_const unsigned char *in;  /* local strm->next_in */
68
9.52M
    const unsigned char *last;  /* have enough input while in < last */
69
9.52M
    unsigned char *out;         /* local strm->next_out */
70
9.52M
    unsigned char *beg;         /* inflate()'s initial strm->next_out */
71
9.52M
    unsigned char *end;         /* while out < end, enough space available */
72
9.52M
    unsigned char *safe;        /* can use chunkcopy provided out < safe */
73
9.52M
    unsigned char *window;      /* allocated sliding window, if wsize != 0 */
74
9.52M
    unsigned wsize;             /* window size or zero if not using window */
75
9.52M
    unsigned whave;             /* valid bytes in the window */
76
9.52M
    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
9.52M
    bits_t bits;                /* local strm->bits */
116
9.52M
    uint64_t hold;              /* local strm->hold */
117
9.52M
    unsigned lmask;             /* mask for first level of length codes */
118
9.52M
    unsigned dmask;             /* mask for first level of distance codes */
119
9.52M
    code const *lcode;          /* local strm->lencode */
120
9.52M
    code const *dcode;          /* local strm->distcode */
121
9.52M
    code here;                  /* retrieved table entry */
122
9.52M
    unsigned op;                /* code bits, operation, extra bits, or */
123
                                /*  window position, window bytes to copy */
124
9.52M
    unsigned len;               /* match length, unused bytes */
125
9.52M
    unsigned char *from;        /* where to copy match from */
126
9.52M
    unsigned dist;              /* match distance */
127
9.52M
    uint64_t old;               /* look-behind buffer for extra bits */
128
129
    /* copy state to local variables */
130
9.52M
    state = (struct inflate_state *)strm->state;
131
9.52M
    in = strm->next_in;
132
9.52M
    last = in + (strm->avail_in - (INFLATE_FAST_MIN_HAVE - 1));
133
9.52M
    out = strm->next_out;
134
9.52M
    beg = out - (start - strm->avail_out);
135
9.52M
    safe = out + strm->avail_out;
136
#ifdef USE_SAFE_MODE
137
    end = safe - INFLATE_FAST_MIN_SAFE + 1;
138
#else
139
9.52M
    end = safe - INFLATE_FAST_MIN_LEFT + 1;
140
9.52M
#endif
141
9.52M
    wsize = state->wsize;
142
9.52M
    whave = state->whave;
143
9.52M
    wnext = state->wnext;
144
9.52M
    window = state->window;
145
9.52M
    hold = state->hold;
146
9.52M
    bits = (bits_t)state->bits;
147
9.52M
    lcode = state->lencode;
148
9.52M
    dcode = state->distcode;
149
9.52M
    lmask = (1U << state->lenbits) - 1;
150
9.52M
    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
9.52M
    REFILL();
157
158
    /* decode literals and length/distances until end-of-block or not enough
159
       input data or output space */
160
201M
    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
201M
        here = lcode[hold & lmask];
168
201M
        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
201M
        REFILL();
173
201M
        old = hold;
174
201M
        DROPBITS(here.bits);
175
201M
        if (LIKELY(here.op == 0)) {
176
171M
            TRACE_LITERAL(here.val);
177
171M
            *out++ = (unsigned char)(here.val);
178
171M
            here = lcode[hold & lmask];
179
171M
            Z_TOUCH(here);
180
171M
            old = hold;
181
171M
            DROPBITS(here.bits);
182
171M
            if (LIKELY(here.op == 0)) {
183
161M
                TRACE_LITERAL(here.val);
184
161M
                *out++ = (unsigned char)(here.val);
185
161M
                here = lcode[hold & lmask];
186
161M
                Z_TOUCH(here);
187
163M
            dolen:
188
163M
                old = hold;
189
163M
                DROPBITS(here.bits);
190
163M
                if (LIKELY(here.op == 0)) {
191
158M
                    TRACE_LITERAL(here.val);
192
158M
                    *out++ = (unsigned char)(here.val);
193
158M
                    continue;
194
158M
                }
195
163M
            }
196
171M
        }
197
43.9M
        op = here.op;
198
43.9M
        if (LIKELY(op & 16)) {                  /* length base */
199
33.0M
            len = here.val + EXTRA_BITS(old, here, op);
200
33.0M
            TRACE_LENGTH(len);
201
33.0M
            here = dcode[hold & dmask];
202
33.0M
            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
33.0M
            if (UNLIKELY(bits < MAX_BITS + MAX_DIST_EXTRA_BITS + MAX_LEN_ROOT_BITS)) {
207
1.90M
                REFILL();
208
1.90M
            }
209
33.0M
          dodist:
210
33.0M
            old = hold;
211
33.0M
            DROPBITS(here.bits);
212
33.0M
            op = here.op;
213
33.0M
            if (LIKELY(op & 16)) {              /* distance base */
214
33.0M
                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
33.0M
                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
33.0M
                op = (unsigned)(out - beg);     /* max distance in output */
236
33.0M
                if (UNLIKELY(dist > op)) {      /* see if copy from window */
237
347k
                    op = dist - op;             /* distance back in window */
238
347k
                    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
719
                        SET_BAD("invalid distance too far back");
258
719
                        break;
259
719
#endif
260
719
                    }
261
346k
                    from = window;
262
346k
                    if (LIKELY(wnext == 0)) {           /* very common case */
263
273k
                        from += wsize - op;
264
273k
                    } else if (LIKELY(wnext >= op)) {   /* contiguous in window */
265
39.8k
                        from += wnext - op;
266
39.8k
                    } else {                            /* wrap around window */
267
32.7k
                        op -= wnext;
268
32.7k
                        from += wsize - op;
269
32.7k
                        if (UNLIKELY(op < len)) {       /* some from end of window */
270
96
                            len -= op;
271
96
                            out = CHUNKCOPY_SAFE(out, from, op, safe);
272
96
                            from = window;              /* more from start of window */
273
96
                            op = wnext;
274
                            /* This (rare) case can create a situation where
275
                               the first chunkcopy below must be checked.
276
                             */
277
96
                        }
278
32.7k
                    }
279
346k
                    if (UNLIKELY(op < len)) {           /* still need some from output */
280
1.91k
                        len -= op;
281
1.91k
#ifndef USE_SAFE_MODE
282
1.91k
                        out = CHUNKCOPY_SAFE(out, from, op, safe);
283
1.91k
                        out = CHUNKUNROLL(out, &dist, &len);
284
1.91k
                        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
344k
                    } else {
293
344k
#if !defined(USE_SAFE_MODE) || defined(HAVE_MASKED_READWRITE)
294
344k
                        out = CHUNKCOPY_SAFE(out, from, len, safe);
295
#else
296
                        out = chunkcopy_safe(out, from, len, safe);
297
#endif
298
344k
                    }
299
32.6M
                } else {
300
32.6M
#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
32.6M
#ifdef USE_WIDE_COPY
307
32.6M
                    if (LIKELY(dist >= len || dist >= 2 * CHUNKSIZE()))
308
26.3M
                        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
6.36M
                    else
314
6.36M
                        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
32.6M
                }
321
33.0M
            } else if (UNLIKELY((op & 64) == 0)) {          /* 2nd level distance code */
322
53.0k
                here = dcode[here.val + BITS(op)];
323
53.0k
                Z_TOUCH(here);
324
53.0k
                goto dodist;
325
53.0k
            } else {
326
54
                SET_BAD("invalid distance code");
327
54
                break;
328
54
            }
329
33.0M
        } else if (UNLIKELY((op & 64) == 0)) {              /* 2nd level length code */
330
1.45M
            here = lcode[here.val + BITS(op)];
331
1.45M
            Z_TOUCH(here);
332
1.45M
            goto dolen;
333
9.46M
        } else if (UNLIKELY(op & 32)) {                     /* end-of-block */
334
9.46M
            TRACE_END_OF_BLOCK();
335
9.46M
            state->mode = TYPE;
336
9.46M
            break;
337
9.46M
        } else {
338
45
            SET_BAD("invalid literal/length code");
339
45
            break;
340
45
        }
341
191M
    } while (in < last && out < end);
342
343
    /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
344
9.52M
    len = bits >> 3;
345
9.52M
    in -= len;
346
9.52M
    bits -= (bits_t)(len << 3);
347
9.52M
    hold &= (UINT64_C(1) << bits) - 1;
348
349
    /* update state and return */
350
9.52M
    strm->next_in = in;
351
9.52M
    strm->next_out = out;
352
9.52M
    strm->avail_in = (unsigned)(in < last ? (INFLATE_FAST_MIN_HAVE - 1) + (last - in)
353
9.52M
                                          : (INFLATE_FAST_MIN_HAVE - 1) - (in - last));
354
9.52M
    strm->avail_out = (unsigned)(safe - out);
355
356
9.52M
    Assert(bits <= 32, "Remaining bits greater than 32");
357
9.52M
    state->hold = (uint32_t)hold;
358
9.52M
    state->bits = bits;
359
9.52M
    return;
360
9.52M
}
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
9.57M
void Z_INTERNAL INFLATE_FAST(PREFIX3(stream) *strm, uint32_t start) {
56
9.57M
#endif
57
9.57M
#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
9.57M
    if (((struct inflate_state *)strm->state)->wide_len) {
61
9.52M
        DISPATCH_WIDE(strm, start);
62
9.52M
        return;
63
9.52M
    }
64
53.7k
#endif
65
    /* start: inflate()'s starting value for strm->avail_out */
66
53.7k
    struct inflate_state *state;
67
53.7k
    z_const unsigned char *in;  /* local strm->next_in */
68
53.7k
    const unsigned char *last;  /* have enough input while in < last */
69
53.7k
    unsigned char *out;         /* local strm->next_out */
70
53.7k
    unsigned char *beg;         /* inflate()'s initial strm->next_out */
71
53.7k
    unsigned char *end;         /* while out < end, enough space available */
72
53.7k
    unsigned char *safe;        /* can use chunkcopy provided out < safe */
73
53.7k
    unsigned char *window;      /* allocated sliding window, if wsize != 0 */
74
53.7k
    unsigned wsize;             /* window size or zero if not using window */
75
53.7k
    unsigned whave;             /* valid bytes in the window */
76
53.7k
    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
53.7k
    bits_t bits;                /* local strm->bits */
116
53.7k
    uint64_t hold;              /* local strm->hold */
117
53.7k
    unsigned lmask;             /* mask for first level of length codes */
118
53.7k
    unsigned dmask;             /* mask for first level of distance codes */
119
53.7k
    code const *lcode;          /* local strm->lencode */
120
53.7k
    code const *dcode;          /* local strm->distcode */
121
53.7k
    code here;                  /* retrieved table entry */
122
53.7k
    unsigned op;                /* code bits, operation, extra bits, or */
123
                                /*  window position, window bytes to copy */
124
53.7k
    unsigned len;               /* match length, unused bytes */
125
53.7k
    unsigned char *from;        /* where to copy match from */
126
53.7k
    unsigned dist;              /* match distance */
127
53.7k
    uint64_t old;               /* look-behind buffer for extra bits */
128
129
    /* copy state to local variables */
130
53.7k
    state = (struct inflate_state *)strm->state;
131
53.7k
    in = strm->next_in;
132
53.7k
    last = in + (strm->avail_in - (INFLATE_FAST_MIN_HAVE - 1));
133
53.7k
    out = strm->next_out;
134
53.7k
    beg = out - (start - strm->avail_out);
135
53.7k
    safe = out + strm->avail_out;
136
#ifdef USE_SAFE_MODE
137
    end = safe - INFLATE_FAST_MIN_SAFE + 1;
138
#else
139
53.7k
    end = safe - INFLATE_FAST_MIN_LEFT + 1;
140
53.7k
#endif
141
53.7k
    wsize = state->wsize;
142
53.7k
    whave = state->whave;
143
53.7k
    wnext = state->wnext;
144
53.7k
    window = state->window;
145
53.7k
    hold = state->hold;
146
53.7k
    bits = (bits_t)state->bits;
147
53.7k
    lcode = state->lencode;
148
53.7k
    dcode = state->distcode;
149
53.7k
    lmask = (1U << state->lenbits) - 1;
150
53.7k
    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
53.7k
    REFILL();
157
158
    /* decode literals and length/distances until end-of-block or not enough
159
       input data or output space */
160
100M
    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
100M
        here = lcode[hold & lmask];
168
100M
        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
100M
        REFILL();
173
100M
        old = hold;
174
100M
        DROPBITS(here.bits);
175
100M
        if (LIKELY(here.op == 0)) {
176
97.8M
            TRACE_LITERAL(here.val);
177
97.8M
            *out++ = (unsigned char)(here.val);
178
97.8M
            here = lcode[hold & lmask];
179
97.8M
            Z_TOUCH(here);
180
97.8M
            old = hold;
181
97.8M
            DROPBITS(here.bits);
182
97.8M
            if (LIKELY(here.op == 0)) {
183
96.4M
                TRACE_LITERAL(here.val);
184
96.4M
                *out++ = (unsigned char)(here.val);
185
96.4M
                here = lcode[hold & lmask];
186
96.4M
                Z_TOUCH(here);
187
97.6M
            dolen:
188
97.6M
                old = hold;
189
97.6M
                DROPBITS(here.bits);
190
97.6M
                if (LIKELY(here.op == 0)) {
191
95.6M
                    TRACE_LITERAL(here.val);
192
95.6M
                    *out++ = (unsigned char)(here.val);
193
95.6M
                    continue;
194
95.6M
                }
195
97.6M
            }
196
97.8M
        }
197
5.57M
        op = here.op;
198
5.57M
        if (LIKELY(op & 16)) {                  /* length base */
199
4.30M
            len = here.val + EXTRA_BITS(old, here, op);
200
4.30M
            TRACE_LENGTH(len);
201
4.30M
            here = dcode[hold & dmask];
202
4.30M
            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
4.30M
            if (UNLIKELY(bits < MAX_BITS + MAX_DIST_EXTRA_BITS + MAX_LEN_ROOT_BITS)) {
207
659k
                REFILL();
208
659k
            }
209
4.31M
          dodist:
210
4.31M
            old = hold;
211
4.31M
            DROPBITS(here.bits);
212
4.31M
            op = here.op;
213
4.31M
            if (LIKELY(op & 16)) {              /* distance base */
214
4.30M
                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
4.30M
                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
4.30M
                op = (unsigned)(out - beg);     /* max distance in output */
236
4.30M
                if (UNLIKELY(dist > op)) {      /* see if copy from window */
237
90.6k
                    op = dist - op;             /* distance back in window */
238
90.6k
                    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
43
                        SET_BAD("invalid distance too far back");
258
43
                        break;
259
43
#endif
260
43
                    }
261
90.5k
                    from = window;
262
90.5k
                    if (LIKELY(wnext == 0)) {           /* very common case */
263
70.6k
                        from += wsize - op;
264
70.6k
                    } else if (LIKELY(wnext >= op)) {   /* contiguous in window */
265
9.59k
                        from += wnext - op;
266
10.3k
                    } else {                            /* wrap around window */
267
10.3k
                        op -= wnext;
268
10.3k
                        from += wsize - op;
269
10.3k
                        if (UNLIKELY(op < len)) {       /* some from end of window */
270
21
                            len -= op;
271
21
                            out = CHUNKCOPY_SAFE(out, from, op, safe);
272
21
                            from = window;              /* more from start of window */
273
21
                            op = wnext;
274
                            /* This (rare) case can create a situation where
275
                               the first chunkcopy below must be checked.
276
                             */
277
21
                        }
278
10.3k
                    }
279
90.5k
                    if (UNLIKELY(op < len)) {           /* still need some from output */
280
141
                        len -= op;
281
141
#ifndef USE_SAFE_MODE
282
141
                        out = CHUNKCOPY_SAFE(out, from, op, safe);
283
141
                        out = CHUNKUNROLL(out, &dist, &len);
284
141
                        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
90.4k
                    } else {
293
90.4k
#if !defined(USE_SAFE_MODE) || defined(HAVE_MASKED_READWRITE)
294
90.4k
                        out = CHUNKCOPY_SAFE(out, from, len, safe);
295
#else
296
                        out = chunkcopy_safe(out, from, len, safe);
297
#endif
298
90.4k
                    }
299
4.21M
                } else {
300
4.21M
#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
4.21M
                    if (LIKELY(dist >= len || dist > CHUNKSIZE()))
311
3.73M
                        out = CHUNKCOPY(out, out - dist, len);
312
475k
#endif
313
475k
                    else
314
475k
                        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
4.21M
                }
321
4.30M
            } else if (UNLIKELY((op & 64) == 0)) {          /* 2nd level distance code */
322
7.22k
                here = dcode[here.val + BITS(op)];
323
7.22k
                Z_TOUCH(here);
324
7.22k
                goto dodist;
325
7.22k
            } else {
326
5
                SET_BAD("invalid distance code");
327
5
                break;
328
5
            }
329
4.31M
        } else if (UNLIKELY((op & 64) == 0)) {              /* 2nd level length code */
330
1.22M
            here = lcode[here.val + BITS(op)];
331
1.22M
            Z_TOUCH(here);
332
1.22M
            goto dolen;
333
1.22M
        } else if (UNLIKELY(op & 32)) {                     /* end-of-block */
334
40.3k
            TRACE_END_OF_BLOCK();
335
40.3k
            state->mode = TYPE;
336
40.3k
            break;
337
40.3k
        } else {
338
1
            SET_BAD("invalid literal/length code");
339
1
            break;
340
1
        }
341
99.9M
    } while (in < last && out < end);
342
343
    /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
344
53.7k
    len = bits >> 3;
345
53.7k
    in -= len;
346
53.7k
    bits -= (bits_t)(len << 3);
347
53.7k
    hold &= (UINT64_C(1) << bits) - 1;
348
349
    /* update state and return */
350
53.7k
    strm->next_in = in;
351
53.7k
    strm->next_out = out;
352
53.7k
    strm->avail_in = (unsigned)(in < last ? (INFLATE_FAST_MIN_HAVE - 1) + (last - in)
353
53.7k
                                          : (INFLATE_FAST_MIN_HAVE - 1) - (in - last));
354
53.7k
    strm->avail_out = (unsigned)(safe - out);
355
356
53.7k
    Assert(bits <= 32, "Remaining bits greater than 32");
357
53.7k
    state->hold = (uint32_t)hold;
358
53.7k
    state->bits = bits;
359
53.7k
    return;
360
53.7k
}
inflate_fast_safe_avx2
Line
Count
Source
55
85.8k
void Z_INTERNAL INFLATE_FAST(PREFIX3(stream) *strm, uint32_t start) {
56
85.8k
#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
85.8k
    struct inflate_state *state;
67
85.8k
    z_const unsigned char *in;  /* local strm->next_in */
68
85.8k
    const unsigned char *last;  /* have enough input while in < last */
69
85.8k
    unsigned char *out;         /* local strm->next_out */
70
85.8k
    unsigned char *beg;         /* inflate()'s initial strm->next_out */
71
85.8k
    unsigned char *end;         /* while out < end, enough space available */
72
85.8k
    unsigned char *safe;        /* can use chunkcopy provided out < safe */
73
85.8k
    unsigned char *window;      /* allocated sliding window, if wsize != 0 */
74
85.8k
    unsigned wsize;             /* window size or zero if not using window */
75
85.8k
    unsigned whave;             /* valid bytes in the window */
76
85.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
85.8k
    bits_t bits;                /* local strm->bits */
116
85.8k
    uint64_t hold;              /* local strm->hold */
117
85.8k
    unsigned lmask;             /* mask for first level of length codes */
118
85.8k
    unsigned dmask;             /* mask for first level of distance codes */
119
85.8k
    code const *lcode;          /* local strm->lencode */
120
85.8k
    code const *dcode;          /* local strm->distcode */
121
85.8k
    code here;                  /* retrieved table entry */
122
85.8k
    unsigned op;                /* code bits, operation, extra bits, or */
123
                                /*  window position, window bytes to copy */
124
85.8k
    unsigned len;               /* match length, unused bytes */
125
85.8k
    unsigned char *from;        /* where to copy match from */
126
85.8k
    unsigned dist;              /* match distance */
127
85.8k
    uint64_t old;               /* look-behind buffer for extra bits */
128
129
    /* copy state to local variables */
130
85.8k
    state = (struct inflate_state *)strm->state;
131
85.8k
    in = strm->next_in;
132
85.8k
    last = in + (strm->avail_in - (INFLATE_FAST_MIN_HAVE - 1));
133
85.8k
    out = strm->next_out;
134
85.8k
    beg = out - (start - strm->avail_out);
135
85.8k
    safe = out + strm->avail_out;
136
85.8k
#ifdef USE_SAFE_MODE
137
85.8k
    end = safe - INFLATE_FAST_MIN_SAFE + 1;
138
#else
139
    end = safe - INFLATE_FAST_MIN_LEFT + 1;
140
#endif
141
85.8k
    wsize = state->wsize;
142
85.8k
    whave = state->whave;
143
85.8k
    wnext = state->wnext;
144
85.8k
    window = state->window;
145
85.8k
    hold = state->hold;
146
85.8k
    bits = (bits_t)state->bits;
147
85.8k
    lcode = state->lencode;
148
85.8k
    dcode = state->distcode;
149
85.8k
    lmask = (1U << state->lenbits) - 1;
150
85.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
85.8k
    REFILL();
157
158
    /* decode literals and length/distances until end-of-block or not enough
159
       input data or output space */
160
836k
    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
836k
        here = lcode[hold & lmask];
168
836k
        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
836k
        REFILL();
173
836k
        old = hold;
174
836k
        DROPBITS(here.bits);
175
836k
        if (LIKELY(here.op == 0)) {
176
716k
            TRACE_LITERAL(here.val);
177
716k
            *out++ = (unsigned char)(here.val);
178
716k
            here = lcode[hold & lmask];
179
716k
            Z_TOUCH(here);
180
716k
            old = hold;
181
716k
            DROPBITS(here.bits);
182
716k
            if (LIKELY(here.op == 0)) {
183
640k
                TRACE_LITERAL(here.val);
184
640k
                *out++ = (unsigned char)(here.val);
185
640k
                here = lcode[hold & lmask];
186
640k
                Z_TOUCH(here);
187
656k
            dolen:
188
656k
                old = hold;
189
656k
                DROPBITS(here.bits);
190
656k
                if (LIKELY(here.op == 0)) {
191
611k
                    TRACE_LITERAL(here.val);
192
611k
                    *out++ = (unsigned char)(here.val);
193
611k
                    continue;
194
611k
                }
195
656k
            }
196
716k
        }
197
241k
        op = here.op;
198
241k
        if (LIKELY(op & 16)) {                  /* length base */
199
220k
            len = here.val + EXTRA_BITS(old, here, op);
200
220k
            TRACE_LENGTH(len);
201
220k
            here = dcode[hold & dmask];
202
220k
            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
220k
            if (UNLIKELY(bits < MAX_BITS + MAX_DIST_EXTRA_BITS + MAX_LEN_ROOT_BITS)) {
207
22.5k
                REFILL();
208
22.5k
            }
209
221k
          dodist:
210
221k
            old = hold;
211
221k
            DROPBITS(here.bits);
212
221k
            op = here.op;
213
221k
            if (LIKELY(op & 16)) {              /* distance base */
214
220k
                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
220k
                TRACE_DISTANCE(dist);
222
223
220k
#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
220k
                if (UNLIKELY(len > (unsigned)(safe - out))) {
227
1.96k
                    state->mode = MATCH;
228
1.96k
                    state->length = len;
229
1.96k
                    state->was = len;
230
1.96k
                    state->offset = dist;
231
1.96k
                    break;
232
1.96k
                }
233
218k
#endif
234
235
218k
                op = (unsigned)(out - beg);     /* max distance in output */
236
218k
                if (UNLIKELY(dist > op)) {      /* see if copy from window */
237
11.7k
                    op = dist - op;             /* distance back in window */
238
11.7k
                    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
194
                        SET_BAD("invalid distance too far back");
258
194
                        break;
259
194
#endif
260
194
                    }
261
11.5k
                    from = window;
262
11.5k
                    if (LIKELY(wnext == 0)) {           /* very common case */
263
2.26k
                        from += wsize - op;
264
9.29k
                    } else if (LIKELY(wnext >= op)) {   /* contiguous in window */
265
9.29k
                        from += wnext - op;
266
9.29k
                    } 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
11.5k
                    if (UNLIKELY(op < len)) {           /* still need some from output */
280
4.57k
                        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
4.57k
                        out = chunkcopy_safe(out, from, op, safe);
290
4.57k
                        out = chunkcopy_safe(out, out - dist, len, safe);
291
4.57k
#endif
292
6.97k
                    } else {
293
#if !defined(USE_SAFE_MODE) || defined(HAVE_MASKED_READWRITE)
294
                        out = CHUNKCOPY_SAFE(out, from, len, safe);
295
#else
296
6.97k
                        out = chunkcopy_safe(out, from, len, safe);
297
6.97k
#endif
298
6.97k
                    }
299
206k
                } 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
206k
                    out = chunkcopy_safe(out, out - dist, len, safe);
319
206k
#endif
320
206k
                }
321
218k
            } else if (UNLIKELY((op & 64) == 0)) {          /* 2nd level distance code */
322
741
                here = dcode[here.val + BITS(op)];
323
741
                Z_TOUCH(here);
324
741
                goto dodist;
325
741
            } else {
326
13
                SET_BAD("invalid distance code");
327
13
                break;
328
13
            }
329
221k
        } else if (UNLIKELY((op & 64) == 0)) {              /* 2nd level length code */
330
16.8k
            here = lcode[here.val + BITS(op)];
331
16.8k
            Z_TOUCH(here);
332
16.8k
            goto dolen;
333
16.8k
        } else if (UNLIKELY(op & 32)) {                     /* end-of-block */
334
4.22k
            TRACE_END_OF_BLOCK();
335
4.22k
            state->mode = TYPE;
336
4.22k
            break;
337
4.22k
        } else {
338
11
            SET_BAD("invalid literal/length code");
339
11
            break;
340
11
        }
341
830k
    } while (in < last && out < end);
342
343
    /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
344
85.8k
    len = bits >> 3;
345
85.8k
    in -= len;
346
85.8k
    bits -= (bits_t)(len << 3);
347
85.8k
    hold &= (UINT64_C(1) << bits) - 1;
348
349
    /* update state and return */
350
85.8k
    strm->next_in = in;
351
85.8k
    strm->next_out = out;
352
85.8k
    strm->avail_in = (unsigned)(in < last ? (INFLATE_FAST_MIN_HAVE - 1) + (last - in)
353
85.8k
                                          : (INFLATE_FAST_MIN_HAVE - 1) - (in - last));
354
85.8k
    strm->avail_out = (unsigned)(safe - out);
355
356
85.8k
    Assert(bits <= 32, "Remaining bits greater than 32");
357
85.8k
    state->hold = (uint32_t)hold;
358
85.8k
    state->bits = bits;
359
85.8k
    return;
360
85.8k
}
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