Coverage Report

Created: 2026-01-09 06:32

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-2025 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
29.1k
void ZLIB_INTERNAL inflate_fast(z_streamp strm, unsigned start) {
51
29.1k
    struct inflate_state FAR *state;
52
29.1k
    z_const unsigned char FAR *in;      /* local strm->next_in */
53
29.1k
    z_const unsigned char FAR *last;    /* have enough input while in < last */
54
29.1k
    unsigned char FAR *out;     /* local strm->next_out */
55
29.1k
    unsigned char FAR *beg;     /* inflate()'s initial strm->next_out */
56
29.1k
    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
29.1k
    unsigned wsize;             /* window size or zero if not using window */
61
29.1k
    unsigned whave;             /* valid bytes in the window */
62
29.1k
    unsigned wnext;             /* window write index */
63
29.1k
    unsigned char FAR *window;  /* allocated sliding window, if wsize != 0 */
64
29.1k
    unsigned long hold;         /* local strm->hold */
65
29.1k
    unsigned bits;              /* local strm->bits */
66
29.1k
    code const FAR *lcode;      /* local strm->lencode */
67
29.1k
    code const FAR *dcode;      /* local strm->distcode */
68
29.1k
    unsigned lmask;             /* mask for first level of length codes */
69
29.1k
    unsigned dmask;             /* mask for first level of distance codes */
70
29.1k
    code const *here;           /* retrieved table entry */
71
29.1k
    unsigned op;                /* code bits, operation, extra bits, or */
72
                                /*  window position, window bytes to copy */
73
29.1k
    unsigned len;               /* match length, unused bytes */
74
29.1k
    unsigned dist;              /* match distance */
75
29.1k
    unsigned char FAR *from;    /* where to copy match from */
76
77
    /* copy state to local variables */
78
29.1k
    state = (struct inflate_state FAR *)strm->state;
79
29.1k
    in = strm->next_in;
80
29.1k
    last = in + (strm->avail_in - 5);
81
29.1k
    out = strm->next_out;
82
29.1k
    beg = out - (start - strm->avail_out);
83
29.1k
    end = out + (strm->avail_out - 257);
84
#ifdef INFLATE_STRICT
85
    dmax = state->dmax;
86
#endif
87
29.1k
    wsize = state->wsize;
88
29.1k
    whave = state->whave;
89
29.1k
    wnext = state->wnext;
90
29.1k
    window = state->window;
91
29.1k
    hold = state->hold;
92
29.1k
    bits = state->bits;
93
29.1k
    lcode = state->lencode;
94
29.1k
    dcode = state->distcode;
95
29.1k
    lmask = (1U << state->lenbits) - 1;
96
29.1k
    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
4.22M
    do {
101
4.22M
        if (bits < 15) {
102
2.02M
            hold += (unsigned long)(*in++) << bits;
103
2.02M
            bits += 8;
104
2.02M
            hold += (unsigned long)(*in++) << bits;
105
2.02M
            bits += 8;
106
2.02M
        }
107
4.22M
        here = lcode + (hold & lmask);
108
4.36M
      dolen:
109
4.36M
        op = (unsigned)(here->bits);
110
4.36M
        hold >>= op;
111
4.36M
        bits -= op;
112
4.36M
        op = (unsigned)(here->op);
113
4.36M
        if (op == 0) {                          /* literal */
114
2.47M
            Tracevv((stderr, here->val >= 0x20 && here->val < 0x7f ?
115
2.47M
                    "inflate:         literal '%c'\n" :
116
2.47M
                    "inflate:         literal 0x%02x\n", here->val));
117
2.47M
            *out++ = (unsigned char)(here->val);
118
2.47M
        }
119
1.89M
        else if (op & 16) {                     /* length base */
120
1.74M
            len = (unsigned)(here->val);
121
1.74M
            op &= 15;                           /* number of extra bits */
122
1.74M
            if (op) {
123
319k
                if (bits < op) {
124
3.67k
                    hold += (unsigned long)(*in++) << bits;
125
3.67k
                    bits += 8;
126
3.67k
                }
127
319k
                len += (unsigned)hold & ((1U << op) - 1);
128
319k
                hold >>= op;
129
319k
                bits -= op;
130
319k
            }
131
1.74M
            Tracevv((stderr, "inflate:         length %u\n", len));
132
1.74M
            if (bits < 15) {
133
506k
                hold += (unsigned long)(*in++) << bits;
134
506k
                bits += 8;
135
506k
                hold += (unsigned long)(*in++) << bits;
136
506k
                bits += 8;
137
506k
            }
138
1.74M
            here = dcode + (hold & dmask);
139
1.78M
          dodist:
140
1.78M
            op = (unsigned)(here->bits);
141
1.78M
            hold >>= op;
142
1.78M
            bits -= op;
143
1.78M
            op = (unsigned)(here->op);
144
1.78M
            if (op & 16) {                      /* distance base */
145
1.74M
                dist = (unsigned)(here->val);
146
1.74M
                op &= 15;                       /* number of extra bits */
147
1.74M
                if (bits < op) {
148
3.72k
                    hold += (unsigned long)(*in++) << bits;
149
3.72k
                    bits += 8;
150
3.72k
                    if (bits < op) {
151
0
                        hold += (unsigned long)(*in++) << bits;
152
0
                        bits += 8;
153
0
                    }
154
3.72k
                }
155
1.74M
                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
1.74M
                hold >>= op;
165
1.74M
                bits -= op;
166
1.74M
                Tracevv((stderr, "inflate:         distance %u\n", dist));
167
1.74M
                op = (unsigned)(out - beg);     /* max distance in output */
168
1.74M
                if (dist > op) {                /* see if copy from window */
169
9.13k
                    op = dist - op;             /* distance back in window */
170
9.13k
                    if (op > whave) {
171
375
                        if (state->sane) {
172
375
                            strm->msg = (z_const char *)
173
375
                                "invalid distance too far back";
174
375
                            state->mode = BAD;
175
375
                            break;
176
375
                        }
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
375
                    }
197
8.75k
                    from = window;
198
8.75k
                    if (wnext == 0) {           /* very common case */
199
6.53k
                        from += wsize - op;
200
6.53k
                        if (op < len) {         /* some from window */
201
246
                            len -= op;
202
15.6k
                            do {
203
15.6k
                                *out++ = *from++;
204
15.6k
                            } while (--op);
205
246
                            from = out - dist;  /* rest from output */
206
246
                        }
207
6.53k
                    }
208
2.22k
                    else if (wnext < op) {      /* wrap around window */
209
0
                        from += wsize + wnext - op;
210
0
                        op -= wnext;
211
0
                        if (op < len) {         /* some from end of window */
212
0
                            len -= op;
213
0
                            do {
214
0
                                *out++ = *from++;
215
0
                            } while (--op);
216
0
                            from = window;
217
0
                            if (wnext < len) {  /* some from start of window */
218
0
                                op = wnext;
219
0
                                len -= op;
220
0
                                do {
221
0
                                    *out++ = *from++;
222
0
                                } while (--op);
223
0
                                from = out - dist;      /* rest from output */
224
0
                            }
225
0
                        }
226
0
                    }
227
2.22k
                    else {                      /* contiguous in window */
228
2.22k
                        from += wnext - op;
229
2.22k
                        if (op < len) {         /* some from window */
230
257
                            len -= op;
231
833
                            do {
232
833
                                *out++ = *from++;
233
833
                            } while (--op);
234
257
                            from = out - dist;  /* rest from output */
235
257
                        }
236
2.22k
                    }
237
51.4k
                    while (len > 2) {
238
42.7k
                        *out++ = *from++;
239
42.7k
                        *out++ = *from++;
240
42.7k
                        *out++ = *from++;
241
42.7k
                        len -= 3;
242
42.7k
                    }
243
8.75k
                    if (len) {
244
5.51k
                        *out++ = *from++;
245
5.51k
                        if (len > 1)
246
2.31k
                            *out++ = *from++;
247
5.51k
                    }
248
8.75k
                }
249
1.73M
                else {
250
1.73M
                    from = out - dist;          /* copy direct from output */
251
19.9M
                    do {                        /* minimum length is three */
252
19.9M
                        *out++ = *from++;
253
19.9M
                        *out++ = *from++;
254
19.9M
                        *out++ = *from++;
255
19.9M
                        len -= 3;
256
19.9M
                    } while (len > 2);
257
1.73M
                    if (len) {
258
923k
                        *out++ = *from++;
259
923k
                        if (len > 1)
260
418k
                            *out++ = *from++;
261
923k
                    }
262
1.73M
                }
263
1.74M
            }
264
36.1k
            else if ((op & 64) == 0) {          /* 2nd level distance code */
265
36.1k
                here = dcode + here->val + (hold & ((1U << op) - 1));
266
36.1k
                goto dodist;
267
36.1k
            }
268
24
            else {
269
24
                strm->msg = (z_const char *)"invalid distance code";
270
24
                state->mode = BAD;
271
24
                break;
272
24
            }
273
1.78M
        }
274
150k
        else if ((op & 64) == 0) {              /* 2nd level length code */
275
141k
            here = lcode + here->val + (hold & ((1U << op) - 1));
276
141k
            goto dolen;
277
141k
        }
278
9.32k
        else if (op & 32) {                     /* end-of-block */
279
9.31k
            Tracevv((stderr, "inflate:         end of block\n"));
280
9.31k
            state->mode = TYPE;
281
9.31k
            break;
282
9.31k
        }
283
12
        else {
284
12
            strm->msg = (z_const char *)"invalid literal/length code";
285
12
            state->mode = BAD;
286
12
            break;
287
12
        }
288
4.36M
    } while (in < last && out < end);
289
290
    /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
291
29.1k
    len = bits >> 3;
292
29.1k
    in -= len;
293
29.1k
    bits -= len << 3;
294
29.1k
    hold &= (1U << bits) - 1;
295
296
    /* update state and return */
297
29.1k
    strm->next_in = in;
298
29.1k
    strm->next_out = out;
299
29.1k
    strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
300
29.1k
    strm->avail_out = (unsigned)(out < end ?
301
28.6k
                                 257 + (end - out) : 257 - (out - end));
302
29.1k
    state->hold = hold;
303
29.1k
    state->bits = bits;
304
29.1k
    return;
305
29.1k
}
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 */