You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fse_decompress.c 9.8KB

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