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