LCOV - code coverage report
Current view: top level - lib/zlib_inflate - inflate.c (source / functions) Hit Total Coverage
Test: landlock.info Lines: 0 432 0.0 %
Date: 2021-04-22 12:43:58 Functions: 0 8 0.0 %

          Line data    Source code
       1             : /* inflate.c -- zlib decompression
       2             :  * Copyright (C) 1995-2005 Mark Adler
       3             :  * For conditions of distribution and use, see copyright notice in zlib.h
       4             :  *
       5             :  * Based on zlib 1.2.3 but modified for the Linux Kernel by
       6             :  * Richard Purdie <richard@openedhand.com>
       7             :  *
       8             :  * Changes mainly for static instead of dynamic memory allocation
       9             :  *
      10             :  */
      11             : 
      12             : #include <linux/zutil.h>
      13             : #include "inftrees.h"
      14             : #include "inflate.h"
      15             : #include "inffast.h"
      16             : #include "infutil.h"
      17             : 
      18             : /* architecture-specific bits */
      19             : #ifdef CONFIG_ZLIB_DFLTCC
      20             : #  include "../zlib_dfltcc/dfltcc.h"
      21             : #else
      22             : #define INFLATE_RESET_HOOK(strm) do {} while (0)
      23             : #define INFLATE_TYPEDO_HOOK(strm, flush) do {} while (0)
      24             : #define INFLATE_NEED_UPDATEWINDOW(strm) 1
      25             : #define INFLATE_NEED_CHECKSUM(strm) 1
      26             : #endif
      27             : 
      28           0 : int zlib_inflate_workspacesize(void)
      29             : {
      30           0 :     return sizeof(struct inflate_workspace);
      31             : }
      32             : 
      33           0 : int zlib_inflateReset(z_streamp strm)
      34             : {
      35           0 :     struct inflate_state *state;
      36             : 
      37           0 :     if (strm == NULL || strm->state == NULL) return Z_STREAM_ERROR;
      38           0 :     state = (struct inflate_state *)strm->state;
      39           0 :     strm->total_in = strm->total_out = state->total = 0;
      40           0 :     strm->msg = NULL;
      41           0 :     strm->adler = 1;        /* to support ill-conceived Java test suite */
      42           0 :     state->mode = HEAD;
      43           0 :     state->last = 0;
      44           0 :     state->havedict = 0;
      45           0 :     state->dmax = 32768U;
      46           0 :     state->hold = 0;
      47           0 :     state->bits = 0;
      48           0 :     state->lencode = state->distcode = state->next = state->codes;
      49             : 
      50             :     /* Initialise Window */
      51           0 :     state->wsize = 1U << state->wbits;
      52           0 :     state->write = 0;
      53           0 :     state->whave = 0;
      54             : 
      55           0 :     INFLATE_RESET_HOOK(strm);
      56           0 :     return Z_OK;
      57             : }
      58             : 
      59           0 : int zlib_inflateInit2(z_streamp strm, int windowBits)
      60             : {
      61           0 :     struct inflate_state *state;
      62             : 
      63           0 :     if (strm == NULL) return Z_STREAM_ERROR;
      64           0 :     strm->msg = NULL;                 /* in case we return an error */
      65             : 
      66           0 :     state = &WS(strm)->inflate_state;
      67           0 :     strm->state = (struct internal_state *)state;
      68             : 
      69           0 :     if (windowBits < 0) {
      70           0 :         state->wrap = 0;
      71           0 :         windowBits = -windowBits;
      72             :     }
      73             :     else {
      74           0 :         state->wrap = (windowBits >> 4) + 1;
      75             :     }
      76           0 :     if (windowBits < 8 || windowBits > 15) {
      77             :         return Z_STREAM_ERROR;
      78             :     }
      79           0 :     state->wbits = (unsigned)windowBits;
      80             : #ifdef CONFIG_ZLIB_DFLTCC
      81             :     /*
      82             :      * DFLTCC requires the window to be page aligned.
      83             :      * Thus, we overallocate and take the aligned portion of the buffer.
      84             :      */
      85             :     state->window = PTR_ALIGN(&WS(strm)->working_window[0], PAGE_SIZE);
      86             : #else
      87           0 :     state->window = &WS(strm)->working_window[0];
      88             : #endif
      89             : 
      90           0 :     return zlib_inflateReset(strm);
      91             : }
      92             : 
      93             : /*
      94             :    Return state with length and distance decoding tables and index sizes set to
      95             :    fixed code decoding.  This returns fixed tables from inffixed.h.
      96             :  */
      97           0 : static void zlib_fixedtables(struct inflate_state *state)
      98             : {
      99             : #   include "inffixed.h"
     100           0 :     state->lencode = lenfix;
     101           0 :     state->lenbits = 9;
     102           0 :     state->distcode = distfix;
     103           0 :     state->distbits = 5;
     104             : }
     105             : 
     106             : 
     107             : /*
     108             :    Update the window with the last wsize (normally 32K) bytes written before
     109             :    returning. This is only called when a window is already in use, or when
     110             :    output has been written during this inflate call, but the end of the deflate
     111             :    stream has not been reached yet. It is also called to window dictionary data
     112             :    when a dictionary is loaded.
     113             : 
     114             :    Providing output buffers larger than 32K to inflate() should provide a speed
     115             :    advantage, since only the last 32K of output is copied to the sliding window
     116             :    upon return from inflate(), and since all distances after the first 32K of
     117             :    output will fall in the output data, making match copies simpler and faster.
     118             :    The advantage may be dependent on the size of the processor's data caches.
     119             :  */
     120           0 : static void zlib_updatewindow(z_streamp strm, unsigned out)
     121             : {
     122           0 :     struct inflate_state *state;
     123           0 :     unsigned copy, dist;
     124             : 
     125           0 :     state = (struct inflate_state *)strm->state;
     126             : 
     127             :     /* copy state->wsize or less output bytes into the circular window */
     128           0 :     copy = out - strm->avail_out;
     129           0 :     if (copy >= state->wsize) {
     130           0 :         memcpy(state->window, strm->next_out - state->wsize, state->wsize);
     131           0 :         state->write = 0;
     132           0 :         state->whave = state->wsize;
     133             :     }
     134             :     else {
     135           0 :         dist = state->wsize - state->write;
     136           0 :         if (dist > copy) dist = copy;
     137           0 :         memcpy(state->window + state->write, strm->next_out - copy, dist);
     138           0 :         copy -= dist;
     139           0 :         if (copy) {
     140           0 :             memcpy(state->window, strm->next_out - copy, copy);
     141           0 :             state->write = copy;
     142           0 :             state->whave = state->wsize;
     143             :         }
     144             :         else {
     145           0 :             state->write += dist;
     146           0 :             if (state->write == state->wsize) state->write = 0;
     147           0 :             if (state->whave < state->wsize) state->whave += dist;
     148             :         }
     149             :     }
     150           0 : }
     151             : 
     152             : 
     153             : /*
     154             :  * At the end of a Deflate-compressed PPP packet, we expect to have seen
     155             :  * a `stored' block type value but not the (zero) length bytes.
     156             :  */
     157             : /*
     158             :    Returns true if inflate is currently at the end of a block generated by
     159             :    Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
     160             :    implementation to provide an additional safety check. PPP uses
     161             :    Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
     162             :    block. When decompressing, PPP checks that at the end of input packet,
     163             :    inflate is waiting for these length bytes.
     164             :  */
     165           0 : static int zlib_inflateSyncPacket(z_streamp strm)
     166             : {
     167           0 :     struct inflate_state *state;
     168             : 
     169           0 :     if (strm == NULL || strm->state == NULL) return Z_STREAM_ERROR;
     170           0 :     state = (struct inflate_state *)strm->state;
     171             : 
     172           0 :     if (state->mode == STORED && state->bits == 0) {
     173           0 :         state->mode = TYPE;
     174           0 :         return Z_OK;
     175             :     }
     176             :     return Z_DATA_ERROR;
     177             : }
     178             : 
     179             : /* Macros for inflate(): */
     180             : 
     181             : /* check function to use adler32() for zlib or crc32() for gzip */
     182             : #define UPDATE(check, buf, len) zlib_adler32(check, buf, len)
     183             : 
     184             : /* Load registers with state in inflate() for speed */
     185             : #define LOAD() \
     186             :     do { \
     187             :         put = strm->next_out; \
     188             :         left = strm->avail_out; \
     189             :         next = strm->next_in; \
     190             :         have = strm->avail_in; \
     191             :         hold = state->hold; \
     192             :         bits = state->bits; \
     193             :     } while (0)
     194             : 
     195             : /* Restore state from registers in inflate() */
     196             : #define RESTORE() \
     197             :     do { \
     198             :         strm->next_out = put; \
     199             :         strm->avail_out = left; \
     200             :         strm->next_in = next; \
     201             :         strm->avail_in = have; \
     202             :         state->hold = hold; \
     203             :         state->bits = bits; \
     204             :     } while (0)
     205             : 
     206             : /* Clear the input bit accumulator */
     207             : #define INITBITS() \
     208             :     do { \
     209             :         hold = 0; \
     210             :         bits = 0; \
     211             :     } while (0)
     212             : 
     213             : /* Get a byte of input into the bit accumulator, or return from inflate()
     214             :    if there is no input available. */
     215             : #define PULLBYTE() \
     216             :     do { \
     217             :         if (have == 0) goto inf_leave; \
     218             :         have--; \
     219             :         hold += (unsigned long)(*next++) << bits; \
     220             :         bits += 8; \
     221             :     } while (0)
     222             : 
     223             : /* Assure that there are at least n bits in the bit accumulator.  If there is
     224             :    not enough available input to do that, then return from inflate(). */
     225             : #define NEEDBITS(n) \
     226             :     do { \
     227             :         while (bits < (unsigned)(n)) \
     228             :             PULLBYTE(); \
     229             :     } while (0)
     230             : 
     231             : /* Return the low n bits of the bit accumulator (n < 16) */
     232             : #define BITS(n) \
     233             :     ((unsigned)hold & ((1U << (n)) - 1))
     234             : 
     235             : /* Remove n bits from the bit accumulator */
     236             : #define DROPBITS(n) \
     237             :     do { \
     238             :         hold >>= (n); \
     239             :         bits -= (unsigned)(n); \
     240             :     } while (0)
     241             : 
     242             : /* Remove zero to seven bits as needed to go to a byte boundary */
     243             : #define BYTEBITS() \
     244             :     do { \
     245             :         hold >>= bits & 7; \
     246             :         bits -= bits & 7; \
     247             :     } while (0)
     248             : 
     249             : /*
     250             :    inflate() uses a state machine to process as much input data and generate as
     251             :    much output data as possible before returning.  The state machine is
     252             :    structured roughly as follows:
     253             : 
     254             :     for (;;) switch (state) {
     255             :     ...
     256             :     case STATEn:
     257             :         if (not enough input data or output space to make progress)
     258             :             return;
     259             :         ... make progress ...
     260             :         state = STATEm;
     261             :         break;
     262             :     ...
     263             :     }
     264             : 
     265             :    so when inflate() is called again, the same case is attempted again, and
     266             :    if the appropriate resources are provided, the machine proceeds to the
     267             :    next state.  The NEEDBITS() macro is usually the way the state evaluates
     268             :    whether it can proceed or should return.  NEEDBITS() does the return if
     269             :    the requested bits are not available.  The typical use of the BITS macros
     270             :    is:
     271             : 
     272             :         NEEDBITS(n);
     273             :         ... do something with BITS(n) ...
     274             :         DROPBITS(n);
     275             : 
     276             :    where NEEDBITS(n) either returns from inflate() if there isn't enough
     277             :    input left to load n bits into the accumulator, or it continues.  BITS(n)
     278             :    gives the low n bits in the accumulator.  When done, DROPBITS(n) drops
     279             :    the low n bits off the accumulator.  INITBITS() clears the accumulator
     280             :    and sets the number of available bits to zero.  BYTEBITS() discards just
     281             :    enough bits to put the accumulator on a byte boundary.  After BYTEBITS()
     282             :    and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
     283             : 
     284             :    NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
     285             :    if there is no input available.  The decoding of variable length codes uses
     286             :    PULLBYTE() directly in order to pull just enough bytes to decode the next
     287             :    code, and no more.
     288             : 
     289             :    Some states loop until they get enough input, making sure that enough
     290             :    state information is maintained to continue the loop where it left off
     291             :    if NEEDBITS() returns in the loop.  For example, want, need, and keep
     292             :    would all have to actually be part of the saved state in case NEEDBITS()
     293             :    returns:
     294             : 
     295             :     case STATEw:
     296             :         while (want < need) {
     297             :             NEEDBITS(n);
     298             :             keep[want++] = BITS(n);
     299             :             DROPBITS(n);
     300             :         }
     301             :         state = STATEx;
     302             :     case STATEx:
     303             : 
     304             :    As shown above, if the next state is also the next case, then the break
     305             :    is omitted.
     306             : 
     307             :    A state may also return if there is not enough output space available to
     308             :    complete that state.  Those states are copying stored data, writing a
     309             :    literal byte, and copying a matching string.
     310             : 
     311             :    When returning, a "goto inf_leave" is used to update the total counters,
     312             :    update the check value, and determine whether any progress has been made
     313             :    during that inflate() call in order to return the proper return code.
     314             :    Progress is defined as a change in either strm->avail_in or strm->avail_out.
     315             :    When there is a window, goto inf_leave will update the window with the last
     316             :    output written.  If a goto inf_leave occurs in the middle of decompression
     317             :    and there is no window currently, goto inf_leave will create one and copy
     318             :    output to the window for the next call of inflate().
     319             : 
     320             :    In this implementation, the flush parameter of inflate() only affects the
     321             :    return code (per zlib.h).  inflate() always writes as much as possible to
     322             :    strm->next_out, given the space available and the provided input--the effect
     323             :    documented in zlib.h of Z_SYNC_FLUSH.  Furthermore, inflate() always defers
     324             :    the allocation of and copying into a sliding window until necessary, which
     325             :    provides the effect documented in zlib.h for Z_FINISH when the entire input
     326             :    stream available.  So the only thing the flush parameter actually does is:
     327             :    when flush is set to Z_FINISH, inflate() cannot return Z_OK.  Instead it
     328             :    will return Z_BUF_ERROR if it has not reached the end of the stream.
     329             :  */
     330             : 
     331           0 : int zlib_inflate(z_streamp strm, int flush)
     332             : {
     333           0 :     struct inflate_state *state;
     334           0 :     const unsigned char *next;  /* next input */
     335           0 :     unsigned char *put;         /* next output */
     336           0 :     unsigned have, left;        /* available input and output */
     337           0 :     unsigned long hold;         /* bit buffer */
     338           0 :     unsigned bits;              /* bits in bit buffer */
     339           0 :     unsigned in, out;           /* save starting available input and output */
     340           0 :     unsigned copy;              /* number of stored or match bytes to copy */
     341           0 :     unsigned char *from;        /* where to copy match bytes from */
     342           0 :     code this;                  /* current decoding table entry */
     343           0 :     code last;                  /* parent table entry */
     344           0 :     unsigned len;               /* length to copy for repeats, bits to drop */
     345           0 :     int ret;                    /* return code */
     346           0 :     static const unsigned short order[19] = /* permutation of code lengths */
     347             :         {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
     348             : 
     349             :     /* Do not check for strm->next_out == NULL here as ppc zImage
     350             :        inflates to strm->next_out = 0 */
     351             : 
     352           0 :     if (strm == NULL || strm->state == NULL ||
     353           0 :         (strm->next_in == NULL && strm->avail_in != 0))
     354             :         return Z_STREAM_ERROR;
     355             : 
     356           0 :     state = (struct inflate_state *)strm->state;
     357             : 
     358           0 :     if (state->mode == TYPE) state->mode = TYPEDO;      /* skip check */
     359           0 :     LOAD();
     360           0 :     in = have;
     361           0 :     out = left;
     362           0 :     ret = Z_OK;
     363           0 :     for (;;)
     364           0 :         switch (state->mode) {
     365           0 :         case HEAD:
     366           0 :             if (state->wrap == 0) {
     367           0 :                 state->mode = TYPEDO;
     368           0 :                 break;
     369             :             }
     370           0 :             NEEDBITS(16);
     371           0 :             if (
     372           0 :                 ((BITS(8) << 8) + (hold >> 8)) % 31) {
     373           0 :                 strm->msg = (char *)"incorrect header check";
     374           0 :                 state->mode = BAD;
     375           0 :                 break;
     376             :             }
     377           0 :             if (BITS(4) != Z_DEFLATED) {
     378           0 :                 strm->msg = (char *)"unknown compression method";
     379           0 :                 state->mode = BAD;
     380           0 :                 break;
     381             :             }
     382           0 :             DROPBITS(4);
     383           0 :             len = BITS(4) + 8;
     384           0 :             if (len > state->wbits) {
     385           0 :                 strm->msg = (char *)"invalid window size";
     386           0 :                 state->mode = BAD;
     387           0 :                 break;
     388             :             }
     389           0 :             state->dmax = 1U << len;
     390           0 :             strm->adler = state->check = zlib_adler32(0L, NULL, 0);
     391           0 :             state->mode = hold & 0x200 ? DICTID : TYPE;
     392           0 :             INITBITS();
     393           0 :             break;
     394             :         case DICTID:
     395           0 :             NEEDBITS(32);
     396           0 :             strm->adler = state->check = REVERSE(hold);
     397           0 :             INITBITS();
     398           0 :             state->mode = DICT;
     399           0 :             fallthrough;
     400           0 :         case DICT:
     401           0 :             if (state->havedict == 0) {
     402           0 :                 RESTORE();
     403           0 :                 return Z_NEED_DICT;
     404             :             }
     405           0 :             strm->adler = state->check = zlib_adler32(0L, NULL, 0);
     406           0 :             state->mode = TYPE;
     407           0 :             fallthrough;
     408           0 :         case TYPE:
     409           0 :             if (flush == Z_BLOCK) goto inf_leave;
     410           0 :             fallthrough;
     411             :         case TYPEDO:
     412           0 :             INFLATE_TYPEDO_HOOK(strm, flush);
     413           0 :             if (state->last) {
     414           0 :                 BYTEBITS();
     415           0 :                 state->mode = CHECK;
     416           0 :                 break;
     417             :             }
     418           0 :             NEEDBITS(3);
     419           0 :             state->last = BITS(1);
     420           0 :             DROPBITS(1);
     421           0 :             switch (BITS(2)) {
     422           0 :             case 0:                             /* stored block */
     423           0 :                 state->mode = STORED;
     424           0 :                 break;
     425             :             case 1:                             /* fixed block */
     426           0 :                 zlib_fixedtables(state);
     427           0 :                 state->mode = LEN;              /* decode codes */
     428           0 :                 break;
     429           0 :             case 2:                             /* dynamic block */
     430           0 :                 state->mode = TABLE;
     431           0 :                 break;
     432           0 :             case 3:
     433           0 :                 strm->msg = (char *)"invalid block type";
     434           0 :                 state->mode = BAD;
     435             :             }
     436           0 :             DROPBITS(2);
     437           0 :             break;
     438           0 :         case STORED:
     439           0 :             BYTEBITS();                         /* go to byte boundary */
     440           0 :             NEEDBITS(32);
     441           0 :             if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
     442           0 :                 strm->msg = (char *)"invalid stored block lengths";
     443           0 :                 state->mode = BAD;
     444           0 :                 break;
     445             :             }
     446           0 :             state->length = (unsigned)hold & 0xffff;
     447           0 :             INITBITS();
     448           0 :             state->mode = COPY;
     449           0 :             fallthrough;
     450           0 :         case COPY:
     451           0 :             copy = state->length;
     452           0 :             if (copy) {
     453           0 :                 if (copy > have) copy = have;
     454           0 :                 if (copy > left) copy = left;
     455           0 :                 if (copy == 0) goto inf_leave;
     456           0 :                 memcpy(put, next, copy);
     457           0 :                 have -= copy;
     458           0 :                 next += copy;
     459           0 :                 left -= copy;
     460           0 :                 put += copy;
     461           0 :                 state->length -= copy;
     462           0 :                 break;
     463             :             }
     464           0 :             state->mode = TYPE;
     465           0 :             break;
     466             :         case TABLE:
     467           0 :             NEEDBITS(14);
     468           0 :             state->nlen = BITS(5) + 257;
     469           0 :             DROPBITS(5);
     470           0 :             state->ndist = BITS(5) + 1;
     471           0 :             DROPBITS(5);
     472           0 :             state->ncode = BITS(4) + 4;
     473           0 :             DROPBITS(4);
     474             : #ifndef PKZIP_BUG_WORKAROUND
     475           0 :             if (state->nlen > 286 || state->ndist > 30) {
     476           0 :                 strm->msg = (char *)"too many length or distance symbols";
     477           0 :                 state->mode = BAD;
     478           0 :                 break;
     479             :             }
     480             : #endif
     481           0 :             state->have = 0;
     482           0 :             state->mode = LENLENS;
     483             :             fallthrough;
     484             :         case LENLENS:
     485           0 :             while (state->have < state->ncode) {
     486           0 :                 NEEDBITS(3);
     487           0 :                 state->lens[order[state->have++]] = (unsigned short)BITS(3);
     488           0 :                 DROPBITS(3);
     489             :             }
     490           0 :             while (state->have < 19)
     491           0 :                 state->lens[order[state->have++]] = 0;
     492           0 :             state->next = state->codes;
     493           0 :             state->lencode = (code const *)(state->next);
     494           0 :             state->lenbits = 7;
     495           0 :             ret = zlib_inflate_table(CODES, state->lens, 19, &(state->next),
     496           0 :                                 &(state->lenbits), state->work);
     497           0 :             if (ret) {
     498           0 :                 strm->msg = (char *)"invalid code lengths set";
     499           0 :                 state->mode = BAD;
     500           0 :                 break;
     501             :             }
     502           0 :             state->have = 0;
     503           0 :             state->mode = CODELENS;
     504             :             fallthrough;
     505             :         case CODELENS:
     506           0 :             while (state->have < state->nlen + state->ndist) {
     507           0 :                 for (;;) {
     508           0 :                     this = state->lencode[BITS(state->lenbits)];
     509           0 :                     if ((unsigned)(this.bits) <= bits) break;
     510           0 :                     PULLBYTE();
     511             :                 }
     512           0 :                 if (this.val < 16) {
     513           0 :                     NEEDBITS(this.bits);
     514           0 :                     DROPBITS(this.bits);
     515           0 :                     state->lens[state->have++] = this.val;
     516             :                 }
     517             :                 else {
     518           0 :                     if (this.val == 16) {
     519           0 :                         NEEDBITS(this.bits + 2);
     520           0 :                         DROPBITS(this.bits);
     521           0 :                         if (state->have == 0) {
     522           0 :                             strm->msg = (char *)"invalid bit length repeat";
     523           0 :                             state->mode = BAD;
     524           0 :                             break;
     525             :                         }
     526           0 :                         len = state->lens[state->have - 1];
     527           0 :                         copy = 3 + BITS(2);
     528           0 :                         DROPBITS(2);
     529             :                     }
     530           0 :                     else if (this.val == 17) {
     531           0 :                         NEEDBITS(this.bits + 3);
     532           0 :                         DROPBITS(this.bits);
     533           0 :                         len = 0;
     534           0 :                         copy = 3 + BITS(3);
     535           0 :                         DROPBITS(3);
     536             :                     }
     537             :                     else {
     538           0 :                         NEEDBITS(this.bits + 7);
     539           0 :                         DROPBITS(this.bits);
     540           0 :                         len = 0;
     541           0 :                         copy = 11 + BITS(7);
     542           0 :                         DROPBITS(7);
     543             :                     }
     544           0 :                     if (state->have + copy > state->nlen + state->ndist) {
     545           0 :                         strm->msg = (char *)"invalid bit length repeat";
     546           0 :                         state->mode = BAD;
     547           0 :                         break;
     548             :                     }
     549           0 :                     while (copy--)
     550           0 :                         state->lens[state->have++] = (unsigned short)len;
     551             :                 }
     552             :             }
     553             : 
     554             :             /* handle error breaks in while */
     555           0 :             if (state->mode == BAD) break;
     556             : 
     557             :             /* build code tables */
     558           0 :             state->next = state->codes;
     559           0 :             state->lencode = (code const *)(state->next);
     560           0 :             state->lenbits = 9;
     561           0 :             ret = zlib_inflate_table(LENS, state->lens, state->nlen, &(state->next),
     562           0 :                                 &(state->lenbits), state->work);
     563           0 :             if (ret) {
     564           0 :                 strm->msg = (char *)"invalid literal/lengths set";
     565           0 :                 state->mode = BAD;
     566           0 :                 break;
     567             :             }
     568           0 :             state->distcode = (code const *)(state->next);
     569           0 :             state->distbits = 6;
     570           0 :             ret = zlib_inflate_table(DISTS, state->lens + state->nlen, state->ndist,
     571             :                             &(state->next), &(state->distbits), state->work);
     572           0 :             if (ret) {
     573           0 :                 strm->msg = (char *)"invalid distances set";
     574           0 :                 state->mode = BAD;
     575           0 :                 break;
     576             :             }
     577           0 :             state->mode = LEN;
     578           0 :             fallthrough;
     579           0 :         case LEN:
     580           0 :             if (have >= 6 && left >= 258) {
     581           0 :                 RESTORE();
     582           0 :                 inflate_fast(strm, out);
     583           0 :                 LOAD();
     584           0 :                 break;
     585             :             }
     586           0 :             for (;;) {
     587           0 :                 this = state->lencode[BITS(state->lenbits)];
     588           0 :                 if ((unsigned)(this.bits) <= bits) break;
     589           0 :                 PULLBYTE();
     590             :             }
     591           0 :             if (this.op && (this.op & 0xf0) == 0) {
     592           0 :                 last = this;
     593           0 :                 for (;;) {
     594           0 :                     this = state->lencode[last.val +
     595           0 :                             (BITS(last.bits + last.op) >> last.bits)];
     596           0 :                     if ((unsigned)(last.bits + this.bits) <= bits) break;
     597           0 :                     PULLBYTE();
     598             :                 }
     599           0 :                 DROPBITS(last.bits);
     600             :             }
     601           0 :             DROPBITS(this.bits);
     602           0 :             state->length = (unsigned)this.val;
     603           0 :             if ((int)(this.op) == 0) {
     604           0 :                 state->mode = LIT;
     605           0 :                 break;
     606             :             }
     607           0 :             if (this.op & 32) {
     608           0 :                 state->mode = TYPE;
     609           0 :                 break;
     610             :             }
     611           0 :             if (this.op & 64) {
     612           0 :                 strm->msg = (char *)"invalid literal/length code";
     613           0 :                 state->mode = BAD;
     614           0 :                 break;
     615             :             }
     616           0 :             state->extra = (unsigned)(this.op) & 15;
     617           0 :             state->mode = LENEXT;
     618           0 :             fallthrough;
     619           0 :         case LENEXT:
     620           0 :             if (state->extra) {
     621           0 :                 NEEDBITS(state->extra);
     622           0 :                 state->length += BITS(state->extra);
     623           0 :                 DROPBITS(state->extra);
     624             :             }
     625           0 :             state->mode = DIST;
     626           0 :             fallthrough;
     627           0 :         case DIST:
     628           0 :             for (;;) {
     629           0 :                 this = state->distcode[BITS(state->distbits)];
     630           0 :                 if ((unsigned)(this.bits) <= bits) break;
     631           0 :                 PULLBYTE();
     632             :             }
     633           0 :             if ((this.op & 0xf0) == 0) {
     634           0 :                 last = this;
     635           0 :                 for (;;) {
     636           0 :                     this = state->distcode[last.val +
     637           0 :                             (BITS(last.bits + last.op) >> last.bits)];
     638           0 :                     if ((unsigned)(last.bits + this.bits) <= bits) break;
     639           0 :                     PULLBYTE();
     640             :                 }
     641           0 :                 DROPBITS(last.bits);
     642             :             }
     643           0 :             DROPBITS(this.bits);
     644           0 :             if (this.op & 64) {
     645           0 :                 strm->msg = (char *)"invalid distance code";
     646           0 :                 state->mode = BAD;
     647           0 :                 break;
     648             :             }
     649           0 :             state->offset = (unsigned)this.val;
     650           0 :             state->extra = (unsigned)(this.op) & 15;
     651           0 :             state->mode = DISTEXT;
     652           0 :             fallthrough;
     653           0 :         case DISTEXT:
     654           0 :             if (state->extra) {
     655           0 :                 NEEDBITS(state->extra);
     656           0 :                 state->offset += BITS(state->extra);
     657           0 :                 DROPBITS(state->extra);
     658             :             }
     659             : #ifdef INFLATE_STRICT
     660             :             if (state->offset > state->dmax) {
     661             :                 strm->msg = (char *)"invalid distance too far back";
     662             :                 state->mode = BAD;
     663             :                 break;
     664             :             }
     665             : #endif
     666           0 :             if (state->offset > state->whave + out - left) {
     667           0 :                 strm->msg = (char *)"invalid distance too far back";
     668           0 :                 state->mode = BAD;
     669           0 :                 break;
     670             :             }
     671           0 :             state->mode = MATCH;
     672           0 :             fallthrough;
     673           0 :         case MATCH:
     674           0 :             if (left == 0) goto inf_leave;
     675           0 :             copy = out - left;
     676           0 :             if (state->offset > copy) {         /* copy from window */
     677           0 :                 copy = state->offset - copy;
     678           0 :                 if (copy > state->write) {
     679           0 :                     copy -= state->write;
     680           0 :                     from = state->window + (state->wsize - copy);
     681             :                 }
     682             :                 else
     683           0 :                     from = state->window + (state->write - copy);
     684           0 :                 if (copy > state->length) copy = state->length;
     685             :             }
     686             :             else {                              /* copy from output */
     687           0 :                 from = put - state->offset;
     688           0 :                 copy = state->length;
     689             :             }
     690           0 :             if (copy > left) copy = left;
     691           0 :             left -= copy;
     692           0 :             state->length -= copy;
     693           0 :             do {
     694           0 :                 *put++ = *from++;
     695           0 :             } while (--copy);
     696           0 :             if (state->length == 0) state->mode = LEN;
     697             :             break;
     698           0 :         case LIT:
     699           0 :             if (left == 0) goto inf_leave;
     700           0 :             *put++ = (unsigned char)(state->length);
     701           0 :             left--;
     702           0 :             state->mode = LEN;
     703           0 :             break;
     704           0 :         case CHECK:
     705           0 :             if (state->wrap) {
     706           0 :                 NEEDBITS(32);
     707           0 :                 out -= left;
     708           0 :                 strm->total_out += out;
     709           0 :                 state->total += out;
     710           0 :                 if (INFLATE_NEED_CHECKSUM(strm) && out)
     711           0 :                     strm->adler = state->check =
     712           0 :                         UPDATE(state->check, put - out, out);
     713           0 :                 out = left;
     714           0 :                 if ((
     715           0 :                      REVERSE(hold)) != state->check) {
     716           0 :                     strm->msg = (char *)"incorrect data check";
     717           0 :                     state->mode = BAD;
     718           0 :                     break;
     719             :                 }
     720           0 :                 INITBITS();
     721             :             }
     722           0 :             state->mode = DONE;
     723           0 :             fallthrough;
     724           0 :         case DONE:
     725           0 :             ret = Z_STREAM_END;
     726           0 :             goto inf_leave;
     727           0 :         case BAD:
     728           0 :             ret = Z_DATA_ERROR;
     729           0 :             goto inf_leave;
     730             :         case MEM:
     731             :             return Z_MEM_ERROR;
     732           0 :         case SYNC:
     733             :         default:
     734           0 :             return Z_STREAM_ERROR;
     735             :         }
     736             : 
     737             :     /*
     738             :        Return from inflate(), updating the total counts and the check value.
     739             :        If there was no progress during the inflate() call, return a buffer
     740             :        error.  Call zlib_updatewindow() to create and/or update the window state.
     741             :      */
     742           0 :   inf_leave:
     743           0 :     RESTORE();
     744           0 :     if (INFLATE_NEED_UPDATEWINDOW(strm) &&
     745           0 :             (state->wsize || (state->mode < CHECK && out != strm->avail_out)))
     746           0 :         zlib_updatewindow(strm, out);
     747             : 
     748           0 :     in -= strm->avail_in;
     749           0 :     out -= strm->avail_out;
     750           0 :     strm->total_in += in;
     751           0 :     strm->total_out += out;
     752           0 :     state->total += out;
     753           0 :     if (INFLATE_NEED_CHECKSUM(strm) && state->wrap && out)
     754           0 :         strm->adler = state->check =
     755           0 :             UPDATE(state->check, strm->next_out - out, out);
     756             : 
     757           0 :     strm->data_type = state->bits + (state->last ? 64 : 0) +
     758           0 :                       (state->mode == TYPE ? 128 : 0);
     759             : 
     760           0 :     if (flush == Z_PACKET_FLUSH && ret == Z_OK &&
     761           0 :             strm->avail_out != 0 && strm->avail_in == 0)
     762           0 :                 return zlib_inflateSyncPacket(strm);
     763             : 
     764           0 :     if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
     765           0 :         ret = Z_BUF_ERROR;
     766             : 
     767             :     return ret;
     768             : }
     769             : 
     770           0 : int zlib_inflateEnd(z_streamp strm)
     771             : {
     772           0 :     if (strm == NULL || strm->state == NULL)
     773           0 :         return Z_STREAM_ERROR;
     774             :     return Z_OK;
     775             : }
     776             : 
     777             : /*
     778             :  * This subroutine adds the data at next_in/avail_in to the output history
     779             :  * without performing any output.  The output buffer must be "caught up";
     780             :  * i.e. no pending output but this should always be the case. The state must
     781             :  * be waiting on the start of a block (i.e. mode == TYPE or HEAD).  On exit,
     782             :  * the output will also be caught up, and the checksum will have been updated
     783             :  * if need be.
     784             :  */
     785           0 : int zlib_inflateIncomp(z_stream *z)
     786             : {
     787           0 :     struct inflate_state *state = (struct inflate_state *)z->state;
     788           0 :     Byte *saved_no = z->next_out;
     789           0 :     uInt saved_ao = z->avail_out;
     790             : 
     791           0 :     if (state->mode != TYPE && state->mode != HEAD)
     792             :         return Z_DATA_ERROR;
     793             : 
     794             :     /* Setup some variables to allow misuse of updateWindow */
     795           0 :     z->avail_out = 0;
     796           0 :     z->next_out = (unsigned char*)z->next_in + z->avail_in;
     797             : 
     798           0 :     zlib_updatewindow(z, z->avail_in);
     799             : 
     800             :     /* Restore saved variables */
     801           0 :     z->avail_out = saved_ao;
     802           0 :     z->next_out = saved_no;
     803             : 
     804           0 :     z->adler = state->check =
     805           0 :         UPDATE(state->check, z->next_in, z->avail_in);
     806             : 
     807           0 :     z->total_out += z->avail_in;
     808           0 :     z->total_in += z->avail_in;
     809           0 :     z->next_in += z->avail_in;
     810           0 :     state->total += z->avail_in;
     811           0 :     z->avail_in = 0;
     812             : 
     813           0 :     return Z_OK;
     814             : }

Generated by: LCOV version 1.14