1 /**
2  * Copyright (c) 2006-2019 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 #include "CompressedData.h"
24 #include "Compressor.h"
25 #include "HashFunction.h"
26 #include "DataView.h"
27 #include "ByteData.h"
28 
29 // LOVE
30 #include "common/Module.h"
31 #include "common/int.h"
32 
33 namespace love
34 {
35 namespace data
36 {
37 
38 enum EncodeFormat
39 {
40 	ENCODE_BASE64,
41 	ENCODE_HEX,
42 	ENCODE_MAX_ENUM
43 };
44 
45 enum ContainerType
46 {
47 	CONTAINER_DATA,
48 	CONTAINER_STRING,
49 	CONTAINER_MAX_ENUM
50 };
51 
52 /**
53  * Compresses a block of memory using the given compression format.
54  *
55  * @param format The compression format to use.
56  * @param rawbytes The data to compress.
57  * @param rawsize The size in bytes of the data to compress.
58  * @param level The amount of compression to apply (between 0 and 9.)
59  *              A value of -1 indicates the default amount of compression.
60  *              Specific formats may not use every level.
61  * @return The newly compressed data.
62  **/
63 CompressedData *compress(Compressor::Format format, const char *rawbytes, size_t rawsize, int level = -1);
64 
65 /**
66  * Decompresses existing compressed data into raw bytes.
67  *
68  * @param[in] data The compressed data to decompress.
69  * @param[out] decompressedsize The size in bytes of the decompressed data.
70  * @return The newly decompressed data (allocated with new[]).
71  **/
72 char *decompress(CompressedData *data, size_t &decompressedsize);
73 
74 /**
75  * Decompresses existing compressed data into raw bytes.
76  *
77  * @param[in] format The compression format the data is in.
78  * @param[in] cbytes The compressed data to decompress.
79  * @param[in] compressedsize The size in bytes of the compressed data.
80  * @param[in,out] rawsize On input, the size in bytes of the original
81  *               uncompressed data, or 0 if unknown. On return, the size in
82  *               bytes of the newly decompressed data.
83  * @return The newly decompressed data (allocated with new[]).
84  **/
85 char *decompress(Compressor::Format format, const char *cbytes, size_t compressedsize, size_t &rawsize);
86 
87 char *encode(EncodeFormat format, const char *src, size_t srclen, size_t &dstlen, size_t linelen = 0);
88 char *decode(EncodeFormat format, const char *src, size_t srclen, size_t &dstlen);
89 
90 /**
91  * Hash the input, producing an set of bytes as output.
92  *
93  * @param[in] function The selected hash function.
94  * @param[in] input The input data to hash.
95  * @return An std::string of bytes, representing the result of the hash
96  *         function.
97  **/
98 std::string hash(HashFunction::Function function, Data *input);
99 std::string hash(HashFunction::Function function, const char *input, uint64_t size);
100 void hash(HashFunction::Function function, Data *input, HashFunction::Value &output);
101 void hash(HashFunction::Function function, const char *input, uint64_t size, HashFunction::Value &output);
102 
103 
104 bool getConstant(const char *in, EncodeFormat &out);
105 bool getConstant(EncodeFormat in, const char *&out);
106 std::vector<std::string> getConstants(EncodeFormat);
107 
108 bool getConstant(const char *in, ContainerType &out);
109 bool getConstant(ContainerType in, const char *&out);
110 std::vector<std::string> getConstants(ContainerType);
111 
112 
113 class DataModule : public Module
114 {
115 public:
116 
117 	DataModule();
118 	virtual ~DataModule();
119 
120 	// Implements Module.
getModuleType()121 	ModuleType getModuleType() const override { return M_DATA; }
getName()122 	const char *getName() const override { return "love.data"; }
123 
124 	DataView *newDataView(Data *data, size_t offset, size_t size);
125 	ByteData *newByteData(size_t size);
126 	ByteData *newByteData(const void *d, size_t size);
127 	ByteData *newByteData(void *d, size_t size, bool own);
128 
129 }; // DataModule
130 
131 } // data
132 } // love
133