os/ossrv/compressionlibs/ziplib/src/zlib/infback.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* Portions Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
 * All rights reserved.
sl@0
     3
 */
sl@0
     4
sl@0
     5
/* infback.cpp -- inflate using a call-back interface
sl@0
     6
 * Copyright (C) 1995-2005 Mark Adler
sl@0
     7
 * For conditions of distribution and use, see copyright notice in zlib.h
sl@0
     8
 */
sl@0
     9
sl@0
    10
/*
sl@0
    11
   This code is largely copied from inflate.c.  Normally either infback.o or
sl@0
    12
   inflate.o would be linked into an application--not both.  The interface
sl@0
    13
   with inffast.c is retained so that optimized assembler-coded versions of
sl@0
    14
   inflate_fast() can be used with either inflate.c or infback.c.
sl@0
    15
 */
sl@0
    16
sl@0
    17
#include "zutil.h"
sl@0
    18
#include "inftrees.h"
sl@0
    19
#include "inflate.h"
sl@0
    20
#include "inffast.h"
sl@0
    21
sl@0
    22
/* function prototypes */
sl@0
    23
local void fixedtables OF((struct inflate_state FAR *state));
sl@0
    24
sl@0
    25
/*
sl@0
    26
   strm provides memory allocation functions in zalloc and zfree, or
sl@0
    27
   Z_NULL to use the library memory allocation functions.
sl@0
    28
sl@0
    29
   windowBits is in the range 8..15, and window is a user-supplied
sl@0
    30
   window and output buffer that is 2**windowBits bytes.
sl@0
    31
 */
sl@0
    32
sl@0
    33
#ifdef __SYMBIAN32__
sl@0
    34
EXPORT_C int inflateBackInit__r(z_streamp strm, int windowBits,unsigned char FAR *  window,const char *  version,int stream_size)
sl@0
    35
#else
sl@0
    36
int ZEXPORT inflateBackInit_(strm, windowBits, window, version, stream_size)
sl@0
    37
z_streamp strm;
sl@0
    38
int windowBits;
sl@0
    39
unsigned char FAR *window;
sl@0
    40
const char *version;
sl@0
    41
int stream_size;
sl@0
    42
#endif //__SYMBIAN32__
sl@0
    43
{
sl@0
    44
    struct inflate_state FAR *state;
sl@0
    45
sl@0
    46
    if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
sl@0
    47
        stream_size != (int)(sizeof(z_stream)))
sl@0
    48
        return Z_VERSION_ERROR;
sl@0
    49
    if (strm == Z_NULL || window == Z_NULL ||
sl@0
    50
        windowBits < 8 || windowBits > 15)
sl@0
    51
        return Z_STREAM_ERROR;
sl@0
    52
    strm->msg = Z_NULL;                 /* in case we return an error */
sl@0
    53
    if (strm->zalloc == (alloc_func)0) {
sl@0
    54
        strm->zalloc = zcalloc;
sl@0
    55
        strm->opaque = (voidpf)0;
sl@0
    56
    }
sl@0
    57
    if (strm->zfree == (free_func)0) strm->zfree = zcfree;
sl@0
    58
    state = (struct inflate_state FAR *)ZALLOC(strm, 1,
sl@0
    59
                                               sizeof(struct inflate_state));
sl@0
    60
    if (state == Z_NULL) return Z_MEM_ERROR;
sl@0
    61
    Tracev((stderr, "inflate: allocated\n"));
sl@0
    62
    strm->state = (struct internal_state FAR *)state;
sl@0
    63
    state->dmax = 32768U;
sl@0
    64
    state->wbits = windowBits;
sl@0
    65
    state->wsize = 1U << windowBits;
sl@0
    66
    state->window = window;
sl@0
    67
    state->write = 0;
sl@0
    68
    state->whave = 0;
sl@0
    69
    return Z_OK;
sl@0
    70
}
sl@0
    71
sl@0
    72
sl@0
    73
