diff --git a/lib/zstd_compress.c b/lib/zstd_compress.c index eb5d8ae92..8bda0169e 100644 --- a/lib/zstd_compress.c +++ b/lib/zstd_compress.c @@ -103,8 +103,6 @@ struct ZSTD_CCtx_s void* workSpace; size_t workSpaceSize; size_t blockSize; - size_t hbSize; - char headerBuffer[ZSTD_FRAMEHEADERSIZE_MAX]; seqStore_t seqStore; /* sequences storage ptrs */ U32* hashTable; @@ -272,7 +270,6 @@ static size_t ZSTD_resetCCtx_advanced (ZSTD_CCtx* zc, zc->seqStore.offCodeStart = zc->seqStore.mlCodeStart + maxNbSeq; zc->seqStore.litStart = zc->seqStore.offCodeStart + maxNbSeq; - zc->hbSize = 0; zc->stage = 1; zc->loadedDictEnd = 0; @@ -299,10 +296,6 @@ size_t ZSTD_copyCCtx(ZSTD_CCtx* dstCCtx, const ZSTD_CCtx* srcCCtx) memcpy(dstCCtx->workSpace, srcCCtx->workSpace, tableSpace); } - /* copy frame header */ - dstCCtx->hbSize = srcCCtx->hbSize; - memcpy(dstCCtx->headerBuffer , srcCCtx->headerBuffer, srcCCtx->hbSize); - /* copy dictionary pointers */ dstCCtx->nextToUpdate = srcCCtx->nextToUpdate; dstCCtx->nextToUpdate3= srcCCtx->nextToUpdate3; @@ -2068,22 +2061,46 @@ static size_t ZSTD_compress_generic (ZSTD_CCtx* zc, } +static size_t ZSTD_writeFrameHeader(void* dst, size_t dstCapacity, + ZSTD_parameters params, U64 pledgedSrcSize) +{ BYTE* const op = (BYTE*)dst; + U32 const fcsId = params.fParams.contentSizeFlag ? + (pledgedSrcSize>0) + (pledgedSrcSize>=256) + (pledgedSrcSize>=65536+256) : /* 0-3 */ + 0; + BYTE const fdescriptor = (BYTE)((params.cParams.windowLog - ZSTD_WINDOWLOG_ABSOLUTEMIN) /* windowLog : 4 KB - 128 MB */ + | (fcsId << 6) ); + size_t const hSize = ZSTD_frameHeaderSize_min + ZSTD_fcs_fieldSize[fcsId]; + if (hSize > dstCapacity) return ERROR(dstSize_tooSmall); + + MEM_writeLE32(dst, ZSTD_MAGICNUMBER); + op[4] = fdescriptor; + switch(fcsId) + { + default: /* impossible */ + case 0 : break; + case 1 : op[5] = (BYTE)(pledgedSrcSize); break; + case 2 : MEM_writeLE16(op+5, (U16)(pledgedSrcSize-256)); break; + case 3 : MEM_writeLE64(op+5, (U64)(pledgedSrcSize)); break; + } + return hSize; +} + + static size_t ZSTD_compressContinue_internal (ZSTD_CCtx* zc, void* dst, size_t dstCapacity, const void* src, size_t srcSize, U32 frame) { const BYTE* const ip = (const BYTE*) src; - size_t hbSize = 0; + size_t fhSize = 0; if (zc->stage==0) return ERROR(stage_wrong); if (frame && (zc->stage==1)) { /* copy saved header */ - hbSize = zc->hbSize; - if (dstCapacity <= hbSize) return ERROR(dstSize_tooSmall); + fhSize = ZSTD_writeFrameHeader(dst, dstCapacity, zc->params, srcSize); + if (ZSTD_isError(fhSize)) return fhSize; + dstCapacity -= fhSize; + dst = (char*)dst + fhSize; zc->stage = 2; - memcpy(dst, zc->headerBuffer, hbSize); - dstCapacity -= hbSize; - dst = (char*)dst + hbSize; } /* Check if blocks follow each other */ @@ -2124,7 +2141,7 @@ static size_t ZSTD_compressContinue_internal (ZSTD_CCtx* zc, ZSTD_compress_generic (zc, dst, dstCapacity, src, srcSize) : ZSTD_compressBlock_internal (zc, dst, dstCapacity, src, srcSize); if (ZSTD_isError(cSize)) return cSize; - return cSize + hbSize; + return cSize + fhSize; } } @@ -2250,46 +2267,18 @@ static size_t ZSTD_compress_insertDictionary(ZSTD_CCtx* zc, const void* dict, si } -static size_t ZSTD_writeFrameHeader(void* dst, size_t dstCapacity, - ZSTD_parameters params, U64 pledgedSrcSize) -{ BYTE* const op = (BYTE*)dst; - U32 const fcsId = (pledgedSrcSize>0) + (pledgedSrcSize>=256) + (pledgedSrcSize>=65536+256); /* 0-3 */ - BYTE const fdescriptor = (BYTE)((params.cParams.windowLog - ZSTD_WINDOWLOG_ABSOLUTEMIN) /* windowLog : 4 KB - 128 MB */ - | (fcsId << 6) ); - size_t const hSize = ZSTD_frameHeaderSize_min + ZSTD_fcs_fieldSize[fcsId]; - if (hSize > dstCapacity) return ERROR(dstSize_tooSmall); - - MEM_writeLE32(dst, ZSTD_MAGICNUMBER); - op[4] = fdescriptor; - switch(fcsId) - { - default: /* impossible */ - case 0 : break; - case 1 : op[5] = (BYTE)(pledgedSrcSize); break; - case 2 : MEM_writeLE16(op+5, (U16)(pledgedSrcSize-256)); break; - case 3 : MEM_writeLE64(op+5, (U64)(pledgedSrcSize)); break; - } - return hSize; -} - - /*! ZSTD_compressBegin_internal() : * @return : 0, or an error code */ static size_t ZSTD_compressBegin_internal(ZSTD_CCtx* zc, const void* dict, size_t dictSize, ZSTD_parameters params, U64 pledgedSrcSize) { - U32 hashLog3 = (pledgedSrcSize || pledgedSrcSize >= 8192) ? ZSTD_HASHLOG3_MAX : ((pledgedSrcSize >= 2048) ? ZSTD_HASHLOG3_MIN + 1 : ZSTD_HASHLOG3_MIN); - zc->hashLog3 = (params.cParams.searchLength==3) ? hashLog3 : 0; -// printf("windowLog=%d hashLog=%d hashLog3=%d \n", params.windowLog, params.hashLog, zc->hashLog3); + { U32 const hashLog3 = (pledgedSrcSize || pledgedSrcSize >= 8192) ? ZSTD_HASHLOG3_MAX : ((pledgedSrcSize >= 2048) ? ZSTD_HASHLOG3_MIN + 1 : ZSTD_HASHLOG3_MIN); + zc->hashLog3 = (params.cParams.searchLength==3) ? hashLog3 : 0; } { size_t const resetError = ZSTD_resetCCtx_advanced(zc, params, 1); if (ZSTD_isError(resetError)) return resetError; } - /* Write Frame Header into ctx headerBuffer */ - zc->hbSize = ZSTD_writeFrameHeader(zc->headerBuffer, ZSTD_FRAMEHEADERSIZE_MAX, - params, pledgedSrcSize); - return ZSTD_compress_insertDictionary(zc, dict, dictSize); } @@ -2343,19 +2332,18 @@ size_t ZSTD_compressBegin(ZSTD_CCtx* zc, int compressionLevel) size_t ZSTD_compressEnd(ZSTD_CCtx* zc, void* dst, size_t dstCapacity) { BYTE* op = (BYTE*)dst; - size_t hbSize = 0; + size_t fhSize = 0; /* not even init ! */ if (zc->stage==0) return ERROR(stage_wrong); /* special case : empty frame */ if (zc->stage==1) { - hbSize = zc->hbSize; - if (dstCapacity <= hbSize) return ERROR(dstSize_tooSmall); + fhSize = ZSTD_writeFrameHeader(dst, dstCapacity, zc->params, 0); + if (ZSTD_isError(fhSize)) return fhSize; + dstCapacity -= fhSize; + op += fhSize; zc->stage = 2; - memcpy(dst, zc->headerBuffer, hbSize); - dstCapacity -= hbSize; - op += hbSize; } /* frame epilogue */ @@ -2365,7 +2353,7 @@ size_t ZSTD_compressEnd(ZSTD_CCtx* zc, void* dst, size_t dstCapacity) op[2] = 0; zc->stage = 0; /* return to "created by not init" status */ - return 3+hbSize; + return 3+fhSize; }