Back to home page

OSCL-LXR

 
 

    


0001 /* inflate.c -- zlib decompression
0002  * Copyright (C) 1995-2005 Mark Adler
0003  * For conditions of distribution and use, see copyright notice in zlib.h
0004  *
0005  * Based on zlib 1.2.3 but modified for the Linux Kernel by
0006  * Richard Purdie <richard@openedhand.com>
0007  *
0008  * Changes mainly for static instead of dynamic memory allocation
0009  *
0010  */
0011 
0012 #include <linux/zutil.h>
0013 #include "inftrees.h"
0014 #include "inflate.h"
0015 #include "inffast.h"
0016 #include "infutil.h"
0017 
0018 /* architecture-specific bits */
0019 #ifdef CONFIG_ZLIB_DFLTCC
0020 #  include "../zlib_dfltcc/dfltcc.h"
0021 #else
0022 #define INFLATE_RESET_HOOK(strm) do {} while (0)
0023 #define INFLATE_TYPEDO_HOOK(strm, flush) do {} while (0)
0024 #define INFLATE_NEED_UPDATEWINDOW(strm) 1
0025 #define INFLATE_NEED_CHECKSUM(strm) 1
0026 #endif
0027 
0028 int zlib_inflate_workspacesize(void)
0029 {
0030     return sizeof(struct inflate_workspace);
0031 }
0032 
0033 int zlib_inflateReset(z_streamp strm)
0034 {
0035     struct inflate_state *state;
0036 
0037     if (strm == NULL || strm->state == NULL) return Z_STREAM_ERROR;
0038     state = (struct inflate_state *)strm->state;
0039     strm->total_in = strm->total_out = state->total = 0;
0040     strm->msg = NULL;
0041     strm->adler = 1;        /* to support ill-conceived Java test suite */
0042     state->mode = HEAD;
0043     state->last = 0;
0044     state->havedict = 0;
0045     state->dmax = 32768U;
0046     state->hold = 0;
0047     state->bits = 0;
0048     state->lencode = state->distcode = state->next = state->codes;
0049 
0050     /* Initialise Window */
0051     state->wsize = 1U << state->wbits;
0052     state->write = 0;
0053     state->whave = 0;
0054 
0055     INFLATE_RESET_HOOK(strm);
0056     return Z_OK;
0057 }
0058 
0059 int zlib_inflateInit2(z_streamp strm, int windowBits)
0060 {
0061     struct inflate_state *state;
0062 
0063     if (strm == NULL) return Z_STREAM_ERROR;
0064     strm->msg = NULL;                 /* in case we return an error */
0065 
0066     state = &WS(strm)->inflate_state;
0067     strm->state = (struct internal_state *)state;
0068 
0069     if (windowBits < 0) {
0070         state->wrap = 0;
0071         windowBits = -windowBits;
0072     }
0073     else {
0074         state->wrap = (windowBits >> 4) + 1;
0075     }
0076     if (windowBits < 8 || windowBits > 15) {
0077         return Z_STREAM_ERROR;
0078     }
0079     state->wbits = (unsigned)windowBits;
0080 #ifdef CONFIG_ZLIB_DFLTCC
0081     /*
0082      * DFLTCC requires the window to be page aligned.
0083      * Thus, we overallocate and take the aligned portion of the buffer.
0084      */
0085     state->window = PTR_ALIGN(&WS(strm)->working_window[0], PAGE_SIZE);
0086 #else
0087     state->window = &WS(strm)->working_window[0];
0088 #endif
0089 
0090     return zlib_inflateReset(strm);
0091 }
0092 
0093 /*
0094    Return state with length and distance decoding tables and index sizes set to
0095    fixed code decoding.  This returns fixed tables from inffixed.h.
0096  */
0097 static void zlib_fixedtables(struct inflate_state *state)
0098 {
0099 #   include "inffixed.h"
0100     state->lencode = lenfix;
0101     state->lenbits = 9;
0102     state->distcode = distfix;
0103     state->distbits = 5;
0104 }
0105 
0106 
0107 /*
0108    Update the window with the last wsize (normally 32K) bytes written before
0109    returning. This is only called when a window is already in use, or when
0110    output has been written during this inflate call, but the end of the deflate
0111    stream has not been reached yet. It is also called to window dictionary data
0112    when a dictionary is loaded.
0113 
0114    Providing output buffers larger than 32K to inflate() should provide a speed
0115    advantage, since only the last 32K of output is copied to the sliding window
0116    upon return from inflate(), and since all distances after the first 32K of
0117    output will fall in the output data, making match copies simpler and faster.
0118    The advantage may be dependent on the size of the processor's data caches.
0119  */
0120 static void zlib_updatewindow(z_streamp strm, unsigned out)
0121 {
0122     struct inflate_state *state;
0123     unsigned copy, dist;
0124 
0125     state = (struct inflate_state *)strm->state;
0126 
0127     /* copy state->wsize or less output bytes into the circular window */
0128     copy = out - strm->avail_out;
0129     if (copy >= state->wsize) {
0130         memcpy(state->window, strm->next_out - state->wsize, state->wsize);
0131         state->write = 0;
0132         state->whave = state->wsize;
0133     }
0134     else {
0135         dist = state->wsize - state->write;
0136         if (dist > copy) dist = copy;
0137         memcpy(state->window + state->write, strm->next_out - copy, dist);
0138         copy -= dist;
0139         if (copy) {
0140             memcpy(state->window, strm->next_out - copy, copy);
0141             state->write = copy;
0142             state->whave = state->wsize;
0143         }
0144         else {
0145             state->write += dist;
0146             if (state->write == state->wsize) state->write = 0;
0147             if (state->whave < state->wsize) state->whave += dist;
0148         }
0149     }
0150 }
0151 
0152 
0153 /*
0154  * At the end of a Deflate-compressed PPP packet, we expect to have seen
0155  * a `stored' block type value but not the (zero) length bytes.
0156  */
0157 /*
0158    Returns true if inflate is currently at the end of a block generated by
0159    Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
0160    implementation to provide an additional safety check. PPP uses
0161    Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
0162    block. When decompressing, PPP checks that at the end of input packet,
0163    inflate is waiting for these length bytes.
0164  */
0165 static int zlib_inflateSyncPacket(z_streamp strm)
0166 {
0167     struct inflate_state *state;
0168 
0169     if (strm == NULL || strm->state == NULL) return Z_STREAM_ERROR;
0170     state = (struct inflate_state *)strm->state;
0171 
0172     if (state->mode == STORED && state->bits == 0) {
0173     state->mode = TYPE;
0174         return Z_OK;
0175     }
0176     return Z_DATA_ERROR;
0177 }
0178 
0179 /* Macros for inflate(): */
0180 
0181 /* check function to use adler32() for zlib or crc32() for gzip */
0182 #define UPDATE(check, buf, len) zlib_adler32(check, buf, len)
0183 
0184 /* Load registers with state in inflate() for speed */
0185 #define LOAD() \
0186     do { \
0187         put = strm->next_out; \
0188         left = strm->avail_out; \
0189         next = strm->next_in; \
0190         have = strm->avail_in; \
0191         hold = state->hold; \
0192         bits = state->bits; \
0193     } while (0)
0194 
0195 /* Restore state from registers in inflate() */
0196 #define RESTORE() \
0197     do { \
0198         strm->next_out = put; \
0199         strm->avail_out = left; \
0200         strm->next_in = next; \
0201         strm->avail_in = have; \
0202         state->hold = hold; \
0203         state->bits = bits; \
0204     } while (0)
0205 
0206 /* Clear the input bit accumulator */
0207 #define INITBITS() \
0208     do { \
0209         hold = 0; \
0210         bits = 0; \
0211     } while (0)
0212 
0213 /* Get a byte of input into the bit accumulator, or return from inflate()
0214    if there is no input available. */
0215 #define PULLBYTE() \
0216     do { \
0217         if (have == 0) goto inf_leave; \
0218         have--; \
0219         hold += (unsigned long)(*next++) << bits; \
0220         bits += 8; \
0221     } while (0)
0222 
0223 /* Assure that there are at least n bits in the bit accumulator.  If there is
0224    not enough available input to do that, then return from inflate(). */
0225 #define NEEDBITS(n) \
0226     do { \
0227         while (bits < (unsigned)(n)) \
0228             PULLBYTE(); \
0229     } while (0)
0230 
0231 /* Return the low n bits of the bit accumulator (n < 16) */
0232 #define BITS(n) \
0233     ((unsigned)hold & ((1U << (n)) - 1))
0234 
0235 /* Remove n bits from the bit accumulator */
0236 #define DROPBITS(n) \
0237     do { \
0238         hold >>= (n); \
0239         bits -= (unsigned)(n); \
0240     } while (0)
0241 
0242 /* Remove zero to seven bits as needed to go to a byte boundary */
0243 #define BYTEBITS() \
0244     do { \
0245         hold >>= bits & 7; \
0246         bits -= bits & 7; \
0247     } while (0)
0248 
0249 /*
0250    inflate() uses a state machine to process as much input data and generate as
0251    much output data as possible before returning.  The state machine is
0252    structured roughly as follows:
0253 
0254     for (;;) switch (state) {
0255     ...
0256     case STATEn:
0257         if (not enough input data or output space to make progress)
0258             return;
0259         ... make progress ...
0260         state = STATEm;
0261         break;
0262     ...
0263     }
0264 
0265    so when inflate() is called again, the same case is attempted again, and
0266    if the appropriate resources are provided, the machine proceeds to the
0267    next state.  The NEEDBITS() macro is usually the way the state evaluates
0268    whether it can proceed or should return.  NEEDBITS() does the return if
0269    the requested bits are not available.  The typical use of the BITS macros
0270    is:
0271 
0272         NEEDBITS(n);
0273         ... do something with BITS(n) ...
0274         DROPBITS(n);
0275 
0276    where NEEDBITS(n) either returns from inflate() if there isn't enough
0277    input left to load n bits into the accumulator, or it continues.  BITS(n)
0278    gives the low n bits in the accumulator.  When done, DROPBITS(n) drops
0279    the low n bits off the accumulator.  INITBITS() clears the accumulator
0280    and sets the number of available bits to zero.  BYTEBITS() discards just
0281    enough bits to put the accumulator on a byte boundary.  After BYTEBITS()
0282    and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
0283 
0284    NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
0285    if there is no input available.  The decoding of variable length codes uses
0286    PULLBYTE() directly in order to pull just enough bytes to decode the next
0287    code, and no more.
0288 
0289    Some states loop until they get enough input, making sure that enough
0290    state information is maintained to continue the loop where it left off
0291    if NEEDBITS() returns in the loop.  For example, want, need, and keep
0292    would all have to actually be part of the saved state in case NEEDBITS()
0293    returns:
0294 
0295     case STATEw:
0296         while (want < need) {
0297             NEEDBITS(n);
0298             keep[want++] = BITS(n);
0299             DROPBITS(n);
0300         }
0301         state = STATEx;
0302     case STATEx:
0303 
0304    As shown above, if the next state is also the next case, then the break
0305    is omitted.
0306 
0307    A state may also return if there is not enough output space available to
0308    complete that state.  Those states are copying stored data, writing a
0309    literal byte, and copying a matching string.
0310 
0311    When returning, a "goto inf_leave" is used to update the total counters,
0312    update the check value, and determine whether any progress has been made
0313    during that inflate() call in order to return the proper return code.
0314    Progress is defined as a change in either strm->avail_in or strm->avail_out.
0315    When there is a window, goto inf_leave will update the window with the last
0316    output written.  If a goto inf_leave occurs in the middle of decompression
0317    and there is no window currently, goto inf_leave will create one and copy
0318    output to the window for the next call of inflate().
0319 
0320    In this implementation, the flush parameter of inflate() only affects the
0321    return code (per zlib.h).  inflate() always writes as much as possible to
0322    strm->next_out, given the space available and the provided input--the effect
0323    documented in zlib.h of Z_SYNC_FLUSH.  Furthermore, inflate() always defers
0324    the allocation of and copying into a sliding window until necessary, which
0325    provides the effect documented in zlib.h for Z_FINISH when the entire input
0326    stream available.  So the only thing the flush parameter actually does is:
0327    when flush is set to Z_FINISH, inflate() cannot return Z_OK.  Instead it
0328    will return Z_BUF_ERROR if it has not reached the end of the stream.
0329  */
0330 
0331 int zlib_inflate(z_streamp strm, int flush)
0332 {
0333     struct inflate_state *state;
0334     const unsigned char *next;  /* next input */
0335     unsigned char *put;         /* next output */
0336     unsigned have, left;        /* available input and output */
0337     unsigned long hold;         /* bit buffer */
0338     unsigned bits;              /* bits in bit buffer */
0339     unsigned in, out;           /* save starting available input and output */
0340     unsigned copy;              /* number of stored or match bytes to copy */
0341     unsigned char *from;        /* where to copy match bytes from */
0342     code this;                  /* current decoding table entry */
0343     code last;                  /* parent table entry */
0344     unsigned len;               /* length to copy for repeats, bits to drop */
0345     int ret;                    /* return code */
0346     static const unsigned short order[19] = /* permutation of code lengths */
0347         {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
0348 
0349     /* Do not check for strm->next_out == NULL here as ppc zImage
0350        inflates to strm->next_out = 0 */
0351 
0352     if (strm == NULL || strm->state == NULL ||
0353         (strm->next_in == NULL && strm->avail_in != 0))
0354         return Z_STREAM_ERROR;
0355 
0356     state = (struct inflate_state *)strm->state;
0357 
0358     if (state->mode == TYPE) state->mode = TYPEDO;      /* skip check */
0359     LOAD();
0360     in = have;
0361     out = left;
0362     ret = Z_OK;
0363     for (;;)
0364         switch (state->mode) {
0365         case HEAD:
0366             if (state->wrap == 0) {
0367                 state->mode = TYPEDO;
0368                 break;
0369             }
0370             NEEDBITS(16);
0371             if (
0372                 ((BITS(8) << 8) + (hold >> 8)) % 31) {
0373                 strm->msg = (char *)"incorrect header check";
0374                 state->mode = BAD;
0375                 break;
0376             }
0377             if (BITS(4) != Z_DEFLATED) {
0378                 strm->msg = (char *)"unknown compression method";
0379                 state->mode = BAD;
0380                 break;
0381             }
0382             DROPBITS(4);
0383             len = BITS(4) + 8;
0384             if (len > state->wbits) {
0385                 strm->msg = (char *)"invalid window size";
0386                 state->mode = BAD;
0387                 break;
0388             }
0389             state->dmax = 1U << len;
0390             strm->adler = state->check = zlib_adler32(0L, NULL, 0);
0391             state->mode = hold & 0x200 ? DICTID : TYPE;
0392             INITBITS();
0393             break;
0394         case DICTID:
0395             NEEDBITS(32);
0396             strm->adler = state->check = REVERSE(hold);
0397             INITBITS();
0398             state->mode = DICT;
0399         fallthrough;
0400         case DICT:
0401             if (state->havedict == 0) {
0402                 RESTORE();
0403                 return Z_NEED_DICT;
0404             }
0405             strm->adler = state->check = zlib_adler32(0L, NULL, 0);
0406             state->mode = TYPE;
0407         fallthrough;
0408         case TYPE:
0409             if (flush == Z_BLOCK) goto inf_leave;
0410         fallthrough;
0411         case TYPEDO:
0412             INFLATE_TYPEDO_HOOK(strm, flush);
0413             if (state->last) {
0414                 BYTEBITS();
0415                 state->mode = CHECK;
0416                 break;
0417             }
0418             NEEDBITS(3);
0419             state->last = BITS(1);
0420             DROPBITS(1);
0421             switch (BITS(2)) {
0422             case 0:                             /* stored block */
0423                 state->mode = STORED;
0424                 break;
0425             case 1:                             /* fixed block */
0426                 zlib_fixedtables(state);
0427                 state->mode = LEN;              /* decode codes */
0428                 break;
0429             case 2:                             /* dynamic block */
0430                 state->mode = TABLE;
0431                 break;
0432             case 3:
0433                 strm->msg = (char *)"invalid block type";
0434                 state->mode = BAD;
0435             }
0436             DROPBITS(2);
0437             break;
0438         case STORED:
0439             BYTEBITS();                         /* go to byte boundary */
0440             NEEDBITS(32);
0441             if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
0442                 strm->msg = (char *)"invalid stored block lengths";
0443                 state->mode = BAD;
0444                 break;
0445             }
0446             state->length = (unsigned)hold & 0xffff;
0447             INITBITS();
0448             state->mode = COPY;
0449         fallthrough;
0450         case COPY:
0451             copy = state->length;
0452             if (copy) {
0453                 if (copy > have) copy = have;
0454                 if (copy > left) copy = left;
0455                 if (copy == 0) goto inf_leave;
0456                 memcpy(put, next, copy);
0457                 have -= copy;
0458                 next += copy;
0459                 left -= copy;
0460                 put += copy;
0461                 state->length -= copy;
0462                 break;
0463             }
0464             state->mode = TYPE;
0465             break;
0466         case TABLE:
0467             NEEDBITS(14);
0468             state->nlen = BITS(5) + 257;
0469             DROPBITS(5);
0470             state->ndist = BITS(5) + 1;
0471             DROPBITS(5);
0472             state->ncode = BITS(4) + 4;
0473             DROPBITS(4);
0474 #ifndef PKZIP_BUG_WORKAROUND
0475             if (state->nlen > 286 || state->ndist > 30) {
0476                 strm->msg = (char *)"too many length or distance symbols";
0477                 state->mode = BAD;
0478                 break;
0479             }
0480 #endif
0481             state->have = 0;
0482             state->mode = LENLENS;
0483         fallthrough;
0484         case LENLENS:
0485             while (state->have < state->ncode) {
0486                 NEEDBITS(3);
0487                 state->lens[order[state->have++]] = (unsigned short)BITS(3);
0488                 DROPBITS(3);
0489             }
0490             while (state->have < 19)
0491                 state->lens[order[state->have++]] = 0;
0492             state->next = state->codes;
0493             state->lencode = (code const *)(state->next);
0494             state->lenbits = 7;
0495             ret = zlib_inflate_table(CODES, state->lens, 19, &(state->next),
0496                                 &(state->lenbits), state->work);
0497             if (ret) {
0498                 strm->msg = (char *)"invalid code lengths set";
0499                 state->mode = BAD;
0500                 break;
0501             }
0502             state->have = 0;
0503             state->mode = CODELENS;
0504         fallthrough;
0505         case CODELENS:
0506             while (state->have < state->nlen + state->ndist) {
0507                 for (;;) {
0508                     this = state->lencode[BITS(state->lenbits)];
0509                     if ((unsigned)(this.bits) <= bits) break;
0510                     PULLBYTE();
0511                 }
0512                 if (this.val < 16) {
0513                     NEEDBITS(this.bits);
0514                     DROPBITS(this.bits);
0515                     state->lens[state->have++] = this.val;
0516                 }
0517                 else {
0518                     if (this.val == 16) {
0519                         NEEDBITS(this.bits + 2);
0520                         DROPBITS(this.bits);
0521                         if (state->have == 0) {
0522                             strm->msg = (char *)"invalid bit length repeat";
0523                             state->mode = BAD;
0524                             break;
0525                         }
0526                         len = state->lens[state->have - 1];
0527                         copy = 3 + BITS(2);
0528                         DROPBITS(2);
0529                     }
0530                     else if (this.val == 17) {
0531                         NEEDBITS(this.bits + 3);
0532                         DROPBITS(this.bits);
0533                         len = 0;
0534                         copy = 3 + BITS(3);
0535                         DROPBITS(3);
0536                     }
0537                     else {
0538                         NEEDBITS(this.bits + 7);
0539                         DROPBITS(this.bits);
0540                         len = 0;
0541                         copy = 11 + BITS(7);
0542                         DROPBITS(7);
0543                     }
0544                     if (state->have + copy > state->nlen + state->ndist) {
0545                         strm->msg = (char *)"invalid bit length repeat";
0546                         state->mode = BAD;
0547                         break;
0548                     }
0549                     while (copy--)
0550                         state->lens[state->have++] = (unsigned short)len;
0551                 }
0552             }
0553 
0554             /* handle error breaks in while */
0555             if (state->mode == BAD) break;
0556 
0557             /* build code tables */
0558             state->next = state->codes;
0559             state->lencode = (code const *)(state->next);
0560             state->lenbits = 9;
0561             ret = zlib_inflate_table(LENS, state->lens, state->nlen, &(state->next),
0562                                 &(state->lenbits), state->work);
0563             if (ret) {
0564                 strm->msg = (char *)"invalid literal/lengths set";
0565                 state->mode = BAD;
0566                 break;
0567             }
0568             state->distcode = (code const *)(state->next);
0569             state->distbits = 6;
0570             ret = zlib_inflate_table(DISTS, state->lens + state->nlen, state->ndist,
0571                             &(state->next), &(state->distbits), state->work);
0572             if (ret) {
0573                 strm->msg = (char *)"invalid distances set";
0574                 state->mode = BAD;
0575                 break;
0576             }
0577             state->mode = LEN;
0578         fallthrough;
0579         case LEN:
0580             if (have >= 6 && left >= 258) {
0581                 RESTORE();
0582                 inflate_fast(strm, out);
0583                 LOAD();
0584                 break;
0585             }
0586             for (;;) {
0587                 this = state->lencode[BITS(state->lenbits)];
0588                 if ((unsigned)(this.bits) <= bits) break;
0589                 PULLBYTE();
0590             }
0591             if (this.op && (this.op & 0xf0) == 0) {
0592                 last = this;
0593                 for (;;) {
0594                     this = state->lencode[last.val +
0595                             (BITS(last.bits + last.op) >> last.bits)];
0596                     if ((unsigned)(last.bits + this.bits) <= bits) break;
0597                     PULLBYTE();
0598                 }
0599                 DROPBITS(last.bits);
0600             }
0601             DROPBITS(this.bits);
0602             state->length = (unsigned)this.val;
0603             if ((int)(this.op) == 0) {
0604                 state->mode = LIT;
0605                 break;
0606             }
0607             if (this.op & 32) {
0608                 state->mode = TYPE;
0609                 break;
0610             }
0611             if (this.op & 64) {
0612                 strm->msg = (char *)"invalid literal/length code";
0613                 state->mode = BAD;
0614                 break;
0615             }
0616             state->extra = (unsigned)(this.op) & 15;
0617             state->mode = LENEXT;
0618         fallthrough;
0619         case LENEXT:
0620             if (state->extra) {
0621                 NEEDBITS(state->extra);
0622                 state->length += BITS(state->extra);
0623                 DROPBITS(state->extra);
0624             }
0625             state->mode = DIST;
0626         fallthrough;
0627         case DIST:
0628             for (;;) {
0629                 this = state->distcode[BITS(state->distbits)];
0630                 if ((unsigned)(this.bits) <= bits) break;
0631                 PULLBYTE();
0632             }
0633             if ((this.op & 0xf0) == 0) {
0634                 last = this;
0635                 for (;;) {
0636                     this = state->distcode[last.val +
0637                             (BITS(last.bits + last.op) >> last.bits)];
0638                     if ((unsigned)(last.bits + this.bits) <= bits) break;
0639                     PULLBYTE();
0640                 }
0641                 DROPBITS(last.bits);
0642             }
0643             DROPBITS(this.bits);
0644             if (this.op & 64) {
0645                 strm->msg = (char *)"invalid distance code";
0646                 state->mode = BAD;
0647                 break;
0648             }
0649             state->offset = (unsigned)this.val;
0650             state->extra = (unsigned)(this.op) & 15;
0651             state->mode = DISTEXT;
0652         fallthrough;
0653         case DISTEXT:
0654             if (state->extra) {
0655                 NEEDBITS(state->extra);
0656                 state->offset += BITS(state->extra);
0657                 DROPBITS(state->extra);
0658             }
0659 #ifdef INFLATE_STRICT
0660             if (state->offset > state->dmax) {
0661                 strm->msg = (char *)"invalid distance too far back";
0662                 state->mode = BAD;
0663                 break;
0664             }
0665 #endif
0666             if (state->offset > state->whave + out - left) {
0667                 strm->msg = (char *)"invalid distance too far back";
0668                 state->mode = BAD;
0669                 break;
0670             }
0671             state->mode = MATCH;
0672         fallthrough;
0673         case MATCH:
0674             if (left == 0) goto inf_leave;
0675             copy = out - left;
0676             if (state->offset > copy) {         /* copy from window */
0677                 copy = state->offset - copy;
0678                 if (copy > state->write) {
0679                     copy -= state->write;
0680                     from = state->window + (state->wsize - copy);
0681                 }
0682                 else
0683                     from = state->window + (state->write - copy);
0684                 if (copy > state->length) copy = state->length;
0685             }
0686             else {                              /* copy from output */
0687                 from = put - state->offset;
0688                 copy = state->length;
0689             }
0690             if (copy > left) copy = left;
0691             left -= copy;
0692             state->length -= copy;
0693             do {
0694                 *put++ = *from++;
0695             } while (--copy);
0696             if (state->length == 0) state->mode = LEN;
0697             break;
0698         case LIT:
0699             if (left == 0) goto inf_leave;
0700             *put++ = (unsigned char)(state->length);
0701             left--;
0702             state->mode = LEN;
0703             break;
0704         case CHECK:
0705             if (state->wrap) {
0706                 NEEDBITS(32);
0707                 out -= left;
0708                 strm->total_out += out;
0709                 state->total += out;
0710                 if (INFLATE_NEED_CHECKSUM(strm) && out)
0711                     strm->adler = state->check =
0712                         UPDATE(state->check, put - out, out);
0713                 out = left;
0714                 if ((
0715                      REVERSE(hold)) != state->check) {
0716                     strm->msg = (char *)"incorrect data check";
0717                     state->mode = BAD;
0718                     break;
0719                 }
0720                 INITBITS();
0721             }
0722             state->mode = DONE;
0723         fallthrough;
0724         case DONE:
0725             ret = Z_STREAM_END;
0726             goto inf_leave;
0727         case BAD:
0728             ret = Z_DATA_ERROR;
0729             goto inf_leave;
0730         case MEM:
0731             return Z_MEM_ERROR;
0732         case SYNC:
0733         default:
0734             return Z_STREAM_ERROR;
0735         }
0736 
0737     /*
0738        Return from inflate(), updating the total counts and the check value.
0739        If there was no progress during the inflate() call, return a buffer
0740        error.  Call zlib_updatewindow() to create and/or update the window state.
0741      */
0742   inf_leave:
0743     RESTORE();
0744     if (INFLATE_NEED_UPDATEWINDOW(strm) &&
0745             (state->wsize || (state->mode < CHECK && out != strm->avail_out)))
0746         zlib_updatewindow(strm, out);
0747 
0748     in -= strm->avail_in;
0749     out -= strm->avail_out;
0750     strm->total_in += in;
0751     strm->total_out += out;
0752     state->total += out;
0753     if (INFLATE_NEED_CHECKSUM(strm) && state->wrap && out)
0754         strm->adler = state->check =
0755             UPDATE(state->check, strm->next_out - out, out);
0756 
0757     strm->data_type = state->bits + (state->last ? 64 : 0) +
0758                       (state->mode == TYPE ? 128 : 0);
0759 
0760     if (flush == Z_PACKET_FLUSH && ret == Z_OK &&
0761             strm->avail_out != 0 && strm->avail_in == 0)
0762         return zlib_inflateSyncPacket(strm);
0763 
0764     if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
0765         ret = Z_BUF_ERROR;
0766 
0767     return ret;
0768 }
0769 
0770 int zlib_inflateEnd(z_streamp strm)
0771 {
0772     if (strm == NULL || strm->state == NULL)
0773         return Z_STREAM_ERROR;
0774     return Z_OK;
0775 }
0776 
0777 /*
0778  * This subroutine adds the data at next_in/avail_in to the output history
0779  * without performing any output.  The output buffer must be "caught up";
0780  * i.e. no pending output but this should always be the case. The state must
0781  * be waiting on the start of a block (i.e. mode == TYPE or HEAD).  On exit,
0782  * the output will also be caught up, and the checksum will have been updated
0783  * if need be.
0784  */
0785 int zlib_inflateIncomp(z_stream *z)
0786 {
0787     struct inflate_state *state = (struct inflate_state *)z->state;
0788     Byte *saved_no = z->next_out;
0789     uInt saved_ao = z->avail_out;
0790 
0791     if (state->mode != TYPE && state->mode != HEAD)
0792     return Z_DATA_ERROR;
0793 
0794     /* Setup some variables to allow misuse of updateWindow */
0795     z->avail_out = 0;
0796     z->next_out = (unsigned char*)z->next_in + z->avail_in;
0797 
0798     zlib_updatewindow(z, z->avail_in);
0799 
0800     /* Restore saved variables */
0801     z->avail_out = saved_ao;
0802     z->next_out = saved_no;
0803 
0804     z->adler = state->check =
0805         UPDATE(state->check, z->next_in, z->avail_in);
0806 
0807     z->total_out += z->avail_in;
0808     z->total_in += z->avail_in;
0809     z->next_in += z->avail_in;
0810     state->total += z->avail_in;
0811     z->avail_in = 0;
0812 
0813     return Z_OK;
0814 }