aboutsummaryrefslogtreecommitdiffstats
path: root/src/w32-gettext.c
diff options
context:
space:
mode:
authorWerner Koch <[email protected]>2020-08-21 19:11:21 +0000
committerWerner Koch <[email protected]>2020-08-21 19:11:21 +0000
commita68c1975bda47b0698b48761a62e95b371095e9a (patch)
treebc5220f82e0da76eaa44e3601773e04c4e470bac /src/w32-gettext.c
parentargparse: Handle a corner case of wrong ARGC. (diff)
downloadlibgpg-error-a68c1975bda47b0698b48761a62e95b371095e9a.tar.gz
libgpg-error-a68c1975bda47b0698b48761a62e95b371095e9a.zip
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 <[email protected]>
Diffstat (limited to 'src/w32-gettext.c')
-rw-r--r--src/w32-gettext.c39
1 files changed, 34 insertions, 5 deletions
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. */