1
#pragma once
2

            
3
#include <memory>
4

            
5
#include "envoy/buffer/buffer.h"
6

            
7
#include "zlib.h"
8

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

            
15
/**
16
 * Shared code between the compressor and the decompressor.
17
 */
18
class Base {
19
public:
20
  Base(uint64_t chunk_size, std::function<void(z_stream*)> zstream_deleter);
21

            
22
  /**
23
   * It returns the checksum of all output produced so far. Compressor's checksum at the end of
24
   * the stream has to match decompressor's checksum produced at the end of the decompression.
25
   * Likewise, the decompressor's checksum has to match the compressor's checksum at the end of
26
   * compression.
27
   * @return uint64_t CRC-32 if a gzip stream is being read or Adler-32 for other compression
28
   * types.
29
   */
30
  uint64_t checksum();
31

            
32
protected:
33
  void updateOutput(Buffer::Instance& output_buffer);
34

            
35
  const uint64_t chunk_size_;
36
  bool initialized_{false};
37

            
38
  const std::unique_ptr<unsigned char[]> chunk_char_ptr_;
39
  const std::unique_ptr<z_stream, std::function<void(z_stream*)>> zstream_ptr_;
40
};
41

            
42
} // namespace Common
43
} // namespace Gzip
44
} // namespace Compression
45
} // namespace Extensions
46
} // namespace Envoy