bencode

package module
v0.0.0-...-0300d74 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

README

bencode

License Language version Go report Go Reference

bencode (pronounced like Bee-encode) is the encoding used by the peer-to-peer file sharing system BitTorrent for storing and transmitting loosely structured data.

About opencave

opencave is an open source organization focusing on peer-to-peer solutions, find more information here

Install

go get github.com/opencave/bencode

Usage

bencode.Marshal take a object and returns a byte slice. bencode.Unmarshal take a byte slice and bind data pointer.

import "github.com/opencave/bencode"

func main() {
	type FileInfo struct {
		Name       string  `bencode:"name"`
		Size       int     `bencode:"size"`
		FloatValue float64 // will be ignored, bencode not support float64 datatype
	}

	type Torrent struct {
		Announce string     `bencode:"announce"`
		Files    []FileInfo `bencode:"files"`
		Created  int64      `bencode:"-"`
	}

	tor := Torrent{
		Announce: "http://tracker",
		Files: []FileInfo{
			{Name: "file1.txt", Size: 1024, FloatValue: 1.0},
		},
	}
	data, err := Marshal(tor)
	if err != nil {
	  // do something
	}
	fmt.Println(string(data)) // d8:announce14:http://tracker5:filesld4:name9:file1.txt4:sizei1024eeee

	var decoded Torrent
	err = Unmarshal(data, &decoded)
	if err != nil {
	  // do something
	}
	fmt.Printf("announce: %s\n", tor.Announce) // announce: http://tracker
}

License

openHoles is licensed under the Apache License 2.0. Refer to LICENSE for more details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

func Marshal(v any) ([]byte, error)

Marshal encode any object to bencode data.

func Unmarshal

func Unmarshal(data []byte, v any) error

Unmarshal decode bencode data to the target object

Types

This section is empty.

Jump to

Keyboard shortcuts

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