1 | /** |
2 | * Copyright (c) 2006-2023 LOVE Development Team |
3 | * |
4 | * This software is provided 'as-is', without any express or implied |
5 | * warranty. In no event will the authors be held liable for any damages |
6 | * arising from the use of this software. |
7 | * |
8 | * Permission is granted to anyone to use this software for any purpose, |
9 | * including commercial applications, and to alter it and redistribute it |
10 | * freely, subject to the following restrictions: |
11 | * |
12 | * 1. The origin of this software must not be misrepresented; you must not |
13 | * claim that you wrote the original software. If you use this software |
14 | * in a product, an acknowledgment in the product documentation would be |
15 | * appreciated but is not required. |
16 | * 2. Altered source versions must be plainly marked as such, and must not be |
17 | * misrepresented as being the original software. |
18 | * 3. This notice may not be removed or altered from any source distribution. |
19 | **/ |
20 | |
21 | #pragma once |
22 | |
23 | // LOVE |
24 | #include "common/StringMap.h" |
25 | |
26 | namespace love |
27 | { |
28 | namespace data |
29 | { |
30 | |
31 | /** |
32 | * Base class for backends for different compression formats. |
33 | **/ |
34 | class Compressor |
35 | { |
36 | public: |
37 | |
38 | enum Format |
39 | { |
40 | FORMAT_LZ4, |
41 | FORMAT_ZLIB, |
42 | FORMAT_GZIP, |
43 | FORMAT_DEFLATE, |
44 | FORMAT_MAX_ENUM |
45 | }; |
46 | |
47 | /** |
48 | * Gets a Compressor that can compress and decompress a specific format. |
49 | * Returns null if there are no supported compressors for the given format. |
50 | **/ |
51 | static Compressor *getCompressor(Format format); |
52 | |
53 | virtual ~Compressor() {} |
54 | |
55 | /** |
56 | * Compresses input data, and returns the compressed result. |
57 | * |
58 | * @param[in] format The format to compress to. |
59 | * @param[in] data The input (uncompressed) data. |
60 | * @param[in] dataSize The size in bytes of the input data. |
61 | * @param[in] level The amount of compression to apply (between 0 and 9.) |
62 | * A value of -1 indicates the default amount of compression. |
63 | * Specific formats may not use every level. |
64 | * @param[out] compressedSize The size in bytes of the compressed result. |
65 | * |
66 | * @return The newly compressed data (allocated with new[]). |
67 | **/ |
68 | virtual char *compress(Format format, const char *data, size_t dataSize, int level, size_t &compressedSize) = 0; |
69 | |
70 | /** |
71 | * Decompresses compressed data, and returns the decompressed result. |
72 | * |
73 | * @param[in] format The format the compressed data is in. |
74 | * @param[in] data The input (compressed) data. |
75 | * @param[in] dataSize The size in bytes of the compressed data. |
76 | * @param[in,out] decompressedSize On input, the size in bytes of the |
77 | * original uncompressed data, or 0 if unknown. On return, the |
78 | * size in bytes of the decompressed data. |
79 | * |
80 | * @return The decompressed data (allocated with new[]). |
81 | **/ |
82 | virtual char *decompress(Format format, const char *data, size_t dataSize, size_t &decompressedSize) = 0; |
83 | |
84 | /** |
85 | * Gets whether a specific format is supported by this backend. |
86 | **/ |
87 | virtual bool isSupported(Format format) const = 0; |
88 | |
89 | static bool getConstant(const char *in, Format &out); |
90 | static bool getConstant(Format in, const char *&out); |
91 | static std::vector<std::string> getConstants(Format); |
92 | |
93 | protected: |
94 | |
95 | Compressor() {} |
96 | |
97 | private: |
98 | |
99 | static StringMap<Format, FORMAT_MAX_ENUM>::Entry formatEntries[]; |
100 | static StringMap<Format, FORMAT_MAX_ENUM> formatNames; |
101 | |
102 | }; // Compressor |
103 | |
104 | } // data |
105 | } // love |
106 | |