summaryrefslogtreecommitdiffstats
path: root/src/StringCompression.h
blob: c091c27a0be3c044cc11552f770c3125e7e0c620 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85

// StringCompression.h

// Interfaces to the wrapping functions for compression and decompression

#pragma once





class cByteBuffer;

struct libdeflate_compressor;
struct libdeflate_decompressor;





namespace Compression
{
	/** Contains the result of a compression or extraction operation. */
	struct Result
	{
		using Static = std::array<std::byte, 128 KiB>;
		using Dynamic = std::unique_ptr<std::byte[]>;

		static constexpr size_t StaticCapacity = sizeof(Compression::Result::Static) / sizeof(Compression::Result::Static::value_type);

		/** Returns a view (of type char) of the internal store. */
		std::string_view GetStringView() const;

		/** Returns a view (of type std::byte) of the internal store. */
		ContiguousByteBufferView GetView() const;

		/** A store allocated on either the stack or heap. */
		std::variant<Static, Dynamic> Storage;

		/** The length of valid data in the store. */
		size_t Size;
	};

	/** Contains routines for data compression. */
	class Compressor
	{
	public:

		/** Creates a new compressor instance with a compression factor [0-12]. */
		Compressor(int CompressionFactor = 6);
		~Compressor();

		Result CompressGZip(ContiguousByteBufferView Input);
		Result CompressZLib(ContiguousByteBufferView Input);
		Result CompressZLib(const void * Input, size_t Size);

	private:

		template <auto Algorithm>
		Result Compress(const void * Input, size_t Size);

		libdeflate_compressor * m_Handle;
	};

	/** Contains routines for data extraction. */
	class Extractor
	{
	public:

		/** Creates a new extractor instance. */
		Extractor();
		~Extractor();

		Result ExtractGZip(ContiguousByteBufferView Input);
		Result ExtractZLib(ContiguousByteBufferView Input);
		Result ExtractZLib(ContiguousByteBufferView Input, size_t UncompressedSize);

	private:

		template <auto Algorithm> Result Extract(ContiguousByteBufferView Input);
		template <auto Algorithm> Result Extract(ContiguousByteBufferView Input, size_t UncompressedSize);

		libdeflate_decompressor * m_Handle;
	};
}