srcanamdw/codescanner/pyinstaller/source/zlib/inffast.c
author noe\swadi
Thu, 18 Feb 2010 12:29:02 +0530
changeset 1 22878952f6e2
permissions -rw-r--r--
Committing the CodeScanner Core tool This component has been moved from the StaticAnaApps package. BUG : 5889 (http://developer.symbian.org/webbugs/show_bug.cgi?id=5889).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     1
/* inffast.c -- fast decoding
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     2
 * Copyright (C) 1995-2004 Mark Adler
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     3
 * For conditions of distribution and use, see copyright notice in zlib.h
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     4
 */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     5
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     6
#include "zutil.h"
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     7
#include "inftrees.h"
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     8
#include "inflate.h"
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     9
#include "inffast.h"
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    10
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    11
#ifndef ASMINF
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    12
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    13
/* Allow machine dependent optimization for post-increment or pre-increment.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    14
   Based on testing to date,
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    15
   Pre-increment preferred for:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    16
   - PowerPC G3 (Adler)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    17
   - MIPS R5000 (Randers-Pehrson)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    18
   Post-increment preferred for:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    19
   - none
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    20
   No measurable difference:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    21
   - Pentium III (Anderson)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    22
   - M68060 (Nikl)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    23
 */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    24
#ifdef POSTINC
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    25
#  define OFF 0
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    26
#  define PUP(a) *(a)++
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    27
#else
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    28
#  define OFF 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    29
#  define PUP(a) *++(a)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    30
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    31
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    32
/*
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    33
   Decode literal, length, and distance codes and write out the resulting
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    34
   literal and match bytes until either not enough input or output is
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    35
   available, an end-of-block is encountered, or a data error is encountered.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    36
   When large enough input and output buffers are supplied to inflate(), for
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    37
   example, a 16K input buffer and a 64K output buffer, more than 95% of the
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    38
   inflate execution time is spent in this routine.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    39
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    40
   Entry assumptions:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    41
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    42
        state->mode == LEN
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    43
        strm->avail_in >= 6
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    44
        strm->avail_out >= 258
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    45
        start >= strm->avail_out
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    46
        state->bits < 8
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    47
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    48
   On return, state->mode is one of:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    49
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    50
        LEN -- ran out of enough output space or enough available input
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    51
        TYPE -- reached end of block code, inflate() to interpret next block
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    52
        BAD -- error in block data
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    53
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    54
   Notes:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    55
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    56
    - The maximum input bits used by a length/distance pair is 15 bits for the
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    57
      length code, 5 bits for the length extra, 15 bits for the distance code,
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    58
      and 13 bits for the distance extra.  This totals 48 bits, or six bytes.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    59
      Therefore if strm->avail_in >= 6, then there is enough input to avoid
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    60
      checking for available input while decoding.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    61
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    62
    - The maximum bytes that a single length/distance pair can output is 258
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    63
      bytes, which is the maximum length that can be coded.  inflate_fast()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    64
      requires strm->avail_out >= 258 for each loop to avoid checking for
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    65
      output space.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    66
 */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    67
void inflate_fast(strm, start)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    68
z_streamp strm;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    69
unsigned start;         /* inflate()'s starting value for strm->avail_out */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    70
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    71
    struct inflate_state FAR *state;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    72
    unsigned char FAR *in;      /* local strm->next_in */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    73
    unsigned char FAR *last;    /* while in < last, enough input available */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    74
    unsigned char FAR *out;     /* local strm->next_out */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    75
    unsigned char FAR *beg;     /* inflate()'s initial strm->next_out */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    76
    unsigned char FAR *end;     /* while out < end, enough space available */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    77
#ifdef INFLATE_STRICT
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    78
    unsigned dmax;              /* maximum distance from zlib header */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    79
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    80
    unsigned wsize;             /* window size or zero if not using window */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    81
    unsigned whave;             /* valid bytes in the window */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    82
    unsigned write;             /* window write index */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    83
    unsigned char FAR *window;  /* allocated sliding window, if wsize != 0 */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    84
    unsigned long hold;         /* local strm->hold */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    85
    unsigned bits;              /* local strm->bits */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    86
    code const FAR *lcode;      /* local strm->lencode */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    87
    code const FAR *dcode;      /* local strm->distcode */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    88
    unsigned lmask;             /* mask for first level of length codes */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    89
    unsigned dmask;             /* mask for first level of distance codes */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    90
    code this;                  /* retrieved table entry */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    91
    unsigned op;                /* code bits, operation, extra bits, or */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    92
                                /*  window position, window bytes to copy */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    93
    unsigned len;               /* match length, unused bytes */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    94
    unsigned dist;              /* match distance */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    95
    unsigned char FAR *from;    /* where to copy match from */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    96
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    97
    /* copy state to local variables */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    98
    state = (struct inflate_state FAR *)strm->state;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    99
    in = strm->next_in - OFF;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   100
    last = in + (strm->avail_in - 5);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   101
    out = strm->next_out - OFF;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   102
    beg = out - (start - strm->avail_out);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   103
    end = out + (strm->avail_out - 257);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   104
#ifdef INFLATE_STRICT
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   105
    dmax = state->dmax;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   106
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   107
    wsize = state->wsize;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   108
    whave = state->whave;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   109
    write = state->write;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   110
    window = state->window;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   111
    hold = state->hold;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   112
    bits = state->bits;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   113
    lcode = state->lencode;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   114
    dcode = state->distcode;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   115
    lmask = (1U << state->lenbits) - 1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   116
    dmask = (1U << state->distbits) - 1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   117
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   118
    /* decode literals and length/distances until end-of-block or not enough
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   119
       input data or output space */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   120
    do {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   121
        if (bits < 15) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   122
            hold += (unsigned long)(PUP(in)) << bits;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   123
            bits += 8;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   124
            hold += (unsigned long)(PUP(in)) << bits;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   125
            bits += 8;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   126
        }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   127
        this = lcode[hold & lmask];
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   128
      dolen:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   129
        op = (unsigned)(this.bits);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   130
        hold >>= op;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   131
        bits -= op;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   132
        op = (unsigned)(this.op);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   133
        if (op == 0) {                          /* literal */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   134
            Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   135
                    "inflate:         literal '%c'\n" :
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   136
                    "inflate:         literal 0x%02x\n", this.val));
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   137
            PUP(out) = (unsigned char)(this.val);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   138
        }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   139
        else if (op & 16) {                     /* length base */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   140
            len = (unsigned)(this.val);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   141
            op &= 15;                           /* number of extra bits */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   142
            if (op) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   143
                if (bits < op) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   144
                    hold += (unsigned long)(PUP(in)) << bits;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   145
                    bits += 8;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   146
                }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   147
                len += (unsigned)hold & ((1U << op) - 1);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   148
                hold >>= op;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   149
                bits -= op;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   150
            }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   151
            Tracevv((stderr, "inflate:         length %u\n", len));
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   152
            if (bits < 15) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   153
                hold += (unsigned long)(PUP(in)) << bits;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   154
                bits += 8;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   155
                hold += (unsigned long)(PUP(in)) << bits;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   156
                bits += 8;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   157
            }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   158
            this = dcode[hold & dmask];
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   159
          dodist:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   160
            op = (unsigned)(this.bits);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   161
            hold >>= op;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   162
            bits -= op;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   163
            op = (unsigned)(this.op);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   164
            if (op & 16) {                      /* distance base */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   165
                dist = (unsigned)(this.val);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   166
                op &= 15;                       /* number of extra bits */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   167
                if (bits < op) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   168
                    hold += (unsigned long)(PUP(in)) << bits;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   169
                    bits += 8;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   170
                    if (bits < op) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   171
                        hold += (unsigned long)(PUP(in)) << bits;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   172
                        bits += 8;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   173
                    }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   174
                }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   175
                dist += (unsigned)hold & ((1U << op) - 1);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   176
