diff --git a/lib/zbuff.c b/lib/zbuff.c index 06a7ec4e..721fad22 100644 --- a/lib/zbuff.c +++ b/lib/zbuff.c @@ -136,7 +136,7 @@ size_t ZBUFF_compressInit_advanced(ZBUFF_CCtx* zbc, zbc->inBuff = (char*)malloc(neededInBuffSize); if (zbc->inBuff == NULL) return ERROR(memory_allocation); } - zbc->blockSize = MIN(ZSTD_BLOCKSIZE_MAX, neededInBuffSize); + zbc->blockSize = MIN(ZSTD_BLOCKSIZE_MAX, neededInBuffSize/2); } if (zbc->outBuffSize < ZSTD_compressBound(zbc->blockSize)+1) { zbc->outBuffSize = ZSTD_compressBound(zbc->blockSize)+1; @@ -222,7 +222,7 @@ static size_t ZBUFF_compressContinue_generic(ZBUFF_CCtx* zbc, /* prepare next block */ zbc->inBuffTarget = zbc->inBuffPos + zbc->blockSize; if (zbc->inBuffTarget > zbc->inBuffSize) - { zbc->inBuffPos = 0; zbc->inBuffTarget = zbc->blockSize; } /* note : inBuffSize >= blockSize */ + zbc->inBuffPos = 0, zbc->inBuffTarget = zbc->blockSize; /* note : inBuffSize >= blockSize */ zbc->inToCompress = zbc->inBuffPos; if (cDst == op) { op += cSize; break; } /* no need to flush */ zbc->outBuffContentSize = cSize; @@ -326,10 +326,11 @@ typedef enum { ZBUFFds_init, ZBUFFds_readHeader, struct ZBUFF_DCtx_s { ZSTD_DCtx* zc; ZSTD_frameParams fParams; - char* inBuff; + size_t blockSize; + char* inBuff; size_t inBuffSize; size_t inPos; - char* outBuff; + char* outBuff; size_t outBuffSize; size_t outStart; size_t outEnd; @@ -405,20 +406,21 @@ size_t ZBUFF_decompressContinue(ZBUFF_DCtx* zbc, } } /* Frame header instruct buffer sizes */ - { size_t const neededInSize = ZSTD_BLOCKSIZE_MAX; /* a block is never > ZSTD_BLOCKSIZE_MAX */ - if (zbc->inBuffSize < neededInSize) { + { size_t const blockSize = MIN(1 << zbc->fParams.windowLog, ZSTD_BLOCKSIZE_MAX); + zbc->blockSize = blockSize; + if (zbc->inBuffSize < blockSize) { free(zbc->inBuff); - zbc->inBuffSize = neededInSize; - zbc->inBuff = (char*)malloc(neededInSize); + zbc->inBuffSize = blockSize; + zbc->inBuff = (char*)malloc(blockSize); if (zbc->inBuff == NULL) return ERROR(memory_allocation); - } } - { size_t const neededOutSize = (size_t)1 << zbc->fParams.windowLog; - if (zbc->outBuffSize < neededOutSize) { - free(zbc->outBuff); - zbc->outBuffSize = neededOutSize; - zbc->outBuff = (char*)malloc(neededOutSize); - if (zbc->outBuff == NULL) return ERROR(memory_allocation); - } } + } + { size_t const neededOutSize = ((size_t)1 << zbc->fParams.windowLog) + blockSize; + if (zbc->outBuffSize < neededOutSize) { + free(zbc->outBuff); + zbc->outBuffSize = neededOutSize; + zbc->outBuff = (char*)malloc(neededOutSize); + if (zbc->outBuff == NULL) return ERROR(memory_allocation); + } } } zbc->stage = ZBUFFds_read; case ZBUFFds_read: @@ -471,7 +473,7 @@ size_t ZBUFF_decompressContinue(ZBUFF_DCtx* zbc, zbc->outStart += flushedSize; if (flushedSize == toFlushSize) { zbc->stage = ZBUFFds_read; - if (zbc->outStart + ZSTD_BLOCKSIZE_MAX > zbc->outBuffSize) + if (zbc->outStart + zbc->blockSize > zbc->outBuffSize) zbc->outStart = zbc->outEnd = 0; break; } diff --git a/lib/zstd_decompress.c b/lib/zstd_decompress.c index 68bcaa8e..fd9b4023 100644 --- a/lib/zstd_decompress.c +++ b/lib/zstd_decompress.c @@ -686,18 +686,18 @@ FORCE_INLINE size_t ZSTD_execSequence(BYTE* op, size_t const sequenceLength = sequence.litLength + sequence.matchLength; BYTE* const oMatchEnd = op + sequenceLength; /* risk : address space overflow (32-bits) */ BYTE* const oend_8 = oend-8; - const BYTE* const litEnd = *litPtr + sequence.litLength; + const BYTE* const iLitEnd = *litPtr + sequence.litLength; const BYTE* match = oLitEnd - sequence.offset; /* check */ if (oLitEnd > oend_8) return ERROR(dstSize_tooSmall); /* last match must start at a minimum distance of 8 from oend */ if (oMatchEnd > oend) return ERROR(dstSize_tooSmall); /* overwrite beyond dst buffer */ - if (litEnd > litLimit_8) return ERROR(corruption_detected); /* over-read beyond lit buffer */ + if (iLitEnd > litLimit_8) return ERROR(corruption_detected); /* over-read beyond lit buffer */ /* copy Literals */ ZSTD_wildcopy(op, *litPtr, sequence.litLength); /* note : oLitEnd <= oend-8 : no risk of overwrite beyond oend */ op = oLitEnd; - *litPtr = litEnd; /* update for next sequence */ + *litPtr = iLitEnd; /* update for next sequence */ /* copy Match */ if (sequence.offset > (size_t)(oLitEnd - base)) { @@ -761,7 +761,6 @@ static size_t ZSTD_decompressSequences( const BYTE* litPtr = dctx->litPtr; const BYTE* const litLimit_8 = litPtr + dctx->litBufSize - 8; const BYTE* const litEnd = litPtr + dctx->litSize; - int nbSeq; U32* DTableLL = dctx->LLTable; U32* DTableML = dctx->MLTable; U32* DTableOffb = dctx->OffTable; @@ -769,13 +768,14 @@ static size_t ZSTD_decompressSequences( const BYTE* const vBase = (const BYTE*) (dctx->vBase); const BYTE* const dictEnd = (const BYTE*) (dctx->dictEnd); const U32 mls = dctx->fParams.mml; + int nbSeq; /* Build Decoding Tables */ - { size_t const errorCode = ZSTD_decodeSeqHeaders(&nbSeq, + { size_t const seqHSize = ZSTD_decodeSeqHeaders(&nbSeq, DTableLL, DTableML, DTableOffb, ip, seqSize); - if (ZSTD_isError(errorCode)) return errorCode; - ip += errorCode; + if (ZSTD_isError(seqHSize)) return seqHSize; + ip += seqHSize; } /* Regen sequences */