From 0cc8701c38b1eb0a7843d736fdf9b8c9a560b83e Mon Sep 17 00:00:00 2001 From: conor42 Date: Thu, 2 May 2019 12:51:03 +1000 Subject: [PATCH] Pass blockSize to fast-lzma2 and limit it to 256 Mb --- CPP/7zip/Compress/Lzma2Encoder.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/CPP/7zip/Compress/Lzma2Encoder.cpp b/CPP/7zip/Compress/Lzma2Encoder.cpp index 9b3a2614..d264f779 100644 --- a/CPP/7zip/Compress/Lzma2Encoder.cpp +++ b/CPP/7zip/Compress/Lzma2Encoder.cpp @@ -146,6 +146,9 @@ static HRESULT TranslateError(size_t res) #define CHECK_P(f) if (FL2_isError(f)) return E_INVALIDARG; /* check and convert error code */ +#define MIN_BLOCK_SIZE (1U << 20) +#define MAX_BLOCK_SIZE (1U << 28) + CFastEncoder::FastLzma2::FastLzma2() : fcs(NULL), dict_pos(0) @@ -203,6 +206,18 @@ HRESULT CFastEncoder::FastLzma2::SetCoderProperties(const PROPID *propIDs, const CHECK_P(FL2_CCtx_setParameter(fcs, FL2_p_literalPosBits, lzma2Props.lzmaProps.lp)); if (lzma2Props.lzmaProps.pb >= 0) CHECK_P(FL2_CCtx_setParameter(fcs, FL2_p_posBits, lzma2Props.lzmaProps.pb)); + if (lzma2Props.blockSize == 0) + lzma2Props.blockSize = min(max(MIN_BLOCK_SIZE, dictSize * 4U), MAX_BLOCK_SIZE); + else if (lzma2Props.blockSize == LZMA2_ENC_PROPS__BLOCK_SIZE__SOLID) + lzma2Props.blockSize = 0; + unsigned r = 0; + if (lzma2Props.blockSize != 0) { + r = 1; + // Do not exceed the block size. TODO: the lib should support setting a value instead of a multiplier. + while (r < FL2_RESET_INTERVAL_MAX && (r + 1) * (UInt64)dictSize <= lzma2Props.blockSize) + ++r; + } + CHECK_P(FL2_CCtx_setParameter(fcs, FL2_p_resetInterval, r)); FL2_CCtx_setParameter(fcs, FL2_p_omitProperties, 1); FL2_setCStreamTimeout(fcs, 500); return S_OK;