2016-04-04 11:48:39 +08:00
|
|
|
package metainfo
|
|
|
|
|
2018-07-12 07:15:15 +08:00
|
|
|
import (
|
2021-06-23 15:24:50 +08:00
|
|
|
"github.com/anacrolix/missinggo/v2"
|
2018-07-12 07:15:15 +08:00
|
|
|
)
|
2016-04-04 11:48:39 +08:00
|
|
|
|
|
|
|
type Piece struct {
|
2021-01-18 11:48:24 +08:00
|
|
|
Info *Info // Can we embed the fields here instead, or is it something to do with saving memory?
|
2018-07-12 07:15:15 +08:00
|
|
|
i pieceIndex
|
2016-04-04 11:48:39 +08:00
|
|
|
}
|
|
|
|
|
2018-07-12 07:15:15 +08:00
|
|
|
type pieceIndex = int
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (p Piece) Length() int64 {
|
2018-07-12 07:15:15 +08:00
|
|
|
if int(p.i) == p.Info.NumPieces()-1 {
|
2016-04-19 12:11:11 +08:00
|
|
|
return p.Info.TotalLength() - int64(p.i)*p.Info.PieceLength
|
2016-04-04 11:48:39 +08:00
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
return p.Info.PieceLength
|
2016-04-04 11:48:39 +08:00
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (p Piece) Offset() int64 {
|
|
|
|
return int64(p.i) * p.Info.PieceLength
|
2016-04-04 11:48:39 +08:00
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (p Piece) Hash() (ret Hash) {
|
2017-12-29 09:17:58 +08:00
|
|
|
missinggo.CopyExact(&ret, p.Info.Pieces[p.i*HashSize:(p.i+1)*HashSize])
|
2016-04-04 11:48:39 +08:00
|
|
|
return
|
|
|
|
}
|
2016-06-20 15:51:05 +08:00
|
|
|
|
2018-07-12 07:15:15 +08:00
|
|
|
func (p Piece) Index() pieceIndex {
|
2016-06-20 15:51:05 +08:00
|
|
|
return p.i
|
|
|
|
}
|