diff options
author | Sam T <pxqr.sta@gmail.com> | 2013-06-29 23:22:25 +0400 |
---|---|---|
committer | Sam T <pxqr.sta@gmail.com> | 2013-06-29 23:22:25 +0400 |
commit | f556bf196bf07308f024cc43c1a51dfd4c21188c (patch) | |
tree | 228de5a632e8b758d507df7ddabf7fd85d113694 /src/Data | |
parent | d4ada1b8a392d67f2835935084c5f0f3ecef2ab5 (diff) |
+ Scetch basic broadcasting.
Diffstat (limited to 'src/Data')
-rw-r--r-- | src/Data/Bitfield.hs | 5 | ||||
-rw-r--r-- | src/Data/Torrent.hs | 16 |
2 files changed, 21 insertions, 0 deletions
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 | |||
52 | 52 | ||
53 | -- * Serialization | 53 | -- * Serialization |
54 | , fromBitmap, toBitmap | 54 | , fromBitmap, toBitmap |
55 | , toList | ||
55 | 56 | ||
56 | -- * Selection | 57 | -- * Selection |
57 | , Selector | 58 | , Selector |
@@ -259,6 +260,10 @@ unions = foldl' union (haveNone 0) | |||
259 | Serialization | 260 | Serialization |
260 | -----------------------------------------------------------------------} | 261 | -----------------------------------------------------------------------} |
261 | 262 | ||
263 | -- | List all have indexes. | ||
264 | toList :: Bitfield -> [PieceIx] | ||
265 | toList Bitfield {..} = S.toList bfSet | ||
266 | |||
262 | -- | Unpack 'Bitfield' from tightly packed bit array. Note resulting | 267 | -- | Unpack 'Bitfield' from tightly packed bit array. Note resulting |
263 | -- size might be more than real bitfield size, use 'adjustSize'. | 268 | -- size might be more than real bitfield size, use 'adjustSize'. |
264 | fromBitmap :: ByteString -> Bitfield | 269 | 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 | |||
356 | isMultiFile MultiFile {} = True | 356 | isMultiFile MultiFile {} = True |
357 | isMultiFile _ = False | 357 | isMultiFile _ = False |
358 | 358 | ||
359 | slice :: Int -> Int -> ByteString -> ByteString | ||
360 | slice from to = B.take to . B.drop from | ||
361 | |||
362 | -- | Extract validation hash by specified piece index. | ||
363 | pieceHash :: ContentInfo -> Int -> ByteString | ||
364 | pieceHash 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. | ||
370 | checkPiece :: ContentInfo -> Int -> ByteString -> Bool | ||
371 | checkPiece 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. |
360 | fromFile :: FilePath -> IO Torrent | 376 | fromFile :: FilePath -> IO Torrent |
361 | fromFile filepath = do | 377 | fromFile filepath = do |