bencode

package module
v0.0.15 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2024 License: MIT Imports: 4 Imported by: 1

README

go-bencode

GitHub tag (latest SemVer) Go Reference

Decoding and encoding bencode.

Support All go type including map/slice/struct/array, and simple type like bool/int/uint/string/....

float32 and float64 are not supported, bencode doesn't have this type.

Encoding and decoding some type from standard library like time.Time, net.IP are not supported. If you have any thought about how to support these types, please create an issue.

Or you can wrap these types and implement bencode.Marshaler or bencode.Unmarshaler

Supported and tested go version

  • 1.22

Install

go get github.com/trim21/go-bencode

Usage

See examples

Marshal

If you want to encode customize type as struct field with omitempty, do implement both bencode.Marshaler and bencode.IsZeroValue, so encoder could know if it's a empty value and skip fields.

Bencode doesn't have null type, so all struct field with pointer type(*T) will get omitempty by default.

Unmarshal

go any type will be decoded as map[string]any, []any, int64 or string.

[]uint8([]byte) and [N]uint8([N]byte) will be decoded as bencode string.

Decode Go string may not be valid utf8 string.

Go Array will be decoded with size check. Only bencode string/list with same length are valid, otherwise it will return a error.

Note

go reflect package allow you to create dynamic struct with reflect.StructOf, but please use it with caution.

For performance, this package will try to "compile" input type to a static encoder/decoder at first time and cache it for future use.

So a dynamic struct may cause memory leak.

License

MIT License

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendBytes added in v0.0.13

func AppendBytes(b []byte, s []byte) []byte

func AppendInt added in v0.0.13

func AppendInt(b []byte, i int64) []byte

func AppendStr added in v0.0.13

func AppendStr(b []byte, s string) []byte

func Marshal

func Marshal(v any) ([]byte, error)
Example
type User struct {
	ID   uint32 `bencode:"id,string"`
	Name string `bencode:"name"`
}

type Inner struct {
	V int    `bencode:"v"`
	S string `bencode:"a long string name replace field name"`
}

type With struct {
	Users   []User `bencode:"users,omitempty"`
	Obj     Inner  `bencode:"obj"`
	Ignored bool   `bencode:"-"`
}

var data = With{
	Users: []User{
		{ID: 1, Name: "sai"},
		{ID: 2, Name: "trim21"},
	},
	Obj: Inner{V: 2, S: "vvv"},
}
var b, err = bencode.Marshal(data)
if err != nil {
	panic(err)
}

fmt.Println(string(b))
Output:

d3:objd37:a long string name replace field name3:vvv1:vi2ee5:usersld2:idi1e4:name3:saied2:idi2e4:name6:trim21eee

func Unmarshal

func Unmarshal(data []byte, v any) error
Example
var v struct {
	S     map[[20]byte]struct{} `bencode:"s"`
	Value map[string]string     `bencode:"value" json:"value"`
}
raw := `d5:valued4:five1:5ee`

err := bencode.Unmarshal([]byte(raw), &v)
if err != nil {
	panic(err)
}

fmt.Println(v.Value["five"])
Output:

5

Types

type Encoder added in v0.0.8

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

func NewEncoder added in v0.0.9

func NewEncoder(w io.Writer) *Encoder

func (*Encoder) Encode added in v0.0.8

func (e *Encoder) Encode(v any) error

type IsZeroValue added in v0.0.4

type IsZeroValue interface {
	// IsZeroBencodeValue enable support for omitempty feature.
	// if it's being used as struct field, and it returns true, this field will be skipped.
	IsZeroBencodeValue() bool
}

IsZeroValue add support type implements Marshaler and omitempty

var s struct {
	Field T `bencode:"field,omitempty"`
}

if `T` implements Marshaler, it can implement IsZeroValue so bencode know if it's

type Marshaler

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

Marshaler allow users to implement its own encoder.

type RawBytes added in v0.0.3

type RawBytes []byte

func (RawBytes) IsZeroBencodeValue added in v0.0.4

func (b RawBytes) IsZeroBencodeValue() bool

func (RawBytes) MarshalBencode added in v0.0.3

func (b RawBytes) MarshalBencode() ([]byte, error)

func (*RawBytes) UnmarshalBencode added in v0.0.3

func (b *RawBytes) UnmarshalBencode(bytes []byte) error

type Unmarshaler

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

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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