2012-06-20 21:21:32 +08:00
|
|
|
package bencode
|
|
|
|
|
2014-11-19 12:08:08 +08:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
2015-11-03 20:21:10 +08:00
|
|
|
"fmt"
|
2014-11-19 12:08:08 +08:00
|
|
|
"io"
|
2015-09-20 19:08:42 +08:00
|
|
|
"math/big"
|
2014-11-19 12:08:08 +08:00
|
|
|
"reflect"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
2018-07-23 08:50:58 +08:00
|
|
|
"sync"
|
2023-03-20 07:50:22 +08:00
|
|
|
"unsafe"
|
2014-11-19 12:08:08 +08:00
|
|
|
)
|
2012-06-20 21:21:32 +08:00
|
|
|
|
2022-01-07 16:05:03 +08:00
|
|
|
// The default bencode string length limit. This is a poor attempt to prevent excessive memory
|
|
|
|
// allocation when parsing, but also leaves the window open to implement a better solution.
|
|
|
|
const DefaultDecodeMaxStrLen = 1<<27 - 1 // ~128MiB
|
|
|
|
|
|
|
|
type MaxStrLen = int64
|
|
|
|
|
2016-06-15 13:00:51 +08:00
|
|
|
type Decoder struct {
|
2022-01-07 16:05:03 +08:00
|
|
|
// Maximum parsed bencode string length. Defaults to DefaultMaxStrLen if zero.
|
|
|
|
MaxStrLen MaxStrLen
|
|
|
|
|
2016-06-15 12:51:58 +08:00
|
|
|
r interface {
|
|
|
|
io.ByteScanner
|
|
|
|
io.Reader
|
|
|
|
}
|
2017-11-05 12:45:30 +08:00
|
|
|
// Sum of bytes used to Decode values.
|
2017-11-05 12:42:37 +08:00
|
|
|
Offset int64
|
2012-06-20 21:21:32 +08:00
|
|
|
buf bytes.Buffer
|
|
|
|
}
|
|
|
|
|
2016-06-15 13:00:51 +08:00
|
|
|
func (d *Decoder) Decode(v interface{}) (err error) {
|
2012-06-20 21:34:10 +08:00
|
|
|
defer func() {
|
2018-02-12 21:55:15 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r := recover()
|
2022-01-07 16:06:33 +08:00
|
|
|
if r == nil {
|
|
|
|
return
|
|
|
|
}
|
2018-02-12 21:55:15 +08:00
|
|
|
_, ok := r.(runtime.Error)
|
|
|
|
if ok {
|
|
|
|
panic(r)
|
|
|
|
}
|
2022-01-07 16:06:33 +08:00
|
|
|
if err, ok = r.(error); !ok {
|
2018-02-12 21:55:15 +08:00
|
|
|
panic(r)
|
2012-06-20 21:34:10 +08:00
|
|
|
}
|
2022-01-07 16:06:33 +08:00
|
|
|
// Errors thrown from deeper in parsing are unexpected. At value boundaries, errors should
|
|
|
|
// be returned directly (at least until all the panic nonsense is removed entirely).
|
|
|
|
if err == io.EOF {
|
|
|
|
err = io.ErrUnexpectedEOF
|
|
|
|
}
|
2012-06-20 21:34:10 +08:00
|
|
|
}()
|
2012-06-20 21:21:32 +08:00
|
|
|
|
|
|
|
pv := reflect.ValueOf(v)
|
|
|
|
if pv.Kind() != reflect.Ptr || pv.IsNil() {
|
|
|
|
return &UnmarshalInvalidArgError{reflect.TypeOf(v)}
|
|
|
|
}
|
|
|
|
|
2018-01-25 18:46:50 +08:00
|
|
|
ok, err := d.parseValue(pv.Elem())
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !ok {
|
2017-11-05 12:42:37 +08:00
|
|
|
d.throwSyntaxError(d.Offset-1, errors.New("unexpected 'e'"))
|
2015-08-23 15:45:59 +08:00
|
|
|
}
|
2018-01-25 18:46:50 +08:00
|
|
|
return
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
|
|
|
|
2016-08-26 11:13:30 +08:00
|
|
|
func checkForUnexpectedEOF(err error, offset int64) {
|
2012-06-20 21:21:32 +08:00
|
|
|
if err == io.EOF {
|
|
|
|
panic(&SyntaxError{
|
|
|
|
Offset: offset,
|
2014-11-19 12:08:08 +08:00
|
|
|
What: io.ErrUnexpectedEOF,
|
2012-06-20 21:21:32 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-26 11:13:30 +08:00
|
|
|
func (d *Decoder) readByte() byte {
|
2016-06-15 12:51:58 +08:00
|
|
|
b, err := d.r.ReadByte()
|
2012-06-20 21:21:32 +08:00
|
|
|
if err != nil {
|
2017-11-05 12:42:37 +08:00
|
|
|
checkForUnexpectedEOF(err, d.Offset)
|
2012-06-20 21:21:32 +08:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2017-11-05 12:42:37 +08:00
|
|
|
d.Offset++
|
2012-06-20 21:21:32 +08: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 11:13:30 +08:00
|
|
|
func (d *Decoder) readUntil(sep byte) {
|
2012-06-20 21:21:32 +08:00
|
|
|
for {
|
2016-08-26 11:13:30 +08:00
|
|
|
b := d.readByte()
|
2012-06-20 21:21:32 +08:00
|
|
|
if b == sep {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
d.buf.WriteByte(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-26 11:13:30 +08:00
|
|
|
func checkForIntParseError(err error, offset int64) {
|
2012-06-20 21:21:32 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(&SyntaxError{
|
|
|
|
Offset: offset,
|
2014-11-19 12:08:08 +08:00
|
|
|
What: err,
|
2012-06-20 21:21:32 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-15 13:00:51 +08:00
|
|
|
func (d *Decoder) throwSyntaxError(offset int64, err error) {
|
2015-08-23 15:45:59 +08:00
|
|
|
panic(&SyntaxError{
|
|
|
|
Offset: offset,
|
|
|
|
What: err,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-10 10:36:53 +08:00
|
|
|
// Assume the 'i' is already consumed. Read and validate the rest of an int into the buffer.
|
|
|
|
func (d *Decoder) readInt() error {
|
|
|
|
// start := d.Offset - 1
|
|
|
|
d.readUntil('e')
|
2021-12-10 11:29:35 +08:00
|
|
|
if err := d.checkBufferedInt(); err != nil {
|
2021-12-10 10:36:53 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// if d.buf.Len() == 0 {
|
|
|
|
// panic(&SyntaxError{
|
|
|
|
// Offset: start,
|
|
|
|
// What: errors.New("empty integer value"),
|
|
|
|
// })
|
|
|
|
// }
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// called when 'i' was consumed, for the integer type in v.
|
2021-08-12 11:46:02 +08:00
|
|
|
func (d *Decoder) parseInt(v reflect.Value) error {
|
2017-11-05 12:42:37 +08:00
|
|
|
start := d.Offset - 1
|
2012-06-20 21:21:32 +08:00
|
|
|
|
2021-12-10 10:36:53 +08:00
|
|
|
if err := d.readInt(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-07-23 11:12:14 +08:00
|
|
|
s := bytesAsString(d.buf.Bytes())
|
2015-08-23 15:51:24 +08:00
|
|
|
|
2012-06-20 21:21:32 +08:00
|
|
|
switch v.Kind() {
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
2015-08-23 15:51:24 +08:00
|
|
|
n, err := strconv.ParseInt(s, 10, 64)
|
2016-08-26 11:13:30 +08:00
|
|
|
checkForIntParseError(err, start)
|
2012-06-20 21:21:32 +08:00
|
|
|
|
|
|
|
if v.OverflowInt(n) {
|
2021-08-12 11:46:02 +08:00
|
|
|
return &UnmarshalTypeError{
|
|
|
|
BencodeTypeName: "int",
|
|
|
|
UnmarshalTargetType: v.Type(),
|
|
|
|
}
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
|
|
|
v.SetInt(n)
|
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
2015-08-23 15:51:24 +08:00
|
|
|
n, err := strconv.ParseUint(s, 10, 64)
|
2016-08-26 11:13:30 +08:00
|
|
|
checkForIntParseError(err, start)
|
2012-06-20 21:21:32 +08:00
|
|
|
|
|
|
|
if v.OverflowUint(n) {
|
2021-08-12 11:46:02 +08:00
|
|
|
return &UnmarshalTypeError{
|
|
|
|
BencodeTypeName: "int",
|
|
|
|
UnmarshalTargetType: v.Type(),
|
|
|
|
}
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
|
|
|
v.SetUint(n)
|
|
|
|
case reflect.Bool:
|
2015-08-23 15:51:24 +08:00
|
|
|
v.SetBool(s != "0")
|
2012-06-20 21:21:32 +08:00
|
|
|
default:
|
2021-08-12 11:46:02 +08:00
|
|
|
return &UnmarshalTypeError{
|
|
|
|
BencodeTypeName: "int",
|
|
|
|
UnmarshalTargetType: v.Type(),
|
|
|
|
}
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
|
|
|
d.buf.Reset()
|
2021-08-12 11:46:02 +08:00
|
|
|
return nil
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
|
|
|
|
2021-12-10 11:29:35 +08:00
|
|
|
func (d *Decoder) checkBufferedInt() error {
|
2021-12-10 10:36:53 +08:00
|
|
|
b := d.buf.Bytes()
|
2021-12-10 11:29:35 +08:00
|
|
|
if len(b) <= 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if b[0] == '-' {
|
|
|
|
b = b[1:]
|
|
|
|
}
|
|
|
|
if b[0] < '1' || b[0] > '9' {
|
|
|
|
return errors.New("invalid leading digit")
|
2021-12-10 10:36:53 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2012-06-20 21:21:32 +08:00
|
|
|
|
2023-01-03 22:25:26 +08:00
|
|
|
func (d *Decoder) parseStringLength() (int, error) {
|
2021-12-10 10:36:53 +08:00
|
|
|
// We should have already consumed the first byte of the length into the Decoder buf.
|
|
|
|
start := d.Offset - 1
|
2016-08-26 11:13:30 +08:00
|
|
|
d.readUntil(':')
|
2021-12-10 11:29:35 +08:00
|
|
|
if err := d.checkBufferedInt(); err != nil {
|
2021-12-10 10:36:53 +08:00
|
|
|
return 0, err
|
|
|
|
}
|
2022-01-07 16:05:03 +08:00
|
|
|
// Really the limit should be the uint size for the platform. But we can't pass in an allocator,
|
|
|
|
// or limit total memory use in Go, the best we might hope to do is limit the size of a single
|
|
|
|
// decoded value (by reading it in in-place and then operating on a view).
|
2023-01-03 22:25:26 +08:00
|
|
|
length, err := strconv.ParseInt(bytesAsString(d.buf.Bytes()), 10, 0)
|
2016-08-26 11:13:30 +08:00
|
|
|
checkForIntParseError(err, start)
|
2022-01-07 16:05:03 +08:00
|
|
|
if int64(length) > d.getMaxStrLen() {
|
|
|
|
err = fmt.Errorf("parsed string length %v exceeds limit (%v)", length, DefaultDecodeMaxStrLen)
|
|
|
|
}
|
2021-12-10 10:36:53 +08:00
|
|
|
d.buf.Reset()
|
2023-01-03 22:25:26 +08:00
|
|
|
return int(length), err
|
2021-12-10 10:36:53 +08:00
|
|
|
}
|
2012-06-20 21:21:32 +08:00
|
|
|
|
2021-12-10 10:36:53 +08:00
|
|
|
func (d *Decoder) parseString(v reflect.Value) error {
|
|
|
|
length, err := d.parseStringLength()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-07-23 11:12:14 +08:00
|
|
|
defer d.buf.Reset()
|
|
|
|
read := func(b []byte) {
|
|
|
|
n, err := io.ReadFull(d.r, b)
|
|
|
|
d.Offset += int64(n)
|
|
|
|
if err != nil {
|
|
|
|
checkForUnexpectedEOF(err, d.Offset)
|
|
|
|
panic(&SyntaxError{
|
|
|
|
Offset: d.Offset,
|
|
|
|
What: errors.New("unexpected I/O error: " + err.Error()),
|
|
|
|
})
|
|
|
|
}
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
switch v.Kind() {
|
|
|
|
case reflect.String:
|
2018-07-23 11:12:14 +08:00
|
|
|
b := make([]byte, length)
|
|
|
|
read(b)
|
|
|
|
v.SetString(bytesAsString(b))
|
2018-07-11 13:22:29 +08:00
|
|
|
return nil
|
2012-06-20 21:21:32 +08:00
|
|
|
case reflect.Slice:
|
|
|
|
if v.Type().Elem().Kind() != reflect.Uint8 {
|
2018-07-11 13:22:29 +08:00
|
|
|
break
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
2018-07-23 11:12:14 +08:00
|
|
|
b := make([]byte, length)
|
|
|
|
read(b)
|
|
|
|
v.SetBytes(b)
|
2018-07-11 13:22:29 +08:00
|
|
|
return nil
|
|
|
|
case reflect.Array:
|
|
|
|
if v.Type().Elem().Kind() != reflect.Uint8 {
|
|
|
|
break
|
2018-01-25 18:46:50 +08:00
|
|
|
}
|
2023-03-20 07:50:22 +08:00
|
|
|
d.buf.Grow(length)
|
2018-07-23 11:12:14 +08:00
|
|
|
b := d.buf.Bytes()[:length]
|
|
|
|
read(b)
|
|
|
|
reflect.Copy(v, reflect.ValueOf(b))
|
2018-07-11 13:22:29 +08:00
|
|
|
return nil
|
2023-03-20 07:50:22 +08:00
|
|
|
case reflect.Bool:
|
|
|
|
d.buf.Grow(length)
|
|
|
|
b := d.buf.Bytes()[:length]
|
|
|
|
read(b)
|
|
|
|
x, err := strconv.ParseBool(unsafe.String(unsafe.SliceData(b), len(b)))
|
|
|
|
if err != nil {
|
|
|
|
x = length != 0
|
|
|
|
}
|
|
|
|
v.SetBool(x)
|
|
|
|
return nil
|
2018-07-11 13:22:29 +08:00
|
|
|
}
|
2023-03-20 07:50:22 +08:00
|
|
|
// Can't move this into default clause because some cases above fail through to here after
|
|
|
|
// additional checks.
|
|
|
|
d.buf.Grow(length)
|
2018-07-23 11:12:14 +08:00
|
|
|
read(d.buf.Bytes()[:length])
|
2018-07-11 13:22:29 +08:00
|
|
|
// I believe we return here to support "ignore_unmarshal_type_error".
|
|
|
|
return &UnmarshalTypeError{
|
2021-08-12 11:46:02 +08:00
|
|
|
BencodeTypeName: "string",
|
|
|
|
UnmarshalTargetType: v.Type(),
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-27 11:31:12 +08:00
|
|
|
// Info for parsing a dict value.
|
|
|
|
type dictField struct {
|
2021-05-21 21:50:29 +08:00
|
|
|
Type reflect.Type
|
|
|
|
Get func(value reflect.Value) func(reflect.Value)
|
|
|
|
Tags tag
|
2018-01-27 11:31:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns specifics for parsing a dict field value.
|
2021-08-12 10:16:53 +08:00
|
|
|
func getDictField(dict reflect.Type, key string) (_ dictField, err error) {
|
2018-01-27 11:31:12 +08:00
|
|
|
// get valuev as a map value or as a struct field
|
2021-08-12 10:16:53 +08:00
|
|
|
switch k := dict.Kind(); k {
|
2018-01-27 11:31:12 +08:00
|
|
|
case reflect.Map:
|
|
|
|
return dictField{
|
2021-05-21 21:50:29 +08:00
|
|
|
Type: dict.Elem(),
|
|
|
|
Get: func(mapValue reflect.Value) func(reflect.Value) {
|
|
|
|
return func(value reflect.Value) {
|
|
|
|
if mapValue.IsNil() {
|
|
|
|
mapValue.Set(reflect.MakeMap(dict))
|
|
|
|
}
|
|
|
|
// Assigns the value into the map.
|
2021-11-08 11:47:01 +08:00
|
|
|
// log.Printf("map type: %v", mapValue.Type())
|
2021-05-21 21:50:29 +08:00
|
|
|
mapValue.SetMapIndex(reflect.ValueOf(key).Convert(dict.Key()), value)
|
2018-07-11 13:33:41 +08:00
|
|
|
}
|
2018-01-27 11:31:12 +08:00
|
|
|
},
|
2021-08-12 10:16:53 +08:00
|
|
|
}, nil
|
2018-01-27 11:31:12 +08:00
|
|
|
case reflect.Struct:
|
2021-08-12 10:16:53 +08:00
|
|
|
return getStructFieldForKey(dict, key), nil
|
2021-12-10 10:36:53 +08:00
|
|
|
// if sf.r.PkgPath != "" {
|
2021-05-21 21:50:29 +08:00
|
|
|
// panic(&UnmarshalFieldError{
|
|
|
|
// Key: key,
|
|
|
|
// Type: dict.Type(),
|
|
|
|
// Field: sf.r,
|
|
|
|
// })
|
2021-12-10 10:36:53 +08:00
|
|
|
// }
|
2018-01-27 11:31:12 +08:00
|
|
|
default:
|
2021-08-12 11:46:02 +08:00
|
|
|
err = fmt.Errorf("can't assign bencode dict items into a %v", k)
|
2021-08-12 10:16:53 +08:00
|
|
|
return
|
2018-01-27 11:31:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-23 08:50:58 +08:00
|
|
|
var (
|
|
|
|
structFieldsMu sync.Mutex
|
2021-05-21 21:50:29 +08:00
|
|
|
structFields = map[reflect.Type]map[string]dictField{}
|
2018-07-23 08:50:58 +08:00
|
|
|
)
|
|
|
|
|
2021-05-22 09:02:39 +08:00
|
|
|
func parseStructFields(struct_ reflect.Type, each func(key string, df dictField)) {
|
2021-05-21 21:50:29 +08:00
|
|
|
for _i, n := 0, struct_.NumField(); _i < n; _i++ {
|
|
|
|
i := _i
|
2018-07-23 08:50:58 +08:00
|
|
|
f := struct_.Field(i)
|
2018-07-23 09:09:42 +08:00
|
|
|
if f.Anonymous {
|
2021-05-24 15:31:52 +08:00
|
|
|
t := f.Type
|
|
|
|
if t.Kind() == reflect.Ptr {
|
|
|
|
t = t.Elem()
|
|
|
|
}
|
|
|
|
parseStructFields(t, func(key string, df dictField) {
|
2021-05-22 09:02:39 +08:00
|
|
|
innerGet := df.Get
|
|
|
|
df.Get = func(value reflect.Value) func(reflect.Value) {
|
|
|
|
anonPtr := value.Field(i)
|
2021-05-24 15:31:52 +08:00
|
|
|
if anonPtr.Kind() == reflect.Ptr && anonPtr.IsNil() {
|
2021-05-22 09:02:39 +08:00
|
|
|
anonPtr.Set(reflect.New(f.Type.Elem()))
|
2021-05-24 15:31:52 +08:00
|
|
|
anonPtr = anonPtr.Elem()
|
2021-05-22 09:02:39 +08:00
|
|
|
}
|
2021-05-24 15:31:52 +08:00
|
|
|
return innerGet(anonPtr)
|
2021-05-22 09:02:39 +08:00
|
|
|
}
|
|
|
|
each(key, df)
|
|
|
|
})
|
2018-01-27 11:31:12 +08:00
|
|
|
continue
|
|
|
|
}
|
2018-07-23 09:09:42 +08:00
|
|
|
tagStr := f.Tag.Get("bencode")
|
|
|
|
if tagStr == "-" {
|
2018-01-27 11:31:12 +08:00
|
|
|
continue
|
|
|
|
}
|
2018-07-23 09:09:42 +08:00
|
|
|
tag := parseTag(tagStr)
|
|
|
|
key := tag.Key()
|
|
|
|
if key == "" {
|
|
|
|
key = f.Name
|
2018-01-27 11:31:12 +08:00
|
|
|
}
|
2021-05-21 21:50:29 +08:00
|
|
|
each(key, dictField{f.Type, func(value reflect.Value) func(reflect.Value) {
|
|
|
|
return value.Field(i).Set
|
|
|
|
}, tag})
|
2018-07-23 08:50:58 +08:00
|
|
|
}
|
|
|
|
}
|
2018-01-27 11:31:12 +08:00
|
|
|
|
2018-07-23 08:50:58 +08:00
|
|
|
func saveStructFields(struct_ reflect.Type) {
|
2021-05-21 21:50:29 +08:00
|
|
|
m := make(map[string]dictField)
|
|
|
|
parseStructFields(struct_, func(key string, sf dictField) {
|
2018-07-23 08:50:58 +08:00
|
|
|
m[key] = sf
|
|
|
|
})
|
|
|
|
structFields[struct_] = m
|
|
|
|
}
|
2018-01-27 11:31:12 +08:00
|
|
|
|
2021-05-21 21:50:29 +08:00
|
|
|
func getStructFieldForKey(struct_ reflect.Type, key string) (f dictField) {
|
2018-07-23 08:50:58 +08:00
|
|
|
structFieldsMu.Lock()
|
|
|
|
if _, ok := structFields[struct_]; !ok {
|
|
|
|
saveStructFields(struct_)
|
2018-01-27 11:31:12 +08:00
|
|
|
}
|
2021-05-21 21:50:29 +08:00
|
|
|
f, ok := structFields[struct_][key]
|
2018-07-23 08:50:58 +08:00
|
|
|
structFieldsMu.Unlock()
|
2021-05-21 21:50:29 +08:00
|
|
|
if !ok {
|
|
|
|
var discard interface{}
|
|
|
|
return dictField{
|
|
|
|
Type: reflect.TypeOf(discard),
|
|
|
|
Get: func(reflect.Value) func(reflect.Value) { return func(reflect.Value) {} },
|
|
|
|
Tags: nil,
|
|
|
|
}
|
|
|
|
}
|
2018-01-27 11:31:12 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-25 18:46:50 +08:00
|
|
|
func (d *Decoder) parseDict(v reflect.Value) error {
|
2021-08-12 10:16:53 +08:00
|
|
|
// At this point 'd' byte was consumed, now read key/value pairs
|
2012-06-20 21:21:32 +08:00
|
|
|
for {
|
2018-01-27 11:31:12 +08:00
|
|
|
var keyStr string
|
|
|
|
keyValue := reflect.ValueOf(&keyStr).Elem()
|
|
|
|
ok, err := d.parseValue(keyValue)
|
2018-01-25 18:46:50 +08:00
|
|
|
if err != nil {
|
2021-08-12 10:16:53 +08:00
|
|
|
return fmt.Errorf("error parsing dict key: %w", err)
|
2018-01-25 18:46:50 +08:00
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return nil
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
|
|
|
|
2021-08-12 10:16:53 +08:00
|
|
|
df, err := getDictField(v.Type(), keyStr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parsing bencode dict into %v: %w", v.Type(), err)
|
|
|
|
}
|
2012-06-20 21:21:32 +08:00
|
|
|
|
2018-01-27 11:31:12 +08:00
|
|
|
// now we need to actually parse it
|
2021-05-21 21:50:29 +08:00
|
|
|
if df.Type == nil {
|
2018-01-27 11:31:12 +08:00
|
|
|
// Discard the value, there's nowhere to put it.
|
|
|
|
var if_ interface{}
|
|
|
|
if_, ok = d.parseValueInterface()
|
|
|
|
if if_ == nil {
|
2021-05-21 21:50:29 +08:00
|
|
|
return fmt.Errorf("error parsing value for key %q", keyStr)
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
2021-05-21 21:50:29 +08:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("missing value for key %q", keyStr)
|
|
|
|
}
|
|
|
|
continue
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
2021-05-21 21:50:29 +08:00
|
|
|
setValue := reflect.New(df.Type).Elem()
|
2021-11-08 11:47:01 +08:00
|
|
|
// log.Printf("parsing into %v", setValue.Type())
|
2021-05-21 21:50:29 +08:00
|
|
|
ok, err = d.parseValue(setValue)
|
2018-01-25 18:46:50 +08:00
|
|
|
if err != nil {
|
2021-08-12 11:46:02 +08:00
|
|
|
var target *UnmarshalTypeError
|
|
|
|
if !(errors.As(err, &target) && df.Tags.IgnoreUnmarshalTypeError()) {
|
|
|
|
return fmt.Errorf("parsing value for key %q: %w", keyStr, err)
|
2018-01-27 11:31:12 +08:00
|
|
|
}
|
2018-01-25 18:46:50 +08:00
|
|
|
}
|
|
|
|
if !ok {
|
2018-01-27 11:31:12 +08:00
|
|
|
return fmt.Errorf("missing value for key %q", keyStr)
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
2021-05-21 21:50:29 +08:00
|
|
|
df.Get(v)(setValue)
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 18:46:50 +08:00
|
|
|
func (d *Decoder) parseList(v reflect.Value) error {
|
2012-06-20 21:21:32 +08:00
|
|
|
switch v.Kind() {
|
|
|
|
default:
|
2019-06-13 11:07:37 +08:00
|
|
|
// If the list is a singleton of the expected type, use that value. See
|
|
|
|
// https://github.com/anacrolix/torrent/issues/297.
|
|
|
|
l := reflect.New(reflect.SliceOf(v.Type()))
|
|
|
|
if err := d.parseList(l.Elem()); err != nil {
|
|
|
|
return err
|
2019-06-13 10:35:11 +08:00
|
|
|
}
|
2019-06-13 11:07:37 +08:00
|
|
|
if l.Elem().Len() != 1 {
|
|
|
|
return &UnmarshalTypeError{
|
2021-08-12 11:46:02 +08:00
|
|
|
BencodeTypeName: "list",
|
|
|
|
UnmarshalTargetType: v.Type(),
|
2019-06-13 11:07:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
v.Set(l.Elem().Index(0))
|
|
|
|
return nil
|
|
|
|
case reflect.Array, reflect.Slice:
|
|
|
|
// We can work with this. Normal case, fallthrough.
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
i := 0
|
2018-01-25 18:46:50 +08:00
|
|
|
for ; ; i++ {
|
2012-06-20 21:21:32 +08: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 18:46:50 +08:00
|
|
|
ok, err := d.parseValue(v.Index(i))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
2012-06-20 21:21:32 +08:00
|
|
|
} else {
|
2018-01-25 18:46:50 +08:00
|
|
|
_, ok := d.parseValueInterface()
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
2012-06-20 21:21:32 +08: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 18:46:50 +08:00
|
|
|
return nil
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
|
|
|
|
2016-08-26 11:13:30 +08:00
|
|
|
func (d *Decoder) readOneValue() bool {
|
2016-06-15 12:51:58 +08:00
|
|
|
b, err := d.r.ReadByte()
|
2012-06-28 04:21:26 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if b == 'e' {
|
2016-06-15 12:51:58 +08:00
|
|
|
d.r.UnreadByte()
|
2012-06-28 04:21:26 +08:00
|
|
|
return false
|
|
|
|
} else {
|
2017-11-05 12:42:37 +08:00
|
|
|
d.Offset++
|
2012-06-28 04:21:26 +08:00
|
|
|
d.buf.WriteByte(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch b {
|
|
|
|
case 'd', 'l':
|
|
|
|
// read until there is nothing to read
|
2016-08-26 11:13:30 +08:00
|
|
|
for d.readOneValue() {
|
2014-03-19 22:54:18 +08:00
|
|
|
}
|
2012-06-28 04:21:26 +08:00
|
|
|
// consume 'e' as well
|
2016-08-26 11:13:30 +08:00
|
|
|
b = d.readByte()
|
2012-06-28 04:21:26 +08:00
|
|
|
d.buf.WriteByte(b)
|
|
|
|
case 'i':
|
2016-08-26 11:13:30 +08:00
|
|
|
d.readUntil('e')
|
2012-06-28 04:21:26 +08:00
|
|
|
d.buf.WriteString("e")
|
|
|
|
default:
|
|
|
|
if b >= '0' && b <= '9' {
|
|
|
|
start := d.buf.Len() - 1
|
2016-08-26 11:13:30 +08:00
|
|
|
d.readUntil(':')
|
2018-07-23 11:12:14 +08:00
|
|
|
length, err := strconv.ParseInt(bytesAsString(d.buf.Bytes()[start:]), 10, 64)
|
2017-11-05 12:42:37 +08:00
|
|
|
checkForIntParseError(err, d.Offset-1)
|
2012-06-28 04:21:26 +08:00
|
|
|
|
|
|
|
d.buf.WriteString(":")
|
2016-06-15 12:51:58 +08:00
|
|
|
n, err := io.CopyN(&d.buf, d.r, length)
|
2017-11-05 12:42:37 +08:00
|
|
|
d.Offset += n
|
2012-06-28 04:21:26 +08:00
|
|
|
if err != nil {
|
2017-11-05 12:42:37 +08:00
|
|
|
checkForUnexpectedEOF(err, d.Offset)
|
2012-06-28 04:21:26 +08:00
|
|
|
panic(&SyntaxError{
|
2017-11-05 12:42:37 +08:00
|
|
|
Offset: d.Offset,
|
2014-11-19 12:08:08 +08:00
|
|
|
What: errors.New("unexpected I/O error: " + err.Error()),
|
2012-06-28 04:21:26 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2017-11-05 12:42:37 +08:00
|
|
|
d.raiseUnknownValueType(b, d.Offset-1)
|
2012-06-28 04:21:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-08-26 11:13:30 +08:00
|
|
|
func (d *Decoder) parseUnmarshaler(v reflect.Value) bool {
|
2018-07-23 11:12:14 +08:00
|
|
|
if !v.Type().Implements(unmarshalerType) {
|
|
|
|
if v.Addr().Type().Implements(unmarshalerType) {
|
|
|
|
v = v.Addr()
|
|
|
|
} else {
|
|
|
|
return false
|
2012-06-28 04:21:26 +08:00
|
|
|
}
|
|
|
|
}
|
2018-07-23 11:12:14 +08:00
|
|
|
d.buf.Reset()
|
|
|
|
if !d.readOneValue() {
|
|
|
|
return false
|
2012-06-28 04:21:26 +08:00
|
|
|
}
|
2018-07-23 11:12:14 +08:00
|
|
|
m := v.Interface().(Unmarshaler)
|
|
|
|
err := m.UnmarshalBencode(d.buf.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
panic(&UnmarshalerError{v.Type(), err})
|
|
|
|
}
|
|
|
|
return true
|
2012-06-28 04:21:26 +08:00
|
|
|
}
|
|
|
|
|
2015-10-01 22:13:43 +08: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 18:46:50 +08:00
|
|
|
func (d *Decoder) parseValue(v reflect.Value) (bool, error) {
|
2012-06-28 04:21:26 +08:00
|
|
|
// we support one level of indirection at the moment
|
|
|
|
if v.Kind() == reflect.Ptr {
|
2012-06-20 21:21:32 +08:00
|
|
|
// if the pointer is nil, allocate a new element of the type it
|
|
|
|
// points to
|
2012-06-28 04:21:26 +08:00
|
|
|
if v.IsNil() {
|
|
|
|
v.Set(reflect.New(v.Type().Elem()))
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
2012-06-28 04:21:26 +08:00
|
|
|
v = v.Elem()
|
|
|
|
}
|
|
|
|
|
2016-08-26 11:13:30 +08:00
|
|
|
if d.parseUnmarshaler(v) {
|
2018-01-25 18:46:50 +08:00
|
|
|
return true, nil
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
|
|
|
|
2012-06-28 04:21:26 +08:00
|
|
|
// common case: interface{}
|
|
|
|
if v.Kind() == reflect.Interface && v.NumMethod() == 0 {
|
2016-08-26 11:13:30 +08:00
|
|
|
iface, _ := d.parseValueInterface()
|
2012-06-20 21:21:32 +08:00
|
|
|
v.Set(reflect.ValueOf(iface))
|
2018-01-25 18:46:50 +08:00
|
|
|
return true, nil
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
|
|
|
|
2016-06-15 12:51:58 +08:00
|
|
|
b, err := d.r.ReadByte()
|
2012-06-20 21:21:32 +08:00
|
|
|
if err != nil {
|
2022-01-07 16:06:33 +08:00
|
|
|
return false, err
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
2017-11-05 12:42:37 +08:00
|
|
|
d.Offset++
|
2012-06-20 21:21:32 +08:00
|
|
|
|
|
|
|
switch b {
|
|
|
|
case 'e':
|
2018-01-25 18:46:50 +08:00
|
|
|
return false, nil
|
2012-06-20 21:21:32 +08:00
|
|
|
case 'd':
|
2018-01-25 18:46:50 +08:00
|
|
|
return true, d.parseDict(v)
|
2012-06-20 21:21:32 +08:00
|
|
|
case 'l':
|
2018-01-25 18:46:50 +08:00
|
|
|
return true, d.parseList(v)
|
2012-06-20 21:21:32 +08:00
|
|
|
case 'i':
|
2021-08-12 11:46:02 +08:00
|
|
|
return true, d.parseInt(v)
|
2012-06-20 21:21:32 +08:00
|
|
|
default:
|
|
|
|
if b >= '0' && b <= '9' {
|
2018-04-13 15:07:19 +08:00
|
|
|
// It's a string.
|
|
|
|
d.buf.Reset()
|
2018-07-23 08:50:58 +08:00
|
|
|
// Write the first digit of the length to the buffer.
|
2012-06-20 21:21:32 +08:00
|
|
|
d.buf.WriteByte(b)
|
2018-01-25 18:46:50 +08:00
|
|
|
return true, d.parseString(v)
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
|
|
|
|
2017-11-05 12:42:37 +08:00
|
|
|
d.raiseUnknownValueType(b, d.Offset-1)
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
2018-01-25 18:46:50 +08:00
|
|
|
panic("unreachable")
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
|
|
|
|
2015-11-03 20:21:10 +08:00
|
|
|
// An unknown bencode type character was encountered.
|
2016-06-15 13:00:51 +08:00
|
|
|
func (d *Decoder) raiseUnknownValueType(b byte, offset int64) {
|
2015-11-03 20:21:10 +08:00
|
|
|
panic(&SyntaxError{
|
|
|
|
Offset: offset,
|
|
|
|
What: fmt.Errorf("unknown value type %+q", b),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-08-26 11:13:30 +08:00
|
|
|
func (d *Decoder) parseValueInterface() (interface{}, bool) {
|
2016-06-15 12:51:58 +08:00
|
|
|
b, err := d.r.ReadByte()
|
2012-06-20 21:21:32 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-11-05 12:42:37 +08:00
|
|
|
d.Offset++
|
2012-06-20 21:21:32 +08:00
|
|
|
|
|
|
|
switch b {
|
|
|
|
case 'e':
|
|
|
|
return nil, false
|
|
|
|
case 'd':
|
2016-08-26 11:13:30 +08:00
|
|
|
return d.parseDictInterface(), true
|
2012-06-20 21:21:32 +08:00
|
|
|
case 'l':
|
2016-08-26 11:13:30 +08:00
|
|
|
return d.parseListInterface(), true
|
2012-06-20 21:21:32 +08:00
|
|
|
case 'i':
|
2016-08-26 11:13:30 +08:00
|
|
|
return d.parseIntInterface(), true
|
2012-06-20 21:21:32 +08:00
|
|
|
default:
|
|
|
|
if b >= '0' && b <= '9' {
|
|
|
|
// string
|
|
|
|
// append first digit of the length to the buffer
|
|
|
|
d.buf.WriteByte(b)
|
2016-08-26 11:13:30 +08:00
|
|
|
return d.parseStringInterface(), true
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
|
|
|
|
2017-11-05 12:42:37 +08:00
|
|
|
d.raiseUnknownValueType(b, d.Offset-1)
|
2015-11-03 20:21:10 +08:00
|
|
|
panic("unreachable")
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-10 10:36:53 +08:00
|
|
|
// Called after 'i', for an arbitrary integer size.
|
2016-08-26 11:13:30 +08:00
|
|
|
func (d *Decoder) parseIntInterface() (ret interface{}) {
|
2017-11-05 12:42:37 +08:00
|
|
|
start := d.Offset - 1
|
2012-06-20 21:21:32 +08:00
|
|
|
|
2021-12-10 10:36:53 +08:00
|
|
|
if err := d.readInt(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2012-06-20 21:21:32 +08:00
|
|
|
n, err := strconv.ParseInt(d.buf.String(), 10, 64)
|
2015-09-20 19:08:42 +08: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 11:13:30 +08:00
|
|
|
checkForIntParseError(err, start)
|
2015-09-20 19:08:42 +08:00
|
|
|
ret = n
|
|
|
|
}
|
|
|
|
|
2012-06-20 21:21:32 +08:00
|
|
|
d.buf.Reset()
|
2015-09-20 19:08:42 +08:00
|
|
|
return
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
|
|
|
|
2021-12-10 12:10:26 +08:00
|
|
|
func (d *Decoder) readBytes(length int) []byte {
|
|
|
|
b, err := io.ReadAll(io.LimitReader(d.r, int64(length)))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if len(b) != length {
|
|
|
|
panic(fmt.Errorf("read %v bytes expected %v", len(b), length))
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2021-09-29 07:50:01 +08:00
|
|
|
func (d *Decoder) parseStringInterface() string {
|
2021-12-10 10:36:53 +08:00
|
|
|
length, err := d.parseStringLength()
|
2012-06-20 21:21:32 +08:00
|
|
|
if err != nil {
|
2021-12-10 10:36:53 +08:00
|
|
|
panic(err)
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
2021-12-10 12:10:26 +08:00
|
|
|
b := d.readBytes(int(length))
|
|
|
|
d.Offset += int64(len(b))
|
2021-09-29 07:50:01 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(&SyntaxError{Offset: d.Offset, What: err})
|
2021-09-29 07:30:35 +08:00
|
|
|
}
|
2021-09-29 07:50:01 +08:00
|
|
|
return bytesAsString(b)
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
|
|
|
|
2016-08-26 11:13:30 +08:00
|
|
|
func (d *Decoder) parseDictInterface() interface{} {
|
2012-06-20 21:21:32 +08:00
|
|
|
dict := make(map[string]interface{})
|
2022-01-07 16:11:37 +08:00
|
|
|
var lastKey string
|
|
|
|
lastKeyOk := false
|
2012-06-20 21:21:32 +08:00
|
|
|
for {
|
2021-12-10 11:05:00 +08:00
|
|
|
start := d.Offset
|
2016-08-26 11:13:30 +08:00
|
|
|
keyi, ok := d.parseValueInterface()
|
2012-06-20 21:21:32 +08:00
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
key, ok := keyi.(string)
|
|
|
|
if !ok {
|
|
|
|
panic(&SyntaxError{
|
2017-11-05 12:42:37 +08:00
|
|
|
Offset: d.Offset,
|
2014-11-19 12:08:08 +08:00
|
|
|
What: errors.New("non-string key in a dict"),
|
2012-06-20 21:21:32 +08:00
|
|
|
})
|
|
|
|
}
|
2022-01-07 16:11:37 +08:00
|
|
|
if lastKeyOk && key <= lastKey {
|
2021-12-10 11:05:00 +08:00
|
|
|
d.throwSyntaxError(start, fmt.Errorf("dict keys unsorted: %q <= %q", key, lastKey))
|
|
|
|
}
|
|
|
|
start = d.Offset
|
2016-08-26 11:13:30 +08:00
|
|
|
valuei, ok := d.parseValueInterface()
|
2012-06-20 21:21:32 +08:00
|
|
|
if !ok {
|
2021-12-10 11:05:00 +08:00
|
|
|
d.throwSyntaxError(start, fmt.Errorf("dict elem missing value [key=%v]", key))
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
|
|
|
|
2021-12-10 11:05:00 +08:00
|
|
|
lastKey = key
|
2022-01-07 16:11:37 +08:00
|
|
|
lastKeyOk = true
|
2012-06-20 21:21:32 +08:00
|
|
|
dict[key] = valuei
|
|
|
|
}
|
|
|
|
return dict
|
|
|
|
}
|
|
|
|
|
2021-09-18 10:42:20 +08:00
|
|
|
func (d *Decoder) parseListInterface() (list []interface{}) {
|
|
|
|
list = []interface{}{}
|
|
|
|
valuei, ok := d.parseValueInterface()
|
|
|
|
for ok {
|
2012-06-20 21:21:32 +08:00
|
|
|
list = append(list, valuei)
|
2021-09-18 10:42:20 +08:00
|
|
|
valuei, ok = d.parseValueInterface()
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
2021-09-18 10:42:20 +08:00
|
|
|
return
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
2022-01-07 16:05:03 +08:00
|
|
|
|
|
|
|
func (d *Decoder) getMaxStrLen() int64 {
|
|
|
|
if d.MaxStrLen == 0 {
|
|
|
|
return DefaultDecodeMaxStrLen
|
|
|
|
}
|
|
|
|
return d.MaxStrLen
|
|
|
|
}
|