/*
sl@0
    74
   Return state with length and distance decoding tables and index sizes set to
sl@0
    75
   fixed code decoding.  Normally this returns fixed tables from inffixed.h.
sl@0
    76
   If BUILDFIXED is defined, then instead this routine builds the tables the
sl@0
    77
   first time it's called, and returns those tables the first time and
sl@0
    78
   thereafter.  This reduces the size of the code by about 2K bytes, in
sl@0
    79
   exchange for a little execution time.  However, BUILDFIXED should not be
sl@0
    80
   used for threaded applications, since the rewriting of the tables and virgin
sl@0
    81
   may not be thread-safe.
sl@0
    82
 */
sl@0
    83
sl@0
    84
#ifdef __SYMBIAN32__
sl@0
    85
local void fixedtables(struct inflate_state FAR * state)
sl@0
    86
#else
sl@0
    87
local void fixedtables(state)
sl@0
    88
struct inflate_state FAR *state;
sl@0
    89
#endif //__SYMBIAN32__
sl@0
    90
{
sl@0
    91
#ifdef BUILDFIXED
sl@0
    92
    static int virgin = 1;
sl@0
    93
    static code *lenfix, *distfix;
sl@0
    94
    static code fixed[544];
sl@0
    95
sl@0
    96
    /* build fixed huffman tables if first call (may not be thread safe) */
sl@0
    97
    if (virgin) {
sl@0
    98
        unsigned sym, bits;
sl@0
    99
        static code *next;
sl@0
   100
sl@0
   101
        /* literal/length table */
sl@0
   102
        sym = 0;
sl@0
   103
        while (sym < 144) state->lens[sym++] = 8;
sl@0
   104
        while (sym < 256) state->lens[sym++] = 9;
sl@0
   105
        while (sym < 280) state->lens[sym++] = 7;
sl@0
   106
        while (sym < 288) state->lens[sym++] = 8;
sl@0
   107
        next = fixed;
sl@0
   108
        lenfix = next;
sl@0
   109
        bits = 9;
sl@0
   110
        inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
sl@0
   111
sl@0
   112
        /* distance table */
sl@0
   113
        sym = 0;
sl@0
   114
        while (sym < 32) state->lens[sym++] = 5;
sl@0
   115
        distfix = next;
sl@0
   116
        bits = 5;
sl@0
   117
        inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
sl@0
   118
sl@0
   119
        /* do this just once */
sl@0
   120
        virgin = 0;
sl@0
   121
    }
sl@0
   122
#else /* !BUILDFIXED */
sl@0
   123
#   include "inffixed.h"
sl@0
   124
#endif /* BUILDFIXED */
sl@0
   125
    state->lencode = lenfix;
sl@0
   126
    state->lenbits = 9;
sl@0
   127
    state->distcode = distfix;
sl@0
   128
    state->distbits = 5;
sl@0
   129
}
sl@0
   130
sl@0
   131
sl@0
   132
/* Macros for inflateBack(): */
sl@0
   133
sl@0
   134
/* Load returned state from inflate_fast() */
sl@0
   135
#define LOAD() \
sl@0
   136
    do { \
sl@0
   137
        put = strm->next_out; \
sl@0
   138
        left = strm->avail_out; \
sl@0
   139
        next = strm->next_in; \
sl@0
   140
        have = strm->avail_in; \
sl@0
   141
        hold = state->hold; \
sl@0
   142
        bits = state->bits; \
sl@0
   143
    } while (0)
sl@0
   144
sl@0
   145
/* Set state from registers for inflate_fast() */
sl@0
   146
#define RESTORE() \
sl@0
   147
    do { \
sl@0
   148
        strm->next_out = put; \
sl@0
   149
        strm->avail_out = left; \
sl@0
   150
        strm->next_in = next; \
sl@0
   151
        strm->avail_in = have; \
sl@0
   152
        state->hold = hold; \
sl@0
   153
        state->bits = bits; \
sl@0
   154
    } while (0)
sl@0
   155
sl@0
   156
/* Clear the input bit accumulator */
sl@0
   157
#define INITBITS() \
sl@0
   158
    do { \
sl@0
   159
        hold = 0; \
sl@0
   160
        bits = 0; \
sl@0
   161
    } while (0)
sl@0
   162
sl@0
   163
/* Assure that some input is available.  If input is requested, but denied,
sl@0
   164
   then return a Z_BUF_ERROR from inflateBack(). */
