summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2014-01-01 15:27:56 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2014-01-01 15:27:56 +0400
commit200be6bacf3237f727980c0f80dd02fd9cb15e3e (patch)
treef22206f7990ecdd4f8a5ca23657d66cbf8f7ef36
parentab80dc87b0d0440a8566e389f4f5f2d95d931a1a (diff)
Rename HashArray to HashList.
Hash list is well established name for this kind of objects. Also `hash array' sometimes refer for `hash table's but not `hash list's.
-rw-r--r--src/Data/Torrent/Piece.hs24
-rw-r--r--tests/Data/Torrent/MetainfoSpec.hs4
2 files changed, 14 insertions, 14 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
diff --git a/tests/Data/Torrent/MetainfoSpec.hs b/tests/Data/Torrent/MetainfoSpec.hs
index d2e0e52e..3d1a9213 100644
--- a/tests/Data/Torrent/MetainfoSpec.hs
+++ b/tests/Data/Torrent/MetainfoSpec.hs
@@ -39,8 +39,8 @@ instance Arbitrary URI where
39-- Instances 39-- Instances
40-----------------------------------------------------------------------} 40-----------------------------------------------------------------------}
41 41
42instance Arbitrary HashArray where 42instance Arbitrary HashList where
43 arbitrary = HashArray <$> arbitrary 43 arbitrary = HashList <$> arbitrary
44 44
45instance Arbitrary PieceInfo where 45instance Arbitrary PieceInfo where
46 arbitrary = PieceInfo <$> arbitrary <*> arbitrary 46 arbitrary = PieceInfo <$> arbitrary <*> arbitrary