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/Torrent.hs | |
parent | d4ada1b8a392d67f2835935084c5f0f3ecef2ab5 (diff) |
+ Scetch basic broadcasting.
Diffstat (limited to 'src/Data/Torrent.hs')
-rw-r--r-- | src/Data/Torrent.hs | 16 |
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 | |||
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 |