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