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