-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbencode.go
More file actions
96 lines (87 loc) · 1.95 KB
/
Copy pathbencode.go
File metadata and controls
96 lines (87 loc) · 1.95 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
package btgo
import (
"fmt"
"math/big"
"reflect"
"sort"
"strings"
)
func Bencode(t interface{}) (bencoded string) {
switch k := reflect.TypeOf(t).Kind(); k {
default:
panic(fmt.Sprintf("unexpected type passed to Bencode: %T", t))
case reflect.String:
if st, ok := t.(string); ok {
bencoded = bencodeString(st)
}
case reflect.Int:
if st, ok := t.(int); ok {
bencoded = bencodeInt(st)
}
case reflect.Slice:
if stringBytes, ok := t.([]byte); ok {
bencoded = bencodeString(string(stringBytes))
} else {
v := reflect.ValueOf(t)
st := make([]interface{}, v.Len())
for i := 0; i < v.Len(); i++ {
st[i] = interface{}(v.Index(i).Interface())
}
bencoded = bencodeSlice(st)
}
case reflect.Map:
v := reflect.ValueOf(t)
st := make(map[string]interface{})
keys := v.MapKeys()
for _, key := range keys {
st[key.String()] = v.MapIndex(key).Interface()
}
bencoded = bencodeMap(st)
case reflect.Ptr:
b, ok := t.(*big.Int)
if !ok {
panic(fmt.Sprintf("unexpected pointer passed to Bencode: %T", t))
}
bencoded = bencodeBigInt(b)
}
return
}
func bencodeString(s string) string {
return fmt.Sprintf("%d:%s", len(s), s)
}
func bencodeInt(i int) string {
return fmt.Sprintf("i%de", i)
}
func bencodeBigInt(i *big.Int) string {
return fmt.Sprintf("i%se", i)
}
func bencodeSlice(s []interface{}) string {
parts := make([]string, len(s))
for i, e := range s {
parts[i] = Bencode(e)
}
joined := strings.Join(parts, "")
return fmt.Sprintf("l%se", joined)
}
func bencodeMap(m map[string]interface{}) string {
parts := make([]string, len(m)*2)
keys := keysInMap(m)
sort.Strings(keys)
i := 0
for _, key := range keys {
parts[i] = Bencode(key)
parts[i+1] = Bencode(m[key])
i += 2
}
joined := strings.Join(parts, "")
return fmt.Sprintf("d%se", joined)
}
func keysInMap(m map[string]interface{}) []string {
keys := make([]string, len(m))
i := 0
for key, _ := range m {
keys[i] = key
i++
}
return keys
}