Marek BehĂșn | 8509f22 | 2019-04-29 22:40:44 +0200 | [diff] [blame^] | 1 | /* SPDX-License-Identifier: (GPL-2.0 or BSD-2-Clause) */ |
| 2 | /* |
| 3 | * Huffman coder, part of New Generation Entropy library |
| 4 | * header file |
| 5 | * Copyright (C) 2013-2016, Yann Collet. |
| 6 | * |
| 7 | * You can contact the author at : |
| 8 | * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy |
| 9 | */ |
| 10 | #ifndef HUF_H_298734234 |
| 11 | #define HUF_H_298734234 |
| 12 | |
| 13 | /* *** Dependencies *** */ |
| 14 | #include <linux/types.h> /* size_t */ |
| 15 | |
| 16 | /* *** Tool functions *** */ |
| 17 | #define HUF_BLOCKSIZE_MAX (128 * 1024) /**< maximum input size for a single block compressed with HUF_compress */ |
| 18 | size_t HUF_compressBound(size_t size); /**< maximum compressed size (worst case) */ |
| 19 | |
| 20 | /* Error Management */ |
| 21 | unsigned HUF_isError(size_t code); /**< tells if a return value is an error code */ |
| 22 | |
| 23 | /* *** Advanced function *** */ |
| 24 | |
| 25 | /** HUF_compress4X_wksp() : |
| 26 | * Same as HUF_compress2(), but uses externally allocated `workSpace`, which must be a table of >= 1024 unsigned */ |
| 27 | size_t HUF_compress4X_wksp(void *dst, size_t dstSize, const void *src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void *workSpace, |
| 28 | size_t wkspSize); /**< `workSpace` must be a table of at least HUF_COMPRESS_WORKSPACE_SIZE_U32 unsigned */ |
| 29 | |
| 30 | /* *** Dependencies *** */ |
| 31 | #include "mem.h" /* U32 */ |
| 32 | |
| 33 | /* *** Constants *** */ |
| 34 | #define HUF_TABLELOG_MAX 12 /* max configured tableLog (for static allocation); can be modified up to HUF_ABSOLUTEMAX_TABLELOG */ |
| 35 | #define HUF_TABLELOG_DEFAULT 11 /* tableLog by default, when not specified */ |
| 36 | #define HUF_SYMBOLVALUE_MAX 255 |
| 37 | |
| 38 | #define HUF_TABLELOG_ABSOLUTEMAX 15 /* absolute limit of HUF_MAX_TABLELOG. Beyond that value, code does not work */ |
| 39 | #if (HUF_TABLELOG_MAX > HUF_TABLELOG_ABSOLUTEMAX) |
| 40 | #error "HUF_TABLELOG_MAX is too large !" |
| 41 | #endif |
| 42 | |
| 43 | /* **************************************** |
| 44 | * Static allocation |
| 45 | ******************************************/ |
| 46 | /* HUF buffer bounds */ |
| 47 | #define HUF_CTABLEBOUND 129 |
| 48 | #define HUF_BLOCKBOUND(size) (size + (size >> 8) + 8) /* only true if incompressible pre-filtered with fast heuristic */ |
| 49 | #define HUF_COMPRESSBOUND(size) (HUF_CTABLEBOUND + HUF_BLOCKBOUND(size)) /* Macro version, useful for static allocation */ |
| 50 | |
| 51 | /* static allocation of HUF's Compression Table */ |
| 52 | #define HUF_CREATE_STATIC_CTABLE(name, maxSymbolValue) \ |
| 53 | U32 name##hb[maxSymbolValue + 1]; \ |
| 54 | void *name##hv = &(name##hb); \ |
| 55 | HUF_CElt *name = (HUF_CElt *)(name##hv) /* no final ; */ |
| 56 | |
| 57 | /* static allocation of HUF's DTable */ |
| 58 | typedef U32 HUF_DTable; |
| 59 | #define HUF_DTABLE_SIZE(maxTableLog) (1 + (1 << (maxTableLog))) |
| 60 | #define HUF_CREATE_STATIC_DTABLEX2(DTable, maxTableLog) HUF_DTable DTable[HUF_DTABLE_SIZE((maxTableLog)-1)] = {((U32)((maxTableLog)-1) * 0x01000001)} |
| 61 | #define HUF_CREATE_STATIC_DTABLEX4(DTable, maxTableLog) HUF_DTable DTable[HUF_DTABLE_SIZE(maxTableLog)] = {((U32)(maxTableLog)*0x01000001)} |
| 62 | |
| 63 | /* The workspace must have alignment at least 4 and be at least this large */ |
| 64 | #define HUF_COMPRESS_WORKSPACE_SIZE (6 << 10) |
| 65 | #define HUF_COMPRESS_WORKSPACE_SIZE_U32 (HUF_COMPRESS_WORKSPACE_SIZE / sizeof(U32)) |
| 66 | |
| 67 | /* The workspace must have alignment at least 4 and be at least this large */ |
| 68 | #define HUF_DECOMPRESS_WORKSPACE_SIZE (3 << 10) |
| 69 | #define HUF_DECOMPRESS_WORKSPACE_SIZE_U32 (HUF_DECOMPRESS_WORKSPACE_SIZE / sizeof(U32)) |
| 70 | |
| 71 | /* **************************************** |
| 72 | * Advanced decompression functions |
| 73 | ******************************************/ |
| 74 | 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 */ |
| 75 | size_t HUF_decompress4X_hufOnly_wksp(HUF_DTable *dctx, void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, void *workspace, |
| 76 | size_t workspaceSize); /**< considers RLE and uncompressed as errors */ |
| 77 | size_t HUF_decompress4X2_DCtx_wksp(HUF_DTable *dctx, void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, void *workspace, |
| 78 | size_t workspaceSize); /**< single-symbol decoder */ |
| 79 | size_t HUF_decompress4X4_DCtx_wksp(HUF_DTable *dctx, void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, void *workspace, |
| 80 | size_t workspaceSize); /**< double-symbols decoder */ |
| 81 | |
| 82 | /* **************************************** |
| 83 | * HUF detailed API |
| 84 | ******************************************/ |
| 85 | /*! |
| 86 | HUF_compress() does the following: |
| 87 | 1. count symbol occurrence from source[] into table count[] using FSE_count() |
| 88 | 2. (optional) refine tableLog using HUF_optimalTableLog() |
| 89 | 3. build Huffman table from count using HUF_buildCTable() |
| 90 | 4. save Huffman table to memory buffer using HUF_writeCTable_wksp() |
| 91 | 5. encode the data stream using HUF_compress4X_usingCTable() |
| 92 | |
| 93 | The following API allows targeting specific sub-functions for advanced tasks. |
| 94 | For example, it's possible to compress several blocks using the same 'CTable', |
| 95 | or to save and regenerate 'CTable' using external methods. |
| 96 | */ |
| 97 | /* FSE_count() : find it within "fse.h" */ |
| 98 | unsigned HUF_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue); |
| 99 | typedef struct HUF_CElt_s HUF_CElt; /* incomplete type */ |
| 100 | size_t HUF_writeCTable_wksp(void *dst, size_t maxDstSize, const HUF_CElt *CTable, unsigned maxSymbolValue, unsigned huffLog, void *workspace, size_t workspaceSize); |
| 101 | size_t HUF_compress4X_usingCTable(void *dst, size_t dstSize, const void *src, size_t srcSize, const HUF_CElt *CTable); |
| 102 | |
| 103 | typedef enum { |
| 104 | HUF_repeat_none, /**< Cannot use the previous table */ |
| 105 | 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, |
| 106 | 4}X_repeat */ |
| 107 | HUF_repeat_valid /**< Can use the previous table and it is asumed to be valid */ |
| 108 | } HUF_repeat; |
| 109 | /** HUF_compress4X_repeat() : |
| 110 | * Same as HUF_compress4X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none. |
| 111 | * If it uses hufTable it does not modify hufTable or repeat. |
| 112 | * If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used. |
| 113 | * If preferRepeat then the old table will always be used if valid. */ |
| 114 | size_t HUF_compress4X_repeat(void *dst, size_t dstSize, const void *src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void *workSpace, |
| 115 | size_t wkspSize, HUF_CElt *hufTable, HUF_repeat *repeat, |
| 116 | int preferRepeat); /**< `workSpace` must be a table of at least HUF_COMPRESS_WORKSPACE_SIZE_U32 unsigned */ |
| 117 | |
| 118 | /** HUF_buildCTable_wksp() : |
| 119 | * Same as HUF_buildCTable(), but using externally allocated scratch buffer. |
| 120 | * `workSpace` must be aligned on 4-bytes boundaries, and be at least as large as a table of 1024 unsigned. |
| 121 | */ |
| 122 | size_t HUF_buildCTable_wksp(HUF_CElt *tree, const U32 *count, U32 maxSymbolValue, U32 maxNbBits, void *workSpace, size_t wkspSize); |
| 123 | |
| 124 | /*! HUF_readStats() : |
| 125 | Read compact Huffman tree, saved by HUF_writeCTable(). |
| 126 | `huffWeight` is destination buffer. |
| 127 | @return : size read from `src` , or an error Code . |
| 128 | Note : Needed by HUF_readCTable() and HUF_readDTableXn() . */ |
| 129 | size_t HUF_readStats_wksp(BYTE *huffWeight, size_t hwSize, U32 *rankStats, U32 *nbSymbolsPtr, U32 *tableLogPtr, const void *src, size_t srcSize, |
| 130 | void *workspace, size_t workspaceSize); |
| 131 | |
| 132 | /** HUF_readCTable() : |
| 133 | * Loading a CTable saved with HUF_writeCTable() */ |
| 134 | size_t HUF_readCTable_wksp(HUF_CElt *CTable, unsigned maxSymbolValue, const void *src, size_t srcSize, void *workspace, size_t workspaceSize); |
| 135 | |
| 136 | /* |
| 137 | HUF_decompress() does the following: |
| 138 | 1. select the decompression algorithm (X2, X4) based on pre-computed heuristics |
| 139 | 2. build Huffman table from save, using HUF_readDTableXn() |
| 140 | 3. decode 1 or 4 segments in parallel using HUF_decompressSXn_usingDTable |
| 141 | */ |
| 142 | |
| 143 | /** HUF_selectDecoder() : |
| 144 | * Tells which decoder is likely to decode faster, |
| 145 | * based on a set of pre-determined metrics. |
| 146 | * @return : 0==HUF_decompress4X2, 1==HUF_decompress4X4 . |
| 147 | * Assumption : 0 < cSrcSize < dstSize <= 128 KB */ |
| 148 | U32 HUF_selectDecoder(size_t dstSize, size_t cSrcSize); |
| 149 | |
| 150 | size_t HUF_readDTableX2_wksp(HUF_DTable *DTable, const void *src, size_t srcSize, void *workspace, size_t workspaceSize); |
| 151 | size_t HUF_readDTableX4_wksp(HUF_DTable *DTable, const void *src, size_t srcSize, void *workspace, size_t workspaceSize); |
| 152 | |
| 153 | size_t HUF_decompress4X_usingDTable(void *dst, size_t maxDstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable); |
| 154 | size_t HUF_decompress4X2_usingDTable(void *dst, size_t maxDstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable); |
| 155 | size_t HUF_decompress4X4_usingDTable(void *dst, size_t maxDstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable); |
| 156 | |
| 157 | /* single stream variants */ |
| 158 | |
| 159 | size_t HUF_compress1X_wksp(void *dst, size_t dstSize, const void *src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void *workSpace, |
| 160 | size_t wkspSize); /**< `workSpace` must be a table of at least HUF_COMPRESS_WORKSPACE_SIZE_U32 unsigned */ |
| 161 | size_t HUF_compress1X_usingCTable(void *dst, size_t dstSize, const void *src, size_t srcSize, const HUF_CElt *CTable); |
| 162 | /** HUF_compress1X_repeat() : |
| 163 | * Same as HUF_compress1X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none. |
| 164 | * If it uses hufTable it does not modify hufTable or repeat. |
| 165 | * If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used. |
| 166 | * If preferRepeat then the old table will always be used if valid. */ |
| 167 | size_t HUF_compress1X_repeat(void *dst, size_t dstSize, const void *src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void *workSpace, |
| 168 | size_t wkspSize, HUF_CElt *hufTable, HUF_repeat *repeat, |
| 169 | int preferRepeat); /**< `workSpace` must be a table of at least HUF_COMPRESS_WORKSPACE_SIZE_U32 unsigned */ |
| 170 | |
| 171 | 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); |
| 172 | size_t HUF_decompress1X2_DCtx_wksp(HUF_DTable *dctx, void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, void *workspace, |
| 173 | size_t workspaceSize); /**< single-symbol decoder */ |
| 174 | size_t HUF_decompress1X4_DCtx_wksp(HUF_DTable *dctx, void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, void *workspace, |
| 175 | size_t workspaceSize); /**< double-symbols decoder */ |
| 176 | |
| 177 | size_t HUF_decompress1X_usingDTable(void *dst, size_t maxDstSize, const void *cSrc, size_t cSrcSize, |
| 178 | const HUF_DTable *DTable); /**< automatic selection of sing or double symbol decoder, based on DTable */ |
| 179 | size_t HUF_decompress1X2_usingDTable(void *dst, size_t maxDstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable); |
| 180 | size_t HUF_decompress1X4_usingDTable(void *dst, size_t maxDstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable); |
| 181 | |
| 182 | #endif /* HUF_H_298734234 */ |