bencode

package module
v0.0.0-...-f360d85 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2018 License: MIT Imports: 4 Imported by: 0

README

go-bencode

A Go library for handling data in the bencode format. This is the format used by the Mainline DHT and BitTorrent protocols.

Install

go get -u github.com/felix/go-bencode

Usage

All encoded data objects are []byte. This is the format returned by many network libraries such as UDPConn.Read() etc.

import bencode "github.com/felix/go-bencode"

var i interface{}
var packet []byte
var str string

i, err = bencode.Decode(packet)

// Or if you know the type
str, err = bencode.DecodeString(packet)

// Encoding a string, int, slice or map
packet, err = bencode.Encode(str)

Benchmarks

Just for fun, and a comparison.

go test -bench=.

BenchmarkFelix-4                  100000             16544 ns/op
BenchmarkMarkSamman-4              50000             32664 ns/op
BenchmarkJackpal-4                 50000             36755 ns/op

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(data []byte) (r interface{}, err error)

Decode decodes a bencoded string to string, int, list or map.

func DecodeDict

func DecodeDict(data []byte, start int) (map[string]interface{}, int, error)

DecodeDict decodes a dict as a map. It returns the map and the number of bytes successfully read.

func DecodeInt

func DecodeInt(data []byte, start int) (r int64, end int, err error)

DecodeInt decodes an integer value. It returns the integer and the number of bytes successfully read.

func DecodeList

func DecodeList(data []byte, start int) (r []interface{}, end int, err error)

DecodeList decodes a list value. It returns the array and the number of bytes successfully read.

func DecodeString

func DecodeString(data []byte, start int) (r string, n int, err error)

DecodeString decodes a string from a given offset. It returns the string, the number of bytes successfully read.

func Encode

func Encode(data interface{}) ([]byte, error)

Encode encodes a string, int, dict or list value to a bencoded string.

func EncodeDict

func EncodeDict(data map[string]interface{}) ([]byte, error)

EncodeDict encodes a dict value.

func EncodeInt

func EncodeInt(data int64) ([]byte, error)

EncodeInt encodes a int value.

func EncodeList

func EncodeList(data []interface{}) ([]byte, error)

EncodeList encodes a list value.

func EncodeString

func EncodeString(data string) ([]byte, error)

EncodeString encodes a string value.

Types

This section is empty.

Jump to

Keyboard shortcuts

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