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