#ifdef INFLATE_STRICT
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   177
                if (dist > dmax) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   178
                    strm->msg = (char *)"invalid distance too far back";
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   179
                    state->mode = BAD;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   180
                    break;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   181
                }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   182
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   183
                hold >>= op;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   184
                bits -= op;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   185
                Tracevv((stderr, "inflate:         distance %u\n", dist));
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   186
                op = (unsigned)(out - beg);     /* max distance in output */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   187
                if (dist > op) {                /* see if copy from window */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   188
                    op = dist - op;             /* distance back in window */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   189
                    if (op > whave) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   190
                        strm->msg = (char *)"invalid distance too far back";
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   191
                        state->mode = BAD;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   192
                        break;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   193
                    }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   194
                    from = window - OFF;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   195
                    if (write == 0) {           /* very common case */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   196
                        from += wsize - op;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   197
                        if (op < len) {         /* some from window */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   198
                            len -= op;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   199
                            do {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   200
                                PUP(out) = PUP(from);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   201
                            } while (--op);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   202
                            from = out - dist;  /* rest from output */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   203
                        }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   204
                    }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   205
                    else if (write < op) {      /* wrap around window */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   206
                        from += wsize + write - op;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   207
                        op -= write;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   208
                        if (op < len) {         /* some from end of window */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   209
                            len -= op;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   210
                            do {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   211
                                PUP(out) = PUP(from);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   212
                            } while (--op);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   213
                            from = window - OFF;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   214
                            if (write < len) {  /* some from start of window */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   215
                                op = write;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   216
                                len -= op;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   217
                                do {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   218
                                    PUP(out) = PUP(from);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   219
                                } while (--op);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   220
                                from = out - dist;      /* rest from output */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   221
                            }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   222
                        }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   223
                    }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   224
                    else {                      /* contiguous in window */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   225
                        from += write - op;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   226
                        if (op < len) {         /* some from window */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   227
                            len -= op;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   228
                            do {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   229
                                PUP(out) = PUP(from);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   230
                            } while (--op);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   231
                            from = out - dist;  /* rest from output */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   232
                        }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   233
                    }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   234
                    while (len > 2) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   235
                        PUP(out) = PUP(from);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   236
                        PUP(out) = PUP(from);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   237
                        PUP(out) = PUP(from);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   238
                        len -= 3;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   239
                    }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   240
                    if (len) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   241
                        PUP(out) = PUP(from);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   242
                        if (len > 1)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   243
                            PUP(out) = PUP(from);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   244
                    }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   245
                }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   246
                else {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   247
                    from = out - dist;          /* copy direct from output */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   248
                    do {                        /* minimum length is three */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   249
                        PUP(out) = PUP(from);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   250
                        PUP(out) = PUP(from);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   251
                        PUP(out) = PUP(from);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   252
                        len -= 3;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   253
                    } while (len > 2);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   254
                    if (len) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   255
                        PUP(out) = PUP(from);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   256
                        if (len > 1)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   257
                            PUP(out) = PUP(from);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   258
                    }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   259
                }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   260
            }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   261
            else if ((op & 64) == 0) {          /* 2nd level distance code */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   262
                this = dcode[this.val + (hold & ((1U << op) - 1))];
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   263
                goto dodist;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   264
            }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   265
            else {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   266
                strm->msg = (char *)"invalid distance code";
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   267
                state->mode = BAD;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   268
                break;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   269
            }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   270
        }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   271
        else if ((op & 64) == 0) {              /* 2nd level length code */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   272
            this = lcode[this.val + (hold & ((1U << op) - 1))];
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   273
            goto dolen;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   274
        }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   275
        else if (op & 32) {                     /* end-of-block */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   276
            Tracevv((stderr, "inflate:         end of block\n"));
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   277
            state->mode = TYPE;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   278
            break;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   279
        }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   280
        else {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   281
            strm->msg = (char *)"invalid literal/length code";
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   282
            state->mode = BAD;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   283
            break;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   284
        }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   285
    } while (in < last && out < end);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   286
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   287
    /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   288
    len = bits >> 3;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   289
    in -= len;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   290
    bits -= len << 3;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   291
    hold &= (1U << bits) - 1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   292
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   293
    /* update state and return */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   294
    strm->next_in = in + OFF;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   295
    strm->next_out = out + OFF;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   296
    strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   297
    strm->avail_out = (unsigned)(out < end ?
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   298
                                 257 + (end - out) : 257 - (out - end));
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   299
    state->hold = hold;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   300
    state->bits = bits;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   301
    return;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   302
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   303
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   304
/*
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   305
   inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   306
   - Using bit fields for code structure
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   307
   - Different op definition to avoid & for extra bits (do & for table bits)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   308
   - Three separate decoding do-loops for direct, window, and write == 0
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   309
   - Special case for distance > 1 copies to do overlapped load and store copy
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   310
   - Explicit branch predictions (based on measured branch probabilities)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   311
   - Deferring match copy and interspersed it with decoding subsequent codes
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   312
   - Swapping literal/length else
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   313
   - Swapping window/direct else
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   314
   - Larger unrolled copy loops (three is about right)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   315
   - Moving len -= 3 statement into middle of loop
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   316
 */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   317
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   318
#endif /* !ASMINF */