jdk/src/java.base/share/native/libzip/zlib/inffast.c
author sherman
Wed, 15 Feb 2017 12:11:03 -0800
changeset 43806 36e9a4583949
permissions -rw-r--r--
8173207: Upgrade compression library Reviewed-by: alanb, erikj
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43806
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
     1
/*
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
     3
 *
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
     6
 * published by the Free Software Foundation.  Oracle designates this
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
     8
 * by Oracle in the LICENSE file that accompanied this code.
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
     9
 *
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    14
 * accompanied this code).
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    15
 *
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    19
 *
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    22
 * questions.
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    23
 */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    24
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    25
/* inffast.c -- fast decoding
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    26
 * Copyright (C) 1995-2017 Mark Adler
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    27
 * For conditions of distribution and use, see copyright notice in zlib.h
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    28
 */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    29
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    30
#include "zutil.h"
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    31
#include "inftrees.h"
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    32
#include "inflate.h"
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    33
#include "inffast.h"
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    34
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    35
#ifdef ASMINF
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    36
#  pragma message("Assembler code may have bugs -- use at your own risk")
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    37
#else
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    38
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    39
/*
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    40
   Decode literal, length, and distance codes and write out the resulting
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    41
   literal and match bytes until either not enough input or output is
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    42
   available, an end-of-block is encountered, or a data error is encountered.
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    43
   When large enough input and output buffers are supplied to inflate(), for
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    44
   example, a 16K input buffer and a 64K output buffer, more than 95% of the
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    45
   inflate execution time is spent in this routine.
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    46
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    47
   Entry assumptions:
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    48
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    49
        state->mode == LEN
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    50
        strm->avail_in >= 6
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    51
        strm->avail_out >= 258
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    52
        start >= strm->avail_out
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    53
        state->bits < 8
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    54
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    55
   On return, state->mode is one of:
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    56
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    57
        LEN -- ran out of enough output space or enough available input
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    58
        TYPE -- reached end of block code, inflate() to interpret next block
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    59
        BAD -- error in block data
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    60
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    61
   Notes:
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    62
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    63
    - The maximum input bits used by a length/distance pair is 15 bits for the
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    64
      length code, 5 bits for the length extra, 15 bits for the distance code,
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    65
      and 13 bits for the distance extra.  This totals 48 bits, or six bytes.
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    66
      Therefore if strm->avail_in >= 6, then there is enough input to avoid
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    67
      checking for available input while decoding.
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    68
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    69
    - The maximum bytes that a single length/distance pair can output is 258
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    70
      bytes, which is the maximum length that can be coded.  inflate_fast()
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    71
      requires strm->avail_out >= 258 for each loop to avoid checking for
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    72
      output space.
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    73
 */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    74