sl@0
   165
#define PULL() \
sl@0
   166
    do { \
sl@0
   167
        if (have == 0) { \
sl@0
   168
            have = in(in_desc, &next); \
sl@0
   169
            if (have == 0) { \
sl@0
   170
                next = Z_NULL; \
sl@0
   171
                ret = Z_BUF_ERROR; \
sl@0
   172
                goto inf_leave; \
sl@0
   173
            } \
sl@0
   174
        } \
sl@0
   175
    } while (0)
sl@0
   176
sl@0
   177
/* Get a byte of input into the bit accumulator, or return from inflateBack()
sl@0
   178
   with an error if there is no input available. */
sl@0
   179
#define PULLBYTE() \
sl@0
   180
    do { \
sl@0
   181
        PULL(); \
sl@0
   182
        have--; \
sl@0
   183
        hold += (unsigned long)(*next++) << bits; \
sl@0
   184
        bits += 8; \
sl@0
   185
    } while (0)
sl@0
   186
sl@0
   187
/* Assure that there are at least n bits in the bit accumulator.  If there is
sl@0
   188
   not enough available input to do that, then return from inflateBack() with
sl@0
   189
   an error. */
sl@0
   190
#define NEEDBITS(n) \
sl@0
   191
    do { \
sl@0
   192
        while (bits < (unsigned)(n)) \
sl@0
   193
            PULLBYTE(); \
sl@0
   194
    } while (0)
sl@0
   195
sl@0
   196
/* Return the low n bits of the bit accumulator (n < 16) */
sl@0
   197
#define BITS(n) \
sl@0
   198
    ((unsigned)hold & ((1U << (n)) - 1))
sl@0
   199
sl@0
   200
/* Remove n bits from the bit accumulator */
sl@0
   201
#define DROPBITS(n) \
sl@0
   202
    do { \
sl@0
   203
        hold >>= (n); \
sl@0
   204
        bits -= (unsigned)(n); \
sl@0
   205
    } while (0)
sl@0
   206
sl@0
   207
/* Remove zero to seven bits as needed to go to a byte boundary */
sl@0
   208
#define BYTEBITS() \
sl@0
   209
    do { \
sl@0
   210
        hold >>= bits & 7; \
sl@0
   211
        bits -= bits & 7; \
sl@0
   212
    } while (0)
sl@0
   213
sl@0
   214
/* Assure that some output space is available, by writing out the window
sl@0
   215
   if it's full.  If the write fails, return from inflateBack() with a
sl@0
   216
   Z_BUF_ERROR. */
sl@0
   217
#define ROOM() \
sl@0
   218
    do { \
sl@0
   219
        if (left == 0) { \
sl@0
   220
            put = state->window; \
sl@0
   221
            left = state->wsize; \
sl@0
   222
            state->whave = left; \
sl@0
   223
            if (out(out_desc, put, left)) { \
sl@0
   224
                ret = Z_BUF_ERROR; \
sl@0
   225
                goto inf_leave; \
sl@0
   226
            } \
sl@0
   227
        } \
sl@0
   228
    } while (0)
sl@0
   229
sl@0
   230
/*
sl@0
   231
   strm provides the memory allocation functions and window buffer on input,
sl@0
   232
   and provides information on the unused input on return.  For Z_DATA_ERROR
sl@0
   233
   returns, strm will also provide an error message.
sl@0
   234
sl@0
   235
   in() and out() are the call-back input and output functions.  When
sl@0
   236
   inflateBack() needs more input, it calls in().  When inflateBack() has
sl@0
   237
   filled the window with output, or when it completes with data in the
sl@0
   238
   window, it calls out() to write out the data.  The application must not
sl@0
   239
   change the provided input until in() is called again or inflateBack()
sl@0
   240
   returns.  The application must not change the window/output buffer until
sl@0
   241
   inflateBack() returns.
sl@0
   242
sl@0
   243
   in() and out() are called with a descriptor parameter provided in the
sl@0
   244
   inflateBack() call.  This parameter can be a structure that provides the
sl@0
   245
   information required to do the read or write, as well as accumulated
sl@0
   246
   information on the input and output such as totals and check values.
sl@0
   247
sl@0
   248
   in() should return zero on failure.  out() should return non-zero on
sl@0
   249
   failure.  If either in() or out() fails, than inflateBack() returns a
sl@0
   250
   Z_BUF_ERROR.  strm->next_in can be checked for Z_NULL to see whether it
sl@0
   251
   was in() or out() that caused in the error.  Otherwise,  inflateBack()
sl@0
   252
   returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format
sl@0
   253
   error, or Z_MEM_ERROR if it could not allocate memory for the state.
sl@0
   254
   inflateBack() can also return Z_STREAM_ERROR if the input parameters
sl@0
   255
   are not correct, i.e. strm is Z_NULL or the state was not initialized.
sl@0
   256
 */
