19#define FSE_STATIC_LINKING_ONLY
21#define HUF_STATIC_LINKING_ONLY
72 memset(cctx, 0,
sizeof(*cctx));
85 if (!customMem.customAlloc ^ !customMem.customFree)
return NULL;
87 if (!cctx)
return NULL;
98 if ((
size_t)workspace & 7)
return NULL;
141#ifdef ZSTD_MULTITHREAD
142 ZSTDMT_freeCCtx(cctx->mtctx); cctx->mtctx =
NULL;
149 if (cctx==
NULL)
return 0;
151 "not compatible with static CCtx");
155 if (!cctxInWorkspace) {
165#ifdef ZSTD_MULTITHREAD
166 return ZSTDMT_sizeof_CCtx(cctx->mtctx);
176 if (cctx==
NULL)
return 0;
193 ZSTD_compressionParameters cParams)
195 ZSTD_CCtx_params cctxParams;
196 memset(&cctxParams, 0,
sizeof(cctxParams));
197 cctxParams.cParams = cParams;
200 cctxParams.fParams.contentSizeFlag = 1;
205 ZSTD_customMem customMem)
208 if (!customMem.customAlloc ^ !customMem.customFree)
return NULL;
210 sizeof(ZSTD_CCtx_params), customMem);
212 params->customMem = customMem;
214 params->fParams.contentSizeFlag = 1;
237 memset(cctxParams, 0,
sizeof(*cctxParams));
238 cctxParams->compressionLevel = compressionLevel;
239 cctxParams->fParams.contentSizeFlag = 1;
247 memset(cctxParams, 0,
sizeof(*cctxParams));
249 cctxParams->cParams =
params.cParams;
250 cctxParams->fParams =
params.fParams;
258 const ZSTD_CCtx_params* cctxParams,
const ZSTD_parameters*
params)
260 ZSTD_CCtx_params
ret = *cctxParams;
331#ifdef ZSTD_MULTITHREAD
340#ifdef ZSTD_MULTITHREAD
348#ifdef ZSTD_MULTITHREAD
373 bounds.
lowerBound = ZSTD_LDM_BUCKETSIZELOG_MIN;
374 bounds.
upperBound = ZSTD_LDM_BUCKETSIZELOG_MAX;
383 case ZSTD_c_rsyncable:
388 case ZSTD_c_forceMaxWindow :
399 case ZSTD_c_forceAttachDict:
405 case ZSTD_c_literalCompressionMode:
406 ZSTD_STATIC_ASSERT(ZSTD_lcm_auto < ZSTD_lcm_huffman && ZSTD_lcm_huffman < ZSTD_lcm_uncompressed);
411 case ZSTD_c_targetCBlockSize:
412 bounds.
lowerBound = ZSTD_TARGETCBLOCKSIZE_MIN;
413 bounds.
upperBound = ZSTD_TARGETCBLOCKSIZE_MAX;
416 case ZSTD_c_srcSizeHint:
439#define BOUNDCHECK(cParam, val) { \
440 RETURN_ERROR_IF(!ZSTD_cParam_withinBounds(cParam,val), \
441 parameter_outOfBound, "Param out of bounds"); \
463 case ZSTD_c_forceMaxWindow :
467 case ZSTD_c_rsyncable:
473 case ZSTD_c_forceAttachDict:
474 case ZSTD_c_literalCompressionMode:
475 case ZSTD_c_targetCBlockSize:
476 case ZSTD_c_srcSizeHint:
489 RETURN_ERROR(stage_wrong,
"can only set params in ctx init stage");
496 "MT not compatible with static alloc");
512 case ZSTD_c_forceMaxWindow:
513 case ZSTD_c_forceAttachDict:
514 case ZSTD_c_literalCompressionMode:
517 case ZSTD_c_rsyncable:
522 case ZSTD_c_targetCBlockSize:
523 case ZSTD_c_srcSizeHint:
526 default:
RETURN_ERROR(parameter_unsupported,
"unknown parameter");
539 CCtxParams->format = (ZSTD_format_e)
value;
540 return (
size_t)CCtxParams->format;
545 CCtxParams->compressionLevel =
value;
547 if (CCtxParams->compressionLevel >= 0)
return (
size_t)CCtxParams->compressionLevel;
554 CCtxParams->cParams.windowLog = (
U32)
value;
555 return CCtxParams->cParams.windowLog;
560 CCtxParams->cParams.hashLog = (
U32)
value;
561 return CCtxParams->cParams.hashLog;
566 CCtxParams->cParams.chainLog = (
U32)
value;
567 return CCtxParams->cParams.chainLog;
572 CCtxParams->cParams.searchLog = (
U32)
value;
573 return (
size_t)
value;
578 CCtxParams->cParams.minMatch =
value;
579 return CCtxParams->cParams.minMatch;
583 CCtxParams->cParams.targetLength =
value;
584 return CCtxParams->cParams.targetLength;
590 return (
size_t)CCtxParams->cParams.strategy;
595 CCtxParams->fParams.contentSizeFlag =
value != 0;
596 return CCtxParams->fParams.contentSizeFlag;
600 CCtxParams->fParams.checksumFlag =
value != 0;
601 return CCtxParams->fParams.checksumFlag;
605 CCtxParams->fParams.noDictIDFlag = !
value;
606 return !CCtxParams->fParams.noDictIDFlag;
608 case ZSTD_c_forceMaxWindow :
609 CCtxParams->forceWindow = (
value != 0);
610 return CCtxParams->forceWindow;
612 case ZSTD_c_forceAttachDict : {
613 const ZSTD_dictAttachPref_e pref = (ZSTD_dictAttachPref_e)
value;
615 CCtxParams->attachDictPref = pref;
616 return CCtxParams->attachDictPref;
619 case ZSTD_c_literalCompressionMode : {
620 const ZSTD_literalCompressionMode_e lcm = (ZSTD_literalCompressionMode_e)
value;
621 BOUNDCHECK(ZSTD_c_literalCompressionMode, lcm);
622 CCtxParams->literalCompressionMode = lcm;
623 return CCtxParams->literalCompressionMode;
627#ifndef ZSTD_MULTITHREAD
632 CCtxParams->nbWorkers =
value;
633 return CCtxParams->nbWorkers;
637#ifndef ZSTD_MULTITHREAD
643 value = ZSTDMT_JOBSIZE_MIN;
646 CCtxParams->jobSize =
value;
647 return CCtxParams->jobSize;
651#ifndef ZSTD_MULTITHREAD
656 CCtxParams->overlapLog =
value;
657 return CCtxParams->overlapLog;
660 case ZSTD_c_rsyncable :
661#ifndef ZSTD_MULTITHREAD
666 CCtxParams->rsyncable =
value;
667 return CCtxParams->rsyncable;
671 CCtxParams->ldmParams.enableLdm = (
value!=0);
672 return CCtxParams->ldmParams.enableLdm;
677 CCtxParams->ldmParams.hashLog =
value;
678 return CCtxParams->ldmParams.hashLog;
683 CCtxParams->ldmParams.minMatchLength =
value;
684 return CCtxParams->ldmParams.minMatchLength;
689 CCtxParams->ldmParams.bucketSizeLog =
value;
690 return CCtxParams->ldmParams.bucketSizeLog;
694 parameter_outOfBound,
"Param out of bounds!");
695 CCtxParams->ldmParams.hashRateLog =
value;
696 return CCtxParams->ldmParams.hashRateLog;
698 case ZSTD_c_targetCBlockSize :
701 CCtxParams->targetCBlockSize =
value;
702 return CCtxParams->targetCBlockSize;
704 case ZSTD_c_srcSizeHint :
707 CCtxParams->srcSizeHint =
value;
708 return CCtxParams->srcSizeHint;
710 default:
RETURN_ERROR(parameter_unsupported,
"unknown parameter");
725 *
value = CCtxParams->format;
728 *
value = CCtxParams->compressionLevel;
731 *
value = (
int)CCtxParams->cParams.windowLog;
734 *
value = (
int)CCtxParams->cParams.hashLog;
737 *
value = (
int)CCtxParams->cParams.chainLog;
740 *
value = CCtxParams->cParams.searchLog;
743 *
value = CCtxParams->cParams.minMatch;
746 *
value = CCtxParams->cParams.targetLength;
749 *
value = (
unsigned)CCtxParams->cParams.strategy;
752 *
value = CCtxParams->fParams.contentSizeFlag;
755 *
value = CCtxParams->fParams.checksumFlag;
758 *
value = !CCtxParams->fParams.noDictIDFlag;
760 case ZSTD_c_forceMaxWindow :
761 *
value = CCtxParams->forceWindow;
763 case ZSTD_c_forceAttachDict :
764 *
value = CCtxParams->attachDictPref;
766 case ZSTD_c_literalCompressionMode :
767 *
value = CCtxParams->literalCompressionMode;
770#ifndef ZSTD_MULTITHREAD
771 assert(CCtxParams->nbWorkers == 0);
773 *
value = CCtxParams->nbWorkers;
776#ifndef ZSTD_MULTITHREAD
777 RETURN_ERROR(parameter_unsupported,
"not compiled with multithreading");
784#ifndef ZSTD_MULTITHREAD
785 RETURN_ERROR(parameter_unsupported,
"not compiled with multithreading");
787 *
value = CCtxParams->overlapLog;
790 case ZSTD_c_rsyncable :
791#ifndef ZSTD_MULTITHREAD
792 RETURN_ERROR(parameter_unsupported,
"not compiled with multithreading");
794 *
value = CCtxParams->rsyncable;
798 *
value = CCtxParams->ldmParams.enableLdm;
801 *
value = CCtxParams->ldmParams.hashLog;
804 *
value = CCtxParams->ldmParams.minMatchLength;
807 *
value = CCtxParams->ldmParams.bucketSizeLog;
810 *
value = CCtxParams->ldmParams.hashRateLog;
812 case ZSTD_c_targetCBlockSize :
813 *
value = (
int)CCtxParams->targetCBlockSize;
815 case ZSTD_c_srcSizeHint :
816 *
value = (
int)CCtxParams->srcSizeHint;
818 default:
RETURN_ERROR(parameter_unsupported,
"unknown parameter");
833 DEBUGLOG(4,
"ZSTD_CCtx_setParametersUsingCCtxParams");
835 "The context is in the wrong stage!");
837 "Can't override parameters with cdict attached (some must "
838 "be inherited from the cdict).");
846 DEBUGLOG(4,
"ZSTD_CCtx_setPledgedSrcSize to %u bytes", (
U32)pledgedSrcSize);
848 "Can't set pledgedSrcSize when not in init stage.");
892 ZSTD_CCtx* cctx,
const void* dict,
size_t dictSize,
893 ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType)
896 "Can't load a dictionary when ctx is not in init stage.");
898 "no malloc for static CCtx");
899 DEBUGLOG(4,
"ZSTD_CCtx_loadDictionary_advanced (size: %u)", (
U32)dictSize);
901 if (dict ==
NULL || dictSize == 0)
903 if (dictLoadMethod == ZSTD_dlm_byRef) {
908 memcpy(dictBuffer, dict, dictSize);
918 ZSTD_CCtx* cctx,
const void* dict,
size_t dictSize)
921 cctx, dict, dictSize, ZSTD_dlm_byRef, ZSTD_dct_auto);
927 cctx, dict, dictSize, ZSTD_dlm_byCopy, ZSTD_dct_auto);
934 "Can't ref a dict when ctx not in init stage.");
947 ZSTD_CCtx* cctx,
const void* prefix,
size_t prefixSize, ZSTD_dictContentType_e dictContentType)
950 "Can't ref a prefix when ctx not in init stage.");
952 if (prefix !=
NULL && prefixSize > 0) {
972 "Can't reset parameters only when not in init stage.");
998static ZSTD_compressionParameters
1001# define CLAMP_TYPE(cParam, val, type) { \
1002 ZSTD_bounds const bounds = ZSTD_cParam_getBounds(cParam); \
1003 if ((int)val<bounds.lowerBound) val=(type)bounds.lowerBound; \
1004 else if ((int)val>bounds.upperBound) val=(type)bounds.upperBound; \
1006# define CLAMP(cParam, val) CLAMP_TYPE(cParam, val, unsigned)
1022 return hashLog - btScale;
1031static ZSTD_compressionParameters
1033 unsigned long long srcSize,
1036 static const U64 minSrcSize = 513;
1037 static const U64 maxWindowResize = 1ULL << (ZSTD_WINDOWLOG_MAX-1);
1041 srcSize = minSrcSize;
1044 if ( (srcSize < maxWindowResize)
1045 && (dictSize < maxWindowResize) ) {
1046 U32 const tSize = (
U32)(srcSize + dictSize);
1047 static U32 const hashSizeMin = 1 << ZSTD_HASHLOG_MIN;
1048 U32 const srcLog = (tSize < hashSizeMin) ? ZSTD_HASHLOG_MIN :
1050 if (cPar.windowLog > srcLog) cPar.windowLog = srcLog;
1052 if (cPar.hashLog > cPar.windowLog+1) cPar.hashLog = cPar.windowLog+1;
1054 if (cycleLog > cPar.windowLog)
1055 cPar.chainLog -= (cycleLog - cPar.windowLog);
1064ZSTD_compressionParameters
1066 unsigned long long srcSize,
1074static ZSTD_compressionParameters
ZSTD_getCParams_internal(
int compressionLevel,
unsigned long long srcSizeHint,
size_t dictSize);
1075static ZSTD_parameters
ZSTD_getParams_internal(
int compressionLevel,
unsigned long long srcSizeHint,
size_t dictSize);
1078 const ZSTD_CCtx_params* CCtxParams,
U64 srcSizeHint,
size_t dictSize)
1080 ZSTD_compressionParameters cParams;
1082 srcSizeHint = CCtxParams->srcSizeHint;
1086 if (CCtxParams->cParams.windowLog) cParams.windowLog = CCtxParams->cParams.windowLog;
1087 if (CCtxParams->cParams.hashLog) cParams.hashLog = CCtxParams->cParams.hashLog;
1088 if (CCtxParams->cParams.chainLog) cParams.chainLog = CCtxParams->cParams.chainLog;
1089 if (CCtxParams->cParams.searchLog) cParams.searchLog = CCtxParams->cParams.searchLog;
1090 if (CCtxParams->cParams.minMatch) cParams.minMatch = CCtxParams->cParams.minMatch;
1091 if (CCtxParams->cParams.targetLength) cParams.targetLength = CCtxParams->cParams.targetLength;
1092 if (CCtxParams->cParams.strategy) cParams.strategy = CCtxParams->cParams.strategy;
1102 size_t const chainSize = (cParams->strategy ==
ZSTD_fast) ? 0 : ((
size_t)1 << cParams->chainLog);
1103 size_t const hSize = ((
size_t)1) << cParams->hashLog;
1104 U32 const hashLog3 = (forCCtx && cParams->minMatch==3) ?
MIN(ZSTD_HASHLOG3_MAX, cParams->windowLog) : 0;
1105 size_t const h3Size = hashLog3 ? ((
size_t)1) << hashLog3 : 0;
1108 size_t const tableSpace = chainSize *
sizeof(
U32)
1109 + hSize *
sizeof(
U32)
1110 + h3Size *
sizeof(
U32);
1111 size_t const optPotentialSpace =
1118 size_t const optSpace = (forCCtx && (cParams->strategy >=
ZSTD_btopt))
1121 DEBUGLOG(4,
"chainSize: %u - hSize: %u - h3Size: %u",
1122 (
U32)chainSize, (
U32)hSize, (
U32)h3Size);
1123 return tableSpace + optSpace;
1128 RETURN_ERROR_IF(
params->nbWorkers > 0, GENERIC,
"Estimate CCtx size is supported for single-threaded compression only.");
1129 { ZSTD_compressionParameters
const cParams =
1132 U32 const divider = (cParams.minMatch==3) ? 3 : 4;
1133 size_t const maxNbSeq = blockSize / divider;
1152 size_t const neededSpace =
1162 DEBUGLOG(5,
"estimate workspace : %u", (
U32)neededSpace);
1182 size_t memBudget = 0;
1185 if (newMB > memBudget) memBudget = newMB;
1192 RETURN_ERROR_IF(
params->nbWorkers > 0, GENERIC,
"Estimate CCtx size is supported for single-threaded compression only.");
1193 { ZSTD_compressionParameters
const cParams =
1197 size_t const inBuffSize = ((
size_t)1 << cParams.windowLog) + blockSize;
1202 return CCtxSize + streamingSize;
1221 size_t memBudget = 0;
1224 if (newMB > memBudget) memBudget = newMB;
1235#ifdef ZSTD_MULTITHREAD
1237 return ZSTDMT_getFrameProgression(cctx->mtctx);
1240 { ZSTD_frameProgression fp;
1241 size_t const buffered = (cctx->
inBuff ==
NULL) ? 0 :
1249 fp.currentJobID = 0;
1250 fp.nbActiveWorkers = 0;
1259#ifdef ZSTD_MULTITHREAD
1261 return ZSTDMT_toFlushNow(cctx->mtctx);
1269 ZSTD_compressionParameters cParams2)
1273 assert(cParams1.windowLog == cParams2.windowLog);
1274 assert(cParams1.chainLog == cParams2.chainLog);
1275 assert(cParams1.hashLog == cParams2.hashLog);
1276 assert(cParams1.searchLog == cParams2.searchLog);
1277 assert(cParams1.minMatch == cParams2.minMatch);
1278 assert(cParams1.targetLength == cParams2.targetLength);
1279 assert(cParams1.strategy == cParams2.strategy);
1287 bs->entropy.huf.repeatMode = HUF_repeat_none;
1288 bs->entropy.fse.offcode_repeatMode = FSE_repeat_none;
1289 bs->entropy.fse.matchlength_repeatMode = FSE_repeat_none;
1290 bs->entropy.fse.litlength_repeatMode = FSE_repeat_none;
1347 const ZSTD_compressionParameters* cParams,
1352 size_t const chainSize = (cParams->strategy ==
ZSTD_fast) ? 0 : ((
size_t)1 << cParams->chainLog);
1353 size_t const hSize = ((
size_t)1) << cParams->hashLog;
1354 U32 const hashLog3 = ((forWho ==
ZSTD_resetTarget_CCtx) && cParams->minMatch==3) ?
MIN(ZSTD_HASHLOG3_MAX, cParams->windowLog) : 0;
1355 size_t const h3Size = hashLog3 ? ((
size_t)1) << hashLog3 : 0;
1371 DEBUGLOG(5,
"reserving table space");
1377 "failed a workspace allocation in ZSTD_reset_matchState");
1387 DEBUGLOG(4,
"reserving optimal parser space");
1399 "failed a workspace allocation in ZSTD_reset_matchState");
1411#define ZSTD_INDEXOVERFLOW_MARGIN (16 MB)
1421 U64 const pledgedSrcSize,
1426 DEBUGLOG(4,
"ZSTD_resetCCtx_internal: pledgedSrcSize=%u, wlog=%u",
1427 (
U32)pledgedSrcSize,
params.cParams.windowLog);
1432 if (
params.ldmParams.enableLdm) {
1440 {
size_t const windowSize =
MAX(1, (
size_t)
MIN(((
U64)1 <<
params.cParams.windowLog), pledgedSrcSize));
1442 U32 const divider = (
params.cParams.minMatch==3) ? 3 : 4;
1443 size_t const maxNbSeq = blockSize / divider;
1448 size_t const buffInSize = (zbuff==
ZSTDb_buffered) ? windowSize + blockSize : 0;
1468 size_t const neededSpace =
1481 DEBUGLOG(4,
"Need %zuKB workspace, including %zuKB for match state, and %zuKB for buffers",
1482 neededSpace>>10, matchStateSize>>10, bufferSpace>>10);
1483 DEBUGLOG(4,
"windowSize: %zu - blockSize: %zu", windowSize, blockSize);
1485 if (workspaceTooSmall || workspaceWasteful) {
1486 DEBUGLOG(4,
"Resize workspaceSize from %zuKB to %zuKB",
1497 DEBUGLOG(5,
"reserving object space");
1520 DEBUGLOG(4,
"pledged content size : %u ; flag : %u",
1521 (
unsigned)pledgedSrcSize, zc->
appliedParams.fParams.contentSizeFlag);
1543 if (
params.ldmParams.enableLdm) {
1545 size_t const ldmBucketSize =
1547 params.ldmParams.bucketSizeLog);
1569 if (
params.ldmParams.enableLdm) {
1571 size_t const ldmHSize = ((
size_t)1) <<
params.ldmParams.hashLog;
1617 const ZSTD_CCtx_params*
params,
1621 return ( pledgedSrcSize <= cutoff
1623 ||
params->attachDictPref == ZSTD_dictForceAttach )
1624 &&
params->attachDictPref != ZSTD_dictForceCopy
1636 {
const ZSTD_compressionParameters*
const cdict_cParams = &cdict->
matchState.
cParams;
1637 unsigned const windowLog =
params.cParams.windowLog;
1643 params.cParams.windowLog = windowLog;
1652 if (cdictLen == 0) {
1654 DEBUGLOG(4,
"skipping attaching empty dictionary");
1656 DEBUGLOG(4,
"attaching dictionary into context");
1686 DEBUGLOG(4,
"copying dictionary into context");
1688 {
unsigned const windowLog =
params.cParams.windowLog;
1691 params.cParams = *cdict_cParams;
1692 params.cParams.windowLog = windowLog;
1703 {
size_t const chainSize = (cdict_cParams->strategy ==
ZSTD_fast) ? 0 : ((
size_t)1 << cdict_cParams->chainLog);
1704 size_t const hSize = (
size_t)1 << cdict_cParams->hashLog;
1708 hSize *
sizeof(
U32));
1711 chainSize *
sizeof(
U32));
1716 size_t const h3Size = h3log ? ((
size_t)1 << h3log) : 0;
1744 const ZSTD_CCtx_params*
params,
1749 DEBUGLOG(4,
"ZSTD_resetCCtx_usingCDict (pledgedSrcSize=%u)",
1750 (
unsigned)pledgedSrcSize);
1754 cctx, cdict, *
params, pledgedSrcSize, zbuff);
1757 cctx, cdict, *
params, pledgedSrcSize, zbuff);
1770 ZSTD_frameParameters fParams,
1774 DEBUGLOG(5,
"ZSTD_copyCCtx_internal");
1776 "Can't copy a ctx that's not in init stage.");
1782 params.fParams = fParams;
1798 size_t const h3Size = h3log ? ((
size_t)1 << h3log) : 0;
1802 hSize *
sizeof(
U32));
1805 chainSize *
sizeof(
U32));
1808 h3Size *
sizeof(
U32));
1836 ZSTD_frameParameters fParams = { 1 , 0 , 0 };
1843 fParams, pledgedSrcSize,
1848#define ZSTD_ROWSIZE 16
1864#if defined (MEMORY_SANITIZER) && !defined (ZSTD_MSAN_DONT_POISON_WORKSPACE)
1877 for (rowNb=0 ; rowNb < nbRows ; rowNb++) {
1882 table[cellNb] += adder;
1884 if (
table[cellNb] < reducerValue)
table[cellNb] = 0;
1885 else table[cellNb] -= reducerValue;
1909 U32 const chainSize = (
U32)1 <<
params->cParams.chainLog;
1932 BYTE*
const llCodeTable = seqStorePtr->
llCode;
1933 BYTE*
const ofCodeTable = seqStorePtr->
ofCode;
1934 BYTE*
const mlCodeTable = seqStorePtr->
mlCode;
1937 assert(nbSeq <= seqStorePtr->maxNbSeq);
1938 for (
u=0;
u<nbSeq;
u++) {
1957 DEBUGLOG(5,
"ZSTD_useTargetCBlockSize (targetCBlockSize=%zu)", cctxParams->targetCBlockSize);
1958 return (cctxParams->targetCBlockSize != 0);
1967 const ZSTD_CCtx_params* cctxParams,
1968 void*
dst,
size_t dstCapacity,
1969 void* entropyWorkspace,
size_t entropyWkspSize,
1973 ZSTD_strategy const strategy = cctxParams->cParams.strategy;
1978 U32 LLtype, Offtype, MLtype;
1980 const BYTE*
const ofCodeTable = seqStorePtr->
ofCode;
1981 const BYTE*
const llCodeTable = seqStorePtr->
llCode;
1982 const BYTE*
const mlCodeTable = seqStorePtr->
mlCode;
1984 BYTE*
const oend = ostart + dstCapacity;
1990 DEBUGLOG(5,
"ZSTD_compressSequences_internal (nbSeq=%zu)", nbSeq);
1995 size_t const litSize = (
size_t)(seqStorePtr->
lit - literals);
1997 &prevEntropy->
huf, &nextEntropy->
huf,
1998 cctxParams->cParams.strategy,
2002 entropyWorkspace, entropyWkspSize,
2005 assert(cSize <= dstCapacity);
2011 dstSize_tooSmall,
"Can't fit seq hdr in output buf!");
2015 op[0] = (
BYTE)((nbSeq>>8) + 0x80);
2026 memcpy(&nextEntropy->
fse, &prevEntropy->
fse,
sizeof(prevEntropy->
fse));
2027 return (
size_t)(
op - ostart);
2047 assert(!(LLtype < set_compressed && nextEntropy->fse.litlength_repeatMode != FSE_repeat_none));
2049 op, (
size_t)(oend -
op),
2055 entropyWorkspace, entropyWkspSize);
2065 count, &
max, ofCodeTable, nbSeq, entropyWorkspace, entropyWkspSize);
2074 defaultPolicy, strategy);
2075 assert(!(Offtype < set_compressed && nextEntropy->fse.offcode_repeatMode != FSE_repeat_none));
2077 op, (
size_t)(oend -
op),
2083 entropyWorkspace, entropyWkspSize);
2093 count, &
max, mlCodeTable, nbSeq, entropyWorkspace, entropyWkspSize);
2094 DEBUGLOG(5,
"Building ML table (remaining space : %i)", (
int)(oend-
op));
2101 assert(!(MLtype < set_compressed && nextEntropy->fse.matchlength_repeatMode != FSE_repeat_none));
2103 op, (
size_t)(oend -
op),
2109 entropyWorkspace, entropyWkspSize);
2117 *seqHead = (
BYTE)((LLtype<<6) + (Offtype<<4) + (MLtype<<2));
2120 op, (
size_t)(oend -
op),
2121 CTable_MatchLength, mlCodeTable,
2122 CTable_OffsetBits, ofCodeTable,
2123 CTable_LitLength, llCodeTable,
2127 op += bitstreamSize;
2137 if (lastNCount && (
op - lastNCount) < 4) {
2140 DEBUGLOG(5,
"Avoiding bug in zstd decoder in versions <= 1.3.4 by "
2141 "emitting an uncompressed block.");
2146 DEBUGLOG(5,
"compressed block size : %u", (
unsigned)(
op - ostart));
2147 return (
size_t)(
op - ostart);
2154 const ZSTD_CCtx_params* cctxParams,
2155 void*
dst,
size_t dstCapacity,
2157 void* entropyWorkspace,
size_t entropyWkspSize,
2161 seqStorePtr, prevEntropy, nextEntropy, cctxParams,
2163 entropyWorkspace, entropyWkspSize, bmi2);
2164 if (cSize == 0)
return 0;
2168 if ((cSize ==
ERROR(dstSize_tooSmall)) & (srcSize <= dstCapacity))
2173 {
size_t const maxCSize = srcSize -
ZSTD_minGain(srcSize, cctxParams->cParams.strategy);
2174 if (cSize >= maxCSize)
return 0;
2221 selectedCompressor = blockCompressor[(
int)dictMode][(
int)strat];
2223 return selectedCompressor;
2227 const BYTE* anchor,
size_t lastLLSize)
2229 memcpy(seqStorePtr->
lit, anchor, lastLLSize);
2230 seqStorePtr->
lit += lastLLSize;
2245 DEBUGLOG(5,
"ZSTD_buildSeqStore (srcSize=%zu)", srcSize);
2308 {
const BYTE*
const lastLiterals = (
const BYTE*)
src + srcSize - lastLLSize;
2318 size_t seqsSize = seqStore->
sequences - seqs;
2321 size_t i;
size_t position;
int repIdx;
2324 for (
i = 0, position = 0;
i < seqsSize; ++
i) {
2325 outSeqs[
i].offset = seqs[
i].
offset;
2331 outSeqs[
i].litLength += 0x10000;
2333 outSeqs[
i].matchLength += 0x10000;
2338 outSeqs[
i].rep = outSeqs[
i].offset;
2341 if (outSeqs[
i].litLength == 0) {
2345 repIdx = (
unsigned int)
i - 1;
2350 outSeqs[
i].offset = repIdx >= 0 ? outSeqs[repIdx].offset :
repStartValue[-repIdx - 1];
2351 if (outSeqs[
i].rep == 4) {
2352 --outSeqs[
i].offset;
2358 position += outSeqs[
i].litLength;
2359 outSeqs[
i].matchPos = (
unsigned int)position;
2360 position += outSeqs[
i].matchLength;
2366 size_t outSeqsSize,
const void*
src,
size_t srcSize)
2388 if (
length < 2)
return 1;
2390 if (
ip[0] !=
ip[
i])
return 0;
2404 return nbSeqs < 4 && nbLits < 10;
2415 void*
dst,
size_t dstCapacity,
2416 const void*
src,
size_t srcSize,
U32 frame)
2422 const U32 rleMaxLength = 25;
2426 DEBUGLOG(5,
"ZSTD_compressBlock_internal (dstCapacity=%u, dictLimit=%u, nextToUpdate=%u)",
2455 cSize < rleMaxLength &&
2477 void*
dst,
size_t dstCapacity,
2478 const void*
src,
size_t srcSize,
2479 const size_t bss,
U32 lastBlock)
2481 DEBUGLOG(6,
"Attempting ZSTD_compressSuperBlock()");
2513 if (cSize !=
ERROR(dstSize_tooSmall)) {
2524 DEBUGLOG(6,
"Resorting to ZSTD_noCompressBlock()");
2532 void*
dst,
size_t dstCapacity,
2533 const void*
src,
size_t srcSize,
2538 DEBUGLOG(5,
"ZSTD_compressBlock_targetCBlockSize (dstCapacity=%u, dictLimit=%u, nextToUpdate=%u, srcSize=%zu)",
2543 FORWARD_IF_ERROR(cSize,
"ZSTD_compressBlock_targetCBlockSize_body failed");
2553 ZSTD_CCtx_params
const*
params,
2558 U32 const maxDist = (
U32)1 <<
params->cParams.windowLog;
2583 void*
dst,
size_t dstCapacity,
2584 const void*
src,
size_t srcSize,
2588 size_t remaining = srcSize;
2596 DEBUGLOG(5,
"ZSTD_compress_frameChunk (blockSize=%u)", (
unsigned)blockSize);
2602 U32 const lastBlock = lastFrameChunk & (blockSize >= remaining);
2606 "not enough space to store compressed block");
2607 if (remaining < blockSize) blockSize = remaining;
2632 U32 const cBlockHeader = cSize == 1 ?
2633 lastBlock + (((
U32)
bt_rle)<<1) + (
U32)(blockSize << 3) :
2642 assert(remaining >= blockSize);
2643 remaining -= blockSize;
2645 assert(dstCapacity >= cSize);
2646 dstCapacity -= cSize;
2648 DEBUGLOG(5,
"ZSTD_compress_frameChunk: adding a block of size %u",
2653 return (
size_t)(
op-ostart);
2658 const ZSTD_CCtx_params*
params,
U64 pledgedSrcSize,
U32 dictID)
2660 U32 const dictIDSizeCodeLength = (dictID>0) + (dictID>=256) + (dictID>=65536);
2661 U32 const dictIDSizeCode =
params->fParams.noDictIDFlag ? 0 : dictIDSizeCodeLength;
2662 U32 const checksumFlag =
params->fParams.checksumFlag>0;
2663 U32 const windowSize = (
U32)1 <<
params->cParams.windowLog;
2664 U32 const singleSegment =
params->fParams.contentSizeFlag && (windowSize >= pledgedSrcSize);
2666 U32 const fcsCode =
params->fParams.contentSizeFlag ?
2667 (pledgedSrcSize>=256) + (pledgedSrcSize>=65536+256) + (pledgedSrcSize>=0xFFFFFFFFU) : 0;
2668 BYTE const frameHeaderDescriptionByte = (
BYTE)(dictIDSizeCode + (checksumFlag<<2) + (singleSegment<<5) + (fcsCode<<6) );
2672 RETURN_ERROR_IF(dstCapacity < ZSTD_FRAMEHEADERSIZE_MAX, dstSize_tooSmall,
2673 "dst buf is too small to fit worst-case frame header size.");
2674 DEBUGLOG(4,
"ZSTD_writeFrameHeader : dictIDFlag : %u ; dictID : %u ; dictIDSizeCode : %u",
2675 !
params->fParams.noDictIDFlag, (
unsigned)dictID, (
unsigned)dictIDSizeCode);
2677 if (
params->format == ZSTD_f_zstd1) {
2681 op[
pos++] = frameHeaderDescriptionByte;
2682 if (!singleSegment)
op[
pos++] = windowLogByte;
2683 switch(dictIDSizeCode)
2694 case 0 :
if (singleSegment)
op[
pos++] = (
BYTE)(pledgedSrcSize);
break;
2710 "dst buf is too small to write frame trailer empty block.");
2720 "wrong cctx stage");
2722 parameter_unsupported,
2723 "incompatible with ldm");
2733 void*
dst,
size_t dstCapacity,
2734 const void*
src,
size_t srcSize,
2735 U32 frame,
U32 lastFrameChunk)
2740 DEBUGLOG(5,
"ZSTD_compressContinue_internal, stage: %u, srcSize: %u",
2741 cctx->
stage, (
unsigned)srcSize);
2743 "missing init (ZSTD_compressBegin)");
2749 assert(fhSize <= dstCapacity);
2750 dstCapacity -= fhSize;
2751 dst = (
char*)
dst + fhSize;
2755 if (!srcSize)
return fhSize;
2771 DEBUGLOG(5,
"ZSTD_compressContinue_internal (blockSize=%u)", (
unsigned)cctx->
blockSize);
2772 {
size_t const cSize = frame ?
2775 FORWARD_IF_ERROR(cSize,
"%s", frame ?
"ZSTD_compress_frameChunk failed" :
"ZSTD_compressBlock_internal failed");
2784 "error : pledgedSrcSize = %u, while realSrcSize >= %u",
2788 return cSize + fhSize;
2793 void*
dst,
size_t dstCapacity,
2794 const void*
src,
size_t srcSize)
2796 DEBUGLOG(5,
"ZSTD_compressContinue (srcSize=%u)", (
unsigned)srcSize);
2803 ZSTD_compressionParameters
const cParams = cctx->
appliedParams.cParams;
2810 DEBUGLOG(5,
"ZSTD_compressBlock: srcSize = %u", (
unsigned)srcSize);
2812 RETURN_ERROR_IF(srcSize > blockSizeMax, srcSize_wrong,
"input is larger than a block"); }
2823 ZSTD_CCtx_params
const*
params,
2824 const void*
src,
size_t srcSize,
2828 const BYTE*
const iend =
ip + srcSize;
2835 ls->loadedDictEnd =
params->forceWindow ? 0 : (
U32)(iend -
ls->window.base);
2844 size_t const remaining = (
size_t)(iend -
ip);
2853 switch(
params->cParams.strategy)
2893static size_t ZSTD_checkDictNCount(
short* normalizedCounter,
unsigned dictMaxSymbolValue,
unsigned maxSymbolValue) {
2895 RETURN_ERROR_IF(dictMaxSymbolValue < maxSymbolValue, dictionary_corrupted,
"dict fse tables don't have all symbols");
2896 for (
s = 0;
s <= maxSymbolValue; ++
s) {
2897 RETURN_ERROR_IF(normalizedCounter[
s] == 0, dictionary_corrupted,
"dict fse tables don't have all symbols");
2903 short* offcodeNCount,
unsigned* offcodeMaxValue,
2904 const void*
const dict,
size_t dictSize)
2906 const BYTE* dictPtr = (
const BYTE*)dict;
2907 const BYTE*
const dictEnd = dictPtr + dictSize;
2909 bs->entropy.huf.repeatMode = HUF_repeat_check;
2911 {
unsigned maxSymbolValue = 255;
2912 unsigned hasZeroWeights = 1;
2913 size_t const hufHeaderSize =
HUF_readCTable((HUF_CElt*)
bs->entropy.huf.CTable, &maxSymbolValue, dictPtr,
2914 dictEnd-dictPtr, &hasZeroWeights);
2918 if (!hasZeroWeights)
2919 bs->entropy.huf.repeatMode = HUF_repeat_valid;
2923 dictPtr += hufHeaderSize;
2926 {
unsigned offcodeLog;
2927 size_t const offcodeHeaderSize =
FSE_readNCount(offcodeNCount, offcodeMaxValue, &offcodeLog, dictPtr, dictEnd-dictPtr);
2933 bs->entropy.fse.offcodeCTable,
2934 offcodeNCount,
MaxOff, offcodeLog,
2936 dictionary_corrupted,
"");
2937 dictPtr += offcodeHeaderSize;
2940 {
short matchlengthNCount[
MaxML+1];
2941 unsigned matchlengthMaxValue =
MaxML, matchlengthLog;
2942 size_t const matchlengthHeaderSize =
FSE_readNCount(matchlengthNCount, &matchlengthMaxValue, &matchlengthLog, dictPtr, dictEnd-dictPtr);
2948 bs->entropy.fse.matchlengthCTable,
2949 matchlengthNCount, matchlengthMaxValue, matchlengthLog,
2951 dictionary_corrupted,
"");
2952 dictPtr += matchlengthHeaderSize;
2955 {
short litlengthNCount[
MaxLL+1];
2956 unsigned litlengthMaxValue =
MaxLL, litlengthLog;
2957 size_t const litlengthHeaderSize =
FSE_readNCount(litlengthNCount, &litlengthMaxValue, &litlengthLog, dictPtr, dictEnd-dictPtr);
2963 bs->entropy.fse.litlengthCTable,
2964 litlengthNCount, litlengthMaxValue, litlengthLog,
2966 dictionary_corrupted,
"");
2967 dictPtr += litlengthHeaderSize;
2976 return dictPtr - (
const BYTE*)dict;
2991 ZSTD_CCtx_params
const*
params,
2992 const void* dict,
size_t dictSize,
2996 const BYTE* dictPtr = (
const BYTE*)dict;
2997 const BYTE*
const dictEnd = dictPtr + dictSize;
2998 short offcodeNCount[
MaxOff+1];
2999 unsigned offcodeMaxValue =
MaxOff;
3008 eSize =
ZSTD_loadCEntropy(
bs, workspace, offcodeNCount, &offcodeMaxValue, dict, dictSize);
3012 {
size_t const dictContentSize = (
size_t)(dictEnd - dictPtr);
3014 if (dictContentSize <= ((
U32)-1) - 128
KB) {
3015 U32 const maxOffset = (
U32)dictContentSize + 128
KB;
3022 for (
u=0;
u<3;
u++) {
3027 bs->entropy.fse.offcode_repeatMode = FSE_repeat_valid;
3028 bs->entropy.fse.matchlength_repeatMode = FSE_repeat_valid;
3029 bs->entropy.fse.litlength_repeatMode = FSE_repeat_valid;
3031 ms,
NULL,
ws,
params, dictPtr, dictContentSize, dtlm),
"");
3043 const ZSTD_CCtx_params*
params,
3044 const void* dict,
size_t dictSize,
3045 ZSTD_dictContentType_e dictContentType,
3049 DEBUGLOG(4,
"ZSTD_compress_insertDictionary (dictSize=%u)", (
U32)dictSize);
3050 if ((dict==
NULL) || (dictSize<8)) {
3051 RETURN_ERROR_IF(dictContentType == ZSTD_dct_fullDict, dictionary_wrong,
"");
3058 if (dictContentType == ZSTD_dct_rawContent)
3062 if (dictContentType == ZSTD_dct_auto) {
3063 DEBUGLOG(4,
"raw content dictionary detected");
3067 RETURN_ERROR_IF(dictContentType == ZSTD_dct_fullDict, dictionary_wrong,
"");
3073 bs, ms,
ws,
params, dict, dictSize, dtlm, workspace);
3076#define ZSTD_USE_CDICT_PARAMS_SRCSIZE_CUTOFF (128 KB)
3077#define ZSTD_USE_CDICT_PARAMS_DICTSIZE_MULTIPLIER (6)
3082 const void* dict,
size_t dictSize,
3083 ZSTD_dictContentType_e dictContentType,
3086 const ZSTD_CCtx_params*
params,
U64 pledgedSrcSize,
3089 DEBUGLOG(4,
"ZSTD_compressBegin_internal: wlog=%u",
params->cParams.windowLog);
3092 assert(!((dict) && (cdict)));
3099 && (
params->attachDictPref != ZSTD_dictForceLoad) ) {
3105 {
size_t const dictID = cdict ?
3123 const void* dict,
size_t dictSize,
3124 ZSTD_dictContentType_e dictContentType,
3127 const ZSTD_CCtx_params*
params,
3128 unsigned long long pledgedSrcSize)
3130 DEBUGLOG(4,
"ZSTD_compressBegin_advanced_internal: wlog=%u",
params->cParams.windowLog);
3134 dict, dictSize, dictContentType, dtlm,
3143 const void* dict,
size_t dictSize,
3144 ZSTD_parameters
params,
unsigned long long pledgedSrcSize)
3146 ZSTD_CCtx_params
const cctxParams =
3151 &cctxParams, pledgedSrcSize);
3157 ZSTD_CCtx_params
const cctxParams =
3159 DEBUGLOG(4,
"ZSTD_compressBegin_usingDict (dictSize=%u)", (
unsigned)dictSize);
3186 dstCapacity -= fhSize;
3194 RETURN_ERROR_IF(dstCapacity<4, dstSize_tooSmall,
"no room for epilogue");
3202 RETURN_ERROR_IF(dstCapacity<4, dstSize_tooSmall,
"no room for checksum");
3203 DEBUGLOG(4,
"ZSTD_writeEpilogue: write checksum : %08X", (
unsigned)
checksum);
3213 void*
dst,
size_t dstCapacity,
3214 const void*
src,
size_t srcSize)
3218 dst, dstCapacity,
src, srcSize,
3226 DEBUGLOG(4,
"end of frame : controlling src size");
3230 "error : pledgedSrcSize = %u, while realSrcSize = %u",
3234 return cSize + endResult;
3239 void*
dst,
size_t dstCapacity,
3240 const void*
src,
size_t srcSize,
3241 const void* dict,
size_t dictSize,
3242 const ZSTD_parameters*
params)
3244 ZSTD_CCtx_params
const cctxParams =
3246 DEBUGLOG(4,
"ZSTD_compress_internal");
3255 void*
dst,
size_t dstCapacity,
3256 const void*
src,
size_t srcSize,
3257 const void* dict,
size_t dictSize,
3260 DEBUGLOG(4,
"ZSTD_compress_advanced");
3272 void*
dst,
size_t dstCapacity,
3273 const void*
src,
size_t srcSize,
3274 const void* dict,
size_t dictSize,
3275 const ZSTD_CCtx_params*
params)
3277 DEBUGLOG(4,
"ZSTD_compress_advanced_internal (srcSize:%u)", (
unsigned)srcSize);
3285 void*
dst,
size_t dstCapacity,
3286 const void*
src,
size_t srcSize,
3287 const void* dict,
size_t dictSize,
3288 int compressionLevel)
3292 DEBUGLOG(4,
"ZSTD_compress_usingDict (srcSize=%u)", (
unsigned)srcSize);
3298 void*
dst,
size_t dstCapacity,
3299 const void*
src,
size_t srcSize,
3300 int compressionLevel)
3302 DEBUGLOG(4,
"ZSTD_compressCCtx (srcSize=%u)", (
unsigned)srcSize);
3308 const void*
src,
size_t srcSize,
3309 int compressionLevel)
3325 size_t dictSize, ZSTD_compressionParameters cParams,
3326 ZSTD_dictLoadMethod_e dictLoadMethod)
3332 + (dictLoadMethod == ZSTD_dlm_byRef ? 0
3344 if (cdict==
NULL)
return 0;
3345 DEBUGLOG(5,
"sizeof(*cdict) : %u", (
unsigned)
sizeof(*cdict));
3353 const void* dictBuffer,
size_t dictSize,
3354 ZSTD_dictLoadMethod_e dictLoadMethod,
3355 ZSTD_dictContentType_e dictContentType,
3356 ZSTD_compressionParameters cParams)
3358 DEBUGLOG(3,
"ZSTD_initCDict_internal (dictContentType:%u)", (
unsigned)dictContentType);
3361 if ((dictLoadMethod == ZSTD_dlm_byRef) || (!dictBuffer) || (!dictSize)) {
3367 memcpy(internalBuffer, dictBuffer, dictSize);
3386 { ZSTD_CCtx_params
params;
3389 params.fParams.contentSizeFlag = 1;
3390 params.cParams = cParams;
3405 ZSTD_dictLoadMethod_e dictLoadMethod,
3406 ZSTD_dictContentType_e dictContentType,
3407 ZSTD_compressionParameters cParams, ZSTD_customMem customMem)
3409 DEBUGLOG(3,
"ZSTD_createCDict_advanced, mode %u", (
unsigned)dictContentType);
3410 if (!customMem.customAlloc ^ !customMem.customFree)
return NULL;
3412 {
size_t const workspaceSize =
3416 (dictLoadMethod == ZSTD_dlm_byRef ? 0
3418 void*
const workspace =
ZSTD_malloc(workspaceSize, customMem);
3436 dictBuffer, dictSize,
3437 dictLoadMethod, dictContentType,
3451 ZSTD_dlm_byCopy, ZSTD_dct_auto,
3452 cParams, ZSTD_defaultCMem);
3462 ZSTD_dlm_byRef, ZSTD_dct_auto,
3463 cParams, ZSTD_defaultCMem);
3468 if (cdict==
NULL)
return 0;
3469 { ZSTD_customMem
const cMem = cdict->
customMem;
3472 if (!cdictInWorkspace) {
3493 void* workspace,
size_t workspaceSize,
3494 const void* dict,
size_t dictSize,
3495 ZSTD_dictLoadMethod_e dictLoadMethod,
3496 ZSTD_dictContentType_e dictContentType,
3497 ZSTD_compressionParameters cParams)
3501 + (dictLoadMethod == ZSTD_dlm_byRef ? 0
3507 if ((
size_t)workspace & 7)
return NULL;
3517 DEBUGLOG(4,
"(workspaceSize < neededSize) : (%u < %u) => %u",
3518 (
unsigned)workspaceSize, (
unsigned)neededSize, (
unsigned)(workspaceSize < neededSize));
3519 if (workspaceSize < neededSize)
return NULL;
3523 dictLoadMethod, dictContentType,
3540 ZSTD_frameParameters
const fParams,
unsigned long long const pledgedSrcSize)
3542 DEBUGLOG(4,
"ZSTD_compressBegin_usingCDict_advanced");
3549 && (
params.attachDictPref != ZSTD_dictForceLoad) ?
3559 U32 const limitedSrcSize = (
U32)
MIN(pledgedSrcSize, 1U << 19);
3560 U32 const limitedSrcLog = limitedSrcSize > 1 ?
ZSTD_highbit32(limitedSrcSize - 1) + 1 : 1;
3561 params.cParams.windowLog =
MAX(
params.cParams.windowLog, limitedSrcLog);
3563 params.fParams = fParams;
3577 ZSTD_frameParameters
const fParams = { 0 , 0 , 0 };
3578 DEBUGLOG(4,
"ZSTD_compressBegin_usingCDict : dictIDFlag == %u", !fParams.noDictIDFlag);
3583 void*
dst,
size_t dstCapacity,
3584 const void*
src,
size_t srcSize,
3585 const ZSTD_CDict* cdict, ZSTD_frameParameters fParams)
3597 void*
dst,
size_t dstCapacity,
3598 const void*
src,
size_t srcSize,
3601 ZSTD_frameParameters
const fParams = { 1 , 0 , 0 };
3644 const void*
const dict,
size_t const dictSize, ZSTD_dictContentType_e
const dictContentType,
3646 ZSTD_CCtx_params
params,
unsigned long long const pledgedSrcSize)
3648 DEBUGLOG(4,
"ZSTD_resetCStream_internal");
3653 assert(!((dict) && (cdict)));
3680 DEBUGLOG(4,
"ZSTD_resetCStream: pledgedSrcSize = %u", (
unsigned)pledgedSrcSize);
3691 const void* dict,
size_t dictSize,
const ZSTD_CDict* cdict,
3692 const ZSTD_CCtx_params*
params,
3693 unsigned long long pledgedSrcSize)
3695 DEBUGLOG(4,
"ZSTD_initCStream_internal");
3700 assert(!((dict) && (cdict)));
3714 ZSTD_frameParameters fParams,
3715 unsigned long long pledgedSrcSize)
3717 DEBUGLOG(4,
"ZSTD_initCStream_usingCDict_advanced");
3728 DEBUGLOG(4,
"ZSTD_initCStream_usingCDict");
3740 const void* dict,
size_t dictSize,
3741 ZSTD_parameters
params,
unsigned long long pss)
3748 DEBUGLOG(4,
"ZSTD_initCStream_advanced");
3759 DEBUGLOG(4,
"ZSTD_initCStream_usingDict");
3773 DEBUGLOG(4,
"ZSTD_initCStream_srcSize");
3795 if (hintInSize==0) hintInSize = cctx->
blockSize;
3808 const char*
const istart = (
const char*)
input->src;
3809 const char*
const iend =
input->size != 0 ? istart +
input->size : istart;
3810 const char*
ip =
input->pos != 0 ? istart +
input->pos : istart;
3811 char*
const ostart = (
char*)output->
dst;
3812 char*
const oend = output->
size != 0 ? ostart + output->
size : ostart;
3813 char*
op = output->
pos != 0 ? ostart + output->
pos : ostart;
3814 U32 someMoreWork = 1;
3817 DEBUGLOG(5,
"ZSTD_compressStream_generic, flush=%u", (
unsigned)flushMode);
3825 while (someMoreWork) {
3829 RETURN_ERROR(init_missing,
"call ZSTD_initCStream() first!");
3838 DEBUGLOG(4,
"ZSTD_compressEnd : cSize=%u", (
unsigned)cSize);
3844 someMoreWork = 0;
break;
3857 someMoreWork = 0;
break;
3862 someMoreWork = 0;
break;
3866 DEBUGLOG(5,
"stream compression stage (flushMode==%u)", flushMode);
3870 size_t oSize = oend-
op;
3871 unsigned const lastBlock = (flushMode ==
ZSTD_e_end) && (
ip==iend);
3881 FORWARD_IF_ERROR(cSize,
"%s", lastBlock ?
"ZSTD_compressEnd failed" :
"ZSTD_compressContinue failed");
3887 DEBUGLOG(5,
"inBuffTarget:%u / inBuffSize:%u",
3895 DEBUGLOG(5,
"Frame completed directly in outBuffer");
3911 DEBUGLOG(5,
"toFlush: %u into %u ==> flushed: %u",
3912 (
unsigned)toFlush, (
unsigned)(oend-
op), (
unsigned)flushed);
3916 if (toFlush!=flushed) {
3924 DEBUGLOG(5,
"Frame completed on flush");
3939 output->
pos =
op - ostart;
3946#ifdef ZSTD_MULTITHREAD
3949 return ZSTDMT_nextInputSizeHint(cctx->mtctx);
3968 DEBUGLOG(5,
"ZSTD_compressStream2, endOp=%u ", (
unsigned)endOp);
3981 DEBUGLOG(4,
"ZSTD_compressStream2 : transparent init stage");
3987#ifdef ZSTD_MULTITHREAD
3991 if (
params.nbWorkers > 0) {
3993 if (cctx->mtctx ==
NULL) {
3994 DEBUGLOG(4,
"ZSTD_compressStream2: creating new mtctx for nbWorkers=%u",
4000 DEBUGLOG(4,
"call ZSTDMT_initCStream_internal as nbWorkers=%u",
params.nbWorkers);
4019#ifdef ZSTD_MULTITHREAD
4025 ZSTDMT_updateCParams_whileCompressing(cctx->mtctx, &cctx->
requestedParams);
4029 flushMin = ZSTDMT_compressStream_generic(cctx->mtctx, output,
input, endOp);
4031 || (endOp ==
ZSTD_e_end && flushMin == 0) ) {