Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:11

0001 /* infback.c -- inflate using a call-back interface
0002  * Copyright (C) 1995-2022 Mark Adler
0003  * For conditions of distribution and use, see copyright notice in zlib.h
0004  */
0005 
0006 /*
0007    This code is largely copied from inflate.c.  Normally either infback.o or
0008    inflate.o would be linked into an application--not both.  The interface
0009    with inffast.c is retained so that optimized assembler-coded versions of
0010    inflate_fast() can be used with either inflate.c or infback.c.
0011  */
0012 
0013 #include "zutil.h"
0014 #include "inftrees.h"
0015 #include "inflate.h"
0016 #include "inffast.h"
0017 
0018 /* function prototypes */
0019 local void fixedtables OF((struct inflate_state FAR *state));
0020 
0021 /*
0022    strm provides memory allocation functions in zalloc and zfree, or
0023    Z_NULL to use the library memory allocation functions.
0024 
0025    windowBits is in the range 8..15, and window is a user-supplied
0026    window and output buffer that is 2**windowBits bytes.
0027  */
0028 int ZEXPORT inflateBackInit_(strm, windowBits, window, version, stream_size)
0029 z_streamp strm;
0030 int windowBits;
0031 unsigned char FAR *window;
0032 const char *version;
0033 int stream_size;
0034 {
0035     struct inflate_state FAR *state;
0036 
0037     if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
0038         stream_size != (int)(sizeof(z_stream)))
0039         return Z_VERSION_ERROR;
0040     if (strm == Z_NULL || window == Z_NULL ||
0041         windowBits < 8 || windowBits > 15)
0042         return Z_STREAM_ERROR;
0043     strm->msg = Z_NULL;                 /* in case we return an error */
0044     if (strm->zalloc == (alloc_func)0) {
0045 #ifdef Z_SOLO
0046         return Z_STREAM_ERROR;
0047 #else
0048         strm->zalloc = zcalloc;
0049         strm->opaque = (voidpf)0;
0050 #endif
0051     }
0052     if (strm->zfree == (free_func)0)
0053 #ifdef Z_SOLO
0054         return Z_STREAM_ERROR;
0055 #else
0056     strm->zfree = zcfree;
0057 #endif
0058     state = (struct inflate_state FAR *)ZALLOC(strm, 1,
0059                                                sizeof(struct inflate_state));
0060     if (state == Z_NULL) return Z_MEM_ERROR;
0061     Tracev((stderr, "inflate: allocated\n"));
0062     strm->state = (struct internal_state FAR *)state;
0063     state->dmax = 32768U;
0064     state->wbits = (uInt)windowBits;
0065     state->wsize = 1U << windowBits;
0066     state->window = window;
0067     state->wnext = 0;
0068     state->whave = 0;
0069     state->sane = 1;
0070     return Z_OK;
0071 }
0072 
0073 /*
0074    Return state with length and distance decoding tables and index sizes set to
0075    fixed code decoding.  Normally this returns fixed tables from inffixed.h.
0076    If BUILDFIXED is defined, then instead this routine builds the tables the
0077    first time it's called, and returns those tables the first time and
0078    thereafter.  This reduces the size of the code by about 2K bytes, in
0079    exchange for a little execution time.  However, BUILDFIXED should not be
0080    used for threaded applications, since the rewriting of the tables and virgin
0081    may not be thread-safe.
0082  */
0083 local void fixedtables(state)
0084 struct inflate_state FAR *state;
0085 {
0086 #ifdef BUILDFIXED
0087     static int virgin = 1;
0088     static code *lenfix, *distfix;
0089     static code fixed[544];
0090 
0091     /* build fixed huffman tables if first call (may not be thread safe) */
0092     if (virgin) {
0093         unsigned sym, bits;
0094         static code *next;
0095 
0096         /* literal/length table */
0097         sym = 0;
0098         while (sym < 144) state->lens[sym++] = 8;
0099         while (sym < 256) state->lens[sym++] = 9;
0100         while (sym < 280) state->lens[sym++] = 7;
0101         while (sym < 288) state->lens[sym++] = 8;
0102         next = fixed;
0103         lenfix = next;
0104         bits = 9;
0105         inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
0106 
0107         /* distance table */
0108         sym = 0;
0109         while (sym < 32) state->lens[sym++] = 5;
0110         distfix = next;
0111         bits = 5;
0112         inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
0113 
0114         /* do this just once */
0115         virgin = 0;
0116     }
0117 #else /* !BUILDFIXED */
0118 #   include "inffixed.h"
0119 #endif /* BUILDFIXED */
0120     state->lencode = lenfix;
0121     state->lenbits = 9;
0122     state->distcode = distfix;
0123     state->distbits = 5;
0124 }
0125 
0126 /* Macros for inflateBack(): */
0127 
0128 /* Load returned state from inflate_fast() */
0129 #define LOAD() \
0130     do { \
0131         put = strm->next_out; \
0132         left = strm->avail_out; \
0133         next = strm->next_in; \
0134         have = strm->avail_in; \
0135         hold = state->hold; \
0136         bits = state->bits; \
0137     } while (0)
0138 
0139 /* Set state from registers for inflate_fast() */
0140 #define RESTORE() \
0141     do { \
0142         strm->next_out = put; \
0143         strm->avail_out = left; \
0144         strm->next_in = next; \
0145         strm->avail_in = have; \
0146         state->hold = hold; \
0147         state->bits = bits; \
0148     } while (0)
0149 
0150 /* Clear the input bit accumulator */
0151 #define INITBITS() \
0152     do { \
0153         hold = 0; \
0154         bits = 0; \
0155     } while (0)
0156 
0157 /* Assure that some input is available.  If input is requested, but denied,
0158    then return a Z_BUF_ERROR from inflateBack(). */
0159 #define PULL() \
0160     do { \
0161         if (have == 0) { \
0162             have = in(in_desc, &next); \
0163             if (have == 0) { \
0164                 next = Z_NULL; \
0165                 ret = Z_BUF_ERROR; \
0166                 goto inf_leave; \
0167             } \
0168         } \
0169     } while (0)
0170 
0171 /* Get a byte of input into the bit accumulator, or return from inflateBack()
0172    with an error if there is no input available. */
0173 #define PULLBYTE() \
0174     do { \
0175         PULL(); \
0176         have--; \
0177         hold += (unsigned long)(*next++) << bits; \
0178         bits += 8; \
0179     } while (0)
0180 
0181 /* Assure that there are at least n bits in the bit accumulator.  If there is
0182    not enough available input to do that, then return from inflateBack() with
0183    an error. */
0184 #define NEEDBITS(n) \
0185     do { \
0186         while (bits < (unsigned)(n)) \
0187             PULLBYTE(); \
0188     } while (0)
0189 
0190 /* Return the low n bits of the bit accumulator (n < 16) */
0191 #define BITS(n) \
0192     ((unsigned)hold & ((1U << (n)) - 1))
0193 
0194 /* Remove n bits from the bit accumulator */
0195 #define DROPBITS(n) \
0196     do { \
0197         hold >>= (n); \
0198         bits -= (unsigned)(n); \
0199     } while (0)
0200 
0201 /* Remove zero to seven bits as needed to go to a byte boundary */
0202 #define BYTEBITS() \
0203     do { \
0204         hold >>= bits & 7; \
0205         bits -= bits & 7; \
0206     } while (0)
0207 
0208 /* Assure that some output space is available, by writing out the window
0209    if it's full.  If the write fails, return from inflateBack() with a
0210    Z_BUF_ERROR. */
0211 #define ROOM() \
0212     do { \
0213         if (left == 0) { \
0214             put = state->window; \
0215             left = state->wsize; \
0216             state->whave = left; \
0217             if (out(out_desc, put, left)) { \
0218                 ret = Z_BUF_ERROR; \
0219                 goto inf_leave; \
0220             } \
0221         } \
0222     } while (0)
0223 
0224 /*
0225    strm provides the memory allocation functions and window buffer on input,
0226    and provides information on the unused input on return.  For Z_DATA_ERROR
0227    returns, strm will also provide an error message.
0228 
0229    in() and out() are the call-back input and output functions.  When
0230    inflateBack() needs more input, it calls in().  When inflateBack() has
0231    filled the window with output, or when it completes with data in the
0232    window, it calls out() to write out the data.  The application must not
0233    change the provided input until in() is called again or inflateBack()
0234    returns.  The application must not change the window/output buffer until
0235    inflateBack() returns.
0236 
0237    in() and out() are called with a descriptor parameter provided in the
0238    inflateBack() call.  This parameter can be a structure that provides the
0239    information required to do the read or write, as well as accumulated
0240    information on the input and output such as totals and check values.
0241 
0242    in() should return zero on failure.  out() should return non-zero on
0243    failure.  If either in() or out() fails, than inflateBack() returns a
0244    Z_BUF_ERROR.  strm->next_in can be checked for Z_NULL to see whether it
0245    was in() or out() that caused in the error.  Otherwise,  inflateBack()
0246    returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format
0247    error, or Z_MEM_ERROR if it could not allocate memory for the state.
0248    inflateBack() can also return Z_STREAM_ERROR if the input parameters
0249    are not correct, i.e. strm is Z_NULL or the state was not initialized.
0250  */
0251 int ZEXPORT inflateBack(strm, in, in_desc, out, out_desc)
0252 z_streamp strm;
0253 in_func in;
0254 void FAR *in_desc;
0255 out_func out;
0256 void FAR *out_desc;
0257 {
0258     struct inflate_state FAR *state;
0259     z_const unsigned char FAR *next;    /* next input */
0260     unsigned char FAR *put;     /* next output */
0261     unsigned have, left;        /* available input and output */
0262     unsigned long hold;         /* bit buffer */
0263     unsigned bits;              /* bits in bit buffer */
0264     unsigned copy;              /* number of stored or match bytes to copy */
0265     unsigned char FAR *from;    /* where to copy match bytes from */
0266     code here;                  /* current decoding table entry */
0267     code last;                  /* parent table entry */
0268     unsigned len;               /* length to copy for repeats, bits to drop */
0269     int ret;                    /* return code */
0270     static const unsigned short order[19] = /* permutation of code lengths */
0271         {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
0272 
0273     /* Check that the strm exists and that the state was initialized */
0274     if (strm == Z_NULL || strm->state == Z_NULL)
0275         return Z_STREAM_ERROR;
0276     state = (struct inflate_state FAR *)strm->state;
0277 
0278     /* Reset the state */
0279     strm->msg = Z_NULL;
0280     state->mode = TYPE;
0281     state->last = 0;
0282     state->whave = 0;
0283     next = strm->next_in;
0284     have = next != Z_NULL ? strm->avail_in : 0;
0285     hold = 0;
0286     bits = 0;
0287     put = state->window;
0288     left = state->wsize;
0289 
0290     /* Inflate until end of block marked as last */
0291     for (;;)
0292         switch (state->mode) {
0293         case TYPE:
0294             /* determine and dispatch block type */
0295             if (state->last) {
0296                 BYTEBITS();
0297                 state->mode = DONE;
0298                 break;
0299             }
0300             NEEDBITS(3);
0301             state->last = BITS(1);
0302             DROPBITS(1);
0303             switch (BITS(2)) {
0304             case 0:                             /* stored block */
0305                 Tracev((stderr, "inflate:     stored block%s\n",
0306                         state->last ? " (last)" : ""));
0307                 state->mode = STORED;
0308                 break;
0309             case 1:                             /* fixed block */
0310                 fixedtables(state);
0311                 Tracev((stderr, "inflate:     fixed codes block%s\n",
0312                         state->last ? " (last)" : ""));
0313                 state->mode = LEN;              /* decode codes */
0314                 break;
0315             case 2:                             /* dynamic block */
0316                 Tracev((stderr, "inflate:     dynamic codes block%s\n",
0317                         state->last ? " (last)" : ""));
0318                 state->mode = TABLE;
0319                 break;
0320             case 3:
0321                 strm->msg = (char *)"invalid block type";
0322                 state->mode = BAD;
0323             }
0324             DROPBITS(2);
0325             break;
0326 
0327         case STORED:
0328             /* get and verify stored block length */
0329             BYTEBITS();                         /* go to byte boundary */
0330             NEEDBITS(32);
0331             if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
0332                 strm->msg = (char *)"invalid stored block lengths";
0333                 state->mode = BAD;
0334                 break;
0335             }
0336             state->length = (unsigned)hold & 0xffff;
0337             Tracev((stderr, "inflate:       stored length %u\n",
0338                     state->length));
0339             INITBITS();
0340 
0341             /* copy stored block from input to output */
0342             while (state->length != 0) {
0343                 copy = state->length;
0344                 PULL();
0345                 ROOM();
0346                 if (copy > have) copy = have;
0347                 if (copy > left) copy = left;
0348                 zmemcpy(put, next, copy);
0349                 have -= copy;
0350                 next += copy;
0351                 left -= copy;
0352                 put += copy;
0353                 state->length -= copy;
0354             }
0355             Tracev((stderr, "inflate:       stored end\n"));
0356             state->mode = TYPE;
0357             break;
0358 
0359         case TABLE:
0360             /* get dynamic table entries descriptor */
0361             NEEDBITS(14);
0362             state->nlen = BITS(5) + 257;
0363             DROPBITS(5);
0364             state->ndist = BITS(5) + 1;
0365             DROPBITS(5);
0366             state->ncode = BITS(4) + 4;
0367             DROPBITS(4);
0368 #ifndef PKZIP_BUG_WORKAROUND
0369             if (state->nlen > 286 || state->ndist > 30) {
0370                 strm->msg = (char *)"too many length or distance symbols";
0371                 state->mode = BAD;
0372                 break;
0373             }
0374 #endif
0375             Tracev((stderr, "inflate:       table sizes ok\n"));
0376 
0377             /* get code length code lengths (not a typo) */
0378             state->have = 0;
0379             while (state->have < state->ncode) {
0380                 NEEDBITS(3);
0381                 state->lens[order[state->have++]] = (unsigned short)BITS(3);
0382                 DROPBITS(3);
0383             }
0384             while (state->have < 19)
0385                 state->lens[order[state->have++]] = 0;
0386             state->next = state->codes;
0387             state->lencode = (code const FAR *)(state->next);
0388             state->lenbits = 7;
0389             ret = inflate_table(CODES, state->lens, 19, &(state->next),
0390                                 &(state->lenbits), state->work);
0391             if (ret) {
0392                 strm->msg = (char *)"invalid code lengths set";
0393                 state->mode = BAD;
0394                 break;
0395             }
0396             Tracev((stderr, "inflate:       code lengths ok\n"));
0397 
0398             /* get length and distance code code lengths */
0399             state->have = 0;
0400             while (state->have < state->nlen + state->ndist) {
0401                 for (;;) {
0402                     here = state->lencode[BITS(state->lenbits)];
0403                     if ((unsigned)(here.bits) <= bits) break;
0404                     PULLBYTE();
0405                 }
0406                 if (here.val < 16) {
0407                     DROPBITS(here.bits);
0408                     state->lens[state->have++] = here.val;
0409                 }
0410                 else {
0411                     if (here.val == 16) {
0412                         NEEDBITS(here.bits + 2);
0413                         DROPBITS(here.bits);
0414                         if (state->have == 0) {
0415                             strm->msg = (char *)"invalid bit length repeat";
0416                             state->mode = BAD;
0417                             break;
0418                         }
0419                         len = (unsigned)(state->lens[state->have - 1]);
0420                         copy = 3 + BITS(2);
0421                         DROPBITS(2);
0422                     }
0423                     else if (here.val == 17) {
0424                         NEEDBITS(here.bits + 3);
0425                         DROPBITS(here.bits);
0426                         len = 0;
0427                         copy = 3 + BITS(3);
0428                         DROPBITS(3);
0429                     }
0430                     else {
0431                         NEEDBITS(here.bits + 7);
0432                         DROPBITS(here.bits);
0433                         len = 0;
0434                         copy = 11 + BITS(7);
0435                         DROPBITS(7);
0436                     }
0437                     if (state->have + copy > state->nlen + state->ndist) {
0438                         strm->msg = (char *)"invalid bit length repeat";
0439                         state->mode = BAD;
0440                         break;
0441                     }
0442                     while (copy--)
0443                         state->lens[state->have++] = (unsigned short)len;
0444                 }
0445             }
0446 
0447             /* handle error breaks in while */
0448             if (state->mode == BAD) break;
0449 
0450             /* check for end-of-block code (better have one) */
0451             if (state->lens[256] == 0) {
0452                 strm->msg = (char *)"invalid code -- missing end-of-block";
0453                 state->mode = BAD;
0454                 break;
0455             }
0456 
0457             /* build code tables -- note: do not change the lenbits or distbits
0458                values here (9 and 6) without reading the comments in inftrees.h
0459                concerning the ENOUGH constants, which depend on those values */
0460             state->next = state->codes;
0461             state->lencode = (code const FAR *)(state->next);
0462             state->lenbits = 9;
0463             ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
0464                                 &(state->lenbits), state->work);
0465             if (ret) {
0466                 strm->msg = (char *)"invalid literal/lengths set";
0467                 state->mode = BAD;
0468                 break;
0469             }
0470             state->distcode = (code const FAR *)(state->next);
0471             state->distbits = 6;
0472             ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
0473                             &(state->next), &(state->distbits), state->work);
0474             if (ret) {
0475                 strm->msg = (char *)"invalid distances set";
0476                 state->mode = BAD;
0477                 break;
0478             }
0479             Tracev((stderr, "inflate:       codes ok\n"));
0480             state->mode = LEN;
0481                 /* fallthrough */
0482 
0483         case LEN:
0484             /* use inflate_fast() if we have enough input and output */
0485             if (have >= 6 && left >= 258) {
0486                 RESTORE();
0487                 if (state->whave < state->wsize)
0488                     state->whave = state->wsize - left;
0489                 inflate_fast(strm, state->wsize);
0490                 LOAD();
0491                 break;
0492             }
0493 
0494             /* get a literal, length, or end-of-block code */
0495             for (;;) {
0496                 here = state->lencode[BITS(state->lenbits)];
0497                 if ((unsigned)(here.bits) <= bits) break;
0498                 PULLBYTE();
0499             }
0500             if (here.op && (here.op & 0xf0) == 0) {
0501                 last = here;
0502                 for (;;) {
0503                     here = state->lencode[last.val +
0504                             (BITS(last.bits + last.op) >> last.bits)];
0505                     if ((unsigned)(last.bits + here.bits) <= bits) break;
0506                     PULLBYTE();
0507                 }
0508                 DROPBITS(last.bits);
0509             }
0510             DROPBITS(here.bits);
0511             state->length = (unsigned)here.val;
0512 
0513             /* process literal */
0514             if (here.op == 0) {
0515                 Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
0516                         "inflate:         literal '%c'\n" :
0517                         "inflate:         literal 0x%02x\n", here.val));
0518                 ROOM();
0519                 *put++ = (unsigned char)(state->length);
0520                 left--;
0521                 state->mode = LEN;
0522                 break;
0523             }
0524 
0525             /* process end of block */
0526             if (here.op & 32) {
0527                 Tracevv((stderr, "inflate:         end of block\n"));
0528                 state->mode = TYPE;
0529                 break;
0530             }
0531 
0532             /* invalid code */
0533             if (here.op & 64) {
0534                 strm->msg = (char *)"invalid literal/length code";
0535                 state->mode = BAD;
0536                 break;
0537             }
0538 
0539             /* length code -- get extra bits, if any */
0540             state->extra = (unsigned)(here.op) & 15;
0541             if (state->extra != 0) {
0542                 NEEDBITS(state->extra);
0543                 state->length += BITS(state->extra);
0544                 DROPBITS(state->extra);
0545             }
0546             Tracevv((stderr, "inflate:         length %u\n", state->length));
0547 
0548             /* get distance code */
0549             for (;;) {
0550                 here = state->distcode[BITS(state->distbits)];
0551                 if ((unsigned)(here.bits) <= bits) break;
0552                 PULLBYTE();
0553             }
0554             if ((here.op & 0xf0) == 0) {
0555                 last = here;
0556                 for (;;) {
0557                     here = state->distcode[last.val +
0558                             (BITS(last.bits + last.op) >> last.bits)];
0559                     if ((unsigned)(last.bits + here.bits) <= bits) break;
0560                     PULLBYTE();
0561                 }
0562                 DROPBITS(last.bits);
0563             }
0564             DROPBITS(here.bits);
0565             if (here.op & 64) {
0566                 strm->msg = (char *)"invalid distance code";
0567                 state->mode = BAD;
0568                 break;
0569             }
0570             state->offset = (unsigned)here.val;
0571 
0572             /* get distance extra bits, if any */
0573             state->extra = (unsigned)(here.op) & 15;
0574             if (state->extra != 0) {
0575                 NEEDBITS(state->extra);
0576                 state->offset += BITS(state->extra);
0577                 DROPBITS(state->extra);
0578             }
0579             if (state->offset > state->wsize - (state->whave < state->wsize ?
0580                                                 left : 0)) {
0581                 strm->msg = (char *)"invalid distance too far back";
0582                 state->mode = BAD;
0583                 break;
0584             }
0585             Tracevv((stderr, "inflate:         distance %u\n", state->offset));
0586 
0587             /* copy match from window to output */
0588             do {
0589                 ROOM();
0590                 copy = state->wsize - state->offset;
0591                 if (copy < left) {
0592                     from = put + copy;
0593                     copy = left - copy;
0594                 }
0595                 else {
0596                     from = put - state->offset;
0597                     copy = left;
0598                 }
0599                 if (copy > state->length) copy = state->length;
0600                 state->length -= copy;
0601                 left -= copy;
0602                 do {
0603                     *put++ = *from++;
0604                 } while (--copy);
0605             } while (state->length != 0);
0606             break;
0607 
0608         case DONE:
0609             /* inflate stream terminated properly */
0610             ret = Z_STREAM_END;
0611             goto inf_leave;
0612 
0613         case BAD:
0614             ret = Z_DATA_ERROR;
0615             goto inf_leave;
0616 
0617         default:
0618             /* can't happen, but makes compilers happy */
0619             ret = Z_STREAM_ERROR;
0620             goto inf_leave;
0621         }
0622 
0623     /* Write leftover output and return unused input */
0624   inf_leave:
0625     if (left < state->wsize) {
0626         if (out(out_desc, state->window, state->wsize - left) &&
0627             ret == Z_STREAM_END)
0628             ret = Z_BUF_ERROR;
0629     }
0630     strm->next_in = next;
0631     strm->avail_in = have;
0632     return ret;
0633 }
0634 
0635 int ZEXPORT inflateBackEnd(strm)
0636 z_streamp strm;
0637 {
0638     if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
0639         return Z_STREAM_ERROR;
0640     ZFREE(strm, strm->state);
0641     strm->state = Z_NULL;
0642     Tracev((stderr, "inflate: end\n"));
0643     return Z_OK;
0644 }