summaryrefslogtreecommitdiff
path: root/Utility
diff options
context:
space:
mode:
authorGravatar Joey Hess <joeyh@joeyh.name>2015-08-11 18:40:59 -0400
committerGravatar Joey Hess <joeyh@joeyh.name>2015-08-11 18:40:59 -0400
commit88aeb849f620a13da47508045daae461a223c997 (patch)
treea93b1d67d5fe887c7e958d9cffbea3d7014e496a /Utility
parent96705a943615528f79a121e6e94101d5852ba44f (diff)
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.)
Diffstat (limited to 'Utility')
-rw-r--r--Utility/Base64.hs9
-rw-r--r--Utility/FileSystemEncoding.hs11
2 files changed, 17 insertions, 3 deletions
diff --git a/Utility/Base64.hs b/Utility/Base64.hs
index 6ab3c984f..0e3087276 100644
--- a/Utility/Base64.hs
+++ b/Utility/Base64.hs
@@ -1,5 +1,8 @@
{- 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>
-
- License: BSD-2-clause
@@ -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
diff --git a/Utility/FileSystemEncoding.hs b/Utility/FileSystemEncoding.hs
index 41c5972a0..25a09ecc0 100644
--- a/Utility/FileSystemEncoding.hs
+++ b/Utility/FileSystemEncoding.hs
@@ -13,6 +13,7 @@ module Utility.FileSystemEncoding (
withFilePath,
md5FilePath,
decodeBS,
+ encodeBS,
decodeW8,
encodeW8,
encodeW8NUL,
@@ -81,13 +82,21 @@ md5FilePath = MD5.Str . _encodeFilePath
{- Decodes a ByteString into a FilePath, applying the filesystem encoding. -}
decodeBS :: L.ByteString -> FilePath
#ifndef mingw32_HOST_OS
-decodeBS = encodeW8 . L.unpack
+decodeBS = encodeW8NUL . L.unpack
#else
{- On Windows, we assume that the ByteString is utf-8, since Windows
- only uses unicode for filenames. -}
decodeBS = L8.toString
#endif
+{- Encodes a FilePath into a ByteString, applying the filesystem encoding. -}
+encodeBS :: FilePath -> L.ByteString
+#ifndef mingw32_HOST_OS
+encodeBS = L.pack . decodeW8NUL
+#else
+encodeBS = L8.fromString
+#endif
+
{- Converts a [Word8] to a FilePath, encoding using the filesystem encoding.
-
- w82c produces a String, which may contain Chars that are invalid