2019-01-07 14:18:24 -04:00
|
|
|
{- Simple Base64 encoding
|
Fix setting/setting/viewing metadata that contains unicode or other special characters, when in a non-unicode locale.
Oh boy, not again. So, another place that the filesystem encoding needs to
be applied. Yay.
In passing, I changed decodeBS so if a NUL is embedded in the input, the
resulting FilePath doesn't get truncated at that NUL. This was needed to
make prop_b64_roundtrips pass, and on reviewing the callers of decodeBS, I
didn't see any where this wouldn't make sense. When a FilePath is used to
operate on the filesystem, it'll get truncated at a NUL anyway, whereas if
a String is being used for something else, it might conceivably have a NUL
in it, and we wouldn't want it to get truncated when going through
decodeBS.
(NB: There may be a speed impact from this change.)
2015-08-11 18:40:59 -04:00
|
|
|
-
|
2023-10-26 12:42:32 -04:00
|
|
|
- Copyright 2011-2023 Joey Hess <id@joeyh.name>
|
2011-05-01 14:27:40 -04:00
|
|
|
-
|
2014-05-10 11:01:27 -03:00
|
|
|
- License: BSD-2-clause
|
2011-05-01 14:27:40 -04:00
|
|
|
-}
|
|
|
|
|
2019-01-07 14:18:24 -04:00
|
|
|
module Utility.Base64 where
|
2011-05-01 14:27:40 -04:00
|
|
|
|
2023-04-10 13:38:14 -04:00
|
|
|
import Utility.Exception
|
2015-08-12 10:36:51 -04:00
|
|
|
|
2023-10-26 12:42:32 -04:00
|
|
|
import Codec.Binary.Base64 as B64
|
2013-03-06 16:29:19 -04:00
|
|
|
import Data.Maybe
|
2019-01-07 14:18:24 -04:00
|
|
|
import qualified Data.ByteString as B
|
2011-05-01 14:27:40 -04:00
|
|
|
|
2023-10-26 12:42:32 -04:00
|
|
|
toB64 :: B.ByteString -> B.ByteString
|
|
|
|
toB64 = B64.encode
|
2019-01-07 14:18:24 -04:00
|
|
|
|
2023-10-26 12:42:32 -04:00
|
|
|
fromB64Maybe :: B.ByteString -> Maybe (B.ByteString)
|
|
|
|
fromB64Maybe = either (const Nothing) Just . B64.decode
|
2013-03-06 16:29:19 -04:00
|
|
|
|
2023-10-26 12:42:32 -04:00
|
|
|
fromB64 :: B.ByteString -> B.ByteString
|
2013-03-06 16:29:19 -04:00
|
|
|
fromB64 = fromMaybe bad . fromB64Maybe
|
|
|
|
where
|
2023-04-10 13:38:14 -04:00
|
|
|
bad = giveup "bad base64 encoded data"
|