fixed buffer sync error in zbuff

This commit is contained in:
Yann Collet
2016-04-03 20:24:25 +02:00
parent ef36390e4a
commit 5a88a70f97
2 changed files with 26 additions and 24 deletions
+19 -17
View File
@@ -136,7 +136,7 @@ size_t ZBUFF_compressInit_advanced(ZBUFF_CCtx* zbc,
zbc->inBuff = (char*)malloc(neededInBuffSize); zbc->inBuff = (char*)malloc(neededInBuffSize);
if (zbc->inBuff == NULL) return ERROR(memory_allocation); 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) { if (zbc->outBuffSize < ZSTD_compressBound(zbc->blockSize)+1) {
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 */ /* prepare next block */
zbc->inBuffTarget = zbc->inBuffPos + zbc->blockSize; zbc->inBuffTarget = zbc->inBuffPos + zbc->blockSize;
if (zbc->inBuffTarget > zbc->inBuffSize) 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; zbc->inToCompress = zbc->inBuffPos;
if (cDst == op) { op += cSize; break; } /* no need to flush */ if (cDst == op) { op += cSize; break; } /* no need to flush */
zbc->outBuffContentSize = cSize; zbc->outBuffContentSize = cSize;
@@ -326,10 +326,11 @@ typedef enum { ZBUFFds_init, ZBUFFds_readHeader,
struct ZBUFF_DCtx_s { struct ZBUFF_DCtx_s {
ZSTD_DCtx* zc; ZSTD_DCtx* zc;
ZSTD_frameParams fParams; ZSTD_frameParams fParams;
char* inBuff; size_t blockSize;
char* inBuff;
size_t inBuffSize; size_t inBuffSize;
size_t inPos; size_t inPos;
char* outBuff; char* outBuff;
size_t outBuffSize; size_t outBuffSize;
size_t outStart; size_t outStart;
size_t outEnd; size_t outEnd;
@@ -405,20 +406,21 @@ size_t ZBUFF_decompressContinue(ZBUFF_DCtx* zbc,
} } } }
/* Frame header instruct buffer sizes */ /* Frame header instruct buffer sizes */
{ size_t const neededInSize = ZSTD_BLOCKSIZE_MAX; /* a block is never > ZSTD_BLOCKSIZE_MAX */ { size_t const blockSize = MIN(1 << zbc->fParams.windowLog, ZSTD_BLOCKSIZE_MAX);
if (zbc->inBuffSize < neededInSize) { zbc->blockSize = blockSize;
if (zbc->inBuffSize < blockSize) {
free(zbc->inBuff); free(zbc->inBuff);
zbc->inBuffSize = neededInSize; zbc->inBuffSize = blockSize;
zbc->inBuff = (char*)malloc(neededInSize); zbc->inBuff = (char*)malloc(blockSize);
if (zbc->inBuff == NULL) return ERROR(memory_allocation); if (zbc->inBuff == NULL) return ERROR(memory_allocation);
} } }
{ size_t const neededOutSize = (size_t)1 << zbc->fParams.windowLog; { size_t const neededOutSize = ((size_t)1 << zbc->fParams.windowLog) + blockSize;
if (zbc->outBuffSize < neededOutSize) { if (zbc->outBuffSize < neededOutSize) {
free(zbc->outBuff); free(zbc->outBuff);
zbc->outBuffSize = neededOutSize; zbc->outBuffSize = neededOutSize;
zbc->outBuff = (char*)malloc(neededOutSize); zbc->outBuff = (char*)malloc(neededOutSize);
if (zbc->outBuff == NULL) return ERROR(memory_allocation); if (zbc->outBuff == NULL) return ERROR(memory_allocation);
} } } } }
zbc->stage = ZBUFFds_read; zbc->stage = ZBUFFds_read;
case ZBUFFds_read: case ZBUFFds_read:
@@ -471,7 +473,7 @@ size_t ZBUFF_decompressContinue(ZBUFF_DCtx* zbc,
zbc->outStart += flushedSize; zbc->outStart += flushedSize;
if (flushedSize == toFlushSize) { if (flushedSize == toFlushSize) {
zbc->stage = ZBUFFds_read; zbc->stage = ZBUFFds_read;
if (zbc->outStart + ZSTD_BLOCKSIZE_MAX > zbc->outBuffSize) if (zbc->outStart + zbc->blockSize > zbc->outBuffSize)
zbc->outStart = zbc->outEnd = 0; zbc->outStart = zbc->outEnd = 0;
break; break;
} }
+7 -7
View File
@@ -686,18 +686,18 @@ FORCE_INLINE size_t ZSTD_execSequence(BYTE* op,
size_t const sequenceLength = sequence.litLength + sequence.matchLength; size_t const sequenceLength = sequence.litLength + sequence.matchLength;
BYTE* const oMatchEnd = op + sequenceLength; /* risk : address space overflow (32-bits) */ BYTE* const oMatchEnd = op + sequenceLength; /* risk : address space overflow (32-bits) */
BYTE* const oend_8 = oend-8; 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; const BYTE* match = oLitEnd - sequence.offset;
/* check */ /* check */
if (oLitEnd > oend_8) return ERROR(dstSize_tooSmall); /* last match must start at a minimum distance of 8 from oend */ 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 (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 */ /* copy Literals */
ZSTD_wildcopy(op, *litPtr, sequence.litLength); /* note : oLitEnd <= oend-8 : no risk of overwrite beyond oend */ ZSTD_wildcopy(op, *litPtr, sequence.litLength); /* note : oLitEnd <= oend-8 : no risk of overwrite beyond oend */
op = oLitEnd; op = oLitEnd;
*litPtr = litEnd; /* update for next sequence */ *litPtr = iLitEnd; /* update for next sequence */
/* copy Match */ /* copy Match */
if (sequence.offset > (size_t)(oLitEnd - base)) { if (sequence.offset > (size_t)(oLitEnd - base)) {
@@ -761,7 +761,6 @@ static size_t ZSTD_decompressSequences(
const BYTE* litPtr = dctx->litPtr; const BYTE* litPtr = dctx->litPtr;
const BYTE* const litLimit_8 = litPtr + dctx->litBufSize - 8; const BYTE* const litLimit_8 = litPtr + dctx->litBufSize - 8;
const BYTE* const litEnd = litPtr + dctx->litSize; const BYTE* const litEnd = litPtr + dctx->litSize;
int nbSeq;
U32* DTableLL = dctx->LLTable; U32* DTableLL = dctx->LLTable;
U32* DTableML = dctx->MLTable; U32* DTableML = dctx->MLTable;
U32* DTableOffb = dctx->OffTable; U32* DTableOffb = dctx->OffTable;
@@ -769,13 +768,14 @@ static size_t ZSTD_decompressSequences(
const BYTE* const vBase = (const BYTE*) (dctx->vBase); const BYTE* const vBase = (const BYTE*) (dctx->vBase);
const BYTE* const dictEnd = (const BYTE*) (dctx->dictEnd); const BYTE* const dictEnd = (const BYTE*) (dctx->dictEnd);
const U32 mls = dctx->fParams.mml; const U32 mls = dctx->fParams.mml;
int nbSeq;
/* Build Decoding Tables */ /* Build Decoding Tables */
{ size_t const errorCode = ZSTD_decodeSeqHeaders(&nbSeq, { size_t const seqHSize = ZSTD_decodeSeqHeaders(&nbSeq,
DTableLL, DTableML, DTableOffb, DTableLL, DTableML, DTableOffb,
ip, seqSize); ip, seqSize);
if (ZSTD_isError(errorCode)) return errorCode; if (ZSTD_isError(seqHSize)) return seqHSize;
ip += errorCode; ip += seqHSize;
} }
/* Regen sequences */ /* Regen sequences */