diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2014-01-01 15:42:18 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2014-01-01 15:42:18 +0400 |
commit | 61650c4513d173967fa3a4cc73a76244319340cf (patch) | |
tree | c69f8ed848b3af57c36b3a3b5cbc5d31b6b816bc | |
parent | 200be6bacf3237f727980c0f80dd02fd9cb15e3e (diff) |
Add PieceHash type synonym
-rw-r--r-- | src/Data/Torrent/Piece.hs | 13 |
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 | ||
150 | type PieceHash = ByteString | ||
151 | |||
152 | hashsize :: Int | ||
153 | hashsize = 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. |
150 | newtype HashList = HashList { unHashList :: ByteString } | 157 | newtype 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 | |||
203 | instance Pretty PieceInfo where | 210 | instance Pretty PieceInfo where |
204 | pretty PieceInfo {..} = "Piece size: " <> int piPieceLength | 211 | pretty PieceInfo {..} = "Piece size: " <> int piPieceLength |
205 | 212 | ||
206 | hashsize :: Int | ||
207 | hashsize = 20 | ||
208 | {-# INLINE hashsize #-} | ||
209 | |||
210 | slice :: Int -> Int -> ByteString -> ByteString | 213 | slice :: Int -> Int -> ByteString -> ByteString |
211 | slice start len = BS.take len . BS.drop start | 214 | slice 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. |
215 | pieceHash :: PieceInfo -> PieceIx -> ByteString | 218 | pieceHash :: PieceInfo -> PieceIx -> PieceHash |
216 | pieceHash PieceInfo {..} i = slice (hashsize * i) hashsize (unHashList piPieceHashes) | 219 | pieceHash 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 |