From a68c1975bda47b0698b48761a62e95b371095e9a Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Fri, 21 Aug 2020 21:11:21 +0200 Subject: core,w32: Add UTF-8 support to gpgrt_fopen, gpgrt_mkdir and gpgrt_chdir. * src/protos.h: New. * src/Makefile.am (libgpg_error_la_SOURCES): Add file. * src/gpgrt-int.h: Include protos.h. * src/sysutils.c (_gpgrt_mkdir) [W32]: Make UTF-8 aware. (_gpgrt_chdir) [W32]: Ditto. * src/w32-gettext.c: Include protos.h. (utf8_to_wchar): Allow for strings. (_gpgrt_utf8_to_wchar): New. (_gpgrt_free_wchar): New. * src/estream.c (map_w32_to_errno): Add more error codes. (_gpgrt_w32_set_errno): New. (any8bitchar) [W32]: New helper. (func_file_create) [W32]: Convert file name and use _wopen. -- GnuPG-bug-id: 4083 Signed-off-by: Werner Koch --- src/w32-gettext.c | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) (limited to 'src/w32-gettext.c') diff --git a/src/w32-gettext.c b/src/w32-gettext.c index 11e4f3d..a734ef2 100644 --- a/src/w32-gettext.c +++ b/src/w32-gettext.c @@ -52,6 +52,7 @@ #include "init.h" #include "gpg-error.h" +#include "protos.h" /* Override values initialized by gpgrt_w32_override_locale. If NAME * is not the empty string LANGID will be used. */ @@ -1354,15 +1355,23 @@ load_domain (const char *filename) /* Return a malloced wide char string from an UTF-8 encoded input string STRING. Caller must free this value. On failure returns NULL. The result of calling this function with STRING set to NULL - is not defined. */ + is not defined. If LENGTH is zero and RETLEN NULL the fucntion + assumes that STRING is a nul-terminated string and returns a + (wchar_t)0-terminated string. */ static wchar_t * utf8_to_wchar (const char *string, size_t length, size_t *retlen) { int n; wchar_t *result; size_t nbytes; + int cbmultibyte; + + if (!length && !retlen) + cbmultibyte = -1; + else + cbmultibyte = length; - n = MultiByteToWideChar (CP_UTF8, 0, string, length, NULL, 0); + n = MultiByteToWideChar (CP_UTF8, 0, string, cbmultibyte, NULL, 0); if (n < 0 || (n+1) <= 0) return NULL; @@ -1376,17 +1385,38 @@ utf8_to_wchar (const char *string, size_t length, size_t *retlen) if (!result) return NULL; - n = MultiByteToWideChar (CP_UTF8, 0, string, length, result, n); + n = MultiByteToWideChar (CP_UTF8, 0, string, cbmultibyte, result, n); if (n < 0) { jnlib_free (result); return NULL; } - *retlen = n; + if (retlen) + *retlen = n; return result; } +/* Convert an UTF8 string to a WCHAR string. Caller should use + * _gpgrt_free_wchar to release the result. */ +wchar_t * +_gpgrt_utf8_to_wchar (const char *string) +{ + return utf8_to_wchar (string, 0, NULL); +} + + +/* We provide a dedicated release function to be sure that we don't + * use a somehow mapped free function but the one which matches the + * used alloc. */ +void +_gpgrt_free_wchar (wchar_t *wstring) +{ + if (wstring) + jnlib_free (wstring); +} + + /* Return a malloced string encoded in the native console codepage from the wide char input string STRING. Caller must free this value. On failure returns NULL. @@ -1445,7 +1475,6 @@ utf8_to_native (const char *string, size_t length, size_t *retlen) } - /* Specify that the DOMAINNAME message catalog will be found in DIRNAME rather than in the system locale data base. */ -- cgit v1.2.3