Skip to content

Commit

Permalink
Calculate dst buffer size with ZSTD_compressBound.
Browse files Browse the repository at this point in the history
Fixes #512.
  • Loading branch information
MarkCallow committed Jan 30, 2022
1 parent b2c73d2 commit 71afc96
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions lib/writer2.c
Original file line number Diff line number Diff line change
Expand Up @@ -684,25 +684,34 @@ ktxTexture2_DeflateZstd(ktxTexture2* This, ktx_uint32_t compressionLevel)
{
ktx_uint32_t levelIndexByteLength =
This->numLevels * sizeof(ktxLevelIndexEntry);
// Allocate a temporary buffer the same size as the current data since
// that will clearly be big enough.
ktx_uint8_t* workBuf = malloc(This->dataSize + levelIndexByteLength);
ktx_uint8_t* workBuf;
ktx_uint8_t* cmpData;
ktx_size_t dstRemainingByteLength = This->dataSize;
ktx_size_t dstRemainingByteLength = 0;
ktx_size_t byteLengthCmp = 0;
ktx_size_t levelOffset = 0;
ktxLevelIndexEntry* cindex = This->_private->_levelIndex;
ktxLevelIndexEntry* nindex = (ktxLevelIndexEntry*)workBuf;
ktx_uint8_t* pCmpDst = &workBuf[levelIndexByteLength];
ktxLevelIndexEntry* nindex;
ktx_uint8_t* pCmpDst;

ZSTD_CCtx* cctx = ZSTD_createCCtx();

if (workBuf == NULL)
return KTX_OUT_OF_MEMORY;

if (This->supercompressionScheme != KTX_SS_NONE)
return KTX_INVALID_OPERATION;

// On rare occasions the deflated data can be a few bytes larger than
// the source data. Calculating the dst buffer size using
// ZSTD_compressBound provides a suitable size plus compression is said
// to run faster when the dst buffer is >= compressBound.
for (int32_t level = This->numLevels - 1; level >= 0; level--) {
dstRemainingByteLength += ZSTD_compressBound(cindex[level].byteLength);
}

workBuf = malloc(dstRemainingByteLength + levelIndexByteLength);
if (workBuf == NULL)
return KTX_OUT_OF_MEMORY;
nindex = (ktxLevelIndexEntry*)workBuf;
pCmpDst = &workBuf[levelIndexByteLength];

for (int32_t level = This->numLevels - 1; level >= 0; level--) {
size_t levelByteLengthCmp =
ZSTD_compressCCtx(cctx, pCmpDst + levelOffset,
Expand Down

0 comments on commit 71afc96

Please sign in to comment.