summaryrefslogtreecommitdiff
path: root/src/Data/Torrent.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Data/Torrent.hs')
-rw-r--r--src/Data/Torrent.hs15
1 files changed, 11 insertions, 4 deletions
diff --git a/src/Data/Torrent.hs b/src/Data/Torrent.hs
index 551a260c..bdd38630 100644
--- a/src/Data/Torrent.hs
+++ b/src/Data/Torrent.hs
@@ -34,6 +34,8 @@ module Data.Torrent
34 , contentLength, pieceCount, blockCount 34 , contentLength, pieceCount, blockCount
35 , isSingleFile, isMultiFile 35 , isSingleFile, isMultiFile
36 36
37 , checkPiece
38
37 -- * Info hash 39 -- * Info hash
38#if defined (TESTING) 40#if defined (TESTING)
39 , InfoHash(..) 41 , InfoHash(..)
@@ -77,6 +79,9 @@ import Network.URI
77import System.FilePath 79import System.FilePath
78import Numeric 80import Numeric
79 81
82import Data.ByteString.Internal
83import Debug.Trace
84
80 85
81type Time = Text 86type Time = Text
82 87
@@ -141,6 +146,8 @@ simpleTorrent announce info = torrent announce info
141 Nothing Nothing Nothing 146 Nothing Nothing Nothing
142 Nothing Nothing 147 Nothing Nothing
143 148
149-- TODO check if pieceLength is power of 2
150
144-- | Info part of the .torrent file contain info about each content file. 151-- | Info part of the .torrent file contain info about each content file.
145data ContentInfo = 152data ContentInfo =
146 SingleFile { 153 SingleFile {
@@ -361,14 +368,14 @@ slice from to = B.take to . B.drop from
361 368
362-- | Extract validation hash by specified piece index. 369-- | Extract validation hash by specified piece index.
363pieceHash :: ContentInfo -> Int -> ByteString 370pieceHash :: ContentInfo -> Int -> ByteString
364pieceHash ci ix = slice offset size (ciPieces ci) 371pieceHash ci ix = slice (hashsize * ix) hashsize (ciPieces ci)
365 where 372 where
366 offset = ciPieceLength ci * ix 373 hashsize = 20
367 size = ciPieceLength ci
368 374
369-- | Validate piece with metainfo hash. 375-- | Validate piece with metainfo hash.
370checkPiece :: ContentInfo -> Int -> ByteString -> Bool 376checkPiece :: ContentInfo -> Int -> ByteString -> Bool
371checkPiece ci ix piece 377checkPiece ci ix piece @ (PS _ off si)
378 | traceShow (ix, off, si) True
372 = B.length piece == ciPieceLength ci 379 = B.length piece == ciPieceLength ci
373 && hash piece == InfoHash (pieceHash ci ix) 380 && hash piece == InfoHash (pieceHash ci ix)
374 381