misc/libphysfs/physfs_miniz.h
author Wuzzy <Wuzzy2@mail.ru>
Sat, 21 Oct 2017 02:28:36 +0200
changeset 12726 54300512e742
parent 12213 bb5522e88ab2
permissions -rw-r--r--
Fix small German typo
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     1
/* tinfl.c v1.11 - public domain inflate with zlib header parsing/adler32 checking (inflate-only subset of miniz.c)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     2
   See "unlicense" statement at the end of this file.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     3
   Rich Geldreich <richgel99@gmail.com>, last updated May 20, 2011
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
     4
   Implements RFC 1950: https://www.ietf.org/rfc/rfc1950.txt and RFC 1951: https://www.ietf.org/rfc/rfc1951.txt
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     5
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     6
   The entire decompressor coroutine is implemented in tinfl_decompress(). The other functions are optional high-level helpers.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     7
*/
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     8
#ifndef TINFL_HEADER_INCLUDED
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     9
#define TINFL_HEADER_INCLUDED
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    10
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    11
typedef PHYSFS_uint8 mz_uint8;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    12
typedef PHYSFS_sint16 mz_int16;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    13
typedef PHYSFS_uint16 mz_uint16;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    14
typedef PHYSFS_uint32 mz_uint32;
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
    15
typedef unsigned int mz_uint; 
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    16
typedef PHYSFS_uint64 mz_uint64;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    17
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    18
/* For more compatibility with zlib, miniz.c uses unsigned long for some parameters/struct members. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    19
typedef unsigned long mz_ulong;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    20
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    21
/* Heap allocation callbacks. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    22
typedef void *(*mz_alloc_func)(void *opaque, unsigned int items, unsigned int size);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    23
typedef void (*mz_free_func)(void *opaque, void *address);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    24
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    25
#if defined(_M_IX86) || defined(_M_X64)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    26
/* Set MINIZ_USE_UNALIGNED_LOADS_AND_STORES to 1 if integer loads and stores to unaligned addresses are acceptable on the target platform (slightly faster). */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    27
#define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 1
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    28
/* Set MINIZ_LITTLE_ENDIAN to 1 if the processor is little endian. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    29
#define MINIZ_LITTLE_ENDIAN 1
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    30
#endif
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    31
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    32
#if defined(_WIN64) || defined(__MINGW64__) || defined(_LP64) || defined(__LP64__)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    33
/* Set MINIZ_HAS_64BIT_REGISTERS to 1 if the processor has 64-bit general purpose registers (enables 64-bit bitbuffer in inflator) */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    34
#define MINIZ_HAS_64BIT_REGISTERS 1
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    35
#endif
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    36
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    37
/* Works around MSVC's spammy "warning C4127: conditional expression is constant" message. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    38
#ifdef _MSC_VER
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    39
#define MZ_MACRO_END while (0, 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    40
#else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    41
#define MZ_MACRO_END while (0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    42
#endif
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    43
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    44
/* Decompression flags. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    45
enum
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    46
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    47
  TINFL_FLAG_PARSE_ZLIB_HEADER = 1,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    48
  TINFL_FLAG_HAS_MORE_INPUT = 2,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    49
  TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF = 4,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    50
  TINFL_FLAG_COMPUTE_ADLER32 = 8
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    51
};
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    52
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    53
struct tinfl_decompressor_tag; typedef struct tinfl_decompressor_tag tinfl_decompressor;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    54
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    55
/* Max size of LZ dictionary. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    56
#define TINFL_LZ_DICT_SIZE 32768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    57
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    58
/* Return status. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    59
typedef enum
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    60
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    61
  TINFL_STATUS_BAD_PARAM = -3,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    62
  TINFL_STATUS_ADLER32_MISMATCH = -2,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    63
  TINFL_STATUS_FAILED = -1,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    64
  TINFL_STATUS_DONE = 0,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    65
  TINFL_STATUS_NEEDS_MORE_INPUT = 1,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    66
  TINFL_STATUS_HAS_MORE_OUTPUT = 2
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    67
} tinfl_status;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    68
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    69
/* Initializes the decompressor to its initial state. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    70
#define tinfl_init(r) do { (r)->m_state = 0; } MZ_MACRO_END
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    71
#define tinfl_get_adler32(r) (r)->m_check_adler32
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    72
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    73
/* Main low-level decompressor coroutine function. This is the only function actually needed for decompression. All the other functions are just high-level helpers for improved usability. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    74
/* This is a universal API, i.e. it can be used as a building block to build any desired higher level decompression API. In the limit case, it can be called once per every byte input or output. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    75
static tinfl_status tinfl_decompress(tinfl_decompressor *r, const mz_uint8 *pIn_buf_next, size_t *pIn_buf_size, mz_uint8 *pOut_buf_start, mz_uint8 *pOut_buf_next, size_t *pOut_buf_size, const mz_uint32 decomp_flags);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    76
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    77
/* Internal/private bits follow. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    78
enum
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    79
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    80
  TINFL_MAX_HUFF_TABLES = 3, TINFL_MAX_HUFF_SYMBOLS_0 = 288, TINFL_MAX_HUFF_SYMBOLS_1 = 32, TINFL_MAX_HUFF_SYMBOLS_2 = 19,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    81
  TINFL_FAST_LOOKUP_BITS = 10, TINFL_FAST_LOOKUP_SIZE = 1 << TINFL_FAST_LOOKUP_BITS
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    82
};
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    83
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    84
typedef struct
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    85
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    86
  mz_uint8 m_code_size[TINFL_MAX_HUFF_SYMBOLS_0];
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    87
  mz_int16 m_look_up[TINFL_FAST_LOOKUP_SIZE], m_tree[TINFL_MAX_HUFF_SYMBOLS_0 * 2];
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    88
} tinfl_huff_table;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    89
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    90
#if MINIZ_HAS_64BIT_REGISTERS
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    91
  #define TINFL_USE_64BIT_BITBUF 1
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    92
#endif
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    93
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    94
#if TINFL_USE_64BIT_BITBUF
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    95
  typedef mz_uint64 tinfl_bit_buf_t;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    96
  #define TINFL_BITBUF_SIZE (64)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    97
#else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    98
  typedef mz_uint32 tinfl_bit_buf_t;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    99
  #define TINFL_BITBUF_SIZE (32)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   100
#endif
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   101
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   102
struct tinfl_decompressor_tag
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   103
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   104
  mz_uint32 m_state, m_num_bits, m_zhdr0, m_zhdr1, m_z_adler32, m_final, m_type, m_check_adler32, m_dist, m_counter, m_num_extra, m_table_sizes[TINFL_MAX_HUFF_TABLES];
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   105
  tinfl_bit_buf_t m_bit_buf;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   106
  size_t m_dist_from_out_buf_start;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   107
  tinfl_huff_table m_tables[TINFL_MAX_HUFF_TABLES];
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   108
  mz_uint8 m_raw_header[4], m_len_codes[TINFL_MAX_HUFF_SYMBOLS_0 + TINFL_MAX_HUFF_SYMBOLS_1 + 137];
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   109
};
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   110
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   111
#endif /* #ifdef TINFL_HEADER_INCLUDED */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   112
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   113
/* ------------------- End of Header: Implementation follows. (If you only want the header, define MINIZ_HEADER_FILE_ONLY.) */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   114
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   115
#ifndef TINFL_HEADER_FILE_ONLY
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   116
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   117
#define MZ_MAX(a,b) (((a)>(b))?(a):(b))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   118
#define MZ_MIN(a,b) (((a)<(b))?(a):(b))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   119
#define MZ_CLEAR_OBJ(obj) memset(&(obj), 0, sizeof(obj))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   120
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   121
#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   122
  #define MZ_READ_LE16(p) *((const mz_uint16 *)(p))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   123
  #define MZ_READ_LE32(p) *((const mz_uint32 *)(p))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   124
