inffast.c 12.7 KB
Newer Older
M
Mark Adler 已提交
1
/* inffast.c -- fast decoding
M
Mark Adler 已提交
2
 * Copyright (C) 1995-2017 Mark Adler
M
Mark Adler 已提交
3
 * For conditions of distribution and use, see copyright notice in zlib.h
M
Mark Adler 已提交
4 5 6 7
 */

#include "zutil.h"
#include "inftrees.h"
M
Mark Adler 已提交
8
#include "inflate.h"
M
Mark Adler 已提交
9 10
#include "inffast.h"

11 12 13
#ifdef ASMINF
#  pragma message("Assembler code may have bugs -- use at your own risk")
#else
M
Mark Adler 已提交
14

M
Mark Adler 已提交
15 16 17 18 19 20 21
/*
   Decode literal, length, and distance codes and write out the resulting
   literal and match bytes until either not enough input or output is
   available, an end-of-block is encountered, or a data error is encountered.
   When large enough input and output buffers are supplied to inflate(), for
   example, a 16K input buffer and a 64K output buffer, more than 95% of the
   inflate execution time is spent in this routine.
M
Mark Adler 已提交
22

M
Mark Adler 已提交
23
   Entry assumptions:
M
Mark Adler 已提交
24

M
Mark Adler 已提交
25 26 27 28 29
        state->mode == LEN
        strm->avail_in >= 6
        strm->avail_out >= 258
        start >= strm->avail_out
        state->bits < 8
M
Mark Adler 已提交
30

M
Mark Adler 已提交
31
   On return, state->mode is one of:
M
Mark Adler 已提交
32

M
Mark Adler 已提交
33 34 35
        LEN -- ran out of enough output space or enough available input
        TYPE -- reached end of block code, inflate() to interpret next block
        BAD -- error in block data
M
Mark Adler 已提交
36

M
Mark Adler 已提交
37
   Notes:
M
Mark Adler 已提交
38

M
Mark Adler 已提交
39 40 41 42 43 44 45 46 47 48 49
    - The maximum input bits used by a length/distance pair is 15 bits for the
      length code, 5 bits for the length extra, 15 bits for the distance code,
      and 13 bits for the distance extra.  This totals 48 bits, or six bytes.
      Therefore if strm->avail_in >= 6, then there is enough input to avoid
      checking for available input while decoding.

    - The maximum bytes that a single length/distance pair can output is 258
      bytes, which is the maximum length that can be coded.  inflate_fast()
      requires strm->avail_out >= 258 for each loop to avoid checking for
      output space.
 */
