From f556bf196bf07308f024cc43c1a51dfd4c21188c Mon Sep 17 00:00:00 2001 From: Sam T Date: Sat, 29 Jun 2013 23:22:25 +0400 Subject: + Scetch basic broadcasting. --- src/Data/Bitfield.hs | 5 +++++ src/Data/Torrent.hs | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) (limited to 'src/Data') diff --git a/src/Data/Bitfield.hs b/src/Data/Bitfield.hs index 11897786..46e0a71f 100644 --- a/src/Data/Bitfield.hs +++ b/src/Data/Bitfield.hs @@ -52,6 +52,7 @@ module Data.Bitfield -- * Serialization , fromBitmap, toBitmap + , toList -- * Selection , Selector @@ -259,6 +260,10 @@ unions = foldl' union (haveNone 0) Serialization -----------------------------------------------------------------------} +-- | List all have indexes. +toList :: Bitfield -> [PieceIx] +toList Bitfield {..} = S.toList bfSet + -- | Unpack 'Bitfield' from tightly packed bit array. Note resulting -- size might be more than real bitfield size, use 'adjustSize'. fromBitmap :: ByteString -> Bitfield 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 isMultiFile MultiFile {} = True isMultiFile _ = False +slice :: Int -> Int -> ByteString -> ByteString +slice from to = B.take to . B.drop from + +-- | Extract validation hash by specified piece index. +pieceHash :: ContentInfo -> Int -> ByteString +pieceHash ci ix = slice offset size (ciPieces ci) + where + offset = ciPieceLength ci * ix + size = ciPieceLength ci + +-- | Validate piece with metainfo hash. +checkPiece :: ContentInfo -> Int -> ByteString -> Bool +checkPiece ci ix piece + = B.length piece == ciPieceLength ci + && hash piece == InfoHash (pieceHash ci ix) + -- | Read and decode a .torrent file. fromFile :: FilePath -> IO Torrent fromFile filepath = do -- cgit v1.2.3