ยปCore Development>Code coverage>Modules/zlib/infback.c

Python code coverage for Modules/zlib/infback.c

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