Coverage Report

Created: 2026-04-12 06:41

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
31.0k
void Z_INTERNAL INFLATE_FAST(PREFIX3(stream) *strm, uint32_t start) {
53
    /* start: inflate()'s starting value for strm->avail_out */
54
31.0k
    struct inflate_state *state;
55
31.0k
    z_const unsigned char *in;  /* local strm->next_in */
56
31.0k
    const unsigned char *last;  /* have enough input while in < last */
57
31.0k
    unsigned char *out;         /* local strm->next_out */
58
31.0k
    unsigned char *beg;         /* inflate()'s initial strm->next_out */
59
31.0k
    unsigned char *end;         /* while out < end, enough space available */
60
31.0k
    unsigned char *safe;        /* can use chunkcopy provided out < safe */
61
31.0k
    unsigned char *window;      /* allocated sliding window, if wsize != 0 */
62
31.0k
    unsigned wsize;             /* window size or zero if not using window */
63
31.0k
    unsigned whave;             /* valid bytes in the window */
64
31.0k
    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
31.0k
    bits_t bits;                /* local strm->bits */
104
31.0k
    uint64_t hold;              /* local strm->hold */
105
31.0k
    unsigned lmask;             /* mask for first level of length codes */
106
31.0k
    unsigned dmask;             /* mask for first level of distance codes */
107
31.0k
    code const *lcode;          /* local strm->lencode */
108
31.0k
    code const *dcode;          /* local strm->distcode */
109
31.0k
    code here;                  /* retrieved table entry */
110
31.0k
    unsigned op;                /* code bits, operation, extra bits, or */
111
                                /*  window position, window bytes to copy */
112
31.0k
    unsigned len;               /* match length, unused bytes */
113
31.0k
    unsigned char *from;        /* where to copy match from */
114
31.0k
    unsigned dist;              /* match distance */
115
31.0k
    unsigned extra_safe;        /* copy chunks safely in all cases */
116
31.0k
    uint64_t old;               /* look-behind buffer for extra bits */
117
118
    /* copy state to local variables */
119
31.0k
    state = (struct inflate_state *)strm->state;
120
31.0k
    in = strm->next_in;
121
31.0k
    last = in + (strm->avail_in - (INFLATE_FAST_MIN_HAVE - 1));
122
31.0k
    out = strm->next_out;
123
31.0k
    beg = out - (start - strm->avail_out);
124
31.0k
    end = out + (strm->avail_out - (INFLATE_FAST_MIN_LEFT - 1));
125
31.0k
    safe = out + strm->avail_out;
126
31.0k
    wsize = state->wsize;
127
31.0k
    whave = state->whave;
128
31.0k
    wnext = state->wnext;
129
31.0k
    window = state->window;
130
31.0k
    hold = state->hold;
131
31.0k
    bits = (bits_t)state->bits;
132
31.0k
    lcode = state->lencode;
133
31.0k
    dcode = state->distcode;
134
31.0k
    lmask = (1U << state->lenbits) - 1;
135
31.0k
    dmask = (1U << state->distbits) - 1;
136
137
    /* Detect if out and window point to the same memory allocation. In this instance it is
138
       necessary to use safe chunk copy functions to prevent overwriting the window. If the
139
       window is overwritten then future matches with far distances will fail to copy correctly. */
140
31.0k
    extra_safe = (wsize != 0 && out >= window && out + INFLATE_FAST_MIN_LEFT <= window + state->wbufsize);
141
142
    /* decode literals and length/distances until end-of-block or not enough
143
       input data or output space */
144
3.46M
    do {
145
3.46M
        REFILL();
146
3.46M
        here = lcode[hold & lmask];
147
3.46M
        Z_TOUCH(here);
148
3.46M
        old = hold;
149
3.46M
        DROPBITS(here.bits);
150
3.46M
        if (LIKELY(here.op == 0)) {
151
3.17M
            TRACE_LITERAL(here.val);
152
3.17M
            *out++ = (unsigned char)(here.val);
153
3.17M
            here = lcode[hold & lmask];
154
3.17M
            Z_TOUCH(here);
155
3.17M
            old = hold;
156
3.17M
            DROPBITS(here.bits);
157
3.17M
            if (LIKELY(here.op == 0)) {
158
2.97M
                TRACE_LITERAL(here.val);
159
2.97M
                *out++ = (unsigned char)(here.val);
160
2.97M
                here = lcode[hold & lmask];
161
2.97M
                Z_TOUCH(here);
162
3.02M
            dolen:
163
3.02M
                old = hold;
164
3.02M
                DROPBITS(here.bits);
165
3.02M
                if (LIKELY(here.op == 0)) {
166
2.90M
                    TRACE_LITERAL(here.val);
167
2.90M
                    *out++ = (unsigned char)(here.val);
168
2.90M
                    continue;
169
2.90M
                }
170
3.02M
            }
171
3.17M
        }
172
613k
        op = here.op;
173
613k
        if (LIKELY(op & 16)) {                  /* length base */
174
538k
            len = here.val + EXTRA_BITS(old, here, op);
175
538k
            TRACE_LENGTH(len);
176
538k
            here = dcode[hold & dmask];
177
538k
            Z_TOUCH(here);
178
538k
            if (UNLIKELY(bits < MAX_BITS + MAX_DIST_EXTRA_BITS)) {
179
864
                REFILL();
180
864
            }
181
538k
          dodist:
182
538k
            old = hold;
183
538k
            DROPBITS(here.bits);
184
538k
            op = here.op;
185
538k
            if (LIKELY(op & 16)) {              /* distance base */
186
538k
                dist = here.val + EXTRA_BITS(old, here, op);
187
#ifdef INFLATE_STRICT
188
                if (UNLIKELY(dist > state->dmax)) {
189
                    SET_BAD("invalid distance too far back");
190
                    break;
191
                }
192
#endif
193
538k
                TRACE_DISTANCE(dist);
194
538k
                op = (unsigned)(out - beg);     /* max distance in output */
195
538k
                if (UNLIKELY(dist > op)) {      /* see if copy from window */
196
9.19k
                    op = dist - op;             /* distance back in window */
197
9.19k
                    if (UNLIKELY(op > whave)) {
198
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
199
                        if (LIKELY(state->sane)) {
200
                            SET_BAD("invalid distance too far back");
201
                            break;
202
                        }
203
                        unsigned gap = op - whave;
204
                        unsigned zeros = MIN(len, gap);
205
                        memset(out, 0, zeros);  /* fill missing bytes with zeros */
206
                        out += zeros;
207
                        len -= zeros;
208
                        if (UNLIKELY(len == 0))
209
                            continue;
210
                        op = whave;
211
                        if (UNLIKELY(op == 0)) {/* copy from already-decoded output */
212
                            out = chunkcopy_safe(out, out - dist, len, safe);
213
                            continue;
214
                        }
215
#else
216
0
                        SET_BAD("invalid distance too far back");
217
0
                        break;
218
0
#endif
219
0
                    }
220
9.19k
                    from = window;
221
9.19k
                    if (LIKELY(wnext == 0)) {           /* very common case */
222
0
                        from += wsize - op;
223
9.19k
                    } else if (LIKELY(wnext >= op)) {   /* contiguous in window */
224
9.19k
                        from += wnext - op;
225
9.19k
                    } else {                            /* wrap around window */
226
0
                        op -= wnext;
227
0
                        from += wsize - op;
228
0
                        if (UNLIKELY(op < len)) {       /* some from end of window */
229
0
                            len -= op;
230
0
                            out = CHUNKCOPY_SAFE(out, from, op, safe);
231
0
                            from = window;              /* more from start of window */
232
0
                            op = wnext;
233
                            /* This (rare) case can create a situation where
234
                               the first chunkcopy below must be checked.
235
                             */
236
0
                        }
237
0
                    }
238
9.19k
                    if (UNLIKELY(op < len)) {           /* still need some from output */
239
1.05k
                        len -= op;
240
1.05k
                        if (LIKELY(!extra_safe)) {
241
1.05k
                            out = CHUNKCOPY_SAFE(out, from, op, safe);
242
1.05k
                            out = CHUNKUNROLL(out, &dist, &len);
243
1.05k
                            out = CHUNKCOPY_SAFE(out, out - dist, len, safe);
244
1.05k
                        } else {
245
0
                            out = chunkcopy_safe(out, from, op, safe);
246
0
                            out = chunkcopy_safe(out, out - dist, len, safe);
247
0
                        }
248
8.14k
                    } else {
249
#ifndef HAVE_MASKED_READWRITE
250
8.14k
                        if (UNLIKELY(extra_safe))
251
0
                            out = chunkcopy_safe(out, from, len, safe);
252
8.14k
                        else
253
8.14k
#endif
254
8.14k
                            out = CHUNKCOPY_SAFE(out, from, len, safe);
255
8.14k
                    }
256
#ifndef HAVE_MASKED_READWRITE
257
529k
                } else if (UNLIKELY(extra_safe)) {
258
                    /* Whole reference is in range of current output. */
259
                        out = chunkcopy_safe(out, out - dist, len, safe);
260
#endif
261
529k
                } else {
262
                    /* Whole reference is in range of current output.  No range checks are
263
                       necessary because we start with room for at least 258 bytes of output,
264
                       so unroll and roundoff operations can write beyond `out+len` so long
265
                       as they stay within 258 bytes of `out`.
266
                    */
267
529k
                    if (LIKELY(dist >= len || dist >= CHUNKSIZE()))
268
352k
                        out = CHUNKCOPY(out, out - dist, len);
269
176k
                    else
270
176k
                        out = CHUNKMEMSET(out, out - dist, len);
271
529k
                }
272
538k
            } else if (UNLIKELY((op & 64) == 0)) {          /* 2nd level distance code */
273
443
                here = dcode[here.val + BITS(op)];
274
443
                Z_TOUCH(here);
275
443
                goto dodist;
276
443
            } else {
277
0
                SET_BAD("invalid distance code");
278
0
                break;
279
0
            }
280
538k
        } else if (UNLIKELY((op & 64) == 0)) {              /* 2nd level length code */
281
47.4k
            here = lcode[here.val + BITS(op)];
282
47.4k
            Z_TOUCH(here);
283
47.4k
            goto dolen;
284
47.4k
        } else if (UNLIKELY(op & 32)) {                     /* end-of-block */
285
28.1k
            TRACE_END_OF_BLOCK();
286
28.1k
            state->mode = TYPE;
287
28.1k
            break;
288
28.1k
        } else {
289
0
            SET_BAD("invalid literal/length code");
290
0
            break;
291
0
        }
292
3.43M
    } while (in < last && out < end);
293
294
    /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
295
31.0k
    len = bits >> 3;
296
31.0k
    in -= len;
297
31.0k
    bits -= (bits_t)(len << 3);
298
31.0k
    hold &= (UINT64_C(1) << bits) - 1;
299
300
    /* update state and return */
301
31.0k
    strm->next_in = in;
302
31.0k
    strm->next_out = out;
303
31.0k
    strm->avail_in = (unsigned)(in < last ? (INFLATE_FAST_MIN_HAVE - 1) + (last - in)
304
31.0k
                                          : (INFLATE_FAST_MIN_HAVE - 1) - (in - last));
305
31.0k
    strm->avail_out = (unsigned)(out < end ? (INFLATE_FAST_MIN_LEFT - 1) + (end - out)
306
31.0k
                                           : (INFLATE_FAST_MIN_LEFT - 1) - (out - end));
307
308
31.0k
    Assert(bits <= 32, "Remaining bits greater than 32");
309
31.0k
    state->hold = (uint32_t)hold;
310
31.0k
    state->bits = bits;
311
31.0k
    return;
312
31.0k
}
Unexecuted instantiation: inflate_fast_sse2
Unexecuted instantiation: inflate_fast_ssse3
inflate_fast_avx2
Line
Count
Source
52
31.0k
void Z_INTERNAL INFLATE_FAST(PREFIX3(stream) *strm, uint32_t start) {
53
    /* start: inflate()'s starting value for strm->avail_out */
54
31.0k
    struct inflate_state *state;
55
31.0k
    z_const unsigned char *in;  /* local strm->next_in */
56
31.0k
    const unsigned char *last;  /* have enough input while in < last */
57
31.0k
    unsigned char *out;         /* local strm->next_out */
58
31.0k
    unsigned char *beg;         /* inflate()'s initial strm->next_out */
59
31.0k
    unsigned char *end;         /* while out < end, enough space available */
60
31.0k
    unsigned char *safe;        /* can use chunkcopy provided out < safe */
61
31.0k
    unsigned char *window;      /* allocated sliding window, if wsize != 0 */
62
31.0k
    unsigned wsize;             /* window size or zero if not using window */
63
31.0k
    unsigned whave;             /* valid bytes in the window */
64
31.0k
    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
31.0k
    bits_t bits;                /* local strm->bits */
104
31.0k
    uint64_t hold;              /* local strm->hold */
105
31.0k
    unsigned lmask;             /* mask for first level of length codes */
106
31.0k
    unsigned dmask;             /* mask for first level of distance codes */
107
31.0k
    code const *lcode;          /* local strm->lencode */
108
31.0k
    code const *dcode;          /* local strm->distcode */
109
31.0k
    code here;                  /* retrieved table entry */
110
31.0k
    unsigned op;                /* code bits, operation, extra bits, or */
111
                                /*  window position, window bytes to copy */
112
31.0k
    unsigned len;               /* match length, unused bytes */
113
31.0k
    unsigned char *from;        /* where to copy match from */
114
31.0k
    unsigned dist;              /* match distance */
115
31.0k
    unsigned extra_safe;        /* copy chunks safely in all cases */
116
31.0k
    uint64_t old;               /* look-behind buffer for extra bits */
117
118
    /* copy state to local variables */
119
31.0k
    state = (struct inflate_state *)strm->state;
120
31.0k
    in = strm->next_in;
121
31.0k
    last = in + (strm->avail_in - (INFLATE_FAST_MIN_HAVE - 1));
122
31.0k
    out = strm->next_out;
123
31.0k
    beg = out - (start - strm->avail_out);
124
31.0k
    end = out + (strm->avail_out - (INFLATE_FAST_MIN_LEFT - 1));
125
31.0k
    safe = out + strm->avail_out;
126
31.0k
    wsize = state->wsize;
127
31.0k
    whave = state->whave;
128
31.0k
    wnext = state->wnext;
129
31.0k
    window = state->window;
130
31.0k
    hold = state->hold;
131
31.0k
    bits = (bits_t)state->bits;
132
31.0k
    lcode = state->lencode;
133
31.0k
    dcode = state->distcode;
134
31.0k
    lmask = (1U << state->lenbits) - 1;
135
31.0k
    dmask = (1U << state->distbits) - 1;
136
137
    /* Detect if out and window point to the same memory allocation. In this instance it is
138
       necessary to use safe chunk copy functions to prevent overwriting the window. If the
139
       window is overwritten then future matches with far distances will fail to copy correctly. */
140
31.0k
    extra_safe = (wsize != 0 && out >= window && out + INFLATE_FAST_MIN_LEFT <= window + state->wbufsize);
141
142
    /* decode literals and length/distances until end-of-block or not enough
143
       input data or output space */
144
3.46M
    do {
145
3.46M
        REFILL();
146
3.46M
        here = lcode[hold & lmask];
147
3.46M
        Z_TOUCH(here);
148
3.46M
        old = hold;
149
3.46M
        DROPBITS(here.bits);
150
3.46M
        if (LIKELY(here.op == 0)) {
151
3.17M
            TRACE_LITERAL(here.val);
152
3.17M
            *out++ = (unsigned char)(here.val);
153
3.17M
            here = lcode[hold & lmask];
154
3.17M
            Z_TOUCH(here);
155
3.17M
            old = hold;
156
3.17M
            DROPBITS(here.bits);
157
3.17M
            if (LIKELY(here.op == 0)) {
158
2.97M
                TRACE_LITERAL(here.val);
159
2.97M
                *out++ = (unsigned char)(here.val);
160
2.97M
                here = lcode[hold & lmask];
161
2.97M
                Z_TOUCH(here);
162
3.02M
            dolen:
163
3.02M
                old = hold;
164
3.02M
                DROPBITS(here.bits);
165
3.02M
                if (LIKELY(here.op == 0)) {
166
2.90M
                    TRACE_LITERAL(here.val);
167
2.90M
                    *out++ = (unsigned char)(here.val);
168
2.90M
                    continue;
169
2.90M
                }
170
3.02M
            }
171
3.17M
        }
172
613k
        op = here.op;
173
613k
        if (LIKELY(op & 16)) {                  /* length base */
174
538k
            len = here.val + EXTRA_BITS(old, here, op);
175
538k
            TRACE_LENGTH(len);
176
538k
            here = dcode[hold & dmask];
177
538k
            Z_TOUCH(here);
178
538k
            if (UNLIKELY(bits < MAX_BITS + MAX_DIST_EXTRA_BITS)) {
179
864
                REFILL();
180
864
            }
181
538k
          dodist:
182
538k
            old = hold;
183
538k
            DROPBITS(here.bits);
184
538k
            op = here.op;
185
538k
            if (LIKELY(op & 16)) {              /* distance base */
186
538k
                dist = here.val + EXTRA_BITS(old, here, op);
187
#ifdef INFLATE_STRICT
188
                if (UNLIKELY(dist > state->dmax)) {
189
                    SET_BAD("invalid distance too far back");
190
                    break;
191
                }
192
#endif
193
538k
                TRACE_DISTANCE(dist);
194
538k
                op = (unsigned)(out - beg);     /* max distance in output */
195
538k
                if (UNLIKELY(dist > op)) {      /* see if copy from window */
196
9.19k
                    op = dist - op;             /* distance back in window */
197
9.19k
                    if (UNLIKELY(op > whave)) {
198
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
199
                        if (LIKELY(state->sane)) {
200
                            SET_BAD("invalid distance too far back");
201
                            break;
202
                        }
203
                        unsigned gap = op - whave;
204
                        unsigned zeros = MIN(len, gap);
205
                        memset(out, 0, zeros);  /* fill missing bytes with zeros */
206
                        out += zeros;
207
                        len -= zeros;
208
                        if (UNLIKELY(len == 0))
209
                            continue;
210
                        op = whave;
211
                        if (UNLIKELY(op == 0)) {/* copy from already-decoded output */
212
                            out = chunkcopy_safe(out, out - dist, len, safe);
213
                            continue;
214
                        }
215
#else
216
0
                        SET_BAD("invalid distance too far back");
217
0
                        break;
218
0
#endif
219
0
                    }
220
9.19k
                    from = window;
221
9.19k
                    if (LIKELY(wnext == 0)) {           /* very common case */
222
0
                        from += wsize - op;
223
9.19k
                    } else if (LIKELY(wnext >= op)) {   /* contiguous in window */
224
9.19k
                        from += wnext - op;
225
9.19k
                    } else {                            /* wrap around window */
226
0
                        op -= wnext;
227
0
                        from += wsize - op;
228
0
                        if (UNLIKELY(op < len)) {       /* some from end of window */
229
0
                            len -= op;
230
0
                            out = CHUNKCOPY_SAFE(out, from, op, safe);
231
0
                            from = window;              /* more from start of window */
232
0
                            op = wnext;
233
                            /* This (rare) case can create a situation where
234
                               the first chunkcopy below must be checked.
235
                             */
236
0
                        }
237
0
                    }
238
9.19k
                    if (UNLIKELY(op < len)) {           /* still need some from output */
239
1.05k
                        len -= op;
240
1.05k
                        if (LIKELY(!extra_safe)) {
241
1.05k
                            out = CHUNKCOPY_SAFE(out, from, op, safe);
242
1.05k
                            out = CHUNKUNROLL(out, &dist, &len);
243
1.05k
                            out = CHUNKCOPY_SAFE(out, out - dist, len, safe);
244
1.05k
                        } else {
245
0
                            out = chunkcopy_safe(out, from, op, safe);
246
0
                            out = chunkcopy_safe(out, out - dist, len, safe);
247
0
                        }
248
8.14k
                    } else {
249
8.14k
#ifndef HAVE_MASKED_READWRITE
250
8.14k
                        if (UNLIKELY(extra_safe))
251
0
                            out = chunkcopy_safe(out, from, len, safe);
252
8.14k
                        else
253
8.14k
#endif
254
8.14k
                            out = CHUNKCOPY_SAFE(out, from, len, safe);
255
8.14k
                    }
256
9.19k
#ifndef HAVE_MASKED_READWRITE
257
529k
                } else if (UNLIKELY(extra_safe)) {
258
                    /* Whole reference is in range of current output. */
259
0
                        out = chunkcopy_safe(out, out - dist, len, safe);
260
0
#endif
261
529k
                } else {
262
                    /* Whole reference is in range of current output.  No range checks are
263
                       necessary because we start with room for at least 258 bytes of output,
264
                       so unroll and roundoff operations can write beyond `out+len` so long
265
                       as they stay within 258 bytes of `out`.
266
                    */
267
529k
                    if (LIKELY(dist >= len || dist >= CHUNKSIZE()))
268
352k
                        out = CHUNKCOPY(out, out - dist, len);
269
176k
                    else
270
176k
                        out = CHUNKMEMSET(out, out - dist, len);
271
529k
                }
272
538k
            } else if (UNLIKELY((op & 64) == 0)) {          /* 2nd level distance code */
273
443
                here = dcode[here.val + BITS(op)];
274
443
                Z_TOUCH(here);
275
443
                goto dodist;
276
443
            } else {
277
0
                SET_BAD("invalid distance code");
278
0
                break;
279
0
            }
280
538k
        } else if (UNLIKELY((op & 64) == 0)) {              /* 2nd level length code */
281
47.4k
            here = lcode[here.val + BITS(op)];
282
47.4k
            Z_TOUCH(here);
283
47.4k
            goto dolen;
284
47.4k
        } else if (UNLIKELY(op & 32)) {                     /* end-of-block */
285
28.1k
            TRACE_END_OF_BLOCK();
286
28.1k
            state->mode = TYPE;
287
28.1k
            break;
288
28.1k
        } else {
289
0
            SET_BAD("invalid literal/length code");
290
0
            break;
291
0
        }
292
3.43M
    } while (in < last && out < end);
293
294
    /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
295
31.0k
    len = bits >> 3;
296
31.0k
    in -= len;
297
31.0k
    bits -= (bits_t)(len << 3);
298
31.0k
    hold &= (UINT64_C(1) << bits) - 1;
299
300
    /* update state and return */
301
31.0k
    strm->next_in = in;
302
31.0k
    strm->next_out = out;
303
31.0k
    strm->avail_in = (unsigned)(in < last ? (INFLATE_FAST_MIN_HAVE - 1) + (last - in)
304
31.0k
                                          : (INFLATE_FAST_MIN_HAVE - 1) - (in - last));
305
31.0k
    strm->avail_out = (unsigned)(out < end ? (INFLATE_FAST_MIN_LEFT - 1) + (end - out)
306
31.0k
                                           : (INFLATE_FAST_MIN_LEFT - 1) - (out - end));
307
308
31.0k
    Assert(bits <= 32, "Remaining bits greater than 32");
309
31.0k
    state->hold = (uint32_t)hold;
310
31.0k
    state->bits = bits;
311
31.0k
    return;
312
31.0k
}
Unexecuted instantiation: inflate_fast_avx512
313
314
/*
315
   inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
316
   - Using bit fields for code structure
317
   - Different op definition to avoid & for extra bits (do & for table bits)
318
   - Three separate decoding do-loops for direct, window, and wnext == 0
319
   - Special case for distance > 1 copies to do overlapped load and store copy
320
   - Explicit branch predictions (based on measured branch probabilities)
321
   - Deferring match copy and interspersed it with decoding subsequent codes
322
   - Swapping literal/length else
323
   - Swapping window/direct else
324
   - Larger unrolled copy loops (three is about right)
325
   - Moving len -= 3 statement into middle of loop
326
 */
327
328
 // Cleanup
329
#undef CHUNKCOPY
330
#undef CHUNKMEMSET
331
#undef CHUNKMEMSET_SAFE
332
#undef CHUNKSIZE
333
#undef CHUNKUNROLL
334
#undef HAVE_CHUNKCOPY
335
#undef HAVE_CHUNKMEMSET_2
336
#undef HAVE_CHUNKMEMSET_4
337
#undef HAVE_CHUNKMEMSET_8
338
#undef HAVE_CHUNKMEMSET_16
339
#undef HAVE_CHUNK_MAG
340
#undef HAVE_HALFCHUNKCOPY
341
#undef HAVE_HALF_CHUNK
342
#undef HAVE_MASKED_READWRITE
343
#undef INFLATE_FAST