2012-06-20 21:21:32 +08:00
|
|
|
package bencode
|
|
|
|
|
2014-11-19 12:08:08 +08:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"reflect"
|
2018-07-10 10:20:36 +08:00
|
|
|
|
|
|
|
"github.com/anacrolix/missinggo/expect"
|
2014-11-19 12:08:08 +08:00
|
|
|
)
|
2012-06-22 21:34:04 +08:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// Errors
|
|
|
|
//----------------------------------------------------------------------------
|
2012-06-20 21:21:32 +08:00
|
|
|
|
2012-07-01 00:26:20 +08:00
|
|
|
// In case if marshaler cannot encode a type, it will return this error. Typical
|
|
|
|
// example of such type is float32/float64 which has no bencode representation.
|
2012-06-20 21:21:32 +08:00
|
|
|
type MarshalTypeError struct {
|
|
|
|
Type reflect.Type
|
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (e *MarshalTypeError) Error() string {
|
|
|
|
return "bencode: unsupported type: " + e.Type.String()
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal argument must be a non-nil value of some pointer type.
|
|
|
|
type UnmarshalInvalidArgError struct {
|
|
|
|
Type reflect.Type
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *UnmarshalInvalidArgError) Error() string {
|
|
|
|
if e.Type == nil {
|
|
|
|
return "bencode: Unmarshal(nil)"
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.Type.Kind() != reflect.Ptr {
|
|
|
|
return "bencode: Unmarshal(non-pointer " + e.Type.String() + ")"
|
|
|
|
}
|
|
|
|
return "bencode: Unmarshal(nil " + e.Type.String() + ")"
|
|
|
|
}
|
|
|
|
|
2012-07-01 00:26:20 +08:00
|
|
|
// Unmarshaler spotted a value that was not appropriate for a given Go value.
|
2012-06-20 21:21:32 +08:00
|
|
|
type UnmarshalTypeError struct {
|
2021-08-12 11:46:02 +08:00
|
|
|
BencodeTypeName string
|
|
|
|
UnmarshalTargetType reflect.Type
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
|
|
|
|
2021-08-12 11:46:02 +08:00
|
|
|
// This could probably be a value type, but we may already have users assuming
|
|
|
|
// that it's passed by pointer.
|
2012-06-20 21:21:32 +08:00
|
|
|
func (e *UnmarshalTypeError) Error() string {
|
2021-08-12 11:46:02 +08:00
|
|
|
return fmt.Sprintf(
|
|
|
|
"can't unmarshal a bencode %v into a %v",
|
|
|
|
e.BencodeTypeName,
|
|
|
|
e.UnmarshalTargetType,
|
|
|
|
)
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshaler tried to write to an unexported (therefore unwritable) field.
|
|
|
|
type UnmarshalFieldError struct {
|
|
|
|
Key string
|
|
|
|
Type reflect.Type
|
|
|
|
Field reflect.StructField
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *UnmarshalFieldError) Error() string {
|
|
|
|
return "bencode: key \"" + e.Key + "\" led to an unexported field \"" +
|
|
|
|
e.Field.Name + "\" in type: " + e.Type.String()
|
|
|
|
}
|
|
|
|
|
2012-07-01 00:26:20 +08:00
|
|
|
// Malformed bencode input, unmarshaler failed to parse it.
|
2012-06-20 21:21:32 +08:00
|
|
|
type SyntaxError struct {
|
2014-11-19 12:08:08 +08:00
|
|
|
Offset int64 // location of the error
|
|
|
|
What error // error description
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *SyntaxError) Error() string {
|
2014-11-19 12:08:08 +08:00
|
|
|
return fmt.Sprintf("bencode: syntax error (offset: %d): %s", e.Offset, e.What)
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
|
|
|
|
2012-07-01 00:26:20 +08:00
|
|
|
// A non-nil error was returned after calling MarshalBencode on a type which
|
|
|
|
// implements the Marshaler interface.
|
2012-06-24 18:59:04 +08:00
|
|
|
type MarshalerError struct {
|
|
|
|
Type reflect.Type
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *MarshalerError) Error() string {
|
|
|
|
return "bencode: error calling MarshalBencode for type " + e.Type.String() + ": " + e.Err.Error()
|
|
|
|
}
|
|
|
|
|
2012-07-01 00:26:20 +08:00
|
|
|
// A non-nil error was returned after calling UnmarshalBencode on a type which
|
|
|
|
// implements the Unmarshaler interface.
|
|
|
|
type UnmarshalerError struct {
|
|
|
|
Type reflect.Type
|
2014-11-19 12:08:08 +08:00
|
|
|
Err error
|
2012-07-01 00:26:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *UnmarshalerError) Error() string {
|
|
|
|
return "bencode: error calling UnmarshalBencode for type " + e.Type.String() + ": " + e.Err.Error()
|
|
|
|
}
|
|
|
|
|
2012-06-22 21:34:04 +08:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// Interfaces
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
2012-07-01 00:26:20 +08:00
|
|
|
// Any type which implements this interface, will be marshaled using the
|
|
|
|
// specified method.
|
2012-06-22 21:34:04 +08:00
|
|
|
type Marshaler interface {
|
|
|
|
MarshalBencode() ([]byte, error)
|
|
|
|
}
|
|
|
|
|
2012-07-01 00:26:20 +08:00
|
|
|
// Any type which implements this interface, will be unmarshaled using the
|
|
|
|
// specified method.
|
2012-06-22 21:34:04 +08:00
|
|
|
type Unmarshaler interface {
|
|
|
|
UnmarshalBencode([]byte) error
|
|
|
|
}
|
|
|
|
|
2018-07-23 08:50:18 +08:00
|
|
|
// Marshal the value 'v' to the bencode form, return the result as []byte and
|
|
|
|
// an error if any.
|
2012-06-20 21:21:32 +08:00
|
|
|
func Marshal(v interface{}) ([]byte, error) {
|
|
|
|
var buf bytes.Buffer
|
2018-07-23 08:50:18 +08:00
|
|
|
e := Encoder{w: &buf}
|
2016-06-15 13:00:51 +08:00
|
|
|
err := e.Encode(v)
|
2012-06-20 21:21:32 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2012-06-24 18:59:04 +08:00
|
|
|
return buf.Bytes(), nil
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
|
|
|
|
2018-07-10 10:20:36 +08:00
|
|
|
func MustMarshal(v interface{}) []byte {
|
|
|
|
b, err := Marshal(v)
|
|
|
|
expect.Nil(err)
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2021-06-21 15:58:04 +08:00
|
|
|
// Unmarshal the bencode value in the 'data' to a value pointed by the 'v' pointer, return a non-nil
|
|
|
|
// error if any. If there are trailing bytes, this results in ErrUnusedTrailingBytes, but the value
|
|
|
|
// will be valid. It's probably more consistent to use Decoder.Decode if you want to rely on this
|
|
|
|
// behaviour (inspired by Rust's serde here).
|
2018-02-12 21:21:28 +08:00
|
|
|
func Unmarshal(data []byte, v interface{}) (err error) {
|
2021-05-05 13:48:57 +08:00
|
|
|
buf := bytes.NewReader(data)
|
2018-02-12 21:21:28 +08:00
|
|
|
e := Decoder{r: buf}
|
|
|
|
err = e.Decode(v)
|
|
|
|
if err == nil && buf.Len() != 0 {
|
|
|
|
err = ErrUnusedTrailingBytes{buf.Len()}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type ErrUnusedTrailingBytes struct {
|
|
|
|
NumUnusedBytes int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me ErrUnusedTrailingBytes) Error() string {
|
|
|
|
return fmt.Sprintf("%d unused trailing bytes", me.NumUnusedBytes)
|
2012-06-22 21:34:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDecoder(r io.Reader) *Decoder {
|
2017-11-05 12:38:09 +08:00
|
|
|
return &Decoder{r: &scanner{r: r}}
|
2012-06-22 21:34:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewEncoder(w io.Writer) *Encoder {
|
2018-07-23 08:50:18 +08:00
|
|
|
return &Encoder{w: w}
|
2012-06-22 21:34:04 +08:00
|
|
|
}
|