LCOV - code coverage report
Current view: top level - frmts/zlib/contrib/infback9 - infback9.c (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 248 330 75.2 %
Date: 2024-11-21 22:18:42 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /* infback9.c -- inflate deflate64 data using a call-back interface
       2             :  * Copyright (C) 1995-2008 Mark Adler
       3             :  * For conditions of distribution and use, see copyright notice in zlib.h
       4             :  */
       5             : 
       6             : #include "minified_zutil.h"
       7             : #include "infback9.h"
       8             : #include "inftree9.h"
       9             : #include "inflate9.h"
      10             : #include <assert.h>
      11             : #include <string.h>
      12             : 
      13             : #undef ZEXPORT
      14             : #define ZEXPORT
      15             : 
      16             : #define WSIZE 65536UL
      17             : 
      18             : /*
      19             :    strm provides memory allocation functions in zalloc and zfree, or
      20             :    Z_NULL to use the library memory allocation functions.
      21             : 
      22             :    window is a user-supplied window and output buffer that is 64K bytes.
      23             :  */
      24           5 : int ZEXPORT inflateBack9Init_(z_stream FAR *strm,
      25             :                               unsigned char FAR *window,
      26             :                               const char *version,
      27             :                               int stream_size)
      28             : {
      29             :     struct inflate_state FAR *state;
      30             : 
      31           5 :     if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
      32             :         stream_size != (int)(sizeof(z_stream)))
      33           0 :         return Z_VERSION_ERROR;
      34           5 :     if (strm == Z_NULL /* || window == Z_NULL*/)
      35           0 :         return Z_STREAM_ERROR;
      36           5 :     strm->msg = Z_NULL;                 /* in case we return an error */
      37           5 :     if (strm->zalloc == (alloc_func)0) {
      38           1 :         strm->zalloc = zcalloc;
      39           1 :         strm->opaque = (voidpf)0;
      40             :     }
      41           5 :     if (strm->zfree == (free_func)0) strm->zfree = zcfree;
      42           5 :     state = (struct inflate_state FAR *)ZALLOC(strm, 1,
      43             :                                                sizeof(struct inflate_state));
      44           5 :     if (state == Z_NULL) return Z_MEM_ERROR;
      45           5 :     memset(state, 0, sizeof(struct inflate_state));
      46             :     Tracev((stderr, "inflate: allocated\n"));
      47           5 :     strm->state = (voidpf)state;
      48             : 
      49             :     // Added by E. Rouault
      50           5 :     if( window == Z_NULL )
      51             :     {
      52             :         window = (unsigned char FAR *)
      53           5 :                         ZALLOC(strm, WSIZE,
      54             :                                sizeof(unsigned char));
      55           5 :         if (window == Z_NULL) return Z_MEM_ERROR;
      56             :     }
      57             : 
      58           5 :     state->window = window;
      59           5 :     state->mode = TYPE;
      60           5 :     state->left = WSIZE;
      61           5 :     return Z_OK;
      62             : }
      63             : 
      64             : /*
      65             :    Build and output length and distance decoding tables for fixed code
      66             :    decoding.
      67             :  */
      68             : #ifdef MAKEFIXED
      69             : #include <stdio.h>
      70             : 
      71             : void makefixed9(void)
      72             : {
      73             :     unsigned sym, bits, low, size;
      74             :     code *next, *lenfix, *distfix;
      75             :     struct inflate_state state;
      76             :     code fixed[544];
      77             : 
      78             :     /* literal/length table */
      79             :     sym = 0;
      80             :     while (sym < 144) state.lens[sym++] = 8;
      81             :     while (sym < 256) state.lens[sym++] = 9;
      82             :     while (sym < 280) state.lens[sym++] = 7;
      83             :     while (sym < 288) state.lens[sym++] = 8;
      84             :     next = fixed;
      85             :     lenfix = next;
      86             :     bits = 9;
      87             :     inflate_table9(LENS, state.lens, 288, &(next), &(bits), state.work);
      88             : 
      89             :     /* distance table */
      90             :     sym = 0;
      91             :     while (sym < 32) state.lens[sym++] = 5;
      92             :     distfix = next;
      93             :     bits = 5;
      94             :     inflate_table9(DISTS, state.lens, 32, &(next), &(bits), state.work);
      95             : 
      96             :     /* write tables */
      97             :     puts("    /* inffix9.h -- table for decoding deflate64 fixed codes");
      98             :     puts("     * Generated automatically by makefixed9().");
      99             :     puts("     */");
     100             :     puts("");
     101             :     puts("    /* WARNING: this file should *not* be used by applications.");
     102             :     puts("       It is part of the implementation of this library and is");
     103             :     puts("       subject to change. Applications should only use zlib.h.");
     104             :     puts("     */");
     105             :     puts("");
     106             :     size = 1U << 9;
     107             :     printf("    static const code lenfix[%u] = {", size);
     108             :     low = 0;
     109             :     for (;;) {
     110             :         if ((low % 6) == 0) printf("\n        ");
     111             :         printf("{%u,%u,%d}", lenfix[low].op, lenfix[low].bits,
     112             :                lenfix[low].val);
     113             :         if (++low == size) break;
     114             :         putchar(',');
     115             :     }
     116             :     puts("\n    };");
     117             :     size = 1U << 5;
     118             :     printf("\n    static const code distfix[%u] = {", size);
     119             :     low = 0;
     120             :     for (;;) {
     121             :         if ((low % 5) == 0) printf("\n        ");
     122             :         printf("{%u,%u,%d}", distfix[low].op, distfix[low].bits,
     123             :                distfix[low].val);
     124             :         if (++low == size) break;
     125             :         putchar(',');
     126             :     }
     127             :     puts("\n    };");
     128             : }
     129             : #endif /* MAKEFIXED */
     130             : 
     131             : /* Macros for inflateBack(): */
     132             : 
     133             : /* Clear the input bit accumulator */
     134             : #define INITBITS() \
     135             :     do { \
     136             :         hold = 0; \
     137             :         bits = 0; \
     138             :     } while (0)
     139             : 
     140             : /* Assure that some input is available.  If input is requested, but denied,
     141             :    then return a Z_BUF_ERROR from inflateBack(). */
     142             : #define PULL() \
     143             :     do { \
     144             :         if (have == 0) { \
     145             :             have = in(in_desc, &next); \
     146             :             if (have == 0) { \
     147             :                 next = Z_NULL; \
     148             :                 ret = Z_BUF_ERROR; \
     149             :                 goto inf_leave; \
     150             :             } \
     151             :         } \
     152             :     } while (0)
     153             : 
     154             : /* Get a byte of input into the bit accumulator, or return from inflateBack()
     155             :    with an error if there is no input available. */
     156             : #define PULLBYTE() \
     157             :     do { \
     158             :         PULL(); \
     159             :         have--; \
     160             :         hold += (unsigned long)(*next++) << bits; \
     161             :         bits += 8; \
     162             :     } while (0)
     163             : 
     164             : /* Assure that there are at least n bits in the bit accumulator.  If there is
     165             :    not enough available input to do that, then return from inflateBack() with
     166             :    an error. */
     167             : #define NEEDBITS(n) \
     168             :     do { \
     169             :         while (bits < (unsigned)(n)) \
     170             :             PULLBYTE(); \
     171             :     } while (0)
     172             : 
     173             : /* Return the low n bits of the bit accumulator (n <= 16) */
     174             : #define BITS(n) \
     175             :     ((unsigned)hold & ((1U << (n)) - 1))
     176             : 
     177             : /* Remove n bits from the bit accumulator */
     178             : #define DROPBITS(n) \
     179             :     do { \
     180             :         hold >>= (n); \
     181             :         bits -= (unsigned)(n); \
     182             :     } while (0)
     183             : 
     184             : /* Remove zero to seven bits as needed to go to a byte boundary */
     185             : #define BYTEBITS() \
     186             :     do { \
     187             :         hold >>= bits & 7; \
     188             :         bits -= bits & 7; \
     189             :     } while (0)
     190             : 
     191             : /* Assure that some output space is available, by writing out the window
     192             :    if it's full.  If the write fails, return from inflateBack() with a
     193             :    Z_BUF_ERROR. */
     194             : #define ROOM() \
     195             :     do { \
     196             :         if (left == 0) { \
     197             :             put = window; \
     198             :             left = WSIZE; \
     199             :             wrap = 1; \
     200             :             if (out(out_desc, put, (unsigned)left)) { \
     201             :                 ret = Z_BUF_ERROR; \
     202             :                 goto inf_leave; \
     203             :             } \
     204             :         } \
     205             :     } while (0)
     206             : 
     207             : /*
     208             :    strm provides the memory allocation functions and window buffer on input,
     209             :    and provides information on the unused input on return.  For Z_DATA_ERROR
     210             :    returns, strm will also provide an error message.
     211             : 
     212             :    in() and out() are the call-back input and output functions.  When
     213             :    inflateBack() needs more input, it calls in().  When inflateBack() has
     214             :    filled the window with output, or when it completes with data in the
     215             :    window, it calls out() to write out the data.  The application must not
     216             :    change the provided input until in() is called again or inflateBack()
     217             :    returns.  The application must not change the window/output buffer until
     218             :    inflateBack() returns.
     219             : 
     220             :    in() and out() are called with a descriptor parameter provided in the
     221             :    inflateBack() call.  This parameter can be a structure that provides the
     222             :    information required to do the read or write, as well as accumulated
     223             :    information on the input and output such as totals and check values.
     224             : 
     225             :    in() should return zero on failure.  out() should return non-zero on
     226             :    failure.  If either in() or out() fails, than inflateBack() returns a
     227             :    Z_BUF_ERROR.  strm->next_in can be checked for Z_NULL to see whether it
     228             :    was in() or out() that caused in the error.  Otherwise,  inflateBack()
     229             :    returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format
     230             :    error, or Z_MEM_ERROR if it could not allocate memory for the state.
     231             :    inflateBack() can also return Z_STREAM_ERROR if the input parameters
     232             :    are not correct, i.e. strm is Z_NULL or the state was not initialized.
     233             :  */
     234          11 : int ZEXPORT inflateBack9(z_stream FAR *strm, in_func in, void FAR *in_desc, out_func out, void FAR *out_desc)
     235             : {
     236             :     struct inflate_state FAR *state;
     237             :     z_const unsigned char FAR *next;    /* next input */
     238             :     unsigned char FAR *put;     /* next output */
     239             :     unsigned have;              /* available input */
     240             :     unsigned long left;         /* available output */
     241             :     inflate_mode mode;          /* current inflate mode */
     242             :     int lastblock;              /* true if processing last block */
     243             :     int wrap;                   /* true if the window has wrapped */
     244             :     unsigned char FAR *window;  /* allocated sliding window, if needed */
     245             :     unsigned long hold;         /* bit buffer */
     246             :     unsigned bits;              /* bits in bit buffer */
     247             :     unsigned extra;             /* extra bits needed */
     248             :     unsigned long length;       /* literal or length of data to copy */
     249             :     unsigned long offset;       /* distance back to copy string from */
     250             :     unsigned long copy;         /* number of stored or match bytes to copy */
     251             :     unsigned char FAR *from;    /* where to copy match bytes from */
     252             :     code const FAR *lencode;    /* starting table for length/literal codes */
     253             :     code const FAR *distcode;   /* starting table for distance codes */
     254             :     unsigned lenbits;           /* index bits for lencode */
     255             :     unsigned distbits;          /* index bits for distcode */
     256             :     code here;                  /* current decoding table entry */
     257             :     code last;                  /* parent table entry */
     258             :     unsigned len;               /* length to copy for repeats, bits to drop */
     259             :     int ret;                    /* return code */
     260             :     static const unsigned short order[19] = /* permutation of code lengths */
     261             :         {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
     262             : #include "inffix9.h"
     263             : 
     264             :     /* Check that the strm exists and that the state was initialized */
     265          11 :     if (strm == Z_NULL || strm->state == Z_NULL)
     266           0 :         return Z_STREAM_ERROR;
     267          11 :     state = (struct inflate_state FAR *)strm->state;
     268             : 
     269             :     /* Reset the state */
     270          11 :     strm->msg = Z_NULL;
     271             :     //mode = TYPE;
     272             :     //lastblock = 0;
     273             :     //wrap = 0;
     274          11 :     window = state->window;
     275          11 :     next = strm->next_in;
     276          11 :     have = next != Z_NULL ? strm->avail_in : 0;
     277             :     //hold = 0;
     278             :     //bits = 0;
     279          11 :     left = state->left;
     280          11 :     put = window + (unsigned)(WSIZE - state->left);
     281             :     //lencode = Z_NULL;
     282             :     //distcode = Z_NULL;
     283             : 
     284          11 :     mode = state->mode;
     285          11 :     wrap = state->wrap; // wrap meaning is different from inflate.c
     286          11 :     hold = state->hold;
     287          11 :     bits = state->bits;
     288          11 :     extra = state->extra;
     289          11 :     length = state->length;
     290          11 :     offset = state->offset;
     291          11 :     lencode = state->lencode;
     292          11 :     distcode = state->distcode;
     293          11 :     lenbits = state->lenbits;
     294          11 :     distbits = state->distbits;
     295          11 :     lastblock = state->last;
     296             : 
     297             :     /* Inflate until end of block marked as last */
     298             :     for (;;)
     299      715996 :         switch (mode) {
     300         133 :         case TYPE:
     301             :             /* determine and dispatch block type */
     302         133 :             if (lastblock) {
     303           4 :                 BYTEBITS();
     304           4 :                 mode = DONE;
     305           4 :                 break;
     306             :             }
     307         167 :             NEEDBITS(3);
     308         129 :             lastblock = BITS(1);
     309         129 :             DROPBITS(1);
     310         129 :             switch (BITS(2)) {
     311           0 :             case 0:                             /* stored block */
     312             :                 Tracev((stderr, "inflate:     stored block%s\n",
     313             :                         lastblock ? " (last)" : ""));
     314           0 :                 mode = STORED;
     315           0 :                 break;
     316           0 :             case 1:                             /* fixed block */
     317           0 :                 lencode = lenfix;
     318           0 :                 lenbits = 9;
     319           0 :                 distcode = distfix;
     320           0 :                 distbits = 5;
     321             :                 Tracev((stderr, "inflate:     fixed codes block%s\n",
     322             :                         lastblock ? " (last)" : ""));
     323           0 :                 mode = LEN;                     /* decode codes */
     324           0 :                 break;
     325         129 :             case 2:                             /* dynamic block */
     326             :                 Tracev((stderr, "inflate:     dynamic codes block%s\n",
     327             :                         lastblock ? " (last)" : ""));
     328         129 :                 mode = TABLE;
     329         129 :                 break;
     330           0 :             case 3:
     331           0 :                 strm->msg = (char *)"invalid block type";
     332           0 :                 mode = BAD;
     333             :             }
     334         129 :             DROPBITS(2);
     335         129 :             break;
     336             : 
     337           0 :         case STORED:
     338             :             /* get and verify stored block length */
     339           0 :             BYTEBITS();                         /* go to byte boundary */
     340           0 :             NEEDBITS(32);
     341           0 :             if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
     342           0 :                 strm->msg = (char *)"invalid stored block lengths";
     343           0 :                 mode = BAD;
     344           0 :                 break;
     345             :             }
     346           0 :             length = (unsigned)hold & 0xffff;
     347             :             Tracev((stderr, "inflate:       stored length %lu\n",
     348             :                     length));
     349           0 :             INITBITS();
     350           0 :             mode = COPY;
     351             :             //fallthrough
     352             : 
     353           0 :         case COPY:
     354             :             /* copy stored block from input to output */
     355           0 :             while (length != 0) {
     356           0 :                 copy = length;
     357           0 :                 PULL();
     358           0 :                 ROOM();
     359           0 :                 if (copy > have) copy = have;
     360           0 :                 if (copy > left) copy = left;
     361           0 :                 zmemcpy(put, next, (unsigned)copy);
     362           0 :                 have -= copy;
     363           0 :                 next += copy;
     364           0 :                 left -= copy;
     365           0 :                 put += copy;
     366           0 :                 length -= copy;
     367             :             }
     368             :             Tracev((stderr, "inflate:       stored end\n"));
     369           0 :             mode = TYPE;
     370           0 :             break;
     371             : 
     372         129 :         case TABLE:
     373             :             /* get dynamic table entries descriptor */
     374         366 :             NEEDBITS(14);
     375         129 :             state->nlen = BITS(5) + 257;
     376         129 :             DROPBITS(5);
     377         129 :             state->ndist = BITS(5) + 1;
     378         129 :             DROPBITS(5);
     379         129 :             state->ncode = BITS(4) + 4;
     380         129 :             DROPBITS(4);
     381         129 :             if (state->nlen > 286) {
     382           0 :                 strm->msg = (char *)"too many length symbols";
     383           0 :                 mode = BAD;
     384           0 :                 break;
     385             :             }
     386             :             Tracev((stderr, "inflate:       table sizes ok\n"));
     387             : 
     388             :             /* get code length code lengths (not a typo) */
     389         129 :             state->have = 0;
     390         129 :             mode = LENLENS;
     391             :             // fallthrough
     392             : 
     393         129 :         case LENLENS:
     394        2451 :             while (state->have < state->ncode) {
     395        3191 :                 NEEDBITS(3);
     396        2322 :                 state->lens[order[state->have++]] = (unsigned short)BITS(3);
     397        2322 :                 DROPBITS(3);
     398             :             }
     399         258 :             while (state->have < 19)
     400         129 :                 state->lens[order[state->have++]] = 0;
     401         129 :             state->next = state->codes;
     402         129 :             lencode = (code const FAR *)(state->next);
     403         129 :             lenbits = 7;
     404         129 :             ret = inflate_table9(CODES, state->lens, 19, &(state->next),
     405         129 :                                 &(lenbits), state->work);
     406         129 :             if (ret) {
     407           0 :                 strm->msg = (char *)"invalid code lengths set";
     408           0 :                 mode = BAD;
     409           0 :                 break;
     410             :             }
     411             :             Tracev((stderr, "inflate:       code lengths ok\n"));
     412             : 
     413             :             /* get length and distance code code lengths */
     414         129 :             state->have = 0;
     415         129 :             mode = CODELENS;
     416             :             // fallthrough
     417             : 
     418         129 :         case CODELENS:
     419        4262 :             while (state->have < state->nlen + state->ndist) {
     420             :                 for (;;) {
     421        5863 :                     here = lencode[BITS(lenbits)];
     422        5863 :                     if ((unsigned)(here.bits) <= bits) break;
     423        1730 :                     PULLBYTE();
     424             :                 }
     425        4133 :                 if (here.val < 16) {
     426        3152 :                     NEEDBITS(here.bits);
     427        3152 :                     DROPBITS(here.bits);
     428        3152 :                     state->lens[state->have++] = here.val;
     429             :                 }
     430             :                 else {
     431         981 :                     if (here.val == 16) {
     432         235 :                         NEEDBITS(here.bits + 2);
     433         183 :                         DROPBITS(here.bits);
     434         183 :                         if (state->have == 0) {
     435           0 :                             strm->msg = (char *)"invalid bit length repeat";
     436           0 :                             mode = BAD;
     437           0 :                             break;
     438             :                         }
     439         183 :                         len = (unsigned)(state->lens[state->have - 1]);
     440         183 :                         copy = 3 + BITS(2);
     441         183 :                         DROPBITS(2);
     442             :                     }
     443         798 :                     else if (here.val == 17) {
     444         547 :                         NEEDBITS(here.bits + 3);
     445         399 :                         DROPBITS(here.bits);
     446         399 :                         len = 0;
     447         399 :                         copy = 3 + BITS(3);
     448         399 :                         DROPBITS(3);
     449             :                     }
     450             :                     else {
     451         737 :                         NEEDBITS(here.bits + 7);
     452         399 :                         DROPBITS(here.bits);
     453         399 :                         len = 0;
     454         399 :                         copy = 11 + BITS(7);
     455         399 :                         DROPBITS(7);
     456             :                     }
     457         981 :                     if (state->have + copy > state->nlen + state->ndist) {
     458           0 :                         strm->msg = (char *)"invalid bit length repeat";
     459           0 :                         mode = BAD;
     460           0 :                         break;
     461             :                     }
     462       35638 :                     while (copy)
     463             :                     {
     464       34657 :                         --copy;
     465       34657 :                         state->lens[state->have++] = (unsigned short)len;
     466             :                     }
     467             :                 }
     468             :             }
     469             : 
     470             :             /* handle error breaks in while */
     471         129 :             if (mode == BAD) break;
     472             : 
     473             :             /* check for end-of-block code (better have one) */
     474         129 :             if (state->lens[256] == 0) {
     475           0 :                 strm->msg = (char *)"invalid code -- missing end-of-block";
     476           0 :                 mode = BAD;
     477           0 :                 break;
     478             :             }
     479             : 
     480             :             /* build code tables -- note: do not change the lenbits or distbits
     481             :                values here (9 and 6) without reading the comments in inftree9.h
     482             :                concerning the ENOUGH constants, which depend on those values */
     483         129 :             state->next = state->codes;
     484         129 :             lencode = (code const FAR *)(state->next);
     485         129 :             lenbits = 9;
     486         129 :             ret = inflate_table9(LENS, state->lens, state->nlen,
     487         129 :                             &(state->next), &(lenbits), state->work);
     488         129 :             if (ret) {
     489           0 :                 strm->msg = (char *)"invalid literal/lengths set";
     490           0 :                 mode = BAD;
     491           0 :                 break;
     492             :             }
     493         129 :             distcode = (code const FAR *)(state->next);
     494         129 :             distbits = 6;
     495         129 :             ret = inflate_table9(DISTS, state->lens + state->nlen,
     496             :                             state->ndist, &(state->next), &(distbits),
     497         129 :                             state->work);
     498         129 :             if (ret) {
     499           0 :                 strm->msg = (char *)"invalid distances set";
     500           0 :                 mode = BAD;
     501           0 :                 break;
     502             :             }
     503             :             Tracev((stderr, "inflate:       codes ok\n"));
     504         129 :             mode = LEN;
     505             :             // fallthrough
     506             : 
     507      958549 :         case LEN:
     508             :             /* get a literal, length, or end-of-block code */
     509             :             for (;;) {
     510      958549 :                 here = lencode[BITS(lenbits)];
     511      958549 :                 if ((unsigned)(here.bits) <= bits) break;
     512      242698 :                 PULLBYTE();
     513             :             }
     514      715851 :             if (here.op && (here.op & 0xf0) == 0) {
     515         935 :                 last = here;
     516             :                 for (;;) {
     517        1053 :                     here = lencode[last.val +
     518        1053 :                             (BITS(last.bits + last.op) >> last.bits)];
     519        1053 :                     if ((unsigned)(last.bits + here.bits) <= bits) break;
     520         118 :                     PULLBYTE();
     521             :                 }
     522         935 :                 DROPBITS(last.bits);
     523             :             }
     524      715851 :             DROPBITS(here.bits);
     525      715851 :             length = (unsigned)here.val;
     526             : 
     527             :             /* process literal */
     528      715851 :             if (here.op == 0) {
     529             :                 Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
     530             :                         "inflate:         literal '%c'\n" :
     531             :                         "inflate:         literal 0x%02x\n", here.val));
     532      358220 :                 ROOM();
     533      358220 :                 *put++ = (unsigned char)(length);
     534      358220 :                 left--;
     535      358220 :                 mode = LEN;
     536      358220 :                 break;
     537             :             }
     538             : 
     539             :             /* process end of block */
     540      357631 :             if (here.op & 32) {
     541             :                 Tracevv((stderr, "inflate:         end of block\n"));
     542         129 :                 mode = TYPE;
     543         129 :                 break;
     544             :             }
     545             : 
     546             :             /* invalid code */
     547      357502 :             if (here.op & 64) {
     548           0 :                 strm->msg = (char *)"invalid literal/length code";
     549           0 :                 mode = BAD;
     550           0 :                 break;
     551             :             }
     552             : 
     553             :             /* length code -- get extra bits, if any */
     554      357502 :             extra = (unsigned)(here.op) & 31;
     555      357502 :             mode = LENEXT;
     556             :             // fallthrough
     557             : 
     558      357506 :         case LENEXT:
     559      357506 :             if (extra != 0) {
     560      445057 :                 NEEDBITS(extra);
     561      356158 :                 length += BITS(extra);
     562      356158 :                 DROPBITS(extra);
     563             :             }
     564             :             Tracevv((stderr, "inflate:         length %lu\n", length));
     565             : 
     566             :             /* get distance code */
     567      357503 :             mode = DIST;
     568             :             // fallthrough
     569             : 
     570      407083 :         case DIST:
     571             :             for (;;) {
     572      407083 :                 here = distcode[BITS(distbits)];
     573      407083 :                 if ((unsigned)(here.bits) <= bits) break;
     574       49580 :                 PULLBYTE();
     575             :             }
     576      357503 :             if ((here.op & 0xf0) == 0) {
     577         128 :                 last = here;
     578             :                 for (;;) {
     579         148 :                     here = distcode[last.val +
     580         148 :                             (BITS(last.bits + last.op) >> last.bits)];
     581         148 :                     if ((unsigned)(last.bits + here.bits) <= bits) break;
     582          20 :                     PULLBYTE();
     583             :                 }
     584         128 :                 DROPBITS(last.bits);
     585             :             }
     586      357503 :             DROPBITS(here.bits);
     587      357503 :             if (here.op & 64) {
     588           0 :                 strm->msg = (char *)"invalid distance code";
     589           0 :                 mode = BAD;
     590           0 :                 break;
     591             :             }
     592      357503 :             offset = (unsigned)here.val;
     593             : 
     594             :             /* get distance extra bits, if any */
     595      357503 :             extra = (unsigned)(here.op) & 15;
     596      357503 :             mode = DISTEXT;
     597             :             // fallthrough
     598             : 
     599      357503 :         case DISTEXT:
     600      357503 :             if (extra != 0) {
     601      506219 :                 NEEDBITS(extra);
     602      357499 :                 offset += BITS(extra);
     603      357499 :                 DROPBITS(extra);
     604             :             }
     605      357503 :             if (offset > WSIZE - (wrap ? 0: left)) {
     606           0 :                 strm->msg = (char *)"invalid distance too far back";
     607           0 :                 mode = BAD;
     608           0 :                 break;
     609             :             }
     610             :             Tracevv((stderr, "inflate:         distance %lu\n", offset));
     611             : 
     612             :             /* copy match from window to output */
     613      357503 :             mode = MATCH;
     614             :             // fallthrough
     615             : 
     616      357725 :         case MATCH:
     617             :             do {
     618      357725 :                 ROOM();
     619      357725 :                 copy = WSIZE - offset;
     620      357725 :                 if (copy < left) {
     621         338 :                     from = put + copy;
     622         338 :                     copy = left - copy;
     623             :                 }
     624             :                 else {
     625      357387 :                     from = put - offset;
     626      357387 :                     copy = left;
     627             :                 }
     628      357725 :                 if (copy > length) copy = length;
     629      357725 :                 length -= copy;
     630      357725 :                 left -= copy;
     631             :                 do {
     632     7432900 :                     *put++ = *from++;
     633     7432900 :                 } while (--copy);
     634      357725 :             } while (length != 0);
     635      357503 :             mode = LEN;
     636      357503 :             break;
     637             : 
     638           4 :         case DONE:
     639             :             /* inflate stream terminated properly -- write leftover output */
     640           4 :             ret = Z_STREAM_END;
     641           4 :             if (left < WSIZE) {
     642           4 :                 if (out(out_desc, window, (unsigned)(WSIZE - left)))
     643           0 :                     ret = Z_BUF_ERROR;
     644             :             }
     645           4 :             goto inf_leave;
     646             : 
     647           0 :         case BAD:
     648           0 :             ret = Z_DATA_ERROR;
     649           0 :             goto inf_leave;
     650             : 
     651           0 :         default:                /* can't happen, but makes compilers happy */
     652           0 :             ret = Z_STREAM_ERROR;
     653           0 :             goto inf_leave;
     654             :         }
     655             : 
     656             :     /* Return unused input */
     657          11 :   inf_leave:
     658          11 :     strm->next_in = next;
     659          11 :     strm->avail_in = have;
     660          11 :     state->left = left;
     661             : 
     662          11 :     state->mode = mode;
     663          11 :     state->wrap = wrap;
     664          11 :     state->hold = hold;
     665          11 :     state->bits = bits;
     666          11 :     state->extra = extra;
     667          11 :     state->length = length;
     668          11 :     state->offset = offset;
     669          11 :     state->lencode = lencode;
     670          11 :     state->distcode = distcode;
     671          11 :     state->lenbits = lenbits;
     672          11 :     state->distbits = distbits;
     673          11 :     state->last = lastblock;
     674          11 :     return ret;
     675             : }
     676             : 
     677          10 : int ZEXPORT inflateBack9End(z_stream FAR *strm)
     678             : {
     679          10 :     if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
     680           0 :         return Z_STREAM_ERROR;
     681             :     // Added by E. Rouault
     682          10 :     struct inflate_state FAR *state = (struct inflate_state FAR *)strm->state;
     683          10 :     if (state->window != Z_NULL) ZFREE(strm, state->window);
     684          10 :     ZFREE(strm, strm->state);
     685          10 :     strm->state = Z_NULL;
     686             :     Tracev((stderr, "inflate: end\n"));
     687          10 :     return Z_OK;
     688             : }
     689             : 
     690             : // Added by E. Rouault (ported from inflateCopy())
     691           5 : int ZEXPORT inflateBack9Copy(z_streamp dest, z_streamp source)
     692             : {
     693             :     struct inflate_state FAR *state;
     694             :     struct inflate_state FAR *copy;
     695             :     unsigned char FAR *window;
     696             : 
     697             :     /* check input */
     698           5 :     if (/*inflateStateCheck(source) ||*/ dest == Z_NULL)
     699           0 :         return Z_STREAM_ERROR;
     700           5 :     state = (struct inflate_state FAR *)source->state;
     701             : 
     702             :     /* allocate space */
     703             :     copy = (struct inflate_state FAR *)
     704           5 :            ZALLOC(source, 1, sizeof(struct inflate_state));
     705           5 :     if (copy == Z_NULL) return Z_MEM_ERROR;
     706           5 :     window = Z_NULL;
     707           5 :     if (state->window != Z_NULL) {
     708             :         window = (unsigned char FAR *)
     709           5 :                  ZALLOC(source, WSIZE, sizeof(unsigned char));
     710           5 :         if (window == Z_NULL) {
     711           0 :             ZFREE(source, copy);
     712           0 :             return Z_MEM_ERROR;
     713             :         }
     714             :     }
     715             : 
     716             :     /* copy state */
     717           5 :     zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
     718           5 :     zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state));
     719             : 
     720           5 :     if (state->lencode >= state->codes &&
     721           4 :         state->lencode <= state->codes + ENOUGH - 1) {
     722           4 :         copy->lencode = copy->codes + (state->lencode - state->codes);
     723           4 :         copy->distcode = copy->codes + (state->distcode - state->codes);
     724             :     }
     725           5 :     if( state->next )
     726             :     {
     727           4 :         assert(state->next >= state->codes);
     728           4 :         copy->next = copy->codes + (state->next - state->codes);
     729             :     }
     730           5 :     if (window != Z_NULL) {
     731           5 :         zmemcpy(window, state->window, WSIZE);
     732             :     }
     733           5 :     copy->window = window;
     734           5 :     dest->state = (struct internal_state FAR *)copy;
     735           5 :     return Z_OK;
     736             : }

Generated by: LCOV version 1.14