Hi Dmitry,

On Wed, Nov 7, 2012 at 11:25 AM, Dmitry Timoshkov <[email protected]> wrote:
> So, how is this supposed to work under Windows?

I wrote a test case, to my surprise, Windows doesn't support UTF8 .inf
file, no matter with or without BOM.
However, Windows support Little-endian UTF-16 Unicode .inf file.
The test case compiled with mingw-w64 and run on
https://testbot.winehq.org/JobDetails.pl?Key=22822

However, in git log I see such a commit:
commit 31ade1eb67e6b55f1b0c09ef7a806cd53652b1e2
Author: Alexandre Julliard <[email protected]>
Date:   Wed Mar 21 13:52:43 2007 +0100

    setupapi: Add support for .inf files in utf-8 format.

Did I miss anything or was there a reason to add utf8 support for inf
files in Wine?

Thanks a lot!

--
Regards,
Qian Hong

-
Sent from Ubuntu
http://www.ubuntu.com/
#include <windows.h>
#include <stdio.h>
#include <setupapi.h>

BOOL create_inf(const WCHAR *tmpfilenameW, void *data, UINT sz)
{
    HANDLE handle = CreateFileW(tmpfilenameW, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, 0);
    DWORD res;

    if (handle == INVALID_HANDLE_VALUE) return FALSE;

    if (!WriteFile(handle, data, sz, &res, NULL))
    {
        CloseHandle(handle);
        DeleteFileW(tmpfilenameW);
        return FALSE;
    }
    CloseHandle(handle);
    return TRUE;
}

void check_inf(const WCHAR *tmpfilenameW)
{
    HINF inf; 
    UINT err_line = 0;
    BOOL ret;
    INFCONTEXT context;
    WCHAR bufferW[MAX_INF_STRING_LENGTH+32];
    char bufferA[MAX_INF_STRING_LENGTH+32];
    WCHAR chineseW[] = {'c','h','i','n','e','s','e','\0'}; 
    WCHAR hanziW[] = {0x4e2d, 0x6587, 0};
    inf = SetupOpenInfFileW(tmpfilenameW, 0, INF_STYLE_WIN4, &err_line);
    if (inf == INVALID_HANDLE_VALUE)
    {
        printf("SetupOpenInfFile fail\n");
        return;
    }

    ret = SetupFindFirstLineA(inf, "Test", 0, &context);

    SetupGetStringFieldW(&context, 0, bufferW, sizeof(bufferW), NULL);
    WideCharToMultiByte(CP_ACP, 0, bufferW, -1, bufferA, sizeof(bufferA), NULL, FALSE);
    ret = lstrcmpW(bufferW, chineseW);
    printf("%s %s\n", bufferA, ret == 0 ? "matches" : "does not match");

    SetupGetStringFieldW(&context, 1, bufferW, sizeof(bufferW), NULL);
    WideCharToMultiByte(CP_ACP, 0, bufferW, -1, bufferA, sizeof(bufferA), NULL, FALSE);
    ret = lstrcmpW(bufferW, hanziW);
    printf("%s %s\n", bufferA, ret == 0 ? "matches" : "does not match");

    return;
}

int main(void)
{
    WCHAR tmpfilenameW[] = {'.','\\','t','m','p','.','i','n','f','\0'};
    //const char tmpfilename[] = ".\\tmp.inf";
    WCHAR data[] = {0xfeff, '[','v','e','r','s','i','o','n',']','\r','\n','S','i','g','n','a','t','u','r','e','=','"','$','C','H','I','C','A','G','O','$','"','\r','\n', '[','T','e','s','t',']','\r','\n','c','h','i','n','e','s','e','=', 0x4e2d, 0x6587,'\r','\n'};
    char data_utf8[4096];
    char data_utf8_bom[4096];
    INT srclen, dstlen;

    srclen = sizeof(data)/sizeof(data[0]);
    dstlen = WideCharToMultiByte(CP_UTF8, 0, data + 1, srclen - 1, NULL, 0, NULL, FALSE);
    WideCharToMultiByte(CP_UTF8, 0, data + 1, srclen -1, data_utf8, dstlen, NULL, FALSE);

    memcpy(data_utf8_bom, "\xef\xbb\xbf", 3);
    memcpy(data_utf8_bom + 3, data_utf8, dstlen);

    printf("Testing Little-endian UTF-16 Unicode .inf file...\n");
    if (create_inf(tmpfilenameW, data, sizeof(data)))
    {
        check_inf(tmpfilenameW);
        DeleteFileW(tmpfilenameW);
    }
    printf("\n");

    printf("Testing UTF-8 without BOM...\n");
    if (create_inf(tmpfilenameW, data_utf8, dstlen))
    {
        check_inf(tmpfilenameW);
        DeleteFileW(tmpfilenameW);
    }
    printf("\n");

    printf("Testing UTF-8 with BOM...\n");
    if (create_inf(tmpfilenameW, data_utf8_bom, dstlen + 3))
    {
        check_inf(tmpfilenameW);
        DeleteFileW(tmpfilenameW);
    }
    printf("\n");

    return 0;
}


Reply via email to