From 85ddb646afd1c57b00f421284356e35cc5fa989e Mon Sep 17 00:00:00 2001 From: Moritz Angermann Date: Sun, 13 Aug 2023 01:15:11 +0800 Subject: [PATCH] core: explicitly set encoding to utf-8 (fixes unicode filenames on mobile) (#2908) * Explicitly set encoding to utf-8 on mobile GHC's rts tries to obtain the encoding from iconv, however this is not really available on iOS. It therefore defaults to US-ASCII, and then breaks with unicode data. We now explicitly set this to utf-8 here. * ormolu --------- Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> --- src/Simplex/Chat/Mobile.hs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Simplex/Chat/Mobile.hs b/src/Simplex/Chat/Mobile.hs index 85900a6d72..6e62fbce06 100644 --- a/src/Simplex/Chat/Mobile.hs +++ b/src/Simplex/Chat/Mobile.hs @@ -30,6 +30,7 @@ import Foreign.C.Types (CInt (..)) import Foreign.Ptr import Foreign.StablePtr import Foreign.Storable (poke) +import GHC.IO.Encoding (setLocaleEncoding, setFileSystemEncoding, setForeignEncoding) import GHC.Generics (Generic) import Simplex.Chat import Simplex.Chat.Controller @@ -47,6 +48,7 @@ import Simplex.Messaging.Encoding.String import Simplex.Messaging.Parsers (dropPrefix, sumTypeJSON) import Simplex.Messaging.Protocol (AProtoServerWithAuth (..), AProtocolType (..), BasicAuth (..), CorrId (..), ProtoServerWithAuth (..), ProtocolServer (..)) import Simplex.Messaging.Util (catchAll, liftEitherWith, safeDecodeUtf8) +import System.IO (utf8) import System.Timeout (timeout) foreign export ccall "chat_migrate_init" cChatMigrateInit :: CString -> CString -> CString -> Ptr (StablePtr ChatController) -> IO CJSONString @@ -70,6 +72,12 @@ foreign export ccall "chat_decrypt_media" cChatDecryptMedia :: CString -> Ptr Wo -- | check / migrate database and initialize chat controller on success cChatMigrateInit :: CString -> CString -> CString -> Ptr (StablePtr ChatController) -> IO CJSONString cChatMigrateInit fp key conf ctrl = do + -- ensure we are set to UTF-8; iOS does not have locale, and will default to + -- US-ASCII all the time. + setLocaleEncoding utf8 + setFileSystemEncoding utf8 + setForeignEncoding utf8 + dbPath <- peekCAString fp dbKey <- peekCAString key confirm <- peekCAString conf