summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bittorrent.cabal8
-rw-r--r--src/Data/Torrent.hs12
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
71import qualified Data.ByteString.Lazy.Builder as B 72import qualified Data.ByteString.Lazy.Builder as B
72import qualified Data.ByteString.Lazy.Builder.ASCII as B 73import qualified Data.ByteString.Lazy.Builder.ASCII as B
73import qualified Data.List as L 74import qualified Data.List as L
75import Data.Hashable as Hashable
74import Data.Text (Text) 76import Data.Text (Text)
75import Data.Serialize as S hiding (Result) 77import Data.Serialize as S hiding (Result)
76import Text.PrettyPrint 78import 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
132instance 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
377checkPiece ci ix piece @ (PS _ off si) 382checkPiece 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.
383fromFile :: FilePath -> IO Torrent 388fromFile :: FilePath -> IO Torrent
@@ -395,6 +400,9 @@ fromFile filepath = do
395newtype InfoHash = InfoHash { getInfoHash :: ByteString } 400newtype InfoHash = InfoHash { getInfoHash :: ByteString }
396 deriving (Eq, Ord) 401 deriving (Eq, Ord)
397 402
403instance Hashable InfoHash where
404 hash = Hashable.hash . getInfoHash
405
398instance BEncodable InfoHash where 406instance BEncodable InfoHash where
399 toBEncode = toBEncode . getInfoHash 407 toBEncode = toBEncode . getInfoHash
400 fromBEncode be = InfoHash <$> fromBEncode be 408 fromBEncode be = InfoHash <$> fromBEncode be