Cuberite
A lightweight, fast and extensible game server for Minecraft
StringCompression.h
Go to the documentation of this file.
1 
2 // StringCompression.h
3 
4 // Interfaces to the wrapping functions for compression and decompression
5 
6 #pragma once
7 
8 
9 
10 
11 
12 struct libdeflate_compressor;
13 struct libdeflate_decompressor;
14 
15 
16 
17 
18 
19 namespace Compression
20 {
22  struct Result
23  {
24  using Static = std::array<std::byte, 128 KiB>;
25  using Dynamic = std::unique_ptr<std::byte[]>;
26 
27  static constexpr size_t StaticCapacity = sizeof(Compression::Result::Static) / sizeof(Compression::Result::Static::value_type);
28 
30  std::string_view GetStringView() const;
31 
34 
36  std::variant<Static, Dynamic> Storage;
37 
39  size_t Size;
40  };
41 
43  class Compressor
44  {
45  public:
46 
48  Compressor(int CompressionFactor = 6);
49  ~Compressor();
50 
53  Result CompressZLib(const void * Input, size_t Size);
54 
55  private:
56 
57  template <auto Algorithm>
58  Result Compress(const void * Input, size_t Size);
59 
60  libdeflate_compressor * m_Handle;
61  };
62 
64  class Extractor
65  {
66  public:
67 
69  Extractor();
70  ~Extractor();
71 
74  Result ExtractZLib(ContiguousByteBufferView Input, size_t UncompressedSize);
75 
76  private:
77 
78  template <auto Algorithm> Result Extract(ContiguousByteBufferView Input);
79  template <auto Algorithm> Result Extract(ContiguousByteBufferView Input, size_t UncompressedSize);
80 
81  libdeflate_decompressor * m_Handle;
82  };
83 }
std::basic_string_view< std::byte > ContiguousByteBufferView
Definition: Globals.h:376
Contains the result of a compression or extraction operation.
std::unique_ptr< std::byte[]> Dynamic
std::variant< Static, Dynamic > Storage
A store allocated on either the stack or heap.
static constexpr size_t StaticCapacity
ContiguousByteBufferView GetView() const
Returns a view (of type std::byte) of the internal store.
size_t Size
The length of valid data in the store.
std::string_view GetStringView() const
Returns a view (of type char) of the internal store.
std::array< std::byte, 128 KiB > Static
Contains routines for data compression.
Result Compress(const void *Input, size_t Size)
Result CompressGZip(ContiguousByteBufferView Input)
Compressor(int CompressionFactor=6)
Creates a new compressor instance with a compression factor [0-12].
Result CompressZLib(ContiguousByteBufferView Input)
libdeflate_compressor * m_Handle
Contains routines for data extraction.
Result ExtractZLib(ContiguousByteBufferView Input)
Result ExtractGZip(ContiguousByteBufferView Input)
Result Extract(ContiguousByteBufferView Input)
Result Extract(ContiguousByteBufferView Input, size_t UncompressedSize)
libdeflate_decompressor * m_Handle
Extractor()
Creates a new extractor instance.