From c624966e139da7d0f9dbb96c99a5981948915584 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ale=C5=A1=20Mat=C4=9Bj?= Date: Mon, 18 Jan 2021 12:22:32 +0100 Subject: [PATCH] Add an option for zstd long distance matching compression The behavior is supposed to match the --long[=#] option of zstd binary. --- rpmio/rpmio.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/rpmio/rpmio.c b/rpmio/rpmio.c index 66fc31af14..fd15573469 100644 --- a/rpmio/rpmio.c +++ b/rpmio/rpmio.c @@ -1058,6 +1058,8 @@ static rpmzstd rpmzstdNew(int fdno, const char *fmode) char *te = t + sizeof(stdio) - 2; int c; int threads = 0; + int windowlog = 27; + int longdist = 0; switch ((c = *s++)) { case 'a': @@ -1094,6 +1096,22 @@ static rpmzstd rpmzstdNew(int fdno, const char *fmode) threads = -1; } continue; + case 'L': + c = *s++; + longdist = 1; + if (c >= (int)'0' && c <= (int)'9') { + windowlog = strtol(s-1, (char **)&s, 10); + ZSTD_bounds bounds = ZSTD_cParam_getBounds(ZSTD_c_windowLog); + if (windowlog < bounds.lowerBound){ + windowlog = bounds.lowerBound; + rpmlog(RPMLOG_WARNING, "Invalid long distance matching window log for zstd. Using %i instead.\n", bounds.lowerBound); + } + if (windowlog > bounds.upperBound) { + windowlog = bounds.upperBound; + rpmlog(RPMLOG_WARNING, "Invalid long distance matching window log for zstd. Using %i instead.\n", bounds.upperBound); + } + } + continue; default: if (c >= (int)'0' && c <= (int)'9') { level = strtol(s-1, (char **)&s, 10); @@ -1132,6 +1150,13 @@ static rpmzstd rpmzstdNew(int fdno, const char *fmode) goto err; } + if (longdist) { + if (ZSTD_isError(ZSTD_CCtx_setParameter(_stream, ZSTD_c_enableLongDistanceMatching, longdist)) + || ZSTD_isError(ZSTD_CCtx_setParameter(_stream, ZSTD_c_windowLog, windowlog))) { + goto err; + } + } + threads = get_compression_threads(threads); if (threads > 0) { if (ZSTD_isError (ZSTD_CCtx_setParameter(_stream, ZSTD_c_nbWorkers, threads)))