huf_static.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #include <contrib/libs/zstd06/renames.h>
  2. /* ******************************************************************
  3. Huffman codec, part of New Generation Entropy library
  4. header file, for static linking only
  5. Copyright (C) 2013-2016, Yann Collet
  6. BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
  7. Redistribution and use in source and binary forms, with or without
  8. modification, are permitted provided that the following conditions are
  9. met:
  10. * Redistributions of source code must retain the above copyright
  11. notice, this list of conditions and the following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the following disclaimer
  14. in the documentation and/or other materials provided with the
  15. distribution.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  20. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. You can contact the author at :
  28. - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
  29. ****************************************************************** */
  30. #ifndef HUF_STATIC_H
  31. #define HUF_STATIC_H
  32. #if defined (__cplusplus)
  33. extern "C" {
  34. #endif
  35. /* ****************************************
  36. * Dependency
  37. ******************************************/
  38. #include "huf.h"
  39. #include "fse.h"
  40. #include "bitstream.h"
  41. /* ****************************************
  42. * Static allocation
  43. ******************************************/
  44. /* HUF buffer bounds */
  45. #define HUF_CTABLEBOUND 129
  46. #define HUF_BLOCKBOUND(size) (size + (size>>8) + 8) /* only true if incompressible pre-filtered with fast heuristic */
  47. #define HUF_COMPRESSBOUND(size) (HUF_CTABLEBOUND + HUF_BLOCKBOUND(size)) /* Macro version, useful for static allocation */
  48. /* static allocation of HUF's Compression Table */
  49. #define HUF_CREATE_STATIC_CTABLE(name, maxSymbolValue) \
  50. U32 name##hb[maxSymbolValue+1]; \
  51. void* name##hv = &(name##hb); \
  52. HUF_CElt* name = (HUF_CElt*)(name##hv) /* no final ; */
  53. /* static allocation of HUF's DTable */
  54. #define HUF_DTABLE_SIZE(maxTableLog) (1 + (1<<maxTableLog))
  55. #define HUF_CREATE_STATIC_DTABLEX2(DTable, maxTableLog) \
  56. unsigned short DTable[HUF_DTABLE_SIZE(maxTableLog)] = { maxTableLog }
  57. #define HUF_CREATE_STATIC_DTABLEX4(DTable, maxTableLog) \
  58. unsigned int DTable[HUF_DTABLE_SIZE(maxTableLog)] = { maxTableLog }
  59. #define HUF_CREATE_STATIC_DTABLEX6(DTable, maxTableLog) \
  60. unsigned int DTable[HUF_DTABLE_SIZE(maxTableLog) * 3 / 2] = { maxTableLog }
  61. /* ****************************************
  62. * Advanced decompression functions
  63. ******************************************/
  64. size_t HUF_decompress4X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* single-symbol decoder */
  65. size_t HUF_decompress4X4 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* double-symbols decoder */
  66. size_t HUF_decompress4X6 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* quad-symbols decoder, only works for dstSize >= 64 */
  67. /* ****************************************
  68. * HUF detailed API
  69. ******************************************/
  70. /*!
  71. HUF_compress() does the following:
  72. 1. count symbol occurrence from source[] into table count[] using FSE_count()
  73. 2. build Huffman table from count using HUF_buildCTable()
  74. 3. save Huffman table to memory buffer using HUF_writeCTable()
  75. 4. encode the data stream using HUF_compress4X_usingCTable()
  76. The following API allows targeting specific sub-functions for advanced tasks.
  77. For example, it's possible to compress several blocks using the same 'CTable',
  78. or to save and regenerate 'CTable' using external methods.
  79. */
  80. /* FSE_count() : find it within "fse.h" */
  81. typedef struct HUF_CElt_s HUF_CElt; /* incomplete type */
  82. size_t HUF_buildCTable (HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue, unsigned maxNbBits);
  83. size_t HUF_writeCTable (void* dst, size_t maxDstSize, const HUF_CElt* CTable, unsigned maxSymbolValue, unsigned huffLog);
  84. size_t HUF_compress4X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable);
  85. /*!
  86. HUF_decompress() does the following:
  87. 1. select the decompression algorithm (X2, X4, X6) based on pre-computed heuristics
  88. 2. build Huffman table from save, using HUF_readDTableXn()
  89. 3. decode 1 or 4 segments in parallel using HUF_decompressSXn_usingDTable
  90. */
  91. size_t HUF_readDTableX2 (unsigned short* DTable, const void* src, size_t srcSize);
  92. size_t HUF_readDTableX4 (unsigned* DTable, const void* src, size_t srcSize);
  93. size_t HUF_readDTableX6 (unsigned* DTable, const void* src, size_t srcSize);
  94. size_t HUF_decompress4X2_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const unsigned short* DTable);
  95. size_t HUF_decompress4X4_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const unsigned* DTable);
  96. size_t HUF_decompress4X6_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const unsigned* DTable);
  97. /* single stream variants */
  98. size_t HUF_compress1X (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog);
  99. size_t HUF_compress1X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable);
  100. size_t HUF_decompress1X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* single-symbol decoder */
  101. size_t HUF_decompress1X4 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* double-symbol decoder */
  102. size_t HUF_decompress1X6 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* quad-symbols decoder, only works for dstSize >= 64 */
  103. size_t HUF_decompress1X2_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const unsigned short* DTable);
  104. size_t HUF_decompress1X4_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const unsigned* DTable);
  105. size_t HUF_decompress1X6_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const unsigned* DTable);
  106. /* Loading a CTable saved with HUF_writeCTable() */
  107. size_t HUF_readCTable (HUF_CElt* CTable, unsigned maxSymbolValue, const void* src, size_t srcSize);
  108. /* **************************************************************
  109. * Constants
  110. ****************************************************************/
  111. #define HUF_ABSOLUTEMAX_TABLELOG 16 /* absolute limit of HUF_MAX_TABLELOG. Beyond that value, code does not work */
  112. #define HUF_MAX_TABLELOG 12 /* max configured tableLog (for static allocation); can be modified up to HUF_ABSOLUTEMAX_TABLELOG */
  113. #define HUF_DEFAULT_TABLELOG HUF_MAX_TABLELOG /* tableLog by default, when not specified */
  114. #define HUF_MAX_SYMBOL_VALUE 255
  115. #if (HUF_MAX_TABLELOG > HUF_ABSOLUTEMAX_TABLELOG)
  116. # error "HUF_MAX_TABLELOG is too large !"
  117. #endif
  118. /*! HUF_readStats() :
  119. Read compact Huffman tree, saved by HUF_writeCTable().
  120. `huffWeight` is destination buffer.
  121. @return : size read from `src`
  122. */
  123. MEM_STATIC size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
  124. U32* nbSymbolsPtr, U32* tableLogPtr,
  125. const void* src, size_t srcSize)
  126. {
  127. U32 weightTotal;
  128. const BYTE* ip = (const BYTE*) src;
  129. size_t iSize = ip[0];
  130. size_t oSize;
  131. //memset(huffWeight, 0, hwSize); /* is not necessary, even though some analyzer complain ... */
  132. if (iSize >= 128) { /* special header */
  133. if (iSize >= (242)) { /* RLE */
  134. static U32 l[14] = { 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64, 127, 128 };
  135. oSize = l[iSize-242];
  136. memset(huffWeight, 1, hwSize);
  137. iSize = 0;
  138. }
  139. else { /* Incompressible */
  140. oSize = iSize - 127;
  141. iSize = ((oSize+1)/2);
  142. if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
  143. if (oSize >= hwSize) return ERROR(corruption_detected);
  144. ip += 1;
  145. { U32 n;
  146. for (n=0; n<oSize; n+=2) {
  147. huffWeight[n] = ip[n/2] >> 4;
  148. huffWeight[n+1] = ip[n/2] & 15;
  149. } } } }
  150. else { /* header compressed with FSE (normal case) */
  151. if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
  152. oSize = FSE_decompress(huffWeight, hwSize-1, ip+1, iSize); /* max (hwSize-1) values decoded, as last one is implied */
  153. if (FSE_isError(oSize)) return oSize;
  154. }
  155. /* collect weight stats */
  156. memset(rankStats, 0, (HUF_ABSOLUTEMAX_TABLELOG + 1) * sizeof(U32));
  157. weightTotal = 0;
  158. { U32 n; for (n=0; n<oSize; n++) {
  159. if (huffWeight[n] >= HUF_ABSOLUTEMAX_TABLELOG) return ERROR(corruption_detected);
  160. rankStats[huffWeight[n]]++;
  161. weightTotal += (1 << huffWeight[n]) >> 1;
  162. } }
  163. /* get last non-null symbol weight (implied, total must be 2^n) */
  164. { U32 const tableLog = BIT_highbit32(weightTotal) + 1;
  165. if (tableLog > HUF_ABSOLUTEMAX_TABLELOG) return ERROR(corruption_detected);
  166. *tableLogPtr = tableLog;
  167. /* determine last weight */
  168. { U32 const total = 1 << tableLog;
  169. U32 const rest = total - weightTotal;
  170. U32 const verif = 1 << BIT_highbit32(rest);
  171. U32 const lastWeight = BIT_highbit32(rest) + 1;
  172. if (verif != rest) return ERROR(corruption_detected); /* last value must be a clean power of 2 */
  173. huffWeight[oSize] = (BYTE)lastWeight;
  174. rankStats[lastWeight]++;
  175. } }
  176. /* check tree construction validity */
  177. if ((rankStats[1] < 2) || (rankStats[1] & 1)) return ERROR(corruption_detected); /* by construction : at least 2 elts of rank 1, must be even */
  178. /* results */
  179. *nbSymbolsPtr = (U32)(oSize+1);
  180. return iSize+1;
  181. }
  182. #if defined (__cplusplus)
  183. }
  184. #endif
  185. #endif /* HUF_STATIC_H */