1
#pragma once
2

            
3
#include "envoy/compression/compressor/compressor.h"
4

            
5
#include "source/extensions/compression/brotli/common/base.h"
6

            
7
#include "brotli/encode.h"
8

            
9
namespace Envoy {
10
namespace Extensions {
11
namespace Compression {
12
namespace Brotli {
13
namespace Compressor {
14

            
15
/**
16
 * Implementation of compressor's interface.
17
 */
18
class BrotliCompressorImpl : public Envoy::Compression::Compressor::Compressor, NonCopyable {
19
public:
20
  /**
21
   * Enum values are used for setting the encoder mode.
22
   * Generic: in this mode the compressor does not know anything in advance about the properties of
23
   * the input;
24
   * Text: compression mode for UTF-8 formatted text input;
25
   * Font: compression mode used in `WOFF` 2.0;
26
   * Default: compression mode used by brotli encoder by default which is Generic currently.
27
   * @see BROTLI_DEFAULT_MODE in brotli manual.
28
   */
29
  enum class EncoderMode : uint32_t {
30
    Generic = BROTLI_MODE_GENERIC,
31
    Text = BROTLI_MODE_TEXT,
32
    Font = BROTLI_MODE_FONT,
33
    Default = BROTLI_DEFAULT_MODE,
34
  };
35

            
36
  /**
37
   * Constructor.
38
   * @param quality sets compression level. The higher the quality, the slower the
39
   * compression. @see BROTLI_PARAM_QUALITY (brotli manual).
40
   * @param window_bits sets recommended sliding `LZ77` window size.
41
   * @param input_block_bits sets recommended input block size. Bigger input block size allows
42
   * better compression, but consumes more memory.
43
   * @param disable_literal_context_modeling affects usage of "literal context modeling" format
44
   * feature. This flag is a "decoding-speed vs compression ratio" trade-off.
45
   * @param mode tunes encoder for specific input. @see EncoderMode enum.
46
   * @param chunk_size amount of memory reserved for the compressor output.
47
   */
48
  BrotliCompressorImpl(const uint32_t quality, const uint32_t window_bits,
49
                       const uint32_t input_block_bits, const bool disable_literal_context_modeling,
50
                       const EncoderMode mode, const uint32_t chunk_size);
51

            
52
  // Compression::Compressor::Compressor
53
  void compress(Buffer::Instance& buffer, Envoy::Compression::Compressor::State state) override;
54

            
55
private:
56
  void process(Common::BrotliContext& ctx, Buffer::Instance& output_buffer,
57
               const BrotliEncoderOperation op);
58

            
59
  const uint32_t chunk_size_;
60
  std::unique_ptr<BrotliEncoderState, decltype(&BrotliEncoderDestroyInstance)> state_;
61
};
62

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