1 {-# LANGUAGE OverloadedStrings #-}
2 module FuncTorrent.Bencode
13 import Prelude hiding (length, concat)
15 import Control.Applicative ((<*))
16 import Data.ByteString (ByteString, length, concat)
17 import Data.ByteString.Char8 (unpack, pack)
18 import Data.Functor ((<$>))
19 import Data.Map.Strict (Map, fromList, keys, (!))
20 import Text.ParserCombinators.Parsec
21 import qualified Text.Parsec.ByteString as ParsecBS
23 data BVal = Bint Integer
26 | Bdict (Map String BVal)
27 deriving (Ord, Eq, Show)
30 bValToInteger :: BVal -> Maybe Integer
31 bValToInteger (Bint x) = Just x
32 bValToInteger _ = Nothing
34 bValToBstr :: BVal -> Maybe ByteString
35 bValToBstr (Bstr bs) = Just bs
36 bValToBstr _ = Nothing
38 bValToBList :: BVal -> Maybe [BVal]
39 bValToBList (Blist lst) = Just lst
40 bValToBList _ = Nothing
42 bValToInfoDict :: BVal -> Maybe (Map String BVal)
43 bValToInfoDict (Bdict dict) = Just dict
44 bValToInfoDict _ = Nothing
46 bstrToString :: BVal -> Maybe String
47 bstrToString bval = unpack <$> bValToBstr bval
50 -- >>> import Data.Either
54 -- >>> parse bencStr "Bstr" (pack "4:spam")
56 -- >>> parse bencStr "Bstr" (pack "0:")
58 -- >>> parse bencStr "Bstr" (pack "0:hello")
61 bencStr :: ParsecBS.Parser ByteString
62 bencStr = do _ <- spaces
63 ds <- many1 digit <* char ':'
64 s <- count (read ds) anyChar
69 -- >>> parse bencInt "Bint" (pack "i42e")
71 -- >>> parse bencInt "Bint" (pack "i123e")
73 -- >>> parse bencInt "Bint" (pack "i1e")
75 -- >>> parse bencInt "Bint" (pack "i0e")
77 -- >>> parse bencInt "Bint" (pack "i-1e")
79 -- >>> isLeft $ parse bencInt "Bint" (pack "i01e")
81 -- >>> isLeft $ parse bencInt "Bint" (pack "i00e")
83 -- >>> isLeft $ parse bencInt "Bint" (pack "i002e")
85 bencInt :: ParsecBS.Parser Integer
86 bencInt = do _ <- spaces
87 ds <- between (char 'i') (char 'e') numbers
89 where numbers = do d' <- char '-' <|> digit
92 parseNumber '0' [] = return "0"
93 parseNumber '0' _ = unexpected "numbers cannot be left-padded with zeros"
94 parseNumber '-' [] = unexpected "sign without any digits"
95 parseNumber '-' (d'':_) | d'' == '0' = unexpected "numbers cannot be left-padded with zeros"
96 parseNumber d'' ds'' = return (d'':ds'')
100 -- >>> parse bencList "Blist" (pack "le")
102 -- >>> parse bencList "Blist" (pack "l4:spam4:eggse")
103 -- Right [Bstr "spam",Bstr "eggs"]
104 -- >>> parse bencList "Blist" (pack "l4:spami42ee")
105 -- Right [Bstr "spam",Bint 42]
106 -- >>> parse bencList "Blist" (pack "l4:spam4:eggsli42eee")
107 -- Right [Bstr "spam",Bstr "eggs",Blist [Bint 42]]
108 bencList :: ParsecBS.Parser [BVal]
109 bencList = do _ <- spaces
110 between (char 'l') (char 'e') (many bencVal)
114 -- >>> parse bencDict "Bdict" (pack "de")
115 -- Right (fromList [])
116 -- >>> parse bencDict "Bdict" (pack "d3:cow3:moo4:spam4:eggse")
117 -- Right (fromList [("cow",Bstr "moo"),("spam",Bstr "eggs")])
118 -- >>> parse bencDict "Bdict" (pack "d4:spaml1:a1:bee")
119 -- Right (fromList [("spam",Blist [Bstr "a",Bstr "b"])])
120 -- >>> parse bencDict "Bdict" (pack "d9:publisher3:bob17:publisher-webpage15:www.example.com18:publisher.location4:homee")
121 -- Right (fromList [("publisher",Bstr "bob"),("publisher-webpage",Bstr "www.example.com"),("publisher.location",Bstr "home")])
122 bencDict :: ParsecBS.Parser (Map String BVal)
123 bencDict = between (char 'd') (char 'e') $ fromList <$> many kvpair
124 where kvpair = do k <- bencStr
128 bencVal :: ParsecBS.Parser BVal
129 bencVal = Bstr <$> bencStr <|>
131 Blist <$> bencList <|>
134 decode :: ByteString -> Either ParseError BVal
135 decode = parse bencVal "BVal"
137 -- Encode BVal into a bencoded ByteString. Inverse of decode
139 -- TODO: Use builders and lazy byte string to get O(1) concatenation over O(n)
140 -- provided by lists.
142 -- TODO: encode . decode pair might be a good candidate for Quickcheck.
143 -- | encode bencoded-values
145 -- >>> encode (Bstr (pack ""))
147 -- >>> encode (Bstr (pack "spam"))
149 -- >>> encode (Bint 0)
151 -- >>> encode (Bint 42)
153 -- >>> encode (Blist [(Bstr (pack "spam")), (Bstr (pack "eggs"))])
155 -- >>> encode (Blist [])
157 -- >>> encode (Bdict (fromList [("spam", Bstr $ pack "eggs")]))
159 encode :: BVal -> ByteString
160 encode (Bstr bs) = pack $ show (length bs) ++ ":" ++ unpack bs
161 encode (Bint i) = pack $ "i" ++ show i ++ "e"
162 encode (Blist xs) = concat ["l", concat $ map encode xs, "e"]
163 encode (Bdict d) = concat ["d", concat kvlist, "e"]
165 kvlist :: [ByteString]
166 kvlist = [concat [encode . Bstr . pack $ k , encode (d ! k)] | k <- keys d]