From 67f778670c085615470b67eb3c54885b8c2d482e Mon Sep 17 00:00:00 2001 From: eldritch horrors Date: Fri, 3 May 2024 22:23:02 +0200 Subject: libutil: add makeDecompressionSource Change-Id: Iac7f24d79e24417436b9b5cbefd6af051aeea0a6 --- src/libutil/compression.cc | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'src/libutil/compression.cc') diff --git a/src/libutil/compression.cc b/src/libutil/compression.cc index e78d76500..d93a1b1d6 100644 --- a/src/libutil/compression.cc +++ b/src/libutil/compression.cc @@ -192,11 +192,9 @@ struct BrotliDecompressionSource : Source std::string decompress(const std::string & method, std::string_view in) { - StringSink ssink; - auto sink = makeDecompressionSink(method, ssink); - (*sink)(in); - sink->finish(); - return std::move(ssink.s); + StringSource src{in}; + auto filter = makeDecompressionSource(method, src); + return filter->drain(); } std::unique_ptr makeDecompressionSink(const std::string & method, Sink & nextSink) @@ -224,6 +222,19 @@ std::unique_ptr makeDecompressionSink(const std::string & method, Si }); } +std::unique_ptr makeDecompressionSource(const std::string & method, Source & inner) +{ + if (method == "none" || method == "") { + return std::make_unique([&](char * data, size_t len) { + return inner.read(data, len); + }); + } else if (method == "br") { + return std::make_unique(inner); + } else { + return std::make_unique(inner); + } +} + struct BrotliCompressionSink : ChunkedCompressionSink { Sink & nextSink; -- cgit v1.2.3