sl@0
   257
sl@0
   258
#ifdef __SYMBIAN32__
sl@0
   259
EXPORT_C int inflateBack_r(z_streamp strm,in_func  in, void FAR * in_desc,out_func  out,void FAR * out_desc)
sl@0
   260
#else
sl@0
   261
int ZEXPORT inflateBack(strm, in, in_desc, out, out_desc)
sl@0
   262
z_streamp strm;
sl@0
   263
in_func in;
sl@0
   264
void FAR *in_desc;
sl@0
   265
out_func out;
sl@0
   266
void FAR *out_desc;
sl@0
   267
#endif //__SYMBIAN32__
sl@0
   268
{
sl@0
   269
    struct inflate_state FAR *state;
sl@0
   270
    unsigned char FAR *next;    /* next input */
sl@0
   271
    unsigned char FAR *put;     /* next output */
sl@0
   272
    unsigned have, left;        /* available input and output */
sl@0
   273
    unsigned long hold;         /* bit buffer */
sl@0
   274
    unsigned bits;              /* bits in bit buffer */
sl@0
   275
    unsigned copy;              /* number of stored or match bytes to copy */
sl@0
   276
    unsigned char FAR *from;    /* where to copy match bytes from */
sl@0
   277
    
sl@0
   278
/*  Need to replace "this" variable with "current" as "this" is a reserved 
sl@0
   279
 *  keyword in C++ which is prefectly fine for a c code. As this file
sl@0
   280
 *  has been changed to C++ "this" needs to be changed.
sl@0
   281
 */ 
sl@0
   282
#   define this current      
sl@0
   283
    code this;                  /* current decoding table entry */
sl@0
   284
    code last;                  /* parent table entry */
sl@0
   285
    unsigned len;               /* length to copy for repeats, bits to drop */
sl@0
   286
    int ret;                    /* return code */
sl@0
   287
    static const unsigned short order[19] = /* permutation of code lengths */
sl@0
   288
        {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
sl@0
   289
sl@0
   290
    /* Check that the strm exists and that the state was initialized */
sl@0
   291
    if (strm == Z_NULL || strm->state == Z_NULL)
sl@0
   292
        return Z_STREAM_ERROR;
sl@0
   293
    state = (struct inflate_state FAR *)strm->state;
sl@0
   294
sl@0
   295
    /* Reset the state */
sl@0
   296
    strm->msg = Z_NULL;
sl@0
   297
    state->mode = TYPE;
sl@0
   298
    state->last = 0;
sl@0
   299
    state->whave = 0;
sl@0
   300
    next = strm->next_in;
sl@0
   301
    have = next != Z_NULL ? strm->avail_in : 0;
sl@0
   302
    hold = 0;
sl@0
   303
    bits = 0;
sl@0
   304
    put = state->window;
sl@0
   305
    left = state->wsize;
sl@0
   306
sl@0
   307
    /* Inflate until end of block marked as last */
sl@0
   308
    for (;;)
sl@0
   309
        switch (state->mode) {
sl@0
   310
        case TYPE:
sl@0
   311
            /* determine and dispatch block type */
sl@0
   312
            if (state->last) {
sl@0
   313
                BYTEBITS();
sl@0
   314
                state->mode = DONE;
sl@0
   315
                break;
sl@0
   316
            }
sl@0
   317
            NEEDBITS(3);
sl@0
   318
            state->last = BITS(1);
sl@0
   319
            DROPBITS(1);
sl@0
   320
            switch (BITS(2)) {
sl@0
   321
            case 0:                             /* stored block */
sl@0
   322
                Tracev((stderr, "inflate:     stored block%s\n",
sl@0
   323
                        state->last ? " (last)" : ""));
sl@0
   324
                state->mode = STORED;
sl@0
   325
                break;
sl@0
   326
            case 1:                             /* fixed block */
sl@0
   327
                fixedtables(state);
sl@0
   328
                Tracev((stderr, "inflate:     fixed codes block%s\n",
sl@0
   329
                        state->last ? " (last)" : ""));
sl@0
   330
                state->mode = LEN;              /* decode codes */
sl@0
   331
                break;
sl@0
   332
            case 2:                             /* dynamic block */
sl@0
   333
                Tracev((stderr, "inflate:     dynamic codes block%s\n",
sl@0
   334
                        state->last ? " (last)" : ""));
sl@0
   335
                state->mode = TABLE;
sl@0
   336
                break;
sl@0
   337
            case 3:
sl@0
   338
                strm->msg = (char *)"invalid block type";
sl@0
   339
                state->mode = BAD;
sl@0
   340
            }
sl@0
   341
            DROPBITS(2);
sl@0
   342
            break;
sl@0
   343
sl@0
   344
        case STORED:
sl@0
   345
            /* get and verify stored block length */
sl@0
   346
            BYTEBITS();                         /* go to byte boundary */
sl@0
   347
            NEEDBITS(32);
sl@0
   348
            if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
sl@0
   349
                strm->msg = (char *)"invalid stored block lengths";
sl@0
   350
                state->mode = BAD;
sl@0
   351
                break;
sl@0
   352
            }
sl@0
   353
            state->length = (unsigned)hold & 0xffff;
sl@0
   354
            Tracev((stderr, "inflate:       stored length %u\n",
sl@0
   355
                    state->length));
sl@0
   356
            INITBITS();
sl@0
   357
sl@0
   358
            /* copy stored block from input to output */
sl@0
   359
            while (state->length != 0) {
sl@0
   360
                copy = state->length;
sl@0
   361
                PULL();
sl@0
   362
                ROOM();
sl@0
   363
                if (copy > have) copy = have;
sl@0
   364
                if (copy > left) copy = left;
sl@0
   365
                zmemcpy(put, next, copy);
sl@0
   366
                have -= copy;
sl@0
   367
                next += copy;
sl@0
   368
                left -= copy;
sl@0
   369
                put += copy;
sl@0
   370
                state->length -= copy;
sl@0
   371
            }
sl@0
   372
            Tracev((stderr, "inflate:       stored end\n"));
sl@0
   373
            state->mode = TYPE;
sl@0
   374
            break;
sl@0
   375
sl@0
   376
        case TABLE:
sl@0
   377
            /* get dynamic table entries descriptor */
sl@0
   378
            NEEDBITS(14);
sl@0
   379
            state->nlen = BITS(5) + 257;
sl@0
   380
            DROPBITS(5);
sl@0
   381
            state->ndist = BITS(5) + 1;
sl@0
   382
            DROPBITS(5);
sl@0
   383
            state->ncode = BITS(4) + 4;
sl@0
   384
            DROPBITS(4);
sl@0
   385
#ifndef PKZIP_BUG_WORKAROUND
sl@0
   386
            if (state->nlen > 286 || state->ndist > 30) {
sl@0
   387
                strm->msg = (char *)"too many length or distance symbols";
sl@0
   388
                state->mode = BAD;
sl@0
   389
                break;
sl@0
   390
            }
sl@0
   391
#endif
sl@0
   392
            Tracev((stderr, "inflate:       table sizes ok\n"));
sl@0
   393
sl@0
   394
            /* get code length code lengths (not a typo) */
sl@0
   395
            state->have = 0;
sl@0
   396
            while (state->have < state->ncode) {
sl@0
   397
                NEEDBITS(3);
sl@0
   398
                state->lens[order[state->have++]] = (unsigned short)BITS(3);
sl@0
   399
                DROPBITS(3);
sl@0
   400
            }
sl@0
   401
            while (state->have < 19)
sl@0
   402
                state->lens[order[state->have++]] = 0;
sl@0
   403
            state->next = state->codes;
sl@0
   404
            state->lencode = (code const FAR *)(state->next);
sl@0
   405
            state->lenbits = 7;
sl@0
   406
            ret = inflate_table(CODES, state->lens, 19, &(state->next),
sl@0
   407
                                &(state->lenbits), state->work);
sl@0
   408
            if (ret) {
sl@0
   409
                strm->msg = (char *)"invalid code lengths set";
sl@0
   410
                state->mode = BAD;
sl@0
   411
                break;
sl@0
   412
            }
sl@0
   413
            Tracev((stderr, "inflate:       code lengths ok\n"));
sl@0
   414
sl@0
   415
            /* get length and distance code code lengths */
sl@0
   416
            state->have = 0;
sl@0
   417
            while (state->have < state->nlen + state->ndist) {
sl@0
   418
                for (;;) {
sl@0
   419
                    this = state->lencode[BITS(state->lenbits)];
sl@0
   420
                    if ((unsigned)(this.bits) <= bits) break;
sl@0
   421
                    PULLBYTE();
sl@0
   422
                }
sl@0
   423
                if (this.val < 16) {
sl@0
   424
                    NEEDBITS(this.bits);
sl@0
   425
                    DROPBITS(this.bits);
sl@0
   426
                    state->lens[state->have++] = this.val;
sl@0
   427
                }
sl@0
   428
                else {
sl@0
   429
                    if (this.val == 16) {
sl@0
   430
                        NEEDBITS(this.bits + 2);
sl@0
   431
                        DROPBITS(this.bits);
sl@0
   432
                        if (state->have == 0) {
sl@0
   433
                            strm->msg = (char *)"invalid bit length repeat";
sl@0
   434
                            state->mode = BAD;
sl@0
   435
                            break;
sl@0
   436
                        }
sl@0
   437
                        len = (unsigned)(state->lens[state->have - 1]);
sl@0
   438
                        copy = 3 + BITS(2);
sl@0
   439
                        DROPBITS(2);
sl@0
   440
                    }
sl@0
   441
                    else if (this.val == 17) {
sl@0
   442
                        NEEDBITS(this.bits + 3);
sl@0
   443
                        DROPBITS(this.bits);
sl@0
   444
                        len = 0;
sl@0
   445
                        copy = 3 + BITS(3);
sl@0
   446
                        DROPBITS(3);
sl@0
   447
                    }
sl@0
   448
                    else {
sl@0
   449
                        NEEDBITS(this.bits + 7);
sl@0
   450
                        DROPBITS(this.bits);
sl@0
   451
                        len = 0;
sl@0
   452
                        copy = 11 + BITS(7);
sl@0
   453
                        DROPBITS(7);
sl@0
   454
                    }
sl@0
   455
                    if (state->have + copy > state->nlen + state->ndist) {
sl@0
   456
                        strm->msg = (char *)"invalid bit length repeat";
sl@0
   457
                        state->mode = BAD;
sl@0
   458
                        break;
sl@0
   459
                    }
sl@0
   460
                    while (copy--)
sl@0
   461
                        state->lens[state->have++] = (unsigned short)len;
sl@0
   462
                }
sl@0
   463
            }
sl@0
   464
sl@0
   465
            /* handle error breaks in while */
sl@0
   466
            if (state->mode == BAD) break;
sl@0
   467
sl@0
   468
            /* build code tables */
sl@0
   469
            state->next = state->codes;
sl@0
   470
            state->lencode = (code const FAR *)(state->next);
sl@0
   471
            state->lenbits = 9;
sl@0
   472
            ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
sl@0
   473
                                &(state->lenbits), state->work);
sl@0
   474
            if (ret) {
sl@0
   475
                strm->msg = (char *)"invalid literal/lengths set";
sl@0
   476
                state->mode = BAD;
sl@0
   477
                break;
sl@0
   478
            }
sl@0
   479
            state->distcode = (code const FAR *)(state->next);
sl@0
   480
            state->distbits = 6;
sl@0
   481
            ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
sl@0
   482
                            &(state->next), &(state->distbits), state->work);
