-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode.cpp
More file actions
63 lines (48 loc) · 1.39 KB
/
encode.cpp
File metadata and controls
63 lines (48 loc) · 1.39 KB
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
#include <boost/dynamic_bitset.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include "encode.hpp"
using namespace std;
using namespace boost::numeric;
using boost::dynamic_bitset;
const auto generator_mat = [] {
ublas::matrix<bool> generator_m(7, 4);
vector<bool> generator_init = {
1, 1, 0, 1,
1, 0, 1, 1,
1, 0, 0, 0,
0, 1, 1, 1,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
};
copy(generator_init.begin(),
generator_init.end(),
generator_m.data().begin()
);
return generator_m;
}();
vector<uint8_t> encode(const vector<uint8_t> &bytes) {
vector<bool> plain;
plain.reserve(bytes.size() * 8);
for (auto && c : bytes) {
for (auto i = 0u; i < 8u; ++i) {
plain.push_back(c & (1 << i));
}
}
dynamic_bitset<uint8_t> result;
for (auto it = plain.begin(); it != plain.end(); advance(it, 4)) {
ublas::matrix<bool> value_mat(4, 1);
copy(it, it+4, value_mat.data().begin());
auto prod = ublas::prod(generator_mat, value_mat);
for_each(prod.begin1(), prod.end1(), [&result] (auto v) {
result.push_back(v % 2);
});
}
result.push_back(1);
while (result.size() % 8 != 0) {
result.push_back(0);
}
vector<uint8_t> blocks(result.num_blocks());
to_block_range(result, blocks.begin());
return blocks;
}