From 98a1b663629838763e0022a45e2010190a71c72e Mon Sep 17 00:00:00 2001 From: Daniel Trick Date: Mon, 14 Jun 2021 14:55:29 +0200 Subject: [PATCH 1/3] Make curl_getenv() work correctly in Unicode build. --- lib/getenv.c | 43 ++++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/lib/getenv.c b/lib/getenv.c index 92c53505c688..bb67ab008607 100644 --- a/lib/getenv.c +++ b/lib/getenv.c @@ -27,22 +27,19 @@ #include "memdebug.h" -static char *GetEnv(const char *variable) +#ifdef(WIN32) +static TCHAR GetEnvWin32(const TCHAR *variable) { -#if defined(_WIN32_WCE) || defined(CURL_WINDOWS_APP) - (void)variable; - return NULL; -#elif defined(WIN32) /* This uses Windows API instead of C runtime getenv() to get the environment variable since some changes aren't always visible to the latter. #4774 */ - char *buf = NULL; - char *tmp; + TCHAR *buf = NULL; + TCHAR *tmp; DWORD bufsize; DWORD rc = 1; const DWORD max = 32768; /* max env var size from MSCRT source */ for(;;) { - tmp = realloc(buf, rc); + tmp = (TCHAR*)realloc(buf, rc * sizeof(TCHAR)); if(!tmp) { free(buf); return NULL; @@ -53,18 +50,42 @@ static char *GetEnv(const char *variable) /* It's possible for rc to be 0 if the variable was found but empty. Since getenv doesn't make that distinction we ignore it as well. */ - rc = GetEnvironmentVariableA(variable, buf, bufsize); + rc = GetEnvironmentVariable(variable, buf, bufsize); if(!rc || rc == bufsize || rc > max) { free(buf); return NULL; } /* if rc < bufsize then rc is bytes written not including null */ - if(rc < bufsize) + if(rc < bufsize) { return buf; - + } /* else rc is bytes needed, try again */ } +} +#endif + +static char *GetEnv(const char *variable) +{ +#if defined(_WIN32_WCE) || defined(CURL_WINDOWS_APP) + (void)variable; + return NULL; +#elif defined(WIN32) +#ifdef UNICODE + char *value = NULL; + wchar_t *variable_w = curlx_convert_UTF8_to_wchar(variable); + if(variable_w) { + wchar_t *value_w = GetEnvWin32(variable_w); + if(value_w) { + value = curlx_convert_wchar_to_UTF8(value_w); + free(value_w); + } + free(variable_w); + } + return value; +#else + return GetEnvWin32(variable); +#endif #else char *env = getenv(variable); return (env && env[0])?strdup(env):NULL; From a48f7ef791d86677a28c3061f6237fc42cb9388f Mon Sep 17 00:00:00 2001 From: Daniel Trick Date: Mon, 14 Jun 2021 15:15:40 +0200 Subject: [PATCH 2/3] Small fix. --- lib/getenv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/getenv.c b/lib/getenv.c index bb67ab008607..fc4c71fa7df1 100644 --- a/lib/getenv.c +++ b/lib/getenv.c @@ -27,7 +27,7 @@ #include "memdebug.h" -#ifdef(WIN32) +#ifdef WIN32 static TCHAR GetEnvWin32(const TCHAR *variable) { /* This uses Windows API instead of C runtime getenv() to get the environment From 7a11837a617c0bb4e0024fd168693b09da6cc4b7 Mon Sep 17 00:00:00 2001 From: Daniel Trick Date: Mon, 14 Jun 2021 16:19:51 +0200 Subject: [PATCH 3/3] Small fix. --- lib/getenv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/getenv.c b/lib/getenv.c index fc4c71fa7df1..f73d00bca94e 100644 --- a/lib/getenv.c +++ b/lib/getenv.c @@ -28,7 +28,7 @@ #include "memdebug.h" #ifdef WIN32 -static TCHAR GetEnvWin32(const TCHAR *variable) +static TCHAR *GetEnvWin32(const TCHAR *variable) { /* This uses Windows API instead of C runtime getenv() to get the environment variable since some changes aren't always visible to the latter. #4774 */