sl@0
   483
            if (ret) {
sl@0
   484
                strm->msg = (char *)"invalid distances set";
sl@0
   485
                state->mode = BAD;
sl@0
   486
                break;
sl@0
   487
            }
sl@0
   488
            Tracev((stderr, "inflate:       codes ok\n"));
sl@0
   489
            state->mode = LEN;
sl@0
   490
sl@0
   491
        case LEN:
sl@0
   492
            /* use inflate_fast() if we have enough input and output */
sl@0
   493
            if (have >= 6 && left >= 258) {
sl@0
   494
                RESTORE();
sl@0
   495
                if (state->whave < state->wsize)
sl@0
   496
                    state->whave = state->wsize - left;
sl@0
   497
                inflate_fast(strm, state->wsize);
sl@0
   498
                LOAD();
sl@0
   499
                break;
sl@0
   500
            }
sl@0
   501
sl@0
   502
            /* get a literal, length, or end-of-block code */
sl@0
   503
            for (;;) {
sl@0
   504
                this = state->lencode[BITS(state->lenbits)];
sl@0
   505
                if ((unsigned)(this.bits) <= bits) break;
sl@0
   506
                PULLBYTE();
sl@0
   507
            }
sl@0
   508
            if (this.op && (this.op & 0xf0) == 0) {
sl@0
   509
                last = this;
sl@0
   510
                for (;;) {
sl@0
   511
                    this = state->lencode[last.val +
sl@0
   512
                            (BITS(last.bits + last.op) >> last.bits)];
sl@0
   513
                    if ((unsigned)(last.bits + this.bits) <= bits) break;
sl@0
   514
                    PULLBYTE();
sl@0
   515
                }
sl@0
   516
                DROPBITS(last.bits);
sl@0
   517
            }
