in csharp/src/ZStdDecompress.cs [2378:2450]
static size_t LoadEntropy(ZSTD_entropyDTables_t entropy, void* dict, size_t dictSize)
{
BYTE* dictPtr = (BYTE*)dict;
BYTE* dictEnd = dictPtr + dictSize;
if (dictSize <= 8) return ERROR(Error.dictionary_corrupted);
dictPtr += 8; /* skip header = magic + dictID */
{
size_t hSize = HUF_readDTableX4_wksp(
entropy.hufTable, dictPtr, (size_t)(dictEnd - dictPtr),
entropy.workspace, sizeof(U32) * HUF_DECOMPRESS_WORKSPACE_SIZE_U32);
if (IsError(hSize)) return ERROR(Error.dictionary_corrupted);
dictPtr += hSize;
}
{
short[] offcodeNCount = new short[MaxOff + 1];
U32 offcodeMaxValue = MaxOff, offcodeLog;
size_t offcodeHeaderSize = ReadNCount(offcodeNCount, &offcodeMaxValue, &offcodeLog, dictPtr, (size_t)(dictEnd - dictPtr));
if (IsError(offcodeHeaderSize)) return ERROR(Error.dictionary_corrupted);
if (offcodeMaxValue > MaxOff) return ERROR(Error.dictionary_corrupted);
if (offcodeLog > OffFSELog) return ERROR(Error.dictionary_corrupted);
BuildFSETable(entropy.OFTable,
offcodeNCount, offcodeMaxValue,
OF_base, OF_bits,
offcodeLog);
dictPtr += offcodeHeaderSize;
}
{
short[] matchlengthNCount = new short[MaxML + 1];
uint matchlengthMaxValue = MaxML, matchlengthLog;
size_t matchlengthHeaderSize = ReadNCount(matchlengthNCount, &matchlengthMaxValue, &matchlengthLog, dictPtr, (size_t)(dictEnd - dictPtr));
if (IsError(matchlengthHeaderSize)) return ERROR(Error.dictionary_corrupted);
if (matchlengthMaxValue > MaxML) return ERROR(Error.dictionary_corrupted);
if (matchlengthLog > MLFSELog) return ERROR(Error.dictionary_corrupted);
BuildFSETable(entropy.MLTable,
matchlengthNCount, matchlengthMaxValue,
ML_base, ML_bits,
matchlengthLog);
dictPtr += matchlengthHeaderSize;
}
{
short[] litlengthNCount = new short[MaxLL + 1];
uint litlengthMaxValue = MaxLL, litlengthLog;
size_t litlengthHeaderSize = ReadNCount(litlengthNCount, &litlengthMaxValue, &litlengthLog, dictPtr, (size_t)(dictEnd - dictPtr));
if (IsError(litlengthHeaderSize)) return ERROR(Error.dictionary_corrupted);
if (litlengthMaxValue > MaxLL) return ERROR(Error.dictionary_corrupted);
if (litlengthLog > LLFSELog) return ERROR(Error.dictionary_corrupted);
BuildFSETable(entropy.LLTable,
litlengthNCount, litlengthMaxValue,
LL_base, LL_bits,
litlengthLog);
dictPtr += litlengthHeaderSize;
}
if (dictPtr + 12 > dictEnd) return ERROR(Error.dictionary_corrupted);
{
int i;
size_t dictContentSize = (size_t)(dictEnd - (dictPtr + 12));
for (i = 0; i < 3; i++)
{
U32 rep = MEM_readLE32(dictPtr); dictPtr += 4;
if (rep == 0 || rep >= dictContentSize) return ERROR(Error.dictionary_corrupted);
entropy.rep[i] = rep;
}
}
return (size_t)(dictPtr - (BYTE*)dict);
}