From 01bb1c1016e5bf9bd0ae7501333d70c5978caf48 Mon Sep 17 00:00:00 2001 From: "W. Felix Handte" Date: Thu, 21 Jun 2018 17:02:50 -0400 Subject: [PATCH] Add CCtx Param Controlling Dict Attachment Behavior --- lib/compress/zstd_compress.c | 17 ++++++++++++++++- lib/compress/zstd_compress_internal.h | 8 ++++++++ lib/zstd.h | 22 ++++++++++++++++++++++ tests/fuzz/zstd_helpers.c | 1 + 4 files changed, 47 insertions(+), 1 deletion(-) diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index c071c5738..d7bcffdf2 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -275,6 +275,7 @@ static int ZSTD_isUpdateAuthorized(ZSTD_cParameter param) case ZSTD_p_ldmMinMatch: case ZSTD_p_ldmBucketSizeLog: case ZSTD_p_ldmHashEveryLog: + case ZSTD_p_forceAttachDict: default: return 0; } @@ -319,6 +320,9 @@ size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, unsigned v * default : 0 when using a CDict, 1 when using a Prefix */ return ZSTD_CCtxParam_setParameter(&cctx->requestedParams, param, value); + case ZSTD_p_forceAttachDict: + return ZSTD_CCtxParam_setParameter(&cctx->requestedParams, param, value); + case ZSTD_p_nbWorkers: if ((value>0) && cctx->staticSize) { return ERROR(parameter_unsupported); /* MT not compatible with static alloc */ @@ -424,6 +428,12 @@ size_t ZSTD_CCtxParam_setParameter( CCtxParams->forceWindow = (value > 0); return CCtxParams->forceWindow; + case ZSTD_p_forceAttachDict : + CCtxParams->attachDictPref = value ? + (value > 0 ? ZSTD_dictForceAttach : ZSTD_dictForceCopy) : + ZSTD_dictDefaultAttach; + return CCtxParams->attachDictPref; + case ZSTD_p_nbWorkers : #ifndef ZSTD_MULTITHREAD if (value>0) return ERROR(parameter_unsupported); @@ -527,6 +537,9 @@ size_t ZSTD_CCtxParam_getParameter( case ZSTD_p_forceMaxWindow : *value = CCtxParams->forceWindow; break; + case ZSTD_p_forceAttachDict : + *value = CCtxParams->attachDictPref; + break; case ZSTD_p_nbWorkers : #ifndef ZSTD_MULTITHREAD assert(CCtxParams->nbWorkers == 0); @@ -1242,7 +1255,9 @@ static size_t ZSTD_resetCCtx_usingCDict(ZSTD_CCtx* cctx, 256 KB /* ZSTD_btultra */ }; const int attachDict = ( pledgedSrcSize <= attachDictSizeCutoffs[cdict->cParams.strategy] - || pledgedSrcSize == ZSTD_CONTENTSIZE_UNKNOWN ) + || pledgedSrcSize == ZSTD_CONTENTSIZE_UNKNOWN + || params.attachDictPref == ZSTD_dictForceAttach ) + && params.attachDictPref != ZSTD_dictForceCopy && !params.forceWindow /* dictMatchState isn't correctly * handled in _enforceMaxDist */ && cdict->cParams.strategy <= ZSTD_btlazy2 diff --git a/lib/compress/zstd_compress_internal.h b/lib/compress/zstd_compress_internal.h index ec9d01ccc..d31542c69 100644 --- a/lib/compress/zstd_compress_internal.h +++ b/lib/compress/zstd_compress_internal.h @@ -48,6 +48,12 @@ extern "C" { typedef enum { ZSTDcs_created=0, ZSTDcs_init, ZSTDcs_ongoing, ZSTDcs_ending } ZSTD_compressionStage_e; typedef enum { zcss_init=0, zcss_load, zcss_flush } ZSTD_cStreamStage; +typedef enum { + ZSTD_dictDefaultAttach = 0, + ZSTD_dictForceAttach = 1, + ZSTD_dictForceCopy = -1, +} ZSTD_dictAttachPref_e; + typedef struct ZSTD_prefixDict_s { const void* dict; size_t dictSize; @@ -186,6 +192,8 @@ struct ZSTD_CCtx_params_s { int forceWindow; /* force back-references to respect limit of * 1<