sl@0
   518
            DROPBITS(this.bits);
sl@0
   519
            state->length = (unsigned)this.val;
sl@0
   520
sl@0
   521
            /* process literal */
sl@0
   522
            if (this.op == 0) {
sl@0
   523
                Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
sl@0
   524
                        "inflate:         literal '%c'\n" :
sl@0
   525
                        "inflate:         literal 0x%02x\n", this.val));
sl@0
   526
                ROOM();
sl@0
   527
                *put++ = (unsigned char)(state->length);
sl@0
   528
                left--;
sl@0
   529
                state->mode = LEN;
sl@0
   530
                break;
sl@0
   531
            }
sl@0
   532
sl@0
   533
            /* process end of block */
sl@0
   534
            if (this.op & 32) {
sl@0
   535
                Tracevv((stderr, "inflate:         end of block\n"));
sl@0
   536
                state->mode = TYPE;
sl@0
   537
                break;
sl@0
   538
            }
sl@0
   539
sl@0
   540
            /* invalid code */
sl@0
   541
            if (this.op & 64) {
sl@0
   542
                strm->msg = (char *)"invalid literal/length code";
sl@0
   543
                state->mode = BAD;
sl@0
   544
                break;
sl@0
   545
            }
sl@0
   546
sl@0
   547
            /* length code -- get extra bits, if any */
