1 /*
2  * PROJECT:     ReactOS API tests
3  * LICENSE:     GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later.html)
4  * PURPOSE:     RTL locale support
5  * COPYRIGHT:   Copyright 2016 Mark Jansen <mark.jansen@reactos.org>
6  *              Copyright 2021 Jérôme Gardou <jerome.gardou@reactos.org>
7  */
8 
9 #include "precomp.h"
10 
11 static BOOL (WINAPI *pWow64DisableWow64FsRedirection)(PVOID *);
12 static BOOL (WINAPI *pWow64RevertWow64FsRedirection)(PVOID);
13 HANDLE hKernel32;
14 
15 static PVOID LoadCodePageData(_In_ ULONG Code)
16 {
17     char filename[MAX_PATH], sysdir[MAX_PATH];
18     HANDLE hFile;
19     PVOID Data = NULL;
20     PVOID FsRedir;
21 
22     if (!hKernel32)
23     {
24         hKernel32 = GetModuleHandleA("kernel32.dll");
25 
26         pWow64DisableWow64FsRedirection = (void*)GetProcAddress(hKernel32, "Wow64DisableWow64FsRedirection");
27         pWow64RevertWow64FsRedirection = (void*)GetProcAddress(hKernel32, "Wow64RevertWow64FsRedirection");
28     }
29 
30     if (pWow64DisableWow64FsRedirection)
31         pWow64DisableWow64FsRedirection(&FsRedir);
32 
33     GetSystemDirectoryA(sysdir, MAX_PATH);
34 
35     if (Code != -1)
36         StringCbPrintfA(filename, sizeof(filename), "%s\\c_%lu.nls", sysdir, Code);
37     else
38         StringCbPrintfA(filename, sizeof(filename), "%s\\l_intl.nls", sysdir);
39 
40     hFile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
41     ok(hFile != INVALID_HANDLE_VALUE, "Failed to open %s, error %u\n", filename, (UINT)GetLastError());
42     if (hFile != INVALID_HANDLE_VALUE)
43     {
44         DWORD dwRead;
45         DWORD dwFileSize = GetFileSize(hFile, NULL);
46         Data = malloc(dwFileSize);
47         ReadFile(hFile, Data, dwFileSize, &dwRead, NULL);
48         CloseHandle(hFile);
49     }
50 
51     if (pWow64RevertWow64FsRedirection)
52         pWow64RevertWow64FsRedirection(FsRedir);
53 
54     return Data;
55 }
56 
57 /* https://www.microsoft.com/resources/msdn/goglobal/default.mspx */
58 void SetupLocale(
59     _In_ ULONG AnsiCode,
60     _In_ ULONG OemCode,
61     _In_ ULONG Unicode)
62 {
63     NLSTABLEINFO NlsTable;
64     PVOID AnsiCodePageData;
65     PVOID OemCodePageData;
66     PVOID UnicodeCaseTableData;
67 
68     AnsiCodePageData = LoadCodePageData(AnsiCode);
69     OemCodePageData = LoadCodePageData(OemCode);
70     UnicodeCaseTableData = LoadCodePageData(Unicode);
71 
72     RtlInitNlsTables(AnsiCodePageData, OemCodePageData, UnicodeCaseTableData, &NlsTable);
73     RtlResetRtlTranslations(&NlsTable);
74     /*
75      * Do NOT free the buffers here, they are directly used!
76      * Yes, we leak the old buffers, but this is a test anyway...
77      */
78 }
79