aboutsummaryrefslogtreecommitdiff
path: root/bencode/bencode.go
diff options
context:
space:
mode:
Diffstat (limited to 'bencode/bencode.go')
-rw-r--r--bencode/bencode.go25
1 files changed, 15 insertions, 10 deletions
diff --git a/bencode/bencode.go b/bencode/bencode.go
index f4f6a85..58527a0 100644
--- a/bencode/bencode.go
+++ b/bencode/bencode.go
@@ -107,21 +107,18 @@ func parseTag(tag string) (string, string) {
}
type decodeState struct {
- data []byte
- off int
- infoOff int
- infoEnd int
+ data []byte
+ off int
}
-func Unmarshal(data []byte, v interface{}) ([]byte, error) {
+func Unmarshal(data []byte, v interface{}) error {
val := reflect.ValueOf(v)
if val.Kind() != reflect.Ptr {
- return nil, errors.New("non-pointer passed to Unmarshal")
+ return errors.New("non-pointer passed to Unmarshal")
}
d := decodeState{data: data}
err := d.unmarshalField(val.Elem())
- sum := sha1.Sum(d.data[d.infoOff:d.infoEnd])
- return sum[:], err
+ return err
}
func (d *decodeState) unmarshalField(v reflect.Value) error {
@@ -146,6 +143,8 @@ func (d *decodeState) unmarshalDict(v reflect.Value) {
}
d.off++
+ var infoOff, infoEnd int
+
for d.data[d.off] != 'e' {
key, n := parseString(d.data[d.off:])
d.off += n
@@ -156,16 +155,22 @@ func (d *decodeState) unmarshalDict(v reflect.Value) {
name, _ := parseTag(tag)
if name == key || f.Name == key {
if key == "info" {
- d.infoOff = d.off
+ infoOff = d.off
}
d.unmarshalField(v.Field(i))
if key == "info" {
- d.infoEnd = d.off
+ infoEnd = d.off
}
break
}
}
}
+ if ih := v.FieldByName("InfoHash"); ih.IsValid() {
+ if infoEnd > infoOff {
+ sum := sha1.Sum(d.data[infoOff:infoEnd])
+ ih.SetBytes(sum[:])
+ }
+ }
d.off++
}