]> git.lizzy.rs Git - zlib.git/blob - inffast.c
zlib 0.8
[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 #ifdef DEBUG
21 #  undef NEXTBYTE
22 #  define NEXTBYTE (n--?0:fprintf(stderr,"inffast underrun\n"),*p++)
23 #endif
24 #define GRABBITS(j) {while(k<(j)){b|=((uLong)NEXTBYTE)<<k;k+=8;}}
25 #define UNGRAB {n+=(c=k>>3);p-=c;k&=7;}
26
27 /* Called with number of bytes left to write in window at least 258
28    (the maximum string length) and number of input bytes available
29    at least ten.  The ten bytes are six bytes for the longest length/
30    distance pair plus four bytes for overloading the bit buffer. */
31
32 int inflate_fast(bl, bd, tl, td, s, z)
33 uInt bl, bd;
34 inflate_huft *tl, *td;
35 struct inflate_blocks_state *s;
36 z_stream *z;
37 {
38   inflate_huft *t;      /* temporary pointer */
39   int e;                /* extra bits or operation */
40   uLong b;              /* bit buffer */
41   uInt k;               /* bits in bit buffer */
42   Byte *p;              /* input data pointer */
43   uInt n;               /* bytes available there */
44   Byte *q;              /* output window write pointer */
45   uInt m;               /* bytes to end of window or read pointer */
46   uInt ml;              /* mask for literal/length tree */
47   uInt md;              /* mask for distance tree */
48   uInt c;               /* bytes to copy */
49   uInt d;               /* distance back to copy from */
50   Byte *r;              /* copy source pointer */
51
52   /* load input, output, bit values */
53   LOAD
54
55   /* initialize masks in registers */
56   ml = inflate_mask[bl];
57   md = inflate_mask[bd];
58
59   /* do until not enough input or output space for fast loop */
60   do {                          /* assume called with m >= 258 && n >= 10 */
61     /* get literal/length code */
62     GRABBITS(20)                /* max bits for literal/length code */
63     if ((e = (t = tl + ((uInt)b & ml))->exop) < 0)
64       do {
65         if (e == -128)
66         {
67           z->msg = "invalid literal/length code";
68           UNGRAB
69           UPDATE
70           return Z_DATA_ERROR;
71         }
72         DUMPBITS(t->bits)
73         e = -e;
74         if (e & 64)             /* end of block */
75         {
76           UNGRAB
77           UPDATE
78           return Z_STREAM_END;
79         }
80       } while ((e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop) < 0);
81     DUMPBITS(t->bits)
82
83     /* process literal or length (end of block already trapped) */
84     if (e & 16)                 /* then it's a literal */
85     {
86       *q++ = (Byte)t->base;
87       m--;
88     }
89     else                        /* it's a length */
90     {
91       /* get length of block to copy (already have extra bits) */
92       c = t->base + ((uInt)b & inflate_mask[e]);
93       DUMPBITS(e);
94
95       /* decode distance base of block to copy */
96       GRABBITS(15);             /* max bits for distance code */
97       if ((e = (t = td + ((uInt)b & md))->exop) < 0)
98         do {
99           if (e == -128)
100           {
101             z->msg = "invalid distance code";
102             UNGRAB
103             UPDATE
104             return Z_DATA_ERROR;
105           }
106           DUMPBITS(t->bits)
107           e = -e;
108         } while ((e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop) < 0);
109       DUMPBITS(t->bits)
110
111       /* get extra bits to add to distance base */
112       GRABBITS(e)           /* get extra bits (up to 13) */
113       d = t->base + ((uInt)b & inflate_mask[e]);
114       DUMPBITS(e)
115
116       /* do the copy */
117       m -= c;
118       if (q - s->window >= d)           /* if offset before destination, */
119       {                                 /*  just copy */
120         r = q - d;
121         *q++ = *r++;  c--;              /* minimum count is three, */
122         *q++ = *r++;  c--;              /*  so unroll loop a little */
123         do {
124           *q++ = *r++;
125         } while (--c);
126       }
127       else                              /* else offset after destination */
128       {
129         e = d - (q - s->window);        /* bytes from offset to end */
130         r = s->end - e;                 /* pointer to offset */
131         if (c > e)                      /* if source crosses, */
132         {
133           c -= e;                       /* copy to end of window */
134           do {
135             *q++ = *r++;
136           } while (--e);
137           r = s->window;                /* copy rest from start of window */
138         }
139         do {                            /* copy all or what's left */
140           *q++ = *r++;
141         } while (--c);
142       }
143     }
144   } while (m >= 258 && n >= 10);
145
146   /* not enough input or output--restore pointers and return */
147   UNGRAB
148   UPDATE
149   return Z_OK;
150 }