diff options
Diffstat (limited to 'src/Data')
-rw-r--r-- | src/Data/Torrent/Block.hs | 23 | ||||
-rw-r--r-- | src/Data/Torrent/InfoHash.hs | 16 |
2 files changed, 8 insertions, 31 deletions
diff --git a/src/Data/Torrent/Block.hs b/src/Data/Torrent/Block.hs index 987de653..37889a7a 100644 --- a/src/Data/Torrent/Block.hs +++ b/src/Data/Torrent/Block.hs | |||
@@ -37,9 +37,6 @@ import Data.Aeson.TH | |||
37 | import qualified Data.ByteString.Lazy as Lazy | 37 | import qualified Data.ByteString.Lazy as Lazy |
38 | import Data.Char | 38 | import Data.Char |
39 | import Data.List as L | 39 | import Data.List as L |
40 | import Data.Binary as B | ||
41 | import Data.Binary.Get as B | ||
42 | import Data.Binary.Put as B | ||
43 | import Data.Serialize as S | 40 | import Data.Serialize as S |
44 | import Text.PrettyPrint | 41 | import Text.PrettyPrint |
45 | import Text.PrettyPrint.Class | 42 | import Text.PrettyPrint.Class |
@@ -110,14 +107,6 @@ putInt :: S.Putter Int | |||
110 | putInt = S.putWord32be . fromIntegral | 107 | putInt = S.putWord32be . fromIntegral |
111 | {-# INLINE putInt #-} | 108 | {-# INLINE putInt #-} |
112 | 109 | ||
113 | getIntB :: B.Get Int | ||
114 | getIntB = fromIntegral <$> B.getWord32be | ||
115 | {-# INLINE getIntB #-} | ||
116 | |||
117 | putIntB :: Int -> B.Put | ||
118 | putIntB = B.putWord32be . fromIntegral | ||
119 | {-# INLINE putIntB #-} | ||
120 | |||
121 | instance Serialize BlockIx where | 110 | instance Serialize BlockIx where |
122 | {-# SPECIALIZE instance Serialize BlockIx #-} | 111 | {-# SPECIALIZE instance Serialize BlockIx #-} |
123 | get = BlockIx <$> getInt | 112 | get = BlockIx <$> getInt |
@@ -131,18 +120,6 @@ instance Serialize BlockIx where | |||
131 | putInt ixLength | 120 | putInt ixLength |
132 | {-# INLINE put #-} | 121 | {-# INLINE put #-} |
133 | 122 | ||
134 | instance Binary BlockIx where | ||
135 | {-# SPECIALIZE instance Binary BlockIx #-} | ||
136 | get = BlockIx <$> getIntB | ||
137 | <*> getIntB | ||
138 | <*> getIntB | ||
139 | {-# INLINE get #-} | ||
140 | |||
141 | put BlockIx {..} = do | ||
142 | putIntB ixPiece | ||
143 | putIntB ixOffset | ||
144 | putIntB ixLength | ||
145 | |||
146 | instance Pretty BlockIx where | 123 | instance Pretty BlockIx where |
147 | pretty BlockIx {..} = | 124 | pretty BlockIx {..} = |
148 | "piece = " <> int ixPiece <> "," <+> | 125 | "piece = " <> int ixPiece <> "," <+> |
diff --git a/src/Data/Torrent/InfoHash.hs b/src/Data/Torrent/InfoHash.hs index 6313948b..15682250 100644 --- a/src/Data/Torrent/InfoHash.hs +++ b/src/Data/Torrent/InfoHash.hs | |||
@@ -125,21 +125,21 @@ instance Convertible BS.ByteString InfoHash where | |||
125 | -- | Parse infohash from base16\/base32\/base64 encoded string. | 125 | -- | Parse infohash from base16\/base32\/base64 encoded string. |
126 | instance Convertible Text InfoHash where | 126 | instance Convertible Text InfoHash where |
127 | safeConvert t | 127 | safeConvert t |
128 | | hashLen <= 28 = | 128 | | 26 <= hashLen && hashLen <= 28 = |
129 | case Base64.decode hashStr of | 129 | case Base64.decode hashStr of |
130 | Left msg -> convError ("invalid base64 encoding " ++ msg) t | 130 | Left msg -> convError ("invalid base64 encoding " ++ msg) t |
131 | Right ihStr -> pure $ InfoHash ihStr | 131 | Right ihStr -> safeConvert ihStr |
132 | 132 | ||
133 | | hashLen == 32 = | 133 | | hashLen == 32 = |
134 | case Base32.decode hashStr of | 134 | case Base32.decode hashStr of |
135 | Left msg -> convError msg t | 135 | Left msg -> convError msg t |
136 | Right ihStr -> pure $ InfoHash ihStr | 136 | Right ihStr -> safeConvert ihStr |
137 | 137 | ||
138 | | hashLen == 40 = | 138 | | hashLen == 40 = |
139 | let (ihStr, inv) = Base16.decode hashStr | 139 | let (ihStr, inv) = Base16.decode hashStr |
140 | in if BS.length inv == 0 | 140 | in if BS.length inv /= 0 |
141 | then pure $ InfoHash ihStr | 141 | then convError "invalid base16 encoding" t |
142 | else convError "invalid base16 encoding" t | 142 | else safeConvert ihStr |
143 | 143 | ||
144 | | otherwise = convError "invalid length" t | 144 | | otherwise = convError "invalid length" t |
145 | where | 145 | where |
@@ -156,8 +156,8 @@ instance ToJSON InfoHash where | |||
156 | 156 | ||
157 | -- | Convert from base16\/base32\/base64 encoded JSON string. | 157 | -- | Convert from base16\/base32\/base64 encoded JSON string. |
158 | instance FromJSON InfoHash where | 158 | instance FromJSON InfoHash where |
159 | parseJSON = withText "JSON" $ -- TODO | 159 | parseJSON = withText "InfoHash" $ |
160 | maybe (fail "could not parse InfoHash") pure . textToInfoHash | 160 | either (fail . prettyConvertError) pure . safeConvert |
161 | 161 | ||
162 | ignoreErrorMsg :: Either a b -> Maybe b | 162 | ignoreErrorMsg :: Either a b -> Maybe b |
163 | ignoreErrorMsg = either (const Nothing) Just | 163 | ignoreErrorMsg = either (const Nothing) Just |