forked from zeebo/bencode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode.go
More file actions
183 lines (157 loc) · 4.39 KB
/
encode.go
File metadata and controls
183 lines (157 loc) · 4.39 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
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
package bencode
import (
"bytes"
"fmt"
"io"
"reflect"
"sort"
)
type sortValues []reflect.Value
func (p sortValues) Len() int { return len(p) }
func (p sortValues) Less(i, j int) bool { return p[i].String() < p[j].String() }
func (p sortValues) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
type sortFields []reflect.StructField
func (p sortFields) Len() int { return len(p) }
func (p sortFields) Less(i, j int) bool {
iName, jName := p[i].Name, p[j].Name
if name, _ := parseTag(p[i].Tag.Get("bencode")); name != "" {
iName = name
}
if name, _ := parseTag(p[j].Tag.Get("bencode")); name != "" {
jName = name
}
return iName < jName
}
func (p sortFields) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
//An Encoder writes bencoded objects to an output stream.
type Encoder struct {
w io.Writer
}
//NewEncoder returns a new encoder that writes to w.
func NewEncoder(w io.Writer) *Encoder {
return &Encoder{w}
}
//Encode writes the bencoded data of val to its output stream.
//See the documentation for Decode about the conversion of Go values to
//bencoded data.
func (e *Encoder) Encode(val interface{}) error {
return encodeValue(e.w, reflect.ValueOf(val))
}
//EncodeString returns the bencoded data of val as a string.
func EncodeString(val interface{}) (string, error) {
buf := new(bytes.Buffer)
e := NewEncoder(buf)
if err := e.Encode(val); err != nil {
return "", err
}
return buf.String(), nil
}
func encodeValue(w io.Writer, val reflect.Value) error {
//inspect the val to check
v := indirect(val)
//send in a raw message if we have that type
if rm, ok := v.Interface().(RawMessage); ok {
_, err := io.Copy(w, bytes.NewReader(rm))
return err
}
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
_, err := fmt.Fprintf(w, "i%de", v.Int())
return err
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
_, err := fmt.Fprintf(w, "i%de", v.Uint())
return err
case reflect.String:
_, err := fmt.Fprintf(w, "%d:%s", len(v.String()), v.String())
return err
case reflect.Slice, reflect.Array:
if _, err := fmt.Fprint(w, "l"); err != nil {
return err
}
for i := 0; i < v.Len(); i++ {
if err := encodeValue(w, v.Index(i)); err != nil {
return err
}
}
_, err := fmt.Fprint(w, "e")
return err
case reflect.Map:
if _, err := fmt.Fprint(w, "d"); err != nil {
return err
}
var (
keys sortValues = v.MapKeys()
mval reflect.Value
)
sort.Sort(keys)
for i := range keys {
if err := encodeValue(w, keys[i]); err != nil {
return err
}
mval = v.MapIndex(keys[i])
if err := encodeValue(w, mval); err != nil {
return err
}
}
_, err := fmt.Fprint(w, "e")
return err
case reflect.Struct:
t := v.Type()
if _, err := fmt.Fprint(w, "d"); err != nil {
return err
}
//put keys into keys
var (
keys = make(sortFields, t.NumField())
fieldValue reflect.Value
rkey reflect.Value
)
for i := range keys {
keys[i] = t.Field(i)
}
sort.Sort(keys)
for _, key := range keys {
rkey = reflect.ValueOf(key.Name)
fieldValue = v.FieldByIndex(key.Index)
/* Tags
* Near identical to usage in JSON except with key 'bencode'
* Struct values encode as BEncode dictionaries. Each exported
struct field becomes a set in the dictionary unless
- the field's tag is "-", or
- the field is empty and its tag specifies the "omitempty"
option.
* The default key string is the struct field name but can be
specified in the struct field's tag value. The "bencode"
key in struct field's tag value is the key name, followed
by an optional comma and options.
*/
tagValue := key.Tag.Get("bencode")
if tagValue != "" {
// Keys with '-' are omit from output
if tagValue == "-" {
continue
}
name, options := parseTag(tagValue)
// Keys with 'omitempty' are omitted if the field is empty
if options.Contains("omitempty") && isEmptyValue(fieldValue) {
continue
}
// All other values are treated as the key string
if isValidTag(name) {
rkey = reflect.ValueOf(name)
}
}
//encode the key
if err := encodeValue(w, rkey); err != nil {
return err
}
//encode the value
if err := encodeValue(w, fieldValue); err != nil {
return err
}
}
_, err := fmt.Fprint(w, "e")
return err
}
return fmt.Errorf("Can't encode type: %s", v.Type())
}