2019-01-07 18:18:24 +00: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 22:40:59 +00:00
|
|
|
-
|
2023-10-26 16:42:32 +00:00
|
|
|
- Copyright 2011-2023 Joey Hess <id@joeyh.name>
|
2011-05-01 18:27:40 +00:00
|
|
|
-
|
2014-05-10 14:01:27 +00:00
|
|
|
- License: BSD-2-clause
|
2011-05-01 18:27:40 +00:00
|
|
|
-}
|
|
|
|
|
2019-01-07 18:18:24 +00:00
|
|
|
module Utility.Base64 where
|
2011-05-01 18:27:40 +00:00
|
|
|
|
2023-04-10 17:38:14 +00:00
|
|
|
import Utility.Exception
|
2015-08-12 14:36:51 +00:00
|
|
|
|
2023-10-26 16:42:32 +00:00
|
|
|
import Codec.Binary.Base64 as B64
|
2013-03-06 20:29:19 +00:00
|
|
|
import Data.Maybe
|
2019-01-07 18:18:24 +00:00
|
|
|
import qualified Data.ByteString as B
|
2011-05-01 18:27:40 +00:00
|
|
|
|
2023-10-26 16:42:32 +00:00
|
|
|
toB64 :: B.ByteString -> B.ByteString
|
|
|
|
toB64 = B64.encode
|
2019-01-07 18:18:24 +00:00
|
|
|
|
2023-10-26 16:42:32 +00:00
|
|
|
fromB64Maybe :: B.ByteString -> Maybe (B.ByteString)
|
|
|
|
fromB64Maybe = either (const Nothing) Just . B64.decode
|
2013-03-06 20:29:19 +00:00
|
|
|
|
2023-10-26 16:42:32 +00:00
|
|
|
fromB64 :: B.ByteString -> B.ByteString
|
2013-03-06 20:29:19 +00:00
|
|
|
fromB64 = fromMaybe bad . fromB64Maybe
|
|
|
|
where
|
2023-04-10 17:38:14 +00:00
|
|
|
bad = giveup "bad base64 encoded data"
|