jdk/src/share/native/java/util/zip/zlib-1.2.5/inffast.c
changeset 11112 6d340c7b6a32
equal deleted inserted replaced
11111:6d84419a893a 11112:6d340c7b6a32
       
     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-2008, 2010 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 ZLIB_INTERNAL 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 wnext;             /* 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 here;                  /* 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     wnext = state->wnext;
       
   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         here = lcode[hold & lmask];
       
   152       dolen:
       
   153         op = (unsigned)(here.bits);
       
   154         hold >>= op;
       
   155         bits -= op;
       
   156         op = (unsigned)(here.op);
       
   157         if (op == 0) {                          /* literal */
       
   158             Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
       
   159                     "inflate:         literal '%c'\n" :
       
   160                     "inflate:         literal 0x%02x\n", here.val));
       
   161             PUP(out) = (unsigned char)(here.val);
       
   162         }
       
   163         else if (op & 16) {                     /* length base */
       
   164             len = (unsigned)(here.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             here = dcode[hold & dmask];
       
   183           dodist:
       
   184             op = (unsigned)(here.bits);
       
   185             hold >>= op;
       
   186             bits -= op;
       
   187             op = (unsigned)(here.op);
       
   188             if (op & 16) {                      /* distance base */
       
   189                 dist = (unsigned)(here.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                         if (state->sane) {
       
   215                             strm->msg =
       
   216                                 (char *)"invalid distance too far back";
       
   217                             state->mode = BAD;
       
   218                             break;
       
   219                         }
       
   220 #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
       
   221                         if (len <= op - whave) {
       
   222                             do {
       
   223                                 PUP(out) = 0;
       
   224                             } while (--len);
       
   225                             continue;
       
   226                         }
       
   227                         len -= op - whave;
       
   228                         do {
       
   229                             PUP(out) = 0;
       
   230                         } while (--op > whave);
       
   231                         if (op == 0) {
       
   232                             from = out - dist;
       
   233                             do {
       
   234                                 PUP(out) = PUP(from);
       
   235                             } while (--len);
       
   236                             continue;
       
   237                         }
       
   238 #endif
       
   239                     }
       
   240                     from = window - OFF;
       
   241                     if (wnext == 0) {           /* very common case */
       
   242                         from += wsize - op;
       
   243                         if (op < len) {         /* some from window */
       
   244                             len -= op;
       
   245                             do {
       
   246                                 PUP(out) = PUP(from);
       
   247                             } while (--op);
       
   248                             from = out - dist;  /* rest from output */
       
   249                         }
       
   250                     }
       
   251                     else if (wnext < op) {      /* wrap around window */
       
   252                         from += wsize + wnext - op;
       
   253                         op -= wnext;
       
   254                         if (op < len) {         /* some from end of window */
       
   255                             len -= op;
       
   256                             do {
       
   257                                 PUP(out) = PUP(from);
       
   258                             } while (--op);
       
   259                             from = window - OFF;
       
   260                             if (wnext < len) {  /* some from start of window */
       
   261                                 op = wnext;
       
   262                                 len -= op;
       
   263                                 do {
       
   264                                     PUP(out) = PUP(from);
       
   265                                 } while (--op);
       
   266                                 from = out - dist;      /* rest from output */
       
   267                             }
       
   268                         }
       
   269                     }
       
   270                     else {                      /* contiguous in window */
       
   271                         from += wnext - op;
       
   272                         if (op < len) {         /* some from window */
       
   273                             len -= op;
       
   274                             do {
       
   275                                 PUP(out) = PUP(from);
       
   276                             } while (--op);
       
   277                             from = out - dist;  /* rest from output */
       
   278                         }
       
   279                     }
       
   280                     while (len > 2) {
       
   281                         PUP(out) = PUP(from);
       
   282                         PUP(out) = PUP(from);
       
   283                         PUP(out) = PUP(from);
       
   284                         len -= 3;
       
   285                     }
       
   286                     if (len) {
       
   287                         PUP(out) = PUP(from);
       
   288                         if (len > 1)
       
   289                             PUP(out) = PUP(from);
       
   290                     }
       
   291                 }
       
   292                 else {
       
   293                     from = out - dist;          /* copy direct from output */
       
   294                     do {                        /* minimum length is three */
       
   295                         PUP(out) = PUP(from);
       
   296                         PUP(out) = PUP(from);
       
   297                         PUP(out) = PUP(from);
       
   298                         len -= 3;
       
   299                     } while (len > 2);
       
   300                     if (len) {
       
   301                         PUP(out) = PUP(from);
       
   302                         if (len > 1)
       
   303                             PUP(out) = PUP(from);
       
   304                     }
       
   305                 }
       
   306             }
       
   307             else if ((op & 64) == 0) {          /* 2nd level distance code */
       
   308                 here = dcode[here.val + (hold & ((1U << op) - 1))];
       
   309                 goto dodist;
       
   310             }
       
   311             else {
       
   312                 strm->msg = (char *)"invalid distance code";
       
   313                 state->mode = BAD;
       
   314                 break;
       
   315             }
       
   316         }
       
   317         else if ((op & 64) == 0) {              /* 2nd level length code */
       
   318             here = lcode[here.val + (hold & ((1U << op) - 1))];
       
   319             goto dolen;
       
   320         }
       
   321         else if (op & 32) {                     /* end-of-block */
       
   322             Tracevv((stderr, "inflate:         end of block\n"));
       
   323             state->mode = TYPE;
       
   324             break;
       
   325         }
       
   326         else {
       
   327             strm->msg = (char *)"invalid literal/length code";
       
   328             state->mode = BAD;
       
   329             break;
       
   330         }
       
   331     } while (in < last && out < end);
       
   332 
       
   333     /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
       
   334     len = bits >> 3;
       
   335     in -= len;
       
   336     bits -= len << 3;
       
   337     hold &= (1U << bits) - 1;
       
   338 
       
   339     /* update state and return */
       
   340     strm->next_in = in + OFF;
       
   341     strm->next_out = out + OFF;
       
   342     strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
       
   343     strm->avail_out = (unsigned)(out < end ?
       
   344                                  257 + (end - out) : 257 - (out - end));
       
   345     state->hold = hold;
       
   346     state->bits = bits;
       
   347     return;
       
   348 }
       
   349 
       
   350 /*
       
   351    inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
       
   352    - Using bit fields for code structure
       
   353    - Different op definition to avoid & for extra bits (do & for table bits)
       
   354    - Three separate decoding do-loops for direct, window, and wnext == 0
       
   355    - Special case for distance > 1 copies to do overlapped load and store copy
       
   356    - Explicit branch predictions (based on measured branch probabilities)
       
   357    - Deferring match copy and interspersed it with decoding subsequent codes
       
   358    - Swapping literal/length else
       
   359    - Swapping window/direct else
       
   360    - Larger unrolled copy loops (three is about right)
       
   361    - Moving len -= 3 statement into middle of loop
       
   362  */
       
   363 
       
   364 #endif /* !ASMINF */