Coverage Report

Created: 2026-08-25 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/zlib/inffast.c
Line
Count
Source
1
/* inffast.c -- fast decoding
2
 * Copyright (C) 1995-2026 Mark Adler
3
 * For conditions of distribution and use, see copyright notice in zlib.h
4
 */
5
6
#include "zutil.h"
7
#include "inftrees.h"
8
#include "inflate.h"
9
#include "inffast.h"
10
11
#ifdef ASMINF
12
#  pragma message("Assembler code may have bugs -- use at your own risk")
13
#else
14
15
/*
16
   Decode literal, length, and distance codes and write out the resulting
17
   literal and match bytes until either not enough input or output is
18
   available, an end-of-block is encountered, or a data error is encountered.
19
   When large enough input and output buffers are supplied to inflate(), for
20
   example, a 16K input buffer and a 64K output buffer, more than 95% of the
21
   inflate execution time is spent in this routine.
22
23
   Entry assumptions:
24
25
        state->mode == LEN
26
        strm->avail_in >= 6
27
        strm->avail_out >= 258
28
        start >= strm->avail_out
29
        state->bits < 8
30
31
   On return, state->mode is one of:
32
33
        LEN -- ran out of enough output space or enough available input
34
        TYPE -- reached end of block code, inflate() to interpret next block
35
        BAD -- error in block data
36
37
   Notes:
38
39
    - The maximum input bits used by a length/distance pair is 15 bits for the
40
      length code, 5 bits for the length extra, 15 bits for the distance code,
41
      and 13 bits for the distance extra.  This totals 48 bits, or six bytes.
42
      Therefore if strm->avail_in >= 6, then there is enough input to avoid
43
      checking for available input while decoding.
44
45
    - The maximum bytes that a single length/distance pair can output is 258
46
      bytes, which is the maximum length that can be coded.  inflate_fast()
47
      requires strm->avail_out >= 258 for each loop to avoid checking for
48
      output space.
49
 */
