2 * Huffman coder, part of New Generation Entropy library
4 * Copyright (C) 2013-2016, Yann Collet.
6 * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * This program is free software; you can redistribute it and/or modify it under
32 * the terms of the GNU General Public License version 2 as published by the
33 * Free Software Foundation. This program is dual-licensed; you may select
34 * either version 2 of the GNU General Public License ("GPL") or BSD license
37 * You can contact the author at :
38 * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
40 #ifndef HUF_H_298734234
41 #define HUF_H_298734234
43 /* *** Dependencies *** */
44 #include <linux/types.h> /* size_t */
46 /* *** Tool functions *** */
47 #define HUF_BLOCKSIZE_MAX (128 * 1024) /**< maximum input size for a single block compressed with HUF_compress */
48 size_t HUF_compressBound(size_t size
); /**< maximum compressed size (worst case) */
50 /* Error Management */
51 unsigned HUF_isError(size_t code
); /**< tells if a return value is an error code */
53 /* *** Advanced function *** */
55 /** HUF_compress4X_wksp() :
56 * Same as HUF_compress2(), but uses externally allocated `workSpace`, which must be a table of >= 1024 unsigned */
57 size_t HUF_compress4X_wksp(void *dst
, size_t dstSize
, const void *src
, size_t srcSize
, unsigned maxSymbolValue
, unsigned tableLog
, void *workSpace
,
58 size_t wkspSize
); /**< `workSpace` must be a table of at least HUF_COMPRESS_WORKSPACE_SIZE_U32 unsigned */
60 /* *** Dependencies *** */
61 #include "mem.h" /* U32 */
63 /* *** Constants *** */
64 #define HUF_TABLELOG_MAX 12 /* max configured tableLog (for static allocation); can be modified up to HUF_ABSOLUTEMAX_TABLELOG */
65 #define HUF_TABLELOG_DEFAULT 11 /* tableLog by default, when not specified */
66 #define HUF_SYMBOLVALUE_MAX 255
68 #define HUF_TABLELOG_ABSOLUTEMAX 15 /* absolute limit of HUF_MAX_TABLELOG. Beyond that value, code does not work */
69 #if (HUF_TABLELOG_MAX > HUF_TABLELOG_ABSOLUTEMAX)
70 #error "HUF_TABLELOG_MAX is too large !"
73 /* ****************************************
75 ******************************************/
76 /* HUF buffer bounds */
77 #define HUF_CTABLEBOUND 129
78 #define HUF_BLOCKBOUND(size) (size + (size >> 8) + 8) /* only true if incompressible pre-filtered with fast heuristic */
79 #define HUF_COMPRESSBOUND(size) (HUF_CTABLEBOUND + HUF_BLOCKBOUND(size)) /* Macro version, useful for static allocation */
81 /* static allocation of HUF's Compression Table */
82 #define HUF_CREATE_STATIC_CTABLE(name, maxSymbolValue) \
83 U32 name##hb[maxSymbolValue + 1]; \
84 void *name##hv = &(name##hb); \
85 HUF_CElt *name = (HUF_CElt *)(name##hv) /* no final ; */
87 /* static allocation of HUF's DTable */
88 typedef U32 HUF_DTable
;
89 #define HUF_DTABLE_SIZE(maxTableLog) (1 + (1 << (maxTableLog)))
90 #define HUF_CREATE_STATIC_DTABLEX2(DTable, maxTableLog) HUF_DTable DTable[HUF_DTABLE_SIZE((maxTableLog)-1)] = {((U32)((maxTableLog)-1) * 0x01000001)}
91 #define HUF_CREATE_STATIC_DTABLEX4(DTable, maxTableLog) HUF_DTable DTable[HUF_DTABLE_SIZE(maxTableLog)] = {((U32)(maxTableLog)*0x01000001)}
93 /* The workspace must have alignment at least 4 and be at least this large */
94 #define HUF_COMPRESS_WORKSPACE_SIZE (6 << 10)
95 #define HUF_COMPRESS_WORKSPACE_SIZE_U32 (HUF_COMPRESS_WORKSPACE_SIZE / sizeof(U32))
97 /* The workspace must have alignment at least 4 and be at least this large */
98 #define HUF_DECOMPRESS_WORKSPACE_SIZE (3 << 10)
99 #define HUF_DECOMPRESS_WORKSPACE_SIZE_U32 (HUF_DECOMPRESS_WORKSPACE_SIZE / sizeof(U32))
101 /* ****************************************
102 * Advanced decompression functions
103 ******************************************/
104 size_t HUF_decompress4X_DCtx_wksp(HUF_DTable
*dctx
, void *dst
, size_t dstSize
, const void *cSrc
, size_t cSrcSize
, void *workspace
, size_t workspaceSize
); /**< decodes RLE and uncompressed */
105 size_t HUF_decompress4X_hufOnly_wksp(HUF_DTable
*dctx
, void *dst
, size_t dstSize
, const void *cSrc
, size_t cSrcSize
, void *workspace
,
106 size_t workspaceSize
); /**< considers RLE and uncompressed as errors */
107 size_t HUF_decompress4X2_DCtx_wksp(HUF_DTable
*dctx
, void *dst
, size_t dstSize
, const void *cSrc
, size_t cSrcSize
, void *workspace
,
108 size_t workspaceSize
); /**< single-symbol decoder */
109 size_t HUF_decompress4X4_DCtx_wksp(HUF_DTable
*dctx
, void *dst
, size_t dstSize
, const void *cSrc
, size_t cSrcSize
, void *workspace
,
110 size_t workspaceSize
); /**< double-symbols decoder */
112 /* ****************************************
114 ******************************************/
116 HUF_compress() does the following:
117 1. count symbol occurrence from source[] into table count[] using FSE_count()
118 2. (optional) refine tableLog using HUF_optimalTableLog()
119 3. build Huffman table from count using HUF_buildCTable()
120 4. save Huffman table to memory buffer using HUF_writeCTable_wksp()
121 5. encode the data stream using HUF_compress4X_usingCTable()
123 The following API allows targeting specific sub-functions for advanced tasks.
124 For example, it's possible to compress several blocks using the same 'CTable',
125 or to save and regenerate 'CTable' using external methods.
127 /* FSE_count() : find it within "fse.h" */
128 unsigned HUF_optimalTableLog(unsigned maxTableLog
, size_t srcSize
, unsigned maxSymbolValue
);
129 typedef struct HUF_CElt_s HUF_CElt
; /* incomplete type */
130 size_t HUF_writeCTable_wksp(void *dst
, size_t maxDstSize
, const HUF_CElt
*CTable
, unsigned maxSymbolValue
, unsigned huffLog
, void *workspace
, size_t workspaceSize
);
131 size_t HUF_compress4X_usingCTable(void *dst
, size_t dstSize
, const void *src
, size_t srcSize
, const HUF_CElt
*CTable
);
134 HUF_repeat_none
, /**< Cannot use the previous table */
135 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,
137 HUF_repeat_valid
/**< Can use the previous table and it is asumed to be valid */
139 /** HUF_compress4X_repeat() :
140 * Same as HUF_compress4X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none.
141 * If it uses hufTable it does not modify hufTable or repeat.
142 * If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used.
143 * If preferRepeat then the old table will always be used if valid. */
144 size_t HUF_compress4X_repeat(void *dst
, size_t dstSize
, const void *src
, size_t srcSize
, unsigned maxSymbolValue
, unsigned tableLog
, void *workSpace
,
145 size_t wkspSize
, HUF_CElt
*hufTable
, HUF_repeat
*repeat
,
146 int preferRepeat
); /**< `workSpace` must be a table of at least HUF_COMPRESS_WORKSPACE_SIZE_U32 unsigned */
148 /** HUF_buildCTable_wksp() :
149 * Same as HUF_buildCTable(), but using externally allocated scratch buffer.
150 * `workSpace` must be aligned on 4-bytes boundaries, and be at least as large as a table of 1024 unsigned.
152 size_t HUF_buildCTable_wksp(HUF_CElt
*tree
, const U32
*count
, U32 maxSymbolValue
, U32 maxNbBits
, void *workSpace
, size_t wkspSize
);
154 /*! HUF_readStats() :
155 Read compact Huffman tree, saved by HUF_writeCTable().
156 `huffWeight` is destination buffer.
157 @return : size read from `src` , or an error Code .
158 Note : Needed by HUF_readCTable() and HUF_readDTableXn() . */
159 size_t HUF_readStats_wksp(BYTE
*huffWeight
, size_t hwSize
, U32
*rankStats
, U32
*nbSymbolsPtr
, U32
*tableLogPtr
, const void *src
, size_t srcSize
,
160 void *workspace
, size_t workspaceSize
);
162 /** HUF_readCTable() :
163 * Loading a CTable saved with HUF_writeCTable() */
164 size_t HUF_readCTable_wksp(HUF_CElt
*CTable
, unsigned maxSymbolValue
, const void *src
, size_t srcSize
, void *workspace
, size_t workspaceSize
);
167 HUF_decompress() does the following:
168 1. select the decompression algorithm (X2, X4) based on pre-computed heuristics
169 2. build Huffman table from save, using HUF_readDTableXn()
170 3. decode 1 or 4 segments in parallel using HUF_decompressSXn_usingDTable
173 /** HUF_selectDecoder() :
174 * Tells which decoder is likely to decode faster,
175 * based on a set of pre-determined metrics.
176 * @return : 0==HUF_decompress4X2, 1==HUF_decompress4X4 .
177 * Assumption : 0 < cSrcSize < dstSize <= 128 KB */
178 U32
HUF_selectDecoder(size_t dstSize
, size_t cSrcSize
);
180 size_t HUF_readDTableX2_wksp(HUF_DTable
*DTable
, const void *src
, size_t srcSize
, void *workspace
, size_t workspaceSize
);
181 size_t HUF_readDTableX4_wksp(HUF_DTable
*DTable
, const void *src
, size_t srcSize
, void *workspace
, size_t workspaceSize
);
183 size_t HUF_decompress4X_usingDTable(void *dst
, size_t maxDstSize
, const void *cSrc
, size_t cSrcSize
, const HUF_DTable
*DTable
);
184 size_t HUF_decompress4X2_usingDTable(void *dst
, size_t maxDstSize
, const void *cSrc
, size_t cSrcSize
, const HUF_DTable
*DTable
);
185 size_t HUF_decompress4X4_usingDTable(void *dst
, size_t maxDstSize
, const void *cSrc
, size_t cSrcSize
, const HUF_DTable
*DTable
);
187 /* single stream variants */
189 size_t HUF_compress1X_wksp(void *dst
, size_t dstSize
, const void *src
, size_t srcSize
, unsigned maxSymbolValue
, unsigned tableLog
, void *workSpace
,
190 size_t wkspSize
); /**< `workSpace` must be a table of at least HUF_COMPRESS_WORKSPACE_SIZE_U32 unsigned */
191 size_t HUF_compress1X_usingCTable(void *dst
, size_t dstSize
, const void *src
, size_t srcSize
, const HUF_CElt
*CTable
);
192 /** HUF_compress1X_repeat() :
193 * Same as HUF_compress1X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none.
194 * If it uses hufTable it does not modify hufTable or repeat.
195 * If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used.
196 * If preferRepeat then the old table will always be used if valid. */
197 size_t HUF_compress1X_repeat(void *dst
, size_t dstSize
, const void *src
, size_t srcSize
, unsigned maxSymbolValue
, unsigned tableLog
, void *workSpace
,
198 size_t wkspSize
, HUF_CElt
*hufTable
, HUF_repeat
*repeat
,
199 int preferRepeat
); /**< `workSpace` must be a table of at least HUF_COMPRESS_WORKSPACE_SIZE_U32 unsigned */
201 size_t HUF_decompress1X_DCtx_wksp(HUF_DTable
*dctx
, void *dst
, size_t dstSize
, const void *cSrc
, size_t cSrcSize
, void *workspace
, size_t workspaceSize
);
202 size_t HUF_decompress1X2_DCtx_wksp(HUF_DTable
*dctx
, void *dst
, size_t dstSize
, const void *cSrc
, size_t cSrcSize
, void *workspace
,
203 size_t workspaceSize
); /**< single-symbol decoder */
204 size_t HUF_decompress1X4_DCtx_wksp(HUF_DTable
*dctx
, void *dst
, size_t dstSize
, const void *cSrc
, size_t cSrcSize
, void *workspace
,
205 size_t workspaceSize
); /**< double-symbols decoder */
207 size_t HUF_decompress1X_usingDTable(void *dst
, size_t maxDstSize
, const void *cSrc
, size_t cSrcSize
,
208 const HUF_DTable
*DTable
); /**< automatic selection of sing or double symbol decoder, based on DTable */
209 size_t HUF_decompress1X2_usingDTable(void *dst
, size_t maxDstSize
, const void *cSrc
, size_t cSrcSize
, const HUF_DTable
*DTable
);
210 size_t HUF_decompress1X4_usingDTable(void *dst
, size_t maxDstSize
, const void *cSrc
, size_t cSrcSize
, const HUF_DTable
*DTable
);
212 #endif /* HUF_H_298734234 */