bencode

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2022 License: MIT Imports: 8 Imported by: 0

README

Bencode

License Go Reference Test

bencode is a golang package for bencoding and bdecoding data from and from to equivalents.

Bencode (pronounced like Ben-code) is the encoding used by the peer-to-peer file sharing system BitTorrent for storing and transmitting loosely structured data. Wikipedia

Installation

go get github.com/ArunMurugan78/bencode

Quick Start

package main

import (
	"bytes"
	"fmt"

	"github.com/ArunMurugan78/bencode"
)

func main() {
	data := map[string]interface{}{"name": "rose", "list": []interface{}{"pickachu", "anakin", "NYC", 78}}

	encodedData, _ := bencode.Encode(data)

	fmt.Println(string(encodedData)) // d4:listl8:pickachu6:anakin3:NYCi78ee4:name4:rosee

	decodedData, _ := bencode.Parse(bytes.NewReader(encodedData))

	fmt.Println(decodedData) // map[list:[pickachu anakin NYC 78] name:rose]
}

Invalid Data?

To be able to encode a given data into bencode, it's data type has to be one of the below.

Type Description
int Can be any one of the int and uint variants.
string UTF-8 encoded strings.
[]interface{} The slice can hold any one of the valid data type.
map[string]interface{} The key has to be a string and the value can be any one of the valid data type.

License

The project is licensed under the MIT license.

Documentation

Overview

Package bencode provides utilities to read bencoded data in golang

Package bencode is a golang package for bencoding and bdecoding data from and from to equivalents.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidFormat = errors.New("invalid format")

Functions

func Encode

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

Encode encodes the provided data into bencoded bytes if valid

func Parse

func Parse(reader io.Reader) (interface{}, error)

Parse accepts an io.Reader and parses the bencoded data to return their golang equivalents

func ParseString

func ParseString(str string) (interface{}, error)

ParseString same as Parse but accepts a string to parse

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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