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