summaryrefslogtreecommitdiff
path: root/src/Data/Torrent/Piece.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Data/Torrent/Piece.hs')
-rw-r--r--src/Data/Torrent/Piece.hs24
1 files changed, 12 insertions, 12 deletions
diff --git a/src/Data/Torrent/Piece.hs b/src/Data/Torrent/Piece.hs
index d79da2ee..23ac11b4 100644
--- a/src/Data/Torrent/Piece.hs
+++ b/src/Data/Torrent/Piece.hs
@@ -25,7 +25,7 @@ module Data.Torrent.Piece
25 , pieceSize 25 , pieceSize
26 26
27 -- * Piece control 27 -- * Piece control
28 , HashArray (..) 28 , HashList (..)
29 , PieceInfo (..) 29 , PieceInfo (..)
30 , pieceCount 30 , pieceCount
31 31
@@ -146,24 +146,24 @@ pieceSize Piece {..} = fromIntegral (BL.length pieceData)
146-- Piece control 146-- Piece control
147-----------------------------------------------------------------------} 147-----------------------------------------------------------------------}
148 148
149-- | A flat array of SHA1 sums of each piece. 149-- | A flat array of SHA1 hash for each piece.
150newtype HashArray = HashArray { unHashArray :: ByteString } 150newtype HashList = HashList { unHashList :: ByteString }
151 deriving (Show, Read, Eq, BEncode) 151 deriving (Show, Read, Eq, BEncode, Typeable)
152 152
153-- | Represented as base64 encoded JSON string. 153-- | Represented as base64 encoded JSON string.
154instance ToJSON HashArray where 154instance ToJSON HashList where
155 toJSON (HashArray bs) = String $ T.decodeUtf8 $ Base64.encode bs 155 toJSON (HashList bs) = String $ T.decodeUtf8 $ Base64.encode bs
156 156
157instance FromJSON HashArray where 157instance FromJSON HashList where
158 parseJSON = withText "HashArray" $ 158 parseJSON = withText "HashArray" $
159 either fail (return . HashArray) . Base64.decode . T.encodeUtf8 159 either fail (return . HashList) . Base64.decode . T.encodeUtf8
160 160
161-- | Part of torrent file used for torrent content validation. 161-- | Part of torrent file used for torrent content validation.
162data PieceInfo = PieceInfo 162data PieceInfo = PieceInfo
163 { piPieceLength :: {-# UNPACK #-} !PieceSize 163 { piPieceLength :: {-# UNPACK #-} !PieceSize
164 -- ^ Number of bytes in each piece. 164 -- ^ Number of bytes in each piece.
165 165
166 , piPieceHashes :: !HashArray 166 , piPieceHashes :: !HashList
167 -- ^ Concatenation of all 20-byte SHA1 hash values. 167 -- ^ Concatenation of all 20-byte SHA1 hash values.
168 } deriving (Show, Read, Eq, Typeable) 168 } deriving (Show, Read, Eq, Typeable)
169 169
@@ -179,7 +179,7 @@ instance NFData PieceInfo
179 179
180instance Lint PieceInfo where 180instance Lint PieceInfo where
181 lint pinfo @ PieceInfo {..} 181 lint pinfo @ PieceInfo {..}
182 | BS.length (unHashArray piPieceHashes) `rem` hashsize == 0 182 | BS.length (unHashList piPieceHashes) `rem` hashsize == 0
183 , piPieceLength >= 0 = return pinfo 183 , piPieceLength >= 0 = return pinfo
184 | otherwise = Left undefined 184 | otherwise = Left undefined
185 185
@@ -213,12 +213,12 @@ slice start len = BS.take len . BS.drop start
213 213
214-- | Extract validation hash by specified piece index. 214-- | Extract validation hash by specified piece index.
215pieceHash :: PieceInfo -> PieceIx -> ByteString 215pieceHash :: PieceInfo -> PieceIx -> ByteString
216pieceHash PieceInfo {..} i = slice (hashsize * i) hashsize (unHashArray piPieceHashes) 216pieceHash PieceInfo {..} i = slice (hashsize * i) hashsize (unHashList piPieceHashes)
217 217
218-- | Find count of pieces in the torrent. If torrent size is not a 218-- | Find count of pieces in the torrent. If torrent size is not a
219-- multiple of piece size then the count is rounded up. 219-- multiple of piece size then the count is rounded up.
220pieceCount :: PieceInfo -> PieceCount 220pieceCount :: PieceInfo -> PieceCount
221pieceCount PieceInfo {..} = BS.length (unHashArray piPieceHashes) `quot` hashsize 221pieceCount PieceInfo {..} = BS.length (unHashList piPieceHashes) `quot` hashsize
222 222
223-- | Test if this is last piece in torrent content. 223-- | Test if this is last piece in torrent content.
224isLastPiece :: PieceInfo -> PieceIx -> Bool 224isLastPiece :: PieceInfo -> PieceIx -> Bool