M
Mark Adler 已提交
50
void ZLIB_INTERNAL inflate_fast(strm, start)
M
Mark Adler 已提交
51 52 53 54
z_streamp strm;
unsigned start;         /* inflate()'s starting value for strm->avail_out */
{
    struct inflate_state FAR *state;
55
    z_const unsigned char FAR *in;      /* local strm->next_in */
M
Mark Adler 已提交
56
    z_const unsigned char FAR *last;    /* have enough input while in < last */
M
Mark Adler 已提交
57 58 59
    unsigned char FAR *out;     /* local strm->next_out */
    unsigned char FAR *beg;     /* inflate()'s initial strm->next_out */
    unsigned char FAR *end;     /* while out < end, enough space available */
M
Mark Adler 已提交
60 61 62
#ifdef INFLATE_STRICT
    unsigned dmax;              /* maximum distance from zlib header */
#endif
M
Mark Adler 已提交
63
    unsigned wsize;             /* window size or zero if not using window */
M
Mark Adler 已提交
64
    unsigned whave;             /* valid bytes in the window */
M
Mark Adler 已提交
65
    unsigned wnext;             /* window write index */
M
Mark Adler 已提交
66 67 68 69 70 71 72
    unsigned char FAR *window;  /* allocated sliding window, if wsize != 0 */
    unsigned long hold;         /* local strm->hold */
    unsigned bits;              /* local strm->bits */
    code const FAR *lcode;      /* local strm->lencode */
    code const FAR *dcode;      /* local strm->distcode */
    unsigned lmask;             /* mask for first level of length codes */
    unsigned dmask;             /* mask for first level of distance codes */
M
Mark Adler 已提交
73
    code here;                  /* retrieved table entry */
M
Mark Adler 已提交
74 75 76 77 78
    unsigned op;                /* code bits, operation, extra bits, or */
                                /*  window position, window bytes to copy */
    unsigned len;               /* match length, unused bytes */
    unsigned dist;              /* match distance */
    unsigned char FAR *from;    /* where to copy match from */
M
Mark Adler 已提交
79

M
Mark Adler 已提交
80 81
    /* copy state to local variables */
    state = (struct inflate_state FAR *)strm->state;
82
    in = strm->next_in;
M
Mark Adler 已提交
83
    last = in + (strm->avail_in - 5);
84
    out = strm->next_out;
M
Mark Adler 已提交
85 86
    beg = out - (start - strm->avail_out);
    end = out + (strm->avail_out - 257);
M
Mark Adler 已提交
87 88 89
#ifdef INFLATE_STRICT
    dmax = state->dmax;
#endif
M
Mark Adler 已提交
90
    wsize = state->wsize;
M
Mark Adler 已提交
91
    whave = state->whave;
M
Mark Adler 已提交
92
    wnext = state->wnext;
M
Mark Adler 已提交
93 94 95 96 97 98 99
    window = state->window;
    hold = state->hold;
    bits = state->bits;
    lcode = state->lencode;
    dcode = state->distcode;
    lmask = (1U << state->lenbits) - 1;
    dmask = (1U << state->distbits) - 1;
M
Mark Adler 已提交
100

M
Mark Adler 已提交
101 102 103 104
    /* decode literals and length/distances until end-of-block or not enough
       input data or output space */
    do {
        if (bits < 15) {
105
            hold += (unsigned long)(*in++) << bits;
M
Mark Adler 已提交
106
            bits += 8;
107
            hold += (unsigned long)(*in++) << bits;
M
Mark Adler 已提交
108 109
            bits += 8;
        }
M
Mark Adler 已提交
110
        here = lcode[hold & lmask];
M
Mark Adler 已提交
111
      dolen:
M
Mark Adler 已提交
112
        op = (unsigned)(here.bits);
M
Mark Adler 已提交
113 114
        hold >>= op;
        bits -= op;
M
Mark Adler 已提交
115
        op = (unsigned)(here.op);
M
Mark Adler 已提交
116
        if (op == 0) {                          /* literal */
M
Mark Adler 已提交
117
            Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
M
Mark Adler 已提交
118
                    "inflate:         literal '%c'\n" :
M
Mark Adler 已提交
119
                    "inflate:         literal 0x%02x\n", here.val));
120
            *out++ = (unsigned char)(here.val);
M
Mark Adler 已提交
121 122
        }
        else if (op & 16) {                     /* length base */
M
Mark Adler 已提交
123
            len = (unsigned)(here.val);
M
Mark Adler 已提交
124 125 126
            op &= 15;                           /* number of extra bits */
            if (op) {
                if (bits < op) {
127
                    hold += (unsigned long)(*in++) << bits;
M
Mark Adler 已提交
128 129
                    bits += 8;
                }
M
Mark Adler 已提交
130
                len += (unsigned)hold & ((1U << op) - 1);
M
Mark Adler 已提交
131 132 133 134 135
                hold >>= op;
                bits -= op;
            }
            Tracevv((stderr, "inflate:         length %u\n", len));
            if (bits < 15) {
136
                hold += (unsigned long)(*in++) << bits;
M
Mark Adler 已提交
137
                bits += 8;
138
                hold += (unsigned long)(*in++) << bits;
M
Mark Adler 已提交
139
                bits += 8;
M
Mark Adler 已提交
140
            }
M
Mark Adler 已提交
141
            here = dcode[hold & dmask];
M
Mark Adler 已提交
142
          dodist:
M
Mark Adler 已提交
143
            op = (unsigned)(here.bits);
M
Mark Adler 已提交
144 145
            hold >>= op;
            bits -= op;
M
Mark Adler 已提交
146
            op = (unsigned)(here.op);
M
Mark Adler 已提交
147
            if (op & 16) {                      /* distance base */
M
Mark Adler 已提交
148
                dist = (unsigned)(here.val);
M
Mark Adler 已提交
149 150
                op &= 15;                       /* number of extra bits */
                if (bits < op) {
151
                    hold += (unsigned long)(*in++) << bits;
M
Mark Adler 已提交
152 153
                    bits += 8;
                    if (bits < op) {
154
                        hold += (unsigned long)(*in++) << bits;
M
Mark Adler 已提交
155 156 157
                        bits += 8;
                    }
                }
M
Mark Adler 已提交
158
                dist += (unsigned)hold & ((1U << op) - 1);
M
Mark Adler 已提交
159 160 161 162 163 164 165
#ifdef INFLATE_STRICT
                if (dist > dmax) {
                    strm->msg = (char *)"invalid distance too far back";
                    state->mode = BAD;
                    break;
                }
#endif
M
Mark Adler 已提交
166 167 168 169 170
                hold >>= op;
                bits -= op;
                Tracevv((stderr, "inflate:         distance %u\n", dist));
                op = (unsigned)(out - beg);     /* max distance in output */
                if (dist > op) {                /* see if copy from window */
M
Mark Adler 已提交
171 172
                    op = dist - op;             /* distance back in window */
                    if (op > whave) {
M
Mark Adler 已提交
173
                        if (state->sane) {
M
Mark Adler 已提交
174 175
                            strm->msg =
                                (char *)"invalid distance too far back";
M
Mark Adler 已提交
176 177 178 179 180 181
                            state->mode = BAD;
                            break;
                        }
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
                        if (len <= op - whave) {
                            do {
182
                                *out++ = 0;
M
Mark Adler 已提交
183 184 185 186 187
                            } while (--len);
                            continue;
                        }
                        len -= op - whave;
                        do {
188
                            *out++ = 0;
M
Mark Adler 已提交
189 190 191 192
                        } while (--op > whave);
                        if (op == 0) {
                            from = out - dist;
                            do {
193
                                *out++ = *from++;
M
Mark Adler 已提交
194 195 196 197
                            } while (--len);
                            continue;
                        }
#endif
M
Mark Adler 已提交
198
                    }
199
                    from = window;
M
Mark Adler 已提交
200
                    if (wnext == 0) {           /* very common case */
M
Mark Adler 已提交
201 202 203 204
                        from += wsize - op;
                        if (op < len) {         /* some from window */
                            len -= op;
                            do {
205
                                *out++ = *from++;
M
Mark Adler 已提交
206 207 208 209
                            } while (--op);
                            from = out - dist;  /* rest from output */
                        }
                    }
M
Mark Adler 已提交
210 211 212
                    else if (wnext < op) {      /* wrap around window */
                        from += wsize + wnext - op;
                        op -= wnext;
M
Mark Adler 已提交
213 214 215
                        if (op < len) {         /* some from end of window */
                            len -= op;
                            do {
216
                                *out++ = *from++;
M
Mark Adler 已提交
217
                            } while (--op);
218
                            from = window;
M
Mark Adler 已提交
219 220
                            if (wnext < len) {  /* some from start of window */
                                op = wnext;
M
Mark Adler 已提交
221 222
                                len -= op;
                                do {
223
                                    *out++ = *from++;
M
Mark Adler 已提交
224 225 226 227 228 229
                                } while (--op);
                                from = out - dist;      /* rest from output */
                            }
                        }
                    }
                    else {                      /* contiguous in window */
M
Mark Adler 已提交
230
                        from += wnext - op;
M
Mark Adler 已提交
231 232 233
                        if (op < len) {         /* some from window */
                            len -= op;
                            do {
234
                                *out++ = *from++;
M
Mark Adler 已提交
235 236 237 238 239
                            } while (--op);
                            from = out - dist;  /* rest from output */
                        }
                    }
                    while (len > 2) {
240 241 242
                        *out++ = *from++;
                        *out++ = *from++;
                        *out++ = *from++;
M
Mark Adler 已提交
243 244 245
                        len -= 3;
                    }
                    if (len) {
246
                        *out++ = *from++;
M
Mark Adler 已提交
247
                        if (len > 1)
248
                            *out++ = *from++;
M
Mark Adler 已提交
249 250 251 252 253
                    }
                }
                else {
                    from = out - dist;          /* copy direct from output */
                    do {                        /* minimum length is three */
254 255 256
                        *out++ = *from++;
                        *out++ = *from++;
                        *out++ = *from++;
M
Mark Adler 已提交
257 258 259
                        len -= 3;
                    } while (len > 2);
                    if (len) {
260
                        *out++ = *from++;
M
Mark Adler 已提交
261
                        if (len > 1)
262
                            *out++ = *from++;
M
Mark Adler 已提交
263 264
                    }
                }
M
Mark Adler 已提交
265
            }
M
Mark Adler 已提交
266
            else if ((op & 64) == 0) {          /* 2nd level distance code */
M
Mark Adler 已提交
267
                here = dcode[here.val + (hold & ((1U << op) - 1))];
M
Mark Adler 已提交
268 269 270 271 272 273 274 275 276
                goto dodist;
            }
            else {
                strm->msg = (char *)"invalid distance code";
                state->mode = BAD;
                break;
            }
        }
        else if ((op & 64) == 0) {              /* 2nd level length code */
M
Mark Adler 已提交
277
            here = lcode[here.val + (hold & ((1U << op) - 1))];
M
Mark Adler 已提交
278 279 280 281 282
            goto dolen;
        }
        else if (op & 32) {                     /* end-of-block */
            Tracevv((stderr, "inflate:         end of block\n"));
            state->mode = TYPE;
M
Mark Adler 已提交
283
            break;
M
Mark Adler 已提交
284
        }
