bencode

package
v1.58.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 17, 2024 License: MPL-2.0 Imports: 14 Imported by: 1,470

README

Bencode encoding/decoding sub package. Uses similar API design to Go's json package.

Install

go get github.com/anacrolix/torrent

Usage

package demo

import (
	bencode "github.com/anacrolix/torrent/bencode"
)

type Message struct {
	Query    string `json:"q,omitempty" bencode:"q,omitempty"`
}

var v Message

func main(){
	// encode
	data, err := bencode.Marshal(v)
	if err != nil {
		log.Fatal(err)
	}
	
	//decode
	err := bencode.Unmarshal(data, &v)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(v)
}

Documentation

Index

Constants

View Source
const DefaultDecodeMaxStrLen = 1<<27 - 1 // ~128MiB

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.

Variables

This section is empty.

Functions

func Marshal

func Marshal(v interface{}) ([]byte, error)

Marshal the value 'v' to the bencode form, return the result as []byte and an error if any.

func MustMarshal

func MustMarshal(v interface{}) []byte

func Unmarshal

func Unmarshal(data []byte, v interface{}) (err error)

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).

Types

type Bytes

type Bytes []byte

func (Bytes) GoString added in v1.42.0

func (me Bytes) GoString() string

func (Bytes) MarshalBencode

func (me Bytes) MarshalBencode() ([]byte, error)

func (*Bytes) UnmarshalBencode

func (me *Bytes) UnmarshalBencode(b []byte) error

type Decoder

type Decoder struct {
	// Maximum parsed bencode string length. Defaults to DefaultMaxStrLen if zero.
	MaxStrLen MaxStrLen

	// Sum of bytes used to Decode values.
	Offset int64
	// contains filtered or unexported fields
}

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

func (*Decoder) Decode

func (d *Decoder) Decode(v interface{}) (err error)

func (*Decoder) ReadEOF added in v1.57.0

func (d *Decoder) ReadEOF() error

Check for EOF in the decoder input stream. Used to assert the input ends on a clean message boundary.

type Encoder

type Encoder struct {
	// contains filtered or unexported fields
}

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

func (*Encoder) Encode

func (e *Encoder) Encode(v interface{}) (err error)

type ErrUnusedTrailingBytes

type ErrUnusedTrailingBytes struct {
	NumUnusedBytes int
}

func (ErrUnusedTrailingBytes) Error

func (me ErrUnusedTrailingBytes) Error() string

type MarshalTypeError

type MarshalTypeError struct {
	Type reflect.Type
}

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.

func (*MarshalTypeError) Error

func (e *MarshalTypeError) Error() string

type Marshaler

type Marshaler interface {
	MarshalBencode() ([]byte, error)
}

Any type which implements this interface, will be marshaled using the specified method.

type MarshalerError

type MarshalerError struct {
	Type reflect.Type
	Err  error
}

A non-nil error was returned after calling MarshalBencode on a type which implements the Marshaler interface.

func (*MarshalerError) Error

func (e *MarshalerError) Error() string

type MaxStrLen added in v1.41.0

type MaxStrLen = int64

type SyntaxError

type SyntaxError struct {
	Offset int64 // location of the error
	What   error // error description
}

Malformed bencode input, unmarshaler failed to parse it.

func (*SyntaxError) Error

func (e *SyntaxError) Error() string

type UnmarshalFieldError

type UnmarshalFieldError struct {
	Key   string
	Type  reflect.Type
	Field reflect.StructField
}

Unmarshaler tried to write to an unexported (therefore unwritable) field.

func (*UnmarshalFieldError) Error

func (e *UnmarshalFieldError) Error() string

type UnmarshalInvalidArgError

type UnmarshalInvalidArgError struct {
	Type reflect.Type
}

Unmarshal argument must be a non-nil value of some pointer type.

func (*UnmarshalInvalidArgError) Error

func (e *UnmarshalInvalidArgError) Error() string

type UnmarshalTypeError

type UnmarshalTypeError struct {
	BencodeTypeName     string
	UnmarshalTargetType reflect.Type
}

Unmarshaler spotted a value that was not appropriate for a given Go value.

func (*UnmarshalTypeError) Error

func (e *UnmarshalTypeError) Error() string

This could probably be a value type, but we may already have users assuming that it's passed by pointer.

type Unmarshaler

type Unmarshaler interface {
	UnmarshalBencode([]byte) error
}

Any type which implements this interface, will be unmarshaled using the specified method.

type UnmarshalerError

type UnmarshalerError struct {
	Type reflect.Type
	Err  error
}

A non-nil error was returned after calling UnmarshalBencode on a type which implements the Unmarshaler interface.

func (*UnmarshalerError) Error

func (e *UnmarshalerError) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL