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;
/* 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;
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;
} }
/* 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:
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;
}
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)) {
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;
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 */