-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_bencoder.h
More file actions
344 lines (299 loc) · 11.1 KB
/
Copy pathcustom_bencoder.h
File metadata and controls
344 lines (299 loc) · 11.1 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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#ifndef CUSTOM_BENCODER_H
#define CUSTOM_BENCODER_H
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <memory>
#include <type_traits>
#include <cstdint>
namespace bencoding
{
struct string_subs
{
public:
std::string str;
std::string::const_iterator citer;
string_subs(std::string input) : str{input}, citer{str.begin()} {}
void refresh() { citer = str.begin(); }
};
class bencode_base
{
public:
static const char delimiter_token = ':';
static const char end_token = 'e';
static const char integer_token = 'i';
static const char list_token = 'l';
static const char dict_token = 'd';
bencode_base() = default;
bencode_base(const bencode_base &) = default;
bencode_base &operator=(const bencode_base &) = default;
bencode_base(bencode_base &&other) noexcept = default;
bencode_base &operator=(bencode_base &&other) noexcept = default;
virtual ~bencode_base() = default;
virtual std::string encode() const = 0;
virtual void encode_n_dump(std::ostream &out) const = 0;
virtual void decode(const std::string &in, std::string::const_iterator &start) = 0;
virtual std::string get_as_str() const = 0;
};
class bencode_integer : public bencode_base
{
public:
using value_type = int64_t;
bencode_integer() = default;
bencode_integer(value_type value) : integer_value_{value} {}
bencode_integer &operator=(value_type value)
{
integer_value_ = value;
return *this;
}
value_type get() const { return integer_value_; }
std::string encode() const override
{
return std::string(1, integer_token) + std::to_string(integer_value_) + std::string(1, end_token);
}
void encode_n_dump(std::ostream &out) const override
{
out << integer_token << integer_value_ << end_token;
}
void decode(const std::string &in, std::string::const_iterator &start) override
{
start++; // Assuming *start == 'i'
value_type value = 0;
bool neg = false;
if (*start == '-')
neg = true, start++;
while (start != in.end() && *start != end_token)
value = value * 10 + (*start - '0'), start++;
integer_value_ = neg ? -value : value;
if (start != in.end())
start++; // assuming it ends with 'e'
}
std::string get_as_str() const override
{
return std::to_string(integer_value_);
}
private:
value_type integer_value_{0};
};
class bencode_string : public bencode_base
{
public:
using string_type = std::string;
using iterator = string_type::iterator;
using const_iterator = string_type::const_iterator;
using CharT = char;
bencode_string() = default;
bencode_string(string_type str) : str_{str} {}
bencode_string(const CharT *cstr) : str_{cstr} {}
iterator begin() { return str_.begin(); }
const_iterator begin() const { return str_.begin(); }
const_iterator cbegin() const { return str_.cbegin(); }
iterator end() { return str_.end(); }
const_iterator end() const { return str_.end(); }
const_iterator cend() const { return str_.cend(); }
bencode_string &operator=(string_type str)
{
str_ = str;
return *this;
}
bencode_string &operator=(const CharT *cstr)
{
str_ = cstr;
return *this;
}
size_t size() const { return str_.length(); }
string_type get() const { return str_; }
std::string encode() const override
{
return std::to_string(str_.length()) + std::string(1, delimiter_token) + str_;
}
void encode_n_dump(std::ostream &out) const override
{
out << str_.length() << delimiter_token << str_;
}
void decode(const std::string &in, std::string::const_iterator &start) override
{
size_t len = 0; // Assuming it starts with length
while (start != in.end() && *start != delimiter_token)
len = len * 10 + (*start - '0'), start++;
start++;
str_ = std::string(start, start + len);
start += len;
}
std::string get_as_str() const override
{
return this->get();
}
private:
string_type str_;
};
std::unique_ptr<bencode_base> make_value(const std::string &in, std::string::const_iterator &start);
template <typename T>
concept bencodeDerived = std::is_base_of<bencode_base, T>::value;
class bencode_list : public bencode_base
{
public:
using value_ptr_type = std::unique_ptr<bencode_base>;
using container_type = std::vector<value_ptr_type>;
using iterator = container_type::iterator;
using const_iterator = container_type::const_iterator;
bencode_list() = default;
bencode_list(size_t size_) : bencode_list() { list_.resize(size_); }
iterator begin() { return list_.begin(); }
const_iterator begin() const { return list_.begin(); }
const_iterator cbegin() const { return list_.cbegin(); }
iterator end() { return list_.end(); }
const_iterator end() const { return list_.end(); }
const_iterator cend() const { return list_.cend(); }
size_t size() const { return list_.size(); }
value_ptr_type &operator[](std::size_t index) { return list_[index]; }
template <bencodeDerived T, typename... Args>
void push_back(Args &&...args)
{
list_.emplace_back(std::make_unique<T>(std::forward<Args>(args)...));
}
void pop_back()
{
if (!list_.empty())
list_.pop_back();
}
std::string encode() const override
{
std::string enc_str(1, list_token);
for (const value_ptr_type &ptr_ : list_)
if (ptr_)
enc_str += (ptr_->encode());
enc_str += end_token;
return enc_str;
}
void encode_n_dump(std::ostream &out) const override
{
out << list_token;
for (const value_ptr_type &ptr_ : list_)
if (ptr_)
ptr_->encode_n_dump(out);
out << end_token;
}
void decode(const std::string &in, std::string::const_iterator &start) override
{
list_.clear();
start++; // assuming *start == 'l'
while (start != in.end() && *start != end_token)
list_.push_back(make_value(in, start));
start++; // assuming *start == 'e'
}
std::string get_as_str() const override
{
return this->encode();
}
private:
container_type list_;
};
class bencode_dict : public bencode_base
{
private:
// custom comparator
template <typename T_>
struct lexicographical_compare
{
bool operator()(const T_ &lhs, const T_ &rhs) const
{
return std::lexicographical_compare(lhs.cbegin(), lhs.cend(), rhs.cbegin(), rhs.cend());
}
};
public:
using key_type = bencode_string;
using mapped_type = std::unique_ptr<bencode_base>;
using value_type = std::pair<const key_type, mapped_type>;
using comparator_type = lexicographical_compare<key_type>;
using container_type = std::map<key_type, mapped_type, comparator_type>;
using iterator = container_type::iterator;
using const_iterator = container_type::const_iterator;
bencode_dict() = default;
iterator begin() { return dict_.begin(); }
const_iterator begin() const { return dict_.begin(); }
const_iterator cbegin() const { return dict_.cbegin(); }
iterator end() { return dict_.end(); }
const_iterator end() const { return dict_.end(); }
const_iterator cend() const { return dict_.cend(); }
std::size_t size() const { return dict_.size(); }
mapped_type &operator[](const key_type &index) { return dict_[index]; }
std::size_t erase(const key_type &key) { return dict_.erase(key); }
iterator erase(iterator pos) { return dict_.erase(pos); }
iterator find(const key_type &key) { return dict_.find(key); }
std::pair<iterator, bool> insert(const key_type &key, mapped_type value)
{
return dict_.emplace(key, std::move(value));
}
template <bencodeDerived T, typename... Args>
std::pair<iterator, bool> insert_or_assign(const key_type &key, Args &&...args)
{
mapped_type value_ptr = std::make_unique<T>(std::forward<Args>(args)...);
return dict_.insert_or_assign(key, std::move(value_ptr));
}
// Rejected Idea :
// overloading [] operator for assigning derived object
// to later construct unique_ptr. (since too messy and unintuitive)
std::string encode() const override
{
std::string enc_str(1, dict_token);
for (const auto &[key_, ptr_] : dict_)
{
if (ptr_)
{
enc_str += key_.encode();
enc_str += ptr_->encode();
}
}
enc_str += end_token;
return enc_str;
}
void encode_n_dump(std::ostream &out) const override
{
out << dict_token;
for (const auto &[key_, ptr_] : dict_)
{
if (ptr_)
{
key_.encode_n_dump(out);
ptr_->encode_n_dump(out);
}
}
out << end_token;
}
void decode(const std::string &in, std::string::const_iterator &start) override
{
dict_.clear();
start++; // assuming *start = 'd'
while (start != in.end() && *start != end_token)
{
key_type key_;
key_.decode(in, start);
dict_[key_] = make_value(in, start);
}
start++; // assuming *start = 'e'
}
std::string get_as_str() const override
{
return this->encode();
}
private:
container_type dict_;
};
std::unique_ptr<bencode_base> make_value(const std::string &in, std::string::const_iterator &start)
{
std::unique_ptr<bencode_base> ptr_;
if (*start == bencode_base::integer_token)
ptr_ = std::make_unique<bencode_integer>();
else if (*start == bencode_base::list_token)
ptr_ = std::make_unique<bencode_list>();
else if (*start == bencode_base::dict_token)
ptr_ = std::make_unique<bencode_dict>();
else
ptr_ = std::make_unique<bencode_string>();
ptr_->decode(in, start);
return ptr_;
}
}
#endif