Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bencoder implementation #1

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Go

on:
push:
branches: [ "development","main" ]
pull_request:
branches: [ "main" ]

jobs:

Check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.22.4'

- name: golangci-lint
uses: golangci/golangci-lint-action@v3

- name: gofmt
run: make format


- name: Test
run: make test
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
test:
go test -v -cover ./...

format:
go fmt ./...
42 changes: 41 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,41 @@
# bencode-nabil
# bencode-nabil

Encoder and Decode of Bencode

## Installation

As a library

```shell
go get github.com/codescalersinternships/bencode-nabil/pkg
```

## Usage

in your Go app you can do something like

```go
package main

import (
"fmt"

bencoder "github.com/codescalersinternships/bencode-nabil/pkg"
)

func main() {
s := "d3:bar3:moo4:spaml4:spam4:foooee"

ret,_ := bencoder.Decoder(s)
fmt.Println(ret)
x,_ := bencoder.Encoder(ret)
fmt.Println(string(x[:]))
}

```

## Testing

```shell
make test
```
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/codescalersinternships/bencode-nabil

go 1.22.4

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-stack/stack v1.8.1 h1:ntEHSVwIt7PNXNpgPmVfMrNhLtgjlmnZha2kOpuRiDw=
github.com/go-stack/stack v1.8.1/go.mod h1:dcoOX6HbPZSZptuspn9bctJ+N/CnF5gGygcUP3XYfe4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
231 changes: 231 additions & 0 deletions pkg/bencoder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
package bencoder

import (
"fmt"
"reflect"
"slices"
"strconv"
"strings"
)

func readInteger(idx *int, str *string, terminator string) (int64, error) {
out := ""
lim := strings.Index((*str)[*idx:], terminator)
if lim == -1 {
return -1, fmt.Errorf("%s", "error of terminator wasn't found")
}
lim = lim + (*idx)
out = (*str)[*idx:lim]
*idx = lim
integer, err := strconv.ParseInt(out, 10, 64)
if err != nil {
return -1, err
}
return integer, nil
}

func readBulkString(idx *int, str *string) (string, error) {
out := ""
length, err := readInteger(idx, str, ":")
if err != nil {
return "", err
}
if length == -1 {
return "", nil
}
(*idx)++
lim := (*idx) + int(length)
if lim > len(*str) {
return "", fmt.Errorf("%s", "error of string lenght isn't enough")
}
out = (*str)[*idx:lim]
if out == "-1" {
return "", nil
}
*idx = lim - 1
return out, nil
}

func readMap(idx *int, str *string) (map[interface{}]interface{}, error) {
var out = make(map[interface{}]interface{})
var prev interface{}
for i := 0; ; i++ {
var val interface{}
if (*str)[*idx] == 'i' {
(*idx)++
integer, err := readInteger(idx, str, "e")
if err != nil {
return nil, err
}
val = integer
(*idx)++
if i%2 == 1 {
out[prev] = val
}

prev = val
continue
}
if (*str)[*idx] >= '0' && (*str)[*idx] <= '9' {
bulkString, err := readBulkString(idx, str)
if err != nil {
return nil, err
}
val = bulkString
(*idx)++
if i%2 == 1 {
out[prev] = val
}

prev = val
continue
}
if (*str)[*idx] == 'l' {
(*idx)++
array, err := Decoder(*str, idx)
if err != nil {
return nil, err
}
val = array
(*idx)++
if i%2 == 1 {
out[prev] = val
}

prev = val
continue
}

if (*str)[*idx] == 'd' {
(*idx)++
respMap, err := readMap(idx, str)
if err != nil {
return nil, err
}
val = respMap
(*idx)++
if i%2 == 1 {
out[prev] = val
}

prev = val
continue
}
if (*str)[*idx] == 'e' {
break
}
}
return out, nil
}

// Decoder reads an bencoded string, returning an array of interfaces of items
func Decoder(str string, start ...*int) (interface{}, error) {
var out []interface{}
var idx int = 0
if len(start) > 0 {
idx = *start[0]
}
for ; idx < len(str); idx++ {
if str[idx] == 'i' {
idx++
integer, err := readInteger(&idx, &str, "e")
if err != nil {
return nil, err
}
out = append(out, integer)
continue
}
if str[idx] >= '0' && str[idx] <= '9' {
bulkString, err := readBulkString(&idx, &str)
if err != nil {
return nil, err
}
out = append(out, bulkString)
continue
}
if str[idx] == 'l' {
idx++
array, err := Decoder(str, &idx)
if err != nil {
return nil, err
}
out = append(out, array)
continue
}

if str[idx] == 'd' {
idx++
respMap, err := readMap(&idx, &str)
if err != nil {
return nil, err
}
out = append(out, respMap)
continue
}
if str[idx] == 'e' {
break
}
}
if len(start) > 0 {
*start[0] = idx
}
if len(out) == 1 {
return out[0], nil
}
return out, nil
}

// Encoder reads an interface, returning an byte array of the bencoded interface
func Encoder(benco interface{}) ([]byte, error) {

var encoded []byte

switch ty := benco.(type) {
case int, int64:
encoded = append(encoded, 'i')
encoded = append(encoded, []byte(strconv.FormatInt(reflect.ValueOf(ty).Int(), 10))...)
encoded = append(encoded, 'e')

case string:
encoded = append(encoded, []byte(strconv.Itoa(len(ty)))...)
encoded = append(encoded, ':')
encoded = append(encoded, []byte(ty)...)

case []interface{}:
encoded = append(encoded, 'l')
for _, item := range ty {
encodedItem, err := Encoder(item)
if err != nil {
return nil, err
}
encoded = append(encoded, encodedItem...)
}
encoded = append(encoded, 'e')

case map[interface{}]interface{}:
encoded = append(encoded, 'd')
var keyArr []string
for key := range ty {
keyArr = append(keyArr, key.(string))
}
slices.Sort(keyArr)
for _, key := range keyArr {
encodedKey, err := Encoder(key)
if err != nil {
return nil, err
}
encodedValue, err := Encoder(ty[key])
if err != nil {
return nil, err
}
encoded = append(encoded, encodedKey...)
encoded = append(encoded, encodedValue...)
}
encoded = append(encoded, 'e')

default:
return nil, fmt.Errorf("unsupported data type")
}

return encoded, nil
}
Loading
Loading