1
/* infback9.c -- inflate deflate64 data using a call-back interface
2
* Copyright (C) 1995-2008 Mark Adler
3
* For conditions of distribution and use, see copyright notice in zlib.h
14
strm provides memory allocation functions in zalloc and zfree, or
15
Z_NULL to use the library memory allocation functions.
17
window is a user-supplied window and output buffer that is 64K bytes.
19
int ZEXPORT inflateBack9Init_(strm, window, version, stream_size)
21
unsigned char FAR *window;
25
struct inflate_state FAR *state;
27
if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
28
stream_size != (int)(sizeof(z_stream)))
29
return Z_VERSION_ERROR;
30
if (strm == Z_NULL || window == Z_NULL)
31
return Z_STREAM_ERROR;
32
strm->msg = Z_NULL; /* in case we return an error */
33
if (strm->zalloc == (alloc_func)0) {
34
strm->zalloc = zcalloc;
35
strm->opaque = (voidpf)0;
37
if (strm->zfree == (free_func)0) strm->zfree = zcfree;
38
state = (struct inflate_state FAR *)ZALLOC(strm, 1,
39
sizeof(struct inflate_state));
40
if (state == Z_NULL) return Z_MEM_ERROR;
41
Tracev((stderr, "inflate: allocated\n"));
42
strm->state = (voidpf)state;
43
state->window = window;
48
Build and output length and distance decoding tables for fixed code
56
unsigned sym, bits, low, size;
57
code *next, *lenfix, *distfix;
58
struct inflate_state state;
61
/* literal/length table */
63
while (sym < 144) state.lens[sym++] = 8;
64
while (sym < 256) state.lens[sym++] = 9;
65
while (sym < 280) state.lens[sym++] = 7;
66
while (sym < 288) state.lens[sym++] = 8;
70
inflate_table9(LENS, state.lens, 288, &(next), &(bits), state.work);
74
while (sym < 32) state.lens[sym++] = 5;
77
inflate_table9(DISTS, state.lens, 32, &(next), &(bits), state.work);
80
puts(" /* inffix9.h -- table for decoding deflate64 fixed codes");
81
puts(" * Generated automatically by makefixed9().");
84
puts(" /* WARNING: this file should *not* be used by applications.");
85
puts(" It is part of the implementation of this library and is");
86
puts(" subject to change. Applications should only use zlib.h.");
90
printf(" static const code lenfix[%u] = {", size);
93
if ((low % 6) == 0) printf("\n ");
94
printf("{%u,%u,%d}", lenfix[low].op, lenfix[low].bits,
96
if (++low == size) break;
101
printf("\n static const code distfix[%u] = {", size);
104
if ((low % 5) == 0) printf("\n ");
105
printf("{%u,%u,%d}", distfix[low].op, distfix[low].bits,
107
if (++low == size) break;
112
#endif /* MAKEFIXED */
114
/* Macros for inflateBack(): */
116
/* Clear the input bit accumulator */
123
/* Assure that some input is available. If input is requested, but denied,
124
then return a Z_BUF_ERROR from inflateBack(). */
128
have = in(in_desc, &next); \
137
/* Get a byte of input into the bit accumulator, or return from inflateBack()
138
with an error if there is no input available. */
143
hold += (unsigned long)(*next++) << bits; \
147
/* Assure that there are at least n bits in the bit accumulator. If there is
148
not enough available input to do that, then return from inflateBack() with
150
#define NEEDBITS(n) \
152
while (bits < (unsigned)(n)) \
156
/* Return the low n bits of the bit accumulator (n <= 16) */
158
((unsigned)hold & ((1U << (n)) - 1))
160
/* Remove n bits from the bit accumulator */
161
#define DROPBITS(n) \
164
bits -= (unsigned)(n); \
167
/* Remove zero to seven bits as needed to go to a byte boundary */
174
/* Assure that some output space is available, by writing out the window
175
if it's full. If the write fails, return from inflateBack() with a
183
if (out(out_desc, put, (unsigned)left)) { \
191
strm provides the memory allocation functions and window buffer on input,
192
and provides information on the unused input on return. For Z_DATA_ERROR
193
returns, strm will also provide an error message.
195
in() and out() are the call-back input and output functions. When
196
inflateBack() needs more input, it calls in(). When inflateBack() has
197
filled the window with output, or when it completes with data in the
198
window, it calls out() to write out the data. The application must not
199
change the provided input until in() is called again or inflateBack()
200
returns. The application must not change the window/output buffer until
201
inflateBack() returns.
203
in() and out() are called with a descriptor parameter provided in the
204
inflateBack() call. This parameter can be a structure that provides the
205
information required to do the read or write, as well as accumulated
206
information on the input and output such as totals and check values.
208
in() should return zero on failure. out() should return non-zero on
209
failure. If either in() or out() fails, than inflateBack() returns a
210
Z_BUF_ERROR. strm->next_in can be checked for Z_NULL to see whether it
211
was in() or out() that caused in the error. Otherwise, inflateBack()
212
returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format
213
error, or Z_MEM_ERROR if it could not allocate memory for the state.
214
inflateBack() can also return Z_STREAM_ERROR if the input parameters
215
are not correct, i.e. strm is Z_NULL or the state was not initialized.
217
int ZEXPORT inflateBack9(strm, in, in_desc, out, out_desc)
224
struct inflate_state FAR *state;
225
unsigned char FAR *next; /* next input */
226
unsigned char FAR *put; /* next output */
227
unsigned have; /* available input */
228
unsigned long left; /* available output */
229
inflate_mode mode; /* current inflate mode */
230
int lastblock; /* true if processing last block */
231
int wrap; /* true if the window has wrapped */
232
unsigned long write; /* window write index */
233
unsigned char FAR *window; /* allocated sliding window, if needed */
234
unsigned long hold; /* bit buffer */
235
unsigned bits; /* bits in bit buffer */
236
unsigned extra; /* extra bits needed */
237
unsigned long length; /* literal or length of data to copy */
238
unsigned long offset; /* distance back to copy string from */
239
unsigned long copy; /* number of stored or match bytes to copy */
240
unsigned char FAR *from; /* where to copy match bytes from */
241
code const FAR *lencode; /* starting table for length/literal codes */
242
code const FAR *distcode; /* starting table for distance codes */
243
unsigned lenbits; /* index bits for lencode */
244
unsigned distbits; /* index bits for distcode */
245
code here; /* current decoding table entry */
246
code last; /* parent table entry */
247
unsigned len; /* length to copy for repeats, bits to drop */
248
int ret; /* return code */
249
static const unsigned short order[19] = /* permutation of code lengths */
250
{16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
253
/* Check that the strm exists and that the state was initialized */
254
if (strm == Z_NULL || strm->state == Z_NULL)
255
return Z_STREAM_ERROR;
256
state = (struct inflate_state FAR *)strm->state;
258
/* Reset the state */
264
window = state->window;
265
next = strm->next_in;
266
have = next != Z_NULL ? strm->avail_in : 0;
274
/* Inflate until end of block marked as last */
278
/* determine and dispatch block type */
288
case 0: /* stored block */
289
Tracev((stderr, "inflate: stored block%s\n",
290
lastblock ? " (last)" : ""));
293
case 1: /* fixed block */
298
Tracev((stderr, "inflate: fixed codes block%s\n",
299
lastblock ? " (last)" : ""));
300
mode = LEN; /* decode codes */
302
case 2: /* dynamic block */
303
Tracev((stderr, "inflate: dynamic codes block%s\n",
304
lastblock ? " (last)" : ""));
308
strm->msg = (char *)"invalid block type";
315
/* get and verify stored block length */
316
BYTEBITS(); /* go to byte boundary */
318
if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
319
strm->msg = (char *)"invalid stored block lengths";
323
length = (unsigned)hold & 0xffff;
324
Tracev((stderr, "inflate: stored length %lu\n",
328
/* copy stored block from input to output */
329
while (length != 0) {
333
if (copy > have) copy = have;
334
if (copy > left) copy = left;
335
zmemcpy(put, next, copy);
342
Tracev((stderr, "inflate: stored end\n"));
347
/* get dynamic table entries descriptor */
349
state->nlen = BITS(5) + 257;
351
state->ndist = BITS(5) + 1;
353
state->ncode = BITS(4) + 4;
355
if (state->nlen > 286) {
356
strm->msg = (char *)"too many length symbols";
360
Tracev((stderr, "inflate: table sizes ok\n"));
362
/* get code length code lengths (not a typo) */
364
while (state->have < state->ncode) {
366
state->lens[order[state->have++]] = (unsigned short)BITS(3);
369
while (state->have < 19)
370
state->lens[order[state->have++]] = 0;
371
state->next = state->codes;
372
lencode = (code const FAR *)(state->next);
374
ret = inflate_table9(CODES, state->lens, 19, &(state->next),
375
&(lenbits), state->work);
377
strm->msg = (char *)"invalid code lengths set";
381
Tracev((stderr, "inflate: code lengths ok\n"));
383
/* get length and distance code code lengths */
385
while (state->have < state->nlen + state->ndist) {
387
here = lencode[BITS(lenbits)];
388
if ((unsigned)(here.bits) <= bits) break;
394
state->lens[state->have++] = here.val;
397
if (here.val == 16) {
398
NEEDBITS(here.bits + 2);
400
if (state->have == 0) {
401
strm->msg = (char *)"invalid bit length repeat";
405
len = (unsigned)(state->lens[state->have - 1]);
409
else if (here.val == 17) {
410
NEEDBITS(here.bits + 3);
417
NEEDBITS(here.bits + 7);
423
if (state->have + copy > state->nlen + state->ndist) {
424
strm->msg = (char *)"invalid bit length repeat";
429
state->lens[state->have++] = (unsigned short)len;
433
/* handle error breaks in while */
434
if (mode == BAD) break;
436
/* check for end-of-block code (better have one) */
437
if (state->lens[256] == 0) {
438
strm->msg = (char *)"invalid code -- missing end-of-block";
443
/* build code tables -- note: do not change the lenbits or distbits
444
values here (9 and 6) without reading the comments in inftree9.h
445
concerning the ENOUGH constants, which depend on those values */
446
state->next = state->codes;
447
lencode = (code const FAR *)(state->next);
449
ret = inflate_table9(LENS, state->lens, state->nlen,
450
&(state->next), &(lenbits), state->work);
452
strm->msg = (char *)"invalid literal/lengths set";
456
distcode = (code const FAR *)(state->next);
458
ret = inflate_table9(DISTS, state->lens + state->nlen,
459
state->ndist, &(state->next), &(distbits),
462
strm->msg = (char *)"invalid distances set";
466
Tracev((stderr, "inflate: codes ok\n"));
470
/* get a literal, length, or end-of-block code */
472
here = lencode[BITS(lenbits)];
473
if ((unsigned)(here.bits) <= bits) break;
476
if (here.op && (here.op & 0xf0) == 0) {
479
here = lencode[last.val +
480
(BITS(last.bits + last.op) >> last.bits)];
481
if ((unsigned)(last.bits + here.bits) <= bits) break;
487
length = (unsigned)here.val;
489
/* process literal */
491
Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
492
"inflate: literal '%c'\n" :
493
"inflate: literal 0x%02x\n", here.val));
495
*put++ = (unsigned char)(length);
501
/* process end of block */
503
Tracevv((stderr, "inflate: end of block\n"));
510
strm->msg = (char *)"invalid literal/length code";
515
/* length code -- get extra bits, if any */
516
extra = (unsigned)(here.op) & 31;
519
length += BITS(extra);
522
Tracevv((stderr, "inflate: length %lu\n", length));
524
/* get distance code */
526
here = distcode[BITS(distbits)];
527
if ((unsigned)(here.bits) <= bits) break;
530
if ((here.op & 0xf0) == 0) {
533
here = distcode[last.val +
534
(BITS(last.bits + last.op) >> last.bits)];
535
if ((unsigned)(last.bits + here.bits) <= bits) break;
542
strm->msg = (char *)"invalid distance code";
546
offset = (unsigned)here.val;
548
/* get distance extra bits, if any */
549
extra = (unsigned)(here.op) & 15;
552
offset += BITS(extra);
555
if (offset > WSIZE - (wrap ? 0: left)) {
556
strm->msg = (char *)"invalid distance too far back";
560
Tracevv((stderr, "inflate: distance %lu\n", offset));
562
/* copy match from window to output */
565
copy = WSIZE - offset;
574
if (copy > length) copy = length;
580
} while (length != 0);
584
/* inflate stream terminated properly -- write leftover output */
587
if (out(out_desc, window, (unsigned)(WSIZE - left)))
596
default: /* can't happen, but makes compilers happy */
597
ret = Z_STREAM_ERROR;
601
/* Return unused input */
603
strm->next_in = next;
604
strm->avail_in = have;
608
int ZEXPORT inflateBack9End(strm)
611
if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
612
return Z_STREAM_ERROR;
613
ZFREE(strm, strm->state);
614
strm->state = Z_NULL;
615
Tracev((stderr, "inflate: end\n"));