-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
98 lines (89 loc) · 3.31 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
//
// Created by matt on 16/03/19.
//
#include <string>
#include <sstream>
#include <iostream>
#include <bzlib.h>
#include <emscripten/bind.h>
#include <iomanip>
class BZ2CC {
public:
BZ2CC() = default;
int error = BZ_OK;
std::string error_msg;
unsigned char* output;
unsigned int outLen;
emscripten::val getOutput() const {
return emscripten::val(emscripten::typed_memory_view(outLen, output));
}
};
BZ2CC compressBZ2 (std::string input, int block_size, int work_factor) {
BZ2CC resp;
// Convert C++ strings to terrible C pointer arrays
auto inp = (char*) input.c_str();
resp.outLen = (unsigned int) (input.length() * 1.01) + 600; // Advised output buffer length is 101% of input + 600 bytes
char* out = new char[resp.outLen];
resp.error = BZ2_bzBuffToBuffCompress(out, &resp.outLen, inp, (unsigned int) input.length(), block_size, 0, work_factor);
switch (resp.error) {
case BZ_OK:
resp.output = (unsigned char*) out;
break;
case BZ_CONFIG_ERROR:
case BZ_PARAM_ERROR:
case BZ_OUTBUFF_FULL:
resp.error_msg = "There's been an issue with libbzip2-wasm. Please file an issue at https://github.com/artemisbot/libbzip2-wasm.";
break;
case BZ_MEM_ERROR:
resp.error_msg = "libbzip2-wasm has run out of memory.";
break;
default:
resp.error_msg = "Unknown error.";
break;
}
return resp;
}
BZ2CC decompressBZ2 (std::string input, int small) {
BZ2CC resp;
// Convert C++ strings to terrible C pointer arrays
auto inp = (char*) input.c_str();
resp.outLen = (unsigned int) 10000000; // I'm pretty sure that bzip2 isn't *this* good
char* out = new char[resp.outLen];
resp.error = BZ2_bzBuffToBuffDecompress(out, &resp.outLen, inp, (unsigned int) input.length(), small, 0);
switch (resp.error) {
case BZ_OK:
resp.output = (unsigned char*) out;
break;
case BZ_CONFIG_ERROR:
case BZ_PARAM_ERROR:
resp.error_msg = "There's been an issue with libbzip2-wasm. Please file an issue at https://github.com/artemisbot/libbzip2-wasm.";
break;
case BZ_OUTBUFF_FULL:
resp.error_msg = "The decompressed data exceeds the length of the destination buffer. The max size of a decrypted file is 100MB.";
break;
case BZ_MEM_ERROR:
resp.error_msg = "libbzip2-wasm has run out of memory.";
break;
case BZ_DATA_ERROR:
resp.error_msg = "The input data did not pass the integrity checks.";
break;
case BZ_DATA_ERROR_MAGIC:
resp.error_msg = "The input data does not begin with the correct magic bytes.";
break;
case BZ_UNEXPECTED_EOF:
resp.error_msg = "The input data ends unexpectedly.";
break;
default:
resp.error_msg = "Unknown error.";
break;
}
return resp;
}
EMSCRIPTEN_BINDINGS(my_module) {
emscripten::class_<BZ2CC>("BZ2CC")
.property("errorMsg", &BZ2CC::error_msg)
.property("error", &BZ2CC::error)
.property("output", &BZ2CC::getOutput);
emscripten::function("compressBZ2", &compressBZ2);
emscripten::function("decompressBZ2", &decompressBZ2);
}