Coverage Report

Created: 2026-01-10 06:46

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 "zendian.h"
8
#include "zutil.h"
9
#include "inftrees.h"
10
#include "inflate.h"
11
#include "inflate_p.h"
12
#include "functable.h"
13
14
/*
15
   Decode literal, length, and distance codes and write out the resulting
16
   literal and match bytes until either not enough input or output is
17
   available, an end-of-block is encountered, or a data error is encountered.
18
   When large enough input and output buffers are supplied to inflate(), for
19
   example, a 16K input buffer and a 64K output buffer, more than 95% of the
20
   inflate execution time is spent in this routine.
21
22
   Entry assumptions:
23
24
        state->mode == LEN
25
        strm->avail_in >= INFLATE_FAST_MIN_HAVE
26
        strm->avail_out >= INFLATE_FAST_MIN_LEFT
27
        start >= strm->avail_out
28
        state->bits < 8
29
30
   On return, state->mode is one of:
31
32
        LEN -- ran out of enough output space or enough available input
33
        TYPE -- reached end of block code, inflate() to interpret next block
34
        BAD -- error in block data
35
36
   Notes:
37
38
    - The maximum input bits used by a length/distance pair is 15 bits for the
39
      length code, 5 bits for the length extra, 15 bits for the distance code,
40
      and 13 bits for the distance extra.  This totals 48 bits, or six bytes.
41
      Therefore if strm->avail_in >= 6, then there is enough input to avoid
42
      checking for available input while decoding.
43
44
    - On some architectures, it can be significantly faster (e.g. up to 1.2x
45
      faster on x86_64) to load from strm->next_in 64 bits, or 8 bytes, at a
46
      time, so INFLATE_FAST_MIN_HAVE == 8.
47
48
    - The maximum bytes that a single length/distance pair can output is 258
49
      bytes, which is the maximum length that can be coded.  inflate_fast()
50
      requires strm->avail_out >= 258 for each loop to avoid checking for
51
      output space.
52
 */
