The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/libkern/zlib/inffast.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 7b6485ece545f739174804cf83671a4a


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.