comparison zlib/inffast.c @ 10:1040ca591f2e

First entry of Paradise Server 2.9 patch 10 Beta
author darius
date Sat, 06 Dec 1997 04:37:18 +0000
parents
children
comparison
equal deleted inserted replaced
9:331055a97a9d 10:1040ca591f2e
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 "infblock.h"
9 #include "infcodes.h"
10 #include "infutil.h"
11 #include "inffast.h"
12
13 struct inflate_codes_state {int dummy;}; /* for buggy compilers */
14
15 /* simplify the use of the inflate_huft type with some defines */
16 #define base more.Base
17 #define next more.Next
18 #define exop word.what.Exop
19 #define bits word.what.Bits
20
21 /* macros for bit input with no checking and for returning unused bytes */
22 #define GRABBITS(j) {while(k<(j)){b|=((uLong)NEXTBYTE)<<k;k+=8;}}
23 #define UNGRAB {n+=(c=k>>3);p-=c;k&=7;}
24
25 /* Called with number of bytes left to write in window at least 258
26 (the maximum string length) and number of input bytes available
27 at least ten. The ten bytes are six bytes for the longest length/
28 distance pair plus four bytes for overloading the bit buffer. */
29
30 int inflate_fast(bl, bd, tl, td, s, z)
31 uInt bl, bd;
32 inflate_huft *tl, *td;
33 inflate_blocks_statef *s;
34 z_stream *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((stderr, 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((stderr, "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((stderr, "inflate: * distance %u\n", d));
94
95 /* do the copy */
96 m -= c;
97 if ((uInt)(q - s->window) >= d) /* offset before dest */
98 { /* just copy */
99 r = q - d;
100 *q++ = *r++; c--; /* minimum count is three, */
101 *q++ = *r++; c--; /* so unroll loop a little */
102 }
103 else /* else offset after destination */
104 {
105 e = d - (q - s->window); /* bytes from offset to end */
106 r = s->end - e; /* pointer to offset */
107 if (c > e) /* if source crosses, */
108 {
109 c -= e; /* copy to end of window */
110 do {
111 *q++ = *r++;
112 } while (--e);
113 r = s->window; /* copy rest from start of window */
114 }
115 }
116 do { /* copy all or what's left */
117 *q++ = *r++;
118 } while (--c);
119 break;
120 }
121 else if ((e & 64) == 0)
122 e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop;
123 else
124 {
125 z->msg = "invalid distance code";
126 UNGRAB
127 UPDATE
128 return Z_DATA_ERROR;
129 }
130 } while (1);
131 break;
132 }
133 if ((e & 64) == 0)
134 {
135 if ((e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop) == 0)
136 {
137 DUMPBITS(t->bits)
138 Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
139 "inflate: * literal '%c'\n" :
140 "inflate: * literal 0x%02x\n", t->base));
141 *q++ = (Byte)t->base;
142 m--;
143 break;
144 }
145 }
146 else if (e & 32)
147 {
148 Tracevv((stderr, "inflate: * end of block\n"));
149 UNGRAB
150 UPDATE
151 return Z_STREAM_END;
152 }
153 else
154 {
155 z->msg = "invalid literal/length code";
156 UNGRAB
157 UPDATE
158 return Z_DATA_ERROR;
159 }
160 } while (1);
161 } while (m >= 258 && n >= 10);
162
163 /* not enough input or output--restore pointers and return */
164 UNGRAB
165 UPDATE
166 return Z_OK;
167 }