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