aboutsummaryrefslogtreecommitdiff
path: root/bencode/bencode.go
diff options
context:
space:
mode:
Diffstat (limited to 'bencode/bencode.go')
-rw-r--r--bencode/bencode.go23
1 files changed, 17 insertions, 6 deletions
diff --git a/bencode/bencode.go b/bencode/bencode.go
index 01467f9..f4f6a85 100644
--- a/bencode/bencode.go
+++ b/bencode/bencode.go
@@ -2,6 +2,7 @@ package bencode
import (
"bytes"
+ "crypto/sha1"
"errors"
"fmt"
"io"
@@ -106,17 +107,21 @@ func parseTag(tag string) (string, string) {
}
type decodeState struct {
- data []byte
- off int
+ data []byte
+ off int
+ infoOff int
+ infoEnd int
}
-func Unmarshal(data []byte, v interface{}) error {
+func Unmarshal(data []byte, v interface{}) ([]byte, error) {
val := reflect.ValueOf(v)
if val.Kind() != reflect.Ptr {
- return errors.New("non-pointer passed to Unmarshal")
+ return nil, errors.New("non-pointer passed to Unmarshal")
}
- d := decodeState{data, 0}
- return d.unmarshalField(val.Elem())
+ d := decodeState{data: data}
+ err := d.unmarshalField(val.Elem())
+ sum := sha1.Sum(d.data[d.infoOff:d.infoEnd])
+ return sum[:], err
}
func (d *decodeState) unmarshalField(v reflect.Value) error {
@@ -150,7 +155,13 @@ func (d *decodeState) unmarshalDict(v reflect.Value) {
tag := f.Tag.Get("bencode")
name, _ := parseTag(tag)
if name == key || f.Name == key {
+ if key == "info" {
+ d.infoOff = d.off
+ }
d.unmarshalField(v.Field(i))
+ if key == "info" {
+ d.infoEnd = d.off
+ }
break
}
}