{-# LANGUAGE OverloadedStrings #-}
module Data.MIME.Base64
(
b
, contentTransferEncodeBase64
, contentTransferEncodingBase64
) where
import Control.Lens (prism')
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString.Base64 as B64
import qualified Data.ByteString.Base64.Lazy as L64
import Data.Word (Word8)
import Data.MIME.Types (ContentTransferEncoding)
isBase64Char :: Word8 -> Bool
isBase64Char :: Word8 -> Bool
isBase64Char Word8
c =
(Word8
c forall a. Ord a => a -> a -> Bool
>= Word8
0x41 Bool -> Bool -> Bool
&& Word8
c forall a. Ord a => a -> a -> Bool
<= Word8
0x5a)
Bool -> Bool -> Bool
|| (Word8
c forall a. Ord a => a -> a -> Bool
>= Word8
0x61 Bool -> Bool -> Bool
&& Word8
c forall a. Ord a => a -> a -> Bool
<= Word8
0x7a)
Bool -> Bool -> Bool
|| (Word8
c forall a. Ord a => a -> a -> Bool
>= Word8
0x30 Bool -> Bool -> Bool
&& Word8
c forall a. Ord a => a -> a -> Bool
<= Word8
0x39)
Bool -> Bool -> Bool
|| Word8
c forall a. Eq a => a -> a -> Bool
== Word8
43
Bool -> Bool -> Bool
|| Word8
c forall a. Eq a => a -> a -> Bool
== Word8
47
Bool -> Bool -> Bool
|| Word8
c forall a. Eq a => a -> a -> Bool
== Word8
61
contentTransferEncodeBase64 :: B.ByteString -> B.ByteString
contentTransferEncodeBase64 :: ByteString -> ByteString
contentTransferEncodeBase64 = ByteString -> ByteString
L.toStrict forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
wrap forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
L64.encode forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
L.fromStrict
where
wrap :: ByteString -> ByteString
wrap ByteString
s = case Int64 -> ByteString -> (ByteString, ByteString)
L.splitAt Int64
76 ByteString
s of
(ByteString
l, ByteString
"") -> ByteString
l
(ByteString
l, ByteString
s') -> ByteString
l forall a. Semigroup a => a -> a -> a
<> ByteString
"\r\n" forall a. Semigroup a => a -> a -> a
<> ByteString -> ByteString
wrap ByteString
s'
contentTransferDecodeBase64 :: B.ByteString -> Either String B.ByteString
contentTransferDecodeBase64 :: ByteString -> Either String ByteString
contentTransferDecodeBase64 = ByteString -> Either String ByteString
B64.decode forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Word8 -> Bool) -> ByteString -> ByteString
B.filter Word8 -> Bool
isBase64Char
contentTransferEncodingBase64 :: ContentTransferEncoding
contentTransferEncodingBase64 :: ContentTransferEncoding
contentTransferEncodingBase64 = forall b s a. (b -> s) -> (s -> Maybe a) -> Prism s s a b
prism'
ByteString -> ByteString
contentTransferEncodeBase64
(forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall a b. a -> b -> a
const forall a. Maybe a
Nothing) forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either String ByteString
contentTransferDecodeBase64)
b :: ContentTransferEncoding
b :: ContentTransferEncoding
b = forall b s a. (b -> s) -> (s -> Maybe a) -> Prism s s a b
prism'
ByteString -> ByteString
B64.encode
(forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall a b. a -> b -> a
const forall a. Maybe a
Nothing) forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either String ByteString
contentTransferDecodeBase64)