From 543d0e72a9e6eb833bf78c3f5d9909bcfb78b681 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Mon, 1 Dec 2014 23:28:22 -0600 Subject: [PATCH] Use the same info bytes from a decode, for a recode This handles strange uses of optional info fields, like private=0 --- metainfo/metainfo.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/metainfo/metainfo.go b/metainfo/metainfo.go index 4d7c9f68..8d205792 100644 --- a/metainfo/metainfo.go +++ b/metainfo/metainfo.go @@ -2,9 +2,10 @@ package metainfo import ( "crypto/sha1" - "github.com/anacrolix/libtorgo/bencode" "io" "os" + + "github.com/anacrolix/libtorgo/bencode" ) // Information specific to a single file inside the MetaInfo structure. @@ -53,16 +54,27 @@ type InfoEx struct { Bytes []byte } +var ( + _ bencode.Marshaler = InfoEx{} + _ bencode.Unmarshaler = &InfoEx{} +) + func (this *InfoEx) UnmarshalBencode(data []byte) error { this.Bytes = make([]byte, 0, len(data)) this.Bytes = append(this.Bytes, data...) h := sha1.New() - h.Write(this.Bytes) - this.Hash = h.Sum(this.Hash) + _, err := h.Write(this.Bytes) + if err != nil { + panic(err) + } + this.Hash = h.Sum(nil) return bencode.Unmarshal(data, &this.Info) } -func (this *InfoEx) MarshalBencode() ([]byte, error) { +func (this InfoEx) MarshalBencode() ([]byte, error) { + if this.Bytes != nil { + return this.Bytes, nil + } return bencode.Marshal(&this.Info) }