2
0
mirror of synced 2025-02-24 06:38:14 +00:00

Use the same info bytes from a decode, for a recode

This handles strange uses of optional info fields, like private=0
This commit is contained in:
Matt Joiner 2014-12-01 23:28:22 -06:00
parent 256d2e6530
commit 543d0e72a9

View File

@ -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)
}