aboutsummaryrefslogtreecommitdiffstats
path: root/src/init.c
diff options
context:
space:
mode:
authorWerner Koch <[email protected]>2010-01-18 20:14:25 +0000
committerWerner Koch <[email protected]>2010-01-18 20:14:25 +0000
commite4da3564e0dee1dfd39ac7891dad8d2fb7cd6be0 (patch)
tree7deee1221410c3801c0ef298f0a00ac6689a6cad /src/init.c
parentAdd some code to build nativley under WindowsCE - not finished. (diff)
downloadlibgpg-error-e4da3564e0dee1dfd39ac7891dad8d2fb7cd6be0.tar.gz
libgpg-error-e4da3564e0dee1dfd39ac7891dad8d2fb7cd6be0.zip
Fix w32ce strerror.
Fix non-w32ce dependency problem.
Diffstat (limited to 'src/init.c')
-rw-r--r--src/init.c89
1 files changed, 83 insertions, 6 deletions
diff --git a/src/init.c b/src/init.c
index c9e0047..5a5ac90 100644
--- a/src/init.c
+++ b/src/init.c
@@ -35,13 +35,16 @@
/* Locale directory support. */
#if HAVE_W32_SYSTEM
+
+/* 119 bytes for an error message should be enough. With this size we
+ can assume that the allocation does not take up more than 128 bytes
+ per thread. */
+#define STRBUFFER_SIZE 120
+
/* The TLS space definition. */
struct tls_space_s
{
- /* 119 bytes for an error message should be enough. With this size
- we can assume that the allocation does not take up more than 128
- bytes per thread. */
- char strerror_buffer[120];
+ char strerror_buffer[STRBUFFER_SIZE];
};
static int tls_index; /* Index for the TLS functions. */
@@ -82,6 +85,68 @@ gpg_err_init (void)
#include <windows.h>
+/* Return a malloced string encoded in UTF-8 from the wide char input
+ string STRING. Caller must free this value. Returns NULL on
+ failure. Caller may use GetLastError to get the actual error
+ number. The result of calling this function with STRING set to
+ NULL is not defined. */
+static char *
+wchar_to_utf8 (const wchar_t *string)
+{
+ int n;
+ char *result;
+
+ /* Note, that CP_UTF8 is not defined in Windows versions earlier
+ than NT. */
+ n = WideCharToMultiByte (CP_UTF8, 0, string, -1, NULL, 0, NULL, NULL);
+ if (n < 0)
+ return NULL;
+
+ result = malloc (n+1);
+ if (result)
+ {
+ n = WideCharToMultiByte (CP_UTF8, 0, string, -1, result, n, NULL, NULL);
+ if (n < 0)
+ {
+ free (result);
+ result = NULL;
+ }
+ }
+ return result;
+}
+
+
+/* Return a malloced wide char string from an UTF-8 encoded input
+ string STRING. Caller must free this value. Returns NULL on
+ failure. Caller may use GetLastError to get the actual error
+ number. The result of calling this function with STRING set to
+ NULL is not defined. */
+static wchar_t *
+utf8_to_wchar (const char *string)
+{
+ int n;
+ wchar_t *result;
+
+ n = MultiByteToWideChar (CP_UTF8, 0, string, -1, NULL, 0);
+ if (n < 0)
+ return NULL;
+
+ result = malloc ((n+1) * sizeof *result);
+ if (result)
+ {
+ n = MultiByteToWideChar (CP_UTF8, 0, string, -1, result, n);
+ if (n < 0)
+ {
+ free (result);
+ result = NULL;
+ }
+ return NULL;
+ }
+ return result;
+}
+
+
+
static HKEY
get_root_key(const char *root)
{
@@ -295,13 +360,25 @@ char *
_gpg_w32ce_strerror (int err)
{
struct tls_space_s *tls = get_tls ();
+ wchar_t tmpbuf[STRBUFFER_SIZE];
+ int n;
if (err == -1)
err = _gpg_w32ce_get_errno ();
- if (!FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, err,
+ if (FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM, NULL, err,
MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
- tls->strerror_buffer, sizeof tls->strerror_buffer -1,
+ tmpbuf, STRBUFFER_SIZE -1,
NULL))
+ {
+ n = WideCharToMultiByte (CP_UTF8, 0, tmpbuf, -1,
+ tls->strerror_buffer,
+ sizeof tls->strerror_buffer -1,
+ NULL, NULL);
+ }
+ else
+ n = -1;
+
+ if (n < 0)
snprintf (tls->strerror_buffer, sizeof tls->strerror_buffer -1,
"[w32err=%d]", err);
return tls->strerror_buffer;