1
#pragma once
2

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

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

            
7
#include "zlib.h"
8

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

            
15
/**
16
 * Implementation of compressor's interface.
17
 */
18
class ZlibCompressorImpl : public Common::Base, public Envoy::Compression::Compressor::Compressor {
19
public:
20
  ZlibCompressorImpl();
21

            
22
  /**
23
   * Constructor that allows setting the size of compressor's output buffer. It
24
   * should be called whenever a buffer size different than the 4096 bytes, normally set by the
25
   * default constructor, is desired. If memory is available and it makes sense to output large
26
   * chunks of compressed data, zlib documentation suggests buffers sizes on the order of 128K or
27
   * 256K bytes. @see http://zlib.net/zlib_how.html
28
   * @param chunk_size amount of memory reserved for the compressor output.
29
   */
30
  ZlibCompressorImpl(uint64_t chunk_size);
31

            
32
  /**
33
   * Enum values used to set compression level during initialization.
34
   * best: gives best compression.
35
   * speed: gives best performance.
36
   * levelX: allows to adjust trad-offs more precisely - from level1 (best speed, but very
37
   * low compression ratio) to level9 (best compression, but low speed).
38
   * standard: requests a default compromise between speed and compression. (default) @see zlib
39
   * manual.
40
   */
41
  enum class CompressionLevel : int64_t {
42
    Best = Z_BEST_COMPRESSION,
43
    Level1 = 1,
44
    Level2 = 2,
45
    Level3 = 3,
46
    Level4 = 4,
47
    Level5 = 5,
48
    Level6 = 6,
49
    Level7 = 7,
50
    Level8 = 8,
51
    Level9 = 9,
52
    Speed = Z_BEST_SPEED,
53
    Standard = Z_DEFAULT_COMPRESSION,
54
  };
55

            
56
  /**
57
   * Enum values are used for setting the compression algorithm strategy.
58
   * filtered: used for data produced by a filter. (or predictor) @see Z_FILTERED (zlib manual)
59
   * fixed: disable dynamic Huffman codes. @see Z_FIXED (zlib manual)
60
   * huffman: used to enforce Huffman encoding. @see RFC 1951
61
   * rle: used to limit match distances to one. (Run-length encoding)
62
   * standard: used for normal data. (default) @see Z_DEFAULT_STRATEGY in zlib manual.
63
   */
64
  enum class CompressionStrategy : uint64_t {
65
    Filtered = Z_FILTERED,
66
    Fixed = Z_FIXED,
67
    Huffman = Z_HUFFMAN_ONLY,
68
    Rle = Z_RLE,
69
    Standard = Z_DEFAULT_STRATEGY,
70
  };
71

            
72
  /**
73
   * Init must be called in order to initialize the compressor. Once compressor is initialized, it
74
   * cannot be initialized again. Init should run before compressing any data.
75
   * @param level @see CompressionLevel enum
76
   * @param strategy @see CompressionStrategy enum
77
   * @param window_bits sets the size of the history buffer. Larger values result in better
78
   * compression, but will use more memory @see window_bits. (zlib manual)
79
   * @param memory_level sets how much memory should be allocated for the internal compression, min
80
   * 1 and max 9. @see memory_level (zlib manual)
81
   */
82
  void init(CompressionLevel level, CompressionStrategy strategy, int64_t window_bits,
83
            uint64_t memory_level);
84

            
85
  // Compression::Compressor::Compressor
86
  void compress(Buffer::Instance& buffer, Envoy::Compression::Compressor::State state) override;
87

            
88
private:
89
  bool deflateNext(int64_t flush_state);
90
  void process(Buffer::Instance& output_buffer, int64_t flush_state);
91
};
92

            
93
} // namespace Compressor
94
} // namespace Gzip
95
} // namespace Compression
96
} // namespace Extensions
97
} // namespace Envoy