Chris@4
|
1 /* infback9.c -- inflate deflate64 data using a call-back interface
|
Chris@4
|
2 * Copyright (C) 1995-2008 Mark Adler
|
Chris@4
|
3 * For conditions of distribution and use, see copyright notice in zlib.h
|
Chris@4
|
4 */
|
Chris@4
|
5
|
Chris@4
|
6 #include "zutil.h"
|
Chris@4
|
7 #include "infback9.h"
|
Chris@4
|
8 #include "inftree9.h"
|
Chris@4
|
9 #include "inflate9.h"
|
Chris@4
|
10
|
Chris@4
|
11 #define WSIZE 65536UL
|
Chris@4
|
12
|
Chris@4
|
13 /*
|
Chris@4
|
14 strm provides memory allocation functions in zalloc and zfree, or
|
Chris@4
|
15 Z_NULL to use the library memory allocation functions.
|
Chris@4
|
16
|
Chris@4
|
17 window is a user-supplied window and output buffer that is 64K bytes.
|
Chris@4
|
18 */
|
Chris@4
|
19 int ZEXPORT inflateBack9Init_(strm, window, version, stream_size)
|
Chris@4
|
20 z_stream FAR *strm;
|
Chris@4
|
21 unsigned char FAR *window;
|
Chris@4
|
22 const char *version;
|
Chris@4
|
23 int stream_size;
|
Chris@4
|
24 {
|
Chris@4
|
25 struct inflate_state FAR *state;
|
Chris@4
|
26
|
Chris@4
|
27 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
|
Chris@4
|
28 stream_size != (int)(sizeof(z_stream)))
|
Chris@4
|
29 return Z_VERSION_ERROR;
|
Chris@4
|
30 if (strm == Z_NULL || window == Z_NULL)
|
Chris@4
|
31 return Z_STREAM_ERROR;
|
Chris@4
|
32 strm->msg = Z_NULL; /* in case we return an error */
|
Chris@4
|
33 if (strm->zalloc == (alloc_func)0) {
|
Chris@4
|
34 strm->zalloc = zcalloc;
|
Chris@4
|
35 strm->opaque = (voidpf)0;
|
Chris@4
|
36 }
|
Chris@4
|
37 if (strm->zfree == (free_func)0) strm->zfree = zcfree;
|
Chris@4
|
38 state = (struct inflate_state FAR *)ZALLOC(strm, 1,
|
Chris@4
|
39 sizeof(struct inflate_state));
|
Chris@4
|
40 if (state == Z_NULL) return Z_MEM_ERROR;
|
Chris@4
|
41 Tracev((stderr, "inflate: allocated\n"));
|
Chris@4
|
42 strm->state = (voidpf)state;
|
Chris@4
|
43 state->window = window;
|
Chris@4
|
44 return Z_OK;
|
Chris@4
|
45 }
|
Chris@4
|
46
|
Chris@4
|
47 /*
|
Chris@4
|
48 Build and output length and distance decoding tables for fixed code
|
Chris@4
|
49 decoding.
|
Chris@4
|
50 */
|
Chris@4
|
51 #ifdef MAKEFIXED
|
Chris@4
|
52 #include <stdio.h>
|
Chris@4
|
53
|
Chris@4
|
54 void makefixed9(void)
|
Chris@4
|
55 {
|
Chris@4
|
56 unsigned sym, bits, low, size;
|
Chris@4
|
57 code *next, *lenfix, *distfix;
|
Chris@4
|
58 struct inflate_state state;
|
Chris@4
|
59 code fixed[544];
|
Chris@4
|
60
|
Chris@4
|
61 /* literal/length table */
|
Chris@4
|
62 sym = 0;
|
Chris@4
|
63 while (sym < 144) state.lens[sym++] = 8;
|
Chris@4
|
64 while (sym < 256) state.lens[sym++] = 9;
|
Chris@4
|
65 while (sym < 280) state.lens[sym++] = 7;
|
Chris@4
|
66 while (sym < 288) state.lens[sym++] = 8;
|
Chris@4
|
67 next = fixed;
|
Chris@4
|
68 lenfix = next;
|
Chris@4
|
69 bits = 9;
|
Chris@4
|
70 inflate_table9(LENS, state.lens, 288, &(next), &(bits), state.work);
|
Chris@4
|
71
|
Chris@4
|
72 /* distance table */
|
Chris@4
|
73 sym = 0;
|
Chris@4
|
74 while (sym < 32) state.lens[sym++] = 5;
|
Chris@4
|
75 distfix = next;
|
Chris@4
|
76 bits = 5;
|
Chris@4
|
77 inflate_table9(DISTS, state.lens, 32, &(next), &(bits), state.work);
|
Chris@4
|
78
|
Chris@4
|
79 /* write tables */
|
Chris@4
|
80 puts(" /* inffix9.h -- table for decoding deflate64 fixed codes");
|
Chris@4
|
81 puts(" * Generated automatically by makefixed9().");
|
Chris@4
|
82 puts(" */");
|
Chris@4
|
83 puts("");
|
Chris@4
|
84 puts(" /* WARNING: this file should *not* be used by applications.");
|
Chris@4
|
85 puts(" It is part of the implementation of this library and is");
|
Chris@4
|
86 puts(" subject to change. Applications should only use zlib.h.");
|
Chris@4
|
87 puts(" */");
|
Chris@4
|
88 puts("");
|
Chris@4
|
89 size = 1U << 9;
|
Chris@4
|
90 printf(" static const code lenfix[%u] = {", size);
|
Chris@4
|
91 low = 0;
|
Chris@4
|
92 for (;;) {
|
Chris@4
|
93 if ((low % 6) == 0) printf("\n ");
|
Chris@4
|
94 printf("{%u,%u,%d}", lenfix[low].op, lenfix[low].bits,
|
Chris@4
|
95 lenfix[low].val);
|
Chris@4
|
96 if (++low == size) break;
|
Chris@4
|
97 putchar(',');
|
Chris@4
|
98 }
|
Chris@4
|
99 puts("\n };");
|
Chris@4
|
100 size = 1U << 5;
|
Chris@4
|
101 printf("\n static const code distfix[%u] = {", size);
|
Chris@4
|
102 low = 0;
|
Chris@4
|
103 for (;;) {
|
Chris@4
|
104 if ((low % 5) == 0) printf("\n ");
|
Chris@4
|
105 printf("{%u,%u,%d}", distfix[low].op, distfix[low].bits,
|
Chris@4
|
106 distfix[low].val);
|
Chris@4
|
107 if (++low == size) break;
|
Chris@4
|
108 putchar(',');
|
Chris@4
|
109 }
|
Chris@4
|
110 puts("\n };");
|
Chris@4
|
111 }
|
Chris@4
|
112 #endif /* MAKEFIXED */
|
Chris@4
|
113
|
Chris@4
|
114 /* Macros for inflateBack(): */
|
Chris@4
|
115
|
Chris@4
|
116 /* Clear the input bit accumulator */
|
Chris@4
|
117 #define INITBITS() \
|
Chris@4
|
118 do { \
|
Chris@4
|
119 hold = 0; \
|
Chris@4
|
120 bits = 0; \
|
Chris@4
|
121 } while (0)
|
Chris@4
|
122
|
Chris@4
|
123 /* Assure that some input is available. If input is requested, but denied,
|
Chris@4
|
124 then return a Z_BUF_ERROR from inflateBack(). */
|
Chris@4
|
125 #define PULL() \
|
Chris@4
|
126 do { \
|
Chris@4
|
127 if (have == 0) { \
|
Chris@4
|
128 have = in(in_desc, &next); \
|
Chris@4
|
129 if (have == 0) { \
|
Chris@4
|
130 next = Z_NULL; \
|
Chris@4
|
131 ret = Z_BUF_ERROR; \
|
Chris@4
|
132 goto inf_leave; \
|
Chris@4
|
133 } \
|
Chris@4
|
134 } \
|
Chris@4
|
135 } while (0)
|
Chris@4
|
136
|
Chris@4
|
137 /* Get a byte of input into the bit accumulator, or return from inflateBack()
|
Chris@4
|
138 with an error if there is no input available. */
|
Chris@4
|
139 #define PULLBYTE() \
|
Chris@4
|
140 do { \
|
Chris@4
|
141 PULL(); \
|
Chris@4
|
142 have--; \
|
Chris@4
|
143 hold += (unsigned long)(*next++) << bits; \
|
Chris@4
|
144 bits += 8; \
|
Chris@4
|
145 } while (0)
|
Chris@4
|
146
|
Chris@4
|
147 /* Assure that there are at least n bits in the bit accumulator. If there is
|
Chris@4
|
148 not enough available input to do that, then return from inflateBack() with
|
Chris@4
|
149 an error. */
|
Chris@4
|
150 #define NEEDBITS(n) \
|
Chris@4
|
151 do { \
|
Chris@4
|
152 while (bits < (unsigned)(n)) \
|
Chris@4
|
153 PULLBYTE(); \
|
Chris@4
|
154 } while (0)
|
Chris@4
|
155
|
Chris@4
|
156 /* Return the low n bits of the bit accumulator (n <= 16) */
|
Chris@4
|
157 #define BITS(n) \
|
Chris@4
|
158 ((unsigned)hold & ((1U << (n)) - 1))
|
Chris@4
|
159
|
Chris@4
|
160 /* Remove n bits from the bit accumulator */
|
Chris@4
|
161 #define DROPBITS(n) \
|
Chris@4
|
162 do { \
|
Chris@4
|
163 hold >>= (n); \
|
Chris@4
|
164 bits -= (unsigned)(n); \
|
Chris@4
|
165 } while (0)
|
Chris@4
|
166
|
Chris@4
|
167 /* Remove zero to seven bits as needed to go to a byte boundary */
|
Chris@4
|
168 #define BYTEBITS() \
|
Chris@4
|
169 do { \
|
Chris@4
|
170 hold >>= bits & 7; \
|
Chris@4
|
171 bits -= bits & 7; \
|
Chris@4
|
172 } while (0)
|
Chris@4
|
173
|
Chris@4
|
174 /* Assure that some output space is available, by writing out the window
|
Chris@4
|
175 if it's full. If the write fails, return from inflateBack() with a
|
Chris@4
|
176 Z_BUF_ERROR. */
|
Chris@4
|
177 #define ROOM() \
|
Chris@4
|
178 do { \
|
Chris@4
|
179 if (left == 0) { \
|
Chris@4
|
180 put = window; \
|
Chris@4
|
181 left = WSIZE; \
|
Chris@4
|
182 wrap = 1; \
|
Chris@4
|
183 if (out(out_desc, put, (unsigned)left)) { \
|
Chris@4
|
184 ret = Z_BUF_ERROR; \
|
Chris@4
|
185 goto inf_leave; \
|
Chris@4
|
186 } \
|
Chris@4
|
187 } \
|
Chris@4
|
188 } while (0)
|
Chris@4
|
189
|
Chris@4
|
190 /*
|
Chris@4
|
191 strm provides the memory allocation functions and window buffer on input,
|
Chris@4
|
192 and provides information on the unused input on return. For Z_DATA_ERROR
|
Chris@4
|
193 returns, strm will also provide an error message.
|
Chris@4
|
194
|
Chris@4
|
195 in() and out() are the call-back input and output functions. When
|
Chris@4
|
196 inflateBack() needs more input, it calls in(). When inflateBack() has
|
Chris@4
|
197 filled the window with output, or when it completes with data in the
|
Chris@4
|
198 window, it calls out() to write out the data. The application must not
|
Chris@4
|
199 change the provided input until in() is called again or inflateBack()
|
Chris@4
|
200 returns. The application must not change the window/output buffer until
|
Chris@4
|
201 inflateBack() returns.
|
Chris@4
|
202
|
Chris@4
|
203 in() and out() are called with a descriptor parameter provided in the
|
Chris@4
|
204 inflateBack() call. This parameter can be a structure that provides the
|
Chris@4
|
205 information required to do the read or write, as well as accumulated
|
Chris@4
|
206 information on the input and output such as totals and check values.
|
Chris@4
|
207
|
Chris@4
|
208 in() should return zero on failure. out() should return non-zero on
|
Chris@4
|
209 failure. If either in() or out() fails, than inflateBack() returns a
|
Chris@4
|
210 Z_BUF_ERROR. strm->next_in can be checked for Z_NULL to see whether it
|
Chris@4
|
211 was in() or out() that caused in the error. Otherwise, inflateBack()
|
Chris@4
|
212 returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format
|
Chris@4
|
213 error, or Z_MEM_ERROR if it could not allocate memory for the state.
|
Chris@4
|
214 inflateBack() can also return Z_STREAM_ERROR if the input parameters
|
Chris@4
|
215 are not correct, i.e. strm is Z_NULL or the state was not initialized.
|
Chris@4
|
216 */
|
Chris@4
|
217 int ZEXPORT inflateBack9(strm, in, in_desc, out, out_desc)
|
Chris@4
|
218 z_stream FAR *strm;
|
Chris@4
|
219 in_func in;
|
Chris@4
|
220 void FAR *in_desc;
|
Chris@4
|
221 out_func out;
|
Chris@4
|
222 void FAR *out_desc;
|
Chris@4
|
223 {
|
Chris@4
|
224 struct inflate_state FAR *state;
|
Chris@4
|
225 unsigned char FAR *next; /* next input */
|
Chris@4
|
226 unsigned char FAR *put; /* next output */
|
Chris@4
|
227 unsigned have; /* available input */
|
Chris@4
|
228 unsigned long left; /* available output */
|
Chris@4
|
229 inflate_mode mode; /* current inflate mode */
|
Chris@4
|
230 int lastblock; /* true if processing last block */
|
Chris@4
|
231 int wrap; /* true if the window has wrapped */
|
Chris@4
|
232 unsigned long write; /* window write index */
|
Chris@4
|
233 unsigned char FAR *window; /* allocated sliding window, if needed */
|
Chris@4
|
234 unsigned long hold; /* bit buffer */
|
Chris@4
|
235 unsigned bits; /* bits in bit buffer */
|
Chris@4
|
236 unsigned extra; /* extra bits needed */
|
Chris@4
|
237 unsigned long length; /* literal or length of data to copy */
|
Chris@4
|
238 unsigned long offset; /* distance back to copy string from */
|
Chris@4
|
239 unsigned long copy; /* number of stored or match bytes to copy */
|
Chris@4
|
240 unsigned char FAR *from; /* where to copy match bytes from */
|
Chris@4
|
241 code const FAR *lencode; /* starting table for length/literal codes */
|
Chris@4
|
242 code const FAR *distcode; /* starting table for distance codes */
|
Chris@4
|
243 unsigned lenbits; /* index bits for lencode */
|
Chris@4
|
244 unsigned distbits; /* index bits for distcode */
|
Chris@4
|
245 code here; /* current decoding table entry */
|
Chris@4
|
246 code last; /* parent table entry */
|
Chris@4
|
247 unsigned len; /* length to copy for repeats, bits to drop */
|
Chris@4
|
248 int ret; /* return code */
|
Chris@4
|
249 static const unsigned short order[19] = /* permutation of code lengths */
|
Chris@4
|
250 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
|
Chris@4
|
251 #include "inffix9.h"
|
Chris@4
|
252
|
Chris@4
|
253 /* Check that the strm exists and that the state was initialized */
|
Chris@4
|
254 if (strm == Z_NULL || strm->state == Z_NULL)
|
Chris@4
|
255 return Z_STREAM_ERROR;
|
Chris@4
|
256 state = (struct inflate_state FAR *)strm->state;
|
Chris@4
|
257
|
Chris@4
|
258 /* Reset the state */
|
Chris@4
|
259 strm->msg = Z_NULL;
|
Chris@4
|
260 mode = TYPE;
|
Chris@4
|
261 lastblock = 0;
|
Chris@4
|
262 write = 0;
|
Chris@4
|
263 wrap = 0;
|
Chris@4
|
264 window = state->window;
|
Chris@4
|
265 next = strm->next_in;
|
Chris@4
|
266 have = next != Z_NULL ? strm->avail_in : 0;
|
Chris@4
|
267 hold = 0;
|
Chris@4
|
268 bits = 0;
|
Chris@4
|
269 put = window;
|
Chris@4
|
270 left = WSIZE;
|
Chris@4
|
271 lencode = Z_NULL;
|
Chris@4
|
272 distcode = Z_NULL;
|
Chris@4
|
273
|
Chris@4
|
274 /* Inflate until end of block marked as last */
|
Chris@4
|
275 for (;;)
|
Chris@4
|
276 switch (mode) {
|
Chris@4
|
277 case TYPE:
|
Chris@4
|
278 /* determine and dispatch block type */
|
Chris@4
|
279 if (lastblock) {
|
Chris@4
|
280 BYTEBITS();
|
Chris@4
|
281 mode = DONE;
|
Chris@4
|
282 break;
|
Chris@4
|
283 }
|
Chris@4
|
284 NEEDBITS(3);
|
Chris@4
|
285 lastblock = BITS(1);
|
Chris@4
|
286 DROPBITS(1);
|
Chris@4
|
287 switch (BITS(2)) {
|
Chris@4
|
288 case 0: /* stored block */
|
Chris@4
|
289 Tracev((stderr, "inflate: stored block%s\n",
|
Chris@4
|
290 lastblock ? " (last)" : ""));
|
Chris@4
|
291 mode = STORED;
|
Chris@4
|
292 break;
|
Chris@4
|
293 case 1: /* fixed block */
|
Chris@4
|
294 lencode = lenfix;
|
Chris@4
|
295 lenbits = 9;
|
Chris@4
|
296 distcode = distfix;
|
Chris@4
|
297 distbits = 5;
|
Chris@4
|
298 Tracev((stderr, "inflate: fixed codes block%s\n",
|
Chris@4
|
299 lastblock ? " (last)" : ""));
|
Chris@4
|
300 mode = LEN; /* decode codes */
|
Chris@4
|
301 break;
|
Chris@4
|
302 case 2: /* dynamic block */
|
Chris@4
|
303 Tracev((stderr, "inflate: dynamic codes block%s\n",
|
Chris@4
|
304 lastblock ? " (last)" : ""));
|
Chris@4
|
305 mode = TABLE;
|
Chris@4
|
306 break;
|
Chris@4
|
307 case 3:
|
Chris@4
|
308 strm->msg = (char *)"invalid block type";
|
Chris@4
|
309 mode = BAD;
|
Chris@4
|
310 }
|
Chris@4
|
311 DROPBITS(2);
|
Chris@4
|
312 break;
|
Chris@4
|
313
|
Chris@4
|
314 case STORED:
|
Chris@4
|
315 /* get and verify stored block length */
|
Chris@4
|
316 BYTEBITS(); /* go to byte boundary */
|
Chris@4
|
317 NEEDBITS(32);
|
Chris@4
|
318 if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
|
Chris@4
|
319 strm->msg = (char *)"invalid stored block lengths";
|
Chris@4
|
320 mode = BAD;
|
Chris@4
|
321 break;
|
Chris@4
|
322 }
|
Chris@4
|
323 length = (unsigned)hold & 0xffff;
|
Chris@4
|
324 Tracev((stderr, "inflate: stored length %lu\n",
|
Chris@4
|
325 length));
|
Chris@4
|
326 INITBITS();
|
Chris@4
|
327
|
Chris@4
|
328 /* copy stored block from input to output */
|
Chris@4
|
329 while (length != 0) {
|
Chris@4
|
330 copy = length;
|
Chris@4
|
331 PULL();
|
Chris@4
|
332 ROOM();
|
Chris@4
|
333 if (copy > have) copy = have;
|
Chris@4
|
334 if (copy > left) copy = left;
|
Chris@4
|
335 zmemcpy(put, next, copy);
|
Chris@4
|
336 have -= copy;
|
Chris@4
|
337 next += copy;
|
Chris@4
|
338 left -= copy;
|
Chris@4
|
339 put += copy;
|
Chris@4
|
340 length -= copy;
|
Chris@4
|
341 }
|
Chris@4
|
342 Tracev((stderr, "inflate: stored end\n"));
|
Chris@4
|
343 mode = TYPE;
|
Chris@4
|
344 break;
|
Chris@4
|
345
|
Chris@4
|
346 case TABLE:
|
Chris@4
|
347 /* get dynamic table entries descriptor */
|
Chris@4
|
348 NEEDBITS(14);
|
Chris@4
|
349 state->nlen = BITS(5) + 257;
|
Chris@4
|
350 DROPBITS(5);
|
Chris@4
|
351 state->ndist = BITS(5) + 1;
|
Chris@4
|
352 DROPBITS(5);
|
Chris@4
|
353 state->ncode = BITS(4) + 4;
|
Chris@4
|
354 DROPBITS(4);
|
Chris@4
|
355 if (state->nlen > 286) {
|
Chris@4
|
356 strm->msg = (char *)"too many length symbols";
|
Chris@4
|
357 mode = BAD;
|
Chris@4
|
358 break;
|
Chris@4
|
359 }
|
Chris@4
|
360 Tracev((stderr, "inflate: table sizes ok\n"));
|
Chris@4
|
361
|
Chris@4
|
362 /* get code length code lengths (not a typo) */
|
Chris@4
|
363 state->have = 0;
|
Chris@4
|
364 while (state->have < state->ncode) {
|
Chris@4
|
365 NEEDBITS(3);
|
Chris@4
|
366 state->lens[order[state->have++]] = (unsigned short)BITS(3);
|
Chris@4
|
367 DROPBITS(3);
|
Chris@4
|
368 }
|
Chris@4
|
369 while (state->have < 19)
|
Chris@4
|
370 state->lens[order[state->have++]] = 0;
|
Chris@4
|
371 state->next = state->codes;
|
Chris@4
|
372 lencode = (code const FAR *)(state->next);
|
Chris@4
|
373 lenbits = 7;
|
Chris@4
|
374 ret = inflate_table9(CODES, state->lens, 19, &(state->next),
|
Chris@4
|
375 &(lenbits), state->work);
|
Chris@4
|
376 if (ret) {
|
Chris@4
|
377 strm->msg = (char *)"invalid code lengths set";
|
Chris@4
|
378 mode = BAD;
|
Chris@4
|
379 break;
|
Chris@4
|
380 }
|
Chris@4
|
381 Tracev((stderr, "inflate: code lengths ok\n"));
|
Chris@4
|
382
|
Chris@4
|
383 /* get length and distance code code lengths */
|
Chris@4
|
384 state->have = 0;
|
Chris@4
|
385 while (state->have < state->nlen + state->ndist) {
|
Chris@4
|
386 for (;;) {
|
Chris@4
|
387 here = lencode[BITS(lenbits)];
|
Chris@4
|
388 if ((unsigned)(here.bits) <= bits) break;
|
Chris@4
|
389 PULLBYTE();
|
Chris@4
|
390 }
|
Chris@4
|
391 if (here.val < 16) {
|
Chris@4
|
392 NEEDBITS(here.bits);
|
Chris@4
|
393 DROPBITS(here.bits);
|
Chris@4
|
394 state->lens[state->have++] = here.val;
|
Chris@4
|
395 }
|
Chris@4
|
396 else {
|
Chris@4
|
397 if (here.val == 16) {
|
Chris@4
|
398 NEEDBITS(here.bits + 2);
|
Chris@4
|
399 DROPBITS(here.bits);
|
Chris@4
|
400 if (state->have == 0) {
|
Chris@4
|
401 strm->msg = (char *)"invalid bit length repeat";
|
Chris@4
|
402 mode = BAD;
|
Chris@4
|
403 break;
|
Chris@4
|
404 }
|
Chris@4
|
405 len = (unsigned)(state->lens[state->have - 1]);
|
Chris@4
|
406 copy = 3 + BITS(2);
|
Chris@4
|
407 DROPBITS(2);
|
Chris@4
|
408 }
|
Chris@4
|
409 else if (here.val == 17) {
|
Chris@4
|
410 NEEDBITS(here.bits + 3);
|
Chris@4
|
411 DROPBITS(here.bits);
|
Chris@4
|
412 len = 0;
|
Chris@4
|
413 copy = 3 + BITS(3);
|
Chris@4
|
414 DROPBITS(3);
|
Chris@4
|
415 }
|
Chris@4
|
416 else {
|
Chris@4
|
417 NEEDBITS(here.bits + 7);
|
Chris@4
|
418 DROPBITS(here.bits);
|
Chris@4
|
419 len = 0;
|
Chris@4
|
420 copy = 11 + BITS(7);
|
Chris@4
|
421 DROPBITS(7);
|
Chris@4
|
422 }
|
Chris@4
|
423 if (state->have + copy > state->nlen + state->ndist) {
|
Chris@4
|
424 strm->msg = (char *)"invalid bit length repeat";
|
Chris@4
|
425 mode = BAD;
|
Chris@4
|
426 break;
|
Chris@4
|
427 }
|
Chris@4
|
428 while (copy--)
|
Chris@4
|
429 state->lens[state->have++] = (unsigned short)len;
|
Chris@4
|
430 }
|
Chris@4
|
431 }
|
Chris@4
|
432
|
Chris@4
|
433 /* handle error breaks in while */
|
Chris@4
|
434 if (mode == BAD) break;
|
Chris@4
|
435
|
Chris@4
|
436 /* check for end-of-block code (better have one) */
|
Chris@4
|
437 if (state->lens[256] == 0) {
|
Chris@4
|
438 strm->msg = (char *)"invalid code -- missing end-of-block";
|
Chris@4
|
439 mode = BAD;
|
Chris@4
|
440 break;
|
Chris@4
|
441 }
|
Chris@4
|
442
|
Chris@4
|
443 /* build code tables -- note: do not change the lenbits or distbits
|
Chris@4
|
444 values here (9 and 6) without reading the comments in inftree9.h
|
Chris@4
|
445 concerning the ENOUGH constants, which depend on those values */
|
Chris@4
|
446 state->next = state->codes;
|
Chris@4
|
447 lencode = (code const FAR *)(state->next);
|
Chris@4
|
448 lenbits = 9;
|
Chris@4
|
449 ret = inflate_table9(LENS, state->lens, state->nlen,
|
Chris@4
|
450 &(state->next), &(lenbits), state->work);
|
Chris@4
|
451 if (ret) {
|
Chris@4
|
452 strm->msg = (char *)"invalid literal/lengths set";
|
Chris@4
|
453 mode = BAD;
|
Chris@4
|
454 break;
|
Chris@4
|
455 }
|
Chris@4
|
456 distcode = (code const FAR *)(state->next);
|
Chris@4
|
457 distbits = 6;
|
Chris@4
|
458 ret = inflate_table9(DISTS, state->lens + state->nlen,
|
Chris@4
|
459 state->ndist, &(state->next), &(distbits),
|
Chris@4
|
460 state->work);
|
Chris@4
|
461 if (ret) {
|
Chris@4
|
462 strm->msg = (char *)"invalid distances set";
|
Chris@4
|
463 mode = BAD;
|
Chris@4
|
464 break;
|
Chris@4
|
465 }
|
Chris@4
|
466 Tracev((stderr, "inflate: codes ok\n"));
|
Chris@4
|
467 mode = LEN;
|
Chris@4
|
468
|
Chris@4
|
469 case LEN:
|
Chris@4
|
470 /* get a literal, length, or end-of-block code */
|
Chris@4
|
471 for (;;) {
|
Chris@4
|
472 here = lencode[BITS(lenbits)];
|
Chris@4
|
473 if ((unsigned)(here.bits) <= bits) break;
|
Chris@4
|
474 PULLBYTE();
|
Chris@4
|
475 }
|
Chris@4
|
476 if (here.op && (here.op & 0xf0) == 0) {
|
Chris@4
|
477 last = here;
|
Chris@4
|
478 for (;;) {
|
Chris@4
|
479 here = lencode[last.val +
|
Chris@4
|
480 (BITS(last.bits + last.op) >> last.bits)];
|
Chris@4
|
481 if ((unsigned)(last.bits + here.bits) <= bits) break;
|
Chris@4
|
482 PULLBYTE();
|
Chris@4
|
483 }
|
Chris@4
|
484 DROPBITS(last.bits);
|
Chris@4
|
485 }
|
Chris@4
|
486 DROPBITS(here.bits);
|
Chris@4
|
487 length = (unsigned)here.val;
|
Chris@4
|
488
|
Chris@4
|
489 /* process literal */
|
Chris@4
|
490 if (here.op == 0) {
|
Chris@4
|
491 Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
|
Chris@4
|
492 "inflate: literal '%c'\n" :
|
Chris@4
|
493 "inflate: literal 0x%02x\n", here.val));
|
Chris@4
|
494 ROOM();
|
Chris@4
|
495 *put++ = (unsigned char)(length);
|
Chris@4
|
496 left--;
|
Chris@4
|
497 mode = LEN;
|
Chris@4
|
498 break;
|
Chris@4
|
499 }
|
Chris@4
|
500
|
Chris@4
|
501 /* process end of block */
|
Chris@4
|
502 if (here.op & 32) {
|
Chris@4
|
503 Tracevv((stderr, "inflate: end of block\n"));
|
Chris@4
|
504 mode = TYPE;
|
Chris@4
|
505 break;
|
Chris@4
|
506 }
|
Chris@4
|
507
|
Chris@4
|
508 /* invalid code */
|
Chris@4
|
509 if (here.op & 64) {
|
Chris@4
|
510 strm->msg = (char *)"invalid literal/length code";
|
Chris@4
|
511 mode = BAD;
|
Chris@4
|
512 break;
|
Chris@4
|
513 }
|
Chris@4
|
514
|
Chris@4
|
515 /* length code -- get extra bits, if any */
|
Chris@4
|
516 extra = (unsigned)(here.op) & 31;
|
Chris@4
|
517 if (extra != 0) {
|
Chris@4
|
518 NEEDBITS(extra);
|
Chris@4
|
519 length += BITS(extra);
|
Chris@4
|
520 DROPBITS(extra);
|
Chris@4
|
521 }
|
Chris@4
|
522 Tracevv((stderr, "inflate: length %lu\n", length));
|
Chris@4
|
523
|
Chris@4
|
524 /* get distance code */
|
Chris@4
|
525 for (;;) {
|
Chris@4
|
526 here = distcode[BITS(distbits)];
|
Chris@4
|
527 if ((unsigned)(here.bits) <= bits) break;
|
Chris@4
|
528 PULLBYTE();
|
Chris@4
|
529 }
|
Chris@4
|
530 if ((here.op & 0xf0) == 0) {
|
Chris@4
|
531 last = here;
|
Chris@4
|
532 for (;;) {
|
Chris@4
|
533 here = distcode[last.val +
|
Chris@4
|
534 (BITS(last.bits + last.op) >> last.bits)];
|
Chris@4
|
535 if ((unsigned)(last.bits + here.bits) <= bits) break;
|
Chris@4
|
536 PULLBYTE();
|
Chris@4
|
537 }
|
Chris@4
|
538 DROPBITS(last.bits);
|
Chris@4
|
539 }
|
Chris@4
|
540 DROPBITS(here.bits);
|
Chris@4
|
541 if (here.op & 64) {
|
Chris@4
|
542 strm->msg = (char *)"invalid distance code";
|
Chris@4
|
543 mode = BAD;
|
Chris@4
|
544 break;
|
Chris@4
|
545 }
|
Chris@4
|
546 offset = (unsigned)here.val;
|
Chris@4
|
547
|
Chris@4
|
548 /* get distance extra bits, if any */
|
Chris@4
|
549 extra = (unsigned)(here.op) & 15;
|
Chris@4
|
550 if (extra != 0) {
|
Chris@4
|
551 NEEDBITS(extra);
|
Chris@4
|
552 offset += BITS(extra);
|
Chris@4
|
553 DROPBITS(extra);
|
Chris@4
|
554 }
|
Chris@4
|
555 if (offset > WSIZE - (wrap ? 0: left)) {
|
Chris@4
|
556 strm->msg = (char *)"invalid distance too far back";
|
Chris@4
|
557 mode = BAD;
|
Chris@4
|
558 break;
|
Chris@4
|
559 }
|
Chris@4
|
560 Tracevv((stderr, "inflate: distance %lu\n", offset));
|
Chris@4
|
561
|
Chris@4
|
562 /* copy match from window to output */
|
Chris@4
|
563 do {
|
Chris@4
|
564 ROOM();
|
Chris@4
|
565 copy = WSIZE - offset;
|
Chris@4
|
566 if (copy < left) {
|
Chris@4
|
567 from = put + copy;
|
Chris@4
|
568 copy = left - copy;
|
Chris@4
|
569 }
|
Chris@4
|
570 else {
|
Chris@4
|
571 from = put - offset;
|
Chris@4
|
572 copy = left;
|
Chris@4
|
573 }
|
Chris@4
|
574 if (copy > length) copy = length;
|
Chris@4
|
575 length -= copy;
|
Chris@4
|
576 left -= copy;
|
Chris@4
|
577 do {
|
Chris@4
|
578 *put++ = *from++;
|
Chris@4
|
579 } while (--copy);
|
Chris@4
|
580 } while (length != 0);
|
Chris@4
|
581 break;
|
Chris@4
|
582
|
Chris@4
|
583 case DONE:
|
Chris@4
|
584 /* inflate stream terminated properly -- write leftover output */
|
Chris@4
|
585 ret = Z_STREAM_END;
|
Chris@4
|
586 if (left < WSIZE) {
|
Chris@4
|
587 if (out(out_desc, window, (unsigned)(WSIZE - left)))
|
Chris@4
|
588 ret = Z_BUF_ERROR;
|
Chris@4
|
589 }
|
Chris@4
|
590 goto inf_leave;
|
Chris@4
|
591
|
Chris@4
|
592 case BAD:
|
Chris@4
|
593 ret = Z_DATA_ERROR;
|
Chris@4
|
594 goto inf_leave;
|
Chris@4
|
595
|
Chris@4
|
596 default: /* can't happen, but makes compilers happy */
|
Chris@4
|
597 ret = Z_STREAM_ERROR;
|
Chris@4
|
598 goto inf_leave;
|
Chris@4
|
599 }
|
Chris@4
|
600
|
Chris@4
|
601 /* Return unused input */
|
Chris@4
|
602 inf_leave:
|
Chris@4
|
603 strm->next_in = next;
|
Chris@4
|
604 strm->avail_in = have;
|
Chris@4
|
605 return ret;
|
Chris@4
|
606 }
|
Chris@4
|
607
|
Chris@4
|
608 int ZEXPORT inflateBack9End(strm)
|
Chris@4
|
609 z_stream FAR *strm;
|
Chris@4
|
610 {
|
Chris@4
|
611 if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
|
Chris@4
|
612 return Z_STREAM_ERROR;
|
Chris@4
|
613 ZFREE(strm, strm->state);
|
Chris@4
|
614 strm->state = Z_NULL;
|
Chris@4
|
615 Tracev((stderr, "inflate: end\n"));
|
Chris@4
|
616 return Z_OK;
|
Chris@4
|
617 }
|