]> git.lizzy.rs Git - zlib.git/blob - inffast.c
zlib 0.93
[zlib.git] / inffast.c
1 /* inffast.c -- process literals and length/distance pairs fast
2  * Copyright (C) 1995 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 "infutil.h"
9 #include "inffast.h"
10
11 struct inflate_codes_state {int dummy;}; /* for buggy compilers */
12
13 /* simplify the use of the inflate_huft type with some defines */
14 #define base more.Base
15 #define next more.Next
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 {n+=(c=k>>3);p-=c;k&=7;}
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(bl, bd, tl, td, s, z)
29 uInt bl, bd;
30 inflate_huft *tl, *td;
31 struct inflate_blocks_state *s;
32 z_stream *z;
33 {
34   inflate_huft *t;      /* temporary pointer */
35   uInt e;               /* extra bits or operation */
36   uLong b;              /* bit buffer */
37   uInt k;               /* bits in bit buffer */
38   Byte *p;              /* input data pointer */
39   uInt n;               /* bytes available there */
40   Byte *q;              /* output window write pointer */
41   uInt m;               /* bytes to end of window or read pointer */
42   uInt ml;              /* mask for literal/length tree */
43   uInt md;              /* mask for distance tree */
44   uInt c;               /* bytes to copy */
45   uInt d;               /* distance back to copy from */
46   Byte *r;              /* copy source pointer */
47
48   /* load input, output, bit values */
49   LOAD
50
51   /* initialize masks */
52   ml = inflate_mask[bl];
53   md = inflate_mask[bd];
54
55   /* do until not enough input or output space for fast loop */
56   do {                          /* assume called with m >= 258 && n >= 10 */
57     /* get literal/length code */
58     GRABBITS(20)                /* max bits for literal/length code */
59     if ((e = (t = tl + ((uInt)b & ml))->exop) == 0)
60     {
61       DUMPBITS(t->bits)
62       Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
63                 "inflate:         * literal '%c'\n" :
64                 "inflate:         * literal 0x%02x\n", t->base));
65       *q++ = (Byte)t->base;
66       m--;
67       continue;
68     }
69     do {
70       DUMPBITS(t->bits)
71       if (e & 16)
72       {
73         /* get extra bits for length */
74         e &= 15;
75         c = t->base + ((uInt)b & inflate_mask[e]);
76         DUMPBITS(e)
77         Tracevv((stderr, "inflate:         * length %u\n", c));
78
79         /* decode distance base of block to copy */
80         GRABBITS(15);           /* max bits for distance code */
81         e = (t = td + ((uInt)b & md))->exop;
82         do {
83           DUMPBITS(t->bits)
84           if (e & 16)
85           {
86             /* get extra bits to add to distance base */
87             e &= 15;
88             GRABBITS(e)         /* get extra bits (up to 13) */
89             d = t->base + ((uInt)b & inflate_mask[e]);
90             DUMPBITS(e)
91             Tracevv((stderr, "inflate:         * distance %u\n", d));
92
93             /* do the copy */
94             m -= c;
95             if ((uInt)(q - s->window) >= d)     /* offset before dest */
96             {                                   /*  just copy */
97               r = q - d;
98               *q++ = *r++;  c--;        /* minimum count is three, */
99               *q++ = *r++;  c--;        /*  so unroll loop a little */
100             }
101             else                        /* else offset after destination */
102             {
103               e = d - (q - s->window);  /* bytes from offset to end */
104               r = s->end - e;           /* pointer to offset */
105               if (c > e)                /* if source crosses, */
106               {
107                 c -= e;                 /* copy to end of window */
108                 do {
109                   *q++ = *r++;
110                 } while (--e);
111                 r = s->window;          /* copy rest from start of window */
112               }
113             }
114             do {                        /* copy all or what's left */
115               *q++ = *r++;
116             } while (--c);
117             break;
118           }
119           else if ((e & 64) == 0)
120             e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop;
121           else
122           {
123             z->msg = "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         if ((e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop) == 0)
134         {
135           DUMPBITS(t->bits)
136           Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
137                     "inflate:         * literal '%c'\n" :
138                     "inflate:         * literal 0x%02x\n", t->base));
139           *q++ = (Byte)t->base;
140           m--;
141           break;
142         }
143       }
144       else if (e & 32)
145       {
146         Tracevv((stderr, "inflate:         * end of block\n"));
147         UNGRAB
148         UPDATE
149         return Z_STREAM_END;
150       }
151       else
152       {
153         z->msg = "invalid literal/length code";
154         UNGRAB
155         UPDATE
156         return Z_DATA_ERROR;
157       }
158     } while (1);
159   } while (m >= 258 && n >= 10);
160
161   /* not enough input or output--restore pointers and return */
162   UNGRAB
163   UPDATE
164   return Z_OK;
165 }