[PATCH] msvcrt: Fix the btowc() function.
Chip Davis
cdavis at codeweavers.com
Sun Jan 26 20:13:36 CST 2020
This function returns WEOF on conversion failure. It should only operate
on single-byte characters in the C locale. Finally, it should use the
current locale's codepage, instead of the current locale itself, to do
conversions.
Signed-off-by: Chip Davis <cdavis at codeweavers.com>
---
dlls/msvcrt/locale.c | 8 +++--
dlls/msvcrt/tests/string.c | 73 ++++++++++++++++++++++++++++++++++++++
2 files changed, 78 insertions(+), 3 deletions(-)
diff --git a/dlls/msvcrt/locale.c b/dlls/msvcrt/locale.c
index ae9f7c9c2166..02b74554e6bc 100644
--- a/dlls/msvcrt/locale.c
+++ b/dlls/msvcrt/locale.c
@@ -786,9 +786,11 @@ MSVCRT_wint_t CDECL MSVCRT_btowc(int c)
unsigned char letter = c;
MSVCRT_wchar_t ret;
- if(!MultiByteToWideChar(get_locinfo()->lc_handle[MSVCRT_LC_CTYPE],
- 0, (LPCSTR)&letter, 1, &ret, 1))
- return 0;
+ if(!get_locinfo()->lc_codepage)
+ return c & 255;
+ if(!MultiByteToWideChar(get_locinfo()->lc_codepage,
+ MB_ERR_INVALID_CHARS, (LPCSTR)&letter, 1, &ret, 1))
+ return MSVCRT_WEOF;
return ret;
}
diff --git a/dlls/msvcrt/tests/string.c b/dlls/msvcrt/tests/string.c
index da0400fa832f..7de776c65bf8 100644
--- a/dlls/msvcrt/tests/string.c
+++ b/dlls/msvcrt/tests/string.c
@@ -90,6 +90,7 @@ static int (__cdecl *p_wcslwr_s)(wchar_t*,size_t);
static errno_t (__cdecl *p_mbsupr_s)(unsigned char *str, size_t numberOfElements);
static errno_t (__cdecl *p_mbslwr_s)(unsigned char *str, size_t numberOfElements);
static int (__cdecl *p_wctob)(wint_t);
+static wint_t (__cdecl *p_btowc)(int);
static size_t (__cdecl *p_wcrtomb)(char*, wchar_t, mbstate_t*);
static int (__cdecl *p_wcrtomb_s)(size_t*, char*, size_t, wchar_t, mbstate_t*);
static int (__cdecl *p_tolower)(int);
@@ -2880,6 +2881,76 @@ static void test_wctob(void)
_setmbcp(cp);
}
+static void test_btowc(void)
+{
+ wint_t ret;
+ int cp = _getmbcp();
+
+ if(!p_btowc || !setlocale(LC_ALL, "chinese-traditional")) {
+ win_skip("Skipping btowc tests\n");
+ return;
+ }
+
+ ret = p_btowc(0x61);
+ ok(ret == 0x61, "ret = %x\n", ret);
+
+ ret = p_btowc(0x81);
+ ok(ret == WEOF, "ret = %x\n", ret);
+
+ ret = p_btowc(0xe0);
+ ok(ret == WEOF, "ret = %x\n", ret);
+
+ _setmbcp(1250);
+ ret = p_btowc(0x61);
+ ok(ret == 0x61, "ret = %x\n", ret);
+
+ ret = p_btowc(0x81);
+ ok(ret == WEOF, "ret = %x\n", ret);
+
+ ret = p_btowc(0xe0);
+ ok(ret == WEOF, "ret = %x\n", ret);
+
+ setlocale(LC_ALL, "C");
+ ret = p_btowc(0x61);
+ ok(ret == 0x61, "ret = %x\n", ret);
+
+ ret = p_btowc(0x81);
+ ok(ret == 0x81, "ret = %x\n", ret);
+
+ ret = p_btowc(0x9f);
+ ok(ret == 0x9f, "ret = %x\n", ret);
+
+ ret = p_btowc(0xe0);
+ ok(ret == 0xe0, "ret = %x\n", ret);
+
+ ret = p_btowc(0x100);
+ ok(ret == 0x00, "ret = %x\n", ret);
+
+ ret = p_btowc(0x1e0);
+ ok(ret == 0xe0, "ret = %x\n", ret);
+
+ _setmbcp(0);
+ ret = p_btowc(0x61);
+ ok(ret == 0x61, "ret = %x\n", ret);
+
+ ret = p_btowc(0x81);
+ ok(ret == 0x81, "ret = %x\n", ret);
+
+ ret = p_btowc(0x9f);
+ ok(ret == 0x9f, "ret = %x\n", ret);
+
+ ret = p_btowc(0xe0);
+ ok(ret == 0xe0, "ret = %x\n", ret);
+
+ ret = p_btowc(0x100);
+ ok(ret == 0x00, "ret = %x\n", ret);
+
+ ret = p_btowc(0x1e0);
+ ok(ret == 0xe0, "ret = %x\n", ret);
+
+ _setmbcp(cp);
+}
+
static void test_wctomb(void)
{
mbstate_t state;
@@ -4127,6 +4198,7 @@ START_TEST(string)
p_wcslwr_s = (void*)GetProcAddress(hMsvcrt, "_wcslwr_s");
p_mbsupr_s = (void*)GetProcAddress(hMsvcrt, "_mbsupr_s");
p_mbslwr_s = (void*)GetProcAddress(hMsvcrt, "_mbslwr_s");
+ p_btowc = (void*)GetProcAddress(hMsvcrt, "btowc");
p_wctob = (void*)GetProcAddress(hMsvcrt, "wctob");
p_wcrtomb = (void*)GetProcAddress(hMsvcrt, "wcrtomb");
p_wcrtomb_s = (void*)GetProcAddress(hMsvcrt, "wcrtomb_s");
@@ -4204,6 +4276,7 @@ START_TEST(string)
test__mbsupr_s();
test__mbslwr_s();
test_wctob();
+ test_btowc();
test_wctomb();
test__atodbl();
test__stricmp();
--
2.24.0
More information about the wine-devel
mailing list