summaryrefslogtreecommitdiff
path: root/src/win32/utf-conv.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/win32/utf-conv.c')
-rw-r--r--src/win32/utf-conv.c23
1 files changed, 10 insertions, 13 deletions
diff --git a/src/win32/utf-conv.c b/src/win32/utf-conv.c
index 76f1e4237..0a705c0ad 100644
--- a/src/win32/utf-conv.c
+++ b/src/win32/utf-conv.c
@@ -32,19 +32,16 @@ void gitwin_set_utf8(void)
wchar_t* gitwin_to_utf16(const char* str)
{
wchar_t* ret;
- size_t cb;
+ int cb;
if (!str)
return NULL;
- cb = strlen(str) * sizeof(wchar_t);
+ cb = MultiByteToWideChar(_active_codepage, 0, str, -1, NULL, 0);
if (cb == 0)
return (wchar_t *)git__calloc(1, sizeof(wchar_t));
- /* Add space for null terminator */
- cb += sizeof(wchar_t);
-
- ret = (wchar_t *)git__malloc(cb);
+ ret = (wchar_t *)git__malloc(cb * sizeof(wchar_t));
if (!ret)
return NULL;
@@ -59,7 +56,8 @@ wchar_t* gitwin_to_utf16(const char* str)
int gitwin_append_utf16(wchar_t *buffer, const char *str, size_t len)
{
- int result = MultiByteToWideChar(_active_codepage, 0, str, -1, buffer, (int)len);
+ int result = MultiByteToWideChar(
+ _active_codepage, 0, str, -1, buffer, (int)len);
if (result == 0)
giterr_set(GITERR_OS, "Could not convert string to UTF-16");
return result;
@@ -68,23 +66,22 @@ int gitwin_append_utf16(wchar_t *buffer, const char *str, size_t len)
char* gitwin_from_utf16(const wchar_t* str)
{
char* ret;
- size_t cb;
+ int cb;
if (!str)
return NULL;
- cb = wcslen(str) * sizeof(char);
+ cb = WideCharToMultiByte(_active_codepage, 0, str, -1, NULL, 0, NULL, NULL);
if (cb == 0)
return (char *)git__calloc(1, sizeof(char));
- /* Add space for null terminator */
- cb += sizeof(char);
-
ret = (char*)git__malloc(cb);
if (!ret)
return NULL;
- if (WideCharToMultiByte(_active_codepage, 0, str, -1, ret, (int)cb, NULL, NULL) == 0) {
+ if (WideCharToMultiByte(
+ _active_codepage, 0, str, -1, ret, (int)cb, NULL, NULL) == 0)
+ {
giterr_set(GITERR_OS, "Could not convert string to UTF-8");
git__free(ret);
ret = NULL;