void ZLIB_INTERNAL inflate_fast(strm, start)
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    75
z_streamp strm;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    76
unsigned start;         /* inflate()'s starting value for strm->avail_out */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    77
{
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    78
    struct inflate_state FAR *state;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    79
    z_const unsigned char FAR *in;      /* local strm->next_in */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    80
    z_const unsigned char FAR *last;    /* have enough input while in < last */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    81
    unsigned char FAR *out;     /* local strm->next_out */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    82
    unsigned char FAR *beg;     /* inflate()'s initial strm->next_out */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    83
    unsigned char FAR *end;     /* while out < end, enough space available */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    84
#ifdef INFLATE_STRICT
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    85
    unsigned dmax;              /* maximum distance from zlib header */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    86
#endif
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    87
    unsigned wsize;             /* window size or zero if not using window */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    88
    unsigned whave;             /* valid bytes in the window */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    89
    unsigned wnext;             /* window write index */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    90
    unsigned char FAR *window;  /* allocated sliding window, if wsize != 0 */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    91
    unsigned long hold;         /* local strm->hold */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    92
    unsigned bits;              /* local strm->bits */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    93
    code const FAR *lcode;      /* local strm->lencode */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    94
    code const FAR *dcode;      /* local strm->distcode */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    95
    unsigned lmask;             /* mask for first level of length codes */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    96
    unsigned dmask;             /* mask for first level of distance codes */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    97
    code here;                  /* retrieved table entry */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    98
    unsigned op;                /* code bits, operation, extra bits, or */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
    99
                                /*  window position, window bytes to copy */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   100
    unsigned len;               /* match length, unused bytes */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   101
    unsigned dist;              /* match distance */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   102
    unsigned char FAR *from;    /* where to copy match from */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   103
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   104
    /* copy state to local variables */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   105
    state = (struct inflate_state FAR *)strm->state;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   106
    in = strm->next_in;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   107
    last = in + (strm->avail_in - 5);
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   108
    out = strm->next_out;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   109
    beg = out - (start - strm->avail_out);
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   110
    end = out + (strm->avail_out - 257);
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   111
#ifdef INFLATE_STRICT
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   112
    dmax = state->dmax;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   113
#endif
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   114
    wsize = state->wsize;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   115
    whave = state->whave;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   116
    wnext = state->wnext;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   117
    window = state->window;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   118
    hold = state->hold;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   119
    bits = state->bits;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   120
    lcode = state->lencode;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   121
    dcode = state->distcode;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   122
    lmask = (1U << state->lenbits) - 1;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   123
    dmask = (1U << state->distbits) - 1;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   124
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   125
    /* decode literals and length/distances until end-of-block or not enough
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   126
       input data or output space */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   127
    do {
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   128
        if (bits < 15) {
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   129
            hold += (unsigned long)(*in++) << bits;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   130
            bits += 8;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   131
            hold += (unsigned long)(*in++) << bits;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   132
            bits += 8;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   133
        }
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   134
        here = lcode[hold & lmask];
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   135
      dolen:
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   136
        op = (unsigned)(here.bits);
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   137
        hold >>= op;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   138
        bits -= op;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   139
        op = (unsigned)(here.op);
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   140
        if (op == 0) {                          /* literal */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   141
            Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   142
                    "inflate:         literal '%c'\n" :
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   143
                    "inflate:         literal 0x%02x\n", here.val));
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   144
            *out++ = (unsigned char)(here.val);
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   145
        }
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   146
        else if (op & 16) {                     /* length base */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   147
            len = (unsigned)(here.val);
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   148
            op &= 15;                           /* number of extra bits */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   149
            if (op) {
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   150
                if (bits < op) {
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   151
                    hold += (unsigned long)(*in++) << bits;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   152
                    bits += 8;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   153
                }
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   154
                len += (unsigned)hold & ((1U << op) - 1);
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   155
                hold >>= op;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   156
                bits -= op;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   157
            }
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   158
            Tracevv((stderr, "inflate:         length %u\n", len));
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   159
            if (bits < 15) {
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   160
                hold += (unsigned long)(*in++) << bits;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   161
                bits += 8;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   162
                hold += (unsigned long)(*in++) << bits;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   163
                bits += 8;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   164
            }
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   165
            here = dcode[hold & dmask];
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   166
          dodist:
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   167
            op = (unsigned)(here.bits);
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   168
            hold >>= op;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   169
            bits -= op;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   170
            op = (unsigned)(here.op);
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   171
            if (op & 16) {                      /* distance base */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   172
                dist = (unsigned)(here.val);
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   173
                op &= 15;                       /* number of extra bits */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   174
                if (bits < op) {
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   175
                    hold += (unsigned long)(*in++) << bits;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   176
                    bits += 8;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   177
                    if (bits < op) {
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   178
                        hold += (unsigned long)(*in++) << bits;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   179
                        bits += 8;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   180
                    }
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   181
                }
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   182
                dist += (unsigned)hold & ((1U << op) - 1);
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   183
#ifdef INFLATE_STRICT
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   184
                if (dist > dmax) {
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   185
                    strm->msg = (char *)"invalid distance too far back";
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   186
                    state->mode = BAD;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   187
                    break;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   188
                }
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   189
#endif
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   190
                hold >>= op;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   191
                bits -= op;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   192
                Tracevv((stderr, "inflate:         distance %u\n", dist));
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   193
                op = (unsigned)(out - beg);     /* max distance in output */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   194
                if (dist > op) {                /* see if copy from window */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   195
                    op = dist - op;             /* distance back in window */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   196
                    if (op > whave) {
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   197
                        if (state->sane) {
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   198
                            strm->msg =
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   199
                                (char *)"invalid distance too far back";
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   200
                            state->mode = BAD;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   201
                            break;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   202
                        }
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   203
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   204
                        if (len <= op - whave) {
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   205
                            do {
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   206
                                *out++ = 0;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   207
                            } while (--len);
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   208
                            continue;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   209
                        }
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   210
                        len -= op - whave;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   211
                        do {
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   212
                            *out++ = 0;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   213
                        } while (--op > whave);
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   214
                        if (op == 0) {
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   215
                            from = out - dist;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   216
                            do {
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   217
                                *out++ = *from++;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   218
                            } while (--len);
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   219
                            continue;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   220
                        }
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   221
#endif
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   222
                    }
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   223
                    from = window;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   224
                    if (wnext == 0) {           /* very common case */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   225
                        from += wsize - op;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   226
                        if (op < len) {         /* some from window */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   227
                            len -= op;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   228
                            do {
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   229
                                *out++ = *from++;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   230
                            } while (--op);
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   231
                            from = out - dist;  /* rest from output */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   232
                        }
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   233
                    }
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   234
                    else if (wnext < op) {      /* wrap around window */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   235
                        from += wsize + wnext - op;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   236
                        op -= wnext;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   237
                        if (op < len) {         /* some from end of window */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   238
                            len -= op;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   239
                            do {
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   240
                                *out++ = *from++;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   241
                            } while (--op);
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   242
                            from = window;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   243
                            if (wnext < len) {  /* some from start of window */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   244
                                op = wnext;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   245
                                len -= op;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   246
                                do {
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   247
                                    *out++ = *from++;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   248
                                } while (--op);
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   249
                                from = out - dist;      /* rest from output */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   250
                            }
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   251
                        }
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   252
                    }
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   253
                    else {                      /* contiguous in window */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   254
                        from += wnext - op;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   255
                        if (op < len) {         /* some from window */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   256
                            len -= op;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   257
                            do {
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   258
                                *out++ = *from++;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   259
                            } while (--op);
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   260
                            from = out - dist;  /* rest from output */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   261
                        }
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   262
                    }
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   263
                    while (len > 2) {
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   264
                        *out++ = *from++;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   265
                        *out++ = *from++;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   266
                        *out++ = *from++;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   267
                        len -= 3;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   268
                    }
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   269
                    if (len) {
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   270
                        *out++ = *from++;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   271
                        if (len > 1)
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   272
                            *out++ = *from++;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   273
                    }
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   274
                }
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   275
                else {
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   276
                    from = out - dist;          /* copy direct from output */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   277
                    do {                        /* minimum length is three */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   278
                        *out++ = *from++;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   279
                        *out++ = *from++;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   280
                        *out++ = *from++;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   281
                        len -= 3;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   282
                    } while (len > 2);
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   283
                    if (len) {
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   284
                        *out++ = *from++;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   285
                        if (len > 1)
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   286
                            *out++ = *from++;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   287
                    }
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   288
                }
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   289
            }
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   290
            else if ((op & 64) == 0) {          /* 2nd level distance code */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   291
                here = dcode[here.val + (hold & ((1U << op) - 1))];
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   292
                goto dodist;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   293
            }
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   294
            else {
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   295
                strm->msg = (char *)"invalid distance code";
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   296
                state->mode = BAD;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   297
                break;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   298
            }
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   299
        }
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   300
        else if ((op & 64) == 0) {              /* 2nd level length code */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   301
            here = lcode[here.val + (hold & ((1U << op) - 1))];
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   302
            goto dolen;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   303
        }
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   304
        else if (op & 32) {                     /* end-of-block */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   305
            Tracevv((stderr, "inflate:         end of block\n"));
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   306
            state->mode = TYPE;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   307
            break;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   308
        }
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   309
        else {
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   310
            strm->msg = (char *)"invalid literal/length code";
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   311
            state->mode = BAD;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   312
            break;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   313
        }
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   314
    } while (in < last && out < end);
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   315
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   316
    /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   317
    len = bits >> 3;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   318
    in -= len;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   319
    bits -= len << 3;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   320
    hold &= (1U << bits) - 1;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   321
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   322
    /* update state and return */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   323
    strm->next_in = in;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   324
    strm->next_out = out;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   325
    strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   326
    strm->avail_out = (unsigned)(out < end ?
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   327
                                 257 + (end - out) : 257 - (out - end));
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   328
    state->hold = hold;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   329
    state->bits = bits;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   330
    return;
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   331
}
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   332
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   333
/*
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   334
   inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   335
   - Using bit fields for code structure
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   336
   - Different op definition to avoid & for extra bits (do & for table bits)
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   337
   - Three separate decoding do-loops for direct, window, and wnext == 0
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   338
   - Special case for distance > 1 copies to do overlapped load and store copy
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   339
   - Explicit branch predictions (based on measured branch probabilities)
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   340
   - Deferring match copy and interspersed it with decoding subsequent codes
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   341
   - Swapping literal/length else
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   342
   - Swapping window/direct else
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   343
   - Larger unrolled copy loops (three is about right)
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   344
   - Moving len -= 3 statement into middle of loop
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   345
 */
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   346
36e9a4583949 8173207: Upgrade compression library
sherman
parents:
diff changeset
   347
#endif /* !ASMINF */