Coverage Report

Created: 2026-11-12 06:21

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
56.2k
void Z_INTERNAL INFLATE_FAST(PREFIX3(stream) *strm, uint32_t start) {
53
    /* start: inflate()'s starting value for strm->avail_out */
54
56.2k
    struct inflate_state *state;
55
56.2k
    z_const unsigned char *in;  /* local strm->next_in */
56
56.2k
    const unsigned char *last;  /* have enough input while in < last */
57
56.2k
    unsigned char *out;         /* local strm->next_out */
58
56.2k
    unsigned char *beg;         /* inflate()'s initial strm->next_out */
59
56.2k
    unsigned char *end;         /* while out < end, enough space available */
60
56.2k
    unsigned char *safe;        /* can use chunkcopy provided out < safe */
61
56.2k
    unsigned char *window;      /* allocated sliding window, if wsize != 0 */
62
56.2k
    unsigned wsize;             /* window size or zero if not using window */
63
56.2k
    unsigned whave;             /* valid bytes in the window */
64
56.2k
    unsigned wnext;             /* window write index */
65
66
    /* hold is a local copy of strm->hold. By default, hold satisfies the same
67
       invariants that strm->hold does, namely that (hold >> bits) == 0. This
68
       invariant is kept by loading bits into hold one byte at a time, like:
69
70
       hold |= next_byte_of_input << bits; in++; bits += 8;
71
72
       If we need to ensure that bits >= 15 then this code snippet is simply
73
       repeated. Over one iteration of the outermost do/while loop, this
74
       happens up to six times (48 bits of input), as described in the NOTES
75
       above.
76
77
       However, on some little endian architectures, it can be significantly
78
       faster to load 64 bits once instead of 8 bits six times:
79
80
       if (bits <= 16) {
81
         hold |= next_8_bytes_of_input << bits; in += 6; bits += 48;
82
       }
83
84
       Unlike the simpler one byte load, shifting the next_8_bytes_of_input
85
       by bits will overflow and lose those high bits, up to 2 bytes' worth.
86
       The conservative estimate is therefore that we have read only 6 bytes
87
       (48 bits). Again, as per the NOTES above, 48 bits is sufficient for the
88
       rest of the iteration, and we will not need to load another 8 bytes.
89
90
       Inside this function, we no longer satisfy (hold >> bits) == 0, but
91
       this is not problematic, even if that overflow does not land on an 8 bit
92
       byte boundary. Those excess bits will eventually shift down lower as the
93
       Huffman decoder consumes input, and when new input bits need to be loaded
94
       into the bits variable, the same input bits will be or'ed over those
95
       existing bits. A bitwise or is idempotent: (a | b | b) equals (a | b).
96
       Note that we therefore write that load operation as "hold |= etc" and not
97
       "hold += etc".
98
99
       Outside that loop, at the end of the function, hold is bitwise and'ed
100
       with (1<<bits)-1 to drop those excess bits so that, on function exit, we
101
       keep the invariant that (state->hold >> state->bits) == 0.
102
    */
103
56.2k
    bits_t bits;                /* local strm->bits */
104
56.2k
    uint64_t hold;              /* local strm->hold */
105
56.2k
    unsigned lmask;             /* mask for first level of length codes */
106
56.2k
    unsigned dmask;             /* mask for first level of distance codes */
107
56.2k
    code const *lcode;          /* local strm->lencode */
108
56.2k
    code const *dcode;          /* local strm->distcode */
109
56.2k
    code here;                  /* retrieved table entry */
110
56.2k
    unsigned op;                /* code bits, operation, extra bits, or */
111
                                /*  window position, window bytes to copy */
112
56.2k
    unsigned len;               /* match length, unused bytes */
113
56.2k
    unsigned char *from;        /* where to copy match from */
114
56.2k
    unsigned dist;              /* match distance */
115
56.2k
    uint64_t old;               /* look-behind buffer for extra bits */
116
117
    /* copy state to local variables */
118
56.2k
    state = (struct inflate_state *)strm->state;
119
56.2k
    in = strm->next_in;
120
56.2k
    last = in + (strm->avail_in - (INFLATE_FAST_MIN_HAVE - 1));
121
56.2k
    out = strm->next_out;
122
56.2k
    beg = out - (start - strm->avail_out);
123
56.2k
    safe = out + strm->avail_out;
124
#ifdef USE_SAFE_MODE
125
14.8k
    end = safe - INFLATE_FAST_MIN_SAFE + 1;
126
#else
127
41.3k
    end = safe - INFLATE_FAST_MIN_LEFT + 1;
128
#endif
129
56.2k
    wsize = state->wsize;
130
56.2k
    whave = state->whave;
131
56.2k
    wnext = state->wnext;
132
56.2k
    window = state->window;
133
56.2k
    hold = state->hold;
134
56.2k
    bits = (bits_t)state->bits;
135
56.2k
    lcode = state->lencode;
136
56.2k
    dcode = state->distcode;
137
56.2k
    lmask = (1U << state->lenbits) - 1;
138
56.2k
    dmask = (1U << state->distbits) - 1;
139
140
    /* Ensure enough bits for the first length table lookup. Every refill after
141
       this one is issued behind a table lookup that only needs bits already in
142
       hold, so the refill's load latency overlaps with the table load instead
143
       of feeding it. */
144
56.2k
    REFILL();
145
146
    /* decode literals and length/distances until end-of-block or not enough
147
       input data or output space */
148
4.67M
    do {
149
        /* This lookup needs bits >= MAX_LEN_ROOT_BITS, which every path here
150
           guarantees: each refill leaves bits >= 56, the literal-only paths
151
           loop after consuming at most 35 bits, and the distance path refills
152
           to at least MAX_BITS + MAX_DIST_EXTRA_BITS + MAX_LEN_ROOT_BITS
153
           before consuming at most MAX_BITS + MAX_DIST_EXTRA_BITS. */
154
4.67M
        here = lcode[hold & lmask];
155
4.67M
        Z_TOUCH(here);
156
        /* No-op on the first iteration: the pre-loop refill already filled hold
157
           and the lookup above consumes nothing, so bits is still >= 56. Tops
158
           hold back up on every later iteration. */
159
4.67M
        REFILL();
160
4.67M
        old = hold;
161
4.67M
        DROPBITS(here.bits);
162
4.67M
        if (LIKELY(here.op == 0)) {
163
4.23M
            TRACE_LITERAL(here.val);
164
4.23M
            *out++ = (unsigned char)(here.val);
165
4.23M
            here = lcode[hold & lmask];
166
4.23M
            Z_TOUCH(here);
167
4.23M
            old = hold;
168
4.23M
            DROPBITS(here.bits);
169
4.23M
            if (LIKELY(here.op == 0)) {
170
3.95M
                TRACE_LITERAL(here.val);
171
3.95M
                *out++ = (unsigned char)(here.val);
172
3.95M
                here = lcode[hold & lmask];
173
3.95M
                Z_TOUCH(here);
174
4.02M
            dolen:
175
4.02M
                old = hold;
176
4.02M
                DROPBITS(here.bits);
177
4.02M
                if (LIKELY(here.op == 0)) {
178
3.85M
                    TRACE_LITERAL(here.val);
179
3.85M
                    *out++ = (unsigned char)(here.val);
180
3.85M
                    continue;
181
3.85M
                }
182
4.02M
            }
183
4.23M
        }
184
891k
        op = here.op;
185
891k
        if (LIKELY(op & 16)) {                  /* length base */
186
786k
            len = here.val + EXTRA_BITS(old, here, op);
187
786k
            TRACE_LENGTH(len);
188
786k
            here = dcode[hold & dmask];
189
786k
            Z_TOUCH(here);
190
            /* Reserve bits for the distance code and its extra bits, plus the
191
               next iteration's length table lookup, which happens before the
192
               next refill. */
193
786k
            if (UNLIKELY(bits < MAX_BITS + MAX_DIST_EXTRA_BITS + MAX_LEN_ROOT_BITS)) {
194
62.9k
                REFILL();
195
62.9k
            }
196
787k
          dodist:
197
787k
            old = hold;
198
787k
            DROPBITS(here.bits);
199
787k
            op = here.op;
200
787k
            if (LIKELY(op & 16)) {              /* distance base */
201
786k
                dist = here.val + EXTRA_BITS(old, here, op);
202
#ifdef INFLATE_STRICT
203
                if (UNLIKELY(dist > state->dmax)) {
204
                    SET_BAD("invalid distance too far back");
205
                    break;
206
                }
207
#endif
208
786k
                TRACE_DISTANCE(dist);
209
210
#ifdef USE_SAFE_MODE
211
                /* If there isn't enough output space for the full copy, bail to the
212
                   slow path's MATCH state which handles partial copies. */
213
57.7k
                if (UNLIKELY(len > (unsigned)(safe - out))) {
214
0
                    state->mode = MATCH;
215
0
                    state->length = len;
216
0
                    state->was = len;
217
0
                    state->offset = dist;
218
0
                    break;
219
0
                }
220
57.7k
#endif
221
222
57.7k
                op = (unsigned)(out - beg);     /* max distance in output */
223
786k
                if (UNLIKELY(dist > op)) {      /* see if copy from window */
224
21.4k
                    op = dist - op;             /* distance back in window */
225
21.4k
                    if (UNLIKELY(op > whave)) {
226
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
227
                        if (LIKELY(state->sane)) {
228
                            SET_BAD("invalid distance too far back");
229
                            break;
230
                        }
231
                        unsigned gap = op - whave;
232
                        unsigned zeros = MIN(len, gap);
233
                        memset(out, 0, zeros);  /* fill missing bytes with zeros */
234
                        out += zeros;
235
                        len -= zeros;
236
                        if (UNLIKELY(len == 0))
237
                            continue;
238
                        op = whave;
239
                        if (UNLIKELY(op == 0)) {/* copy from already-decoded output */
240
                            out = chunkcopy_safe(out, out - dist, len, safe);
241
                            continue;
242
                        }
243
#else
244
0
                        SET_BAD("invalid distance too far back");
245
0
                        break;
246
0
#endif
247
0
                    }
248
21.4k
                    from = window;
249
21.4k
                    if (LIKELY(wnext == 0)) {           /* very common case */
250
0
                        from += wsize - op;
251
21.4k
                    } else if (LIKELY(wnext >= op)) {   /* contiguous in window */
252
21.4k
                        from += wnext - op;
253
21.4k
                    } else {                            /* wrap around window */
254
0
                        op -= wnext;
255
0
                        from += wsize - op;
256
0
                        if (UNLIKELY(op < len)) {       /* some from end of window */
257
0
                            len -= op;
258
0
                            out = CHUNKCOPY_SAFE(out, from, op, safe);
259
0
                            from = window;              /* more from start of window */
260
0
                            op = wnext;
261
                            /* This (rare) case can create a situation where
262
                               the first chunkcopy below must be checked.
263
                             */
264
0
                        }
265
0
                    }
266
21.4k
                    if (UNLIKELY(op < len)) {           /* still need some from output */
267
4.81k
                        len -= op;
268
#ifndef USE_SAFE_MODE
269
                        out = CHUNKCOPY_SAFE(out, from, op, safe);
270
1.16k
                        out = CHUNKUNROLL(out, &dist, &len);
271
                        out = CHUNKCOPY_SAFE(out, out - dist, len, safe);
272
#elif defined(HAVE_MASKED_READWRITE)
273
                        out = CHUNKCOPY_SAFE(out, from, op, safe);
274
                        out = CHUNKCOPY_SAFE(out, out - dist, len, safe);
275
#else
276
                        out = chunkcopy_safe(out, from, op, safe);
277
                        out = chunkcopy_safe(out, out - dist, len, safe);
278
#endif
279
16.6k
                    } else {
280
#if !defined(USE_SAFE_MODE) || defined(HAVE_MASKED_READWRITE)
281
                        out = CHUNKCOPY_SAFE(out, from, len, safe);
282
#else
283
                        out = chunkcopy_safe(out, from, len, safe);
284
#endif
285
16.6k
                    }
286
765k
                } else {
287
#ifndef USE_SAFE_MODE
288
                    /* Whole reference is in range of current output.  No range checks are
289
                       necessary because we start with room for at least 258 bytes of output,
290
                       so unroll and roundoff operations can write beyond `out+len` so long
291
                       as they stay within 258 bytes of `out`.
292
                    */
293
717k
                    if (LIKELY(dist >= len || dist > CHUNKSIZE()))
294
479k
                        out = CHUNKCOPY(out, out - dist, len);
295
238k
                    else
296
238k
                        out = CHUNKMEMSET(out, out - dist, len);
297
#elif defined(HAVE_MASKED_READWRITE)
298
                    out = CHUNKCOPY_SAFE(out, out - dist, len, safe);
299
#else
300
                    out = chunkcopy_safe(out, out - dist, len, safe);
301
#endif
302
765k
                }
303
57.7k
            } else if (UNLIKELY((op & 64) == 0)) {          /* 2nd level distance code */
304
561
                here = dcode[here.val + BITS(op)];
305
561
                Z_TOUCH(here);
306
561
                goto dodist;
307
561
            } else {
308
0
                SET_BAD("invalid distance code");
309
0
                break;
310
0
            }
311
787k
        } else if (UNLIKELY((op & 64) == 0)) {              /* 2nd level length code */
312
66.4k
            here = lcode[here.val + BITS(op)];
313
66.4k
            Z_TOUCH(here);
314
66.4k
            goto dolen;
315
66.4k
        } else if (UNLIKELY(op & 32)) {                     /* end-of-block */
316
38.3k
            TRACE_END_OF_BLOCK();
317
38.3k
            state->mode = TYPE;
318
38.3k
            break;
319
38.3k
        } else {
320
0
            SET_BAD("invalid literal/length code");
321
0
            break;
322
0
        }
323
4.63M
    } while (in < last && out < end);
324
325
    /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
326
56.2k
    len = bits >> 3;
327
56.2k
    in -= len;
328
56.2k
    bits -= (bits_t)(len << 3);
329
56.2k
    hold &= (UINT64_C(1) << bits) - 1;
330
331
    /* update state and return */
332
56.2k
    strm->next_in = in;
333
56.2k
    strm->next_out = out;
334
56.2k
    strm->avail_in = (unsigned)(in < last ? (INFLATE_FAST_MIN_HAVE - 1) + (last - in)
335
56.2k
                                          : (INFLATE_FAST_MIN_HAVE - 1) - (in - last));
336
56.2k
    strm->avail_out = (unsigned)(safe - out);
337
338
56.2k
    Assert(bits <= 32, "Remaining bits greater than 32");
339
56.2k
    state->hold = (uint32_t)hold;
340
56.2k
    state->bits = bits;
341
56.2k
    return;
342
56.2k
}
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
52
41.3k
void Z_INTERNAL INFLATE_FAST(PREFIX3(stream) *strm, uint32_t start) {
53
    /* start: inflate()'s starting value for strm->avail_out */
54
41.3k
    struct inflate_state *state;
55
41.3k
    z_const unsigned char *in;  /* local strm->next_in */
56
41.3k
    const unsigned char *last;  /* have enough input while in < last */
57
41.3k
    unsigned char *out;         /* local strm->next_out */
58
41.3k
    unsigned char *beg;         /* inflate()'s initial strm->next_out */
59
41.3k
    unsigned char *end;         /* while out < end, enough space available */
60
41.3k
    unsigned char *safe;        /* can use chunkcopy provided out < safe */
61
41.3k
    unsigned char *window;      /* allocated sliding window, if wsize != 0 */
62
41.3k
    unsigned wsize;             /* window size or zero if not using window */
63
41.3k
    unsigned whave;             /* valid bytes in the window */
64
41.3k
    unsigned wnext;             /* window write index */
65
66
    /* hold is a local copy of strm->hold. By default, hold satisfies the same
67
       invariants that strm->hold does, namely that (hold >> bits) == 0. This
68
       invariant is kept by loading bits into hold one byte at a time, like:
69
70
       hold |= next_byte_of_input << bits; in++; bits += 8;
71
72
       If we need to ensure that bits >= 15 then this code snippet is simply
73
       repeated. Over one iteration of the outermost do/while loop, this
74
       happens up to six times (48 bits of input), as described in the NOTES
75
       above.
76
77
       However, on some little endian architectures, it can be significantly
78
       faster to load 64 bits once instead of 8 bits six times:
79
80
       if (bits <= 16) {
81
         hold |= next_8_bytes_of_input << bits; in += 6; bits += 48;
82
       }
83
84
       Unlike the simpler one byte load, shifting the next_8_bytes_of_input
85
       by bits will overflow and lose those high bits, up to 2 bytes' worth.
86
       The conservative estimate is therefore that we have read only 6 bytes
87
       (48 bits). Again, as per the NOTES above, 48 bits is sufficient for the
88
       rest of the iteration, and we will not need to load another 8 bytes.
89
90
       Inside this function, we no longer satisfy (hold >> bits) == 0, but
91
       this is not problematic, even if that overflow does not land on an 8 bit
92
       byte boundary. Those excess bits will eventually shift down lower as the
93
       Huffman decoder consumes input, and when new input bits need to be loaded
94
       into the bits variable, the same input bits will be or'ed over those
95
       existing bits. A bitwise or is idempotent: (a | b | b) equals (a | b).
96
       Note that we therefore write that load operation as "hold |= etc" and not
97
       "hold += etc".
98
99
       Outside that loop, at the end of the function, hold is bitwise and'ed
100
       with (1<<bits)-1 to drop those excess bits so that, on function exit, we
101
       keep the invariant that (state->hold >> state->bits) == 0.
102
    */
103
41.3k
    bits_t bits;                /* local strm->bits */
104
41.3k
    uint64_t hold;              /* local strm->hold */
105
41.3k
    unsigned lmask;             /* mask for first level of length codes */
106
41.3k
    unsigned dmask;             /* mask for first level of distance codes */
107
41.3k
    code const *lcode;          /* local strm->lencode */
108
41.3k
    code const *dcode;          /* local strm->distcode */
109
41.3k
    code here;                  /* retrieved table entry */
110
41.3k
    unsigned op;                /* code bits, operation, extra bits, or */
111
                                /*  window position, window bytes to copy */
112
41.3k
    unsigned len;               /* match length, unused bytes */
113
41.3k
    unsigned char *from;        /* where to copy match from */
114
41.3k
    unsigned dist;              /* match distance */
115
41.3k
    uint64_t old;               /* look-behind buffer for extra bits */
116
117
    /* copy state to local variables */
118
41.3k
    state = (struct inflate_state *)strm->state;
119
41.3k
    in = strm->next_in;
120
41.3k
    last = in + (strm->avail_in - (INFLATE_FAST_MIN_HAVE - 1));
121
41.3k
    out = strm->next_out;
122
41.3k
    beg = out - (start - strm->avail_out);
123
41.3k
    safe = out + strm->avail_out;
124
#ifdef USE_SAFE_MODE
125
    end = safe - INFLATE_FAST_MIN_SAFE + 1;
126
#else
127
41.3k
    end = safe - INFLATE_FAST_MIN_LEFT + 1;
128
41.3k
#endif
129
41.3k
    wsize = state->wsize;
130
41.3k
    whave = state->whave;
131
41.3k
    wnext = state->wnext;
132
41.3k
    window = state->window;
133
41.3k
    hold = state->hold;
134
41.3k
    bits = (bits_t)state->bits;
135
41.3k
    lcode = state->lencode;
136
41.3k
    dcode = state->distcode;
137
41.3k
    lmask = (1U << state->lenbits) - 1;
138
41.3k
    dmask = (1U << state->distbits) - 1;
139
140
    /* Ensure enough bits for the first length table lookup. Every refill after
141
       this one is issued behind a table lookup that only needs bits already in
142
       hold, so the refill's load latency overlaps with the table load instead
143
       of feeding it. */
144
41.3k
    REFILL();
145
146
    /* decode literals and length/distances until end-of-block or not enough
147
       input data or output space */
148
4.48M
    do {
149
        /* This lookup needs bits >= MAX_LEN_ROOT_BITS, which every path here
150
           guarantees: each refill leaves bits >= 56, the literal-only paths
151
           loop after consuming at most 35 bits, and the distance path refills
152
           to at least MAX_BITS + MAX_DIST_EXTRA_BITS + MAX_LEN_ROOT_BITS
153
           before consuming at most MAX_BITS + MAX_DIST_EXTRA_BITS. */
154
4.48M
        here = lcode[hold & lmask];
155
4.48M
        Z_TOUCH(here);
156
        /* No-op on the first iteration: the pre-loop refill already filled hold
157
           and the lookup above consumes nothing, so bits is still >= 56. Tops
158
           hold back up on every later iteration. */
159
4.48M
        REFILL();
160
4.48M
        old = hold;
161
4.48M
        DROPBITS(here.bits);
162
4.48M
        if (LIKELY(here.op == 0)) {
163
4.07M
            TRACE_LITERAL(here.val);
164
4.07M
            *out++ = (unsigned char)(here.val);
165
4.07M
            here = lcode[hold & lmask];
166
4.07M
            Z_TOUCH(here);
167
4.07M
            old = hold;
168
4.07M
            DROPBITS(here.bits);
169
4.07M
            if (LIKELY(here.op == 0)) {
170
3.81M
                TRACE_LITERAL(here.val);
171
3.81M
                *out++ = (unsigned char)(here.val);
172
3.81M
                here = lcode[hold & lmask];
173
3.81M
                Z_TOUCH(here);
174
3.88M
            dolen:
175
3.88M
                old = hold;
176
3.88M
                DROPBITS(here.bits);
177
3.88M
                if (LIKELY(here.op == 0)) {
178
3.72M
                    TRACE_LITERAL(here.val);
179
3.72M
                    *out++ = (unsigned char)(here.val);
180
3.72M
                    continue;
181
3.72M
                }
182
3.88M
            }
183
4.07M
        }
184
828k
        op = here.op;
185
828k
        if (LIKELY(op & 16)) {                  /* length base */
186
729k
            len = here.val + EXTRA_BITS(old, here, op);
187
729k
            TRACE_LENGTH(len);
188
729k
            here = dcode[hold & dmask];
189
729k
            Z_TOUCH(here);
190
            /* Reserve bits for the distance code and its extra bits, plus the
191
               next iteration's length table lookup, which happens before the
192
               next refill. */
193
729k
            if (UNLIKELY(bits < MAX_BITS + MAX_DIST_EXTRA_BITS + MAX_LEN_ROOT_BITS)) {
194
57.3k
                REFILL();
195
57.3k
            }
196
729k
          dodist:
197
729k
            old = hold;
198
729k
            DROPBITS(here.bits);
199
729k
            op = here.op;
200
729k
            if (LIKELY(op & 16)) {              /* distance base */
201
729k
                dist = here.val + EXTRA_BITS(old, here, op);
202
#ifdef INFLATE_STRICT
203
                if (UNLIKELY(dist > state->dmax)) {
204
                    SET_BAD("invalid distance too far back");
205
                    break;
206
                }
207
#endif
208
729k
                TRACE_DISTANCE(dist);
209
210
#ifdef USE_SAFE_MODE
211
                /* If there isn't enough output space for the full copy, bail to the
212
                   slow path's MATCH state which handles partial copies. */
213
                if (UNLIKELY(len > (unsigned)(safe - out))) {
214
                    state->mode = MATCH;
215
                    state->length = len;
216
                    state->was = len;
217
                    state->offset = dist;
218
                    break;
219
                }
220
#endif
221
222
729k
                op = (unsigned)(out - beg);     /* max distance in output */
223
729k
                if (UNLIKELY(dist > op)) {      /* see if copy from window */
224
11.6k
                    op = dist - op;             /* distance back in window */
225
11.6k
                    if (UNLIKELY(op > whave)) {
226
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
227
                        if (LIKELY(state->sane)) {
228
                            SET_BAD("invalid distance too far back");
229
                            break;
230
                        }
231
                        unsigned gap = op - whave;
232
                        unsigned zeros = MIN(len, gap);
233
                        memset(out, 0, zeros);  /* fill missing bytes with zeros */
234
                        out += zeros;
235
                        len -= zeros;
236
                        if (UNLIKELY(len == 0))
237
                            continue;
238
                        op = whave;
239
                        if (UNLIKELY(op == 0)) {/* copy from already-decoded output */
240
                            out = chunkcopy_safe(out, out - dist, len, safe);
241
                            continue;
242
                        }
243
#else
244
0
                        SET_BAD("invalid distance too far back");
245
0
                        break;
246
0
#endif
247
0
                    }
248
11.6k
                    from = window;
249
11.6k
                    if (LIKELY(wnext == 0)) {           /* very common case */
250
0
                        from += wsize - op;
251
11.6k
                    } else if (LIKELY(wnext >= op)) {   /* contiguous in window */
252
11.6k
                        from += wnext - op;
253
11.6k
                    } else {                            /* wrap around window */
254
0
                        op -= wnext;
255
0
                        from += wsize - op;
256
0
                        if (UNLIKELY(op < len)) {       /* some from end of window */
257
0
                            len -= op;
258
0
                            out = CHUNKCOPY_SAFE(out, from, op, safe);
259
0
                            from = window;              /* more from start of window */
260
0
                            op = wnext;
261
                            /* This (rare) case can create a situation where
262
                               the first chunkcopy below must be checked.
263
                             */
264
0
                        }
265
0
                    }
266
11.6k
                    if (UNLIKELY(op < len)) {           /* still need some from output */
267
1.16k
                        len -= op;
268
1.16k
#ifndef USE_SAFE_MODE
269
1.16k
                        out = CHUNKCOPY_SAFE(out, from, op, safe);
270
1.16k
                        out = CHUNKUNROLL(out, &dist, &len);
271
1.16k
                        out = CHUNKCOPY_SAFE(out, out - dist, len, safe);
272
#elif defined(HAVE_MASKED_READWRITE)
273
                        out = CHUNKCOPY_SAFE(out, from, op, safe);
274
                        out = CHUNKCOPY_SAFE(out, out - dist, len, safe);
275
#else
276
                        out = chunkcopy_safe(out, from, op, safe);
277
                        out = chunkcopy_safe(out, out - dist, len, safe);
278
#endif
279
10.4k
                    } else {
280
10.4k
#if !defined(USE_SAFE_MODE) || defined(HAVE_MASKED_READWRITE)
281
10.4k
                        out = CHUNKCOPY_SAFE(out, from, len, safe);
282
#else
283
                        out = chunkcopy_safe(out, from, len, safe);
284
#endif
285
10.4k
                    }
286
717k
                } else {
287
717k
#ifndef USE_SAFE_MODE
288
                    /* Whole reference is in range of current output.  No range checks are
289
                       necessary because we start with room for at least 258 bytes of output,
290
                       so unroll and roundoff operations can write beyond `out+len` so long
291
                       as they stay within 258 bytes of `out`.
292
                    */
293
717k
                    if (LIKELY(dist >= len || dist > CHUNKSIZE()))
294
479k
                        out = CHUNKCOPY(out, out - dist, len);
295
238k
                    else
296
238k
                        out = CHUNKMEMSET(out, out - dist, len);
297
#elif defined(HAVE_MASKED_READWRITE)
298
                    out = CHUNKCOPY_SAFE(out, out - dist, len, safe);
299
#else
300
                    out = chunkcopy_safe(out, out - dist, len, safe);
301
#endif
302
717k
                }
303
729k
            } else if (UNLIKELY((op & 64) == 0)) {          /* 2nd level distance code */
304
438
                here = dcode[here.val + BITS(op)];
305
438
                Z_TOUCH(here);
306
438
                goto dodist;
307
438
            } else {
308
0
                SET_BAD("invalid distance code");
309
0
                break;
310
0
            }
311
729k
        } else if (UNLIKELY((op & 64) == 0)) {              /* 2nd level length code */
312
62.6k
            here = lcode[here.val + BITS(op)];
313
62.6k
            Z_TOUCH(here);
314
62.6k
            goto dolen;
315
62.6k
        } else if (UNLIKELY(op & 32)) {                     /* end-of-block */
316
36.6k
            TRACE_END_OF_BLOCK();
317
36.6k
            state->mode = TYPE;
318
36.6k
            break;
319
36.6k
        } else {
320
0
            SET_BAD("invalid literal/length code");
321
0
            break;
322
0
        }
323
4.44M
    } while (in < last && out < end);
324
325
    /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
326
41.3k
    len = bits >> 3;
327
41.3k
    in -= len;
328
41.3k
    bits -= (bits_t)(len << 3);
329
41.3k
    hold &= (UINT64_C(1) << bits) - 1;
330
331
    /* update state and return */
332
41.3k
    strm->next_in = in;
333
41.3k
    strm->next_out = out;
334
41.3k
    strm->avail_in = (unsigned)(in < last ? (INFLATE_FAST_MIN_HAVE - 1) + (last - in)
335
41.3k
                                          : (INFLATE_FAST_MIN_HAVE - 1) - (in - last));
336
41.3k
    strm->avail_out = (unsigned)(safe - out);
337
338
41.3k
    Assert(bits <= 32, "Remaining bits greater than 32");
339
41.3k
    state->hold = (uint32_t)hold;
340
41.3k
    state->bits = bits;
341
41.3k
    return;
342
41.3k
}
inflate_fast_safe_avx2
Line
Count
Source
52
14.8k
void Z_INTERNAL INFLATE_FAST(PREFIX3(stream) *strm, uint32_t start) {
53
    /* start: inflate()'s starting value for strm->avail_out */
54
14.8k
    struct inflate_state *state;
55
14.8k
    z_const unsigned char *in;  /* local strm->next_in */
56
14.8k
    const unsigned char *last;  /* have enough input while in < last */
57
14.8k
    unsigned char *out;         /* local strm->next_out */
58
14.8k
    unsigned char *beg;         /* inflate()'s initial strm->next_out */
59
14.8k
    unsigned char *end;         /* while out < end, enough space available */
60
14.8k
    unsigned char *safe;        /* can use chunkcopy provided out < safe */
61
14.8k
    unsigned char *window;      /* allocated sliding window, if wsize != 0 */
62
14.8k
    unsigned wsize;             /* window size or zero if not using window */
63
14.8k
    unsigned whave;             /* valid bytes in the window */
64
14.8k
    unsigned wnext;             /* window write index */
65
66
    /* hold is a local copy of strm->hold. By default, hold satisfies the same
67
       invariants that strm->hold does, namely that (hold >> bits) == 0. This
68
       invariant is kept by loading bits into hold one byte at a time, like:
69
70
       hold |= next_byte_of_input << bits; in++; bits += 8;
71
72
       If we need to ensure that bits >= 15 then this code snippet is simply
73
       repeated. Over one iteration of the outermost do/while loop, this
74
       happens up to six times (48 bits of input), as described in the NOTES
75
       above.
76
77
       However, on some little endian architectures, it can be significantly
78
       faster to load 64 bits once instead of 8 bits six times:
79
80
       if (bits <= 16) {
81
         hold |= next_8_bytes_of_input << bits; in += 6; bits += 48;
82
       }
83
84
       Unlike the simpler one byte load, shifting the next_8_bytes_of_input
85
       by bits will overflow and lose those high bits, up to 2 bytes' worth.
86
       The conservative estimate is therefore that we have read only 6 bytes
87
       (48 bits). Again, as per the NOTES above, 48 bits is sufficient for the
88
       rest of the iteration, and we will not need to load another 8 bytes.
89
90
       Inside this function, we no longer satisfy (hold >> bits) == 0, but
91
       this is not problematic, even if that overflow does not land on an 8 bit
92
       byte boundary. Those excess bits will eventually shift down lower as the
93
       Huffman decoder consumes input, and when new input bits need to be loaded
94
       into the bits variable, the same input bits will be or'ed over those
95
       existing bits. A bitwise or is idempotent: (a | b | b) equals (a | b).
96
       Note that we therefore write that load operation as "hold |= etc" and not
97
       "hold += etc".
98
99
       Outside that loop, at the end of the function, hold is bitwise and'ed
100
       with (1<<bits)-1 to drop those excess bits so that, on function exit, we
101
       keep the invariant that (state->hold >> state->bits) == 0.
102
    */
103
14.8k
    bits_t bits;                /* local strm->bits */
104
14.8k
    uint64_t hold;              /* local strm->hold */
105
14.8k
    unsigned lmask;             /* mask for first level of length codes */
106
14.8k
    unsigned dmask;             /* mask for first level of distance codes */
107
14.8k
    code const *lcode;          /* local strm->lencode */
108
14.8k
    code const *dcode;          /* local strm->distcode */
109
14.8k
    code here;                  /* retrieved table entry */
110
14.8k
    unsigned op;                /* code bits, operation, extra bits, or */
111
                                /*  window position, window bytes to copy */
112
14.8k
    unsigned len;               /* match length, unused bytes */
113
14.8k
    unsigned char *from;        /* where to copy match from */
114
14.8k
    unsigned dist;              /* match distance */
115
14.8k
    uint64_t old;               /* look-behind buffer for extra bits */
116
117
    /* copy state to local variables */
118
14.8k
    state = (struct inflate_state *)strm->state;
119
14.8k
    in = strm->next_in;
120
14.8k
    last = in + (strm->avail_in - (INFLATE_FAST_MIN_HAVE - 1));
121
14.8k
    out = strm->next_out;
122
14.8k
    beg = out - (start - strm->avail_out);
123
14.8k
    safe = out + strm->avail_out;
124
14.8k
#ifdef USE_SAFE_MODE
125
14.8k
    end = safe - INFLATE_FAST_MIN_SAFE + 1;
126
#else
127
    end = safe - INFLATE_FAST_MIN_LEFT + 1;
128
#endif
129
14.8k
    wsize = state->wsize;
130
14.8k
    whave = state->whave;
131
14.8k
    wnext = state->wnext;
132
14.8k
    window = state->window;
133
14.8k
    hold = state->hold;
134
14.8k
    bits = (bits_t)state->bits;
135
14.8k
    lcode = state->lencode;
136
14.8k
    dcode = state->distcode;
137
14.8k
    lmask = (1U << state->lenbits) - 1;
138
14.8k
    dmask = (1U << state->distbits) - 1;
139
140
    /* Ensure enough bits for the first length table lookup. Every refill after
141
       this one is issued behind a table lookup that only needs bits already in
142
       hold, so the refill's load latency overlaps with the table load instead
143
       of feeding it. */
144
14.8k
    REFILL();
145
146
    /* decode literals and length/distances until end-of-block or not enough
147
       input data or output space */
148
189k
    do {
149
        /* This lookup needs bits >= MAX_LEN_ROOT_BITS, which every path here
150
           guarantees: each refill leaves bits >= 56, the literal-only paths
151
           loop after consuming at most 35 bits, and the distance path refills
152
           to at least MAX_BITS + MAX_DIST_EXTRA_BITS + MAX_LEN_ROOT_BITS
153
           before consuming at most MAX_BITS + MAX_DIST_EXTRA_BITS. */
154
189k
        here = lcode[hold & lmask];
155
189k
        Z_TOUCH(here);
156
        /* No-op on the first iteration: the pre-loop refill already filled hold
157
           and the lookup above consumes nothing, so bits is still >= 56. Tops
158
           hold back up on every later iteration. */
159
189k
        REFILL();
160
189k
        old = hold;
161
189k
        DROPBITS(here.bits);
162
189k
        if (LIKELY(here.op == 0)) {
163
159k
            TRACE_LITERAL(here.val);
164
159k
            *out++ = (unsigned char)(here.val);
165
159k
            here = lcode[hold & lmask];
166
159k
            Z_TOUCH(here);
167
159k
            old = hold;
168
159k
            DROPBITS(here.bits);
169
159k
            if (LIKELY(here.op == 0)) {
170
136k
                TRACE_LITERAL(here.val);
171
136k
                *out++ = (unsigned char)(here.val);
172
136k
                here = lcode[hold & lmask];
173
136k
                Z_TOUCH(here);
174
140k
            dolen:
175
140k
                old = hold;
176
140k
                DROPBITS(here.bits);
177
140k
                if (LIKELY(here.op == 0)) {
178
129k
                    TRACE_LITERAL(here.val);
179
129k
                    *out++ = (unsigned char)(here.val);
180
129k
                    continue;
181
129k
                }
182
140k
            }
183
159k
        }
184
63.2k
        op = here.op;
185
63.2k
        if (LIKELY(op & 16)) {                  /* length base */
186
57.7k
            len = here.val + EXTRA_BITS(old, here, op);
187
57.7k
            TRACE_LENGTH(len);
188
57.7k
            here = dcode[hold & dmask];
189
57.7k
            Z_TOUCH(here);
190
            /* Reserve bits for the distance code and its extra bits, plus the
191
               next iteration's length table lookup, which happens before the
192
               next refill. */
193
57.7k
            if (UNLIKELY(bits < MAX_BITS + MAX_DIST_EXTRA_BITS + MAX_LEN_ROOT_BITS)) {
194
5.65k
                REFILL();
195
5.65k
            }
196
57.8k
          dodist:
197
57.8k
            old = hold;
198
57.8k
            DROPBITS(here.bits);
199
57.8k
            op = here.op;
200
57.8k
            if (LIKELY(op & 16)) {              /* distance base */
201
57.7k
                dist = here.val + EXTRA_BITS(old, here, op);
202
#ifdef INFLATE_STRICT
203
                if (UNLIKELY(dist > state->dmax)) {
204
                    SET_BAD("invalid distance too far back");
205
                    break;
206
                }
207
#endif
208
57.7k
                TRACE_DISTANCE(dist);
209
210
57.7k
#ifdef USE_SAFE_MODE
211
                /* If there isn't enough output space for the full copy, bail to the
212
                   slow path's MATCH state which handles partial copies. */
213
57.7k
                if (UNLIKELY(len > (unsigned)(safe - out))) {
214
0
                    state->mode = MATCH;
215
0
                    state->length = len;
216
0
                    state->was = len;
217
0
                    state->offset = dist;
218
0
                    break;
219
0
                }
220
57.7k
#endif
221
222
57.7k
                op = (unsigned)(out - beg);     /* max distance in output */
223
57.7k
                if (UNLIKELY(dist > op)) {      /* see if copy from window */
224
9.80k
                    op = dist - op;             /* distance back in window */
225
9.80k
                    if (UNLIKELY(op > whave)) {
226
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
227
                        if (LIKELY(state->sane)) {
228
                            SET_BAD("invalid distance too far back");
229
                            break;
230
                        }
231
                        unsigned gap = op - whave;
232
                        unsigned zeros = MIN(len, gap);
233
                        memset(out, 0, zeros);  /* fill missing bytes with zeros */
234
                        out += zeros;
235
                        len -= zeros;
236
                        if (UNLIKELY(len == 0))
237
                            continue;
238
                        op = whave;
239
                        if (UNLIKELY(op == 0)) {/* copy from already-decoded output */
240
                            out = chunkcopy_safe(out, out - dist, len, safe);
241
                            continue;
242
                        }
243
#else
244
0
                        SET_BAD("invalid distance too far back");
245
0
                        break;
246
0
#endif
247
0
                    }
248
9.80k
                    from = window;
249
9.80k
                    if (LIKELY(wnext == 0)) {           /* very common case */
250
0
                        from += wsize - op;
251
9.80k
                    } else if (LIKELY(wnext >= op)) {   /* contiguous in window */
252
9.80k
                        from += wnext - op;
253
9.80k
                    } else {                            /* wrap around window */
254
0
                        op -= wnext;
255
0
                        from += wsize - op;
256
0
                        if (UNLIKELY(op < len)) {       /* some from end of window */
257
0
                            len -= op;
258
0
                            out = CHUNKCOPY_SAFE(out, from, op, safe);
259
0
                            from = window;              /* more from start of window */
260
0
                            op = wnext;
261
                            /* This (rare) case can create a situation where
262
                               the first chunkcopy below must be checked.
263
                             */
264
0
                        }
265
0
                    }
266
9.80k
                    if (UNLIKELY(op < len)) {           /* still need some from output */
267
3.65k
                        len -= op;
268
#ifndef USE_SAFE_MODE
269
                        out = CHUNKCOPY_SAFE(out, from, op, safe);
270
                        out = CHUNKUNROLL(out, &dist, &len);
271
                        out = CHUNKCOPY_SAFE(out, out - dist, len, safe);
272
#elif defined(HAVE_MASKED_READWRITE)
273
                        out = CHUNKCOPY_SAFE(out, from, op, safe);
274
                        out = CHUNKCOPY_SAFE(out, out - dist, len, safe);
275
#else
276
3.65k
                        out = chunkcopy_safe(out, from, op, safe);
277
3.65k
                        out = chunkcopy_safe(out, out - dist, len, safe);
278
3.65k
#endif
279
6.14k
                    } else {
280
#if !defined(USE_SAFE_MODE) || defined(HAVE_MASKED_READWRITE)
281
                        out = CHUNKCOPY_SAFE(out, from, len, safe);
282
#else
283
6.14k
                        out = chunkcopy_safe(out, from, len, safe);
284
6.14k
#endif
285
6.14k
                    }
286
47.9k
                } else {
287
#ifndef USE_SAFE_MODE
288
                    /* Whole reference is in range of current output.  No range checks are
289
                       necessary because we start with room for at least 258 bytes of output,
290
                       so unroll and roundoff operations can write beyond `out+len` so long
291
                       as they stay within 258 bytes of `out`.
292
                    */
293
                    if (LIKELY(dist >= len || dist > CHUNKSIZE()))
294
                        out = CHUNKCOPY(out, out - dist, len);
295
                    else
296
                        out = CHUNKMEMSET(out, out - dist, len);
297
#elif defined(HAVE_MASKED_READWRITE)
298
                    out = CHUNKCOPY_SAFE(out, out - dist, len, safe);
299
#else
300
47.9k
                    out = chunkcopy_safe(out, out - dist, len, safe);
301
47.9k
#endif
302
47.9k
                }
303
57.7k
            } else if (UNLIKELY((op & 64) == 0)) {          /* 2nd level distance code */
304
123
                here = dcode[here.val + BITS(op)];
305
123
                Z_TOUCH(here);
306
123
                goto dodist;
307
123
            } else {
308
0
                SET_BAD("invalid distance code");
309
0
                break;
310
0
            }
311
57.8k
        } else if (UNLIKELY((op & 64) == 0)) {              /* 2nd level length code */
312
3.85k
            here = lcode[here.val + BITS(op)];
313
3.85k
            Z_TOUCH(here);
314
3.85k
            goto dolen;
315
3.85k
        } else if (UNLIKELY(op & 32)) {                     /* end-of-block */
316
1.70k
            TRACE_END_OF_BLOCK();
317
1.70k
            state->mode = TYPE;
318
1.70k
            break;
319
1.70k
        } else {
320
0
            SET_BAD("invalid literal/length code");
321
0
            break;
322
0
        }
323
187k
    } while (in < last && out < end);
324
325
    /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
326
14.8k
    len = bits >> 3;
327
14.8k
    in -= len;
328
14.8k
    bits -= (bits_t)(len << 3);
329
14.8k
    hold &= (UINT64_C(1) << bits) - 1;
330
331
    /* update state and return */
332
14.8k
    strm->next_in = in;
333
14.8k
    strm->next_out = out;
334
14.8k
    strm->avail_in = (unsigned)(in < last ? (INFLATE_FAST_MIN_HAVE - 1) + (last - in)
335
14.8k
                                          : (INFLATE_FAST_MIN_HAVE - 1) - (in - last));
336
14.8k
    strm->avail_out = (unsigned)(safe - out);
337
338
14.8k
    Assert(bits <= 32, "Remaining bits greater than 32");
339
14.8k
    state->hold = (uint32_t)hold;
340
14.8k
    state->bits = bits;
341
14.8k
    return;
342
14.8k
}
Unexecuted instantiation: inflate_fast_avx512
Unexecuted instantiation: inflate_fast_safe_avx512
343
344
/*
345
   inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
346
   - Using bit fields for code structure
347
   - Different op definition to avoid & for extra bits (do & for table bits)
348
   - Three separate decoding do-loops for direct, window, and wnext == 0
349
   - Special case for distance > 1 copies to do overlapped load and store copy
350
   - Explicit branch predictions (based on measured branch probabilities)
351
   - Deferring match copy and interspersed it with decoding subsequent codes
352
   - Swapping literal/length else
353
   - Swapping window/direct else
354
   - Larger unrolled copy loops (three is about right)
355
   - Moving len -= 3 statement into middle of loop
356
 */
357
358
 // Cleanup
359
#undef INFLATE_FAST
360
361
/* The chunkset macros are shared by both instantiations, so they can only be
362
   released once the safe one has been emitted. */
363
#ifdef USE_SAFE_MODE
364
#  undef CHUNKCOPY
365
#  undef CHUNKMEMSET
366
#  undef CHUNKMEMSET_SAFE
367
#  undef CHUNKSIZE
368
#  undef CHUNKUNROLL
369
#  undef HAVE_CHUNKCOPY
370
#  undef HAVE_CHUNKMEMSET_2
371
#  undef HAVE_CHUNKMEMSET_4
372
#  undef HAVE_CHUNKMEMSET_8
373
#  undef HAVE_CHUNKMEMSET_16
374
#  undef HAVE_CHUNK_MAG
375
#  undef HAVE_HALFCHUNKCOPY
376
#  undef HAVE_HALF_CHUNK
377
#  undef HAVE_MASKED_READWRITE
378
#  undef USE_SAFE_MODE
379
#endif