bencode

package
v0.0.0-...-38ae272 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2023 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

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

Marshal returns the bencode of v.

func Unmarshal

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

Unmarshal parses the bencode data and stores the result in the value pointed to by v. If v is nil or not a pointer, Unmarshal returns an InvalidUnmarshalError.

Types

type Decoder

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

A Decoder reads and decodes bencode values from an input stream.

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

NewDecoder returns a new decoder that reads from r.

func (*Decoder) BytesParsed

func (dec *Decoder) BytesParsed() int

BytesParsed returns the number of bytes that have actually been parsed

func (*Decoder) Decode

func (dec *Decoder) Decode(v interface{}) error

Decode reads the next bencoded value from its input and stores it in the value pointed to by v.

func (*Decoder) DisallowUnknownFields

func (dec *Decoder) DisallowUnknownFields()

DisallowUnknownFields causes the Decoder to return an error when the destination is a struct and the input contains object keys which do not match any non-ignored, exported fields in the destination.

func (*Decoder) DisallowUnorderedKeys

func (dec *Decoder) DisallowUnorderedKeys()

DisallowUnorderedKeys will cause the decoder to fail when encountering unordered keys. The default is to not fail.

type Encoder

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

An Encoder writes bencoded objects to an output stream.

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

NewEncoder returns a new encoder that writes to w.

func (*Encoder) Encode

func (e *Encoder) Encode(val interface{}) error

Encode writes the bencoded data of val to its output stream. If an encountered value implements the Marshaler interface, its MarshalBencode method is called to produce the bencode output for this value. If no MarshalBencode method is present but the value implements encoding.TextMarshaler instead, its MarshalText method is called, which encodes the result as a bencode string. See the documentation for Decode about the conversion of Go values to bencoded data.

type InvalidUnmarshalError

type InvalidUnmarshalError struct {
	Type reflect.Type
}

An InvalidUnmarshalError describes an invalid argument passed to Unmarshal. (The argument to Unmarshal must be a non-nil pointer.)

func (*InvalidUnmarshalError) Error

func (e *InvalidUnmarshalError) Error() string

type Marshaler

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

Marshaler is the interface implemented by types that can marshal themselves into valid bencode.

type RawMessage

type RawMessage []byte

RawMessage is a raw encoded bencode value. It implements Marshaler and Unmarshaler and can be used to delay bencode decoding or precompute a bencoding.

func (RawMessage) MarshalBencode

func (m RawMessage) MarshalBencode() ([]byte, error)

MarshalBencode returns m as the bencoding of m.

func (*RawMessage) UnmarshalBencode

func (m *RawMessage) UnmarshalBencode(data []byte) error

UnmarshalBencode sets *m to a copy of data.

type SyntaxError

type SyntaxError struct {
	Offset int64 // error occurred after reading Offset bytes
	// contains filtered or unexported fields
}

A SyntaxError is a description of a bencode syntax error.

func (*SyntaxError) Error

func (e *SyntaxError) Error() string

type UnmarshalTypeError

type UnmarshalTypeError struct {
	Value  string       // description of bencode value - "integer", "string", "list", "dict"
	Type   reflect.Type // type of Go value it could not be assigned to
	Offset int64        // error occurred after reading Offset bytes
	Struct string       // name of the struct type containing the field
	Field  string       // name of the field holding the Go value
}

An UnmarshalTypeError describes a bencode value that was not appropriate for a value of a specific Go type.

func (*UnmarshalTypeError) Error

func (e *UnmarshalTypeError) Error() string

type Unmarshaler

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

Unmarshaler is the interface implemented by types that can unmarshal a bencode description of themselves.

type UnsupportedTypeError

type UnsupportedTypeError struct {
	Type reflect.Type
}

An UnsupportedTypeError is returned by Marshal when attempting to encode an unsupported value type.

func (*UnsupportedTypeError) Error

func (e *UnsupportedTypeError) Error() string

Jump to

Keyboard shortcuts

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