53
5.94M
void Z_INTERNAL INFLATE_FAST(PREFIX3(stream) *strm, uint32_t start) {
54
    /* start: inflate()'s starting value for strm->avail_out */
55
5.94M
    struct inflate_state *state;
56
5.94M
    z_const unsigned char *in;  /* local strm->next_in */
57
5.94M
    const unsigned char *last;  /* have enough input while in < last */
58
5.94M
    unsigned char *out;         /* local strm->next_out */
59
5.94M
    unsigned char *beg;         /* inflate()'s initial strm->next_out */
60
5.94M
    unsigned char *end;         /* while out < end, enough space available */
61
5.94M
    unsigned char *safe;        /* can use chunkcopy provided out < safe */
62
5.94M
    unsigned char *window;      /* allocated sliding window, if wsize != 0 */
63
5.94M
    unsigned wsize;             /* window size or zero if not using window */
64
5.94M
    unsigned whave;             /* valid bytes in the window */
65
5.94M
    unsigned wnext;             /* window write index */
66
67
    /* hold is a local copy of strm->hold. By default, hold satisfies the same
68
       invariants that strm->hold does, namely that (hold >> bits) == 0. This
69
       invariant is kept by loading bits into hold one byte at a time, like:
70
71
       hold |= next_byte_of_input << bits; in++; bits += 8;
72
73
       If we need to ensure that bits >= 15 then this code snippet is simply
74
       repeated. Over one iteration of the outermost do/while loop, this
75
       happens up to six times (48 bits of input), as described in the NOTES
76
       above.
77
78
       However, on some little endian architectures, it can be significantly
79
       faster to load 64 bits once instead of 8 bits six times:
80
81
       if (bits <= 16) {
82
         hold |= next_8_bytes_of_input << bits; in += 6; bits += 48;
83
       }
84
85
       Unlike the simpler one byte load, shifting the next_8_bytes_of_input
86
       by bits will overflow and lose those high bits, up to 2 bytes' worth.
87
       The conservative estimate is therefore that we have read only 6 bytes
88
       (48 bits). Again, as per the NOTES above, 48 bits is sufficient for the
89
       rest of the iteration, and we will not need to load another 8 bytes.
90
91
       Inside this function, we no longer satisfy (hold >> bits) == 0, but
92
       this is not problematic, even if that overflow does not land on an 8 bit
93
       byte boundary. Those excess bits will eventually shift down lower as the
94
       Huffman decoder consumes input, and when new input bits need to be loaded
95
       into the bits variable, the same input bits will be or'ed over those
96
       existing bits. A bitwise or is idempotent: (a | b | b) equals (a | b).
97
       Note that we therefore write that load operation as "hold |= etc" and not
98
       "hold += etc".
99
100
       Outside that loop, at the end of the function, hold is bitwise and'ed
101
       with (1<<bits)-1 to drop those excess bits so that, on function exit, we
102
       keep the invariant that (state->hold >> state->bits) == 0.
103
    */
104
5.94M
    bits_t bits;                /* local strm->bits */
105
5.94M
    uint64_t hold;              /* local strm->hold */
106
5.94M
    unsigned lmask;             /* mask for first level of length codes */
107
5.94M
    unsigned dmask;             /* mask for first level of distance codes */
108
5.94M
    code const *lcode;          /* local strm->lencode */
109
5.94M
    code const *dcode;          /* local strm->distcode */
110
5.94M
    code here;                  /* retrieved table entry */
111
5.94M
    unsigned op;                /* code bits, operation, extra bits, or */
112
                                /*  window position, window bytes to copy */
113
5.94M
    unsigned len;               /* match length, unused bytes */
114
5.94M
    unsigned char *from;        /* where to copy match from */
115
5.94M
    unsigned dist;              /* match distance */
116
5.94M
    unsigned extra_safe;        /* copy chunks safely in all cases */
117
118
    /* copy state to local variables */
119
5.94M
    state = (struct inflate_state *)strm->state;
120
5.94M
    in = strm->next_in;
121
5.94M
    last = in + (strm->avail_in - (INFLATE_FAST_MIN_HAVE - 1));
122
5.94M
    out = strm->next_out;
123
5.94M
    beg = out - (start - strm->avail_out);
124
5.94M
    end = out + (strm->avail_out - (INFLATE_FAST_MIN_LEFT - 1));
125
5.94M
    safe = out + strm->avail_out;
126
5.94M
    wsize = state->wsize;
127
5.94M
    whave = state->whave;
128
5.94M
    wnext = state->wnext;
129
5.94M
    window = state->window;
130
5.94M
    hold = state->hold;
131
5.94M
    bits = (bits_t)state->bits;
132
5.94M
    lcode = state->lencode;
133
5.94M
    dcode = state->distcode;
134
5.94M
    lmask = (1U << state->lenbits) - 1;
135
5.94M
    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
5.94M
    extra_safe = (wsize != 0 && out >= window && out + INFLATE_FAST_MIN_LEFT <= window + state->wbufsize);
141
142
121M
#define REFILL() do { \
143
121M
        hold |= load_64_bits(in, bits); \
144
121M
        in += (63 ^ bits) >> 3; \
145
121M
        bits |= 56; \
146
121M
    } while (0)
147
148
    /* decode literals and length/distances until end-of-block or not enough
149
       input data or output space */
150
121M
    do {
151
121M
        REFILL();
152
121M
        here = lcode[hold & lmask];
153
121M
        Z_TOUCH(here);
154
121M
        DROPBITS(here.bits);
155
121M
        if (here.op == 0) {
156
104M
            *out++ = (unsigned char)(here.val);
157
104M
            here = lcode[hold & lmask];
158
104M
            Z_TOUCH(here);
159
104M
            DROPBITS(here.bits);
160
104M
            if (here.op == 0) {
161
101M
                *out++ = (unsigned char)(here.val);
162
101M
                here = lcode[hold & lmask];
163
101M
                Z_TOUCH(here);
164
101M
                DROPBITS(here.bits);
165
101M
            }
166
104M
        }
167
123M
      dolen:
168
123M
        op = here.op;
169
123M
        if (op == 0) {                          /* literal */
170
100M
            Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
171
100M
                    "inflate:         literal '%c'\n" :
172
100M
                    "inflate:         literal 0x%02x\n", here.val));
173
100M
            *out++ = (unsigned char)(here.val);
174
100M
        } else if (op & 16) {                     /* length base */
175
15.5M
            len = here.val;
176
15.5M
            op &= MAX_BITS;                       /* number of extra bits */
177
15.5M
            len += BITS(op);
178
15.5M
            DROPBITS(op);
179
15.5M
            Tracevv((stderr, "inflate:         length %u\n", len));
180
15.5M
            here = dcode[hold & dmask];
181
15.5M
            Z_TOUCH(here);
182
15.5M
            if (bits < MAX_BITS + MAX_DIST_EXTRA_BITS) {
183
11.7k
                REFILL();
184
11.7k
            }
185
15.6M
          dodist:
186
15.6M
            DROPBITS(here.bits);
187
15.6M
            op = here.op;
188
15.6M
            if (op & 16) {                      /* distance base */
189
15.5M
                dist = here.val;
190
15.5M
                op &= MAX_BITS;                 /* number of extra bits */
191
15.5M
                dist += BITS(op);
192
#ifdef INFLATE_STRICT
193
                if (dist > state->dmax) {
194
                    SET_BAD("invalid distance too far back");
195
                    break;
196
                }
197
#endif
198
15.5M
                DROPBITS(op);
199
15.5M
                Tracevv((stderr, "inflate:         distance %u\n", dist));
200
15.5M
                op = (unsigned)(out - beg);     /* max distance in output */
201
15.5M
                if (dist > op) {                /* see if copy from window */
202
302
                    op = dist - op;             /* distance back in window */
203
302
                    if (op > whave) {
204
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
205
                        if (state->sane) {
206
                            SET_BAD("invalid distance too far back");
207
                            break;
208
                        }
209
                        if (len <= op - whave) {
210
                            do {
211
                                *out++ = 0;
212
                            } while (--len);
213
                            continue;
214
                        }
215
                        len -= op - whave;
216
                        do {
217
                            *out++ = 0;
218
                        } while (--op > whave);
219
                        if (op == 0) {
220
                            from = out - dist;
221
                            do {
222
                                *out++ = *from++;
223
                            } while (--len);
224
                            continue;
225
                        }
226
#else
227
302
                        SET_BAD("invalid distance too far back");
228
302
                        break;
229
302
#endif
230
302
                    }
231
0
                    from = window;
232
0
                    if (wnext == 0) {           /* very common case */
233
0
                        from += wsize - op;
234
0
                    } else if (wnext >= op) {   /* contiguous in window */
235
0
                        from += wnext - op;
236
0
                    } else {                    /* wrap around window */
237
0
                        op -= wnext;
238
0
                        from += wsize - op;
239
0
                        if (op < len) {         /* some from end of window */
240
0
                            len -= op;
241
0
                            out = CHUNKCOPY_SAFE(out, from, op, safe);
242
0
                            from = window;      /* more from start of window */
243
0
                            op = wnext;
244
                            /* This (rare) case can create a situation where
245
                               the first chunkcopy below must be checked.
246
                             */
247
0
                        }
248
0
                    }
249
0
                    if (op < len) {             /* still need some from output */
250
0
                        len -= op;
251
0
                        if (!extra_safe) {
252
0
                            out = CHUNKCOPY_SAFE(out, from, op, safe);
253
0
                            out = CHUNKUNROLL(out, &dist, &len);
254
0
                            out = CHUNKCOPY_SAFE(out, out - dist, len, safe);
255
0
                        } else {
256
0
                            out = chunkcopy_safe(out, from, op, safe);
257
0
                            out = chunkcopy_safe(out, out - dist, len, safe);
258
0
                        }
259
0
                    } else {
260
#ifndef HAVE_MASKED_READWRITE
261
0
                        if (extra_safe)
262
0
                            out = chunkcopy_safe(out, from, len, safe);
263
0
                        else
264
0
#endif
265
0
                            out = CHUNKCOPY_SAFE(out, from, len, safe);
266
0
                    }
267
#ifndef HAVE_MASKED_READWRITE
268
15.5M
                } else if (extra_safe) {
269
                    /* Whole reference is in range of current output. */
270
                        out = chunkcopy_safe(out, out - dist, len, safe);
271
#endif
272
15.5M
                } else {
273
                    /* Whole reference is in range of current output.  No range checks are
274
                       necessary because we start with room for at least 258 bytes of output,
275
                       so unroll and roundoff operations can write beyond `out+len` so long
276
                       as they stay within 258 bytes of `out`.
277
                    */
278
15.5M
                    if (dist >= len || dist >= CHUNKSIZE())
279
14.5M
                        out = CHUNKCOPY(out, out - dist, len);
280
1.02M
                    else
281
1.02M
                        out = CHUNKMEMSET(out, out - dist, len);
282
15.5M
                }
283
15.5M
            } else if ((op & 64) == 0) {          /* 2nd level distance code */
284
14.5k
                here = dcode[here.val + BITS(op)];
285
14.5k
                Z_TOUCH(here);
286
14.5k
                goto dodist;
287
14.5k
            } else {
288
39
                SET_BAD("invalid distance code");
289
39
                break;
290
39
            }
291
15.6M
        } else if ((op & 64) == 0) {              /* 2nd level length code */
292
1.33M
            here = lcode[here.val + BITS(op)];
293
1.33M
            Z_TOUCH(here);
294
1.33M
            DROPBITS(here.bits);
295
1.33M
            goto dolen;
296
5.93M
        } else if (op & 32) {                     /* end-of-block */
297
5.93M
            Tracevv((stderr, "inflate:         end of block\n"));
298
5.93M
            state->mode = TYPE;
299
5.93M
            break;
300
5.93M
        } else {
301
8
            SET_BAD("invalid literal/length code");
302
8
            break;
303
8
        }
