From: Nick Terrell Date: Wed, 17 Mar 2021 23:50:37 +0000 (-0700) Subject: [huf][fse] Clean up workspaces X-Git-Tag: v1.4.10~18^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=756bd59322600d260138a9c1cd95cb4674bc6e05;p=thirdparty%2Fzstd.git [huf][fse] Clean up workspaces * Move `counting` to a struct in `FSE_decompress_wksp_body()` * Fix error code in `FSE_decompress_wksp_body()` * Rename a variable in `HUF_ReadDTableX2_Workspace` --- diff --git a/lib/common/fse_decompress.c b/lib/common/fse_decompress.c index cc45babd9..af865fb4d 100644 --- a/lib/common/fse_decompress.c +++ b/lib/common/fse_decompress.c @@ -310,6 +310,11 @@ size_t FSE_decompress_wksp(void* dst, size_t dstCapacity, const void* cSrc, size return FSE_decompress_wksp_bmi2(dst, dstCapacity, cSrc, cSrcSize, maxLog, workSpace, wkspSize, /* bmi2 */ 0); } +typedef struct { + short count[FSE_MAX_SYMBOL_VALUE + 1]; +} FSE_NormalizedCount; + + FORCE_INLINE_TEMPLATE size_t FSE_decompress_wksp_body( void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, @@ -320,16 +325,15 @@ FORCE_INLINE_TEMPLATE size_t FSE_decompress_wksp_body( const BYTE* ip = istart; unsigned tableLog; unsigned maxSymbolValue = FSE_MAX_SYMBOL_VALUE; - short* counting = (short*) workSpace; - size_t const countingSize = sizeof(short) * (FSE_MAX_SYMBOL_VALUE + 1); - FSE_DTable* const dtable = (FSE_DTable*)(void*)((BYTE*)workSpace + countingSize); + FSE_NormalizedCount* const ncount = (FSE_NormalizedCount*)workSpace; + FSE_DTable* const dtable = (FSE_DTable*)(void*)((BYTE*)workSpace + sizeof(*ncount)); DEBUG_STATIC_ASSERT((FSE_MAX_SYMBOL_VALUE + 1) % 2 == 0); - if (wkspSize < countingSize) return ERROR(GENERIC); + if (wkspSize < sizeof(*ncount)) return ERROR(GENERIC); /* normal FSE decoding mode */ { - size_t const NCountLength = FSE_readNCount_bmi2(counting, &maxSymbolValue, &tableLog, istart, cSrcSize, bmi2); + size_t const NCountLength = FSE_readNCount_bmi2(ncount->count, &maxSymbolValue, &tableLog, istart, cSrcSize, bmi2); if (FSE_isError(NCountLength)) return NCountLength; if (tableLog > maxLog) return ERROR(tableLog_tooLarge); assert(NCountLength <= cSrcSize); @@ -340,9 +344,9 @@ FORCE_INLINE_TEMPLATE size_t FSE_decompress_wksp_body( if (FSE_DECOMPRESS_WKSP_SIZE(tableLog, maxSymbolValue) > wkspSize) return ERROR(tableLog_tooLarge); workSpace = dtable + FSE_DTABLE_SIZE_U32(tableLog); wkspSize -= FSE_DTABLE_SIZE(tableLog); - wkspSize -= countingSize; + wkspSize -= sizeof(*ncount); - CHECK_F( FSE_buildDTable_internal(dtable, counting, maxSymbolValue, tableLog, workSpace, wkspSize) ); + CHECK_F( FSE_buildDTable_internal(dtable, ncount->count, maxSymbolValue, tableLog, workSpace, wkspSize) ); { const void* ptr = dtable; diff --git a/lib/decompress/huf_decompress.c b/lib/decompress/huf_decompress.c index 4481484f7..07954a931 100644 --- a/lib/decompress/huf_decompress.c +++ b/lib/decompress/huf_decompress.c @@ -620,7 +620,7 @@ typedef struct { U32 rankStart0[HUF_TABLELOG_MAX + 2]; sortedSymbol_t sortedSymbol[HUF_SYMBOLVALUE_MAX + 1]; BYTE weightList[HUF_SYMBOLVALUE_MAX + 1]; - U32 wksp[HUF_READ_STATS_WORKSPACE_SIZE_U32]; + U32 calleeWksp[HUF_READ_STATS_WORKSPACE_SIZE_U32]; } HUF_ReadDTableX2_Workspace; size_t HUF_readDTableX2_wksp(HUF_DTable* DTable, @@ -635,9 +635,9 @@ size_t HUF_readDTableX2_wksp(HUF_DTable* DTable, HUF_DEltX2* const dt = (HUF_DEltX2*)dtPtr; U32 *rankStart; - HUF_ReadDTableX2_Workspace* wksp = (HUF_ReadDTableX2_Workspace*)workSpace; + HUF_ReadDTableX2_Workspace* const wksp = (HUF_ReadDTableX2_Workspace*)workSpace; - if (sizeof(*wksp) > wkspSize) return ERROR(tableLog_tooLarge); + if (sizeof(*wksp) > wkspSize) return ERROR(GENERIC); rankStart = wksp->rankStart0 + 1; ZSTD_memset(wksp->rankStats, 0, sizeof(wksp->rankStats)); @@ -647,7 +647,7 @@ size_t HUF_readDTableX2_wksp(HUF_DTable* DTable, if (maxTableLog > HUF_TABLELOG_MAX) return ERROR(tableLog_tooLarge); /* ZSTD_memset(weightList, 0, sizeof(weightList)); */ /* is not necessary, even though some analyzer complain ... */ - iSize = HUF_readStats_wksp(wksp->weightList, HUF_SYMBOLVALUE_MAX + 1, wksp->rankStats, &nbSymbols, &tableLog, src, srcSize, wksp->wksp, sizeof(wksp->wksp), /* bmi2 */ 0); + iSize = HUF_readStats_wksp(wksp->weightList, HUF_SYMBOLVALUE_MAX + 1, wksp->rankStats, &nbSymbols, &tableLog, src, srcSize, wksp->calleeWksp, sizeof(wksp->calleeWksp), /* bmi2 */ 0); if (HUF_isError(iSize)) return iSize; /* check result */ @@ -701,7 +701,7 @@ size_t HUF_readDTableX2_wksp(HUF_DTable* DTable, wksp->sortedSymbol, sizeOfSort, wksp->rankStart0, wksp->rankVal, maxW, tableLog+1, - wksp->wksp, sizeof(wksp->wksp) / sizeof(U32)); + wksp->calleeWksp, sizeof(wksp->calleeWksp) / sizeof(U32)); dtd.tableLog = (BYTE)maxTableLog; dtd.tableType = 1;