File indexing completed on 2024-04-14 03:46:48

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