< prev index next >

src/java.base/share/native/libzip/zlib/inflate.c

Print this page

  98  * - Changed inflateBack() interface to provide separate opaque descriptors
  99  *   for the in() and out() functions
 100  * - Changed inflateBack() argument and in_func typedef to swap the length
 101  *   and buffer address return values for the input function
 102  * - Check next_in and next_out for Z_NULL on entry to inflate()
 103  *
 104  * The history for versions after 1.2.0 are in ChangeLog in zlib distribution.
 105  */
 106 
 107 #include "zutil.h"
 108 #include "inftrees.h"
 109 #include "inflate.h"
 110 #include "inffast.h"
 111 
 112 #ifdef MAKEFIXED
 113 #  ifndef BUILDFIXED
 114 #    define BUILDFIXED
 115 #  endif
 116 #endif
 117 
 118 /* function prototypes */
 119 local int inflateStateCheck OF((z_streamp strm));
 120 local void fixedtables OF((struct inflate_state FAR *state));
 121 local int updatewindow OF((z_streamp strm, const unsigned char FAR *end,
 122                            unsigned copy));
 123 #ifdef BUILDFIXED
 124    void makefixed OF((void));
 125 #endif
 126 local unsigned syncsearch OF((unsigned FAR *have, const unsigned char FAR *buf,
 127                               unsigned len));
 128 
 129 local int inflateStateCheck(strm)
 130 z_streamp strm;
 131 {
 132     struct inflate_state FAR *state;
 133     if (strm == Z_NULL ||
 134         strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)
 135         return 1;
 136     state = (struct inflate_state FAR *)strm->state;
 137     if (state == Z_NULL || state->strm != strm ||
 138         state->mode < HEAD || state->mode > SYNC)
 139         return 1;
 140     return 0;
 141 }
 142 
 143 int ZEXPORT inflateResetKeep(strm)
 144 z_streamp strm;
 145 {
 146     struct inflate_state FAR *state;
 147 
 148     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
 149     state = (struct inflate_state FAR *)strm->state;
 150     strm->total_in = strm->total_out = state->total = 0;
 151     strm->msg = Z_NULL;
 152     if (state->wrap)        /* to support ill-conceived Java test suite */
 153         strm->adler = state->wrap & 1;
 154     state->mode = HEAD;
 155     state->last = 0;
 156     state->havedict = 0;
 157     state->flags = -1;
 158     state->dmax = 32768U;
 159     state->head = Z_NULL;
 160     state->hold = 0;
 161     state->bits = 0;
 162     state->lencode = state->distcode = state->next = state->codes;
 163     state->sane = 1;
 164     state->back = -1;
 165     Tracev((stderr, "inflate: reset\n"));
 166     return Z_OK;
 167 }
 168 
 169 int ZEXPORT inflateReset(strm)
 170 z_streamp strm;
 171 {
 172     struct inflate_state FAR *state;
 173 
 174     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
 175     state = (struct inflate_state FAR *)strm->state;
 176     state->wsize = 0;
 177     state->whave = 0;
 178     state->wnext = 0;
 179     return inflateResetKeep(strm);
 180 }
 181 
 182 int ZEXPORT inflateReset2(strm, windowBits)
 183 z_streamp strm;
 184 int windowBits;
 185 {
 186     int wrap;
 187     struct inflate_state FAR *state;
 188 
 189     /* get the state */
 190     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
 191     state = (struct inflate_state FAR *)strm->state;
 192 
 193     /* extract wrap request from windowBits parameter */
 194     if (windowBits < 0) {
 195         if (windowBits < -15)
 196             return Z_STREAM_ERROR;
 197         wrap = 0;
 198         windowBits = -windowBits;
 199     }
 200     else {
 201         wrap = (windowBits >> 4) + 5;
 202 #ifdef GUNZIP
 203         if (windowBits < 48)
 204             windowBits &= 15;
 205 #endif
 206     }
 207 
 208     /* set number of window bits, free window if different */
 209     if (windowBits && (windowBits < 8 || windowBits > 15))
 210         return Z_STREAM_ERROR;
 211     if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) {
 212         ZFREE(strm, state->window);
 213         state->window = Z_NULL;
 214     }
 215 
 216     /* update state and reset the rest of it */
 217     state->wrap = wrap;
 218     state->wbits = (unsigned)windowBits;
 219     return inflateReset(strm);
 220 }
 221 
 222 int ZEXPORT inflateInit2_(strm, windowBits, version, stream_size)
 223 z_streamp strm;
 224 int windowBits;
 225 const char *version;
 226 int stream_size;
 227 {
 228     int ret;
 229     struct inflate_state FAR *state;
 230 
 231     if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
 232         stream_size != (int)(sizeof(z_stream)))
 233         return Z_VERSION_ERROR;
 234     if (strm == Z_NULL) return Z_STREAM_ERROR;
 235     strm->msg = Z_NULL;                 /* in case we return an error */
 236     if (strm->zalloc == (alloc_func)0) {
 237 #ifdef Z_SOLO
 238         return Z_STREAM_ERROR;
 239 #else
 240         strm->zalloc = zcalloc;
 241         strm->opaque = (voidpf)0;
 242 #endif
 243     }
 244     if (strm->zfree == (free_func)0)
 245 #ifdef Z_SOLO
 246         return Z_STREAM_ERROR;
 247 #else
 248         strm->zfree = zcfree;
 249 #endif
 250     state = (struct inflate_state FAR *)
 251             ZALLOC(strm, 1, sizeof(struct inflate_state));
 252     if (state == Z_NULL) return Z_MEM_ERROR;
 253     Tracev((stderr, "inflate: allocated\n"));
 254     strm->state = (struct internal_state FAR *)state;
 255     state->strm = strm;
 256     state->window = Z_NULL;
 257     state->mode = HEAD;     /* to pass state test in inflateReset2() */
 258     ret = inflateReset2(strm, windowBits);
 259     if (ret != Z_OK) {
 260         ZFREE(strm, state);
 261         strm->state = Z_NULL;
 262     }
 263     return ret;
 264 }
 265 
 266 int ZEXPORT inflateInit_(strm, version, stream_size)
 267 z_streamp strm;
 268 const char *version;
 269 int stream_size;
 270 {
 271     return inflateInit2_(strm, DEF_WBITS, version, stream_size);
 272 }
 273 
 274 int ZEXPORT inflatePrime(strm, bits, value)
 275 z_streamp strm;
 276 int bits;
 277 int value;
 278 {
 279     struct inflate_state FAR *state;
 280 
 281     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;


 282     state = (struct inflate_state FAR *)strm->state;
 283     if (bits < 0) {
 284         state->hold = 0;
 285         state->bits = 0;
 286         return Z_OK;
 287     }
 288     if (bits > 16 || state->bits + (uInt)bits > 32) return Z_STREAM_ERROR;
 289     value &= (1L << bits) - 1;
 290     state->hold += (unsigned)value << state->bits;
 291     state->bits += (uInt)bits;
 292     return Z_OK;
 293 }
 294 
 295 /*
 296    Return state with length and distance decoding tables and index sizes set to
 297    fixed code decoding.  Normally this returns fixed tables from inffixed.h.
 298    If BUILDFIXED is defined, then instead this routine builds the tables the
 299    first time it's called, and returns those tables the first time and
 300    thereafter.  This reduces the size of the code by about 2K bytes, in
 301    exchange for a little execution time.  However, BUILDFIXED should not be
 302    used for threaded applications, since the rewriting of the tables and virgin
 303    may not be thread-safe.
 304  */
 305 local void fixedtables(state)
 306 struct inflate_state FAR *state;
 307 {
 308 #ifdef BUILDFIXED
 309     static int virgin = 1;
 310     static code *lenfix, *distfix;
 311     static code fixed[544];
 312 
 313     /* build fixed huffman tables if first call (may not be thread safe) */
 314     if (virgin) {
 315         unsigned sym, bits;
 316         static code *next;
 317 
 318         /* literal/length table */
 319         sym = 0;
 320         while (sym < 144) state->lens[sym++] = 8;
 321         while (sym < 256) state->lens[sym++] = 9;
 322         while (sym < 280) state->lens[sym++] = 7;
 323         while (sym < 288) state->lens[sym++] = 8;
 324         next = fixed;
 325         lenfix = next;
 326         bits = 9;
 327         inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);

 349 #include <stdio.h>
 350 
 351 /*
 352    Write out the inffixed.h that is #include'd above.  Defining MAKEFIXED also
 353    defines BUILDFIXED, so the tables are built on the fly.  makefixed() writes
 354    those tables to stdout, which would be piped to inffixed.h.  A small program
 355    can simply call makefixed to do this:
 356 
 357     void makefixed(void);
 358 
 359     int main(void)
 360     {
 361         makefixed();
 362         return 0;
 363     }
 364 
 365    Then that can be linked with zlib built with MAKEFIXED defined and run:
 366 
 367     a.out > inffixed.h
 368  */
 369 void makefixed()
 370 {
 371     unsigned low, size;
 372     struct inflate_state state;
 373 
 374     fixedtables(&state);
 375     puts("    /* inffixed.h -- table for decoding fixed codes");
 376     puts("     * Generated automatically by makefixed().");
 377     puts("     */");
 378     puts("");
 379     puts("    /* WARNING: this file should *not* be used by applications.");
 380     puts("       It is part of the implementation of this library and is");
 381     puts("       subject to change. Applications should only use zlib.h.");
 382     puts("     */");
 383     puts("");
 384     size = 1U << 9;
 385     printf("    static const code lenfix[%u] = {", size);
 386     low = 0;
 387     for (;;) {
 388         if ((low % 7) == 0) printf("\n        ");
 389         printf("{%u,%u,%d}", (low & 127) == 99 ? 64 : state.lencode[low].op,

 403         putchar(',');
 404     }
 405     puts("\n    };");
 406 }
 407 #endif /* MAKEFIXED */
 408 
 409 /*
 410    Update the window with the last wsize (normally 32K) bytes written before
 411    returning.  If window does not exist yet, create it.  This is only called
 412    when a window is already in use, or when output has been written during this
 413    inflate call, but the end of the deflate stream has not been reached yet.
 414    It is also called to create a window for dictionary data when a dictionary
 415    is loaded.
 416 
 417    Providing output buffers larger than 32K to inflate() should provide a speed
 418    advantage, since only the last 32K of output is copied to the sliding window
 419    upon return from inflate(), and since all distances after the first 32K of
 420    output will fall in the output data, making match copies simpler and faster.
 421    The advantage may be dependent on the size of the processor's data caches.
 422  */
 423 local int updatewindow(strm, end, copy)
 424 z_streamp strm;
 425 const Bytef *end;
 426 unsigned copy;
 427 {
 428     struct inflate_state FAR *state;
 429     unsigned dist;
 430 
 431     state = (struct inflate_state FAR *)strm->state;
 432 
 433     /* if it hasn't been done already, allocate space for the window */
 434     if (state->window == Z_NULL) {
 435         state->window = (unsigned char FAR *)
 436                         ZALLOC(strm, 1U << state->wbits,
 437                                sizeof(unsigned char));
 438         if (state->window == Z_NULL) return 1;
 439     }
 440 
 441     /* if window not in use yet, initialize */
 442     if (state->wsize == 0) {
 443         state->wsize = 1U << state->wbits;
 444         state->wnext = 0;
 445         state->whave = 0;
 446     }
 447 

 629    When returning, a "goto inf_leave" is used to update the total counters,
 630    update the check value, and determine whether any progress has been made
 631    during that inflate() call in order to return the proper return code.
 632    Progress is defined as a change in either strm->avail_in or strm->avail_out.
 633    When there is a window, goto inf_leave will update the window with the last
 634    output written.  If a goto inf_leave occurs in the middle of decompression
 635    and there is no window currently, goto inf_leave will create one and copy
 636    output to the window for the next call of inflate().
 637 
 638    In this implementation, the flush parameter of inflate() only affects the
 639    return code (per zlib.h).  inflate() always writes as much as possible to
 640    strm->next_out, given the space available and the provided input--the effect
 641    documented in zlib.h of Z_SYNC_FLUSH.  Furthermore, inflate() always defers
 642    the allocation of and copying into a sliding window until necessary, which
 643    provides the effect documented in zlib.h for Z_FINISH when the entire input
 644    stream available.  So the only thing the flush parameter actually does is:
 645    when flush is set to Z_FINISH, inflate() cannot return Z_OK.  Instead it
 646    will return Z_BUF_ERROR if it has not reached the end of the stream.
 647  */
 648 
 649 int ZEXPORT inflate(strm, flush)
 650 z_streamp strm;
 651 int flush;
 652 {
 653     struct inflate_state FAR *state;
 654     z_const unsigned char FAR *next;    /* next input */
 655     unsigned char FAR *put;     /* next output */
 656     unsigned have, left;        /* available input and output */
 657     unsigned long hold;         /* bit buffer */
 658     unsigned bits;              /* bits in bit buffer */
 659     unsigned in, out;           /* save starting available input and output */
 660     unsigned copy;              /* number of stored or match bytes to copy */
 661     unsigned char FAR *from;    /* where to copy match bytes from */
 662     code here;                  /* current decoding table entry */
 663     code last;                  /* parent table entry */
 664     unsigned len;               /* length to copy for repeats, bits to drop */
 665     int ret;                    /* return code */
 666 #ifdef GUNZIP
 667     unsigned char hbuf[4];      /* buffer for gzip header crc calculation */
 668 #endif
 669     static const unsigned short order[19] = /* permutation of code lengths */
 670         {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
 671 
 672     if (inflateStateCheck(strm) || strm->next_out == Z_NULL ||

1308         if (updatewindow(strm, strm->next_out, out - strm->avail_out)) {
1309             state->mode = MEM;
1310             return Z_MEM_ERROR;
1311         }
1312     in -= strm->avail_in;
1313     out -= strm->avail_out;
1314     strm->total_in += in;
1315     strm->total_out += out;
1316     state->total += out;
1317     if ((state->wrap & 4) && out)
1318         strm->adler = state->check =
1319             UPDATE_CHECK(state->check, strm->next_out - out, out);
1320     strm->data_type = (int)state->bits + (state->last ? 64 : 0) +
1321                       (state->mode == TYPE ? 128 : 0) +
1322                       (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);
1323     if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
1324         ret = Z_BUF_ERROR;
1325     return ret;
1326 }
1327 
1328 int ZEXPORT inflateEnd(strm)
1329 z_streamp strm;
1330 {
1331     struct inflate_state FAR *state;
1332     if (inflateStateCheck(strm))
1333         return Z_STREAM_ERROR;
1334     state = (struct inflate_state FAR *)strm->state;
1335     if (state->window != Z_NULL) ZFREE(strm, state->window);
1336     ZFREE(strm, strm->state);
1337     strm->state = Z_NULL;
1338     Tracev((stderr, "inflate: end\n"));
1339     return Z_OK;
1340 }
1341 
1342 int ZEXPORT inflateGetDictionary(strm, dictionary, dictLength)
1343 z_streamp strm;
1344 Bytef *dictionary;
1345 uInt *dictLength;
1346 {
1347     struct inflate_state FAR *state;
1348 
1349     /* check state */
1350     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1351     state = (struct inflate_state FAR *)strm->state;
1352 
1353     /* copy dictionary */
1354     if (state->whave && dictionary != Z_NULL) {
1355         zmemcpy(dictionary, state->window + state->wnext,
1356                 state->whave - state->wnext);
1357         zmemcpy(dictionary + state->whave - state->wnext,
1358                 state->window, state->wnext);
1359     }
1360     if (dictLength != Z_NULL)
1361         *dictLength = state->whave;
1362     return Z_OK;
1363 }
1364 
1365 int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength)
1366 z_streamp strm;
1367 const Bytef *dictionary;
1368 uInt dictLength;
1369 {
1370     struct inflate_state FAR *state;
1371     unsigned long dictid;
1372     int ret;
1373 
1374     /* check state */
1375     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1376     state = (struct inflate_state FAR *)strm->state;
1377     if (state->wrap != 0 && state->mode != DICT)
1378         return Z_STREAM_ERROR;
1379 
1380     /* check for correct dictionary identifier */
1381     if (state->mode == DICT) {
1382         dictid = adler32(0L, Z_NULL, 0);
1383         dictid = adler32(dictid, dictionary, dictLength);
1384         if (dictid != state->check)
1385             return Z_DATA_ERROR;
1386     }
1387 
1388     /* copy dictionary to window using updatewindow(), which will amend the
1389        existing dictionary if appropriate */
1390     ret = updatewindow(strm, dictionary + dictLength, dictLength);
1391     if (ret) {
1392         state->mode = MEM;
1393         return Z_MEM_ERROR;
1394     }
1395     state->havedict = 1;
1396     Tracev((stderr, "inflate:   dictionary set\n"));
1397     return Z_OK;
1398 }
1399 
1400 int ZEXPORT inflateGetHeader(strm, head)
1401 z_streamp strm;
1402 gz_headerp head;
1403 {
1404     struct inflate_state FAR *state;
1405 
1406     /* check state */
1407     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1408     state = (struct inflate_state FAR *)strm->state;
1409     if ((state->wrap & 2) == 0) return Z_STREAM_ERROR;
1410 
1411     /* save header structure */
1412     state->head = head;
1413     head->done = 0;
1414     return Z_OK;
1415 }
1416 
1417 /*
1418    Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff.  Return when found
1419    or when out of input.  When called, *have is the number of pattern bytes
1420    found in order so far, in 0..3.  On return *have is updated to the new
1421    state.  If on return *have equals four, then the pattern was found and the
1422    return value is how many bytes were read including the last byte of the
1423    pattern.  If *have is less than four, then the pattern has not been found
1424    yet and the return value is len.  In the latter case, syncsearch() can be
1425    called again with more data and the *have state.  *have is initialized to
1426    zero for the first call.
1427  */
1428 local unsigned syncsearch(have, buf, len)
1429 unsigned FAR *have;
1430 const unsigned char FAR *buf;
1431 unsigned len;
1432 {
1433     unsigned got;
1434     unsigned next;
1435 
1436     got = *have;
1437     next = 0;
1438     while (next < len && got < 4) {
1439         if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))
1440             got++;
1441         else if (buf[next])
1442             got = 0;
1443         else
1444             got = 4 - got;
1445         next++;
1446     }
1447     *have = got;
1448     return next;
1449 }
1450 
1451 int ZEXPORT inflateSync(strm)
1452 z_streamp strm;
1453 {
1454     unsigned len;               /* number of bytes to look at or looked at */
1455     int flags;                  /* temporary to save header status */
1456     unsigned long in, out;      /* temporary to save total_in and total_out */
1457     unsigned char buf[4];       /* to restore bit buffer to byte string */
1458     struct inflate_state FAR *state;
1459 
1460     /* check parameters */
1461     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1462     state = (struct inflate_state FAR *)strm->state;
1463     if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR;
1464 
1465     /* if first time, start search in bit buffer */
1466     if (state->mode != SYNC) {
1467         state->mode = SYNC;
1468         state->hold <<= state->bits & 7;
1469         state->bits -= state->bits & 7;
1470         len = 0;
1471         while (state->bits >= 8) {
1472             buf[len++] = (unsigned char)(state->hold);
1473             state->hold >>= 8;
1474             state->bits -= 8;
1475         }
1476         state->have = 0;
1477         syncsearch(&(state->have), buf, len);
1478     }
1479 
1480     /* search available input */
1481     len = syncsearch(&(state->have), strm->next_in, strm->avail_in);
1482     strm->avail_in -= len;
1483     strm->next_in += len;
1484     strm->total_in += len;
1485 
1486     /* return no joy or set up to restart inflate() on a new block */
1487     if (state->have != 4) return Z_DATA_ERROR;
1488     if (state->flags == -1)
1489         state->wrap = 0;    /* if no header yet, treat as raw */
1490     else
1491         state->wrap &= ~4;  /* no point in computing a check value now */
1492     flags = state->flags;
1493     in = strm->total_in;  out = strm->total_out;
1494     inflateReset(strm);
1495     strm->total_in = in;  strm->total_out = out;
1496     state->flags = flags;
1497     state->mode = TYPE;
1498     return Z_OK;
1499 }
1500 
1501 /*
1502    Returns true if inflate is currently at the end of a block generated by
1503    Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
1504    implementation to provide an additional safety check. PPP uses
1505    Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
1506    block. When decompressing, PPP checks that at the end of input packet,
1507    inflate is waiting for these length bytes.
1508  */
1509 int ZEXPORT inflateSyncPoint(strm)
1510 z_streamp strm;
1511 {
1512     struct inflate_state FAR *state;
1513 
1514     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1515     state = (struct inflate_state FAR *)strm->state;
1516     return state->mode == STORED && state->bits == 0;
1517 }
1518 
1519 int ZEXPORT inflateCopy(dest, source)
1520 z_streamp dest;
1521 z_streamp source;
1522 {
1523     struct inflate_state FAR *state;
1524     struct inflate_state FAR *copy;
1525     unsigned char FAR *window;
1526     unsigned wsize;
1527 
1528     /* check input */
1529     if (inflateStateCheck(source) || dest == Z_NULL)
1530         return Z_STREAM_ERROR;
1531     state = (struct inflate_state FAR *)source->state;
1532 
1533     /* allocate space */
1534     copy = (struct inflate_state FAR *)
1535            ZALLOC(source, 1, sizeof(struct inflate_state));
1536     if (copy == Z_NULL) return Z_MEM_ERROR;
1537     window = Z_NULL;
1538     if (state->window != Z_NULL) {
1539         window = (unsigned char FAR *)
1540                  ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));
1541         if (window == Z_NULL) {
1542             ZFREE(source, copy);

1546 
1547     /* copy state */
1548     zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
1549     zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state));
1550     copy->strm = dest;
1551     if (state->lencode >= state->codes &&
1552         state->lencode <= state->codes + ENOUGH - 1) {
1553         copy->lencode = copy->codes + (state->lencode - state->codes);
1554         copy->distcode = copy->codes + (state->distcode - state->codes);
1555     }
1556     copy->next = copy->codes + (state->next - state->codes);
1557     if (window != Z_NULL) {
1558         wsize = 1U << state->wbits;
1559         zmemcpy(window, state->window, wsize);
1560     }
1561     copy->window = window;
1562     dest->state = (struct internal_state FAR *)copy;
1563     return Z_OK;
1564 }
1565 
1566 int ZEXPORT inflateUndermine(strm, subvert)
1567 z_streamp strm;
1568 int subvert;
1569 {
1570     struct inflate_state FAR *state;
1571 
1572     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1573     state = (struct inflate_state FAR *)strm->state;
1574 #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1575     state->sane = !subvert;
1576     return Z_OK;
1577 #else
1578     (void)subvert;
1579     state->sane = 1;
1580     return Z_DATA_ERROR;
1581 #endif
1582 }
1583 
1584 int ZEXPORT inflateValidate(strm, check)
1585 z_streamp strm;
1586 int check;
1587 {
1588     struct inflate_state FAR *state;
1589 
1590     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1591     state = (struct inflate_state FAR *)strm->state;
1592     if (check && state->wrap)
1593         state->wrap |= 4;
1594     else
1595         state->wrap &= ~4;
1596     return Z_OK;
1597 }
1598 
1599 long ZEXPORT inflateMark(strm)
1600 z_streamp strm;
1601 {
1602     struct inflate_state FAR *state;
1603 
1604     if (inflateStateCheck(strm))
1605         return -(1L << 16);
1606     state = (struct inflate_state FAR *)strm->state;
1607     return (long)(((unsigned long)((long)state->back)) << 16) +
1608         (state->mode == COPY ? state->length :
1609             (state->mode == MATCH ? state->was - state->length : 0));
1610 }
1611 
1612 unsigned long ZEXPORT inflateCodesUsed(strm)
1613 z_streamp strm;
1614 {
1615     struct inflate_state FAR *state;
1616     if (inflateStateCheck(strm)) return (unsigned long)-1;
1617     state = (struct inflate_state FAR *)strm->state;
1618     return (unsigned long)(state->next - state->codes);
1619 }

  98  * - Changed inflateBack() interface to provide separate opaque descriptors
  99  *   for the in() and out() functions
 100  * - Changed inflateBack() argument and in_func typedef to swap the length
 101  *   and buffer address return values for the input function
 102  * - Check next_in and next_out for Z_NULL on entry to inflate()
 103  *
 104  * The history for versions after 1.2.0 are in ChangeLog in zlib distribution.
 105  */
 106 
 107 #include "zutil.h"
 108 #include "inftrees.h"
 109 #include "inflate.h"
 110 #include "inffast.h"
 111 
 112 #ifdef MAKEFIXED
 113 #  ifndef BUILDFIXED
 114 #    define BUILDFIXED
 115 #  endif
 116 #endif
 117 
 118 local int inflateStateCheck(z_streamp strm) {













 119     struct inflate_state FAR *state;
 120     if (strm == Z_NULL ||
 121         strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)
 122         return 1;
 123     state = (struct inflate_state FAR *)strm->state;
 124     if (state == Z_NULL || state->strm != strm ||
 125         state->mode < HEAD || state->mode > SYNC)
 126         return 1;
 127     return 0;
 128 }
 129 
 130 int ZEXPORT inflateResetKeep(z_streamp strm) {


 131     struct inflate_state FAR *state;
 132 
 133     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
 134     state = (struct inflate_state FAR *)strm->state;
 135     strm->total_in = strm->total_out = state->total = 0;
 136     strm->msg = Z_NULL;
 137     if (state->wrap)        /* to support ill-conceived Java test suite */
 138         strm->adler = state->wrap & 1;
 139     state->mode = HEAD;
 140     state->last = 0;
 141     state->havedict = 0;
 142     state->flags = -1;
 143     state->dmax = 32768U;
 144     state->head = Z_NULL;
 145     state->hold = 0;
 146     state->bits = 0;
 147     state->lencode = state->distcode = state->next = state->codes;
 148     state->sane = 1;
 149     state->back = -1;
 150     Tracev((stderr, "inflate: reset\n"));
 151     return Z_OK;
 152 }
 153 
 154 int ZEXPORT inflateReset(z_streamp strm) {


 155     struct inflate_state FAR *state;
 156 
 157     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
 158     state = (struct inflate_state FAR *)strm->state;
 159     state->wsize = 0;
 160     state->whave = 0;
 161     state->wnext = 0;
 162     return inflateResetKeep(strm);
 163 }
 164 
 165 int ZEXPORT inflateReset2(z_streamp strm, int windowBits) {



 166     int wrap;
 167     struct inflate_state FAR *state;
 168 
 169     /* get the state */
 170     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
 171     state = (struct inflate_state FAR *)strm->state;
 172 
 173     /* extract wrap request from windowBits parameter */
 174     if (windowBits < 0) {
 175         if (windowBits < -15)
 176             return Z_STREAM_ERROR;
 177         wrap = 0;
 178         windowBits = -windowBits;
 179     }
 180     else {
 181         wrap = (windowBits >> 4) + 5;
 182 #ifdef GUNZIP
 183         if (windowBits < 48)
 184             windowBits &= 15;
 185 #endif
 186     }
 187 
 188     /* set number of window bits, free window if different */
 189     if (windowBits && (windowBits < 8 || windowBits > 15))
 190         return Z_STREAM_ERROR;
 191     if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) {
 192         ZFREE(strm, state->window);
 193         state->window = Z_NULL;
 194     }
 195 
 196     /* update state and reset the rest of it */
 197     state->wrap = wrap;
 198     state->wbits = (unsigned)windowBits;
 199     return inflateReset(strm);
 200 }
 201 
 202 int ZEXPORT inflateInit2_(z_streamp strm, int windowBits,
 203                           const char *version, int stream_size) {




 204     int ret;
 205     struct inflate_state FAR *state;
 206 
 207     if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
 208         stream_size != (int)(sizeof(z_stream)))
 209         return Z_VERSION_ERROR;
 210     if (strm == Z_NULL) return Z_STREAM_ERROR;
 211     strm->msg = Z_NULL;                 /* in case we return an error */
 212     if (strm->zalloc == (alloc_func)0) {
 213 #ifdef Z_SOLO
 214         return Z_STREAM_ERROR;
 215 #else
 216         strm->zalloc = zcalloc;
 217         strm->opaque = (voidpf)0;
 218 #endif
 219     }
 220     if (strm->zfree == (free_func)0)
 221 #ifdef Z_SOLO
 222         return Z_STREAM_ERROR;
 223 #else
 224         strm->zfree = zcfree;
 225 #endif
 226     state = (struct inflate_state FAR *)
 227             ZALLOC(strm, 1, sizeof(struct inflate_state));
 228     if (state == Z_NULL) return Z_MEM_ERROR;
 229     Tracev((stderr, "inflate: allocated\n"));
 230     strm->state = (struct internal_state FAR *)state;
 231     state->strm = strm;
 232     state->window = Z_NULL;
 233     state->mode = HEAD;     /* to pass state test in inflateReset2() */
 234     ret = inflateReset2(strm, windowBits);
 235     if (ret != Z_OK) {
 236         ZFREE(strm, state);
 237         strm->state = Z_NULL;
 238     }
 239     return ret;
 240 }
 241 
 242 int ZEXPORT inflateInit_(z_streamp strm, const char *version,
 243                          int stream_size) {



 244     return inflateInit2_(strm, DEF_WBITS, version, stream_size);
 245 }
 246 
 247 int ZEXPORT inflatePrime(z_streamp strm, int bits, int value) {




 248     struct inflate_state FAR *state;
 249 
 250     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
 251     if (bits == 0)
 252         return Z_OK;
 253     state = (struct inflate_state FAR *)strm->state;
 254     if (bits < 0) {
 255         state->hold = 0;
 256         state->bits = 0;
 257         return Z_OK;
 258     }
 259     if (bits > 16 || state->bits + (uInt)bits > 32) return Z_STREAM_ERROR;
 260     value &= (1L << bits) - 1;
 261     state->hold += (unsigned)value << state->bits;
 262     state->bits += (uInt)bits;
 263     return Z_OK;
 264 }
 265 
 266 /*
 267    Return state with length and distance decoding tables and index sizes set to
 268    fixed code decoding.  Normally this returns fixed tables from inffixed.h.
 269    If BUILDFIXED is defined, then instead this routine builds the tables the
 270    first time it's called, and returns those tables the first time and
 271    thereafter.  This reduces the size of the code by about 2K bytes, in
 272    exchange for a little execution time.  However, BUILDFIXED should not be
 273    used for threaded applications, since the rewriting of the tables and virgin
 274    may not be thread-safe.
 275  */
 276 local void fixedtables(struct inflate_state FAR *state) {


 277 #ifdef BUILDFIXED
 278     static int virgin = 1;
 279     static code *lenfix, *distfix;
 280     static code fixed[544];
 281 
 282     /* build fixed huffman tables if first call (may not be thread safe) */
 283     if (virgin) {
 284         unsigned sym, bits;
 285         static code *next;
 286 
 287         /* literal/length table */
 288         sym = 0;
 289         while (sym < 144) state->lens[sym++] = 8;
 290         while (sym < 256) state->lens[sym++] = 9;
 291         while (sym < 280) state->lens[sym++] = 7;
 292         while (sym < 288) state->lens[sym++] = 8;
 293         next = fixed;
 294         lenfix = next;
 295         bits = 9;
 296         inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);

 318 #include <stdio.h>
 319 
 320 /*
 321    Write out the inffixed.h that is #include'd above.  Defining MAKEFIXED also
 322    defines BUILDFIXED, so the tables are built on the fly.  makefixed() writes
 323    those tables to stdout, which would be piped to inffixed.h.  A small program
 324    can simply call makefixed to do this:
 325 
 326     void makefixed(void);
 327 
 328     int main(void)
 329     {
 330         makefixed();
 331         return 0;
 332     }
 333 
 334    Then that can be linked with zlib built with MAKEFIXED defined and run:
 335 
 336     a.out > inffixed.h
 337  */
 338 void makefixed(void)
 339 {
 340     unsigned low, size;
 341     struct inflate_state state;
 342 
 343     fixedtables(&state);
 344     puts("    /* inffixed.h -- table for decoding fixed codes");
 345     puts("     * Generated automatically by makefixed().");
 346     puts("     */");
 347     puts("");
 348     puts("    /* WARNING: this file should *not* be used by applications.");
 349     puts("       It is part of the implementation of this library and is");
 350     puts("       subject to change. Applications should only use zlib.h.");
 351     puts("     */");
 352     puts("");
 353     size = 1U << 9;
 354     printf("    static const code lenfix[%u] = {", size);
 355     low = 0;
 356     for (;;) {
 357         if ((low % 7) == 0) printf("\n        ");
 358         printf("{%u,%u,%d}", (low & 127) == 99 ? 64 : state.lencode[low].op,

 372         putchar(',');
 373     }
 374     puts("\n    };");
 375 }
 376 #endif /* MAKEFIXED */
 377 
 378 /*
 379    Update the window with the last wsize (normally 32K) bytes written before
 380    returning.  If window does not exist yet, create it.  This is only called
 381    when a window is already in use, or when output has been written during this
 382    inflate call, but the end of the deflate stream has not been reached yet.
 383    It is also called to create a window for dictionary data when a dictionary
 384    is loaded.
 385 
 386    Providing output buffers larger than 32K to inflate() should provide a speed
 387    advantage, since only the last 32K of output is copied to the sliding window
 388    upon return from inflate(), and since all distances after the first 32K of
 389    output will fall in the output data, making match copies simpler and faster.
 390    The advantage may be dependent on the size of the processor's data caches.
 391  */
 392 local int updatewindow(z_streamp strm, const Bytef *end, unsigned copy) {




 393     struct inflate_state FAR *state;
 394     unsigned dist;
 395 
 396     state = (struct inflate_state FAR *)strm->state;
 397 
 398     /* if it hasn't been done already, allocate space for the window */
 399     if (state->window == Z_NULL) {
 400         state->window = (unsigned char FAR *)
 401                         ZALLOC(strm, 1U << state->wbits,
 402                                sizeof(unsigned char));
 403         if (state->window == Z_NULL) return 1;
 404     }
 405 
 406     /* if window not in use yet, initialize */
 407     if (state->wsize == 0) {
 408         state->wsize = 1U << state->wbits;
 409         state->wnext = 0;
 410         state->whave = 0;
 411     }
 412 

 594    When returning, a "goto inf_leave" is used to update the total counters,
 595    update the check value, and determine whether any progress has been made
 596    during that inflate() call in order to return the proper return code.
 597    Progress is defined as a change in either strm->avail_in or strm->avail_out.
 598    When there is a window, goto inf_leave will update the window with the last
 599    output written.  If a goto inf_leave occurs in the middle of decompression
 600    and there is no window currently, goto inf_leave will create one and copy
 601    output to the window for the next call of inflate().
 602 
 603    In this implementation, the flush parameter of inflate() only affects the
 604    return code (per zlib.h).  inflate() always writes as much as possible to
 605    strm->next_out, given the space available and the provided input--the effect
 606    documented in zlib.h of Z_SYNC_FLUSH.  Furthermore, inflate() always defers
 607    the allocation of and copying into a sliding window until necessary, which
 608    provides the effect documented in zlib.h for Z_FINISH when the entire input
 609    stream available.  So the only thing the flush parameter actually does is:
 610    when flush is set to Z_FINISH, inflate() cannot return Z_OK.  Instead it
 611    will return Z_BUF_ERROR if it has not reached the end of the stream.
 612  */
 613 
 614 int ZEXPORT inflate(z_streamp strm, int flush) {



 615     struct inflate_state FAR *state;
 616     z_const unsigned char FAR *next;    /* next input */
 617     unsigned char FAR *put;     /* next output */
 618     unsigned have, left;        /* available input and output */
 619     unsigned long hold;         /* bit buffer */
 620     unsigned bits;              /* bits in bit buffer */
 621     unsigned in, out;           /* save starting available input and output */
 622     unsigned copy;              /* number of stored or match bytes to copy */
 623     unsigned char FAR *from;    /* where to copy match bytes from */
 624     code here;                  /* current decoding table entry */
 625     code last;                  /* parent table entry */
 626     unsigned len;               /* length to copy for repeats, bits to drop */
 627     int ret;                    /* return code */
 628 #ifdef GUNZIP
 629     unsigned char hbuf[4];      /* buffer for gzip header crc calculation */
 630 #endif
 631     static const unsigned short order[19] = /* permutation of code lengths */
 632         {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
 633 
 634     if (inflateStateCheck(strm) || strm->next_out == Z_NULL ||

1270         if (updatewindow(strm, strm->next_out, out - strm->avail_out)) {
1271             state->mode = MEM;
1272             return Z_MEM_ERROR;
1273         }
1274     in -= strm->avail_in;
1275     out -= strm->avail_out;
1276     strm->total_in += in;
1277     strm->total_out += out;
1278     state->total += out;
1279     if ((state->wrap & 4) && out)
1280         strm->adler = state->check =
1281             UPDATE_CHECK(state->check, strm->next_out - out, out);
1282     strm->data_type = (int)state->bits + (state->last ? 64 : 0) +
1283                       (state->mode == TYPE ? 128 : 0) +
1284                       (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);
1285     if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
1286         ret = Z_BUF_ERROR;
1287     return ret;
1288 }
1289 
1290 int ZEXPORT inflateEnd(z_streamp strm) {


1291     struct inflate_state FAR *state;
1292     if (inflateStateCheck(strm))
1293         return Z_STREAM_ERROR;
1294     state = (struct inflate_state FAR *)strm->state;
1295     if (state->window != Z_NULL) ZFREE(strm, state->window);
1296     ZFREE(strm, strm->state);
1297     strm->state = Z_NULL;
1298     Tracev((stderr, "inflate: end\n"));
1299     return Z_OK;
1300 }
1301 
1302 int ZEXPORT inflateGetDictionary(z_streamp strm, Bytef *dictionary,
1303                                  uInt *dictLength) {



1304     struct inflate_state FAR *state;
1305 
1306     /* check state */
1307     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1308     state = (struct inflate_state FAR *)strm->state;
1309 
1310     /* copy dictionary */
1311     if (state->whave && dictionary != Z_NULL) {
1312         zmemcpy(dictionary, state->window + state->wnext,
1313                 state->whave - state->wnext);
1314         zmemcpy(dictionary + state->whave - state->wnext,
1315                 state->window, state->wnext);
1316     }
1317     if (dictLength != Z_NULL)
1318         *dictLength = state->whave;
1319     return Z_OK;
1320 }
1321 
1322 int ZEXPORT inflateSetDictionary(z_streamp strm, const Bytef *dictionary,
1323                                  uInt dictLength) {



1324     struct inflate_state FAR *state;
1325     unsigned long dictid;
1326     int ret;
1327 
1328     /* check state */
1329     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1330     state = (struct inflate_state FAR *)strm->state;
1331     if (state->wrap != 0 && state->mode != DICT)
1332         return Z_STREAM_ERROR;
1333 
1334     /* check for correct dictionary identifier */
1335     if (state->mode == DICT) {
1336         dictid = adler32(0L, Z_NULL, 0);
1337         dictid = adler32(dictid, dictionary, dictLength);
1338         if (dictid != state->check)
1339             return Z_DATA_ERROR;
1340     }
1341 
1342     /* copy dictionary to window using updatewindow(), which will amend the
1343        existing dictionary if appropriate */
1344     ret = updatewindow(strm, dictionary + dictLength, dictLength);
1345     if (ret) {
1346         state->mode = MEM;
1347         return Z_MEM_ERROR;
1348     }
1349     state->havedict = 1;
1350     Tracev((stderr, "inflate:   dictionary set\n"));
1351     return Z_OK;
1352 }
1353 
1354 int ZEXPORT inflateGetHeader(z_streamp strm, gz_headerp head) {



1355     struct inflate_state FAR *state;
1356 
1357     /* check state */
1358     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1359     state = (struct inflate_state FAR *)strm->state;
1360     if ((state->wrap & 2) == 0) return Z_STREAM_ERROR;
1361 
1362     /* save header structure */
1363     state->head = head;
1364     head->done = 0;
1365     return Z_OK;
1366 }
1367 
1368 /*
1369    Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff.  Return when found
1370    or when out of input.  When called, *have is the number of pattern bytes
1371    found in order so far, in 0..3.  On return *have is updated to the new
1372    state.  If on return *have equals four, then the pattern was found and the
1373    return value is how many bytes were read including the last byte of the
1374    pattern.  If *have is less than four, then the pattern has not been found
1375    yet and the return value is len.  In the latter case, syncsearch() can be
1376    called again with more data and the *have state.  *have is initialized to
1377    zero for the first call.
1378  */
1379 local unsigned syncsearch(unsigned FAR *have, const unsigned char FAR *buf,
1380                           unsigned len) {



1381     unsigned got;
1382     unsigned next;
1383 
1384     got = *have;
1385     next = 0;
1386     while (next < len && got < 4) {
1387         if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))
1388             got++;
1389         else if (buf[next])
1390             got = 0;
1391         else
1392             got = 4 - got;
1393         next++;
1394     }
1395     *have = got;
1396     return next;
1397 }
1398 
1399 int ZEXPORT inflateSync(z_streamp strm) {


1400     unsigned len;               /* number of bytes to look at or looked at */
1401     int flags;                  /* temporary to save header status */
1402     unsigned long in, out;      /* temporary to save total_in and total_out */
1403     unsigned char buf[4];       /* to restore bit buffer to byte string */
1404     struct inflate_state FAR *state;
1405 
1406     /* check parameters */
1407     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1408     state = (struct inflate_state FAR *)strm->state;
1409     if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR;
1410 
1411     /* if first time, start search in bit buffer */
1412     if (state->mode != SYNC) {
1413         state->mode = SYNC;
1414         state->hold >>= state->bits & 7;
1415         state->bits -= state->bits & 7;
1416         len = 0;
1417         while (state->bits >= 8) {
1418             buf[len++] = (unsigned char)(state->hold);
1419             state->hold >>= 8;
1420             state->bits -= 8;
1421         }
1422         state->have = 0;
1423         syncsearch(&(state->have), buf, len);
1424     }
1425 
1426     /* search available input */
1427     len = syncsearch(&(state->have), strm->next_in, strm->avail_in);
1428     strm->avail_in -= len;
1429     strm->next_in += len;
1430     strm->total_in += len;
1431 
1432     /* return no joy or set up to restart inflate() on a new block */
1433     if (state->have != 4) return Z_DATA_ERROR;
1434     if (state->flags == -1)
1435         state->wrap = 0;    /* if no header yet, treat as raw */
1436     else
1437         state->wrap &= ~4;  /* no point in computing a check value now */
1438     flags = state->flags;
1439     in = strm->total_in;  out = strm->total_out;
1440     inflateReset(strm);
1441     strm->total_in = in;  strm->total_out = out;
1442     state->flags = flags;
1443     state->mode = TYPE;
1444     return Z_OK;
1445 }
1446 
1447 /*
1448    Returns true if inflate is currently at the end of a block generated by
1449    Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
1450    implementation to provide an additional safety check. PPP uses
1451    Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
1452    block. When decompressing, PPP checks that at the end of input packet,
1453    inflate is waiting for these length bytes.
1454  */
1455 int ZEXPORT inflateSyncPoint(z_streamp strm) {


1456     struct inflate_state FAR *state;
1457 
1458     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1459     state = (struct inflate_state FAR *)strm->state;
1460     return state->mode == STORED && state->bits == 0;
1461 }
1462 
1463 int ZEXPORT inflateCopy(z_streamp dest, z_streamp source) {



1464     struct inflate_state FAR *state;
1465     struct inflate_state FAR *copy;
1466     unsigned char FAR *window;
1467     unsigned wsize;
1468 
1469     /* check input */
1470     if (inflateStateCheck(source) || dest == Z_NULL)
1471         return Z_STREAM_ERROR;
1472     state = (struct inflate_state FAR *)source->state;
1473 
1474     /* allocate space */
1475     copy = (struct inflate_state FAR *)
1476            ZALLOC(source, 1, sizeof(struct inflate_state));
1477     if (copy == Z_NULL) return Z_MEM_ERROR;
1478     window = Z_NULL;
1479     if (state->window != Z_NULL) {
1480         window = (unsigned char FAR *)
1481                  ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));
1482         if (window == Z_NULL) {
1483             ZFREE(source, copy);

1487 
1488     /* copy state */
1489     zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
1490     zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state));
1491     copy->strm = dest;
1492     if (state->lencode >= state->codes &&
1493         state->lencode <= state->codes + ENOUGH - 1) {
1494         copy->lencode = copy->codes + (state->lencode - state->codes);
1495         copy->distcode = copy->codes + (state->distcode - state->codes);
1496     }
1497     copy->next = copy->codes + (state->next - state->codes);
1498     if (window != Z_NULL) {
1499         wsize = 1U << state->wbits;
1500         zmemcpy(window, state->window, wsize);
1501     }
1502     copy->window = window;
1503     dest->state = (struct internal_state FAR *)copy;
1504     return Z_OK;
1505 }
1506 
1507 int ZEXPORT inflateUndermine(z_streamp strm, int subvert) {



1508     struct inflate_state FAR *state;
1509 
1510     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1511     state = (struct inflate_state FAR *)strm->state;
1512 #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1513     state->sane = !subvert;
1514     return Z_OK;
1515 #else
1516     (void)subvert;
1517     state->sane = 1;
1518     return Z_DATA_ERROR;
1519 #endif
1520 }
1521 
1522 int ZEXPORT inflateValidate(z_streamp strm, int check) {



1523     struct inflate_state FAR *state;
1524 
1525     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1526     state = (struct inflate_state FAR *)strm->state;
1527     if (check && state->wrap)
1528         state->wrap |= 4;
1529     else
1530         state->wrap &= ~4;
1531     return Z_OK;
1532 }
1533 
1534 long ZEXPORT inflateMark(z_streamp strm) {


1535     struct inflate_state FAR *state;
1536 
1537     if (inflateStateCheck(strm))
1538         return -(1L << 16);
1539     state = (struct inflate_state FAR *)strm->state;
1540     return (long)(((unsigned long)((long)state->back)) << 16) +
1541         (state->mode == COPY ? state->length :
1542             (state->mode == MATCH ? state->was - state->length : 0));
1543 }
1544 
1545 unsigned long ZEXPORT inflateCodesUsed(z_streamp strm) {


1546     struct inflate_state FAR *state;
1547     if (inflateStateCheck(strm)) return (unsigned long)-1;
1548     state = (struct inflate_state FAR *)strm->state;
1549     return (unsigned long)(state->next - state->codes);
1550 }
< prev index next >