]> Sergey Matveev's repositories - btrtrc.git/blob - misc.go
Merge all our global consts and vars into global.go
[btrtrc.git] / misc.go
1 package torrent
2
3 import (
4         "errors"
5
6         "github.com/anacrolix/torrent/metainfo"
7         pp "github.com/anacrolix/torrent/peer_protocol"
8 )
9
10 type chunkSpec struct {
11         Begin, Length pp.Integer
12 }
13
14 type request struct {
15         Index pp.Integer
16         chunkSpec
17 }
18
19 func newRequest(index, begin, length pp.Integer) request {
20         return request{index, chunkSpec{begin, length}}
21 }
22
23 // The size in bytes of a metadata extension piece.
24 func metadataPieceSize(totalSize int, piece int) int {
25         ret := totalSize - piece*(1<<14)
26         if ret > 1<<14 {
27                 ret = 1 << 14
28         }
29         return ret
30 }
31
32 // Return the request that would include the given offset into the torrent data.
33 func torrentOffsetRequest(torrentLength, pieceSize, chunkSize, offset int64) (
34         r request, ok bool) {
35         if offset < 0 || offset >= torrentLength {
36                 return
37         }
38         r.Index = pp.Integer(offset / pieceSize)
39         r.Begin = pp.Integer(offset % pieceSize / chunkSize * chunkSize)
40         r.Length = pp.Integer(chunkSize)
41         pieceLeft := pp.Integer(pieceSize - int64(r.Begin))
42         if r.Length > pieceLeft {
43                 r.Length = pieceLeft
44         }
45         torrentLeft := torrentLength - int64(r.Index)*pieceSize - int64(r.Begin)
46         if int64(r.Length) > torrentLeft {
47                 r.Length = pp.Integer(torrentLeft)
48         }
49         ok = true
50         return
51 }
52
53 func torrentRequestOffset(torrentLength, pieceSize int64, r request) (off int64) {
54         off = int64(r.Index)*pieceSize + int64(r.Begin)
55         if off < 0 || off >= torrentLength {
56                 panic("invalid request")
57         }
58         return
59 }
60
61 func validateInfo(info *metainfo.Info) error {
62         if len(info.Pieces)%20 != 0 {
63                 return errors.New("pieces has invalid length")
64         }
65         if int((info.TotalLength()+info.PieceLength-1)/info.PieceLength) != info.NumPieces() {
66                 return errors.New("piece count and file lengths are at odds")
67         }
68         return nil
69 }
70
71 func chunkIndexSpec(index int, pieceLength, chunkSize pp.Integer) chunkSpec {
72         ret := chunkSpec{pp.Integer(index) * chunkSize, chunkSize}
73         if ret.Begin+ret.Length > pieceLength {
74                 ret.Length = pieceLength - ret.Begin
75         }
76         return ret
77 }