-
Notifications
You must be signed in to change notification settings - Fork 1
/
encode.go
377 lines (319 loc) · 7.43 KB
/
encode.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package bencode
import (
"errors"
"fmt"
"io"
"math"
"reflect"
"sort"
"strconv"
)
// An Encoder writes Bencode values to an output stream.
type Encoder struct {
w io.Writer
buf []byte
}
// NewEncoder returns a new encoder that writes to w.
func NewEncoder(w io.Writer) *Encoder {
return NewEncoderWithBuffer(w, make([]byte, 0, 512))
}
// NewEncoderWithBuffer returns a new encoder that writes to w.
func NewEncoderWithBuffer(w io.Writer, buf []byte) *Encoder {
return &Encoder{
w: w,
buf: buf,
}
}
// Encode writes the Bencode encoding of v to the stream.
func (e *Encoder) Encode(v any) error {
e.buf = e.buf[:0]
if err := e.marshal(v); err != nil {
return fmt.Errorf("bencode: encode failed: %w", err)
}
_, err := e.w.Write(e.buf)
return err
}
func (e *Encoder) marshal(v any) error {
switch v := v.(type) {
case []byte:
e.marshalBytes(v)
case string:
e.marshalString(v)
case M:
return e.marshalDictionary(v)
case D:
return e.marshalDictionaryNew(v)
case A:
return e.marshalSlice(v)
case map[string]any:
return e.marshalDictionary(v)
case []any:
return e.marshalSlice(v)
case int, int8, int16, int32, int64:
e.marshalIntGen(v)
case uint, uint8, uint16, uint32, uint64:
e.marshalIntGen(v)
case float32:
e.marshalInt(int64(math.Float32bits(v)))
case float64:
e.marshalInt(int64(math.Float64bits(v)))
case bool:
var n int64
if v {
n = 1
}
e.marshalInt(n)
case Marshaler:
raw, err := v.MarshalBencode()
if err != nil {
return err
}
e.buf = append(e.buf, raw...)
default:
return e.marshalReflect(reflect.ValueOf(v))
}
return nil
}
func (e *Encoder) writeInt(n int64) {
var bs [20]byte // max_str_len( math.MaxInt64, math.MinInt64 ) base 10
buf := strconv.AppendInt(bs[0:0], n, 10)
e.buf = append(e.buf, buf...)
}
func (e *Encoder) marshalBytes(b []byte) {
// manual inline of writeInt
var bs [20]byte // max_str_len( math.MaxInt64, math.MinInt64 ) base 10
buf := strconv.AppendInt(bs[0:0], int64(len(b)), 10)
buf = append(buf, ':')
e.buf = append(e.buf, buf...)
e.buf = append(e.buf, b...)
}
func (e *Encoder) marshalString(s string) {
// manual inline of writeInt
var bs [20]byte // max_str_len( math.MaxInt64, math.MinInt64 ) base 10
buf := strconv.AppendInt(bs[0:0], int64(len(s)), 10)
buf = append(buf, ':')
e.buf = append(e.buf, buf...)
e.buf = append(e.buf, s...)
}
func (e *Encoder) marshalIntGen(val any) {
var num int64
switch val := val.(type) {
case int64:
num = val
case int32:
num = int64(val)
case int16:
num = int64(val)
case int8:
num = int64(val)
case int:
num = int64(val)
case uint64:
num = int64(val)
case uint32:
num = int64(val)
case uint16:
num = int64(val)
case uint8:
num = int64(val)
case uint:
num = int64(val)
}
e.marshalInt(num)
}
func (e *Encoder) marshalInt(num int64) {
e.buf = append(e.buf, 'i')
e.writeInt(num)
e.buf = append(e.buf, 'e')
}
func (e *Encoder) marshalReflect(val reflect.Value) error {
switch val.Kind() {
case reflect.Slice:
return e.marshalSliceReflect(val)
case reflect.Array:
return e.marshalArrayReflect(val)
case reflect.Map:
return e.marshalMap(val)
case reflect.Struct:
return e.marshalStruct(val)
case reflect.Ptr, reflect.Interface:
if val.IsNil() {
return nil
}
return e.marshal(val.Elem().Interface())
default:
return fmt.Errorf("unknown kind: %q", val)
}
}
func (e *Encoder) marshalSliceReflect(val reflect.Value) error {
elemKind := val.Type().Elem().Kind()
if elemKind == reflect.Uint8 {
e.marshalBytes(val.Bytes())
return nil
}
return e.marshalList(val)
}
func (e *Encoder) marshalArrayReflect(val reflect.Value) error {
elemKind := val.Type().Elem().Kind()
if elemKind != reflect.Uint8 {
return e.marshalList(val)
}
e.writeInt(int64(val.Len()))
buf := make([]byte, 1+val.Len())
buf[0] = ':'
for i := 1; i <= val.Len(); i++ {
buf[i] = byte(val.Index(i - 1).Uint())
}
e.buf = append(e.buf, buf...)
return nil
}
func (e *Encoder) marshalList(val reflect.Value) error {
if val.Len() == 0 {
e.buf = append(e.buf, "le"...)
return nil
}
e.buf = append(e.buf, 'l')
for i := 0; i < val.Len(); i++ {
if err := e.marshal(val.Index(i).Interface()); err != nil {
return err
}
}
e.buf = append(e.buf, 'e')
return nil
}
func (e *Encoder) marshalMap(val reflect.Value) error {
rawKeys := val.MapKeys()
if len(rawKeys) == 0 {
e.buf = append(e.buf, "de"...)
return nil
}
keys := make([]string, len(rawKeys))
for i, key := range rawKeys {
if key.Kind() != reflect.String {
return errors.New("map can be marshaled only if keys are of type 'string'")
}
keys[i] = key.String()
}
sortStrings(keys)
e.buf = append(e.buf, 'd')
for _, key := range keys {
e.marshalString(key)
value := val.MapIndex(reflect.ValueOf(key))
if err := e.marshal(value.Interface()); err != nil {
return err
}
}
e.buf = append(e.buf, 'e')
return nil
}
func (e *Encoder) marshalStruct(x reflect.Value) error {
dict := make(dictStruct, 0, x.Type().NumField())
dict, err := walkStruct(dict, x)
if err != nil {
return err
}
sort.Sort(dict)
e.buf = append(e.buf, 'd')
for _, def := range dict {
e.marshalString(def.Key)
if err := e.marshal(def.Value.Interface()); err != nil {
return err
}
}
e.buf = append(e.buf, 'e')
return nil
}
type dictStruct []dictPair
type dictPair struct {
Key string
Value reflect.Value
}
func (d dictStruct) Len() int { return len(d) }
func (d dictStruct) Less(i, j int) bool { return d[i].Key < d[j].Key }
func (d dictStruct) Swap(i, j int) { d[i], d[j] = d[j], d[i] }
func walkStruct(dict dictStruct, v reflect.Value) (dictStruct, error) {
t := v.Type()
for i := 0; i < t.NumField(); i++ {
strField := t.Field(i)
field := v.FieldByIndex(strField.Index)
if !field.CanInterface() || isNil(field) {
continue
}
tag, ok := fieldTag(strField, field)
if !ok {
continue
}
if tag == "" && strField.Anonymous &&
strField.Type.Kind() == reflect.Struct {
var err error
dict, err = walkStruct(dict, field)
if err != nil {
return nil, err
}
} else {
dict = append(dict, dictPair{Key: tag, Value: field})
}
}
return dict, nil
}
func (e *Encoder) marshalDictionaryNew(dict D) error {
if len(dict) == 0 {
e.buf = append(e.buf, "de"...)
return nil
}
// TODO(cristaloleg): maybe reuse sort from util.go ?
sort.Slice(dict, func(i, j int) bool {
return dict[i].K < dict[j].K
})
e.buf = append(e.buf, 'd')
for _, pair := range dict {
e.marshalString(pair.K)
if err := e.marshal(pair.V); err != nil {
return err
}
}
e.buf = append(e.buf, 'e')
return nil
}
func (e *Encoder) marshalDictionary(dict map[string]any) error {
if len(dict) == 0 {
e.buf = append(e.buf, "de"...)
return nil
}
// less than `strSliceLen` keys in dict? - take from pool
var keys []string
if len(dict) <= strSliceLen {
strArr := getStrArray()
defer putStrArray(strArr)
keys = strArr[:0:len(dict)]
} else {
keys = make([]string, 0, len(dict))
}
for key := range dict {
keys = append(keys, key)
}
sortStrings(keys)
e.buf = append(e.buf, 'd')
for _, key := range keys {
e.marshalString(key)
if err := e.marshal(dict[key]); err != nil {
return err
}
}
e.buf = append(e.buf, 'e')
return nil
}
func (e *Encoder) marshalSlice(v []any) error {
if len(v) == 0 {
e.buf = append(e.buf, "le"...)
return nil
}
e.buf = append(e.buf, 'l')
for _, data := range v {
if err := e.marshal(data); err != nil {
return err
}
}
e.buf = append(e.buf, 'e')
return nil
}