fork(2) download
  1. {-# LANGUAGE OverloadedStrings #-}
  2.  
  3. import Data.Maybe (fromMaybe)
  4. import Data.Word (Word8)
  5. import qualified Data.ByteString as B
  6. import qualified Data.ByteString.Char8 as C
  7. import Data.ByteString
  8.  
  9. unhexDigit :: Word8 -> Word8
  10. unhexDigit x
  11. | (x >= 48 && x <= 57) = x - 48
  12. | (x >= 65 && x <= 71) = x + 10 - 65
  13. | (x >= 97 && x <= 102) = x + 10 - 97
  14. | otherwise = error "fuck"
  15.  
  16. unhexImpl :: [Word8] -> Maybe [Word8] -> Maybe [Word8]
  17. unhexImpl [] (Just state) = Just $ Prelude.reverse state
  18. unhexImpl (x:y:xs) (Just state) =
  19. unhexImpl xs $ Just $ a * 16 + b:state
  20. where
  21. a = unhexDigit x
  22. b = unhexDigit y
  23.  
  24. unhexImpl bytes state = Nothing
  25.  
  26. unhex :: B.ByteString -> Maybe B.ByteString
  27. unhex bytes = fmap B.pack $ unhexImpl (B.unpack bytes) (Just [])
  28.  
  29. input :: B.ByteString
  30. input = "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d"
  31.  
  32. main :: IO ()
  33. main = C.putStrLn $ fromMaybe "invalid input" $ unhex input
Success #stdin #stdout 0s 4620KB
stdin
Standard input is empty
stdout
I'm killing your brain like a poisonous mushroom