M
Mark Adler 已提交
285 286 287 288 289 290 291 292 293 294 295 296
        else {
            strm->msg = (char *)"invalid literal/length code";
            state->mode = BAD;
            break;
        }
    } while (in < last && out < end);

    /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
    len = bits >> 3;
    in -= len;
    bits -= len << 3;
    hold &= (1U << bits) - 1;
M
Mark Adler 已提交
297

M
Mark Adler 已提交
298
    /* update state and return */
299 300
    strm->next_in = in;
    strm->next_out = out;
M
Mark Adler 已提交
301 302 303 304 305 306
    strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
    strm->avail_out = (unsigned)(out < end ?
                                 257 + (end - out) : 257 - (out - end));
    state->hold = hold;
    state->bits = bits;
    return;
M
Mark Adler 已提交
307
}
M
Mark Adler 已提交
308 309 310 311 312

/*
   inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
   - Using bit fields for code structure
   - Different op definition to avoid & for extra bits (do & for table bits)
M
Mark Adler 已提交
313
   - Three separate decoding do-loops for direct, window, and wnext == 0
M
Mark Adler 已提交
314 315 316 317 318 319 320 321
   - Special case for distance > 1 copies to do overlapped load and store copy
   - Explicit branch predictions (based on measured branch probabilities)
   - Deferring match copy and interspersed it with decoding subsequent codes
   - Swapping literal/length else
   - Swapping window/direct else
   - Larger unrolled copy loops (three is about right)
   - Moving len -= 3 statement into middle of loop
 */
M
Mark Adler 已提交
322 323

#endif /* !ASMINF */