-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbencode.h
More file actions
48 lines (36 loc) · 812 Bytes
/
bencode.h
File metadata and controls
48 lines (36 loc) · 812 Bytes
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
#ifndef _BENCODE_H
#define _BENCODE_H
#include "variant.h"
#include <string>
#include <assert.h>
namespace ben {
class decoder {
public:
decoder(const std::string &buf);
~decoder();
const char *error() const { return m_error; }
int pos() const { return m_pos; }
bool at_end() const { return m_pos == m_len; }
void set_error(const char *fmt, ...);
int decode(variant *value);
static int decode_all(variant *value, const std::string &buf)
{
decoder decoder(buf);
if (decoder.decode(value))
return -1;
if (!decoder.at_end())
return -1;
return 0;
}
private:
std::string m_buf;
size_t m_pos;
size_t m_len;
int m_depth;
char *m_error;
int decode_string(std::string *s);
bool validate_int(const std::string &s);
};
int encode(std::string *out, const variant &value);
}
#endif