summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2013-11-29 18:30:13 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2013-11-29 18:30:13 +0400
commitc689257a818c0a7581666f4bdfd4549e52dbd075 (patch)
tree4c65eab91d7d09bb28d439265a0d88ee8319f672
parent2f3b01abced907c47059c73e0b1e56998a3a24f7 (diff)
Catch base32 decoding errors in pure code
-rw-r--r--bittorrent.cabal4
-rw-r--r--src/Data/Torrent/InfoHash.hs12
2 files changed, 7 insertions, 9 deletions
diff --git a/bittorrent.cabal b/bittorrent.cabal
index 6d31ca1f..46be6600 100644
--- a/bittorrent.cabal
+++ b/bittorrent.cabal
@@ -97,7 +97,6 @@ library
97 , conduit >= 1.0 97 , conduit >= 1.0
98 , network-conduit >= 1.0 98 , network-conduit >= 1.0
99 , cereal-conduit >= 0.5 99 , cereal-conduit >= 0.5
100 , binary-conduit
101 100
102 -- Data & Data structures 101 -- Data & Data structures
103 , bytestring >= 0.10.0.0 102 , bytestring >= 0.10.0.0
@@ -117,10 +116,9 @@ library
117 -- Codecs & Serialization 116 -- Codecs & Serialization
118 , aeson 117 , aeson
119 , base16-bytestring 118 , base16-bytestring
120 , base32-bytestring 119 , base32-bytestring >= 0.2
121 , base64-bytestring 120 , base64-bytestring
122 , bencoding >= 0.4 121 , bencoding >= 0.4
123 , binary
124 , cereal >= 0.3 122 , cereal >= 0.3
125 123
126 -- Time 124 -- Time
diff --git a/src/Data/Torrent/InfoHash.hs b/src/Data/Torrent/InfoHash.hs
index f730effa..6313948b 100644
--- a/src/Data/Torrent/InfoHash.hs
+++ b/src/Data/Torrent/InfoHash.hs
@@ -127,11 +127,13 @@ instance Convertible Text InfoHash where
127 safeConvert t 127 safeConvert t
128 | hashLen <= 28 = 128 | 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 -> pure $ InfoHash ihStr
132 132
133 | hashLen == 32 = pure $ InfoHash $ Base32.decode hashStr 133 | hashLen == 32 =
134-- TODO FIX Base32.decode can return 'undefined' 134 case Base32.decode hashStr of
135 Left msg -> convError msg t
136 Right ihStr -> pure $ InfoHash ihStr
135 137
136 | hashLen == 40 = 138 | hashLen == 40 =
137 let (ihStr, inv) = Base16.decode hashStr 139 let (ihStr, inv) = Base16.decode hashStr
@@ -146,9 +148,7 @@ instance Convertible Text InfoHash where
146 148
147-- | Decode from base16\/base32\/base64 encoded string. 149-- | Decode from base16\/base32\/base64 encoded string.
148instance IsString InfoHash where 150instance IsString InfoHash where
149 fromString str = fromMaybe err $ textToInfoHash $ T.pack str 151 fromString = either (error . prettyConvertError) id . safeConvert . T.pack
150 where
151 err = error $ "fromString: invalid infohash string" ++ str
152 152
153-- | Convert to base16 encoded JSON string. 153-- | Convert to base16 encoded JSON string.
154instance ToJSON InfoHash where 154instance ToJSON InfoHash where