304
123M
    } while (in < last && out < end);
305
306
    /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
307
5.94M
    len = bits >> 3;
308
5.94M
    in -= len;
309
5.94M
    bits -= (bits_t)(len << 3);
310
5.94M
    hold &= (UINT64_C(1) << bits) - 1;
311
312
    /* update state and return */
313
5.94M
    strm->next_in = in;
314
5.94M
    strm->next_out = out;
315
5.94M
    strm->avail_in = (unsigned)(in < last ? (INFLATE_FAST_MIN_HAVE - 1) + (last - in)
316
5.94M
                                          : (INFLATE_FAST_MIN_HAVE - 1) - (in - last));
317
5.94M
    strm->avail_out = (unsigned)(out < end ? (INFLATE_FAST_MIN_LEFT - 1) + (end - out)
318
5.94M
                                           : (INFLATE_FAST_MIN_LEFT - 1) - (out - end));
319
320
5.94M
    Assert(bits <= 32, "Remaining bits greater than 32");
321
5.94M
    state->hold = (uint32_t)hold;
322
5.94M
    state->bits = bits;
323
5.94M
    return;
324
5.94M
}
Unexecuted instantiation: inflate_fast_sse2
Unexecuted instantiation: inflate_fast_ssse3
inflate_fast_avx2
Line
Count
Source
53
5.94M
void Z_INTERNAL INFLATE_FAST(PREFIX3(stream) *strm, uint32_t start) {
54
    /* start: inflate()'s starting value for strm->avail_out */
55
5.94M
    struct inflate_state *state;
56
5.94M
    z_const unsigned char *in;  /* local strm->next_in */
57
5.94M
    const unsigned char *last;  /* have enough input while in < last */
58
5.94M
    unsigned char *out;         /* local strm->next_out */
59
5.94M
    unsigned char *beg;         /* inflate()'s initial strm->next_out */
60
5.94M
    unsigned char *end;         /* while out < end, enough space available */
61
5.94M
    unsigned char *safe;        /* can use chunkcopy provided out < safe */
62
5.94M
    unsigned char *window;      /* allocated sliding window, if wsize != 0 */
63
5.94M
    unsigned wsize;             /* window size or zero if not using window */
64
5.94M
    unsigned whave;             /* valid bytes in the window */
65
5.94M
    unsigned wnext;             /* window write index */
66
67
    /* hold is a local copy of strm->hold. By default, hold satisfies the same
68
       invariants that strm->hold does, namely that (hold >> bits) == 0. This
69
       invariant is kept by loading bits into hold one byte at a time, like:
70
71
       hold |= next_byte_of_input << bits; in++; bits += 8;
72
73
       If we need to ensure that bits >= 15 then this code snippet is simply
74
       repeated. Over one iteration of the outermost do/while loop, this
75
       happens up to six times (48 bits of input), as described in the NOTES
76
       above.
77
78
       However, on some little endian architectures, it can be significantly
79
       faster to load 64 bits once instead of 8 bits six times:
80
81
       if (bits <= 16) {
82
         hold |= next_8_bytes_of_input << bits; in += 6; bits += 48;
83
       }
84
85
       Unlike the simpler one byte load, shifting the next_8_bytes_of_input
86
       by bits will overflow and lose those high bits, up to 2 bytes' worth.
87
       The conservative estimate is therefore that we have read only 6 bytes
88
       (48 bits). Again, as per the NOTES above, 48 bits is sufficient for the
89
       rest of the iteration, and we will not need to load another 8 bytes.
90
91
       Inside this function, we no longer satisfy (hold >> bits) == 0, but
92
       this is not problematic, even if that overflow does not land on an 8 bit
93
       byte boundary. Those excess bits will eventually shift down lower as the
94
       Huffman decoder consumes input, and when new input bits need to be loaded
95
       into the bits variable, the same input bits will be or'ed over those
96
       existing bits. A bitwise or is idempotent: (a | b | b) equals (a | b).
97
       Note that we therefore write that load operation as "hold |= etc" and not
98
       "hold += etc".
99
100
       Outside that loop, at the end of the function, hold is bitwise and'ed
101
       with (1<<bits)-1 to drop those excess bits so that, on function exit, we
102
       keep the invariant that (state->hold >> state->bits) == 0.
103
    */
104
5.94M
    bits_t bits;                /* local strm->bits */
105
5.94M
    uint64_t hold;              /* local strm->hold */
106
5.94M
    unsigned lmask;             /* mask for first level of length codes */
107
5.94M
    unsigned dmask;             /* mask for first level of distance codes */
108
5.94M
    code const *lcode;          /* local strm->lencode */
109
5.94M
    code const *dcode;          /* local strm->distcode */
110
5.94M
    code here;                  /* retrieved table entry */
111
5.94M
    unsigned op;                /* code bits, operation, extra bits, or */
112
                                /*  window position, window bytes to copy */
113
5.94M
    unsigned len;               /* match length, unused bytes */
114
5.94M
    unsigned char *from;        /* where to copy match from */
115
5.94M
    unsigned dist;              /* match distance */
116
5.94M
    unsigned extra_safe;        /* copy chunks safely in all cases */
117
118
    /* copy state to local variables */
119
5.94M
    state = (struct inflate_state *)strm->state;
120
5.94M
    in = strm->next_in;
121
5.94M
    last = in + (strm->avail_in - (INFLATE_FAST_MIN_HAVE - 1));
122
5.94M
    out = strm->next_out;
123
5.94M
    beg = out - (start - strm->avail_out);
124
5.94M
    end = out + (strm->avail_out - (INFLATE_FAST_MIN_LEFT - 1));
125
5.94M
    safe = out + strm->avail_out;
126
5.94M
    wsize = state->wsize;
127
5.94M
    whave = state->whave;
128
5.94M
    wnext = state->wnext;
129
5.94M
    window = state->window;
130
5.94M
    hold = state->hold;
131
5.94M
    bits = (bits_t)state->bits;
132
5.94M
    lcode = state->lencode;
133
5.94M
    dcode = state->distcode;
134
5.94M
    lmask = (1U << state->lenbits) - 1;
135
5.94M
    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
5.94M
    extra_safe = (wsize != 0 && out >= window && out + INFLATE_FAST_MIN_LEFT <= window + state->wbufsize);
141
142
5.94M
#define REFILL() do { \
143
5.94M
        hold |= load_64_bits(in, bits); \
144
5.94M
        in += (63 ^ bits) >> 3; \
145
5.94M
        bits |= 56; \
146
5.94M
    } while (0)
