summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2014-01-01 15:42:18 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2014-01-01 15:42:18 +0400
commit61650c4513d173967fa3a4cc73a76244319340cf (patch)
treec69f8ed848b3af57c36b3a3b5cbc5d31b6b816bc /src
parent200be6bacf3237f727980c0f80dd02fd9cb15e3e (diff)
Add PieceHash type synonym
Diffstat (limited to 'src')
-rw-r--r--src/Data/Torrent/Piece.hs13
1 files changed, 8 insertions, 5 deletions
diff --git a/src/Data/Torrent/Piece.hs b/src/Data/Torrent/Piece.hs
index 23ac11b4..a7ea4700 100644
--- a/src/Data/Torrent/Piece.hs
+++ b/src/Data/Torrent/Piece.hs
@@ -19,6 +19,7 @@ module Data.Torrent.Piece
19 , minPieceSize 19 , minPieceSize
20 , maxPieceSize 20 , maxPieceSize
21 , defaultPieceSize 21 , defaultPieceSize
22 , PieceHash
22 23
23 -- * Piece data 24 -- * Piece data
24 , Piece (..) 25 , Piece (..)
@@ -146,6 +147,12 @@ pieceSize Piece {..} = fromIntegral (BL.length pieceData)
146-- Piece control 147-- Piece control
147-----------------------------------------------------------------------} 148-----------------------------------------------------------------------}
148 149
150type PieceHash = ByteString
151
152hashsize :: Int
153hashsize = 20
154{-# INLINE hashsize #-}
155
149-- | A flat array of SHA1 hash for each piece. 156-- | A flat array of SHA1 hash for each piece.
150newtype HashList = HashList { unHashList :: ByteString } 157newtype HashList = HashList { unHashList :: ByteString }
151 deriving (Show, Read, Eq, BEncode, Typeable) 158 deriving (Show, Read, Eq, BEncode, Typeable)
@@ -203,16 +210,12 @@ instance BEncode PieceInfo where
203instance Pretty PieceInfo where 210instance Pretty PieceInfo where
204 pretty PieceInfo {..} = "Piece size: " <> int piPieceLength 211 pretty PieceInfo {..} = "Piece size: " <> int piPieceLength
205 212
206hashsize :: Int
207hashsize = 20
208{-# INLINE hashsize #-}
209
210slice :: Int -> Int -> ByteString -> ByteString 213slice :: Int -> Int -> ByteString -> ByteString
211slice start len = BS.take len . BS.drop start 214slice start len = BS.take len . BS.drop start
212{-# INLINE slice #-} 215{-# INLINE slice #-}
213 216
214-- | Extract validation hash by specified piece index. 217-- | Extract validation hash by specified piece index.
215pieceHash :: PieceInfo -> PieceIx -> ByteString 218pieceHash :: PieceInfo -> PieceIx -> PieceHash
216pieceHash PieceInfo {..} i = slice (hashsize * i) hashsize (unHashList piPieceHashes) 219pieceHash PieceInfo {..} i = slice (hashsize * i) hashsize (unHashList piPieceHashes)
217 220
218-- | Find count of pieces in the torrent. If torrent size is not a 221-- | Find count of pieces in the torrent. If torrent size is not a