huf.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /* ******************************************************************
  2. Huffman coder, part of New Generation Entropy library
  3. header file
  4. Copyright (C) 2013-2016, Yann Collet.
  5. BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions are
  8. met:
  9. * Redistributions of source code must retain the above copyright
  10. notice, this list of conditions and the following disclaimer.
  11. * Redistributions in binary form must reproduce the above
  12. copyright notice, this list of conditions and the following disclaimer
  13. in the documentation and/or other materials provided with the
  14. distribution.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. You can contact the author at :
  27. - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
  28. ****************************************************************** */
  29. #if defined (__cplusplus)
  30. extern "C" {
  31. #endif
  32. #ifndef HUF_H_298734234
  33. #define HUF_H_298734234
  34. /* *** Dependencies *** */
  35. #include <stddef.h> /* size_t */
  36. /* *** library symbols visibility *** */
  37. /* Note : when linking with -fvisibility=hidden on gcc, or by default on Visual,
  38. * HUF symbols remain "private" (internal symbols for library only).
  39. * Set macro FSE_DLL_EXPORT to 1 if you want HUF symbols visible on DLL interface */
  40. #if defined(FSE_DLL_EXPORT) && (FSE_DLL_EXPORT==1) && defined(__GNUC__) && (__GNUC__ >= 4)
  41. # define HUF_PUBLIC_API __attribute__ ((visibility ("default")))
  42. #elif defined(FSE_DLL_EXPORT) && (FSE_DLL_EXPORT==1) /* Visual expected */
  43. # define HUF_PUBLIC_API __declspec(dllexport)
  44. #elif defined(FSE_DLL_IMPORT) && (FSE_DLL_IMPORT==1)
  45. # define HUF_PUBLIC_API __declspec(dllimport) /* not required, just to generate faster code (saves a function pointer load from IAT and an indirect jump) */
  46. #else
  47. # define HUF_PUBLIC_API
  48. #endif
  49. /* *** simple functions *** */
  50. /**
  51. HUF_compress() :
  52. Compress content from buffer 'src', of size 'srcSize', into buffer 'dst'.
  53. 'dst' buffer must be already allocated.
  54. Compression runs faster if `dstCapacity` >= HUF_compressBound(srcSize).
  55. `srcSize` must be <= `HUF_BLOCKSIZE_MAX` == 128 KB.
  56. @return : size of compressed data (<= `dstCapacity`).
  57. Special values : if return == 0, srcData is not compressible => Nothing is stored within dst !!!
  58. if return == 1, srcData is a single repeated byte symbol (RLE compression).
  59. if HUF_isError(return), compression failed (more details using HUF_getErrorName())
  60. */
  61. HUF_PUBLIC_API size_t HUF_compress(void* dst, size_t dstCapacity,
  62. const void* src, size_t srcSize);
  63. /**
  64. HUF_decompress() :
  65. Decompress HUF data from buffer 'cSrc', of size 'cSrcSize',
  66. into already allocated buffer 'dst', of minimum size 'dstSize'.
  67. `originalSize` : **must** be the ***exact*** size of original (uncompressed) data.
  68. Note : in contrast with FSE, HUF_decompress can regenerate
  69. RLE (cSrcSize==1) and uncompressed (cSrcSize==dstSize) data,
  70. because it knows size to regenerate.
  71. @return : size of regenerated data (== originalSize),
  72. or an error code, which can be tested using HUF_isError()
  73. */
  74. HUF_PUBLIC_API size_t HUF_decompress(void* dst, size_t originalSize,
  75. const void* cSrc, size_t cSrcSize);
  76. /* *** Tool functions *** */
  77. #define HUF_BLOCKSIZE_MAX (128 * 1024) /**< maximum input size for a single block compressed with HUF_compress */
  78. HUF_PUBLIC_API size_t HUF_compressBound(size_t size); /**< maximum compressed size (worst case) */
  79. /* Error Management */
  80. HUF_PUBLIC_API unsigned HUF_isError(size_t code); /**< tells if a return value is an error code */
  81. HUF_PUBLIC_API const char* HUF_getErrorName(size_t code); /**< provides error code string (useful for debugging) */
  82. /* *** Advanced function *** */
  83. /** HUF_compress2() :
  84. * Same as HUF_compress(), but offers direct control over `maxSymbolValue` and `tableLog`.
  85. * `tableLog` must be `<= HUF_TABLELOG_MAX` . */
  86. HUF_PUBLIC_API size_t HUF_compress2 (void* dst, size_t dstCapacity, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog);
  87. /** HUF_compress4X_wksp() :
  88. * Same as HUF_compress2(), but uses externally allocated `workSpace`.
  89. * `workspace` must have minimum alignment of 4, and be at least as large as following macro */
  90. #define HUF_WORKSPACE_SIZE (6 << 10)
  91. #define HUF_WORKSPACE_SIZE_U32 (HUF_WORKSPACE_SIZE / sizeof(U32))
  92. HUF_PUBLIC_API size_t HUF_compress4X_wksp (void* dst, size_t dstCapacity, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize);
  93. /**
  94. * The minimum workspace size for the `workSpace` used in
  95. * HUF_readDTableX2_wksp() and HUF_readDTableX4_wksp().
  96. *
  97. * The space used depends on HUF_TABLELOG_MAX, ranging from ~1500 bytes when
  98. * HUF_TABLE_LOG_MAX=12 to ~1850 bytes when HUF_TABLE_LOG_MAX=15.
  99. * Buffer overflow errors may potentially occur if code modifications result in
  100. * a required workspace size greater than that specified in the following
  101. * macro.
  102. */
  103. #define HUF_DECOMPRESS_WORKSPACE_SIZE (2 << 10)
  104. #define HUF_DECOMPRESS_WORKSPACE_SIZE_U32 (HUF_DECOMPRESS_WORKSPACE_SIZE / sizeof(U32))
  105. #endif /* HUF_H_298734234 */
  106. /* ******************************************************************
  107. * WARNING !!
  108. * The following section contains advanced and experimental definitions
  109. * which shall never be used in the context of dll
  110. * because they are not guaranteed to remain stable in the future.
  111. * Only consider them in association with static linking.
  112. *******************************************************************/
  113. #if defined(HUF_STATIC_LINKING_ONLY) && !defined(HUF_H_HUF_STATIC_LINKING_ONLY)
  114. #define HUF_H_HUF_STATIC_LINKING_ONLY
  115. /* *** Dependencies *** */
  116. #include "mem.h" /* U32 */
  117. /* *** Constants *** */
  118. #define HUF_TABLELOG_MAX 12 /* max configured tableLog (for static allocation); can be modified up to HUF_ABSOLUTEMAX_TABLELOG */
  119. #define HUF_TABLELOG_DEFAULT 11 /* tableLog by default, when not specified */
  120. #define HUF_SYMBOLVALUE_MAX 255
  121. #define HUF_TABLELOG_ABSOLUTEMAX 15 /* absolute limit of HUF_MAX_TABLELOG. Beyond that value, code does not work */
  122. #if (HUF_TABLELOG_MAX > HUF_TABLELOG_ABSOLUTEMAX)
  123. # error "HUF_TABLELOG_MAX is too large !"
  124. #endif
  125. /* ****************************************
  126. * Static allocation
  127. ******************************************/
  128. /* HUF buffer bounds */
  129. #define HUF_CTABLEBOUND 129
  130. #define HUF_BLOCKBOUND(size) (size + (size>>8) + 8) /* only true when incompressible is pre-filtered with fast heuristic */
  131. #define HUF_COMPRESSBOUND(size) (HUF_CTABLEBOUND + HUF_BLOCKBOUND(size)) /* Macro version, useful for static allocation */
  132. /* static allocation of HUF's Compression Table */
  133. #define HUF_CTABLE_SIZE_U32(maxSymbolValue) ((maxSymbolValue)+1) /* Use tables of U32, for proper alignment */
  134. #define HUF_CTABLE_SIZE(maxSymbolValue) (HUF_CTABLE_SIZE_U32(maxSymbolValue) * sizeof(U32))
  135. #define HUF_CREATE_STATIC_CTABLE(name, maxSymbolValue) \
  136. U32 name##hb[HUF_CTABLE_SIZE_U32(maxSymbolValue)]; \
  137. void* name##hv = &(name##hb); \
  138. HUF_CElt* name = (HUF_CElt*)(name##hv) /* no final ; */
  139. /* static allocation of HUF's DTable */
  140. typedef U32 HUF_DTable;
  141. #define HUF_DTABLE_SIZE(maxTableLog) (1 + (1<<(maxTableLog)))
  142. #define HUF_CREATE_STATIC_DTABLEX2(DTable, maxTableLog) \
  143. HUF_DTable DTable[HUF_DTABLE_SIZE((maxTableLog)-1)] = { ((U32)((maxTableLog)-1) * 0x01000001) }
  144. #define HUF_CREATE_STATIC_DTABLEX4(DTable, maxTableLog) \
  145. HUF_DTable DTable[HUF_DTABLE_SIZE(maxTableLog)] = { ((U32)(maxTableLog) * 0x01000001) }
  146. /* ****************************************
  147. * Advanced decompression functions
  148. ******************************************/
  149. size_t HUF_decompress4X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< single-symbol decoder */
  150. size_t HUF_decompress4X4 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< double-symbols decoder */
  151. size_t HUF_decompress4X_DCtx (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< decodes RLE and uncompressed */
  152. size_t HUF_decompress4X_hufOnly(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< considers RLE and uncompressed as errors */
  153. size_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 */
  154. size_t HUF_decompress4X2_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< single-symbol decoder */
  155. size_t HUF_decompress4X2_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize); /**< single-symbol decoder */
  156. size_t HUF_decompress4X4_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< double-symbols decoder */
  157. size_t HUF_decompress4X4_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize); /**< double-symbols decoder */
  158. /* ****************************************
  159. * HUF detailed API
  160. ******************************************/
  161. /*!
  162. HUF_compress() does the following:
  163. 1. count symbol occurrence from source[] into table count[] using FSE_count()
  164. 2. (optional) refine tableLog using HUF_optimalTableLog()
  165. 3. build Huffman table from count using HUF_buildCTable()
  166. 4. save Huffman table to memory buffer using HUF_writeCTable()
  167. 5. encode the data stream using HUF_compress4X_usingCTable()
  168. The following API allows targeting specific sub-functions for advanced tasks.
  169. For example, it's possible to compress several blocks using the same 'CTable',
  170. or to save and regenerate 'CTable' using external methods.
  171. */
  172. /* FSE_count() : find it within "fse.h" */
  173. unsigned HUF_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue);
  174. typedef struct HUF_CElt_s HUF_CElt; /* incomplete type */
  175. size_t HUF_buildCTable (HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue, unsigned maxNbBits);
  176. size_t HUF_writeCTable (void* dst, size_t maxDstSize, const HUF_CElt* CTable, unsigned maxSymbolValue, unsigned huffLog);
  177. size_t HUF_compress4X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable);
  178. typedef enum {
  179. HUF_repeat_none, /**< Cannot use the previous table */
  180. HUF_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 */
  181. HUF_repeat_valid /**< Can use the previous table and it is asumed to be valid */
  182. } HUF_repeat;
  183. /** HUF_compress4X_repeat() :
  184. * Same as HUF_compress4X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none.
  185. * If it uses hufTable it does not modify hufTable or repeat.
  186. * If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used.
  187. * If preferRepeat then the old table will always be used if valid. */
  188. size_t HUF_compress4X_repeat(void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize, HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat); /**< `workSpace` must be a table of at least HUF_WORKSPACE_SIZE_U32 unsigned */
  189. /** HUF_buildCTable_wksp() :
  190. * Same as HUF_buildCTable(), but using externally allocated scratch buffer.
  191. * `workSpace` must be aligned on 4-bytes boundaries, and be at least as large as a table of 1024 unsigned.
  192. */
  193. size_t HUF_buildCTable_wksp (HUF_CElt* tree, const U32* count, U32 maxSymbolValue, U32 maxNbBits, void* workSpace, size_t wkspSize);
  194. /*! HUF_readStats() :
  195. Read compact Huffman tree, saved by HUF_writeCTable().
  196. `huffWeight` is destination buffer.
  197. @return : size read from `src` , or an error Code .
  198. Note : Needed by HUF_readCTable() and HUF_readDTableXn() . */
  199. size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
  200. U32* nbSymbolsPtr, U32* tableLogPtr,
  201. const void* src, size_t srcSize);
  202. /** HUF_readCTable() :
  203. * Loading a CTable saved with HUF_writeCTable() */
  204. size_t HUF_readCTable (HUF_CElt* CTable, unsigned* maxSymbolValuePtr, const void* src, size_t srcSize);
  205. /*
  206. HUF_decompress() does the following:
  207. 1. select the decompression algorithm (X2, X4) based on pre-computed heuristics
  208. 2. build Huffman table from save, using HUF_readDTableXn()
  209. 3. decode 1 or 4 segments in parallel using HUF_decompressSXn_usingDTable
  210. */
  211. /** HUF_selectDecoder() :
  212. * Tells which decoder is likely to decode faster,
  213. * based on a set of pre-determined metrics.
  214. * @return : 0==HUF_decompress4X2, 1==HUF_decompress4X4 .
  215. * Assumption : 0 < cSrcSize < dstSize <= 128 KB */
  216. U32 HUF_selectDecoder (size_t dstSize, size_t cSrcSize);
  217. size_t HUF_readDTableX2 (HUF_DTable* DTable, const void* src, size_t srcSize);
  218. size_t HUF_readDTableX2_wksp (HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize);
  219. size_t HUF_readDTableX4 (HUF_DTable* DTable, const void* src, size_t srcSize);
  220. size_t HUF_readDTableX4_wksp (HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize);
  221. size_t HUF_decompress4X_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);
  222. size_t HUF_decompress4X2_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);
  223. size_t HUF_decompress4X4_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);
  224. /* single stream variants */
  225. size_t HUF_compress1X (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog);
  226. size_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_U32 unsigned */
  227. size_t HUF_compress1X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable);
  228. /** HUF_compress1X_repeat() :
  229. * Same as HUF_compress1X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none.
  230. * If it uses hufTable it does not modify hufTable or repeat.
  231. * If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used.
  232. * If preferRepeat then the old table will always be used if valid. */
  233. size_t HUF_compress1X_repeat(void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize, HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat); /**< `workSpace` must be a table of at least HUF_WORKSPACE_SIZE_U32 unsigned */
  234. size_t HUF_decompress1X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* single-symbol decoder */
  235. size_t HUF_decompress1X4 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* double-symbol decoder */
  236. size_t HUF_decompress1X_DCtx (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);
  237. size_t HUF_decompress1X_DCtx_wksp (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize);
  238. size_t HUF_decompress1X2_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< single-symbol decoder */
  239. size_t HUF_decompress1X2_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize); /**< single-symbol decoder */
  240. size_t HUF_decompress1X4_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< double-symbols decoder */
  241. size_t HUF_decompress1X4_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize); /**< double-symbols decoder */
  242. size_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 */
  243. size_t HUF_decompress1X2_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);
  244. size_t HUF_decompress1X4_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);
  245. #endif /* HUF_STATIC_LINKING_ONLY */
  246. #if defined (__cplusplus)
  247. }
  248. #endif