]> Sergey Matveev's repositories - btrtrc.git/blob - metainfo/piece.go
a68f1bce7f538c196b24dbde6bedd64005dcd1a7
[btrtrc.git] / metainfo / piece.go
1 package metainfo
2
3 import "github.com/anacrolix/missinggo"
4
5 type Piece struct {
6         Info *InfoEx
7         i    int
8 }
9
10 func (p Piece) Length() int64 {
11         if p.i == p.Info.NumPieces()-1 {
12                 return p.Info.TotalLength() - int64(p.i)*p.Info.PieceLength
13         }
14         return p.Info.PieceLength
15 }
16
17 func (p Piece) Offset() int64 {
18         return int64(p.i) * p.Info.PieceLength
19 }
20
21 func (p Piece) Hash() (ret Hash) {
22         missinggo.CopyExact(&ret, p.Info.Pieces[p.i*20:(p.i+1)*20])
23         return
24 }
25
26 func (p Piece) Index() int {
27         return p.i
28 }
29
30 func (p Piece) Key() PieceKey {
31         return PieceKey{p.Info.Hash(), p.i}
32 }
33
34 type PieceKey struct {
35         Hash  Hash
36         Index int
37 }