aboutsummaryrefslogtreecommitdiff
path: root/inffast.c
diff options
context:
space:
mode:
authorMark Adler <madler@alumni.caltech.edu>2011-09-09 22:52:17 -0700
committerMark Adler <madler@alumni.caltech.edu>2011-09-09 22:52:17 -0700
commit913afb9174bb474104049906c1382dec81826424 (patch)
tree46bb8ca746088f81382b4f33970b9d43c33d9ba3 /inffast.c
parentbcf78a20978d76f64b7cd46d1a4d7a79a578c77b (diff)
downloadzlib-913afb9174bb474104049906c1382dec81826424.tar.gz
zlib-913afb9174bb474104049906c1382dec81826424.tar.bz2
zlib-913afb9174bb474104049906c1382dec81826424.zip
zlib 0.79v0.79
Diffstat (limited to 'inffast.c')
-rw-r--r--inffast.c148
1 files changed, 148 insertions, 0 deletions
diff --git a/inffast.c b/inffast.c
new file mode 100644
index 0000000..8c3e4ce
--- /dev/null
+++ b/inffast.c
@@ -0,0 +1,148 @@
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/* simplify the use of the inflate_huft type with some defines */
12#define base more.Base
13#define next more.Next
14#define exop word.what.Exop
15#define bits word.what.Bits
16
17/* macros for bit input with no checking and for returning unused bytes */
18#ifdef DEBUG
19# undef NEXTBYTE
20# define NEXTBYTE (n--?0:fprintf(stderr,"inffast underrun\n"),*p++)
21#endif
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
30int inflate_fast(bl, bd, tl, td, s, z)
31uInt bl, bd;
32inflate_huft *tl, *td;
33struct inflate_blocks_state *s;
34z_stream *z;
35{
36 inflate_huft *t; /* temporary pointer */
37 int e; /* extra bits or operation */
38 uLong b; /* bit buffer */
39 uInt k; /* bits in bit buffer */
40 Byte *p; /* input data pointer */
41 uInt n; /* bytes available there */
42 Byte *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 Byte *r; /* copy source pointer */
49
50 /* load input, output, bit values */
51 LOAD
52
53 /* initialize masks in registers */
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 do {
63 if (e == -128)
64 {
65 z->msg = "invalid literal/length code";
66 UNGRAB
67 UPDATE
68 return Z_DATA_ERROR;
69 }
70 DUMPBITS(t->bits)
71 e = -e;
72 if (e & 64) /* end of block */
73 {
74 UNGRAB
75 UPDATE
76 return Z_STREAM_END;
77 }
78 } while ((e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop) < 0);
79 DUMPBITS(t->bits)
80
81 /* process literal or length (end of block already trapped) */
82 if (e & 16) /* then it's a literal */
83 {
84 *q++ = (Byte)t->base;
85 m--;
86 }
87 else /* it's a length */
88 {
89 /* get length of block to copy (already have extra bits) */
90 c = t->base + ((uInt)b & inflate_mask[e]);
91 DUMPBITS(e);
92
93 /* decode distance base of block to copy */
94 GRABBITS(15); /* max bits for distance code */
95 if ((e = (t = td + ((uInt)b & md))->exop) < 0)
96 do {
97 if (e == -128)
98 {
99 z->msg = "invalid distance code";
100 UNGRAB
101 UPDATE
102 return Z_DATA_ERROR;
103 }
104 DUMPBITS(t->bits)
105 e = -e;
106 } while ((e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop) < 0);
107 DUMPBITS(t->bits)
108
109 /* get extra bits to add to distance base */
110 GRABBITS(e) /* get extra bits (up to 13) */
111 d = t->base + ((uInt)b & inflate_mask[e]);
112 DUMPBITS(e)
113
114 /* do the copy */
115 m -= c;
116 if (q - s->window >= d) /* if offset before destination, */
117 { /* just copy */
118 r = q - d;
119 *q++ = *r++; c--; /* minimum count is three, */
120 *q++ = *r++; c--; /* so unroll loop a little */
121 do {
122 *q++ = *r++;
123 } while (--c);
124 }
125 else /* else offset after destination */
126 {
127 e = d - (q - s->window); /* bytes from offset to end */
128 r = s->end - e; /* pointer to offset */
129 if (c > e) /* if source crosses, */
130 {
131 c -= e; /* copy to end of window */
132 do {
133 *q++ = *r++;
134 } while (--e);
135 r = s->window; /* copy rest from start of window */
136 }
137 do { /* copy all or what's left */
138 *q++ = *r++;
139 } while (--c);
140 }
141 }
142 } while (m >= 258 && n >= 10);
143
144 /* not enough input or output--restore pointers and return */
145 UNGRAB
146 UPDATE
147 return Z_OK;
148}