summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Tox.hs2
-rw-r--r--src/Text/XXD.hs26
2 files changed, 27 insertions, 1 deletions
diff --git a/Tox.hs b/Tox.hs
index 7ba0d63e..1f145432 100644
--- a/Tox.hs
+++ b/Tox.hs
@@ -91,7 +91,7 @@ import qualified DHTHandlers as DHT
91import qualified OnionTransport as Onion 91import qualified OnionTransport as Onion
92import qualified OnionHandlers as Onion 92import qualified OnionHandlers as Onion
93import CryptoTransport (NetCrypto) 93import CryptoTransport (NetCrypto)
94import XXD 94import Text.XXD
95 95
96newCrypto :: IO TransportCrypto 96newCrypto :: IO TransportCrypto
97newCrypto = do 97newCrypto = do
diff --git a/src/Text/XXD.hs b/src/Text/XXD.hs
new file mode 100644
index 00000000..d835b238
--- /dev/null
+++ b/src/Text/XXD.hs
@@ -0,0 +1,26 @@
1module Text.XXD where
2
3import qualified Data.ByteString.Base16 as Base16
4import Data.ByteString (ByteString)
5import qualified Data.ByteString as B
6import Data.Word
7import Data.Bits
8import Data.Char
9import Text.Printf
10
11nibble :: Word8 -> Char
12nibble b = intToDigit (fromIntegral (b .&. 0x0F))
13
14xxd :: Int -> ByteString -> [String]
15xxd offset bs | B.null bs = []
16xxd offset bs = printf "%03x: %s" offset ds : xxd (offset + B.length xs) bs'
17 where
18 ds = unwords $ map (\byte -> [nibble (byte `shiftR` 4), nibble byte])
19 $ B.unpack xs
20 (xs,bs') = B.splitAt 16 bs
21
22{-
23main = do
24 bs <- B.getContents
25 mapM_ putStrLn $ xxd 0 bs
26 -}