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