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.)
This commit is contained in:
Joey Hess 2015-08-11 18:40:59 -04:00
parent 1e40bfda49
commit 23e9d3bb77
5 changed files with 45 additions and 4 deletions

View file

@ -1,4 +1,7 @@
{- Simple Base64 encoding of Strings
-
- Note that this uses the FileSystemEncoding, so it can be used on Strings
- that repesent filepaths containing arbitrarily encoded characters.
-
- Copyright 2011, 2015 Joey Hess <id@joeyh.name>
-
@ -9,13 +12,15 @@ module Utility.Base64 (toB64, fromB64Maybe, fromB64, prop_b64_roundtrips) where
import qualified "sandi" Codec.Binary.Base64 as B64
import Data.Maybe
import qualified Data.ByteString.Lazy as L
import Data.ByteString.UTF8 (fromString, toString)
import Utility.FileSystemEncoding
toB64 :: String -> String
toB64 = toString . B64.encode . fromString
toB64 = toString . B64.encode . L.toStrict . encodeBS
fromB64Maybe :: String -> Maybe String
fromB64Maybe s = either (const Nothing) (Just . toString)
fromB64Maybe s = either (const Nothing) (Just . decodeBS . L.fromStrict)
(B64.decode $ fromString s)
fromB64 :: String -> String