From fc4ed85a313f93b4fb0c46b500ee12c38e94df68 Mon Sep 17 00:00:00 2001 From: Sam Truzjan Date: Thu, 28 Nov 2013 06:28:06 +0400 Subject: Implement AnnounceQuery parsing --- src/Data/Torrent/InfoHash.hs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'src/Data/Torrent/InfoHash.hs') diff --git a/src/Data/Torrent/InfoHash.hs b/src/Data/Torrent/InfoHash.hs index 3d2bbe25..a4d6a02e 100644 --- a/src/Data/Torrent/InfoHash.hs +++ b/src/Data/Torrent/InfoHash.hs @@ -14,6 +14,7 @@ module Data.Torrent.InfoHash InfoHash(..) -- * Parsing + , byteStringToInfoHash , textToInfoHash -- * Rendering @@ -54,6 +55,16 @@ import Text.PrettyPrint import Text.PrettyPrint.Class +-- TODO +-- +-- data Word160 = Word160 {-# UNPACK #-} !Word64 +-- {-# UNPACK #-} !Word64 +-- {-# UNPACK #-} !Word32 +-- +-- newtype InfoHash = InfoHash Word160 +-- +-- reason: bytestring have overhead = 8 words, while infohash have length 20 bytes + -- | Exactly 20 bytes long SHA1 hash of the info part of torrent file. newtype InfoHash = InfoHash { getInfoHash :: BS.ByteString } deriving (Eq, Ord) @@ -110,6 +121,15 @@ instance URLShow InfoHash where instance Pretty InfoHash where pretty = text . BC.unpack . ppHex . getInfoHash +infoHashLen :: Int +infoHashLen = 20 + +-- | Convert raw bytes to info hash. +byteStringToInfoHash :: BS.ByteString -> Maybe InfoHash +byteStringToInfoHash bs + | BS.length bs == infoHashLen = Just (InfoHash bs) + | otherwise = Nothing + -- | Tries both base16 and base32 while decoding info hash. textToInfoHash :: Text -> Maybe InfoHash textToInfoHash t -- cgit v1.2.3