diff options
Diffstat (limited to 'src/Data/Torrent/Piece.hs')
-rw-r--r-- | src/Data/Torrent/Piece.hs | 24 |
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. |
150 | newtype HashArray = HashArray { unHashArray :: ByteString } | 150 | newtype 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. |
154 | instance ToJSON HashArray where | 154 | instance 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 | ||
157 | instance FromJSON HashArray where | 157 | instance 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. |
162 | data PieceInfo = PieceInfo | 162 | data 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 | ||
180 | instance Lint PieceInfo where | 180 | instance 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. |
215 | pieceHash :: PieceInfo -> PieceIx -> ByteString | 215 | pieceHash :: PieceInfo -> PieceIx -> ByteString |
216 | pieceHash PieceInfo {..} i = slice (hashsize * i) hashsize (unHashArray piPieceHashes) | 216 | pieceHash 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. |
220 | pieceCount :: PieceInfo -> PieceCount | 220 | pieceCount :: PieceInfo -> PieceCount |
221 | pieceCount PieceInfo {..} = BS.length (unHashArray piPieceHashes) `quot` hashsize | 221 | pieceCount 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. |
224 | isLastPiece :: PieceInfo -> PieceIx -> Bool | 224 | isLastPiece :: PieceInfo -> PieceIx -> Bool |