Commit b0ab1b76 authored by Piotr Caban's avatar Piotr Caban Committed by Alexandre Julliard

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

Spotted by Alistair Leslie-Hughes. Signed-off-by: 's avatarPiotr Caban <piotr@codeweavers.com> Signed-off-by: 's avatarAlexandre Julliard <julliard@winehq.org>
parent 00a02472
......@@ -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);
......
......@@ -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);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment