]> git.saurik.com Git - wxWidgets.git/blob - src/zlib/infcodes.c
1. wxMSW::wxNotebook::SetPageSize() and SetPadding() added
[wxWidgets.git] / src / zlib / infcodes.c
1 /* infcodes.c -- process literals and length/distance pairs
2 * Copyright (C) 1995-1998 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 /* simplify the use of the inflate_huft type with some defines */
14 #define exop word.what.Exop
15 #define bits word.what.Bits
16
17 typedef enum { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
18 START, /* x: set up for LEN */
19 LEN, /* i: get length/literal/eob next */
20 LENEXT, /* i: getting length extra (have base) */
21 DIST, /* i: get distance next */
22 DISTEXT, /* i: getting distance extra */
23 COPY, /* o: copying bytes in window, waiting for space */
24 LIT, /* o: got literal, waiting for output space */
25 WASH, /* o: got eob, possibly still output waiting */
26 END, /* x: got eob and all data flushed */
27 BADCODE} /* x: got error */
28 inflate_codes_mode;
29
30 /* inflate codes private state */
31 struct inflate_codes_state {
32
33 /* mode */
34 inflate_codes_mode mode; /* current inflate_codes mode */
35
36 /* mode dependent information */
37 uInt len;
38 union {
39 struct {
40 inflate_huft *tree; /* pointer into tree */
41 uInt need; /* bits needed */
42 } code; /* if LEN or DIST, where in tree */
43 uInt lit; /* if LIT, literal */
44 struct {
45 uInt get; /* bits to get for extra */
46 uInt dist; /* distance back to copy from */
47 } copy; /* if EXT or COPY, where and how much */
48 } sub; /* submode */
49
50 /* mode independent information */
51 Byte lbits; /* ltree bits decoded per branch */
52 Byte dbits; /* dtree bits decoder per branch */
53 inflate_huft *ltree; /* literal/length/eob tree */
54 inflate_huft *dtree; /* distance tree */
55
56 };
57
58 #if defined(__VISAGECPP__) /* Visualage can't handle this antiquated interface */
59 inflate_codes_statef* inflate_codes_new(uInt bl, uInt bd, inflate_huft* tl, inflate_huft* td, z_streamp z)
60 #else
61 inflate_codes_statef *inflate_codes_new(bl, bd, tl, td, z)
62 uInt bl, bd;
63 inflate_huft *tl;
64 inflate_huft *td; /* need separate declaration for Borland C++ */
65 z_streamp z;
66 #endif
67 {
68 inflate_codes_statef *c;
69
70 if ((c = (inflate_codes_statef *)
71 ZALLOC(z,1,sizeof(struct inflate_codes_state))) != Z_NULL)
72 {
73 c->mode = START;
74 c->lbits = (Byte)bl;
75 c->dbits = (Byte)bd;
76 c->ltree = tl;
77 c->dtree = td;
78 Tracev((stderr, "inflate: codes new\n"));
79 }
80 return c;
81 }
82
83 #if defined(__VISAGECPP__) /* Visualage can't handle this antiquated interface */
84 int inflate_codes(inflate_blocks_statef* s, z_streamp z, int r)
85 #else
86 int inflate_codes(s, z, r)
87 inflate_blocks_statef *s;
88 z_streamp z;
89 int r;
90 #endif
91 {
92 uInt j; /* temporary storage */
93 inflate_huft *t; /* temporary pointer */
94 uInt e; /* extra bits or operation */
95 uLong b; /* bit buffer */
96 uInt k; /* bits in bit buffer */
97 Bytef *p; /* input data pointer */
98 uInt n; /* bytes available there */
99 Bytef *q; /* output window write pointer */
100 uInt m; /* bytes to end of window or read pointer */
101 Bytef *f; /* pointer to copy strings from */
102 inflate_codes_statef *c = s->sub.decode.codes; /* codes state */
103
104 /* copy input/output information to locals (UPDATE macro restores) */
105 LOAD
106
107 /* process input and output based on current state */
108 while (1) switch (c->mode)
109 { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
110 case START: /* x: set up for LEN */
111 #ifndef SLOW
112 if (m >= 258 && n >= 10)
113 {
114 UPDATE
115 r = inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z);
116 LOAD
117 if (r != Z_OK)
118 {
119 c->mode = r == Z_STREAM_END ? WASH : BADCODE;
120 break;
121 }
122 }
123 #endif /* !SLOW */
124 c->sub.code.need = c->lbits;
125 c->sub.code.tree = c->ltree;
126 c->mode = LEN;
127 case LEN: /* i: get length/literal/eob next */
128 j = c->sub.code.need;
129 NEEDBITS(j)
130 t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
131 DUMPBITS(t->bits)
132 e = (uInt)(t->exop);
133 if (e == 0) /* literal */
134 {
135 c->sub.lit = t->base;
136 Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
137 "inflate: literal '%c'\n" :
138 "inflate: literal 0x%02x\n", t->base));
139 c->mode = LIT;
140 break;
141 }
142 if (e & 16) /* length */
143 {
144 c->sub.copy.get = e & 15;
145 c->len = t->base;
146 c->mode = LENEXT;
147 break;
148 }
149 if ((e & 64) == 0) /* next table */
150 {
151 c->sub.code.need = e;
152 c->sub.code.tree = t + t->base;
153 break;
154 }
155 if (e & 32) /* end of block */
156 {
157 Tracevv((stderr, "inflate: end of block\n"));
158 c->mode = WASH;
159 break;
160 }
161 c->mode = BADCODE; /* invalid code */
162 z->msg = (char*)"invalid literal/length code";
163 r = Z_DATA_ERROR;
164 LEAVE
165 case LENEXT: /* i: getting length extra (have base) */
166 j = c->sub.copy.get;
167 NEEDBITS(j)
168 c->len += (uInt)b & inflate_mask[j];
169 DUMPBITS(j)
170 c->sub.code.need = c->dbits;
171 c->sub.code.tree = c->dtree;
172 Tracevv((stderr, "inflate: length %u\n", c->len));
173 c->mode = DIST;
174 case DIST: /* i: get distance next */
175 j = c->sub.code.need;
176 NEEDBITS(j)
177 t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
178 DUMPBITS(t->bits)
179 e = (uInt)(t->exop);
180 if (e & 16) /* distance */
181 {
182 c->sub.copy.get = e & 15;
183 c->sub.copy.dist = t->base;
184 c->mode = DISTEXT;
185 break;
186 }
187 if ((e & 64) == 0) /* next table */
188 {
189 c->sub.code.need = e;
190 c->sub.code.tree = t + t->base;
191 break;
192 }
193 c->mode = BADCODE; /* invalid code */
194 z->msg = (char*)"invalid distance code";
195 r = Z_DATA_ERROR;
196 LEAVE
197 case DISTEXT: /* i: getting distance extra */
198 j = c->sub.copy.get;
199 NEEDBITS(j)
200 c->sub.copy.dist += (uInt)b & inflate_mask[j];
201 DUMPBITS(j)
202 Tracevv((stderr, "inflate: distance %u\n", c->sub.copy.dist));
203 c->mode = COPY;
204 case COPY: /* o: copying bytes in window, waiting for space */
205 #ifndef __TURBOC__ /* Turbo C bug for following expression */
206 f = (uInt)(q - s->window) < c->sub.copy.dist ?
207 s->end - (c->sub.copy.dist - (q - s->window)) :
208 q - c->sub.copy.dist;
209 #else
210 f = q - c->sub.copy.dist;
211 if ((uInt)(q - s->window) < c->sub.copy.dist)
212 f = s->end - (c->sub.copy.dist - (uInt)(q - s->window));
213 #endif
214 while (c->len)
215 {
216 NEEDOUT
217 OUTBYTE(*f++)
218 if (f == s->end)
219 f = s->window;
220 c->len--;
221 }
222 c->mode = START;
223 break;
224 case LIT: /* o: got literal, waiting for output space */
225 NEEDOUT
226 OUTBYTE(c->sub.lit)
227 c->mode = START;
228 break;
229 case WASH: /* o: got eob, possibly more output */
230 FLUSH
231 if (s->read != s->write)
232 LEAVE
233 c->mode = END;
234 case END:
235 r = Z_STREAM_END;
236 LEAVE
237 case BADCODE: /* x: got error */
238 r = Z_DATA_ERROR;
239 LEAVE
240 default:
241 r = Z_STREAM_ERROR;
242 LEAVE
243 }
244 #ifdef NEED_DUMMY_RETURN
245 return Z_STREAM_ERROR; /* Some dumb compilers complain without this */
246 #endif
247 }
248
249 #if defined(__VISAGECPP__) /* Visualage can't handle this antiquated interface */
250 void inflate_codes_free(inflate_codes_statef* c, z_streamp z)
251 #else
252 void inflate_codes_free(c, z)
253 inflate_codes_statef *c;
254 z_streamp z;
255 #endif
256 {
257 ZFREE(z, c);
258 Tracev((stderr, "inflate: codes free\n"));
259 }