bencode

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2024 License: MIT Imports: 3 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 int, uint ...etc.

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.20
  • 1.21
  • 1.22

Install

go get github.com/trim21/go-bencode

Usage

See examples

Unmarshal

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

String may not be valid utf8 string.

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 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 Marshaler

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

Marshaler allow users to implement its own encoder. **it's return value will not be validated**, please make sure you return valid encoded bytes.

type RawMessage

type RawMessage []byte

func (RawMessage) MarshalBencode

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

func (*RawMessage) UnmarshalBencode

func (b *RawMessage) 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