{-# LANGUAGE DataKinds #-}
-- |
-- Module       : Data.ByteString.Short.Base64
-- Copyright    : (c) 2019-2026 Emily Pillmore
-- License      : BSD-style
--
-- Maintainer   : Emily Pillmore <emilypi@cohomolo.gy>
-- Stability    : stable
-- Portability  : non-portable
--
-- This module contains 'Data.ByteString.Short.ShortByteString'-valued combinators for
-- implementing the RFC 4648 specification of the Base64
-- encoding format. This includes lenient decoding variants, as well as
-- internal and external validation for canonicity.
--
module Data.ByteString.Short.Base64
( -- * Encoding
  encodeBase64
, encodeBase64'
  -- * Decoding
, decodeBase64
, decodeBase64Untyped
, decodeBase64Lenient
  -- * Parsing
, parseBase64
) where

import Data.Base64.Types
import qualified Data.ByteString.Base64 as B64
import Data.ByteString.Short (ShortByteString, fromShort, toShort)
import Data.Text (Text)
import Data.Text.Short (ShortText)
import Data.Text.Short.Unsafe (fromShortByteStringUnsafe)

-- $setup
--
-- >>> import Data.Base64.Types
-- >>> :set -XOverloadedStrings
-- >>> :set -XTypeApplications
-- >>> :set -XDataKinds
--


-- | Encode a 'ShortByteString' value as Base64 'ShortText' with padding.
--
-- See: <https://tools.ietf.org/html/rfc4648#section-4 RFC-4648 section 4>
--
-- === __Examples__:
--
-- >>> encodeBase64 "Sun"
-- "U3Vu"
--
encodeBase64 :: ShortByteString -> Base64 'StdPadded ShortText
encodeBase64 :: ShortByteString -> Base64 'StdPadded ShortText
encodeBase64 = (ShortByteString -> ShortText)
-> Base64 'StdPadded ShortByteString -> Base64 'StdPadded ShortText
forall a b. (a -> b) -> Base64 'StdPadded a -> Base64 'StdPadded b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ShortByteString -> ShortText
fromShortByteStringUnsafe (Base64 'StdPadded ShortByteString -> Base64 'StdPadded ShortText)
-> (ShortByteString -> Base64 'StdPadded ShortByteString)
-> ShortByteString
-> Base64 'StdPadded ShortText
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> Base64 'StdPadded ShortByteString
encodeBase64'
{-# INLINE encodeBase64 #-}

-- | Encode a 'ShortByteString' value as a Base64 'ShortByteString'  value with padding.
--
-- See: <https://tools.ietf.org/html/rfc4648#section-4 RFC-4648 section 4>
--
-- === __Examples__:
--
-- >>> encodeBase64' "Sun"
-- "U3Vu"
--
encodeBase64' :: ShortByteString -> Base64 'StdPadded ShortByteString
encodeBase64' :: ShortByteString -> Base64 'StdPadded ShortByteString
encodeBase64' = (ByteString -> ShortByteString)
-> Base64 'StdPadded ByteString
-> Base64 'StdPadded ShortByteString
forall a b. (a -> b) -> Base64 'StdPadded a -> Base64 'StdPadded b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> ShortByteString
toShort (Base64 'StdPadded ByteString -> Base64 'StdPadded ShortByteString)
-> (ShortByteString -> Base64 'StdPadded ByteString)
-> ShortByteString
-> Base64 'StdPadded ShortByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Base64 'StdPadded ByteString
B64.encodeBase64' (ByteString -> Base64 'StdPadded ByteString)
-> (ShortByteString -> ByteString)
-> ShortByteString
-> Base64 'StdPadded ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> ByteString
fromShort
{-# INLINE encodeBase64' #-}

-- | Decode a padded Base64-encoded 'ShortByteString' value.
--
-- See: <https://tools.ietf.org/html/rfc4648#section-4 RFC-4648 section 4>
--
-- === __Examples__:
--
-- >>> decodeBase64 $ assertBase64 @'StdPadded "U3Vu"
-- "Sun"
--
decodeBase64
  :: StdAlphabet k
  => Base64 k ShortByteString
  -> ShortByteString
decodeBase64 :: forall (k :: Alphabet).
StdAlphabet k =>
Base64 k ShortByteString -> ShortByteString
decodeBase64 = ByteString -> ShortByteString
toShort (ByteString -> ShortByteString)
-> (Base64 k ShortByteString -> ByteString)
-> Base64 k ShortByteString
-> ShortByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Base64 k ByteString -> ByteString
forall (k :: Alphabet).
StdAlphabet k =>
Base64 k ByteString -> ByteString
B64.decodeBase64 (Base64 k ByteString -> ByteString)
-> (Base64 k ShortByteString -> Base64 k ByteString)
-> Base64 k ShortByteString
-> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ShortByteString -> ByteString)
-> Base64 k ShortByteString -> Base64 k ByteString
forall a b. (a -> b) -> Base64 k a -> Base64 k b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ShortByteString -> ByteString
fromShort
{-# INLINE decodeBase64 #-}

-- | Decode a padded Base64-encoded 'ShortByteString' value.
--
-- See: <https://tools.ietf.org/html/rfc4648#section-4 RFC-4648 section 4>
--
-- === __Examples__:
--
-- >>> decodeBase64Untyped "U3Vu"
-- Right "Sun"
--
-- >>> decodeBase64Untyped "U3V"
-- Left "Base64-encoded bytestring requires padding"
--
-- >>> decodeBase64Untyped "U3V="
-- Left "non-canonical encoding detected at offset: 2"
--
decodeBase64Untyped :: ShortByteString -> Either Text ShortByteString
decodeBase64Untyped :: ShortByteString -> Either Text ShortByteString
decodeBase64Untyped = (ByteString -> ShortByteString)
-> Either Text ByteString -> Either Text ShortByteString
forall a b. (a -> b) -> Either Text a -> Either Text b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> ShortByteString
toShort (Either Text ByteString -> Either Text ShortByteString)
-> (ShortByteString -> Either Text ByteString)
-> ShortByteString
-> Either Text ShortByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either Text ByteString
B64.decodeBase64Untyped (ByteString -> Either Text ByteString)
-> (ShortByteString -> ByteString)
-> ShortByteString
-> Either Text ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> ByteString
fromShort
{-# inline decodeBase64Untyped #-}

-- | Leniently decode an unpadded Base64-encoded 'ShortByteString' value. This function
-- will not generate parse errors. If input data contains padding chars,
-- then the input will be parsed up until the first pad character.
--
-- __Note:__ This is not RFC 4648-compliant.
--
-- === __Examples__:
--
-- >>> decodeBase64Lenient "U3Vu"
-- "Sun"
--
-- >>> decodeBase64Lenient "U3V"
-- "Su"
--
-- >>> decodeBase64Lenient "U3V="
-- "Su"
--
decodeBase64Lenient :: ShortByteString -> ShortByteString
decodeBase64Lenient :: ShortByteString -> ShortByteString
decodeBase64Lenient = ByteString -> ShortByteString
toShort (ByteString -> ShortByteString)
-> (ShortByteString -> ByteString)
-> ShortByteString
-> ShortByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
B64.decodeBase64Lenient (ByteString -> ByteString)
-> (ShortByteString -> ByteString) -> ShortByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> ByteString
fromShort
{-# INLINE decodeBase64Lenient #-}

-- | Parse a canonical, padded Base64-encoded 'ShortByteString' value.
--
-- Checks the alphabet, length, padding, and unused padding bits. On success,
-- returns the original encoded value wrapped in 'Base64', ready for
-- 'decodeBase64'. On failure, returns the same error as 'decodeBase64Untyped'.
-- The input is neither decoded nor normalized in the returned value.
--
-- === __Examples__:
--
-- >>> parseBase64 "U3Vu"
-- Right "U3Vu"
--
-- >>> fmap decodeBase64 (parseBase64 "U3Vu")
-- Right "Sun"
--
-- >>> parseBase64 "U3V"
-- Left "Base64-encoded bytestring requires padding"
--
-- >>> parseBase64 "U3V="
-- Left "non-canonical encoding detected at offset: 2"
--
-- @since 2.0
--
parseBase64 :: ShortByteString -> Either Text (Base64 'StdPadded ShortByteString)
parseBase64 :: ShortByteString -> Either Text (Base64 'StdPadded ShortByteString)
parseBase64 ShortByteString
bs = ShortByteString -> Base64 'StdPadded ShortByteString
forall (k :: Alphabet) a. a -> Base64 k a
assertBase64 ShortByteString
bs Base64 'StdPadded ShortByteString
-> Either Text (Base64 'StdPadded ByteString)
-> Either Text (Base64 'StdPadded ShortByteString)
forall a b. a -> Either Text b -> Either Text a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ByteString -> Either Text (Base64 'StdPadded ByteString)
B64.parseBase64 (ShortByteString -> ByteString
fromShort ShortByteString
bs)
{-# INLINE parseBase64 #-}