#else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   125
  #define MZ_READ_LE16(p) ((mz_uint32)(((const mz_uint8 *)(p))[0]) | ((mz_uint32)(((const mz_uint8 *)(p))[1]) << 8U))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   126
  #define MZ_READ_LE32(p) ((mz_uint32)(((const mz_uint8 *)(p))[0]) | ((mz_uint32)(((const mz_uint8 *)(p))[1]) << 8U) | ((mz_uint32)(((const mz_uint8 *)(p))[2]) << 16U) | ((mz_uint32)(((const mz_uint8 *)(p))[3]) << 24U))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   127
#endif
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   128
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   129
#define TINFL_MEMCPY(d, s, l) memcpy(d, s, l)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   130
#define TINFL_MEMSET(p, c, l) memset(p, c, l)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   131
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   132
#define TINFL_CR_BEGIN switch(r->m_state) { case 0:
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   133
#define TINFL_CR_RETURN(state_index, result) do { status = result; r->m_state = state_index; goto common_exit; case state_index:; } MZ_MACRO_END
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   134
#define TINFL_CR_RETURN_FOREVER(state_index, result) do { for ( ; ; ) { TINFL_CR_RETURN(state_index, result); } } MZ_MACRO_END
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   135
#define TINFL_CR_FINISH }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   136
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   137
/* TODO: If the caller has indicated that there's no more input, and we attempt to read beyond the input buf, then something is wrong with the input because the inflator never */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   138
/* reads ahead more than it needs to. Currently TINFL_GET_BYTE() pads the end of the stream with 0's in this scenario. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   139
#define TINFL_GET_BYTE(state_index, c) do { \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   140
  if (pIn_buf_cur >= pIn_buf_end) { \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   141
    for ( ; ; ) { \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   142
      if (decomp_flags & TINFL_FLAG_HAS_MORE_INPUT) { \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   143
        TINFL_CR_RETURN(state_index, TINFL_STATUS_NEEDS_MORE_INPUT); \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   144
        if (pIn_buf_cur < pIn_buf_end) { \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   145
          c = *pIn_buf_cur++; \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   146
          break; \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   147
        } \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   148
      } else { \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   149
        c = 0; \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   150
        break; \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   151
      } \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   152
    } \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   153
  } else c = *pIn_buf_cur++; } MZ_MACRO_END
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   154
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   155
#define TINFL_NEED_BITS(state_index, n) do { mz_uint c; TINFL_GET_BYTE(state_index, c); bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); num_bits += 8; } while (num_bits < (mz_uint)(n))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   156
#define TINFL_SKIP_BITS(state_index, n) do { if (num_bits < (mz_uint)(n)) { TINFL_NEED_BITS(state_index, n); } bit_buf >>= (n); num_bits -= (n); } MZ_MACRO_END
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   157
#define TINFL_GET_BITS(state_index, b, n) do { if (num_bits < (mz_uint)(n)) { TINFL_NEED_BITS(state_index, n); } b = bit_buf & ((1 << (n)) - 1); bit_buf >>= (n); num_bits -= (n); } MZ_MACRO_END
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   158
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   159
/* TINFL_HUFF_BITBUF_FILL() is only used rarely, when the number of bytes remaining in the input buffer falls below 2. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   160
/* It reads just enough bytes from the input stream that are needed to decode the next Huffman code (and absolutely no more). It works by trying to fully decode a */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   161
/* Huffman code by using whatever bits are currently present in the bit buffer. If this fails, it reads another byte, and tries again until it succeeds or until the */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   162
/* bit buffer contains >=15 bits (deflate's max. Huffman code size). */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   163
#define TINFL_HUFF_BITBUF_FILL(state_index, pHuff) \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   164
  do { \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   165
    temp = (pHuff)->m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]; \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   166
    if (temp >= 0) { \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   167
      code_len = temp >> 9; \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   168
      if ((code_len) && (num_bits >= code_len)) \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   169
      break; \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   170
    } else if (num_bits > TINFL_FAST_LOOKUP_BITS) { \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   171
       code_len = TINFL_FAST_LOOKUP_BITS; \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   172
       do { \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   173
          temp = (pHuff)->m_tree[~temp + ((bit_buf >> code_len++) & 1)]; \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   174
       } while ((temp < 0) && (num_bits >= (code_len + 1))); if (temp >= 0) break; \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   175
    } TINFL_GET_BYTE(state_index, c); bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); num_bits += 8; \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   176
  } while (num_bits < 15);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   177
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   178
/* TINFL_HUFF_DECODE() decodes the next Huffman coded symbol. It's more complex than you would initially expect because the zlib API expects the decompressor to never read */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   179
/* beyond the final byte of the deflate stream. (In other words, when this macro wants to read another byte from the input, it REALLY needs another byte in order to fully */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   180
/* decode the next Huffman code.) Handling this properly is particularly important on raw deflate (non-zlib) streams, which aren't followed by a byte aligned adler-32. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   181
/* The slow path is only executed at the very end of the input buffer. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   182
#define TINFL_HUFF_DECODE(state_index, sym, pHuff) do { \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   183
  int temp; mz_uint code_len, c; \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   184
  if (num_bits < 15) { \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   185
    if ((pIn_buf_end - pIn_buf_cur) < 2) { \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   186
       TINFL_HUFF_BITBUF_FILL(state_index, pHuff); \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   187
    } else { \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   188
       bit_buf |= (((tinfl_bit_buf_t)pIn_buf_cur[0]) << num_bits) | (((tinfl_bit_buf_t)pIn_buf_cur[1]) << (num_bits + 8)); pIn_buf_cur += 2; num_bits += 16; \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   189
    } \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   190
  } \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   191
  if ((temp = (pHuff)->m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0) \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   192
    code_len = temp >> 9, temp &= 511; \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   193
  else { \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   194
    code_len = TINFL_FAST_LOOKUP_BITS; do { temp = (pHuff)->m_tree[~temp + ((bit_buf >> code_len++) & 1)]; } while (temp < 0); \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   195
  } sym = temp; bit_buf >>= code_len; num_bits -= code_len; } MZ_MACRO_END
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   196
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   197
static tinfl_status tinfl_decompress(tinfl_decompressor *r, const mz_uint8 *pIn_buf_next, size_t *pIn_buf_size, mz_uint8 *pOut_buf_start, mz_uint8 *pOut_buf_next, size_t *pOut_buf_size, const mz_uint32 decomp_flags)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   198
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   199
  static const int s_length_base[31] = { 3,4,5,6,7,8,9,10,11,13, 15,17,19,23,27,31,35,43,51,59, 67,83,99,115,131,163,195,227,258,0,0 };
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   200
  static const int s_length_extra[31]= { 0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0,0,0 };
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   201
  static const int s_dist_base[32] = { 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193, 257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577,0,0};
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   202
  static const int s_dist_extra[32] = { 0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13};
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   203
  static const mz_uint8 s_length_dezigzag[19] = { 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15 };
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   204
  static const int s_min_table_sizes[3] = { 257, 1, 4 };
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   205
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   206
  tinfl_status status = TINFL_STATUS_FAILED; mz_uint32 num_bits, dist, counter, num_extra; tinfl_bit_buf_t bit_buf;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   207
  const mz_uint8 *pIn_buf_cur = pIn_buf_next, *const pIn_buf_end = pIn_buf_next + *pIn_buf_size;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   208
  mz_uint8 *pOut_buf_cur = pOut_buf_next, *const pOut_buf_end = pOut_buf_next + *pOut_buf_size;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   209
  size_t out_buf_size_mask = (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF) ? (size_t)-1 : ((pOut_buf_next - pOut_buf_start) + *pOut_buf_size) - 1, dist_from_out_buf_start;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   210
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   211
  /* Ensure the output buffer's size is a power of 2, unless the output buffer is large enough to hold the entire output file (in which case it doesn't matter). */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   212
  if (((out_buf_size_mask + 1) & out_buf_size_mask) || (pOut_buf_next < pOut_buf_start)) { *pIn_buf_size = *pOut_buf_size = 0; return TINFL_STATUS_BAD_PARAM; }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   213
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   214
  num_bits = r->m_num_bits; bit_buf = r->m_bit_buf; dist = r->m_dist; counter = r->m_counter; num_extra = r->m_num_extra; dist_from_out_buf_start = r->m_dist_from_out_buf_start;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   215
  TINFL_CR_BEGIN
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   216
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   217
  bit_buf = num_bits = dist = counter = num_extra = r->m_zhdr0 = r->m_zhdr1 = 0; r->m_z_adler32 = r->m_check_adler32 = 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   218
  if (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   219
  {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   220
    TINFL_GET_BYTE(1, r->m_zhdr0); TINFL_GET_BYTE(2, r->m_zhdr1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   221
    counter = (((r->m_zhdr0 * 256 + r->m_zhdr1) % 31 != 0) || (r->m_zhdr1 & 32) || ((r->m_zhdr0 & 15) != 8));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   222
    if (!(decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF)) counter |= (((1U << (8U + (r->m_zhdr0 >> 4))) > 32768U) || ((out_buf_size_mask + 1) < (size_t)(1U << (8U + (r->m_zhdr0 >> 4)))));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   223
    if (counter) { TINFL_CR_RETURN_FOREVER(36, TINFL_STATUS_FAILED); }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   224
  }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   225
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   226
  do
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   227
  {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   228
    TINFL_GET_BITS(3, r->m_final, 3); r->m_type = r->m_final >> 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   229
    if (r->m_type == 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   230
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   231
      TINFL_SKIP_BITS(5, num_bits & 7);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   232
      for (counter = 0; counter < 4; ++counter) { if (num_bits) TINFL_GET_BITS(6, r->m_raw_header[counter], 8); else TINFL_GET_BYTE(7, r->m_raw_header[counter]); }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   233
      if ((counter = (r->m_raw_header[0] | (r->m_raw_header[1] << 8))) != (mz_uint)(0xFFFF ^ (r->m_raw_header[2] | (r->m_raw_header[3] << 8)))) { TINFL_CR_RETURN_FOREVER(39, TINFL_STATUS_FAILED); }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   234
      while ((counter) && (num_bits))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   235
      {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   236
        TINFL_GET_BITS(51, dist, 8);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   237
        while (pOut_buf_cur >= pOut_buf_end) { TINFL_CR_RETURN(52, TINFL_STATUS_HAS_MORE_OUTPUT); }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   238
        *pOut_buf_cur++ = (mz_uint8)dist;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   239
        counter--;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   240
      }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   241
      while (counter)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   242
      {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   243
        size_t n; while (pOut_buf_cur >= pOut_buf_end) { TINFL_CR_RETURN(9, TINFL_STATUS_HAS_MORE_OUTPUT); }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   244
        while (pIn_buf_cur >= pIn_buf_end)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   245
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   246
          if (decomp_flags & TINFL_FLAG_HAS_MORE_INPUT)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   247
          {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   248
            TINFL_CR_RETURN(38, TINFL_STATUS_NEEDS_MORE_INPUT);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   249
          }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   250
          else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   251
          {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   252
            TINFL_CR_RETURN_FOREVER(40, TINFL_STATUS_FAILED);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   253
          }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   254
        }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   255
        n = MZ_MIN(MZ_MIN((size_t)(pOut_buf_end - pOut_buf_cur), (size_t)(pIn_buf_end - pIn_buf_cur)), counter);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   256
        TINFL_MEMCPY(pOut_buf_cur, pIn_buf_cur, n); pIn_buf_cur += n; pOut_buf_cur += n; counter -= (mz_uint)n;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   257
      }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   258
    }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   259
    else if (r->m_type == 3)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   260
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   261
      TINFL_CR_RETURN_FOREVER(10, TINFL_STATUS_FAILED);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   262
    }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   263
    else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   264
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   265
      if (r->m_type == 1)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   266
      {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   267
        mz_uint8 *p = r->m_tables[0].m_code_size; mz_uint i;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   268
        r->m_table_sizes[0] = 288; r->m_table_sizes[1] = 32; TINFL_MEMSET(r->m_tables[1].m_code_size, 5, 32);
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   269
        for ( i = 0; i <= 143; ++i) *p++ = 8;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   270
        for ( ; i <= 255; ++i) *p++ = 9;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   271
        for ( ; i <= 279; ++i) *p++ = 7;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   272
        for ( ; i <= 287; ++i) *p++ = 8;
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   273
      }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   274
      else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   275
      {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   276
        for (counter = 0; counter < 3; counter++) { TINFL_GET_BITS(11, r->m_table_sizes[counter], "\05\05\04"[counter]); r->m_table_sizes[counter] += s_min_table_sizes[counter]; }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   277
        MZ_CLEAR_OBJ(r->m_tables[2].m_code_size); for (counter = 0; counter < r->m_table_sizes[2]; counter++) { mz_uint s; TINFL_GET_BITS(14, s, 3); r->m_tables[2].m_code_size[s_length_dezigzag[counter]] = (mz_uint8)s; }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   278
        r->m_table_sizes[2] = 19;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   279
      }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   280
      for ( ; (int)r->m_type >= 0; r->m_type--)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   281
      {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   282
        int tree_next, tree_cur; tinfl_huff_table *pTable;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   283
        mz_uint i, j, used_syms, total, sym_index, next_code[17], total_syms[16]; pTable = &r->m_tables[r->m_type]; MZ_CLEAR_OBJ(total_syms); MZ_CLEAR_OBJ(pTable->m_look_up); MZ_CLEAR_OBJ(pTable->m_tree);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   284
        for (i = 0; i < r->m_table_sizes[r->m_type]; ++i) total_syms[pTable->m_code_size[i]]++;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   285
        used_syms = 0, total = 0; next_code[0] = next_code[1] = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   286
        for (i = 1; i <= 15; ++i) { used_syms += total_syms[i]; next_code[i + 1] = (total = ((total + total_syms[i]) << 1)); }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   287
        if ((65536 != total) && (used_syms > 1))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   288
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   289
          TINFL_CR_RETURN_FOREVER(35, TINFL_STATUS_FAILED);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   290
        }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   291
        for (tree_next = -1, sym_index = 0; sym_index < r->m_table_sizes[r->m_type]; ++sym_index)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   292
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   293
          mz_uint rev_code = 0, l, cur_code, code_size = pTable->m_code_size[sym_index]; if (!code_size) continue;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   294
          cur_code = next_code[code_size]++; for (l = code_size; l > 0; l--, cur_code >>= 1) rev_code = (rev_code << 1) | (cur_code & 1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   295
          if (code_size <= TINFL_FAST_LOOKUP_BITS) { mz_int16 k = (mz_int16)((code_size << 9) | sym_index); while (rev_code < TINFL_FAST_LOOKUP_SIZE) { pTable->m_look_up[rev_code] = k; rev_code += (1 << code_size); } continue; }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   296
          if (0 == (tree_cur = pTable->m_look_up[rev_code & (TINFL_FAST_LOOKUP_SIZE - 1)])) { pTable->m_look_up[rev_code & (TINFL_FAST_LOOKUP_SIZE - 1)] = (mz_int16)tree_next; tree_cur = tree_next; tree_next -= 2; }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   297
          rev_code >>= (TINFL_FAST_LOOKUP_BITS - 1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   298
          for (j = code_size; j > (TINFL_FAST_LOOKUP_BITS + 1); j--)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   299
          {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   300
            tree_cur -= ((rev_code >>= 1) & 1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   301
            if (!pTable->m_tree[-tree_cur - 1]) { pTable->m_tree[-tree_cur - 1] = (mz_int16)tree_next; tree_cur = tree_next; tree_next -= 2; } else tree_cur = pTable->m_tree[-tree_cur - 1];
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   302
          }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   303
          tree_cur -= ((rev_code >>= 1) & 1); pTable->m_tree[-tree_cur - 1] = (mz_int16)sym_index;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   304
        }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   305
        if (r->m_type == 2)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   306
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   307
          for (counter = 0; counter < (r->m_table_sizes[0] + r->m_table_sizes[1]); )
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   308
          {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   309
            mz_uint s; TINFL_HUFF_DECODE(16, dist, &r->m_tables[2]); if (dist < 16) { r->m_len_codes[counter++] = (mz_uint8)dist; continue; }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   310
            if ((dist == 16) && (!counter))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   311
            {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   312
              TINFL_CR_RETURN_FOREVER(17, TINFL_STATUS_FAILED);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   313
            }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   314
            num_extra = "\02\03\07"[dist - 16]; TINFL_GET_BITS(18, s, num_extra); s += "\03\03\013"[dist - 16];
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   315
            TINFL_MEMSET(r->m_len_codes + counter, (dist == 16) ? r->m_len_codes[counter - 1] : 0, s); counter += s;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   316
          }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   317
          if ((r->m_table_sizes[0] + r->m_table_sizes[1]) != counter)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   318
          {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   319
            TINFL_CR_RETURN_FOREVER(21, TINFL_STATUS_FAILED);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   320
          }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   321
          TINFL_MEMCPY(r->m_tables[0].m_code_size, r->m_len_codes, r->m_table_sizes[0]); TINFL_MEMCPY(r->m_tables[1].m_code_size, r->m_len_codes + r->m_table_sizes[0], r->m_table_sizes[1]);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   322
        }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   323
      }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   324
      for ( ; ; )
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   325
      {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   326
        mz_uint8 *pSrc;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   327
        for ( ; ; )
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   328
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   329
          if (((pIn_buf_end - pIn_buf_cur) < 4) || ((pOut_buf_end - pOut_buf_cur) < 2))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   330
          {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   331
            TINFL_HUFF_DECODE(23, counter, &r->m_tables[0]);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   332
            if (counter >= 256)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   333
              break;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   334
            while (pOut_buf_cur >= pOut_buf_end) { TINFL_CR_RETURN(24, TINFL_STATUS_HAS_MORE_OUTPUT); }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   335
            *pOut_buf_cur++ = (mz_uint8)counter;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   336
          }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   337
          else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   338
          {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   339
            int sym2; mz_uint code_len;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   340
#if TINFL_USE_64BIT_BITBUF
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   341
            if (num_bits < 30) { bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE32(pIn_buf_cur)) << num_bits); pIn_buf_cur += 4; num_bits += 32; }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   342
#else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   343
            if (num_bits < 15) { bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE16(pIn_buf_cur)) << num_bits); pIn_buf_cur += 2; num_bits += 16; }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   344
