aboutsummaryrefslogtreecommitdiff
path: root/meta/torrent.go
diff options
context:
space:
mode:
Diffstat (limited to 'meta/torrent.go')
-rw-r--r--meta/torrent.go31
1 files changed, 31 insertions, 0 deletions
diff --git a/meta/torrent.go b/meta/torrent.go
index 002480c..9720b34 100644
--- a/meta/torrent.go
+++ b/meta/torrent.go
@@ -19,6 +19,11 @@ type File struct {
Path []string `bencode:"path"`
}
+type Piece struct {
+ Length int
+ CheckSum [sha1.Size]byte
+}
+
type Info struct {
Files []File `bencode:"files"`
Length int `bencode:"length"`
@@ -29,6 +34,27 @@ type Info struct {
Private bool `bencode:"private"`
}
+func (i Info) pieces() []Piece {
+ for _, f := range i.Files {
+ i.Length += f.Length
+ }
+ n := i.Length / i.PieceLength
+ l := i.Length % i.PieceLength
+ if l > 0 {
+ n++
+ }
+ p := make([]Piece, n)
+ for k := 0; k < n; k++ {
+ p[k].Length = i.PieceLength
+ if k+1 == n && l > 0 {
+ p[k].Length = l
+ }
+ off := k * sha1.Size
+ copy(p[k].CheckSum[:], i.Pieces[off:off+sha1.Size])
+ }
+ return p
+}
+
func (i Info) NPieces() int {
return len(i.Pieces) / sha1.Size
}
@@ -152,6 +178,11 @@ func (i Info) isSingleFile() bool {
// PieceCount returns count of full pieces and size of last piece
func (i Info) PieceCount() (int, int) {
+ if i.Length == 0 {
+ for _, f := range i.Files {
+ i.Length += f.Length
+ }
+ }
return i.Length / i.PieceLength, i.Length % i.PieceLength
}