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