Skip to content

Commit

Permalink
StringToUTF8(): Don't include the terminating \0 in the returned length.
Browse files Browse the repository at this point in the history
The majority of Win32 API functions we cover actually behave like this, with
GetCurrentDirectory() being the only exception. And yes, this indeed fixes
some weird bugs in applications that use the Borland VCL library, and EditDisk
in particular.
  • Loading branch information
nmlgc committed Jun 14, 2015
1 parent 9401007 commit 6869156
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/utf.c
Expand Up @@ -32,7 +32,10 @@ wchar_t* StringToUTF16_VLA(wchar_t *str_w, const char *str_mb, int str_len)

int StringToUTF8(char *str_utf8, const wchar_t *str_w, int str_utf8_len)
{
return WideCharToMultiByte(CP_UTF8, 0, str_w, -1, str_utf8, str_utf8_len, NULL, NULL);
int ret = WideCharToMultiByte(
CP_UTF8, 0, str_w, -1, str_utf8, str_utf8_len, NULL, NULL
);
return str_w ? ret - 1 : ret;
}

int StringToMBFixed(char *str_mb, const wchar_t *str_w, int str_mb_len, int str_w_len)
Expand Down
3 changes: 2 additions & 1 deletion src/utf.h
Expand Up @@ -30,7 +30,8 @@ int StringToUTF16(wchar_t *str_w, const char *str_mb, int str_len);
// [str_mb] (and thus, [str_w]) point to a valid string.
wchar_t* StringToUTF16_VLA(wchar_t *str_w, const char *str_mb, int str_len);

// Converts a UTF-16 string to UTF-8.
// Converts a null-terminated UTF-16 string to UTF-8 and returns the
// converted size excluding the terminating null character.
// [str_utf8_len] takes the size of [str_utf8] in bytes.
int StringToUTF8(char *str_utf8, const wchar_t *str_w, int str_utf8_len);

Expand Down
2 changes: 1 addition & 1 deletion src/wrappers.c
Expand Up @@ -41,7 +41,7 @@ DWORD WINAPI WrapGetString(
// Hey, let's be nice and return the _actual_ length.
VLA(wchar_t, lpBufferReal_w, ret);
func(ret, lpBufferReal_w);
ret = StringToUTF8(NULL, lpBufferReal_w, 0);
ret = StringToUTF8(NULL, lpBufferReal_w, 0) + 1;
VLA_FREE(lpBufferReal_w);
}
VLA_FREE(lpBuffer_w);
Expand Down

0 comments on commit 6869156

Please sign in to comment.