2016-08-03 09:11:52 +02:00
|
|
|
package bencode
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// An Encoder writes bencoded objects to an output stream.
|
|
|
|
type Encoder struct {
|
|
|
|
w io.Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewEncoder returns a new encoder that writes to w.
|
|
|
|
func NewEncoder(w io.Writer) *Encoder {
|
|
|
|
return &Encoder{w: w}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode writes the bencoding of v to the stream.
|
|
|
|
func (enc *Encoder) Encode(v interface{}) error {
|
|
|
|
return marshal(enc.w, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Marshal returns the bencoding of v.
|
|
|
|
func Marshal(v interface{}) ([]byte, error) {
|
2016-09-07 05:40:41 +02:00
|
|
|
var buf bytes.Buffer
|
|
|
|
err := marshal(&buf, v)
|
2016-08-03 09:11:52 +02:00
|
|
|
return buf.Bytes(), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Marshaler is the interface implemented by objects that can marshal
|
|
|
|
// themselves.
|
|
|
|
type Marshaler interface {
|
|
|
|
MarshalBencode() ([]byte, error)
|
|
|
|
}
|
|
|
|
|
2016-09-07 05:40:41 +02:00
|
|
|
// marshal writes types bencoded to an io.Writer.
|
|
|
|
func marshal(w io.Writer, data interface{}) (err error) {
|
2016-08-03 09:11:52 +02:00
|
|
|
switch v := data.(type) {
|
|
|
|
case Marshaler:
|
2016-09-07 05:40:41 +02:00
|
|
|
var bencoded []byte
|
|
|
|
bencoded, err = v.MarshalBencode()
|
2016-08-03 09:11:52 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = w.Write(bencoded)
|
2016-09-07 05:40:41 +02:00
|
|
|
|
|
|
|
case []byte:
|
|
|
|
err = marshalBytes(w, v)
|
2016-08-03 09:11:52 +02:00
|
|
|
|
|
|
|
case string:
|
2016-09-07 05:40:41 +02:00
|
|
|
err = marshalString(w, v)
|
2016-08-03 09:11:52 +02:00
|
|
|
|
2016-09-07 05:40:41 +02:00
|
|
|
case []string:
|
|
|
|
err = marshalStringSlice(w, v)
|
2016-08-03 09:11:52 +02:00
|
|
|
|
2016-09-07 05:40:41 +02:00
|
|
|
case int:
|
|
|
|
err = marshalInt(w, int64(v))
|
2016-08-03 09:11:52 +02:00
|
|
|
|
|
|
|
case int16:
|
2016-09-07 05:40:41 +02:00
|
|
|
err = marshalInt(w, int64(v))
|
2016-08-03 09:11:52 +02:00
|
|
|
|
|
|
|
case int32:
|
2016-09-07 05:40:41 +02:00
|
|
|
err = marshalInt(w, int64(v))
|
2016-08-03 09:11:52 +02:00
|
|
|
|
|
|
|
case int64:
|
2016-09-07 05:40:41 +02:00
|
|
|
err = marshalInt(w, int64(v))
|
2016-08-03 09:11:52 +02:00
|
|
|
|
2016-09-07 05:40:41 +02:00
|
|
|
case uint:
|
|
|
|
err = marshalUint(w, uint64(v))
|
2016-08-03 09:11:52 +02:00
|
|
|
|
2016-09-07 05:40:41 +02:00
|
|
|
case uint16:
|
|
|
|
err = marshalUint(w, uint64(v))
|
2016-08-03 09:11:52 +02:00
|
|
|
|
2016-09-07 05:40:41 +02:00
|
|
|
case uint32:
|
|
|
|
err = marshalUint(w, uint64(v))
|
2016-08-03 09:11:52 +02:00
|
|
|
|
2016-09-07 05:40:41 +02:00
|
|
|
case uint64:
|
|
|
|
err = marshalUint(w, uint64(v))
|
2016-08-03 09:11:52 +02:00
|
|
|
|
2016-09-07 05:40:41 +02:00
|
|
|
case time.Duration: // Assume seconds
|
|
|
|
err = marshalInt(w, int64(v/time.Second))
|
2016-08-03 09:11:52 +02:00
|
|
|
|
2016-09-07 05:40:41 +02:00
|
|
|
case map[string]interface{}:
|
|
|
|
err = marshalMap(w, v)
|
2016-08-03 09:11:52 +02:00
|
|
|
|
|
|
|
case []interface{}:
|
2016-09-07 05:40:41 +02:00
|
|
|
err = marshalList(w, v)
|
2016-08-03 09:11:52 +02:00
|
|
|
|
|
|
|
default:
|
2016-09-27 23:56:09 +02:00
|
|
|
return fmt.Errorf("attempted to marshal unsupported type:\n%T", v)
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
|
2016-09-07 05:40:41 +02:00
|
|
|
return err
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
|
2016-09-07 05:40:41 +02:00
|
|
|
func marshalInt(w io.Writer, v int64) error {
|
|
|
|
if _, err := w.Write([]byte{'i'}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := w.Write([]byte(strconv.FormatInt(v, 10))); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := w.Write([]byte{'e'})
|
|
|
|
return err
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
|
2016-09-07 05:40:41 +02:00
|
|
|
func marshalUint(w io.Writer, v uint64) error {
|
|
|
|
if _, err := w.Write([]byte{'i'}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := w.Write([]byte(strconv.FormatUint(v, 10))); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := w.Write([]byte{'e'})
|
|
|
|
return err
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
|
2016-09-07 05:40:41 +02:00
|
|
|
func marshalBytes(w io.Writer, v []byte) error {
|
|
|
|
if _, err := w.Write([]byte(strconv.Itoa(len(v)))); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := w.Write([]byte{':'}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := w.Write(v)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func marshalString(w io.Writer, v string) error {
|
|
|
|
return marshalBytes(w, []byte(v))
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
|
2016-09-07 05:40:41 +02:00
|
|
|
func marshalStringSlice(w io.Writer, v []string) error {
|
|
|
|
if _, err := w.Write([]byte{'l'}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, val := range v {
|
|
|
|
if err := marshal(w, val); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := w.Write([]byte{'e'})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func marshalList(w io.Writer, v []interface{}) error {
|
|
|
|
if _, err := w.Write([]byte{'l'}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, val := range v {
|
|
|
|
if err := marshal(w, val); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := w.Write([]byte{'e'})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func marshalMap(w io.Writer, v map[string]interface{}) error {
|
|
|
|
if _, err := w.Write([]byte{'d'}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, val := range v {
|
|
|
|
if err := marshalString(w, key); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := marshal(w, val); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := w.Write([]byte{'e'})
|
|
|
|
return err
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|