forked from NicolasFlamel1/SMAZ-Node.js-Addon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
219 lines (156 loc) · 5.56 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Header files
#include <cstring>
#include <node_api.h>
#include <tuple>
#include <vector>
using namespace std;
// SMAZ namespace
namespace Smaz {
// Header files
#include "./SMAZ-NPM-Package-master/main.cpp"
}
// Constants
// Operation failed
static napi_value OPERATION_FAILED;
// Function prototypes
// Compress
static napi_value compress(napi_env environment, napi_callback_info arguments);
// Decompress
static napi_value decompress(napi_env environment, napi_callback_info arguments);
// Uint8 array to buffer
static tuple<uint8_t *, size_t, bool> uint8ArrayToBuffer(napi_env environment, napi_value uint8Array);
// Buffer to uint8 array
static napi_value bufferToUint8Array(napi_env environment, uint8_t *data, size_t size);
// Main function
// Initialize module
NAPI_MODULE_INIT() {
// Check if initializing operation failed failed
if(napi_get_null(env, &OPERATION_FAILED) != napi_ok) {
// Return nothing
return nullptr;
}
// Check if creating compress property failed
napi_value temp;
if(napi_create_function(env, nullptr, 0, compress, nullptr, &temp) != napi_ok || napi_set_named_property(env, exports, "compress", temp) != napi_ok) {
// Return nothing
return nullptr;
}
// Check if creating decompress property failed
if(napi_create_function(env, nullptr, 0, decompress, nullptr, &temp) != napi_ok || napi_set_named_property(env, exports, "decompress", temp) != napi_ok) {
// Return nothing
return nullptr;
}
// Check if creating operation failed property failed
if(napi_set_named_property(env, exports, "OPERATION_FAILED", OPERATION_FAILED) != napi_ok) {
// Return nothing
return nullptr;
}
// Return exports
return exports;
}
// Supporting function implementation
// Compress
napi_value compress(napi_env environment, napi_callback_info arguments) {
// Check if not enough arguments were provided
size_t argc = 1;
vector<napi_value> argv(argc);
if(napi_get_cb_info(environment, arguments, &argc, argv.data(), nullptr, nullptr) != napi_ok || argc != argv.size()) {
// Return operation failed
return OPERATION_FAILED;
}
// Check if getting input from arguments failed
const tuple<uint8_t *, size_t, bool> input = uint8ArrayToBuffer(environment, argv[0]);
if(!get<2>(input)) {
// Return operation failed
return OPERATION_FAILED;
}
// Check if getting compressed size failed
const size_t compressedSize = Smaz::compressSize(get<0>(input), get<1>(input));
if(compressedSize == Smaz::invalidSize()) {
// Return operation failed
return OPERATION_FAILED;
}
// Check if compressing input failed
vector<uint8_t> result(compressedSize);
if(!Smaz::compress(result.data(), result.size(), get<0>(input), get<1>(input))) {
// Return operation failed
return OPERATION_FAILED;
}
// Return result as a uint8 array
return bufferToUint8Array(environment, result.data(), result.size());
}
// Decompress
napi_value decompress(napi_env environment, napi_callback_info arguments) {
// Check if not enough arguments were provided
size_t argc = 1;
vector<napi_value> argv(argc);
if(napi_get_cb_info(environment, arguments, &argc, argv.data(), nullptr, nullptr) != napi_ok || argc != argv.size()) {
// Return operation failed
return OPERATION_FAILED;
}
// Check if getting input from arguments failed
const tuple<uint8_t *, size_t, bool> input = uint8ArrayToBuffer(environment, argv[0]);
if(!get<2>(input)) {
// Return operation failed
return OPERATION_FAILED;
}
// Check if getting decompressed size failed
const size_t decompressedSize = Smaz::decompressSize(get<0>(input), get<1>(input));
if(decompressedSize == Smaz::invalidSize()) {
// Return operation failed
return OPERATION_FAILED;
}
// Check if decompressing input failed
vector<uint8_t> result(decompressedSize);
if(!Smaz::decompress(result.data(), result.size(), get<0>(input), get<1>(input))) {
// Return operation failed
return OPERATION_FAILED;
}
// Return result as a uint8 array
return bufferToUint8Array(environment, result.data(), result.size());
}
// Uint8 array to buffer
tuple<uint8_t *, size_t, bool> uint8ArrayToBuffer(napi_env environment, napi_value uint8Array) {
// Check if uint8 array isn't a typed array
bool isTypedArray;
if(napi_is_typedarray(environment, uint8Array, &isTypedArray) != napi_ok || !isTypedArray) {
// Return failure
return {nullptr, 0, false};
}
// Check if uint8 array isn't a uint8 array
napi_typedarray_type type;
size_t size;
uint8_t *data;
if(napi_get_typedarray_info(environment, uint8Array, &type, &size, reinterpret_cast<void **>(&data), nullptr, nullptr) != napi_ok || type != napi_uint8_array) {
// Return failure
return {nullptr, 0, false};
}
// Return data and size
return {data, size, true};
}
// Buffer to uint8 array
napi_value bufferToUint8Array(napi_env environment, uint8_t *data, size_t size) {
// Check if creating array buffer failed
uint8_t *arrayBufferData;
napi_value arrayBuffer;
if(napi_create_arraybuffer(environment, size, reinterpret_cast<void **>(&arrayBufferData), &arrayBuffer) != napi_ok) {
// Clear data
memset(data, 0, size);
// Return operation failed
return OPERATION_FAILED;
}
// Copy data to array buffer
memcpy(arrayBufferData, data, size);
// Clear data
memset(data, 0, size);
// Check if creating uint8 array from array buffer failed
napi_value uint8Array;
if(napi_create_typedarray(environment, napi_uint8_array, size, arrayBuffer, 0, &uint8Array) != napi_ok) {
// Clear array buffer
memset(arrayBufferData, 0, size);
// Return operation failed
return OPERATION_FAILED;
}
// Return uint8 array
return uint8Array;
}