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