xref: /third_party/node/deps/zlib/inffast.c (revision 1cb0ef41)
11cb0ef41Sopenharmony_ci/* inffast.c -- fast decoding
21cb0ef41Sopenharmony_ci * Copyright (C) 1995-2017 Mark Adler
31cb0ef41Sopenharmony_ci * For conditions of distribution and use, see copyright notice in zlib.h
41cb0ef41Sopenharmony_ci */
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci#include "zutil.h"
71cb0ef41Sopenharmony_ci#include "inftrees.h"
81cb0ef41Sopenharmony_ci#include "inflate.h"
91cb0ef41Sopenharmony_ci#include "inffast.h"
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci#ifdef ASMINF
121cb0ef41Sopenharmony_ci#  pragma message("Assembler code may have bugs -- use at your own risk")
131cb0ef41Sopenharmony_ci#else
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci/*
161cb0ef41Sopenharmony_ci   Decode literal, length, and distance codes and write out the resulting
171cb0ef41Sopenharmony_ci   literal and match bytes until either not enough input or output is
181cb0ef41Sopenharmony_ci   available, an end-of-block is encountered, or a data error is encountered.
191cb0ef41Sopenharmony_ci   When large enough input and output buffers are supplied to inflate(), for
201cb0ef41Sopenharmony_ci   example, a 16K input buffer and a 64K output buffer, more than 95% of the
211cb0ef41Sopenharmony_ci   inflate() execution time is spent in this routine.
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci   Entry assumptions:
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci        state->mode == LEN
261cb0ef41Sopenharmony_ci        strm->avail_in >= INFLATE_FAST_MIN_INPUT (6 bytes)
271cb0ef41Sopenharmony_ci        strm->avail_out >= INFLATE_FAST_MIN_OUTPUT (258 bytes)
281cb0ef41Sopenharmony_ci        start >= strm->avail_out
291cb0ef41Sopenharmony_ci        state->bits < 8
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci   On return, state->mode is one of:
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci        LEN -- ran out of enough output space or enough available input
341cb0ef41Sopenharmony_ci        TYPE -- reached end of block code, inflate() to interpret next block
351cb0ef41Sopenharmony_ci        BAD -- error in block data
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci   Notes:
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci    INFLATE_FAST_MIN_INPUT: 6 bytes
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci    - The maximum input bits used by a length/distance pair is 15 bits for the
421cb0ef41Sopenharmony_ci      length code, 5 bits for the length extra, 15 bits for the distance code,
431cb0ef41Sopenharmony_ci      and 13 bits for the distance extra.  This totals 48 bits, or six bytes.
441cb0ef41Sopenharmony_ci      Therefore if strm->avail_in >= 6, then there is enough input to avoid
451cb0ef41Sopenharmony_ci      checking for available input while decoding.
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci    INFLATE_FAST_MIN_OUTPUT: 258 bytes
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci    - The maximum bytes that a single length/distance pair can output is 258
501cb0ef41Sopenharmony_ci      bytes, which is the maximum length that can be coded.  inflate_fast()
511cb0ef41Sopenharmony_ci      requires strm->avail_out >= 258 for each loop to avoid checking for
521cb0ef41Sopenharmony_ci      available output space while decoding.
531cb0ef41Sopenharmony_ci */
541cb0ef41Sopenharmony_civoid ZLIB_INTERNAL inflate_fast(z_streamp strm, unsigned start) {
551cb0ef41Sopenharmony_ci    struct inflate_state FAR *state;
561cb0ef41Sopenharmony_ci    z_const unsigned char FAR *in;      /* local strm->next_in */
571cb0ef41Sopenharmony_ci    z_const unsigned char FAR *last;    /* have enough input while in < last */
581cb0ef41Sopenharmony_ci    unsigned char FAR *out;     /* local strm->next_out */
591cb0ef41Sopenharmony_ci    unsigned char FAR *beg;     /* inflate()'s initial strm->next_out */
601cb0ef41Sopenharmony_ci    unsigned char FAR *end;     /* while out < end, enough space available */
611cb0ef41Sopenharmony_ci#ifdef INFLATE_STRICT
621cb0ef41Sopenharmony_ci    unsigned dmax;              /* maximum distance from zlib header */
631cb0ef41Sopenharmony_ci#endif
641cb0ef41Sopenharmony_ci    unsigned wsize;             /* window size or zero if not using window */
651cb0ef41Sopenharmony_ci    unsigned whave;             /* valid bytes in the window */
661cb0ef41Sopenharmony_ci    unsigned wnext;             /* window write index */
671cb0ef41Sopenharmony_ci    unsigned char FAR *window;  /* allocated sliding window, if wsize != 0 */
681cb0ef41Sopenharmony_ci    unsigned long hold;         /* local strm->hold */
691cb0ef41Sopenharmony_ci    unsigned bits;              /* local strm->bits */
701cb0ef41Sopenharmony_ci    code const FAR *lcode;      /* local strm->lencode */
711cb0ef41Sopenharmony_ci    code const FAR *dcode;      /* local strm->distcode */
721cb0ef41Sopenharmony_ci    unsigned lmask;             /* mask for first level of length codes */
731cb0ef41Sopenharmony_ci    unsigned dmask;             /* mask for first level of distance codes */
741cb0ef41Sopenharmony_ci    code const *here;           /* retrieved table entry */
751cb0ef41Sopenharmony_ci    unsigned op;                /* code bits, operation, extra bits, or */
761cb0ef41Sopenharmony_ci                                /*  window position, window bytes to copy */
771cb0ef41Sopenharmony_ci    unsigned len;               /* match length, unused bytes */
781cb0ef41Sopenharmony_ci    unsigned dist;              /* match distance */
791cb0ef41Sopenharmony_ci    unsigned char FAR *from;    /* where to copy match from */
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci    /* copy state to local variables */
821cb0ef41Sopenharmony_ci    state = (struct inflate_state FAR *)strm->state;
831cb0ef41Sopenharmony_ci    in = strm->next_in;
841cb0ef41Sopenharmony_ci    last = in + (strm->avail_in - (INFLATE_FAST_MIN_INPUT - 1));
851cb0ef41Sopenharmony_ci    out = strm->next_out;
861cb0ef41Sopenharmony_ci    beg = out - (start - strm->avail_out);
871cb0ef41Sopenharmony_ci    end = out + (strm->avail_out - (INFLATE_FAST_MIN_OUTPUT - 1));
881cb0ef41Sopenharmony_ci#ifdef INFLATE_STRICT
891cb0ef41Sopenharmony_ci    dmax = state->dmax;
901cb0ef41Sopenharmony_ci#endif
911cb0ef41Sopenharmony_ci    wsize = state->wsize;
921cb0ef41Sopenharmony_ci    whave = state->whave;
931cb0ef41Sopenharmony_ci    wnext = state->wnext;
941cb0ef41Sopenharmony_ci    window = state->window;
951cb0ef41Sopenharmony_ci    hold = state->hold;
961cb0ef41Sopenharmony_ci    bits = state->bits;
971cb0ef41Sopenharmony_ci    lcode = state->lencode;
981cb0ef41Sopenharmony_ci    dcode = state->distcode;
991cb0ef41Sopenharmony_ci    lmask = (1U << state->lenbits) - 1;
1001cb0ef41Sopenharmony_ci    dmask = (1U << state->distbits) - 1;
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci    /* decode literals and length/distances until end-of-block or not enough
1031cb0ef41Sopenharmony_ci       input data or output space */
1041cb0ef41Sopenharmony_ci    do {
1051cb0ef41Sopenharmony_ci        if (bits < 15) {
1061cb0ef41Sopenharmony_ci            hold += (unsigned long)(*in++) << bits;
1071cb0ef41Sopenharmony_ci            bits += 8;
1081cb0ef41Sopenharmony_ci            hold += (unsigned long)(*in++) << bits;
1091cb0ef41Sopenharmony_ci            bits += 8;
1101cb0ef41Sopenharmony_ci        }
1111cb0ef41Sopenharmony_ci        here = lcode + (hold & lmask);
1121cb0ef41Sopenharmony_ci      dolen:
1131cb0ef41Sopenharmony_ci        op = (unsigned)(here->bits);
1141cb0ef41Sopenharmony_ci        hold >>= op;
1151cb0ef41Sopenharmony_ci        bits -= op;
1161cb0ef41Sopenharmony_ci        op = (unsigned)(here->op);
1171cb0ef41Sopenharmony_ci        if (op == 0) {                          /* literal */
1181cb0ef41Sopenharmony_ci            Tracevv((stderr, here->val >= 0x20 && here->val < 0x7f ?
1191cb0ef41Sopenharmony_ci                    "inflate:         literal '%c'\n" :
1201cb0ef41Sopenharmony_ci                    "inflate:         literal 0x%02x\n", here->val));
1211cb0ef41Sopenharmony_ci            *out++ = (unsigned char)(here->val);
1221cb0ef41Sopenharmony_ci        }
1231cb0ef41Sopenharmony_ci        else if (op & 16) {                     /* length base */
1241cb0ef41Sopenharmony_ci            len = (unsigned)(here->val);
1251cb0ef41Sopenharmony_ci            op &= 15;                           /* number of extra bits */
1261cb0ef41Sopenharmony_ci            if (op) {
1271cb0ef41Sopenharmony_ci                if (bits < op) {
1281cb0ef41Sopenharmony_ci                    hold += (unsigned long)(*in++) << bits;
1291cb0ef41Sopenharmony_ci                    bits += 8;
1301cb0ef41Sopenharmony_ci                }
1311cb0ef41Sopenharmony_ci                len += (unsigned)hold & ((1U << op) - 1);
1321cb0ef41Sopenharmony_ci                hold >>= op;
1331cb0ef41Sopenharmony_ci                bits -= op;
1341cb0ef41Sopenharmony_ci            }
1351cb0ef41Sopenharmony_ci            Tracevv((stderr, "inflate:         length %u\n", len));
1361cb0ef41Sopenharmony_ci            if (bits < 15) {
1371cb0ef41Sopenharmony_ci                hold += (unsigned long)(*in++) << bits;
1381cb0ef41Sopenharmony_ci                bits += 8;
1391cb0ef41Sopenharmony_ci                hold += (unsigned long)(*in++) << bits;
1401cb0ef41Sopenharmony_ci                bits += 8;
1411cb0ef41Sopenharmony_ci            }
1421cb0ef41Sopenharmony_ci            here = dcode + (hold & dmask);
1431cb0ef41Sopenharmony_ci          dodist:
1441cb0ef41Sopenharmony_ci            op = (unsigned)(here->bits);
1451cb0ef41Sopenharmony_ci            hold >>= op;
1461cb0ef41Sopenharmony_ci            bits -= op;
1471cb0ef41Sopenharmony_ci            op = (unsigned)(here->op);
1481cb0ef41Sopenharmony_ci            if (op & 16) {                      /* distance base */
1491cb0ef41Sopenharmony_ci                dist = (unsigned)(here->val);
1501cb0ef41Sopenharmony_ci                op &= 15;                       /* number of extra bits */
1511cb0ef41Sopenharmony_ci                if (bits < op) {
1521cb0ef41Sopenharmony_ci                    hold += (unsigned long)(*in++) << bits;
1531cb0ef41Sopenharmony_ci                    bits += 8;
1541cb0ef41Sopenharmony_ci                    if (bits < op) {
1551cb0ef41Sopenharmony_ci                        hold += (unsigned long)(*in++) << bits;
1561cb0ef41Sopenharmony_ci                        bits += 8;
1571cb0ef41Sopenharmony_ci                    }
1581cb0ef41Sopenharmony_ci                }
1591cb0ef41Sopenharmony_ci                dist += (unsigned)hold & ((1U << op) - 1);
1601cb0ef41Sopenharmony_ci#ifdef INFLATE_STRICT
1611cb0ef41Sopenharmony_ci                if (dist > dmax) {
1621cb0ef41Sopenharmony_ci                    strm->msg = (char *)"invalid distance too far back";
1631cb0ef41Sopenharmony_ci                    state->mode = BAD;
1641cb0ef41Sopenharmony_ci                    break;
1651cb0ef41Sopenharmony_ci                }
1661cb0ef41Sopenharmony_ci#endif
1671cb0ef41Sopenharmony_ci                hold >>= op;
1681cb0ef41Sopenharmony_ci                bits -= op;
1691cb0ef41Sopenharmony_ci                Tracevv((stderr, "inflate:         distance %u\n", dist));
1701cb0ef41Sopenharmony_ci                op = (unsigned)(out - beg);     /* max distance in output */
1711cb0ef41Sopenharmony_ci                if (dist > op) {                /* see if copy from window */
1721cb0ef41Sopenharmony_ci                    op = dist - op;             /* distance back in window */
1731cb0ef41Sopenharmony_ci                    if (op > whave) {
1741cb0ef41Sopenharmony_ci                        if (state->sane) {
1751cb0ef41Sopenharmony_ci                            strm->msg =
1761cb0ef41Sopenharmony_ci                                (char *)"invalid distance too far back";
1771cb0ef41Sopenharmony_ci                            state->mode = BAD;
1781cb0ef41Sopenharmony_ci                            break;
1791cb0ef41Sopenharmony_ci                        }
1801cb0ef41Sopenharmony_ci#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1811cb0ef41Sopenharmony_ci                        if (len <= op - whave) {
1821cb0ef41Sopenharmony_ci                            do {
1831cb0ef41Sopenharmony_ci                                *out++ = 0;
1841cb0ef41Sopenharmony_ci                            } while (--len);
1851cb0ef41Sopenharmony_ci                            continue;
1861cb0ef41Sopenharmony_ci                        }
1871cb0ef41Sopenharmony_ci                        len -= op - whave;
1881cb0ef41Sopenharmony_ci                        do {
1891cb0ef41Sopenharmony_ci                            *out++ = 0;
1901cb0ef41Sopenharmony_ci                        } while (--op > whave);
1911cb0ef41Sopenharmony_ci                        if (op == 0) {
1921cb0ef41Sopenharmony_ci                            from = out - dist;
1931cb0ef41Sopenharmony_ci                            do {
1941cb0ef41Sopenharmony_ci                                *out++ = *from++;
1951cb0ef41Sopenharmony_ci                            } while (--len);
1961cb0ef41Sopenharmony_ci                            continue;
1971cb0ef41Sopenharmony_ci                        }
1981cb0ef41Sopenharmony_ci#endif
1991cb0ef41Sopenharmony_ci                    }
2001cb0ef41Sopenharmony_ci                    from = window;
2011cb0ef41Sopenharmony_ci                    if (wnext == 0) {           /* very common case */
2021cb0ef41Sopenharmony_ci                        from += wsize - op;
2031cb0ef41Sopenharmony_ci                        if (op < len) {         /* some from window */
2041cb0ef41Sopenharmony_ci                            len -= op;
2051cb0ef41Sopenharmony_ci                            do {
2061cb0ef41Sopenharmony_ci                                *out++ = *from++;
2071cb0ef41Sopenharmony_ci                            } while (--op);
2081cb0ef41Sopenharmony_ci                            from = out - dist;  /* rest from output */
2091cb0ef41Sopenharmony_ci                        }
2101cb0ef41Sopenharmony_ci                    }
2111cb0ef41Sopenharmony_ci                    else if (wnext < op) {      /* wrap around window */
2121cb0ef41Sopenharmony_ci                        from += wsize + wnext - op;
2131cb0ef41Sopenharmony_ci                        op -= wnext;
2141cb0ef41Sopenharmony_ci                        if (op < len) {         /* some from end of window */
2151cb0ef41Sopenharmony_ci                            len -= op;
2161cb0ef41Sopenharmony_ci                            do {
2171cb0ef41Sopenharmony_ci                                *out++ = *from++;
2181cb0ef41Sopenharmony_ci                            } while (--op);
2191cb0ef41Sopenharmony_ci                            from = window;
2201cb0ef41Sopenharmony_ci                            if (wnext < len) {  /* some from start of window */
2211cb0ef41Sopenharmony_ci                                op = wnext;
2221cb0ef41Sopenharmony_ci                                len -= op;
2231cb0ef41Sopenharmony_ci                                do {
2241cb0ef41Sopenharmony_ci                                    *out++ = *from++;
2251cb0ef41Sopenharmony_ci                                } while (--op);
2261cb0ef41Sopenharmony_ci                                from = out - dist;      /* rest from output */
2271cb0ef41Sopenharmony_ci                            }
2281cb0ef41Sopenharmony_ci                        }
2291cb0ef41Sopenharmony_ci                    }
2301cb0ef41Sopenharmony_ci                    else {                      /* contiguous in window */
2311cb0ef41Sopenharmony_ci                        from += wnext - op;
2321cb0ef41Sopenharmony_ci                        if (op < len) {         /* some from window */
2331cb0ef41Sopenharmony_ci                            len -= op;
2341cb0ef41Sopenharmony_ci                            do {
2351cb0ef41Sopenharmony_ci                                *out++ = *from++;
2361cb0ef41Sopenharmony_ci                            } while (--op);
2371cb0ef41Sopenharmony_ci                            from = out - dist;  /* rest from output */
2381cb0ef41Sopenharmony_ci                        }
2391cb0ef41Sopenharmony_ci                    }
2401cb0ef41Sopenharmony_ci                    while (len > 2) {
2411cb0ef41Sopenharmony_ci                        *out++ = *from++;
2421cb0ef41Sopenharmony_ci                        *out++ = *from++;
2431cb0ef41Sopenharmony_ci                        *out++ = *from++;
2441cb0ef41Sopenharmony_ci                        len -= 3;
2451cb0ef41Sopenharmony_ci                    }
2461cb0ef41Sopenharmony_ci                    if (len) {
2471cb0ef41Sopenharmony_ci                        *out++ = *from++;
2481cb0ef41Sopenharmony_ci                        if (len > 1)
2491cb0ef41Sopenharmony_ci                            *out++ = *from++;
2501cb0ef41Sopenharmony_ci                    }
2511cb0ef41Sopenharmony_ci                }
2521cb0ef41Sopenharmony_ci                else {
2531cb0ef41Sopenharmony_ci                    from = out - dist;          /* copy direct from output */
2541cb0ef41Sopenharmony_ci                    do {                        /* minimum length is three */
2551cb0ef41Sopenharmony_ci                        *out++ = *from++;
2561cb0ef41Sopenharmony_ci                        *out++ = *from++;
2571cb0ef41Sopenharmony_ci                        *out++ = *from++;
2581cb0ef41Sopenharmony_ci                        len -= 3;
2591cb0ef41Sopenharmony_ci                    } while (len > 2);
2601cb0ef41Sopenharmony_ci                    if (len) {
2611cb0ef41Sopenharmony_ci                        *out++ = *from++;
2621cb0ef41Sopenharmony_ci                        if (len > 1)
2631cb0ef41Sopenharmony_ci                            *out++ = *from++;
2641cb0ef41Sopenharmony_ci                    }
2651cb0ef41Sopenharmony_ci                }
2661cb0ef41Sopenharmony_ci            }
2671cb0ef41Sopenharmony_ci            else if ((op & 64) == 0) {          /* 2nd level distance code */
2681cb0ef41Sopenharmony_ci                here = dcode + here->val + (hold & ((1U << op) - 1));
2691cb0ef41Sopenharmony_ci                goto dodist;
2701cb0ef41Sopenharmony_ci            }
2711cb0ef41Sopenharmony_ci            else {
2721cb0ef41Sopenharmony_ci                strm->msg = (char *)"invalid distance code";
2731cb0ef41Sopenharmony_ci                state->mode = BAD;
2741cb0ef41Sopenharmony_ci                break;
2751cb0ef41Sopenharmony_ci            }
2761cb0ef41Sopenharmony_ci        }
2771cb0ef41Sopenharmony_ci        else if ((op & 64) == 0) {              /* 2nd level length code */
2781cb0ef41Sopenharmony_ci            here = lcode + here->val + (hold & ((1U << op) - 1));
2791cb0ef41Sopenharmony_ci            goto dolen;
2801cb0ef41Sopenharmony_ci        }
2811cb0ef41Sopenharmony_ci        else if (op & 32) {                     /* end-of-block */
2821cb0ef41Sopenharmony_ci            Tracevv((stderr, "inflate:         end of block\n"));
2831cb0ef41Sopenharmony_ci            state->mode = TYPE;
2841cb0ef41Sopenharmony_ci            break;
2851cb0ef41Sopenharmony_ci        }
2861cb0ef41Sopenharmony_ci        else {
2871cb0ef41Sopenharmony_ci            strm->msg = (char *)"invalid literal/length code";
2881cb0ef41Sopenharmony_ci            state->mode = BAD;
2891cb0ef41Sopenharmony_ci            break;
2901cb0ef41Sopenharmony_ci        }
2911cb0ef41Sopenharmony_ci    } while (in < last && out < end);
2921cb0ef41Sopenharmony_ci
2931cb0ef41Sopenharmony_ci    /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
2941cb0ef41Sopenharmony_ci    len = bits >> 3;
2951cb0ef41Sopenharmony_ci    in -= len;
2961cb0ef41Sopenharmony_ci    bits -= len << 3;
2971cb0ef41Sopenharmony_ci    hold &= (1U << bits) - 1;
2981cb0ef41Sopenharmony_ci
2991cb0ef41Sopenharmony_ci    /* update state and return */
3001cb0ef41Sopenharmony_ci    strm->next_in = in;
3011cb0ef41Sopenharmony_ci    strm->next_out = out;
3021cb0ef41Sopenharmony_ci    strm->avail_in = (unsigned)(in < last ?
3031cb0ef41Sopenharmony_ci        (INFLATE_FAST_MIN_INPUT - 1) + (last - in) :
3041cb0ef41Sopenharmony_ci        (INFLATE_FAST_MIN_INPUT - 1) - (in - last));
3051cb0ef41Sopenharmony_ci    strm->avail_out = (unsigned)(out < end ?
3061cb0ef41Sopenharmony_ci        (INFLATE_FAST_MIN_OUTPUT - 1) + (end - out) :
3071cb0ef41Sopenharmony_ci        (INFLATE_FAST_MIN_OUTPUT - 1) - (out - end));
3081cb0ef41Sopenharmony_ci    state->hold = hold;
3091cb0ef41Sopenharmony_ci    state->bits = bits;
3101cb0ef41Sopenharmony_ci    return;
3111cb0ef41Sopenharmony_ci}
3121cb0ef41Sopenharmony_ci
3131cb0ef41Sopenharmony_ci/*
3141cb0ef41Sopenharmony_ci   inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
3151cb0ef41Sopenharmony_ci   - Using bit fields for code structure
3161cb0ef41Sopenharmony_ci   - Different op definition to avoid & for extra bits (do & for table bits)
3171cb0ef41Sopenharmony_ci   - Three separate decoding do-loops for direct, window, and wnext == 0
3181cb0ef41Sopenharmony_ci   - Special case for distance > 1 copies to do overlapped load and store copy
3191cb0ef41Sopenharmony_ci   - Explicit branch predictions (based on measured branch probabilities)
3201cb0ef41Sopenharmony_ci   - Deferring match copy and interspersed it with decoding subsequent codes
3211cb0ef41Sopenharmony_ci   - Swapping literal/length else
3221cb0ef41Sopenharmony_ci   - Swapping window/direct else
3231cb0ef41Sopenharmony_ci   - Larger unrolled copy loops (three is about right)
3241cb0ef41Sopenharmony_ci   - Moving len -= 3 statement into middle of loop
3251cb0ef41Sopenharmony_ci */
3261cb0ef41Sopenharmony_ci
3271cb0ef41Sopenharmony_ci#endif /* !ASMINF */
328