/* ******************************************************************1* huff0 huffman codec,2* part of Finite State Entropy library3* Copyright (c) Yann Collet, Facebook, Inc.4*5* You can contact the author at :6* - Source repository : https://github.com/Cyan4973/FiniteStateEntropy7*8* This source code is licensed under both the BSD-style license (found in the9* LICENSE file in the root directory of this source tree) and the GPLv2 (found10* in the COPYING file in the root directory of this source tree).11* You may select, at your option, one of the above-listed licenses.12****************************************************************** */1314#if defined (__cplusplus)15extern "C" {16#endif1718#ifndef HUF_H_29873423419#define HUF_H_2987342342021/* *** Dependencies *** */22#include "zstd_deps.h" /* size_t */232425/* *** library symbols visibility *** */26/* Note : when linking with -fvisibility=hidden on gcc, or by default on Visual,27* HUF symbols remain "private" (internal symbols for library only).28* Set macro FSE_DLL_EXPORT to 1 if you want HUF symbols visible on DLL interface */29#if defined(FSE_DLL_EXPORT) && (FSE_DLL_EXPORT==1) && defined(__GNUC__) && (__GNUC__ >= 4)30# define HUF_PUBLIC_API __attribute__ ((visibility ("default")))31#elif defined(FSE_DLL_EXPORT) && (FSE_DLL_EXPORT==1) /* Visual expected */32# define HUF_PUBLIC_API __declspec(dllexport)33#elif defined(FSE_DLL_IMPORT) && (FSE_DLL_IMPORT==1)34# define HUF_PUBLIC_API __declspec(dllimport) /* not required, just to generate faster code (saves a function pointer load from IAT and an indirect jump) */35#else36# define HUF_PUBLIC_API37#endif383940/* ========================== */41/* *** simple functions *** */42/* ========================== */4344/** HUF_compress() :45* Compress content from buffer 'src', of size 'srcSize', into buffer 'dst'.46* 'dst' buffer must be already allocated.47* Compression runs faster if `dstCapacity` >= HUF_compressBound(srcSize).48* `srcSize` must be <= `HUF_BLOCKSIZE_MAX` == 128 KB.49* @return : size of compressed data (<= `dstCapacity`).50* Special values : if return == 0, srcData is not compressible => Nothing is stored within dst !!!51* if HUF_isError(return), compression failed (more details using HUF_getErrorName())52*/53HUF_PUBLIC_API size_t HUF_compress(void* dst, size_t dstCapacity,54const void* src, size_t srcSize);5556/** HUF_decompress() :57* Decompress HUF data from buffer 'cSrc', of size 'cSrcSize',58* into already allocated buffer 'dst', of minimum size 'dstSize'.59* `originalSize` : **must** be the ***exact*** size of original (uncompressed) data.60* Note : in contrast with FSE, HUF_decompress can regenerate61* RLE (cSrcSize==1) and uncompressed (cSrcSize==dstSize) data,62* because it knows size to regenerate (originalSize).63* @return : size of regenerated data (== originalSize),64* or an error code, which can be tested using HUF_isError()65*/66HUF_PUBLIC_API size_t HUF_decompress(void* dst, size_t originalSize,67const void* cSrc, size_t cSrcSize);686970/* *** Tool functions *** */71#define HUF_BLOCKSIZE_MAX (128 * 1024) /**< maximum input size for a single block compressed with HUF_compress */72HUF_PUBLIC_API size_t HUF_compressBound(size_t size); /**< maximum compressed size (worst case) */7374/* Error Management */75HUF_PUBLIC_API unsigned HUF_isError(size_t code); /**< tells if a return value is an error code */76HUF_PUBLIC_API const char* HUF_getErrorName(size_t code); /**< provides error code string (useful for debugging) */777879/* *** Advanced function *** */8081/** HUF_compress2() :82* Same as HUF_compress(), but offers control over `maxSymbolValue` and `tableLog`.83* `maxSymbolValue` must be <= HUF_SYMBOLVALUE_MAX .84* `tableLog` must be `<= HUF_TABLELOG_MAX` . */85HUF_PUBLIC_API size_t HUF_compress2 (void* dst, size_t dstCapacity,86const void* src, size_t srcSize,87unsigned maxSymbolValue, unsigned tableLog);8889/** HUF_compress4X_wksp() :90* Same as HUF_compress2(), but uses externally allocated `workSpace`.91* `workspace` must be at least as large as HUF_WORKSPACE_SIZE */92#define HUF_WORKSPACE_SIZE ((8 << 10) + 512 /* sorting scratch space */)93#define HUF_WORKSPACE_SIZE_U64 (HUF_WORKSPACE_SIZE / sizeof(U64))94HUF_PUBLIC_API size_t HUF_compress4X_wksp (void* dst, size_t dstCapacity,95const void* src, size_t srcSize,96unsigned maxSymbolValue, unsigned tableLog,97void* workSpace, size_t wkspSize);9899#endif /* HUF_H_298734234 */100101/* ******************************************************************102* WARNING !!103* The following section contains advanced and experimental definitions104* which shall never be used in the context of a dynamic library,105* because they are not guaranteed to remain stable in the future.106* Only consider them in association with static linking.107* *****************************************************************/108#if defined(HUF_STATIC_LINKING_ONLY) && !defined(HUF_H_HUF_STATIC_LINKING_ONLY)109#define HUF_H_HUF_STATIC_LINKING_ONLY110111/* *** Dependencies *** */112#include "mem.h" /* U32 */113#define FSE_STATIC_LINKING_ONLY114#include "fse.h"115116117/* *** Constants *** */118#define HUF_TABLELOG_MAX 12 /* max runtime value of tableLog (due to static allocation); can be modified up to HUF_TABLELOG_ABSOLUTEMAX */119#define HUF_TABLELOG_DEFAULT 11 /* default tableLog value when none specified */120#define HUF_SYMBOLVALUE_MAX 255121122#define HUF_TABLELOG_ABSOLUTEMAX 12 /* absolute limit of HUF_MAX_TABLELOG. Beyond that value, code does not work */123#if (HUF_TABLELOG_MAX > HUF_TABLELOG_ABSOLUTEMAX)124# error "HUF_TABLELOG_MAX is too large !"125#endif126127128/* ****************************************129* Static allocation130******************************************/131/* HUF buffer bounds */132#define HUF_CTABLEBOUND 129133#define HUF_BLOCKBOUND(size) (size + (size>>8) + 8) /* only true when incompressible is pre-filtered with fast heuristic */134#define HUF_COMPRESSBOUND(size) (HUF_CTABLEBOUND + HUF_BLOCKBOUND(size)) /* Macro version, useful for static allocation */135136/* static allocation of HUF's Compression Table */137/* this is a private definition, just exposed for allocation and strict aliasing purpose. never EVER access its members directly */138typedef size_t HUF_CElt; /* consider it an incomplete type */139#define HUF_CTABLE_SIZE_ST(maxSymbolValue) ((maxSymbolValue)+2) /* Use tables of size_t, for proper alignment */140#define HUF_CTABLE_SIZE(maxSymbolValue) (HUF_CTABLE_SIZE_ST(maxSymbolValue) * sizeof(size_t))141#define HUF_CREATE_STATIC_CTABLE(name, maxSymbolValue) \142HUF_CElt name[HUF_CTABLE_SIZE_ST(maxSymbolValue)] /* no final ; */143144/* static allocation of HUF's DTable */145typedef U32 HUF_DTable;146#define HUF_DTABLE_SIZE(maxTableLog) (1 + (1<<(maxTableLog)))147#define HUF_CREATE_STATIC_DTABLEX1(DTable, maxTableLog) \148HUF_DTable DTable[HUF_DTABLE_SIZE((maxTableLog)-1)] = { ((U32)((maxTableLog)-1) * 0x01000001) }149#define HUF_CREATE_STATIC_DTABLEX2(DTable, maxTableLog) \150HUF_DTable DTable[HUF_DTABLE_SIZE(maxTableLog)] = { ((U32)(maxTableLog) * 0x01000001) }151152153/* ****************************************154* Advanced decompression functions155******************************************/156size_t HUF_decompress4X1 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< single-symbol decoder */157#ifndef HUF_FORCE_DECOMPRESS_X1158size_t HUF_decompress4X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< double-symbols decoder */159#endif160161size_t HUF_decompress4X_DCtx (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< decodes RLE and uncompressed */162size_t HUF_decompress4X_hufOnly(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< considers RLE and uncompressed as errors */163size_t HUF_decompress4X_hufOnly_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize); /**< considers RLE and uncompressed as errors */164size_t HUF_decompress4X1_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< single-symbol decoder */165size_t HUF_decompress4X1_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize); /**< single-symbol decoder */166#ifndef HUF_FORCE_DECOMPRESS_X1167size_t HUF_decompress4X2_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< double-symbols decoder */168size_t HUF_decompress4X2_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize); /**< double-symbols decoder */169#endif170171172/* ****************************************173* HUF detailed API174* ****************************************/175176/*! HUF_compress() does the following:177* 1. count symbol occurrence from source[] into table count[] using FSE_count() (exposed within "fse.h")178* 2. (optional) refine tableLog using HUF_optimalTableLog()179* 3. build Huffman table from count using HUF_buildCTable()180* 4. save Huffman table to memory buffer using HUF_writeCTable()181* 5. encode the data stream using HUF_compress4X_usingCTable()182*183* The following API allows targeting specific sub-functions for advanced tasks.184* For example, it's possible to compress several blocks using the same 'CTable',185* or to save and regenerate 'CTable' using external methods.186*/187unsigned HUF_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue);188size_t HUF_buildCTable (HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue, unsigned maxNbBits); /* @return : maxNbBits; CTable and count can overlap. In which case, CTable will overwrite count content */189size_t HUF_writeCTable (void* dst, size_t maxDstSize, const HUF_CElt* CTable, unsigned maxSymbolValue, unsigned huffLog);190size_t HUF_writeCTable_wksp(void* dst, size_t maxDstSize, const HUF_CElt* CTable, unsigned maxSymbolValue, unsigned huffLog, void* workspace, size_t workspaceSize);191size_t HUF_compress4X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable);192size_t HUF_compress4X_usingCTable_bmi2(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable, int bmi2);193size_t HUF_estimateCompressedSize(const HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue);194int HUF_validateCTable(const HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue);195196typedef enum {197HUF_repeat_none, /**< Cannot use the previous table */198HUF_repeat_check, /**< Can use the previous table but it must be checked. Note : The previous table must have been constructed by HUF_compress{1, 4}X_repeat */199HUF_repeat_valid /**< Can use the previous table and it is assumed to be valid */200} HUF_repeat;201/** HUF_compress4X_repeat() :202* Same as HUF_compress4X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none.203* If it uses hufTable it does not modify hufTable or repeat.204* If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used.205* If preferRepeat then the old table will always be used if valid.206* If suspectUncompressible then some sampling checks will be run to potentially skip huffman coding */207size_t HUF_compress4X_repeat(void* dst, size_t dstSize,208const void* src, size_t srcSize,209unsigned maxSymbolValue, unsigned tableLog,210void* workSpace, size_t wkspSize, /**< `workSpace` must be aligned on 4-bytes boundaries, `wkspSize` must be >= HUF_WORKSPACE_SIZE */211HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat, int bmi2, unsigned suspectUncompressible);212213/** HUF_buildCTable_wksp() :214* Same as HUF_buildCTable(), but using externally allocated scratch buffer.215* `workSpace` must be aligned on 4-bytes boundaries, and its size must be >= HUF_CTABLE_WORKSPACE_SIZE.216*/217#define HUF_CTABLE_WORKSPACE_SIZE_U32 (2*HUF_SYMBOLVALUE_MAX +1 +1)218#define HUF_CTABLE_WORKSPACE_SIZE (HUF_CTABLE_WORKSPACE_SIZE_U32 * sizeof(unsigned))219size_t HUF_buildCTable_wksp (HUF_CElt* tree,220const unsigned* count, U32 maxSymbolValue, U32 maxNbBits,221void* workSpace, size_t wkspSize);222223/*! HUF_readStats() :224* Read compact Huffman tree, saved by HUF_writeCTable().225* `huffWeight` is destination buffer.226* @return : size read from `src` , or an error Code .227* Note : Needed by HUF_readCTable() and HUF_readDTableXn() . */228size_t HUF_readStats(BYTE* huffWeight, size_t hwSize,229U32* rankStats, U32* nbSymbolsPtr, U32* tableLogPtr,230const void* src, size_t srcSize);231232/*! HUF_readStats_wksp() :233* Same as HUF_readStats() but takes an external workspace which must be234* 4-byte aligned and its size must be >= HUF_READ_STATS_WORKSPACE_SIZE.235* If the CPU has BMI2 support, pass bmi2=1, otherwise pass bmi2=0.236*/237#define HUF_READ_STATS_WORKSPACE_SIZE_U32 FSE_DECOMPRESS_WKSP_SIZE_U32(6, HUF_TABLELOG_MAX-1)238#define HUF_READ_STATS_WORKSPACE_SIZE (HUF_READ_STATS_WORKSPACE_SIZE_U32 * sizeof(unsigned))239size_t HUF_readStats_wksp(BYTE* huffWeight, size_t hwSize,240U32* rankStats, U32* nbSymbolsPtr, U32* tableLogPtr,241const void* src, size_t srcSize,242void* workspace, size_t wkspSize,243int bmi2);244245/** HUF_readCTable() :246* Loading a CTable saved with HUF_writeCTable() */247size_t HUF_readCTable (HUF_CElt* CTable, unsigned* maxSymbolValuePtr, const void* src, size_t srcSize, unsigned *hasZeroWeights);248249/** HUF_getNbBitsFromCTable() :250* Read nbBits from CTable symbolTable, for symbol `symbolValue` presumed <= HUF_SYMBOLVALUE_MAX251* Note 1 : is not inlined, as HUF_CElt definition is private */252U32 HUF_getNbBitsFromCTable(const HUF_CElt* symbolTable, U32 symbolValue);253254/*255* HUF_decompress() does the following:256* 1. select the decompression algorithm (X1, X2) based on pre-computed heuristics257* 2. build Huffman table from save, using HUF_readDTableX?()258* 3. decode 1 or 4 segments in parallel using HUF_decompress?X?_usingDTable()259*/260261/** HUF_selectDecoder() :262* Tells which decoder is likely to decode faster,263* based on a set of pre-computed metrics.264* @return : 0==HUF_decompress4X1, 1==HUF_decompress4X2 .265* Assumption : 0 < dstSize <= 128 KB */266U32 HUF_selectDecoder (size_t dstSize, size_t cSrcSize);267268/**269* The minimum workspace size for the `workSpace` used in270* HUF_readDTableX1_wksp() and HUF_readDTableX2_wksp().271*272* The space used depends on HUF_TABLELOG_MAX, ranging from ~1500 bytes when273* HUF_TABLE_LOG_MAX=12 to ~1850 bytes when HUF_TABLE_LOG_MAX=15.274* Buffer overflow errors may potentially occur if code modifications result in275* a required workspace size greater than that specified in the following276* macro.277*/278#define HUF_DECOMPRESS_WORKSPACE_SIZE ((2 << 10) + (1 << 9))279#define HUF_DECOMPRESS_WORKSPACE_SIZE_U32 (HUF_DECOMPRESS_WORKSPACE_SIZE / sizeof(U32))280281#ifndef HUF_FORCE_DECOMPRESS_X2282size_t HUF_readDTableX1 (HUF_DTable* DTable, const void* src, size_t srcSize);283size_t HUF_readDTableX1_wksp (HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize);284#endif285#ifndef HUF_FORCE_DECOMPRESS_X1286size_t HUF_readDTableX2 (HUF_DTable* DTable, const void* src, size_t srcSize);287size_t HUF_readDTableX2_wksp (HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize);288#endif289290size_t HUF_decompress4X_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);291#ifndef HUF_FORCE_DECOMPRESS_X2292size_t HUF_decompress4X1_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);293#endif294#ifndef HUF_FORCE_DECOMPRESS_X1295size_t HUF_decompress4X2_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);296#endif297298299/* ====================== */300/* single stream variants */301/* ====================== */302303size_t HUF_compress1X (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog);304size_t HUF_compress1X_wksp (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize); /**< `workSpace` must be a table of at least HUF_WORKSPACE_SIZE_U64 U64 */305size_t HUF_compress1X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable);306size_t HUF_compress1X_usingCTable_bmi2(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable, int bmi2);307/** HUF_compress1X_repeat() :308* Same as HUF_compress1X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none.309* If it uses hufTable it does not modify hufTable or repeat.310* If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used.311* If preferRepeat then the old table will always be used if valid.312* If suspectUncompressible then some sampling checks will be run to potentially skip huffman coding */313size_t HUF_compress1X_repeat(void* dst, size_t dstSize,314const void* src, size_t srcSize,315unsigned maxSymbolValue, unsigned tableLog,316void* workSpace, size_t wkspSize, /**< `workSpace` must be aligned on 4-bytes boundaries, `wkspSize` must be >= HUF_WORKSPACE_SIZE */317HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat, int bmi2, unsigned suspectUncompressible);318319size_t HUF_decompress1X1 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* single-symbol decoder */320#ifndef HUF_FORCE_DECOMPRESS_X1321size_t HUF_decompress1X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* double-symbol decoder */322#endif323324size_t HUF_decompress1X_DCtx (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);325size_t HUF_decompress1X_DCtx_wksp (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize);326#ifndef HUF_FORCE_DECOMPRESS_X2327size_t HUF_decompress1X1_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< single-symbol decoder */328size_t HUF_decompress1X1_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize); /**< single-symbol decoder */329#endif330#ifndef HUF_FORCE_DECOMPRESS_X1331size_t HUF_decompress1X2_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< double-symbols decoder */332size_t HUF_decompress1X2_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize); /**< double-symbols decoder */333#endif334335size_t HUF_decompress1X_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable); /**< automatic selection of sing or double symbol decoder, based on DTable */336#ifndef HUF_FORCE_DECOMPRESS_X2337size_t HUF_decompress1X1_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);338#endif339#ifndef HUF_FORCE_DECOMPRESS_X1340size_t HUF_decompress1X2_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);341#endif342343/* BMI2 variants.344* If the CPU has BMI2 support, pass bmi2=1, otherwise pass bmi2=0.345*/346size_t HUF_decompress1X_usingDTable_bmi2(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable, int bmi2);347#ifndef HUF_FORCE_DECOMPRESS_X2348size_t HUF_decompress1X1_DCtx_wksp_bmi2(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize, int bmi2);349#endif350size_t HUF_decompress4X_usingDTable_bmi2(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable, int bmi2);351size_t HUF_decompress4X_hufOnly_wksp_bmi2(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize, int bmi2);352#ifndef HUF_FORCE_DECOMPRESS_X2353size_t HUF_readDTableX1_wksp_bmi2(HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize, int bmi2);354#endif355#ifndef HUF_FORCE_DECOMPRESS_X1356size_t HUF_readDTableX2_wksp_bmi2(HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize, int bmi2);357#endif358359#endif /* HUF_STATIC_LINKING_ONLY */360361#if defined (__cplusplus)362}363#endif364365366