aboutsummaryrefslogtreecommitdiff
path: root/bencode
diff options
context:
space:
mode:
authorFelix Hanley <felix@userspace.com.au>2018-02-09 08:42:22 +0000
committerFelix Hanley <felix@userspace.com.au>2018-02-09 08:42:22 +0000
commit2ded0704c8f675c3d92cf2b4874a32c65faf2553 (patch)
treec3b437cbe7129ea300d7a98e438b0f5fd3fd8344 /bencode
parentbe4f4b36cc7fb46c70364d524d64c7188c1ae0f3 (diff)
downloaddhtsearch-2ded0704c8f675c3d92cf2b4874a32c65faf2553.tar.gz
dhtsearch-2ded0704c8f675c3d92cf2b4874a32c65faf2553.tar.bz2
Basic DHT functions
Diffstat (limited to 'bencode')
-rw-r--r--bencode/comparison_test.go56
-rw-r--r--bencode/decode.go9
-rw-r--r--bencode/encode.go2
3 files changed, 7 insertions, 60 deletions
diff --git a/bencode/comparison_test.go b/bencode/comparison_test.go
deleted file mode 100644
index 24ded8f..0000000
--- a/bencode/comparison_test.go
+++ /dev/null
@@ -1,56 +0,0 @@
-package bencode
-
-import (
- "bytes"
- "testing"
-
- alt1 "github.com/marksamman/bencode"
-)
-
-func BenchmarkMyStringDecode(b *testing.B) {
- for n := 0; n < b.N; n++ {
- Decode([]byte("11:hello world"))
- }
-}
-
-func BenchmarkMyIntDecode(b *testing.B) {
- for n := 0; n < b.N; n++ {
- Decode([]byte("i1234234e"))
- }
-}
-
-func BenchmarkMyListDecode(b *testing.B) {
- for n := 0; n < b.N; n++ {
- Decode([]byte("l4:spam4:eggse"))
- }
-}
-
-func BenchmarkMyDictDecode(b *testing.B) {
- for n := 0; n < b.N; n++ {
- Decode([]byte("d4:spam4:eggse"))
- }
-}
-
-func BenchmarkAlt1StringDecode(b *testing.B) {
- for n := 0; n < b.N; n++ {
- alt1.Decode(bytes.NewBufferString("11:hello world"))
- }
-}
-
-func BenchmarkAlt1IntDecode(b *testing.B) {
- for n := 0; n < b.N; n++ {
- alt1.Decode(bytes.NewBufferString("i1234234e"))
- }
-}
-
-func BenchmarkAlt1ListDecode(b *testing.B) {
- for n := 0; n < b.N; n++ {
- alt1.Decode(bytes.NewBufferString("l4:spam4:eggse"))
- }
-}
-
-func BenchmarkAlt1DictDecode(b *testing.B) {
- for n := 0; n < b.N; n++ {
- alt1.Decode(bytes.NewBufferString("d4:spam4:eggse"))
- }
-}
diff --git a/bencode/decode.go b/bencode/decode.go
index f6bb468..2e356c2 100644
--- a/bencode/decode.go
+++ b/bencode/decode.go
@@ -15,20 +15,21 @@ func Decode(data []byte) (r interface{}, err error) {
// DecodeString decodes a string from a given index
// It returns the string and the position of the last character
+// or the offset at the point of error
func DecodeString(data []byte, start int) (r string, end int, err error) {
if start >= len(data) || data[start] < '0' || data[start] > '9' {
err = errors.New("bencode: invalid string length")
- return r, end, err
+ return r, 0, err
}
prefix, i, err := readUntil(data, start, ':')
if err != nil {
- return r, end, err
+ return r, i, err
}
length, err := strconv.ParseInt(string(prefix), 10, 0)
if err != nil {
- return r, end, err
+ return r, 0, err
}
end = i + int(length)
@@ -146,7 +147,6 @@ func DecodeDict(data []byte, start int) (r map[string]interface{}, end int, err
// decodeItem decodes the next type at the given index
// It returns the index of the last character decoded
func decodeItem(data []byte, start int) (r interface{}, end int, err error) {
-
switch data[start] {
case 'l':
return DecodeList(data, start)
@@ -161,6 +161,7 @@ func decodeItem(data []byte, start int) (r interface{}, end int, err error) {
// Read until the given character
// Returns the slice before the character and the index of the next character
+// or the offset at the point of error
func readUntil(data []byte, start int, c byte) ([]byte, int, error) {
i := start
for ; i < len(data); i++ {
diff --git a/bencode/encode.go b/bencode/encode.go
index 4890359..7664169 100644
--- a/bencode/encode.go
+++ b/bencode/encode.go
@@ -72,6 +72,8 @@ func EncodeList(data []interface{}) ([]byte, error) {
// EncodeItem encodes an item of dict or list.
func encodeItem(data interface{}) ([]byte, error) {
switch v := data.(type) {
+ case []byte:
+ return EncodeString(string(v))
case string:
return EncodeString(v)
case int: