18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Huffman coder, part of New Generation Entropy library 38c2ecf20Sopenharmony_ci * header file 48c2ecf20Sopenharmony_ci * Copyright (C) 2013-2016, Yann Collet. 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or without 98c2ecf20Sopenharmony_ci * modification, are permitted provided that the following conditions are 108c2ecf20Sopenharmony_ci * met: 118c2ecf20Sopenharmony_ci * 128c2ecf20Sopenharmony_ci * * Redistributions of source code must retain the above copyright 138c2ecf20Sopenharmony_ci * notice, this list of conditions and the following disclaimer. 148c2ecf20Sopenharmony_ci * * Redistributions in binary form must reproduce the above 158c2ecf20Sopenharmony_ci * copyright notice, this list of conditions and the following disclaimer 168c2ecf20Sopenharmony_ci * in the documentation and/or other materials provided with the 178c2ecf20Sopenharmony_ci * distribution. 188c2ecf20Sopenharmony_ci * 198c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 208c2ecf20Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 218c2ecf20Sopenharmony_ci * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 228c2ecf20Sopenharmony_ci * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 238c2ecf20Sopenharmony_ci * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 248c2ecf20Sopenharmony_ci * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 258c2ecf20Sopenharmony_ci * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 268c2ecf20Sopenharmony_ci * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 278c2ecf20Sopenharmony_ci * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 288c2ecf20Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 298c2ecf20Sopenharmony_ci * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 308c2ecf20Sopenharmony_ci * 318c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or modify it under 328c2ecf20Sopenharmony_ci * the terms of the GNU General Public License version 2 as published by the 338c2ecf20Sopenharmony_ci * Free Software Foundation. This program is dual-licensed; you may select 348c2ecf20Sopenharmony_ci * either version 2 of the GNU General Public License ("GPL") or BSD license 358c2ecf20Sopenharmony_ci * ("BSD"). 368c2ecf20Sopenharmony_ci * 378c2ecf20Sopenharmony_ci * You can contact the author at : 388c2ecf20Sopenharmony_ci * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy 398c2ecf20Sopenharmony_ci */ 408c2ecf20Sopenharmony_ci#ifndef HUF_H_298734234 418c2ecf20Sopenharmony_ci#define HUF_H_298734234 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci/* *** Dependencies *** */ 448c2ecf20Sopenharmony_ci#include <linux/types.h> /* size_t */ 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci/* *** Tool functions *** */ 478c2ecf20Sopenharmony_ci#define HUF_BLOCKSIZE_MAX (128 * 1024) /**< maximum input size for a single block compressed with HUF_compress */ 488c2ecf20Sopenharmony_cisize_t HUF_compressBound(size_t size); /**< maximum compressed size (worst case) */ 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci/* Error Management */ 518c2ecf20Sopenharmony_ciunsigned HUF_isError(size_t code); /**< tells if a return value is an error code */ 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci/* *** Advanced function *** */ 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci/** HUF_compress4X_wksp() : 568c2ecf20Sopenharmony_ci* Same as HUF_compress2(), but uses externally allocated `workSpace`, which must be a table of >= 1024 unsigned */ 578c2ecf20Sopenharmony_cisize_t HUF_compress4X_wksp(void *dst, size_t dstSize, const void *src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void *workSpace, 588c2ecf20Sopenharmony_ci size_t wkspSize); /**< `workSpace` must be a table of at least HUF_COMPRESS_WORKSPACE_SIZE_U32 unsigned */ 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci/* *** Dependencies *** */ 618c2ecf20Sopenharmony_ci#include "mem.h" /* U32 */ 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci/* *** Constants *** */ 648c2ecf20Sopenharmony_ci#define HUF_TABLELOG_MAX 12 /* max configured tableLog (for static allocation); can be modified up to HUF_ABSOLUTEMAX_TABLELOG */ 658c2ecf20Sopenharmony_ci#define HUF_TABLELOG_DEFAULT 11 /* tableLog by default, when not specified */ 668c2ecf20Sopenharmony_ci#define HUF_SYMBOLVALUE_MAX 255 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci#define HUF_TABLELOG_ABSOLUTEMAX 15 /* absolute limit of HUF_MAX_TABLELOG. Beyond that value, code does not work */ 698c2ecf20Sopenharmony_ci#if (HUF_TABLELOG_MAX > HUF_TABLELOG_ABSOLUTEMAX) 708c2ecf20Sopenharmony_ci#error "HUF_TABLELOG_MAX is too large !" 718c2ecf20Sopenharmony_ci#endif 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci/* **************************************** 748c2ecf20Sopenharmony_ci* Static allocation 758c2ecf20Sopenharmony_ci******************************************/ 768c2ecf20Sopenharmony_ci/* HUF buffer bounds */ 778c2ecf20Sopenharmony_ci#define HUF_CTABLEBOUND 129 788c2ecf20Sopenharmony_ci#define HUF_BLOCKBOUND(size) (size + (size >> 8) + 8) /* only true if incompressible pre-filtered with fast heuristic */ 798c2ecf20Sopenharmony_ci#define HUF_COMPRESSBOUND(size) (HUF_CTABLEBOUND + HUF_BLOCKBOUND(size)) /* Macro version, useful for static allocation */ 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci/* static allocation of HUF's Compression Table */ 828c2ecf20Sopenharmony_ci#define HUF_CREATE_STATIC_CTABLE(name, maxSymbolValue) \ 838c2ecf20Sopenharmony_ci U32 name##hb[maxSymbolValue + 1]; \ 848c2ecf20Sopenharmony_ci void *name##hv = &(name##hb); \ 858c2ecf20Sopenharmony_ci HUF_CElt *name = (HUF_CElt *)(name##hv) /* no final ; */ 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci/* static allocation of HUF's DTable */ 888c2ecf20Sopenharmony_citypedef U32 HUF_DTable; 898c2ecf20Sopenharmony_ci#define HUF_DTABLE_SIZE(maxTableLog) (1 + (1 << (maxTableLog))) 908c2ecf20Sopenharmony_ci#define HUF_CREATE_STATIC_DTABLEX2(DTable, maxTableLog) HUF_DTable DTable[HUF_DTABLE_SIZE((maxTableLog)-1)] = {((U32)((maxTableLog)-1) * 0x01000001)} 918c2ecf20Sopenharmony_ci#define HUF_CREATE_STATIC_DTABLEX4(DTable, maxTableLog) HUF_DTable DTable[HUF_DTABLE_SIZE(maxTableLog)] = {((U32)(maxTableLog)*0x01000001)} 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci/* The workspace must have alignment at least 4 and be at least this large */ 948c2ecf20Sopenharmony_ci#define HUF_COMPRESS_WORKSPACE_SIZE (6 << 10) 958c2ecf20Sopenharmony_ci#define HUF_COMPRESS_WORKSPACE_SIZE_U32 (HUF_COMPRESS_WORKSPACE_SIZE / sizeof(U32)) 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci/* The workspace must have alignment at least 4 and be at least this large */ 988c2ecf20Sopenharmony_ci#define HUF_DECOMPRESS_WORKSPACE_SIZE (3 << 10) 998c2ecf20Sopenharmony_ci#define HUF_DECOMPRESS_WORKSPACE_SIZE_U32 (HUF_DECOMPRESS_WORKSPACE_SIZE / sizeof(U32)) 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci/* **************************************** 1028c2ecf20Sopenharmony_ci* Advanced decompression functions 1038c2ecf20Sopenharmony_ci******************************************/ 1048c2ecf20Sopenharmony_cisize_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 */ 1058c2ecf20Sopenharmony_cisize_t HUF_decompress4X_hufOnly_wksp(HUF_DTable *dctx, void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, void *workspace, 1068c2ecf20Sopenharmony_ci size_t workspaceSize); /**< considers RLE and uncompressed as errors */ 1078c2ecf20Sopenharmony_cisize_t HUF_decompress4X2_DCtx_wksp(HUF_DTable *dctx, void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, void *workspace, 1088c2ecf20Sopenharmony_ci size_t workspaceSize); /**< single-symbol decoder */ 1098c2ecf20Sopenharmony_cisize_t HUF_decompress4X4_DCtx_wksp(HUF_DTable *dctx, void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, void *workspace, 1108c2ecf20Sopenharmony_ci size_t workspaceSize); /**< double-symbols decoder */ 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci/* **************************************** 1138c2ecf20Sopenharmony_ci* HUF detailed API 1148c2ecf20Sopenharmony_ci******************************************/ 1158c2ecf20Sopenharmony_ci/*! 1168c2ecf20Sopenharmony_ciHUF_compress() does the following: 1178c2ecf20Sopenharmony_ci1. count symbol occurrence from source[] into table count[] using FSE_count() 1188c2ecf20Sopenharmony_ci2. (optional) refine tableLog using HUF_optimalTableLog() 1198c2ecf20Sopenharmony_ci3. build Huffman table from count using HUF_buildCTable() 1208c2ecf20Sopenharmony_ci4. save Huffman table to memory buffer using HUF_writeCTable_wksp() 1218c2ecf20Sopenharmony_ci5. encode the data stream using HUF_compress4X_usingCTable() 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ciThe following API allows targeting specific sub-functions for advanced tasks. 1248c2ecf20Sopenharmony_ciFor example, it's possible to compress several blocks using the same 'CTable', 1258c2ecf20Sopenharmony_cior to save and regenerate 'CTable' using external methods. 1268c2ecf20Sopenharmony_ci*/ 1278c2ecf20Sopenharmony_ci/* FSE_count() : find it within "fse.h" */ 1288c2ecf20Sopenharmony_ciunsigned HUF_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue); 1298c2ecf20Sopenharmony_citypedef struct HUF_CElt_s HUF_CElt; /* incomplete type */ 1308c2ecf20Sopenharmony_cisize_t HUF_writeCTable_wksp(void *dst, size_t maxDstSize, const HUF_CElt *CTable, unsigned maxSymbolValue, unsigned huffLog, void *workspace, size_t workspaceSize); 1318c2ecf20Sopenharmony_cisize_t HUF_compress4X_usingCTable(void *dst, size_t dstSize, const void *src, size_t srcSize, const HUF_CElt *CTable); 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_citypedef enum { 1348c2ecf20Sopenharmony_ci HUF_repeat_none, /**< Cannot use the previous table */ 1358c2ecf20Sopenharmony_ci 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, 1368c2ecf20Sopenharmony_ci 4}X_repeat */ 1378c2ecf20Sopenharmony_ci HUF_repeat_valid /**< Can use the previous table and it is asumed to be valid */ 1388c2ecf20Sopenharmony_ci} HUF_repeat; 1398c2ecf20Sopenharmony_ci/** HUF_compress4X_repeat() : 1408c2ecf20Sopenharmony_ci* Same as HUF_compress4X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none. 1418c2ecf20Sopenharmony_ci* If it uses hufTable it does not modify hufTable or repeat. 1428c2ecf20Sopenharmony_ci* If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used. 1438c2ecf20Sopenharmony_ci* If preferRepeat then the old table will always be used if valid. */ 1448c2ecf20Sopenharmony_cisize_t HUF_compress4X_repeat(void *dst, size_t dstSize, const void *src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void *workSpace, 1458c2ecf20Sopenharmony_ci size_t wkspSize, HUF_CElt *hufTable, HUF_repeat *repeat, 1468c2ecf20Sopenharmony_ci int preferRepeat); /**< `workSpace` must be a table of at least HUF_COMPRESS_WORKSPACE_SIZE_U32 unsigned */ 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci/** HUF_buildCTable_wksp() : 1498c2ecf20Sopenharmony_ci * Same as HUF_buildCTable(), but using externally allocated scratch buffer. 1508c2ecf20Sopenharmony_ci * `workSpace` must be aligned on 4-bytes boundaries, and be at least as large as a table of 1024 unsigned. 1518c2ecf20Sopenharmony_ci */ 1528c2ecf20Sopenharmony_cisize_t HUF_buildCTable_wksp(HUF_CElt *tree, const U32 *count, U32 maxSymbolValue, U32 maxNbBits, void *workSpace, size_t wkspSize); 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci/*! HUF_readStats() : 1558c2ecf20Sopenharmony_ci Read compact Huffman tree, saved by HUF_writeCTable(). 1568c2ecf20Sopenharmony_ci `huffWeight` is destination buffer. 1578c2ecf20Sopenharmony_ci @return : size read from `src` , or an error Code . 1588c2ecf20Sopenharmony_ci Note : Needed by HUF_readCTable() and HUF_readDTableXn() . */ 1598c2ecf20Sopenharmony_cisize_t HUF_readStats_wksp(BYTE *huffWeight, size_t hwSize, U32 *rankStats, U32 *nbSymbolsPtr, U32 *tableLogPtr, const void *src, size_t srcSize, 1608c2ecf20Sopenharmony_ci void *workspace, size_t workspaceSize); 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci/** HUF_readCTable() : 1638c2ecf20Sopenharmony_ci* Loading a CTable saved with HUF_writeCTable() */ 1648c2ecf20Sopenharmony_cisize_t HUF_readCTable_wksp(HUF_CElt *CTable, unsigned maxSymbolValue, const void *src, size_t srcSize, void *workspace, size_t workspaceSize); 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci/* 1678c2ecf20Sopenharmony_ciHUF_decompress() does the following: 1688c2ecf20Sopenharmony_ci1. select the decompression algorithm (X2, X4) based on pre-computed heuristics 1698c2ecf20Sopenharmony_ci2. build Huffman table from save, using HUF_readDTableXn() 1708c2ecf20Sopenharmony_ci3. decode 1 or 4 segments in parallel using HUF_decompressSXn_usingDTable 1718c2ecf20Sopenharmony_ci*/ 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci/** HUF_selectDecoder() : 1748c2ecf20Sopenharmony_ci* Tells which decoder is likely to decode faster, 1758c2ecf20Sopenharmony_ci* based on a set of pre-determined metrics. 1768c2ecf20Sopenharmony_ci* @return : 0==HUF_decompress4X2, 1==HUF_decompress4X4 . 1778c2ecf20Sopenharmony_ci* Assumption : 0 < cSrcSize < dstSize <= 128 KB */ 1788c2ecf20Sopenharmony_ciU32 HUF_selectDecoder(size_t dstSize, size_t cSrcSize); 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_cisize_t HUF_readDTableX2_wksp(HUF_DTable *DTable, const void *src, size_t srcSize, void *workspace, size_t workspaceSize); 1818c2ecf20Sopenharmony_cisize_t HUF_readDTableX4_wksp(HUF_DTable *DTable, const void *src, size_t srcSize, void *workspace, size_t workspaceSize); 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_cisize_t HUF_decompress4X_usingDTable(void *dst, size_t maxDstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable); 1848c2ecf20Sopenharmony_cisize_t HUF_decompress4X2_usingDTable(void *dst, size_t maxDstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable); 1858c2ecf20Sopenharmony_cisize_t HUF_decompress4X4_usingDTable(void *dst, size_t maxDstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable); 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_ci/* single stream variants */ 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_cisize_t HUF_compress1X_wksp(void *dst, size_t dstSize, const void *src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void *workSpace, 1908c2ecf20Sopenharmony_ci size_t wkspSize); /**< `workSpace` must be a table of at least HUF_COMPRESS_WORKSPACE_SIZE_U32 unsigned */ 1918c2ecf20Sopenharmony_cisize_t HUF_compress1X_usingCTable(void *dst, size_t dstSize, const void *src, size_t srcSize, const HUF_CElt *CTable); 1928c2ecf20Sopenharmony_ci/** HUF_compress1X_repeat() : 1938c2ecf20Sopenharmony_ci* Same as HUF_compress1X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none. 1948c2ecf20Sopenharmony_ci* If it uses hufTable it does not modify hufTable or repeat. 1958c2ecf20Sopenharmony_ci* If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used. 1968c2ecf20Sopenharmony_ci* If preferRepeat then the old table will always be used if valid. */ 1978c2ecf20Sopenharmony_cisize_t HUF_compress1X_repeat(void *dst, size_t dstSize, const void *src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void *workSpace, 1988c2ecf20Sopenharmony_ci size_t wkspSize, HUF_CElt *hufTable, HUF_repeat *repeat, 1998c2ecf20Sopenharmony_ci int preferRepeat); /**< `workSpace` must be a table of at least HUF_COMPRESS_WORKSPACE_SIZE_U32 unsigned */ 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_cisize_t HUF_decompress1X_DCtx_wksp(HUF_DTable *dctx, void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, void *workspace, size_t workspaceSize); 2028c2ecf20Sopenharmony_cisize_t HUF_decompress1X2_DCtx_wksp(HUF_DTable *dctx, void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, void *workspace, 2038c2ecf20Sopenharmony_ci size_t workspaceSize); /**< single-symbol decoder */ 2048c2ecf20Sopenharmony_cisize_t HUF_decompress1X4_DCtx_wksp(HUF_DTable *dctx, void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, void *workspace, 2058c2ecf20Sopenharmony_ci size_t workspaceSize); /**< double-symbols decoder */ 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_cisize_t HUF_decompress1X_usingDTable(void *dst, size_t maxDstSize, const void *cSrc, size_t cSrcSize, 2088c2ecf20Sopenharmony_ci const HUF_DTable *DTable); /**< automatic selection of sing or double symbol decoder, based on DTable */ 2098c2ecf20Sopenharmony_cisize_t HUF_decompress1X2_usingDTable(void *dst, size_t maxDstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable); 2108c2ecf20Sopenharmony_cisize_t HUF_decompress1X4_usingDTable(void *dst, size_t maxDstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable); 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_ci#endif /* HUF_H_298734234 */ 213