]> git.saurik.com Git - wxWidgets.git/blame - src/zlib/inffast.c
no real changes
[wxWidgets.git] / src / zlib / inffast.c
CommitLineData
c801d85f
KB
1/* inffast.c -- process literals and length/distance pairs fast
2 * Copyright (C) 1995-1998 Mark Adler
e6ebb514 3 * For conditions of distribution and use, see copyright notice in zlib.h
c801d85f
KB
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
13struct inflate_codes_state {int dummy;}; /* for buggy compilers */
14
15/* simplify the use of the inflate_huft type with some defines */
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#define GRABBITS(j) {while(k<(j)){b|=((uLong)NEXTBYTE)<<k;k+=8;}}
21#define UNGRAB {c=z->avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;}
22
23/* Called with number of bytes left to write in window at least 258
24 (the maximum string length) and number of input bytes available
25 at least ten. The ten bytes are six bytes for the longest length/
26 distance pair plus four bytes for overloading the bit buffer. */
27
75bce3ad 28#if defined(__VISAGECPP__) /* Visualage can't handle this antiquated interface */
e6ebb514
DW
29int inflate_fast(uInt bl, uInt bd, inflate_huft* tl, inflate_huft* td, inflate_blocks_statef* s, z_streamp z)
30#else
c801d85f
KB
31int inflate_fast(bl, bd, tl, td, s, z)
32uInt bl, bd;
33inflate_huft *tl;
34inflate_huft *td; /* need separate declaration for Borland C++ */
35inflate_blocks_statef *s;
36z_streamp z;
e6ebb514 37#endif
c801d85f
KB
38{
39 inflate_huft *t; /* temporary pointer */
40 uInt e; /* extra bits or operation */
41 uLong b; /* bit buffer */
42 uInt k; /* bits in bit buffer */
43 Bytef *p; /* input data pointer */
44 uInt n; /* bytes available there */
45 Bytef *q; /* output window write pointer */
46 uInt m; /* bytes to end of window or read pointer */
47 uInt ml; /* mask for literal/length tree */
48 uInt md; /* mask for distance tree */
49 uInt c; /* bytes to copy */
50 uInt d; /* distance back to copy from */
51 Bytef *r; /* copy source pointer */
52
53 /* load input, output, bit values */
54 LOAD
55
56 /* initialize masks */
57 ml = inflate_mask[bl];
58 md = inflate_mask[bd];
59
60 /* do until not enough input or output space for fast loop */
61 do { /* assume called with m >= 258 && n >= 10 */
62 /* get literal/length code */
63 GRABBITS(20) /* max bits for literal/length code */
64 if ((e = (t = tl + ((uInt)b & ml))->exop) == 0)
65 {
66 DUMPBITS(t->bits)
67 Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
68 "inflate: * literal '%c'\n" :
69 "inflate: * literal 0x%02x\n", t->base));
70 *q++ = (Byte)t->base;
71 m--;
72 continue;
73 }
74 do {
75 DUMPBITS(t->bits)
76 if (e & 16)
77 {
78 /* get extra bits for length */
79 e &= 15;
80 c = t->base + ((uInt)b & inflate_mask[e]);
81 DUMPBITS(e)
82 Tracevv((stderr, "inflate: * length %u\n", c));
83
84 /* decode distance base of block to copy */
85 GRABBITS(15); /* max bits for distance code */
86 e = (t = td + ((uInt)b & md))->exop;
87 do {
88 DUMPBITS(t->bits)
89 if (e & 16)
90 {
91 /* get extra bits to add to distance base */
92 e &= 15;
93 GRABBITS(e) /* get extra bits (up to 13) */
94 d = t->base + ((uInt)b & inflate_mask[e]);
95 DUMPBITS(e)
96 Tracevv((stderr, "inflate: * distance %u\n", d));
97
98 /* do the copy */
99 m -= c;
100 if ((uInt)(q - s->window) >= d) /* offset before dest */
101 { /* just copy */
102 r = q - d;
103 *q++ = *r++; c--; /* minimum count is three, */
104 *q++ = *r++; c--; /* so unroll loop a little */
105 }
106 else /* else offset after destination */
107 {
108 e = d - (uInt)(q - s->window); /* bytes from offset to end */
109 r = s->end - e; /* pointer to offset */
110 if (c > e) /* if source crosses, */
111 {
112 c -= e; /* copy to end of window */
113 do {
114 *q++ = *r++;
115 } while (--e);
116 r = s->window; /* copy rest from start of window */
117 }
118 }
119 do { /* copy all or what's left */
120 *q++ = *r++;
121 } while (--c);
122 break;
123 }
124 else if ((e & 64) == 0)
125 {
126 t += t->base;
127 e = (t += ((uInt)b & inflate_mask[e]))->exop;
128 }
129 else
130 {
131 z->msg = (char*)"invalid distance code";
132 UNGRAB
133 UPDATE
134 return Z_DATA_ERROR;
135 }
136 } while (1);
137 break;
138 }
139 if ((e & 64) == 0)
140 {
141 t += t->base;
142 if ((e = (t += ((uInt)b & inflate_mask[e]))->exop) == 0)
143 {
144 DUMPBITS(t->bits)
145 Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
146 "inflate: * literal '%c'\n" :
147 "inflate: * literal 0x%02x\n", t->base));
148 *q++ = (Byte)t->base;
149 m--;
150 break;
151 }
152 }
153 else if (e & 32)
154 {
155 Tracevv((stderr, "inflate: * end of block\n"));
156 UNGRAB
157 UPDATE
158 return Z_STREAM_END;
159 }
160 else
161 {
162 z->msg = (char*)"invalid literal/length code";
163 UNGRAB
164 UPDATE
165 return Z_DATA_ERROR;
166 }
167 } while (1);
168 } while (m >= 258 && n >= 10);
169
170 /* not enough input or output--restore pointers and return */
171 UNGRAB
172 UPDATE
173 return Z_OK;
174}