2012-06-20 21:21:32 +08:00
|
|
|
package bencode
|
|
|
|
|
2015-10-18 22:25:56 +08:00
|
|
|
import (
|
2016-06-15 13:09:34 +08:00
|
|
|
"io"
|
2017-11-08 18:34:24 +08:00
|
|
|
"math/big"
|
2015-10-18 22:25:56 +08:00
|
|
|
"reflect"
|
|
|
|
"runtime"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
2015-12-07 00:28:28 +08:00
|
|
|
|
|
|
|
"github.com/anacrolix/missinggo"
|
2015-10-18 22:25:56 +08:00
|
|
|
)
|
2012-06-20 21:21:32 +08:00
|
|
|
|
2016-08-26 11:13:30 +08:00
|
|
|
func isEmptyValue(v reflect.Value) bool {
|
2015-12-07 00:28:28 +08:00
|
|
|
return missinggo.IsEmptyValue(v)
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
|
|
|
|
2016-06-15 13:00:51 +08:00
|
|
|
type Encoder struct {
|
2018-07-23 08:50:18 +08:00
|
|
|
w io.Writer
|
2012-06-20 21:21:32 +08:00
|
|
|
scratch [64]byte
|
|
|
|
}
|
|
|
|
|
2016-06-15 13:00:51 +08:00
|
|
|
func (e *Encoder) Encode(v interface{}) (err error) {
|
2015-10-23 09:35:38 +08:00
|
|
|
if v == nil {
|
|
|
|
return
|
|
|
|
}
|
2012-06-20 21:21:32 +08:00
|
|
|
defer func() {
|
|
|
|
if e := recover(); e != nil {
|
|
|
|
if _, ok := e.(runtime.Error); ok {
|
|
|
|
panic(e)
|
|
|
|
}
|
2015-10-23 09:36:47 +08:00
|
|
|
var ok bool
|
|
|
|
err, ok = e.(error)
|
|
|
|
if !ok {
|
|
|
|
panic(e)
|
|
|
|
}
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
|
|
|
}()
|
2016-08-26 11:13:30 +08:00
|
|
|
e.reflectValue(reflect.ValueOf(v))
|
2018-07-23 08:50:18 +08:00
|
|
|
return nil
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
|
|
|
|
2019-12-23 10:39:34 +08:00
|
|
|
type stringValues []reflect.Value
|
2012-06-20 21:21:32 +08:00
|
|
|
|
2019-12-23 10:39:34 +08:00
|
|
|
func (sv stringValues) Len() int { return len(sv) }
|
|
|
|
func (sv stringValues) Swap(i, j int) { sv[i], sv[j] = sv[j], sv[i] }
|
|
|
|
func (sv stringValues) Less(i, j int) bool { return sv.get(i) < sv.get(j) }
|
|
|
|
func (sv stringValues) get(i int) string { return sv[i].String() }
|
2012-06-20 21:21:32 +08:00
|
|
|
|
2016-06-15 13:00:51 +08:00
|
|
|
func (e *Encoder) write(s []byte) {
|
2016-06-15 13:09:34 +08:00
|
|
|
_, err := e.w.Write(s)
|
2012-06-24 18:59:04 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-26 11:13:30 +08:00
|
|
|
func (e *Encoder) writeString(s string) {
|
2018-07-23 08:50:18 +08:00
|
|
|
for s != "" {
|
|
|
|
n := copy(e.scratch[:], s)
|
|
|
|
s = s[n:]
|
|
|
|
e.write(e.scratch[:n])
|
2012-06-24 18:59:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-26 11:13:30 +08:00
|
|
|
func (e *Encoder) reflectString(s string) {
|
2012-06-20 21:21:32 +08:00
|
|
|
b := strconv.AppendInt(e.scratch[:0], int64(len(s)), 10)
|
2012-06-24 18:59:04 +08:00
|
|
|
e.write(b)
|
2016-08-26 11:13:30 +08:00
|
|
|
e.writeString(":")
|
|
|
|
e.writeString(s)
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
|
|
|
|
2016-08-26 11:13:30 +08:00
|
|
|
func (e *Encoder) reflectByteSlice(s []byte) {
|
2012-06-20 21:21:32 +08:00
|
|
|
b := strconv.AppendInt(e.scratch[:0], int64(len(s)), 10)
|
2012-06-24 18:59:04 +08:00
|
|
|
e.write(b)
|
2016-08-26 11:13:30 +08:00
|
|
|
e.writeString(":")
|
2012-06-24 18:59:04 +08:00
|
|
|
e.write(s)
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
|
|
|
|
2018-07-23 11:12:14 +08:00
|
|
|
// Returns true if the value implements Marshaler interface and marshaling was
|
|
|
|
// done successfully.
|
2016-08-26 11:13:30 +08:00
|
|
|
func (e *Encoder) reflectMarshaler(v reflect.Value) bool {
|
2018-07-23 11:12:14 +08:00
|
|
|
if !v.Type().Implements(marshalerType) {
|
|
|
|
if v.Kind() != reflect.Ptr && v.CanAddr() && v.Addr().Type().Implements(marshalerType) {
|
|
|
|
v = v.Addr()
|
|
|
|
} else {
|
|
|
|
return false
|
2012-06-24 18:59:04 +08:00
|
|
|
}
|
|
|
|
}
|
2018-07-23 11:12:14 +08:00
|
|
|
m := v.Interface().(Marshaler)
|
|
|
|
data, err := m.MarshalBencode()
|
|
|
|
if err != nil {
|
|
|
|
panic(&MarshalerError{v.Type(), err})
|
2012-06-28 04:21:26 +08:00
|
|
|
}
|
2018-07-23 11:12:14 +08:00
|
|
|
e.write(data)
|
|
|
|
return true
|
2012-06-28 04:21:26 +08:00
|
|
|
}
|
|
|
|
|
2021-09-18 10:44:47 +08:00
|
|
|
var bigIntType = reflect.TypeOf((*big.Int)(nil)).Elem()
|
2018-07-17 19:25:15 +08:00
|
|
|
|
2016-08-26 11:13:30 +08:00
|
|
|
func (e *Encoder) reflectValue(v reflect.Value) {
|
2012-06-28 04:21:26 +08:00
|
|
|
|
2016-08-26 11:13:30 +08:00
|
|
|
if e.reflectMarshaler(v) {
|
2012-06-24 19:10:53 +08:00
|
|
|
return
|
2012-06-24 18:59:04 +08:00
|
|
|
}
|
|
|
|
|
2018-07-17 19:25:15 +08:00
|
|
|
if v.Type() == bigIntType {
|
2017-11-08 18:34:24 +08:00
|
|
|
e.writeString("i")
|
2018-07-17 19:25:15 +08:00
|
|
|
bi := v.Interface().(big.Int)
|
|
|
|
e.writeString(bi.String())
|
2017-11-08 18:34:24 +08:00
|
|
|
e.writeString("e")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-06-20 21:21:32 +08:00
|
|
|
switch v.Kind() {
|
|
|
|
case reflect.Bool:
|
|
|
|
if v.Bool() {
|
2016-08-26 11:13:30 +08:00
|
|
|
e.writeString("i1e")
|
2012-06-20 21:21:32 +08:00
|
|
|
} else {
|
2016-08-26 11:13:30 +08:00
|
|
|
e.writeString("i0e")
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
2016-08-26 11:13:30 +08:00
|
|
|
e.writeString("i")
|
2018-07-23 08:50:18 +08:00
|
|
|
b := strconv.AppendInt(e.scratch[:0], v.Int(), 10)
|
2012-06-24 18:59:04 +08:00
|
|
|
e.write(b)
|
2016-08-26 11:13:30 +08:00
|
|
|
e.writeString("e")
|
2012-06-20 21:21:32 +08:00
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
2016-08-26 11:13:30 +08:00
|
|
|
e.writeString("i")
|
2018-07-23 08:50:18 +08:00
|
|
|
b := strconv.AppendUint(e.scratch[:0], v.Uint(), 10)
|
2012-06-24 18:59:04 +08:00
|
|
|
e.write(b)
|
2016-08-26 11:13:30 +08:00
|
|
|
e.writeString("e")
|
2012-06-20 21:21:32 +08:00
|
|
|
case reflect.String:
|
2016-08-26 11:13:30 +08:00
|
|
|
e.reflectString(v.String())
|
2012-06-20 21:21:32 +08:00
|
|
|
case reflect.Struct:
|
2016-08-26 11:13:30 +08:00
|
|
|
e.writeString("d")
|
2021-05-22 09:02:39 +08:00
|
|
|
for _, ef := range getEncodeFields(v.Type()) {
|
|
|
|
fieldValue := ef.i(v)
|
|
|
|
if !fieldValue.IsValid() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if ef.omitEmpty && isEmptyValue(fieldValue) {
|
2012-06-20 21:21:32 +08:00
|
|
|
continue
|
|
|
|
}
|
2016-08-26 11:13:30 +08:00
|
|
|
e.reflectString(ef.tag)
|
2021-05-22 09:02:39 +08:00
|
|
|
e.reflectValue(fieldValue)
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
2016-08-26 11:13:30 +08:00
|
|
|
e.writeString("e")
|
2012-06-20 21:21:32 +08:00
|
|
|
case reflect.Map:
|
|
|
|
if v.Type().Key().Kind() != reflect.String {
|
|
|
|
panic(&MarshalTypeError{v.Type()})
|
|
|
|
}
|
|
|
|
if v.IsNil() {
|
2016-08-26 11:13:30 +08:00
|
|
|
e.writeString("de")
|
2012-06-20 21:21:32 +08:00
|
|
|
break
|
|
|
|
}
|
2016-08-26 11:13:30 +08:00
|
|
|
e.writeString("d")
|
2019-12-23 10:39:34 +08:00
|
|
|
sv := stringValues(v.MapKeys())
|
2012-06-20 21:21:32 +08:00
|
|
|
sort.Sort(sv)
|
|
|
|
for _, key := range sv {
|
2016-08-26 11:13:30 +08:00
|
|
|
e.reflectString(key.String())
|
|
|
|
e.reflectValue(v.MapIndex(key))
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
2016-08-26 11:13:30 +08:00
|
|
|
e.writeString("e")
|
2012-06-20 21:21:32 +08:00
|
|
|
case reflect.Slice:
|
|
|
|
if v.Type().Elem().Kind() == reflect.Uint8 {
|
|
|
|
s := v.Bytes()
|
2016-08-26 11:13:30 +08:00
|
|
|
e.reflectByteSlice(s)
|
2012-06-20 21:21:32 +08:00
|
|
|
break
|
|
|
|
}
|
2018-11-27 05:57:17 +08:00
|
|
|
if v.IsNil() {
|
|
|
|
e.writeString("le")
|
|
|
|
break
|
|
|
|
}
|
2012-06-20 21:21:32 +08:00
|
|
|
fallthrough
|
|
|
|
case reflect.Array:
|
2016-08-26 11:13:30 +08:00
|
|
|
e.writeString("l")
|
2012-06-20 21:21:32 +08:00
|
|
|
for i, n := 0, v.Len(); i < n; i++ {
|
2016-08-26 11:13:30 +08:00
|
|
|
e.reflectValue(v.Index(i))
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
2016-08-26 11:13:30 +08:00
|
|
|
e.writeString("e")
|
2015-10-23 09:37:30 +08:00
|
|
|
case reflect.Interface:
|
2016-08-26 11:13:30 +08:00
|
|
|
e.reflectValue(v.Elem())
|
2015-10-23 09:37:30 +08:00
|
|
|
case reflect.Ptr:
|
2012-06-20 21:21:32 +08:00
|
|
|
if v.IsNil() {
|
2015-10-18 22:28:36 +08:00
|
|
|
v = reflect.Zero(v.Type().Elem())
|
|
|
|
} else {
|
|
|
|
v = v.Elem()
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
2016-08-26 11:13:30 +08:00
|
|
|
e.reflectValue(v)
|
2012-06-20 21:21:32 +08:00
|
|
|
default:
|
|
|
|
panic(&MarshalTypeError{v.Type()})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-26 11:13:30 +08:00
|
|
|
type encodeField struct {
|
2021-05-22 09:02:39 +08:00
|
|
|
i func(v reflect.Value) reflect.Value
|
|
|
|
tag string
|
|
|
|
omitEmpty bool
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
|
|
|
|
2016-08-26 11:13:30 +08:00
|
|
|
type encodeFieldsSortType []encodeField
|
2012-06-20 21:21:32 +08:00
|
|
|
|
2016-08-26 11:13:30 +08:00
|
|
|
func (ef encodeFieldsSortType) Len() int { return len(ef) }
|
|
|
|
func (ef encodeFieldsSortType) Swap(i, j int) { ef[i], ef[j] = ef[j], ef[i] }
|
|
|
|
func (ef encodeFieldsSortType) Less(i, j int) bool { return ef[i].tag < ef[j].tag }
|
2012-06-20 21:21:32 +08:00
|
|
|
|
|
|
|
var (
|
2016-08-26 11:13:30 +08:00
|
|
|
typeCacheLock sync.RWMutex
|
|
|
|
encodeFieldsCache = make(map[reflect.Type][]encodeField)
|
2012-06-20 21:21:32 +08:00
|
|
|
)
|
|
|
|
|
2021-05-22 09:02:39 +08:00
|
|
|
func getEncodeFields(t reflect.Type) []encodeField {
|
2016-08-26 11:13:30 +08:00
|
|
|
typeCacheLock.RLock()
|
|
|
|
fs, ok := encodeFieldsCache[t]
|
|
|
|
typeCacheLock.RUnlock()
|
2012-06-20 21:21:32 +08:00
|
|
|
if ok {
|
|
|
|
return fs
|
|
|
|
}
|
2021-05-22 09:02:39 +08:00
|
|
|
fs = makeEncodeFields(t)
|
2016-08-26 11:13:30 +08:00
|
|
|
typeCacheLock.Lock()
|
|
|
|
defer typeCacheLock.Unlock()
|
2021-05-22 09:02:39 +08:00
|
|
|
encodeFieldsCache[t] = fs
|
|
|
|
return fs
|
|
|
|
}
|
2012-06-20 21:21:32 +08:00
|
|
|
|
2021-05-22 09:02:39 +08:00
|
|
|
func makeEncodeFields(t reflect.Type) (fs []encodeField) {
|
|
|
|
for _i, n := 0, t.NumField(); _i < n; _i++ {
|
|
|
|
i := _i
|
2012-06-20 21:21:32 +08:00
|
|
|
f := t.Field(i)
|
|
|
|
if f.PkgPath != "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if f.Anonymous {
|
2021-05-24 15:31:52 +08:00
|
|
|
t := f.Type
|
|
|
|
if t.Kind() == reflect.Ptr {
|
|
|
|
t = t.Elem()
|
|
|
|
}
|
|
|
|
anonEFs := makeEncodeFields(t)
|
2021-05-22 09:02:39 +08:00
|
|
|
for aefi := range anonEFs {
|
|
|
|
anonEF := anonEFs[aefi]
|
|
|
|
bottomField := anonEF
|
|
|
|
bottomField.i = func(v reflect.Value) reflect.Value {
|
|
|
|
v = v.Field(i)
|
2021-05-24 15:31:52 +08:00
|
|
|
if v.Kind() == reflect.Ptr {
|
|
|
|
if v.IsNil() {
|
|
|
|
// This will skip serializing this value.
|
|
|
|
return reflect.Value{}
|
|
|
|
}
|
|
|
|
v = v.Elem()
|
2021-05-22 09:02:39 +08:00
|
|
|
}
|
2021-05-24 15:31:52 +08:00
|
|
|
return anonEF.i(v)
|
2021-05-22 09:02:39 +08:00
|
|
|
}
|
|
|
|
fs = append(fs, bottomField)
|
|
|
|
}
|
2012-06-20 21:21:32 +08:00
|
|
|
continue
|
|
|
|
}
|
2016-08-26 11:13:30 +08:00
|
|
|
var ef encodeField
|
2021-05-22 09:02:39 +08:00
|
|
|
ef.i = func(v reflect.Value) reflect.Value {
|
|
|
|
return v.Field(i)
|
|
|
|
}
|
2012-06-20 21:21:32 +08:00
|
|
|
ef.tag = f.Name
|
|
|
|
|
2018-01-27 11:31:12 +08:00
|
|
|
tv := getTag(f.Tag)
|
|
|
|
if tv.Ignore() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if tv.Key() != "" {
|
|
|
|
ef.tag = tv.Key()
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
2021-05-22 09:02:39 +08:00
|
|
|
ef.omitEmpty = tv.OmitEmpty()
|
2012-06-20 21:21:32 +08:00
|
|
|
fs = append(fs, ef)
|
|
|
|
}
|
2016-08-26 11:13:30 +08:00
|
|
|
fss := encodeFieldsSortType(fs)
|
2012-06-20 21:21:32 +08:00
|
|
|
sort.Sort(fss)
|
|
|
|
return fs
|
|
|
|
}
|