/src/fluent-bit/lib/miniz/miniz.c
Line | Count | Source (jump to first uncovered line) |
1 | | /************************************************************************** |
2 | | * |
3 | | * Copyright 2013-2014 RAD Game Tools and Valve Software |
4 | | * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC |
5 | | * All Rights Reserved. |
6 | | * |
7 | | * Permission is hereby granted, free of charge, to any person obtaining a copy |
8 | | * of this software and associated documentation files (the "Software"), to deal |
9 | | * in the Software without restriction, including without limitation the rights |
10 | | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
11 | | * copies of the Software, and to permit persons to whom the Software is |
12 | | * furnished to do so, subject to the following conditions: |
13 | | * |
14 | | * The above copyright notice and this permission notice shall be included in |
15 | | * all copies or substantial portions of the Software. |
16 | | * |
17 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18 | | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
20 | | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
22 | | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
23 | | * THE SOFTWARE. |
24 | | * |
25 | | **************************************************************************/ |
26 | | |
27 | | #include "miniz.h" |
28 | | |
29 | | typedef unsigned char mz_validate_uint16[sizeof(mz_uint16) == 2 ? 1 : -1]; |
30 | | typedef unsigned char mz_validate_uint32[sizeof(mz_uint32) == 4 ? 1 : -1]; |
31 | | typedef unsigned char mz_validate_uint64[sizeof(mz_uint64) == 8 ? 1 : -1]; |
32 | | |
33 | | #ifdef __cplusplus |
34 | | extern "C" { |
35 | | #endif |
36 | | |
37 | | /* ------------------- zlib-style API's */ |
38 | | |
39 | | mz_ulong mz_adler32(mz_ulong adler, const unsigned char *ptr, size_t buf_len) |
40 | 0 | { |
41 | 0 | mz_uint32 i, s1 = (mz_uint32)(adler & 0xffff), s2 = (mz_uint32)(adler >> 16); |
42 | 0 | size_t block_len = buf_len % 5552; |
43 | 0 | if (!ptr) |
44 | 0 | return MZ_ADLER32_INIT; |
45 | 0 | while (buf_len) |
46 | 0 | { |
47 | 0 | for (i = 0; i + 7 < block_len; i += 8, ptr += 8) |
48 | 0 | { |
49 | 0 | s1 += ptr[0], s2 += s1; |
50 | 0 | s1 += ptr[1], s2 += s1; |
51 | 0 | s1 += ptr[2], s2 += s1; |
52 | 0 | s1 += ptr[3], s2 += s1; |
53 | 0 | s1 += ptr[4], s2 += s1; |
54 | 0 | s1 += ptr[5], s2 += s1; |
55 | 0 | s1 += ptr[6], s2 += s1; |
56 | 0 | s1 += ptr[7], s2 += s1; |
57 | 0 | } |
58 | 0 | for (; i < block_len; ++i) |
59 | 0 | s1 += *ptr++, s2 += s1; |
60 | 0 | s1 %= 65521U, s2 %= 65521U; |
61 | 0 | buf_len -= block_len; |
62 | 0 | block_len = 5552; |
63 | 0 | } |
64 | 0 | return (s2 << 16) + s1; |
65 | 0 | } |
66 | | |
67 | | /* Karl Malbrain's compact CRC-32. See "A compact CCITT crc16 and crc32 C implementation that balances processor cache usage against speed": http://www.geocities.com/malbrain/ */ |
68 | | #if 0 |
69 | | mz_ulong mz_crc32(mz_ulong crc, const mz_uint8 *ptr, size_t buf_len) |
70 | | { |
71 | | static const mz_uint32 s_crc32[16] = { 0, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, |
72 | | 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c }; |
73 | | mz_uint32 crcu32 = (mz_uint32)crc; |
74 | | if (!ptr) |
75 | | return MZ_CRC32_INIT; |
76 | | crcu32 = ~crcu32; |
77 | | while (buf_len--) |
78 | | { |
79 | | mz_uint8 b = *ptr++; |
80 | | crcu32 = (crcu32 >> 4) ^ s_crc32[(crcu32 & 0xF) ^ (b & 0xF)]; |
81 | | crcu32 = (crcu32 >> 4) ^ s_crc32[(crcu32 & 0xF) ^ (b >> 4)]; |
82 | | } |
83 | | return ~crcu32; |
84 | | } |
85 | | #elif defined(USE_EXTERNAL_MZCRC) |
86 | | /* If USE_EXTERNAL_CRC is defined, an external module will export the |
87 | | * mz_crc32() symbol for us to use, e.g. an SSE-accelerated version. |
88 | | * Depending on the impl, it may be necessary to ~ the input/output crc values. |
89 | | */ |
90 | | mz_ulong mz_crc32(mz_ulong crc, const mz_uint8 *ptr, size_t buf_len); |
91 | | #else |
92 | | /* Faster, but larger CPU cache footprint. |
93 | | */ |
94 | | mz_ulong mz_crc32(mz_ulong crc, const mz_uint8 *ptr, size_t buf_len) |
95 | 0 | { |
96 | 0 | static const mz_uint32 s_crc_table[256] = |
97 | 0 | { |
98 | 0 | 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535, |
99 | 0 | 0x9E6495A3, 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, |
100 | 0 | 0xE7B82D07, 0x90BF1D91, 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, |
101 | 0 | 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, |
102 | 0 | 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5, 0x3B6E20C8, 0x4C69105E, 0xD56041E4, |
103 | 0 | 0xA2677172, 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, 0x35B5A8FA, 0x42B2986C, |
104 | 0 | 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59, 0x26D930AC, |
105 | 0 | 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F, |
106 | 0 | 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, 0x2F6F7C87, 0x58684C11, 0xC1611DAB, |
107 | 0 | 0xB6662D3D, 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, |
108 | 0 | 0x9FBFE4A5, 0xE8B8D433, 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, |
109 | 0 | 0x086D3D2D, 0x91646C97, 0xE6635C01, 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, |
110 | 0 | 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, |
111 | 0 | 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65, 0x4DB26158, 0x3AB551CE, |
112 | 0 | 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB, 0x4369E96A, |
113 | 0 | 0x346ED9FC, 0xAD678846, 0xDA60B8D0, 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, |
114 | 0 | 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, |
115 | 0 | 0xCE61E49F, 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81, |
116 | 0 | 0xB7BD5C3B, 0xC0BA6CAD, 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, 0xEAD54739, |
117 | 0 | 0x9DD277AF, 0x04DB2615, 0x73DC1683, 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8, |
118 | 0 | 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, 0xF00F9344, 0x8708A3D2, 0x1E01F268, |
119 | 0 | 0x6906C2FE, 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7, 0xFED41B76, 0x89D32BE0, |
120 | 0 | 0x10DA7A5A, 0x67DD4ACC, 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, 0xD6D6A3E8, |
121 | 0 | 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B, |
122 | 0 | 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, |
123 | 0 | 0x4669BE79, 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, 0xCC0C7795, 0xBB0B4703, |
124 | 0 | 0x220216B9, 0x5505262F, 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, |
125 | 0 | 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D, 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, |
126 | 0 | 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713, 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, |
127 | 0 | 0x0CB61B38, 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 0x86D3D2D4, 0xF1D4E242, |
128 | 0 | 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777, 0x88085AE6, |
129 | 0 | 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45, |
130 | 0 | 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, 0xA7672661, 0xD06016F7, 0x4969474D, |
131 | 0 | 0x3E6E77DB, 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, |
132 | 0 | 0x47B2CF7F, 0x30B5FFE9, 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, |
133 | 0 | 0xCDD70693, 0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, |
134 | 0 | 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D |
135 | 0 | }; |
136 | |
|
137 | 0 | mz_uint32 crc32 = (mz_uint32)crc ^ 0xFFFFFFFF; |
138 | 0 | const mz_uint8 *pByte_buf = (const mz_uint8 *)ptr; |
139 | |
|
140 | 0 | while (buf_len >= 4) |
141 | 0 | { |
142 | 0 | crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[0]) & 0xFF]; |
143 | 0 | crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[1]) & 0xFF]; |
144 | 0 | crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[2]) & 0xFF]; |
145 | 0 | crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[3]) & 0xFF]; |
146 | 0 | pByte_buf += 4; |
147 | 0 | buf_len -= 4; |
148 | 0 | } |
149 | |
|
150 | 0 | while (buf_len) |
151 | 0 | { |
152 | 0 | crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[0]) & 0xFF]; |
153 | 0 | ++pByte_buf; |
154 | 0 | --buf_len; |
155 | 0 | } |
156 | |
|
157 | 0 | return ~crc32; |
158 | 0 | } |
159 | | #endif |
160 | | |
161 | | void mz_free(void *p) |
162 | 0 | { |
163 | 0 | MZ_FREE(p); |
164 | 0 | } |
165 | | |
166 | | MINIZ_EXPORT void *miniz_def_alloc_func(void *opaque, size_t items, size_t size) |
167 | 0 | { |
168 | 0 | (void)opaque, (void)items, (void)size; |
169 | 0 | return MZ_MALLOC(items * size); |
170 | 0 | } |
171 | | MINIZ_EXPORT void miniz_def_free_func(void *opaque, void *address) |
172 | 0 | { |
173 | 0 | (void)opaque, (void)address; |
174 | 0 | MZ_FREE(address); |
175 | 0 | } |
176 | | MINIZ_EXPORT void *miniz_def_realloc_func(void *opaque, void *address, size_t items, size_t size) |
177 | 0 | { |
178 | 0 | (void)opaque, (void)address, (void)items, (void)size; |
179 | 0 | return MZ_REALLOC(address, items * size); |
180 | 0 | } |
181 | | |
182 | | const char *mz_version(void) |
183 | 0 | { |
184 | 0 | return MZ_VERSION; |
185 | 0 | } |
186 | | |
187 | | #ifndef MINIZ_NO_ZLIB_APIS |
188 | | |
189 | | #ifndef MINIZ_NO_DEFLATE_APIS |
190 | | |
191 | | int mz_deflateInit(mz_streamp pStream, int level) |
192 | 0 | { |
193 | 0 | return mz_deflateInit2(pStream, level, MZ_DEFLATED, MZ_DEFAULT_WINDOW_BITS, 9, MZ_DEFAULT_STRATEGY); |
194 | 0 | } |
195 | | |
196 | | int mz_deflateInit2(mz_streamp pStream, int level, int method, int window_bits, int mem_level, int strategy) |
197 | 0 | { |
198 | 0 | tdefl_compressor *pComp; |
199 | 0 | mz_uint comp_flags = TDEFL_COMPUTE_ADLER32 | tdefl_create_comp_flags_from_zip_params(level, window_bits, strategy); |
200 | |
|
201 | 0 | if (!pStream) |
202 | 0 | return MZ_STREAM_ERROR; |
203 | 0 | if ((method != MZ_DEFLATED) || ((mem_level < 1) || (mem_level > 9)) || ((window_bits != MZ_DEFAULT_WINDOW_BITS) && (-window_bits != MZ_DEFAULT_WINDOW_BITS))) |
204 | 0 | return MZ_PARAM_ERROR; |
205 | | |
206 | 0 | pStream->data_type = 0; |
207 | 0 | pStream->adler = MZ_ADLER32_INIT; |
208 | 0 | pStream->msg = NULL; |
209 | 0 | pStream->reserved = 0; |
210 | 0 | pStream->total_in = 0; |
211 | 0 | pStream->total_out = 0; |
212 | 0 | if (!pStream->zalloc) |
213 | 0 | pStream->zalloc = miniz_def_alloc_func; |
214 | 0 | if (!pStream->zfree) |
215 | 0 | pStream->zfree = miniz_def_free_func; |
216 | |
|
217 | 0 | pComp = (tdefl_compressor *)pStream->zalloc(pStream->opaque, 1, sizeof(tdefl_compressor)); |
218 | 0 | if (!pComp) |
219 | 0 | return MZ_MEM_ERROR; |
220 | | |
221 | 0 | pStream->state = (struct mz_internal_state *)pComp; |
222 | |
|
223 | 0 | if (tdefl_init(pComp, NULL, NULL, comp_flags) != TDEFL_STATUS_OKAY) |
224 | 0 | { |
225 | 0 | mz_deflateEnd(pStream); |
226 | 0 | return MZ_PARAM_ERROR; |
227 | 0 | } |
228 | | |
229 | 0 | return MZ_OK; |
230 | 0 | } |
231 | | |
232 | | int mz_deflateReset(mz_streamp pStream) |
233 | 0 | { |
234 | 0 | if ((!pStream) || (!pStream->state) || (!pStream->zalloc) || (!pStream->zfree)) |
235 | 0 | return MZ_STREAM_ERROR; |
236 | 0 | pStream->total_in = pStream->total_out = 0; |
237 | 0 | tdefl_init((tdefl_compressor *)pStream->state, NULL, NULL, ((tdefl_compressor *)pStream->state)->m_flags); |
238 | 0 | return MZ_OK; |
239 | 0 | } |
240 | | |
241 | | int mz_deflate(mz_streamp pStream, int flush) |
242 | 0 | { |
243 | 0 | size_t in_bytes, out_bytes; |
244 | 0 | mz_ulong orig_total_in, orig_total_out; |
245 | 0 | int mz_status = MZ_OK; |
246 | |
|
247 | 0 | if ((!pStream) || (!pStream->state) || (flush < 0) || (flush > MZ_FINISH) || (!pStream->next_out)) |
248 | 0 | return MZ_STREAM_ERROR; |
249 | 0 | if (!pStream->avail_out) |
250 | 0 | return MZ_BUF_ERROR; |
251 | | |
252 | 0 | if (flush == MZ_PARTIAL_FLUSH) |
253 | 0 | flush = MZ_SYNC_FLUSH; |
254 | |
|
255 | 0 | if (((tdefl_compressor *)pStream->state)->m_prev_return_status == TDEFL_STATUS_DONE) |
256 | 0 | return (flush == MZ_FINISH) ? MZ_STREAM_END : MZ_BUF_ERROR; |
257 | | |
258 | 0 | orig_total_in = pStream->total_in; |
259 | 0 | orig_total_out = pStream->total_out; |
260 | 0 | for (;;) |
261 | 0 | { |
262 | 0 | tdefl_status defl_status; |
263 | 0 | in_bytes = pStream->avail_in; |
264 | 0 | out_bytes = pStream->avail_out; |
265 | |
|
266 | 0 | defl_status = tdefl_compress((tdefl_compressor *)pStream->state, pStream->next_in, &in_bytes, pStream->next_out, &out_bytes, (tdefl_flush)flush); |
267 | 0 | pStream->next_in += (mz_uint)in_bytes; |
268 | 0 | pStream->avail_in -= (mz_uint)in_bytes; |
269 | 0 | pStream->total_in += (mz_uint)in_bytes; |
270 | 0 | pStream->adler = tdefl_get_adler32((tdefl_compressor *)pStream->state); |
271 | |
|
272 | 0 | pStream->next_out += (mz_uint)out_bytes; |
273 | 0 | pStream->avail_out -= (mz_uint)out_bytes; |
274 | 0 | pStream->total_out += (mz_uint)out_bytes; |
275 | |
|
276 | 0 | if (defl_status < 0) |
277 | 0 | { |
278 | 0 | mz_status = MZ_STREAM_ERROR; |
279 | 0 | break; |
280 | 0 | } |
281 | 0 | else if (defl_status == TDEFL_STATUS_DONE) |
282 | 0 | { |
283 | 0 | mz_status = MZ_STREAM_END; |
284 | 0 | break; |
285 | 0 | } |
286 | 0 | else if (!pStream->avail_out) |
287 | 0 | break; |
288 | 0 | else if ((!pStream->avail_in) && (flush != MZ_FINISH)) |
289 | 0 | { |
290 | 0 | if ((flush) || (pStream->total_in != orig_total_in) || (pStream->total_out != orig_total_out)) |
291 | 0 | break; |
292 | 0 | return MZ_BUF_ERROR; /* Can't make forward progress without some input. |
293 | | */ |
294 | 0 | } |
295 | 0 | } |
296 | 0 | return mz_status; |
297 | 0 | } |
298 | | |
299 | | int mz_deflateEnd(mz_streamp pStream) |
300 | 0 | { |
301 | 0 | if (!pStream) |
302 | 0 | return MZ_STREAM_ERROR; |
303 | 0 | if (pStream->state) |
304 | 0 | { |
305 | 0 | pStream->zfree(pStream->opaque, pStream->state); |
306 | 0 | pStream->state = NULL; |
307 | 0 | } |
308 | 0 | return MZ_OK; |
309 | 0 | } |
310 | | |
311 | | mz_ulong mz_deflateBound(mz_streamp pStream, mz_ulong source_len) |
312 | 0 | { |
313 | 0 | (void)pStream; |
314 | | /* This is really over conservative. (And lame, but it's actually pretty tricky to compute a true upper bound given the way tdefl's blocking works.) */ |
315 | 0 | return MZ_MAX(128 + (source_len * 110) / 100, 128 + source_len + ((source_len / (31 * 1024)) + 1) * 5); |
316 | 0 | } |
317 | | |
318 | | int mz_compress2(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len, int level) |
319 | 0 | { |
320 | 0 | int status; |
321 | 0 | mz_stream stream; |
322 | 0 | memset(&stream, 0, sizeof(stream)); |
323 | | |
324 | | /* In case mz_ulong is 64-bits (argh I hate longs). */ |
325 | 0 | if ((mz_uint64)(source_len | *pDest_len) > 0xFFFFFFFFU) |
326 | 0 | return MZ_PARAM_ERROR; |
327 | | |
328 | 0 | stream.next_in = pSource; |
329 | 0 | stream.avail_in = (mz_uint32)source_len; |
330 | 0 | stream.next_out = pDest; |
331 | 0 | stream.avail_out = (mz_uint32)*pDest_len; |
332 | |
|
333 | 0 | status = mz_deflateInit(&stream, level); |
334 | 0 | if (status != MZ_OK) |
335 | 0 | return status; |
336 | | |
337 | 0 | status = mz_deflate(&stream, MZ_FINISH); |
338 | 0 | if (status != MZ_STREAM_END) |
339 | 0 | { |
340 | 0 | mz_deflateEnd(&stream); |
341 | 0 | return (status == MZ_OK) ? MZ_BUF_ERROR : status; |
342 | 0 | } |
343 | | |
344 | 0 | *pDest_len = stream.total_out; |
345 | 0 | return mz_deflateEnd(&stream); |
346 | 0 | } |
347 | | |
348 | | int mz_compress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len) |
349 | 0 | { |
350 | 0 | return mz_compress2(pDest, pDest_len, pSource, source_len, MZ_DEFAULT_COMPRESSION); |
351 | 0 | } |
352 | | |
353 | | mz_ulong mz_compressBound(mz_ulong source_len) |
354 | 0 | { |
355 | 0 | return mz_deflateBound(NULL, source_len); |
356 | 0 | } |
357 | | |
358 | | #endif /*#ifndef MINIZ_NO_DEFLATE_APIS*/ |
359 | | |
360 | | #ifndef MINIZ_NO_INFLATE_APIS |
361 | | |
362 | | typedef struct |
363 | | { |
364 | | tinfl_decompressor m_decomp; |
365 | | mz_uint m_dict_ofs, m_dict_avail, m_first_call, m_has_flushed; |
366 | | int m_window_bits; |
367 | | mz_uint8 m_dict[TINFL_LZ_DICT_SIZE]; |
368 | | tinfl_status m_last_status; |
369 | | } inflate_state; |
370 | | |
371 | | int mz_inflateInit2(mz_streamp pStream, int window_bits) |
372 | 0 | { |
373 | 0 | inflate_state *pDecomp; |
374 | 0 | if (!pStream) |
375 | 0 | return MZ_STREAM_ERROR; |
376 | 0 | if ((window_bits != MZ_DEFAULT_WINDOW_BITS) && (-window_bits != MZ_DEFAULT_WINDOW_BITS)) |
377 | 0 | return MZ_PARAM_ERROR; |
378 | | |
379 | 0 | pStream->data_type = 0; |
380 | 0 | pStream->adler = 0; |
381 | 0 | pStream->msg = NULL; |
382 | 0 | pStream->total_in = 0; |
383 | 0 | pStream->total_out = 0; |
384 | 0 | pStream->reserved = 0; |
385 | 0 | if (!pStream->zalloc) |
386 | 0 | pStream->zalloc = miniz_def_alloc_func; |
387 | 0 | if (!pStream->zfree) |
388 | 0 | pStream->zfree = miniz_def_free_func; |
389 | |
|
390 | 0 | pDecomp = (inflate_state *)pStream->zalloc(pStream->opaque, 1, sizeof(inflate_state)); |
391 | 0 | if (!pDecomp) |
392 | 0 | return MZ_MEM_ERROR; |
393 | | |
394 | 0 | pStream->state = (struct mz_internal_state *)pDecomp; |
395 | |
|
396 | 0 | tinfl_init(&pDecomp->m_decomp); |
397 | 0 | pDecomp->m_dict_ofs = 0; |
398 | 0 | pDecomp->m_dict_avail = 0; |
399 | 0 | pDecomp->m_last_status = TINFL_STATUS_NEEDS_MORE_INPUT; |
400 | 0 | pDecomp->m_first_call = 1; |
401 | 0 | pDecomp->m_has_flushed = 0; |
402 | 0 | pDecomp->m_window_bits = window_bits; |
403 | |
|
404 | 0 | return MZ_OK; |
405 | 0 | } |
406 | | |
407 | | int mz_inflateInit(mz_streamp pStream) |
408 | 0 | { |
409 | 0 | return mz_inflateInit2(pStream, MZ_DEFAULT_WINDOW_BITS); |
410 | 0 | } |
411 | | |
412 | | int mz_inflateReset(mz_streamp pStream) |
413 | 0 | { |
414 | 0 | inflate_state *pDecomp; |
415 | 0 | if (!pStream) |
416 | 0 | return MZ_STREAM_ERROR; |
417 | | |
418 | 0 | pStream->data_type = 0; |
419 | 0 | pStream->adler = 0; |
420 | 0 | pStream->msg = NULL; |
421 | 0 | pStream->total_in = 0; |
422 | 0 | pStream->total_out = 0; |
423 | 0 | pStream->reserved = 0; |
424 | |
|
425 | 0 | pDecomp = (inflate_state *)pStream->state; |
426 | |
|
427 | 0 | tinfl_init(&pDecomp->m_decomp); |
428 | 0 | pDecomp->m_dict_ofs = 0; |
429 | 0 | pDecomp->m_dict_avail = 0; |
430 | 0 | pDecomp->m_last_status = TINFL_STATUS_NEEDS_MORE_INPUT; |
431 | 0 | pDecomp->m_first_call = 1; |
432 | 0 | pDecomp->m_has_flushed = 0; |
433 | 0 | /* pDecomp->m_window_bits = window_bits */; |
434 | |
|
435 | 0 | return MZ_OK; |
436 | 0 | } |
437 | | |
438 | | int mz_inflate(mz_streamp pStream, int flush) |
439 | 0 | { |
440 | 0 | inflate_state *pState; |
441 | 0 | mz_uint n, first_call, decomp_flags = TINFL_FLAG_COMPUTE_ADLER32; |
442 | 0 | size_t in_bytes, out_bytes, orig_avail_in; |
443 | 0 | tinfl_status status; |
444 | |
|
445 | 0 | if ((!pStream) || (!pStream->state)) |
446 | 0 | return MZ_STREAM_ERROR; |
447 | 0 | if (flush == MZ_PARTIAL_FLUSH) |
448 | 0 | flush = MZ_SYNC_FLUSH; |
449 | 0 | if ((flush) && (flush != MZ_SYNC_FLUSH) && (flush != MZ_FINISH)) |
450 | 0 | return MZ_STREAM_ERROR; |
451 | | |
452 | 0 | pState = (inflate_state *)pStream->state; |
453 | 0 | if (pState->m_window_bits > 0) |
454 | 0 | decomp_flags |= TINFL_FLAG_PARSE_ZLIB_HEADER; |
455 | 0 | orig_avail_in = pStream->avail_in; |
456 | |
|
457 | 0 | first_call = pState->m_first_call; |
458 | 0 | pState->m_first_call = 0; |
459 | 0 | if (pState->m_last_status < 0) |
460 | 0 | return MZ_DATA_ERROR; |
461 | | |
462 | 0 | if (pState->m_has_flushed && (flush != MZ_FINISH)) |
463 | 0 | return MZ_STREAM_ERROR; |
464 | 0 | pState->m_has_flushed |= (flush == MZ_FINISH); |
465 | |
|
466 | 0 | if ((flush == MZ_FINISH) && (first_call)) |
467 | 0 | { |
468 | | /* MZ_FINISH on the first call implies that the input and output buffers are large enough to hold the entire compressed/decompressed file. */ |
469 | 0 | decomp_flags |= TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF; |
470 | 0 | in_bytes = pStream->avail_in; |
471 | 0 | out_bytes = pStream->avail_out; |
472 | 0 | status = tinfl_decompress(&pState->m_decomp, pStream->next_in, &in_bytes, pStream->next_out, pStream->next_out, &out_bytes, decomp_flags); |
473 | 0 | pState->m_last_status = status; |
474 | 0 | pStream->next_in += (mz_uint)in_bytes; |
475 | 0 | pStream->avail_in -= (mz_uint)in_bytes; |
476 | 0 | pStream->total_in += (mz_uint)in_bytes; |
477 | 0 | pStream->adler = tinfl_get_adler32(&pState->m_decomp); |
478 | 0 | pStream->next_out += (mz_uint)out_bytes; |
479 | 0 | pStream->avail_out -= (mz_uint)out_bytes; |
480 | 0 | pStream->total_out += (mz_uint)out_bytes; |
481 | |
|
482 | 0 | if (status < 0) |
483 | 0 | return MZ_DATA_ERROR; |
484 | 0 | else if (status != TINFL_STATUS_DONE) |
485 | 0 | { |
486 | 0 | pState->m_last_status = TINFL_STATUS_FAILED; |
487 | 0 | return MZ_BUF_ERROR; |
488 | 0 | } |
489 | 0 | return MZ_STREAM_END; |
490 | 0 | } |
491 | | /* flush != MZ_FINISH then we must assume there's more input. */ |
492 | 0 | if (flush != MZ_FINISH) |
493 | 0 | decomp_flags |= TINFL_FLAG_HAS_MORE_INPUT; |
494 | |
|
495 | 0 | if (pState->m_dict_avail) |
496 | 0 | { |
497 | 0 | n = MZ_MIN(pState->m_dict_avail, pStream->avail_out); |
498 | 0 | memcpy(pStream->next_out, pState->m_dict + pState->m_dict_ofs, n); |
499 | 0 | pStream->next_out += n; |
500 | 0 | pStream->avail_out -= n; |
501 | 0 | pStream->total_out += n; |
502 | 0 | pState->m_dict_avail -= n; |
503 | 0 | pState->m_dict_ofs = (pState->m_dict_ofs + n) & (TINFL_LZ_DICT_SIZE - 1); |
504 | 0 | return ((pState->m_last_status == TINFL_STATUS_DONE) && (!pState->m_dict_avail)) ? MZ_STREAM_END : MZ_OK; |
505 | 0 | } |
506 | | |
507 | 0 | for (;;) |
508 | 0 | { |
509 | 0 | in_bytes = pStream->avail_in; |
510 | 0 | out_bytes = TINFL_LZ_DICT_SIZE - pState->m_dict_ofs; |
511 | |
|
512 | 0 | 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); |
513 | 0 | pState->m_last_status = status; |
514 | |
|
515 | 0 | pStream->next_in += (mz_uint)in_bytes; |
516 | 0 | pStream->avail_in -= (mz_uint)in_bytes; |
517 | 0 | pStream->total_in += (mz_uint)in_bytes; |
518 | 0 | pStream->adler = tinfl_get_adler32(&pState->m_decomp); |
519 | |
|
520 | 0 | pState->m_dict_avail = (mz_uint)out_bytes; |
521 | |
|
522 | 0 | n = MZ_MIN(pState->m_dict_avail, pStream->avail_out); |
523 | 0 | memcpy(pStream->next_out, pState->m_dict + pState->m_dict_ofs, n); |
524 | 0 | pStream->next_out += n; |
525 | 0 | pStream->avail_out -= n; |
526 | 0 | pStream->total_out += n; |
527 | 0 | pState->m_dict_avail -= n; |
528 | 0 | pState->m_dict_ofs = (pState->m_dict_ofs + n) & (TINFL_LZ_DICT_SIZE - 1); |
529 | |
|
530 | 0 | if (status < 0) |
531 | 0 | return MZ_DATA_ERROR; /* Stream is corrupted (there could be some uncompressed data left in the output dictionary - oh well). */ |
532 | 0 | else if ((status == TINFL_STATUS_NEEDS_MORE_INPUT) && (!orig_avail_in)) |
533 | 0 | return MZ_BUF_ERROR; /* Signal caller that we can't make forward progress without supplying more input or by setting flush to MZ_FINISH. */ |
534 | 0 | else if (flush == MZ_FINISH) |
535 | 0 | { |
536 | | /* The output buffer MUST be large to hold the remaining uncompressed data when flush==MZ_FINISH. */ |
537 | 0 | if (status == TINFL_STATUS_DONE) |
538 | 0 | return pState->m_dict_avail ? MZ_BUF_ERROR : MZ_STREAM_END; |
539 | | /* 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. */ |
540 | 0 | else if (!pStream->avail_out) |
541 | 0 | return MZ_BUF_ERROR; |
542 | 0 | } |
543 | 0 | else if ((status == TINFL_STATUS_DONE) || (!pStream->avail_in) || (!pStream->avail_out) || (pState->m_dict_avail)) |
544 | 0 | break; |
545 | 0 | } |
546 | | |
547 | 0 | return ((status == TINFL_STATUS_DONE) && (!pState->m_dict_avail)) ? MZ_STREAM_END : MZ_OK; |
548 | 0 | } |
549 | | |
550 | | int mz_inflateEnd(mz_streamp pStream) |
551 | 0 | { |
552 | 0 | if (!pStream) |
553 | 0 | return MZ_STREAM_ERROR; |
554 | 0 | if (pStream->state) |
555 | 0 | { |
556 | 0 | pStream->zfree(pStream->opaque, pStream->state); |
557 | 0 | pStream->state = NULL; |
558 | 0 | } |
559 | 0 | return MZ_OK; |
560 | 0 | } |
561 | | int mz_uncompress2(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong *pSource_len) |
562 | 0 | { |
563 | 0 | mz_stream stream; |
564 | 0 | int status; |
565 | 0 | memset(&stream, 0, sizeof(stream)); |
566 | | |
567 | | /* In case mz_ulong is 64-bits (argh I hate longs). */ |
568 | 0 | if ((mz_uint64)(*pSource_len | *pDest_len) > 0xFFFFFFFFU) |
569 | 0 | return MZ_PARAM_ERROR; |
570 | | |
571 | 0 | stream.next_in = pSource; |
572 | 0 | stream.avail_in = (mz_uint32)*pSource_len; |
573 | 0 | stream.next_out = pDest; |
574 | 0 | stream.avail_out = (mz_uint32)*pDest_len; |
575 | |
|
576 | 0 | status = mz_inflateInit(&stream); |
577 | 0 | if (status != MZ_OK) |
578 | 0 | return status; |
579 | | |
580 | 0 | status = mz_inflate(&stream, MZ_FINISH); |
581 | 0 | *pSource_len = *pSource_len - stream.avail_in; |
582 | 0 | if (status != MZ_STREAM_END) |
583 | 0 | { |
584 | 0 | mz_inflateEnd(&stream); |
585 | 0 | return ((status == MZ_BUF_ERROR) && (!stream.avail_in)) ? MZ_DATA_ERROR : status; |
586 | 0 | } |
587 | 0 | *pDest_len = stream.total_out; |
588 | |
|
589 | 0 | return mz_inflateEnd(&stream); |
590 | 0 | } |
591 | | |
592 | | int mz_uncompress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len) |
593 | 0 | { |
594 | 0 | return mz_uncompress2(pDest, pDest_len, pSource, &source_len); |
595 | 0 | } |
596 | | |
597 | | #endif /*#ifndef MINIZ_NO_INFLATE_APIS*/ |
598 | | |
599 | | const char *mz_error(int err) |
600 | 0 | { |
601 | 0 | static struct |
602 | 0 | { |
603 | 0 | int m_err; |
604 | 0 | const char *m_pDesc; |
605 | 0 | } s_error_descs[] = |
606 | 0 | { |
607 | 0 | { MZ_OK, "" }, { MZ_STREAM_END, "stream end" }, { MZ_NEED_DICT, "need dictionary" }, { MZ_ERRNO, "file error" }, { MZ_STREAM_ERROR, "stream error" }, { MZ_DATA_ERROR, "data error" }, { MZ_MEM_ERROR, "out of memory" }, { MZ_BUF_ERROR, "buf error" }, { MZ_VERSION_ERROR, "version error" }, { MZ_PARAM_ERROR, "parameter error" } |
608 | 0 | }; |
609 | 0 | mz_uint i; |
610 | 0 | for (i = 0; i < sizeof(s_error_descs) / sizeof(s_error_descs[0]); ++i) |
611 | 0 | if (s_error_descs[i].m_err == err) |
612 | 0 | return s_error_descs[i].m_pDesc; |
613 | 0 | return NULL; |
614 | 0 | } |
615 | | |
616 | | #endif /*MINIZ_NO_ZLIB_APIS */ |
617 | | |
618 | | #ifdef __cplusplus |
619 | | } |
620 | | #endif |
621 | | |
622 | | /* |
623 | | This is free and unencumbered software released into the public domain. |
624 | | |
625 | | Anyone is free to copy, modify, publish, use, compile, sell, or |
626 | | distribute this software, either in source code form or as a compiled |
627 | | binary, for any purpose, commercial or non-commercial, and by any |
628 | | means. |
629 | | |
630 | | In jurisdictions that recognize copyright laws, the author or authors |
631 | | of this software dedicate any and all copyright interest in the |
632 | | software to the public domain. We make this dedication for the benefit |
633 | | of the public at large and to the detriment of our heirs and |
634 | | successors. We intend this dedication to be an overt act of |
635 | | relinquishment in perpetuity of all present and future rights to this |
636 | | software under copyright law. |
637 | | |
638 | | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
639 | | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
640 | | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
641 | | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
642 | | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
643 | | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
644 | | OTHER DEALINGS IN THE SOFTWARE. |
645 | | |
646 | | For more information, please refer to <http://unlicense.org/> |
647 | | */ |