{-# LANGUAGE CPP #-}
#if __GLASGOW_HASKELL__ >= 702
{-# LANGUAGE Trustworthy #-}
#endif
module Data.ByteString.Base64.Lazy
(
encode
, decode
, decodeLenient
) where
import Data.ByteString.Base64.Internal
import qualified Data.ByteString.Base64 as B64
import qualified Data.ByteString as S
import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString.Lazy.Char8 as LC
import Data.Char
encode :: L.ByteString -> L.ByteString
encode = L.fromChunks . map B64.encode . reChunkIn 3 . L.toChunks
decode :: L.ByteString -> Either String L.ByteString
decode b =
case B64.decode $ S.concat $ L.toChunks b of
Left err -> Left err
Right b' -> Right $ L.fromChunks [b']
decodeLenient :: L.ByteString -> L.ByteString
decodeLenient = L.fromChunks . map B64.decodeLenient . reChunkIn 4 . L.toChunks
. LC.filter goodChar
where
goodChar c = isAlphaNum c || c == '+' || c == '/'