Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / lib / libz / inffast.c
1 /* inffast.c -- process literals and length/distance pairs fast
2  * Copyright (C) 1995-2002 Mark Adler
3  * For conditions of distribution and use, see copyright notice in zlib.h 
4  *
5  * $FreeBSD: src/lib/libz/inffast.c,v 1.1.1.3.6.1 2003/02/01 13:33:12 sobomax Exp $
6  * $DragonFly: src/lib/libz/Attic/inffast.c,v 1.2 2003/06/17 04:26:52 dillon Exp $
7  */
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             r = q - d;
100             if (r < s->window)                  /* wrap if needed */
101             {
102               do {
103                 r += s->end - s->window;        /* force pointer in window */
104               } while (r < s->window);          /* covers invalid distances */
105               e = s->end - r;
106               if (c > e)
107               {
108                 c -= e;                         /* wrapped copy */
109                 do {
110                     *q++ = *r++;
111                 } while (--e);
112                 r = s->window;
113                 do {
114                     *q++ = *r++;
115                 } while (--c);
116               }
117               else                              /* normal copy */
118               {
119                 *q++ = *r++;  c--;
120                 *q++ = *r++;  c--;
121                 do {
122                     *q++ = *r++;
123                 } while (--c);
124               }
125             }
126             else                                /* normal copy */
127             {
128               *q++ = *r++;  c--;
129               *q++ = *r++;  c--;
130               do {
131                 *q++ = *r++;
132               } while (--c);
133             }
134             break;
135           }
136           else if ((e & 64) == 0)
137           {
138             t += t->base;
139             e = (t += ((uInt)b & inflate_mask[e]))->exop;
140           }
141           else
142           {
143             z->msg = (char*)"invalid distance code";
144             UNGRAB
145             UPDATE
146             return Z_DATA_ERROR;
147           }
148         } while (1);
149         break;
150       }
151       if ((e & 64) == 0)
152       {
153         t += t->base;
154         if ((e = (t += ((uInt)b & inflate_mask[e]))->exop) == 0)
155         {
156           DUMPBITS(t->bits)
157           Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
158                     "inflate:         * literal '%c'\n" :
159                     "inflate:         * literal 0x%02x\n", t->base));
160           *q++ = (Byte)t->base;
161           m--;
162           break;
163         }
164       }
165       else if (e & 32)
166       {
167         Tracevv((stderr, "inflate:         * end of block\n"));
168         UNGRAB
169         UPDATE
170         return Z_STREAM_END;
171       }
172       else
173       {
174         z->msg = (char*)"invalid literal/length code";
175         UNGRAB
176         UPDATE
177         return Z_DATA_ERROR;
178       }
179     } while (1);
180   } while (m >= 258 && n >= 10);
181
182   /* not enough input or output--restore pointers and return */
183   UNGRAB
184   UPDATE
185   return Z_OK;
186 }