decodebencode

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

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

Go to latest
Published: Apr 17, 2025 License: MIT Imports: 9 Imported by: 0

README

decode-bencode

A Go lang library for encoding and decoding data in bencode format.

Beencode

Get the pkg

go get -u github.com/jabakot/decode-bencode

Import pkg

import (
	decodebencode "github.com/jabakot/decode-bencode"
)

Decode bencode

input := "d6:answeri42e5:hello5:world2:hi4:mark4:jojo12:ゴゴゴゴ12:wrong-answeri-42ee"

output, err := decodebencode.DecodeBencode(input)

if (err != nil) {
    fmt.Println(err)
}

fmt.Println(output)
// map[answer:42 hello:world hi:mark jojo:ゴゴゴゴ wrong-answer:-42]

Encode int to bencode (str)

bencode := decodebencode.EncodeBencodeInteger(42)
// bencode == "i42e"

Encode str to bencode (str)

bencode1 := decodebencode.EncodeBencodeString("hi!")
// bencode1 == "3:hi!"
bencode2 := decodebencode.EncodeBencodeString("ゴゴゴゴ")
// bencode2 == "12:ゴゴゴゴ"

Encode slice/array to bencode (str)

listWithIntAndString := make([]any, 2)
listWithIntAndString[0] = 1
listWithIntAndString[1] = "hi!"

bencode := decodebencode.EncodeBencodeList(listWithIntAndString)
// bencode == "li1e3:hi!e"

Encode map to bencode (str)

dictWithStringsAndInt := make(map[string]any)
dictWithStringsAndInt["answer"] = 42
dictWithStringsAndInt["hello"] = "world"
dictWithStringsAndInt["hi"] = "mark"
dictWithStringsAndInt["jojo"] = "ゴゴゴゴ"
dictWithStringsAndInt["wrong-answer"] = -42

bencode := decodebencode.EncodeBencodeDict(listWithIntAndString)
// bencode == "d6:answeri42e5:hello5:world2:hi4:mark4:jojo12:ゴゴゴゴ12:wrong-answeri-42ee"

Documentation

Index

Constants

View Source
const (
	INT_CONTROL_SYMBOL   = 'i'
	STR_CONTROL_SYMBOL   = ':'
	LIST_CONTROL_SYMBOL  = 'l'
	DICT_CONTROL_SYMBOL  = 'd'
	CLOSE_CONTROL_SYMBOL = 'e'
)

Variables

View Source
var DICT_MARKER = dict_markerType{}

dictinoary makrer for stack

View Source
var LIST_MARKER = list_markerType{}

list makrer for stack

Functions

func DecodeBencode

func DecodeBencode(input string) (interface{}, error)

func EncodeBencodeDict

func EncodeBencodeDict(dict map[string]any) string

func EncodeBencodeInteger

func EncodeBencodeInteger(i int) string

func EncodeBencodeList

func EncodeBencodeList(list []any) string

func EncodeBencodeString

func EncodeBencodeString(s string) string

yes, length is in bytes

func FindNextRune

func FindNextRune(start int, r rune, input []rune) (int, bool)

finds next [r] rune int input, useful for

func IsDictSymbol

func IsDictSymbol(cadidate interface{}) bool

func IsListSymbol

func IsListSymbol(cadidate interface{}) bool

func ParseInt

func ParseInt(input []rune) (int, error)

func ShrinkDictionary

func ShrinkDictionary(stackSlice []interface{}) (map[string]interface{}, error)

Transforms sequence of elements in buff to map (bencode dictionary)

func ShrinkStack

func ShrinkStack(stack *DataStack) error

Types

type DataStack

type DataStack []interface{}

func (*DataStack) Pop

func (s *DataStack) Pop() (interface{}, error)

func (*DataStack) Push

func (s *DataStack) Push(v interface{})

Jump to

Keyboard shortcuts

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