Lines Matching defs:cctx

94 static void ZSTD_initCCtx(ZSTD_CCtx* cctx, ZSTD_customMem memManager)
96 assert(cctx != NULL);
97 ZSTD_memset(cctx, 0, sizeof(*cctx));
98 cctx->customMem = memManager;
99 cctx->bmi2 = ZSTD_cpuSupportsBmi2();
100 { size_t const err = ZSTD_CCtx_reset(cctx, ZSTD_reset_parameters);
111 { ZSTD_CCtx* const cctx = (ZSTD_CCtx*)ZSTD_customMalloc(sizeof(ZSTD_CCtx), customMem);
112 if (!cctx) return NULL;
113 ZSTD_initCCtx(cctx, customMem);
114 return cctx;
121 ZSTD_CCtx* cctx;
126 cctx = (ZSTD_CCtx*)ZSTD_cwksp_reserve_object(&ws, sizeof(ZSTD_CCtx));
127 if (cctx == NULL) return NULL;
129 ZSTD_memset(cctx, 0, sizeof(ZSTD_CCtx));
130 ZSTD_cwksp_move(&cctx->workspace, &ws);
131 cctx->staticSize = workspaceSize;
134 if (!ZSTD_cwksp_check_available(&cctx->workspace, ENTROPY_WORKSPACE_SIZE + 2 * sizeof(ZSTD_compressedBlockState_t))) return NULL;
135 cctx->blockState.prevCBlock = (ZSTD_compressedBlockState_t*)ZSTD_cwksp_reserve_object(&cctx->workspace, sizeof(ZSTD_compressedBlockState_t));
136 cctx->blockState.nextCBlock = (ZSTD_compressedBlockState_t*)ZSTD_cwksp_reserve_object(&cctx->workspace, sizeof(ZSTD_compressedBlockState_t));
137 cctx->entropyWorkspace = (U32*)ZSTD_cwksp_reserve_object(&cctx->workspace, ENTROPY_WORKSPACE_SIZE);
138 cctx->bmi2 = ZSTD_cpuid_bmi2(ZSTD_cpuid());
139 return cctx;
145 static void ZSTD_clearAllDicts(ZSTD_CCtx* cctx)
147 ZSTD_customFree(cctx->localDict.dictBuffer, cctx->customMem);
148 ZSTD_freeCDict(cctx->localDict.cdict);
149 ZSTD_memset(&cctx->localDict, 0, sizeof(cctx->localDict));
150 ZSTD_memset(&cctx->prefixDict, 0, sizeof(cctx->prefixDict));
151 cctx->cdict = NULL;
161 static void ZSTD_freeCCtxContent(ZSTD_CCtx* cctx)
163 assert(cctx != NULL);
164 assert(cctx->staticSize == 0);
165 ZSTD_clearAllDicts(cctx);
166 ZSTD_cwksp_free(&cctx->workspace, cctx->customMem);
169 size_t ZSTD_freeCCtx(ZSTD_CCtx* cctx)
171 if (cctx==NULL) return 0; /* support free on NULL */
172 RETURN_ERROR_IF(cctx->staticSize, memory_allocation,
175 int cctxInWorkspace = ZSTD_cwksp_owns_buffer(&cctx->workspace, cctx);
176 ZSTD_freeCCtxContent(cctx);
178 ZSTD_customFree(cctx, cctx->customMem);
185 static size_t ZSTD_sizeof_mtctx(const ZSTD_CCtx* cctx)
187 (void)cctx;
192 size_t ZSTD_sizeof_CCtx(const ZSTD_CCtx* cctx)
194 if (cctx==NULL) return 0; /* support sizeof on NULL */
195 /* cctx may be in the workspace */
196 return (cctx->workspace.workspace == cctx ? 0 : sizeof(*cctx))
197 + ZSTD_cwksp_sizeof(&cctx->workspace)
198 + ZSTD_sizeof_localDict(cctx->localDict)
199 + ZSTD_sizeof_mtctx(cctx);
621 size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, int value)
624 if (cctx->streamStage != zcss_init) {
626 cctx->cParamsChanged = 1;
634 RETURN_ERROR_IF((value!=0) && cctx->staticSize, parameter_unsupported,
675 return ZSTD_CCtxParams_setParameter(&cctx->requestedParams, param, value);
873 size_t ZSTD_CCtx_getParameter(ZSTD_CCtx const* cctx, ZSTD_cParameter param, int* value)
875 return ZSTD_CCtxParams_getParameter(&cctx->requestedParams, param, value);
989 * just applies `params` into `cctx`
991 * If ZSTDMT is enabled, parameters are pushed to cctx->mtctx.
996 ZSTD_CCtx* cctx, const ZSTD_CCtx_params* params)
999 RETURN_ERROR_IF(cctx->streamStage != zcss_init, stage_wrong,
1001 RETURN_ERROR_IF(cctx->cdict, stage_wrong,
1005 cctx->requestedParams = *params;
1009 size_t ZSTD_CCtx_setPledgedSrcSize(ZSTD_CCtx* cctx, unsigned long long pledgedSrcSize)
1012 RETURN_ERROR_IF(cctx->streamStage != zcss_init, stage_wrong,
1014 cctx->pledgedSrcSizePlusOne = pledgedSrcSize+1;
1031 static size_t ZSTD_initLocalDict(ZSTD_CCtx* cctx)
1033 ZSTD_localDict* const dl = &cctx->localDict;
1042 assert(cctx->cdict == dl->cdict);
1047 assert(cctx->cdict == NULL);
1048 assert(cctx->prefixDict.dict == NULL);
1055 &cctx->requestedParams,
1056 cctx->customMem);
1058 cctx->cdict = dl->cdict;
1063 ZSTD_CCtx* cctx, const void* dict, size_t dictSize,
1066 RETURN_ERROR_IF(cctx->streamStage != zcss_init, stage_wrong,
1069 ZSTD_clearAllDicts(cctx); /* in case one already exists */
1073 cctx->localDict.dict = dict;
1076 RETURN_ERROR_IF(cctx->staticSize, memory_allocation,
1078 dictBuffer = ZSTD_customMalloc(dictSize, cctx->customMem);
1081 cctx->localDict.dictBuffer = dictBuffer;
1082 cctx->localDict.dict = dictBuffer;
1084 cctx->localDict.dictSize = dictSize;
1085 cctx->localDict.dictContentType = dictContentType;
1090 ZSTD_CCtx* cctx, const void* dict, size_t dictSize)
1093 cctx, dict, dictSize, ZSTD_dlm_byRef, ZSTD_dct_auto);
1096 size_t ZSTD_CCtx_loadDictionary(ZSTD_CCtx* cctx, const void* dict, size_t dictSize)
1099 cctx, dict, dictSize, ZSTD_dlm_byCopy, ZSTD_dct_auto);
1103 size_t ZSTD_CCtx_refCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict)
1105 RETURN_ERROR_IF(cctx->streamStage != zcss_init, stage_wrong,
1108 ZSTD_clearAllDicts(cctx);
1109 cctx->cdict = cdict;
1113 size_t ZSTD_CCtx_refThreadPool(ZSTD_CCtx* cctx, ZSTD_threadPool* pool)
1115 RETURN_ERROR_IF(cctx->streamStage != zcss_init, stage_wrong,
1117 cctx->pool = pool;
1121 size_t ZSTD_CCtx_refPrefix(ZSTD_CCtx* cctx, const void* prefix, size_t prefixSize)
1123 return ZSTD_CCtx_refPrefix_advanced(cctx, prefix, prefixSize, ZSTD_dct_rawContent);
1127 ZSTD_CCtx* cctx, const void* prefix, size_t prefixSize, ZSTD_dictContentType_e dictContentType)
1129 RETURN_ERROR_IF(cctx->streamStage != zcss_init, stage_wrong,
1131 ZSTD_clearAllDicts(cctx);
1133 cctx->prefixDict.dict = prefix;
1134 cctx->prefixDict.dictSize = prefixSize;
1135 cctx->prefixDict.dictContentType = dictContentType;
1142 size_t ZSTD_CCtx_reset(ZSTD_CCtx* cctx, ZSTD_ResetDirective reset)
1146 cctx->streamStage = zcss_init;
1147 cctx->pledgedSrcSizePlusOne = 0;
1151 RETURN_ERROR_IF(cctx->streamStage != zcss_init, stage_wrong,
1153 ZSTD_clearAllDicts(cctx);
1154 return ZSTD_CCtxParams_reset(&cctx->requestedParams);
1549 ZSTD_frameProgression ZSTD_getFrameProgression(const ZSTD_CCtx* cctx)
1552 size_t const buffered = (cctx->inBuff == NULL) ? 0 :
1553 cctx->inBuffPos - cctx->inToCompress;
1554 if (buffered) assert(cctx->inBuffPos >= cctx->inToCompress);
1556 fp.ingested = cctx->consumedSrcSize + buffered;
1557 fp.consumed = cctx->consumedSrcSize;
1558 fp.produced = cctx->producedCSize;
1559 fp.flushed = cctx->producedCSize; /* simplified; some data might still be left within streaming output buffer */
1568 size_t ZSTD_toFlushNow(ZSTD_CCtx* cctx)
1570 (void)cctx;
1801 FORWARD_IF_ERROR(neededSpace, "cctx size estimate failed!");
1817 RETURN_ERROR_IF(zc->staticSize, memory_allocation, "static cctx : no resize");
1923 void ZSTD_invalidateRepCodes(ZSTD_CCtx* cctx) {
1925 for (i=0; i<ZSTD_REP_NUM; i++) cctx->blockState.prevCBlock->rep[i] = 0;
1926 assert(!ZSTD_window_hasExtDict(cctx->blockState.matchState.window));
1962 ZSTD_resetCCtx_byAttachingCDict(ZSTD_CCtx* cctx,
1986 FORWARD_IF_ERROR(ZSTD_resetCCtx_internal(cctx, &params, pledgedSrcSize,
1989 assert(cctx->appliedParams.cParams.strategy == adjusted_cdict_cParams.strategy);
2000 cctx->blockState.matchState.dictMatchState = &cdict->matchState;
2004 if (cctx->blockState.matchState.window.dictLimit < cdictEnd) {
2005 cctx->blockState.matchState.window.nextSrc =
2006 cctx->blockState.matchState.window.base + cdictEnd;
2007 ZSTD_window_clear(&cctx->blockState.matchState.window);
2010 cctx->blockState.matchState.loadedDictEnd = cctx->blockState.matchState.window.dictLimit;
2013 cctx->dictID = cdict->dictID;
2014 cctx->dictContentSize = cdict->dictContentSize;
2017 ZSTD_memcpy(cctx->blockState.prevCBlock, &cdict->cBlockState, sizeof(cdict->cBlockState));
2022 static size_t ZSTD_resetCCtx_byCopyingCDict(ZSTD_CCtx* cctx,
2040 FORWARD_IF_ERROR(ZSTD_resetCCtx_internal(cctx, &params, pledgedSrcSize,
2043 assert(cctx->appliedParams.cParams.strategy == cdict_cParams->strategy);
2044 assert(cctx->appliedParams.cParams.hashLog == cdict_cParams->hashLog);
2045 assert(cctx->appliedParams.cParams.chainLog == cdict_cParams->chainLog);
2048 ZSTD_cwksp_mark_tables_dirty(&cctx->workspace);
2057 ZSTD_memcpy(cctx->blockState.matchState.hashTable,
2060 /* Do not copy cdict's chainTable if cctx has parameters such that it would not use chainTable */
2061 if (ZSTD_allocateChainTable(cctx->appliedParams.cParams.strategy, cctx->appliedParams.useRowMatchFinder, 0 /* forDDSDict */)) {
2062 ZSTD_memcpy(cctx->blockState.matchState.chainTable,
2069 ZSTD_memcpy(cctx->blockState.matchState.tagTable,
2076 { int const h3log = cctx->blockState.matchState.hashLog3;
2079 ZSTD_memset(cctx->blockState.matchState.hashTable3, 0, h3Size * sizeof(U32));
2082 ZSTD_cwksp_mark_tables_clean(&cctx->workspace);
2086 ZSTD_matchState_t* dstMatchState = &cctx->blockState.matchState;
2092 cctx->dictID = cdict->dictID;
2093 cctx->dictContentSize = cdict->dictContentSize;
2096 ZSTD_memcpy(cctx->blockState.prevCBlock, &cdict->cBlockState, sizeof(cdict->cBlockState));
2104 static size_t ZSTD_resetCCtx_usingCDict(ZSTD_CCtx* cctx,
2116 cctx, cdict, *params, pledgedSrcSize, zbuff);
2119 cctx, cdict, *params, pledgedSrcSize, zbuff);
3841 static size_t ZSTD_compress_frameChunk(ZSTD_CCtx* cctx,
3846 size_t blockSize = cctx->blockSize;
3851 U32 const maxDist = (U32)1 << cctx->appliedParams.cParams.windowLog;
3853 assert(cctx->appliedParams.cParams.windowLog <= ZSTD_WINDOWLOG_MAX);
3856 if (cctx->appliedParams.fParams.checksumFlag && srcSize)
3857 xxh64_update(&cctx->xxhState, src, srcSize);
3860 ZSTD_matchState_t* const ms = &cctx->blockState.matchState;
3869 ms, &cctx->workspace, &cctx->appliedParams, ip, ip + blockSize);
3877 if (ZSTD_useTargetCBlockSize(&cctx->appliedParams)) {
3878 cSize = ZSTD_compressBlock_targetCBlockSize(cctx, op, dstCapacity, ip, blockSize, lastBlock);
3882 } else if (ZSTD_blockSplitterEnabled(&cctx->appliedParams)) {
3883 cSize = ZSTD_compressBlock_splitBlock(cctx, op, dstCapacity, ip, blockSize, lastBlock);
3885 assert(cSize > 0 || cctx->seqCollector.collectSequences == 1);
3887 cSize = ZSTD_compressBlock_internal(cctx,
3911 cctx->isFirstBlock = 0;
3916 if (lastFrameChunk && (op>ostart)) cctx->stage = ZSTDcs_ending;
4004 size_t ZSTD_referenceExternalSequences(ZSTD_CCtx* cctx, rawSeq* seq, size_t nbSeq)
4006 RETURN_ERROR_IF(cctx->stage != ZSTDcs_init, stage_wrong,
4007 "wrong cctx stage");
4008 RETURN_ERROR_IF(cctx->appliedParams.ldmParams.enableLdm == ZSTD_ps_enable,
4011 cctx->externSeqStore.seq = seq;
4012 cctx->externSeqStore.size = nbSeq;
4013 cctx->externSeqStore.capacity = nbSeq;
4014 cctx->externSeqStore.pos = 0;
4015 cctx->externSeqStore.posInSequence = 0;
4020 static size_t ZSTD_compressContinue_internal (ZSTD_CCtx* cctx,
4025 ZSTD_matchState_t* const ms = &cctx->blockState.matchState;
4029 cctx->stage, (unsigned)srcSize);
4030 RETURN_ERROR_IF(cctx->stage==ZSTDcs_created, stage_wrong,
4033 if (frame && (cctx->stage==ZSTDcs_init)) {
4034 fhSize = ZSTD_writeFrameHeader(dst, dstCapacity, &cctx->appliedParams,
4035 cctx->pledgedSrcSizePlusOne-1, cctx->dictID);
4040 cctx->stage = ZSTDcs_ongoing;
4049 if (cctx->appliedParams.ldmParams.enableLdm == ZSTD_ps_enable) {
4050 ZSTD_window_update(&cctx->ldmState.window, src, srcSize, /* forceNonContiguous */ 0);
4056 ms, &cctx->workspace, &cctx->appliedParams,
4060 DEBUGLOG(5, "ZSTD_compressContinue_internal (blockSize=%u)", (unsigned)cctx->blockSize);
4062 ZSTD_compress_frameChunk (cctx, dst, dstCapacity, src, srcSize, lastFrameChunk) :
4063 ZSTD_compressBlock_internal (cctx, dst, dstCapacity, src, srcSize, 0 /* frame */);
4065 cctx->consumedSrcSize += srcSize;
4066 cctx->producedCSize += (cSize + fhSize);
4067 assert(!(cctx->appliedParams.fParams.contentSizeFlag && cctx->pledgedSrcSizePlusOne == 0));
4068 if (cctx->pledgedSrcSizePlusOne != 0) { /* control src size */
4071 cctx->consumedSrcSize+1 > cctx->pledgedSrcSizePlusOne,
4074 (unsigned)cctx->pledgedSrcSizePlusOne-1,
4075 (unsigned)cctx->consumedSrcSize);
4081 size_t ZSTD_compressContinue (ZSTD_CCtx* cctx,
4086 return ZSTD_compressContinue_internal(cctx, dst, dstCapacity, src, srcSize, 1 /* frame mode */, 0 /* last chunk */);
4090 size_t ZSTD_getBlockSize(const ZSTD_CCtx* cctx)
4092 ZSTD_compressionParameters const cParams = cctx->appliedParams.cParams;
4097 size_t ZSTD_compressBlock(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize)
4100 { size_t const blockSizeMax = ZSTD_getBlockSize(cctx);
4103 return ZSTD_compressContinue_internal(cctx, dst, dstCapacity, src, srcSize, 0 /* frame mode */, 0 /* last chunk */);
4398 static size_t ZSTD_compressBegin_internal(ZSTD_CCtx* cctx,
4418 return ZSTD_resetCCtx_usingCDict(cctx, cdict, params, pledgedSrcSize, zbuff);
4421 FORWARD_IF_ERROR( ZSTD_resetCCtx_internal(cctx, params, pledgedSrcSize,
4426 cctx->blockState.prevCBlock, &cctx->blockState.matchState,
4427 &cctx->ldmState, &cctx->workspace, &cctx->appliedParams, cdict->dictContent,
4429 cctx->entropyWorkspace)
4431 cctx->blockState.prevCBlock, &cctx->blockState.matchState,
4432 &cctx->ldmState, &cctx->workspace, &cctx->appliedParams, dict, dictSize,
4433 dictContentType, dtlm, cctx->entropyWorkspace);
4436 cctx->dictID = (U32)dictID;
4437 cctx->dictContentSize = dictContentSize;
4442 size_t ZSTD_compressBegin_advanced_internal(ZSTD_CCtx* cctx,
4453 return ZSTD_compressBegin_internal(cctx,
4462 size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* cctx,
4468 return ZSTD_compressBegin_advanced_internal(cctx,
4474 size_t ZSTD_compressBegin_usingDict(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, int compressionLevel)
4482 return ZSTD_compressBegin_internal(cctx, dict, dictSize, ZSTD_dct_auto, ZSTD_dtlm_fast, NULL,
4486 size_t ZSTD_compressBegin(ZSTD_CCtx* cctx, int compressionLevel)
4488 return ZSTD_compressBegin_usingDict(cctx, NULL, 0, compressionLevel);
4495 static size_t ZSTD_writeEpilogue(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity)
4502 RETURN_ERROR_IF(cctx->stage == ZSTDcs_created, stage_wrong, "init missing");
4505 if (cctx->stage == ZSTDcs_init) {
4506 fhSize = ZSTD_writeFrameHeader(dst, dstCapacity, &cctx->appliedParams, 0, 0);
4510 cctx->stage = ZSTDcs_ongoing;
4513 if (cctx->stage != ZSTDcs_ending) {
4522 if (cctx->appliedParams.fParams.checksumFlag) {
4523 U32 const checksum = (U32) xxh64_digest(&cctx->xxhState);
4530 cctx->stage = ZSTDcs_created; /* return to "created but no init" status */
4534 void ZSTD_CCtx_trace(ZSTD_CCtx* cctx, size_t extraCSize)
4536 (void)cctx;
4540 size_t ZSTD_compressEnd (ZSTD_CCtx* cctx,
4545 size_t const cSize = ZSTD_compressContinue_internal(cctx,
4549 endResult = ZSTD_writeEpilogue(cctx, (char*)dst + cSize, dstCapacity-cSize);
4551 assert(!(cctx->appliedParams.fParams.contentSizeFlag && cctx->pledgedSrcSizePlusOne == 0));
4552 if (cctx->pledgedSrcSizePlusOne != 0) { /* control src size */
4556 cctx->pledgedSrcSizePlusOne != cctx->consumedSrcSize+1,
4559 (unsigned)cctx->pledgedSrcSizePlusOne-1,
4560 (unsigned)cctx->consumedSrcSize);
4562 ZSTD_CCtx_trace(cctx, endResult);
4566 size_t ZSTD_compress_advanced (ZSTD_CCtx* cctx,
4574 ZSTD_CCtxParams_init_internal(&cctx->simpleApiParams, &params, ZSTD_NO_CLEVEL);
4575 return ZSTD_compress_advanced_internal(cctx,
4579 &cctx->simpleApiParams);
4584 ZSTD_CCtx* cctx,
4591 FORWARD_IF_ERROR( ZSTD_compressBegin_internal(cctx,
4594 return ZSTD_compressEnd(cctx, dst, dstCapacity, src, srcSize);
4597 size_t ZSTD_compress_usingDict(ZSTD_CCtx* cctx,
4606 ZSTD_CCtxParams_init_internal(&cctx->simpleApiParams, &params, (compressionLevel == 0) ? ZSTD_CLEVEL_DEFAULT: compressionLevel);
4609 return ZSTD_compress_advanced_internal(cctx, dst, dstCapacity, src, srcSize, dict, dictSize, &cctx->simpleApiParams);
4612 size_t ZSTD_compressCCtx(ZSTD_CCtx* cctx,
4618 assert(cctx != NULL);
4619 return ZSTD_compress_usingDict(cctx, dst, dstCapacity, src, srcSize, NULL, 0, compressionLevel);
4627 ZSTD_CCtx* cctx = ZSTD_createCCtx();
4628 RETURN_ERROR_IF(!cctx, memory_allocation, "ZSTD_createCCtx failed");
4629 result = ZSTD_compressCCtx(cctx, dst, dstCapacity, src, srcSize, compressionLevel);
4630 ZSTD_freeCCtx(cctx);
4939 ZSTD_CCtx* const cctx, const ZSTD_CDict* const cdict,
4968 return ZSTD_compressBegin_internal(cctx,
4980 ZSTD_CCtx* const cctx, const ZSTD_CDict* const cdict,
4983 return ZSTD_compressBegin_usingCDict_internal(cctx, cdict, fParams, pledgedSrcSize);
4988 size_t ZSTD_compressBegin_usingCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict)
4991 return ZSTD_compressBegin_usingCDict_internal(cctx, cdict, fParams, ZSTD_CONTENTSIZE_UNKNOWN);
4997 static size_t ZSTD_compress_usingCDict_internal(ZSTD_CCtx* cctx,
5002 FORWARD_IF_ERROR(ZSTD_compressBegin_usingCDict_internal(cctx, cdict, fParams, srcSize), ""); /* will check if cdict != NULL */
5003 return ZSTD_compressEnd(cctx, dst, dstCapacity, src, srcSize);
5009 size_t ZSTD_compress_usingCDict_advanced(ZSTD_CCtx* cctx,
5014 return ZSTD_compress_usingCDict_internal(cctx, dst, dstCapacity, src, srcSize, cdict, fParams);
5022 size_t ZSTD_compress_usingCDict(ZSTD_CCtx* cctx,
5028 return ZSTD_compress_usingCDict_internal(cctx, dst, dstCapacity, src, srcSize, cdict, fParams);
5198 static size_t ZSTD_nextInputSizeHint(const ZSTD_CCtx* cctx)
5200 size_t hintInSize = cctx->inBuffTarget - cctx->inBuffPos;
5201 if (hintInSize==0) hintInSize = cctx->blockSize;
5376 static size_t ZSTD_nextInputSizeHint_MTorST(const ZSTD_CCtx* cctx)
5378 return ZSTD_nextInputSizeHint(cctx);
5391 static void ZSTD_setBufferExpectations(ZSTD_CCtx* cctx, ZSTD_outBuffer const* output, ZSTD_inBuffer const* input)
5393 if (cctx->appliedParams.inBufferMode == ZSTD_bm_stable) {
5394 cctx->expectedInBuffer = *input;
5396 if (cctx->appliedParams.outBufferMode == ZSTD_bm_stable) {
5397 cctx->expectedOutBufferSize = output->size - output->pos;
5404 static size_t ZSTD_checkBufferStability(ZSTD_CCtx const* cctx,
5409 if (cctx->appliedParams.inBufferMode == ZSTD_bm_stable) {
5410 ZSTD_inBuffer const expect = cctx->expectedInBuffer;
5416 if (cctx->appliedParams.outBufferMode == ZSTD_bm_stable) {
5418 if (cctx->expectedOutBufferSize != outBufferSize)
5424 static size_t ZSTD_CCtx_init_compressStream2(ZSTD_CCtx* cctx,
5427 ZSTD_CCtx_params params = cctx->requestedParams;
5428 ZSTD_prefixDict const prefixDict = cctx->prefixDict;
5429 FORWARD_IF_ERROR( ZSTD_initLocalDict(cctx) , ""); /* Init the local dict if present. */
5430 ZSTD_memset(&cctx->prefixDict, 0, sizeof(cctx->prefixDict)); /* single usage */
5431 assert(prefixDict.dict==NULL || cctx->cdict==NULL); /* only one can be set */
5432 if (cctx->cdict && !cctx->localDict.cdict) {
5437 params.compressionLevel = cctx->cdict->compressionLevel;
5440 if (endOp == ZSTD_e_end) cctx->pledgedSrcSizePlusOne = inSize + 1; /* auto-fix pledgedSrcSize */
5444 : (cctx->cdict ? cctx->cdict->dictContentSize : 0);
5445 ZSTD_cParamMode_e const mode = ZSTD_getCParamMode(cctx->cdict, &params, cctx->pledgedSrcSizePlusOne - 1);
5447 &params, cctx->pledgedSrcSizePlusOne-1,
5455 { U64 const pledgedSrcSize = cctx->pledgedSrcSizePlusOne - 1;
5457 FORWARD_IF_ERROR( ZSTD_compressBegin_internal(cctx,
5459 cctx->cdict,
5462 assert(cctx->appliedParams.nbWorkers == 0);
5463 cctx->inToCompress = 0;
5464 cctx->inBuffPos = 0;
5465 if (cctx->appliedParams.inBufferMode == ZSTD_bm_buffered) {
5469 cctx->inBuffTarget = cctx->blockSize + (cctx->blockSize == pledgedSrcSize);
5471 cctx->inBuffTarget = 0;
5473 cctx->outBuffContentSize = cctx->outBuffFlushedSize = 0;
5474 cctx->streamStage = zcss_load;
5475 cctx->frameEnded = 0;
5480 size_t ZSTD_compressStream2( ZSTD_CCtx* cctx,
5490 assert(cctx != NULL);
5493 if (cctx->streamStage == zcss_init) {
5494 FORWARD_IF_ERROR(ZSTD_CCtx_init_compressStream2(cctx, endOp, input->size), "CompressStream2 initialization failed");
5495 ZSTD_setBufferExpectations(cctx, output, input); /* Set initial buffer expectations now that we've initialized */
5499 FORWARD_IF_ERROR(ZSTD_checkBufferStability(cctx, output, input, endOp), "invalid buffers");
5501 FORWARD_IF_ERROR( ZSTD_compressStream_generic(cctx, output, input, endOp) , "");
5503 ZSTD_setBufferExpectations(cctx, output, input);
5504 return cctx->outBuffContentSize - cctx->outBuffFlushedSize; /* remaining to flush */
5508 ZSTD_CCtx* cctx,
5516 size_t const cErr = ZSTD_compressStream2(cctx, &output, &input, endOp);
5522 size_t ZSTD_compress2(ZSTD_CCtx* cctx,
5526 ZSTD_bufferMode_e const originalInBufferMode = cctx->requestedParams.inBufferMode;
5527 ZSTD_bufferMode_e const originalOutBufferMode = cctx->requestedParams.outBufferMode;
5529 ZSTD_CCtx_reset(cctx, ZSTD_reset_session_only);
5531 cctx->requestedParams.inBufferMode = ZSTD_bm_stable;
5532 cctx->requestedParams.outBufferMode = ZSTD_bm_stable;
5535 size_t const result = ZSTD_compressStream2_simpleArgs(cctx,
5540 cctx->requestedParams.inBufferMode = originalInBufferMode;
5541 cctx->requestedParams.outBufferMode = originalOutBufferMode;
5599 ZSTD_copySequencesToSeqStoreExplicitBlockDelim(ZSTD_CCtx* cctx,
5610 if (cctx->cdict) {
5611 dictSize = (U32)cctx->cdict->dictContentSize;
5612 } else if (cctx->prefixDict.dict) {
5613 dictSize = (U32)cctx->prefixDict.dictSize;
5617 ZSTD_memcpy(updatedRepcodes.rep, cctx->blockState.prevCBlock->rep, sizeof(repcodes_t));
5626 if (cctx->appliedParams.validateSequences) {
5629 cctx->appliedParams.cParams.windowLog, dictSize),
5632 RETURN_ERROR_IF(idx - seqPos->idx > cctx->seqStore.maxNbSeq, memory_allocation,
5634 ZSTD_storeSeq(&cctx->seqStore, litLength, ip, iend, offCode, matchLength);
5637 ZSTD_memcpy(cctx->blockState.nextCBlock->rep, updatedRepcodes.rep, sizeof(repcodes_t));
5641 ZSTD_storeLastLiterals(&cctx->seqStore, ip, inSeqs[idx].litLength);
5662 ZSTD_copySequencesToSeqStoreNoBlockDelim(ZSTD_CCtx* cctx, ZSTD_sequencePosition* seqPos,
5676 if (cctx->cdict) {
5677 dictSize = cctx->cdict->dictContentSize;
5678 } else if (cctx->prefixDict.dict) {
5679 dictSize = cctx->prefixDict.dictSize;
5685 ZSTD_memcpy(updatedRepcodes.rep, cctx->blockState.prevCBlock->rep, sizeof(repcodes_t));
5715 if (matchLength > blockSize && firstHalfMatchLength >= cctx->appliedParams.cParams.minMatch) {
5718 if (secondHalfMatchLength < cctx->appliedParams.cParams.minMatch) {
5720 endPosInSequence -= cctx->appliedParams.cParams.minMatch - secondHalfMatchLength;
5721 bytesAdjustment = cctx->appliedParams.cParams.minMatch - secondHalfMatchLength;
5749 if (cctx->appliedParams.validateSequences) {
5752 cctx->appliedParams.cParams.windowLog, dictSize),
5756 RETURN_ERROR_IF(idx - seqPos->idx > cctx->seqStore.maxNbSeq, memory_allocation,
5758 ZSTD_storeSeq(&cctx->seqStore, litLength, ip, iend, offCode, matchLength);
5765 ZSTD_memcpy(cctx->blockState.nextCBlock->rep, updatedRepcodes.rep, sizeof(repcodes_t));
5773 ZSTD_storeLastLiterals(&cctx->seqStore, ip, lastLLSize);
5780 typedef size_t (*ZSTD_sequenceCopier) (ZSTD_CCtx* cctx, ZSTD_sequencePosition* seqPos,
5802 ZSTD_compressSequences_internal(ZSTD_CCtx* cctx,
5816 ZSTD_sequenceCopier const sequenceCopier = ZSTD_selectSequenceCopier(cctx->appliedParams.blockDelimiters);
5832 lastBlock = remaining <= cctx->blockSize;
5833 blockSize = lastBlock ? (U32)remaining : (U32)cctx->blockSize;
5834 ZSTD_resetSeqStore(&cctx->seqStore);
5837 additionalByteAdjustment = sequenceCopier(cctx, &seqPos, inSeqs, inSeqsSize, ip, blockSize);
5854 compressedSeqsSize = ZSTD_entropyCompressSeqStore(&cctx->seqStore,
5855 &cctx->blockState.prevCBlock->entropy, &cctx->blockState.nextCBlock->entropy,
5856 &cctx->appliedParams,
5859 cctx->entropyWorkspace, ENTROPY_WORKSPACE_SIZE /* statically allocated in resetCCtx */,
5860 cctx->bmi2);
5864 if (!cctx->isFirstBlock &&
5865 ZSTD_maybeRLE(&cctx->seqStore) &&
5886 ZSTD_blockState_confirmRepcodesAndEntropyTables(&cctx->blockState);
5887 if (cctx->blockState.prevCBlock->entropy.fse.offcode_repeatMode == FSE_repeat_valid)
5888 cctx->blockState.prevCBlock->entropy.fse.offcode_repeatMode = FSE_repeat_check;
5907 cctx->isFirstBlock = 0;
5914 size_t ZSTD_compressSequences(ZSTD_CCtx* const cctx, void* dst, size_t dstCapacity,
5925 assert(cctx != NULL);
5926 FORWARD_IF_ERROR(ZSTD_CCtx_init_compressStream2(cctx, ZSTD_e_end, srcSize), "CCtx initialization failed");
5928 frameHeaderSize = ZSTD_writeFrameHeader(op, dstCapacity, &cctx->appliedParams, srcSize, cctx->dictID);
5932 if (cctx->appliedParams.fParams.checksumFlag && srcSize) {
5933 xxh64_update(&cctx->xxhState, src, srcSize);
5936 compressedBlocksSize = ZSTD_compressSequences_internal(cctx,
5944 if (cctx->appliedParams.fParams.checksumFlag) {
5945 U32 const checksum = (U32) xxh64_digest(&cctx->xxhState);