20#define FSE_STATIC_LINKING_ONLY
43 const void* headerBuffer,
size_t hbSize)
45 const BYTE*
const istart = (
const BYTE*) headerBuffer;
46 const BYTE*
const iend = istart + hbSize;
47 const BYTE* ip = istart;
54 unsigned const maxSV1 = *maxSVPtr + 1;
61 {
size_t const countSize =
FSE_readNCount(normalizedCounter, maxSVPtr, tableLogPtr,
62 buffer,
sizeof(buffer));
64 if (countSize > hbSize)
return ERROR(corruption_detected);
70 ZSTD_memset(normalizedCounter, 0, (*maxSVPtr+1) *
sizeof(normalizedCounter[0]));
72 nbBits = (bitStream & 0xF) + FSE_MIN_TABLELOG;
73 if (nbBits > FSE_TABLELOG_ABSOLUTE_MAX)
return ERROR(tableLog_tooLarge);
76 *tableLogPtr = nbBits;
77 remaining = (1<<nbBits)+1;
78 threshold = 1<<nbBits;
89 while (repeats >= 12) {
91 if (
LIKELY(ip <= iend-7)) {
94 bitCount -= (int)(8 * (iend - 7 - ip));
101 charnum += 3 * repeats;
102 bitStream >>= 2 * repeats;
103 bitCount += 2 * repeats;
106 assert((bitStream & 3) < 3);
107 charnum += bitStream & 3;
114 if (charnum >= maxSV1)
break;
120 if (
LIKELY(ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
121 assert((bitCount >> 3) <= 3);
125 bitCount -= (int)(8 * (iend - 4 - ip));
132 int const max = (2*threshold-1) - remaining;
135 if ((bitStream & (threshold-1)) < (
U32)max) {
136 count = bitStream & (threshold-1);
137 bitCount += nbBits-1;
139 count = bitStream & (2*threshold-1);
140 if (count >= threshold) count -= max;
154 normalizedCounter[charnum++] = (short)count;
158 if (remaining < threshold) {
163 if (remaining <= 1)
break;
165 threshold = 1 << (nbBits - 1);
167 if (charnum >= maxSV1)
break;
169 if (
LIKELY(ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
173 bitCount -= (int)(8 * (iend - 4 - ip));
179 if (remaining != 1)
return ERROR(corruption_detected);
181 if (charnum > maxSV1)
return ERROR(maxSymbolValue_tooSmall);
182 if (bitCount > 32)
return ERROR(corruption_detected);
183 *maxSVPtr = charnum-1;
185 ip += (bitCount+7)>>3;
190static size_t FSE_readNCount_body_default(
191 short* normalizedCounter,
unsigned* maxSVPtr,
unsigned* tableLogPtr,
192 const void* headerBuffer,
size_t hbSize)
194 return FSE_readNCount_body(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize);
199 short* normalizedCounter,
unsigned* maxSVPtr,
unsigned* tableLogPtr,
200 const void* headerBuffer,
size_t hbSize)
202 return FSE_readNCount_body(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize);
207 short* normalizedCounter,
unsigned* maxSVPtr,
unsigned* tableLogPtr,
208 const void* headerBuffer,
size_t hbSize,
int bmi2)
212 return FSE_readNCount_body_bmi2(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize);
216 return FSE_readNCount_body_default(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize);
220 short* normalizedCounter,
unsigned* maxSVPtr,
unsigned* tableLogPtr,
221 const void* headerBuffer,
size_t hbSize)
223 return FSE_readNCount_bmi2(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize, 0);
235 U32* nbSymbolsPtr,
U32* tableLogPtr,
236 const void* src,
size_t srcSize)
239 return HUF_readStats_wksp(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, wksp,
sizeof(wksp), 0);
244 U32* nbSymbolsPtr,
U32* tableLogPtr,
245 const void* src,
size_t srcSize,
246 void* workSpace,
size_t wkspSize,
254 if (!srcSize)
return ERROR(srcSize_wrong);
260 iSize = ((oSize+1)/2);
261 if (iSize+1 > srcSize)
return ERROR(srcSize_wrong);
262 if (oSize >= hwSize)
return ERROR(corruption_detected);
265 for (n=0; n<oSize; n+=2) {
266 huffWeight[n] = ip[n/2] >> 4;
267 huffWeight[n+1] = ip[n/2] & 15;
270 if (iSize+1 > srcSize)
return ERROR(srcSize_wrong);
279 {
U32 n;
for (n=0; n<oSize; n++) {
281 rankStats[huffWeight[n]]++;
282 weightTotal += (1 << huffWeight[n]) >> 1;
284 if (weightTotal == 0)
return ERROR(corruption_detected);
289 *tableLogPtr = tableLog;
291 {
U32 const total = 1 << tableLog;
292 U32 const rest = total - weightTotal;
295 if (verif != rest)
return ERROR(corruption_detected);
296 huffWeight[oSize] = (
BYTE)lastWeight;
297 rankStats[lastWeight]++;
301 if ((rankStats[1] < 2) || (rankStats[1] & 1))
return ERROR(corruption_detected);
304 *nbSymbolsPtr = (
U32)(oSize+1);
309static size_t HUF_readStats_body_default(
BYTE* huffWeight,
size_t hwSize,
U32* rankStats,
310 U32* nbSymbolsPtr,
U32* tableLogPtr,
311 const void* src,
size_t srcSize,
312 void* workSpace,
size_t wkspSize)
314 return HUF_readStats_body(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize, 0);
319 U32* nbSymbolsPtr,
U32* tableLogPtr,
320 const void* src,
size_t srcSize,
321 void* workSpace,
size_t wkspSize)
323 return HUF_readStats_body(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize, 1);
328 U32* nbSymbolsPtr,
U32* tableLogPtr,
329 const void* src,
size_t srcSize,
330 void* workSpace,
size_t wkspSize,
335 return HUF_readStats_body_bmi2(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize);
339 return HUF_readStats_body_default(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize);
MEM_STATIC unsigned ZSTD_highbit32(U32 val)
MEM_STATIC unsigned ZSTD_countTrailingZeros32(U32 val)
#define BMI2_TARGET_ATTRIBUTE
#define FORCE_INLINE_TEMPLATE
#define assert(condition)
FORCE_INLINE_TEMPLATE size_t HUF_readStats_body(BYTE *huffWeight, size_t hwSize, U32 *rankStats, U32 *nbSymbolsPtr, U32 *tableLogPtr, const void *src, size_t srcSize, void *workSpace, size_t wkspSize, int bmi2)
size_t HUF_readStats_wksp(BYTE *huffWeight, size_t hwSize, U32 *rankStats, U32 *nbSymbolsPtr, U32 *tableLogPtr, const void *src, size_t srcSize, void *workSpace, size_t wkspSize, int flags)
const char * HUF_getErrorName(size_t code)
FORCE_INLINE_TEMPLATE size_t FSE_readNCount_body(short *normalizedCounter, unsigned *maxSVPtr, unsigned *tableLogPtr, const void *headerBuffer, size_t hbSize)
size_t FSE_readNCount_bmi2(short *normalizedCounter, unsigned *maxSVPtr, unsigned *tableLogPtr, const void *headerBuffer, size_t hbSize, int bmi2)
unsigned FSE_versionNumber(void)
size_t FSE_readNCount(short *normalizedCounter, unsigned *maxSVPtr, unsigned *tableLogPtr, const void *headerBuffer, size_t hbSize)
size_t HUF_readStats(BYTE *huffWeight, size_t hwSize, U32 *rankStats, U32 *nbSymbolsPtr, U32 *tableLogPtr, const void *src, size_t srcSize)
const char * FSE_getErrorName(size_t code)
ERR_STATIC const char * ERR_getErrorName(size_t code)
#define FSE_VERSION_NUMBER
size_t FSE_decompress_wksp_bmi2(void *dst, size_t dstCapacity, const void *cSrc, size_t cSrcSize, unsigned maxLog, void *workSpace, size_t wkspSize, int bmi2)
#define HUF_READ_STATS_WORKSPACE_SIZE_U32
MEM_STATIC U32 MEM_readLE32(const void *memPtr)
#define ZSTD_memcpy(d, s, l)
#define ZSTD_memset(p, v, l)