1/* infback.c -- inflate using a call-back interface
2 * Copyright (C) 1995-2016 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6/*
7 This code is largely copied from inflate.c. Normally either infback.o or
8 inflate.o would be linked into an application--not both. The interface
9 with inffast.c is retained so that optimized assembler-coded versions of
10 inflate_fast() can be used with either inflate.c or infback.c.
11 */
12
13#include "zbuild.h"
14#include "zutil.h"
15#include "inftrees.h"
16#include "inflate.h"
17#include "inffast.h"
18#include "inflate_p.h"
19
20/*
21 strm provides memory allocation functions in zalloc and zfree, or
22 NULL to use the library memory allocation functions.
23
24 windowBits is in the range 8..15, and window is a user-supplied
25 window and output buffer that is 2**windowBits bytes.
26 */
27int ZEXPORT PREFIX(inflateBackInit_)(PREFIX3(stream) *strm, int windowBits, unsigned char *window,
28 const char *version, int stream_size) {
29 struct inflate_state *state;
30
31 if (version == NULL || version[0] != PREFIX2(VERSION)[0] || stream_size != (int)(sizeof(PREFIX3(stream))))
32 return Z_VERSION_ERROR;
33 if (strm == NULL || window == NULL || windowBits < 8 || windowBits > 15)
34 return Z_STREAM_ERROR;
35 strm->msg = NULL; /* in case we return an error */
36 if (strm->zalloc == NULL) {
37 strm->zalloc = zng_calloc;
38 strm->opaque = NULL;
39 }
40 if (strm->zfree == NULL)
41 strm->zfree = zng_cfree;
42 state = (struct inflate_state *) ZALLOC(strm, 1, sizeof(struct inflate_state));
43 if (state == NULL)
44 return Z_MEM_ERROR;
45 Tracev((stderr, "inflate: allocated\n"));
46 strm->state = (struct internal_state *)state;
47 state->dmax = 32768U;
48 state->wbits = (unsigned int)windowBits;
49 state->wsize = 1U << windowBits;
50 state->window = window;
51 state->wnext = 0;
52 state->whave = 0;
53 return Z_OK;
54}
55
56/*
57 Private macros for inflateBack()
58 Look in inflate_p.h for macros shared with inflate()
59*/
60
61/* Assure that some input is available. If input is requested, but denied,
62 then return a Z_BUF_ERROR from inflateBack(). */
63#define PULL() \
64 do { \
65 if (have == 0) { \
66 have = in(in_desc, &next); \
67 if (have == 0) { \
68 next = NULL; \
69 ret = Z_BUF_ERROR; \
70 goto inf_leave; \
71 } \
72 } \
73 } while (0)
74
75/* Get a byte of input into the bit accumulator, or return from inflateBack()
76 with an error if there is no input available. */
77#define PULLBYTE() \
78 do { \
79 PULL(); \
80 have--; \
81 hold += (*next++ << bits); \
82 bits += 8; \
83 } while (0)
84
85/* Assure that some output space is available, by writing out the window
86 if it's full. If the write fails, return from inflateBack() with a
87 Z_BUF_ERROR. */
88#define ROOM() \
89 do { \
90 if (left == 0) { \
91 put = state->window; \
92 left = state->wsize; \
93 state->whave = left; \
94 if (out(out_desc, put, left)) { \
95 ret = Z_BUF_ERROR; \
96 goto inf_leave; \
97 } \
98 } \
99 } while (0)
100
101/*
102 strm provides the memory allocation functions and window buffer on input,
103 and provides information on the unused input on return. For Z_DATA_ERROR
104 returns, strm will also provide an error message.
105
106 in() and out() are the call-back input and output functions. When
107 inflateBack() needs more input, it calls in(). When inflateBack() has
108 filled the window with output, or when it completes with data in the
109 window, it calls out() to write out the data. The application must not
110 change the provided input until in() is called again or inflateBack()
111 returns. The application must not change the window/output buffer until
112 inflateBack() returns.
113
114 in() and out() are called with a descriptor parameter provided in the
115 inflateBack() call. This parameter can be a structure that provides the
116 information required to do the read or write, as well as accumulated
117 information on the input and output such as totals and check values.
118
119 in() should return zero on failure. out() should return non-zero on
120 failure. If either in() or out() fails, than inflateBack() returns a
121 Z_BUF_ERROR. strm->next_in can be checked for NULL to see whether it
122 was in() or out() that caused in the error. Otherwise, inflateBack()
123 returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format
124 error, or Z_MEM_ERROR if it could not allocate memory for the state.
125 inflateBack() can also return Z_STREAM_ERROR if the input parameters
126 are not correct, i.e. strm is NULL or the state was not initialized.
127 */
128int ZEXPORT PREFIX(inflateBack)(PREFIX3(stream) *strm, in_func in, void *in_desc, out_func out, void *out_desc) {
129 struct inflate_state *state;
130 const unsigned char *next; /* next input */
131 unsigned char *put; /* next output */
132 unsigned have, left; /* available input and output */
133 uint32_t hold; /* bit buffer */
134 unsigned bits; /* bits in bit buffer */
135 unsigned copy; /* number of stored or match bytes to copy */
136 unsigned char *from; /* where to copy match bytes from */
137 code here; /* current decoding table entry */
138 code last; /* parent table entry */
139 unsigned len; /* length to copy for repeats, bits to drop */
140 int ret; /* return code */
141 static const uint16_t order[19] = /* permutation of code lengths */
142 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
143
144 /* Check that the strm exists and that the state was initialized */
145 if (strm == NULL || strm->state == NULL)
146 return Z_STREAM_ERROR;
147 state = (struct inflate_state *)strm->state;
148
149 /* Reset the state */
150 strm->msg = NULL;
151 state->mode = TYPE;
152 state->last = 0;
153 state->whave = 0;
154 next = strm->next_in;
155 have = next != NULL ? strm->avail_in : 0;
156 hold = 0;
157 bits = 0;
158 put = state->window;
159 left = state->wsize;
160
161 /* Inflate until end of block marked as last */
162 for (;;)
163 switch (state->mode) {
164 case TYPE:
165 /* determine and dispatch block type */
166 if (state->last) {
167 BYTEBITS();
168 state->mode = DONE;
169 break;
170 }
171 NEEDBITS(3);
172 state->last = BITS(1);
173 DROPBITS(1);
174 switch (BITS(2)) {
175 case 0: /* stored block */
176 Tracev((stderr, "inflate: stored block%s\n", state->last ? " (last)" : ""));
177 state->mode = STORED;
178 break;
179 case 1: /* fixed block */
180 fixedtables(state);
181 Tracev((stderr, "inflate: fixed codes block%s\n", state->last ? " (last)" : ""));
182 state->mode = LEN; /* decode codes */
183 break;
184 case 2: /* dynamic block */
185 Tracev((stderr, "inflate: dynamic codes block%s\n", state->last ? " (last)" : ""));
186 state->mode = TABLE;
187 break;
188 case 3:
189 strm->msg = (char *)"invalid block type";
190 state->mode = BAD;
191 }
192 DROPBITS(2);
193 break;
194
195 case STORED:
196 /* get and verify stored block length */
197 BYTEBITS(); /* go to byte boundary */
198 NEEDBITS(32);
199 if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
200 strm->msg = (char *)"invalid stored block lengths";
201 state->mode = BAD;
202 break;
203 }
204 state->length = (uint16_t)hold;
205 Tracev((stderr, "inflate: stored length %u\n", state->length));
206 INITBITS();
207
208 /* copy stored block from input to output */
209 while (state->length != 0) {
210 copy = state->length;
211 PULL();
212 ROOM();
213 if (copy > have) copy = have;
214 if (copy > left) copy = left;
215 memcpy(put, next, copy);
216 have -= copy;
217 next += copy;
218 left -= copy;
219 put += copy;
220 state->length -= copy;
221 }
222 Tracev((stderr, "inflate: stored end\n"));
223 state->mode = TYPE;
224 break;
225
226 case TABLE:
227 /* get dynamic table entries descriptor */
228 NEEDBITS(14);
229 state->nlen = BITS(5) + 257;
230 DROPBITS(5);
231 state->ndist = BITS(5) + 1;
232 DROPBITS(5);
233 state->ncode = BITS(4) + 4;
234 DROPBITS(4);
235#ifndef PKZIP_BUG_WORKAROUND
236 if (state->nlen > 286 || state->ndist > 30) {
237 strm->msg = (char *)"too many length or distance symbols";
238 state->mode = BAD;
239 break;
240 }
241#endif
242 Tracev((stderr, "inflate: table sizes ok\n"));
243 state->have = 0;
244
245 /* get code length code lengths (not a typo) */
246 while (state->have < state->ncode) {
247 NEEDBITS(3);
248 state->lens[order[state->have++]] = (uint16_t)BITS(3);
249 DROPBITS(3);
250 }
251 while (state->have < 19)
252 state->lens[order[state->have++]] = 0;
253 state->next = state->codes;
254 state->lencode = (const code *)(state->next);
255 state->lenbits = 7;
256 ret = zng_inflate_table(CODES, state->lens, 19, &(state->next), &(state->lenbits), state->work);
257 if (ret) {
258 strm->msg = (char *)"invalid code lengths set";
259 state->mode = BAD;
260 break;
261 }
262 Tracev((stderr, "inflate: code lengths ok\n"));
263 state->have = 0;
264
265 /* get length and distance code code lengths */
266 while (state->have < state->nlen + state->ndist) {
267 for (;;) {
268 here = state->lencode[BITS(state->lenbits)];
269 if (here.bits <= bits) break;
270 PULLBYTE();
271 }
272 if (here.val < 16) {
273 DROPBITS(here.bits);
274 state->lens[state->have++] = here.val;
275 } else {
276 if (here.val == 16) {
277 NEEDBITS(here.bits + 2);
278 DROPBITS(here.bits);
279 if (state->have == 0) {
280 strm->msg = (char *)"invalid bit length repeat";
281 state->mode = BAD;
282 break;
283 }
284 len = state->lens[state->have - 1];
285 copy = 3 + BITS(2);
286 DROPBITS(2);
287 } else if (here.val == 17) {
288 NEEDBITS(here.bits + 3);
289 DROPBITS(here.bits);
290 len = 0;
291 copy = 3 + BITS(3);
292 DROPBITS(3);
293 } else {
294 NEEDBITS(here.bits + 7);
295 DROPBITS(here.bits);
296 len = 0;
297 copy = 11 + BITS(7);
298 DROPBITS(7);
299 }
300 if (state->have + copy > state->nlen + state->ndist) {
301 strm->msg = (char *)"invalid bit length repeat";
302 state->mode = BAD;
303 break;
304 }
305 while (copy) {
306 --copy;
307 state->lens[state->have++] = (uint16_t)len;
308 }
309 }
310 }
311
312 /* handle error breaks in while */
313 if (state->mode == BAD)
314 break;
315
316 /* check for end-of-block code (better have one) */
317 if (state->lens[256] == 0) {
318 strm->msg = (char *)"invalid code -- missing end-of-block";
319 state->mode = BAD;
320 break;
321 }
322
323 /* build code tables -- note: do not change the lenbits or distbits
324 values here (9 and 6) without reading the comments in inftrees.h
325 concerning the ENOUGH constants, which depend on those values */
326 state->next = state->codes;
327 state->lencode = (const code *)(state->next);
328 state->lenbits = 9;
329 ret = zng_inflate_table(LENS, state->lens, state->nlen, &(state->next), &(state->lenbits), state->work);
330 if (ret) {
331 strm->msg = (char *)"invalid literal/lengths set";
332 state->mode = BAD;
333 break;
334 }
335 state->distcode = (const code *)(state->next);
336 state->distbits = 6;
337 ret = zng_inflate_table(DISTS, state->lens + state->nlen, state->ndist,
338 &(state->next), &(state->distbits), state->work);
339 if (ret) {
340 strm->msg = (char *)"invalid distances set";
341 state->mode = BAD;
342 break;
343 }
344 Tracev((stderr, "inflate: codes ok\n"));
345 state->mode = LEN;
346
347 case LEN:
348 /* use inflate_fast() if we have enough input and output */
349 if (have >= INFLATE_FAST_MIN_HAVE &&
350 left >= INFLATE_FAST_MIN_LEFT) {
351 RESTORE();
352 if (state->whave < state->wsize)
353 state->whave = state->wsize - left;
354 zng_inflate_fast(strm, state->wsize);
355 LOAD();
356 break;
357 }
358
359 /* get a literal, length, or end-of-block code */
360 for (;;) {
361 here = state->lencode[BITS(state->lenbits)];
362 if (here.bits <= bits)
363 break;
364 PULLBYTE();
365 }
366 if (here.op && (here.op & 0xf0) == 0) {
367 last = here;
368 for (;;) {
369 here = state->lencode[last.val + (BITS(last.bits + last.op) >> last.bits)];
370 if ((unsigned)last.bits + (unsigned)here.bits <= bits)
371 break;
372 PULLBYTE();
373 }
374 DROPBITS(last.bits);
375 }
376 DROPBITS(here.bits);
377 state->length = here.val;
378
379 /* process literal */
380 if ((int)(here.op) == 0) {
381 Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
382 "inflate: literal '%c'\n" :
383 "inflate: literal 0x%02x\n", here.val));
384 ROOM();
385 *put++ = (unsigned char)(state->length);
386 left--;
387 state->mode = LEN;
388 break;
389 }
390
391 /* process end of block */
392 if (here.op & 32) {
393 Tracevv((stderr, "inflate: end of block\n"));
394 state->mode = TYPE;
395 break;
396 }
397
398 /* invalid code */
399 if (here.op & 64) {
400 strm->msg = (char *)"invalid literal/length code";
401 state->mode = BAD;
402 break;
403 }
404
405 /* length code -- get extra bits, if any */
406 state->extra = (here.op & 15);
407 if (state->extra) {
408 NEEDBITS(state->extra);
409 state->length += BITS(state->extra);
410 DROPBITS(state->extra);
411 }
412 Tracevv((stderr, "inflate: length %u\n", state->length));
413
414 /* get distance code */
415 for (;;) {
416 here = state->distcode[BITS(state->distbits)];
417 if (here.bits <= bits)
418 break;
419 PULLBYTE();
420 }
421 if ((here.op & 0xf0) == 0) {
422 last = here;
423 for (;;) {
424 here = state->distcode[last.val + (BITS(last.bits + last.op) >> last.bits)];
425 if ((unsigned)last.bits + (unsigned)here.bits <= bits)
426 break;
427 PULLBYTE();
428 }
429 DROPBITS(last.bits);
430 }
431 DROPBITS(here.bits);
432 if (here.op & 64) {
433 strm->msg = (char *)"invalid distance code";
434 state->mode = BAD;
435 break;
436 }
437 state->offset = here.val;
438 state->extra = (here.op & 15);
439
440 /* get distance extra bits, if any */
441 if (state->extra) {
442 NEEDBITS(state->extra);
443 state->offset += BITS(state->extra);
444 DROPBITS(state->extra);
445 }
446#ifdef INFLATE_STRICT
447 if (state->offset > state->wsize - (state->whave < state->wsize ? left : 0)) {
448 strm->msg = (char *)"invalid distance too far back";
449 state->mode = BAD;
450 break;
451 }
452#endif
453 Tracevv((stderr, "inflate: distance %u\n", state->offset));
454
455 /* copy match from window to output */
456 do {
457 ROOM();
458 copy = state->wsize - state->offset;
459 if (copy < left) {
460 from = put + copy;
461 copy = left - copy;
462 } else {
463 from = put - state->offset;
464 copy = left;
465 }
466 if (copy > state->length)
467 copy = state->length;
468 state->length -= copy;
469 left -= copy;
470 do {
471 *put++ = *from++;
472 } while (--copy);
473 } while (state->length != 0);
474 break;
475
476 case DONE:
477 /* inflate stream terminated properly -- write leftover output */
478 ret = Z_STREAM_END;
479 if (left < state->wsize) {
480 if (out(out_desc, state->window, state->wsize - left))
481 ret = Z_BUF_ERROR;
482 }
483 goto inf_leave;
484
485 case BAD:
486 ret = Z_DATA_ERROR;
487 goto inf_leave;
488
489 default: /* can't happen, but makes compilers happy */
490 ret = Z_STREAM_ERROR;
491 goto inf_leave;
492 }
493
494 /* Return unused input */
495 inf_leave:
496 strm->next_in = next;
497 strm->avail_in = have;
498 return ret;
499}
500
501int ZEXPORT PREFIX(inflateBackEnd)(PREFIX3(stream) *strm) {
502 if (strm == NULL || strm->state == NULL || strm->zfree == NULL)
503 return Z_STREAM_ERROR;
504 ZFREE(strm, strm->state);
505 strm->state = NULL;
506 Tracev((stderr, "inflate: end\n"));
507 return Z_OK;
508}
509