Debian Codegraph @

ext/zlib-ng/infback.c

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