sl@0
   548
            state->extra = (unsigned)(this.op) & 15;
sl@0
   549
            if (state->extra != 0) {
sl@0
   550
                NEEDBITS(state->extra);
sl@0
   551
                state->length += BITS(state->extra);
sl@0
   552
                DROPBITS(state->extra);
sl@0
   553
            }
sl@0
   554
            Tracevv((stderr, "inflate:         length %u\n", state->length));
sl@0
   555
sl@0
   556
            /* get distance code */
sl@0
   557
            for (;;) {
sl@0
   558
                this = state->distcode[BITS(state->distbits)];
sl@0
   559
                if ((unsigned)(this.bits) <= bits) break;
sl@0
   560
                PULLBYTE();
sl@0
   561
            }
sl@0
   562
            if ((this.op & 0xf0) == 0) {
sl@0
   563
                last = this;
sl@0
   564
                for (;;) {
sl@0
   565
                    this = state->distcode[last.val +
sl@0
   566
                            (BITS(last.bits + last.op) >> last.bits)];
sl@0
   567
                    if ((unsigned)(last.bits + this.bits) <= bits) break;
sl@0
   568
                    PULLBYTE();
sl@0
   569
                }
sl@0
   570
                DROPBITS(last.bits);
sl@0
   571
            }
sl@0
   572
            DROPBITS(this.bits);
