-
Notifications
You must be signed in to change notification settings - Fork 0
/
bencodeparser.cpp
154 lines (141 loc) · 4.3 KB
/
bencodeparser.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
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
#include "include/bencodeparser.h"
#include "include/bencode2json.h"
#include <cstring>
namespace link {
// convert a c string to int
int str2int(const char *cstr, int lenth) {
int result = 0;
int base = 1;
while (--lenth >= 0) {
result += (cstr[lenth] - '0') * base;
base *= 10;
}
return result;
}
std::string bencode2json_parser::int_parse(int begin) {
for(int i = begin + 1; i < len; ++i) {
if (src[i] == 'e') {
// str[begin] = 'i', so i - begin bytes is enough
int size = i - begin;
char *res = new char[size];
memcpy(res, src + begin + 1, size - 1);
res[size] = 0;
std::string s {res};
delete [] res;
pos = i;
return s;
}
}
throw syntax_error{};
}
std::string bencode2json_parser::str_parse(int begin) {
for (int i = begin; i < len; ++i) {
if (src[i] == ':') {
int cnt = str2int(src + begin, i - begin);
// TODO: some out of bounds exception check may be needed
char *res = new char[cnt + 1];
memcpy(res, src + i + 1, cnt);
res[cnt] = 0;
std::string s {res};
delete [] res;
pos = i + cnt;
return s;
}
}
throw syntax_error{};
}
std::string bencode2json_parser::list_parse(int begin) {
std::string s = "[";
for(int i = begin + 1; i < len; ++i) {
// The final end marker
if (src[i] == 'e') {
int length = s.size();
// if we get an empty list
if (length == 1) {
s += "]";
} else {
s[length - 1] = ']';
}
pos = i;
return s;
}
action_t act = get_action(src[i]);
// so we get an element of a list
std::string tmp = (this->*act)(i);
if(tmp[0] == '{' || tmp[0] == '[') {
s += (tmp + ",");
} else {
s += ("\"" + tmp + "\"" + ",");
}
i = pos;
}
throw syntax_error{};
}
std::string bencode2json_parser::dict_parse(int begin) {
std::string s = "{";
ele_type_t type = ele_type_t::KEY;
for (int i = begin + 1; i < len; ++i) {
if (src[i] == 'e') {
int length = s.size();
if (length == 1) {
s += "}";
} else {
s[length - 1] = '}';
}
pos = i;
return s;
}
action_t act = get_action(src[i]);
std::string tmp = (this->*act)(i);
if(tmp[0] == '{' || tmp[0] == '[') {
s += tmp;
} else {
s += ("\"" + tmp + "\"");
}
// then colon or comma
if (type == ele_type_t::KEY) {
s += ":";
type = ele_type_t::VALUE;
} else {
s += ",";
type = ele_type_t::KEY;
}
i = pos;
}
throw syntax_error{};
}
bencode2json_parser::action_t bencode2json_parser::get_action(char c) const {
switch(c) {
case 'i':
return &bencode2json_parser::int_parse;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return &bencode2json_parser::str_parse;
case 'l':
return &bencode2json_parser::list_parse;
case 'd':
return &bencode2json_parser::dict_parse;
}
}
bencode2json_parser::bencode2json_parser(const char *str, int len) : len(len) {
src = new char[len + 1];
memcpy(src, str, len);
src[len] = 0;
}
bencode2json_parser::~bencode2json_parser() {
delete [] src;
}
std::string bencode2json_parser::parse() {
int pos = 0;
action_t act = get_action(src[0]);
return (this->*act)(0);
}
}