Mercurial > ~darius > hgwebdir.cgi > paradise_client
comparison zlib/inffast.c @ 3:5a977ccbc7a9 default tip
Empty changelog
author | darius |
---|---|
date | Sat, 06 Dec 1997 05:41:29 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
2:fba0b6e6cdc7 | 3:5a977ccbc7a9 |
---|---|
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 Tracevv((stderr, "inflate: * end of block\n")); | |
77 UNGRAB | |
78 UPDATE | |
79 return Z_STREAM_END; | |
80 } | |
81 } while ((e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop) < 0); | |
82 DUMPBITS(t->bits) | |
83 | |
84 /* process literal or length (end of block already trapped) */ | |
85 if (e & 16) /* then it's a literal */ | |
86 { | |
87 Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ? | |
88 "inflate: * literal '%c'\n" : | |
89 "inflate: * literal 0x%02x\n", t->base)); | |
90 *q++ = (Byte)t->base; | |
91 m--; | |
92 } | |
93 else /* it's a length */ | |
94 { | |
95 /* get length of block to copy (already have extra bits) */ | |
96 c = t->base + ((uInt)b & inflate_mask[e]); | |
97 DUMPBITS(e); | |
98 Tracevv((stderr, "inflate: * length %u\n", c)); | |
99 | |
100 /* decode distance base of block to copy */ | |
101 GRABBITS(15); /* max bits for distance code */ | |
102 if ((e = (t = td + ((uInt)b & md))->exop) < 0) | |
103 do { | |
104 if (e == -128) | |
105 { | |
106 z->msg = "invalid distance code"; | |
107 UNGRAB | |
108 UPDATE | |
109 return Z_DATA_ERROR; | |
110 } | |
111 DUMPBITS(t->bits) | |
112 e = -e; | |
113 } while ((e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop) < 0); | |
114 DUMPBITS(t->bits) | |
115 | |
116 /* get extra bits to add to distance base */ | |
117 GRABBITS((uInt)e) /* get extra bits (up to 13) */ | |
118 d = t->base + ((uInt)b & inflate_mask[e]); | |
119 DUMPBITS(e) | |
120 Tracevv((stderr, "inflate: * distance %u\n", d)); | |
121 | |
122 /* do the copy */ | |
123 m -= c; | |
124 if ((uInt)(q - s->window) >= d) /* if offset before destination, */ | |
125 { /* just copy */ | |
126 r = q - d; | |
127 *q++ = *r++; c--; /* minimum count is three, */ | |
128 *q++ = *r++; c--; /* so unroll loop a little */ | |
129 do { | |
130 *q++ = *r++; | |
131 } while (--c); | |
132 } | |
133 else /* else offset after destination */ | |
134 { | |
135 e = d - (q - s->window); /* bytes from offset to end */ | |
136 r = s->end - e; /* pointer to offset */ | |
137 if (c > (uInt)e) /* if source crosses, */ | |
138 { | |
139 c -= e; /* copy to end of window */ | |
140 do { | |
141 *q++ = *r++; | |
142 } while (--e); | |
143 r = s->window; /* copy rest from start of window */ | |
144 } | |
145 do { /* copy all or what's left */ | |
146 *q++ = *r++; | |
147 } while (--c); | |
148 } | |
149 } | |
150 } while (m >= 258 && n >= 10); | |
151 | |
152 /* not enough input or output--restore pointers and return */ | |
153 UNGRAB | |
154 UPDATE | |
155 return Z_OK; | |
156 } |