#endif
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   345
            if ((sym2 = r->m_tables[0].m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   346
              code_len = sym2 >> 9;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   347
            else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   348
            {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   349
              code_len = TINFL_FAST_LOOKUP_BITS; do { sym2 = r->m_tables[0].m_tree[~sym2 + ((bit_buf >> code_len++) & 1)]; } while (sym2 < 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   350
            }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   351
            counter = sym2; bit_buf >>= code_len; num_bits -= code_len;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   352
            if (counter & 256)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   353
              break;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   354
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   355
#if !TINFL_USE_64BIT_BITBUF
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   356
            if (num_bits < 15) { bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE16(pIn_buf_cur)) << num_bits); pIn_buf_cur += 2; num_bits += 16; }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   357
#endif
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   358
            if ((sym2 = r->m_tables[0].m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   359
              code_len = sym2 >> 9;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   360
            else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   361
            {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   362
              code_len = TINFL_FAST_LOOKUP_BITS; do { sym2 = r->m_tables[0].m_tree[~sym2 + ((bit_buf >> code_len++) & 1)]; } while (sym2 < 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   363
            }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   364
            bit_buf >>= code_len; num_bits -= code_len;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   365
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   366
            pOut_buf_cur[0] = (mz_uint8)counter;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   367
            if (sym2 & 256)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   368
            {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   369
              pOut_buf_cur++;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   370
              counter = sym2;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   371
              break;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   372
            }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   373
            pOut_buf_cur[1] = (mz_uint8)sym2;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   374
            pOut_buf_cur += 2;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   375
          }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   376
        }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   377
        if ((counter &= 511) == 256) break;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   378
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   379
        num_extra = s_length_extra[counter - 257]; counter = s_length_base[counter - 257];
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   380
        if (num_extra) { mz_uint extra_bits; TINFL_GET_BITS(25, extra_bits, num_extra); counter += extra_bits; }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   381
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   382
        TINFL_HUFF_DECODE(26, dist, &r->m_tables[1]);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   383
        num_extra = s_dist_extra[dist]; dist = s_dist_base[dist];
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   384
        if (num_extra) { mz_uint extra_bits; TINFL_GET_BITS(27, extra_bits, num_extra); dist += extra_bits; }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   385
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   386
        dist_from_out_buf_start = pOut_buf_cur - pOut_buf_start;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   387
        if ((dist > dist_from_out_buf_start) && (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   388
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   389
          TINFL_CR_RETURN_FOREVER(37, TINFL_STATUS_FAILED);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   390
        }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   391
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   392
        pSrc = pOut_buf_start + ((dist_from_out_buf_start - dist) & out_buf_size_mask);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   393
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   394
        if ((MZ_MAX(pOut_buf_cur, pSrc) + counter) > pOut_buf_end)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   395
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   396
          while (counter--)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   397
          {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   398
            while (pOut_buf_cur >= pOut_buf_end) { TINFL_CR_RETURN(53, TINFL_STATUS_HAS_MORE_OUTPUT); }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   399
            *pOut_buf_cur++ = pOut_buf_start[(dist_from_out_buf_start++ - dist) & out_buf_size_mask];
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   400
          }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   401
          continue;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   402
        }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   403
#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   404
        else if ((counter >= 9) && (counter <= dist))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   405
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   406
          const mz_uint8 *pSrc_end = pSrc + (counter & ~7);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   407
          do
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   408
          {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   409
            ((mz_uint32 *)pOut_buf_cur)[0] = ((const mz_uint32 *)pSrc)[0];
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   410
            ((mz_uint32 *)pOut_buf_cur)[1] = ((const mz_uint32 *)pSrc)[1];
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   411
            pOut_buf_cur += 8;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   412
          } while ((pSrc += 8) < pSrc_end);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   413
          if ((counter &= 7) < 3)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   414
          {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   415
            if (counter)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   416
            {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   417
              pOut_buf_cur[0] = pSrc[0];
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   418
              if (counter > 1)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   419
                pOut_buf_cur[1] = pSrc[1];
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   420
              pOut_buf_cur += counter;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   421
            }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   422
            continue;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   423
          }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   424
        }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   425
#endif
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   426
        do
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   427
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   428
          pOut_buf_cur[0] = pSrc[0];
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   429
          pOut_buf_cur[1] = pSrc[1];
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   430
          pOut_buf_cur[2] = pSrc[2];
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   431
          pOut_buf_cur += 3; pSrc += 3;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   432
        } while ((int)(counter -= 3) > 2);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   433
        if ((int)counter > 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   434
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   435
          pOut_buf_cur[0] = pSrc[0];
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   436
          if ((int)counter > 1)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   437
            pOut_buf_cur[1] = pSrc[1];
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   438
          pOut_buf_cur += counter;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   439
        }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   440
      }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   441
    }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   442
  } while (!(r->m_final & 1));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   443
  if (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   444
  {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   445
    TINFL_SKIP_BITS(32, num_bits & 7); for (counter = 0; counter < 4; ++counter) { mz_uint s; if (num_bits) TINFL_GET_BITS(41, s, 8); else TINFL_GET_BYTE(42, s); r->m_z_adler32 = (r->m_z_adler32 << 8) | s; }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   446
  }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   447
  TINFL_CR_RETURN_FOREVER(34, TINFL_STATUS_DONE);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   448
  TINFL_CR_FINISH
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   449
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   450
common_exit:
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   451
  r->m_num_bits = num_bits; r->m_bit_buf = bit_buf; r->m_dist = dist; r->m_counter = counter; r->m_num_extra = num_extra; r->m_dist_from_out_buf_start = dist_from_out_buf_start;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   452
  *pIn_buf_size = pIn_buf_cur - pIn_buf_next; *pOut_buf_size = pOut_buf_cur - pOut_buf_next;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   453
  if ((decomp_flags & (TINFL_FLAG_PARSE_ZLIB_HEADER | TINFL_FLAG_COMPUTE_ADLER32)) && (status >= 0))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   454
  {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   455
    const mz_uint8 *ptr = pOut_buf_next; size_t buf_len = *pOut_buf_size;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   456
    mz_uint32 i, s1 = r->m_check_adler32 & 0xffff, s2 = r->m_check_adler32 >> 16; size_t block_len = buf_len % 5552;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   457
    while (buf_len)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   458
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   459
      for (i = 0; i + 7 < block_len; i += 8, ptr += 8)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   460
      {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   461
        s1 += ptr[0], s2 += s1; s1 += ptr[1], s2 += s1; s1 += ptr[2], s2 += s1; s1 += ptr[3], s2 += s1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   462
        s1 += ptr[4], s2 += s1; s1 += ptr[5], s2 += s1; s1 += ptr[6], s2 += s1; s1 += ptr[7], s2 += s1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   463
      }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   464
      for ( ; i < block_len; ++i) s1 += *ptr++, s2 += s1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   465
      s1 %= 65521U, s2 %= 65521U; buf_len -= block_len; block_len = 5552;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   466
    }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   467
    r->m_check_adler32 = (s2 << 16) + s1; if ((status == TINFL_STATUS_DONE) && (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER) && (r->m_check_adler32 != r->m_z_adler32)) status = TINFL_STATUS_ADLER32_MISMATCH;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   468
  }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   469
  return status;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   470
}
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   471
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   472
/* Flush values. For typical usage you only need MZ_NO_FLUSH and MZ_FINISH. The other stuff is for advanced use. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   473
enum { MZ_NO_FLUSH = 0, MZ_PARTIAL_FLUSH = 1, MZ_SYNC_FLUSH = 2, MZ_FULL_FLUSH = 3, MZ_FINISH = 4, MZ_BLOCK = 5 };
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   474
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   475
/* Return status codes. MZ_PARAM_ERROR is non-standard. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   476
enum { MZ_OK = 0, MZ_STREAM_END = 1, MZ_NEED_DICT = 2, MZ_ERRNO = -1, MZ_STREAM_ERROR = -2, MZ_DATA_ERROR = -3, MZ_MEM_ERROR = -4, MZ_BUF_ERROR = -5, MZ_VERSION_ERROR = -6, MZ_PARAM_ERROR = -10000 };
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   477
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   478
/* Compression levels. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   479
enum { MZ_NO_COMPRESSION = 0, MZ_BEST_SPEED = 1, MZ_BEST_COMPRESSION = 9, MZ_DEFAULT_COMPRESSION = -1 };
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   480
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   481
/* Window bits */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   482
#define MZ_DEFAULT_WINDOW_BITS 15
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   483
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   484
struct mz_internal_state;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   485
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   486
/* Compression/decompression stream struct. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   487
typedef struct mz_stream_s
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   488
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   489
  const unsigned char *next_in;     /* pointer to next byte to read */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   490
  unsigned int avail_in;            /* number of bytes available at next_in */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   491
  mz_ulong total_in;                /* total number of bytes consumed so far */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   492
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   493
  unsigned char *next_out;          /* pointer to next byte to write */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   494
  unsigned int avail_out;           /* number of bytes that can be written to next_out */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   495
  mz_ulong total_out;               /* total number of bytes produced so far */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   496
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   497
  char *msg;                        /* error msg (unused) */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   498
  struct mz_internal_state *state;  /* internal state, allocated by zalloc/zfree */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   499
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   500
  mz_alloc_func zalloc;             /* optional heap allocation function (defaults to malloc) */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   501
  mz_free_func zfree;               /* optional heap free function (defaults to free) */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   502
  void *opaque;                     /* heap alloc function user pointer */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   503
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   504
  int data_type;                    /* data_type (unused) */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   505
  mz_ulong adler;                   /* adler32 of the source or uncompressed data */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   506
  mz_ulong reserved;                /* not used */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   507
} mz_stream;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   508
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   509
typedef mz_stream *mz_streamp;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   510
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   511
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   512
typedef struct
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   513
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   514
  tinfl_decompressor m_decomp;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   515
  mz_uint m_dict_ofs, m_dict_avail, m_first_call, m_has_flushed; int m_window_bits;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   516
  mz_uint8 m_dict[TINFL_LZ_DICT_SIZE];
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   517
  tinfl_status m_last_status;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   518
} inflate_state;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   519
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   520
static int mz_inflateInit2(mz_streamp pStream, int window_bits)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   521
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   522
  inflate_state *pDecomp;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   523
  if (!pStream) return MZ_STREAM_ERROR;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   524
  if ((window_bits != MZ_DEFAULT_WINDOW_BITS) && (-window_bits != MZ_DEFAULT_WINDOW_BITS)) return MZ_PARAM_ERROR;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   525
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   526
  pStream->data_type = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   527
  pStream->adler = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   528
  pStream->msg = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   529
  pStream->total_in = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   530
  pStream->total_out = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   531
  pStream->reserved = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   532
  /* if (!pStream->zalloc) pStream->zalloc = def_alloc_func; */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   533
  /* if (!pStream->zfree) pStream->zfree = def_free_func; */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   534
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   535
  pDecomp = (inflate_state*)pStream->zalloc(pStream->opaque, 1, sizeof(inflate_state));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   536
  if (!pDecomp) return MZ_MEM_ERROR;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   537
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   538
  pStream->state = (struct mz_internal_state *)pDecomp;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   539
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   540
  tinfl_init(&pDecomp->m_decomp);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   541
  pDecomp->m_dict_ofs = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   542
  pDecomp->m_dict_avail = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   543
  pDecomp->m_last_status = TINFL_STATUS_NEEDS_MORE_INPUT;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   544
  pDecomp->m_first_call = 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   545
  pDecomp->m_has_flushed = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   546
  pDecomp->m_window_bits = window_bits;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   547
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   548
  return MZ_OK;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   549
}
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   550
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   551
static int mz_inflate(mz_streamp pStream, int flush)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   552
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   553
  inflate_state* pState;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   554
  mz_uint n, first_call, decomp_flags = TINFL_FLAG_COMPUTE_ADLER32;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   555
  size_t in_bytes, out_bytes, orig_avail_in;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   556
  tinfl_status status;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   557
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   558
  if ((!pStream) || (!pStream->state)) return MZ_STREAM_ERROR;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   559
  if (flush == MZ_PARTIAL_FLUSH) flush = MZ_SYNC_FLUSH;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   560
  if ((flush) && (flush != MZ_SYNC_FLUSH) && (flush != MZ_FINISH)) return MZ_STREAM_ERROR;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   561
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   562
  pState = (inflate_state*)pStream->state;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   563
  if (pState->m_window_bits > 0) decomp_flags |= TINFL_FLAG_PARSE_ZLIB_HEADER;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   564
  orig_avail_in = pStream->avail_in;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   565
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   566
  first_call = pState->m_first_call; pState->m_first_call = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   567
  if (pState->m_last_status < 0) return MZ_DATA_ERROR;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   568
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   569
  if (pState->m_has_flushed && (flush != MZ_FINISH)) return MZ_STREAM_ERROR;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   570
  pState->m_has_flushed |= (flush == MZ_FINISH);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   571
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   572
  if ((flush == MZ_FINISH) && (first_call))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   573
  {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   574
    /* MZ_FINISH on the first call implies that the input and output buffers are large enough to hold the entire compressed/decompressed file. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   575
    decomp_flags |= TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   576
    in_bytes = pStream->avail_in; out_bytes = pStream->avail_out;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   577
    status = tinfl_decompress(&pState->m_decomp, pStream->next_in, &in_bytes, pStream->next_out, pStream->next_out, &out_bytes, decomp_flags);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   578
    pState->m_last_status = status;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   579
    pStream->next_in += (mz_uint)in_bytes; pStream->avail_in -= (mz_uint)in_bytes; pStream->total_in += (mz_uint)in_bytes;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   580
    pStream->adler = tinfl_get_adler32(&pState->m_decomp);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   581
    pStream->next_out += (mz_uint)out_bytes; pStream->avail_out -= (mz_uint)out_bytes; pStream->total_out += (mz_uint)out_bytes;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   582
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   583
    if (status < 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   584
      return MZ_DATA_ERROR;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   585
    else if (status != TINFL_STATUS_DONE)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   586
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   587
      pState->m_last_status = TINFL_STATUS_FAILED;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   588
      return MZ_BUF_ERROR;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   589
    }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   590
    return MZ_STREAM_END;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   591
  }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   592
  /* flush != MZ_FINISH then we must assume there's more input. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   593
  if (flush != MZ_FINISH) decomp_flags |= TINFL_FLAG_HAS_MORE_INPUT;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   594
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   595
  if (pState->m_dict_avail)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   596
  {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   597
    n = MZ_MIN(pState->m_dict_avail, pStream->avail_out);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   598
    memcpy(pStream->next_out, pState->m_dict + pState->m_dict_ofs, n);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   599
    pStream->next_out += n; pStream->avail_out -= n; pStream->total_out += n;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   600
    pState->m_dict_avail -= n; pState->m_dict_ofs = (pState->m_dict_ofs + n) & (TINFL_LZ_DICT_SIZE - 1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   601
    return ((pState->m_last_status == TINFL_STATUS_DONE) && (!pState->m_dict_avail)) ? MZ_STREAM_END : MZ_OK;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   602
  }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   603
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   604
  for ( ; ; )
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   605
  {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   606
    in_bytes = pStream->avail_in;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   607
    out_bytes = TINFL_LZ_DICT_SIZE - pState->m_dict_ofs;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   608
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   609
    status = tinfl_decompress(&pState->m_decomp, pStream->next_in, &in_bytes, pState->m_dict, pState->m_dict + pState->m_dict_ofs, &out_bytes, decomp_flags);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   610
    pState->m_last_status = status;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   611
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   612
    pStream->next_in += (mz_uint)in_bytes; pStream->avail_in -= (mz_uint)in_bytes;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   613
    pStream->total_in += (mz_uint)in_bytes; pStream->adler = tinfl_get_adler32(&pState->m_decomp);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   614
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   615
    pState->m_dict_avail = (mz_uint)out_bytes;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   616
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   617
    n = MZ_MIN(pState->m_dict_avail, pStream->avail_out);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   618
    memcpy(pStream->next_out, pState->m_dict + pState->m_dict_ofs, n);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   619
    pStream->next_out += n; pStream->avail_out -= n; pStream->total_out += n;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   620
    pState->m_dict_avail -= n; pState->m_dict_ofs = (pState->m_dict_ofs + n) & (TINFL_LZ_DICT_SIZE - 1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   621
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   622
    if (status < 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   623
       return MZ_DATA_ERROR; /* Stream is corrupted (there could be some uncompressed data left in the output dictionary - oh well). */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   624
    else if ((status == TINFL_STATUS_NEEDS_MORE_INPUT) && (!orig_avail_in))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   625
      return MZ_BUF_ERROR; /* Signal caller that we can't make forward progress without supplying more input or by setting flush to MZ_FINISH. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   626
    else if (flush == MZ_FINISH)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   627
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   628
       /* The output buffer MUST be large to hold the remaining uncompressed data when flush==MZ_FINISH. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   629
       if (status == TINFL_STATUS_DONE)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   630
          return pState->m_dict_avail ? MZ_BUF_ERROR : MZ_STREAM_END;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   631
       /* status here must be TINFL_STATUS_HAS_MORE_OUTPUT, which means there's at least 1 more byte on the way. If there's no more room left in the output buffer then something is wrong. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   632
       else if (!pStream->avail_out)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   633
          return MZ_BUF_ERROR;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   634
    }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   635
    else if ((status == TINFL_STATUS_DONE) || (!pStream->avail_in) || (!pStream->avail_out) || (pState->m_dict_avail))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   636
      break;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   637
  }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   638
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   639
  return ((status == TINFL_STATUS_DONE) && (!pState->m_dict_avail)) ? MZ_STREAM_END : MZ_OK;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   640
}
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   641
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   642
static int mz_inflateEnd(mz_streamp pStream)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   643
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   644
  if (!pStream)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   645
    return MZ_STREAM_ERROR;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   646
  if (pStream->state)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   647
  {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   648
    pStream->zfree(pStream->opaque, pStream->state);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   649
    pStream->state = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   650
  }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   651
  return MZ_OK;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   652
}
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   653
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   654
/* make this a drop-in replacement for zlib... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   655
  #define voidpf void*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   656
  #define uInt unsigned int
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   657
  #define z_stream              mz_stream
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   658
  #define inflateInit2          mz_inflateInit2
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   659
  #define inflate               mz_inflate
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   660
  #define inflateEnd            mz_inflateEnd
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   661
  #define Z_SYNC_FLUSH          MZ_SYNC_FLUSH
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   662
  #define Z_FINISH              MZ_FINISH
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   663
  #define Z_OK                  MZ_OK
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   664
  #define Z_STREAM_END          MZ_STREAM_END
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   665
  #define Z_NEED_DICT           MZ_NEED_DICT
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   666
  #define Z_ERRNO               MZ_ERRNO
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   667
  #define Z_STREAM_ERROR        MZ_STREAM_ERROR
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   668
  #define Z_DATA_ERROR          MZ_DATA_ERROR
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   669
  #define Z_MEM_ERROR           MZ_MEM_ERROR
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   670
  #define Z_BUF_ERROR           MZ_BUF_ERROR
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   671
  #define Z_VERSION_ERROR       MZ_VERSION_ERROR
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   672
  #define MAX_WBITS             15
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   673
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   674
#endif /* #ifndef TINFL_HEADER_FILE_ONLY */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   675
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   676
/* 
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   677
  This is free and unencumbered software released into the public domain.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   678
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   679
  Anyone is free to copy, modify, publish, use, compile, sell, or
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   680
  distribute this software, either in source code form or as a compiled
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   681
  binary, for any purpose, commercial or non-commercial, and by any
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   682
  means.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   683
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   684
  In jurisdictions that recognize copyright laws, the author or authors
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   685
  of this software dedicate any and all copyright interest in the
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   686
  software to the public domain. We make this dedication for the benefit
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   687
  of the public at large and to the detriment of our heirs and
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   688
  successors. We intend this dedication to be an overt act of
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   689
  relinquishment in perpetuity of all present and future rights to this
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   690
  software under copyright law.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   691
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   692
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   693
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   694
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   695
  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   696
  OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   697
  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   698
  OTHER DEALINGS IN THE SOFTWARE.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   699
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   700
  For more information, please refer to <https://unlicense.org/>
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   701
*/