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/extensions/compression/brotli/common/base.h"
8

            
9
#include "brotli/decode.h"
10

            
11
namespace Envoy {
12
namespace Extensions {
13
namespace Compression {
14
namespace Brotli {
15
namespace Decompressor {
16

            
17
/**
18
 * All brotli decompressor stats. @see stats_macros.h
19
 */
20
#define ALL_BROTLI_DECOMPRESSOR_STATS(COUNTER)                                                     \
21
95
  COUNTER(brotli_error)           /*Decompression error of all.*/                                  \
22
95
  COUNTER(brotli_output_overflow) /*Decompression error because of the overflow output.*/          \
23
95
  COUNTER(brotli_redundant_input) /*Decompression error because of the redundant input.*/
24

            
25
/**
26
 * Struct definition for brotli decompressor stats. @see stats_macros.h
27
 */
28
struct BrotliDecompressorStats {
29
  ALL_BROTLI_DECOMPRESSOR_STATS(GENERATE_COUNTER_STRUCT)
30
};
31

            
32
/**
33
 * Implementation of decompressor's interface.
34
 */
35
class BrotliDecompressorImpl : public Envoy::Compression::Decompressor::Decompressor, NonCopyable {
36
public:
37
  /**
38
   * Constructor.
39
   * @param chunk_size amount of memory reserved for the decompressor output.
40
   * @param disable_ring_buffer_reallocation if true disables "canny" ring buffer allocation
41
   * strategy. Ring buffer is allocated according to window size, despite the real size of the
42
   * content.
43
   */
44
  BrotliDecompressorImpl(Stats::Scope& scope, const std::string& stats_prefix,
45
                         const uint32_t chunk_size, bool disable_ring_buffer_reallocation);
46

            
47
  // Envoy::Compression::Decompressor::Decompressor
48
  void decompress(const Buffer::Instance& input_buffer, Buffer::Instance& output_buffer) override;
49

            
50
private:
51
95
  static BrotliDecompressorStats generateStats(const std::string& prefix, Stats::Scope& scope) {
52
95
    return BrotliDecompressorStats{
53
95
        ALL_BROTLI_DECOMPRESSOR_STATS(POOL_COUNTER_PREFIX(scope, prefix))};
54
95
  }
55

            
56
  bool process(Common::BrotliContext& ctx, Buffer::Instance& output_buffer);
57

            
58
  const uint32_t chunk_size_;
59
  std::unique_ptr<BrotliDecoderState, decltype(&BrotliDecoderDestroyInstance)> state_;
60
  const BrotliDecompressorStats stats_;
61
};
62

            
63
} // namespace Decompressor
64
} // namespace Brotli
65
} // namespace Compression
66
} // namespace Extensions
67
} // namespace Envoy