2 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
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.
12 #include <stddef.h> /* size_t, ptrdiff_t */
14 #include "error_private.h"
17 /******************************************
19 ******************************************/
20 #if defined(_MSC_VER) /* Visual Studio */
21 # include <stdlib.h> /* _byteswap_ulong */
22 # include <intrin.h> /* _byteswap_* */
27 /* ******************************************************************
29 low-level memory access routines
30 Copyright (C) 2013-2015, Yann Collet.
32 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
34 Redistribution and use in source and binary forms, with or without
35 modification, are permitted provided that the following conditions are
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
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.
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 ****************************************************************** */
64 #if defined (__cplusplus)
68 /******************************************
70 ******************************************/
71 #include <stddef.h> /* size_t, ptrdiff_t */
72 #include <string.h> /* memcpy */
75 /******************************************
77 ******************************************/
79 # define MEM_STATIC static __attribute__((unused))
80 #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
81 # define MEM_STATIC static inline
82 #elif defined(_MSC_VER)
83 # define MEM_STATIC static __inline
85 # define MEM_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
89 /****************************************************************
91 *****************************************************************/
92 #if defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
102 typedef unsigned char BYTE;
103 typedef unsigned short U16;
104 typedef signed short S16;
105 typedef unsigned int U32;
106 typedef signed int S32;
107 typedef unsigned long long U64;
108 typedef signed long long S64;
112 /****************************************************************
114 *****************************************************************/
115 /* MEM_FORCE_MEMORY_ACCESS
116 * By default, access to unaligned memory is controlled by `memcpy()`, which is safe and portable.
117 * Unfortunately, on some target/compiler combinations, the generated assembly is sub-optimal.
118 * The below switch allow to select different access method for improved performance.
119 * Method 0 (default) : use `memcpy()`. Safe and portable.
120 * Method 1 : `__packed` statement. It depends on compiler extension (ie, not portable).
121 * This method is safe if your compiler supports it, and *generally* as fast or faster than `memcpy`.
122 * Method 2 : direct access. This method is portable but violate C standard.
123 * It can generate buggy code on targets generating assembly depending on alignment.
124 * But in some circumstances, it's the only known way to get the most performance (ie GCC + ARMv6)
125 * See http://fastcompression.blogspot.fr/2015/08/accessing-unaligned-memory.html for details.
126 * Prefer these methods in priority order (0 > 1 > 2)
128 #ifndef MEM_FORCE_MEMORY_ACCESS /* can be defined externally, on command line for example */
129 # if defined(__GNUC__) && ( defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__) )
130 # define MEM_FORCE_MEMORY_ACCESS 2
131 # elif (defined(__INTEL_COMPILER) && !defined(WIN32)) || \
132 (defined(__GNUC__) && ( defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7S__) ))
133 # define MEM_FORCE_MEMORY_ACCESS 1
137 MEM_STATIC unsigned MEM_32bits(void) { return sizeof(void*)==4; }
138 MEM_STATIC unsigned MEM_64bits(void) { return sizeof(void*)==8; }
140 MEM_STATIC unsigned MEM_isLittleEndian(void)
142 const union { U32 u; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */
146 #if defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS==2)
148 /* violates C standard on structure alignment.
149 Only use if no other choice to achieve best performance on target platform */
150 MEM_STATIC U16 MEM_read16(const void* memPtr) { return *(const U16*) memPtr; }
151 MEM_STATIC U32 MEM_read32(const void* memPtr) { return *(const U32*) memPtr; }
152 MEM_STATIC U64 MEM_read64(const void* memPtr) { return *(const U64*) memPtr; }
154 MEM_STATIC void MEM_write16(void* memPtr, U16 value) { *(U16*)memPtr = value; }
156 #elif defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS==1)
158 /* __pack instructions are safer, but compiler specific, hence potentially problematic for some compilers */
159 /* currently only defined for gcc and icc */
160 typedef union { U16 u16; U32 u32; U64 u64; } __attribute__((packed)) unalign;
162 MEM_STATIC U16 MEM_read16(const void* ptr) { return ((const unalign*)ptr)->u16; }
163 MEM_STATIC U32 MEM_read32(const void* ptr) { return ((const unalign*)ptr)->u32; }
164 MEM_STATIC U64 MEM_read64(const void* ptr) { return ((const unalign*)ptr)->u64; }
166 MEM_STATIC void MEM_write16(void* memPtr, U16 value) { ((unalign*)memPtr)->u16 = value; }
170 /* default method, safe and standard.
171 can sometimes prove slower */
173 MEM_STATIC U16 MEM_read16(const void* memPtr)
175 U16 val; memcpy(&val, memPtr, sizeof(val)); return val;
178 MEM_STATIC U32 MEM_read32(const void* memPtr)
180 U32 val; memcpy(&val, memPtr, sizeof(val)); return val;
183 MEM_STATIC U64 MEM_read64(const void* memPtr)
185 U64 val; memcpy(&val, memPtr, sizeof(val)); return val;
188 MEM_STATIC void MEM_write16(void* memPtr, U16 value)
190 memcpy(memPtr, &value, sizeof(value));
194 #endif // MEM_FORCE_MEMORY_ACCESS
197 MEM_STATIC U16 MEM_readLE16(const void* memPtr)
199 if (MEM_isLittleEndian())
200 return MEM_read16(memPtr);
203 const BYTE* p = (const BYTE*)memPtr;
204 return (U16)(p[0] + (p[1]<<8));
208 MEM_STATIC void MEM_writeLE16(void* memPtr, U16 val)
210 if (MEM_isLittleEndian())
212 MEM_write16(memPtr, val);
216 BYTE* p = (BYTE*)memPtr;
218 p[1] = (BYTE)(val>>8);
222 MEM_STATIC U32 MEM_readLE32(const void* memPtr)
224 if (MEM_isLittleEndian())
225 return MEM_read32(memPtr);
228 const BYTE* p = (const BYTE*)memPtr;
229 return (U32)((U32)p[0] + ((U32)p[1]<<8) + ((U32)p[2]<<16) + ((U32)p[3]<<24));
233 MEM_STATIC U64 MEM_readLE64(const void* memPtr)
235 if (MEM_isLittleEndian())
236 return MEM_read64(memPtr);
239 const BYTE* p = (const BYTE*)memPtr;
240 return (U64)((U64)p[0] + ((U64)p[1]<<8) + ((U64)p[2]<<16) + ((U64)p[3]<<24)
241 + ((U64)p[4]<<32) + ((U64)p[5]<<40) + ((U64)p[6]<<48) + ((U64)p[7]<<56));
246 MEM_STATIC size_t MEM_readLEST(const void* memPtr)
249 return (size_t)MEM_readLE32(memPtr);
251 return (size_t)MEM_readLE64(memPtr);
255 #if defined (__cplusplus)
259 #endif /* MEM_H_MODULE */
262 /* ******************************************************************
264 Part of NewGen Entropy library
265 header file (to include)
266 Copyright (C) 2013-2015, Yann Collet.
268 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
270 Redistribution and use in source and binary forms, with or without
271 modification, are permitted provided that the following conditions are
274 * Redistributions of source code must retain the above copyright
275 notice, this list of conditions and the following disclaimer.
276 * Redistributions in binary form must reproduce the above
277 copyright notice, this list of conditions and the following disclaimer
278 in the documentation and/or other materials provided with the
281 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
282 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
283 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
284 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
285 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
286 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
287 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
288 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
289 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
290 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
291 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
293 You can contact the author at :
294 - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
295 - Public forum : https://groups.google.com/forum/#!forum/lz4c
296 ****************************************************************** */
297 #ifndef BITSTREAM_H_MODULE
298 #define BITSTREAM_H_MODULE
300 #if defined (__cplusplus)
306 * This API consists of small unitary functions, which highly benefit from being inlined.
307 * Since link-time-optimization is not available for all compilers,
308 * these functions are defined into a .h to be included.
312 /**********************************************
313 * bitStream decompression API (read backward)
314 **********************************************/
318 unsigned bitsConsumed;
323 typedef enum { BIT_DStream_unfinished = 0,
324 BIT_DStream_endOfBuffer = 1,
325 BIT_DStream_completed = 2,
326 BIT_DStream_overflow = 3 } BIT_DStream_status; /* result of BIT_reloadDStream() */
327 /* 1,2,4,8 would be better for bitmap combinations, but slows down performance a bit ... :( */
329 MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize);
330 MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, unsigned nbBits);
331 MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD);
332 MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* bitD);
336 /******************************************
338 ******************************************/
339 MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits);
340 /* faster, but works only if nbBits >= 1 */
344 /****************************************************************
346 ****************************************************************/
347 MEM_STATIC unsigned BIT_highbit32 (U32 val)
349 # if defined(_MSC_VER) /* Visual */
351 _BitScanReverse ( &r, val );
353 # elif defined(__GNUC__) && (__GNUC__ >= 3) /* Use GCC Intrinsic */
354 return 31 - __builtin_clz (val);
355 # else /* Software version */
356 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 };
364 r = DeBruijnClz[ (U32) (v * 0x07C4ACDDU) >> 27];
371 /**********************************************************
373 **********************************************************/
376 * Initialize a BIT_DStream_t.
377 * @bitD : a pointer to an already allocated BIT_DStream_t structure
378 * @srcBuffer must point at the beginning of a bitStream
379 * @srcSize must be the exact size of the bitStream
380 * @result : size of stream (== srcSize) or an errorCode if a problem is detected
382 MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize)
384 if (srcSize < 1) { memset(bitD, 0, sizeof(*bitD)); return ERROR(srcSize_wrong); }
386 if (srcSize >= sizeof(size_t)) /* normal case */
389 bitD->start = (const char*)srcBuffer;
390 bitD->ptr = (const char*)srcBuffer + srcSize - sizeof(size_t);
391 bitD->bitContainer = MEM_readLEST(bitD->ptr);
392 contain32 = ((const BYTE*)srcBuffer)[srcSize-1];
393 if (contain32 == 0) return ERROR(GENERIC); /* endMark not present */
394 bitD->bitsConsumed = 8 - BIT_highbit32(contain32);
399 bitD->start = (const char*)srcBuffer;
400 bitD->ptr = bitD->start;
401 bitD->bitContainer = *(const BYTE*)(bitD->start);
404 case 7: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[6]) << (sizeof(size_t)*8 - 16);
406 case 6: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[5]) << (sizeof(size_t)*8 - 24);
408 case 5: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[4]) << (sizeof(size_t)*8 - 32);
410 case 4: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[3]) << 24;
412 case 3: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[2]) << 16;
414 case 2: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[1]) << 8;
418 contain32 = ((const BYTE*)srcBuffer)[srcSize-1];
419 if (contain32 == 0) return ERROR(GENERIC); /* endMark not present */
420 bitD->bitsConsumed = 8 - BIT_highbit32(contain32);
421 bitD->bitsConsumed += (U32)(sizeof(size_t) - srcSize)*8;
426 MEM_STATIC size_t BIT_lookBits(BIT_DStream_t* bitD, U32 nbBits)
428 const U32 bitMask = sizeof(bitD->bitContainer)*8 - 1;
429 return ((bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> 1) >> ((bitMask-nbBits) & bitMask);
432 /*! BIT_lookBitsFast :
433 * unsafe version; only works only if nbBits >= 1 */
434 MEM_STATIC size_t BIT_lookBitsFast(BIT_DStream_t* bitD, U32 nbBits)
436 const U32 bitMask = sizeof(bitD->bitContainer)*8 - 1;
437 return (bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> (((bitMask+1)-nbBits) & bitMask);
440 MEM_STATIC void BIT_skipBits(BIT_DStream_t* bitD, U32 nbBits)
442 bitD->bitsConsumed += nbBits;
445 MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, U32 nbBits)
447 size_t value = BIT_lookBits(bitD, nbBits);
448 BIT_skipBits(bitD, nbBits);
452 /*!BIT_readBitsFast :
453 * unsafe version; only works only if nbBits >= 1 */
454 MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, U32 nbBits)
456 size_t value = BIT_lookBitsFast(bitD, nbBits);
457 BIT_skipBits(bitD, nbBits);
461 MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD)
463 if (bitD->bitsConsumed > (sizeof(bitD->bitContainer)*8)) /* should never happen */
464 return BIT_DStream_overflow;
466 if (bitD->ptr >= bitD->start + sizeof(bitD->bitContainer))
468 bitD->ptr -= bitD->bitsConsumed >> 3;
469 bitD->bitsConsumed &= 7;
470 bitD->bitContainer = MEM_readLEST(bitD->ptr);
471 return BIT_DStream_unfinished;
473 if (bitD->ptr == bitD->start)
475 if (bitD->bitsConsumed < sizeof(bitD->bitContainer)*8) return BIT_DStream_endOfBuffer;
476 return BIT_DStream_completed;
479 U32 nbBytes = bitD->bitsConsumed >> 3;
480 BIT_DStream_status result = BIT_DStream_unfinished;
481 if (bitD->ptr - nbBytes < bitD->start)
483 nbBytes = (U32)(bitD->ptr - bitD->start); /* ptr > start */
484 result = BIT_DStream_endOfBuffer;
486 bitD->ptr -= nbBytes;
487 bitD->bitsConsumed -= nbBytes*8;
488 bitD->bitContainer = MEM_readLEST(bitD->ptr); /* reminder : srcSize > sizeof(bitD) */
494 * @return Tells if DStream has reached its exact end
496 MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* DStream)
498 return ((DStream->ptr == DStream->start) && (DStream->bitsConsumed == sizeof(DStream->bitContainer)*8));
501 #if defined (__cplusplus)
505 #endif /* BITSTREAM_H_MODULE */
506 /* ******************************************************************
507 Error codes and messages
508 Copyright (C) 2013-2015, Yann Collet
510 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
512 Redistribution and use in source and binary forms, with or without
513 modification, are permitted provided that the following conditions are
516 * Redistributions of source code must retain the above copyright
517 notice, this list of conditions and the following disclaimer.
518 * Redistributions in binary form must reproduce the above
519 copyright notice, this list of conditions and the following disclaimer
520 in the documentation and/or other materials provided with the
523 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
524 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
525 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
526 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
527 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
528 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
529 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
530 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
531 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
532 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
533 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
535 You can contact the author at :
536 - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
537 - Public forum : https://groups.google.com/forum/#!forum/lz4c
538 ****************************************************************** */
539 #ifndef ERROR_H_MODULE
540 #define ERROR_H_MODULE
542 #if defined (__cplusplus)
547 /******************************************
549 ******************************************/
550 #if defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
551 # define ERR_STATIC static inline
552 #elif defined(_MSC_VER)
553 # define ERR_STATIC static __inline
554 #elif defined(__GNUC__)
555 # define ERR_STATIC static __attribute__((unused))
557 # define ERR_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
561 /******************************************
563 ******************************************/
564 #define PREFIX(name) ZSTD_error_##name
566 #define ERROR(name) (size_t)-PREFIX(name)
568 #define ERROR_LIST(ITEM) \
569 ITEM(PREFIX(No_Error)) ITEM(PREFIX(GENERIC)) \
570 ITEM(PREFIX(dstSize_tooSmall)) ITEM(PREFIX(srcSize_wrong)) \
571 ITEM(PREFIX(prefix_unknown)) ITEM(PREFIX(corruption_detected)) \
572 ITEM(PREFIX(tableLog_tooLarge)) ITEM(PREFIX(maxSymbolValue_tooLarge)) ITEM(PREFIX(maxSymbolValue_tooSmall)) \
573 ITEM(PREFIX(maxCode))
575 #define ERROR_GENERATE_ENUM(ENUM) ENUM,
576 typedef enum { ERROR_LIST(ERROR_GENERATE_ENUM) } ERR_codes; /* enum is exposed, to detect & handle specific errors; compare function result to -enum value */
578 #define ERROR_CONVERTTOSTRING(STRING) #STRING,
579 #define ERROR_GENERATE_STRING(EXPR) ERROR_CONVERTTOSTRING(EXPR)
580 static const char* ERR_strings[] = { ERROR_LIST(ERROR_GENERATE_STRING) };
582 ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); }
584 ERR_STATIC const char* ERR_getErrorName(size_t code)
586 static const char* codeError = "Unspecified error code";
587 if (ERR_isError(code)) return ERR_strings[-(int)(code)];
592 #if defined (__cplusplus)
596 #endif /* ERROR_H_MODULE */
598 Constructor and Destructor of type FSE_CTable
599 Note that its size depends on 'tableLog' and 'maxSymbolValue' */
600 typedef unsigned FSE_CTable; /* don't allocate that. It's just a way to be more restrictive than void* */
601 typedef unsigned FSE_DTable; /* don't allocate that. It's just a way to be more restrictive than void* */
604 /* ******************************************************************
605 FSE : Finite State Entropy coder
606 header file for static linking (only)
607 Copyright (C) 2013-2015, Yann Collet
609 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
611 Redistribution and use in source and binary forms, with or without
612 modification, are permitted provided that the following conditions are
615 * Redistributions of source code must retain the above copyright
616 notice, this list of conditions and the following disclaimer.
617 * Redistributions in binary form must reproduce the above
618 copyright notice, this list of conditions and the following disclaimer
619 in the documentation and/or other materials provided with the
622 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
623 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
624 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
625 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
626 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
627 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
628 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
629 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
630 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
631 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
632 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
634 You can contact the author at :
635 - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
636 - Public forum : https://groups.google.com/forum/#!forum/lz4c
637 ****************************************************************** */
638 #if defined (__cplusplus)
643 /******************************************
645 ******************************************/
646 /* FSE buffer bounds */
647 #define FSE_NCOUNTBOUND 512
648 #define FSE_BLOCKBOUND(size) (size + (size>>7))
649 #define FSE_COMPRESSBOUND(size) (FSE_NCOUNTBOUND + FSE_BLOCKBOUND(size)) /* Macro version, useful for static allocation */
651 /* You can statically allocate FSE CTable/DTable as a table of unsigned using below macro */
652 #define FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue) (1 + (1<<(maxTableLog-1)) + ((maxSymbolValue+1)*2))
653 #define FSE_DTABLE_SIZE_U32(maxTableLog) (1 + (1<<maxTableLog))
656 /******************************************
658 ******************************************/
659 static size_t FSE_buildDTable_raw (FSE_DTable* dt, unsigned nbBits);
660 /* build a fake FSE_DTable, designed to read an uncompressed bitstream where each symbol uses nbBits */
662 static size_t FSE_buildDTable_rle (FSE_DTable* dt, unsigned char symbolValue);
663 /* build a fake FSE_DTable, designed to always generate the same symbolValue */
666 /******************************************
667 * FSE symbol decompression API
668 ******************************************/
672 const void* table; /* precise table may vary, depending on U16 */
676 static void FSE_initDState(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD, const FSE_DTable* dt);
678 static unsigned char FSE_decodeSymbol(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD);
680 static unsigned FSE_endOfDState(const FSE_DState_t* DStatePtr);
683 /******************************************
685 ******************************************/
686 static unsigned char FSE_decodeSymbolFast(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD);
687 /* faster, but works only if nbBits is always >= 1 (otherwise, result will be corrupted) */
690 /******************************************
691 * Implementation of inline functions
692 ******************************************/
699 } FSE_DTableHeader; /* sizeof U32 */
703 unsigned short newState;
704 unsigned char symbol;
705 unsigned char nbBits;
706 } FSE_decode_t; /* size == U32 */
708 MEM_STATIC void FSE_initDState(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD, const FSE_DTable* dt)
710 FSE_DTableHeader DTableH;
711 memcpy(&DTableH, dt, sizeof(DTableH));
712 DStatePtr->state = BIT_readBits(bitD, DTableH.tableLog);
713 BIT_reloadDStream(bitD);
714 DStatePtr->table = dt + 1;
717 MEM_STATIC BYTE FSE_decodeSymbol(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD)
719 const FSE_decode_t DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state];
720 const U32 nbBits = DInfo.nbBits;
721 BYTE symbol = DInfo.symbol;
722 size_t lowBits = BIT_readBits(bitD, nbBits);
724 DStatePtr->state = DInfo.newState + lowBits;
728 MEM_STATIC BYTE FSE_decodeSymbolFast(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD)
730 const FSE_decode_t DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state];
731 const U32 nbBits = DInfo.nbBits;
732 BYTE symbol = DInfo.symbol;
733 size_t lowBits = BIT_readBitsFast(bitD, nbBits);
735 DStatePtr->state = DInfo.newState + lowBits;
739 MEM_STATIC unsigned FSE_endOfDState(const FSE_DState_t* DStatePtr)
741 return DStatePtr->state == 0;
745 #if defined (__cplusplus)
748 /* ******************************************************************
749 Huff0 : Huffman coder, part of New Generation Entropy library
750 header file for static linking (only)
751 Copyright (C) 2013-2015, Yann Collet
753 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
755 Redistribution and use in source and binary forms, with or without
756 modification, are permitted provided that the following conditions are
759 * Redistributions of source code must retain the above copyright
760 notice, this list of conditions and the following disclaimer.
761 * Redistributions in binary form must reproduce the above
762 copyright notice, this list of conditions and the following disclaimer
763 in the documentation and/or other materials provided with the
766 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
767 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
768 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
769 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
770 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
771 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
772 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
773 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
774 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
775 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
776 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
778 You can contact the author at :
779 - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
780 - Public forum : https://groups.google.com/forum/#!forum/lz4c
781 ****************************************************************** */
783 #if defined (__cplusplus)
787 /******************************************
788 * Static allocation macros
789 ******************************************/
790 /* Huff0 buffer bounds */
791 #define HUF_CTABLEBOUND 129
792 #define HUF_BLOCKBOUND(size) (size + (size>>8) + 8) /* only true if incompressible pre-filtered with fast heuristic */
793 #define HUF_COMPRESSBOUND(size) (HUF_CTABLEBOUND + HUF_BLOCKBOUND(size)) /* Macro version, useful for static allocation */
795 /* static allocation of Huff0's DTable */
796 #define HUF_DTABLE_SIZE(maxTableLog) (1 + (1<<maxTableLog)) /* nb Cells; use unsigned short for X2, unsigned int for X4 */
797 #define HUF_CREATE_STATIC_DTABLEX2(DTable, maxTableLog) \
798 unsigned short DTable[HUF_DTABLE_SIZE(maxTableLog)] = { maxTableLog }
799 #define HUF_CREATE_STATIC_DTABLEX4(DTable, maxTableLog) \
800 unsigned int DTable[HUF_DTABLE_SIZE(maxTableLog)] = { maxTableLog }
801 #define HUF_CREATE_STATIC_DTABLEX6(DTable, maxTableLog) \
802 unsigned int DTable[HUF_DTABLE_SIZE(maxTableLog) * 3 / 2] = { maxTableLog }
805 /******************************************
807 ******************************************/
808 static size_t HUF_decompress4X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* single-symbol decoder */
809 static size_t HUF_decompress4X4 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* double-symbols decoder */
812 #if defined (__cplusplus)
817 zstd - standard compression library
819 Copyright (C) 2014-2015, Yann Collet.
821 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
823 Redistribution and use in source and binary forms, with or without
824 modification, are permitted provided that the following conditions are
826 * Redistributions of source code must retain the above copyright
827 notice, this list of conditions and the following disclaimer.
828 * Redistributions in binary form must reproduce the above
829 copyright notice, this list of conditions and the following disclaimer
830 in the documentation and/or other materials provided with the
832 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
833 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
834 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
835 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
836 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
837 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
838 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
839 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
840 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
841 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
842 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
844 You can contact the author at :
845 - zstd source repository : https://github.com/Cyan4973/zstd
846 - ztsd public forum : https://groups.google.com/forum/#!forum/lz4c
849 #if defined (__cplusplus)
853 /* *************************************
855 ***************************************/
856 #include <stddef.h> /* size_t */
859 /* *************************************
861 ***************************************/
862 #define ZSTD_VERSION_MAJOR 0 /* for breaking interface changes */
863 #define ZSTD_VERSION_MINOR 2 /* for new (non-breaking) interface capabilities */
864 #define ZSTD_VERSION_RELEASE 2 /* for tweaks, bug-fixes, or development */
865 #define ZSTD_VERSION_NUMBER (ZSTD_VERSION_MAJOR *100*100 + ZSTD_VERSION_MINOR *100 + ZSTD_VERSION_RELEASE)
868 /* *************************************
870 ***************************************/
871 typedef struct ZSTD_CCtx_s ZSTD_CCtx; /* incomplete type */
873 #if defined (__cplusplus)
877 zstd - standard compression library
878 Header File for static linking only
879 Copyright (C) 2014-2015, Yann Collet.
881 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
883 Redistribution and use in source and binary forms, with or without
884 modification, are permitted provided that the following conditions are
886 * Redistributions of source code must retain the above copyright
887 notice, this list of conditions and the following disclaimer.
888 * Redistributions in binary form must reproduce the above
889 copyright notice, this list of conditions and the following disclaimer
890 in the documentation and/or other materials provided with the
892 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
893 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
894 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
895 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
896 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
897 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
898 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
899 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
900 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
901 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
902 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
904 You can contact the author at :
905 - zstd source repository : https://github.com/Cyan4973/zstd
906 - ztsd public forum : https://groups.google.com/forum/#!forum/lz4c
909 /* The objects defined into this file should be considered experimental.
910 * They are not labelled stable, as their prototype may change in the future.
911 * You can use them for tests, provide feedback, or if you can endure risk of future changes.
914 #if defined (__cplusplus)
918 /* *************************************
919 * Streaming functions
920 ***************************************/
922 typedef struct ZSTD_DCtx_s ZSTD_DCtx;
925 Use above functions alternatively.
926 ZSTD_nextSrcSizeToDecompress() tells how much bytes to provide as 'srcSize' to ZSTD_decompressContinue().
927 ZSTD_decompressContinue() will use previous data blocks to improve compression if they are located prior to current block.
928 Result is the number of bytes regenerated within 'dst'.
929 It can be zero, which is not an error; it just means ZSTD_decompressContinue() has decoded some header.
932 /* *************************************
933 * Prefix - version detection
934 ***************************************/
935 #define ZSTD_magicNumber 0xFD2FB523 /* v0.3 */
938 #if defined (__cplusplus)
941 /* ******************************************************************
942 FSE : Finite State Entropy coder
943 Copyright (C) 2013-2015, Yann Collet.
945 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
947 Redistribution and use in source and binary forms, with or without
948 modification, are permitted provided that the following conditions are
951 * Redistributions of source code must retain the above copyright
952 notice, this list of conditions and the following disclaimer.
953 * Redistributions in binary form must reproduce the above
954 copyright notice, this list of conditions and the following disclaimer
955 in the documentation and/or other materials provided with the
958 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
959 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
960 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
961 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
962 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
963 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
964 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
965 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
966 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
967 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
968 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
970 You can contact the author at :
971 - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy
972 - Public forum : https://groups.google.com/forum/#!forum/lz4c
973 ****************************************************************** */
975 #ifndef FSE_COMMONDEFS_ONLY
977 /****************************************************************
979 ****************************************************************/
981 * Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.)
982 * Increasing memory usage improves compression ratio
983 * Reduced memory usage can improve speed, due to cache effect
984 * Recommended max value is 14, for 16KB, which nicely fits into Intel x86 L1 cache */
985 #define FSE_MAX_MEMORY_USAGE 14
986 #define FSE_DEFAULT_MEMORY_USAGE 13
988 /* FSE_MAX_SYMBOL_VALUE :
989 * Maximum symbol value authorized.
990 * Required for proper stack allocation */
991 #define FSE_MAX_SYMBOL_VALUE 255
994 /****************************************************************
995 * template functions type & suffix
996 ****************************************************************/
997 #define FSE_FUNCTION_TYPE BYTE
998 #define FSE_FUNCTION_EXTENSION
1001 /****************************************************************
1003 ****************************************************************/
1004 #endif /* !FSE_COMMONDEFS_ONLY */
1007 /****************************************************************
1008 * Compiler specifics
1009 ****************************************************************/
1010 #ifdef _MSC_VER /* Visual Studio */
1011 # define FORCE_INLINE static __forceinline
1012 # include <intrin.h> /* For Visual 2005 */
1013 # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
1014 # pragma warning(disable : 4214) /* disable: C4214: non-int bitfields */
1016 # if defined (__cplusplus) || defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
1018 # define FORCE_INLINE static inline __attribute__((always_inline))
1020 # define FORCE_INLINE static inline
1023 # define FORCE_INLINE static
1024 # endif /* __STDC_VERSION__ */
1028 /****************************************************************
1030 ****************************************************************/
1031 #include <stdlib.h> /* malloc, free, qsort */
1032 #include <string.h> /* memcpy, memset */
1033 #include <stdio.h> /* printf (debug) */
1035 /****************************************************************
1037 *****************************************************************/
1038 #define FSE_MAX_TABLELOG (FSE_MAX_MEMORY_USAGE-2)
1039 #define FSE_MAX_TABLESIZE (1U<<FSE_MAX_TABLELOG)
1040 #define FSE_MAXTABLESIZE_MASK (FSE_MAX_TABLESIZE-1)
1041 #define FSE_DEFAULT_TABLELOG (FSE_DEFAULT_MEMORY_USAGE-2)
1042 #define FSE_MIN_TABLELOG 5
1044 #define FSE_TABLELOG_ABSOLUTE_MAX 15
1045 #if FSE_MAX_TABLELOG > FSE_TABLELOG_ABSOLUTE_MAX
1046 #error "FSE_MAX_TABLELOG > FSE_TABLELOG_ABSOLUTE_MAX is not supported"
1050 /****************************************************************
1052 ****************************************************************/
1053 #define FSE_STATIC_ASSERT(c) { enum { FSE_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */
1056 /****************************************************************
1058 ****************************************************************/
1059 typedef U32 DTable_max_t[FSE_DTABLE_SIZE_U32(FSE_MAX_TABLELOG)];
1062 /****************************************************************
1064 ****************************************************************/
1066 designed to be included
1067 for type-specific functions (template emulation in C)
1068 Objective is to write these functions only once, for improved maintenance
1072 #ifndef FSE_FUNCTION_EXTENSION
1073 # error "FSE_FUNCTION_EXTENSION must be defined"
1075 #ifndef FSE_FUNCTION_TYPE
1076 # error "FSE_FUNCTION_TYPE must be defined"
1079 /* Function names */
1080 #define FSE_CAT(X,Y) X##Y
1081 #define FSE_FUNCTION_NAME(X,Y) FSE_CAT(X,Y)
1082 #define FSE_TYPE_NAME(X,Y) FSE_CAT(X,Y)
1085 /* Function templates */
1087 #define FSE_DECODE_TYPE FSE_decode_t
1089 static U32 FSE_tableStep(U32 tableSize) { return (tableSize>>1) + (tableSize>>3) + 3; }
1091 static size_t FSE_buildDTable
1092 (FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog)
1095 FSE_DTableHeader DTableH;
1096 FSE_DECODE_TYPE* const tableDecode = (FSE_DECODE_TYPE*)ptr;
1097 const U32 tableSize = 1 << tableLog;
1098 const U32 tableMask = tableSize-1;
1099 const U32 step = FSE_tableStep(tableSize);
1100 U16 symbolNext[FSE_MAX_SYMBOL_VALUE+1];
1102 U32 highThreshold = tableSize-1;
1103 const S16 largeLimit= (S16)(1 << (tableLog-1));
1108 if (maxSymbolValue > FSE_MAX_SYMBOL_VALUE) return ERROR(maxSymbolValue_tooLarge);
1109 if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge);
1111 /* Init, lay down lowprob symbols */
1112 DTableH.tableLog = (U16)tableLog;
1113 for (s=0; s<=maxSymbolValue; s++)
1115 if (normalizedCounter[s]==-1)
1117 tableDecode[highThreshold--].symbol = (FSE_FUNCTION_TYPE)s;
1122 if (normalizedCounter[s] >= largeLimit) noLarge=0;
1123 symbolNext[s] = normalizedCounter[s];
1127 /* Spread symbols */
1128 for (s=0; s<=maxSymbolValue; s++)
1131 for (i=0; i<normalizedCounter[s]; i++)
1133 tableDecode[position].symbol = (FSE_FUNCTION_TYPE)s;
1134 position = (position + step) & tableMask;
1135 while (position > highThreshold) position = (position + step) & tableMask; /* lowprob area */
1139 if (position!=0) return ERROR(GENERIC); /* position must reach all cells once, otherwise normalizedCounter is incorrect */
1141 /* Build Decoding table */
1144 for (i=0; i<tableSize; i++)
1146 FSE_FUNCTION_TYPE symbol = (FSE_FUNCTION_TYPE)(tableDecode[i].symbol);
1147 U16 nextState = symbolNext[symbol]++;
1148 tableDecode[i].nbBits = (BYTE) (tableLog - BIT_highbit32 ((U32)nextState) );
1149 tableDecode[i].newState = (U16) ( (nextState << tableDecode[i].nbBits) - tableSize);
1153 DTableH.fastMode = (U16)noLarge;
1154 memcpy(dt, &DTableH, sizeof(DTableH));
1159 #ifndef FSE_COMMONDEFS_ONLY
1160 /******************************************
1161 * FSE helper functions
1162 ******************************************/
1163 static unsigned FSE_isError(size_t code) { return ERR_isError(code); }
1166 /****************************************************************
1167 * FSE NCount encoding-decoding
1168 ****************************************************************/
1169 static short FSE_abs(short a)
1171 return a<0 ? -a : a;
1174 static size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
1175 const void* headerBuffer, size_t hbSize)
1177 const BYTE* const istart = (const BYTE*) headerBuffer;
1178 const BYTE* const iend = istart + hbSize;
1179 const BYTE* ip = istart;
1185 unsigned charnum = 0;
1188 if (hbSize < 4) return ERROR(srcSize_wrong);
1189 bitStream = MEM_readLE32(ip);
1190 nbBits = (bitStream & 0xF) + FSE_MIN_TABLELOG; /* extract tableLog */
1191 if (nbBits > FSE_TABLELOG_ABSOLUTE_MAX) return ERROR(tableLog_tooLarge);
1194 *tableLogPtr = nbBits;
1195 remaining = (1<<nbBits)+1;
1196 threshold = 1<<nbBits;
1199 while ((remaining>1) && (charnum<=*maxSVPtr))
1203 unsigned n0 = charnum;
1204 while ((bitStream & 0xFFFF) == 0xFFFF)
1210 bitStream = MEM_readLE32(ip) >> bitCount;
1218 while ((bitStream & 3) == 3)
1224 n0 += bitStream & 3;
1226 if (n0 > *maxSVPtr) return ERROR(maxSymbolValue_tooSmall);
1227 while (charnum < n0) normalizedCounter[charnum++] = 0;
1228 if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4))
1232 bitStream = MEM_readLE32(ip) >> bitCount;
1238 const short max = (short)((2*threshold-1)-remaining);
1241 if ((bitStream & (threshold-1)) < (U32)max)
1243 count = (short)(bitStream & (threshold-1));
1244 bitCount += nbBits-1;
1248 count = (short)(bitStream & (2*threshold-1));
1249 if (count >= threshold) count -= max;
1253 count--; /* extra accuracy */
1254 remaining -= FSE_abs(count);
1255 normalizedCounter[charnum++] = count;
1257 while (remaining < threshold)
1264 if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4))
1271 bitCount -= (int)(8 * (iend - 4 - ip));
1274 bitStream = MEM_readLE32(ip) >> (bitCount & 31);
1278 if (remaining != 1) return ERROR(GENERIC);
1279 *maxSVPtr = charnum-1;
1281 ip += (bitCount+7)>>3;
1282 if ((size_t)(ip-istart) > hbSize) return ERROR(srcSize_wrong);
1287 /*********************************************************
1288 * Decompression (Byte symbols)
1289 *********************************************************/
1290 static size_t FSE_buildDTable_rle (FSE_DTable* dt, BYTE symbolValue)
1293 FSE_DTableHeader* const DTableH = (FSE_DTableHeader*)ptr;
1294 FSE_decode_t* const cell = (FSE_decode_t*)(ptr) + 1;
1296 DTableH->tableLog = 0;
1297 DTableH->fastMode = 0;
1300 cell->symbol = symbolValue;
1307 static size_t FSE_buildDTable_raw (FSE_DTable* dt, unsigned nbBits)
1310 FSE_DTableHeader* const DTableH = (FSE_DTableHeader*)ptr;
1311 FSE_decode_t* const dinfo = (FSE_decode_t*)(ptr) + 1;
1312 const unsigned tableSize = 1 << nbBits;
1313 const unsigned tableMask = tableSize - 1;
1314 const unsigned maxSymbolValue = tableMask;
1318 if (nbBits < 1) return ERROR(GENERIC); /* min size */
1320 /* Build Decoding Table */
1321 DTableH->tableLog = (U16)nbBits;
1322 DTableH->fastMode = 1;
1323 for (s=0; s<=maxSymbolValue; s++)
1325 dinfo[s].newState = 0;
1326 dinfo[s].symbol = (BYTE)s;
1327 dinfo[s].nbBits = (BYTE)nbBits;
1333 FORCE_INLINE size_t FSE_decompress_usingDTable_generic(
1334 void* dst, size_t maxDstSize,
1335 const void* cSrc, size_t cSrcSize,
1336 const FSE_DTable* dt, const unsigned fast)
1338 BYTE* const ostart = (BYTE*) dst;
1340 BYTE* const omax = op + maxDstSize;
1341 BYTE* const olimit = omax-3;
1344 FSE_DState_t state1;
1345 FSE_DState_t state2;
1349 errorCode = BIT_initDStream(&bitD, cSrc, cSrcSize); /* replaced last arg by maxCompressed Size */
1350 if (FSE_isError(errorCode)) return errorCode;
1352 FSE_initDState(&state1, &bitD, dt);
1353 FSE_initDState(&state2, &bitD, dt);
1355 #define FSE_GETSYMBOL(statePtr) fast ? FSE_decodeSymbolFast(statePtr, &bitD) : FSE_decodeSymbol(statePtr, &bitD)
1357 /* 4 symbols per loop */
1358 for ( ; (BIT_reloadDStream(&bitD)==BIT_DStream_unfinished) && (op<olimit) ; op+=4)
1360 op[0] = FSE_GETSYMBOL(&state1);
1362 if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */
1363 BIT_reloadDStream(&bitD);
1365 op[1] = FSE_GETSYMBOL(&state2);
1367 if (FSE_MAX_TABLELOG*4+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */
1368 { if (BIT_reloadDStream(&bitD) > BIT_DStream_unfinished) { op+=2; break; } }
1370 op[2] = FSE_GETSYMBOL(&state1);
1372 if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */
1373 BIT_reloadDStream(&bitD);
1375 op[3] = FSE_GETSYMBOL(&state2);
1379 /* note : BIT_reloadDStream(&bitD) >= FSE_DStream_partiallyFilled; Ends at exactly BIT_DStream_completed */
1382 if ( (BIT_reloadDStream(&bitD)>BIT_DStream_completed) || (op==omax) || (BIT_endOfDStream(&bitD) && (fast || FSE_endOfDState(&state1))) )
1385 *op++ = FSE_GETSYMBOL(&state1);
1387 if ( (BIT_reloadDStream(&bitD)>BIT_DStream_completed) || (op==omax) || (BIT_endOfDStream(&bitD) && (fast || FSE_endOfDState(&state2))) )
1390 *op++ = FSE_GETSYMBOL(&state2);
1394 if (BIT_endOfDStream(&bitD) && FSE_endOfDState(&state1) && FSE_endOfDState(&state2))
1397 if (op==omax) return ERROR(dstSize_tooSmall); /* dst buffer is full, but cSrc unfinished */
1399 return ERROR(corruption_detected);
1403 static size_t FSE_decompress_usingDTable(void* dst, size_t originalSize,
1404 const void* cSrc, size_t cSrcSize,
1405 const FSE_DTable* dt)
1407 FSE_DTableHeader DTableH;
1408 memcpy(&DTableH, dt, sizeof(DTableH));
1410 /* select fast mode (static) */
1411 if (DTableH.fastMode) return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 1);
1412 return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 0);
1416 static size_t FSE_decompress(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize)
1418 const BYTE* const istart = (const BYTE*)cSrc;
1419 const BYTE* ip = istart;
1420 short counting[FSE_MAX_SYMBOL_VALUE+1];
1421 DTable_max_t dt; /* Static analyzer seems unable to understand this table will be properly initialized later */
1423 unsigned maxSymbolValue = FSE_MAX_SYMBOL_VALUE;
1426 if (cSrcSize<2) return ERROR(srcSize_wrong); /* too small input size */
1428 /* normal FSE decoding mode */
1429 errorCode = FSE_readNCount (counting, &maxSymbolValue, &tableLog, istart, cSrcSize);
1430 if (FSE_isError(errorCode)) return errorCode;
1431 if (errorCode >= cSrcSize) return ERROR(srcSize_wrong); /* too small input size */
1433 cSrcSize -= errorCode;
1435 errorCode = FSE_buildDTable (dt, counting, maxSymbolValue, tableLog);
1436 if (FSE_isError(errorCode)) return errorCode;
1438 /* always return, even if it is an error code */
1439 return FSE_decompress_usingDTable (dst, maxDstSize, ip, cSrcSize, dt);
1444 #endif /* FSE_COMMONDEFS_ONLY */
1445 /* ******************************************************************
1446 Huff0 : Huffman coder, part of New Generation Entropy library
1447 Copyright (C) 2013-2015, Yann Collet.
1449 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
1451 Redistribution and use in source and binary forms, with or without
1452 modification, are permitted provided that the following conditions are
1455 * Redistributions of source code must retain the above copyright
1456 notice, this list of conditions and the following disclaimer.
1457 * Redistributions in binary form must reproduce the above
1458 copyright notice, this list of conditions and the following disclaimer
1459 in the documentation and/or other materials provided with the
1462 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1463 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1464 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1465 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1466 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1467 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1468 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1469 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1470 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1471 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1472 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1474 You can contact the author at :
1475 - FSE+Huff0 source repository : https://github.com/Cyan4973/FiniteStateEntropy
1476 - Public forum : https://groups.google.com/forum/#!forum/lz4c
1477 ****************************************************************** */
1479 /****************************************************************
1480 * Compiler specifics
1481 ****************************************************************/
1482 #if defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
1483 /* inline is defined */
1484 #elif defined(_MSC_VER)
1485 # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
1486 # define inline __inline
1488 # define inline /* disable inline */
1492 /****************************************************************
1494 ****************************************************************/
1495 #include <stdlib.h> /* malloc, free, qsort */
1496 #include <string.h> /* memcpy, memset */
1497 #include <stdio.h> /* printf (debug) */
1499 /****************************************************************
1501 ****************************************************************/
1502 #define HUF_STATIC_ASSERT(c) { enum { HUF_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */
1505 /******************************************
1507 ******************************************/
1508 static unsigned HUF_isError(size_t code) { return ERR_isError(code); }
1510 #define HUF_ABSOLUTEMAX_TABLELOG 16 /* absolute limit of HUF_MAX_TABLELOG. Beyond that value, code does not work */
1511 #define HUF_MAX_TABLELOG 12 /* max configured tableLog (for static allocation); can be modified up to HUF_ABSOLUTEMAX_TABLELOG */
1512 #define HUF_DEFAULT_TABLELOG HUF_MAX_TABLELOG /* tableLog by default, when not specified */
1513 #define HUF_MAX_SYMBOL_VALUE 255
1514 #if (HUF_MAX_TABLELOG > HUF_ABSOLUTEMAX_TABLELOG)
1515 # error "HUF_MAX_TABLELOG is too large !"
1520 /*********************************************************
1521 * Huff0 : Huffman block decompression
1522 *********************************************************/
1523 typedef struct { BYTE byte; BYTE nbBits; } HUF_DEltX2; /* single-symbol decoding */
1525 typedef struct { U16 sequence; BYTE nbBits; BYTE length; } HUF_DEltX4; /* double-symbols decoding */
1527 typedef struct { BYTE symbol; BYTE weight; } sortedSymbol_t;
1530 Read compact Huffman tree, saved by HUF_writeCTable
1531 @huffWeight : destination buffer
1532 @return : size read from `src`
1534 static size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
1535 U32* nbSymbolsPtr, U32* tableLogPtr,
1536 const void* src, size_t srcSize)
1540 const BYTE* ip = (const BYTE*) src;
1545 if (!srcSize) return ERROR(srcSize_wrong);
1547 //memset(huffWeight, 0, hwSize); /* is not necessary, even though some analyzer complain ... */
1549 if (iSize >= 128) /* special header */
1551 if (iSize >= (242)) /* RLE */
1553 static int l[14] = { 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64, 127, 128 };
1554 oSize = l[iSize-242];
1555 memset(huffWeight, 1, hwSize);
1558 else /* Incompressible */
1560 oSize = iSize - 127;
1561 iSize = ((oSize+1)/2);
1562 if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
1563 if (oSize >= hwSize) return ERROR(corruption_detected);
1565 for (n=0; n<oSize; n+=2)
1567 huffWeight[n] = ip[n/2] >> 4;
1568 huffWeight[n+1] = ip[n/2] & 15;
1572 else /* header compressed with FSE (normal case) */
1574 if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
1575 oSize = FSE_decompress(huffWeight, hwSize-1, ip+1, iSize); /* max (hwSize-1) values decoded, as last one is implied */
1576 if (FSE_isError(oSize)) return oSize;
1579 /* collect weight stats */
1580 memset(rankStats, 0, (HUF_ABSOLUTEMAX_TABLELOG + 1) * sizeof(U32));
1582 for (n=0; n<oSize; n++)
1584 if (huffWeight[n] >= HUF_ABSOLUTEMAX_TABLELOG) return ERROR(corruption_detected);
1585 rankStats[huffWeight[n]]++;
1586 weightTotal += (1 << huffWeight[n]) >> 1;
1588 if (weightTotal == 0) return ERROR(corruption_detected);
1590 /* get last non-null symbol weight (implied, total must be 2^n) */
1591 tableLog = BIT_highbit32(weightTotal) + 1;
1592 if (tableLog > HUF_ABSOLUTEMAX_TABLELOG) return ERROR(corruption_detected);
1594 U32 total = 1 << tableLog;
1595 U32 rest = total - weightTotal;
1596 U32 verif = 1 << BIT_highbit32(rest);
1597 U32 lastWeight = BIT_highbit32(rest) + 1;
1598 if (verif != rest) return ERROR(corruption_detected); /* last value must be a clean power of 2 */
1599 huffWeight[oSize] = (BYTE)lastWeight;
1600 rankStats[lastWeight]++;
1603 /* check tree construction validity */
1604 if ((rankStats[1] < 2) || (rankStats[1] & 1)) return ERROR(corruption_detected); /* by construction : at least 2 elts of rank 1, must be even */
1607 *nbSymbolsPtr = (U32)(oSize+1);
1608 *tableLogPtr = tableLog;
1613 /**************************/
1614 /* single-symbol decoding */
1615 /**************************/
1617 static size_t HUF_readDTableX2 (U16* DTable, const void* src, size_t srcSize)
1619 BYTE huffWeight[HUF_MAX_SYMBOL_VALUE + 1];
1620 U32 rankVal[HUF_ABSOLUTEMAX_TABLELOG + 1]; /* large enough for values from 0 to 16 */
1622 const BYTE* ip = (const BYTE*) src;
1623 size_t iSize = ip[0];
1627 void* ptr = DTable+1;
1628 HUF_DEltX2* const dt = (HUF_DEltX2*)(ptr);
1630 HUF_STATIC_ASSERT(sizeof(HUF_DEltX2) == sizeof(U16)); /* if compilation fails here, assertion is false */
1631 //memset(huffWeight, 0, sizeof(huffWeight)); /* is not necessary, even though some analyzer complain ... */
1633 iSize = HUF_readStats(huffWeight, HUF_MAX_SYMBOL_VALUE + 1, rankVal, &nbSymbols, &tableLog, src, srcSize);
1634 if (HUF_isError(iSize)) return iSize;
1637 if (tableLog > DTable[0]) return ERROR(tableLog_tooLarge); /* DTable is too small */
1638 DTable[0] = (U16)tableLog; /* maybe should separate sizeof DTable, as allocated, from used size of DTable, in case of DTable re-use */
1642 for (n=1; n<=tableLog; n++)
1644 U32 current = nextRankStart;
1645 nextRankStart += (rankVal[n] << (n-1));
1646 rankVal[n] = current;
1650 for (n=0; n<nbSymbols; n++)
1652 const U32 w = huffWeight[n];
1653 const U32 length = (1 << w) >> 1;
1656 D.byte = (BYTE)n; D.nbBits = (BYTE)(tableLog + 1 - w);
1657 for (i = rankVal[w]; i < rankVal[w] + length; i++)
1659 rankVal[w] += length;
1665 static BYTE HUF_decodeSymbolX2(BIT_DStream_t* Dstream, const HUF_DEltX2* dt, const U32 dtLog)
1667 const size_t val = BIT_lookBitsFast(Dstream, dtLog); /* note : dtLog >= 1 */
1668 const BYTE c = dt[val].byte;
1669 BIT_skipBits(Dstream, dt[val].nbBits);
1673 #define HUF_DECODE_SYMBOLX2_0(ptr, DStreamPtr) \
1674 *ptr++ = HUF_decodeSymbolX2(DStreamPtr, dt, dtLog)
1676 #define HUF_DECODE_SYMBOLX2_1(ptr, DStreamPtr) \
1677 if (MEM_64bits() || (HUF_MAX_TABLELOG<=12)) \
1678 HUF_DECODE_SYMBOLX2_0(ptr, DStreamPtr)
1680 #define HUF_DECODE_SYMBOLX2_2(ptr, DStreamPtr) \
1682 HUF_DECODE_SYMBOLX2_0(ptr, DStreamPtr)
1684 static inline size_t HUF_decodeStreamX2(BYTE* p, BIT_DStream_t* const bitDPtr, BYTE* const pEnd, const HUF_DEltX2* const dt, const U32 dtLog)
1686 BYTE* const pStart = p;
1688 /* up to 4 symbols at a time */
1689 while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) && (p <= pEnd-4))
1691 HUF_DECODE_SYMBOLX2_2(p, bitDPtr);
1692 HUF_DECODE_SYMBOLX2_1(p, bitDPtr);
1693 HUF_DECODE_SYMBOLX2_2(p, bitDPtr);
1694 HUF_DECODE_SYMBOLX2_0(p, bitDPtr);
1697 /* closer to the end */
1698 while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) && (p < pEnd))
1699 HUF_DECODE_SYMBOLX2_0(p, bitDPtr);
1701 /* no more data to retrieve from bitstream, hence no need to reload */
1703 HUF_DECODE_SYMBOLX2_0(p, bitDPtr);
1709 static size_t HUF_decompress4X2_usingDTable(
1710 void* dst, size_t dstSize,
1711 const void* cSrc, size_t cSrcSize,
1714 if (cSrcSize < 10) return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */
1717 const BYTE* const istart = (const BYTE*) cSrc;
1718 BYTE* const ostart = (BYTE*) dst;
1719 BYTE* const oend = ostart + dstSize;
1721 const void* ptr = DTable;
1722 const HUF_DEltX2* const dt = ((const HUF_DEltX2*)ptr) +1;
1723 const U32 dtLog = DTable[0];
1727 BIT_DStream_t bitD1;
1728 BIT_DStream_t bitD2;
1729 BIT_DStream_t bitD3;
1730 BIT_DStream_t bitD4;
1731 const size_t length1 = MEM_readLE16(istart);
1732 const size_t length2 = MEM_readLE16(istart+2);
1733 const size_t length3 = MEM_readLE16(istart+4);
1735 const BYTE* const istart1 = istart + 6; /* jumpTable */
1736 const BYTE* const istart2 = istart1 + length1;
1737 const BYTE* const istart3 = istart2 + length2;
1738 const BYTE* const istart4 = istart3 + length3;
1739 const size_t segmentSize = (dstSize+3) / 4;
1740 BYTE* const opStart2 = ostart + segmentSize;
1741 BYTE* const opStart3 = opStart2 + segmentSize;
1742 BYTE* const opStart4 = opStart3 + segmentSize;
1744 BYTE* op2 = opStart2;
1745 BYTE* op3 = opStart3;
1746 BYTE* op4 = opStart4;
1749 length4 = cSrcSize - (length1 + length2 + length3 + 6);
1750 if (length4 > cSrcSize) return ERROR(corruption_detected); /* overflow */
1751 errorCode = BIT_initDStream(&bitD1, istart1, length1);
1752 if (HUF_isError(errorCode)) return errorCode;
1753 errorCode = BIT_initDStream(&bitD2, istart2, length2);
1754 if (HUF_isError(errorCode)) return errorCode;
1755 errorCode = BIT_initDStream(&bitD3, istart3, length3);
1756 if (HUF_isError(errorCode)) return errorCode;
1757 errorCode = BIT_initDStream(&bitD4, istart4, length4);
1758 if (HUF_isError(errorCode)) return errorCode;
1760 /* 16-32 symbols per loop (4-8 symbols per stream) */
1761 endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
1762 for ( ; (endSignal==BIT_DStream_unfinished) && (op4<(oend-7)) ; )
1764 HUF_DECODE_SYMBOLX2_2(op1, &bitD1);
1765 HUF_DECODE_SYMBOLX2_2(op2, &bitD2);
1766 HUF_DECODE_SYMBOLX2_2(op3, &bitD3);
1767 HUF_DECODE_SYMBOLX2_2(op4, &bitD4);
1768 HUF_DECODE_SYMBOLX2_1(op1, &bitD1);
1769 HUF_DECODE_SYMBOLX2_1(op2, &bitD2);
1770 HUF_DECODE_SYMBOLX2_1(op3, &bitD3);
1771 HUF_DECODE_SYMBOLX2_1(op4, &bitD4);
1772 HUF_DECODE_SYMBOLX2_2(op1, &bitD1);
1773 HUF_DECODE_SYMBOLX2_2(op2, &bitD2);
1774 HUF_DECODE_SYMBOLX2_2(op3, &bitD3);
1775 HUF_DECODE_SYMBOLX2_2(op4, &bitD4);
1776 HUF_DECODE_SYMBOLX2_0(op1, &bitD1);
1777 HUF_DECODE_SYMBOLX2_0(op2, &bitD2);
1778 HUF_DECODE_SYMBOLX2_0(op3, &bitD3);
1779 HUF_DECODE_SYMBOLX2_0(op4, &bitD4);
1781 endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
1784 /* check corruption */
1785 if (op1 > opStart2) return ERROR(corruption_detected);
1786 if (op2 > opStart3) return ERROR(corruption_detected);
1787 if (op3 > opStart4) return ERROR(corruption_detected);
1788 /* note : op4 supposed already verified within main loop */
1790 /* finish bitStreams one by one */
1791 HUF_decodeStreamX2(op1, &bitD1, opStart2, dt, dtLog);
1792 HUF_decodeStreamX2(op2, &bitD2, opStart3, dt, dtLog);
1793 HUF_decodeStreamX2(op3, &bitD3, opStart4, dt, dtLog);
1794 HUF_decodeStreamX2(op4, &bitD4, oend, dt, dtLog);
1797 endSignal = BIT_endOfDStream(&bitD1) & BIT_endOfDStream(&bitD2) & BIT_endOfDStream(&bitD3) & BIT_endOfDStream(&bitD4);
1798 if (!endSignal) return ERROR(corruption_detected);
1806 static size_t HUF_decompress4X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
1808 HUF_CREATE_STATIC_DTABLEX2(DTable, HUF_MAX_TABLELOG);
1809 const BYTE* ip = (const BYTE*) cSrc;
1812 errorCode = HUF_readDTableX2 (DTable, cSrc, cSrcSize);
1813 if (HUF_isError(errorCode)) return errorCode;
1814 if (errorCode >= cSrcSize) return ERROR(srcSize_wrong);
1816 cSrcSize -= errorCode;
1818 return HUF_decompress4X2_usingDTable (dst, dstSize, ip, cSrcSize, DTable);
1822 /***************************/
1823 /* double-symbols decoding */
1824 /***************************/
1826 static void HUF_fillDTableX4Level2(HUF_DEltX4* DTable, U32 sizeLog, const U32 consumed,
1827 const U32* rankValOrigin, const int minWeight,
1828 const sortedSymbol_t* sortedSymbols, const U32 sortedListSize,
1829 U32 nbBitsBaseline, U16 baseSeq)
1832 U32 rankVal[HUF_ABSOLUTEMAX_TABLELOG + 1];
1835 /* get pre-calculated rankVal */
1836 memcpy(rankVal, rankValOrigin, sizeof(rankVal));
1838 /* fill skipped values */
1841 U32 i, skipSize = rankVal[minWeight];
1842 MEM_writeLE16(&(DElt.sequence), baseSeq);
1843 DElt.nbBits = (BYTE)(consumed);
1845 for (i = 0; i < skipSize; i++)
1850 for (s=0; s<sortedListSize; s++) /* note : sortedSymbols already skipped */
1852 const U32 symbol = sortedSymbols[s].symbol;
1853 const U32 weight = sortedSymbols[s].weight;
1854 const U32 nbBits = nbBitsBaseline - weight;
1855 const U32 length = 1 << (sizeLog-nbBits);
1856 const U32 start = rankVal[weight];
1858 const U32 end = start + length;
1860 MEM_writeLE16(&(DElt.sequence), (U16)(baseSeq + (symbol << 8)));
1861 DElt.nbBits = (BYTE)(nbBits + consumed);
1863 do { DTable[i++] = DElt; } while (i<end); /* since length >= 1 */
1865 rankVal[weight] += length;
1869 typedef U32 rankVal_t[HUF_ABSOLUTEMAX_TABLELOG][HUF_ABSOLUTEMAX_TABLELOG + 1];
1871 static void HUF_fillDTableX4(HUF_DEltX4* DTable, const U32 targetLog,
1872 const sortedSymbol_t* sortedList, const U32 sortedListSize,
1873 const U32* rankStart, rankVal_t rankValOrigin, const U32 maxWeight,
1874 const U32 nbBitsBaseline)
1876 U32 rankVal[HUF_ABSOLUTEMAX_TABLELOG + 1];
1877 const int scaleLog = nbBitsBaseline - targetLog; /* note : targetLog >= srcLog, hence scaleLog <= 1 */
1878 const U32 minBits = nbBitsBaseline - maxWeight;
1881 memcpy(rankVal, rankValOrigin, sizeof(rankVal));
1884 for (s=0; s<sortedListSize; s++)
1886 const U16 symbol = sortedList[s].symbol;
1887 const U32 weight = sortedList[s].weight;
1888 const U32 nbBits = nbBitsBaseline - weight;
1889 const U32 start = rankVal[weight];
1890 const U32 length = 1 << (targetLog-nbBits);
1892 if (targetLog-nbBits >= minBits) /* enough room for a second symbol */
1895 int minWeight = nbBits + scaleLog;
1896 if (minWeight < 1) minWeight = 1;
1897 sortedRank = rankStart[minWeight];
1898 HUF_fillDTableX4Level2(DTable+start, targetLog-nbBits, nbBits,
1899 rankValOrigin[nbBits], minWeight,
1900 sortedList+sortedRank, sortedListSize-sortedRank,
1901 nbBitsBaseline, symbol);
1906 const U32 end = start + length;
1909 MEM_writeLE16(&(DElt.sequence), symbol);
1910 DElt.nbBits = (BYTE)(nbBits);
1912 for (i = start; i < end; i++)
1915 rankVal[weight] += length;
1919 static size_t HUF_readDTableX4 (U32* DTable, const void* src, size_t srcSize)
1921 BYTE weightList[HUF_MAX_SYMBOL_VALUE + 1];
1922 sortedSymbol_t sortedSymbol[HUF_MAX_SYMBOL_VALUE + 1];
1923 U32 rankStats[HUF_ABSOLUTEMAX_TABLELOG + 1] = { 0 };
1924 U32 rankStart0[HUF_ABSOLUTEMAX_TABLELOG + 2] = { 0 };
1925 U32* const rankStart = rankStart0+1;
1927 U32 tableLog, maxW, sizeOfSort, nbSymbols;
1928 const U32 memLog = DTable[0];
1929 const BYTE* ip = (const BYTE*) src;
1930 size_t iSize = ip[0];
1932 HUF_DEltX4* const dt = ((HUF_DEltX4*)ptr) + 1;
1934 HUF_STATIC_ASSERT(sizeof(HUF_DEltX4) == sizeof(U32)); /* if compilation fails here, assertion is false */
1935 if (memLog > HUF_ABSOLUTEMAX_TABLELOG) return ERROR(tableLog_tooLarge);
1936 //memset(weightList, 0, sizeof(weightList)); /* is not necessary, even though some analyzer complain ... */
1938 iSize = HUF_readStats(weightList, HUF_MAX_SYMBOL_VALUE + 1, rankStats, &nbSymbols, &tableLog, src, srcSize);
1939 if (HUF_isError(iSize)) return iSize;
1942 if (tableLog > memLog) return ERROR(tableLog_tooLarge); /* DTable can't fit code depth */
1944 /* find maxWeight */
1945 for (maxW = tableLog; rankStats[maxW]==0; maxW--)
1946 { if (!maxW) return ERROR(GENERIC); } /* necessarily finds a solution before maxW==0 */
1948 /* Get start index of each weight */
1950 U32 w, nextRankStart = 0;
1951 for (w=1; w<=maxW; w++)
1953 U32 current = nextRankStart;
1954 nextRankStart += rankStats[w];
1955 rankStart[w] = current;
1957 rankStart[0] = nextRankStart; /* put all 0w symbols at the end of sorted list*/
1958 sizeOfSort = nextRankStart;
1961 /* sort symbols by weight */
1964 for (s=0; s<nbSymbols; s++)
1966 U32 w = weightList[s];
1967 U32 r = rankStart[w]++;
1968 sortedSymbol[r].symbol = (BYTE)s;
1969 sortedSymbol[r].weight = (BYTE)w;
1971 rankStart[0] = 0; /* forget 0w symbols; this is beginning of weight(1) */
1976 const U32 minBits = tableLog+1 - maxW;
1977 U32 nextRankVal = 0;
1979 const int rescale = (memLog-tableLog) - 1; /* tableLog <= memLog */
1980 U32* rankVal0 = rankVal[0];
1981 for (w=1; w<=maxW; w++)
1983 U32 current = nextRankVal;
1984 nextRankVal += rankStats[w] << (w+rescale);
1985 rankVal0[w] = current;
1987 for (consumed = minBits; consumed <= memLog - minBits; consumed++)
1989 U32* rankValPtr = rankVal[consumed];
1990 for (w = 1; w <= maxW; w++)
1992 rankValPtr[w] = rankVal0[w] >> consumed;
1997 HUF_fillDTableX4(dt, memLog,
1998 sortedSymbol, sizeOfSort,
1999 rankStart0, rankVal, maxW,
2006 static U32 HUF_decodeSymbolX4(void* op, BIT_DStream_t* DStream, const HUF_DEltX4* dt, const U32 dtLog)
2008 const size_t val = BIT_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */
2009 memcpy(op, dt+val, 2);
2010 BIT_skipBits(DStream, dt[val].nbBits);
2011 return dt[val].length;
2014 static U32 HUF_decodeLastSymbolX4(void* op, BIT_DStream_t* DStream, const HUF_DEltX4* dt, const U32 dtLog)
2016 const size_t val = BIT_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */
2017 memcpy(op, dt+val, 1);
2018 if (dt[val].length==1) BIT_skipBits(DStream, dt[val].nbBits);
2021 if (DStream->bitsConsumed < (sizeof(DStream->bitContainer)*8))
2023 BIT_skipBits(DStream, dt[val].nbBits);
2024 if (DStream->bitsConsumed > (sizeof(DStream->bitContainer)*8))
2025 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 */
2032 #define HUF_DECODE_SYMBOLX4_0(ptr, DStreamPtr) \
2033 ptr += HUF_decodeSymbolX4(ptr, DStreamPtr, dt, dtLog)
2035 #define HUF_DECODE_SYMBOLX4_1(ptr, DStreamPtr) \
2036 if (MEM_64bits() || (HUF_MAX_TABLELOG<=12)) \
2037 ptr += HUF_decodeSymbolX4(ptr, DStreamPtr, dt, dtLog)
2039 #define HUF_DECODE_SYMBOLX4_2(ptr, DStreamPtr) \
2041 ptr += HUF_decodeSymbolX4(ptr, DStreamPtr, dt, dtLog)
2043 static inline size_t HUF_decodeStreamX4(BYTE* p, BIT_DStream_t* bitDPtr, BYTE* const pEnd, const HUF_DEltX4* const dt, const U32 dtLog)
2045 BYTE* const pStart = p;
2047 /* up to 8 symbols at a time */
2048 while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) && (p < pEnd-7))
2050 HUF_DECODE_SYMBOLX4_2(p, bitDPtr);
2051 HUF_DECODE_SYMBOLX4_1(p, bitDPtr);
2052 HUF_DECODE_SYMBOLX4_2(p, bitDPtr);
2053 HUF_DECODE_SYMBOLX4_0(p, bitDPtr);
2056 /* closer to the end */
2057 while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) && (p <= pEnd-2))
2058 HUF_DECODE_SYMBOLX4_0(p, bitDPtr);
2061 HUF_DECODE_SYMBOLX4_0(p, bitDPtr); /* no need to reload : reached the end of DStream */
2064 p += HUF_decodeLastSymbolX4(p, bitDPtr, dt, dtLog);
2071 static size_t HUF_decompress4X4_usingDTable(
2072 void* dst, size_t dstSize,
2073 const void* cSrc, size_t cSrcSize,
2076 if (cSrcSize < 10) return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */
2079 const BYTE* const istart = (const BYTE*) cSrc;
2080 BYTE* const ostart = (BYTE*) dst;
2081 BYTE* const oend = ostart + dstSize;
2083 const void* ptr = DTable;
2084 const HUF_DEltX4* const dt = ((const HUF_DEltX4*)ptr) +1;
2085 const U32 dtLog = DTable[0];
2089 BIT_DStream_t bitD1;
2090 BIT_DStream_t bitD2;
2091 BIT_DStream_t bitD3;
2092 BIT_DStream_t bitD4;
2093 const size_t length1 = MEM_readLE16(istart);
2094 const size_t length2 = MEM_readLE16(istart+2);
2095 const size_t length3 = MEM_readLE16(istart+4);
2097 const BYTE* const istart1 = istart + 6; /* jumpTable */
2098 const BYTE* const istart2 = istart1 + length1;
2099 const BYTE* const istart3 = istart2 + length2;
2100 const BYTE* const istart4 = istart3 + length3;
2101 const size_t segmentSize = (dstSize+3) / 4;
2102 BYTE* const opStart2 = ostart + segmentSize;
2103 BYTE* const opStart3 = opStart2 + segmentSize;
2104 BYTE* const opStart4 = opStart3 + segmentSize;
2106 BYTE* op2 = opStart2;
2107 BYTE* op3 = opStart3;
2108 BYTE* op4 = opStart4;
2111 length4 = cSrcSize - (length1 + length2 + length3 + 6);
2112 if (length4 > cSrcSize) return ERROR(corruption_detected); /* overflow */
2113 errorCode = BIT_initDStream(&bitD1, istart1, length1);
2114 if (HUF_isError(errorCode)) return errorCode;
2115 errorCode = BIT_initDStream(&bitD2, istart2, length2);
2116 if (HUF_isError(errorCode)) return errorCode;
2117 errorCode = BIT_initDStream(&bitD3, istart3, length3);
2118 if (HUF_isError(errorCode)) return errorCode;
2119 errorCode = BIT_initDStream(&bitD4, istart4, length4);
2120 if (HUF_isError(errorCode)) return errorCode;
2122 /* 16-32 symbols per loop (4-8 symbols per stream) */
2123 endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
2124 for ( ; (endSignal==BIT_DStream_unfinished) && (op4<(oend-7)) ; )
2126 HUF_DECODE_SYMBOLX4_2(op1, &bitD1);
2127 HUF_DECODE_SYMBOLX4_2(op2, &bitD2);
2128 HUF_DECODE_SYMBOLX4_2(op3, &bitD3);
2129 HUF_DECODE_SYMBOLX4_2(op4, &bitD4);
2130 HUF_DECODE_SYMBOLX4_1(op1, &bitD1);
2131 HUF_DECODE_SYMBOLX4_1(op2, &bitD2);
2132 HUF_DECODE_SYMBOLX4_1(op3, &bitD3);
2133 HUF_DECODE_SYMBOLX4_1(op4, &bitD4);
2134 HUF_DECODE_SYMBOLX4_2(op1, &bitD1);
2135 HUF_DECODE_SYMBOLX4_2(op2, &bitD2);
2136 HUF_DECODE_SYMBOLX4_2(op3, &bitD3);
2137 HUF_DECODE_SYMBOLX4_2(op4, &bitD4);
2138 HUF_DECODE_SYMBOLX4_0(op1, &bitD1);
2139 HUF_DECODE_SYMBOLX4_0(op2, &bitD2);
2140 HUF_DECODE_SYMBOLX4_0(op3, &bitD3);
2141 HUF_DECODE_SYMBOLX4_0(op4, &bitD4);
2143 endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
2146 /* check corruption */
2147 if (op1 > opStart2) return ERROR(corruption_detected);
2148 if (op2 > opStart3) return ERROR(corruption_detected);
2149 if (op3 > opStart4) return ERROR(corruption_detected);
2150 /* note : op4 supposed already verified within main loop */
2152 /* finish bitStreams one by one */
2153 HUF_decodeStreamX4(op1, &bitD1, opStart2, dt, dtLog);
2154 HUF_decodeStreamX4(op2, &bitD2, opStart3, dt, dtLog);
2155 HUF_decodeStreamX4(op3, &bitD3, opStart4, dt, dtLog);
2156 HUF_decodeStreamX4(op4, &bitD4, oend, dt, dtLog);
2159 endSignal = BIT_endOfDStream(&bitD1) & BIT_endOfDStream(&bitD2) & BIT_endOfDStream(&bitD3) & BIT_endOfDStream(&bitD4);
2160 if (!endSignal) return ERROR(corruption_detected);
2168 static size_t HUF_decompress4X4 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
2170 HUF_CREATE_STATIC_DTABLEX4(DTable, HUF_MAX_TABLELOG);
2171 const BYTE* ip = (const BYTE*) cSrc;
2173 size_t hSize = HUF_readDTableX4 (DTable, cSrc, cSrcSize);
2174 if (HUF_isError(hSize)) return hSize;
2175 if (hSize >= cSrcSize) return ERROR(srcSize_wrong);
2179 return HUF_decompress4X4_usingDTable (dst, dstSize, ip, cSrcSize, DTable);
2183 /**********************************/
2184 /* Generic decompression selector */
2185 /**********************************/
2187 typedef struct { U32 tableTime; U32 decode256Time; } algo_time_t;
2188 static const algo_time_t algoTime[16 /* Quantization */][3 /* single, double, quad */] =
2190 /* single, double, quad */
2191 {{0,0}, {1,1}, {2,2}}, /* Q==0 : impossible */
2192 {{0,0}, {1,1}, {2,2}}, /* Q==1 : impossible */
2193 {{ 38,130}, {1313, 74}, {2151, 38}}, /* Q == 2 : 12-18% */
2194 {{ 448,128}, {1353, 74}, {2238, 41}}, /* Q == 3 : 18-25% */
2195 {{ 556,128}, {1353, 74}, {2238, 47}}, /* Q == 4 : 25-32% */
2196 {{ 714,128}, {1418, 74}, {2436, 53}}, /* Q == 5 : 32-38% */
2197 {{ 883,128}, {1437, 74}, {2464, 61}}, /* Q == 6 : 38-44% */
2198 {{ 897,128}, {1515, 75}, {2622, 68}}, /* Q == 7 : 44-50% */
2199 {{ 926,128}, {1613, 75}, {2730, 75}}, /* Q == 8 : 50-56% */
2200 {{ 947,128}, {1729, 77}, {3359, 77}}, /* Q == 9 : 56-62% */
2201 {{1107,128}, {2083, 81}, {4006, 84}}, /* Q ==10 : 62-69% */
2202 {{1177,128}, {2379, 87}, {4785, 88}}, /* Q ==11 : 69-75% */
2203 {{1242,128}, {2415, 93}, {5155, 84}}, /* Q ==12 : 75-81% */
2204 {{1349,128}, {2644,106}, {5260,106}}, /* Q ==13 : 81-87% */
2205 {{1455,128}, {2422,124}, {4174,124}}, /* Q ==14 : 87-93% */
2206 {{ 722,128}, {1891,145}, {1936,146}}, /* Q ==15 : 93-99% */
2209 typedef size_t (*decompressionAlgo)(void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);
2211 static size_t HUF_decompress (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
2213 static const decompressionAlgo decompress[3] = { HUF_decompress4X2, HUF_decompress4X4, NULL };
2214 /* estimate decompression time */
2216 const U32 D256 = (U32)(dstSize >> 8);
2221 /* validation checks */
2222 if (dstSize == 0) return ERROR(dstSize_tooSmall);
2223 if (cSrcSize > dstSize) return ERROR(corruption_detected); /* invalid */
2224 if (cSrcSize == dstSize) { memcpy(dst, cSrc, dstSize); return dstSize; } /* not compressed */
2225 if (cSrcSize == 1) { memset(dst, *(const BYTE*)cSrc, dstSize); return dstSize; } /* RLE */
2227 /* decoder timing evaluation */
2228 Q = (U32)(cSrcSize * 16 / dstSize); /* Q < 16 since dstSize > cSrcSize */
2230 Dtime[n] = algoTime[Q][n].tableTime + (algoTime[Q][n].decode256Time * D256);
2232 Dtime[1] += Dtime[1] >> 4; Dtime[2] += Dtime[2] >> 3; /* advantage to algorithms using less memory, for cache eviction */
2234 if (Dtime[1] < Dtime[0]) algoNb = 1;
2236 return decompress[algoNb](dst, dstSize, cSrc, cSrcSize);
2238 //return HUF_decompress4X2(dst, dstSize, cSrc, cSrcSize); /* multi-streams single-symbol decoding */
2239 //return HUF_decompress4X4(dst, dstSize, cSrc, cSrcSize); /* multi-streams double-symbols decoding */
2240 //return HUF_decompress4X6(dst, dstSize, cSrc, cSrcSize); /* multi-streams quad-symbols decoding */
2243 zstd - standard compression library
2244 Copyright (C) 2014-2015, Yann Collet.
2246 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
2248 Redistribution and use in source and binary forms, with or without
2249 modification, are permitted provided that the following conditions are
2251 * Redistributions of source code must retain the above copyright
2252 notice, this list of conditions and the following disclaimer.
2253 * Redistributions in binary form must reproduce the above
2254 copyright notice, this list of conditions and the following disclaimer
2255 in the documentation and/or other materials provided with the
2257 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2258 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2259 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2260 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2261 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2262 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2263 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2264 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2265 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2266 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2267 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2269 You can contact the author at :
2270 - zstd source repository : https://github.com/Cyan4973/zstd
2271 - ztsd public forum : https://groups.google.com/forum/#!forum/lz4c
2274 /* ***************************************************************
2276 *****************************************************************/
2279 * Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.)
2280 * Increasing memory usage improves compression ratio
2281 * Reduced memory usage can improve speed, due to cache effect
2283 #define ZSTD_MEMORY_USAGE 17
2287 * Select how default compression functions will allocate memory for their hash table,
2288 * in memory stack (0, fastest), or in memory heap (1, requires malloc())
2289 * Note that compression context is fairly large, as a consequence heap memory is recommended.
2291 #ifndef ZSTD_HEAPMODE
2292 # define ZSTD_HEAPMODE 1
2293 #endif /* ZSTD_HEAPMODE */
2297 * decompressor can decode older formats (starting from Zstd 0.1+)
2299 #ifndef ZSTD_LEGACY_SUPPORT
2300 # define ZSTD_LEGACY_SUPPORT 1
2304 /* *******************************************************
2306 *********************************************************/
2307 #include <stdlib.h> /* calloc */
2308 #include <string.h> /* memcpy, memmove */
2309 #include <stdio.h> /* debug : printf */
2312 /* *******************************************************
2313 * Compiler specifics
2314 *********************************************************/
2316 # include <immintrin.h> /* AVX2 intrinsics */
2319 #ifdef _MSC_VER /* Visual Studio */
2320 # include <intrin.h> /* For Visual 2005 */
2321 # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
2322 # pragma warning(disable : 4324) /* disable: C4324: padded structure */
2324 # define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
2328 /* *******************************************************
2330 *********************************************************/
2331 #define HASH_LOG (ZSTD_MEMORY_USAGE - 2)
2332 #define HASH_TABLESIZE (1 << HASH_LOG)
2333 #define HASH_MASK (HASH_TABLESIZE - 1)
2335 #define KNUTH 2654435761
2344 #define KB *(1 <<10)
2345 #define MB *(1 <<20)
2346 #define GB *(1U<<30)
2348 #define BLOCKSIZE (128 KB) /* define, for static allocation */
2349 #define MIN_SEQUENCES_SIZE (2 /*seqNb*/ + 2 /*dumps*/ + 3 /*seqTables*/ + 1 /*bitStream*/)
2350 #define MIN_CBLOCK_SIZE (3 /*litCSize*/ + MIN_SEQUENCES_SIZE)
2354 #define WORKPLACESIZE (BLOCKSIZE*3)
2359 #define MaxML ((1<<MLbits )-1)
2360 #define MaxLL ((1<<LLbits )-1)
2362 #define LitFSELog 11
2366 #define MAX(a,b) ((a)<(b)?(b):(a))
2367 #define MaxSeq MAX(MaxLL, MaxML)
2369 #define LITERAL_NOENTROPY 63
2370 #define COMMAND_NOENTROPY 7 /* to remove */
2372 #define ZSTD_CONTENTSIZE_ERROR (0ULL - 2)
2374 static const size_t ZSTD_blockHeaderSize = 3;
2375 static const size_t ZSTD_frameHeaderSize = 4;
2378 /* *******************************************************
2380 **********************************************************/
2381 static void ZSTD_copy4(void* dst, const void* src) { memcpy(dst, src, 4); }
2383 static void ZSTD_copy8(void* dst, const void* src) { memcpy(dst, src, 8); }
2385 #define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; }
2387 /*! ZSTD_wildcopy : custom version of memcpy(), can copy up to 7-8 bytes too many */
2388 static void ZSTD_wildcopy(void* dst, const void* src, ptrdiff_t length)
2390 const BYTE* ip = (const BYTE*)src;
2391 BYTE* op = (BYTE*)dst;
2392 BYTE* const oend = op + length;
2393 do COPY8(op, ip) while (op < oend);
2397 /* **************************************
2399 ****************************************/
2400 typedef enum { bt_compressed, bt_raw, bt_rle, bt_end } blockType_t;
2404 blockType_t blockType;
2406 } blockProperties_t;
2416 BYTE* litLengthStart;
2418 BYTE* matchLengthStart;
2425 /* *************************************
2427 ***************************************/
2429 * tells if a return value is an error code */
2430 static unsigned ZSTD_isError(size_t code) { return ERR_isError(code); }
2434 /* *************************************************************
2435 * Decompression section
2436 ***************************************************************/
2439 U32 LLTable[FSE_DTABLE_SIZE_U32(LLFSELog)];
2440 U32 OffTable[FSE_DTABLE_SIZE_U32(OffFSELog)];
2441 U32 MLTable[FSE_DTABLE_SIZE_U32(MLFSELog)];
2442 void* previousDstEnd;
2449 BYTE litBuffer[BLOCKSIZE + 8 /* margin for wildcopy */];
2450 }; /* typedef'd to ZSTD_Dctx within "zstd_static.h" */
2453 static size_t ZSTD_getcBlockSize(const void* src, size_t srcSize, blockProperties_t* bpPtr)
2455 const BYTE* const in = (const BYTE* const)src;
2459 if (srcSize < 3) return ERROR(srcSize_wrong);
2462 cSize = in[2] + (in[1]<<8) + ((in[0] & 7)<<16);
2464 bpPtr->blockType = (blockType_t)(headerFlags >> 6);
2465 bpPtr->origSize = (bpPtr->blockType == bt_rle) ? cSize : 0;
2467 if (bpPtr->blockType == bt_end) return 0;
2468 if (bpPtr->blockType == bt_rle) return 1;
2472 static size_t ZSTD_copyUncompressedBlock(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
2474 if (srcSize > maxDstSize) return ERROR(dstSize_tooSmall);
2475 memcpy(dst, src, srcSize);
2480 /** ZSTD_decompressLiterals
2481 @return : nb of bytes read from src, or an error code*/
2482 static size_t ZSTD_decompressLiterals(void* dst, size_t* maxDstSizePtr,
2483 const void* src, size_t srcSize)
2485 const BYTE* ip = (const BYTE*)src;
2487 const size_t litSize = (MEM_readLE32(src) & 0x1FFFFF) >> 2; /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */
2488 const size_t litCSize = (MEM_readLE32(ip+2) & 0xFFFFFF) >> 5; /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */
2490 if (litSize > *maxDstSizePtr) return ERROR(corruption_detected);
2491 if (litCSize + 5 > srcSize) return ERROR(corruption_detected);
2493 if (HUF_isError(HUF_decompress(dst, litSize, ip+5, litCSize))) return ERROR(corruption_detected);
2495 *maxDstSizePtr = litSize;
2496 return litCSize + 5;
2500 /** ZSTD_decodeLiteralsBlock
2501 @return : nb of bytes read from src (< srcSize )*/
2502 static size_t ZSTD_decodeLiteralsBlock(void* ctx,
2503 const void* src, size_t srcSize)
2505 ZSTD_DCtx* dctx = (ZSTD_DCtx*)ctx;
2506 const BYTE* const istart = (const BYTE* const)src;
2508 /* any compressed block with literals segment must be at least this size */
2509 if (srcSize < MIN_CBLOCK_SIZE) return ERROR(corruption_detected);
2516 size_t litSize = BLOCKSIZE;
2517 const size_t readSize = ZSTD_decompressLiterals(dctx->litBuffer, &litSize, src, srcSize);
2518 dctx->litPtr = dctx->litBuffer;
2519 dctx->litSize = litSize;
2520 memset(dctx->litBuffer + dctx->litSize, 0, 8);
2521 return readSize; /* works if it's an error too */
2525 const size_t litSize = (MEM_readLE32(istart) & 0xFFFFFF) >> 2; /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */
2526 if (litSize > srcSize-11) /* risk of reading too far with wildcopy */
2528 if (litSize > srcSize-3) return ERROR(corruption_detected);
2529 memcpy(dctx->litBuffer, istart, litSize);
2530 dctx->litPtr = dctx->litBuffer;
2531 dctx->litSize = litSize;
2532 memset(dctx->litBuffer + dctx->litSize, 0, 8);
2535 /* direct reference into compressed stream */
2536 dctx->litPtr = istart+3;
2537 dctx->litSize = litSize;
2542 const size_t litSize = (MEM_readLE32(istart) & 0xFFFFFF) >> 2; /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */
2543 if (litSize > BLOCKSIZE) return ERROR(corruption_detected);
2544 memset(dctx->litBuffer, istart[3], litSize + 8);
2545 dctx->litPtr = dctx->litBuffer;
2546 dctx->litSize = litSize;
2553 static size_t ZSTD_decodeSeqHeaders(int* nbSeq, const BYTE** dumpsPtr, size_t* dumpsLengthPtr,
2554 FSE_DTable* DTableLL, FSE_DTable* DTableML, FSE_DTable* DTableOffb,
2555 const void* src, size_t srcSize)
2557 const BYTE* const istart = (const BYTE* const)src;
2558 const BYTE* ip = istart;
2559 const BYTE* const iend = istart + srcSize;
2560 U32 LLtype, Offtype, MLtype;
2561 U32 LLlog, Offlog, MLlog;
2565 if (srcSize < 5) return ERROR(srcSize_wrong);
2568 *nbSeq = MEM_readLE16(ip); ip+=2;
2570 Offtype = (*ip >> 4) & 3;
2571 MLtype = (*ip >> 2) & 3;
2574 dumpsLength = ip[2];
2575 dumpsLength += ip[1] << 8;
2580 dumpsLength = ip[1];
2581 dumpsLength += (ip[0] & 1) << 8;
2586 *dumpsLengthPtr = dumpsLength;
2589 if (ip > iend-3) return ERROR(srcSize_wrong); /* min : all 3 are "raw", hence no header, but at least xxLog bits per type */
2593 S16 norm[MaxML+1]; /* assumption : MaxML >= MaxLL and MaxOff */
2601 FSE_buildDTable_rle(DTableLL, *ip++); break;
2604 FSE_buildDTable_raw(DTableLL, LLbits); break;
2607 headerSize = FSE_readNCount(norm, &max, &LLlog, ip, iend-ip);
2608 if (FSE_isError(headerSize)) return ERROR(GENERIC);
2609 if (LLlog > LLFSELog) return ERROR(corruption_detected);
2611 FSE_buildDTable(DTableLL, norm, max, LLlog);
2618 if (ip > iend-2) return ERROR(srcSize_wrong); /* min : "raw", hence no header, but at least xxLog bits */
2619 FSE_buildDTable_rle(DTableOffb, *ip++ & MaxOff); /* if *ip > MaxOff, data is corrupted */
2623 FSE_buildDTable_raw(DTableOffb, Offbits); break;
2626 headerSize = FSE_readNCount(norm, &max, &Offlog, ip, iend-ip);
2627 if (FSE_isError(headerSize)) return ERROR(GENERIC);
2628 if (Offlog > OffFSELog) return ERROR(corruption_detected);
2630 FSE_buildDTable(DTableOffb, norm, max, Offlog);
2637 if (ip > iend-2) return ERROR(srcSize_wrong); /* min : "raw", hence no header, but at least xxLog bits */
2638 FSE_buildDTable_rle(DTableML, *ip++); break;
2641 FSE_buildDTable_raw(DTableML, MLbits); break;
2644 headerSize = FSE_readNCount(norm, &max, &MLlog, ip, iend-ip);
2645 if (FSE_isError(headerSize)) return ERROR(GENERIC);
2646 if (MLlog > MLFSELog) return ERROR(corruption_detected);
2648 FSE_buildDTable(DTableML, norm, max, MLlog);
2662 BIT_DStream_t DStream;
2663 FSE_DState_t stateLL;
2664 FSE_DState_t stateOffb;
2665 FSE_DState_t stateML;
2668 const BYTE* dumpsEnd;
2672 static void ZSTD_decodeSequence(seq_t* seq, seqState_t* seqState)
2678 const BYTE* dumps = seqState->dumps;
2679 const BYTE* const de = seqState->dumpsEnd;
2681 /* Literal length */
2682 litLength = FSE_decodeSymbol(&(seqState->stateLL), &(seqState->DStream));
2683 prevOffset = litLength ? seq->offset : seqState->prevOffset;
2684 seqState->prevOffset = seq->offset;
2685 if (litLength == MaxLL)
2688 if (add < 255) litLength += add;
2691 litLength = MEM_readLE32(dumps) & 0xFFFFFF; /* no pb : dumps is always followed by seq tables > 1 byte */
2694 if (dumps >= de) dumps = de-1; /* late correction, to avoid read overflow (data is now corrupted anyway) */
2699 static const size_t offsetPrefix[MaxOff+1] = { /* note : size_t faster than U32 */
2700 1 /*fake*/, 1, 2, 4, 8, 16, 32, 64, 128, 256,
2701 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144,
2702 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, /*fake*/ 1, 1, 1, 1, 1 };
2703 U32 offsetCode, nbBits;
2704 offsetCode = FSE_decodeSymbol(&(seqState->stateOffb), &(seqState->DStream)); /* <= maxOff, by table construction */
2705 if (MEM_32bits()) BIT_reloadDStream(&(seqState->DStream));
2706 nbBits = offsetCode - 1;
2707 if (offsetCode==0) nbBits = 0; /* cmove */
2708 offset = offsetPrefix[offsetCode] + BIT_readBits(&(seqState->DStream), nbBits);
2709 if (MEM_32bits()) BIT_reloadDStream(&(seqState->DStream));
2710 if (offsetCode==0) offset = prevOffset; /* cmove */
2714 matchLength = FSE_decodeSymbol(&(seqState->stateML), &(seqState->DStream));
2715 if (matchLength == MaxML)
2718 if (add < 255) matchLength += add;
2721 matchLength = MEM_readLE32(dumps) & 0xFFFFFF; /* no pb : dumps is always followed by seq tables > 1 byte */
2724 if (dumps >= de) dumps = de-1; /* late correction, to avoid read overflow (data is now corrupted anyway) */
2726 matchLength += MINMATCH;
2729 seq->litLength = litLength;
2730 seq->offset = offset;
2731 seq->matchLength = matchLength;
2732 seqState->dumps = dumps;
2736 static size_t ZSTD_execSequence(BYTE* op,
2738 const BYTE** litPtr, const BYTE* const litLimit,
2739 BYTE* const base, BYTE* const oend)
2741 static const int dec32table[] = {0, 1, 2, 1, 4, 4, 4, 4}; /* added */
2742 static const int dec64table[] = {8, 8, 8, 7, 8, 9,10,11}; /* subtracted */
2743 const BYTE* const ostart = op;
2744 BYTE* const oLitEnd = op + sequence.litLength;
2745 BYTE* const oMatchEnd = op + sequence.litLength + sequence.matchLength; /* risk : address space overflow (32-bits) */
2746 BYTE* const oend_8 = oend-8;
2747 const BYTE* const litEnd = *litPtr + sequence.litLength;
2750 if (oLitEnd > oend_8) return ERROR(dstSize_tooSmall); /* last match must start at a minimum distance of 8 from oend */
2751 if (oMatchEnd > oend) return ERROR(dstSize_tooSmall); /* overwrite beyond dst buffer */
2752 if (litEnd > litLimit) return ERROR(corruption_detected); /* overRead beyond lit buffer */
2755 ZSTD_wildcopy(op, *litPtr, sequence.litLength); /* note : oLitEnd <= oend-8 : no risk of overwrite beyond oend */
2757 *litPtr = litEnd; /* update for next sequence */
2761 const BYTE* match = op - sequence.offset;
2764 if (sequence.offset > (size_t)op) return ERROR(corruption_detected); /* address space overflow test (this test seems kept by clang optimizer) */
2765 //if (match > op) return ERROR(corruption_detected); /* address space overflow test (is clang optimizer removing this test ?) */
2766 if (match < base) return ERROR(corruption_detected);
2768 /* close range match, overlap */
2769 if (sequence.offset < 8)
2771 const int dec64 = dec64table[sequence.offset];
2776 match += dec32table[sequence.offset];
2777 ZSTD_copy4(op+4, match);
2782 ZSTD_copy8(op, match);
2784 op += 8; match += 8;
2786 if (oMatchEnd > oend-(16-MINMATCH))
2790 ZSTD_wildcopy(op, match, oend_8 - op);
2791 match += oend_8 - op;
2794 while (op < oMatchEnd) *op++ = *match++;
2798 ZSTD_wildcopy(op, match, (ptrdiff_t)sequence.matchLength-8); /* works even if matchLength < 8 */
2802 return oMatchEnd - ostart;
2805 static size_t ZSTD_decompressSequences(
2807 void* dst, size_t maxDstSize,
2808 const void* seqStart, size_t seqSize)
2810 ZSTD_DCtx* dctx = (ZSTD_DCtx*)ctx;
2811 const BYTE* ip = (const BYTE*)seqStart;
2812 const BYTE* const iend = ip + seqSize;
2813 BYTE* const ostart = (BYTE* const)dst;
2815 BYTE* const oend = ostart + maxDstSize;
2816 size_t errorCode, dumpsLength;
2817 const BYTE* litPtr = dctx->litPtr;
2818 const BYTE* const litEnd = litPtr + dctx->litSize;
2821 U32* DTableLL = dctx->LLTable;
2822 U32* DTableML = dctx->MLTable;
2823 U32* DTableOffb = dctx->OffTable;
2824 BYTE* const base = (BYTE*) (dctx->base);
2826 /* Build Decoding Tables */
2827 errorCode = ZSTD_decodeSeqHeaders(&nbSeq, &dumps, &dumpsLength,
2828 DTableLL, DTableML, DTableOffb,
2830 if (ZSTD_isError(errorCode)) return errorCode;
2833 /* Regen sequences */
2836 seqState_t seqState;
2838 memset(&sequence, 0, sizeof(sequence));
2839 seqState.dumps = dumps;
2840 seqState.dumpsEnd = dumps + dumpsLength;
2841 seqState.prevOffset = sequence.offset = 4;
2842 errorCode = BIT_initDStream(&(seqState.DStream), ip, iend-ip);
2843 if (ERR_isError(errorCode)) return ERROR(corruption_detected);
2844 FSE_initDState(&(seqState.stateLL), &(seqState.DStream), DTableLL);
2845 FSE_initDState(&(seqState.stateOffb), &(seqState.DStream), DTableOffb);
2846 FSE_initDState(&(seqState.stateML), &(seqState.DStream), DTableML);
2848 for ( ; (BIT_reloadDStream(&(seqState.DStream)) <= BIT_DStream_completed) && (nbSeq>0) ; )
2852 ZSTD_decodeSequence(&sequence, &seqState);
2853 oneSeqSize = ZSTD_execSequence(op, sequence, &litPtr, litEnd, base, oend);
2854 if (ZSTD_isError(oneSeqSize)) return oneSeqSize;
2858 /* check if reached exact end */
2859 if ( !BIT_endOfDStream(&(seqState.DStream)) ) return ERROR(corruption_detected); /* requested too much : data is corrupted */
2860 if (nbSeq<0) return ERROR(corruption_detected); /* requested too many sequences : data is corrupted */
2862 /* last literal segment */
2864 size_t lastLLSize = litEnd - litPtr;
2865 if (litPtr > litEnd) return ERROR(corruption_detected);
2866 if (op+lastLLSize > oend) return ERROR(dstSize_tooSmall);
2867 if (op != litPtr) memmove(op, litPtr, lastLLSize);
2876 static size_t ZSTD_decompressBlock(
2878 void* dst, size_t maxDstSize,
2879 const void* src, size_t srcSize)
2881 /* blockType == blockCompressed */
2882 const BYTE* ip = (const BYTE*)src;
2884 /* Decode literals sub-block */
2885 size_t litCSize = ZSTD_decodeLiteralsBlock(ctx, src, srcSize);
2886 if (ZSTD_isError(litCSize)) return litCSize;
2888 srcSize -= litCSize;
2890 return ZSTD_decompressSequences(ctx, dst, maxDstSize, ip, srcSize);
2894 static size_t ZSTD_decompressDCtx(void* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize)
2896 const BYTE* ip = (const BYTE*)src;
2897 const BYTE* iend = ip + srcSize;
2898 BYTE* const ostart = (BYTE* const)dst;
2900 BYTE* const oend = ostart + maxDstSize;
2901 size_t remainingSize = srcSize;
2903 blockProperties_t blockProperties;
2906 if (srcSize < ZSTD_frameHeaderSize+ZSTD_blockHeaderSize) return ERROR(srcSize_wrong);
2907 magicNumber = MEM_readLE32(src);
2908 if (magicNumber != ZSTD_magicNumber) return ERROR(prefix_unknown);
2909 ip += ZSTD_frameHeaderSize; remainingSize -= ZSTD_frameHeaderSize;
2911 /* Loop on each block */
2914 size_t decodedSize=0;
2915 size_t cBlockSize = ZSTD_getcBlockSize(ip, iend-ip, &blockProperties);
2916 if (ZSTD_isError(cBlockSize)) return cBlockSize;
2918 ip += ZSTD_blockHeaderSize;
2919 remainingSize -= ZSTD_blockHeaderSize;
2920 if (cBlockSize > remainingSize) return ERROR(srcSize_wrong);
2922 switch(blockProperties.blockType)
2925 decodedSize = ZSTD_decompressBlock(ctx, op, oend-op, ip, cBlockSize);
2928 decodedSize = ZSTD_copyUncompressedBlock(op, oend-op, ip, cBlockSize);
2931 return ERROR(GENERIC); /* not yet supported */
2935 if (remainingSize) return ERROR(srcSize_wrong);
2938 return ERROR(GENERIC); /* impossible */
2940 if (cBlockSize == 0) break; /* bt_end */
2942 if (ZSTD_isError(decodedSize)) return decodedSize;
2945 remainingSize -= cBlockSize;
2951 static size_t ZSTD_decompress(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
2955 return ZSTD_decompressDCtx(&ctx, dst, maxDstSize, src, srcSize);
2958 /* ZSTD_errorFrameSizeInfoLegacy() :
2959 assumes `cSize` and `dBound` are _not_ NULL */
2960 MEM_STATIC void ZSTD_errorFrameSizeInfoLegacy(size_t* cSize, unsigned long long* dBound, size_t ret)
2963 *dBound = ZSTD_CONTENTSIZE_ERROR;
2966 void ZSTDv03_findFrameSizeInfoLegacy(const void *src, size_t srcSize, size_t* cSize, unsigned long long* dBound)
2968 const BYTE* ip = (const BYTE*)src;
2969 size_t remainingSize = srcSize;
2970 size_t nbBlocks = 0;
2972 blockProperties_t blockProperties;
2975 if (srcSize < ZSTD_frameHeaderSize+ZSTD_blockHeaderSize) {
2976 ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(srcSize_wrong));
2979 magicNumber = MEM_readLE32(src);
2980 if (magicNumber != ZSTD_magicNumber) {
2981 ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(prefix_unknown));
2984 ip += ZSTD_frameHeaderSize; remainingSize -= ZSTD_frameHeaderSize;
2986 /* Loop on each block */
2989 size_t cBlockSize = ZSTD_getcBlockSize(ip, remainingSize, &blockProperties);
2990 if (ZSTD_isError(cBlockSize)) {
2991 ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, cBlockSize);
2995 ip += ZSTD_blockHeaderSize;
2996 remainingSize -= ZSTD_blockHeaderSize;
2997 if (cBlockSize > remainingSize) {
2998 ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(srcSize_wrong));
3002 if (cBlockSize == 0) break; /* bt_end */
3005 remainingSize -= cBlockSize;
3009 *cSize = ip - (const BYTE*)src;
3010 *dBound = nbBlocks * BLOCKSIZE;
3014 /*******************************
3015 * Streaming Decompression API
3016 *******************************/
3018 static size_t ZSTD_resetDCtx(ZSTD_DCtx* dctx)
3020 dctx->expected = ZSTD_frameHeaderSize;
3022 dctx->previousDstEnd = NULL;
3027 static ZSTD_DCtx* ZSTD_createDCtx(void)
3029 ZSTD_DCtx* dctx = (ZSTD_DCtx*)malloc(sizeof(ZSTD_DCtx));
3030 if (dctx==NULL) return NULL;
3031 ZSTD_resetDCtx(dctx);
3035 static size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx)
3041 static size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx)
3043 return dctx->expected;
3046 static size_t ZSTD_decompressContinue(ZSTD_DCtx* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize)
3049 if (srcSize != ctx->expected) return ERROR(srcSize_wrong);
3050 if (dst != ctx->previousDstEnd) /* not contiguous */
3053 /* Decompress : frame header */
3054 if (ctx->phase == 0)
3056 /* Check frame magic header */
3057 U32 magicNumber = MEM_readLE32(src);
3058 if (magicNumber != ZSTD_magicNumber) return ERROR(prefix_unknown);
3060 ctx->expected = ZSTD_blockHeaderSize;
3064 /* Decompress : block header */
3065 if (ctx->phase == 1)
3067 blockProperties_t bp;
3068 size_t blockSize = ZSTD_getcBlockSize(src, ZSTD_blockHeaderSize, &bp);
3069 if (ZSTD_isError(blockSize)) return blockSize;
3070 if (bp.blockType == bt_end)
3077 ctx->expected = blockSize;
3078 ctx->bType = bp.blockType;
3085 /* Decompress : block content */
3091 rSize = ZSTD_decompressBlock(ctx, dst, maxDstSize, src, srcSize);
3094 rSize = ZSTD_copyUncompressedBlock(dst, maxDstSize, src, srcSize);
3097 return ERROR(GENERIC); /* not yet handled */
3099 case bt_end : /* should never happen (filtered at phase 1) */
3103 return ERROR(GENERIC);
3106 ctx->expected = ZSTD_blockHeaderSize;
3107 ctx->previousDstEnd = (void*)( ((char*)dst) + rSize);
3116 unsigned ZSTDv03_isError(size_t code)
3118 return ZSTD_isError(code);
3121 size_t ZSTDv03_decompress( void* dst, size_t maxOriginalSize,
3122 const void* src, size_t compressedSize)
3124 return ZSTD_decompress(dst, maxOriginalSize, src, compressedSize);
3127 ZSTDv03_Dctx* ZSTDv03_createDCtx(void)
3129 return (ZSTDv03_Dctx*)ZSTD_createDCtx();
3132 size_t ZSTDv03_freeDCtx(ZSTDv03_Dctx* dctx)
3134 return ZSTD_freeDCtx((ZSTD_DCtx*)dctx);
3137 size_t ZSTDv03_resetDCtx(ZSTDv03_Dctx* dctx)
3139 return ZSTD_resetDCtx((ZSTD_DCtx*)dctx);
3142 size_t ZSTDv03_nextSrcSizeToDecompress(ZSTDv03_Dctx* dctx)
3144 return ZSTD_nextSrcSizeToDecompress((ZSTD_DCtx*)dctx);
3147 size_t ZSTDv03_decompressContinue(ZSTDv03_Dctx* dctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize)
3149 return ZSTD_decompressContinue((ZSTD_DCtx*)dctx, dst, maxDstSize, src, srcSize);