Coverage Report

Created: 2025-06-16 07:00

/src/zlib/inffast.c
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 "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
758k
void ZLIB_INTERNAL inflate_fast(z_streamp strm, unsigned start) {
51
758k
    struct inflate_state FAR *state;
52
758k
    z_const unsigned char FAR *in;      /* local strm->next_in */
53
758k
    z_const unsigned char FAR *last;    /* have enough input while in < last */
54
758k
    unsigned char FAR *out;     /* local strm->next_out */
55
758k
    unsigned char FAR *beg;     /* inflate()'s initial strm->next_out */
56
758k
    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
758k
    unsigned wsize;             /* window size or zero if not using window */
61
758k
    unsigned whave;             /* valid bytes in the window */
62
758k
    unsigned wnext;             /* window write index */
63
758k
    unsigned char FAR *window;  /* allocated sliding window, if wsize != 0 */
64
758k
    unsigned long hold;         /* local strm->hold */
65
758k
    unsigned bits;              /* local strm->bits */
66
758k
    code const FAR *lcode;      /* local strm->lencode */
67
758k
    code const FAR *dcode;      /* local strm->distcode */
68
758k
    unsigned lmask;             /* mask for first level of length codes */
69
758k
    unsigned dmask;             /* mask for first level of distance codes */
70
758k
    code const *here;           /* retrieved table entry */
71
758k
    unsigned op;                /* code bits, operation, extra bits, or */
72
                                /*  window position, window bytes to copy */
73
758k
    unsigned len;               /* match length, unused bytes */
74
758k
    unsigned dist;              /* match distance */
75
758k
    unsigned char FAR *from;    /* where to copy match from */
76
77
    /* copy state to local variables */
78
758k
    state = (struct inflate_state FAR *)strm->state;
79
758k
    in = strm->next_in;
80
758k
    last = in + (strm->avail_in - 5);
81
758k
    out = strm->next_out;
82
758k
    beg = out - (start - strm->avail_out);
83
758k
    end = out + (strm->avail_out - 257);
84
#ifdef INFLATE_STRICT
85
    dmax = state->dmax;
86
#endif
87
758k
    wsize = state->wsize;
88
758k
    whave = state->whave;
89
758k
    wnext = state->wnext;
90
758k
    window = state->window;
91
758k
    hold = state->hold;
92
758k
    bits = state->bits;
93
758k
    lcode = state->lencode;
94
758k
    dcode = state->distcode;
95
758k
    lmask = (1U << state->lenbits) - 1;
96
758k
    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
18.3M
    do {
101
18.3M
        if (bits < 15) {
102
4.65M
            hold += (unsigned long)(*in++) << bits;
103
4.65M
            bits += 8;
104
4.65M
            hold += (unsigned long)(*in++) << bits;
105
4.65M
            bits += 8;
106
4.65M
        }
107
18.3M
        here = lcode + (hold & lmask);
108
18.7M
      dolen:
109
18.7M
        op = (unsigned)(here->bits);
110
18.7M
        hold >>= op;
111
18.7M
        bits -= op;
112
18.7M
        op = (unsigned)(here->op);
113
18.7M
        if (op == 0) {                          /* literal */
114
3.31M
            Tracevv((stderr, here->val >= 0x20 && here->val < 0x7f ?
115
3.31M
                    "inflate:         literal '%c'\n" :
116
3.31M
                    "inflate:         literal 0x%02x\n", here->val));
117
3.31M
            *out++ = (unsigned char)(here->val);
118
3.31M
        }
119
15.4M
        else if (op & 16) {                     /* length base */
120
14.7M
            len = (unsigned)(here->val);
121
14.7M
            op &= 15;                           /* number of extra bits */
122
14.7M
            if (op) {
123
1.30M
                if (bits < op) {
124
24.7k
                    hold += (unsigned long)(*in++) << bits;
125
24.7k
                    bits += 8;
126
24.7k
                }
127
1.30M
                len += (unsigned)hold & ((1U << op) - 1);
128
1.30M
                hold >>= op;
129
1.30M
                bits -= op;
130
1.30M
            }
131
14.7M
            Tracevv((stderr, "inflate:         length %u\n", len));
132
14.7M
            if (bits < 15) {
133
2.22M
                hold += (unsigned long)(*in++) << bits;
134
2.22M
                bits += 8;
135
2.22M
                hold += (unsigned long)(*in++) << bits;
136
2.22M
                bits += 8;
137
2.22M
            }
138
14.7M
            here = dcode + (hold & dmask);
139
15.1M
          dodist:
140
15.1M
            op = (unsigned)(here->bits);
141
15.1M
            hold >>= op;
142
15.1M
            bits -= op;
143
15.1M
            op = (unsigned)(here->op);
144
15.1M
            if (op & 16) {                      /* distance base */
145
14.7M
                dist = (unsigned)(here->val);
146
14.7M
                op &= 15;                       /* number of extra bits */
147
14.7M
                if (bits < op) {
148
115k
                    hold += (unsigned long)(*in++) << bits;
149
115k
                    bits += 8;
150
115k
                    if (bits < op) {
151
14.9k
                        hold += (unsigned long)(*in++) << bits;
152
14.9k
                        bits += 8;
153
14.9k
                    }
154
115k
                }
155
14.7M
                dist += (unsigned)hold & ((1U << op) - 1);
156
#ifdef INFLATE_STRICT
157
                if (dist > dmax) {
158
                    strm->msg = (z_const char *)"invalid distance too far back";
159
                    state->mode = BAD;
160
                    break;
161
                }
162
#endif
163
14.7M
                hold >>= op;
164
14.7M
                bits -= op;
165
14.7M
                Tracevv((stderr, "inflate:         distance %u\n", dist));
166
14.7M
                op = (unsigned)(out - beg);     /* max distance in output */
167
14.7M
                if (dist > op) {                /* see if copy from window */
168
428k
                    op = dist - op;             /* distance back in window */
169
428k
                    if (op > whave) {
170
2.84k
                        if (state->sane) {
171
2.84k
                            strm->msg =
172
2.84k
                                (z_const char *)"invalid distance too far back";
173
2.84k
                            state->mode = BAD;
174
2.84k
                            break;
175
2.84k
                        }
176
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
177
                        if (len <= op - whave) {
178
                            do {
179
                                *out++ = 0;
180
                            } while (--len);
181
                            continue;
182
                        }
183
                        len -= op - whave;
184
                        do {
185
                            *out++ = 0;
186
                        } while (--op > whave);
187
                        if (op == 0) {
188
                            from = out - dist;
189
                            do {
190
                                *out++ = *from++;
191
                            } while (--len);
192
                            continue;
193
                        }
194
#endif
195
2.84k
                    }
196
426k
                    from = window;
197
426k
                    if (wnext == 0) {           /* very common case */
198
121k
                        from += wsize - op;
199
121k
                        if (op < len) {         /* some from window */
200
12.3k
                            len -= op;
201
1.09M
                            do {
202
1.09M
                                *out++ = *from++;
203
1.09M
                            } while (--op);
204
12.3k
                            from = out - dist;  /* rest from output */
205
12.3k
                        }
206
121k
                    }
207
304k
                    else if (wnext < op) {      /* wrap around window */
208
90.6k
                        from += wsize + wnext - op;
209
90.6k
                        op -= wnext;
210
90.6k
                        if (op < len) {         /* some from end of window */
211
8.56k
                            len -= op;
212
899k
                            do {
213
899k
                                *out++ = *from++;
214
899k
                            } while (--op);
215
8.56k
                            from = window;
216
8.56k
                            if (wnext < len) {  /* some from start of window */
217
5.15k
                                op = wnext;
218
5.15k
                                len -= op;
219
183k
                                do {
220
183k
                                    *out++ = *from++;
221
183k
                                } while (--op);
222
5.15k
                                from = out - dist;      /* rest from output */
223
5.15k
                            }
224
8.56k
                        }
225
90.6k
                    }
226
213k
                    else {                      /* contiguous in window */
227
213k
                        from += wnext - op;
228
213k
                        if (op < len) {         /* some from window */
229
28.3k
                            len -= op;
230
1.80M
                            do {
231
1.80M
                                *out++ = *from++;
232
1.80M
                            } while (--op);
233
28.3k
                            from = out - dist;  /* rest from output */
234
28.3k
                        }
235
213k
                    }
236
17.2M
                    while (len > 2) {
237
16.7M
                        *out++ = *from++;
238
16.7M
                        *out++ = *from++;
239
16.7M
                        *out++ = *from++;
240
16.7M
                        len -= 3;
241
16.7M
                    }
242
426k
                    if (len) {
243
261k
                        *out++ = *from++;
244
261k
                        if (len > 1)
245
79.4k
                            *out++ = *from++;
246
261k
                    }
247
426k
                }
248
14.3M
                else {
249
14.3M
                    from = out - dist;          /* copy direct from output */
250
1.12G
                    do {                        /* minimum length is three */
251
1.12G
                        *out++ = *from++;
252
1.12G
                        *out++ = *from++;
253
1.12G
                        *out++ = *from++;
254
1.12G
                        len -= 3;
255
1.12G
                    } while (len > 2);
256
14.3M
                    if (len) {
257
875k
                        *out++ = *from++;
258
875k
                        if (len > 1)
259
339k
                            *out++ = *from++;
260
875k
                    }
261
14.3M
                }
262
14.7M
            }
263
361k
            else if ((op & 64) == 0) {          /* 2nd level distance code */
264
360k
                here = dcode + here->val + (hold & ((1U << op) - 1));
265
360k
                goto dodist;
266
360k
            }
267
1.17k
            else {
268
1.17k
                strm->msg = (z_const char *)"invalid distance code";
269
1.17k
                state->mode = BAD;
270
1.17k
                break;
271
1.17k
            }
272
15.1M
        }
273
614k
        else if ((op & 64) == 0) {              /* 2nd level length code */
274
410k
            here = lcode + here->val + (hold & ((1U << op) - 1));
275
410k
            goto dolen;
276
410k
        }
277
203k
        else if (op & 32) {                     /* end-of-block */
278
202k
            Tracevv((stderr, "inflate:         end of block\n"));
279
202k
            state->mode = TYPE;
280
202k
            break;
281
202k
        }
282
769
        else {
283
769
            strm->msg = (z_const char *)"invalid literal/length code";
284
769
            state->mode = BAD;
285
769
            break;
286
769
        }
287
18.7M
    } while (in < last && out < end);
288
289
    /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
290
758k
    len = bits >> 3;
291
758k
    in -= len;
292
758k
    bits -= len << 3;
293
758k
    hold &= (1U << bits) - 1;
294
295
    /* update state and return */
296
758k
    strm->next_in = in;
297
758k
    strm->next_out = out;
298
758k
    strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
299
758k
    strm->avail_out = (unsigned)(out < end ?
300
398k
                                 257 + (end - out) : 257 - (out - end));
301
758k
    state->hold = hold;
302
758k
    state->bits = bits;
303
758k
    return;
304
758k
}
305
306
/*
307
   inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
308
   - Using bit fields for code structure
309
   - Different op definition to avoid & for extra bits (do & for table bits)
310
   - Three separate decoding do-loops for direct, window, and wnext == 0
311
   - Special case for distance > 1 copies to do overlapped load and store copy
312
   - Explicit branch predictions (based on measured branch probabilities)
313
   - Deferring match copy and interspersed it with decoding subsequent codes
314
   - Swapping literal/length else
315
   - Swapping window/direct else
316
   - Larger unrolled copy loops (three is about right)
317
   - Moving len -= 3 statement into middle of loop
318
 */
319
320
#endif /* !ASMINF */