This repository has been archived by the owner on Dec 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
fecpp_python.cpp
83 lines (64 loc) · 2.22 KB
/
fecpp_python.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
#include <boost/python.hpp>
#include <boost/python/stl_iterator.hpp>
#include "fecpp.h"
using namespace fecpp;
class save_results
{
public:
save_results(size_t n) : results(n) {}
void operator()(size_t i, size_t, const byte fec[], size_t fec_len)
{
results[i].append(reinterpret_cast<const char*>(fec), fec_len);
}
boost::python::list get_results() const
{
boost::python::list list;
for(size_t i = 0; i != results.size(); ++i)
list.append(boost::python::str(results[i].c_str(),
results[i].length()));
return list;
}
private:
std::vector<std::string> results;
};
boost::python::list fec_encode(fec_code* code,
const std::string& input)
{
save_results fec_saver(code->get_N());
code->encode(reinterpret_cast<const byte*>(input.c_str()),
input.size(),
std::ref(fec_saver));
return fec_saver.get_results();
}
boost::python::list fec_decode(fec_code* code,
boost::python::dict dict)
{
size_t share_size = 0;
std::map<size_t, const byte*> shares;
std::vector<std::string> share_data(code->get_N());
for(size_t i = 0; i != code->get_N(); ++i)
{
if(!dict.has_key(i))
continue;
share_data[i] = boost::python::extract<std::string>(dict.get(i));
if(share_size == 0)
share_size = share_data[i].length();
else if(share_size != share_data[i].length())
throw std::invalid_argument("FEC shares of unusual size");
shares[i] = reinterpret_cast<const byte*>(share_data[i].c_str());
}
if(shares.size() < code->get_K())
throw std::invalid_argument("Could not decode, insufficient shares");
save_results fec_saver(code->get_K());
code->decode(shares, share_size, std::ref(fec_saver));
return fec_saver.get_results();
}
BOOST_PYTHON_MODULE(pyfecpp)
{
boost::python::class_<fec_code>
("fec_code", boost::python::init<size_t, size_t>())
.def("encode", fec_encode)
.def("decode", fec_decode)
.add_property("K", &fec_code::get_K)
.add_property("N", &fec_code::get_N);
}