fse_decompress.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. // SPDX-License-Identifier: (GPL-2.0 or BSD-2-Clause)
  2. /*
  3. * FSE : Finite State Entropy decoder
  4. * Copyright (C) 2013-2015, Yann Collet.
  5. *
  6. * You can contact the author at :
  7. * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
  8. */
  9. /* **************************************************************
  10. * Compiler specifics
  11. ****************************************************************/
  12. #define FORCE_INLINE static __always_inline
  13. /* **************************************************************
  14. * Includes
  15. ****************************************************************/
  16. #include "bitstream.h"
  17. #include "fse.h"
  18. #include <linux/compiler.h>
  19. #include <linux/kernel.h>
  20. #include <linux/string.h> /* memcpy, memset */
  21. /* **************************************************************
  22. * Error Management
  23. ****************************************************************/
  24. #define FSE_isError ERR_isError
  25. #define FSE_STATIC_ASSERT(c) \
  26. { \
  27. enum { FSE_static_assert = 1 / (int)(!!(c)) }; \
  28. } /* use only *after* variable declarations */
  29. /* check and forward error code */
  30. #define CHECK_F(f) \
  31. { \
  32. size_t const e = f; \
  33. if (FSE_isError(e)) \
  34. return e; \
  35. }
  36. /* **************************************************************
  37. * Templates
  38. ****************************************************************/
  39. /*
  40. designed to be included
  41. for type-specific functions (template emulation in C)
  42. Objective is to write these functions only once, for improved maintenance
  43. */
  44. /* safety checks */
  45. #ifndef FSE_FUNCTION_EXTENSION
  46. #error "FSE_FUNCTION_EXTENSION must be defined"
  47. #endif
  48. #ifndef FSE_FUNCTION_TYPE
  49. #error "FSE_FUNCTION_TYPE must be defined"
  50. #endif
  51. /* Function names */
  52. #define FSE_CAT(X, Y) X##Y
  53. #define FSE_FUNCTION_NAME(X, Y) FSE_CAT(X, Y)
  54. #define FSE_TYPE_NAME(X, Y) FSE_CAT(X, Y)
  55. /* Function templates */
  56. size_t FSE_buildDTable_wksp(FSE_DTable *dt, const short *normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void *workspace, size_t workspaceSize)
  57. {
  58. void *const tdPtr = dt + 1; /* because *dt is unsigned, 32-bits aligned on 32-bits */
  59. FSE_DECODE_TYPE *const tableDecode = (FSE_DECODE_TYPE *)(tdPtr);
  60. U16 *symbolNext = (U16 *)workspace;
  61. U32 const maxSV1 = maxSymbolValue + 1;
  62. U32 const tableSize = 1 << tableLog;
  63. U32 highThreshold = tableSize - 1;
  64. /* Sanity Checks */
  65. if (workspaceSize < sizeof(U16) * (FSE_MAX_SYMBOL_VALUE + 1))
  66. return ERROR(tableLog_tooLarge);
  67. if (maxSymbolValue > FSE_MAX_SYMBOL_VALUE)
  68. return ERROR(maxSymbolValue_tooLarge);
  69. if (tableLog > FSE_MAX_TABLELOG)
  70. return ERROR(tableLog_tooLarge);
  71. /* Init, lay down lowprob symbols */
  72. {
  73. FSE_DTableHeader DTableH;
  74. DTableH.tableLog = (U16)tableLog;
  75. DTableH.fastMode = 1;
  76. {
  77. S16 const largeLimit = (S16)(1 << (tableLog - 1));
  78. U32 s;
  79. for (s = 0; s < maxSV1; s++) {
  80. if (normalizedCounter[s] == -1) {
  81. tableDecode[highThreshold--].symbol = (FSE_FUNCTION_TYPE)s;
  82. symbolNext[s] = 1;
  83. } else {
  84. if (normalizedCounter[s] >= largeLimit)
  85. DTableH.fastMode = 0;
  86. symbolNext[s] = normalizedCounter[s];
  87. }
  88. }
  89. }
  90. memcpy(dt, &DTableH, sizeof(DTableH));
  91. }
  92. /* Spread symbols */
  93. {
  94. U32 const tableMask = tableSize - 1;
  95. U32 const step = FSE_TABLESTEP(tableSize);
  96. U32 s, position = 0;
  97. for (s = 0; s < maxSV1; s++) {
  98. int i;
  99. for (i = 0; i < normalizedCounter[s]; i++) {
  100. tableDecode[position].symbol = (FSE_FUNCTION_TYPE)s;
  101. position = (position + step) & tableMask;
  102. while (position > highThreshold)
  103. position = (position + step) & tableMask; /* lowprob area */
  104. }
  105. }
  106. if (position != 0)
  107. return ERROR(GENERIC); /* position must reach all cells once, otherwise normalizedCounter is incorrect */
  108. }
  109. /* Build Decoding table */
  110. {
  111. U32 u;
  112. for (u = 0; u < tableSize; u++) {
  113. FSE_FUNCTION_TYPE const symbol = (FSE_FUNCTION_TYPE)(tableDecode[u].symbol);
  114. U16 nextState = symbolNext[symbol]++;
  115. tableDecode[u].nbBits = (BYTE)(tableLog - BIT_highbit32((U32)nextState));
  116. tableDecode[u].newState = (U16)((nextState << tableDecode[u].nbBits) - tableSize);
  117. }
  118. }
  119. return 0;
  120. }
  121. /*-*******************************************************
  122. * Decompression (Byte symbols)
  123. *********************************************************/
  124. size_t FSE_buildDTable_rle(FSE_DTable *dt, BYTE symbolValue)
  125. {
  126. void *ptr = dt;
  127. FSE_DTableHeader *const DTableH = (FSE_DTableHeader *)ptr;
  128. void *dPtr = dt + 1;
  129. FSE_decode_t *const cell = (FSE_decode_t *)dPtr;
  130. DTableH->tableLog = 0;
  131. DTableH->fastMode = 0;
  132. cell->newState = 0;
  133. cell->symbol = symbolValue;
  134. cell->nbBits = 0;
  135. return 0;
  136. }
  137. size_t FSE_buildDTable_raw(FSE_DTable *dt, unsigned nbBits)
  138. {
  139. void *ptr = dt;
  140. FSE_DTableHeader *const DTableH = (FSE_DTableHeader *)ptr;
  141. void *dPtr = dt + 1;
  142. FSE_decode_t *const dinfo = (FSE_decode_t *)dPtr;
  143. const unsigned tableSize = 1 << nbBits;
  144. const unsigned tableMask = tableSize - 1;
  145. const unsigned maxSV1 = tableMask + 1;
  146. unsigned s;
  147. /* Sanity checks */
  148. if (nbBits < 1)
  149. return ERROR(GENERIC); /* min size */
  150. /* Build Decoding Table */
  151. DTableH->tableLog = (U16)nbBits;
  152. DTableH->fastMode = 1;
  153. for (s = 0; s < maxSV1; s++) {
  154. dinfo[s].newState = 0;
  155. dinfo[s].symbol = (BYTE)s;
  156. dinfo[s].nbBits = (BYTE)nbBits;
  157. }
  158. return 0;
  159. }
  160. FORCE_INLINE size_t FSE_decompress_usingDTable_generic(void *dst, size_t maxDstSize, const void *cSrc, size_t cSrcSize, const FSE_DTable *dt,
  161. const unsigned fast)
  162. {
  163. BYTE *const ostart = (BYTE *)dst;
  164. BYTE *op = ostart;
  165. BYTE *const omax = op + maxDstSize;
  166. BYTE *const olimit = omax - 3;
  167. BIT_DStream_t bitD;
  168. FSE_DState_t state1;
  169. FSE_DState_t state2;
  170. /* Init */
  171. CHECK_F(BIT_initDStream(&bitD, cSrc, cSrcSize));
  172. FSE_initDState(&state1, &bitD, dt);
  173. FSE_initDState(&state2, &bitD, dt);
  174. #define FSE_GETSYMBOL(statePtr) fast ? FSE_decodeSymbolFast(statePtr, &bitD) : FSE_decodeSymbol(statePtr, &bitD)
  175. /* 4 symbols per loop */
  176. for (; (BIT_reloadDStream(&bitD) == BIT_DStream_unfinished) & (op < olimit); op += 4) {
  177. op[0] = FSE_GETSYMBOL(&state1);
  178. if (FSE_MAX_TABLELOG * 2 + 7 > sizeof(bitD.bitContainer) * 8) /* This test must be static */
  179. BIT_reloadDStream(&bitD);
  180. op[1] = FSE_GETSYMBOL(&state2);
  181. if (FSE_MAX_TABLELOG * 4 + 7 > sizeof(bitD.bitContainer) * 8) /* This test must be static */
  182. {
  183. if (BIT_reloadDStream(&bitD) > BIT_DStream_unfinished) {
  184. op += 2;
  185. break;
  186. }
  187. }
  188. op[2] = FSE_GETSYMBOL(&state1);
  189. if (FSE_MAX_TABLELOG * 2 + 7 > sizeof(bitD.bitContainer) * 8) /* This test must be static */
  190. BIT_reloadDStream(&bitD);
  191. op[3] = FSE_GETSYMBOL(&state2);
  192. }
  193. /* tail */
  194. /* note : BIT_reloadDStream(&bitD) >= FSE_DStream_partiallyFilled; Ends at exactly BIT_DStream_completed */
  195. while (1) {
  196. if (op > (omax - 2))
  197. return ERROR(dstSize_tooSmall);
  198. *op++ = FSE_GETSYMBOL(&state1);
  199. if (BIT_reloadDStream(&bitD) == BIT_DStream_overflow) {
  200. *op++ = FSE_GETSYMBOL(&state2);
  201. break;
  202. }
  203. if (op > (omax - 2))
  204. return ERROR(dstSize_tooSmall);
  205. *op++ = FSE_GETSYMBOL(&state2);
  206. if (BIT_reloadDStream(&bitD) == BIT_DStream_overflow) {
  207. *op++ = FSE_GETSYMBOL(&state1);
  208. break;
  209. }
  210. }
  211. return op - ostart;
  212. }
  213. size_t FSE_decompress_usingDTable(void *dst, size_t originalSize, const void *cSrc, size_t cSrcSize, const FSE_DTable *dt)
  214. {
  215. const void *ptr = dt;
  216. const FSE_DTableHeader *DTableH = (const FSE_DTableHeader *)ptr;
  217. const U32 fastMode = DTableH->fastMode;
  218. /* select fast mode (static) */
  219. if (fastMode)
  220. return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 1);
  221. return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 0);
  222. }
  223. size_t FSE_decompress_wksp(void *dst, size_t dstCapacity, const void *cSrc, size_t cSrcSize, unsigned maxLog, void *workspace, size_t workspaceSize)
  224. {
  225. const BYTE *const istart = (const BYTE *)cSrc;
  226. const BYTE *ip = istart;
  227. unsigned tableLog;
  228. unsigned maxSymbolValue = FSE_MAX_SYMBOL_VALUE;
  229. size_t NCountLength;
  230. FSE_DTable *dt;
  231. short *counting;
  232. size_t spaceUsed32 = 0;
  233. FSE_STATIC_ASSERT(sizeof(FSE_DTable) == sizeof(U32));
  234. dt = (FSE_DTable *)((U32 *)workspace + spaceUsed32);
  235. spaceUsed32 += FSE_DTABLE_SIZE_U32(maxLog);
  236. counting = (short *)((U32 *)workspace + spaceUsed32);
  237. spaceUsed32 += ALIGN(sizeof(short) * (FSE_MAX_SYMBOL_VALUE + 1), sizeof(U32)) >> 2;
  238. if ((spaceUsed32 << 2) > workspaceSize)
  239. return ERROR(tableLog_tooLarge);
  240. workspace = (U32 *)workspace + spaceUsed32;
  241. workspaceSize -= (spaceUsed32 << 2);
  242. /* normal FSE decoding mode */
  243. NCountLength = FSE_readNCount(counting, &maxSymbolValue, &tableLog, istart, cSrcSize);
  244. if (FSE_isError(NCountLength))
  245. return NCountLength;
  246. // if (NCountLength >= cSrcSize) return ERROR(srcSize_wrong); /* too small input size; supposed to be already checked in NCountLength, only remaining
  247. // case : NCountLength==cSrcSize */
  248. if (tableLog > maxLog)
  249. return ERROR(tableLog_tooLarge);
  250. ip += NCountLength;
  251. cSrcSize -= NCountLength;
  252. CHECK_F(FSE_buildDTable_wksp(dt, counting, maxSymbolValue, tableLog, workspace, workspaceSize));
  253. return FSE_decompress_usingDTable(dst, dstCapacity, ip, cSrcSize, dt); /* always return, even if it is an error code */
  254. }