bencode

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2021 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package bencode is a library for encoding/decoding bencode data into Go data structures.

Index

Constants

View Source
const (
	// IntegerStart is the starting delimiter for a bencoded integer.
	IntegerStart byte = 'i'
	// IntegerEnd is the ending delimiter for a bencoded integer.
	IntegerEnd byte = 'e'

	// ByteStringDelimiter is the size/string delimiter for a bencoded bytestring.
	ByteStringDelimiter byte = ':'

	// ListStart is the starting delimiter for a bencoded list.
	ListStart byte = 'l'
	// ListEnd is the ending delimiter for a bencoded list.
	ListEnd byte = 'e'

	// DictStart is the starting delimiter for a bencoded dict.
	DictStart byte = 'd'
	// DictEnd is the ending delimiter for a bencoded dict.
	DictEnd byte = 'e'
)

Variables

View Source
var (
	// ErrWrongStartByte is the error returned when a wrong start
	// delimiter is found instead of the expected one
	ErrWrongStartByte = errors.New("wrong object start byte")
	// ErrUnknownType is the error returned when a non-bencode package object
	// (Integer, ByteString, List or Dict) is passed to the API
	ErrUnknownType = errors.New("unknown object type")
)

Functions

This section is empty.

Types

type ByteString

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

ByteString represents the bencode bytestring type.

func NewByteString

func NewByteString(s string) ByteString

NewByteString returns a bencode ByteString initialized with the given parameter.

func (ByteString) MarshalBinary

func (bs ByteString) MarshalBinary() ([]byte, error)

MarshalBinary satisfies the encoding.BinaryMarshaler interface to marshal a ByteString in binary form.

func (*ByteString) UnmarshalBinary

func (bs *ByteString) UnmarshalBinary(data []byte) error

UnmarshalBinary satisfies the encoding.BinaryUnmarshaler interface to unmarshal a ByteString from binary data.

func (ByteString) Value

func (bs ByteString) Value() string

Value returns a representation of the ByteString using Go standard data type string.

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

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

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

type Dict

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

Dict represents the bencode dict type.

func NewDict

func NewDict(d map[ByteString]interface{}) Dict

NewDict returns a bencode Dict initialized with the given parameter.

func (Dict) MarshalBinary

func (d Dict) MarshalBinary() ([]byte, error)

MarshalBinary satisfies the encoding.BinaryMarshaler interface to marshal a Dict in binary form.

func (*Dict) UnmarshalBinary

func (d *Dict) UnmarshalBinary(data []byte) error

UnmarshalBinary satisfies the encoding.BinaryUnmarshaler interface to unmarshal a Dict from binary data.

func (Dict) Value

func (d Dict) Value() map[string]interface{}

Value returns a representation of the Dict using Go standard data type map[string]interface{}.

type Encoder

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

Encoder writes bencode values 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(v interface{}) error

Encode writes the bencode encoding of v to the stream.

type Integer

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

Integer represents the bencode integer type.

func NewInteger

func NewInteger(i int64) Integer

NewInteger returns a bencode Integer initialized with the given parameter.

func (Integer) MarshalBinary

func (i Integer) MarshalBinary() ([]byte, error)

MarshalBinary satisfies the encoding.BinaryMarshaler interface to marshal an Integer in binary form.

func (*Integer) UnmarshalBinary

func (i *Integer) UnmarshalBinary(data []byte) error

UnmarshalBinary satisfies the encoding.BinaryUnmarshaler interface to unmarshal an Integer from binary data.

func (Integer) Value

func (i Integer) Value() int64

Value returns a representation of the Integer using Go standard data type int64.

type List

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

List represents the bencode list type.

func NewList

func NewList(l []interface{}) List

NewList returns a bencode List initialized with the given parameter.

func (List) MarshalBinary

func (l List) MarshalBinary() ([]byte, error)

MarshalBinary satisfies the encoding.BinaryMarshaler interface to marshal a List in binary form.

func (*List) UnmarshalBinary

func (l *List) UnmarshalBinary(data []byte) error

UnmarshalBinary satisfies the encoding.BinaryUnmarshaler interface to unmarshal a List from binary data.

func (List) Value

func (l List) Value() []interface{}

Value returns a representation of the List using Go standard data type []interface{}.

Jump to

Keyboard shortcuts

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