diff options
Diffstat (limited to 'src/Data/Torrent.hs')
-rw-r--r-- | src/Data/Torrent.hs | 15 |
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 | |||
77 | import System.FilePath | 79 | import System.FilePath |
78 | import Numeric | 80 | import Numeric |
79 | 81 | ||
82 | import Data.ByteString.Internal | ||
83 | import Debug.Trace | ||
84 | |||
80 | 85 | ||
81 | type Time = Text | 86 | type 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. |
145 | data ContentInfo = | 152 | data 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. |
363 | pieceHash :: ContentInfo -> Int -> ByteString | 370 | pieceHash :: ContentInfo -> Int -> ByteString |
364 | pieceHash ci ix = slice offset size (ciPieces ci) | 371 | pieceHash 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. |
370 | checkPiece :: ContentInfo -> Int -> ByteString -> Bool | 376 | checkPiece :: ContentInfo -> Int -> ByteString -> Bool |
371 | checkPiece ci ix piece | 377 | checkPiece 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 | ||