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