Path: blob/master/src/java.base/share/native/libzip/zlib/infback.c
41153 views
/*1* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation. Oracle designates this6* particular file as subject to the "Classpath" exception as provided7* by Oracle in the LICENSE file that accompanied this code.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324/* infback.c -- inflate using a call-back interface25* Copyright (C) 1995-2016 Mark Adler26* For conditions of distribution and use, see copyright notice in zlib.h27*/2829/*30This code is largely copied from inflate.c. Normally either infback.o or31inflate.o would be linked into an application--not both. The interface32with inffast.c is retained so that optimized assembler-coded versions of33inflate_fast() can be used with either inflate.c or infback.c.34*/3536#include "zutil.h"37#include "inftrees.h"38#include "inflate.h"39#include "inffast.h"4041/* function prototypes */42local void fixedtables OF((struct inflate_state FAR *state));4344/*45strm provides memory allocation functions in zalloc and zfree, or46Z_NULL to use the library memory allocation functions.4748windowBits is in the range 8..15, and window is a user-supplied49window and output buffer that is 2**windowBits bytes.50*/51int ZEXPORT inflateBackInit_(strm, windowBits, window, version, stream_size)52z_streamp strm;53int windowBits;54unsigned char FAR *window;55const char *version;56int stream_size;57{58struct inflate_state FAR *state;5960if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||61stream_size != (int)(sizeof(z_stream)))62return Z_VERSION_ERROR;63if (strm == Z_NULL || window == Z_NULL ||64windowBits < 8 || windowBits > 15)65return Z_STREAM_ERROR;66strm->msg = Z_NULL; /* in case we return an error */67if (strm->zalloc == (alloc_func)0) {68#ifdef Z_SOLO69return Z_STREAM_ERROR;70#else71strm->zalloc = zcalloc;72strm->opaque = (voidpf)0;73#endif74}75if (strm->zfree == (free_func)0)76#ifdef Z_SOLO77return Z_STREAM_ERROR;78#else79strm->zfree = zcfree;80#endif81state = (struct inflate_state FAR *)ZALLOC(strm, 1,82sizeof(struct inflate_state));83if (state == Z_NULL) return Z_MEM_ERROR;84Tracev((stderr, "inflate: allocated\n"));85strm->state = (struct internal_state FAR *)state;86state->dmax = 32768U;87state->wbits = (uInt)windowBits;88state->wsize = 1U << windowBits;89state->window = window;90state->wnext = 0;91state->whave = 0;92return Z_OK;93}9495/*96Return state with length and distance decoding tables and index sizes set to97fixed code decoding. Normally this returns fixed tables from inffixed.h.98If BUILDFIXED is defined, then instead this routine builds the tables the99first time it's called, and returns those tables the first time and100thereafter. This reduces the size of the code by about 2K bytes, in101exchange for a little execution time. However, BUILDFIXED should not be102used for threaded applications, since the rewriting of the tables and virgin103may not be thread-safe.104*/105local void fixedtables(state)106struct inflate_state FAR *state;107{108#ifdef BUILDFIXED109static int virgin = 1;110static code *lenfix, *distfix;111static code fixed[544];112113/* build fixed huffman tables if first call (may not be thread safe) */114if (virgin) {115unsigned sym, bits;116static code *next;117118/* literal/length table */119sym = 0;120while (sym < 144) state->lens[sym++] = 8;121while (sym < 256) state->lens[sym++] = 9;122while (sym < 280) state->lens[sym++] = 7;123while (sym < 288) state->lens[sym++] = 8;124next = fixed;125lenfix = next;126bits = 9;127inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);128129/* distance table */130sym = 0;131while (sym < 32) state->lens[sym++] = 5;132distfix = next;133bits = 5;134inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);135136/* do this just once */137virgin = 0;138}139#else /* !BUILDFIXED */140# include "inffixed.h"141#endif /* BUILDFIXED */142state->lencode = lenfix;143state->lenbits = 9;144state->distcode = distfix;145state->distbits = 5;146}147148/* Macros for inflateBack(): */149150/* Load returned state from inflate_fast() */151#define LOAD() \152do { \153put = strm->next_out; \154left = strm->avail_out; \155next = strm->next_in; \156have = strm->avail_in; \157hold = state->hold; \158bits = state->bits; \159} while (0)160161/* Set state from registers for inflate_fast() */162#define RESTORE() \163do { \164strm->next_out = put; \165strm->avail_out = left; \166strm->next_in = next; \167strm->avail_in = have; \168state->hold = hold; \169state->bits = bits; \170} while (0)171172/* Clear the input bit accumulator */173#define INITBITS() \174do { \175hold = 0; \176bits = 0; \177} while (0)178179/* Assure that some input is available. If input is requested, but denied,180then return a Z_BUF_ERROR from inflateBack(). */181#define PULL() \182do { \183if (have == 0) { \184have = in(in_desc, &next); \185if (have == 0) { \186next = Z_NULL; \187ret = Z_BUF_ERROR; \188goto inf_leave; \189} \190} \191} while (0)192193/* Get a byte of input into the bit accumulator, or return from inflateBack()194with an error if there is no input available. */195#define PULLBYTE() \196do { \197PULL(); \198have--; \199hold += (unsigned long)(*next++) << bits; \200bits += 8; \201} while (0)202203/* Assure that there are at least n bits in the bit accumulator. If there is204not enough available input to do that, then return from inflateBack() with205an error. */206#define NEEDBITS(n) \207do { \208while (bits < (unsigned)(n)) \209PULLBYTE(); \210} while (0)211212/* Return the low n bits of the bit accumulator (n < 16) */213#define BITS(n) \214((unsigned)hold & ((1U << (n)) - 1))215216/* Remove n bits from the bit accumulator */217#define DROPBITS(n) \218do { \219hold >>= (n); \220bits -= (unsigned)(n); \221} while (0)222223/* Remove zero to seven bits as needed to go to a byte boundary */224#define BYTEBITS() \225do { \226hold >>= bits & 7; \227bits -= bits & 7; \228} while (0)229230/* Assure that some output space is available, by writing out the window231if it's full. If the write fails, return from inflateBack() with a232Z_BUF_ERROR. */233#define ROOM() \234do { \235if (left == 0) { \236put = state->window; \237left = state->wsize; \238state->whave = left; \239if (out(out_desc, put, left)) { \240ret = Z_BUF_ERROR; \241goto inf_leave; \242} \243} \244} while (0)245246/*247strm provides the memory allocation functions and window buffer on input,248and provides information on the unused input on return. For Z_DATA_ERROR249returns, strm will also provide an error message.250251in() and out() are the call-back input and output functions. When252inflateBack() needs more input, it calls in(). When inflateBack() has253filled the window with output, or when it completes with data in the254window, it calls out() to write out the data. The application must not255change the provided input until in() is called again or inflateBack()256returns. The application must not change the window/output buffer until257inflateBack() returns.258259in() and out() are called with a descriptor parameter provided in the260inflateBack() call. This parameter can be a structure that provides the261information required to do the read or write, as well as accumulated262information on the input and output such as totals and check values.263264in() should return zero on failure. out() should return non-zero on265failure. If either in() or out() fails, than inflateBack() returns a266Z_BUF_ERROR. strm->next_in can be checked for Z_NULL to see whether it267was in() or out() that caused in the error. Otherwise, inflateBack()268returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format269error, or Z_MEM_ERROR if it could not allocate memory for the state.270inflateBack() can also return Z_STREAM_ERROR if the input parameters271are not correct, i.e. strm is Z_NULL or the state was not initialized.272*/273int ZEXPORT inflateBack(strm, in, in_desc, out, out_desc)274z_streamp strm;275in_func in;276void FAR *in_desc;277out_func out;278void FAR *out_desc;279{280struct inflate_state FAR *state;281z_const unsigned char FAR *next; /* next input */282unsigned char FAR *put; /* next output */283unsigned have, left; /* available input and output */284unsigned long hold; /* bit buffer */285unsigned bits; /* bits in bit buffer */286unsigned copy; /* number of stored or match bytes to copy */287unsigned char FAR *from; /* where to copy match bytes from */288code here; /* current decoding table entry */289code last; /* parent table entry */290unsigned len; /* length to copy for repeats, bits to drop */291int ret; /* return code */292static const unsigned short order[19] = /* permutation of code lengths */293{16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};294295/* Check that the strm exists and that the state was initialized */296if (strm == Z_NULL || strm->state == Z_NULL)297return Z_STREAM_ERROR;298state = (struct inflate_state FAR *)strm->state;299300/* Reset the state */301strm->msg = Z_NULL;302state->mode = TYPE;303state->last = 0;304state->whave = 0;305next = strm->next_in;306have = next != Z_NULL ? strm->avail_in : 0;307hold = 0;308bits = 0;309put = state->window;310left = state->wsize;311312/* Inflate until end of block marked as last */313for (;;)314switch (state->mode) {315case TYPE:316/* determine and dispatch block type */317if (state->last) {318BYTEBITS();319state->mode = DONE;320break;321}322NEEDBITS(3);323state->last = BITS(1);324DROPBITS(1);325switch (BITS(2)) {326case 0: /* stored block */327Tracev((stderr, "inflate: stored block%s\n",328state->last ? " (last)" : ""));329state->mode = STORED;330break;331case 1: /* fixed block */332fixedtables(state);333Tracev((stderr, "inflate: fixed codes block%s\n",334state->last ? " (last)" : ""));335state->mode = LEN; /* decode codes */336break;337case 2: /* dynamic block */338Tracev((stderr, "inflate: dynamic codes block%s\n",339state->last ? " (last)" : ""));340state->mode = TABLE;341break;342case 3:343strm->msg = (char *)"invalid block type";344state->mode = BAD;345}346DROPBITS(2);347break;348349case STORED:350/* get and verify stored block length */351BYTEBITS(); /* go to byte boundary */352NEEDBITS(32);353if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {354strm->msg = (char *)"invalid stored block lengths";355state->mode = BAD;356break;357}358state->length = (unsigned)hold & 0xffff;359Tracev((stderr, "inflate: stored length %u\n",360state->length));361INITBITS();362363/* copy stored block from input to output */364while (state->length != 0) {365copy = state->length;366PULL();367ROOM();368if (copy > have) copy = have;369if (copy > left) copy = left;370zmemcpy(put, next, copy);371have -= copy;372next += copy;373left -= copy;374put += copy;375state->length -= copy;376}377Tracev((stderr, "inflate: stored end\n"));378state->mode = TYPE;379break;380381case TABLE:382/* get dynamic table entries descriptor */383NEEDBITS(14);384state->nlen = BITS(5) + 257;385DROPBITS(5);386state->ndist = BITS(5) + 1;387DROPBITS(5);388state->ncode = BITS(4) + 4;389DROPBITS(4);390#ifndef PKZIP_BUG_WORKAROUND391if (state->nlen > 286 || state->ndist > 30) {392strm->msg = (char *)"too many length or distance symbols";393state->mode = BAD;394break;395}396#endif397Tracev((stderr, "inflate: table sizes ok\n"));398399/* get code length code lengths (not a typo) */400state->have = 0;401while (state->have < state->ncode) {402NEEDBITS(3);403state->lens[order[state->have++]] = (unsigned short)BITS(3);404DROPBITS(3);405}406while (state->have < 19)407state->lens[order[state->have++]] = 0;408state->next = state->codes;409state->lencode = (code const FAR *)(state->next);410state->lenbits = 7;411ret = inflate_table(CODES, state->lens, 19, &(state->next),412&(state->lenbits), state->work);413if (ret) {414strm->msg = (char *)"invalid code lengths set";415state->mode = BAD;416break;417}418Tracev((stderr, "inflate: code lengths ok\n"));419420/* get length and distance code code lengths */421state->have = 0;422while (state->have < state->nlen + state->ndist) {423for (;;) {424here = state->lencode[BITS(state->lenbits)];425if ((unsigned)(here.bits) <= bits) break;426PULLBYTE();427}428if (here.val < 16) {429DROPBITS(here.bits);430state->lens[state->have++] = here.val;431}432else {433if (here.val == 16) {434NEEDBITS(here.bits + 2);435DROPBITS(here.bits);436if (state->have == 0) {437strm->msg = (char *)"invalid bit length repeat";438state->mode = BAD;439break;440}441len = (unsigned)(state->lens[state->have - 1]);442copy = 3 + BITS(2);443DROPBITS(2);444}445else if (here.val == 17) {446NEEDBITS(here.bits + 3);447DROPBITS(here.bits);448len = 0;449copy = 3 + BITS(3);450DROPBITS(3);451}452else {453NEEDBITS(here.bits + 7);454DROPBITS(here.bits);455len = 0;456copy = 11 + BITS(7);457DROPBITS(7);458}459if (state->have + copy > state->nlen + state->ndist) {460strm->msg = (char *)"invalid bit length repeat";461state->mode = BAD;462break;463}464while (copy--)465state->lens[state->have++] = (unsigned short)len;466}467}468469/* handle error breaks in while */470if (state->mode == BAD) break;471472/* check for end-of-block code (better have one) */473if (state->lens[256] == 0) {474strm->msg = (char *)"invalid code -- missing end-of-block";475state->mode = BAD;476break;477}478479/* build code tables -- note: do not change the lenbits or distbits480values here (9 and 6) without reading the comments in inftrees.h481concerning the ENOUGH constants, which depend on those values */482state->next = state->codes;483state->lencode = (code const FAR *)(state->next);484state->lenbits = 9;485ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),486&(state->lenbits), state->work);487if (ret) {488strm->msg = (char *)"invalid literal/lengths set";489state->mode = BAD;490break;491}492state->distcode = (code const FAR *)(state->next);493state->distbits = 6;494ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,495&(state->next), &(state->distbits), state->work);496if (ret) {497strm->msg = (char *)"invalid distances set";498state->mode = BAD;499break;500}501Tracev((stderr, "inflate: codes ok\n"));502state->mode = LEN;503504case LEN:505/* use inflate_fast() if we have enough input and output */506if (have >= 6 && left >= 258) {507RESTORE();508if (state->whave < state->wsize)509state->whave = state->wsize - left;510inflate_fast(strm, state->wsize);511LOAD();512break;513}514515/* get a literal, length, or end-of-block code */516for (;;) {517here = state->lencode[BITS(state->lenbits)];518if ((unsigned)(here.bits) <= bits) break;519PULLBYTE();520}521if (here.op && (here.op & 0xf0) == 0) {522last = here;523for (;;) {524here = state->lencode[last.val +525(BITS(last.bits + last.op) >> last.bits)];526if ((unsigned)(last.bits + here.bits) <= bits) break;527PULLBYTE();528}529DROPBITS(last.bits);530}531DROPBITS(here.bits);532state->length = (unsigned)here.val;533534/* process literal */535if (here.op == 0) {536Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?537"inflate: literal '%c'\n" :538"inflate: literal 0x%02x\n", here.val));539ROOM();540*put++ = (unsigned char)(state->length);541left--;542state->mode = LEN;543break;544}545546/* process end of block */547if (here.op & 32) {548Tracevv((stderr, "inflate: end of block\n"));549state->mode = TYPE;550break;551}552553/* invalid code */554if (here.op & 64) {555strm->msg = (char *)"invalid literal/length code";556state->mode = BAD;557break;558}559560/* length code -- get extra bits, if any */561state->extra = (unsigned)(here.op) & 15;562if (state->extra != 0) {563NEEDBITS(state->extra);564state->length += BITS(state->extra);565DROPBITS(state->extra);566}567Tracevv((stderr, "inflate: length %u\n", state->length));568569/* get distance code */570for (;;) {571here = state->distcode[BITS(state->distbits)];572if ((unsigned)(here.bits) <= bits) break;573PULLBYTE();574}575if ((here.op & 0xf0) == 0) {576last = here;577for (;;) {578here = state->distcode[last.val +579(BITS(last.bits + last.op) >> last.bits)];580if ((unsigned)(last.bits + here.bits) <= bits) break;581PULLBYTE();582}583DROPBITS(last.bits);584}585DROPBITS(here.bits);586if (here.op & 64) {587strm->msg = (char *)"invalid distance code";588state->mode = BAD;589break;590}591state->offset = (unsigned)here.val;592593/* get distance extra bits, if any */594state->extra = (unsigned)(here.op) & 15;595if (state->extra != 0) {596NEEDBITS(state->extra);597state->offset += BITS(state->extra);598DROPBITS(state->extra);599}600if (state->offset > state->wsize - (state->whave < state->wsize ?601left : 0)) {602strm->msg = (char *)"invalid distance too far back";603state->mode = BAD;604break;605}606Tracevv((stderr, "inflate: distance %u\n", state->offset));607608/* copy match from window to output */609do {610ROOM();611copy = state->wsize - state->offset;612if (copy < left) {613from = put + copy;614copy = left - copy;615}616else {617from = put - state->offset;618copy = left;619}620if (copy > state->length) copy = state->length;621state->length -= copy;622left -= copy;623do {624*put++ = *from++;625} while (--copy);626} while (state->length != 0);627break;628629case DONE:630/* inflate stream terminated properly -- write leftover output */631ret = Z_STREAM_END;632if (left < state->wsize) {633if (out(out_desc, state->window, state->wsize - left))634ret = Z_BUF_ERROR;635}636goto inf_leave;637638case BAD:639ret = Z_DATA_ERROR;640goto inf_leave;641642default: /* can't happen, but makes compilers happy */643ret = Z_STREAM_ERROR;644goto inf_leave;645}646647/* Return unused input */648inf_leave:649strm->next_in = next;650strm->avail_in = have;651return ret;652}653654int ZEXPORT inflateBackEnd(strm)655z_streamp strm;656{657if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)658return Z_STREAM_ERROR;659ZFREE(strm, strm->state);660strm->state = Z_NULL;661Tracev((stderr, "inflate: end\n"));662return Z_OK;663}664665666