diff options
-rw-r--r-- | bittorrent.cabal | 8 | ||||
-rw-r--r-- | src/Data/Torrent.hs | 12 |
2 files changed, 16 insertions, 4 deletions
diff --git a/bittorrent.cabal b/bittorrent.cabal index 211eee06..5f39d3ea 100644 --- a/bittorrent.cabal +++ b/bittorrent.cabal | |||
@@ -74,6 +74,8 @@ library | |||
74 | , binary-conduit | 74 | , binary-conduit |
75 | 75 | ||
76 | -- Data | 76 | -- Data |
77 | , data-default | ||
78 | |||
77 | , array >= 0.4 | 79 | , array >= 0.4 |
78 | , bytestring >= 0.10.0.0 | 80 | , bytestring >= 0.10.0.0 |
79 | 81 | ||
@@ -105,9 +107,11 @@ library | |||
105 | , directory >= 1 | 107 | , directory >= 1 |
106 | , mmap >= 0.5.2 | 108 | , mmap >= 0.5.2 |
107 | 109 | ||
108 | -- Misc | 110 | -- Hash |
109 | , data-default | ||
110 | , cryptohash | 111 | , cryptohash |
112 | , hashable | ||
113 | |||
114 | -- Misc | ||
111 | , pretty | 115 | , pretty |
112 | 116 | ||
113 | -- , bits-atomic >= 0.1 | 117 | -- , bits-atomic >= 0.1 |
diff --git a/src/Data/Torrent.hs b/src/Data/Torrent.hs index bdd38630..8e6f9088 100644 --- a/src/Data/Torrent.hs +++ b/src/Data/Torrent.hs | |||
@@ -50,7 +50,8 @@ module Data.Torrent | |||
50 | 50 | ||
51 | #if defined (TESTING) | 51 | #if defined (TESTING) |
52 | -- * Internal | 52 | -- * Internal |
53 | , hash, hashlazy | 53 | , Data.Torrent.hash |
54 | , Data.Torrent.hashlazy | ||
54 | #endif | 55 | #endif |
55 | ) where | 56 | ) where |
56 | 57 | ||
@@ -71,6 +72,7 @@ import qualified Data.ByteString.Lazy as Lazy | |||
71 | import qualified Data.ByteString.Lazy.Builder as B | 72 | import qualified Data.ByteString.Lazy.Builder as B |
72 | import qualified Data.ByteString.Lazy.Builder.ASCII as B | 73 | import qualified Data.ByteString.Lazy.Builder.ASCII as B |
73 | import qualified Data.List as L | 74 | import qualified Data.List as L |
75 | import Data.Hashable as Hashable | ||
74 | import Data.Text (Text) | 76 | import Data.Text (Text) |
75 | import Data.Serialize as S hiding (Result) | 77 | import Data.Serialize as S hiding (Result) |
76 | import Text.PrettyPrint | 78 | import Text.PrettyPrint |
@@ -127,6 +129,9 @@ data Torrent = Torrent { | |||
127 | -- the encrypted SHA-1 hash of the info dictionary). | 129 | -- the encrypted SHA-1 hash of the info dictionary). |
128 | } deriving (Show, Eq) | 130 | } deriving (Show, Eq) |
129 | 131 | ||
132 | instance Hashable Torrent where | ||
133 | hash = Hashable.hash . tInfoHash | ||
134 | |||
130 | {- note that info hash is actually reduntant field | 135 | {- note that info hash is actually reduntant field |
131 | but it's better to keep it here to avoid heavy recomputations | 136 | but it's better to keep it here to avoid heavy recomputations |
132 | -} | 137 | -} |
@@ -377,7 +382,7 @@ checkPiece :: ContentInfo -> Int -> ByteString -> Bool | |||
377 | checkPiece ci ix piece @ (PS _ off si) | 382 | checkPiece ci ix piece @ (PS _ off si) |
378 | | traceShow (ix, off, si) True | 383 | | traceShow (ix, off, si) True |
379 | = B.length piece == ciPieceLength ci | 384 | = B.length piece == ciPieceLength ci |
380 | && hash piece == InfoHash (pieceHash ci ix) | 385 | && C.hash piece == pieceHash ci ix |
381 | 386 | ||
382 | -- | Read and decode a .torrent file. | 387 | -- | Read and decode a .torrent file. |
383 | fromFile :: FilePath -> IO Torrent | 388 | fromFile :: FilePath -> IO Torrent |
@@ -395,6 +400,9 @@ fromFile filepath = do | |||
395 | newtype InfoHash = InfoHash { getInfoHash :: ByteString } | 400 | newtype InfoHash = InfoHash { getInfoHash :: ByteString } |
396 | deriving (Eq, Ord) | 401 | deriving (Eq, Ord) |
397 | 402 | ||
403 | instance Hashable InfoHash where | ||
404 | hash = Hashable.hash . getInfoHash | ||
405 | |||
398 | instance BEncodable InfoHash where | 406 | instance BEncodable InfoHash where |
399 | toBEncode = toBEncode . getInfoHash | 407 | toBEncode = toBEncode . getInfoHash |
400 | fromBEncode be = InfoHash <$> fromBEncode be | 408 | fromBEncode be = InfoHash <$> fromBEncode be |