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.hs16
1 files changed, 16 insertions, 0 deletions
diff --git a/src/Data/Torrent.hs b/src/Data/Torrent.hs
index 368ec77c..551a260c 100644
--- a/src/Data/Torrent.hs
+++ b/src/Data/Torrent.hs
@@ -356,6 +356,22 @@ isMultiFile :: ContentInfo -> Bool
356isMultiFile MultiFile {} = True 356isMultiFile MultiFile {} = True
357isMultiFile _ = False 357isMultiFile _ = False
358 358
359slice :: Int -> Int -> ByteString -> ByteString
360slice from to = B.take to . B.drop from
361
362-- | Extract validation hash by specified piece index.
363pieceHash :: ContentInfo -> Int -> ByteString
364pieceHash ci ix = slice offset size (ciPieces ci)
365 where
366 offset = ciPieceLength ci * ix
367 size = ciPieceLength ci
368
369-- | Validate piece with metainfo hash.
370checkPiece :: ContentInfo -> Int -> ByteString -> Bool
371checkPiece ci ix piece
372 = B.length piece == ciPieceLength ci
373 && hash piece == InfoHash (pieceHash ci ix)
374
359-- | Read and decode a .torrent file. 375-- | Read and decode a .torrent file.
360fromFile :: FilePath -> IO Torrent 376fromFile :: FilePath -> IO Torrent
361fromFile filepath = do 377fromFile filepath = do