-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
executable file
·153 lines (104 loc) · 3.66 KB
/
main.cpp
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Header files
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <vector>
// Check if using Emscripten
#ifdef __EMSCRIPTEN__
// Header files
#include <emscripten.h>
#include "smaz.h"
// Otherwise
#else
// Header files
extern "C" {
#include "smaz.h"
}
#endif
using namespace std;
// Definitions
// Check if using Emscripten
#ifdef __EMSCRIPTEN__
// Export
#define EXPORT extern "C"
// Otherwise
#else
// Export
#define EXPORT static
// Emscripten keepalive
#define EMSCRIPTEN_KEEPALIVE
#endif
// Constants
// Invalid size
static const size_t INVALID_SIZE = SIZE_MAX;
// Size scaling factor
static const uint8_t SIZE_SCALING_FACTOR = 2;
// Function prototypes
// Invalid size
EXPORT size_t EMSCRIPTEN_KEEPALIVE invalidSize();
// Get compress size
EXPORT size_t EMSCRIPTEN_KEEPALIVE compressSize(const uint8_t *input, size_t inputSize);
// Compress
EXPORT bool EMSCRIPTEN_KEEPALIVE compress(uint8_t *result, size_t resultSize, const uint8_t *input, size_t inputSize);
// Get decompress size
EXPORT size_t EMSCRIPTEN_KEEPALIVE decompressSize(const uint8_t *input, size_t inputSize);
// Deompress
EXPORT bool EMSCRIPTEN_KEEPALIVE decompress(uint8_t *result, size_t resultSize, const uint8_t *input, size_t inputSize);
// Supporting function implementation
// Invalid size
size_t invalidSize() {
// Return invalid size
return INVALID_SIZE;
}
// Compress size
size_t compressSize(const uint8_t *input, size_t inputSize) {
// Go through incrementing result sizes
for(size_t i = 0; inputSize + i * SIZE_SCALING_FACTOR >= inputSize + i && inputSize + i != SIZE_MAX;) {
// Initialize result
vector<uint8_t> result(inputSize + i);
// Get result size
size_t resultSize = smaz_compress(const_cast<char *>(reinterpret_cast<const char *>(input)), inputSize, reinterpret_cast<char *>(result.data()), result.size());
// Clear memory
explicit_bzero(result.data(), result.size());
// Check if the result size was large enough
if(resultSize != result.size() + 1) {
// Return result size
return resultSize;
}
// Update size
i = i ? i * SIZE_SCALING_FACTOR : 1;
}
// Return invalid size
return INVALID_SIZE;
}
// Compress
bool compress(uint8_t *result, size_t resultSize, const uint8_t *input, size_t inputSize) {
// Return if compressing the input was successful
return static_cast<unsigned int>(smaz_compress(const_cast<char *>(reinterpret_cast<const char *>(input)), inputSize, reinterpret_cast<char *>(result), resultSize)) == resultSize;
}
// Decompress size
size_t decompressSize(const uint8_t *input, size_t inputSize) {
// Go through incrementing result sizes
for(size_t i = 0; inputSize + i * SIZE_SCALING_FACTOR >= inputSize + i && inputSize + i != SIZE_MAX;) {
// Initialize result
vector<uint8_t> result(inputSize + i);
// Get result size
size_t resultSize = smaz_decompress(const_cast<char *>(reinterpret_cast<const char *>(input)), inputSize, reinterpret_cast<char *>(result.data()), result.size());
// Clear memory
explicit_bzero(result.data(), result.size());
// Check if the result size was large enough
if(resultSize != result.size() + 1) {
// Return result size
return (resultSize == 0 && inputSize != 0) ? INVALID_SIZE : resultSize;
}
// Update size
i = i ? i * SIZE_SCALING_FACTOR : 1;
}
// Return invalid size
return INVALID_SIZE;
}
// Decompress
bool decompress(uint8_t *result, size_t resultSize, const uint8_t *input, size_t inputSize) {
// Return if compressing the input was successful
return static_cast<unsigned int>(smaz_decompress(const_cast<char *>(reinterpret_cast<const char *>(input)), inputSize, reinterpret_cast<char *>(result), resultSize)) == resultSize;
}