6 "github.com/anacrolix/torrent/metainfo"
7 pp "github.com/anacrolix/torrent/peer_protocol"
10 type chunkSpec struct {
11 Begin, Length pp.Integer
19 func newRequest(index, begin, length pp.Integer) request {
20 return request{index, chunkSpec{begin, length}}
23 // The size in bytes of a metadata extension piece.
24 func metadataPieceSize(totalSize int, piece int) int {
25 ret := totalSize - piece*(1<<14)
32 // Return the request that would include the given offset into the torrent data.
33 func torrentOffsetRequest(torrentLength, pieceSize, chunkSize, offset int64) (
35 if offset < 0 || offset >= torrentLength {
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 {
45 torrentLeft := torrentLength - int64(r.Index)*pieceSize - int64(r.Begin)
46 if int64(r.Length) > torrentLeft {
47 r.Length = pp.Integer(torrentLeft)
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")
61 func validateInfo(info *metainfo.Info) error {
62 if len(info.Pieces)%20 != 0 {
63 return errors.New("pieces has invalid length")
65 if info.PieceLength == 0 {
66 if info.TotalLength() != 0 {
67 return errors.New("zero piece length")
70 if int((info.TotalLength()+info.PieceLength-1)/info.PieceLength) != info.NumPieces() {
71 return errors.New("piece count and file lengths are at odds")
77 func chunkIndexSpec(index int, pieceLength, chunkSize pp.Integer) chunkSpec {
78 ret := chunkSpec{pp.Integer(index) * chunkSize, chunkSize}
79 if ret.Begin+ret.Length > pieceLength {
80 ret.Length = pieceLength - ret.Begin
85 func connLessTrusted(l, r *connection) bool {
86 return l.netGoodPiecesDirtied() < r.netGoodPiecesDirtied()