-
Notifications
You must be signed in to change notification settings - Fork 1
/
bencode.hpp
401 lines (335 loc) · 10.5 KB
/
bencode.hpp
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*Copyright (c) 2014-2020, Jim Porter
All rights reserved.*/
#ifndef INC_BENCODE_HPP
#define INC_BENCODE_HPP
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cstdint>
#include <cstring>
#include <iterator>
#include <iostream>
#include <map>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
#include <boost/variant.hpp>
// Try to use std::string_view, N4480's version, or fall back to Boost's.
#ifdef __has_include
# if __has_include(<string_view>) && (__cplusplus >= 201703L || \
(defined(_MSVC_LANG) && _MSVC_LANG >= 201703L))
# include <string_view>
# define BENCODE_STRING_VIEW std::string_view
# elif __has_include(<experimental/string_view>) && \
!defined(BENCODE_NO_STDLIB_EXTS)
# include <experimental/string_view>
# define BENCODE_STRING_VIEW std::experimental::string_view
# endif
#endif
#ifndef BENCODE_STRING_VIEW
# ifdef BENCODE_USE_STDLIB_EXTS
# include <experimental/string_view>
# define BENCODE_STRING_VIEW std::experimental::string_view
# else
# include <boost/version.hpp>
# if BOOST_VERSION >= 106100
# include <boost/utility/string_view.hpp>
# define BENCODE_STRING_VIEW boost::string_view
# else
# include <boost/utility/string_ref.hpp>
# define BENCODE_STRING_VIEW boost::string_ref
# endif
# endif
#endif
namespace bencode {
using integer = long long;
using integer_view = integer;
using string = std::string;
using string_view = BENCODE_STRING_VIEW;
using data = boost::make_recursive_variant<
integer,
string,
std::vector<boost::recursive_variant_>,
std::map<string, boost::recursive_variant_>
>::type;
using data_view = boost::make_recursive_variant<
integer_view,
string_view,
std::vector<boost::recursive_variant_>,
std::map<string_view, boost::recursive_variant_>
>::type;
using list = std::vector<data>;
using dict = std::map<string, data>;
using list_view = std::vector<data_view>;
using dict_view = std::map<string_view, data_view>;
enum eof_behavior {
check_eof,
no_check_eof
};
template<typename T>
data decode(T &begin, T end);
namespace detail {
template<bool View, typename T>
typename std::conditional<View, data_view, data>::type
decode_data(T &begin, T end);
template<bool View, typename T>
typename std::conditional<View, integer_view, integer>::type
decode_int(T &begin, T end) {
assert(*begin == 'i');
++begin;
bool positive = true;
if(*begin == '-') {
positive = false;
++begin;
}
// TODO: handle overflow
integer value = 0;
for(; begin != end && std::isdigit(*begin); ++begin)
value = value * 10 + *begin - '0';
if(begin == end)
throw std::invalid_argument("unexpected end of string");
if(*begin != 'e')
throw std::invalid_argument("expected 'e'");
++begin;
return positive ? value : -value;
}
template<bool View>
class str_reader {
public:
template<typename Iter, typename Size>
inline string operator ()(Iter &begin, Iter end, Size len) {
return call(
begin, end, len,
typename std::iterator_traits<Iter>::iterator_category()
);
}
private:
template<typename Iter, typename Size>
string call(Iter &begin, Iter end, Size len, std::forward_iterator_tag) {
if(std::distance(begin, end) < static_cast<std::ptrdiff_t>(len))
throw std::invalid_argument("unexpected end of string");
std::string value(len, 0);
std::copy_n(begin, len, value.begin());
std::advance(begin, len);
return value;
}
template<typename Iter, typename Size>
string call(Iter &begin, Iter end, Size len, std::input_iterator_tag) {
std::string value(len, 0);
for(Size i = 0; i < len; i++) {
if(begin == end)
throw std::invalid_argument("unexpected end of string");
value[i] = *begin;
++begin;
}
return value;
}
};
template<>
class str_reader<true> {
public:
template<typename Iter, typename Size>
string_view operator ()(Iter &begin, Iter end, Size len) {
if(std::distance(begin, end) < static_cast<std::ptrdiff_t>(len))
throw std::invalid_argument("unexpected end of string");
string_view value(&*begin, len);
std::advance(begin, len);
return value;
}
};
template<bool View, typename T>
typename std::conditional<View, string_view, string>::type
decode_str(T &begin, T end) {
assert(std::isdigit(*begin));
std::size_t len = 0;
for(; begin != end && std::isdigit(*begin); ++begin)
len = len * 10 + *begin - '0';
if(begin == end)
throw std::invalid_argument("unexpected end of string");
if(*begin != ':')
throw std::invalid_argument("expected ':'");
++begin;
return str_reader<View>{}(begin, end, len);
}
template<bool View, typename T>
typename std::conditional<View, list_view, list>::type
decode_list(T &begin, T end) {
assert(*begin == 'l');
++begin;
typename std::conditional<View, list_view, list>::type value;
while(begin != end && *begin != 'e') {
value.push_back(decode_data<View>(begin, end));
}
if(begin == end)
throw std::invalid_argument("unexpected end of string");
++begin;
return value;
}
template<bool View, typename T>
typename std::conditional<View, dict_view, dict>::type
decode_dict(T &begin, T end) {
assert(*begin == 'd');
++begin;
typename std::conditional<View, dict_view, dict>::type value;
while(begin != end && *begin != 'e') {
if(!std::isdigit(*begin))
throw std::invalid_argument("expected string token");
auto k = decode_str<View>(begin, end);
auto v = decode_data<View>(begin, end);
auto result = value.emplace(std::move(k), std::move(v));
if(!result.second) {
throw std::invalid_argument(
"duplicated key in dict: " + std::string(result.first->first)
);
}
}
if(begin == end)
throw std::invalid_argument("unexpected end of string");
++begin;
return value;
}
template<bool View, typename T>
typename std::conditional<View, data_view, data>::type
decode_data(T &begin, T end) {
if(begin == end)
throw std::invalid_argument("unexpected end of string");
if(*begin == 'i')
return decode_int<View>(begin, end);
else if(*begin == 'l')
return decode_list<View>(begin, end);
else if(*begin == 'd')
return decode_dict<View>(begin, end);
else if(std::isdigit(*begin))
return decode_str<View>(begin, end);
throw std::invalid_argument("unexpected type");
}
}
template<typename T>
inline data decode(T &begin, T end) {
return detail::decode_data<false>(begin, end);
}
template<typename T>
inline data decode(const T &begin, T end) {
T b(begin);
return detail::decode_data<false>(b, end);
}
inline data decode(const string_view &s) {
return decode(s.begin(), s.end());
}
inline data decode(std::istream &s, eof_behavior e = check_eof) {
std::istreambuf_iterator<char> begin(s), end;
auto result = decode(begin, end);
// If we hit EOF, update the parent stream.
if(e == check_eof && begin == end)
s.setstate(std::ios_base::eofbit);
return result;
}
template<typename T>
inline data_view decode_view(T &begin, T end) {
return detail::decode_data<true>(begin, end);
}
template<typename T>
inline data_view decode_view(const T &begin, T end) {
T b(begin);
return detail::decode_data<true>(b, end);
}
inline data_view decode_view(const string_view &s) {
return decode_view(s.begin(), s.end());
}
namespace detail {
class list_encoder {
public:
inline list_encoder(std::ostream &os) : os(os) {
os.put('l');
}
inline ~list_encoder() {
os.put('e');
}
template<typename T>
inline list_encoder & add(T &&value);
private:
std::ostream &os;
};
class dict_encoder {
public:
inline dict_encoder(std::ostream &os) : os(os) {
os.put('d');
}
inline ~dict_encoder() {
os.put('e');
}
template<typename T>
inline dict_encoder & add(const string_view &key, T &&value);
private:
std::ostream &os;
};
}
// TODO: make these use unformatted output?
inline void encode(std::ostream &os, integer value) {
os << "i" << value << "e";
}
inline void encode(std::ostream &os, const string_view &value) {
os << value.size() << ":" << value;
}
template<typename T>
void encode(std::ostream &os, const std::vector<T> &value) {
detail::list_encoder e(os);
for(auto &&i : value)
e.add(i);
}
template<typename T>
void encode(std::ostream &os, const std::map<string, T> &value) {
detail::dict_encoder e(os);
for(auto &&i : value)
e.add(i.first, i.second);
}
template<typename T>
void encode(std::ostream &os, const std::map<string_view, T> &value) {
detail::dict_encoder e(os);
for(auto &&i : value)
e.add(i.first, i.second);
}
namespace detail {
class encode_visitor : public boost::static_visitor<> {
public:
inline encode_visitor(std::ostream &os) : os(os) {}
template<typename T>
void operator ()(T &&operand) const {
encode(os, std::forward<T>(operand));
}
private:
std::ostream &os;
};
}
// Overload for `data` and `data_view`, but only if the passed-in type is
// already one of the two. Don't accept an implicit conversion!
template<typename T>
auto encode(std::ostream &os, const T &value) ->
typename std::enable_if<
std::is_same<T, data>::value || std::is_same<T, data_view>::value
>::type {
boost::apply_visitor(detail::encode_visitor(os), value);
}
namespace detail {
template<typename T>
inline list_encoder & list_encoder::add(T &&value) {
encode(os, std::forward<T>(value));
return *this;
}
template<typename T>
inline dict_encoder &
dict_encoder::add(const string_view &key, T &&value) {
encode(os, key);
encode(os, std::forward<T>(value));
return *this;
}
}
template<typename T>
std::string encode(T &&t) {
std::stringstream ss;
encode(ss, std::forward<T>(t));
return ss.str();
}
}
#endif