147
148
    /* decode literals and length/distances until end-of-block or not enough
149
       input data or output space */
150
121M
    do {
151
121M
        REFILL();
152
121M
        here = lcode[hold & lmask];
153
121M
        Z_TOUCH(here);
154
121M
        DROPBITS(here.bits);
155
121M
        if (here.op == 0) {
156
104M
            *out++ = (unsigned char)(here.val);
157
104M
            here = lcode[hold & lmask];
158
104M
            Z_TOUCH(here);
159
104M
            DROPBITS(here.bits);
160
104M
            if (here.op == 0) {
161
101M
                *out++ = (unsigned char)(here.val);
162
101M
                here = lcode[hold & lmask];
163
101M
                Z_TOUCH(here);
164
101M
                DROPBITS(here.bits);
165
101M
            }
166
104M
        }
167
123M
      dolen:
168
123M
        op = here.op;
169
123M
        if (op == 0) {                          /* literal */
170
100M
            Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
171
100M
                    "inflate:         literal '%c'\n" :
172
100M
                    "inflate:         literal 0x%02x\n", here.val));
173
100M
            *out++ = (unsigned char)(here.val);
174
100M
        } else if (op & 16) {                     /* length base */
175
15.5M
            len = here.val;
176
15.5M
            op &= MAX_BITS;                       /* number of extra bits */
177
15.5M
            len += BITS(op);
178
15.5M
            DROPBITS(op);
179
15.5M
            Tracevv((stderr, "inflate:         length %u\n", len));
180
15.5M
            here = dcode[hold & dmask];
181
15.5M
            Z_TOUCH(here);
182
15.5M
            if (bits < MAX_BITS + MAX_DIST_EXTRA_BITS) {
183
11.7k
                REFILL();
184
11.7k
            }
185
15.6M
          dodist:
186
15.6M
            DROPBITS(here.bits);
187
15.6M
            op = here.op;
188
15.6M
            if (op & 16) {                      /* distance base */
189
15.5M
                dist = here.val;
190
15.5M
                op &= MAX_BITS;                 /* number of extra bits */
191
15.5M
                dist += BITS(op);
192
#ifdef INFLATE_STRICT
193
                if (dist > state->dmax) {
194
                    SET_BAD("invalid distance too far back");
195
                    break;
196
                }
197
#endif
198
15.5M
                DROPBITS(op);
199
15.5M
                Tracevv((stderr, "inflate:         distance %u\n", dist));
200
15.5M
                op = (unsigned)(out - beg);     /* max distance in output */
201
15.5M
                if (dist > op) {                /* see if copy from window */
202
302
                    op = dist - op;             /* distance back in window */
203
302
                    if (op > whave) {
204
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
205
                        if (state->sane) {
206
                            SET_BAD("invalid distance too far back");
207
                            break;
208
                        }
209
                        if (len <= op - whave) {
210
                            do {
211
                                *out++ = 0;
212
                            } while (--len);
213
                            continue;
214
                        }
215
                        len -= op - whave;
216
                        do {
217
                            *out++ = 0;
218
                        } while (--op > whave);
219
                        if (op == 0) {
220
                            from = out - dist;
221
                            do {
222
                                *out++ = *from++;
223
                            } while (--len);
224
                            continue;
225
                        }
226
#else
227
302
                        SET_BAD("invalid distance too far back");
228
302
                        break;
229
302
#endif
230
302
                    }
231
0
                    from = window;
232
0
                    if (wnext == 0) {           /* very common case */
233
0
                        from += wsize - op;
234
0
                    } else if (wnext >= op) {   /* contiguous in window */
235
0
                        from += wnext - op;
236
0
                    } else {                    /* wrap around window */
237
0
                        op -= wnext;
238
0
                        from += wsize - op;
239
0
                        if (op < len) {         /* some from end of window */
240
0
                            len -= op;
241
0
                            out = CHUNKCOPY_SAFE(out, from, op, safe);
242
0
                            from = window;      /* more from start of window */
243
0
                            op = wnext;
244
                            /* This (rare) case can create a situation where
245
                               the first chunkcopy below must be checked.
246
                             */
247
0
                        }
248
0
                    }
249
0
                    if (op < len) {             /* still need some from output */
250
0
                        len -= op;
251
0
                        if (!extra_safe) {
252
0
                            out = CHUNKCOPY_SAFE(out, from, op, safe);
253
0
                            out = CHUNKUNROLL(out, &dist, &len);
254
0
                            out = CHUNKCOPY_SAFE(out, out - dist, len, safe);
255
0
                        } else {
256
0
                            out = chunkcopy_safe(out, from, op, safe);
257
0
                            out = chunkcopy_safe(out, out - dist, len, safe);
258
0
                        }
259
0
                    } else {
260
0
#ifndef HAVE_MASKED_READWRITE
261
0
                        if (extra_safe)
262
0
                            out = chunkcopy_safe(out, from, len, safe);
263
0
                        else
264
0
#endif
265
0
                            out = CHUNKCOPY_SAFE(out, from, len, safe);
266
0
                    }
267
0
#ifndef HAVE_MASKED_READWRITE
268
15.5M
                } else if (extra_safe) {
269
                    /* Whole reference is in range of current output. */
270
0
                        out = chunkcopy_safe(out, out - dist, len, safe);
271
0
#endif
272
15.5M
                } else {
273
                    /* Whole reference is in range of current output.  No range checks are
274
                       necessary because we start with room for at least 258 bytes of output,
275
                       so unroll and roundoff operations can write beyond `out+len` so long
276
                       as they stay within 258 bytes of `out`.
277
                    */
278
15.5M
                    if (dist >= len || dist >= CHUNKSIZE())
279
14.5M
                        out = CHUNKCOPY(out, out - dist, len);
280
1.02M
                    else
281
1.02M
                        out = CHUNKMEMSET(out, out - dist, len);
282
15.5M
                }
283
15.5M
            } else if ((op & 64) == 0) {          /* 2nd level distance code */
284
14.5k
                here = dcode[here.val + BITS(op)];
285
14.5k
                Z_TOUCH(here);
286
14.5k
                goto dodist;
287
14.5k
            } else {
288
39
                SET_BAD("invalid distance code");
289
39
                break;
290
39
            }
291
15.6M
        } else if ((op & 64) == 0) {              /* 2nd level length code */
292
1.33M
            here = lcode[here.val + BITS(op)];
293
1.33M
            Z_TOUCH(here);
294
1.33M
            DROPBITS(here.bits);
295
1.33M
            goto dolen;
296
5.93M
        } else if (op & 32) {                     /* end-of-block */
297
5.93M
            Tracevv((stderr, "inflate:         end of block\n"));
298
5.93M
            state->mode = TYPE;
299
5.93M
            break;
300
5.93M
        } else {
301
8
            SET_BAD("invalid literal/length code");
302
8
            break;
303
8
        }