50
651k
void ZLIB_INTERNAL inflate_fast(z_streamp strm, unsigned start) {
51
651k
    struct inflate_state FAR *state;
52
651k
    z_const unsigned char FAR *in;      /* local strm->next_in */
53
651k
    z_const unsigned char FAR *last;    /* have enough input while in < last */
54
651k
    unsigned char FAR *out;     /* local strm->next_out */
55
651k
    unsigned char FAR *beg;     /* inflate()'s initial strm->next_out */
56
651k
    unsigned char FAR *end;     /* while out < end, enough space available */
57
#ifdef INFLATE_STRICT
58
    unsigned dmax;              /* maximum distance from zlib header */
59
#endif
60
651k
    unsigned wsize;             /* window size or zero if not using window */
61
651k
    unsigned whave;             /* valid bytes in the window */
62
651k
    unsigned wnext;             /* window write index */
63
651k
    unsigned char FAR *window;  /* allocated sliding window, if wsize != 0 */
64
651k
    unsigned long hold;         /* local strm->hold */
65
651k
    unsigned bits;              /* local strm->bits */
66
651k
    code const FAR *lcode;      /* local strm->lencode */
67
651k
    code const FAR *dcode;      /* local strm->distcode */
68
651k
    unsigned lmask;             /* mask for first level of length codes */
69
651k
    unsigned dmask;             /* mask for first level of distance codes */
70
651k
    code const *here;           /* retrieved table entry */
71
651k
    unsigned op;                /* code bits, operation, extra bits, or */
72
                                /*  window position, window bytes to copy */
73
651k
    unsigned len;               /* match length, unused bytes */
74
651k
    unsigned dist;              /* match distance */
75
651k
    unsigned char FAR *from;    /* where to copy match from */
76
77
    /* copy state to local variables */
78
651k
    state = (struct inflate_state FAR *)strm->state;
79
651k
    in = strm->next_in;
80
651k
    last = in + (strm->avail_in - 5);
81
651k
    out = strm->next_out;
82
651k
    beg = out - (start - strm->avail_out);
83
651k
    end = out + (strm->avail_out - 257);
84
#ifdef INFLATE_STRICT
85
    dmax = state->dmax;
86
#endif
87
651k
    wsize = state->wsize;
88
651k
    whave = state->whave;
89
651k
    wnext = state->wnext;
90
651k
    window = state->window;
91
651k
    hold = state->hold;
92
651k
    bits = state->bits;
93
651k
    lcode = state->lencode;
94
651k
    dcode = state->distcode;
95
651k
    lmask = (1U << state->lenbits) - 1;
96
651k
    dmask = (1U << state->distbits) - 1;
97
98
    /* decode literals and length/distances until end-of-block or not enough
99
       input data or output space */
100
896M
    do {
101
896M
        if (bits < 15) {
102
315M
            hold += (unsigned long)(*in++) << bits;
103
315M
            bits += 8;
104
315M
            hold += (unsigned long)(*in++) << bits;
105
315M
            bits += 8;
106
315M
        }
107
896M
        here = lcode + (hold & lmask);
108
911M
      dolen:
109
911M
        op = (unsigned)(here->bits);
110
911M
        hold >>= op;
111
911M
        bits -= op;
112
911M
        op = (unsigned)(here->op);
113
911M
        if (op == 0) {                          /* literal */
114
647M
            Tracevv((stderr, here->val >= 0x20 && here->val < 0x7f ?
115
647M
                    "inflate:         literal '%c'\n" :
116
647M
                    "inflate:         literal 0x%02x\n", here->val));
117
647M
            *out++ = (unsigned char)(here->val);
118
647M
        }
119
264M
        else if (op & 16) {                     /* length base */
120
248M
            len = (unsigned)(here->val);
121
248M
            op &= 15;                           /* number of extra bits */
122
248M
            if (op) {
123
50.2M
                if (bits < op) {
124
52.2k
                    hold += (unsigned long)(*in++) << bits;
125
52.2k
                    bits += 8;
126
52.2k
                }
127
50.2M
                len += (unsigned)hold & ((1U << op) - 1);
128
50.2M
                hold >>= op;
129
50.2M
                bits -= op;
130
50.2M
            }
131
248M
            Tracevv((stderr, "inflate:         length %u\n", len));
132
248M
            if (bits < 15) {
133
100M
                hold += (unsigned long)(*in++) << bits;
134
100M
                bits += 8;
135
100M
                hold += (unsigned long)(*in++) << bits;
136
100M
                bits += 8;
137
100M
            }
138
248M
            here = dcode + (hold & dmask);
139
265M
          dodist:
140
265M
            op = (unsigned)(here->bits);
141
265M
            hold >>= op;
142
265M
            bits -= op;
143
265M
            op = (unsigned)(here->op);
144
265M
            if (op & 16) {                      /* distance base */
145
248M
                dist = (unsigned)(here->val);
146
248M
                op &= 15;                       /* number of extra bits */
147
248M
                if (bits < op) {
148
19.4k
                    hold += (unsigned long)(*in++) << bits;
149
19.4k
                    bits += 8;
150
19.4k
                    if (bits < op) {
151
0
                        hold += (unsigned long)(*in++) << bits;
152
0
                        bits += 8;
153
0
                    }
154
19.4k
                }
155
248M
                dist += (unsigned)hold & ((1U << op) - 1);
156
#ifdef INFLATE_STRICT
157
                if (dist > dmax) {
158
                    strm->msg = (z_const char *)
159
                        "invalid distance too far back";
160
                    state->mode = BAD;
161
                    break;
162
                }
163
#endif
164
248M
                hold >>= op;
165
248M
                bits -= op;
166
248M
                Tracevv((stderr, "inflate:         distance %u\n", dist));
167
248M
                op = (unsigned)(out - beg);     /* max distance in output */
168
248M
                if (dist > op) {                /* see if copy from window */
169
3.45M
                    op = dist - op;             /* distance back in window */
170
3.45M
                    if (op > whave) {
171
3.09k
                        if (state->sane) {
172
3.09k
                            strm->msg = (z_const char *)
173
3.09k
                                "invalid distance too far back";
174
3.09k
                            state->mode = BAD;
175
3.09k
                            break;
176
3.09k
                        }
177
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
178
                        if (len <= op - whave) {
179
                            do {
180
                                *out++ = 0;
181
                            } while (--len);
182
                            continue;
183
                        }
184
                        len -= op - whave;
185
                        do {
186
                            *out++ = 0;
187
                        } while (--op > whave);
188
                        if (op == 0) {
189
                            from = out - dist;
190
                            do {
191
                                *out++ = *from++;
192
                            } while (--len);
193
                            continue;
194
                        }
195
#endif
196
3.09k
                    }
197
3.45M
                    from = window;
198
3.45M
                    if (wnext == 0) {           /* very common case */
199
943k
                        from += wsize - op;
200
943k
                        if (op < len) {         /* some from window */
201
33.7k
                            len -= op;
202
679k
                            do {
203
679k
                                *out++ = *from++;
204
679k
                            } while (--op);
205
33.7k
                            from = out - dist;  /* rest from output */
206
33.7k
                        }
207
943k
                    }
208
2.50M
                    else if (wnext < op) {      /* wrap around window */
209
79.0k
                        from += wsize + wnext - op;
210
79.0k
                        op -= wnext;
211
79.0k
                        if (op < len) {         /* some from end of window */
212
9.09k
                            len -= op;
213
1.08M
                            do {
214
1.08M
                                *out++ = *from++;
215
1.08M
                            } while (--op);
216
9.09k
                            from = window;
217
9.09k
                            if (wnext < len) {  /* some from start of window */
218
1.09k
                                op = wnext;
219
1.09k
                                len -= op;
220
12.8k
                                do {
221
12.8k
                                    *out++ = *from++;
222
12.8k
                                } while (--op);
223
1.09k
                                from = out - dist;      /* rest from output */
224
1.09k
                            }
225
9.09k
                        }
226
79.0k
                    }
227
2.42M
                    else {                      /* contiguous in window */
228
2.42M
                        from += wnext - op;
229
2.42M
                        if (op < len) {         /* some from window */
230
91.3k
                            len -= op;
231
4.04M
                            do {
232
4.04M
                                *out++ = *from++;
233
4.04M
                            } while (--op);
234
91.3k
                            from = out - dist;  /* rest from output */
235
91.3k
                        }
236
2.42M
                    }
237
49.0M
                    while (len > 2) {
238
45.6M
                        *out++ = *from++;
239
45.6M
                        *out++ = *from++;
240
45.6M
                        *out++ = *from++;
241
45.6M
                        len -= 3;
242
45.6M
                    }
243
3.45M
                    if (len) {
244
1.42M
                        *out++ = *from++;
245
1.42M
                        if (len > 1)
246
1.10M
                            *out++ = *from++;
247
1.42M
                    }
248
3.45M
                }
249
245M
                else {
250
245M
                    from = out - dist;          /* copy direct from output */
251
1.31G
                    do {                        /* minimum length is three */
252
1.31G
                        *out++ = *from++;
253
1.31G
                        *out++ = *from++;
254
1.31G
                        *out++ = *from++;
255
1.31G
                        len -= 3;
256
1.31G
                    } while (len > 2);
257
245M
                    if (len) {
258
90.2M
                        *out++ = *from++;
259
90.2M
                        if (len > 1)
260
64.9M
                            *out++ = *from++;
261
90.2M
                    }
262
245M
                }
263
248M
            }
264
17.2M
            else if ((op & 64) == 0) {          /* 2nd level distance code */
265
17.2M
                here = dcode + here->val + (hold & ((1U << op) - 1));
266
17.2M
                goto dodist;
267
17.2M
            }
268
409
            else {
269
409
                strm->msg = (z_const char *)"invalid distance code";
270
409
                state->mode = BAD;
271
409
                break;
272
409
            }
273
265M
        }
274
15.5M
        else if ((op & 64) == 0) {              /* 2nd level length code */
275
15.5M
            here = lcode + here->val + (hold & ((1U << op) - 1));
276
15.5M
            goto dolen;
277
15.5M
        }
278
13.1k
        else if (op & 32) {                     /* end-of-block */
279
12.6k
            Tracevv((stderr, "inflate:         end of block\n"));
280
12.6k
            state->mode = TYPE;
281
12.6k
            break;
282
12.6k
        }
283
592
        else {
284
592
            strm->msg = (z_const char *)"invalid literal/length code";
285
592
            state->mode = BAD;
286
592
            break;
287
592
        }
288
911M
    } while (in < last && out < end);
289
290
    /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
291
651k
    len = bits >> 3;
292
651k
    in -= len;
293
651k
    bits -= len << 3;
294
651k
    hold &= (1U << bits) - 1;
295
296
    /* update state and return */
297
651k
    strm->next_in = in;
298
651k
    strm->next_out = out;
299
651k
    strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
300
651k
    strm->avail_out = (unsigned)(out < end ?
301
350k
                                 257 + (end - out) : 257 - (out - end));
302
651k
    state->hold = hold;
303
651k
    state->bits = bits;
304
651k
    return;
305
651k
}
306
307
/*
308
   inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
309
   - Using bit fields for code structure
310
   - Different op definition to avoid & for extra bits (do & for table bits)
311
   - Three separate decoding do-loops for direct, window, and wnext == 0
312
   - Special case for distance > 1 copies to do overlapped load and store copy
313
   - Explicit branch predictions (based on measured branch probabilities)
314
   - Deferring match copy and interspersed it with decoding subsequent codes
315
   - Swapping literal/length else
316
   - Swapping window/direct else
317
   - Larger unrolled copy loops (three is about right)
318
   - Moving len -= 3 statement into middle of loop
319
 */
320
321
#endif /* !ASMINF */