msvcrt: Implement opening Unicode files with no BOM in _wsopen_dispatch.

Spotted by Alistair Leslie-Hughes.

Signed-off-by: Piotr Caban <piotr@codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Piotr Caban 2020-11-26 15:26:35 +01:00 committed by Alexandre Julliard
parent 00a024725e
commit b0ab1b7602
2 changed files with 51 additions and 6 deletions

View File

@ -2174,17 +2174,15 @@ static int check_bom(HANDLE h, int oflags, BOOL seek)
char bom[sizeof(utf8_bom)];
DWORD r;
oflags &= ~(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT);
if (!ReadFile(h, bom, sizeof(utf8_bom), &r, NULL))
return oflags;
if (r==sizeof(utf8_bom) && !memcmp(bom, utf8_bom, sizeof(utf8_bom))) {
oflags |= MSVCRT__O_U8TEXT;
oflags = (oflags & ~(MSVCRT__O_WTEXT | MSVCRT__O_U16TEXT)) | MSVCRT__O_U8TEXT;
}else if (r>=sizeof(utf16_bom) && !memcmp(bom, utf16_bom, sizeof(utf16_bom))) {
if (seek && r>2)
SetFilePointer(h, 2, NULL, FILE_BEGIN);
oflags |= MSVCRT__O_U16TEXT;
oflags = (oflags & ~(MSVCRT__O_WTEXT | MSVCRT__O_U8TEXT)) | MSVCRT__O_U16TEXT;
}else if (seek) {
SetFilePointer(h, 0, NULL, FILE_BEGIN);
}
@ -2284,8 +2282,6 @@ int CDECL MSVCRT__wsopen_dispatch( const MSVCRT_wchar_t* path, int oflags, int s
oflags = check_bom(hand, oflags, FALSE);
CloseHandle(hand);
}
else
oflags &= ~(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT);
}
hand = CreateFileW(path, access, sharing, &sa, creation, attrib, 0);

View File

@ -971,6 +971,38 @@ static void test_fgetwc_unicode(void)
ok(ch == WEOF, "got %04hx, expected WEOF (unicode)\n", ch);
fclose(tempfh);
tempfh = fopen(tempfile, "r,ccs=utf-8");
ok(tempfh != NULL, "can't open tempfile\n");
for (i = 1; i < ARRAY_SIZE(wchar_text); i++)
{
ch = fgetwc(tempfh);
ok(ch == wchar_text[i],
"got %04hx, expected %04x (unicode[%d])\n", ch, wchar_text[i], i-1);
}
ch = fgetwc(tempfh);
ok(ch == WEOF, "got %04hx, expected WEOF (unicode)\n", ch);
fclose(tempfh);
tempfh = fopen(tempfile, "a,ccs=utf-16le");
ok(tempfh != NULL, "can't open tempfile\n");
ch = fputwc('a', tempfh);
ok(ch == 'a', "fputwc returned %x\n", ch);
fclose(tempfh);
tempfh = fopen(tempfile, "a+,ccs=utf-8");
ok(tempfh != NULL, "can't open tempfile\n");
for (i = 1; i < ARRAY_SIZE(wchar_text); i++)
{
ch = fgetwc(tempfh);
ok(ch == wchar_text[i],
"got %04hx, expected %04x (unicode[%d])\n", ch, wchar_text[i], i-1);
}
ch = fgetwc(tempfh);
ok(ch == 'a', "got %04x, expected 'a'\n", ch);
ch = fgetwc(tempfh);
ok(ch == WEOF, "got %04hx, expected WEOF (unicode)\n", ch);
fclose(tempfh);
tempfh = fopen(tempfile, "wb");
ok(tempfh != NULL, "can't open tempfile\n");
ret = WideCharToMultiByte(CP_UTF8, 0, wchar_text, ARRAY_SIZE(wchar_text),
@ -990,6 +1022,23 @@ static void test_fgetwc_unicode(void)
ch = fgetwc(tempfh);
ok(ch == WEOF, "got %04hx, expected WEOF (utf8)\n", ch);
fclose(tempfh);
tempfh = fopen(tempfile, "wb");
ok(tempfh != NULL, "can't open tempfile\n");
fwrite(wchar_text+1, 1, sizeof(wchar_text)-1, tempfh);
fclose(tempfh);
tempfh = fopen(tempfile, "rt,ccs=utf-16le");
ok(tempfh != NULL, "can't open tempfile\n");
for (i = 1; i < ARRAY_SIZE(wchar_text); i++)
{
ch = fgetwc(tempfh);
ok(ch == wchar_text[i],
"got %04hx, expected %04x (unicode[%d])\n", ch, wchar_text[i], i-1);
}
ch = fgetwc(tempfh);
ok(ch == WEOF, "got %04hx, expected WEOF (unicode)\n", ch);
fclose(tempfh);
unlink(temppath);
}