sl@0
   573
            if (this.op & 64) {
sl@0
   574
                strm->msg = (char *)"invalid distance code";
sl@0
   575
                state->mode = BAD;
sl@0
   576
                break;
sl@0
   577
            }
sl@0
   578
            state->offset = (unsigned)this.val;
sl@0
   579
sl@0
   580
            /* get distance extra bits, if any */
sl@0
   581
            state->extra = (unsigned)(this.op) & 15;
sl@0
   582
            if (state->extra != 0) {
sl@0
   583
                NEEDBITS(state->extra);
sl@0
   584
                state->offset += BITS(state->extra);
sl@0
   585
                DROPBITS(state->extra);
sl@0
   586
            }
sl@0
   587
            if (state->offset > state->wsize - (state->whave < state->wsize ?
sl@0
   588
                                                left : 0)) {
sl@0
   589
                strm->msg = (char *)"invalid distance too far back";
sl@0
   590
                state->mode = BAD;
sl@0
   591
                break;
sl@0
   592
            }
sl@0
   593
            Tracevv((stderr, "inflate:         distance %u\n", state->offset));
sl@0
   594
sl@0
   595
            /* copy match from window to output */
sl@0
   596
            do {
sl@0
   597
                ROOM();
sl@0
   598
                copy = state->wsize - state->offset;
sl@0
   599
                if (copy < left) {
sl@0
   600
                    from = put + copy;
sl@0
   601
                    copy = left - copy;
sl@0
   602
                }
sl@0
   603
                else {
sl@0
   604
                    from = put - state->offset;
sl@0
   605
                    copy = left;
sl@0
   606
                }
sl@0
   607
                if (copy > state->length) copy = state->length;
sl@0
   608
                state->length -= copy;
sl@0
   609
                left -= copy;
sl@0
   610
                do {
sl@0
   611
                    *put++ = *from++;
sl@0
   612
                } while (--copy);
sl@0
   613
            } while (state->length != 0);
sl@0
   614
            break;
sl@0
   615
sl@0
   616
        case DONE:
sl@0
   617
            /* inflate stream terminated properly -- write leftover output */
sl@0
   618
            ret = Z_STREAM_END;
sl@0
   619
            if (left < state->wsize) {
sl@0
   620
                if (out(out_desc, state->window, state->wsize - left))
sl@0
   621
                    ret = Z_BUF_ERROR;
sl@0
   622
            }
sl@0
   623
            goto inf_leave;
sl@0
   624
sl@0
   625
        case BAD:
sl@0
   626
            ret = Z_DATA_ERROR;
sl@0
   627
            goto inf_leave;
sl@0
   628
sl@0
   629
        default:                /* can't happen, but makes compilers happy */
sl@0
   630
            ret = Z_STREAM_ERROR;
sl@0
   631
            goto inf_leave;
sl@0
   632
        }
sl@0
   633
sl@0
   634
    /* Return unused input */
sl@0
   635
  inf_leave:
sl@0
   636
    strm->next_in = next;
sl@0
   637
    strm->avail_in = have;
sl@0
   638
    return ret;
sl@0
   639
}
sl@0
   640
sl@0
   641
sl@0
   642
#ifdef __SYMBIAN32__
sl@0
   643
EXPORT_C int  inflateBackEnd_r(z_streamp strm)
sl@0
   644
#else
sl@0
   645
int ZEXPORT inflateBackEnd(strm)
sl@0
   646
z_streamp strm;
sl@0
   647
 #endif //__SYMBIAN32__
sl@0
   648
{
sl@0
   649
    if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
sl@0
   650
        return Z_STREAM_ERROR;
sl@0
   651
    ZFREE(strm, strm->state);
sl@0
   652
    strm->state = Z_NULL;
sl@0
   653
    Tracev((stderr, "inflate: end\n"));
sl@0
   654
    return Z_OK;
sl@0
   655
}