compressionlibs/ziplib/test/oldezlib/Zlib/inffast.c
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /* inffast.c -- process literals and length/distance pairs fast
       
     2  * Copyright (C) 1995-1998 Mark Adler
       
     3  * For conditions of distribution and use, see copyright notice in zlib.h 
       
     4  */
       
     5 
       
     6 #include "zutil.h"
       
     7 #include "inftrees.h"
       
     8 #include "infblock.h"
       
     9 #include "infcodes.h"
       
    10 #include "infutil.h"
       
    11 #include "inffast.h"
       
    12 
       
    13 struct inflate_codes_state {int dummy;}; /* for buggy compilers */
       
    14 
       
    15 /* simplify the use of the inflate_huft type with some defines */
       
    16 #define exop word.what.Exop
       
    17 #define bits word.what.Bits
       
    18 
       
    19 /* macros for bit input with no checking and for returning unused bytes */
       
    20 #define GRABBITS(j) {while(k<(j)){b|=((uLong)NEXTBYTE)<<k;k+=8;}}
       
    21 #define UNGRAB {c=z->avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;}
       
    22 
       
    23 /* Called with number of bytes left to write in window at least 258
       
    24    (the maximum string length) and number of input bytes available
       
    25    at least ten.  The ten bytes are six bytes for the longest length/
       
    26    distance pair plus four bytes for overloading the bit buffer. */
       
    27 
       
    28 int inflate_fast(uInt bl, uInt bd, inflate_huft *tl, inflate_huft *td, inflate_blocks_statef *s, z_streamp z)
       
    29 /* inflate_huft *td        need separate declaration for Borland C++ */
       
    30 {
       
    31   inflate_huft *t;      /* temporary pointer */
       
    32   uInt e;               /* extra bits or operation */
       
    33   uLong b;              /* bit buffer */
       
    34   uInt k;               /* bits in bit buffer */
       
    35   Bytef *p;             /* input data pointer */
       
    36   uInt n;               /* bytes available there */
       
    37   Bytef *q;             /* output window write pointer */
       
    38   uInt m;               /* bytes to end of window or read pointer */
       
    39   uInt ml;              /* mask for literal/length tree */
       
    40   uInt md;              /* mask for distance tree */
       
    41   uInt c;               /* bytes to copy */
       
    42   uInt d;               /* distance back to copy from */
       
    43   Bytef *r;             /* copy source pointer */
       
    44 
       
    45   /* load input, output, bit values */
       
    46   LOAD
       
    47 
       
    48   /* initialize masks */
       
    49   ml = inflate_mask[bl];
       
    50   md = inflate_mask[bd];
       
    51 
       
    52   /* do until not enough input or output space for fast loop */
       
    53   do {                          /* assume called with m >= 258 && n >= 10 */
       
    54     /* get literal/length code */
       
    55     GRABBITS(20)                /* max bits for literal/length code */
       
    56     if ((e = (t = tl + ((uInt)b & ml))->exop) == 0)
       
    57     {
       
    58       DUMPBITS(t->bits)
       
    59       Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
       
    60                 "inflate:         * literal '%c'\n" :
       
    61                 "inflate:         * literal 0x%02x\n", t->base));
       
    62       *q++ = (Byte)t->base;
       
    63       m--;
       
    64       continue;
       
    65     }
       
    66     do {
       
    67       DUMPBITS(t->bits)
       
    68       if (e & 16)
       
    69       {
       
    70         /* get extra bits for length */
       
    71         e &= 15;
       
    72         c = t->base + ((uInt)b & inflate_mask[e]);
       
    73         DUMPBITS(e)
       
    74         Tracevv((stderr, "inflate:         * length %u\n", c));
       
    75 
       
    76         /* decode distance base of block to copy */
       
    77         GRABBITS(15);           /* max bits for distance code */
       
    78         e = (t = td + ((uInt)b & md))->exop;
       
    79         do {
       
    80           DUMPBITS(t->bits)
       
    81           if (e & 16)
       
    82           {
       
    83             /* get extra bits to add to distance base */
       
    84             e &= 15;
       
    85             GRABBITS(e)         /* get extra bits (up to 13) */
       
    86             d = t->base + ((uInt)b & inflate_mask[e]);
       
    87             DUMPBITS(e)
       
    88             Tracevv((stderr, "inflate:         * distance %u\n", d));
       
    89 
       
    90             /* do the copy */
       
    91             m -= c;
       
    92             if ((uInt)(q - s->window) >= d)     /* offset before dest */
       
    93             {                                   /*  just copy */
       
    94               r = q - d;
       
    95               *q++ = *r++;  c--;        /* minimum count is three, */
       
    96               *q++ = *r++;  c--;        /*  so unroll loop a little */
       
    97             }
       
    98             else                        /* else offset after destination */
       
    99             {
       
   100               e = d - (uInt)(q - s->window); /* bytes from offset to end */
       
   101               r = s->end - e;           /* pointer to offset */
       
   102               if (c > e)                /* if source crosses, */
       
   103               {
       
   104                 c -= e;                 /* copy to end of window */
       
   105                 do {
       
   106                   *q++ = *r++;
       
   107                 } while (--e);
       
   108                 r = s->window;          /* copy rest from start of window */
       
   109               }
       
   110             }
       
   111             do {                        /* copy all or what's left */
       
   112               *q++ = *r++;
       
   113             } while (--c);
       
   114             break;
       
   115           }
       
   116           else if ((e & 64) == 0)
       
   117           {
       
   118             t += t->base;
       
   119             e = (t += ((uInt)b & inflate_mask[e]))->exop;
       
   120           }
       
   121           else
       
   122           {
       
   123             z->msg = (char*)"invalid distance code";
       
   124             UNGRAB
       
   125             UPDATE
       
   126             return Z_DATA_ERROR;
       
   127           }
       
   128         } while (1);
       
   129         break;
       
   130       }
       
   131       if ((e & 64) == 0)
       
   132       {
       
   133         t += t->base;
       
   134         if ((e = (t += ((uInt)b & inflate_mask[e]))->exop) == 0)
       
   135         {
       
   136           DUMPBITS(t->bits)
       
   137           Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
       
   138                     "inflate:         * literal '%c'\n" :
       
   139                     "inflate:         * literal 0x%02x\n", t->base));
       
   140           *q++ = (Byte)t->base;
       
   141           m--;
       
   142           break;
       
   143         }
       
   144       }
       
   145       else if (e & 32)
       
   146       {
       
   147         Tracevv((stderr, "inflate:         * end of block\n"));
       
   148         UNGRAB
       
   149         UPDATE
       
   150         return Z_STREAM_END;
       
   151       }
       
   152       else
       
   153       {
       
   154         z->msg = (char*)"invalid literal/length code";
       
   155         UNGRAB
       
   156         UPDATE
       
   157         return Z_DATA_ERROR;
       
   158       }
       
   159     } while (1);
       
   160   } while (m >= 258 && n >= 10);
       
   161 
       
   162   /* not enough input or output--restore pointers and return */
       
   163   UNGRAB
       
   164   UPDATE
       
   165   return Z_OK;
       
   166 }