diff options
author | levysotsky <levysotsky@yandex-team.ru> | 2022-02-10 16:47:29 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:47:29 +0300 |
commit | 57f874ffc2a75047c1c4fea7a9fc86cb0f56ed50 (patch) | |
tree | ba6454f353979bb4bafaf230cafdf3e02ea120b2 /library/cpp/streams/brotli | |
parent | 23d4769f0fea97cfb1028710e50f2b5ecd0ac2c0 (diff) | |
download | ydb-57f874ffc2a75047c1c4fea7a9fc86cb0f56ed50.tar.gz |
Restoring authorship annotation for <levysotsky@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/streams/brotli')
-rw-r--r-- | library/cpp/streams/brotli/brotli.cpp | 192 | ||||
-rw-r--r-- | library/cpp/streams/brotli/brotli.h | 100 | ||||
-rw-r--r-- | library/cpp/streams/brotli/brotli_ut.cpp | 52 | ||||
-rw-r--r-- | library/cpp/streams/brotli/ut/ya.make | 14 | ||||
-rw-r--r-- | library/cpp/streams/brotli/ya.make | 26 |
5 files changed, 192 insertions, 192 deletions
diff --git a/library/cpp/streams/brotli/brotli.cpp b/library/cpp/streams/brotli/brotli.cpp index 38052cb688..6e58341cbd 100644 --- a/library/cpp/streams/brotli/brotli.cpp +++ b/library/cpp/streams/brotli/brotli.cpp @@ -1,11 +1,11 @@ -#include "brotli.h" - +#include "brotli.h" + #include <contrib/libs/brotli/include/brotli/decode.h> #include <contrib/libs/brotli/include/brotli/encode.h> - + #include <util/generic/yexception.h> -#include <util/memory/addstorage.h> - +#include <util/memory/addstorage.h> + namespace { struct TAllocator { static void* Allocate(void* /* opaque */, size_t size) { @@ -19,27 +19,27 @@ namespace { } -class TBrotliCompress::TImpl { -public: +class TBrotliCompress::TImpl { +public: TImpl(IOutputStream* slave, int quality) - : Slave_(slave) + : Slave_(slave) , EncoderState_(BrotliEncoderCreateInstance(&TAllocator::Allocate, &TAllocator::Deallocate, nullptr)) - { + { if (!EncoderState_) { ythrow yexception() << "Brotli encoder initialization failed"; } - + auto res = BrotliEncoderSetParameter( EncoderState_, BROTLI_PARAM_QUALITY, quality); - + if (!res) { BrotliEncoderDestroyInstance(EncoderState_); ythrow yexception() << "Failed to set brotli encoder quality to " << quality; - } - } - + } + } + ~TImpl() { BrotliEncoderDestroyInstance(EncoderState_); } @@ -48,18 +48,18 @@ public: DoWrite(buffer, size, BROTLI_OPERATION_PROCESS); } - void Flush() { + void Flush() { DoWrite(nullptr, 0, BROTLI_OPERATION_FLUSH); - } - + } + void Finish() { Flush(); DoWrite(nullptr, 0, BROTLI_OPERATION_FINISH); Y_VERIFY(BrotliEncoderIsFinished(EncoderState_)); } -private: - IOutputStream* Slave_; +private: + IOutputStream* Slave_; BrotliEncoderState* EncoderState_; void DoWrite(const void* buffer, size_t size, BrotliEncoderOperation operation) { @@ -89,68 +89,68 @@ private: } } while (size > 0 || BrotliEncoderHasMoreOutput(EncoderState_)); } -}; - -TBrotliCompress::TBrotliCompress(IOutputStream* slave, int quality) { +}; + +TBrotliCompress::TBrotliCompress(IOutputStream* slave, int quality) { Impl_.Reset(new TImpl(slave, quality)); -} - -TBrotliCompress::~TBrotliCompress() { - try { - Finish(); - } catch (...) { - } -} - -void TBrotliCompress::DoWrite(const void* buffer, size_t size) { - Impl_->Write(buffer, size); -} - -void TBrotliCompress::DoFlush() { - if (Impl_) { - Impl_->Flush(); - } -} - -void TBrotliCompress::DoFinish() { - THolder<TImpl> impl(Impl_.Release()); - - if (impl) { +} + +TBrotliCompress::~TBrotliCompress() { + try { + Finish(); + } catch (...) { + } +} + +void TBrotliCompress::DoWrite(const void* buffer, size_t size) { + Impl_->Write(buffer, size); +} + +void TBrotliCompress::DoFlush() { + if (Impl_) { + Impl_->Flush(); + } +} + +void TBrotliCompress::DoFinish() { + THolder<TImpl> impl(Impl_.Release()); + + if (impl) { impl->Finish(); - } -} - -//////////////////////////////////////////////////////////////////////////////// - + } +} + +//////////////////////////////////////////////////////////////////////////////// + class TBrotliDecompress::TImpl: public TAdditionalStorage<TImpl> { -public: - TImpl(IInputStream* slave) - : Slave_(slave) - { +public: + TImpl(IInputStream* slave) + : Slave_(slave) + { InitDecoder(); - } - - ~TImpl() { + } + + ~TImpl() { FreeDecoder(); - } - - size_t Read(void* buffer, size_t size) { + } + + size_t Read(void* buffer, size_t size) { Y_ASSERT(size > 0); - + ui8* outBuffer = static_cast<ui8*>(buffer); - size_t availableOut = size; + size_t availableOut = size; size_t decompressedSize = 0; BrotliDecoderResult result; - do { + do { if (InputAvailable_ == 0 && !InputExhausted_) { - InputBuffer_ = TmpBuf(); + InputBuffer_ = TmpBuf(); InputAvailable_ = Slave_->Read((void*)InputBuffer_, TmpBufLen()); if (InputAvailable_ == 0) { InputExhausted_ = true; - } - } - + } + } + if (SubstreamFinished_ && !InputExhausted_) { ResetState(); } @@ -158,8 +158,8 @@ public: result = BrotliDecoderDecompressStream( DecoderState_, &InputAvailable_, - &InputBuffer_, - &availableOut, + &InputBuffer_, + &availableOut, &outBuffer, nullptr); @@ -173,33 +173,33 @@ public: } else if (result == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) { Y_VERIFY(availableOut != size, "Buffer passed to read in Brotli decoder is too small"); - break; - } + break; + } } while (decompressedSize == 0 && result == BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT && !InputExhausted_); - + if (!SubstreamFinished_ && decompressedSize == 0) { ythrow yexception() << "Input stream is incomplete"; } return decompressedSize; - } - -private: - IInputStream* Slave_; + } + +private: + IInputStream* Slave_; BrotliDecoderState* DecoderState_; - + bool SubstreamFinished_ = false; bool InputExhausted_ = false; const ui8* InputBuffer_ = nullptr; size_t InputAvailable_ = 0; - - unsigned char* TmpBuf() noexcept { - return static_cast<unsigned char*>(AdditionalData()); - } - - size_t TmpBufLen() const noexcept { - return AdditionalDataLength(); - } + + unsigned char* TmpBuf() noexcept { + return static_cast<unsigned char*>(AdditionalData()); + } + + size_t TmpBufLen() const noexcept { + return AdditionalDataLength(); + } void InitDecoder() { DecoderState_ = BrotliDecoderCreateInstance(&TAllocator::Allocate, &TAllocator::Deallocate, nullptr); @@ -217,15 +217,15 @@ private: FreeDecoder(); InitDecoder(); } -}; - -TBrotliDecompress::TBrotliDecompress(IInputStream* slave, size_t bufferSize) +}; + +TBrotliDecompress::TBrotliDecompress(IInputStream* slave, size_t bufferSize) : Impl_(new (bufferSize) TImpl(slave)) -{ -} - -TBrotliDecompress::~TBrotliDecompress() = default; - -size_t TBrotliDecompress::DoRead(void* buffer, size_t size) { - return Impl_->Read(buffer, size); -} +{ +} + +TBrotliDecompress::~TBrotliDecompress() = default; + +size_t TBrotliDecompress::DoRead(void* buffer, size_t size) { + return Impl_->Read(buffer, size); +} diff --git a/library/cpp/streams/brotli/brotli.h b/library/cpp/streams/brotli/brotli.h index b3af869e29..b7e4034b86 100644 --- a/library/cpp/streams/brotli/brotli.h +++ b/library/cpp/streams/brotli/brotli.h @@ -1,52 +1,52 @@ -#pragma once - -#include <util/generic/ptr.h> -#include <util/stream/input.h> -#include <util/stream/output.h> - -/** - * @addtogroup Streams_Archs - * @{ - */ - +#pragma once + +#include <util/generic/ptr.h> +#include <util/stream/input.h> +#include <util/stream/output.h> + +/** + * @addtogroup Streams_Archs + * @{ + */ + class TBrotliCompress: public IOutputStream { -public: - static constexpr int BEST_QUALITY = 11; - - /** - @param slave stream to write compressed data to - @param quality the higher the quality, the slower and better the compression. Range is 0 to 11. - */ - explicit TBrotliCompress(IOutputStream* slave, int quality = BEST_QUALITY); - ~TBrotliCompress() override; - -private: - void DoWrite(const void* buffer, size_t size) override; - void DoFlush() override; - void DoFinish() override; - -public: - class TImpl; - THolder<TImpl> Impl_; -}; - -//////////////////////////////////////////////////////////////////////////////// - +public: + static constexpr int BEST_QUALITY = 11; + + /** + @param slave stream to write compressed data to + @param quality the higher the quality, the slower and better the compression. Range is 0 to 11. + */ + explicit TBrotliCompress(IOutputStream* slave, int quality = BEST_QUALITY); + ~TBrotliCompress() override; + +private: + void DoWrite(const void* buffer, size_t size) override; + void DoFlush() override; + void DoFinish() override; + +public: + class TImpl; + THolder<TImpl> Impl_; +}; + +//////////////////////////////////////////////////////////////////////////////// + class TBrotliDecompress: public IInputStream { -public: - /** - @param slave stream to read compressed data from - @param bufferSize approximate size of buffer compressed data is read in - */ - explicit TBrotliDecompress(IInputStream* slave, size_t bufferSize = 8 * 1024); - ~TBrotliDecompress() override; - -private: - size_t DoRead(void* buffer, size_t size) override; - -private: - class TImpl; - THolder<TImpl> Impl_; -}; - -/** @} */ +public: + /** + @param slave stream to read compressed data from + @param bufferSize approximate size of buffer compressed data is read in + */ + explicit TBrotliDecompress(IInputStream* slave, size_t bufferSize = 8 * 1024); + ~TBrotliDecompress() override; + +private: + size_t DoRead(void* buffer, size_t size) override; + +private: + class TImpl; + THolder<TImpl> Impl_; +}; + +/** @} */ diff --git a/library/cpp/streams/brotli/brotli_ut.cpp b/library/cpp/streams/brotli/brotli_ut.cpp index aeb2e284dc..2d07943226 100644 --- a/library/cpp/streams/brotli/brotli_ut.cpp +++ b/library/cpp/streams/brotli/brotli_ut.cpp @@ -1,9 +1,9 @@ -#include "brotli.h" - +#include "brotli.h" + #include <library/cpp/testing/unittest/registar.h> - -#include <util/random/fast.h> - + +#include <util/random/fast.h> + Y_UNIT_TEST_SUITE(TBrotliTestSuite) { TString Compress(TString data) { TString compressed; @@ -14,7 +14,7 @@ Y_UNIT_TEST_SUITE(TBrotliTestSuite) { output.Finish(); return compressed; } - + TString Decompress(TString data) { TStringInput input(data); TBrotliDecompress decompressStream(&input); @@ -27,20 +27,20 @@ Y_UNIT_TEST_SUITE(TBrotliTestSuite) { TString GenerateRandomString(size_t size) { TReallyFastRng32 rng(42); - TString result; - result.reserve(size + sizeof(ui64)); - while (result.size() < size) { - ui64 value = rng.GenRand64(); - result += TStringBuf(reinterpret_cast<const char*>(&value), sizeof(value)); - } - result.resize(size); - return result; - } - + TString result; + result.reserve(size + sizeof(ui64)); + while (result.size() < size) { + ui64 value = rng.GenRand64(); + result += TStringBuf(reinterpret_cast<const char*>(&value), sizeof(value)); + } + result.resize(size); + return result; + } + Y_UNIT_TEST(TestHelloWorld) { TestCase("hello world"); - } - + } + Y_UNIT_TEST(TestFlush) { TStringStream ss; TBrotliCompress compressStream(&ss); @@ -64,26 +64,26 @@ Y_UNIT_TEST_SUITE(TBrotliTestSuite) { auto c1 = Compress(s1); auto c2 = Compress(s2); UNIT_ASSERT_VALUES_EQUAL(s1 + s2, Decompress(c1 + c2)); - } - + } + Y_UNIT_TEST(TestIncompleteStream) { TString manyAs(64 * 1024, 'a'); auto compressed = Compress(manyAs); TString truncated(compressed.data(), compressed.size() - 1); UNIT_CHECK_GENERATED_EXCEPTION(Decompress(truncated), std::exception); - } - + } + Y_UNIT_TEST(Test64KB) { auto manyAs = TString(64 * 1024, 'a'); TString str("Hello from the Matrix!@#% How are you?}{\n\t\a"); TestCase(manyAs + str + manyAs); - } - + } + Y_UNIT_TEST(Test1MB) { TestCase(GenerateRandomString(1 * 1024 * 1024)); } Y_UNIT_TEST(TestEmpty) { TestCase(""); - } -} + } +} diff --git a/library/cpp/streams/brotli/ut/ya.make b/library/cpp/streams/brotli/ut/ya.make index 243462f1b2..70c207ba7e 100644 --- a/library/cpp/streams/brotli/ut/ya.make +++ b/library/cpp/streams/brotli/ut/ya.make @@ -1,12 +1,12 @@ UNITTEST_FOR(library/cpp/streams/brotli) - + OWNER( levysotsky g:util ) - -SRCS( - brotli_ut.cpp -) - -END() + +SRCS( + brotli_ut.cpp +) + +END() diff --git a/library/cpp/streams/brotli/ya.make b/library/cpp/streams/brotli/ya.make index fa2bfec9cc..108419e69e 100644 --- a/library/cpp/streams/brotli/ya.make +++ b/library/cpp/streams/brotli/ya.make @@ -1,17 +1,17 @@ -LIBRARY() - +LIBRARY() + OWNER( levysotsky g:util ) - -PEERDIR( - contrib/libs/brotli/enc - contrib/libs/brotli/dec -) - -SRCS( - brotli.cpp -) - -END() + +PEERDIR( + contrib/libs/brotli/enc + contrib/libs/brotli/dec +) + +SRCS( + brotli.cpp +) + +END() |