2012-06-20 13:21:32 +00:00
|
|
|
package bencode
|
|
|
|
|
2015-10-18 14:25:56 +00:00
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"reflect"
|
|
|
|
"runtime"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
)
|
2012-06-20 13:21:32 +00:00
|
|
|
|
|
|
|
func is_empty_value(v reflect.Value) bool {
|
|
|
|
switch v.Kind() {
|
|
|
|
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
|
|
|
|
return v.Len() == 0
|
|
|
|
case reflect.Bool:
|
|
|
|
return !v.Bool()
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
return v.Int() == 0
|
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
|
|
|
return v.Uint() == 0
|
|
|
|
case reflect.Float32, reflect.Float64:
|
|
|
|
return v.Float() == 0
|
|
|
|
case reflect.Interface, reflect.Ptr:
|
|
|
|
return v.IsNil()
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
type encoder struct {
|
|
|
|
*bufio.Writer
|
|
|
|
scratch [64]byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *encoder) encode(v interface{}) (err error) {
|
2015-10-23 01:35:38 +00:00
|
|
|
if v == nil {
|
|
|
|
return
|
|
|
|
}
|
2012-06-20 13:21:32 +00:00
|
|
|
defer func() {
|
|
|
|
if e := recover(); e != nil {
|
|
|
|
if _, ok := e.(runtime.Error); ok {
|
|
|
|
panic(e)
|
|
|
|
}
|
2015-10-23 01:36:47 +00:00
|
|
|
var ok bool
|
|
|
|
err, ok = e.(error)
|
|
|
|
if !ok {
|
|
|
|
panic(e)
|
|
|
|
}
|
2012-06-20 13:21:32 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
e.reflect_value(reflect.ValueOf(v))
|
2012-06-24 10:59:04 +00:00
|
|
|
return e.Flush()
|
2012-06-20 13:21:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type string_values []reflect.Value
|
|
|
|
|
|
|
|
func (sv string_values) Len() int { return len(sv) }
|
|
|
|
func (sv string_values) Swap(i, j int) { sv[i], sv[j] = sv[j], sv[i] }
|
|
|
|
func (sv string_values) Less(i, j int) bool { return sv.get(i) < sv.get(j) }
|
|
|
|
func (sv string_values) get(i int) string { return sv[i].String() }
|
|
|
|
|
2012-06-24 10:59:04 +00:00
|
|
|
func (e *encoder) write(s []byte) {
|
|
|
|
_, err := e.Write(s)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *encoder) write_string(s string) {
|
|
|
|
_, err := e.WriteString(s)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-20 13:21:32 +00:00
|
|
|
func (e *encoder) reflect_string(s string) {
|
|
|
|
b := strconv.AppendInt(e.scratch[:0], int64(len(s)), 10)
|
2012-06-24 10:59:04 +00:00
|
|
|
e.write(b)
|
|
|
|
e.write_string(":")
|
|
|
|
e.write_string(s)
|
2012-06-20 13:21:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *encoder) reflect_byte_slice(s []byte) {
|
|
|
|
b := strconv.AppendInt(e.scratch[:0], int64(len(s)), 10)
|
2012-06-24 10:59:04 +00:00
|
|
|
e.write(b)
|
|
|
|
e.write_string(":")
|
|
|
|
e.write(s)
|
2012-06-20 13:21:32 +00:00
|
|
|
}
|
|
|
|
|
2012-06-27 20:21:26 +00:00
|
|
|
// returns true if the value implements Marshaler interface and marshaling was
|
|
|
|
// done successfully
|
|
|
|
func (e *encoder) reflect_marshaler(v reflect.Value) bool {
|
2012-06-24 10:59:04 +00:00
|
|
|
m, ok := v.Interface().(Marshaler)
|
|
|
|
if !ok {
|
2012-06-27 20:21:26 +00:00
|
|
|
// T doesn't work, try *T
|
2012-06-24 10:59:04 +00:00
|
|
|
if v.Kind() != reflect.Ptr && v.CanAddr() {
|
|
|
|
m, ok = v.Addr().Interface().(Marshaler)
|
|
|
|
if ok {
|
|
|
|
v = v.Addr()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ok && (v.Kind() != reflect.Ptr || !v.IsNil()) {
|
|
|
|
data, err := m.MarshalBencode()
|
|
|
|
if err != nil {
|
|
|
|
panic(&MarshalerError{v.Type(), err})
|
|
|
|
}
|
|
|
|
e.write(data)
|
2012-06-27 20:21:26 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *encoder) reflect_value(v reflect.Value) {
|
|
|
|
|
|
|
|
if e.reflect_marshaler(v) {
|
2012-06-24 11:10:53 +00:00
|
|
|
return
|
2012-06-24 10:59:04 +00:00
|
|
|
}
|
|
|
|
|
2012-06-20 13:21:32 +00:00
|
|
|
switch v.Kind() {
|
|
|
|
case reflect.Bool:
|
|
|
|
if v.Bool() {
|
2012-06-24 10:59:04 +00:00
|
|
|
e.write_string("i1e")
|
2012-06-20 13:21:32 +00:00
|
|
|
} else {
|
2012-06-24 10:59:04 +00:00
|
|
|
e.write_string("i0e")
|
2012-06-20 13:21:32 +00:00
|
|
|
}
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
b := strconv.AppendInt(e.scratch[:0], v.Int(), 10)
|
2012-06-24 10:59:04 +00:00
|
|
|
e.write_string("i")
|
|
|
|
e.write(b)
|
|
|
|
e.write_string("e")
|
2012-06-20 13:21:32 +00:00
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
|
|
b := strconv.AppendUint(e.scratch[:0], v.Uint(), 10)
|
2012-06-24 10:59:04 +00:00
|
|
|
e.write_string("i")
|
|
|
|
e.write(b)
|
|
|
|
e.write_string("e")
|
2012-06-20 13:21:32 +00:00
|
|
|
case reflect.String:
|
|
|
|
e.reflect_string(v.String())
|
|
|
|
case reflect.Struct:
|
2012-06-24 10:59:04 +00:00
|
|
|
e.write_string("d")
|
2012-06-20 13:21:32 +00:00
|
|
|
for _, ef := range encode_fields(v.Type()) {
|
|
|
|
field_value := v.Field(ef.i)
|
|
|
|
if ef.omit_empty && is_empty_value(field_value) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
e.reflect_string(ef.tag)
|
|
|
|
e.reflect_value(field_value)
|
|
|
|
}
|
2012-06-24 10:59:04 +00:00
|
|
|
e.write_string("e")
|
2012-06-20 13:21:32 +00:00
|
|
|
case reflect.Map:
|
|
|
|
if v.Type().Key().Kind() != reflect.String {
|
|
|
|
panic(&MarshalTypeError{v.Type()})
|
|
|
|
}
|
|
|
|
if v.IsNil() {
|
2012-06-24 10:59:04 +00:00
|
|
|
e.write_string("de")
|
2012-06-20 13:21:32 +00:00
|
|
|
break
|
|
|
|
}
|
2012-06-24 10:59:04 +00:00
|
|
|
e.write_string("d")
|
2012-06-20 13:21:32 +00:00
|
|
|
sv := string_values(v.MapKeys())
|
|
|
|
sort.Sort(sv)
|
|
|
|
for _, key := range sv {
|
|
|
|
e.reflect_string(key.String())
|
|
|
|
e.reflect_value(v.MapIndex(key))
|
|
|
|
}
|
2012-06-24 10:59:04 +00:00
|
|
|
e.write_string("e")
|
2012-06-20 13:21:32 +00:00
|
|
|
case reflect.Slice:
|
|
|
|
if v.IsNil() {
|
2012-06-24 10:59:04 +00:00
|
|
|
e.write_string("le")
|
2012-06-20 13:21:32 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
if v.Type().Elem().Kind() == reflect.Uint8 {
|
|
|
|
s := v.Bytes()
|
|
|
|
e.reflect_byte_slice(s)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
fallthrough
|
|
|
|
case reflect.Array:
|
2012-06-24 10:59:04 +00:00
|
|
|
e.write_string("l")
|
2012-06-20 13:21:32 +00:00
|
|
|
for i, n := 0, v.Len(); i < n; i++ {
|
|
|
|
e.reflect_value(v.Index(i))
|
|
|
|
}
|
2012-06-24 10:59:04 +00:00
|
|
|
e.write_string("e")
|
2015-10-23 01:37:30 +00:00
|
|
|
case reflect.Interface:
|
|
|
|
e.reflect_value(v.Elem())
|
|
|
|
case reflect.Ptr:
|
2012-06-20 13:21:32 +00:00
|
|
|
if v.IsNil() {
|
2015-10-18 14:28:36 +00:00
|
|
|
v = reflect.Zero(v.Type().Elem())
|
|
|
|
} else {
|
|
|
|
v = v.Elem()
|
2012-06-20 13:21:32 +00:00
|
|
|
}
|
2015-10-18 14:28:36 +00:00
|
|
|
e.reflect_value(v)
|
2012-06-20 13:21:32 +00:00
|
|
|
default:
|
|
|
|
panic(&MarshalTypeError{v.Type()})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type encode_field struct {
|
|
|
|
i int
|
|
|
|
tag string
|
|
|
|
omit_empty bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type encode_fields_sort_type []encode_field
|
|
|
|
|
|
|
|
func (ef encode_fields_sort_type) Len() int { return len(ef) }
|
|
|
|
func (ef encode_fields_sort_type) Swap(i, j int) { ef[i], ef[j] = ef[j], ef[i] }
|
|
|
|
func (ef encode_fields_sort_type) Less(i, j int) bool { return ef[i].tag < ef[j].tag }
|
|
|
|
|
|
|
|
var (
|
|
|
|
type_cache_lock sync.RWMutex
|
|
|
|
encode_fields_cache = make(map[reflect.Type][]encode_field)
|
|
|
|
)
|
|
|
|
|
|
|
|
func encode_fields(t reflect.Type) []encode_field {
|
|
|
|
type_cache_lock.RLock()
|
|
|
|
fs, ok := encode_fields_cache[t]
|
|
|
|
type_cache_lock.RUnlock()
|
|
|
|
if ok {
|
|
|
|
return fs
|
|
|
|
}
|
|
|
|
|
|
|
|
type_cache_lock.Lock()
|
|
|
|
defer type_cache_lock.Unlock()
|
|
|
|
fs, ok = encode_fields_cache[t]
|
|
|
|
if ok {
|
|
|
|
return fs
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, n := 0, t.NumField(); i < n; i++ {
|
|
|
|
f := t.Field(i)
|
|
|
|
if f.PkgPath != "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if f.Anonymous {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var ef encode_field
|
|
|
|
ef.i = i
|
|
|
|
ef.tag = f.Name
|
|
|
|
|
|
|
|
tv := f.Tag.Get("bencode")
|
|
|
|
if tv != "" {
|
|
|
|
if tv == "-" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
name, opts := parse_tag(tv)
|
2015-10-18 14:27:15 +00:00
|
|
|
if name != "" {
|
|
|
|
ef.tag = name
|
|
|
|
}
|
2012-06-20 13:21:32 +00:00
|
|
|
ef.omit_empty = opts.contains("omitempty")
|
|
|
|
}
|
|
|
|
fs = append(fs, ef)
|
|
|
|
}
|
|
|
|
fss := encode_fields_sort_type(fs)
|
|
|
|
sort.Sort(fss)
|
|
|
|
encode_fields_cache[t] = fs
|
|
|
|
return fs
|
|
|
|
}
|