2012-06-20 13:21:32 +00:00
|
|
|
package bencode
|
|
|
|
|
2014-11-19 04:08:08 +00:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
2015-11-03 12:21:10 +00:00
|
|
|
"fmt"
|
2014-11-19 04:08:08 +00:00
|
|
|
"io"
|
2015-09-20 11:08:42 +00:00
|
|
|
"math/big"
|
2014-11-19 04:08:08 +00:00
|
|
|
"reflect"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
2012-06-20 13:21:32 +00:00
|
|
|
|
2016-06-15 05:00:51 +00:00
|
|
|
type Decoder struct {
|
2016-06-15 04:51:58 +00:00
|
|
|
r interface {
|
|
|
|
io.ByteScanner
|
|
|
|
io.Reader
|
|
|
|
}
|
2017-11-05 04:45:30 +00:00
|
|
|
// Sum of bytes used to Decode values.
|
2017-11-05 04:42:37 +00:00
|
|
|
Offset int64
|
2012-06-20 13:21:32 +00:00
|
|
|
buf bytes.Buffer
|
|
|
|
key string
|
|
|
|
}
|
|
|
|
|
2016-06-15 05:00:51 +00:00
|
|
|
func (d *Decoder) Decode(v interface{}) (err error) {
|
2012-06-20 13:34:10 +00:00
|
|
|
defer func() {
|
|
|
|
if e := recover(); e != nil {
|
|
|
|
if _, ok := e.(runtime.Error); ok {
|
|
|
|
panic(e)
|
2012-06-20 13:21:32 +00:00
|
|
|
}
|
2012-06-20 13:34:10 +00:00
|
|
|
err = e.(error)
|
|
|
|
}
|
|
|
|
}()
|
2012-06-20 13:21:32 +00:00
|
|
|
|
|
|
|
pv := reflect.ValueOf(v)
|
|
|
|
if pv.Kind() != reflect.Ptr || pv.IsNil() {
|
|
|
|
return &UnmarshalInvalidArgError{reflect.TypeOf(v)}
|
|
|
|
}
|
|
|
|
|
2018-01-25 10:46:50 +00:00
|
|
|
ok, err := d.parseValue(pv.Elem())
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !ok {
|
2017-11-05 04:42:37 +00:00
|
|
|
d.throwSyntaxError(d.Offset-1, errors.New("unexpected 'e'"))
|
2015-08-23 07:45:59 +00:00
|
|
|
}
|
2018-01-25 10:46:50 +00:00
|
|
|
return
|
2012-06-20 13:21:32 +00:00
|
|
|
}
|
|
|
|
|
2016-08-26 03:13:30 +00:00
|
|
|
func checkForUnexpectedEOF(err error, offset int64) {
|
2012-06-20 13:21:32 +00:00
|
|
|
if err == io.EOF {
|
|
|
|
panic(&SyntaxError{
|
|
|
|
Offset: offset,
|
2014-11-19 04:08:08 +00:00
|
|
|
What: io.ErrUnexpectedEOF,
|
2012-06-20 13:21:32 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-26 03:13:30 +00:00
|
|
|
func (d *Decoder) readByte() byte {
|
2016-06-15 04:51:58 +00:00
|
|
|
b, err := d.r.ReadByte()
|
2012-06-20 13:21:32 +00:00
|
|
|
if err != nil {
|
2017-11-05 04:42:37 +00:00
|
|
|
checkForUnexpectedEOF(err, d.Offset)
|
2012-06-20 13:21:32 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2017-11-05 04:42:37 +00:00
|
|
|
d.Offset++
|
2012-06-20 13:21:32 +00:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// reads data writing it to 'd.buf' until 'sep' byte is encountered, 'sep' byte
|
|
|
|
// is consumed, but not included into the 'd.buf'
|
2016-08-26 03:13:30 +00:00
|
|
|
func (d *Decoder) readUntil(sep byte) {
|
2012-06-20 13:21:32 +00:00
|
|
|
for {
|
2016-08-26 03:13:30 +00:00
|
|
|
b := d.readByte()
|
2012-06-20 13:21:32 +00:00
|
|
|
if b == sep {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
d.buf.WriteByte(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-26 03:13:30 +00:00
|
|
|
func checkForIntParseError(err error, offset int64) {
|
2012-06-20 13:21:32 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(&SyntaxError{
|
|
|
|
Offset: offset,
|
2014-11-19 04:08:08 +00:00
|
|
|
What: err,
|
2012-06-20 13:21:32 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-15 05:00:51 +00:00
|
|
|
func (d *Decoder) throwSyntaxError(offset int64, err error) {
|
2015-08-23 07:45:59 +00:00
|
|
|
panic(&SyntaxError{
|
|
|
|
Offset: offset,
|
|
|
|
What: err,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2012-06-20 13:21:32 +00:00
|
|
|
// called when 'i' was consumed
|
2016-08-26 04:51:38 +00:00
|
|
|
func (d *Decoder) parseInt(v reflect.Value) {
|
2017-11-05 04:42:37 +00:00
|
|
|
start := d.Offset - 1
|
2016-08-26 03:13:30 +00:00
|
|
|
d.readUntil('e')
|
2012-06-20 13:21:32 +00:00
|
|
|
if d.buf.Len() == 0 {
|
|
|
|
panic(&SyntaxError{
|
|
|
|
Offset: start,
|
2014-11-19 04:08:08 +00:00
|
|
|
What: errors.New("empty integer value"),
|
2012-06-20 13:21:32 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-08-23 07:51:24 +00:00
|
|
|
s := d.buf.String()
|
|
|
|
|
2012-06-20 13:21:32 +00:00
|
|
|
switch v.Kind() {
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
2015-08-23 07:51:24 +00:00
|
|
|
n, err := strconv.ParseInt(s, 10, 64)
|
2016-08-26 03:13:30 +00:00
|
|
|
checkForIntParseError(err, start)
|
2012-06-20 13:21:32 +00:00
|
|
|
|
|
|
|
if v.OverflowInt(n) {
|
|
|
|
panic(&UnmarshalTypeError{
|
2015-08-23 07:51:24 +00:00
|
|
|
Value: "integer " + s,
|
2012-06-20 13:21:32 +00:00
|
|
|
Type: v.Type(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
v.SetInt(n)
|
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
2015-08-23 07:51:24 +00:00
|
|
|
n, err := strconv.ParseUint(s, 10, 64)
|
2016-08-26 03:13:30 +00:00
|
|
|
checkForIntParseError(err, start)
|
2012-06-20 13:21:32 +00:00
|
|
|
|
|
|
|
if v.OverflowUint(n) {
|
|
|
|
panic(&UnmarshalTypeError{
|
2015-08-23 07:51:24 +00:00
|
|
|
Value: "integer " + s,
|
2012-06-20 13:21:32 +00:00
|
|
|
Type: v.Type(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
v.SetUint(n)
|
|
|
|
case reflect.Bool:
|
2015-08-23 07:51:24 +00:00
|
|
|
v.SetBool(s != "0")
|
2012-06-20 13:21:32 +00:00
|
|
|
default:
|
|
|
|
panic(&UnmarshalTypeError{
|
2015-08-23 07:51:24 +00:00
|
|
|
Value: "integer " + s,
|
2012-06-20 13:21:32 +00:00
|
|
|
Type: v.Type(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
d.buf.Reset()
|
|
|
|
}
|
|
|
|
|
2018-01-25 10:46:50 +00:00
|
|
|
func (d *Decoder) parseString(v reflect.Value) error {
|
2017-11-05 04:42:37 +00:00
|
|
|
start := d.Offset - 1
|
2012-06-20 13:21:32 +00:00
|
|
|
|
|
|
|
// read the string length first
|
2016-08-26 03:13:30 +00:00
|
|
|
d.readUntil(':')
|
2012-06-20 13:21:32 +00:00
|
|
|
length, err := strconv.ParseInt(d.buf.String(), 10, 64)
|
2016-08-26 03:13:30 +00:00
|
|
|
checkForIntParseError(err, start)
|
2012-06-20 13:21:32 +00:00
|
|
|
|
|
|
|
d.buf.Reset()
|
2016-06-15 04:51:58 +00:00
|
|
|
n, err := io.CopyN(&d.buf, d.r, length)
|
2017-11-05 04:42:37 +00:00
|
|
|
d.Offset += n
|
2012-06-20 13:21:32 +00:00
|
|
|
if err != nil {
|
2017-11-05 04:42:37 +00:00
|
|
|
checkForUnexpectedEOF(err, d.Offset)
|
2012-06-20 13:21:32 +00:00
|
|
|
panic(&SyntaxError{
|
2017-11-05 04:42:37 +00:00
|
|
|
Offset: d.Offset,
|
2014-11-19 04:08:08 +00:00
|
|
|
What: errors.New("unexpected I/O error: " + err.Error()),
|
2012-06-20 13:21:32 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
switch v.Kind() {
|
|
|
|
case reflect.String:
|
|
|
|
v.SetString(d.buf.String())
|
|
|
|
case reflect.Slice:
|
|
|
|
if v.Type().Elem().Kind() != reflect.Uint8 {
|
|
|
|
panic(&UnmarshalTypeError{
|
|
|
|
Value: "string",
|
|
|
|
Type: v.Type(),
|
|
|
|
})
|
|
|
|
}
|
2014-03-19 14:54:18 +00:00
|
|
|
sl := make([]byte, len(d.buf.Bytes()))
|
|
|
|
copy(sl, d.buf.Bytes())
|
|
|
|
v.Set(reflect.ValueOf(sl))
|
2012-06-20 13:21:32 +00:00
|
|
|
default:
|
2018-01-25 10:46:50 +00:00
|
|
|
return &UnmarshalTypeError{
|
2012-06-20 13:21:32 +00:00
|
|
|
Value: "string",
|
|
|
|
Type: v.Type(),
|
2018-01-25 10:46:50 +00:00
|
|
|
}
|
2012-06-20 13:21:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
d.buf.Reset()
|
2018-01-25 10:46:50 +00:00
|
|
|
return nil
|
2012-06-20 13:21:32 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 10:46:50 +00:00
|
|
|
func (d *Decoder) parseDict(v reflect.Value) error {
|
2012-06-20 13:21:32 +00:00
|
|
|
switch v.Kind() {
|
|
|
|
case reflect.Map:
|
|
|
|
t := v.Type()
|
|
|
|
if t.Key().Kind() != reflect.String {
|
|
|
|
panic(&UnmarshalTypeError{
|
|
|
|
Value: "object",
|
|
|
|
Type: t,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if v.IsNil() {
|
|
|
|
v.Set(reflect.MakeMap(t))
|
|
|
|
}
|
|
|
|
case reflect.Struct:
|
|
|
|
default:
|
|
|
|
panic(&UnmarshalTypeError{
|
|
|
|
Value: "object",
|
|
|
|
Type: v.Type(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-08-26 04:51:38 +00:00
|
|
|
var mapElem reflect.Value
|
2012-06-20 13:21:32 +00:00
|
|
|
|
|
|
|
// so, at this point 'd' byte was consumed, let's just read key/value
|
|
|
|
// pairs one by one
|
|
|
|
for {
|
|
|
|
var valuev reflect.Value
|
|
|
|
keyv := reflect.ValueOf(&d.key).Elem()
|
2018-01-25 10:46:50 +00:00
|
|
|
ok, err := d.parseValue(keyv)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error parsing dict key: %s", err)
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return nil
|
2012-06-20 13:21:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// get valuev as a map value or as a struct field
|
|
|
|
switch v.Kind() {
|
|
|
|
case reflect.Map:
|
|
|
|
elem_type := v.Type().Elem()
|
2016-08-26 04:51:38 +00:00
|
|
|
if !mapElem.IsValid() {
|
|
|
|
mapElem = reflect.New(elem_type).Elem()
|
2012-06-20 13:21:32 +00:00
|
|
|
} else {
|
2016-08-26 04:51:38 +00:00
|
|
|
mapElem.Set(reflect.Zero(elem_type))
|
2012-06-20 13:21:32 +00:00
|
|
|
}
|
2016-08-26 04:51:38 +00:00
|
|
|
valuev = mapElem
|
2012-06-20 13:21:32 +00:00
|
|
|
case reflect.Struct:
|
|
|
|
var f reflect.StructField
|
|
|
|
var ok bool
|
|
|
|
|
|
|
|
t := v.Type()
|
|
|
|
for i, n := 0, t.NumField(); i < n; i++ {
|
|
|
|
f = t.Field(i)
|
|
|
|
tag := f.Tag.Get("bencode")
|
|
|
|
if tag == "-" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if f.Anonymous {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-08-26 04:51:38 +00:00
|
|
|
tag_name, _ := parseTag(tag)
|
2012-06-20 13:21:32 +00:00
|
|
|
if tag_name == d.key {
|
|
|
|
ok = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.Name == d.key {
|
|
|
|
ok = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.EqualFold(f.Name, d.key) {
|
|
|
|
ok = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
if f.PkgPath != "" {
|
|
|
|
panic(&UnmarshalFieldError{
|
|
|
|
Key: d.key,
|
|
|
|
Type: v.Type(),
|
|
|
|
Field: f,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
valuev = v.FieldByIndex(f.Index)
|
|
|
|
}
|
|
|
|
} else {
|
2016-08-26 03:13:30 +00:00
|
|
|
_, ok := d.parseValueInterface()
|
2012-06-20 13:21:32 +00:00
|
|
|
if !ok {
|
2018-01-25 10:46:50 +00:00
|
|
|
return fmt.Errorf("error parsing dict value for key %q", d.key)
|
2012-06-20 13:21:32 +00:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// now we need to actually parse it
|
2018-01-25 10:46:50 +00:00
|
|
|
ok, err = d.parseValue(valuev)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parsing value for key %q: %s", d.key, err)
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("missing value for key %q", d.key)
|
2012-06-20 13:21:32 +00:00
|
|
|
}
|
|
|
|
if v.Kind() == reflect.Map {
|
|
|
|
v.SetMapIndex(keyv, valuev)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 10:46:50 +00:00
|
|
|
func (d *Decoder) parseList(v reflect.Value) error {
|
2012-06-20 13:21:32 +00:00
|
|
|
switch v.Kind() {
|
|
|
|
case reflect.Array, reflect.Slice:
|
|
|
|
default:
|
|
|
|
panic(&UnmarshalTypeError{
|
|
|
|
Value: "array",
|
|
|
|
Type: v.Type(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
i := 0
|
2018-01-25 10:46:50 +00:00
|
|
|
for ; ; i++ {
|
2012-06-20 13:21:32 +00:00
|
|
|
if v.Kind() == reflect.Slice && i >= v.Len() {
|
|
|
|
v.Set(reflect.Append(v, reflect.Zero(v.Type().Elem())))
|
|
|
|
}
|
|
|
|
|
|
|
|
if i < v.Len() {
|
2018-01-25 10:46:50 +00:00
|
|
|
ok, err := d.parseValue(v.Index(i))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
2012-06-20 13:21:32 +00:00
|
|
|
} else {
|
2018-01-25 10:46:50 +00:00
|
|
|
_, ok := d.parseValueInterface()
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
2012-06-20 13:21:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if i < v.Len() {
|
|
|
|
if v.Kind() == reflect.Array {
|
|
|
|
z := reflect.Zero(v.Type().Elem())
|
|
|
|
for n := v.Len(); i < n; i++ {
|
|
|
|
v.Index(i).Set(z)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
v.SetLen(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if i == 0 && v.Kind() == reflect.Slice {
|
|
|
|
v.Set(reflect.MakeSlice(v.Type(), 0, 0))
|
|
|
|
}
|
2018-01-25 10:46:50 +00:00
|
|
|
return nil
|
2012-06-20 13:21:32 +00:00
|
|
|
}
|
|
|
|
|
2016-08-26 03:13:30 +00:00
|
|
|
func (d *Decoder) readOneValue() bool {
|
2016-06-15 04:51:58 +00:00
|
|
|
b, err := d.r.ReadByte()
|
2012-06-27 20:21:26 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if b == 'e' {
|
2016-06-15 04:51:58 +00:00
|
|
|
d.r.UnreadByte()
|
2012-06-27 20:21:26 +00:00
|
|
|
return false
|
|
|
|
} else {
|
2017-11-05 04:42:37 +00:00
|
|
|
d.Offset++
|
2012-06-27 20:21:26 +00:00
|
|
|
d.buf.WriteByte(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch b {
|
|
|
|
case 'd', 'l':
|
|
|
|
// read until there is nothing to read
|
2016-08-26 03:13:30 +00:00
|
|
|
for d.readOneValue() {
|
2014-03-19 14:54:18 +00:00
|
|
|
}
|
2012-06-27 20:21:26 +00:00
|
|
|
// consume 'e' as well
|
2016-08-26 03:13:30 +00:00
|
|
|
b = d.readByte()
|
2012-06-27 20:21:26 +00:00
|
|
|
d.buf.WriteByte(b)
|
|
|
|
case 'i':
|
2016-08-26 03:13:30 +00:00
|
|
|
d.readUntil('e')
|
2012-06-27 20:21:26 +00:00
|
|
|
d.buf.WriteString("e")
|
|
|
|
default:
|
|
|
|
if b >= '0' && b <= '9' {
|
|
|
|
start := d.buf.Len() - 1
|
2016-08-26 03:13:30 +00:00
|
|
|
d.readUntil(':')
|
2012-06-27 20:21:26 +00:00
|
|
|
length, err := strconv.ParseInt(d.buf.String()[start:], 10, 64)
|
2017-11-05 04:42:37 +00:00
|
|
|
checkForIntParseError(err, d.Offset-1)
|
2012-06-27 20:21:26 +00:00
|
|
|
|
|
|
|
d.buf.WriteString(":")
|
2016-06-15 04:51:58 +00:00
|
|
|
n, err := io.CopyN(&d.buf, d.r, length)
|
2017-11-05 04:42:37 +00:00
|
|
|
d.Offset += n
|
2012-06-27 20:21:26 +00:00
|
|
|
if err != nil {
|
2017-11-05 04:42:37 +00:00
|
|
|
checkForUnexpectedEOF(err, d.Offset)
|
2012-06-27 20:21:26 +00:00
|
|
|
panic(&SyntaxError{
|
2017-11-05 04:42:37 +00:00
|
|
|
Offset: d.Offset,
|
2014-11-19 04:08:08 +00:00
|
|
|
What: errors.New("unexpected I/O error: " + err.Error()),
|
2012-06-27 20:21:26 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2017-11-05 04:42:37 +00:00
|
|
|
d.raiseUnknownValueType(b, d.Offset-1)
|
2012-06-27 20:21:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-08-26 03:13:30 +00:00
|
|
|
func (d *Decoder) parseUnmarshaler(v reflect.Value) bool {
|
2012-06-27 20:21:26 +00:00
|
|
|
m, ok := v.Interface().(Unmarshaler)
|
|
|
|
if !ok {
|
|
|
|
// T doesn't work, try *T
|
|
|
|
if v.Kind() != reflect.Ptr && v.CanAddr() {
|
|
|
|
m, ok = v.Addr().Interface().(Unmarshaler)
|
|
|
|
if ok {
|
|
|
|
v = v.Addr()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ok && (v.Kind() != reflect.Ptr || !v.IsNil()) {
|
2016-08-26 03:13:30 +00:00
|
|
|
if d.readOneValue() {
|
2012-06-27 20:21:26 +00:00
|
|
|
err := m.UnmarshalBencode(d.buf.Bytes())
|
|
|
|
d.buf.Reset()
|
|
|
|
if err != nil {
|
2012-06-30 16:26:20 +00:00
|
|
|
panic(&UnmarshalerError{v.Type(), err})
|
2012-06-27 20:21:26 +00:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
d.buf.Reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-10-01 14:13:43 +00:00
|
|
|
// Returns true if there was a value and it's now stored in 'v', otherwise
|
|
|
|
// there was an end symbol ("e") and no value was stored.
|
2018-01-25 10:46:50 +00:00
|
|
|
func (d *Decoder) parseValue(v reflect.Value) (bool, error) {
|
2012-06-27 20:21:26 +00:00
|
|
|
// we support one level of indirection at the moment
|
|
|
|
if v.Kind() == reflect.Ptr {
|
2012-06-20 13:21:32 +00:00
|
|
|
// if the pointer is nil, allocate a new element of the type it
|
|
|
|
// points to
|
2012-06-27 20:21:26 +00:00
|
|
|
if v.IsNil() {
|
|
|
|
v.Set(reflect.New(v.Type().Elem()))
|
2012-06-20 13:21:32 +00:00
|
|
|
}
|
2012-06-27 20:21:26 +00:00
|
|
|
v = v.Elem()
|
|
|
|
}
|
|
|
|
|
2016-08-26 03:13:30 +00:00
|
|
|
if d.parseUnmarshaler(v) {
|
2018-01-25 10:46:50 +00:00
|
|
|
return true, nil
|
2012-06-20 13:21:32 +00:00
|
|
|
}
|
|
|
|
|
2012-06-27 20:21:26 +00:00
|
|
|
// common case: interface{}
|
|
|
|
if v.Kind() == reflect.Interface && v.NumMethod() == 0 {
|
2016-08-26 03:13:30 +00:00
|
|
|
iface, _ := d.parseValueInterface()
|
2012-06-20 13:21:32 +00:00
|
|
|
v.Set(reflect.ValueOf(iface))
|
2018-01-25 10:46:50 +00:00
|
|
|
return true, nil
|
2012-06-20 13:21:32 +00:00
|
|
|
}
|
|
|
|
|
2016-06-15 04:51:58 +00:00
|
|
|
b, err := d.r.ReadByte()
|
2012-06-20 13:21:32 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-11-05 04:42:37 +00:00
|
|
|
d.Offset++
|
2012-06-20 13:21:32 +00:00
|
|
|
|
|
|
|
switch b {
|
|
|
|
case 'e':
|
2018-01-25 10:46:50 +00:00
|
|
|
return false, nil
|
2012-06-20 13:21:32 +00:00
|
|
|
case 'd':
|
2018-01-25 10:46:50 +00:00
|
|
|
return true, d.parseDict(v)
|
2012-06-20 13:21:32 +00:00
|
|
|
case 'l':
|
2018-01-25 10:46:50 +00:00
|
|
|
return true, d.parseList(v)
|
2012-06-20 13:21:32 +00:00
|
|
|
case 'i':
|
2016-08-26 04:51:38 +00:00
|
|
|
d.parseInt(v)
|
2018-01-25 10:46:50 +00:00
|
|
|
return true, nil
|
2012-06-20 13:21:32 +00:00
|
|
|
default:
|
|
|
|
if b >= '0' && b <= '9' {
|
|
|
|
// string
|
|
|
|
// append first digit of the length to the buffer
|
|
|
|
d.buf.WriteByte(b)
|
2018-01-25 10:46:50 +00:00
|
|
|
return true, d.parseString(v)
|
2012-06-20 13:21:32 +00:00
|
|
|
}
|
|
|
|
|
2017-11-05 04:42:37 +00:00
|
|
|
d.raiseUnknownValueType(b, d.Offset-1)
|
2012-06-20 13:21:32 +00:00
|
|
|
}
|
2018-01-25 10:46:50 +00:00
|
|
|
panic("unreachable")
|
2012-06-20 13:21:32 +00:00
|
|
|
}
|
|
|
|
|
2015-11-03 12:21:10 +00:00
|
|
|
// An unknown bencode type character was encountered.
|
2016-06-15 05:00:51 +00:00
|
|
|
func (d *Decoder) raiseUnknownValueType(b byte, offset int64) {
|
2015-11-03 12:21:10 +00:00
|
|
|
panic(&SyntaxError{
|
|
|
|
Offset: offset,
|
|
|
|
What: fmt.Errorf("unknown value type %+q", b),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-08-26 03:13:30 +00:00
|
|
|
func (d *Decoder) parseValueInterface() (interface{}, bool) {
|
2016-06-15 04:51:58 +00:00
|
|
|
b, err := d.r.ReadByte()
|
2012-06-20 13:21:32 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-11-05 04:42:37 +00:00
|
|
|
d.Offset++
|
2012-06-20 13:21:32 +00:00
|
|
|
|
|
|
|
switch b {
|
|
|
|
case 'e':
|
|
|
|
return nil, false
|
|
|
|
case 'd':
|
2016-08-26 03:13:30 +00:00
|
|
|
return d.parseDictInterface(), true
|
2012-06-20 13:21:32 +00:00
|
|
|
case 'l':
|
2016-08-26 03:13:30 +00:00
|
|
|
return d.parseListInterface(), true
|
2012-06-20 13:21:32 +00:00
|
|
|
case 'i':
|
2016-08-26 03:13:30 +00:00
|
|
|
return d.parseIntInterface(), true
|
2012-06-20 13:21:32 +00:00
|
|
|
default:
|
|
|
|
if b >= '0' && b <= '9' {
|
|
|
|
// string
|
|
|
|
// append first digit of the length to the buffer
|
|
|
|
d.buf.WriteByte(b)
|
2016-08-26 03:13:30 +00:00
|
|
|
return d.parseStringInterface(), true
|
2012-06-20 13:21:32 +00:00
|
|
|
}
|
|
|
|
|
2017-11-05 04:42:37 +00:00
|
|
|
d.raiseUnknownValueType(b, d.Offset-1)
|
2015-11-03 12:21:10 +00:00
|
|
|
panic("unreachable")
|
2012-06-20 13:21:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-26 03:13:30 +00:00
|
|
|
func (d *Decoder) parseIntInterface() (ret interface{}) {
|
2017-11-05 04:42:37 +00:00
|
|
|
start := d.Offset - 1
|
2016-08-26 03:13:30 +00:00
|
|
|
d.readUntil('e')
|
2012-06-20 13:21:32 +00:00
|
|
|
if d.buf.Len() == 0 {
|
|
|
|
panic(&SyntaxError{
|
|
|
|
Offset: start,
|
2014-11-19 04:08:08 +00:00
|
|
|
What: errors.New("empty integer value"),
|
2012-06-20 13:21:32 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
n, err := strconv.ParseInt(d.buf.String(), 10, 64)
|
2015-09-20 11:08:42 +00:00
|
|
|
if ne, ok := err.(*strconv.NumError); ok && ne.Err == strconv.ErrRange {
|
|
|
|
i := new(big.Int)
|
|
|
|
_, ok := i.SetString(d.buf.String(), 10)
|
|
|
|
if !ok {
|
|
|
|
panic(&SyntaxError{
|
|
|
|
Offset: start,
|
|
|
|
What: errors.New("failed to parse integer"),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
ret = i
|
|
|
|
} else {
|
2016-08-26 03:13:30 +00:00
|
|
|
checkForIntParseError(err, start)
|
2015-09-20 11:08:42 +00:00
|
|
|
ret = n
|
|
|
|
}
|
|
|
|
|
2012-06-20 13:21:32 +00:00
|
|
|
d.buf.Reset()
|
2015-09-20 11:08:42 +00:00
|
|
|
return
|
2012-06-20 13:21:32 +00:00
|
|
|
}
|
|
|
|
|
2016-08-26 03:13:30 +00:00
|
|
|
func (d *Decoder) parseStringInterface() interface{} {
|
2017-11-05 04:42:37 +00:00
|
|
|
start := d.Offset - 1
|
2012-06-20 13:21:32 +00:00
|
|
|
|
|
|
|
// read the string length first
|
2016-08-26 03:13:30 +00:00
|
|
|
d.readUntil(':')
|
2012-06-20 13:21:32 +00:00
|
|
|
length, err := strconv.ParseInt(d.buf.String(), 10, 64)
|
2016-08-26 03:13:30 +00:00
|
|
|
checkForIntParseError(err, start)
|
2012-06-20 13:21:32 +00:00
|
|
|
|
|
|
|
d.buf.Reset()
|
2016-06-15 04:51:58 +00:00
|
|
|
n, err := io.CopyN(&d.buf, d.r, length)
|
2017-11-05 04:42:37 +00:00
|
|
|
d.Offset += n
|
2012-06-20 13:21:32 +00:00
|
|
|
if err != nil {
|
2017-11-05 04:42:37 +00:00
|
|
|
checkForUnexpectedEOF(err, d.Offset)
|
2012-06-20 13:21:32 +00:00
|
|
|
panic(&SyntaxError{
|
2017-11-05 04:42:37 +00:00
|
|
|
Offset: d.Offset,
|
2014-11-19 04:08:08 +00:00
|
|
|
What: errors.New("unexpected I/O error: " + err.Error()),
|
2012-06-20 13:21:32 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
s := d.buf.String()
|
|
|
|
d.buf.Reset()
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2016-08-26 03:13:30 +00:00
|
|
|
func (d *Decoder) parseDictInterface() interface{} {
|
2012-06-20 13:21:32 +00:00
|
|
|
dict := make(map[string]interface{})
|
|
|
|
for {
|
2016-08-26 03:13:30 +00:00
|
|
|
keyi, ok := d.parseValueInterface()
|
2012-06-20 13:21:32 +00:00
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
key, ok := keyi.(string)
|
|
|
|
if !ok {
|
|
|
|
panic(&SyntaxError{
|
2017-11-05 04:42:37 +00:00
|
|
|
Offset: d.Offset,
|
2014-11-19 04:08:08 +00:00
|
|
|
What: errors.New("non-string key in a dict"),
|
2012-06-20 13:21:32 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-08-26 03:13:30 +00:00
|
|
|
valuei, ok := d.parseValueInterface()
|
2012-06-20 13:21:32 +00:00
|
|
|
if !ok {
|
2015-10-01 14:13:43 +00:00
|
|
|
break
|
2012-06-20 13:21:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dict[key] = valuei
|
|
|
|
}
|
|
|
|
return dict
|
|
|
|
}
|
|
|
|
|
2016-08-26 03:13:30 +00:00
|
|
|
func (d *Decoder) parseListInterface() interface{} {
|
2012-06-20 13:21:32 +00:00
|
|
|
var list []interface{}
|
|
|
|
for {
|
2016-08-26 03:13:30 +00:00
|
|
|
valuei, ok := d.parseValueInterface()
|
2012-06-20 13:21:32 +00:00
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
list = append(list, valuei)
|
|
|
|
}
|
2012-06-22 13:34:26 +00:00
|
|
|
if list == nil {
|
|
|
|
list = make([]interface{}, 0, 0)
|
|
|
|
}
|
2012-06-20 13:21:32 +00:00
|
|
|
return list
|
|
|
|
}
|