fse_decompress.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /* ******************************************************************
  2. FSE : Finite State Entropy decoder
  3. Copyright (C) 2013-2015, Yann Collet.
  4. BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions are
  7. met:
  8. * Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above
  11. copyright notice, this list of conditions and the following disclaimer
  12. in the documentation and/or other materials provided with the
  13. distribution.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  18. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  20. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. You can contact the author at :
  26. - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy
  27. - Public forum : https://groups.google.com/forum/#!forum/lz4c
  28. ****************************************************************** */
  29. /* **************************************************************
  30. * Includes
  31. ****************************************************************/
  32. #include <stdlib.h> /* malloc, free, qsort */
  33. #include <string.h> /* memcpy, memset */
  34. #include "bitstream.h"
  35. #include "compiler.h"
  36. #define FSE_STATIC_LINKING_ONLY
  37. #include "fse.h"
  38. #include "error_private.h"
  39. /* **************************************************************
  40. * Error Management
  41. ****************************************************************/
  42. #define FSE_isError ERR_isError
  43. #define FSE_STATIC_ASSERT(c) DEBUG_STATIC_ASSERT(c) /* use only *after* variable declarations */
  44. /* check and forward error code */
  45. #define CHECK_F(f) { size_t const e = f; if (FSE_isError(e)) return e; }
  46. /* **************************************************************
  47. * Templates
  48. ****************************************************************/
  49. /*
  50. designed to be included
  51. for type-specific functions (template emulation in C)
  52. Objective is to write these functions only once, for improved maintenance
  53. */
  54. /* safety checks */
  55. #ifndef FSE_FUNCTION_EXTENSION
  56. # error "FSE_FUNCTION_EXTENSION must be defined"
  57. #endif
  58. #ifndef FSE_FUNCTION_TYPE
  59. # error "FSE_FUNCTION_TYPE must be defined"
  60. #endif
  61. /* Function names */
  62. #define FSE_CAT(X,Y) X##Y
  63. #define FSE_FUNCTION_NAME(X,Y) FSE_CAT(X,Y)
  64. #define FSE_TYPE_NAME(X,Y) FSE_CAT(X,Y)
  65. /* Function templates */
  66. FSE_DTable* FSE_createDTable (unsigned tableLog)
  67. {
  68. if (tableLog > FSE_TABLELOG_ABSOLUTE_MAX) tableLog = FSE_TABLELOG_ABSOLUTE_MAX;
  69. return (FSE_DTable*)calloc( FSE_DTABLE_SIZE_U32(tableLog), sizeof (U32) );
  70. }
  71. void FSE_freeDTable (FSE_DTable* dt)
  72. {
  73. free(dt);
  74. }
  75. size_t FSE_buildDTable(FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog)
  76. {
  77. void* const tdPtr = dt+1; /* because *dt is unsigned, 32-bits aligned on 32-bits */
  78. FSE_DECODE_TYPE* const tableDecode = (FSE_DECODE_TYPE*) (tdPtr);
  79. U16 symbolNext[FSE_MAX_SYMBOL_VALUE+1];
  80. U32 const maxSV1 = maxSymbolValue + 1;
  81. U32 const tableSize = 1 << tableLog;
  82. U32 highThreshold = tableSize-1;
  83. /* Sanity Checks */
  84. if (maxSymbolValue > FSE_MAX_SYMBOL_VALUE) return ERROR(maxSymbolValue_tooLarge);
  85. if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge);
  86. /* Init, lay down lowprob symbols */
  87. { FSE_DTableHeader DTableH;
  88. DTableH.tableLog = (U16)tableLog;
  89. DTableH.fastMode = 1;
  90. { S16 const largeLimit= (S16)(1 << (tableLog-1));
  91. U32 s;
  92. for (s=0; s<maxSV1; s++) {
  93. if (normalizedCounter[s]==-1) {
  94. tableDecode[highThreshold--].symbol = (FSE_FUNCTION_TYPE)s;
  95. symbolNext[s] = 1;
  96. } else {
  97. if (normalizedCounter[s] >= largeLimit) DTableH.fastMode=0;
  98. symbolNext[s] = normalizedCounter[s];
  99. } } }
  100. memcpy(dt, &DTableH, sizeof(DTableH));
  101. }
  102. /* Spread symbols */
  103. { U32 const tableMask = tableSize-1;
  104. U32 const step = FSE_TABLESTEP(tableSize);
  105. U32 s, position = 0;
  106. for (s=0; s<maxSV1; s++) {
  107. int i;
  108. for (i=0; i<normalizedCounter[s]; i++) {
  109. tableDecode[position].symbol = (FSE_FUNCTION_TYPE)s;
  110. position = (position + step) & tableMask;
  111. while (position > highThreshold) position = (position + step) & tableMask; /* lowprob area */
  112. } }
  113. if (position!=0) return ERROR(GENERIC); /* position must reach all cells once, otherwise normalizedCounter is incorrect */
  114. }
  115. /* Build Decoding table */
  116. { U32 u;
  117. for (u=0; u<tableSize; u++) {
  118. FSE_FUNCTION_TYPE const symbol = (FSE_FUNCTION_TYPE)(tableDecode[u].symbol);
  119. U32 const nextState = symbolNext[symbol]++;
  120. tableDecode[u].nbBits = (BYTE) (tableLog - BIT_highbit32(nextState) );
  121. tableDecode[u].newState = (U16) ( (nextState << tableDecode[u].nbBits) - tableSize);
  122. } }
  123. return 0;
  124. }
  125. #ifndef FSE_COMMONDEFS_ONLY
  126. /*-*******************************************************
  127. * Decompression (Byte symbols)
  128. *********************************************************/
  129. size_t FSE_buildDTable_rle (FSE_DTable* dt, BYTE symbolValue)
  130. {
  131. void* ptr = dt;
  132. FSE_DTableHeader* const DTableH = (FSE_DTableHeader*)ptr;
  133. void* dPtr = dt + 1;
  134. FSE_decode_t* const cell = (FSE_decode_t*)dPtr;
  135. DTableH->tableLog = 0;
  136. DTableH->fastMode = 0;
  137. cell->newState = 0;
  138. cell->symbol = symbolValue;
  139. cell->nbBits = 0;
  140. return 0;
  141. }
  142. size_t FSE_buildDTable_raw (FSE_DTable* dt, unsigned nbBits)
  143. {
  144. void* ptr = dt;
  145. FSE_DTableHeader* const DTableH = (FSE_DTableHeader*)ptr;
  146. void* dPtr = dt + 1;
  147. FSE_decode_t* const dinfo = (FSE_decode_t*)dPtr;
  148. const unsigned tableSize = 1 << nbBits;
  149. const unsigned tableMask = tableSize - 1;
  150. const unsigned maxSV1 = tableMask+1;
  151. unsigned s;
  152. /* Sanity checks */
  153. if (nbBits < 1) return ERROR(GENERIC); /* min size */
  154. /* Build Decoding Table */
  155. DTableH->tableLog = (U16)nbBits;
  156. DTableH->fastMode = 1;
  157. for (s=0; s<maxSV1; s++) {
  158. dinfo[s].newState = 0;
  159. dinfo[s].symbol = (BYTE)s;
  160. dinfo[s].nbBits = (BYTE)nbBits;
  161. }
  162. return 0;
  163. }
  164. FORCE_INLINE_TEMPLATE size_t FSE_decompress_usingDTable_generic(
  165. void* dst, size_t maxDstSize,
  166. const void* cSrc, size_t cSrcSize,
  167. const FSE_DTable* dt, const unsigned fast)
  168. {
  169. BYTE* const ostart = (BYTE*) dst;
  170. BYTE* op = ostart;
  171. BYTE* const omax = op + maxDstSize;
  172. BYTE* const olimit = omax-3;
  173. BIT_DStream_t bitD;
  174. FSE_DState_t state1;
  175. FSE_DState_t state2;
  176. /* Init */
  177. CHECK_F(BIT_initDStream(&bitD, cSrc, cSrcSize));
  178. FSE_initDState(&state1, &bitD, dt);
  179. FSE_initDState(&state2, &bitD, dt);
  180. #define FSE_GETSYMBOL(statePtr) fast ? FSE_decodeSymbolFast(statePtr, &bitD) : FSE_decodeSymbol(statePtr, &bitD)
  181. /* 4 symbols per loop */
  182. for ( ; (BIT_reloadDStream(&bitD)==BIT_DStream_unfinished) & (op<olimit) ; op+=4) {
  183. op[0] = FSE_GETSYMBOL(&state1);
  184. if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */
  185. BIT_reloadDStream(&bitD);
  186. op[1] = FSE_GETSYMBOL(&state2);
  187. if (FSE_MAX_TABLELOG*4+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */
  188. { if (BIT_reloadDStream(&bitD) > BIT_DStream_unfinished) { op+=2; break; } }
  189. op[2] = FSE_GETSYMBOL(&state1);
  190. if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */
  191. BIT_reloadDStream(&bitD);
  192. op[3] = FSE_GETSYMBOL(&state2);
  193. }
  194. /* tail */
  195. /* note : BIT_reloadDStream(&bitD) >= FSE_DStream_partiallyFilled; Ends at exactly BIT_DStream_completed */
  196. while (1) {
  197. if (op>(omax-2)) 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)) return ERROR(dstSize_tooSmall);
  204. *op++ = FSE_GETSYMBOL(&state2);
  205. if (BIT_reloadDStream(&bitD)==BIT_DStream_overflow) {
  206. *op++ = FSE_GETSYMBOL(&state1);
  207. break;
  208. } }
  209. return op-ostart;
  210. }
  211. size_t FSE_decompress_usingDTable(void* dst, size_t originalSize,
  212. const void* cSrc, size_t cSrcSize,
  213. 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) return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 1);
  220. return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 0);
  221. }
  222. size_t FSE_decompress_wksp(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, FSE_DTable* workSpace, unsigned maxLog)
  223. {
  224. const BYTE* const istart = (const BYTE*)cSrc;
  225. const BYTE* ip = istart;
  226. short counting[FSE_MAX_SYMBOL_VALUE+1];
  227. unsigned tableLog;
  228. unsigned maxSymbolValue = FSE_MAX_SYMBOL_VALUE;
  229. /* normal FSE decoding mode */
  230. size_t const NCountLength = FSE_readNCount (counting, &maxSymbolValue, &tableLog, istart, cSrcSize);
  231. if (FSE_isError(NCountLength)) return NCountLength;
  232. //if (NCountLength >= cSrcSize) return ERROR(srcSize_wrong); /* too small input size; supposed to be already checked in NCountLength, only remaining case : NCountLength==cSrcSize */
  233. if (tableLog > maxLog) return ERROR(tableLog_tooLarge);
  234. ip += NCountLength;
  235. cSrcSize -= NCountLength;
  236. CHECK_F( FSE_buildDTable (workSpace, counting, maxSymbolValue, tableLog) );
  237. return FSE_decompress_usingDTable (dst, dstCapacity, ip, cSrcSize, workSpace); /* always return, even if it is an error code */
  238. }
  239. typedef FSE_DTable DTable_max_t[FSE_DTABLE_SIZE_U32(FSE_MAX_TABLELOG)];
  240. size_t FSE_decompress(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize)
  241. {
  242. DTable_max_t dt; /* Static analyzer seems unable to understand this table will be properly initialized later */
  243. return FSE_decompress_wksp(dst, dstCapacity, cSrc, cSrcSize, dt, FSE_MAX_TABLELOG);
  244. }
  245. #endif /* FSE_COMMONDEFS_ONLY */