summaryrefslogtreecommitdiff
path: root/base64/base64.hs
diff options
context:
space:
mode:
authorMiguel <m.i@gmx.at>2019-03-10 01:54:53 +0100
committerMiguel <m.i@gmx.at>2019-03-10 01:54:53 +0100
commit9dc8556d2510b125e3449e4eceac1eb126f9179e (patch)
tree52644ff647975ccf98a8a6d185d95e7fa400307f /base64/base64.hs
parent116a5a97f600df811f0f14dd2134a72aece88fbf (diff)
minor beutifications
Diffstat (limited to 'base64/base64.hs')
-rw-r--r--base64/base64.hs11
1 files changed, 6 insertions, 5 deletions
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