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_* */
26 /* ******************************************************************
28 low-level memory access routines
29 Copyright (C) 2013-2015, Yann Collet.
31 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
33 Redistribution and use in source and binary forms, with or without
34 modification, are permitted provided that the following conditions are
37 * Redistributions of source code must retain the above copyright
38 notice, this list of conditions and the following disclaimer.
39 * Redistributions in binary form must reproduce the above
40 copyright notice, this list of conditions and the following disclaimer
41 in the documentation and/or other materials provided with the
44 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
45 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
46 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
47 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
48 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
49 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
50 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
51 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
52 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
53 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
54 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56 You can contact the author at :
57 - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy
58 - Public forum : https://groups.google.com/forum/#!forum/lz4c
59 ****************************************************************** */
63 #if defined (__cplusplus)
67 /******************************************
69 ******************************************/
70 #include <stddef.h> /* size_t, ptrdiff_t */
71 #include <string.h> /* memcpy */
74 /******************************************
76 ******************************************/
78 # define MEM_STATIC static __attribute__((unused))
79 #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
80 # define MEM_STATIC static inline
81 #elif defined(_MSC_VER)
82 # define MEM_STATIC static __inline
84 # define MEM_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
88 /****************************************************************
90 *****************************************************************/
91 #if defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
101 typedef unsigned char BYTE;
102 typedef unsigned short U16;
103 typedef signed short S16;
104 typedef unsigned int U32;
105 typedef signed int S32;
106 typedef unsigned long long U64;
107 typedef signed long long S64;
111 /****************************************************************
113 *****************************************************************/
114 /* MEM_FORCE_MEMORY_ACCESS
115 * By default, access to unaligned memory is controlled by `memcpy()`, which is safe and portable.
116 * Unfortunately, on some target/compiler combinations, the generated assembly is sub-optimal.
117 * The below switch allow to select different access method for improved performance.
118 * Method 0 (default) : use `memcpy()`. Safe and portable.
119 * Method 1 : `__packed` statement. It depends on compiler extension (ie, not portable).
120 * This method is safe if your compiler supports it, and *generally* as fast or faster than `memcpy`.
121 * Method 2 : direct access. This method is portable but violate C standard.
122 * It can generate buggy code on targets generating assembly depending on alignment.
123 * But in some circumstances, it's the only known way to get the most performance (ie GCC + ARMv6)
124 * See http://fastcompression.blogspot.fr/2015/08/accessing-unaligned-memory.html for details.
125 * Prefer these methods in priority order (0 > 1 > 2)
127 #ifndef MEM_FORCE_MEMORY_ACCESS /* can be defined externally, on command line for example */
128 # 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__) )
129 # define MEM_FORCE_MEMORY_ACCESS 2
130 # elif (defined(__INTEL_COMPILER) && !defined(WIN32)) || \
131 (defined(__GNUC__) && ( defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7S__) ))
132 # define MEM_FORCE_MEMORY_ACCESS 1
136 MEM_STATIC unsigned MEM_32bits(void) { return sizeof(void*)==4; }
137 MEM_STATIC unsigned MEM_64bits(void) { return sizeof(void*)==8; }
139 MEM_STATIC unsigned MEM_isLittleEndian(void)
141 const union { U32 u; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */
145 #if defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS==2)
147 /* violates C standard on structure alignment.
148 Only use if no other choice to achieve best performance on target platform */
149 MEM_STATIC U16 MEM_read16(const void* memPtr) { return *(const U16*) memPtr; }
150 MEM_STATIC U32 MEM_read32(const void* memPtr) { return *(const U32*) memPtr; }
151 MEM_STATIC U64 MEM_read64(const void* memPtr) { return *(const U64*) memPtr; }
153 MEM_STATIC void MEM_write16(void* memPtr, U16 value) { *(U16*)memPtr = value; }
155 #elif defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS==1)
157 /* __pack instructions are safer, but compiler specific, hence potentially problematic for some compilers */
158 /* currently only defined for gcc and icc */
159 typedef union { U16 u16; U32 u32; U64 u64; } __attribute__((packed)) unalign;
161 MEM_STATIC U16 MEM_read16(const void* ptr) { return ((const unalign*)ptr)->u16; }
162 MEM_STATIC U32 MEM_read32(const void* ptr) { return ((const unalign*)ptr)->u32; }
163 MEM_STATIC U64 MEM_read64(const void* ptr) { return ((const unalign*)ptr)->u64; }
165 MEM_STATIC void MEM_write16(void* memPtr, U16 value) { ((unalign*)memPtr)->u16 = value; }
169 /* default method, safe and standard.
170 can sometimes prove slower */
172 MEM_STATIC U16 MEM_read16(const void* memPtr)
174 U16 val; memcpy(&val, memPtr, sizeof(val)); return val;
177 MEM_STATIC U32 MEM_read32(const void* memPtr)
179 U32 val; memcpy(&val, memPtr, sizeof(val)); return val;
182 MEM_STATIC U64 MEM_read64(const void* memPtr)
184 U64 val; memcpy(&val, memPtr, sizeof(val)); return val;
187 MEM_STATIC void MEM_write16(void* memPtr, U16 value)
189 memcpy(memPtr, &value, sizeof(value));
192 #endif // MEM_FORCE_MEMORY_ACCESS
195 MEM_STATIC U16 MEM_readLE16(const void* memPtr)
197 if (MEM_isLittleEndian())
198 return MEM_read16(memPtr);
201 const BYTE* p = (const BYTE*)memPtr;
202 return (U16)(p[0] + (p[1]<<8));
206 MEM_STATIC void MEM_writeLE16(void* memPtr, U16 val)
208 if (MEM_isLittleEndian())
210 MEM_write16(memPtr, val);
214 BYTE* p = (BYTE*)memPtr;
216 p[1] = (BYTE)(val>>8);
220 MEM_STATIC U32 MEM_readLE32(const void* memPtr)
222 if (MEM_isLittleEndian())
223 return MEM_read32(memPtr);
226 const BYTE* p = (const BYTE*)memPtr;
227 return (U32)((U32)p[0] + ((U32)p[1]<<8) + ((U32)p[2]<<16) + ((U32)p[3]<<24));
232 MEM_STATIC U64 MEM_readLE64(const void* memPtr)
234 if (MEM_isLittleEndian())
235 return MEM_read64(memPtr);
238 const BYTE* p = (const BYTE*)memPtr;
239 return (U64)((U64)p[0] + ((U64)p[1]<<8) + ((U64)p[2]<<16) + ((U64)p[3]<<24)
240 + ((U64)p[4]<<32) + ((U64)p[5]<<40) + ((U64)p[6]<<48) + ((U64)p[7]<<56));
245 MEM_STATIC size_t MEM_readLEST(const void* memPtr)
248 return (size_t)MEM_readLE32(memPtr);
250 return (size_t)MEM_readLE64(memPtr);
253 #if defined (__cplusplus)
257 #endif /* MEM_H_MODULE */
260 /* ******************************************************************
262 Part of NewGen Entropy library
263 header file (to include)
264 Copyright (C) 2013-2015, Yann Collet.
266 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
268 Redistribution and use in source and binary forms, with or without
269 modification, are permitted provided that the following conditions are
272 * Redistributions of source code must retain the above copyright
273 notice, this list of conditions and the following disclaimer.
274 * Redistributions in binary form must reproduce the above
275 copyright notice, this list of conditions and the following disclaimer
276 in the documentation and/or other materials provided with the
279 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
280 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
281 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
282 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
283 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
284 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
285 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
286 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
287 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
288 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
289 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
291 You can contact the author at :
292 - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
293 - Public forum : https://groups.google.com/forum/#!forum/lz4c
294 ****************************************************************** */
295 #ifndef BITSTREAM_H_MODULE
296 #define BITSTREAM_H_MODULE
298 #if defined (__cplusplus)
304 * This API consists of small unitary functions, which highly benefit from being inlined.
305 * Since link-time-optimization is not available for all compilers,
306 * these functions are defined into a .h to be included.
310 /**********************************************
311 * bitStream decompression API (read backward)
312 **********************************************/
316 unsigned bitsConsumed;
321 typedef enum { BIT_DStream_unfinished = 0,
322 BIT_DStream_endOfBuffer = 1,
323 BIT_DStream_completed = 2,
324 BIT_DStream_overflow = 3 } BIT_DStream_status; /* result of BIT_reloadDStream() */
325 /* 1,2,4,8 would be better for bitmap combinations, but slows down performance a bit ... :( */
327 MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize);
328 MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, unsigned nbBits);
329 MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD);
330 MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* bitD);
334 * Start by invoking BIT_initDStream().
335 * A chunk of the bitStream is then stored into a local register.
336 * Local register size is 64-bits on 64-bits systems, 32-bits on 32-bits systems (size_t).
337 * You can then retrieve bitFields stored into the local register, **in reverse order**.
338 * Local register is manually filled from memory by the BIT_reloadDStream() method.
339 * A reload guarantee a minimum of ((8*sizeof(size_t))-7) bits when its result is BIT_DStream_unfinished.
340 * Otherwise, it can be less than that, so proceed accordingly.
341 * Checking if DStream has reached its end can be performed with BIT_endOfDStream()
345 /******************************************
347 ******************************************/
348 MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits);
349 /* faster, but works only if nbBits >= 1 */
353 /****************************************************************
355 ****************************************************************/
356 MEM_STATIC unsigned BIT_highbit32 (register U32 val)
358 # if defined(_MSC_VER) /* Visual */
360 _BitScanReverse ( &r, val );
362 # elif defined(__GNUC__) && (__GNUC__ >= 3) /* Use GCC Intrinsic */
363 return 31 - __builtin_clz (val);
364 # else /* Software version */
365 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 };
373 r = DeBruijnClz[ (U32) (v * 0x07C4ACDDU) >> 27];
380 /**********************************************************
382 **********************************************************/
385 * Initialize a BIT_DStream_t.
386 * @bitD : a pointer to an already allocated BIT_DStream_t structure
387 * @srcBuffer must point at the beginning of a bitStream
388 * @srcSize must be the exact size of the bitStream
389 * @result : size of stream (== srcSize) or an errorCode if a problem is detected
391 MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize)
393 if (srcSize < 1) { memset(bitD, 0, sizeof(*bitD)); return ERROR(srcSize_wrong); }
395 if (srcSize >= sizeof(size_t)) /* normal case */
398 bitD->start = (const char*)srcBuffer;
399 bitD->ptr = (const char*)srcBuffer + srcSize - sizeof(size_t);
400 bitD->bitContainer = MEM_readLEST(bitD->ptr);
401 contain32 = ((const BYTE*)srcBuffer)[srcSize-1];
402 if (contain32 == 0) return ERROR(GENERIC); /* endMark not present */
403 bitD->bitsConsumed = 8 - BIT_highbit32(contain32);
408 bitD->start = (const char*)srcBuffer;
409 bitD->ptr = bitD->start;
410 bitD->bitContainer = *(const BYTE*)(bitD->start);
413 case 7: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[6]) << (sizeof(size_t)*8 - 16);
414 case 6: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[5]) << (sizeof(size_t)*8 - 24);
415 case 5: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[4]) << (sizeof(size_t)*8 - 32);
416 case 4: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[3]) << 24;
417 case 3: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[2]) << 16;
418 case 2: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[1]) << 8;
421 contain32 = ((const BYTE*)srcBuffer)[srcSize-1];
422 if (contain32 == 0) return ERROR(GENERIC); /* endMark not present */
423 bitD->bitsConsumed = 8 - BIT_highbit32(contain32);
424 bitD->bitsConsumed += (U32)(sizeof(size_t) - srcSize)*8;
431 * Provides next n bits from local register
432 * local register is not modified (bits are still present for next read/look)
433 * On 32-bits, maxNbBits==25
434 * On 64-bits, maxNbBits==57
435 * @return : value extracted
437 MEM_STATIC size_t BIT_lookBits(BIT_DStream_t* bitD, U32 nbBits)
439 const U32 bitMask = sizeof(bitD->bitContainer)*8 - 1;
440 return ((bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> 1) >> ((bitMask-nbBits) & bitMask);
443 /*! BIT_lookBitsFast :
444 * unsafe version; only works only if nbBits >= 1 */
445 MEM_STATIC size_t BIT_lookBitsFast(BIT_DStream_t* bitD, U32 nbBits)
447 const U32 bitMask = sizeof(bitD->bitContainer)*8 - 1;
448 return (bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> (((bitMask+1)-nbBits) & bitMask);
451 MEM_STATIC void BIT_skipBits(BIT_DStream_t* bitD, U32 nbBits)
453 bitD->bitsConsumed += nbBits;
457 * Read next n bits from local register.
458 * pay attention to not read more than nbBits contained into local register.
459 * @return : extracted value.
461 MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, U32 nbBits)
463 size_t value = BIT_lookBits(bitD, nbBits);
464 BIT_skipBits(bitD, nbBits);
468 /*!BIT_readBitsFast :
469 * unsafe version; only works only if nbBits >= 1 */
470 MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, U32 nbBits)
472 size_t value = BIT_lookBitsFast(bitD, nbBits);
473 BIT_skipBits(bitD, nbBits);
477 MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD)
479 if (bitD->bitsConsumed > (sizeof(bitD->bitContainer)*8)) /* should never happen */
480 return BIT_DStream_overflow;
482 if (bitD->ptr >= bitD->start + sizeof(bitD->bitContainer))
484 bitD->ptr -= bitD->bitsConsumed >> 3;
485 bitD->bitsConsumed &= 7;
486 bitD->bitContainer = MEM_readLEST(bitD->ptr);
487 return BIT_DStream_unfinished;
489 if (bitD->ptr == bitD->start)
491 if (bitD->bitsConsumed < sizeof(bitD->bitContainer)*8) return BIT_DStream_endOfBuffer;
492 return BIT_DStream_completed;
495 U32 nbBytes = bitD->bitsConsumed >> 3;
496 BIT_DStream_status result = BIT_DStream_unfinished;
497 if (bitD->ptr - nbBytes < bitD->start)
499 nbBytes = (U32)(bitD->ptr - bitD->start); /* ptr > start */
500 result = BIT_DStream_endOfBuffer;
502 bitD->ptr -= nbBytes;
503 bitD->bitsConsumed -= nbBytes*8;
504 bitD->bitContainer = MEM_readLEST(bitD->ptr); /* reminder : srcSize > sizeof(bitD) */
510 * @return Tells if DStream has reached its exact end
512 MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* DStream)
514 return ((DStream->ptr == DStream->start) && (DStream->bitsConsumed == sizeof(DStream->bitContainer)*8));
517 #if defined (__cplusplus)
521 #endif /* BITSTREAM_H_MODULE */
522 /* ******************************************************************
523 Error codes and messages
524 Copyright (C) 2013-2015, Yann Collet
526 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
528 Redistribution and use in source and binary forms, with or without
529 modification, are permitted provided that the following conditions are
532 * Redistributions of source code must retain the above copyright
533 notice, this list of conditions and the following disclaimer.
534 * Redistributions in binary form must reproduce the above
535 copyright notice, this list of conditions and the following disclaimer
536 in the documentation and/or other materials provided with the
539 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
540 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
541 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
542 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
543 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
544 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
545 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
546 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
547 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
548 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
549 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
551 You can contact the author at :
552 - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
553 - Public forum : https://groups.google.com/forum/#!forum/lz4c
554 ****************************************************************** */
555 #ifndef ERROR_H_MODULE
556 #define ERROR_H_MODULE
558 #if defined (__cplusplus)
563 /******************************************
565 ******************************************/
566 #if defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
567 # define ERR_STATIC static inline
568 #elif defined(_MSC_VER)
569 # define ERR_STATIC static __inline
570 #elif defined(__GNUC__)
571 # define ERR_STATIC static __attribute__((unused))
573 # define ERR_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
577 /******************************************
579 ******************************************/
580 #define PREFIX(name) ZSTD_error_##name
582 #define ERROR(name) (size_t)-PREFIX(name)
584 #define ERROR_LIST(ITEM) \
585 ITEM(PREFIX(No_Error)) ITEM(PREFIX(GENERIC)) \
586 ITEM(PREFIX(dstSize_tooSmall)) ITEM(PREFIX(srcSize_wrong)) \
587 ITEM(PREFIX(prefix_unknown)) ITEM(PREFIX(corruption_detected)) \
588 ITEM(PREFIX(tableLog_tooLarge)) ITEM(PREFIX(maxSymbolValue_tooLarge)) ITEM(PREFIX(maxSymbolValue_tooSmall)) \
589 ITEM(PREFIX(maxCode))
591 #define ERROR_GENERATE_ENUM(ENUM) ENUM,
592 typedef enum { ERROR_LIST(ERROR_GENERATE_ENUM) } ERR_codes; /* enum is exposed, to detect & handle specific errors; compare function result to -enum value */
594 #define ERROR_CONVERTTOSTRING(STRING) #STRING,
595 #define ERROR_GENERATE_STRING(EXPR) ERROR_CONVERTTOSTRING(EXPR)
596 static const char* ERR_strings[] = { ERROR_LIST(ERROR_GENERATE_STRING) };
598 ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); }
600 ERR_STATIC const char* ERR_getErrorName(size_t code)
602 static const char* codeError = "Unspecified error code";
603 if (ERR_isError(code)) return ERR_strings[-(int)(code)];
608 #if defined (__cplusplus)
612 #endif /* ERROR_H_MODULE */
614 Constructor and Destructor of type FSE_CTable
615 Note that its size depends on 'tableLog' and 'maxSymbolValue' */
616 typedef unsigned FSE_CTable; /* don't allocate that. It's just a way to be more restrictive than void* */
617 typedef unsigned FSE_DTable; /* don't allocate that. It's just a way to be more restrictive than void* */
620 /* ******************************************************************
621 FSE : Finite State Entropy coder
622 header file for static linking (only)
623 Copyright (C) 2013-2015, Yann Collet
625 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
627 Redistribution and use in source and binary forms, with or without
628 modification, are permitted provided that the following conditions are
631 * Redistributions of source code must retain the above copyright
632 notice, this list of conditions and the following disclaimer.
633 * Redistributions in binary form must reproduce the above
634 copyright notice, this list of conditions and the following disclaimer
635 in the documentation and/or other materials provided with the
638 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
639 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
640 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
641 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
642 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
643 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
644 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
645 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
646 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
647 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
648 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
650 You can contact the author at :
651 - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
652 - Public forum : https://groups.google.com/forum/#!forum/lz4c
653 ****************************************************************** */
654 #if defined (__cplusplus)
659 /******************************************
661 ******************************************/
662 /* FSE buffer bounds */
663 #define FSE_NCOUNTBOUND 512
664 #define FSE_BLOCKBOUND(size) (size + (size>>7))
665 #define FSE_COMPRESSBOUND(size) (FSE_NCOUNTBOUND + FSE_BLOCKBOUND(size)) /* Macro version, useful for static allocation */
667 /* You can statically allocate FSE CTable/DTable as a table of unsigned using below macro */
668 #define FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue) (1 + (1<<(maxTableLog-1)) + ((maxSymbolValue+1)*2))
669 #define FSE_DTABLE_SIZE_U32(maxTableLog) (1 + (1<<maxTableLog))
672 /******************************************
674 ******************************************/
675 static size_t FSE_buildDTable_raw (FSE_DTable* dt, unsigned nbBits);
676 /* build a fake FSE_DTable, designed to read an uncompressed bitstream where each symbol uses nbBits */
678 static size_t FSE_buildDTable_rle (FSE_DTable* dt, unsigned char symbolValue);
679 /* build a fake FSE_DTable, designed to always generate the same symbolValue */
682 /******************************************
683 * FSE symbol decompression API
684 ******************************************/
688 const void* table; /* precise table may vary, depending on U16 */
692 static void FSE_initDState(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD, const FSE_DTable* dt);
694 static unsigned char FSE_decodeSymbol(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD);
696 static unsigned FSE_endOfDState(const FSE_DState_t* DStatePtr);
699 Let's now decompose FSE_decompress_usingDTable() into its unitary components.
700 You will decode FSE-encoded symbols from the bitStream,
701 and also any other bitFields you put in, **in reverse order**.
703 You will need a few variables to track your bitStream. They are :
705 BIT_DStream_t DStream; // Stream context
706 FSE_DState_t DState; // State context. Multiple ones are possible
707 FSE_DTable* DTablePtr; // Decoding table, provided by FSE_buildDTable()
709 The first thing to do is to init the bitStream.
710 errorCode = BIT_initDStream(&DStream, srcBuffer, srcSize);
712 You should then retrieve your initial state(s)
713 (in reverse flushing order if you have several ones) :
714 errorCode = FSE_initDState(&DState, &DStream, DTablePtr);
716 You can then decode your data, symbol after symbol.
717 For information the maximum number of bits read by FSE_decodeSymbol() is 'tableLog'.
718 Keep in mind that symbols are decoded in reverse order, like a LIFO stack (last in, first out).
719 unsigned char symbol = FSE_decodeSymbol(&DState, &DStream);
721 You can retrieve any bitfield you eventually stored into the bitStream (in reverse order)
722 Note : maximum allowed nbBits is 25, for 32-bits compatibility
723 size_t bitField = BIT_readBits(&DStream, nbBits);
725 All above operations only read from local register (which size depends on size_t).
726 Refueling the register from memory is manually performed by the reload method.
727 endSignal = FSE_reloadDStream(&DStream);
729 BIT_reloadDStream() result tells if there is still some more data to read from DStream.
730 BIT_DStream_unfinished : there is still some data left into the DStream.
731 BIT_DStream_endOfBuffer : Dstream reached end of buffer. Its container may no longer be completely filled.
732 BIT_DStream_completed : Dstream reached its exact end, corresponding in general to decompression completed.
733 BIT_DStream_tooFar : Dstream went too far. Decompression result is corrupted.
735 When reaching end of buffer (BIT_DStream_endOfBuffer), progress slowly, notably if you decode multiple symbols per loop,
736 to properly detect the exact end of stream.
737 After each decoded symbol, check if DStream is fully consumed using this simple test :
738 BIT_reloadDStream(&DStream) >= BIT_DStream_completed
740 When it's done, verify decompression is fully completed, by checking both DStream and the relevant states.
741 Checking if DStream has reached its end is performed by :
742 BIT_endOfDStream(&DStream);
743 Check also the states. There might be some symbols left there, if some high probability ones (>50%) are possible.
744 FSE_endOfDState(&DState);
748 /******************************************
750 ******************************************/
751 static unsigned char FSE_decodeSymbolFast(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD);
752 /* faster, but works only if nbBits is always >= 1 (otherwise, result will be corrupted) */
755 /******************************************
756 * Implementation of inline functions
757 ******************************************/
764 } FSE_DTableHeader; /* sizeof U32 */
768 unsigned short newState;
769 unsigned char symbol;
770 unsigned char nbBits;
771 } FSE_decode_t; /* size == U32 */
773 MEM_STATIC void FSE_initDState(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD, const FSE_DTable* dt)
775 FSE_DTableHeader DTableH;
776 memcpy(&DTableH, dt, sizeof(DTableH));
777 DStatePtr->state = BIT_readBits(bitD, DTableH.tableLog);
778 BIT_reloadDStream(bitD);
779 DStatePtr->table = dt + 1;
782 MEM_STATIC BYTE FSE_decodeSymbol(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD)
784 const FSE_decode_t DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state];
785 const U32 nbBits = DInfo.nbBits;
786 BYTE symbol = DInfo.symbol;
787 size_t lowBits = BIT_readBits(bitD, nbBits);
789 DStatePtr->state = DInfo.newState + lowBits;
793 MEM_STATIC BYTE FSE_decodeSymbolFast(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD)
795 const FSE_decode_t DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state];
796 const U32 nbBits = DInfo.nbBits;
797 BYTE symbol = DInfo.symbol;
798 size_t lowBits = BIT_readBitsFast(bitD, nbBits);
800 DStatePtr->state = DInfo.newState + lowBits;
804 MEM_STATIC unsigned FSE_endOfDState(const FSE_DState_t* DStatePtr)
806 return DStatePtr->state == 0;
810 #if defined (__cplusplus)
813 /* ******************************************************************
814 Huff0 : Huffman coder, part of New Generation Entropy library
815 header file for static linking (only)
816 Copyright (C) 2013-2015, Yann Collet
818 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
820 Redistribution and use in source and binary forms, with or without
821 modification, are permitted provided that the following conditions are
824 * Redistributions of source code must retain the above copyright
825 notice, this list of conditions and the following disclaimer.
826 * Redistributions in binary form must reproduce the above
827 copyright notice, this list of conditions and the following disclaimer
828 in the documentation and/or other materials provided with the
831 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
832 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
833 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
834 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
835 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
836 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
837 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
838 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
839 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
840 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
841 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
843 You can contact the author at :
844 - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
845 - Public forum : https://groups.google.com/forum/#!forum/lz4c
846 ****************************************************************** */
848 #if defined (__cplusplus)
852 /******************************************
853 * Static allocation macros
854 ******************************************/
855 /* Huff0 buffer bounds */
856 #define HUF_CTABLEBOUND 129
857 #define HUF_BLOCKBOUND(size) (size + (size>>8) + 8) /* only true if incompressible pre-filtered with fast heuristic */
858 #define HUF_COMPRESSBOUND(size) (HUF_CTABLEBOUND + HUF_BLOCKBOUND(size)) /* Macro version, useful for static allocation */
860 /* static allocation of Huff0's DTable */
861 #define HUF_DTABLE_SIZE(maxTableLog) (1 + (1<<maxTableLog)) /* nb Cells; use unsigned short for X2, unsigned int for X4 */
862 #define HUF_CREATE_STATIC_DTABLEX2(DTable, maxTableLog) \
863 unsigned short DTable[HUF_DTABLE_SIZE(maxTableLog)] = { maxTableLog }
864 #define HUF_CREATE_STATIC_DTABLEX4(DTable, maxTableLog) \
865 unsigned int DTable[HUF_DTABLE_SIZE(maxTableLog)] = { maxTableLog }
866 #define HUF_CREATE_STATIC_DTABLEX6(DTable, maxTableLog) \
867 unsigned int DTable[HUF_DTABLE_SIZE(maxTableLog) * 3 / 2] = { maxTableLog }
870 /******************************************
872 ******************************************/
873 static size_t HUF_decompress4X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* single-symbol decoder */
874 static size_t HUF_decompress4X4 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* double-symbols decoder */
875 static size_t HUF_decompress4X6 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* quad-symbols decoder */
878 #if defined (__cplusplus)
883 zstd - standard compression library
885 Copyright (C) 2014-2015, Yann Collet.
887 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
889 Redistribution and use in source and binary forms, with or without
890 modification, are permitted provided that the following conditions are
892 * Redistributions of source code must retain the above copyright
893 notice, this list of conditions and the following disclaimer.
894 * Redistributions in binary form must reproduce the above
895 copyright notice, this list of conditions and the following disclaimer
896 in the documentation and/or other materials provided with the
898 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
899 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
900 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
901 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
902 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
903 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
904 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
905 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
906 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
907 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
908 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
910 You can contact the author at :
911 - zstd source repository : https://github.com/Cyan4973/zstd
912 - ztsd public forum : https://groups.google.com/forum/#!forum/lz4c
915 #if defined (__cplusplus)
919 /* *************************************
921 ***************************************/
922 #include <stddef.h> /* size_t */
925 /* *************************************
927 ***************************************/
928 #define ZSTD_VERSION_MAJOR 0 /* for breaking interface changes */
929 #define ZSTD_VERSION_MINOR 2 /* for new (non-breaking) interface capabilities */
930 #define ZSTD_VERSION_RELEASE 2 /* for tweaks, bug-fixes, or development */
931 #define ZSTD_VERSION_NUMBER (ZSTD_VERSION_MAJOR *100*100 + ZSTD_VERSION_MINOR *100 + ZSTD_VERSION_RELEASE)
934 /* *************************************
936 ***************************************/
937 typedef struct ZSTD_CCtx_s ZSTD_CCtx; /* incomplete type */
939 #if defined (__cplusplus)
943 zstd - standard compression library
944 Header File for static linking only
945 Copyright (C) 2014-2015, Yann Collet.
947 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
949 Redistribution and use in source and binary forms, with or without
950 modification, are permitted provided that the following conditions are
952 * Redistributions of source code must retain the above copyright
953 notice, this list of conditions and the following disclaimer.
954 * Redistributions in binary form must reproduce the above
955 copyright notice, this list of conditions and the following disclaimer
956 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 - zstd source repository : https://github.com/Cyan4973/zstd
972 - ztsd public forum : https://groups.google.com/forum/#!forum/lz4c
975 /* The objects defined into this file should be considered experimental.
976 * They are not labelled stable, as their prototype may change in the future.
977 * You can use them for tests, provide feedback, or if you can endure risk of future changes.
980 #if defined (__cplusplus)
984 /* *************************************
985 * Streaming functions
986 ***************************************/
988 typedef struct ZSTD_DCtx_s ZSTD_DCtx;
991 Use above functions alternatively.
992 ZSTD_nextSrcSizeToDecompress() tells how much bytes to provide as 'srcSize' to ZSTD_decompressContinue().
993 ZSTD_decompressContinue() will use previous data blocks to improve compression if they are located prior to current block.
994 Result is the number of bytes regenerated within 'dst'.
995 It can be zero, which is not an error; it just means ZSTD_decompressContinue() has decoded some header.
998 /* *************************************
999 * Prefix - version detection
1000 ***************************************/
1001 #define ZSTD_magicNumber 0xFD2FB522 /* v0.2 (current)*/
1004 #if defined (__cplusplus)
1007 /* ******************************************************************
1008 FSE : Finite State Entropy coder
1009 Copyright (C) 2013-2015, Yann Collet.
1011 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
1013 Redistribution and use in source and binary forms, with or without
1014 modification, are permitted provided that the following conditions are
1017 * Redistributions of source code must retain the above copyright
1018 notice, this list of conditions and the following disclaimer.
1019 * Redistributions in binary form must reproduce the above
1020 copyright notice, this list of conditions and the following disclaimer
1021 in the documentation and/or other materials provided with the
1024 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1025 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1026 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1027 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1028 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1029 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1030 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1031 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1032 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1033 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1034 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1036 You can contact the author at :
1037 - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy
1038 - Public forum : https://groups.google.com/forum/#!forum/lz4c
1039 ****************************************************************** */
1041 #ifndef FSE_COMMONDEFS_ONLY
1043 /****************************************************************
1045 ****************************************************************/
1047 * Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.)
1048 * Increasing memory usage improves compression ratio
1049 * Reduced memory usage can improve speed, due to cache effect
1050 * Recommended max value is 14, for 16KB, which nicely fits into Intel x86 L1 cache */
1051 #define FSE_MAX_MEMORY_USAGE 14
1052 #define FSE_DEFAULT_MEMORY_USAGE 13
1054 /* FSE_MAX_SYMBOL_VALUE :
1055 * Maximum symbol value authorized.
1056 * Required for proper stack allocation */
1057 #define FSE_MAX_SYMBOL_VALUE 255
1060 /****************************************************************
1061 * template functions type & suffix
1062 ****************************************************************/
1063 #define FSE_FUNCTION_TYPE BYTE
1064 #define FSE_FUNCTION_EXTENSION
1067 /****************************************************************
1069 ****************************************************************/
1070 #endif /* !FSE_COMMONDEFS_ONLY */
1073 /****************************************************************
1074 * Compiler specifics
1075 ****************************************************************/
1076 #ifdef _MSC_VER /* Visual Studio */
1077 # define FORCE_INLINE static __forceinline
1078 # include <intrin.h> /* For Visual 2005 */
1079 # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
1080 # pragma warning(disable : 4214) /* disable: C4214: non-int bitfields */
1082 # if defined (__cplusplus) || defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
1084 # define FORCE_INLINE static inline __attribute__((always_inline))
1086 # define FORCE_INLINE static inline
1089 # define FORCE_INLINE static
1090 # endif /* __STDC_VERSION__ */
1094 /****************************************************************
1096 ****************************************************************/
1097 #include <stdlib.h> /* malloc, free, qsort */
1098 #include <string.h> /* memcpy, memset */
1099 #include <stdio.h> /* printf (debug) */
1101 /****************************************************************
1103 *****************************************************************/
1104 #define FSE_MAX_TABLELOG (FSE_MAX_MEMORY_USAGE-2)
1105 #define FSE_MAX_TABLESIZE (1U<<FSE_MAX_TABLELOG)
1106 #define FSE_MAXTABLESIZE_MASK (FSE_MAX_TABLESIZE-1)
1107 #define FSE_DEFAULT_TABLELOG (FSE_DEFAULT_MEMORY_USAGE-2)
1108 #define FSE_MIN_TABLELOG 5
1110 #define FSE_TABLELOG_ABSOLUTE_MAX 15
1111 #if FSE_MAX_TABLELOG > FSE_TABLELOG_ABSOLUTE_MAX
1112 #error "FSE_MAX_TABLELOG > FSE_TABLELOG_ABSOLUTE_MAX is not supported"
1116 /****************************************************************
1118 ****************************************************************/
1119 #define FSE_STATIC_ASSERT(c) { enum { FSE_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */
1122 /****************************************************************
1124 ****************************************************************/
1125 typedef U32 DTable_max_t[FSE_DTABLE_SIZE_U32(FSE_MAX_TABLELOG)];
1128 /****************************************************************
1130 ****************************************************************/
1132 designed to be included
1133 for type-specific functions (template emulation in C)
1134 Objective is to write these functions only once, for improved maintenance
1138 #ifndef FSE_FUNCTION_EXTENSION
1139 # error "FSE_FUNCTION_EXTENSION must be defined"
1141 #ifndef FSE_FUNCTION_TYPE
1142 # error "FSE_FUNCTION_TYPE must be defined"
1145 /* Function names */
1146 #define FSE_CAT(X,Y) X##Y
1147 #define FSE_FUNCTION_NAME(X,Y) FSE_CAT(X,Y)
1148 #define FSE_TYPE_NAME(X,Y) FSE_CAT(X,Y)
1151 /* Function templates */
1153 #define FSE_DECODE_TYPE FSE_decode_t
1155 static U32 FSE_tableStep(U32 tableSize) { return (tableSize>>1) + (tableSize>>3) + 3; }
1157 static size_t FSE_buildDTable
1158 (FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog)
1161 FSE_DECODE_TYPE* const tableDecode = (FSE_DECODE_TYPE*)ptr;
1162 FSE_DTableHeader DTableH;
1163 const U32 tableSize = 1 << tableLog;
1164 const U32 tableMask = tableSize-1;
1165 const U32 step = FSE_tableStep(tableSize);
1166 U16 symbolNext[FSE_MAX_SYMBOL_VALUE+1];
1168 U32 highThreshold = tableSize-1;
1169 const S16 largeLimit= (S16)(1 << (tableLog-1));
1174 if (maxSymbolValue > FSE_MAX_SYMBOL_VALUE) return ERROR(maxSymbolValue_tooLarge);
1175 if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge);
1177 /* Init, lay down lowprob symbols */
1178 DTableH.tableLog = (U16)tableLog;
1179 for (s=0; s<=maxSymbolValue; s++)
1181 if (normalizedCounter[s]==-1)
1183 tableDecode[highThreshold--].symbol = (FSE_FUNCTION_TYPE)s;
1188 if (normalizedCounter[s] >= largeLimit) noLarge=0;
1189 symbolNext[s] = normalizedCounter[s];
1193 /* Spread symbols */
1194 for (s=0; s<=maxSymbolValue; s++)
1197 for (i=0; i<normalizedCounter[s]; i++)
1199 tableDecode[position].symbol = (FSE_FUNCTION_TYPE)s;
1200 position = (position + step) & tableMask;
1201 while (position > highThreshold) position = (position + step) & tableMask; /* lowprob area */
1205 if (position!=0) return ERROR(GENERIC); /* position must reach all cells once, otherwise normalizedCounter is incorrect */
1207 /* Build Decoding table */
1210 for (i=0; i<tableSize; i++)
1212 FSE_FUNCTION_TYPE symbol = (FSE_FUNCTION_TYPE)(tableDecode[i].symbol);
1213 U16 nextState = symbolNext[symbol]++;
1214 tableDecode[i].nbBits = (BYTE) (tableLog - BIT_highbit32 ((U32)nextState) );
1215 tableDecode[i].newState = (U16) ( (nextState << tableDecode[i].nbBits) - tableSize);
1219 DTableH.fastMode = (U16)noLarge;
1220 memcpy(dt, &DTableH, sizeof(DTableH)); /* memcpy(), to avoid strict aliasing warnings */
1225 #ifndef FSE_COMMONDEFS_ONLY
1226 /******************************************
1227 * FSE helper functions
1228 ******************************************/
1229 static unsigned FSE_isError(size_t code) { return ERR_isError(code); }
1232 /****************************************************************
1233 * FSE NCount encoding-decoding
1234 ****************************************************************/
1235 static short FSE_abs(short a)
1237 return (short)(a<0 ? -a : a);
1240 static size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
1241 const void* headerBuffer, size_t hbSize)
1243 const BYTE* const istart = (const BYTE*) headerBuffer;
1244 const BYTE* const iend = istart + hbSize;
1245 const BYTE* ip = istart;
1251 unsigned charnum = 0;
1254 if (hbSize < 4) return ERROR(srcSize_wrong);
1255 bitStream = MEM_readLE32(ip);
1256 nbBits = (bitStream & 0xF) + FSE_MIN_TABLELOG; /* extract tableLog */
1257 if (nbBits > FSE_TABLELOG_ABSOLUTE_MAX) return ERROR(tableLog_tooLarge);
1260 *tableLogPtr = nbBits;
1261 remaining = (1<<nbBits)+1;
1262 threshold = 1<<nbBits;
1265 while ((remaining>1) && (charnum<=*maxSVPtr))
1269 unsigned n0 = charnum;
1270 while ((bitStream & 0xFFFF) == 0xFFFF)
1276 bitStream = MEM_readLE32(ip) >> bitCount;
1284 while ((bitStream & 3) == 3)
1290 n0 += bitStream & 3;
1292 if (n0 > *maxSVPtr) return ERROR(maxSymbolValue_tooSmall);
1293 while (charnum < n0) normalizedCounter[charnum++] = 0;
1294 if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4))
1298 bitStream = MEM_readLE32(ip) >> bitCount;
1304 const short max = (short)((2*threshold-1)-remaining);
1307 if ((bitStream & (threshold-1)) < (U32)max)
1309 count = (short)(bitStream & (threshold-1));
1310 bitCount += nbBits-1;
1314 count = (short)(bitStream & (2*threshold-1));
1315 if (count >= threshold) count -= max;
1319 count--; /* extra accuracy */
1320 remaining -= FSE_abs(count);
1321 normalizedCounter[charnum++] = count;
1323 while (remaining < threshold)
1330 if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4))
1337 bitCount -= (int)(8 * (iend - 4 - ip));
1340 bitStream = MEM_readLE32(ip) >> (bitCount & 31);
1344 if (remaining != 1) return ERROR(GENERIC);
1345 *maxSVPtr = charnum-1;
1347 ip += (bitCount+7)>>3;
1348 if ((size_t)(ip-istart) > hbSize) return ERROR(srcSize_wrong);
1353 /*********************************************************
1354 * Decompression (Byte symbols)
1355 *********************************************************/
1356 static size_t FSE_buildDTable_rle (FSE_DTable* dt, BYTE symbolValue)
1359 FSE_DTableHeader* const DTableH = (FSE_DTableHeader*)ptr;
1360 FSE_decode_t* const cell = (FSE_decode_t*)(ptr) + 1; /* because dt is unsigned */
1362 DTableH->tableLog = 0;
1363 DTableH->fastMode = 0;
1366 cell->symbol = symbolValue;
1373 static size_t FSE_buildDTable_raw (FSE_DTable* dt, unsigned nbBits)
1376 FSE_DTableHeader* const DTableH = (FSE_DTableHeader*)ptr;
1377 FSE_decode_t* const dinfo = (FSE_decode_t*)(ptr) + 1; /* because dt is unsigned */
1378 const unsigned tableSize = 1 << nbBits;
1379 const unsigned tableMask = tableSize - 1;
1380 const unsigned maxSymbolValue = tableMask;
1384 if (nbBits < 1) return ERROR(GENERIC); /* min size */
1386 /* Build Decoding Table */
1387 DTableH->tableLog = (U16)nbBits;
1388 DTableH->fastMode = 1;
1389 for (s=0; s<=maxSymbolValue; s++)
1391 dinfo[s].newState = 0;
1392 dinfo[s].symbol = (BYTE)s;
1393 dinfo[s].nbBits = (BYTE)nbBits;
1399 FORCE_INLINE size_t FSE_decompress_usingDTable_generic(
1400 void* dst, size_t maxDstSize,
1401 const void* cSrc, size_t cSrcSize,
1402 const FSE_DTable* dt, const unsigned fast)
1404 BYTE* const ostart = (BYTE*) dst;
1406 BYTE* const omax = op + maxDstSize;
1407 BYTE* const olimit = omax-3;
1410 FSE_DState_t state1;
1411 FSE_DState_t state2;
1415 errorCode = BIT_initDStream(&bitD, cSrc, cSrcSize); /* replaced last arg by maxCompressed Size */
1416 if (FSE_isError(errorCode)) return errorCode;
1418 FSE_initDState(&state1, &bitD, dt);
1419 FSE_initDState(&state2, &bitD, dt);
1421 #define FSE_GETSYMBOL(statePtr) fast ? FSE_decodeSymbolFast(statePtr, &bitD) : FSE_decodeSymbol(statePtr, &bitD)
1423 /* 4 symbols per loop */
1424 for ( ; (BIT_reloadDStream(&bitD)==BIT_DStream_unfinished) && (op<olimit) ; op+=4)
1426 op[0] = FSE_GETSYMBOL(&state1);
1428 if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */
1429 BIT_reloadDStream(&bitD);
1431 op[1] = FSE_GETSYMBOL(&state2);
1433 if (FSE_MAX_TABLELOG*4+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */
1434 { if (BIT_reloadDStream(&bitD) > BIT_DStream_unfinished) { op+=2; break; } }
1436 op[2] = FSE_GETSYMBOL(&state1);
1438 if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */
1439 BIT_reloadDStream(&bitD);
1441 op[3] = FSE_GETSYMBOL(&state2);
1445 /* note : BIT_reloadDStream(&bitD) >= FSE_DStream_partiallyFilled; Ends at exactly BIT_DStream_completed */
1448 if ( (BIT_reloadDStream(&bitD)>BIT_DStream_completed) || (op==omax) || (BIT_endOfDStream(&bitD) && (fast || FSE_endOfDState(&state1))) )
1451 *op++ = FSE_GETSYMBOL(&state1);
1453 if ( (BIT_reloadDStream(&bitD)>BIT_DStream_completed) || (op==omax) || (BIT_endOfDStream(&bitD) && (fast || FSE_endOfDState(&state2))) )
1456 *op++ = FSE_GETSYMBOL(&state2);
1460 if (BIT_endOfDStream(&bitD) && FSE_endOfDState(&state1) && FSE_endOfDState(&state2))
1463 if (op==omax) return ERROR(dstSize_tooSmall); /* dst buffer is full, but cSrc unfinished */
1465 return ERROR(corruption_detected);
1469 static size_t FSE_decompress_usingDTable(void* dst, size_t originalSize,
1470 const void* cSrc, size_t cSrcSize,
1471 const FSE_DTable* dt)
1473 FSE_DTableHeader DTableH;
1474 memcpy(&DTableH, dt, sizeof(DTableH));
1476 /* select fast mode (static) */
1477 if (DTableH.fastMode) return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 1);
1478 return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 0);
1482 static size_t FSE_decompress(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize)
1484 const BYTE* const istart = (const BYTE*)cSrc;
1485 const BYTE* ip = istart;
1486 short counting[FSE_MAX_SYMBOL_VALUE+1];
1487 DTable_max_t dt; /* Static analyzer seems unable to understand this table will be properly initialized later */
1489 unsigned maxSymbolValue = FSE_MAX_SYMBOL_VALUE;
1492 if (cSrcSize<2) return ERROR(srcSize_wrong); /* too small input size */
1494 /* normal FSE decoding mode */
1495 errorCode = FSE_readNCount (counting, &maxSymbolValue, &tableLog, istart, cSrcSize);
1496 if (FSE_isError(errorCode)) return errorCode;
1497 if (errorCode >= cSrcSize) return ERROR(srcSize_wrong); /* too small input size */
1499 cSrcSize -= errorCode;
1501 errorCode = FSE_buildDTable (dt, counting, maxSymbolValue, tableLog);
1502 if (FSE_isError(errorCode)) return errorCode;
1504 /* always return, even if it is an error code */
1505 return FSE_decompress_usingDTable (dst, maxDstSize, ip, cSrcSize, dt);
1510 #endif /* FSE_COMMONDEFS_ONLY */
1511 /* ******************************************************************
1512 Huff0 : Huffman coder, part of New Generation Entropy library
1513 Copyright (C) 2013-2015, Yann Collet.
1515 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
1517 Redistribution and use in source and binary forms, with or without
1518 modification, are permitted provided that the following conditions are
1521 * Redistributions of source code must retain the above copyright
1522 notice, this list of conditions and the following disclaimer.
1523 * Redistributions in binary form must reproduce the above
1524 copyright notice, this list of conditions and the following disclaimer
1525 in the documentation and/or other materials provided with the
1528 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1529 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1530 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1531 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1532 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1533 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1534 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1535 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1536 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1537 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1538 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1540 You can contact the author at :
1541 - FSE+Huff0 source repository : https://github.com/Cyan4973/FiniteStateEntropy
1542 - Public forum : https://groups.google.com/forum/#!forum/lz4c
1543 ****************************************************************** */
1545 /****************************************************************
1546 * Compiler specifics
1547 ****************************************************************/
1548 #if defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
1549 /* inline is defined */
1550 #elif defined(_MSC_VER)
1551 # define inline __inline
1553 # define inline /* disable inline */
1557 #ifdef _MSC_VER /* Visual Studio */
1558 # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
1562 /****************************************************************
1564 ****************************************************************/
1565 #include <stdlib.h> /* malloc, free, qsort */
1566 #include <string.h> /* memcpy, memset */
1567 #include <stdio.h> /* printf (debug) */
1569 /****************************************************************
1571 ****************************************************************/
1572 #define HUF_STATIC_ASSERT(c) { enum { HUF_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */
1575 /******************************************
1577 ******************************************/
1578 static unsigned HUF_isError(size_t code) { return ERR_isError(code); }
1580 #define HUF_ABSOLUTEMAX_TABLELOG 16 /* absolute limit of HUF_MAX_TABLELOG. Beyond that value, code does not work */
1581 #define HUF_MAX_TABLELOG 12 /* max configured tableLog (for static allocation); can be modified up to HUF_ABSOLUTEMAX_TABLELOG */
1582 #define HUF_DEFAULT_TABLELOG HUF_MAX_TABLELOG /* tableLog by default, when not specified */
1583 #define HUF_MAX_SYMBOL_VALUE 255
1584 #if (HUF_MAX_TABLELOG > HUF_ABSOLUTEMAX_TABLELOG)
1585 # error "HUF_MAX_TABLELOG is too large !"
1590 /*********************************************************
1591 * Huff0 : Huffman block decompression
1592 *********************************************************/
1593 typedef struct { BYTE byte; BYTE nbBits; } HUF_DEltX2; /* single-symbol decoding */
1595 typedef struct { U16 sequence; BYTE nbBits; BYTE length; } HUF_DEltX4; /* double-symbols decoding */
1597 typedef struct { BYTE symbol; BYTE weight; } sortedSymbol_t;
1600 Read compact Huffman tree, saved by HUF_writeCTable
1601 @huffWeight : destination buffer
1602 @return : size read from `src`
1604 static size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
1605 U32* nbSymbolsPtr, U32* tableLogPtr,
1606 const void* src, size_t srcSize)
1610 const BYTE* ip = (const BYTE*) src;
1615 if (!srcSize) return ERROR(srcSize_wrong);
1617 //memset(huffWeight, 0, hwSize); /* is not necessary, even though some analyzer complain ... */
1619 if (iSize >= 128) /* special header */
1621 if (iSize >= (242)) /* RLE */
1623 static int l[14] = { 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64, 127, 128 };
1624 oSize = l[iSize-242];
1625 memset(huffWeight, 1, hwSize);
1628 else /* Incompressible */
1630 oSize = iSize - 127;
1631 iSize = ((oSize+1)/2);
1632 if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
1633 if (oSize >= hwSize) return ERROR(corruption_detected);
1635 for (n=0; n<oSize; n+=2)
1637 huffWeight[n] = ip[n/2] >> 4;
1638 huffWeight[n+1] = ip[n/2] & 15;
1642 else /* header compressed with FSE (normal case) */
1644 if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
1645 oSize = FSE_decompress(huffWeight, hwSize-1, ip+1, iSize); /* max (hwSize-1) values decoded, as last one is implied */
1646 if (FSE_isError(oSize)) return oSize;
1649 /* collect weight stats */
1650 memset(rankStats, 0, (HUF_ABSOLUTEMAX_TABLELOG + 1) * sizeof(U32));
1652 for (n=0; n<oSize; n++)
1654 if (huffWeight[n] >= HUF_ABSOLUTEMAX_TABLELOG) return ERROR(corruption_detected);
1655 rankStats[huffWeight[n]]++;
1656 weightTotal += (1 << huffWeight[n]) >> 1;
1658 if (weightTotal == 0) return ERROR(corruption_detected);
1660 /* get last non-null symbol weight (implied, total must be 2^n) */
1661 tableLog = BIT_highbit32(weightTotal) + 1;
1662 if (tableLog > HUF_ABSOLUTEMAX_TABLELOG) return ERROR(corruption_detected);
1664 U32 total = 1 << tableLog;
1665 U32 rest = total - weightTotal;
1666 U32 verif = 1 << BIT_highbit32(rest);
1667 U32 lastWeight = BIT_highbit32(rest) + 1;
1668 if (verif != rest) return ERROR(corruption_detected); /* last value must be a clean power of 2 */
1669 huffWeight[oSize] = (BYTE)lastWeight;
1670 rankStats[lastWeight]++;
1673 /* check tree construction validity */
1674 if ((rankStats[1] < 2) || (rankStats[1] & 1)) return ERROR(corruption_detected); /* by construction : at least 2 elts of rank 1, must be even */
1677 *nbSymbolsPtr = (U32)(oSize+1);
1678 *tableLogPtr = tableLog;
1683 /**************************/
1684 /* single-symbol decoding */
1685 /**************************/
1687 static size_t HUF_readDTableX2 (U16* DTable, const void* src, size_t srcSize)
1689 BYTE huffWeight[HUF_MAX_SYMBOL_VALUE + 1];
1690 U32 rankVal[HUF_ABSOLUTEMAX_TABLELOG + 1]; /* large enough for values from 0 to 16 */
1692 const BYTE* ip = (const BYTE*) src;
1693 size_t iSize = ip[0];
1697 void* ptr = DTable+1;
1698 HUF_DEltX2* const dt = (HUF_DEltX2*)ptr;
1700 HUF_STATIC_ASSERT(sizeof(HUF_DEltX2) == sizeof(U16)); /* if compilation fails here, assertion is false */
1701 //memset(huffWeight, 0, sizeof(huffWeight)); /* is not necessary, even though some analyzer complain ... */
1703 iSize = HUF_readStats(huffWeight, HUF_MAX_SYMBOL_VALUE + 1, rankVal, &nbSymbols, &tableLog, src, srcSize);
1704 if (HUF_isError(iSize)) return iSize;
1707 if (tableLog > DTable[0]) return ERROR(tableLog_tooLarge); /* DTable is too small */
1708 DTable[0] = (U16)tableLog; /* maybe should separate sizeof DTable, as allocated, from used size of DTable, in case of DTable re-use */
1712 for (n=1; n<=tableLog; n++)
1714 U32 current = nextRankStart;
1715 nextRankStart += (rankVal[n] << (n-1));
1716 rankVal[n] = current;
1720 for (n=0; n<nbSymbols; n++)
1722 const U32 w = huffWeight[n];
1723 const U32 length = (1 << w) >> 1;
1726 D.byte = (BYTE)n; D.nbBits = (BYTE)(tableLog + 1 - w);
1727 for (i = rankVal[w]; i < rankVal[w] + length; i++)
1729 rankVal[w] += length;
1735 static BYTE HUF_decodeSymbolX2(BIT_DStream_t* Dstream, const HUF_DEltX2* dt, const U32 dtLog)
1737 const size_t val = BIT_lookBitsFast(Dstream, dtLog); /* note : dtLog >= 1 */
1738 const BYTE c = dt[val].byte;
1739 BIT_skipBits(Dstream, dt[val].nbBits);
1743 #define HUF_DECODE_SYMBOLX2_0(ptr, DStreamPtr) \
1744 *ptr++ = HUF_decodeSymbolX2(DStreamPtr, dt, dtLog)
1746 #define HUF_DECODE_SYMBOLX2_1(ptr, DStreamPtr) \
1747 if (MEM_64bits() || (HUF_MAX_TABLELOG<=12)) \
1748 HUF_DECODE_SYMBOLX2_0(ptr, DStreamPtr)
1750 #define HUF_DECODE_SYMBOLX2_2(ptr, DStreamPtr) \
1752 HUF_DECODE_SYMBOLX2_0(ptr, DStreamPtr)
1754 static inline size_t HUF_decodeStreamX2(BYTE* p, BIT_DStream_t* const bitDPtr, BYTE* const pEnd, const HUF_DEltX2* const dt, const U32 dtLog)
1756 BYTE* const pStart = p;
1758 /* up to 4 symbols at a time */
1759 while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) && (p <= pEnd-4))
1761 HUF_DECODE_SYMBOLX2_2(p, bitDPtr);
1762 HUF_DECODE_SYMBOLX2_1(p, bitDPtr);
1763 HUF_DECODE_SYMBOLX2_2(p, bitDPtr);
1764 HUF_DECODE_SYMBOLX2_0(p, bitDPtr);
1767 /* closer to the end */
1768 while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) && (p < pEnd))
1769 HUF_DECODE_SYMBOLX2_0(p, bitDPtr);
1771 /* no more data to retrieve from bitstream, hence no need to reload */
1773 HUF_DECODE_SYMBOLX2_0(p, bitDPtr);
1779 static size_t HUF_decompress4X2_usingDTable(
1780 void* dst, size_t dstSize,
1781 const void* cSrc, size_t cSrcSize,
1784 if (cSrcSize < 10) return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */
1787 const BYTE* const istart = (const BYTE*) cSrc;
1788 BYTE* const ostart = (BYTE*) dst;
1789 BYTE* const oend = ostart + dstSize;
1791 const void* ptr = DTable;
1792 const HUF_DEltX2* const dt = ((const HUF_DEltX2*)ptr) +1;
1793 const U32 dtLog = DTable[0];
1797 BIT_DStream_t bitD1;
1798 BIT_DStream_t bitD2;
1799 BIT_DStream_t bitD3;
1800 BIT_DStream_t bitD4;
1801 const size_t length1 = MEM_readLE16(istart);
1802 const size_t length2 = MEM_readLE16(istart+2);
1803 const size_t length3 = MEM_readLE16(istart+4);
1805 const BYTE* const istart1 = istart + 6; /* jumpTable */
1806 const BYTE* const istart2 = istart1 + length1;
1807 const BYTE* const istart3 = istart2 + length2;
1808 const BYTE* const istart4 = istart3 + length3;
1809 const size_t segmentSize = (dstSize+3) / 4;
1810 BYTE* const opStart2 = ostart + segmentSize;
1811 BYTE* const opStart3 = opStart2 + segmentSize;
1812 BYTE* const opStart4 = opStart3 + segmentSize;
1814 BYTE* op2 = opStart2;
1815 BYTE* op3 = opStart3;
1816 BYTE* op4 = opStart4;
1819 length4 = cSrcSize - (length1 + length2 + length3 + 6);
1820 if (length4 > cSrcSize) return ERROR(corruption_detected); /* overflow */
1821 errorCode = BIT_initDStream(&bitD1, istart1, length1);
1822 if (HUF_isError(errorCode)) return errorCode;
1823 errorCode = BIT_initDStream(&bitD2, istart2, length2);
1824 if (HUF_isError(errorCode)) return errorCode;
1825 errorCode = BIT_initDStream(&bitD3, istart3, length3);
1826 if (HUF_isError(errorCode)) return errorCode;
1827 errorCode = BIT_initDStream(&bitD4, istart4, length4);
1828 if (HUF_isError(errorCode)) return errorCode;
1830 /* 16-32 symbols per loop (4-8 symbols per stream) */
1831 endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
1832 for ( ; (endSignal==BIT_DStream_unfinished) && (op4<(oend-7)) ; )
1834 HUF_DECODE_SYMBOLX2_2(op1, &bitD1);
1835 HUF_DECODE_SYMBOLX2_2(op2, &bitD2);
1836 HUF_DECODE_SYMBOLX2_2(op3, &bitD3);
1837 HUF_DECODE_SYMBOLX2_2(op4, &bitD4);
1838 HUF_DECODE_SYMBOLX2_1(op1, &bitD1);
1839 HUF_DECODE_SYMBOLX2_1(op2, &bitD2);
1840 HUF_DECODE_SYMBOLX2_1(op3, &bitD3);
1841 HUF_DECODE_SYMBOLX2_1(op4, &bitD4);
1842 HUF_DECODE_SYMBOLX2_2(op1, &bitD1);
1843 HUF_DECODE_SYMBOLX2_2(op2, &bitD2);
1844 HUF_DECODE_SYMBOLX2_2(op3, &bitD3);
1845 HUF_DECODE_SYMBOLX2_2(op4, &bitD4);
1846 HUF_DECODE_SYMBOLX2_0(op1, &bitD1);
1847 HUF_DECODE_SYMBOLX2_0(op2, &bitD2);
1848 HUF_DECODE_SYMBOLX2_0(op3, &bitD3);
1849 HUF_DECODE_SYMBOLX2_0(op4, &bitD4);
1851 endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
1854 /* check corruption */
1855 if (op1 > opStart2) return ERROR(corruption_detected);
1856 if (op2 > opStart3) return ERROR(corruption_detected);
1857 if (op3 > opStart4) return ERROR(corruption_detected);
1858 /* note : op4 supposed already verified within main loop */
1860 /* finish bitStreams one by one */
1861 HUF_decodeStreamX2(op1, &bitD1, opStart2, dt, dtLog);
1862 HUF_decodeStreamX2(op2, &bitD2, opStart3, dt, dtLog);
1863 HUF_decodeStreamX2(op3, &bitD3, opStart4, dt, dtLog);
1864 HUF_decodeStreamX2(op4, &bitD4, oend, dt, dtLog);
1867 endSignal = BIT_endOfDStream(&bitD1) & BIT_endOfDStream(&bitD2) & BIT_endOfDStream(&bitD3) & BIT_endOfDStream(&bitD4);
1868 if (!endSignal) return ERROR(corruption_detected);
1876 static size_t HUF_decompress4X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
1878 HUF_CREATE_STATIC_DTABLEX2(DTable, HUF_MAX_TABLELOG);
1879 const BYTE* ip = (const BYTE*) cSrc;
1882 errorCode = HUF_readDTableX2 (DTable, cSrc, cSrcSize);
1883 if (HUF_isError(errorCode)) return errorCode;
1884 if (errorCode >= cSrcSize) return ERROR(srcSize_wrong);
1886 cSrcSize -= errorCode;
1888 return HUF_decompress4X2_usingDTable (dst, dstSize, ip, cSrcSize, DTable);
1892 /***************************/
1893 /* double-symbols decoding */
1894 /***************************/
1896 static void HUF_fillDTableX4Level2(HUF_DEltX4* DTable, U32 sizeLog, const U32 consumed,
1897 const U32* rankValOrigin, const int minWeight,
1898 const sortedSymbol_t* sortedSymbols, const U32 sortedListSize,
1899 U32 nbBitsBaseline, U16 baseSeq)
1902 U32 rankVal[HUF_ABSOLUTEMAX_TABLELOG + 1];
1905 /* get pre-calculated rankVal */
1906 memcpy(rankVal, rankValOrigin, sizeof(rankVal));
1908 /* fill skipped values */
1911 U32 i, skipSize = rankVal[minWeight];
1912 MEM_writeLE16(&(DElt.sequence), baseSeq);
1913 DElt.nbBits = (BYTE)(consumed);
1915 for (i = 0; i < skipSize; i++)
1920 for (s=0; s<sortedListSize; s++) /* note : sortedSymbols already skipped */
1922 const U32 symbol = sortedSymbols[s].symbol;
1923 const U32 weight = sortedSymbols[s].weight;
1924 const U32 nbBits = nbBitsBaseline - weight;
1925 const U32 length = 1 << (sizeLog-nbBits);
1926 const U32 start = rankVal[weight];
1928 const U32 end = start + length;
1930 MEM_writeLE16(&(DElt.sequence), (U16)(baseSeq + (symbol << 8)));
1931 DElt.nbBits = (BYTE)(nbBits + consumed);
1933 do { DTable[i++] = DElt; } while (i<end); /* since length >= 1 */
1935 rankVal[weight] += length;
1939 typedef U32 rankVal_t[HUF_ABSOLUTEMAX_TABLELOG][HUF_ABSOLUTEMAX_TABLELOG + 1];
1941 static void HUF_fillDTableX4(HUF_DEltX4* DTable, const U32 targetLog,
1942 const sortedSymbol_t* sortedList, const U32 sortedListSize,
1943 const U32* rankStart, rankVal_t rankValOrigin, const U32 maxWeight,
1944 const U32 nbBitsBaseline)
1946 U32 rankVal[HUF_ABSOLUTEMAX_TABLELOG + 1];
1947 const int scaleLog = nbBitsBaseline - targetLog; /* note : targetLog >= srcLog, hence scaleLog <= 1 */
1948 const U32 minBits = nbBitsBaseline - maxWeight;
1951 memcpy(rankVal, rankValOrigin, sizeof(rankVal));
1954 for (s=0; s<sortedListSize; s++)
1956 const U16 symbol = sortedList[s].symbol;
1957 const U32 weight = sortedList[s].weight;
1958 const U32 nbBits = nbBitsBaseline - weight;
1959 const U32 start = rankVal[weight];
1960 const U32 length = 1 << (targetLog-nbBits);
1962 if (targetLog-nbBits >= minBits) /* enough room for a second symbol */
1965 int minWeight = nbBits + scaleLog;
1966 if (minWeight < 1) minWeight = 1;
1967 sortedRank = rankStart[minWeight];
1968 HUF_fillDTableX4Level2(DTable+start, targetLog-nbBits, nbBits,
1969 rankValOrigin[nbBits], minWeight,
1970 sortedList+sortedRank, sortedListSize-sortedRank,
1971 nbBitsBaseline, symbol);
1976 const U32 end = start + length;
1979 MEM_writeLE16(&(DElt.sequence), symbol);
1980 DElt.nbBits = (BYTE)(nbBits);
1982 for (i = start; i < end; i++)
1985 rankVal[weight] += length;
1989 static size_t HUF_readDTableX4 (U32* DTable, const void* src, size_t srcSize)
1991 BYTE weightList[HUF_MAX_SYMBOL_VALUE + 1];
1992 sortedSymbol_t sortedSymbol[HUF_MAX_SYMBOL_VALUE + 1];
1993 U32 rankStats[HUF_ABSOLUTEMAX_TABLELOG + 1] = { 0 };
1994 U32 rankStart0[HUF_ABSOLUTEMAX_TABLELOG + 2] = { 0 };
1995 U32* const rankStart = rankStart0+1;
1997 U32 tableLog, maxW, sizeOfSort, nbSymbols;
1998 const U32 memLog = DTable[0];
1999 const BYTE* ip = (const BYTE*) src;
2000 size_t iSize = ip[0];
2002 HUF_DEltX4* const dt = ((HUF_DEltX4*)ptr) + 1;
2004 HUF_STATIC_ASSERT(sizeof(HUF_DEltX4) == sizeof(U32)); /* if compilation fails here, assertion is false */
2005 if (memLog > HUF_ABSOLUTEMAX_TABLELOG) return ERROR(tableLog_tooLarge);
2006 //memset(weightList, 0, sizeof(weightList)); /* is not necessary, even though some analyzer complain ... */
2008 iSize = HUF_readStats(weightList, HUF_MAX_SYMBOL_VALUE + 1, rankStats, &nbSymbols, &tableLog, src, srcSize);
2009 if (HUF_isError(iSize)) return iSize;
2012 if (tableLog > memLog) return ERROR(tableLog_tooLarge); /* DTable can't fit code depth */
2014 /* find maxWeight */
2015 for (maxW = tableLog; rankStats[maxW]==0; maxW--)
2016 {if (!maxW) return ERROR(GENERIC); } /* necessarily finds a solution before maxW==0 */
2018 /* Get start index of each weight */
2020 U32 w, nextRankStart = 0;
2021 for (w=1; w<=maxW; w++)
2023 U32 current = nextRankStart;
2024 nextRankStart += rankStats[w];
2025 rankStart[w] = current;
2027 rankStart[0] = nextRankStart; /* put all 0w symbols at the end of sorted list*/
2028 sizeOfSort = nextRankStart;
2031 /* sort symbols by weight */
2034 for (s=0; s<nbSymbols; s++)
2036 U32 w = weightList[s];
2037 U32 r = rankStart[w]++;
2038 sortedSymbol[r].symbol = (BYTE)s;
2039 sortedSymbol[r].weight = (BYTE)w;
2041 rankStart[0] = 0; /* forget 0w symbols; this is beginning of weight(1) */
2046 const U32 minBits = tableLog+1 - maxW;
2047 U32 nextRankVal = 0;
2049 const int rescale = (memLog-tableLog) - 1; /* tableLog <= memLog */
2050 U32* rankVal0 = rankVal[0];
2051 for (w=1; w<=maxW; w++)
2053 U32 current = nextRankVal;
2054 nextRankVal += rankStats[w] << (w+rescale);
2055 rankVal0[w] = current;
2057 for (consumed = minBits; consumed <= memLog - minBits; consumed++)
2059 U32* rankValPtr = rankVal[consumed];
2060 for (w = 1; w <= maxW; w++)
2062 rankValPtr[w] = rankVal0[w] >> consumed;
2067 HUF_fillDTableX4(dt, memLog,
2068 sortedSymbol, sizeOfSort,
2069 rankStart0, rankVal, maxW,
2076 static U32 HUF_decodeSymbolX4(void* op, BIT_DStream_t* DStream, const HUF_DEltX4* dt, const U32 dtLog)
2078 const size_t val = BIT_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */
2079 memcpy(op, dt+val, 2);
2080 BIT_skipBits(DStream, dt[val].nbBits);
2081 return dt[val].length;
2084 static U32 HUF_decodeLastSymbolX4(void* op, BIT_DStream_t* DStream, const HUF_DEltX4* dt, const U32 dtLog)
2086 const size_t val = BIT_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */
2087 memcpy(op, dt+val, 1);
2088 if (dt[val].length==1) BIT_skipBits(DStream, dt[val].nbBits);
2091 if (DStream->bitsConsumed < (sizeof(DStream->bitContainer)*8))
2093 BIT_skipBits(DStream, dt[val].nbBits);
2094 if (DStream->bitsConsumed > (sizeof(DStream->bitContainer)*8))
2095 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 */
2102 #define HUF_DECODE_SYMBOLX4_0(ptr, DStreamPtr) \
2103 ptr += HUF_decodeSymbolX4(ptr, DStreamPtr, dt, dtLog)
2105 #define HUF_DECODE_SYMBOLX4_1(ptr, DStreamPtr) \
2106 if (MEM_64bits() || (HUF_MAX_TABLELOG<=12)) \
2107 ptr += HUF_decodeSymbolX4(ptr, DStreamPtr, dt, dtLog)
2109 #define HUF_DECODE_SYMBOLX4_2(ptr, DStreamPtr) \
2111 ptr += HUF_decodeSymbolX4(ptr, DStreamPtr, dt, dtLog)
2113 static inline size_t HUF_decodeStreamX4(BYTE* p, BIT_DStream_t* bitDPtr, BYTE* const pEnd, const HUF_DEltX4* const dt, const U32 dtLog)
2115 BYTE* const pStart = p;
2117 /* up to 8 symbols at a time */
2118 while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) && (p < pEnd-7))
2120 HUF_DECODE_SYMBOLX4_2(p, bitDPtr);
2121 HUF_DECODE_SYMBOLX4_1(p, bitDPtr);
2122 HUF_DECODE_SYMBOLX4_2(p, bitDPtr);
2123 HUF_DECODE_SYMBOLX4_0(p, bitDPtr);
2126 /* closer to the end */
2127 while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) && (p <= pEnd-2))
2128 HUF_DECODE_SYMBOLX4_0(p, bitDPtr);
2131 HUF_DECODE_SYMBOLX4_0(p, bitDPtr); /* no need to reload : reached the end of DStream */
2134 p += HUF_decodeLastSymbolX4(p, bitDPtr, dt, dtLog);
2141 static size_t HUF_decompress4X4_usingDTable(
2142 void* dst, size_t dstSize,
2143 const void* cSrc, size_t cSrcSize,
2146 if (cSrcSize < 10) return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */
2149 const BYTE* const istart = (const BYTE*) cSrc;
2150 BYTE* const ostart = (BYTE*) dst;
2151 BYTE* const oend = ostart + dstSize;
2153 const void* ptr = DTable;
2154 const HUF_DEltX4* const dt = ((const HUF_DEltX4*)ptr) +1;
2155 const U32 dtLog = DTable[0];
2159 BIT_DStream_t bitD1;
2160 BIT_DStream_t bitD2;
2161 BIT_DStream_t bitD3;
2162 BIT_DStream_t bitD4;
2163 const size_t length1 = MEM_readLE16(istart);
2164 const size_t length2 = MEM_readLE16(istart+2);
2165 const size_t length3 = MEM_readLE16(istart+4);
2167 const BYTE* const istart1 = istart + 6; /* jumpTable */
2168 const BYTE* const istart2 = istart1 + length1;
2169 const BYTE* const istart3 = istart2 + length2;
2170 const BYTE* const istart4 = istart3 + length3;
2171 const size_t segmentSize = (dstSize+3) / 4;
2172 BYTE* const opStart2 = ostart + segmentSize;
2173 BYTE* const opStart3 = opStart2 + segmentSize;
2174 BYTE* const opStart4 = opStart3 + segmentSize;
2176 BYTE* op2 = opStart2;
2177 BYTE* op3 = opStart3;
2178 BYTE* op4 = opStart4;
2181 length4 = cSrcSize - (length1 + length2 + length3 + 6);
2182 if (length4 > cSrcSize) return ERROR(corruption_detected); /* overflow */
2183 errorCode = BIT_initDStream(&bitD1, istart1, length1);
2184 if (HUF_isError(errorCode)) return errorCode;
2185 errorCode = BIT_initDStream(&bitD2, istart2, length2);
2186 if (HUF_isError(errorCode)) return errorCode;
2187 errorCode = BIT_initDStream(&bitD3, istart3, length3);
2188 if (HUF_isError(errorCode)) return errorCode;
2189 errorCode = BIT_initDStream(&bitD4, istart4, length4);
2190 if (HUF_isError(errorCode)) return errorCode;
2192 /* 16-32 symbols per loop (4-8 symbols per stream) */
2193 endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
2194 for ( ; (endSignal==BIT_DStream_unfinished) && (op4<(oend-7)) ; )
2196 HUF_DECODE_SYMBOLX4_2(op1, &bitD1);
2197 HUF_DECODE_SYMBOLX4_2(op2, &bitD2);
2198 HUF_DECODE_SYMBOLX4_2(op3, &bitD3);
2199 HUF_DECODE_SYMBOLX4_2(op4, &bitD4);
2200 HUF_DECODE_SYMBOLX4_1(op1, &bitD1);
2201 HUF_DECODE_SYMBOLX4_1(op2, &bitD2);
2202 HUF_DECODE_SYMBOLX4_1(op3, &bitD3);
2203 HUF_DECODE_SYMBOLX4_1(op4, &bitD4);
2204 HUF_DECODE_SYMBOLX4_2(op1, &bitD1);
2205 HUF_DECODE_SYMBOLX4_2(op2, &bitD2);
2206 HUF_DECODE_SYMBOLX4_2(op3, &bitD3);
2207 HUF_DECODE_SYMBOLX4_2(op4, &bitD4);
2208 HUF_DECODE_SYMBOLX4_0(op1, &bitD1);
2209 HUF_DECODE_SYMBOLX4_0(op2, &bitD2);
2210 HUF_DECODE_SYMBOLX4_0(op3, &bitD3);
2211 HUF_DECODE_SYMBOLX4_0(op4, &bitD4);
2213 endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
2216 /* check corruption */
2217 if (op1 > opStart2) return ERROR(corruption_detected);
2218 if (op2 > opStart3) return ERROR(corruption_detected);
2219 if (op3 > opStart4) return ERROR(corruption_detected);
2220 /* note : op4 supposed already verified within main loop */
2222 /* finish bitStreams one by one */
2223 HUF_decodeStreamX4(op1, &bitD1, opStart2, dt, dtLog);
2224 HUF_decodeStreamX4(op2, &bitD2, opStart3, dt, dtLog);
2225 HUF_decodeStreamX4(op3, &bitD3, opStart4, dt, dtLog);
2226 HUF_decodeStreamX4(op4, &bitD4, oend, dt, dtLog);
2229 endSignal = BIT_endOfDStream(&bitD1) & BIT_endOfDStream(&bitD2) & BIT_endOfDStream(&bitD3) & BIT_endOfDStream(&bitD4);
2230 if (!endSignal) return ERROR(corruption_detected);
2238 static size_t HUF_decompress4X4 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
2240 HUF_CREATE_STATIC_DTABLEX4(DTable, HUF_MAX_TABLELOG);
2241 const BYTE* ip = (const BYTE*) cSrc;
2243 size_t hSize = HUF_readDTableX4 (DTable, cSrc, cSrcSize);
2244 if (HUF_isError(hSize)) return hSize;
2245 if (hSize >= cSrcSize) return ERROR(srcSize_wrong);
2249 return HUF_decompress4X4_usingDTable (dst, dstSize, ip, cSrcSize, DTable);
2253 /**********************************/
2254 /* quad-symbol decoding */
2255 /**********************************/
2256 typedef struct { BYTE nbBits; BYTE nbBytes; } HUF_DDescX6;
2257 typedef union { BYTE byte[4]; U32 sequence; } HUF_DSeqX6;
2259 /* recursive, up to level 3; may benefit from <template>-like strategy to nest each level inline */
2260 static void HUF_fillDTableX6LevelN(HUF_DDescX6* DDescription, HUF_DSeqX6* DSequence, int sizeLog,
2261 const rankVal_t rankValOrigin, const U32 consumed, const int minWeight, const U32 maxWeight,
2262 const sortedSymbol_t* sortedSymbols, const U32 sortedListSize, const U32* rankStart,
2263 const U32 nbBitsBaseline, HUF_DSeqX6 baseSeq, HUF_DDescX6 DDesc)
2265 const int scaleLog = nbBitsBaseline - sizeLog; /* note : targetLog >= (nbBitsBaseline-1), hence scaleLog <= 1 */
2266 const int minBits = nbBitsBaseline - maxWeight;
2267 const U32 level = DDesc.nbBytes;
2268 U32 rankVal[HUF_ABSOLUTEMAX_TABLELOG + 1];
2269 U32 symbolStartPos, s;
2271 /* local rankVal, will be modified */
2272 memcpy(rankVal, rankValOrigin[consumed], sizeof(rankVal));
2274 /* fill skipped values */
2278 const U32 skipSize = rankVal[minWeight];
2279 for (i = 0; i < skipSize; i++)
2281 DSequence[i] = baseSeq;
2282 DDescription[i] = DDesc;
2288 symbolStartPos = rankStart[minWeight];
2289 for (s=symbolStartPos; s<sortedListSize; s++)
2291 const BYTE symbol = sortedSymbols[s].symbol;
2292 const U32 weight = sortedSymbols[s].weight; /* >= 1 (sorted) */
2293 const int nbBits = nbBitsBaseline - weight; /* >= 1 (by construction) */
2294 const int totalBits = consumed+nbBits;
2295 const U32 start = rankVal[weight];
2296 const U32 length = 1 << (sizeLog-nbBits);
2297 baseSeq.byte[level] = symbol;
2298 DDesc.nbBits = (BYTE)totalBits;
2300 if ((level<3) && (sizeLog-totalBits >= minBits)) /* enough room for another symbol */
2302 int nextMinWeight = totalBits + scaleLog;
2303 if (nextMinWeight < 1) nextMinWeight = 1;
2304 HUF_fillDTableX6LevelN(DDescription+start, DSequence+start, sizeLog-nbBits,
2305 rankValOrigin, totalBits, nextMinWeight, maxWeight,
2306 sortedSymbols, sortedListSize, rankStart,
2307 nbBitsBaseline, baseSeq, DDesc); /* recursive (max : level 3) */
2312 const U32 end = start + length;
2313 for (i = start; i < end; i++)
2315 DDescription[i] = DDesc;
2316 DSequence[i] = baseSeq;
2319 rankVal[weight] += length;
2324 /* note : same preparation as X4 */
2325 static size_t HUF_readDTableX6 (U32* DTable, const void* src, size_t srcSize)
2327 BYTE weightList[HUF_MAX_SYMBOL_VALUE + 1];
2328 sortedSymbol_t sortedSymbol[HUF_MAX_SYMBOL_VALUE + 1];
2329 U32 rankStats[HUF_ABSOLUTEMAX_TABLELOG + 1] = { 0 };
2330 U32 rankStart0[HUF_ABSOLUTEMAX_TABLELOG + 2] = { 0 };
2331 U32* const rankStart = rankStart0+1;
2332 U32 tableLog, maxW, sizeOfSort, nbSymbols;
2334 const U32 memLog = DTable[0];
2335 const BYTE* ip = (const BYTE*) src;
2336 size_t iSize = ip[0];
2338 if (memLog > HUF_ABSOLUTEMAX_TABLELOG) return ERROR(tableLog_tooLarge);
2339 //memset(weightList, 0, sizeof(weightList)); /* is not necessary, even though some analyzer complain ... */
2341 iSize = HUF_readStats(weightList, HUF_MAX_SYMBOL_VALUE + 1, rankStats, &nbSymbols, &tableLog, src, srcSize);
2342 if (HUF_isError(iSize)) return iSize;
2345 if (tableLog > memLog) return ERROR(tableLog_tooLarge); /* DTable is too small */
2347 /* find maxWeight */
2348 for (maxW = tableLog; rankStats[maxW]==0; maxW--)
2349 { if (!maxW) return ERROR(GENERIC); } /* necessarily finds a solution before maxW==0 */
2352 /* Get start index of each weight */
2354 U32 w, nextRankStart = 0;
2355 for (w=1; w<=maxW; w++)
2357 U32 current = nextRankStart;
2358 nextRankStart += rankStats[w];
2359 rankStart[w] = current;
2361 rankStart[0] = nextRankStart; /* put all 0w symbols at the end of sorted list*/
2362 sizeOfSort = nextRankStart;
2365 /* sort symbols by weight */
2368 for (s=0; s<nbSymbols; s++)
2370 U32 w = weightList[s];
2371 U32 r = rankStart[w]++;
2372 sortedSymbol[r].symbol = (BYTE)s;
2373 sortedSymbol[r].weight = (BYTE)w;
2375 rankStart[0] = 0; /* forget 0w symbols; this is beginning of weight(1) */
2380 const U32 minBits = tableLog+1 - maxW;
2381 U32 nextRankVal = 0;
2383 const int rescale = (memLog-tableLog) - 1; /* tableLog <= memLog */
2384 U32* rankVal0 = rankVal[0];
2385 for (w=1; w<=maxW; w++)
2387 U32 current = nextRankVal;
2388 nextRankVal += rankStats[w] << (w+rescale);
2389 rankVal0[w] = current;
2391 for (consumed = minBits; consumed <= memLog - minBits; consumed++)
2393 U32* rankValPtr = rankVal[consumed];
2394 for (w = 1; w <= maxW; w++)
2396 rankValPtr[w] = rankVal0[w] >> consumed;
2404 void* ptr = DTable+1;
2405 HUF_DDescX6* DDescription = (HUF_DDescX6*)(ptr);
2406 void* dSeqStart = DTable + 1 + ((size_t)1<<(memLog-1));
2407 HUF_DSeqX6* DSequence = (HUF_DSeqX6*)(dSeqStart);
2413 HUF_fillDTableX6LevelN(DDescription, DSequence, memLog,
2414 (const U32 (*)[HUF_ABSOLUTEMAX_TABLELOG + 1])rankVal, 0, 1, maxW,
2415 sortedSymbol, sizeOfSort, rankStart0,
2416 tableLog+1, DSeq, DDesc);
2423 static U32 HUF_decodeSymbolX6(void* op, BIT_DStream_t* DStream, const HUF_DDescX6* dd, const HUF_DSeqX6* ds, const U32 dtLog)
2425 const size_t val = BIT_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */
2426 memcpy(op, ds+val, sizeof(HUF_DSeqX6));
2427 BIT_skipBits(DStream, dd[val].nbBits);
2428 return dd[val].nbBytes;
2431 static U32 HUF_decodeLastSymbolsX6(void* op, const U32 maxL, BIT_DStream_t* DStream,
2432 const HUF_DDescX6* dd, const HUF_DSeqX6* ds, const U32 dtLog)
2434 const size_t val = BIT_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */
2435 U32 length = dd[val].nbBytes;
2438 memcpy(op, ds+val, length);
2439 BIT_skipBits(DStream, dd[val].nbBits);
2442 memcpy(op, ds+val, maxL);
2443 if (DStream->bitsConsumed < (sizeof(DStream->bitContainer)*8))
2445 BIT_skipBits(DStream, dd[val].nbBits);
2446 if (DStream->bitsConsumed > (sizeof(DStream->bitContainer)*8))
2447 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 */
2453 #define HUF_DECODE_SYMBOLX6_0(ptr, DStreamPtr) \
2454 ptr += HUF_decodeSymbolX6(ptr, DStreamPtr, dd, ds, dtLog)
2456 #define HUF_DECODE_SYMBOLX6_1(ptr, DStreamPtr) \
2457 if (MEM_64bits() || (HUF_MAX_TABLELOG<=12)) \
2458 HUF_DECODE_SYMBOLX6_0(ptr, DStreamPtr)
2460 #define HUF_DECODE_SYMBOLX6_2(ptr, DStreamPtr) \
2462 HUF_DECODE_SYMBOLX6_0(ptr, DStreamPtr)
2464 static inline size_t HUF_decodeStreamX6(BYTE* p, BIT_DStream_t* bitDPtr, BYTE* const pEnd, const U32* DTable, const U32 dtLog)
2466 const void* ddPtr = DTable+1;
2467 const HUF_DDescX6* dd = (const HUF_DDescX6*)(ddPtr);
2468 const void* dsPtr = DTable + 1 + ((size_t)1<<(dtLog-1));
2469 const HUF_DSeqX6* ds = (const HUF_DSeqX6*)(dsPtr);
2470 BYTE* const pStart = p;
2472 /* up to 16 symbols at a time */
2473 while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) && (p <= pEnd-16))
2475 HUF_DECODE_SYMBOLX6_2(p, bitDPtr);
2476 HUF_DECODE_SYMBOLX6_1(p, bitDPtr);
2477 HUF_DECODE_SYMBOLX6_2(p, bitDPtr);
2478 HUF_DECODE_SYMBOLX6_0(p, bitDPtr);
2481 /* closer to the end, up to 4 symbols at a time */
2482 while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) && (p <= pEnd-4))
2483 HUF_DECODE_SYMBOLX6_0(p, bitDPtr);
2486 HUF_DECODE_SYMBOLX6_0(p, bitDPtr); /* no need to reload : reached the end of DStream */
2489 p += HUF_decodeLastSymbolsX6(p, (U32)(pEnd-p), bitDPtr, dd, ds, dtLog);
2496 static size_t HUF_decompress4X6_usingDTable(
2497 void* dst, size_t dstSize,
2498 const void* cSrc, size_t cSrcSize,
2501 if (cSrcSize < 10) return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */
2504 const BYTE* const istart = (const BYTE*) cSrc;
2505 BYTE* const ostart = (BYTE*) dst;
2506 BYTE* const oend = ostart + dstSize;
2508 const U32 dtLog = DTable[0];
2509 const void* ddPtr = DTable+1;
2510 const HUF_DDescX6* dd = (const HUF_DDescX6*)(ddPtr);
2511 const void* dsPtr = DTable + 1 + ((size_t)1<<(dtLog-1));
2512 const HUF_DSeqX6* ds = (const HUF_DSeqX6*)(dsPtr);
2516 BIT_DStream_t bitD1;
2517 BIT_DStream_t bitD2;
2518 BIT_DStream_t bitD3;
2519 BIT_DStream_t bitD4;
2520 const size_t length1 = MEM_readLE16(istart);
2521 const size_t length2 = MEM_readLE16(istart+2);
2522 const size_t length3 = MEM_readLE16(istart+4);
2524 const BYTE* const istart1 = istart + 6; /* jumpTable */
2525 const BYTE* const istart2 = istart1 + length1;
2526 const BYTE* const istart3 = istart2 + length2;
2527 const BYTE* const istart4 = istart3 + length3;
2528 const size_t segmentSize = (dstSize+3) / 4;
2529 BYTE* const opStart2 = ostart + segmentSize;
2530 BYTE* const opStart3 = opStart2 + segmentSize;
2531 BYTE* const opStart4 = opStart3 + segmentSize;
2533 BYTE* op2 = opStart2;
2534 BYTE* op3 = opStart3;
2535 BYTE* op4 = opStart4;
2538 length4 = cSrcSize - (length1 + length2 + length3 + 6);
2539 if (length4 > cSrcSize) return ERROR(corruption_detected); /* overflow */
2540 errorCode = BIT_initDStream(&bitD1, istart1, length1);
2541 if (HUF_isError(errorCode)) return errorCode;
2542 errorCode = BIT_initDStream(&bitD2, istart2, length2);
2543 if (HUF_isError(errorCode)) return errorCode;
2544 errorCode = BIT_initDStream(&bitD3, istart3, length3);
2545 if (HUF_isError(errorCode)) return errorCode;
2546 errorCode = BIT_initDStream(&bitD4, istart4, length4);
2547 if (HUF_isError(errorCode)) return errorCode;
2549 /* 16-64 symbols per loop (4-16 symbols per stream) */
2550 endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
2551 for ( ; (op3 <= opStart4) && (endSignal==BIT_DStream_unfinished) && (op4<=(oend-16)) ; )
2553 HUF_DECODE_SYMBOLX6_2(op1, &bitD1);
2554 HUF_DECODE_SYMBOLX6_2(op2, &bitD2);
2555 HUF_DECODE_SYMBOLX6_2(op3, &bitD3);
2556 HUF_DECODE_SYMBOLX6_2(op4, &bitD4);
2557 HUF_DECODE_SYMBOLX6_1(op1, &bitD1);
2558 HUF_DECODE_SYMBOLX6_1(op2, &bitD2);
2559 HUF_DECODE_SYMBOLX6_1(op3, &bitD3);
2560 HUF_DECODE_SYMBOLX6_1(op4, &bitD4);
2561 HUF_DECODE_SYMBOLX6_2(op1, &bitD1);
2562 HUF_DECODE_SYMBOLX6_2(op2, &bitD2);
2563 HUF_DECODE_SYMBOLX6_2(op3, &bitD3);
2564 HUF_DECODE_SYMBOLX6_2(op4, &bitD4);
2565 HUF_DECODE_SYMBOLX6_0(op1, &bitD1);
2566 HUF_DECODE_SYMBOLX6_0(op2, &bitD2);
2567 HUF_DECODE_SYMBOLX6_0(op3, &bitD3);
2568 HUF_DECODE_SYMBOLX6_0(op4, &bitD4);
2570 endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
2573 /* check corruption */
2574 if (op1 > opStart2) return ERROR(corruption_detected);
2575 if (op2 > opStart3) return ERROR(corruption_detected);
2576 if (op3 > opStart4) return ERROR(corruption_detected);
2577 /* note : op4 supposed already verified within main loop */
2579 /* finish bitStreams one by one */
2580 HUF_decodeStreamX6(op1, &bitD1, opStart2, DTable, dtLog);
2581 HUF_decodeStreamX6(op2, &bitD2, opStart3, DTable, dtLog);
2582 HUF_decodeStreamX6(op3, &bitD3, opStart4, DTable, dtLog);
2583 HUF_decodeStreamX6(op4, &bitD4, oend, DTable, dtLog);
2586 endSignal = BIT_endOfDStream(&bitD1) & BIT_endOfDStream(&bitD2) & BIT_endOfDStream(&bitD3) & BIT_endOfDStream(&bitD4);
2587 if (!endSignal) return ERROR(corruption_detected);
2595 static size_t HUF_decompress4X6 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
2597 HUF_CREATE_STATIC_DTABLEX6(DTable, HUF_MAX_TABLELOG);
2598 const BYTE* ip = (const BYTE*) cSrc;
2600 size_t hSize = HUF_readDTableX6 (DTable, cSrc, cSrcSize);
2601 if (HUF_isError(hSize)) return hSize;
2602 if (hSize >= cSrcSize) return ERROR(srcSize_wrong);
2606 return HUF_decompress4X6_usingDTable (dst, dstSize, ip, cSrcSize, DTable);
2610 /**********************************/
2611 /* Generic decompression selector */
2612 /**********************************/
2614 typedef struct { U32 tableTime; U32 decode256Time; } algo_time_t;
2615 static const algo_time_t algoTime[16 /* Quantization */][3 /* single, double, quad */] =
2617 /* single, double, quad */
2618 {{0,0}, {1,1}, {2,2}}, /* Q==0 : impossible */
2619 {{0,0}, {1,1}, {2,2}}, /* Q==1 : impossible */
2620 {{ 38,130}, {1313, 74}, {2151, 38}}, /* Q == 2 : 12-18% */
2621 {{ 448,128}, {1353, 74}, {2238, 41}}, /* Q == 3 : 18-25% */
2622 {{ 556,128}, {1353, 74}, {2238, 47}}, /* Q == 4 : 25-32% */
2623 {{ 714,128}, {1418, 74}, {2436, 53}}, /* Q == 5 : 32-38% */
2624 {{ 883,128}, {1437, 74}, {2464, 61}}, /* Q == 6 : 38-44% */
2625 {{ 897,128}, {1515, 75}, {2622, 68}}, /* Q == 7 : 44-50% */
2626 {{ 926,128}, {1613, 75}, {2730, 75}}, /* Q == 8 : 50-56% */
2627 {{ 947,128}, {1729, 77}, {3359, 77}}, /* Q == 9 : 56-62% */
2628 {{1107,128}, {2083, 81}, {4006, 84}}, /* Q ==10 : 62-69% */
2629 {{1177,128}, {2379, 87}, {4785, 88}}, /* Q ==11 : 69-75% */
2630 {{1242,128}, {2415, 93}, {5155, 84}}, /* Q ==12 : 75-81% */
2631 {{1349,128}, {2644,106}, {5260,106}}, /* Q ==13 : 81-87% */
2632 {{1455,128}, {2422,124}, {4174,124}}, /* Q ==14 : 87-93% */
2633 {{ 722,128}, {1891,145}, {1936,146}}, /* Q ==15 : 93-99% */
2636 typedef size_t (*decompressionAlgo)(void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);
2638 static size_t HUF_decompress (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
2640 static const decompressionAlgo decompress[3] = { HUF_decompress4X2, HUF_decompress4X4, HUF_decompress4X6 };
2641 /* estimate decompression time */
2643 const U32 D256 = (U32)(dstSize >> 8);
2648 /* validation checks */
2649 if (dstSize == 0) return ERROR(dstSize_tooSmall);
2650 if (cSrcSize > dstSize) return ERROR(corruption_detected); /* invalid */
2651 if (cSrcSize == dstSize) { memcpy(dst, cSrc, dstSize); return dstSize; } /* not compressed */
2652 if (cSrcSize == 1) { memset(dst, *(const BYTE*)cSrc, dstSize); return dstSize; } /* RLE */
2654 /* decoder timing evaluation */
2655 Q = (U32)(cSrcSize * 16 / dstSize); /* Q < 16 since dstSize > cSrcSize */
2657 Dtime[n] = algoTime[Q][n].tableTime + (algoTime[Q][n].decode256Time * D256);
2659 Dtime[1] += Dtime[1] >> 4; Dtime[2] += Dtime[2] >> 3; /* advantage to algorithms using less memory, for cache eviction */
2661 if (Dtime[1] < Dtime[0]) algoNb = 1;
2662 if (Dtime[2] < Dtime[algoNb]) algoNb = 2;
2664 return decompress[algoNb](dst, dstSize, cSrc, cSrcSize);
2666 //return HUF_decompress4X2(dst, dstSize, cSrc, cSrcSize); /* multi-streams single-symbol decoding */
2667 //return HUF_decompress4X4(dst, dstSize, cSrc, cSrcSize); /* multi-streams double-symbols decoding */
2668 //return HUF_decompress4X6(dst, dstSize, cSrc, cSrcSize); /* multi-streams quad-symbols decoding */
2671 zstd - standard compression library
2672 Copyright (C) 2014-2015, Yann Collet.
2674 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
2676 Redistribution and use in source and binary forms, with or without
2677 modification, are permitted provided that the following conditions are
2679 * Redistributions of source code must retain the above copyright
2680 notice, this list of conditions and the following disclaimer.
2681 * Redistributions in binary form must reproduce the above
2682 copyright notice, this list of conditions and the following disclaimer
2683 in the documentation and/or other materials provided with the
2685 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2686 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2687 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2688 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2689 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2690 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2691 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2692 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2693 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2694 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2695 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2697 You can contact the author at :
2698 - zstd source repository : https://github.com/Cyan4973/zstd
2699 - ztsd public forum : https://groups.google.com/forum/#!forum/lz4c
2702 /* ***************************************************************
2704 *****************************************************************/
2707 * Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.)
2708 * Increasing memory usage improves compression ratio
2709 * Reduced memory usage can improve speed, due to cache effect
2711 #define ZSTD_MEMORY_USAGE 17
2715 * Select how default compression functions will allocate memory for their hash table,
2716 * in memory stack (0, fastest), or in memory heap (1, requires malloc())
2717 * Note that compression context is fairly large, as a consequence heap memory is recommended.
2719 #ifndef ZSTD_HEAPMODE
2720 # define ZSTD_HEAPMODE 1
2721 #endif /* ZSTD_HEAPMODE */
2725 * decompressor can decode older formats (starting from Zstd 0.1+)
2727 #ifndef ZSTD_LEGACY_SUPPORT
2728 # define ZSTD_LEGACY_SUPPORT 1
2732 /* *******************************************************
2734 *********************************************************/
2735 #include <stdlib.h> /* calloc */
2736 #include <string.h> /* memcpy, memmove */
2737 #include <stdio.h> /* debug : printf */
2740 /* *******************************************************
2741 * Compiler specifics
2742 *********************************************************/
2744 # include <immintrin.h> /* AVX2 intrinsics */
2747 #ifdef _MSC_VER /* Visual Studio */
2748 # include <intrin.h> /* For Visual 2005 */
2749 # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
2750 # pragma warning(disable : 4324) /* disable: C4324: padded structure */
2754 /* *******************************************************
2756 *********************************************************/
2757 #define HASH_LOG (ZSTD_MEMORY_USAGE - 2)
2758 #define HASH_TABLESIZE (1 << HASH_LOG)
2759 #define HASH_MASK (HASH_TABLESIZE - 1)
2761 #define KNUTH 2654435761
2770 #define KB *(1 <<10)
2771 #define MB *(1 <<20)
2772 #define GB *(1U<<30)
2774 #define BLOCKSIZE (128 KB) /* define, for static allocation */
2775 #define MIN_SEQUENCES_SIZE (2 /*seqNb*/ + 2 /*dumps*/ + 3 /*seqTables*/ + 1 /*bitStream*/)
2776 #define MIN_CBLOCK_SIZE (3 /*litCSize*/ + MIN_SEQUENCES_SIZE)
2780 #define WORKPLACESIZE (BLOCKSIZE*3)
2785 #define MaxML ((1<<MLbits )-1)
2786 #define MaxLL ((1<<LLbits )-1)
2788 #define LitFSELog 11
2792 #define MAX(a,b) ((a)<(b)?(b):(a))
2793 #define MaxSeq MAX(MaxLL, MaxML)
2795 #define LITERAL_NOENTROPY 63
2796 #define COMMAND_NOENTROPY 7 /* to remove */
2798 static const size_t ZSTD_blockHeaderSize = 3;
2799 static const size_t ZSTD_frameHeaderSize = 4;
2802 /* *******************************************************
2804 **********************************************************/
2805 static void ZSTD_copy4(void* dst, const void* src) { memcpy(dst, src, 4); }
2807 static void ZSTD_copy8(void* dst, const void* src) { memcpy(dst, src, 8); }
2809 #define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; }
2811 /*! ZSTD_wildcopy : custom version of memcpy(), can copy up to 7-8 bytes too many */
2812 static void ZSTD_wildcopy(void* dst, const void* src, ptrdiff_t length)
2814 const BYTE* ip = (const BYTE*)src;
2815 BYTE* op = (BYTE*)dst;
2816 BYTE* const oend = op + length;
2817 do COPY8(op, ip) while (op < oend);
2821 /* **************************************
2823 ****************************************/
2824 typedef enum { bt_compressed, bt_raw, bt_rle, bt_end } blockType_t;
2828 blockType_t blockType;
2830 } blockProperties_t;
2840 BYTE* litLengthStart;
2842 BYTE* matchLengthStart;
2849 /* *************************************
2851 ***************************************/
2853 * tells if a return value is an error code */
2854 static unsigned ZSTD_isError(size_t code) { return ERR_isError(code); }
2858 /* *************************************************************
2859 * Decompression section
2860 ***************************************************************/
2863 U32 LLTable[FSE_DTABLE_SIZE_U32(LLFSELog)];
2864 U32 OffTable[FSE_DTABLE_SIZE_U32(OffFSELog)];
2865 U32 MLTable[FSE_DTABLE_SIZE_U32(MLFSELog)];
2866 void* previousDstEnd;
2873 BYTE litBuffer[BLOCKSIZE + 8 /* margin for wildcopy */];
2874 }; /* typedef'd to ZSTD_Dctx within "zstd_static.h" */
2877 static size_t ZSTD_getcBlockSize(const void* src, size_t srcSize, blockProperties_t* bpPtr)
2879 const BYTE* const in = (const BYTE* const)src;
2883 if (srcSize < 3) return ERROR(srcSize_wrong);
2886 cSize = in[2] + (in[1]<<8) + ((in[0] & 7)<<16);
2888 bpPtr->blockType = (blockType_t)(headerFlags >> 6);
2889 bpPtr->origSize = (bpPtr->blockType == bt_rle) ? cSize : 0;
2891 if (bpPtr->blockType == bt_end) return 0;
2892 if (bpPtr->blockType == bt_rle) return 1;
2896 static size_t ZSTD_copyUncompressedBlock(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
2898 if (srcSize > maxDstSize) return ERROR(dstSize_tooSmall);
2899 memcpy(dst, src, srcSize);
2904 /** ZSTD_decompressLiterals
2905 @return : nb of bytes read from src, or an error code*/
2906 static size_t ZSTD_decompressLiterals(void* dst, size_t* maxDstSizePtr,
2907 const void* src, size_t srcSize)
2909 const BYTE* ip = (const BYTE*)src;
2911 const size_t litSize = (MEM_readLE32(src) & 0x1FFFFF) >> 2; /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */
2912 const size_t litCSize = (MEM_readLE32(ip+2) & 0xFFFFFF) >> 5; /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */
2914 if (litSize > *maxDstSizePtr) return ERROR(corruption_detected);
2915 if (litCSize + 5 > srcSize) return ERROR(corruption_detected);
2917 if (HUF_isError(HUF_decompress(dst, litSize, ip+5, litCSize))) return ERROR(corruption_detected);
2919 *maxDstSizePtr = litSize;
2920 return litCSize + 5;
2924 /** ZSTD_decodeLiteralsBlock
2925 @return : nb of bytes read from src (< srcSize )*/
2926 static size_t ZSTD_decodeLiteralsBlock(void* ctx,
2927 const void* src, size_t srcSize)
2929 ZSTD_DCtx* dctx = (ZSTD_DCtx*)ctx;
2930 const BYTE* const istart = (const BYTE* const)src;
2932 /* any compressed block with literals segment must be at least this size */
2933 if (srcSize < MIN_CBLOCK_SIZE) return ERROR(corruption_detected);
2940 size_t litSize = BLOCKSIZE;
2941 const size_t readSize = ZSTD_decompressLiterals(dctx->litBuffer, &litSize, src, srcSize);
2942 dctx->litPtr = dctx->litBuffer;
2943 dctx->litSize = litSize;
2944 memset(dctx->litBuffer + dctx->litSize, 0, 8);
2945 return readSize; /* works if it's an error too */
2949 const size_t litSize = (MEM_readLE32(istart) & 0xFFFFFF) >> 2; /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */
2950 if (litSize > srcSize-11) /* risk of reading too far with wildcopy */
2952 if (litSize > srcSize-3) return ERROR(corruption_detected);
2953 memcpy(dctx->litBuffer, istart, litSize);
2954 dctx->litPtr = dctx->litBuffer;
2955 dctx->litSize = litSize;
2956 memset(dctx->litBuffer + dctx->litSize, 0, 8);
2959 /* direct reference into compressed stream */
2960 dctx->litPtr = istart+3;
2961 dctx->litSize = litSize;
2966 const size_t litSize = (MEM_readLE32(istart) & 0xFFFFFF) >> 2; /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */
2967 if (litSize > BLOCKSIZE) return ERROR(corruption_detected);
2968 memset(dctx->litBuffer, istart[3], litSize + 8);
2969 dctx->litPtr = dctx->litBuffer;
2970 dctx->litSize = litSize;
2977 static size_t ZSTD_decodeSeqHeaders(int* nbSeq, const BYTE** dumpsPtr, size_t* dumpsLengthPtr,
2978 FSE_DTable* DTableLL, FSE_DTable* DTableML, FSE_DTable* DTableOffb,
2979 const void* src, size_t srcSize)
2981 const BYTE* const istart = (const BYTE* const)src;
2982 const BYTE* ip = istart;
2983 const BYTE* const iend = istart + srcSize;
2984 U32 LLtype, Offtype, MLtype;
2985 U32 LLlog, Offlog, MLlog;
2989 if (srcSize < 5) return ERROR(srcSize_wrong);
2992 *nbSeq = MEM_readLE16(ip); ip+=2;
2994 Offtype = (*ip >> 4) & 3;
2995 MLtype = (*ip >> 2) & 3;
2998 dumpsLength = ip[2];
2999 dumpsLength += ip[1] << 8;
3004 dumpsLength = ip[1];
3005 dumpsLength += (ip[0] & 1) << 8;
3010 *dumpsLengthPtr = dumpsLength;
3013 if (ip > iend-3) return ERROR(srcSize_wrong); /* min : all 3 are "raw", hence no header, but at least xxLog bits per type */
3017 S16 norm[MaxML+1]; /* assumption : MaxML >= MaxLL and MaxOff */
3025 FSE_buildDTable_rle(DTableLL, *ip++); break;
3028 FSE_buildDTable_raw(DTableLL, LLbits); break;
3031 headerSize = FSE_readNCount(norm, &max, &LLlog, ip, iend-ip);
3032 if (FSE_isError(headerSize)) return ERROR(GENERIC);
3033 if (LLlog > LLFSELog) return ERROR(corruption_detected);
3035 FSE_buildDTable(DTableLL, norm, max, LLlog);
3042 if (ip > iend-2) return ERROR(srcSize_wrong); /* min : "raw", hence no header, but at least xxLog bits */
3043 FSE_buildDTable_rle(DTableOffb, *ip++ & MaxOff); /* if *ip > MaxOff, data is corrupted */
3047 FSE_buildDTable_raw(DTableOffb, Offbits); break;
3050 headerSize = FSE_readNCount(norm, &max, &Offlog, ip, iend-ip);
3051 if (FSE_isError(headerSize)) return ERROR(GENERIC);
3052 if (Offlog > OffFSELog) return ERROR(corruption_detected);
3054 FSE_buildDTable(DTableOffb, norm, max, Offlog);
3061 if (ip > iend-2) return ERROR(srcSize_wrong); /* min : "raw", hence no header, but at least xxLog bits */
3062 FSE_buildDTable_rle(DTableML, *ip++); break;
3065 FSE_buildDTable_raw(DTableML, MLbits); break;
3068 headerSize = FSE_readNCount(norm, &max, &MLlog, ip, iend-ip);
3069 if (FSE_isError(headerSize)) return ERROR(GENERIC);
3070 if (MLlog > MLFSELog) return ERROR(corruption_detected);
3072 FSE_buildDTable(DTableML, norm, max, MLlog);
3086 BIT_DStream_t DStream;
3087 FSE_DState_t stateLL;
3088 FSE_DState_t stateOffb;
3089 FSE_DState_t stateML;
3092 const BYTE* dumpsEnd;
3096 static void ZSTD_decodeSequence(seq_t* seq, seqState_t* seqState)
3102 const BYTE* dumps = seqState->dumps;
3103 const BYTE* const de = seqState->dumpsEnd;
3105 /* Literal length */
3106 litLength = FSE_decodeSymbol(&(seqState->stateLL), &(seqState->DStream));
3107 prevOffset = litLength ? seq->offset : seqState->prevOffset;
3108 seqState->prevOffset = seq->offset;
3109 if (litLength == MaxLL)
3112 if (add < 255) litLength += add;
3115 litLength = MEM_readLE32(dumps) & 0xFFFFFF; /* no pb : dumps is always followed by seq tables > 1 byte */
3118 if (dumps >= de) dumps = de-1; /* late correction, to avoid read overflow (data is now corrupted anyway) */
3123 static const size_t offsetPrefix[MaxOff+1] = { /* note : size_t faster than U32 */
3124 1 /*fake*/, 1, 2, 4, 8, 16, 32, 64, 128, 256,
3125 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144,
3126 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, /*fake*/ 1, 1, 1, 1, 1 };
3127 U32 offsetCode, nbBits;
3128 offsetCode = FSE_decodeSymbol(&(seqState->stateOffb), &(seqState->DStream)); /* <= maxOff, by table construction */
3129 if (MEM_32bits()) BIT_reloadDStream(&(seqState->DStream));
3130 nbBits = offsetCode - 1;
3131 if (offsetCode==0) nbBits = 0; /* cmove */
3132 offset = offsetPrefix[offsetCode] + BIT_readBits(&(seqState->DStream), nbBits);
3133 if (MEM_32bits()) BIT_reloadDStream(&(seqState->DStream));
3134 if (offsetCode==0) offset = prevOffset; /* cmove */
3138 matchLength = FSE_decodeSymbol(&(seqState->stateML), &(seqState->DStream));
3139 if (matchLength == MaxML)
3142 if (add < 255) matchLength += add;
3145 matchLength = MEM_readLE32(dumps) & 0xFFFFFF; /* no pb : dumps is always followed by seq tables > 1 byte */
3148 if (dumps >= de) dumps = de-1; /* late correction, to avoid read overflow (data is now corrupted anyway) */
3150 matchLength += MINMATCH;
3153 seq->litLength = litLength;
3154 seq->offset = offset;
3155 seq->matchLength = matchLength;
3156 seqState->dumps = dumps;
3160 static size_t ZSTD_execSequence(BYTE* op,
3162 const BYTE** litPtr, const BYTE* const litLimit,
3163 BYTE* const base, BYTE* const oend)
3165 static const int dec32table[] = {0, 1, 2, 1, 4, 4, 4, 4}; /* added */
3166 static const int dec64table[] = {8, 8, 8, 7, 8, 9,10,11}; /* substracted */
3167 const BYTE* const ostart = op;
3168 BYTE* const oLitEnd = op + sequence.litLength;
3169 BYTE* const oMatchEnd = op + sequence.litLength + sequence.matchLength; /* risk : address space overflow (32-bits) */
3170 BYTE* const oend_8 = oend-8;
3171 const BYTE* const litEnd = *litPtr + sequence.litLength;
3174 if (oLitEnd > oend_8) return ERROR(dstSize_tooSmall); /* last match must start at a minimum distance of 8 from oend */
3175 if (oMatchEnd > oend) return ERROR(dstSize_tooSmall); /* overwrite beyond dst buffer */
3176 if (litEnd > litLimit) return ERROR(corruption_detected); /* overRead beyond lit buffer */
3179 ZSTD_wildcopy(op, *litPtr, sequence.litLength); /* note : oLitEnd <= oend-8 : no risk of overwrite beyond oend */
3181 *litPtr = litEnd; /* update for next sequence */
3185 const BYTE* match = op - sequence.offset;
3188 if (sequence.offset > (size_t)op) return ERROR(corruption_detected); /* address space overflow test (this test seems kept by clang optimizer) */
3189 //if (match > op) return ERROR(corruption_detected); /* address space overflow test (is clang optimizer removing this test ?) */
3190 if (match < base) return ERROR(corruption_detected);
3192 /* close range match, overlap */
3193 if (sequence.offset < 8)
3195 const int dec64 = dec64table[sequence.offset];
3200 match += dec32table[sequence.offset];
3201 ZSTD_copy4(op+4, match);
3206 ZSTD_copy8(op, match);
3208 op += 8; match += 8;
3210 if (oMatchEnd > oend-(16-MINMATCH))
3214 ZSTD_wildcopy(op, match, oend_8 - op);
3215 match += oend_8 - op;
3218 while (op < oMatchEnd) *op++ = *match++;
3222 ZSTD_wildcopy(op, match, (ptrdiff_t)sequence.matchLength-8); /* works even if matchLength < 8 */
3226 return oMatchEnd - ostart;
3229 static size_t ZSTD_decompressSequences(
3231 void* dst, size_t maxDstSize,
3232 const void* seqStart, size_t seqSize)
3234 ZSTD_DCtx* dctx = (ZSTD_DCtx*)ctx;
3235 const BYTE* ip = (const BYTE*)seqStart;
3236 const BYTE* const iend = ip + seqSize;
3237 BYTE* const ostart = (BYTE* const)dst;
3239 BYTE* const oend = ostart + maxDstSize;
3240 size_t errorCode, dumpsLength;
3241 const BYTE* litPtr = dctx->litPtr;
3242 const BYTE* const litEnd = litPtr + dctx->litSize;
3245 U32* DTableLL = dctx->LLTable;
3246 U32* DTableML = dctx->MLTable;
3247 U32* DTableOffb = dctx->OffTable;
3248 BYTE* const base = (BYTE*) (dctx->base);
3250 /* Build Decoding Tables */
3251 errorCode = ZSTD_decodeSeqHeaders(&nbSeq, &dumps, &dumpsLength,
3252 DTableLL, DTableML, DTableOffb,
3254 if (ZSTD_isError(errorCode)) return errorCode;
3257 /* Regen sequences */
3260 seqState_t seqState;
3262 memset(&sequence, 0, sizeof(sequence));
3263 seqState.dumps = dumps;
3264 seqState.dumpsEnd = dumps + dumpsLength;
3265 seqState.prevOffset = 1;
3266 errorCode = BIT_initDStream(&(seqState.DStream), ip, iend-ip);
3267 if (ERR_isError(errorCode)) return ERROR(corruption_detected);
3268 FSE_initDState(&(seqState.stateLL), &(seqState.DStream), DTableLL);
3269 FSE_initDState(&(seqState.stateOffb), &(seqState.DStream), DTableOffb);
3270 FSE_initDState(&(seqState.stateML), &(seqState.DStream), DTableML);
3272 for ( ; (BIT_reloadDStream(&(seqState.DStream)) <= BIT_DStream_completed) && (nbSeq>0) ; )
3276 ZSTD_decodeSequence(&sequence, &seqState);
3277 oneSeqSize = ZSTD_execSequence(op, sequence, &litPtr, litEnd, base, oend);
3278 if (ZSTD_isError(oneSeqSize)) return oneSeqSize;
3282 /* check if reached exact end */
3283 if ( !BIT_endOfDStream(&(seqState.DStream)) ) return ERROR(corruption_detected); /* requested too much : data is corrupted */
3284 if (nbSeq<0) return ERROR(corruption_detected); /* requested too many sequences : data is corrupted */
3286 /* last literal segment */
3288 size_t lastLLSize = litEnd - litPtr;
3289 if (litPtr > litEnd) return ERROR(corruption_detected);
3290 if (op+lastLLSize > oend) return ERROR(dstSize_tooSmall);
3291 if (op != litPtr) memmove(op, litPtr, lastLLSize);
3300 static size_t ZSTD_decompressBlock(
3302 void* dst, size_t maxDstSize,
3303 const void* src, size_t srcSize)
3305 /* blockType == blockCompressed */
3306 const BYTE* ip = (const BYTE*)src;
3308 /* Decode literals sub-block */
3309 size_t litCSize = ZSTD_decodeLiteralsBlock(ctx, src, srcSize);
3310 if (ZSTD_isError(litCSize)) return litCSize;
3312 srcSize -= litCSize;
3314 return ZSTD_decompressSequences(ctx, dst, maxDstSize, ip, srcSize);
3318 static size_t ZSTD_decompressDCtx(void* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize)
3320 const BYTE* ip = (const BYTE*)src;
3321 const BYTE* iend = ip + srcSize;
3322 BYTE* const ostart = (BYTE* const)dst;
3324 BYTE* const oend = ostart + maxDstSize;
3325 size_t remainingSize = srcSize;
3327 blockProperties_t blockProperties;
3330 if (srcSize < ZSTD_frameHeaderSize+ZSTD_blockHeaderSize) return ERROR(srcSize_wrong);
3331 magicNumber = MEM_readLE32(src);
3332 if (magicNumber != ZSTD_magicNumber) return ERROR(prefix_unknown);
3333 ip += ZSTD_frameHeaderSize; remainingSize -= ZSTD_frameHeaderSize;
3335 /* Loop on each block */
3338 size_t decodedSize=0;
3339 size_t cBlockSize = ZSTD_getcBlockSize(ip, iend-ip, &blockProperties);
3340 if (ZSTD_isError(cBlockSize)) return cBlockSize;
3342 ip += ZSTD_blockHeaderSize;
3343 remainingSize -= ZSTD_blockHeaderSize;
3344 if (cBlockSize > remainingSize) return ERROR(srcSize_wrong);
3346 switch(blockProperties.blockType)
3349 decodedSize = ZSTD_decompressBlock(ctx, op, oend-op, ip, cBlockSize);
3352 decodedSize = ZSTD_copyUncompressedBlock(op, oend-op, ip, cBlockSize);
3355 return ERROR(GENERIC); /* not yet supported */
3359 if (remainingSize) return ERROR(srcSize_wrong);
3362 return ERROR(GENERIC); /* impossible */
3364 if (cBlockSize == 0) break; /* bt_end */
3366 if (ZSTD_isError(decodedSize)) return decodedSize;
3369 remainingSize -= cBlockSize;
3375 static size_t ZSTD_decompress(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
3379 return ZSTD_decompressDCtx(&ctx, dst, maxDstSize, src, srcSize);
3382 static size_t ZSTD_findFrameCompressedSize(const void *src, size_t srcSize)
3385 const BYTE* ip = (const BYTE*)src;
3386 size_t remainingSize = srcSize;
3388 blockProperties_t blockProperties;
3391 if (srcSize < ZSTD_frameHeaderSize+ZSTD_blockHeaderSize) return ERROR(srcSize_wrong);
3392 magicNumber = MEM_readLE32(src);
3393 if (magicNumber != ZSTD_magicNumber) return ERROR(prefix_unknown);
3394 ip += ZSTD_frameHeaderSize; remainingSize -= ZSTD_frameHeaderSize;
3396 /* Loop on each block */
3399 size_t cBlockSize = ZSTD_getcBlockSize(ip, remainingSize, &blockProperties);
3400 if (ZSTD_isError(cBlockSize)) return cBlockSize;
3402 ip += ZSTD_blockHeaderSize;
3403 remainingSize -= ZSTD_blockHeaderSize;
3404 if (cBlockSize > remainingSize) return ERROR(srcSize_wrong);
3406 if (cBlockSize == 0) break; /* bt_end */
3409 remainingSize -= cBlockSize;
3412 return ip - (const BYTE*)src;
3415 /*******************************
3416 * Streaming Decompression API
3417 *******************************/
3419 static size_t ZSTD_resetDCtx(ZSTD_DCtx* dctx)
3421 dctx->expected = ZSTD_frameHeaderSize;
3423 dctx->previousDstEnd = NULL;
3428 static ZSTD_DCtx* ZSTD_createDCtx(void)
3430 ZSTD_DCtx* dctx = (ZSTD_DCtx*)malloc(sizeof(ZSTD_DCtx));
3431 if (dctx==NULL) return NULL;
3432 ZSTD_resetDCtx(dctx);
3436 static size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx)
3442 static size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx)
3444 return dctx->expected;
3447 static size_t ZSTD_decompressContinue(ZSTD_DCtx* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize)
3450 if (srcSize != ctx->expected) return ERROR(srcSize_wrong);
3451 if (dst != ctx->previousDstEnd) /* not contiguous */
3454 /* Decompress : frame header */
3455 if (ctx->phase == 0)
3457 /* Check frame magic header */
3458 U32 magicNumber = MEM_readLE32(src);
3459 if (magicNumber != ZSTD_magicNumber) return ERROR(prefix_unknown);
3461 ctx->expected = ZSTD_blockHeaderSize;
3465 /* Decompress : block header */
3466 if (ctx->phase == 1)
3468 blockProperties_t bp;
3469 size_t blockSize = ZSTD_getcBlockSize(src, ZSTD_blockHeaderSize, &bp);
3470 if (ZSTD_isError(blockSize)) return blockSize;
3471 if (bp.blockType == bt_end)
3478 ctx->expected = blockSize;
3479 ctx->bType = bp.blockType;
3486 /* Decompress : block content */
3492 rSize = ZSTD_decompressBlock(ctx, dst, maxDstSize, src, srcSize);
3495 rSize = ZSTD_copyUncompressedBlock(dst, maxDstSize, src, srcSize);
3498 return ERROR(GENERIC); /* not yet handled */
3500 case bt_end : /* should never happen (filtered at phase 1) */
3504 return ERROR(GENERIC);
3507 ctx->expected = ZSTD_blockHeaderSize;
3508 ctx->previousDstEnd = (void*)( ((char*)dst) + rSize);
3517 unsigned ZSTDv02_isError(size_t code)
3519 return ZSTD_isError(code);
3522 size_t ZSTDv02_decompress( void* dst, size_t maxOriginalSize,
3523 const void* src, size_t compressedSize)
3525 return ZSTD_decompress(dst, maxOriginalSize, src, compressedSize);
3528 size_t ZSTDv02_findFrameCompressedSize(const void *src, size_t compressedSize)
3530 return ZSTD_findFrameCompressedSize(src, compressedSize);
3533 ZSTDv02_Dctx* ZSTDv02_createDCtx(void)
3535 return (ZSTDv02_Dctx*)ZSTD_createDCtx();
3538 size_t ZSTDv02_freeDCtx(ZSTDv02_Dctx* dctx)
3540 return ZSTD_freeDCtx((ZSTD_DCtx*)dctx);
3543 size_t ZSTDv02_resetDCtx(ZSTDv02_Dctx* dctx)
3545 return ZSTD_resetDCtx((ZSTD_DCtx*)dctx);
3548 size_t ZSTDv02_nextSrcSizeToDecompress(ZSTDv02_Dctx* dctx)
3550 return ZSTD_nextSrcSizeToDecompress((ZSTD_DCtx*)dctx);
3553 size_t ZSTDv02_decompressContinue(ZSTDv02_Dctx* dctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize)
3555 return ZSTD_decompressContinue((ZSTD_DCtx*)dctx, dst, maxDstSize, src, srcSize);