aboutsummaryrefslogtreecommitdiff
path: root/bencode
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-06-15 23:40:51 +0200
committerDimitri Sokolyuk <demon@dim13.org>2016-06-15 23:40:51 +0200
commitded9f5352c3995e20af6fb61bc4c138474969657 (patch)
treec11371e47a96c93abdcf0d7c092fae049262feda /bencode
parent37efe2659706e2a2c6f378ba69b3e8cce66a928f (diff)
Generalize
Diffstat (limited to 'bencode')
-rw-r--r--bencode/bencode.go44
1 files changed, 22 insertions, 22 deletions
diff --git a/bencode/bencode.go b/bencode/bencode.go
index db980f7..0592d5b 100644
--- a/bencode/bencode.go
+++ b/bencode/bencode.go
@@ -157,6 +157,21 @@ func (d *decodeState) unmarshalField(v reflect.Value) error {
return nil
}
+func findKey(key string, v reflect.Value) reflect.Value {
+ if v.CanSet() {
+ t := v.Type()
+ for i := 0; i < t.NumField(); i++ {
+ f := t.Field(i)
+ tag := f.Tag.Get("bencode")
+ name, _ := parseTag(tag)
+ if key == name || key == f.Name || key == strings.ToLower(f.Name) {
+ return v.Field(i)
+ }
+ }
+ }
+ return reflect.Value{}
+}
+
func (d *decodeState) unmarshalDict(v reflect.Value) {
if d.data[d.off] != 'd' {
panic("not a dict")
@@ -164,34 +179,19 @@ 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
- var found bool
- if v.CanSet() {
- t := v.Type()
- for i := 0; i < t.NumField(); i++ {
- f := t.Field(i)
- tag := f.Tag.Get("bencode")
- name, _ := parseTag(tag)
- if key == name || key == f.Name || key == strings.ToLower(f.Name) {
- if key == "info" {
- infoOff = d.off
- }
- d.unmarshalField(v.Field(i))
- if key == "info" {
- infoEnd = d.off
- }
- found = true
- break
- }
- }
+ f := findKey(key, v)
+ if key == "info" {
+ infoOff = d.off
}
- if !found {
- d.unmarshalField(reflect.Value{})
+ d.unmarshalField(f)
+ if key == "info" {
+ infoEnd = d.off
}
}
+
if v.CanSet() {
if ih := v.FieldByName("InfoHash"); ih.IsValid() && infoEnd > infoOff {
sum := sha1.Sum(d.data[infoOff:infoEnd])