304
123M
    } while (in < last && out < end);
305
306
    /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
307
5.94M
    len = bits >> 3;
308
5.94M
    in -= len;
309
5.94M
    bits -= (bits_t)(len << 3);
310
5.94M
    hold &= (UINT64_C(1) << bits) - 1;
311
312
    /* update state and return */
313
5.94M
    strm->next_in = in;
314
5.94M
    strm->next_out = out;
315
5.94M
    strm->avail_in = (unsigned)(in < last ? (INFLATE_FAST_MIN_HAVE - 1) + (last - in)
316
5.94M
                                          : (INFLATE_FAST_MIN_HAVE - 1) - (in - last));
317
5.94M
    strm->avail_out = (unsigned)(out < end ? (INFLATE_FAST_MIN_LEFT - 1) + (end - out)
318
5.94M
                                           : (INFLATE_FAST_MIN_LEFT - 1) - (out - end));
319
320
5.94M
    Assert(bits <= 32, "Remaining bits greater than 32");
321
5.94M
    state->hold = (uint32_t)hold;
322
5.94M
    state->bits = bits;
323
5.94M
    return;
324
5.94M
}
Unexecuted instantiation: inflate_fast_avx512
325
326
/*
327
   inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
328
   - Using bit fields for code structure
329
   - Different op definition to avoid & for extra bits (do & for table bits)
330
   - Three separate decoding do-loops for direct, window, and wnext == 0
331
   - Special case for distance > 1 copies to do overlapped load and store copy
332
   - Explicit branch predictions (based on measured branch probabilities)
333
   - Deferring match copy and interspersed it with decoding subsequent codes
334
   - Swapping literal/length else
335
   - Swapping window/direct else
336
   - Larger unrolled copy loops (three is about right)
337
   - Moving len -= 3 statement into middle of loop
338
 */