summaryrefslogtreecommitdiff
path: root/base64
diff options
context:
space:
mode:
Diffstat (limited to 'base64')
-rw-r--r--base64/Makefile2
-rw-r--r--base64/base64.hs11
2 files changed, 7 insertions, 6 deletions
diff --git a/base64/Makefile b/base64/Makefile
index 0192413..71b16ec 100644
--- a/base64/Makefile
+++ b/base64/Makefile
@@ -1,5 +1,5 @@
base64: base64.hs
- #stack ghc -- -O2 base64.hs -rtsopts -prof -fprof-cafs -fprof-auto
+ #stack ghc -- -O2 base64.hs -rtsopts -prof -fprof-cafs -fprof-auto
stack ghc -- -O2 base64.hs
run: base64
diff --git a/base64/base64.hs b/base64/base64.hs
index 91348fb..6b75a31 100644
--- a/base64/base64.hs
+++ b/base64/base64.hs
@@ -12,14 +12,15 @@ import System.IO.Unsafe (unsafePerformIO)
-- |Perform base64 encoding of data from standard input
main :: IO()
-main = BL.getContents>>=BL.putStr.BL.fromChunks.map encode64.reChunk.BL.toChunks
+main = BL.getContents>>=BL.putStr.flip BL.append (BL.pack [BI.c2w '\n']).
+ BL.fromChunks.map encode64.reChunk.BL.toChunks
-- |Base64 index table
tab64 :: UArray Word32 Word8
tab64 = array (0,63) $ zip [0..] $ map (BI.c2w) $
['A'..'Z']++['a'..'z']++['0'..'9']++['+','/']
--- |Encodes 3 octets into 4 sextets
+-- |Encodes 3 octets as 4 sextets. This is what base64 basically :) is about.
enc64 :: (Word8,Word8,Word8)->(Word8,Word8,Word8,Word8)
enc64 (b1,b2,b3) = (t 3,t 2,t 1,t 0)
where t x = tab64 `unsafeAt` (n `shiftR` (x*6) .&. 63)
@@ -32,11 +33,11 @@ enc64 (b1,b2,b3) = (t 3,t 2,t 1,t 0)
-- at least a length of 3.
reChunk :: [BS.ByteString] -> [BS.ByteString]
reChunk (y:[]) = [y]
-reChunk (y:z:zs) = let c = BS.length y `mod` 3
- in BS.append y (BS.take 3 z):(reChunk $ (BS.drop 3 z):zs)
+reChunk (y:z:zs) = let c = BS.length y `mod` 3 in
+ BS.append y (BS.take 3 z):(reChunk $ (BS.drop 3 z):zs)
-- |Wraps Base64 enode in encode64io in unsafePerformIO to use in
--- pure code, Use this only if you trust my 'encode64io' code is free
+-- pure code. Use this only if you trust my 'encode64io' code is free
-- of side effects and indepedent of the environment. Good Luck!
encode64 :: BS.ByteString -> BS.ByteString
encode64 = unsafePerformIO . encode64io