forked from shiyanhui/dht
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bencode.go
263 lines (214 loc) · 5.16 KB
/
bencode.go
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
package dht
import (
"bytes"
"errors"
"strconv"
"strings"
"unicode"
"unicode/utf8"
)
// find returns the index of first target in data starting from `start`.
// It returns -1 if target not found.
func find(data []byte, start int, target rune) (index int) {
index = bytes.IndexRune(data[start:], target)
if index != -1 {
return index + start
}
return index
}
// DecodeString decodes a string in the data. It returns a tuple
// (decoded result, the end position, error).
func DecodeString(data []byte, start int) (
result interface{}, index int, err error) {
if start >= len(data) || data[start] < '0' || data[start] > '9' {
err = errors.New("invalid string bencode")
return
}
i := find(data, start, ':')
if i == -1 {
err = errors.New("':' not found when decode string")
return
}
length, err := strconv.Atoi(string(data[start:i]))
if err != nil {
return
}
if length < 0 {
err = errors.New("invalid length of string")
return
}
index = i + 1 + length
if index > len(data) || index < i+1 {
err = errors.New("out of range")
return
}
result = string(data[i+1 : index])
return
}
// DecodeInt decodes int value in the data.
func DecodeInt(data []byte, start int) (
result interface{}, index int, err error) {
if start >= len(data) || data[start] != 'i' {
err = errors.New("invalid int bencode")
return
}
index = find(data, start+1, 'e')
if index == -1 {
err = errors.New("':' not found when decode int")
return
}
result, err = strconv.Atoi(string(data[start+1 : index]))
if err != nil {
return
}
index++
return
}
// decodeItem decodes an item of dict or list.
func decodeItem(data []byte, i int) (
result interface{}, index int, err error) {
var decodeFunc = []func([]byte, int) (interface{}, int, error){
DecodeString, DecodeInt, DecodeList, DecodeDict,
}
for _, f := range decodeFunc {
result, index, err = f(data, i)
if err == nil {
return
}
}
err = errors.New("invalid bencode when decode item")
return
}
// DecodeList decodes a list value.
func DecodeList(data []byte, start int) (
result interface{}, index int, err error) {
if start >= len(data) || data[start] != 'l' {
err = errors.New("invalid list bencode")
return
}
var item interface{}
r := make([]interface{}, 0, 8)
index = start + 1
for index < len(data) {
char, _ := utf8.DecodeRune(data[index:])
if char == 'e' {
break
}
item, index, err = decodeItem(data, index)
if err != nil {
return
}
r = append(r, item)
}
if index == len(data) {
err = errors.New("'e' not found when decode list")
return
}
index++
result = r
return
}
// DecodeDict decodes a map value.
func DecodeDict(data []byte, start int) (
result interface{}, index int, err error) {
if start >= len(data) || data[start] != 'd' {
err = errors.New("invalid dict bencode")
return
}
var item, key interface{}
r := make(map[string]interface{})
index = start + 1
for index < len(data) {
char, _ := utf8.DecodeRune(data[index:])
if char == 'e' {
break
}
if !unicode.IsDigit(char) {
err = errors.New("invalid dict bencode")
return
}
key, index, err = DecodeString(data, index)
if err != nil {
return
}
if index >= len(data) {
err = errors.New("out of range")
return
}
item, index, err = decodeItem(data, index)
if err != nil {
return
}
r[key.(string)] = item
}
if index == len(data) {
err = errors.New("'e' not found when decode dict")
return
}
index++
result = r
return
}
// Decode decodes a bencoded string to string, int, list or map.
func Decode(data []byte) (result interface{}, err error) {
result, _, err = decodeItem(data, 0)
return
}
// EncodeString encodes a string value.
func EncodeString(data string) string {
return strings.Join([]string{strconv.Itoa(len(data)), data}, ":")
}
// EncodeInt encodes a int value.
func EncodeInt(data int) string {
return strings.Join([]string{"i", strconv.Itoa(data), "e"}, "")
}
// EncodeItem encodes an item of dict or list.
func encodeItem(data interface{}) (item string) {
switch v := data.(type) {
case string:
item = EncodeString(v)
case int:
item = EncodeInt(v)
case []interface{}:
item = EncodeList(v)
case map[string]interface{}:
item = EncodeDict(v)
default:
panic("invalid type when encode item")
}
return
}
// EncodeList encodes a list value.
func EncodeList(data []interface{}) string {
result := make([]string, len(data))
for i, item := range data {
result[i] = encodeItem(item)
}
return strings.Join([]string{"l", strings.Join(result, ""), "e"}, "")
}
// EncodeDict encodes a dict value.
func EncodeDict(data map[string]interface{}) string {
result, i := make([]string, len(data)), 0
for key, val := range data {
result[i] = strings.Join(
[]string{EncodeString(key), encodeItem(val)},
"")
i++
}
return strings.Join([]string{"d", strings.Join(result, ""), "e"}, "")
}
// Encode encodes a string, int, dict or list value to a bencoded string.
func Encode(data interface{}) string {
switch v := data.(type) {
case string:
return EncodeString(v)
case int:
return EncodeInt(v)
case []interface{}:
return EncodeList(v)
case map[string]interface{}:
return EncodeDict(v)
default:
panic("invalid type when encode")
}
}