11cb0ef41Sopenharmony_ci/* inffast_chunk.c -- fast decoding
21cb0ef41Sopenharmony_ci * Copyright (C) 1995-2017 Mark Adler
31cb0ef41Sopenharmony_ci * Copyright 2023 The Chromium Authors
41cb0ef41Sopenharmony_ci * For conditions of distribution and use, see copyright notice in zlib.h
51cb0ef41Sopenharmony_ci */
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci#include "zutil.h"
81cb0ef41Sopenharmony_ci#include "inftrees.h"
91cb0ef41Sopenharmony_ci#include "inflate.h"
101cb0ef41Sopenharmony_ci#include "contrib/optimizations/inffast_chunk.h"
111cb0ef41Sopenharmony_ci#include "contrib/optimizations/chunkcopy.h"
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci#ifdef ASMINF
141cb0ef41Sopenharmony_ci#  pragma message("Assembler code may have bugs -- use at your own risk")
151cb0ef41Sopenharmony_ci#else
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci/*
181cb0ef41Sopenharmony_ci   Decode literal, length, and distance codes and write out the resulting
191cb0ef41Sopenharmony_ci   literal and match bytes until either not enough input or output is
201cb0ef41Sopenharmony_ci   available, an end-of-block is encountered, or a data error is encountered.
211cb0ef41Sopenharmony_ci   When large enough input and output buffers are supplied to inflate(), for
221cb0ef41Sopenharmony_ci   example, a 16K input buffer and a 64K output buffer, more than 95% of the
231cb0ef41Sopenharmony_ci   inflate() execution time is spent in this routine.
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci   Entry assumptions:
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci        state->mode == LEN
281cb0ef41Sopenharmony_ci        strm->avail_in >= INFLATE_FAST_MIN_INPUT (6 or 8 bytes + 7 bytes)
291cb0ef41Sopenharmony_ci        strm->avail_out >= INFLATE_FAST_MIN_OUTPUT (258 bytes + 2 bytes)
301cb0ef41Sopenharmony_ci        start >= strm->avail_out
311cb0ef41Sopenharmony_ci        state->bits < 8
321cb0ef41Sopenharmony_ci        (state->hold >> state->bits) == 0
331cb0ef41Sopenharmony_ci        strm->next_out[0..strm->avail_out] does not overlap with
341cb0ef41Sopenharmony_ci              strm->next_in[0..strm->avail_in]
351cb0ef41Sopenharmony_ci        strm->state->window is allocated with an additional
361cb0ef41Sopenharmony_ci              CHUNKCOPY_CHUNK_SIZE-1 bytes of padding beyond strm->state->wsize
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci   On return, state->mode is one of:
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci        LEN -- ran out of enough output space or enough available input
411cb0ef41Sopenharmony_ci        TYPE -- reached end of block code, inflate() to interpret next block
421cb0ef41Sopenharmony_ci        BAD -- error in block data
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci   Notes:
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci    INFLATE_FAST_MIN_INPUT: 6 or 8 bytes + 7 bytes
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci    - The maximum input bits used by a length/distance pair is 15 bits for the
491cb0ef41Sopenharmony_ci      length code, 5 bits for the length extra, 15 bits for the distance code,
501cb0ef41Sopenharmony_ci      and 13 bits for the distance extra.  This totals 48 bits, or six bytes.
511cb0ef41Sopenharmony_ci      Therefore if strm->avail_in >= 6, then there is enough input to avoid
521cb0ef41Sopenharmony_ci      checking for available input while decoding.
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci    - The wide input data reading option reads 64 input bits at a time. Thus,
551cb0ef41Sopenharmony_ci      if strm->avail_in >= 8, then there is enough input to avoid checking for
561cb0ef41Sopenharmony_ci      available input while decoding. Reading consumes the input with:
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci          hold |= read64le(in) << bits;
591cb0ef41Sopenharmony_ci          in += 6;
601cb0ef41Sopenharmony_ci          bits += 48;
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci      reporting 6 bytes of new input because |bits| is 0..15 (2 bytes rounded
631cb0ef41Sopenharmony_ci      up, worst case) and 6 bytes is enough to decode as noted above. At exit,
641cb0ef41Sopenharmony_ci      hold &= (1U << bits) - 1 drops excess input to keep the invariant:
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci          (state->hold >> state->bits) == 0
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci    INFLATE_FAST_MIN_OUTPUT: 258 bytes + 2 bytes for literals = 260 bytes
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci    - The maximum bytes that a single length/distance pair can output is 258
711cb0ef41Sopenharmony_ci      bytes, which is the maximum length that can be coded.  inflate_fast()
721cb0ef41Sopenharmony_ci      requires strm->avail_out >= 260 for each loop to avoid checking for
731cb0ef41Sopenharmony_ci      available output space while decoding.
741cb0ef41Sopenharmony_ci */
751cb0ef41Sopenharmony_civoid ZLIB_INTERNAL inflate_fast_chunk_(z_streamp strm, unsigned start) {
761cb0ef41Sopenharmony_ci    struct inflate_state FAR *state;
771cb0ef41Sopenharmony_ci    z_const unsigned char FAR *in;      /* local strm->next_in */
781cb0ef41Sopenharmony_ci    z_const unsigned char FAR *last;    /* have enough input while in < last */
791cb0ef41Sopenharmony_ci    unsigned char FAR *out;     /* local strm->next_out */
801cb0ef41Sopenharmony_ci    unsigned char FAR *beg;     /* inflate()'s initial strm->next_out */
811cb0ef41Sopenharmony_ci    unsigned char FAR *end;     /* while out < end, enough space available */
821cb0ef41Sopenharmony_ci    unsigned char FAR *limit;   /* safety limit for chunky copies */
831cb0ef41Sopenharmony_ci#ifdef INFLATE_STRICT
841cb0ef41Sopenharmony_ci    unsigned dmax;              /* maximum distance from zlib header */
851cb0ef41Sopenharmony_ci#endif
861cb0ef41Sopenharmony_ci    unsigned wsize;             /* window size or zero if not using window */
871cb0ef41Sopenharmony_ci    unsigned whave;             /* valid bytes in the window */
881cb0ef41Sopenharmony_ci    unsigned wnext;             /* window write index */
891cb0ef41Sopenharmony_ci    unsigned char FAR *window;  /* allocated sliding window, if wsize != 0 */
901cb0ef41Sopenharmony_ci    inflate_holder_t hold;      /* local strm->hold */
911cb0ef41Sopenharmony_ci    unsigned bits;              /* local strm->bits */
921cb0ef41Sopenharmony_ci    code const FAR *lcode;      /* local strm->lencode */
931cb0ef41Sopenharmony_ci    code const FAR *dcode;      /* local strm->distcode */
941cb0ef41Sopenharmony_ci    unsigned lmask;             /* mask for first level of length codes */
951cb0ef41Sopenharmony_ci    unsigned dmask;             /* mask for first level of distance codes */
961cb0ef41Sopenharmony_ci    code const *here;           /* retrieved table entry */
971cb0ef41Sopenharmony_ci    unsigned op;                /* code bits, operation, extra bits, or */
981cb0ef41Sopenharmony_ci                                /*  window position, window bytes to copy */
991cb0ef41Sopenharmony_ci    unsigned len;               /* match length, unused bytes */
1001cb0ef41Sopenharmony_ci    unsigned dist;              /* match distance */
1011cb0ef41Sopenharmony_ci    unsigned char FAR *from;    /* where to copy match from */
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_ci    /* copy state to local variables */
1041cb0ef41Sopenharmony_ci    state = (struct inflate_state FAR *)strm->state;
1051cb0ef41Sopenharmony_ci    in = strm->next_in;
1061cb0ef41Sopenharmony_ci    last = in + (strm->avail_in - (INFLATE_FAST_MIN_INPUT - 1));
1071cb0ef41Sopenharmony_ci    out = strm->next_out;
1081cb0ef41Sopenharmony_ci    beg = out - (start - strm->avail_out);
1091cb0ef41Sopenharmony_ci    end = out + (strm->avail_out - (INFLATE_FAST_MIN_OUTPUT - 1));
1101cb0ef41Sopenharmony_ci    limit = out + strm->avail_out;
1111cb0ef41Sopenharmony_ci#ifdef INFLATE_STRICT
1121cb0ef41Sopenharmony_ci    dmax = state->dmax;
1131cb0ef41Sopenharmony_ci#endif
1141cb0ef41Sopenharmony_ci    wsize = state->wsize;
1151cb0ef41Sopenharmony_ci    whave = state->whave;
1161cb0ef41Sopenharmony_ci    wnext = (state->wnext == 0 && whave >= wsize) ? wsize : state->wnext;
1171cb0ef41Sopenharmony_ci    window = state->window;
1181cb0ef41Sopenharmony_ci    hold = state->hold;
1191cb0ef41Sopenharmony_ci    bits = state->bits;
1201cb0ef41Sopenharmony_ci    lcode = state->lencode;
1211cb0ef41Sopenharmony_ci    dcode = state->distcode;
1221cb0ef41Sopenharmony_ci    lmask = (1U << state->lenbits) - 1;
1231cb0ef41Sopenharmony_ci    dmask = (1U << state->distbits) - 1;
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ci#ifdef INFLATE_CHUNK_READ_64LE
1261cb0ef41Sopenharmony_ci#define REFILL() do { \
1271cb0ef41Sopenharmony_ci        Assert(bits < 64, "### Too many bits in inflate_fast."); \
1281cb0ef41Sopenharmony_ci        hold |= read64le(in) << bits; \
1291cb0ef41Sopenharmony_ci        in += 7; \
1301cb0ef41Sopenharmony_ci        in -= bits >> 3; \
1311cb0ef41Sopenharmony_ci        bits |= 56; \
1321cb0ef41Sopenharmony_ci    } while (0)
1331cb0ef41Sopenharmony_ci#endif
1341cb0ef41Sopenharmony_ci
1351cb0ef41Sopenharmony_ci    /* decode literals and length/distances until end-of-block or not enough
1361cb0ef41Sopenharmony_ci       input data or output space */
1371cb0ef41Sopenharmony_ci    do {
1381cb0ef41Sopenharmony_ci#ifdef INFLATE_CHUNK_READ_64LE
1391cb0ef41Sopenharmony_ci        REFILL();
1401cb0ef41Sopenharmony_ci#else
1411cb0ef41Sopenharmony_ci        if (bits < 15) {
1421cb0ef41Sopenharmony_ci            hold += (unsigned long)(*in++) << bits;
1431cb0ef41Sopenharmony_ci            bits += 8;
1441cb0ef41Sopenharmony_ci            hold += (unsigned long)(*in++) << bits;
1451cb0ef41Sopenharmony_ci            bits += 8;
1461cb0ef41Sopenharmony_ci        }
1471cb0ef41Sopenharmony_ci#endif
1481cb0ef41Sopenharmony_ci        here = lcode + (hold & lmask);
1491cb0ef41Sopenharmony_ci#ifdef INFLATE_CHUNK_READ_64LE
1501cb0ef41Sopenharmony_ci        if (here->op == 0) {                    /* literal */
1511cb0ef41Sopenharmony_ci            Tracevv((stderr, here->val >= 0x20 && here->val < 0x7f ?
1521cb0ef41Sopenharmony_ci                    "inflate:         literal '%c'\n" :
1531cb0ef41Sopenharmony_ci                    "inflate:         literal 0x%02x\n", here->val));
1541cb0ef41Sopenharmony_ci            *out++ = (unsigned char)(here->val);
1551cb0ef41Sopenharmony_ci            hold >>= here->bits;
1561cb0ef41Sopenharmony_ci            bits -= here->bits;
1571cb0ef41Sopenharmony_ci            here = lcode + (hold & lmask);
1581cb0ef41Sopenharmony_ci            if (here->op == 0) {                /* literal */
1591cb0ef41Sopenharmony_ci                Tracevv((stderr, here->val >= 0x20 && here->val < 0x7f ?
1601cb0ef41Sopenharmony_ci                        "inflate:    2nd  literal '%c'\n" :
1611cb0ef41Sopenharmony_ci                        "inflate:    2nd  literal 0x%02x\n", here->val));
1621cb0ef41Sopenharmony_ci                *out++ = (unsigned char)(here->val);
1631cb0ef41Sopenharmony_ci                hold >>= here->bits;
1641cb0ef41Sopenharmony_ci                bits -= here->bits;
1651cb0ef41Sopenharmony_ci                here = lcode + (hold & lmask);
1661cb0ef41Sopenharmony_ci            }
1671cb0ef41Sopenharmony_ci        }
1681cb0ef41Sopenharmony_ci#endif
1691cb0ef41Sopenharmony_ci      dolen:
1701cb0ef41Sopenharmony_ci        op = (unsigned)(here->bits);
1711cb0ef41Sopenharmony_ci        hold >>= op;
1721cb0ef41Sopenharmony_ci        bits -= op;
1731cb0ef41Sopenharmony_ci        op = (unsigned)(here->op);
1741cb0ef41Sopenharmony_ci        if (op == 0) {                          /* literal */
1751cb0ef41Sopenharmony_ci            Tracevv((stderr, here->val >= 0x20 && here->val < 0x7f ?
1761cb0ef41Sopenharmony_ci                    "inflate:         literal '%c'\n" :
1771cb0ef41Sopenharmony_ci                    "inflate:         literal 0x%02x\n", here->val));
1781cb0ef41Sopenharmony_ci            *out++ = (unsigned char)(here->val);
1791cb0ef41Sopenharmony_ci        }
1801cb0ef41Sopenharmony_ci        else if (op & 16) {                     /* length base */
1811cb0ef41Sopenharmony_ci            len = (unsigned)(here->val);
1821cb0ef41Sopenharmony_ci            op &= 15;                           /* number of extra bits */
1831cb0ef41Sopenharmony_ci            if (op) {
1841cb0ef41Sopenharmony_ci#ifndef INFLATE_CHUNK_READ_64LE
1851cb0ef41Sopenharmony_ci                if (bits < op) {
1861cb0ef41Sopenharmony_ci                    hold += (unsigned long)(*in++) << bits;
1871cb0ef41Sopenharmony_ci                    bits += 8;
1881cb0ef41Sopenharmony_ci                }
1891cb0ef41Sopenharmony_ci#endif
1901cb0ef41Sopenharmony_ci                len += (unsigned)hold & ((1U << op) - 1);
1911cb0ef41Sopenharmony_ci                hold >>= op;
1921cb0ef41Sopenharmony_ci                bits -= op;
1931cb0ef41Sopenharmony_ci            }
1941cb0ef41Sopenharmony_ci            Tracevv((stderr, "inflate:         length %u\n", len));
1951cb0ef41Sopenharmony_ci#ifndef INFLATE_CHUNK_READ_64LE
1961cb0ef41Sopenharmony_ci            if (bits < 15) {
1971cb0ef41Sopenharmony_ci                hold += (unsigned long)(*in++) << bits;
1981cb0ef41Sopenharmony_ci                bits += 8;
1991cb0ef41Sopenharmony_ci                hold += (unsigned long)(*in++) << bits;
2001cb0ef41Sopenharmony_ci                bits += 8;
2011cb0ef41Sopenharmony_ci            }
2021cb0ef41Sopenharmony_ci#endif
2031cb0ef41Sopenharmony_ci            here = dcode + (hold & dmask);
2041cb0ef41Sopenharmony_ci          dodist:
2051cb0ef41Sopenharmony_ci            op = (unsigned)(here->bits);
2061cb0ef41Sopenharmony_ci            hold >>= op;
2071cb0ef41Sopenharmony_ci            bits -= op;
2081cb0ef41Sopenharmony_ci            op = (unsigned)(here->op);
2091cb0ef41Sopenharmony_ci            if (op & 16) {                      /* distance base */
2101cb0ef41Sopenharmony_ci                dist = (unsigned)(here->val);
2111cb0ef41Sopenharmony_ci                op &= 15;                       /* number of extra bits */
2121cb0ef41Sopenharmony_ci                /* we have two fast-path loads: 10+10 + 15+5 + 15 = 55,
2131cb0ef41Sopenharmony_ci                   but we may need to refill here in the worst case */
2141cb0ef41Sopenharmony_ci                if (bits < op) {
2151cb0ef41Sopenharmony_ci#ifdef INFLATE_CHUNK_READ_64LE
2161cb0ef41Sopenharmony_ci                    REFILL();
2171cb0ef41Sopenharmony_ci#else
2181cb0ef41Sopenharmony_ci                    hold += (unsigned long)(*in++) << bits;
2191cb0ef41Sopenharmony_ci                    bits += 8;
2201cb0ef41Sopenharmony_ci                    if (bits < op) {
2211cb0ef41Sopenharmony_ci                        hold += (unsigned long)(*in++) << bits;
2221cb0ef41Sopenharmony_ci                        bits += 8;
2231cb0ef41Sopenharmony_ci                    }
2241cb0ef41Sopenharmony_ci#endif
2251cb0ef41Sopenharmony_ci                }
2261cb0ef41Sopenharmony_ci                dist += (unsigned)hold & ((1U << op) - 1);
2271cb0ef41Sopenharmony_ci#ifdef INFLATE_STRICT
2281cb0ef41Sopenharmony_ci                if (dist > dmax) {
2291cb0ef41Sopenharmony_ci                    strm->msg = (char *)"invalid distance too far back";
2301cb0ef41Sopenharmony_ci                    state->mode = BAD;
2311cb0ef41Sopenharmony_ci                    break;
2321cb0ef41Sopenharmony_ci                }
2331cb0ef41Sopenharmony_ci#endif
2341cb0ef41Sopenharmony_ci                hold >>= op;
2351cb0ef41Sopenharmony_ci                bits -= op;
2361cb0ef41Sopenharmony_ci                Tracevv((stderr, "inflate:         distance %u\n", dist));
2371cb0ef41Sopenharmony_ci                op = (unsigned)(out - beg);     /* max distance in output */
2381cb0ef41Sopenharmony_ci                if (dist > op) {                /* see if copy from window */
2391cb0ef41Sopenharmony_ci                    op = dist - op;             /* distance back in window */
2401cb0ef41Sopenharmony_ci                    if (op > whave) {
2411cb0ef41Sopenharmony_ci                        if (state->sane) {
2421cb0ef41Sopenharmony_ci                            strm->msg =
2431cb0ef41Sopenharmony_ci                                (char *)"invalid distance too far back";
2441cb0ef41Sopenharmony_ci                            state->mode = BAD;
2451cb0ef41Sopenharmony_ci                            break;
2461cb0ef41Sopenharmony_ci                        }
2471cb0ef41Sopenharmony_ci#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
2481cb0ef41Sopenharmony_ci                        if (len <= op - whave) {
2491cb0ef41Sopenharmony_ci                            do {
2501cb0ef41Sopenharmony_ci                                *out++ = 0;
2511cb0ef41Sopenharmony_ci                            } while (--len);
2521cb0ef41Sopenharmony_ci                            continue;
2531cb0ef41Sopenharmony_ci                        }
2541cb0ef41Sopenharmony_ci                        len -= op - whave;
2551cb0ef41Sopenharmony_ci                        do {
2561cb0ef41Sopenharmony_ci                            *out++ = 0;
2571cb0ef41Sopenharmony_ci                        } while (--op > whave);
2581cb0ef41Sopenharmony_ci                        if (op == 0) {
2591cb0ef41Sopenharmony_ci                            from = out - dist;
2601cb0ef41Sopenharmony_ci                            do {
2611cb0ef41Sopenharmony_ci                                *out++ = *from++;
2621cb0ef41Sopenharmony_ci                            } while (--len);
2631cb0ef41Sopenharmony_ci                            continue;
2641cb0ef41Sopenharmony_ci                        }
2651cb0ef41Sopenharmony_ci#endif
2661cb0ef41Sopenharmony_ci                    }
2671cb0ef41Sopenharmony_ci                    from = window;
2681cb0ef41Sopenharmony_ci                    if (wnext >= op) {          /* contiguous in window */
2691cb0ef41Sopenharmony_ci                        from += wnext - op;
2701cb0ef41Sopenharmony_ci                    }
2711cb0ef41Sopenharmony_ci                    else {                      /* wrap around window */
2721cb0ef41Sopenharmony_ci                        op -= wnext;
2731cb0ef41Sopenharmony_ci                        from += wsize - op;
2741cb0ef41Sopenharmony_ci                        if (op < len) {         /* some from end of window */
2751cb0ef41Sopenharmony_ci                            len -= op;
2761cb0ef41Sopenharmony_ci                            out = chunkcopy_safe(out, from, op, limit);
2771cb0ef41Sopenharmony_ci                            from = window;      /* more from start of window */
2781cb0ef41Sopenharmony_ci                            op = wnext;
2791cb0ef41Sopenharmony_ci                            /* This (rare) case can create a situation where
2801cb0ef41Sopenharmony_ci                               the first chunkcopy below must be checked.
2811cb0ef41Sopenharmony_ci                             */
2821cb0ef41Sopenharmony_ci                        }
2831cb0ef41Sopenharmony_ci                    }
2841cb0ef41Sopenharmony_ci                    if (op < len) {             /* still need some from output */
2851cb0ef41Sopenharmony_ci                        out = chunkcopy_safe(out, from, op, limit);
2861cb0ef41Sopenharmony_ci                        len -= op;
2871cb0ef41Sopenharmony_ci                        /* When dist is small the amount of data that can be
2881cb0ef41Sopenharmony_ci                           copied from the window is also small, and progress
2891cb0ef41Sopenharmony_ci                           towards the dangerous end of the output buffer is
2901cb0ef41Sopenharmony_ci                           also small.  This means that for trivial memsets and
2911cb0ef41Sopenharmony_ci                           for chunkunroll_relaxed() a safety check is
2921cb0ef41Sopenharmony_ci                           unnecessary.  However, these conditions may not be
2931cb0ef41Sopenharmony_ci                           entered at all, and in that case it's possible that
2941cb0ef41Sopenharmony_ci                           the main copy is near the end.
2951cb0ef41Sopenharmony_ci                          */
2961cb0ef41Sopenharmony_ci                        out = chunkunroll_relaxed(out, &dist, &len);
2971cb0ef41Sopenharmony_ci                        out = chunkcopy_safe_ugly(out, dist, len, limit);
2981cb0ef41Sopenharmony_ci                    } else {
2991cb0ef41Sopenharmony_ci                        /* from points to window, so there is no risk of
3001cb0ef41Sopenharmony_ci                           overlapping pointers requiring memset-like behaviour
3011cb0ef41Sopenharmony_ci                         */
3021cb0ef41Sopenharmony_ci                        out = chunkcopy_safe(out, from, len, limit);
3031cb0ef41Sopenharmony_ci                    }
3041cb0ef41Sopenharmony_ci                }
3051cb0ef41Sopenharmony_ci                else {
3061cb0ef41Sopenharmony_ci                    /* Whole reference is in range of current output.  No
3071cb0ef41Sopenharmony_ci                       range checks are necessary because we start with room
3081cb0ef41Sopenharmony_ci                       for at least 258 bytes of output, so unroll and roundoff
3091cb0ef41Sopenharmony_ci                       operations can write beyond `out+len` so long as they
3101cb0ef41Sopenharmony_ci                       stay within 258 bytes of `out`.
3111cb0ef41Sopenharmony_ci                     */
3121cb0ef41Sopenharmony_ci                    out = chunkcopy_lapped_relaxed(out, dist, len);
3131cb0ef41Sopenharmony_ci                }
3141cb0ef41Sopenharmony_ci            }
3151cb0ef41Sopenharmony_ci            else if ((op & 64) == 0) {          /* 2nd level distance code */
3161cb0ef41Sopenharmony_ci                here = dcode + here->val + (hold & ((1U << op) - 1));
3171cb0ef41Sopenharmony_ci                goto dodist;
3181cb0ef41Sopenharmony_ci            }
3191cb0ef41Sopenharmony_ci            else {
3201cb0ef41Sopenharmony_ci                strm->msg = (char *)"invalid distance code";
3211cb0ef41Sopenharmony_ci                state->mode = BAD;
3221cb0ef41Sopenharmony_ci                break;
3231cb0ef41Sopenharmony_ci            }
3241cb0ef41Sopenharmony_ci        }
3251cb0ef41Sopenharmony_ci        else if ((op & 64) == 0) {              /* 2nd level length code */
3261cb0ef41Sopenharmony_ci            here = lcode + here->val + (hold & ((1U << op) - 1));
3271cb0ef41Sopenharmony_ci            goto dolen;
3281cb0ef41Sopenharmony_ci        }
3291cb0ef41Sopenharmony_ci        else if (op & 32) {                     /* end-of-block */
3301cb0ef41Sopenharmony_ci            Tracevv((stderr, "inflate:         end of block\n"));
3311cb0ef41Sopenharmony_ci            state->mode = TYPE;
3321cb0ef41Sopenharmony_ci            break;
3331cb0ef41Sopenharmony_ci        }
3341cb0ef41Sopenharmony_ci        else {
3351cb0ef41Sopenharmony_ci            strm->msg = (char *)"invalid literal/length code";
3361cb0ef41Sopenharmony_ci            state->mode = BAD;
3371cb0ef41Sopenharmony_ci            break;
3381cb0ef41Sopenharmony_ci        }
3391cb0ef41Sopenharmony_ci    } while (in < last && out < end);
3401cb0ef41Sopenharmony_ci
3411cb0ef41Sopenharmony_ci    /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
3421cb0ef41Sopenharmony_ci    len = bits >> 3;
3431cb0ef41Sopenharmony_ci    in -= len;
3441cb0ef41Sopenharmony_ci    bits -= len << 3;
3451cb0ef41Sopenharmony_ci    hold &= (1U << bits) - 1;
3461cb0ef41Sopenharmony_ci
3471cb0ef41Sopenharmony_ci    /* update state and return */
3481cb0ef41Sopenharmony_ci    strm->next_in = in;
3491cb0ef41Sopenharmony_ci    strm->next_out = out;
3501cb0ef41Sopenharmony_ci    strm->avail_in = (unsigned)(in < last ?
3511cb0ef41Sopenharmony_ci        (INFLATE_FAST_MIN_INPUT - 1) + (last - in) :
3521cb0ef41Sopenharmony_ci        (INFLATE_FAST_MIN_INPUT - 1) - (in - last));
3531cb0ef41Sopenharmony_ci    strm->avail_out = (unsigned)(out < end ?
3541cb0ef41Sopenharmony_ci        (INFLATE_FAST_MIN_OUTPUT - 1) + (end - out) :
3551cb0ef41Sopenharmony_ci        (INFLATE_FAST_MIN_OUTPUT - 1) - (out - end));
3561cb0ef41Sopenharmony_ci    state->hold = hold;
3571cb0ef41Sopenharmony_ci    state->bits = bits;
3581cb0ef41Sopenharmony_ci
3591cb0ef41Sopenharmony_ci    Assert((state->hold >> state->bits) == 0, "invalid input data state");
3601cb0ef41Sopenharmony_ci}
3611cb0ef41Sopenharmony_ci
3621cb0ef41Sopenharmony_ci/*
3631cb0ef41Sopenharmony_ci   inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
3641cb0ef41Sopenharmony_ci   - Using bit fields for code structure
3651cb0ef41Sopenharmony_ci   - Different op definition to avoid & for extra bits (do & for table bits)
3661cb0ef41Sopenharmony_ci   - Three separate decoding do-loops for direct, window, and wnext == 0
3671cb0ef41Sopenharmony_ci   - Special case for distance > 1 copies to do overlapped load and store copy
3681cb0ef41Sopenharmony_ci   - Explicit branch predictions (based on measured branch probabilities)
3691cb0ef41Sopenharmony_ci   - Deferring match copy and interspersed it with decoding subsequent codes
3701cb0ef41Sopenharmony_ci   - Swapping literal/length else
3711cb0ef41Sopenharmony_ci   - Swapping window/direct else
3721cb0ef41Sopenharmony_ci   - Larger unrolled copy loops (three is about right)
3731cb0ef41Sopenharmony_ci   - Moving len -= 3 statement into middle of loop
3741cb0ef41Sopenharmony_ci */
3751cb0ef41Sopenharmony_ci
3761cb0ef41Sopenharmony_ci#endif /* !ASMINF */
377