Line data Source code
1 : #pragma once 2 : 3 : #include "envoy/compression/decompressor/decompressor.h" 4 : #include "envoy/stats/scope.h" 5 : #include "envoy/stats/stats_macros.h" 6 : 7 : #include "source/common/common/logger.h" 8 : #include "source/extensions/compression/zstd/common/base.h" 9 : #include "source/extensions/compression/zstd/common/dictionary_manager.h" 10 : 11 : #include "zstd_errors.h" 12 : 13 : namespace Envoy { 14 : namespace Extensions { 15 : namespace Compression { 16 : namespace Zstd { 17 : namespace Decompressor { 18 : 19 : using ZstdDDictManager = 20 : Common::DictionaryManager<ZSTD_DDict, ZSTD_freeDDict, ZSTD_getDictID_fromDDict>; 21 : using ZstdDDictManagerPtr = std::unique_ptr<ZstdDDictManager>; 22 : 23 : /** 24 : * All zstd decompressor stats. @see stats_macros.h 25 : */ 26 : #define ALL_ZSTD_DECOMPRESSOR_STATS(COUNTER) \ 27 0 : COUNTER(zstd_generic_error) \ 28 0 : COUNTER(zstd_dictionary_error) \ 29 0 : COUNTER(zstd_checksum_wrong_error) \ 30 0 : COUNTER(zstd_memory_error) 31 : 32 : /** 33 : * Struct definition for zstd decompressor stats. @see stats_macros.h 34 : */ 35 : struct ZstdDecompressorStats { 36 : ALL_ZSTD_DECOMPRESSOR_STATS(GENERATE_COUNTER_STRUCT) 37 : }; 38 : 39 : /** 40 : * Implementation of decompressor's interface. 41 : */ 42 : class ZstdDecompressorImpl : public Common::Base, 43 : public Envoy::Compression::Decompressor::Decompressor, 44 : public Logger::Loggable<Logger::Id::decompression>, 45 : NonCopyable { 46 : public: 47 : ZstdDecompressorImpl(Stats::Scope& scope, const std::string& stats_prefix, 48 : const ZstdDDictManagerPtr& ddict_manager, uint32_t chunk_size); 49 : 50 : // Envoy::Compression::Decompressor::Decompressor 51 : void decompress(const Buffer::Instance& input_buffer, Buffer::Instance& output_buffer) override; 52 : 53 : private: 54 0 : static ZstdDecompressorStats generateStats(const std::string& prefix, Stats::Scope& scope) { 55 0 : return ZstdDecompressorStats{ALL_ZSTD_DECOMPRESSOR_STATS(POOL_COUNTER_PREFIX(scope, prefix))}; 56 0 : } 57 : 58 : friend class ZstdDecompressorStatsTest; 59 : bool process(Buffer::Instance& output_buffer); 60 : bool isError(size_t result); 61 : 62 : std::unique_ptr<ZSTD_DCtx, decltype(&ZSTD_freeDCtx)> dctx_; 63 : const ZstdDDictManagerPtr& ddict_manager_; 64 : const ZstdDecompressorStats stats_; 65 : bool is_dictionary_set_{false}; 66 : }; 67 : 68 : } // namespace Decompressor 69 : } // namespace Zstd 70 : } // namespace Compression 71 : } // namespace Extensions 72 : } // namespace Envoy