/src/zstd/lib/legacy/zstd_v02.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) Yann Collet, Meta Platforms, Inc. and affiliates. |
3 | | * All rights reserved. |
4 | | * |
5 | | * This source code is licensed under both the BSD-style license (found in the |
6 | | * LICENSE file in the root directory of this source tree) and the GPLv2 (found |
7 | | * in the COPYING file in the root directory of this source tree). |
8 | | * You may select, at your option, one of the above-listed licenses. |
9 | | */ |
10 | | |
11 | | |
12 | | #include <stddef.h> /* size_t, ptrdiff_t */ |
13 | | #include "zstd_v02.h" |
14 | | #include "../common/compiler.h" |
15 | | #include "../common/error_private.h" |
16 | | |
17 | | |
18 | | /****************************************** |
19 | | * Compiler-specific |
20 | | ******************************************/ |
21 | | #if defined(_MSC_VER) /* Visual Studio */ |
22 | | # include <stdlib.h> /* _byteswap_ulong */ |
23 | | # include <intrin.h> /* _byteswap_* */ |
24 | | #endif |
25 | | |
26 | | |
27 | | /* ****************************************************************** |
28 | | mem.h |
29 | | low-level memory access routines |
30 | | Copyright (C) 2013-2015, Yann Collet. |
31 | | |
32 | | BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php) |
33 | | |
34 | | Redistribution and use in source and binary forms, with or without |
35 | | modification, are permitted provided that the following conditions are |
36 | | met: |
37 | | |
38 | | * Redistributions of source code must retain the above copyright |
39 | | notice, this list of conditions and the following disclaimer. |
40 | | * Redistributions in binary form must reproduce the above |
41 | | copyright notice, this list of conditions and the following disclaimer |
42 | | in the documentation and/or other materials provided with the |
43 | | distribution. |
44 | | |
45 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
46 | | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
47 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
48 | | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
49 | | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
50 | | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
51 | | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
52 | | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
53 | | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
54 | | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
55 | | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
56 | | |
57 | | You can contact the author at : |
58 | | - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy |
59 | | - Public forum : https://groups.google.com/forum/#!forum/lz4c |
60 | | ****************************************************************** */ |
61 | | #ifndef MEM_H_MODULE |
62 | | #define MEM_H_MODULE |
63 | | |
64 | | #if defined (__cplusplus) |
65 | | extern "C" { |
66 | | #endif |
67 | | |
68 | | /****************************************** |
69 | | * Includes |
70 | | ******************************************/ |
71 | | #include <stddef.h> /* size_t, ptrdiff_t */ |
72 | | #include <string.h> /* memcpy */ |
73 | | |
74 | | |
75 | | /**************************************************************** |
76 | | * Basic Types |
77 | | *****************************************************************/ |
78 | | #if defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) |
79 | | # if defined(_AIX) |
80 | | # include <inttypes.h> |
81 | | # else |
82 | | # include <stdint.h> /* intptr_t */ |
83 | | # endif |
84 | | typedef uint8_t BYTE; |
85 | | typedef uint16_t U16; |
86 | | typedef int16_t S16; |
87 | | typedef uint32_t U32; |
88 | | typedef int32_t S32; |
89 | | typedef uint64_t U64; |
90 | | typedef int64_t S64; |
91 | | #else |
92 | | typedef unsigned char BYTE; |
93 | | typedef unsigned short U16; |
94 | | typedef signed short S16; |
95 | | typedef unsigned int U32; |
96 | | typedef signed int S32; |
97 | | typedef unsigned long long U64; |
98 | | typedef signed long long S64; |
99 | | #endif |
100 | | |
101 | | |
102 | | /**************************************************************** |
103 | | * Memory I/O |
104 | | *****************************************************************/ |
105 | | |
106 | 659k | MEM_STATIC unsigned MEM_32bits(void) { return sizeof(void*)==4; } |
107 | 1.64M | MEM_STATIC unsigned MEM_64bits(void) { return sizeof(void*)==8; } |
108 | | |
109 | | MEM_STATIC unsigned MEM_isLittleEndian(void) |
110 | 1.04M | { |
111 | 1.04M | const union { U32 u; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */ |
112 | 1.04M | return one.c[0]; |
113 | 1.04M | } |
114 | | |
115 | | MEM_STATIC U16 MEM_read16(const void* memPtr) |
116 | 18.6k | { |
117 | 18.6k | U16 val; memcpy(&val, memPtr, sizeof(val)); return val; |
118 | 18.6k | } |
119 | | |
120 | | MEM_STATIC U32 MEM_read32(const void* memPtr) |
121 | 313k | { |
122 | 313k | U32 val; memcpy(&val, memPtr, sizeof(val)); return val; |
123 | 313k | } |
124 | | |
125 | | MEM_STATIC U64 MEM_read64(const void* memPtr) |
126 | 591k | { |
127 | 591k | U64 val; memcpy(&val, memPtr, sizeof(val)); return val; |
128 | 591k | } |
129 | | |
130 | | MEM_STATIC void MEM_write16(void* memPtr, U16 value) |
131 | 123k | { |
132 | 123k | memcpy(memPtr, &value, sizeof(value)); |
133 | 123k | } |
134 | | |
135 | | MEM_STATIC U16 MEM_readLE16(const void* memPtr) |
136 | 18.6k | { |
137 | 18.6k | if (MEM_isLittleEndian()) |
138 | 18.6k | return MEM_read16(memPtr); |
139 | 0 | else |
140 | 0 | { |
141 | 0 | const BYTE* p = (const BYTE*)memPtr; |
142 | 0 | return (U16)(p[0] + (p[1]<<8)); |
143 | 0 | } |
144 | 18.6k | } |
145 | | |
146 | | MEM_STATIC void MEM_writeLE16(void* memPtr, U16 val) |
147 | 123k | { |
148 | 123k | if (MEM_isLittleEndian()) |
149 | 123k | { |
150 | 123k | MEM_write16(memPtr, val); |
151 | 123k | } |
152 | 0 | else |
153 | 0 | { |
154 | 0 | BYTE* p = (BYTE*)memPtr; |
155 | 0 | p[0] = (BYTE)val; |
156 | 0 | p[1] = (BYTE)(val>>8); |
157 | 0 | } |
158 | 123k | } |
159 | | |
160 | | MEM_STATIC U32 MEM_readLE24(const void* memPtr) |
161 | 430 | { |
162 | 430 | return MEM_readLE16(memPtr) + (((const BYTE*)memPtr)[2] << 16); |
163 | 430 | } |
164 | | |
165 | | MEM_STATIC U32 MEM_readLE32(const void* memPtr) |
166 | 313k | { |
167 | 313k | if (MEM_isLittleEndian()) |
168 | 313k | return MEM_read32(memPtr); |
169 | 0 | else |
170 | 0 | { |
171 | 0 | const BYTE* p = (const BYTE*)memPtr; |
172 | 0 | return (U32)((U32)p[0] + ((U32)p[1]<<8) + ((U32)p[2]<<16) + ((U32)p[3]<<24)); |
173 | 0 | } |
174 | 313k | } |
175 | | |
176 | | |
177 | | MEM_STATIC U64 MEM_readLE64(const void* memPtr) |
178 | 591k | { |
179 | 591k | if (MEM_isLittleEndian()) |
180 | 591k | return MEM_read64(memPtr); |
181 | 0 | else |
182 | 0 | { |
183 | 0 | const BYTE* p = (const BYTE*)memPtr; |
184 | 0 | return (U64)((U64)p[0] + ((U64)p[1]<<8) + ((U64)p[2]<<16) + ((U64)p[3]<<24) |
185 | 0 | + ((U64)p[4]<<32) + ((U64)p[5]<<40) + ((U64)p[6]<<48) + ((U64)p[7]<<56)); |
186 | 0 | } |
187 | 591k | } |
188 | | |
189 | | |
190 | | MEM_STATIC size_t MEM_readLEST(const void* memPtr) |
191 | 591k | { |
192 | 591k | if (MEM_32bits()) |
193 | 0 | return (size_t)MEM_readLE32(memPtr); |
194 | 591k | else |
195 | 591k | return (size_t)MEM_readLE64(memPtr); |
196 | 591k | } |
197 | | |
198 | | #if defined (__cplusplus) |
199 | | } |
200 | | #endif |
201 | | |
202 | | #endif /* MEM_H_MODULE */ |
203 | | |
204 | | |
205 | | /* ****************************************************************** |
206 | | bitstream |
207 | | Part of NewGen Entropy library |
208 | | header file (to include) |
209 | | Copyright (C) 2013-2015, Yann Collet. |
210 | | |
211 | | BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php) |
212 | | |
213 | | Redistribution and use in source and binary forms, with or without |
214 | | modification, are permitted provided that the following conditions are |
215 | | met: |
216 | | |
217 | | * Redistributions of source code must retain the above copyright |
218 | | notice, this list of conditions and the following disclaimer. |
219 | | * Redistributions in binary form must reproduce the above |
220 | | copyright notice, this list of conditions and the following disclaimer |
221 | | in the documentation and/or other materials provided with the |
222 | | distribution. |
223 | | |
224 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
225 | | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
226 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
227 | | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
228 | | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
229 | | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
230 | | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
231 | | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
232 | | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
233 | | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
234 | | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
235 | | |
236 | | You can contact the author at : |
237 | | - Source repository : https://github.com/Cyan4973/FiniteStateEntropy |
238 | | - Public forum : https://groups.google.com/forum/#!forum/lz4c |
239 | | ****************************************************************** */ |
240 | | #ifndef BITSTREAM_H_MODULE |
241 | | #define BITSTREAM_H_MODULE |
242 | | |
243 | | #if defined (__cplusplus) |
244 | | extern "C" { |
245 | | #endif |
246 | | |
247 | | |
248 | | /* |
249 | | * This API consists of small unitary functions, which highly benefit from being inlined. |
250 | | * Since link-time-optimization is not available for all compilers, |
251 | | * these functions are defined into a .h to be included. |
252 | | */ |
253 | | |
254 | | |
255 | | /********************************************** |
256 | | * bitStream decompression API (read backward) |
257 | | **********************************************/ |
258 | | typedef struct |
259 | | { |
260 | | size_t bitContainer; |
261 | | unsigned bitsConsumed; |
262 | | const char* ptr; |
263 | | const char* start; |
264 | | } BIT_DStream_t; |
265 | | |
266 | | typedef enum { BIT_DStream_unfinished = 0, |
267 | | BIT_DStream_endOfBuffer = 1, |
268 | | BIT_DStream_completed = 2, |
269 | | BIT_DStream_overflow = 3 } BIT_DStream_status; /* result of BIT_reloadDStream() */ |
270 | | /* 1,2,4,8 would be better for bitmap combinations, but slows down performance a bit ... :( */ |
271 | | |
272 | | MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize); |
273 | | MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, unsigned nbBits); |
274 | | MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD); |
275 | | MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* bitD); |
276 | | |
277 | | |
278 | | /****************************************** |
279 | | * unsafe API |
280 | | ******************************************/ |
281 | | MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits); |
282 | | /* faster, but works only if nbBits >= 1 */ |
283 | | |
284 | | |
285 | | |
286 | | /**************************************************************** |
287 | | * Helper functions |
288 | | ****************************************************************/ |
289 | | MEM_STATIC unsigned BIT_highbit32 (U32 val) |
290 | 1.49M | { |
291 | | # if defined(_MSC_VER) /* Visual */ |
292 | | unsigned long r; |
293 | | return _BitScanReverse(&r, val) ? (unsigned)r : 0; |
294 | | # elif defined(__GNUC__) && (__GNUC__ >= 3) /* Use GCC Intrinsic */ |
295 | | return __builtin_clz (val) ^ 31; |
296 | | # else /* Software version */ |
297 | | static const unsigned DeBruijnClz[32] = { 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 }; |
298 | | U32 v = val; |
299 | | unsigned r; |
300 | | v |= v >> 1; |
301 | | v |= v >> 2; |
302 | | v |= v >> 4; |
303 | | v |= v >> 8; |
304 | | v |= v >> 16; |
305 | | r = DeBruijnClz[ (U32) (v * 0x07C4ACDDU) >> 27]; |
306 | | return r; |
307 | | # endif |
308 | 1.49M | } |
309 | | |
310 | | |
311 | | |
312 | | /********************************************************** |
313 | | * bitStream decoding |
314 | | **********************************************************/ |
315 | | |
316 | | /*!BIT_initDStream |
317 | | * Initialize a BIT_DStream_t. |
318 | | * @bitD : a pointer to an already allocated BIT_DStream_t structure |
319 | | * @srcBuffer must point at the beginning of a bitStream |
320 | | * @srcSize must be the exact size of the bitStream |
321 | | * @result : size of stream (== srcSize) or an errorCode if a problem is detected |
322 | | */ |
323 | | MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize) |
324 | 19.6k | { |
325 | 19.6k | if (srcSize < 1) { memset(bitD, 0, sizeof(*bitD)); return ERROR(srcSize_wrong); } |
326 | | |
327 | 19.6k | if (srcSize >= sizeof(size_t)) /* normal case */ |
328 | 3.87k | { |
329 | 3.87k | U32 contain32; |
330 | 3.87k | bitD->start = (const char*)srcBuffer; |
331 | 3.87k | bitD->ptr = (const char*)srcBuffer + srcSize - sizeof(size_t); |
332 | 3.87k | bitD->bitContainer = MEM_readLEST(bitD->ptr); |
333 | 3.87k | contain32 = ((const BYTE*)srcBuffer)[srcSize-1]; |
334 | 3.87k | if (contain32 == 0) return ERROR(GENERIC); /* endMark not present */ |
335 | 3.79k | bitD->bitsConsumed = 8 - BIT_highbit32(contain32); |
336 | 3.79k | } |
337 | 15.7k | else |
338 | 15.7k | { |
339 | 15.7k | U32 contain32; |
340 | 15.7k | bitD->start = (const char*)srcBuffer; |
341 | 15.7k | bitD->ptr = bitD->start; |
342 | 15.7k | bitD->bitContainer = *(const BYTE*)(bitD->start); |
343 | 15.7k | switch(srcSize) |
344 | 15.7k | { |
345 | 104 | case 7: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[6]) << (sizeof(size_t)*8 - 16); |
346 | | /* fallthrough */ |
347 | 499 | case 6: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[5]) << (sizeof(size_t)*8 - 24); |
348 | | /* fallthrough */ |
349 | 771 | case 5: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[4]) << (sizeof(size_t)*8 - 32); |
350 | | /* fallthrough */ |
351 | 1.65k | case 4: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[3]) << 24; |
352 | | /* fallthrough */ |
353 | 6.57k | case 3: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[2]) << 16; |
354 | | /* fallthrough */ |
355 | 8.55k | case 2: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[1]) << 8; |
356 | | /* fallthrough */ |
357 | 15.7k | default:; |
358 | 15.7k | } |
359 | 15.7k | contain32 = ((const BYTE*)srcBuffer)[srcSize-1]; |
360 | 15.7k | if (contain32 == 0) return ERROR(GENERIC); /* endMark not present */ |
361 | 15.6k | bitD->bitsConsumed = 8 - BIT_highbit32(contain32); |
362 | 15.6k | bitD->bitsConsumed += (U32)(sizeof(size_t) - srcSize)*8; |
363 | 15.6k | } |
364 | | |
365 | 19.4k | return srcSize; |
366 | 19.6k | } |
367 | | |
368 | | MEM_STATIC size_t BIT_lookBits(BIT_DStream_t* bitD, U32 nbBits) |
369 | 213k | { |
370 | 213k | const U32 bitMask = sizeof(bitD->bitContainer)*8 - 1; |
371 | 213k | return ((bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> 1) >> ((bitMask-nbBits) & bitMask); |
372 | 213k | } |
373 | | |
374 | | /*! BIT_lookBitsFast : |
375 | | * unsafe version; only works if nbBits >= 1 */ |
376 | | MEM_STATIC size_t BIT_lookBitsFast(BIT_DStream_t* bitD, U32 nbBits) |
377 | 15.0M | { |
378 | 15.0M | const U32 bitMask = sizeof(bitD->bitContainer)*8 - 1; |
379 | 15.0M | return (bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> (((bitMask+1)-nbBits) & bitMask); |
380 | 15.0M | } |
381 | | |
382 | | MEM_STATIC void BIT_skipBits(BIT_DStream_t* bitD, U32 nbBits) |
383 | 15.2M | { |
384 | 15.2M | bitD->bitsConsumed += nbBits; |
385 | 15.2M | } |
386 | | |
387 | | MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, U32 nbBits) |
388 | 213k | { |
389 | 213k | size_t value = BIT_lookBits(bitD, nbBits); |
390 | 213k | BIT_skipBits(bitD, nbBits); |
391 | 213k | return value; |
392 | 213k | } |
393 | | |
394 | | /*!BIT_readBitsFast : |
395 | | * unsafe version; only works if nbBits >= 1 */ |
396 | | MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, U32 nbBits) |
397 | 8.93k | { |
398 | 8.93k | size_t value = BIT_lookBitsFast(bitD, nbBits); |
399 | 8.93k | BIT_skipBits(bitD, nbBits); |
400 | 8.93k | return value; |
401 | 8.93k | } |
402 | | |
403 | | MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD) |
404 | 679k | { |
405 | 679k | if (bitD->bitsConsumed > (sizeof(bitD->bitContainer)*8)) /* should never happen */ |
406 | 264 | return BIT_DStream_overflow; |
407 | | |
408 | 678k | if (bitD->ptr >= bitD->start + sizeof(bitD->bitContainer)) |
409 | 569k | { |
410 | 569k | bitD->ptr -= bitD->bitsConsumed >> 3; |
411 | 569k | bitD->bitsConsumed &= 7; |
412 | 569k | bitD->bitContainer = MEM_readLEST(bitD->ptr); |
413 | 569k | return BIT_DStream_unfinished; |
414 | 569k | } |
415 | 109k | if (bitD->ptr == bitD->start) |
416 | 91.4k | { |
417 | 91.4k | if (bitD->bitsConsumed < sizeof(bitD->bitContainer)*8) return BIT_DStream_endOfBuffer; |
418 | 24.7k | return BIT_DStream_completed; |
419 | 91.4k | } |
420 | 17.8k | { |
421 | 17.8k | U32 nbBytes = bitD->bitsConsumed >> 3; |
422 | 17.8k | BIT_DStream_status result = BIT_DStream_unfinished; |
423 | 17.8k | if (bitD->ptr - nbBytes < bitD->start) |
424 | 1.78k | { |
425 | 1.78k | nbBytes = (U32)(bitD->ptr - bitD->start); /* ptr > start */ |
426 | 1.78k | result = BIT_DStream_endOfBuffer; |
427 | 1.78k | } |
428 | 17.8k | bitD->ptr -= nbBytes; |
429 | 17.8k | bitD->bitsConsumed -= nbBytes*8; |
430 | 17.8k | bitD->bitContainer = MEM_readLEST(bitD->ptr); /* reminder : srcSize > sizeof(bitD) */ |
431 | 17.8k | return result; |
432 | 109k | } |
433 | 109k | } |
434 | | |
435 | | /*! BIT_endOfDStream |
436 | | * @return Tells if DStream has reached its exact end |
437 | | */ |
438 | | MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* DStream) |
439 | 38.6k | { |
440 | 38.6k | return ((DStream->ptr == DStream->start) && (DStream->bitsConsumed == sizeof(DStream->bitContainer)*8)); |
441 | 38.6k | } |
442 | | |
443 | | #if defined (__cplusplus) |
444 | | } |
445 | | #endif |
446 | | |
447 | | #endif /* BITSTREAM_H_MODULE */ |
448 | | /* ****************************************************************** |
449 | | Error codes and messages |
450 | | Copyright (C) 2013-2015, Yann Collet |
451 | | |
452 | | BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php) |
453 | | |
454 | | Redistribution and use in source and binary forms, with or without |
455 | | modification, are permitted provided that the following conditions are |
456 | | met: |
457 | | |
458 | | * Redistributions of source code must retain the above copyright |
459 | | notice, this list of conditions and the following disclaimer. |
460 | | * Redistributions in binary form must reproduce the above |
461 | | copyright notice, this list of conditions and the following disclaimer |
462 | | in the documentation and/or other materials provided with the |
463 | | distribution. |
464 | | |
465 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
466 | | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
467 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
468 | | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
469 | | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
470 | | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
471 | | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
472 | | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
473 | | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
474 | | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
475 | | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
476 | | |
477 | | You can contact the author at : |
478 | | - Source repository : https://github.com/Cyan4973/FiniteStateEntropy |
479 | | - Public forum : https://groups.google.com/forum/#!forum/lz4c |
480 | | ****************************************************************** */ |
481 | | #ifndef ERROR_H_MODULE |
482 | | #define ERROR_H_MODULE |
483 | | |
484 | | #if defined (__cplusplus) |
485 | | extern "C" { |
486 | | #endif |
487 | | |
488 | | |
489 | | /****************************************** |
490 | | * Compiler-specific |
491 | | ******************************************/ |
492 | | #if defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) |
493 | | # define ERR_STATIC static inline |
494 | | #elif defined(_MSC_VER) |
495 | | # define ERR_STATIC static __inline |
496 | | #elif defined(__GNUC__) |
497 | | # define ERR_STATIC static __attribute__((unused)) |
498 | | #else |
499 | | # define ERR_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */ |
500 | | #endif |
501 | | |
502 | | |
503 | | /****************************************** |
504 | | * Error Management |
505 | | ******************************************/ |
506 | | #define PREFIX(name) ZSTD_error_##name |
507 | | |
508 | | #define ERROR(name) (size_t)-PREFIX(name) |
509 | | |
510 | | #define ERROR_LIST(ITEM) \ |
511 | | ITEM(PREFIX(No_Error)) ITEM(PREFIX(GENERIC)) \ |
512 | | ITEM(PREFIX(dstSize_tooSmall)) ITEM(PREFIX(srcSize_wrong)) \ |
513 | | ITEM(PREFIX(prefix_unknown)) ITEM(PREFIX(corruption_detected)) \ |
514 | | ITEM(PREFIX(tableLog_tooLarge)) ITEM(PREFIX(maxSymbolValue_tooLarge)) ITEM(PREFIX(maxSymbolValue_tooSmall)) \ |
515 | | ITEM(PREFIX(maxCode)) |
516 | | |
517 | | #define ERROR_GENERATE_ENUM(ENUM) ENUM, |
518 | | typedef enum { ERROR_LIST(ERROR_GENERATE_ENUM) } ERR_codes; /* enum is exposed, to detect & handle specific errors; compare function result to -enum value */ |
519 | | |
520 | | #define ERROR_CONVERTTOSTRING(STRING) #STRING, |
521 | | #define ERROR_GENERATE_STRING(EXPR) ERROR_CONVERTTOSTRING(EXPR) |
522 | | static const char* ERR_strings[] = { ERROR_LIST(ERROR_GENERATE_STRING) }; |
523 | | |
524 | | ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); } |
525 | | |
526 | | ERR_STATIC const char* ERR_getErrorName(size_t code) |
527 | | { |
528 | | static const char* codeError = "Unspecified error code"; |
529 | | if (ERR_isError(code)) return ERR_strings[-(int)(code)]; |
530 | | return codeError; |
531 | | } |
532 | | |
533 | | |
534 | | #if defined (__cplusplus) |
535 | | } |
536 | | #endif |
537 | | |
538 | | #endif /* ERROR_H_MODULE */ |
539 | | /* |
540 | | Constructor and Destructor of type FSE_CTable |
541 | | Note that its size depends on 'tableLog' and 'maxSymbolValue' */ |
542 | | typedef unsigned FSE_CTable; /* don't allocate that. It's just a way to be more restrictive than void* */ |
543 | | typedef unsigned FSE_DTable; /* don't allocate that. It's just a way to be more restrictive than void* */ |
544 | | |
545 | | |
546 | | /* ****************************************************************** |
547 | | FSE : Finite State Entropy coder |
548 | | header file for static linking (only) |
549 | | Copyright (C) 2013-2015, Yann Collet |
550 | | |
551 | | BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php) |
552 | | |
553 | | Redistribution and use in source and binary forms, with or without |
554 | | modification, are permitted provided that the following conditions are |
555 | | met: |
556 | | |
557 | | * Redistributions of source code must retain the above copyright |
558 | | notice, this list of conditions and the following disclaimer. |
559 | | * Redistributions in binary form must reproduce the above |
560 | | copyright notice, this list of conditions and the following disclaimer |
561 | | in the documentation and/or other materials provided with the |
562 | | distribution. |
563 | | |
564 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
565 | | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
566 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
567 | | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
568 | | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
569 | | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
570 | | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
571 | | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
572 | | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
573 | | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
574 | | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
575 | | |
576 | | You can contact the author at : |
577 | | - Source repository : https://github.com/Cyan4973/FiniteStateEntropy |
578 | | - Public forum : https://groups.google.com/forum/#!forum/lz4c |
579 | | ****************************************************************** */ |
580 | | #if defined (__cplusplus) |
581 | | extern "C" { |
582 | | #endif |
583 | | |
584 | | |
585 | | /****************************************** |
586 | | * Static allocation |
587 | | ******************************************/ |
588 | | /* FSE buffer bounds */ |
589 | | #define FSE_NCOUNTBOUND 512 |
590 | | #define FSE_BLOCKBOUND(size) (size + (size>>7)) |
591 | | #define FSE_COMPRESSBOUND(size) (FSE_NCOUNTBOUND + FSE_BLOCKBOUND(size)) /* Macro version, useful for static allocation */ |
592 | | |
593 | | /* You can statically allocate FSE CTable/DTable as a table of unsigned using below macro */ |
594 | | #define FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue) (1 + (1<<(maxTableLog-1)) + ((maxSymbolValue+1)*2)) |
595 | | #define FSE_DTABLE_SIZE_U32(maxTableLog) (1 + (1<<maxTableLog)) |
596 | | |
597 | | |
598 | | /****************************************** |
599 | | * FSE advanced API |
600 | | ******************************************/ |
601 | | static size_t FSE_buildDTable_raw (FSE_DTable* dt, unsigned nbBits); |
602 | | /* build a fake FSE_DTable, designed to read an uncompressed bitstream where each symbol uses nbBits */ |
603 | | |
604 | | static size_t FSE_buildDTable_rle (FSE_DTable* dt, unsigned char symbolValue); |
605 | | /* build a fake FSE_DTable, designed to always generate the same symbolValue */ |
606 | | |
607 | | |
608 | | /****************************************** |
609 | | * FSE symbol decompression API |
610 | | ******************************************/ |
611 | | typedef struct |
612 | | { |
613 | | size_t state; |
614 | | const void* table; /* precise table may vary, depending on U16 */ |
615 | | } FSE_DState_t; |
616 | | |
617 | | |
618 | | static void FSE_initDState(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD, const FSE_DTable* dt); |
619 | | |
620 | | static unsigned char FSE_decodeSymbol(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD); |
621 | | |
622 | | static unsigned FSE_endOfDState(const FSE_DState_t* DStatePtr); |
623 | | |
624 | | |
625 | | /****************************************** |
626 | | * FSE unsafe API |
627 | | ******************************************/ |
628 | | static unsigned char FSE_decodeSymbolFast(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD); |
629 | | /* faster, but works only if nbBits is always >= 1 (otherwise, result will be corrupted) */ |
630 | | |
631 | | |
632 | | /****************************************** |
633 | | * Implementation of inline functions |
634 | | ******************************************/ |
635 | | |
636 | | /* decompression */ |
637 | | |
638 | | typedef struct { |
639 | | U16 tableLog; |
640 | | U16 fastMode; |
641 | | } FSE_DTableHeader; /* sizeof U32 */ |
642 | | |
643 | | typedef struct |
644 | | { |
645 | | unsigned short newState; |
646 | | unsigned char symbol; |
647 | | unsigned char nbBits; |
648 | | } FSE_decode_t; /* size == U32 */ |
649 | | |
650 | | MEM_STATIC void FSE_initDState(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD, const FSE_DTable* dt) |
651 | 38.2k | { |
652 | 38.2k | FSE_DTableHeader DTableH; |
653 | 38.2k | memcpy(&DTableH, dt, sizeof(DTableH)); |
654 | 38.2k | DStatePtr->state = BIT_readBits(bitD, DTableH.tableLog); |
655 | 38.2k | BIT_reloadDStream(bitD); |
656 | 38.2k | DStatePtr->table = dt + 1; |
657 | 38.2k | } |
658 | | |
659 | | MEM_STATIC BYTE FSE_decodeSymbol(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD) |
660 | 140k | { |
661 | 140k | const FSE_decode_t DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state]; |
662 | 140k | const U32 nbBits = DInfo.nbBits; |
663 | 140k | BYTE symbol = DInfo.symbol; |
664 | 140k | size_t lowBits = BIT_readBits(bitD, nbBits); |
665 | | |
666 | 140k | DStatePtr->state = DInfo.newState + lowBits; |
667 | 140k | return symbol; |
668 | 140k | } |
669 | | |
670 | | MEM_STATIC BYTE FSE_decodeSymbolFast(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD) |
671 | 8.93k | { |
672 | 8.93k | const FSE_decode_t DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state]; |
673 | 8.93k | const U32 nbBits = DInfo.nbBits; |
674 | 8.93k | BYTE symbol = DInfo.symbol; |
675 | 8.93k | size_t lowBits = BIT_readBitsFast(bitD, nbBits); |
676 | | |
677 | 8.93k | DStatePtr->state = DInfo.newState + lowBits; |
678 | 8.93k | return symbol; |
679 | 8.93k | } |
680 | | |
681 | | MEM_STATIC unsigned FSE_endOfDState(const FSE_DState_t* DStatePtr) |
682 | 3.74k | { |
683 | 3.74k | return DStatePtr->state == 0; |
684 | 3.74k | } |
685 | | |
686 | | |
687 | | #if defined (__cplusplus) |
688 | | } |
689 | | #endif |
690 | | /* ****************************************************************** |
691 | | Huff0 : Huffman coder, part of New Generation Entropy library |
692 | | header file for static linking (only) |
693 | | Copyright (C) 2013-2015, Yann Collet |
694 | | |
695 | | BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php) |
696 | | |
697 | | Redistribution and use in source and binary forms, with or without |
698 | | modification, are permitted provided that the following conditions are |
699 | | met: |
700 | | |
701 | | * Redistributions of source code must retain the above copyright |
702 | | notice, this list of conditions and the following disclaimer. |
703 | | * Redistributions in binary form must reproduce the above |
704 | | copyright notice, this list of conditions and the following disclaimer |
705 | | in the documentation and/or other materials provided with the |
706 | | distribution. |
707 | | |
708 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
709 | | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
710 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
711 | | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
712 | | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
713 | | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
714 | | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
715 | | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
716 | | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
717 | | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
718 | | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
719 | | |
720 | | You can contact the author at : |
721 | | - Source repository : https://github.com/Cyan4973/FiniteStateEntropy |
722 | | - Public forum : https://groups.google.com/forum/#!forum/lz4c |
723 | | ****************************************************************** */ |
724 | | |
725 | | #if defined (__cplusplus) |
726 | | extern "C" { |
727 | | #endif |
728 | | |
729 | | /****************************************** |
730 | | * Static allocation macros |
731 | | ******************************************/ |
732 | | /* Huff0 buffer bounds */ |
733 | | #define HUF_CTABLEBOUND 129 |
734 | | #define HUF_BLOCKBOUND(size) (size + (size>>8) + 8) /* only true if incompressible pre-filtered with fast heuristic */ |
735 | | #define HUF_COMPRESSBOUND(size) (HUF_CTABLEBOUND + HUF_BLOCKBOUND(size)) /* Macro version, useful for static allocation */ |
736 | | |
737 | | /* static allocation of Huff0's DTable */ |
738 | | #define HUF_DTABLE_SIZE(maxTableLog) (1 + (1<<maxTableLog)) /* nb Cells; use unsigned short for X2, unsigned int for X4 */ |
739 | | #define HUF_CREATE_STATIC_DTABLEX2(DTable, maxTableLog) \ |
740 | 1.99k | unsigned short DTable[HUF_DTABLE_SIZE(maxTableLog)] = { maxTableLog } |
741 | | #define HUF_CREATE_STATIC_DTABLEX4(DTable, maxTableLog) \ |
742 | 301 | unsigned int DTable[HUF_DTABLE_SIZE(maxTableLog)] = { maxTableLog } |
743 | | #define HUF_CREATE_STATIC_DTABLEX6(DTable, maxTableLog) \ |
744 | | unsigned int DTable[HUF_DTABLE_SIZE(maxTableLog) * 3 / 2] = { maxTableLog } |
745 | | |
746 | | |
747 | | /****************************************** |
748 | | * Advanced functions |
749 | | ******************************************/ |
750 | | static size_t HUF_decompress4X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* single-symbol decoder */ |
751 | | static size_t HUF_decompress4X4 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* double-symbols decoder */ |
752 | | |
753 | | |
754 | | #if defined (__cplusplus) |
755 | | } |
756 | | #endif |
757 | | |
758 | | /* |
759 | | zstd - standard compression library |
760 | | Header File |
761 | | Copyright (C) 2014-2015, Yann Collet. |
762 | | |
763 | | BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php) |
764 | | |
765 | | Redistribution and use in source and binary forms, with or without |
766 | | modification, are permitted provided that the following conditions are |
767 | | met: |
768 | | * Redistributions of source code must retain the above copyright |
769 | | notice, this list of conditions and the following disclaimer. |
770 | | * Redistributions in binary form must reproduce the above |
771 | | copyright notice, this list of conditions and the following disclaimer |
772 | | in the documentation and/or other materials provided with the |
773 | | distribution. |
774 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
775 | | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
776 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
777 | | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
778 | | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
779 | | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
780 | | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
781 | | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
782 | | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
783 | | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
784 | | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
785 | | |
786 | | You can contact the author at : |
787 | | - zstd source repository : https://github.com/Cyan4973/zstd |
788 | | - ztsd public forum : https://groups.google.com/forum/#!forum/lz4c |
789 | | */ |
790 | | |
791 | | #if defined (__cplusplus) |
792 | | extern "C" { |
793 | | #endif |
794 | | |
795 | | /* ************************************* |
796 | | * Includes |
797 | | ***************************************/ |
798 | | #include <stddef.h> /* size_t */ |
799 | | |
800 | | |
801 | | /* ************************************* |
802 | | * Version |
803 | | ***************************************/ |
804 | | #define ZSTD_VERSION_MAJOR 0 /* for breaking interface changes */ |
805 | | #define ZSTD_VERSION_MINOR 2 /* for new (non-breaking) interface capabilities */ |
806 | | #define ZSTD_VERSION_RELEASE 2 /* for tweaks, bug-fixes, or development */ |
807 | | #define ZSTD_VERSION_NUMBER (ZSTD_VERSION_MAJOR *100*100 + ZSTD_VERSION_MINOR *100 + ZSTD_VERSION_RELEASE) |
808 | | |
809 | | |
810 | | /* ************************************* |
811 | | * Advanced functions |
812 | | ***************************************/ |
813 | | typedef struct ZSTD_CCtx_s ZSTD_CCtx; /* incomplete type */ |
814 | | |
815 | | #if defined (__cplusplus) |
816 | | } |
817 | | #endif |
818 | | /* |
819 | | zstd - standard compression library |
820 | | Header File for static linking only |
821 | | Copyright (C) 2014-2015, Yann Collet. |
822 | | |
823 | | BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php) |
824 | | |
825 | | Redistribution and use in source and binary forms, with or without |
826 | | modification, are permitted provided that the following conditions are |
827 | | met: |
828 | | * Redistributions of source code must retain the above copyright |
829 | | notice, this list of conditions and the following disclaimer. |
830 | | * Redistributions in binary form must reproduce the above |
831 | | copyright notice, this list of conditions and the following disclaimer |
832 | | in the documentation and/or other materials provided with the |
833 | | distribution. |
834 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
835 | | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
836 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
837 | | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
838 | | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
839 | | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
840 | | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
841 | | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
842 | | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
843 | | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
844 | | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
845 | | |
846 | | You can contact the author at : |
847 | | - zstd source repository : https://github.com/Cyan4973/zstd |
848 | | - ztsd public forum : https://groups.google.com/forum/#!forum/lz4c |
849 | | */ |
850 | | |
851 | | /* The objects defined into this file should be considered experimental. |
852 | | * They are not labelled stable, as their prototype may change in the future. |
853 | | * You can use them for tests, provide feedback, or if you can endure risk of future changes. |
854 | | */ |
855 | | |
856 | | #if defined (__cplusplus) |
857 | | extern "C" { |
858 | | #endif |
859 | | |
860 | | /* ************************************* |
861 | | * Streaming functions |
862 | | ***************************************/ |
863 | | |
864 | | typedef struct ZSTDv02_Dctx_s ZSTD_DCtx; |
865 | | |
866 | | /* |
867 | | Use above functions alternatively. |
868 | | ZSTD_nextSrcSizeToDecompress() tells how much bytes to provide as 'srcSize' to ZSTD_decompressContinue(). |
869 | | ZSTD_decompressContinue() will use previous data blocks to improve compression if they are located prior to current block. |
870 | | Result is the number of bytes regenerated within 'dst'. |
871 | | It can be zero, which is not an error; it just means ZSTD_decompressContinue() has decoded some header. |
872 | | */ |
873 | | |
874 | | /* ************************************* |
875 | | * Prefix - version detection |
876 | | ***************************************/ |
877 | 10.3k | #define ZSTD_magicNumber 0xFD2FB522 /* v0.2 (current)*/ |
878 | | |
879 | | |
880 | | #if defined (__cplusplus) |
881 | | } |
882 | | #endif |
883 | | /* ****************************************************************** |
884 | | FSE : Finite State Entropy coder |
885 | | Copyright (C) 2013-2015, Yann Collet. |
886 | | |
887 | | BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php) |
888 | | |
889 | | Redistribution and use in source and binary forms, with or without |
890 | | modification, are permitted provided that the following conditions are |
891 | | met: |
892 | | |
893 | | * Redistributions of source code must retain the above copyright |
894 | | notice, this list of conditions and the following disclaimer. |
895 | | * Redistributions in binary form must reproduce the above |
896 | | copyright notice, this list of conditions and the following disclaimer |
897 | | in the documentation and/or other materials provided with the |
898 | | distribution. |
899 | | |
900 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
901 | | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
902 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
903 | | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
904 | | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
905 | | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
906 | | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
907 | | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
908 | | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
909 | | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
910 | | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
911 | | |
912 | | You can contact the author at : |
913 | | - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy |
914 | | - Public forum : https://groups.google.com/forum/#!forum/lz4c |
915 | | ****************************************************************** */ |
916 | | |
917 | | #ifndef FSE_COMMONDEFS_ONLY |
918 | | |
919 | | /**************************************************************** |
920 | | * Tuning parameters |
921 | | ****************************************************************/ |
922 | | /* MEMORY_USAGE : |
923 | | * Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.) |
924 | | * Increasing memory usage improves compression ratio |
925 | | * Reduced memory usage can improve speed, due to cache effect |
926 | | * Recommended max value is 14, for 16KB, which nicely fits into Intel x86 L1 cache */ |
927 | 30.0k | #define FSE_MAX_MEMORY_USAGE 14 |
928 | | #define FSE_DEFAULT_MEMORY_USAGE 13 |
929 | | |
930 | | /* FSE_MAX_SYMBOL_VALUE : |
931 | | * Maximum symbol value authorized. |
932 | | * Required for proper stack allocation */ |
933 | 9.33k | #define FSE_MAX_SYMBOL_VALUE 255 |
934 | | |
935 | | |
936 | | /**************************************************************** |
937 | | * template functions type & suffix |
938 | | ****************************************************************/ |
939 | 1.47M | #define FSE_FUNCTION_TYPE BYTE |
940 | | #define FSE_FUNCTION_EXTENSION |
941 | | |
942 | | |
943 | | /**************************************************************** |
944 | | * Byte symbol type |
945 | | ****************************************************************/ |
946 | | #endif /* !FSE_COMMONDEFS_ONLY */ |
947 | | |
948 | | |
949 | | /**************************************************************** |
950 | | * Compiler specifics |
951 | | ****************************************************************/ |
952 | | #ifdef _MSC_VER /* Visual Studio */ |
953 | | # define FORCE_INLINE static __forceinline |
954 | | # include <intrin.h> /* For Visual 2005 */ |
955 | | # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ |
956 | | # pragma warning(disable : 4214) /* disable: C4214: non-int bitfields */ |
957 | | #else |
958 | | # if defined (__cplusplus) || defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */ |
959 | | # ifdef __GNUC__ |
960 | | # define FORCE_INLINE static inline __attribute__((always_inline)) |
961 | | # else |
962 | | # define FORCE_INLINE static inline |
963 | | # endif |
964 | | # else |
965 | | # define FORCE_INLINE static |
966 | | # endif /* __STDC_VERSION__ */ |
967 | | #endif |
968 | | |
969 | | |
970 | | /**************************************************************** |
971 | | * Includes |
972 | | ****************************************************************/ |
973 | | #include <stdlib.h> /* malloc, free, qsort */ |
974 | | #include <string.h> /* memcpy, memset */ |
975 | | #include <stdio.h> /* printf (debug) */ |
976 | | |
977 | | /**************************************************************** |
978 | | * Constants |
979 | | *****************************************************************/ |
980 | 30.0k | #define FSE_MAX_TABLELOG (FSE_MAX_MEMORY_USAGE-2) |
981 | | #define FSE_MAX_TABLESIZE (1U<<FSE_MAX_TABLELOG) |
982 | | #define FSE_MAXTABLESIZE_MASK (FSE_MAX_TABLESIZE-1) |
983 | | #define FSE_DEFAULT_TABLELOG (FSE_DEFAULT_MEMORY_USAGE-2) |
984 | 9.05k | #define FSE_MIN_TABLELOG 5 |
985 | | |
986 | 9.05k | #define FSE_TABLELOG_ABSOLUTE_MAX 15 |
987 | | #if FSE_MAX_TABLELOG > FSE_TABLELOG_ABSOLUTE_MAX |
988 | | #error "FSE_MAX_TABLELOG > FSE_TABLELOG_ABSOLUTE_MAX is not supported" |
989 | | #endif |
990 | | |
991 | | |
992 | | /**************************************************************** |
993 | | * Error Management |
994 | | ****************************************************************/ |
995 | | #define FSE_STATIC_ASSERT(c) { enum { FSE_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */ |
996 | | |
997 | | |
998 | | /**************************************************************** |
999 | | * Complex types |
1000 | | ****************************************************************/ |
1001 | | typedef U32 DTable_max_t[FSE_DTABLE_SIZE_U32(FSE_MAX_TABLELOG)]; |
1002 | | |
1003 | | |
1004 | | /**************************************************************** |
1005 | | * Templates |
1006 | | ****************************************************************/ |
1007 | | /* |
1008 | | designed to be included |
1009 | | for type-specific functions (template emulation in C) |
1010 | | Objective is to write these functions only once, for improved maintenance |
1011 | | */ |
1012 | | |
1013 | | /* safety checks */ |
1014 | | #ifndef FSE_FUNCTION_EXTENSION |
1015 | | # error "FSE_FUNCTION_EXTENSION must be defined" |
1016 | | #endif |
1017 | | #ifndef FSE_FUNCTION_TYPE |
1018 | | # error "FSE_FUNCTION_TYPE must be defined" |
1019 | | #endif |
1020 | | |
1021 | | /* Function names */ |
1022 | | #define FSE_CAT(X,Y) X##Y |
1023 | | #define FSE_FUNCTION_NAME(X,Y) FSE_CAT(X,Y) |
1024 | | #define FSE_TYPE_NAME(X,Y) FSE_CAT(X,Y) |
1025 | | |
1026 | | |
1027 | | /* Function templates */ |
1028 | | |
1029 | 8.83k | #define FSE_DECODE_TYPE FSE_decode_t |
1030 | | |
1031 | 8.83k | static U32 FSE_tableStep(U32 tableSize) { return (tableSize>>1) + (tableSize>>3) + 3; } |
1032 | | |
1033 | | static size_t FSE_buildDTable |
1034 | | (FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog) |
1035 | 8.83k | { |
1036 | 8.83k | void* ptr = dt+1; |
1037 | 8.83k | FSE_DECODE_TYPE* const tableDecode = (FSE_DECODE_TYPE*)ptr; |
1038 | 8.83k | FSE_DTableHeader DTableH; |
1039 | 8.83k | const U32 tableSize = 1 << tableLog; |
1040 | 8.83k | const U32 tableMask = tableSize-1; |
1041 | 8.83k | const U32 step = FSE_tableStep(tableSize); |
1042 | 8.83k | U16 symbolNext[FSE_MAX_SYMBOL_VALUE+1]; |
1043 | 8.83k | U32 position = 0; |
1044 | 8.83k | U32 highThreshold = tableSize-1; |
1045 | 8.83k | const S16 largeLimit= (S16)(1 << (tableLog-1)); |
1046 | 8.83k | U32 noLarge = 1; |
1047 | 8.83k | U32 s; |
1048 | | |
1049 | | /* Sanity Checks */ |
1050 | 8.83k | if (maxSymbolValue > FSE_MAX_SYMBOL_VALUE) return ERROR(maxSymbolValue_tooLarge); |
1051 | 8.83k | if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge); |
1052 | | |
1053 | | /* Init, lay down lowprob symbols */ |
1054 | 8.83k | DTableH.tableLog = (U16)tableLog; |
1055 | 110k | for (s=0; s<=maxSymbolValue; s++) |
1056 | 101k | { |
1057 | 101k | if (normalizedCounter[s]==-1) |
1058 | 37.7k | { |
1059 | 37.7k | tableDecode[highThreshold--].symbol = (FSE_FUNCTION_TYPE)s; |
1060 | 37.7k | symbolNext[s] = 1; |
1061 | 37.7k | } |
1062 | 63.9k | else |
1063 | 63.9k | { |
1064 | 63.9k | if (normalizedCounter[s] >= largeLimit) noLarge=0; |
1065 | 63.9k | symbolNext[s] = normalizedCounter[s]; |
1066 | 63.9k | } |
1067 | 101k | } |
1068 | | |
1069 | | /* Spread symbols */ |
1070 | 110k | for (s=0; s<=maxSymbolValue; s++) |
1071 | 101k | { |
1072 | 101k | int i; |
1073 | 1.53M | for (i=0; i<normalizedCounter[s]; i++) |
1074 | 1.43M | { |
1075 | 1.43M | tableDecode[position].symbol = (FSE_FUNCTION_TYPE)s; |
1076 | 1.43M | position = (position + step) & tableMask; |
1077 | 1.47M | while (position > highThreshold) position = (position + step) & tableMask; /* lowprob area */ |
1078 | 1.43M | } |
1079 | 101k | } |
1080 | | |
1081 | 8.83k | if (position!=0) return ERROR(GENERIC); /* position must reach all cells once, otherwise normalizedCounter is incorrect */ |
1082 | | |
1083 | | /* Build Decoding table */ |
1084 | 8.83k | { |
1085 | 8.83k | U32 i; |
1086 | 1.48M | for (i=0; i<tableSize; i++) |
1087 | 1.47M | { |
1088 | 1.47M | FSE_FUNCTION_TYPE symbol = (FSE_FUNCTION_TYPE)(tableDecode[i].symbol); |
1089 | 1.47M | U16 nextState = symbolNext[symbol]++; |
1090 | 1.47M | tableDecode[i].nbBits = (BYTE) (tableLog - BIT_highbit32 ((U32)nextState) ); |
1091 | 1.47M | tableDecode[i].newState = (U16) ( (nextState << tableDecode[i].nbBits) - tableSize); |
1092 | 1.47M | } |
1093 | 8.83k | } |
1094 | | |
1095 | 8.83k | DTableH.fastMode = (U16)noLarge; |
1096 | 8.83k | memcpy(dt, &DTableH, sizeof(DTableH)); /* memcpy(), to avoid strict aliasing warnings */ |
1097 | 8.83k | return 0; |
1098 | 8.83k | } |
1099 | | |
1100 | | |
1101 | | #ifndef FSE_COMMONDEFS_ONLY |
1102 | | /****************************************** |
1103 | | * FSE helper functions |
1104 | | ******************************************/ |
1105 | 10.3k | static unsigned FSE_isError(size_t code) { return ERR_isError(code); } |
1106 | | |
1107 | | |
1108 | | /**************************************************************** |
1109 | | * FSE NCount encoding-decoding |
1110 | | ****************************************************************/ |
1111 | | static short FSE_abs(short a) |
1112 | 82.4k | { |
1113 | 82.4k | return (short)(a<0 ? -a : a); |
1114 | 82.4k | } |
1115 | | |
1116 | | static size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr, |
1117 | | const void* headerBuffer, size_t hbSize) |
1118 | 9.10k | { |
1119 | 9.10k | const BYTE* const istart = (const BYTE*) headerBuffer; |
1120 | 9.10k | const BYTE* const iend = istart + hbSize; |
1121 | 9.10k | const BYTE* ip = istart; |
1122 | 9.10k | int nbBits; |
1123 | 9.10k | int remaining; |
1124 | 9.10k | int threshold; |
1125 | 9.10k | U32 bitStream; |
1126 | 9.10k | int bitCount; |
1127 | 9.10k | unsigned charnum = 0; |
1128 | 9.10k | int previous0 = 0; |
1129 | | |
1130 | 9.10k | if (hbSize < 4) return ERROR(srcSize_wrong); |
1131 | 9.05k | bitStream = MEM_readLE32(ip); |
1132 | 9.05k | nbBits = (bitStream & 0xF) + FSE_MIN_TABLELOG; /* extract tableLog */ |
1133 | 9.05k | if (nbBits > FSE_TABLELOG_ABSOLUTE_MAX) return ERROR(tableLog_tooLarge); |
1134 | 9.03k | bitStream >>= 4; |
1135 | 9.03k | bitCount = 4; |
1136 | 9.03k | *tableLogPtr = nbBits; |
1137 | 9.03k | remaining = (1<<nbBits)+1; |
1138 | 9.03k | threshold = 1<<nbBits; |
1139 | 9.03k | nbBits++; |
1140 | | |
1141 | 91.4k | while ((remaining>1) && (charnum<=*maxSVPtr)) |
1142 | 82.4k | { |
1143 | 82.4k | if (previous0) |
1144 | 9.82k | { |
1145 | 9.82k | unsigned n0 = charnum; |
1146 | 198k | while ((bitStream & 0xFFFF) == 0xFFFF) |
1147 | 188k | { |
1148 | 188k | n0+=24; |
1149 | 188k | if (ip < iend-5) |
1150 | 188k | { |
1151 | 188k | ip+=2; |
1152 | 188k | bitStream = MEM_readLE32(ip) >> bitCount; |
1153 | 188k | } |
1154 | 135 | else |
1155 | 135 | { |
1156 | 135 | bitStream >>= 16; |
1157 | 135 | bitCount+=16; |
1158 | 135 | } |
1159 | 188k | } |
1160 | 15.0k | while ((bitStream & 3) == 3) |
1161 | 5.21k | { |
1162 | 5.21k | n0+=3; |
1163 | 5.21k | bitStream>>=2; |
1164 | 5.21k | bitCount+=2; |
1165 | 5.21k | } |
1166 | 9.82k | n0 += bitStream & 3; |
1167 | 9.82k | bitCount += 2; |
1168 | 9.82k | if (n0 > *maxSVPtr) return ERROR(maxSymbolValue_tooSmall); |
1169 | 43.0k | while (charnum < n0) normalizedCounter[charnum++] = 0; |
1170 | 9.76k | if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) |
1171 | 6.34k | { |
1172 | 6.34k | ip += bitCount>>3; |
1173 | 6.34k | bitCount &= 7; |
1174 | 6.34k | bitStream = MEM_readLE32(ip) >> bitCount; |
1175 | 6.34k | } |
1176 | 3.42k | else |
1177 | 3.42k | bitStream >>= 2; |
1178 | 9.76k | } |
1179 | 82.4k | { |
1180 | 82.4k | const short max = (short)((2*threshold-1)-remaining); |
1181 | 82.4k | short count; |
1182 | | |
1183 | 82.4k | if ((bitStream & (threshold-1)) < (U32)max) |
1184 | 53.9k | { |
1185 | 53.9k | count = (short)(bitStream & (threshold-1)); |
1186 | 53.9k | bitCount += nbBits-1; |
1187 | 53.9k | } |
1188 | 28.4k | else |
1189 | 28.4k | { |
1190 | 28.4k | count = (short)(bitStream & (2*threshold-1)); |
1191 | 28.4k | if (count >= threshold) count -= max; |
1192 | 28.4k | bitCount += nbBits; |
1193 | 28.4k | } |
1194 | | |
1195 | 82.4k | count--; /* extra accuracy */ |
1196 | 82.4k | remaining -= FSE_abs(count); |
1197 | 82.4k | normalizedCounter[charnum++] = count; |
1198 | 82.4k | previous0 = !count; |
1199 | 135k | while (remaining < threshold) |
1200 | 53.3k | { |
1201 | 53.3k | nbBits--; |
1202 | 53.3k | threshold >>= 1; |
1203 | 53.3k | } |
1204 | | |
1205 | 82.4k | { |
1206 | 82.4k | if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) |
1207 | 71.2k | { |
1208 | 71.2k | ip += bitCount>>3; |
1209 | 71.2k | bitCount &= 7; |
1210 | 71.2k | } |
1211 | 11.1k | else |
1212 | 11.1k | { |
1213 | 11.1k | bitCount -= (int)(8 * (iend - 4 - ip)); |
1214 | 11.1k | ip = iend - 4; |
1215 | 11.1k | } |
1216 | 82.4k | bitStream = MEM_readLE32(ip) >> (bitCount & 31); |
1217 | 82.4k | } |
1218 | 82.4k | } |
1219 | 82.4k | } |
1220 | 8.97k | if (remaining != 1) return ERROR(GENERIC); |
1221 | 8.94k | *maxSVPtr = charnum-1; |
1222 | | |
1223 | 8.94k | ip += (bitCount+7)>>3; |
1224 | 8.94k | if ((size_t)(ip-istart) > hbSize) return ERROR(srcSize_wrong); |
1225 | 8.87k | return ip-istart; |
1226 | 8.94k | } |
1227 | | |
1228 | | |
1229 | | /********************************************************* |
1230 | | * Decompression (Byte symbols) |
1231 | | *********************************************************/ |
1232 | | static size_t FSE_buildDTable_rle (FSE_DTable* dt, BYTE symbolValue) |
1233 | 8.70k | { |
1234 | 8.70k | void* ptr = dt; |
1235 | 8.70k | FSE_DTableHeader* const DTableH = (FSE_DTableHeader*)ptr; |
1236 | 8.70k | FSE_decode_t* const cell = (FSE_decode_t*)(ptr) + 1; /* because dt is unsigned */ |
1237 | | |
1238 | 8.70k | DTableH->tableLog = 0; |
1239 | 8.70k | DTableH->fastMode = 0; |
1240 | | |
1241 | 8.70k | cell->newState = 0; |
1242 | 8.70k | cell->symbol = symbolValue; |
1243 | 8.70k | cell->nbBits = 0; |
1244 | | |
1245 | 8.70k | return 0; |
1246 | 8.70k | } |
1247 | | |
1248 | | |
1249 | | static size_t FSE_buildDTable_raw (FSE_DTable* dt, unsigned nbBits) |
1250 | 20.8k | { |
1251 | 20.8k | void* ptr = dt; |
1252 | 20.8k | FSE_DTableHeader* const DTableH = (FSE_DTableHeader*)ptr; |
1253 | 20.8k | FSE_decode_t* const dinfo = (FSE_decode_t*)(ptr) + 1; /* because dt is unsigned */ |
1254 | 20.8k | const unsigned tableSize = 1 << nbBits; |
1255 | 20.8k | const unsigned tableMask = tableSize - 1; |
1256 | 20.8k | const unsigned maxSymbolValue = tableMask; |
1257 | 20.8k | unsigned s; |
1258 | | |
1259 | | /* Sanity checks */ |
1260 | 20.8k | if (nbBits < 1) return ERROR(GENERIC); /* min size */ |
1261 | | |
1262 | | /* Build Decoding Table */ |
1263 | 20.8k | DTableH->tableLog = (U16)nbBits; |
1264 | 20.8k | DTableH->fastMode = 1; |
1265 | 1.66M | for (s=0; s<=maxSymbolValue; s++) |
1266 | 1.64M | { |
1267 | 1.64M | dinfo[s].newState = 0; |
1268 | 1.64M | dinfo[s].symbol = (BYTE)s; |
1269 | 1.64M | dinfo[s].nbBits = (BYTE)nbBits; |
1270 | 1.64M | } |
1271 | | |
1272 | 20.8k | return 0; |
1273 | 20.8k | } |
1274 | | |
1275 | | FORCE_INLINE size_t FSE_decompress_usingDTable_generic( |
1276 | | void* dst, size_t maxDstSize, |
1277 | | const void* cSrc, size_t cSrcSize, |
1278 | | const FSE_DTable* dt, const unsigned fast) |
1279 | 393 | { |
1280 | 393 | BYTE* const ostart = (BYTE*) dst; |
1281 | 393 | BYTE* op = ostart; |
1282 | 393 | BYTE* const omax = op + maxDstSize; |
1283 | 393 | BYTE* const olimit = omax-3; |
1284 | | |
1285 | 393 | BIT_DStream_t bitD; |
1286 | 393 | FSE_DState_t state1; |
1287 | 393 | FSE_DState_t state2; |
1288 | 393 | size_t errorCode; |
1289 | | |
1290 | | /* Init */ |
1291 | 393 | errorCode = BIT_initDStream(&bitD, cSrc, cSrcSize); /* replaced last arg by maxCompressed Size */ |
1292 | 393 | if (FSE_isError(errorCode)) return errorCode; |
1293 | | |
1294 | 373 | FSE_initDState(&state1, &bitD, dt); |
1295 | 373 | FSE_initDState(&state2, &bitD, dt); |
1296 | | |
1297 | 48.0k | #define FSE_GETSYMBOL(statePtr) fast ? FSE_decodeSymbolFast(statePtr, &bitD) : FSE_decodeSymbol(statePtr, &bitD) |
1298 | | |
1299 | | /* 4 symbols per loop */ |
1300 | 7.42k | for ( ; (BIT_reloadDStream(&bitD)==BIT_DStream_unfinished) && (op<olimit) ; op+=4) |
1301 | 7.05k | { |
1302 | 7.05k | op[0] = FSE_GETSYMBOL(&state1); |
1303 | | |
1304 | 7.05k | if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */ |
1305 | 0 | BIT_reloadDStream(&bitD); |
1306 | | |
1307 | 7.05k | op[1] = FSE_GETSYMBOL(&state2); |
1308 | | |
1309 | 7.05k | if (FSE_MAX_TABLELOG*4+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */ |
1310 | 0 | { if (BIT_reloadDStream(&bitD) > BIT_DStream_unfinished) { op+=2; break; } } |
1311 | | |
1312 | 7.05k | op[2] = FSE_GETSYMBOL(&state1); |
1313 | | |
1314 | 7.05k | if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */ |
1315 | 0 | BIT_reloadDStream(&bitD); |
1316 | | |
1317 | 7.05k | op[3] = FSE_GETSYMBOL(&state2); |
1318 | 7.05k | } |
1319 | | |
1320 | | /* tail */ |
1321 | | /* note : BIT_reloadDStream(&bitD) >= FSE_DStream_partiallyFilled; Ends at exactly BIT_DStream_completed */ |
1322 | 10.1k | while (1) |
1323 | 10.1k | { |
1324 | 10.1k | if ( (BIT_reloadDStream(&bitD)>BIT_DStream_completed) || (op==omax) || (BIT_endOfDStream(&bitD) && (fast || FSE_endOfDState(&state1))) ) |
1325 | 187 | break; |
1326 | | |
1327 | 10.0k | *op++ = FSE_GETSYMBOL(&state1); |
1328 | | |
1329 | 10.0k | if ( (BIT_reloadDStream(&bitD)>BIT_DStream_completed) || (op==omax) || (BIT_endOfDStream(&bitD) && (fast || FSE_endOfDState(&state2))) ) |
1330 | 186 | break; |
1331 | | |
1332 | 9.82k | *op++ = FSE_GETSYMBOL(&state2); |
1333 | 9.82k | } |
1334 | | |
1335 | | /* end ? */ |
1336 | 373 | if (BIT_endOfDStream(&bitD) && FSE_endOfDState(&state1) && FSE_endOfDState(&state2)) |
1337 | 152 | return op-ostart; |
1338 | | |
1339 | 221 | if (op==omax) return ERROR(dstSize_tooSmall); /* dst buffer is full, but cSrc unfinished */ |
1340 | | |
1341 | 163 | return ERROR(corruption_detected); |
1342 | 221 | } |
1343 | | |
1344 | | |
1345 | | static size_t FSE_decompress_usingDTable(void* dst, size_t originalSize, |
1346 | | const void* cSrc, size_t cSrcSize, |
1347 | | const FSE_DTable* dt) |
1348 | 393 | { |
1349 | 393 | FSE_DTableHeader DTableH; |
1350 | 393 | memcpy(&DTableH, dt, sizeof(DTableH)); |
1351 | | |
1352 | | /* select fast mode (static) */ |
1353 | 393 | if (DTableH.fastMode) return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 1); |
1354 | 269 | return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 0); |
1355 | 393 | } |
1356 | | |
1357 | | |
1358 | | static size_t FSE_decompress(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize) |
1359 | 498 | { |
1360 | 498 | const BYTE* const istart = (const BYTE*)cSrc; |
1361 | 498 | const BYTE* ip = istart; |
1362 | 498 | short counting[FSE_MAX_SYMBOL_VALUE+1]; |
1363 | 498 | DTable_max_t dt; /* Static analyzer seems unable to understand this table will be properly initialized later */ |
1364 | 498 | unsigned tableLog; |
1365 | 498 | unsigned maxSymbolValue = FSE_MAX_SYMBOL_VALUE; |
1366 | 498 | size_t errorCode; |
1367 | | |
1368 | 498 | if (cSrcSize<2) return ERROR(srcSize_wrong); /* too small input size */ |
1369 | | |
1370 | | /* normal FSE decoding mode */ |
1371 | 482 | errorCode = FSE_readNCount (counting, &maxSymbolValue, &tableLog, istart, cSrcSize); |
1372 | 482 | if (FSE_isError(errorCode)) return errorCode; |
1373 | 401 | if (errorCode >= cSrcSize) return ERROR(srcSize_wrong); /* too small input size */ |
1374 | 395 | ip += errorCode; |
1375 | 395 | cSrcSize -= errorCode; |
1376 | | |
1377 | 395 | errorCode = FSE_buildDTable (dt, counting, maxSymbolValue, tableLog); |
1378 | 395 | if (FSE_isError(errorCode)) return errorCode; |
1379 | | |
1380 | | /* always return, even if it is an error code */ |
1381 | 393 | return FSE_decompress_usingDTable (dst, maxDstSize, ip, cSrcSize, dt); |
1382 | 395 | } |
1383 | | |
1384 | | |
1385 | | |
1386 | | #endif /* FSE_COMMONDEFS_ONLY */ |
1387 | | /* ****************************************************************** |
1388 | | Huff0 : Huffman coder, part of New Generation Entropy library |
1389 | | Copyright (C) 2013-2015, Yann Collet. |
1390 | | |
1391 | | BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php) |
1392 | | |
1393 | | Redistribution and use in source and binary forms, with or without |
1394 | | modification, are permitted provided that the following conditions are |
1395 | | met: |
1396 | | |
1397 | | * Redistributions of source code must retain the above copyright |
1398 | | notice, this list of conditions and the following disclaimer. |
1399 | | * Redistributions in binary form must reproduce the above |
1400 | | copyright notice, this list of conditions and the following disclaimer |
1401 | | in the documentation and/or other materials provided with the |
1402 | | distribution. |
1403 | | |
1404 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
1405 | | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
1406 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
1407 | | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
1408 | | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
1409 | | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
1410 | | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
1411 | | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
1412 | | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
1413 | | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
1414 | | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
1415 | | |
1416 | | You can contact the author at : |
1417 | | - FSE+Huff0 source repository : https://github.com/Cyan4973/FiniteStateEntropy |
1418 | | - Public forum : https://groups.google.com/forum/#!forum/lz4c |
1419 | | ****************************************************************** */ |
1420 | | |
1421 | | /**************************************************************** |
1422 | | * Compiler specifics |
1423 | | ****************************************************************/ |
1424 | | #if defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) |
1425 | | /* inline is defined */ |
1426 | | #elif defined(_MSC_VER) |
1427 | | # define inline __inline |
1428 | | #else |
1429 | | # define inline /* disable inline */ |
1430 | | #endif |
1431 | | |
1432 | | |
1433 | | #ifdef _MSC_VER /* Visual Studio */ |
1434 | | # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ |
1435 | | #endif |
1436 | | |
1437 | | |
1438 | | /**************************************************************** |
1439 | | * Includes |
1440 | | ****************************************************************/ |
1441 | | #include <stdlib.h> /* malloc, free, qsort */ |
1442 | | #include <string.h> /* memcpy, memset */ |
1443 | | #include <stdio.h> /* printf (debug) */ |
1444 | | |
1445 | | /**************************************************************** |
1446 | | * Error Management |
1447 | | ****************************************************************/ |
1448 | 2.29k | #define HUF_STATIC_ASSERT(c) { enum { HUF_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */ |
1449 | | |
1450 | | |
1451 | | /****************************************** |
1452 | | * Helper functions |
1453 | | ******************************************/ |
1454 | 14.1k | static unsigned HUF_isError(size_t code) { return ERR_isError(code); } |
1455 | | |
1456 | 210k | #define HUF_ABSOLUTEMAX_TABLELOG 16 /* absolute limit of HUF_MAX_TABLELOG. Beyond that value, code does not work */ |
1457 | 0 | #define HUF_MAX_TABLELOG 12 /* max configured tableLog (for static allocation); can be modified up to HUF_ABSOLUTEMAX_TABLELOG */ |
1458 | | #define HUF_DEFAULT_TABLELOG HUF_MAX_TABLELOG /* tableLog by default, when not specified */ |
1459 | 2.29k | #define HUF_MAX_SYMBOL_VALUE 255 |
1460 | | #if (HUF_MAX_TABLELOG > HUF_ABSOLUTEMAX_TABLELOG) |
1461 | | # error "HUF_MAX_TABLELOG is too large !" |
1462 | | #endif |
1463 | | |
1464 | | |
1465 | | |
1466 | | /********************************************************* |
1467 | | * Huff0 : Huffman block decompression |
1468 | | *********************************************************/ |
1469 | | typedef struct { BYTE byte; BYTE nbBits; } HUF_DEltX2; /* single-symbol decoding */ |
1470 | | |
1471 | | typedef struct { U16 sequence; BYTE nbBits; BYTE length; } HUF_DEltX4; /* double-symbols decoding */ |
1472 | | |
1473 | | typedef struct { BYTE symbol; BYTE weight; } sortedSymbol_t; |
1474 | | |
1475 | | /*! HUF_readStats |
1476 | | Read compact Huffman tree, saved by HUF_writeCTable |
1477 | | @huffWeight : destination buffer |
1478 | | @return : size read from `src` |
1479 | | */ |
1480 | | static size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats, |
1481 | | U32* nbSymbolsPtr, U32* tableLogPtr, |
1482 | | const void* src, size_t srcSize) |
1483 | 2.29k | { |
1484 | 2.29k | U32 weightTotal; |
1485 | 2.29k | U32 tableLog; |
1486 | 2.29k | const BYTE* ip = (const BYTE*) src; |
1487 | 2.29k | size_t iSize; |
1488 | 2.29k | size_t oSize; |
1489 | 2.29k | U32 n; |
1490 | | |
1491 | 2.29k | if (!srcSize) return ERROR(srcSize_wrong); |
1492 | 2.27k | iSize = ip[0]; |
1493 | | //memset(huffWeight, 0, hwSize); /* is not necessary, even though some analyzer complain ... */ |
1494 | | |
1495 | 2.27k | if (iSize >= 128) /* special header */ |
1496 | 1.77k | { |
1497 | 1.77k | if (iSize >= (242)) /* RLE */ |
1498 | 1.63k | { |
1499 | 1.63k | static int l[14] = { 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64, 127, 128 }; |
1500 | 1.63k | oSize = l[iSize-242]; |
1501 | 1.63k | memset(huffWeight, 1, hwSize); |
1502 | 1.63k | iSize = 0; |
1503 | 1.63k | } |
1504 | 134 | else /* Incompressible */ |
1505 | 134 | { |
1506 | 134 | oSize = iSize - 127; |
1507 | 134 | iSize = ((oSize+1)/2); |
1508 | 134 | if (iSize+1 > srcSize) return ERROR(srcSize_wrong); |
1509 | 128 | if (oSize >= hwSize) return ERROR(corruption_detected); |
1510 | 128 | ip += 1; |
1511 | 3.46k | for (n=0; n<oSize; n+=2) |
1512 | 3.33k | { |
1513 | 3.33k | huffWeight[n] = ip[n/2] >> 4; |
1514 | 3.33k | huffWeight[n+1] = ip[n/2] & 15; |
1515 | 3.33k | } |
1516 | 128 | } |
1517 | 1.77k | } |
1518 | 503 | else /* header compressed with FSE (normal case) */ |
1519 | 503 | { |
1520 | 503 | if (iSize+1 > srcSize) return ERROR(srcSize_wrong); |
1521 | 498 | oSize = FSE_decompress(huffWeight, hwSize-1, ip+1, iSize); /* max (hwSize-1) values decoded, as last one is implied */ |
1522 | 498 | if (FSE_isError(oSize)) return oSize; |
1523 | 498 | } |
1524 | | |
1525 | | /* collect weight stats */ |
1526 | 1.91k | memset(rankStats, 0, (HUF_ABSOLUTEMAX_TABLELOG + 1) * sizeof(U32)); |
1527 | 1.91k | weightTotal = 0; |
1528 | 208k | for (n=0; n<oSize; n++) |
1529 | 206k | { |
1530 | 206k | if (huffWeight[n] >= HUF_ABSOLUTEMAX_TABLELOG) return ERROR(corruption_detected); |
1531 | 206k | rankStats[huffWeight[n]]++; |
1532 | 206k | weightTotal += (1 << huffWeight[n]) >> 1; |
1533 | 206k | } |
1534 | 1.91k | if (weightTotal == 0) return ERROR(corruption_detected); |
1535 | | |
1536 | | /* get last non-null symbol weight (implied, total must be 2^n) */ |
1537 | 1.90k | tableLog = BIT_highbit32(weightTotal) + 1; |
1538 | 1.90k | if (tableLog > HUF_ABSOLUTEMAX_TABLELOG) return ERROR(corruption_detected); |
1539 | 1.88k | { |
1540 | 1.88k | U32 total = 1 << tableLog; |
1541 | 1.88k | U32 rest = total - weightTotal; |
1542 | 1.88k | U32 verif = 1 << BIT_highbit32(rest); |
1543 | 1.88k | U32 lastWeight = BIT_highbit32(rest) + 1; |
1544 | 1.88k | if (verif != rest) return ERROR(corruption_detected); /* last value must be a clean power of 2 */ |
1545 | 1.86k | huffWeight[oSize] = (BYTE)lastWeight; |
1546 | 1.86k | rankStats[lastWeight]++; |
1547 | 1.86k | } |
1548 | | |
1549 | | /* check tree construction validity */ |
1550 | 1.86k | if ((rankStats[1] < 2) || (rankStats[1] & 1)) return ERROR(corruption_detected); /* by construction : at least 2 elts of rank 1, must be even */ |
1551 | | |
1552 | | /* results */ |
1553 | 1.86k | *nbSymbolsPtr = (U32)(oSize+1); |
1554 | 1.86k | *tableLogPtr = tableLog; |
1555 | 1.86k | return iSize+1; |
1556 | 1.86k | } |
1557 | | |
1558 | | |
1559 | | /**************************/ |
1560 | | /* single-symbol decoding */ |
1561 | | /**************************/ |
1562 | | |
1563 | | static size_t HUF_readDTableX2 (U16* DTable, const void* src, size_t srcSize) |
1564 | 1.99k | { |
1565 | 1.99k | BYTE huffWeight[HUF_MAX_SYMBOL_VALUE + 1]; |
1566 | 1.99k | U32 rankVal[HUF_ABSOLUTEMAX_TABLELOG + 1]; /* large enough for values from 0 to 16 */ |
1567 | 1.99k | U32 tableLog = 0; |
1568 | 1.99k | const BYTE* ip = (const BYTE*) src; |
1569 | 1.99k | size_t iSize = ip[0]; |
1570 | 1.99k | U32 nbSymbols = 0; |
1571 | 1.99k | U32 n; |
1572 | 1.99k | U32 nextRankStart; |
1573 | 1.99k | void* ptr = DTable+1; |
1574 | 1.99k | HUF_DEltX2* const dt = (HUF_DEltX2*)ptr; |
1575 | | |
1576 | 1.99k | HUF_STATIC_ASSERT(sizeof(HUF_DEltX2) == sizeof(U16)); /* if compilation fails here, assertion is false */ |
1577 | | //memset(huffWeight, 0, sizeof(huffWeight)); /* is not necessary, even though some analyzer complain ... */ |
1578 | | |
1579 | 1.99k | iSize = HUF_readStats(huffWeight, HUF_MAX_SYMBOL_VALUE + 1, rankVal, &nbSymbols, &tableLog, src, srcSize); |
1580 | 1.99k | if (HUF_isError(iSize)) return iSize; |
1581 | | |
1582 | | /* check result */ |
1583 | 1.57k | if (tableLog > DTable[0]) return ERROR(tableLog_tooLarge); /* DTable is too small */ |
1584 | 1.57k | DTable[0] = (U16)tableLog; /* maybe should separate sizeof DTable, as allocated, from used size of DTable, in case of DTable re-use */ |
1585 | | |
1586 | | /* Prepare ranks */ |
1587 | 1.57k | nextRankStart = 0; |
1588 | 13.1k | for (n=1; n<=tableLog; n++) |
1589 | 11.5k | { |
1590 | 11.5k | U32 current = nextRankStart; |
1591 | 11.5k | nextRankStart += (rankVal[n] << (n-1)); |
1592 | 11.5k | rankVal[n] = current; |
1593 | 11.5k | } |
1594 | | |
1595 | | /* fill DTable */ |
1596 | 173k | for (n=0; n<nbSymbols; n++) |
1597 | 172k | { |
1598 | 172k | const U32 w = huffWeight[n]; |
1599 | 172k | const U32 length = (1 << w) >> 1; |
1600 | 172k | U32 i; |
1601 | 172k | HUF_DEltX2 D; |
1602 | 172k | D.byte = (BYTE)n; D.nbBits = (BYTE)(tableLog + 1 - w); |
1603 | 545k | for (i = rankVal[w]; i < rankVal[w] + length; i++) |
1604 | 373k | dt[i] = D; |
1605 | 172k | rankVal[w] += length; |
1606 | 172k | } |
1607 | | |
1608 | 1.57k | return iSize; |
1609 | 1.57k | } |
1610 | | |
1611 | | static BYTE HUF_decodeSymbolX2(BIT_DStream_t* Dstream, const HUF_DEltX2* dt, const U32 dtLog) |
1612 | 12.4M | { |
1613 | 12.4M | const size_t val = BIT_lookBitsFast(Dstream, dtLog); /* note : dtLog >= 1 */ |
1614 | 12.4M | const BYTE c = dt[val].byte; |
1615 | 12.4M | BIT_skipBits(Dstream, dt[val].nbBits); |
1616 | 12.4M | return c; |
1617 | 12.4M | } |
1618 | | |
1619 | | #define HUF_DECODE_SYMBOLX2_0(ptr, DStreamPtr) \ |
1620 | 12.4M | *ptr++ = HUF_decodeSymbolX2(DStreamPtr, dt, dtLog) |
1621 | | |
1622 | | #define HUF_DECODE_SYMBOLX2_1(ptr, DStreamPtr) \ |
1623 | 238k | if (MEM_64bits() || (HUF_MAX_TABLELOG<=12)) \ |
1624 | 238k | HUF_DECODE_SYMBOLX2_0(ptr, DStreamPtr) |
1625 | | |
1626 | | #define HUF_DECODE_SYMBOLX2_2(ptr, DStreamPtr) \ |
1627 | 477k | if (MEM_64bits()) \ |
1628 | 477k | HUF_DECODE_SYMBOLX2_0(ptr, DStreamPtr) |
1629 | | |
1630 | | static inline size_t HUF_decodeStreamX2(BYTE* p, BIT_DStream_t* const bitDPtr, BYTE* const pEnd, const HUF_DEltX2* const dt, const U32 dtLog) |
1631 | 5.58k | { |
1632 | 5.58k | BYTE* const pStart = p; |
1633 | | |
1634 | | /* up to 4 symbols at a time */ |
1635 | 192k | while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) && (p <= pEnd-4)) |
1636 | 186k | { |
1637 | 186k | HUF_DECODE_SYMBOLX2_2(p, bitDPtr); |
1638 | 186k | HUF_DECODE_SYMBOLX2_1(p, bitDPtr); |
1639 | 186k | HUF_DECODE_SYMBOLX2_2(p, bitDPtr); |
1640 | 186k | HUF_DECODE_SYMBOLX2_0(p, bitDPtr); |
1641 | 186k | } |
1642 | | |
1643 | | /* closer to the end */ |
1644 | 5.85k | while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) && (p < pEnd)) |
1645 | 273 | HUF_DECODE_SYMBOLX2_0(p, bitDPtr); |
1646 | | |
1647 | | /* no more data to retrieve from bitstream, hence no need to reload */ |
1648 | 11.5M | while (p < pEnd) |
1649 | 11.5M | HUF_DECODE_SYMBOLX2_0(p, bitDPtr); |
1650 | | |
1651 | 5.58k | return pEnd-pStart; |
1652 | 5.58k | } |
1653 | | |
1654 | | |
1655 | | static size_t HUF_decompress4X2_usingDTable( |
1656 | | void* dst, size_t dstSize, |
1657 | | const void* cSrc, size_t cSrcSize, |
1658 | | const U16* DTable) |
1659 | 1.55k | { |
1660 | 1.55k | if (cSrcSize < 10) return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */ |
1661 | | |
1662 | 1.51k | { |
1663 | 1.51k | const BYTE* const istart = (const BYTE*) cSrc; |
1664 | 1.51k | BYTE* const ostart = (BYTE*) dst; |
1665 | 1.51k | BYTE* const oend = ostart + dstSize; |
1666 | | |
1667 | 1.51k | const void* ptr = DTable; |
1668 | 1.51k | const HUF_DEltX2* const dt = ((const HUF_DEltX2*)ptr) +1; |
1669 | 1.51k | const U32 dtLog = DTable[0]; |
1670 | 1.51k | size_t errorCode; |
1671 | | |
1672 | | /* Init */ |
1673 | 1.51k | BIT_DStream_t bitD1; |
1674 | 1.51k | BIT_DStream_t bitD2; |
1675 | 1.51k | BIT_DStream_t bitD3; |
1676 | 1.51k | BIT_DStream_t bitD4; |
1677 | 1.51k | const size_t length1 = MEM_readLE16(istart); |
1678 | 1.51k | const size_t length2 = MEM_readLE16(istart+2); |
1679 | 1.51k | const size_t length3 = MEM_readLE16(istart+4); |
1680 | 1.51k | size_t length4; |
1681 | 1.51k | const BYTE* const istart1 = istart + 6; /* jumpTable */ |
1682 | 1.51k | const BYTE* const istart2 = istart1 + length1; |
1683 | 1.51k | const BYTE* const istart3 = istart2 + length2; |
1684 | 1.51k | const BYTE* const istart4 = istart3 + length3; |
1685 | 1.51k | const size_t segmentSize = (dstSize+3) / 4; |
1686 | 1.51k | BYTE* const opStart2 = ostart + segmentSize; |
1687 | 1.51k | BYTE* const opStart3 = opStart2 + segmentSize; |
1688 | 1.51k | BYTE* const opStart4 = opStart3 + segmentSize; |
1689 | 1.51k | BYTE* op1 = ostart; |
1690 | 1.51k | BYTE* op2 = opStart2; |
1691 | 1.51k | BYTE* op3 = opStart3; |
1692 | 1.51k | BYTE* op4 = opStart4; |
1693 | 1.51k | U32 endSignal; |
1694 | | |
1695 | 1.51k | length4 = cSrcSize - (length1 + length2 + length3 + 6); |
1696 | 1.51k | if (length4 > cSrcSize) return ERROR(corruption_detected); /* overflow */ |
1697 | 1.47k | errorCode = BIT_initDStream(&bitD1, istart1, length1); |
1698 | 1.47k | if (HUF_isError(errorCode)) return errorCode; |
1699 | 1.46k | errorCode = BIT_initDStream(&bitD2, istart2, length2); |
1700 | 1.46k | if (HUF_isError(errorCode)) return errorCode; |
1701 | 1.44k | errorCode = BIT_initDStream(&bitD3, istart3, length3); |
1702 | 1.44k | if (HUF_isError(errorCode)) return errorCode; |
1703 | 1.42k | errorCode = BIT_initDStream(&bitD4, istart4, length4); |
1704 | 1.42k | if (HUF_isError(errorCode)) return errorCode; |
1705 | | |
1706 | | /* 16-32 symbols per loop (4-8 symbols per stream) */ |
1707 | 1.39k | endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4); |
1708 | 14.4k | for ( ; (endSignal==BIT_DStream_unfinished) && (op4<(oend-7)) ; ) |
1709 | 13.0k | { |
1710 | 13.0k | HUF_DECODE_SYMBOLX2_2(op1, &bitD1); |
1711 | 13.0k | HUF_DECODE_SYMBOLX2_2(op2, &bitD2); |
1712 | 13.0k | HUF_DECODE_SYMBOLX2_2(op3, &bitD3); |
1713 | 13.0k | HUF_DECODE_SYMBOLX2_2(op4, &bitD4); |
1714 | 13.0k | HUF_DECODE_SYMBOLX2_1(op1, &bitD1); |
1715 | 13.0k | HUF_DECODE_SYMBOLX2_1(op2, &bitD2); |
1716 | 13.0k | HUF_DECODE_SYMBOLX2_1(op3, &bitD3); |
1717 | 13.0k | HUF_DECODE_SYMBOLX2_1(op4, &bitD4); |
1718 | 13.0k | HUF_DECODE_SYMBOLX2_2(op1, &bitD1); |
1719 | 13.0k | HUF_DECODE_SYMBOLX2_2(op2, &bitD2); |
1720 | 13.0k | HUF_DECODE_SYMBOLX2_2(op3, &bitD3); |
1721 | 13.0k | HUF_DECODE_SYMBOLX2_2(op4, &bitD4); |
1722 | 13.0k | HUF_DECODE_SYMBOLX2_0(op1, &bitD1); |
1723 | 13.0k | HUF_DECODE_SYMBOLX2_0(op2, &bitD2); |
1724 | 13.0k | HUF_DECODE_SYMBOLX2_0(op3, &bitD3); |
1725 | 13.0k | HUF_DECODE_SYMBOLX2_0(op4, &bitD4); |
1726 | | |
1727 | 13.0k | endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4); |
1728 | 13.0k | } |
1729 | | |
1730 | | /* check corruption */ |
1731 | 1.39k | if (op1 > opStart2) return ERROR(corruption_detected); |
1732 | 1.39k | if (op2 > opStart3) return ERROR(corruption_detected); |
1733 | 1.39k | if (op3 > opStart4) return ERROR(corruption_detected); |
1734 | | /* note : op4 supposed already verified within main loop */ |
1735 | | |
1736 | | /* finish bitStreams one by one */ |
1737 | 1.39k | HUF_decodeStreamX2(op1, &bitD1, opStart2, dt, dtLog); |
1738 | 1.39k | HUF_decodeStreamX2(op2, &bitD2, opStart3, dt, dtLog); |
1739 | 1.39k | HUF_decodeStreamX2(op3, &bitD3, opStart4, dt, dtLog); |
1740 | 1.39k | HUF_decodeStreamX2(op4, &bitD4, oend, dt, dtLog); |
1741 | | |
1742 | | /* check */ |
1743 | 1.39k | endSignal = BIT_endOfDStream(&bitD1) & BIT_endOfDStream(&bitD2) & BIT_endOfDStream(&bitD3) & BIT_endOfDStream(&bitD4); |
1744 | 1.39k | if (!endSignal) return ERROR(corruption_detected); |
1745 | | |
1746 | | /* decoded size */ |
1747 | 1.11k | return dstSize; |
1748 | 1.39k | } |
1749 | 1.39k | } |
1750 | | |
1751 | | |
1752 | | static size_t HUF_decompress4X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize) |
1753 | 1.99k | { |
1754 | 1.99k | HUF_CREATE_STATIC_DTABLEX2(DTable, HUF_MAX_TABLELOG); |
1755 | 1.99k | const BYTE* ip = (const BYTE*) cSrc; |
1756 | 1.99k | size_t errorCode; |
1757 | | |
1758 | 1.99k | errorCode = HUF_readDTableX2 (DTable, cSrc, cSrcSize); |
1759 | 1.99k | if (HUF_isError(errorCode)) return errorCode; |
1760 | 1.57k | if (errorCode >= cSrcSize) return ERROR(srcSize_wrong); |
1761 | 1.55k | ip += errorCode; |
1762 | 1.55k | cSrcSize -= errorCode; |
1763 | | |
1764 | 1.55k | return HUF_decompress4X2_usingDTable (dst, dstSize, ip, cSrcSize, DTable); |
1765 | 1.57k | } |
1766 | | |
1767 | | |
1768 | | /***************************/ |
1769 | | /* double-symbols decoding */ |
1770 | | /***************************/ |
1771 | | |
1772 | | static void HUF_fillDTableX4Level2(HUF_DEltX4* DTable, U32 sizeLog, const U32 consumed, |
1773 | | const U32* rankValOrigin, const int minWeight, |
1774 | | const sortedSymbol_t* sortedSymbols, const U32 sortedListSize, |
1775 | | U32 nbBitsBaseline, U16 baseSeq) |
1776 | 20.0k | { |
1777 | 20.0k | HUF_DEltX4 DElt; |
1778 | 20.0k | U32 rankVal[HUF_ABSOLUTEMAX_TABLELOG + 1]; |
1779 | 20.0k | U32 s; |
1780 | | |
1781 | | /* get pre-calculated rankVal */ |
1782 | 20.0k | memcpy(rankVal, rankValOrigin, sizeof(rankVal)); |
1783 | | |
1784 | | /* fill skipped values */ |
1785 | 20.0k | if (minWeight>1) |
1786 | 19.0k | { |
1787 | 19.0k | U32 i, skipSize = rankVal[minWeight]; |
1788 | 19.0k | MEM_writeLE16(&(DElt.sequence), baseSeq); |
1789 | 19.0k | DElt.nbBits = (BYTE)(consumed); |
1790 | 19.0k | DElt.length = 1; |
1791 | 178k | for (i = 0; i < skipSize; i++) |
1792 | 159k | DTable[i] = DElt; |
1793 | 19.0k | } |
1794 | | |
1795 | | /* fill DTable */ |
1796 | 120k | for (s=0; s<sortedListSize; s++) /* note : sortedSymbols already skipped */ |
1797 | 100k | { |
1798 | 100k | const U32 symbol = sortedSymbols[s].symbol; |
1799 | 100k | const U32 weight = sortedSymbols[s].weight; |
1800 | 100k | const U32 nbBits = nbBitsBaseline - weight; |
1801 | 100k | const U32 length = 1 << (sizeLog-nbBits); |
1802 | 100k | const U32 start = rankVal[weight]; |
1803 | 100k | U32 i = start; |
1804 | 100k | const U32 end = start + length; |
1805 | | |
1806 | 100k | MEM_writeLE16(&(DElt.sequence), (U16)(baseSeq + (symbol << 8))); |
1807 | 100k | DElt.nbBits = (BYTE)(nbBits + consumed); |
1808 | 100k | DElt.length = 2; |
1809 | 937k | do { DTable[i++] = DElt; } while (i<end); /* since length >= 1 */ |
1810 | | |
1811 | 100k | rankVal[weight] += length; |
1812 | 100k | } |
1813 | 20.0k | } |
1814 | | |
1815 | | typedef U32 rankVal_t[HUF_ABSOLUTEMAX_TABLELOG][HUF_ABSOLUTEMAX_TABLELOG + 1]; |
1816 | | |
1817 | | static void HUF_fillDTableX4(HUF_DEltX4* DTable, const U32 targetLog, |
1818 | | const sortedSymbol_t* sortedList, const U32 sortedListSize, |
1819 | | const U32* rankStart, rankVal_t rankValOrigin, const U32 maxWeight, |
1820 | | const U32 nbBitsBaseline) |
1821 | 284 | { |
1822 | 284 | U32 rankVal[HUF_ABSOLUTEMAX_TABLELOG + 1]; |
1823 | 284 | const int scaleLog = nbBitsBaseline - targetLog; /* note : targetLog >= srcLog, hence scaleLog <= 1 */ |
1824 | 284 | const U32 minBits = nbBitsBaseline - maxWeight; |
1825 | 284 | U32 s; |
1826 | | |
1827 | 284 | memcpy(rankVal, rankValOrigin, sizeof(rankVal)); |
1828 | | |
1829 | | /* fill DTable */ |
1830 | 24.2k | for (s=0; s<sortedListSize; s++) |
1831 | 23.9k | { |
1832 | 23.9k | const U16 symbol = sortedList[s].symbol; |
1833 | 23.9k | const U32 weight = sortedList[s].weight; |
1834 | 23.9k | const U32 nbBits = nbBitsBaseline - weight; |
1835 | 23.9k | const U32 start = rankVal[weight]; |
1836 | 23.9k | const U32 length = 1 << (targetLog-nbBits); |
1837 | | |
1838 | 23.9k | if (targetLog-nbBits >= minBits) /* enough room for a second symbol */ |
1839 | 20.0k | { |
1840 | 20.0k | U32 sortedRank; |
1841 | 20.0k | int minWeight = nbBits + scaleLog; |
1842 | 20.0k | if (minWeight < 1) minWeight = 1; |
1843 | 20.0k | sortedRank = rankStart[minWeight]; |
1844 | 20.0k | HUF_fillDTableX4Level2(DTable+start, targetLog-nbBits, nbBits, |
1845 | 20.0k | rankValOrigin[nbBits], minWeight, |
1846 | 20.0k | sortedList+sortedRank, sortedListSize-sortedRank, |
1847 | 20.0k | nbBitsBaseline, symbol); |
1848 | 20.0k | } |
1849 | 3.86k | else |
1850 | 3.86k | { |
1851 | 3.86k | U32 i; |
1852 | 3.86k | const U32 end = start + length; |
1853 | 3.86k | HUF_DEltX4 DElt; |
1854 | | |
1855 | 3.86k | MEM_writeLE16(&(DElt.sequence), symbol); |
1856 | 3.86k | DElt.nbBits = (BYTE)(nbBits); |
1857 | 3.86k | DElt.length = 1; |
1858 | 70.6k | for (i = start; i < end; i++) |
1859 | 66.7k | DTable[i] = DElt; |
1860 | 3.86k | } |
1861 | 23.9k | rankVal[weight] += length; |
1862 | 23.9k | } |
1863 | 284 | } |
1864 | | |
1865 | | static size_t HUF_readDTableX4 (U32* DTable, const void* src, size_t srcSize) |
1866 | 301 | { |
1867 | 301 | BYTE weightList[HUF_MAX_SYMBOL_VALUE + 1]; |
1868 | 301 | sortedSymbol_t sortedSymbol[HUF_MAX_SYMBOL_VALUE + 1]; |
1869 | 301 | U32 rankStats[HUF_ABSOLUTEMAX_TABLELOG + 1] = { 0 }; |
1870 | 301 | U32 rankStart0[HUF_ABSOLUTEMAX_TABLELOG + 2] = { 0 }; |
1871 | 301 | U32* const rankStart = rankStart0+1; |
1872 | 301 | rankVal_t rankVal; |
1873 | 301 | U32 tableLog, maxW, sizeOfSort, nbSymbols; |
1874 | 301 | const U32 memLog = DTable[0]; |
1875 | 301 | const BYTE* ip = (const BYTE*) src; |
1876 | 301 | size_t iSize = ip[0]; |
1877 | 301 | void* ptr = DTable; |
1878 | 301 | HUF_DEltX4* const dt = ((HUF_DEltX4*)ptr) + 1; |
1879 | | |
1880 | 301 | HUF_STATIC_ASSERT(sizeof(HUF_DEltX4) == sizeof(U32)); /* if compilation fails here, assertion is false */ |
1881 | 301 | if (memLog > HUF_ABSOLUTEMAX_TABLELOG) return ERROR(tableLog_tooLarge); |
1882 | | //memset(weightList, 0, sizeof(weightList)); /* is not necessary, even though some analyzer complain ... */ |
1883 | | |
1884 | 301 | iSize = HUF_readStats(weightList, HUF_MAX_SYMBOL_VALUE + 1, rankStats, &nbSymbols, &tableLog, src, srcSize); |
1885 | 301 | if (HUF_isError(iSize)) return iSize; |
1886 | | |
1887 | | /* check result */ |
1888 | 286 | if (tableLog > memLog) return ERROR(tableLog_tooLarge); /* DTable can't fit code depth */ |
1889 | | |
1890 | | /* find maxWeight */ |
1891 | 489 | for (maxW = tableLog; rankStats[maxW]==0; maxW--) |
1892 | 205 | {if (!maxW) return ERROR(GENERIC); } /* necessarily finds a solution before maxW==0 */ |
1893 | | |
1894 | | /* Get start index of each weight */ |
1895 | 284 | { |
1896 | 284 | U32 w, nextRankStart = 0; |
1897 | 2.37k | for (w=1; w<=maxW; w++) |
1898 | 2.09k | { |
1899 | 2.09k | U32 current = nextRankStart; |
1900 | 2.09k | nextRankStart += rankStats[w]; |
1901 | 2.09k | rankStart[w] = current; |
1902 | 2.09k | } |
1903 | 284 | rankStart[0] = nextRankStart; /* put all 0w symbols at the end of sorted list*/ |
1904 | 284 | sizeOfSort = nextRankStart; |
1905 | 284 | } |
1906 | | |
1907 | | /* sort symbols by weight */ |
1908 | 284 | { |
1909 | 284 | U32 s; |
1910 | 33.4k | for (s=0; s<nbSymbols; s++) |
1911 | 33.1k | { |
1912 | 33.1k | U32 w = weightList[s]; |
1913 | 33.1k | U32 r = rankStart[w]++; |
1914 | 33.1k | sortedSymbol[r].symbol = (BYTE)s; |
1915 | 33.1k | sortedSymbol[r].weight = (BYTE)w; |
1916 | 33.1k | } |
1917 | 284 | rankStart[0] = 0; /* forget 0w symbols; this is beginning of weight(1) */ |
1918 | 284 | } |
1919 | | |
1920 | | /* Build rankVal */ |
1921 | 284 | { |
1922 | 284 | const U32 minBits = tableLog+1 - maxW; |
1923 | 284 | U32 nextRankVal = 0; |
1924 | 284 | U32 w, consumed; |
1925 | 284 | const int rescale = (memLog-tableLog) - 1; /* tableLog <= memLog */ |
1926 | 284 | U32* rankVal0 = rankVal[0]; |
1927 | 2.37k | for (w=1; w<=maxW; w++) |
1928 | 2.09k | { |
1929 | 2.09k | U32 current = nextRankVal; |
1930 | 2.09k | nextRankVal += rankStats[w] << (w+rescale); |
1931 | 2.09k | rankVal0[w] = current; |
1932 | 2.09k | } |
1933 | 3.00k | for (consumed = minBits; consumed <= memLog - minBits; consumed++) |
1934 | 2.72k | { |
1935 | 2.72k | U32* rankValPtr = rankVal[consumed]; |
1936 | 24.1k | for (w = 1; w <= maxW; w++) |
1937 | 21.4k | { |
1938 | 21.4k | rankValPtr[w] = rankVal0[w] >> consumed; |
1939 | 21.4k | } |
1940 | 2.72k | } |
1941 | 284 | } |
1942 | | |
1943 | 284 | HUF_fillDTableX4(dt, memLog, |
1944 | 284 | sortedSymbol, sizeOfSort, |
1945 | 284 | rankStart0, rankVal, maxW, |
1946 | 284 | tableLog+1); |
1947 | | |
1948 | 284 | return iSize; |
1949 | 284 | } |
1950 | | |
1951 | | |
1952 | | static U32 HUF_decodeSymbolX4(void* op, BIT_DStream_t* DStream, const HUF_DEltX4* dt, const U32 dtLog) |
1953 | 2.52M | { |
1954 | 2.52M | const size_t val = BIT_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */ |
1955 | 2.52M | memcpy(op, dt+val, 2); |
1956 | 2.52M | BIT_skipBits(DStream, dt[val].nbBits); |
1957 | 2.52M | return dt[val].length; |
1958 | 2.52M | } |
1959 | | |
1960 | | static U32 HUF_decodeLastSymbolX4(void* op, BIT_DStream_t* DStream, const HUF_DEltX4* dt, const U32 dtLog) |
1961 | 433 | { |
1962 | 433 | const size_t val = BIT_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */ |
1963 | 433 | memcpy(op, dt+val, 1); |
1964 | 433 | if (dt[val].length==1) BIT_skipBits(DStream, dt[val].nbBits); |
1965 | 267 | else |
1966 | 267 | { |
1967 | 267 | if (DStream->bitsConsumed < (sizeof(DStream->bitContainer)*8)) |
1968 | 115 | { |
1969 | 115 | BIT_skipBits(DStream, dt[val].nbBits); |
1970 | 115 | if (DStream->bitsConsumed > (sizeof(DStream->bitContainer)*8)) |
1971 | 27 | DStream->bitsConsumed = (sizeof(DStream->bitContainer)*8); /* ugly hack; works only because it's the last symbol. Note : can't easily extract nbBits from just this symbol */ |
1972 | 115 | } |
1973 | 267 | } |
1974 | 433 | return 1; |
1975 | 433 | } |
1976 | | |
1977 | | |
1978 | | #define HUF_DECODE_SYMBOLX4_0(ptr, DStreamPtr) \ |
1979 | 1.59M | ptr += HUF_decodeSymbolX4(ptr, DStreamPtr, dt, dtLog) |
1980 | | |
1981 | | #define HUF_DECODE_SYMBOLX4_1(ptr, DStreamPtr) \ |
1982 | 309k | if (MEM_64bits() || (HUF_MAX_TABLELOG<=12)) \ |
1983 | 309k | ptr += HUF_decodeSymbolX4(ptr, DStreamPtr, dt, dtLog) |
1984 | | |
1985 | | #define HUF_DECODE_SYMBOLX4_2(ptr, DStreamPtr) \ |
1986 | 618k | if (MEM_64bits()) \ |
1987 | 618k | ptr += HUF_decodeSymbolX4(ptr, DStreamPtr, dt, dtLog) |
1988 | | |
1989 | | static inline size_t HUF_decodeStreamX4(BYTE* p, BIT_DStream_t* bitDPtr, BYTE* const pEnd, const HUF_DEltX4* const dt, const U32 dtLog) |
1990 | 728 | { |
1991 | 728 | BYTE* const pStart = p; |
1992 | | |
1993 | | /* up to 8 symbols at a time */ |
1994 | 259k | while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) && (p < pEnd-7)) |
1995 | 258k | { |
1996 | 258k | HUF_DECODE_SYMBOLX4_2(p, bitDPtr); |
1997 | 258k | HUF_DECODE_SYMBOLX4_1(p, bitDPtr); |
1998 | 258k | HUF_DECODE_SYMBOLX4_2(p, bitDPtr); |
1999 | 258k | HUF_DECODE_SYMBOLX4_0(p, bitDPtr); |
2000 | 258k | } |
2001 | | |
2002 | | /* closer to the end */ |
2003 | 1.08k | while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) && (p <= pEnd-2)) |
2004 | 361 | HUF_DECODE_SYMBOLX4_0(p, bitDPtr); |
2005 | | |
2006 | 1.28M | while (p <= pEnd-2) |
2007 | 1.28M | HUF_DECODE_SYMBOLX4_0(p, bitDPtr); /* no need to reload : reached the end of DStream */ |
2008 | | |
2009 | 728 | if (p < pEnd) |
2010 | 433 | p += HUF_decodeLastSymbolX4(p, bitDPtr, dt, dtLog); |
2011 | | |
2012 | 728 | return p-pStart; |
2013 | 728 | } |
2014 | | |
2015 | | |
2016 | | |
2017 | | static size_t HUF_decompress4X4_usingDTable( |
2018 | | void* dst, size_t dstSize, |
2019 | | const void* cSrc, size_t cSrcSize, |
2020 | | const U32* DTable) |
2021 | 284 | { |
2022 | 284 | if (cSrcSize < 10) return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */ |
2023 | | |
2024 | 284 | { |
2025 | 284 | const BYTE* const istart = (const BYTE*) cSrc; |
2026 | 284 | BYTE* const ostart = (BYTE*) dst; |
2027 | 284 | BYTE* const oend = ostart + dstSize; |
2028 | | |
2029 | 284 | const void* ptr = DTable; |
2030 | 284 | const HUF_DEltX4* const dt = ((const HUF_DEltX4*)ptr) +1; |
2031 | 284 | const U32 dtLog = DTable[0]; |
2032 | 284 | size_t errorCode; |
2033 | | |
2034 | | /* Init */ |
2035 | 284 | BIT_DStream_t bitD1; |
2036 | 284 | BIT_DStream_t bitD2; |
2037 | 284 | BIT_DStream_t bitD3; |
2038 | 284 | BIT_DStream_t bitD4; |
2039 | 284 | const size_t length1 = MEM_readLE16(istart); |
2040 | 284 | const size_t length2 = MEM_readLE16(istart+2); |
2041 | 284 | const size_t length3 = MEM_readLE16(istart+4); |
2042 | 284 | size_t length4; |
2043 | 284 | const BYTE* const istart1 = istart + 6; /* jumpTable */ |
2044 | 284 | const BYTE* const istart2 = istart1 + length1; |
2045 | 284 | const BYTE* const istart3 = istart2 + length2; |
2046 | 284 | const BYTE* const istart4 = istart3 + length3; |
2047 | 284 | const size_t segmentSize = (dstSize+3) / 4; |
2048 | 284 | BYTE* const opStart2 = ostart + segmentSize; |
2049 | 284 | BYTE* const opStart3 = opStart2 + segmentSize; |
2050 | 284 | BYTE* const opStart4 = opStart3 + segmentSize; |
2051 | 284 | BYTE* op1 = ostart; |
2052 | 284 | BYTE* op2 = opStart2; |
2053 | 284 | BYTE* op3 = opStart3; |
2054 | 284 | BYTE* op4 = opStart4; |
2055 | 284 | U32 endSignal; |
2056 | | |
2057 | 284 | length4 = cSrcSize - (length1 + length2 + length3 + 6); |
2058 | 284 | if (length4 > cSrcSize) return ERROR(corruption_detected); /* overflow */ |
2059 | 237 | errorCode = BIT_initDStream(&bitD1, istart1, length1); |
2060 | 237 | if (HUF_isError(errorCode)) return errorCode; |
2061 | 225 | errorCode = BIT_initDStream(&bitD2, istart2, length2); |
2062 | 225 | if (HUF_isError(errorCode)) return errorCode; |
2063 | 214 | errorCode = BIT_initDStream(&bitD3, istart3, length3); |
2064 | 214 | if (HUF_isError(errorCode)) return errorCode; |
2065 | 202 | errorCode = BIT_initDStream(&bitD4, istart4, length4); |
2066 | 202 | if (HUF_isError(errorCode)) return errorCode; |
2067 | | |
2068 | | /* 16-32 symbols per loop (4-8 symbols per stream) */ |
2069 | 188 | endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4); |
2070 | 12.8k | for ( ; (endSignal==BIT_DStream_unfinished) && (op4<(oend-7)) ; ) |
2071 | 12.6k | { |
2072 | 12.6k | HUF_DECODE_SYMBOLX4_2(op1, &bitD1); |
2073 | 12.6k | HUF_DECODE_SYMBOLX4_2(op2, &bitD2); |
2074 | 12.6k | HUF_DECODE_SYMBOLX4_2(op3, &bitD3); |
2075 | 12.6k | HUF_DECODE_SYMBOLX4_2(op4, &bitD4); |
2076 | 12.6k | HUF_DECODE_SYMBOLX4_1(op1, &bitD1); |
2077 | 12.6k | HUF_DECODE_SYMBOLX4_1(op2, &bitD2); |
2078 | 12.6k | HUF_DECODE_SYMBOLX4_1(op3, &bitD3); |
2079 | 12.6k | HUF_DECODE_SYMBOLX4_1(op4, &bitD4); |
2080 | 12.6k | HUF_DECODE_SYMBOLX4_2(op1, &bitD1); |
2081 | 12.6k | HUF_DECODE_SYMBOLX4_2(op2, &bitD2); |
2082 | 12.6k | HUF_DECODE_SYMBOLX4_2(op3, &bitD3); |
2083 | 12.6k | HUF_DECODE_SYMBOLX4_2(op4, &bitD4); |
2084 | 12.6k | HUF_DECODE_SYMBOLX4_0(op1, &bitD1); |
2085 | 12.6k | HUF_DECODE_SYMBOLX4_0(op2, &bitD2); |
2086 | 12.6k | HUF_DECODE_SYMBOLX4_0(op3, &bitD3); |
2087 | 12.6k | HUF_DECODE_SYMBOLX4_0(op4, &bitD4); |
2088 | | |
2089 | 12.6k | endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4); |
2090 | 12.6k | } |
2091 | | |
2092 | | /* check corruption */ |
2093 | 188 | if (op1 > opStart2) return ERROR(corruption_detected); |
2094 | 186 | if (op2 > opStart3) return ERROR(corruption_detected); |
2095 | 184 | if (op3 > opStart4) return ERROR(corruption_detected); |
2096 | | /* note : op4 supposed already verified within main loop */ |
2097 | | |
2098 | | /* finish bitStreams one by one */ |
2099 | 182 | HUF_decodeStreamX4(op1, &bitD1, opStart2, dt, dtLog); |
2100 | 182 | HUF_decodeStreamX4(op2, &bitD2, opStart3, dt, dtLog); |
2101 | 182 | HUF_decodeStreamX4(op3, &bitD3, opStart4, dt, dtLog); |
2102 | 182 | HUF_decodeStreamX4(op4, &bitD4, oend, dt, dtLog); |
2103 | | |
2104 | | /* check */ |
2105 | 182 | endSignal = BIT_endOfDStream(&bitD1) & BIT_endOfDStream(&bitD2) & BIT_endOfDStream(&bitD3) & BIT_endOfDStream(&bitD4); |
2106 | 182 | if (!endSignal) return ERROR(corruption_detected); |
2107 | | |
2108 | | /* decoded size */ |
2109 | 5 | return dstSize; |
2110 | 182 | } |
2111 | 182 | } |
2112 | | |
2113 | | |
2114 | | static size_t HUF_decompress4X4 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize) |
2115 | 301 | { |
2116 | 301 | HUF_CREATE_STATIC_DTABLEX4(DTable, HUF_MAX_TABLELOG); |
2117 | 301 | const BYTE* ip = (const BYTE*) cSrc; |
2118 | | |
2119 | 301 | size_t hSize = HUF_readDTableX4 (DTable, cSrc, cSrcSize); |
2120 | 301 | if (HUF_isError(hSize)) return hSize; |
2121 | 284 | if (hSize >= cSrcSize) return ERROR(srcSize_wrong); |
2122 | 284 | ip += hSize; |
2123 | 284 | cSrcSize -= hSize; |
2124 | | |
2125 | 284 | return HUF_decompress4X4_usingDTable (dst, dstSize, ip, cSrcSize, DTable); |
2126 | 284 | } |
2127 | | |
2128 | | |
2129 | | /**********************************/ |
2130 | | /* Generic decompression selector */ |
2131 | | /**********************************/ |
2132 | | |
2133 | | typedef struct { U32 tableTime; U32 decode256Time; } algo_time_t; |
2134 | | static const algo_time_t algoTime[16 /* Quantization */][3 /* single, double, quad */] = |
2135 | | { |
2136 | | /* single, double, quad */ |
2137 | | {{0,0}, {1,1}, {2,2}}, /* Q==0 : impossible */ |
2138 | | {{0,0}, {1,1}, {2,2}}, /* Q==1 : impossible */ |
2139 | | {{ 38,130}, {1313, 74}, {2151, 38}}, /* Q == 2 : 12-18% */ |
2140 | | {{ 448,128}, {1353, 74}, {2238, 41}}, /* Q == 3 : 18-25% */ |
2141 | | {{ 556,128}, {1353, 74}, {2238, 47}}, /* Q == 4 : 25-32% */ |
2142 | | {{ 714,128}, {1418, 74}, {2436, 53}}, /* Q == 5 : 32-38% */ |
2143 | | {{ 883,128}, {1437, 74}, {2464, 61}}, /* Q == 6 : 38-44% */ |
2144 | | {{ 897,128}, {1515, 75}, {2622, 68}}, /* Q == 7 : 44-50% */ |
2145 | | {{ 926,128}, {1613, 75}, {2730, 75}}, /* Q == 8 : 50-56% */ |
2146 | | {{ 947,128}, {1729, 77}, {3359, 77}}, /* Q == 9 : 56-62% */ |
2147 | | {{1107,128}, {2083, 81}, {4006, 84}}, /* Q ==10 : 62-69% */ |
2148 | | {{1177,128}, {2379, 87}, {4785, 88}}, /* Q ==11 : 69-75% */ |
2149 | | {{1242,128}, {2415, 93}, {5155, 84}}, /* Q ==12 : 75-81% */ |
2150 | | {{1349,128}, {2644,106}, {5260,106}}, /* Q ==13 : 81-87% */ |
2151 | | {{1455,128}, {2422,124}, {4174,124}}, /* Q ==14 : 87-93% */ |
2152 | | {{ 722,128}, {1891,145}, {1936,146}}, /* Q ==15 : 93-99% */ |
2153 | | }; |
2154 | | |
2155 | | typedef size_t (*decompressionAlgo)(void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); |
2156 | | |
2157 | | static size_t HUF_decompress (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize) |
2158 | 2.90k | { |
2159 | 2.90k | static const decompressionAlgo decompress[2] = { HUF_decompress4X2, HUF_decompress4X4 }; |
2160 | | /* estimate decompression time */ |
2161 | 2.90k | U32 Q; |
2162 | 2.90k | const U32 D256 = (U32)(dstSize >> 8); |
2163 | 2.90k | U32 Dtime[3]; |
2164 | 2.90k | U32 algoNb = 0; |
2165 | 2.90k | int n; |
2166 | | |
2167 | | /* validation checks */ |
2168 | 2.90k | if (dstSize == 0) return ERROR(dstSize_tooSmall); |
2169 | 2.90k | if (cSrcSize > dstSize) return ERROR(corruption_detected); /* invalid */ |
2170 | 2.88k | if (cSrcSize == dstSize) { memcpy(dst, cSrc, dstSize); return dstSize; } /* not compressed */ |
2171 | 2.53k | if (cSrcSize == 1) { memset(dst, *(const BYTE*)cSrc, dstSize); return dstSize; } /* RLE */ |
2172 | | |
2173 | | /* decoder timing evaluation */ |
2174 | 2.29k | Q = (U32)(cSrcSize * 16 / dstSize); /* Q < 16 since dstSize > cSrcSize */ |
2175 | 9.16k | for (n=0; n<3; n++) |
2176 | 6.87k | Dtime[n] = algoTime[Q][n].tableTime + (algoTime[Q][n].decode256Time * D256); |
2177 | | |
2178 | 2.29k | Dtime[1] += Dtime[1] >> 4; Dtime[2] += Dtime[2] >> 3; /* advantage to algorithms using less memory, for cache eviction */ |
2179 | | |
2180 | 2.29k | if (Dtime[1] < Dtime[0]) algoNb = 1; |
2181 | | |
2182 | 2.29k | return decompress[algoNb](dst, dstSize, cSrc, cSrcSize); |
2183 | | |
2184 | | //return HUF_decompress4X2(dst, dstSize, cSrc, cSrcSize); /* multi-streams single-symbol decoding */ |
2185 | | //return HUF_decompress4X4(dst, dstSize, cSrc, cSrcSize); /* multi-streams double-symbols decoding */ |
2186 | | //return HUF_decompress4X6(dst, dstSize, cSrc, cSrcSize); /* multi-streams quad-symbols decoding */ |
2187 | 2.53k | } |
2188 | | /* |
2189 | | zstd - standard compression library |
2190 | | Copyright (C) 2014-2015, Yann Collet. |
2191 | | |
2192 | | BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php) |
2193 | | |
2194 | | Redistribution and use in source and binary forms, with or without |
2195 | | modification, are permitted provided that the following conditions are |
2196 | | met: |
2197 | | * Redistributions of source code must retain the above copyright |
2198 | | notice, this list of conditions and the following disclaimer. |
2199 | | * Redistributions in binary form must reproduce the above |
2200 | | copyright notice, this list of conditions and the following disclaimer |
2201 | | in the documentation and/or other materials provided with the |
2202 | | distribution. |
2203 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
2204 | | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
2205 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
2206 | | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
2207 | | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
2208 | | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
2209 | | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
2210 | | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
2211 | | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
2212 | | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
2213 | | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
2214 | | |
2215 | | You can contact the author at : |
2216 | | - zstd source repository : https://github.com/Cyan4973/zstd |
2217 | | - ztsd public forum : https://groups.google.com/forum/#!forum/lz4c |
2218 | | */ |
2219 | | |
2220 | | /* *************************************************************** |
2221 | | * Tuning parameters |
2222 | | *****************************************************************/ |
2223 | | /*! |
2224 | | * MEMORY_USAGE : |
2225 | | * Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.) |
2226 | | * Increasing memory usage improves compression ratio |
2227 | | * Reduced memory usage can improve speed, due to cache effect |
2228 | | */ |
2229 | | #define ZSTD_MEMORY_USAGE 17 |
2230 | | |
2231 | | /*! |
2232 | | * HEAPMODE : |
2233 | | * Select how default compression functions will allocate memory for their hash table, |
2234 | | * in memory stack (0, fastest), or in memory heap (1, requires malloc()) |
2235 | | * Note that compression context is fairly large, as a consequence heap memory is recommended. |
2236 | | */ |
2237 | | #ifndef ZSTD_HEAPMODE |
2238 | | # define ZSTD_HEAPMODE 1 |
2239 | | #endif /* ZSTD_HEAPMODE */ |
2240 | | |
2241 | | /*! |
2242 | | * LEGACY_SUPPORT : |
2243 | | * decompressor can decode older formats (starting from Zstd 0.1+) |
2244 | | */ |
2245 | | #ifndef ZSTD_LEGACY_SUPPORT |
2246 | | # define ZSTD_LEGACY_SUPPORT 1 |
2247 | | #endif |
2248 | | |
2249 | | |
2250 | | /* ******************************************************* |
2251 | | * Includes |
2252 | | *********************************************************/ |
2253 | | #include <stdlib.h> /* calloc */ |
2254 | | #include <string.h> /* memcpy, memmove */ |
2255 | | #include <stdio.h> /* debug : printf */ |
2256 | | |
2257 | | |
2258 | | /* ******************************************************* |
2259 | | * Compiler specifics |
2260 | | *********************************************************/ |
2261 | | #ifdef __AVX2__ |
2262 | | # include <immintrin.h> /* AVX2 intrinsics */ |
2263 | | #endif |
2264 | | |
2265 | | #ifdef _MSC_VER /* Visual Studio */ |
2266 | | # include <intrin.h> /* For Visual 2005 */ |
2267 | | # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ |
2268 | | # pragma warning(disable : 4324) /* disable: C4324: padded structure */ |
2269 | | #endif |
2270 | | |
2271 | | |
2272 | | /* ******************************************************* |
2273 | | * Constants |
2274 | | *********************************************************/ |
2275 | | #define HASH_LOG (ZSTD_MEMORY_USAGE - 2) |
2276 | | #define HASH_TABLESIZE (1 << HASH_LOG) |
2277 | | #define HASH_MASK (HASH_TABLESIZE - 1) |
2278 | | |
2279 | | #define KNUTH 2654435761 |
2280 | | |
2281 | | #define BIT7 128 |
2282 | | #define BIT6 64 |
2283 | | #define BIT5 32 |
2284 | | #define BIT4 16 |
2285 | 4.13k | #define BIT1 2 |
2286 | 7.04k | #define BIT0 1 |
2287 | | |
2288 | 54.4k | #define KB *(1 <<10) |
2289 | | #define MB *(1 <<20) |
2290 | | #define GB *(1U<<30) |
2291 | | |
2292 | 54.4k | #define BLOCKSIZE (128 KB) /* define, for static allocation */ |
2293 | 14.7k | #define MIN_SEQUENCES_SIZE (2 /*seqNb*/ + 2 /*dumps*/ + 3 /*seqTables*/ + 1 /*bitStream*/) |
2294 | 14.7k | #define MIN_CBLOCK_SIZE (3 /*litCSize*/ + MIN_SEQUENCES_SIZE) |
2295 | 7.04k | #define IS_RAW BIT0 |
2296 | 4.13k | #define IS_RLE BIT1 |
2297 | | |
2298 | | #define WORKPLACESIZE (BLOCKSIZE*3) |
2299 | 67.3k | #define MINMATCH 4 |
2300 | 52.4k | #define MLbits 7 |
2301 | 52.8k | #define LLbits 6 |
2302 | 10.1k | #define Offbits 5 |
2303 | 37.6k | #define MaxML ((1<<MLbits )-1) |
2304 | 36.1k | #define MaxLL ((1<<LLbits )-1) |
2305 | 7.65k | #define MaxOff 31 |
2306 | | #define LitFSELog 11 |
2307 | 3.69k | #define MLFSELog 10 |
2308 | 2.13k | #define LLFSELog 10 |
2309 | 2.64k | #define OffFSELog 9 |
2310 | | #define MAX(a,b) ((a)<(b)?(b):(a)) |
2311 | | #define MaxSeq MAX(MaxLL, MaxML) |
2312 | | |
2313 | | #define LITERAL_NOENTROPY 63 |
2314 | | #define COMMAND_NOENTROPY 7 /* to remove */ |
2315 | | |
2316 | 402 | #define ZSTD_CONTENTSIZE_ERROR (0ULL - 2) |
2317 | | |
2318 | | static const size_t ZSTD_blockHeaderSize = 3; |
2319 | | static const size_t ZSTD_frameHeaderSize = 4; |
2320 | | |
2321 | | |
2322 | | /* ******************************************************* |
2323 | | * Memory operations |
2324 | | **********************************************************/ |
2325 | 20.4k | static void ZSTD_copy4(void* dst, const void* src) { memcpy(dst, src, 4); } |
2326 | | |
2327 | 3.90M | static void ZSTD_copy8(void* dst, const void* src) { memcpy(dst, src, 8); } |
2328 | | |
2329 | 3.88M | #define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; } |
2330 | | |
2331 | | /*! ZSTD_wildcopy : custom version of memcpy(), can copy up to 7-8 bytes too many */ |
2332 | | static void ZSTD_wildcopy(void* dst, const void* src, ptrdiff_t length) |
2333 | 66.6k | { |
2334 | 66.6k | const BYTE* ip = (const BYTE*)src; |
2335 | 66.6k | BYTE* op = (BYTE*)dst; |
2336 | 66.6k | BYTE* const oend = op + length; |
2337 | 3.88M | do COPY8(op, ip) while (op < oend); |
2338 | 66.6k | } |
2339 | | |
2340 | | |
2341 | | /* ************************************** |
2342 | | * Local structures |
2343 | | ****************************************/ |
2344 | | typedef enum { bt_compressed, bt_raw, bt_rle, bt_end } blockType_t; |
2345 | | |
2346 | | typedef struct |
2347 | | { |
2348 | | blockType_t blockType; |
2349 | | U32 origSize; |
2350 | | } blockProperties_t; |
2351 | | |
2352 | | typedef struct { |
2353 | | void* buffer; |
2354 | | U32* offsetStart; |
2355 | | U32* offset; |
2356 | | BYTE* offCodeStart; |
2357 | | BYTE* offCode; |
2358 | | BYTE* litStart; |
2359 | | BYTE* lit; |
2360 | | BYTE* litLengthStart; |
2361 | | BYTE* litLength; |
2362 | | BYTE* matchLengthStart; |
2363 | | BYTE* matchLength; |
2364 | | BYTE* dumpsStart; |
2365 | | BYTE* dumps; |
2366 | | } SeqStore_t; |
2367 | | |
2368 | | |
2369 | | /* ************************************* |
2370 | | * Error Management |
2371 | | ***************************************/ |
2372 | | /*! ZSTD_isError |
2373 | | * tells if a return value is an error code */ |
2374 | 125k | static unsigned ZSTD_isError(size_t code) { return ERR_isError(code); } |
2375 | | |
2376 | | |
2377 | | |
2378 | | /* ************************************************************* |
2379 | | * Decompression section |
2380 | | ***************************************************************/ |
2381 | | struct ZSTDv02_Dctx_s |
2382 | | { |
2383 | | U32 LLTable[FSE_DTABLE_SIZE_U32(LLFSELog)]; |
2384 | | U32 OffTable[FSE_DTABLE_SIZE_U32(OffFSELog)]; |
2385 | | U32 MLTable[FSE_DTABLE_SIZE_U32(MLFSELog)]; |
2386 | | void* previousDstEnd; |
2387 | | void* base; |
2388 | | size_t expected; |
2389 | | blockType_t bType; |
2390 | | U32 phase; |
2391 | | const BYTE* litPtr; |
2392 | | size_t litSize; |
2393 | | BYTE litBuffer[BLOCKSIZE + 8 /* margin for wildcopy */]; |
2394 | | }; /* typedef'd to ZSTD_Dctx within "zstd_static.h" */ |
2395 | | |
2396 | | |
2397 | | static size_t ZSTD_getcBlockSize(const void* src, size_t srcSize, blockProperties_t* bpPtr) |
2398 | 46.0k | { |
2399 | 46.0k | const BYTE* const in = (const BYTE* const)src; |
2400 | 46.0k | BYTE headerFlags; |
2401 | 46.0k | U32 cSize; |
2402 | | |
2403 | 46.0k | if (srcSize < 3) return ERROR(srcSize_wrong); |
2404 | | |
2405 | 46.0k | headerFlags = *in; |
2406 | 46.0k | cSize = in[2] + (in[1]<<8) + ((in[0] & 7)<<16); |
2407 | | |
2408 | 46.0k | bpPtr->blockType = (blockType_t)(headerFlags >> 6); |
2409 | 46.0k | bpPtr->origSize = (bpPtr->blockType == bt_rle) ? cSize : 0; |
2410 | | |
2411 | 46.0k | if (bpPtr->blockType == bt_end) return 0; |
2412 | 41.6k | if (cSize > BLOCKSIZE) return ERROR(corruption_detected); |
2413 | 41.5k | if (bpPtr->blockType == bt_rle) return 1; |
2414 | 40.5k | return cSize; |
2415 | 41.5k | } |
2416 | | |
2417 | | static size_t ZSTD_copyUncompressedBlock(void* dst, size_t maxDstSize, const void* src, size_t srcSize) |
2418 | 4.47k | { |
2419 | 4.47k | if (srcSize > maxDstSize) return ERROR(dstSize_tooSmall); |
2420 | 4.43k | if (srcSize > 0) { |
2421 | 3.89k | memcpy(dst, src, srcSize); |
2422 | 3.89k | } |
2423 | 4.43k | return srcSize; |
2424 | 4.47k | } |
2425 | | |
2426 | | |
2427 | | /** ZSTD_decompressLiterals |
2428 | | @return : nb of bytes read from src, or an error code*/ |
2429 | | static size_t ZSTD_decompressLiterals(void* dst, size_t* maxDstSizePtr, |
2430 | | const void* src, size_t srcSize) |
2431 | 2.98k | { |
2432 | 2.98k | const BYTE* ip = (const BYTE*)src; |
2433 | | |
2434 | 2.98k | const size_t litSize = (MEM_readLE32(src) & 0x1FFFFF) >> 2; /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */ |
2435 | 2.98k | const size_t litCSize = (MEM_readLE32(ip+2) & 0xFFFFFF) >> 5; /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */ |
2436 | | |
2437 | 2.98k | if (litSize > *maxDstSizePtr) return ERROR(corruption_detected); |
2438 | 2.96k | if (litCSize + 5 > srcSize) return ERROR(corruption_detected); |
2439 | | |
2440 | 2.90k | if (HUF_isError(HUF_decompress(dst, litSize, ip+5, litCSize))) return ERROR(corruption_detected); |
2441 | | |
2442 | 1.71k | *maxDstSizePtr = litSize; |
2443 | 1.71k | return litCSize + 5; |
2444 | 2.90k | } |
2445 | | |
2446 | | |
2447 | | /** ZSTD_decodeLiteralsBlock |
2448 | | @return : nb of bytes read from src (< srcSize )*/ |
2449 | | static size_t ZSTD_decodeLiteralsBlock(void* ctx, |
2450 | | const void* src, size_t srcSize) |
2451 | 14.7k | { |
2452 | 14.7k | ZSTD_DCtx* dctx = (ZSTD_DCtx*)ctx; |
2453 | 14.7k | const BYTE* const istart = (const BYTE* const)src; |
2454 | | |
2455 | | /* any compressed block with literals segment must be at least this size */ |
2456 | 14.7k | if (srcSize < MIN_CBLOCK_SIZE) return ERROR(corruption_detected); |
2457 | | |
2458 | 14.1k | switch(*istart & 3) |
2459 | 14.1k | { |
2460 | 778 | default: |
2461 | 2.98k | case 0: |
2462 | 2.98k | { |
2463 | 2.98k | size_t litSize = BLOCKSIZE; |
2464 | 2.98k | const size_t readSize = ZSTD_decompressLiterals(dctx->litBuffer, &litSize, src, srcSize); |
2465 | 2.98k | dctx->litPtr = dctx->litBuffer; |
2466 | 2.98k | dctx->litSize = litSize; |
2467 | 2.98k | memset(dctx->litBuffer + dctx->litSize, 0, 8); |
2468 | 2.98k | return readSize; /* works if it's an error too */ |
2469 | 778 | } |
2470 | 7.04k | case IS_RAW: |
2471 | 7.04k | { |
2472 | 7.04k | const size_t litSize = (MEM_readLE32(istart) & 0xFFFFFF) >> 2; /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */ |
2473 | 7.04k | if (litSize > srcSize-11) /* risk of reading too far with wildcopy */ |
2474 | 262 | { |
2475 | 262 | if (litSize > BLOCKSIZE) return ERROR(corruption_detected); |
2476 | 243 | if (litSize > srcSize-3) return ERROR(corruption_detected); |
2477 | 210 | memcpy(dctx->litBuffer, istart+3, litSize); |
2478 | 210 | dctx->litPtr = dctx->litBuffer; |
2479 | 210 | dctx->litSize = litSize; |
2480 | 210 | memset(dctx->litBuffer + dctx->litSize, 0, 8); |
2481 | 210 | return litSize+3; |
2482 | 243 | } |
2483 | | /* direct reference into compressed stream */ |
2484 | 6.78k | dctx->litPtr = istart+3; |
2485 | 6.78k | dctx->litSize = litSize; |
2486 | 6.78k | return litSize+3; |
2487 | 7.04k | } |
2488 | 4.13k | case IS_RLE: |
2489 | 4.13k | { |
2490 | 4.13k | const size_t litSize = (MEM_readLE32(istart) & 0xFFFFFF) >> 2; /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */ |
2491 | 4.13k | if (litSize > BLOCKSIZE) return ERROR(corruption_detected); |
2492 | 4.12k | memset(dctx->litBuffer, istart[3], litSize + 8); |
2493 | 4.12k | dctx->litPtr = dctx->litBuffer; |
2494 | 4.12k | dctx->litSize = litSize; |
2495 | 4.12k | return 4; |
2496 | 4.13k | } |
2497 | 14.1k | } |
2498 | 14.1k | } |
2499 | | |
2500 | | |
2501 | | static size_t ZSTD_decodeSeqHeaders(int* nbSeq, const BYTE** dumpsPtr, size_t* dumpsLengthPtr, |
2502 | | FSE_DTable* DTableLL, FSE_DTable* DTableML, FSE_DTable* DTableOffb, |
2503 | | const void* src, size_t srcSize) |
2504 | 12.8k | { |
2505 | 12.8k | const BYTE* const istart = (const BYTE* const)src; |
2506 | 12.8k | const BYTE* ip = istart; |
2507 | 12.8k | const BYTE* const iend = istart + srcSize; |
2508 | 12.8k | U32 LLtype, Offtype, MLtype; |
2509 | 12.8k | U32 LLlog, Offlog, MLlog; |
2510 | 12.8k | size_t dumpsLength; |
2511 | | |
2512 | | /* check */ |
2513 | 12.8k | if (srcSize < 5) return ERROR(srcSize_wrong); |
2514 | | |
2515 | | /* SeqHead */ |
2516 | 12.8k | *nbSeq = MEM_readLE16(ip); ip+=2; |
2517 | 12.8k | LLtype = *ip >> 6; |
2518 | 12.8k | Offtype = (*ip >> 4) & 3; |
2519 | 12.8k | MLtype = (*ip >> 2) & 3; |
2520 | 12.8k | if (*ip & 2) |
2521 | 5.84k | { |
2522 | 5.84k | dumpsLength = ip[2]; |
2523 | 5.84k | dumpsLength += ip[1] << 8; |
2524 | 5.84k | ip += 3; |
2525 | 5.84k | } |
2526 | 6.97k | else |
2527 | 6.97k | { |
2528 | 6.97k | dumpsLength = ip[1]; |
2529 | 6.97k | dumpsLength += (ip[0] & 1) << 8; |
2530 | 6.97k | ip += 2; |
2531 | 6.97k | } |
2532 | 12.8k | *dumpsPtr = ip; |
2533 | 12.8k | ip += dumpsLength; |
2534 | 12.8k | *dumpsLengthPtr = dumpsLength; |
2535 | | |
2536 | | /* check */ |
2537 | 12.8k | if (ip > iend-3) return ERROR(srcSize_wrong); /* min : all 3 are "raw", hence no header, but at least xxLog bits per type */ |
2538 | | |
2539 | | /* sequences */ |
2540 | 12.7k | { |
2541 | 12.7k | S16 norm[MaxML+1]; /* assumption : MaxML >= MaxLL and MaxOff */ |
2542 | 12.7k | size_t headerSize; |
2543 | | |
2544 | | /* Build DTables */ |
2545 | 12.7k | switch(LLtype) |
2546 | 12.7k | { |
2547 | 2.24k | case bt_rle : |
2548 | 2.24k | LLlog = 0; |
2549 | 2.24k | FSE_buildDTable_rle(DTableLL, *ip++); break; |
2550 | 8.35k | case bt_raw : |
2551 | 8.35k | LLlog = LLbits; |
2552 | 8.35k | FSE_buildDTable_raw(DTableLL, LLbits); break; |
2553 | 2.17k | default : |
2554 | 2.17k | { U32 max = MaxLL; |
2555 | 2.17k | headerSize = FSE_readNCount(norm, &max, &LLlog, ip, iend-ip); |
2556 | 2.17k | if (FSE_isError(headerSize)) return ERROR(GENERIC); |
2557 | 2.13k | if (LLlog > LLFSELog) return ERROR(corruption_detected); |
2558 | 2.12k | ip += headerSize; |
2559 | 2.12k | FSE_buildDTable(DTableLL, norm, max, LLlog); |
2560 | 2.12k | } } |
2561 | | |
2562 | 12.7k | switch(Offtype) |
2563 | 12.7k | { |
2564 | 4.95k | case bt_rle : |
2565 | 4.95k | Offlog = 0; |
2566 | 4.95k | if (ip > iend-2) return ERROR(srcSize_wrong); /* min : "raw", hence no header, but at least xxLog bits */ |
2567 | 4.94k | FSE_buildDTable_rle(DTableOffb, *ip++ & MaxOff); /* if *ip > MaxOff, data is corrupted */ |
2568 | 4.94k | break; |
2569 | 5.07k | case bt_raw : |
2570 | 5.07k | Offlog = Offbits; |
2571 | 5.07k | FSE_buildDTable_raw(DTableOffb, Offbits); break; |
2572 | 2.70k | default : |
2573 | 2.70k | { U32 max = MaxOff; |
2574 | 2.70k | headerSize = FSE_readNCount(norm, &max, &Offlog, ip, iend-ip); |
2575 | 2.70k | if (FSE_isError(headerSize)) return ERROR(GENERIC); |
2576 | 2.64k | if (Offlog > OffFSELog) return ERROR(corruption_detected); |
2577 | 2.62k | ip += headerSize; |
2578 | 2.62k | FSE_buildDTable(DTableOffb, norm, max, Offlog); |
2579 | 2.62k | } } |
2580 | | |
2581 | 12.6k | switch(MLtype) |
2582 | 12.6k | { |
2583 | 1.51k | case bt_rle : |
2584 | 1.51k | MLlog = 0; |
2585 | 1.51k | if (ip > iend-2) return ERROR(srcSize_wrong); /* min : "raw", hence no header, but at least xxLog bits */ |
2586 | 1.51k | FSE_buildDTable_rle(DTableML, *ip++); break; |
2587 | 7.39k | case bt_raw : |
2588 | 7.39k | MLlog = MLbits; |
2589 | 7.39k | FSE_buildDTable_raw(DTableML, MLbits); break; |
2590 | 3.73k | default : |
2591 | 3.73k | { U32 max = MaxML; |
2592 | 3.73k | headerSize = FSE_readNCount(norm, &max, &MLlog, ip, iend-ip); |
2593 | 3.73k | if (FSE_isError(headerSize)) return ERROR(GENERIC); |
2594 | 3.69k | if (MLlog > MLFSELog) return ERROR(corruption_detected); |
2595 | 3.68k | ip += headerSize; |
2596 | 3.68k | FSE_buildDTable(DTableML, norm, max, MLlog); |
2597 | 3.68k | } } } |
2598 | | |
2599 | 12.5k | return ip-istart; |
2600 | 12.6k | } |
2601 | | |
2602 | | |
2603 | | typedef struct { |
2604 | | size_t litLength; |
2605 | | size_t offset; |
2606 | | size_t matchLength; |
2607 | | } seq_t; |
2608 | | |
2609 | | typedef struct { |
2610 | | BIT_DStream_t DStream; |
2611 | | FSE_DState_t stateLL; |
2612 | | FSE_DState_t stateOffb; |
2613 | | FSE_DState_t stateML; |
2614 | | size_t prevOffset; |
2615 | | const BYTE* dumps; |
2616 | | const BYTE* dumpsEnd; |
2617 | | } seqState_t; |
2618 | | |
2619 | | |
2620 | | static void ZSTD_decodeSequence(seq_t* seq, seqState_t* seqState) |
2621 | 33.9k | { |
2622 | 33.9k | size_t litLength; |
2623 | 33.9k | size_t prevOffset; |
2624 | 33.9k | size_t offset; |
2625 | 33.9k | size_t matchLength; |
2626 | 33.9k | const BYTE* dumps = seqState->dumps; |
2627 | 33.9k | const BYTE* const de = seqState->dumpsEnd; |
2628 | | |
2629 | | /* Literal length */ |
2630 | 33.9k | litLength = FSE_decodeSymbol(&(seqState->stateLL), &(seqState->DStream)); |
2631 | 33.9k | prevOffset = litLength ? seq->offset : seqState->prevOffset; |
2632 | 33.9k | seqState->prevOffset = seq->offset; |
2633 | 33.9k | if (litLength == MaxLL) |
2634 | 4.64k | { |
2635 | 4.64k | const U32 add = dumps<de ? *dumps++ : 0; |
2636 | 4.64k | if (add < 255) litLength += add; |
2637 | 627 | else if (dumps + 3 <= de) |
2638 | 201 | { |
2639 | 201 | litLength = MEM_readLE24(dumps); |
2640 | 201 | dumps += 3; |
2641 | 201 | } |
2642 | 4.64k | if (dumps >= de) dumps = de-1; /* late correction, to avoid read overflow (data is now corrupted anyway) */ |
2643 | 4.64k | } |
2644 | | |
2645 | | /* Offset */ |
2646 | 33.9k | { |
2647 | 33.9k | static const size_t offsetPrefix[MaxOff+1] = { /* note : size_t faster than U32 */ |
2648 | 33.9k | 1 /*fake*/, 1, 2, 4, 8, 16, 32, 64, 128, 256, |
2649 | 33.9k | 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, |
2650 | 33.9k | 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, /*fake*/ 1, 1, 1, 1, 1 }; |
2651 | 33.9k | U32 offsetCode, nbBits; |
2652 | 33.9k | offsetCode = FSE_decodeSymbol(&(seqState->stateOffb), &(seqState->DStream)); /* <= maxOff, by table construction */ |
2653 | 33.9k | if (MEM_32bits()) BIT_reloadDStream(&(seqState->DStream)); |
2654 | 33.9k | nbBits = offsetCode - 1; |
2655 | 33.9k | if (offsetCode==0) nbBits = 0; /* cmove */ |
2656 | 33.9k | offset = offsetPrefix[offsetCode] + BIT_readBits(&(seqState->DStream), nbBits); |
2657 | 33.9k | if (MEM_32bits()) BIT_reloadDStream(&(seqState->DStream)); |
2658 | 33.9k | if (offsetCode==0) offset = prevOffset; /* cmove */ |
2659 | 33.9k | } |
2660 | | |
2661 | | /* MatchLength */ |
2662 | 33.9k | matchLength = FSE_decodeSymbol(&(seqState->stateML), &(seqState->DStream)); |
2663 | 33.9k | if (matchLength == MaxML) |
2664 | 3.35k | { |
2665 | 3.35k | const U32 add = dumps<de ? *dumps++ : 0; |
2666 | 3.35k | if (add < 255) matchLength += add; |
2667 | 527 | else if (dumps + 3 <= de) |
2668 | 229 | { |
2669 | 229 | matchLength = MEM_readLE24(dumps); |
2670 | 229 | dumps += 3; |
2671 | 229 | } |
2672 | 3.35k | if (dumps >= de) dumps = de-1; /* late correction, to avoid read overflow (data is now corrupted anyway) */ |
2673 | 3.35k | } |
2674 | 33.9k | matchLength += MINMATCH; |
2675 | | |
2676 | | /* save result */ |
2677 | 33.9k | seq->litLength = litLength; |
2678 | 33.9k | seq->offset = offset; |
2679 | 33.9k | seq->matchLength = matchLength; |
2680 | 33.9k | seqState->dumps = dumps; |
2681 | 33.9k | } |
2682 | | |
2683 | | |
2684 | | static size_t ZSTD_execSequence(BYTE* op, |
2685 | | seq_t sequence, |
2686 | | const BYTE** litPtr, const BYTE* const litLimit, |
2687 | | BYTE* const base, BYTE* const oend) |
2688 | 33.9k | { |
2689 | 33.9k | static const int dec32table[] = {0, 1, 2, 1, 4, 4, 4, 4}; /* added */ |
2690 | 33.9k | static const int dec64table[] = {8, 8, 8, 7, 8, 9,10,11}; /* subtracted */ |
2691 | 33.9k | const BYTE* const ostart = op; |
2692 | 33.9k | BYTE* const oLitEnd = op + sequence.litLength; |
2693 | 33.9k | BYTE* const oMatchEnd = op + sequence.litLength + sequence.matchLength; /* risk : address space overflow (32-bits) */ |
2694 | 33.9k | BYTE* const oend_8 = oend-8; |
2695 | 33.9k | const BYTE* const litEnd = *litPtr + sequence.litLength; |
2696 | | |
2697 | | /* checks */ |
2698 | 33.9k | size_t const seqLength = sequence.litLength + sequence.matchLength; |
2699 | | |
2700 | 33.9k | if (sequence.offset == 0) return ERROR(corruption_detected); |
2701 | 33.9k | if (seqLength > (size_t)(oend - op)) return ERROR(dstSize_tooSmall); |
2702 | 33.5k | if (sequence.litLength > (size_t)(litLimit - *litPtr)) return ERROR(corruption_detected); |
2703 | | /* Now we know there are no overflow in literal nor match lengths, can use the pointer check */ |
2704 | 33.4k | if (oLitEnd > oend_8) return ERROR(dstSize_tooSmall); |
2705 | 33.4k | if (sequence.offset > (size_t)(oLitEnd - base)) return ERROR(corruption_detected); |
2706 | | |
2707 | 33.3k | if (oMatchEnd > oend) return ERROR(dstSize_tooSmall); /* overwrite beyond dst buffer */ |
2708 | 33.3k | if (litEnd > litLimit) return ERROR(corruption_detected); /* overRead beyond lit buffer */ |
2709 | | |
2710 | | /* copy Literals */ |
2711 | 33.3k | ZSTD_wildcopy(op, *litPtr, (ptrdiff_t)sequence.litLength); /* note : oLitEnd <= oend-8 : no risk of overwrite beyond oend */ |
2712 | 33.3k | op = oLitEnd; |
2713 | 33.3k | *litPtr = litEnd; /* update for next sequence */ |
2714 | | |
2715 | | /* copy Match */ |
2716 | 33.3k | { |
2717 | 33.3k | const BYTE* match = op - sequence.offset; |
2718 | | |
2719 | | /* check */ |
2720 | 33.3k | if (match < base) return ERROR(corruption_detected); |
2721 | | |
2722 | | /* close range match, overlap */ |
2723 | 33.3k | if (sequence.offset < 8) |
2724 | 20.4k | { |
2725 | 20.4k | const int dec64 = dec64table[sequence.offset]; |
2726 | 20.4k | op[0] = match[0]; |
2727 | 20.4k | op[1] = match[1]; |
2728 | 20.4k | op[2] = match[2]; |
2729 | 20.4k | op[3] = match[3]; |
2730 | 20.4k | match += dec32table[sequence.offset]; |
2731 | 20.4k | ZSTD_copy4(op+4, match); |
2732 | 20.4k | match -= dec64; |
2733 | 20.4k | } |
2734 | 12.9k | else |
2735 | 12.9k | { |
2736 | 12.9k | ZSTD_copy8(op, match); |
2737 | 12.9k | } |
2738 | 33.3k | op += 8; match += 8; |
2739 | | |
2740 | 33.3k | if (oMatchEnd > oend-(16-MINMATCH)) |
2741 | 115 | { |
2742 | 115 | if (op < oend_8) |
2743 | 39 | { |
2744 | 39 | ZSTD_wildcopy(op, match, oend_8 - op); |
2745 | 39 | match += oend_8 - op; |
2746 | 39 | op = oend_8; |
2747 | 39 | } |
2748 | 240 | while (op < oMatchEnd) *op++ = *match++; |
2749 | 115 | } |
2750 | 33.2k | else |
2751 | 33.2k | { |
2752 | 33.2k | ZSTD_wildcopy(op, match, (ptrdiff_t)sequence.matchLength-8); /* works even if matchLength < 8 */ |
2753 | 33.2k | } |
2754 | 33.3k | } |
2755 | | |
2756 | 0 | return oMatchEnd - ostart; |
2757 | 33.3k | } |
2758 | | |
2759 | | static size_t ZSTD_decompressSequences( |
2760 | | void* ctx, |
2761 | | void* dst, size_t maxDstSize, |
2762 | | const void* seqStart, size_t seqSize) |
2763 | 12.8k | { |
2764 | 12.8k | ZSTD_DCtx* dctx = (ZSTD_DCtx*)ctx; |
2765 | 12.8k | const BYTE* ip = (const BYTE*)seqStart; |
2766 | 12.8k | const BYTE* const iend = ip + seqSize; |
2767 | 12.8k | BYTE* const ostart = (BYTE* const)dst; |
2768 | 12.8k | BYTE* op = ostart; |
2769 | 12.8k | BYTE* const oend = ostart + maxDstSize; |
2770 | 12.8k | size_t errorCode, dumpsLength; |
2771 | 12.8k | const BYTE* litPtr = dctx->litPtr; |
2772 | 12.8k | const BYTE* const litEnd = litPtr + dctx->litSize; |
2773 | 12.8k | int nbSeq; |
2774 | 12.8k | const BYTE* dumps; |
2775 | 12.8k | U32* DTableLL = dctx->LLTable; |
2776 | 12.8k | U32* DTableML = dctx->MLTable; |
2777 | 12.8k | U32* DTableOffb = dctx->OffTable; |
2778 | 12.8k | BYTE* const base = (BYTE*) (dctx->base); |
2779 | | |
2780 | | /* Build Decoding Tables */ |
2781 | 12.8k | errorCode = ZSTD_decodeSeqHeaders(&nbSeq, &dumps, &dumpsLength, |
2782 | 12.8k | DTableLL, DTableML, DTableOffb, |
2783 | 12.8k | ip, iend-ip); |
2784 | 12.8k | if (ZSTD_isError(errorCode)) return errorCode; |
2785 | 12.5k | ip += errorCode; |
2786 | | |
2787 | | /* Regen sequences */ |
2788 | 12.5k | { |
2789 | 12.5k | seq_t sequence; |
2790 | 12.5k | seqState_t seqState; |
2791 | | |
2792 | 12.5k | memset(&sequence, 0, sizeof(sequence)); |
2793 | 12.5k | seqState.dumps = dumps; |
2794 | 12.5k | seqState.dumpsEnd = dumps + dumpsLength; |
2795 | 12.5k | seqState.prevOffset = 1; |
2796 | 12.5k | errorCode = BIT_initDStream(&(seqState.DStream), ip, iend-ip); |
2797 | 12.5k | if (ERR_isError(errorCode)) return ERROR(corruption_detected); |
2798 | 12.5k | FSE_initDState(&(seqState.stateLL), &(seqState.DStream), DTableLL); |
2799 | 12.5k | FSE_initDState(&(seqState.stateOffb), &(seqState.DStream), DTableOffb); |
2800 | 12.5k | FSE_initDState(&(seqState.stateML), &(seqState.DStream), DTableML); |
2801 | | |
2802 | 45.8k | for ( ; (BIT_reloadDStream(&(seqState.DStream)) <= BIT_DStream_completed) && (nbSeq>0) ; ) |
2803 | 33.9k | { |
2804 | 33.9k | size_t oneSeqSize; |
2805 | 33.9k | nbSeq--; |
2806 | 33.9k | ZSTD_decodeSequence(&sequence, &seqState); |
2807 | 33.9k | oneSeqSize = ZSTD_execSequence(op, sequence, &litPtr, litEnd, base, oend); |
2808 | 33.9k | if (ZSTD_isError(oneSeqSize)) return oneSeqSize; |
2809 | 33.3k | op += oneSeqSize; |
2810 | 33.3k | } |
2811 | | |
2812 | | /* check if reached exact end */ |
2813 | 11.9k | if ( !BIT_endOfDStream(&(seqState.DStream)) ) return ERROR(corruption_detected); /* requested too much : data is corrupted */ |
2814 | 11.7k | if (nbSeq<0) return ERROR(corruption_detected); /* requested too many sequences : data is corrupted */ |
2815 | | |
2816 | | /* last literal segment */ |
2817 | 11.7k | { |
2818 | 11.7k | size_t lastLLSize = litEnd - litPtr; |
2819 | 11.7k | if (litPtr > litEnd) return ERROR(corruption_detected); |
2820 | 11.7k | if (op+lastLLSize > oend) return ERROR(dstSize_tooSmall); |
2821 | 11.7k | if (lastLLSize > 0) { |
2822 | 5.18k | if (op != litPtr) memmove(op, litPtr, lastLLSize); |
2823 | 5.18k | op += lastLLSize; |
2824 | 5.18k | } |
2825 | 11.7k | } |
2826 | 11.7k | } |
2827 | | |
2828 | 0 | return op-ostart; |
2829 | 11.7k | } |
2830 | | |
2831 | | |
2832 | | static size_t ZSTD_decompressBlock( |
2833 | | void* ctx, |
2834 | | void* dst, size_t maxDstSize, |
2835 | | const void* src, size_t srcSize) |
2836 | 14.7k | { |
2837 | | /* blockType == blockCompressed */ |
2838 | 14.7k | const BYTE* ip = (const BYTE*)src; |
2839 | | |
2840 | | /* Decode literals sub-block */ |
2841 | 14.7k | size_t litCSize = ZSTD_decodeLiteralsBlock(ctx, src, srcSize); |
2842 | 14.7k | if (ZSTD_isError(litCSize)) return litCSize; |
2843 | 12.8k | ip += litCSize; |
2844 | 12.8k | srcSize -= litCSize; |
2845 | | |
2846 | 12.8k | return ZSTD_decompressSequences(ctx, dst, maxDstSize, ip, srcSize); |
2847 | 14.7k | } |
2848 | | |
2849 | | |
2850 | | static size_t ZSTD_decompressDCtx(void* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize) |
2851 | 4.54k | { |
2852 | 4.54k | const BYTE* ip = (const BYTE*)src; |
2853 | 4.54k | const BYTE* iend = ip + srcSize; |
2854 | 4.54k | BYTE* const ostart = (BYTE* const)dst; |
2855 | 4.54k | BYTE* op = ostart; |
2856 | 4.54k | BYTE* const oend = ostart + maxDstSize; |
2857 | 4.54k | size_t remainingSize = srcSize; |
2858 | 4.54k | U32 magicNumber; |
2859 | 4.54k | blockProperties_t blockProperties; |
2860 | | |
2861 | | /* Frame Header */ |
2862 | 4.54k | if (srcSize < ZSTD_frameHeaderSize+ZSTD_blockHeaderSize) return ERROR(srcSize_wrong); |
2863 | 4.54k | magicNumber = MEM_readLE32(src); |
2864 | 4.54k | if (magicNumber != ZSTD_magicNumber) return ERROR(prefix_unknown); |
2865 | 4.54k | ip += ZSTD_frameHeaderSize; remainingSize -= ZSTD_frameHeaderSize; |
2866 | | |
2867 | | /* Loop on each block */ |
2868 | 20.2k | while (1) |
2869 | 20.2k | { |
2870 | 20.2k | size_t decodedSize=0; |
2871 | 20.2k | size_t cBlockSize = ZSTD_getcBlockSize(ip, iend-ip, &blockProperties); |
2872 | 20.2k | if (ZSTD_isError(cBlockSize)) return cBlockSize; |
2873 | | |
2874 | 20.2k | ip += ZSTD_blockHeaderSize; |
2875 | 20.2k | remainingSize -= ZSTD_blockHeaderSize; |
2876 | 20.2k | if (cBlockSize > remainingSize) return ERROR(srcSize_wrong); |
2877 | | |
2878 | 20.2k | switch(blockProperties.blockType) |
2879 | 20.2k | { |
2880 | 14.7k | case bt_compressed: |
2881 | 14.7k | decodedSize = ZSTD_decompressBlock(ctx, op, oend-op, ip, cBlockSize); |
2882 | 14.7k | break; |
2883 | 4.47k | case bt_raw : |
2884 | 4.47k | decodedSize = ZSTD_copyUncompressedBlock(op, oend-op, ip, cBlockSize); |
2885 | 4.47k | break; |
2886 | 41 | case bt_rle : |
2887 | 41 | return ERROR(GENERIC); /* not yet supported */ |
2888 | 0 | break; |
2889 | 923 | case bt_end : |
2890 | | /* end of frame */ |
2891 | 923 | if (remainingSize) return ERROR(srcSize_wrong); |
2892 | 923 | break; |
2893 | 923 | default: |
2894 | 0 | return ERROR(GENERIC); /* impossible */ |
2895 | 20.2k | } |
2896 | 20.1k | if (cBlockSize == 0) break; /* bt_end */ |
2897 | | |
2898 | 18.1k | if (ZSTD_isError(decodedSize)) return decodedSize; |
2899 | 15.6k | op += decodedSize; |
2900 | 15.6k | ip += cBlockSize; |
2901 | 15.6k | remainingSize -= cBlockSize; |
2902 | 15.6k | } |
2903 | | |
2904 | 2.06k | return op-ostart; |
2905 | 4.54k | } |
2906 | | |
2907 | | static size_t ZSTD_decompress(void* dst, size_t maxDstSize, const void* src, size_t srcSize) |
2908 | 4.54k | { |
2909 | 4.54k | ZSTD_DCtx ctx; |
2910 | 4.54k | ctx.base = dst; |
2911 | 4.54k | return ZSTD_decompressDCtx(&ctx, dst, maxDstSize, src, srcSize); |
2912 | 4.54k | } |
2913 | | |
2914 | | /* ZSTD_errorFrameSizeInfoLegacy() : |
2915 | | assumes `cSize` and `dBound` are _not_ NULL */ |
2916 | | static void ZSTD_errorFrameSizeInfoLegacy(size_t* cSize, unsigned long long* dBound, size_t ret) |
2917 | 402 | { |
2918 | 402 | *cSize = ret; |
2919 | 402 | *dBound = ZSTD_CONTENTSIZE_ERROR; |
2920 | 402 | } |
2921 | | |
2922 | | void ZSTDv02_findFrameSizeInfoLegacy(const void *src, size_t srcSize, size_t* cSize, unsigned long long* dBound) |
2923 | 5.84k | { |
2924 | 5.84k | const BYTE* ip = (const BYTE*)src; |
2925 | 5.84k | size_t remainingSize = srcSize; |
2926 | 5.84k | size_t nbBlocks = 0; |
2927 | 5.84k | U32 magicNumber; |
2928 | 5.84k | blockProperties_t blockProperties; |
2929 | | |
2930 | | /* Frame Header */ |
2931 | 5.84k | if (srcSize < ZSTD_frameHeaderSize+ZSTD_blockHeaderSize) { |
2932 | 30 | ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(srcSize_wrong)); |
2933 | 30 | return; |
2934 | 30 | } |
2935 | 5.81k | magicNumber = MEM_readLE32(src); |
2936 | 5.81k | if (magicNumber != ZSTD_magicNumber) { |
2937 | 0 | ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(prefix_unknown)); |
2938 | 0 | return; |
2939 | 0 | } |
2940 | 5.81k | ip += ZSTD_frameHeaderSize; remainingSize -= ZSTD_frameHeaderSize; |
2941 | | |
2942 | | /* Loop on each block */ |
2943 | 25.8k | while (1) |
2944 | 25.8k | { |
2945 | 25.8k | size_t cBlockSize = ZSTD_getcBlockSize(ip, remainingSize, &blockProperties); |
2946 | 25.8k | if (ZSTD_isError(cBlockSize)) { |
2947 | 145 | ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, cBlockSize); |
2948 | 145 | return; |
2949 | 145 | } |
2950 | | |
2951 | 25.7k | ip += ZSTD_blockHeaderSize; |
2952 | 25.7k | remainingSize -= ZSTD_blockHeaderSize; |
2953 | 25.7k | if (cBlockSize > remainingSize) { |
2954 | 227 | ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(srcSize_wrong)); |
2955 | 227 | return; |
2956 | 227 | } |
2957 | | |
2958 | 25.5k | if (cBlockSize == 0) break; /* bt_end */ |
2959 | | |
2960 | 20.0k | ip += cBlockSize; |
2961 | 20.0k | remainingSize -= cBlockSize; |
2962 | 20.0k | nbBlocks++; |
2963 | 20.0k | } |
2964 | | |
2965 | 5.44k | *cSize = ip - (const BYTE*)src; |
2966 | 5.44k | *dBound = nbBlocks * BLOCKSIZE; |
2967 | 5.44k | } |
2968 | | |
2969 | | /******************************* |
2970 | | * Streaming Decompression API |
2971 | | *******************************/ |
2972 | | |
2973 | | static size_t ZSTD_resetDCtx(ZSTD_DCtx* dctx) |
2974 | 0 | { |
2975 | 0 | dctx->expected = ZSTD_frameHeaderSize; |
2976 | 0 | dctx->phase = 0; |
2977 | 0 | dctx->previousDstEnd = NULL; |
2978 | 0 | dctx->base = NULL; |
2979 | 0 | return 0; |
2980 | 0 | } |
2981 | | |
2982 | | static ZSTD_DCtx* ZSTD_createDCtx(void) |
2983 | 0 | { |
2984 | 0 | ZSTD_DCtx* dctx = (ZSTD_DCtx*)malloc(sizeof(ZSTD_DCtx)); |
2985 | 0 | if (dctx==NULL) return NULL; |
2986 | 0 | ZSTD_resetDCtx(dctx); |
2987 | 0 | return dctx; |
2988 | 0 | } |
2989 | | |
2990 | | static size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx) |
2991 | 0 | { |
2992 | 0 | free(dctx); |
2993 | 0 | return 0; |
2994 | 0 | } |
2995 | | |
2996 | | static size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx) |
2997 | 0 | { |
2998 | 0 | return dctx->expected; |
2999 | 0 | } |
3000 | | |
3001 | | static size_t ZSTD_decompressContinue(ZSTD_DCtx* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize) |
3002 | 0 | { |
3003 | | /* Sanity check */ |
3004 | 0 | if (srcSize != ctx->expected) return ERROR(srcSize_wrong); |
3005 | 0 | if (dst != ctx->previousDstEnd) /* not contiguous */ |
3006 | 0 | ctx->base = dst; |
3007 | | |
3008 | | /* Decompress : frame header */ |
3009 | 0 | if (ctx->phase == 0) |
3010 | 0 | { |
3011 | | /* Check frame magic header */ |
3012 | 0 | U32 magicNumber = MEM_readLE32(src); |
3013 | 0 | if (magicNumber != ZSTD_magicNumber) return ERROR(prefix_unknown); |
3014 | 0 | ctx->phase = 1; |
3015 | 0 | ctx->expected = ZSTD_blockHeaderSize; |
3016 | 0 | return 0; |
3017 | 0 | } |
3018 | | |
3019 | | /* Decompress : block header */ |
3020 | 0 | if (ctx->phase == 1) |
3021 | 0 | { |
3022 | 0 | blockProperties_t bp; |
3023 | 0 | size_t blockSize = ZSTD_getcBlockSize(src, ZSTD_blockHeaderSize, &bp); |
3024 | 0 | if (ZSTD_isError(blockSize)) return blockSize; |
3025 | 0 | if (bp.blockType == bt_end) |
3026 | 0 | { |
3027 | 0 | ctx->expected = 0; |
3028 | 0 | ctx->phase = 0; |
3029 | 0 | } |
3030 | 0 | else |
3031 | 0 | { |
3032 | 0 | ctx->expected = blockSize; |
3033 | 0 | ctx->bType = bp.blockType; |
3034 | 0 | ctx->phase = 2; |
3035 | 0 | } |
3036 | |
|
3037 | 0 | return 0; |
3038 | 0 | } |
3039 | | |
3040 | | /* Decompress : block content */ |
3041 | 0 | { |
3042 | 0 | size_t rSize; |
3043 | 0 | switch(ctx->bType) |
3044 | 0 | { |
3045 | 0 | case bt_compressed: |
3046 | 0 | rSize = ZSTD_decompressBlock(ctx, dst, maxDstSize, src, srcSize); |
3047 | 0 | break; |
3048 | 0 | case bt_raw : |
3049 | 0 | rSize = ZSTD_copyUncompressedBlock(dst, maxDstSize, src, srcSize); |
3050 | 0 | break; |
3051 | 0 | case bt_rle : |
3052 | 0 | return ERROR(GENERIC); /* not yet handled */ |
3053 | 0 | break; |
3054 | 0 | case bt_end : /* should never happen (filtered at phase 1) */ |
3055 | 0 | rSize = 0; |
3056 | 0 | break; |
3057 | 0 | default: |
3058 | 0 | return ERROR(GENERIC); |
3059 | 0 | } |
3060 | 0 | ctx->phase = 1; |
3061 | 0 | ctx->expected = ZSTD_blockHeaderSize; |
3062 | 0 | if (ZSTD_isError(rSize)) return rSize; |
3063 | 0 | ctx->previousDstEnd = (void*)( ((char*)dst) + rSize); |
3064 | 0 | return rSize; |
3065 | 0 | } |
3066 | |
|
3067 | 0 | } |
3068 | | |
3069 | | |
3070 | | /* wrapper layer */ |
3071 | | |
3072 | | unsigned ZSTDv02_isError(size_t code) |
3073 | 0 | { |
3074 | 0 | return ZSTD_isError(code); |
3075 | 0 | } |
3076 | | |
3077 | | size_t ZSTDv02_decompress( void* dst, size_t maxOriginalSize, |
3078 | | const void* src, size_t compressedSize) |
3079 | 4.54k | { |
3080 | 4.54k | return ZSTD_decompress(dst, maxOriginalSize, src, compressedSize); |
3081 | 4.54k | } |
3082 | | |
3083 | | ZSTDv02_Dctx* ZSTDv02_createDCtx(void) |
3084 | 0 | { |
3085 | 0 | return (ZSTDv02_Dctx*)ZSTD_createDCtx(); |
3086 | 0 | } |
3087 | | |
3088 | | size_t ZSTDv02_freeDCtx(ZSTDv02_Dctx* dctx) |
3089 | 0 | { |
3090 | 0 | return ZSTD_freeDCtx((ZSTD_DCtx*)dctx); |
3091 | 0 | } |
3092 | | |
3093 | | size_t ZSTDv02_resetDCtx(ZSTDv02_Dctx* dctx) |
3094 | 0 | { |
3095 | 0 | return ZSTD_resetDCtx((ZSTD_DCtx*)dctx); |
3096 | 0 | } |
3097 | | |
3098 | | size_t ZSTDv02_nextSrcSizeToDecompress(ZSTDv02_Dctx* dctx) |
3099 | 0 | { |
3100 | 0 | return ZSTD_nextSrcSizeToDecompress((ZSTD_DCtx*)dctx); |
3101 | 0 | } |
3102 | | |
3103 | | size_t ZSTDv02_decompressContinue(ZSTDv02_Dctx* dctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize) |
3104 | 0 | { |
3105 | 0 | return ZSTD_decompressContinue((ZSTD_DCtx*)dctx, dst, maxDstSize, src, srcSize); |
3106 | 0 | } |