bencode

package module
v0.0.0-...-50f494b Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

README

go-bencode

TODO

  • Improve tests.
    • Simplify comparisons.

License

Apache 2.0

Documentation

Overview

Package bencode provides primitives to easily encode values in bencode format.

Index

Constants

This section is empty.

Variables

View Source
var DefaultDecoderOptions = DecoderOptions{
	MaxIntegerLength: 64 * 1024,
	MaxStringLength:  16 * 1024 * 1024,
}

Functions

This section is empty.

Types

type Decoder

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

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

func NewDecoderWithOptions

func NewDecoderWithOptions(r io.Reader, options DecoderOptions) *Decoder

func (*Decoder) Decode

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

func (*Decoder) Token

func (d *Decoder) Token() (Token, error)

Token decodes a new token.

One of the returned values is always nil. That means it returns _either_ a valid token and a nil error, or a nil token and a non-nil error.

type DecoderOptions

type DecoderOptions struct {
	// MaxIntegerLength is the maximum amount of bytes that will be used when
	// reading integer values, excluding delimiters.
	//
	// For example, `i10e` needs `MaxIntegerLength >= 2` because `10` is 2
	// bytes long.
	//
	// This value does NOT affect the parsing of strings.
	MaxIntegerLength int

	// MaxStringLength is the maximum length of the content of a string that
	// can be parsed, excluding its prefix.
	//
	// For example, `4:test` needs `MaxStringLength >= 4` becase `test` is 4
	// bytes long.
	//
	// The parsing of byte strings is NOT affected by `MaxIntegerLength`. When
	// reading a byte string's prefix, the decoder will read at most
	// `len(fmt.Sprintf("%d",MaxStringLength))` bytes for the length part, and
	// will expect a `:` byte after that.
	//
	// For example, if `MaxStringLength=9`, then `12:Lorem ipsum.` will return
	// an error because it will read at most 1 byte for the length (because
	// `len("9")==1`), but since the next byte is not a delimiter (it's a `2`
	// instead of `:`), it will stop reading right there and return an error.
	MaxStringLength int64
}

type Dictionary

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

func NewDictionary

func NewDictionary() *Dictionary

func (*Dictionary) Bencode

func (d *Dictionary) Bencode() []byte

func (*Dictionary) Get

func (d *Dictionary) Get(key String) (Value, bool)

func (*Dictionary) Remove

func (d *Dictionary) Remove(key String)

func (*Dictionary) Set

func (d *Dictionary) Set(key String, value Value)

type ErrInvalidToken

type ErrInvalidToken struct {
	Offset int64
}

func (*ErrInvalidToken) Error

func (e *ErrInvalidToken) Error() string

type ErrStringTooLong

type ErrStringTooLong struct {
	TokenOffset     int64
	NextTokenOffset int64
}

func (*ErrStringTooLong) Error

func (e *ErrStringTooLong) Error() string

type ErrUnexpectedByte

type ErrUnexpectedByte struct {
	Offset   int64
	Got      byte
	Expected byte
}

func (*ErrUnexpectedByte) Error

func (e *ErrUnexpectedByte) Error() string

type Integer

type Integer int64

func (Integer) Bencode

func (i Integer) Bencode() []byte

type List

type List []Value

func (List) Bencode

func (l List) Bencode() []byte

type String

type String []byte

func (String) Bencode

func (s String) Bencode() []byte

func (String) BencodeKey

func (s String) BencodeKey() string

type Token

type Token interface {
	Offset() int64
	Raw() []byte
}

Token is an interface holding one of the token types: TokenDictionaryStart, TokenListStart, TokenString, TokenInteger, TokenEnd.

type TokenDictionaryStart

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

func (*TokenDictionaryStart) Offset

func (t *TokenDictionaryStart) Offset() int64

func (*TokenDictionaryStart) Raw

func (t *TokenDictionaryStart) Raw() []byte

type TokenEnd

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

func (*TokenEnd) Offset

func (t *TokenEnd) Offset() int64

func (*TokenEnd) Raw

func (t *TokenEnd) Raw() []byte

type TokenInteger

type TokenInteger struct {
	Value int64
	// contains filtered or unexported fields
}

func (*TokenInteger) Offset

func (t *TokenInteger) Offset() int64

func (*TokenInteger) Raw

func (t *TokenInteger) Raw() []byte

type TokenListStart

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

func (*TokenListStart) Offset

func (t *TokenListStart) Offset() int64

func (*TokenListStart) Raw

func (t *TokenListStart) Raw() []byte

type TokenReader

type TokenReader interface {
	Token() (Token, error)
}

type TokenString

type TokenString struct {
	Value []byte
	// contains filtered or unexported fields
}

func (*TokenString) Offset

func (ts *TokenString) Offset() int64

func (*TokenString) Raw

func (ts *TokenString) Raw() []byte

type Value

type Value interface {
	Bencode() []byte
}

Jump to

Keyboard shortcuts

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