libassetmap  1.0.0
Hash-table based archive format containing compressed data
ZSTDComp.h
1 #ifndef LIBASSETMAP_ZSTDCOMP_H
2 #define LIBASSETMAP_ZSTDCOMP_H
3 
4 #include "ICompress.h"
5 #include "IDecompress.h"
6 
7 #include <vector>
8 
9 using ZSTD_CCtx = struct ZSTD_CCtx_s;
10 using ZSTD_DCtx = struct ZSTD_DCtx_s;
11 using ZSTD_CDict = struct ZSTD_CDict_s;
12 using ZSTD_DDict = struct ZSTD_DDict_s;
13 
14 namespace AssetMap {
15 
17  template <class Ctx>
18  class ZCtx {
19  Ctx* ctx = nullptr;
20 
21  void Dispose();
22 
23  public:
24  constexpr ZCtx() = default;
25  constexpr ZCtx(ZCtx&&) noexcept;
26  constexpr ZCtx& operator=(ZCtx&&) noexcept;
27  constexpr ZCtx& operator=(Ctx* ctx) noexcept;
28  constexpr explicit operator bool() const noexcept;
29  operator Ctx*() const noexcept;
30  constexpr void Create();
31  ~ZCtx();
32  };
33 
34  class ZSTD : public ICompress, public IDecompress {
35  ZCtx<ZSTD_CCtx> cCtx;
36  ZCtx<ZSTD_DCtx> dCtx;
37  const uint8_t* dictionary = nullptr;
38  size_t dictLen = 0;
39  int compressionLevel = 0;
40  float dictRatio = 0.01f;
41  std::vector<uint8_t> generatedDictionary;
42 
43  struct compress_t {};
44  struct decompress_t {};
45  struct both_t {};
46 
47  ZSTD(float dictRatio) noexcept;
48 
49  public:
50  static constexpr compress_t compress{};
51  static constexpr decompress_t decompress{};
52  static constexpr both_t both{};
53 
58  explicit ZSTD(both_t, float dictRatio = 0.01f);
59 
65  explicit ZSTD(compress_t, float dictRatio = 0.01f);
66 
72  explicit ZSTD(decompress_t);
73 
74  ZSTD(const ZSTD&) = delete;
75 
76  ZSTD(ZSTD&& rhs) noexcept;
77 
78  ZSTD& operator=(ZSTD&& rhs) noexcept;
79 
87  [[nodiscard]] size_t Compress(const uint8_t* src,
88  size_t srcLen,
89  uint8_t* dst,
90  size_t dstLen) override;
91 
101  [[nodiscard]] size_t Decompress(const uint8_t* src,
102  size_t srcLen,
103  uint8_t* dst,
104  size_t dstLen) override;
105 
109  [[nodiscard]] size_t CalcCompressSize(size_t len) const noexcept override;
110 
117  [[nodiscard]] size_t CalcDecompressSize(const uint8_t* src,
118  size_t len) const noexcept override;
119 
128  [[nodiscard]] bool
129  CreateDictionary(std::filesystem::directory_entry samplesDir) override;
130 
136  void UseDictionary(const uint8_t* dict, size_t len) noexcept override;
137 
143  void UseDictionary(std::filesystem::directory_entry ent);
144 
151  void SetCompressLevel(int level) noexcept;
152 
156  void SetStrategyLevel(int level) noexcept;
157 
159  static int MinCompressLevel() noexcept;
160 
162  static int MaxCompressLevel() noexcept;
163 
165  static int DefaultCompressLevel() noexcept;
166 
168  static int MinStrategyLevel() noexcept;
169 
171  static int MaxStrategyLevel() noexcept;
172 
174  static const char* StrategyInfo() noexcept;
175 
178  [[nodiscard]] std::pair<const uint8_t*, size_t>
179  Dictionary() const noexcept override;
180 
181  ~ZSTD() noexcept override;
182  };
183 } // namespace AssetMap
184 
185 #endif // LIBASSETMAP_ZSTDCOMP_H
AssetMap::ZSTD::ZSTD
ZSTD(compress_t, float dictRatio=0.01f)
Construct an instance of ZSTD for compression.
AssetMap::ZSTD::CalcDecompressSize
size_t CalcDecompressSize(const uint8_t *src, size_t len) const noexcept override
Calculates the number of bytes needed to decompress the data.
AssetMap::IDecompress
Definition: IDecompress.h:10
AssetMap::ZSTD::MinCompressLevel
static int MinCompressLevel() noexcept
AssetMap::ZSTD::SetStrategyLevel
void SetStrategyLevel(int level) noexcept
AssetMap::ICompress
Definition: ICompress.h:10
AssetMap::ZSTD::ZSTD
ZSTD(both_t, float dictRatio=0.01f)
Construct an instance of ZSTD for compression and decompression.
AssetMap::ZSTD::Decompress
size_t Decompress(const uint8_t *src, size_t srcLen, uint8_t *dst, size_t dstLen) override
Decompresses data from src into dst.
AssetMap::ZSTD::UseDictionary
void UseDictionary(const uint8_t *dict, size_t len) noexcept override
References a dictionary for (de)compression purposes.
AssetMap::ZSTD::UseDictionary
void UseDictionary(std::filesystem::directory_entry ent)
Reads a file into memory and uses it as a dictionary.
AssetMap::ZSTD::CreateDictionary
bool CreateDictionary(std::filesystem::directory_entry samplesDir) override
Creates a dictionary from a directory of samples.
AssetMap::ZSTD::CalcCompressSize
size_t CalcCompressSize(size_t len) const noexcept override
Calculates the worst-case size needed to compress the data.
AssetMap::ZSTD::Compress
size_t Compress(const uint8_t *src, size_t srcLen, uint8_t *dst, size_t dstLen) override
Compresses data from src into dst.
AssetMap::ZSTD::SetCompressLevel
void SetCompressLevel(int level) noexcept
Sets the desired compression level.
AssetMap
Definition: DirectoryMetadata.h:65
AssetMap::ZSTD::ZSTD
ZSTD(decompress_t)
Construct an instance of ZSTD for decompression.
AssetMap::ZSTD
Definition: ZSTDComp.h:34