ReactOS 0.4.17-dev-1401-g4f43d35
kbdlayout.c File Reference
#include <win32k.h>
#include <immdev.h>
Include dependency graph for kbdlayout.c:

Go to the source code of this file.

Classes

struct  _KBDIMAGE_INFO
 
struct  _KBDVALIDATE
 
struct  tagKBDFILE_NAME
 

Macros

#define CP_ACP   0
 
#define KBDIMG_TAG   'ibKU'
 
#define KBDIMG_MAXSIZE   (1 * 1024 * 1024)
 
#define RVA(m, b)   ((PVOID)((ULONG_PTR)(b) + (ULONG_PTR)(m)))
 
#define KBD_MAX_VKTOBIT   0x100
 
#define KBD_MAX_WCHTABLES   0x20
 
#define KBD_MAX_VKTOWCH   0x200
 
#define KBD_MAX_KEYNAMES   0x200
 
#define KBD_MAX_DEADNAMES   0x400
 
#define KBD_MAX_DEADKEYS   0x10000
 
#define KBD_MAX_VSCVK   0x100
 
#define KBD_MAX_LIGATURES   0x10000
 
#define KBD_MAX_NLS_ENTRIES   0x100
 
#define KBD_MAX_NAME_CCH   128 /* including the terminating NUL */
 
#define KBD_MAX_MOD_COLUMNS   10 /* the widest VK_TO_WCHARS<n> */
 
#define KBD_MAX_LIGATURE_CCH   5 /* the widest LIGATURE<n> */
 
#define KBD_REBASE(pv, field, cbMin, cbAlign, bRequired)    KbdRebaseSlot((pv), (PVOID*)&(field), (cbMin), (cbAlign), (bRequired), #field)
 

Typedefs

typedef struct _KBDIMAGE_INFO KBDIMAGE_INFO
 
typedef struct _KBDIMAGE_INFO * PKBDIMAGE_INFO
 
typedef struct _KBDVALIDATE KBDVALIDATE
 
typedef struct _KBDVALIDATE * PKBDVALIDATE
 
typedef struct tagKBDFILE_NAME KBDFILE_NAME
 
typedef struct tagKBDFILE_NAME * PKBDFILE_NAME
 

Functions

 DBG_DEFAULT_CHANNEL (UserKbdLayout)
 
PKL FASTCALL IntHKLtoPKL (_Inout_ PTHREADINFO pti, _In_ HKL hKL)
 
static UINT APIENTRY IntGetKeyboardLayoutList (_Inout_ PWINSTATION_OBJECT pWinSta, _In_ ULONG nBuff, _Out_ HKL *pHklBuff)
 
static PVOID KbdBuildImage (_In_ PVOID pvRaw, _In_ SIZE_T cbRaw, _Out_ PULONG pcbImage, _Out_ PKBDIMAGE_INFO pInfo)
 Build the image of the keyboard layout file without executing it, due to security reasons.
 
static PVOID KbdLoadImage (_In_ HANDLE hKernelFile, _Out_ PULONG pcbImage, _Out_ PKBDIMAGE_INFO pInfo)
 
static __inline VOID KbdUnloadImage (_In_opt_ PVOID pvImageBase)
 
static __inline BOOL KbdIsRangeInData (_In_ const KBDIMAGE_INFO *pInfo, _In_ ULONG_PTR off, _In_ SIZE_T cb)
 
static BOOL KbdIsBufInRange (_In_ const KBDVALIDATE *pv, _In_ const VOID *p, _In_ SIZE_T cb, _In_ SIZE_T cbAlign)
 
static BOOL KbdRebaseSlot (_In_ PKBDVALIDATE pv, _Inout_ PVOID *ppSlot, _In_ SIZE_T cbMin, _In_ SIZE_T cbAlign, _In_ BOOL bRequired, _In_ PCSTR pszName)
 Converts a pointer slot of the layout tables from a virtual address (relative to ImageBase) to a pointer into our buffer, and verifies that the target has room for cbMin bytes.
 
static BOOL KbdMeasureTable (_In_ const KBDVALIDATE *pv, _In_ const VOID *pTable, _In_ SIZE_T cbElem, _In_ SIZE_T cbKey, _In_ SIZE_T cbAlign, _In_ ULONG cMax, _Out_ PULONG pcElems, _In_ PCSTR pszName)
 Measures a table that ends with a terminating element, verifying every element (the terminator included) in full, that is, with the stride of the element. The terminator is an element whose first cbKey bytes are all zero.
 
static BOOL KbdCheckWideString (_In_ const KBDVALIDATE *pv, _In_ const WCHAR *pwsz)
 Verifies a wide string is terminated within the layout data (and is not unreasonably long).
 
static BOOL KbdCheckModifiers (_In_ PKBDVALIDATE pv, _Inout_ PKBDTABLES pTbl)
 Verifies and rebases the modifiers in the main tables of the layout data.
 
static BOOL KbdCheckVkToWchars (_In_ PKBDVALIDATE pv, _Inout_ PKBDTABLES pTbl, _Out_ PBOOL pbLigature, _Out_ PBOOL pbDead)
 Verifies and rebases the VK_TO_WCHAR_TABLE structure in the layout data.
 
static BOOL KbdCheckKeyNames (_In_ PKBDVALIDATE pv, _Inout_ VSC_LPWSTR **ppNames, _In_ PCSTR pszName)
 Verifies and rebases the key name. This table ends with an entry that has vsc == 0.
 
static BOOL KbdCheckDeadKeyNames (_In_ PKBDVALIDATE pv, _Inout_ PKBDTABLES pTbl)
 Verifies and rebases the dead key names (optional). This array of strings ends with NULL.
 
static BOOL KbdCheckVscToVk (_In_ PKBDVALIDATE pv, _Inout_ PVSC_VK *ppVscVk, _In_ PCSTR pszName)
 Verifies and rebases the VSC -> VK table for the E0/E1 prefixes. They end with an entry that has Vsc == 0.
 
static BOOL KbdCheckDeadKeys (_In_ PKBDVALIDATE pv, _Inout_ PKBDTABLES pTbl)
 Verifies and rebases the dead keys (optional). This table ends with an entry that has dwBoth == 0.
 
static BOOL KbdCheckLigatures (_In_ PKBDVALIDATE pv, _Inout_ PKBDTABLES pTbl)
 Verifies and rebases the ligatures (optional). This table ends with an entry that has VirtualKey == 0.
 
static BOOL KbdCheckNls (_In_ PKBDVALIDATE pv, _Inout_ PKBDNLSTABLES pNls)
 Verifies and rebases the NLS (national language support; for Far-East) tables. These tables are counted, not terminated.
 
static BOOL KbdRebaseTables (_In_ PBYTE pbImage, _In_ const KBDIMAGE_INFO *pInfo, _Inout_ PKBDTABLES pTbl, _Inout_opt_ PKBDNLSTABLES pNls)
 Adjusts the pointers inside the layout tables, and validates the whole layout.
 
static BOOL UserLoadKbdDll (_In_ HANDLE hKernelFile, _In_ ULONG offTable, _Out_ HANDLE *phModule, _Out_ PULONG pcbImage, _Out_ PKBDTABLES *ppKbdTables, _Out_ PKBDNLSTABLES *ppKbdNlsTables)
 
static PKBDFILE UserLoadKbdFile (_In_ HANDLE hKernelFile, _In_ ULONG offTable, _In_ PCUNICODE_STRING pustrKLID)
 
static PKL co_UserLoadKbdLayout (_In_ HANDLE hKernelFile, _In_ ULONG offTable, _In_ const UNICODE_STRING *pustrKLID, _In_ HKL hKL)
 
static VOID UnloadKbdFile (_In_ PKBDFILE pkf)
 
BOOL UserUnloadKbl (PKL pKl)
 
PKL W32kGetDefaultKeyLayout (VOID)
 
PKL NTAPI UserHklToKbl (HKL hKl)
 
VOID FASTCALL IntReorderKeyboardLayouts (_Inout_ PWINSTATION_OBJECT pWinSta, _Inout_ PKL pNewKL)
 
BOOL NTAPI UserSetDefaultInputLang (HKL hKl)
 
VOID APIENTRY IntImmActivateLayout (_Inout_ PTHREADINFO pti, _Inout_ PKL pKL)
 
static VOID co_IntSetKeyboardLayoutForProcess (PPROCESSINFO ppi, PKL pKL)
 
HKL APIENTRY co_UserActivateKeyboardLayout (_Inout_ PKL pKL, _In_ ULONG uFlags, _In_opt_ PWND pWnd)
 
HKL APIENTRY co_IntActivateKeyboardLayout (_Inout_ PWINSTATION_OBJECT pWinSta, _In_ HKL hKL, _In_ ULONG uFlags, _In_opt_ PWND pWnd)
 
static BOOL APIENTRY co_IntUnloadKeyboardLayoutEx (_Inout_ PWINSTATION_OBJECT pWinSta, _Inout_ PKL pKL, _In_ DWORD dwFlags)
 
static BOOL APIENTRY IntUnloadKeyboardLayout (_Inout_ PWINSTATION_OBJECT pWinSta, _In_ HKL hKL)
 
PIMEINFOEX FASTCALL co_UserImmLoadLayout (_In_ HKL hKL)
 Invokes imm32!ImmLoadLayout and returns PIMEINFOEX.
 
HKL APIENTRY co_IntLoadKeyboardLayoutEx (_Inout_ PWINSTATION_OBJECT pWinSta, _In_ HANDLE hKernelFile, _In_opt_ HKL hOldKL, _In_ ULONG offTable, _In_ PUNICODE_STRING puszSafeKLID, _In_ HKL hNewKL, _In_ UINT Flags)
 
static NTSTATUS IntQueryHandleName (HANDLE hObject, PKBDFILE_NAME pName)
 
static BOOL FASTCALL IntEqualDir (_In_ PCUNICODE_STRING pustrParent, _In_ PCUNICODE_STRING pustrDir)
 
HANDLE FASTCALL IntVerifyKeyboardFileHandle (HANDLE hFile)
 Validates that the given user-mode handle corresponds to an opened file in SystemRoot\system32.
 
HKL FASTCALL UserGetKeyboardLayout (DWORD dwThreadId)
 
UINT APIENTRY NtUserGetKeyboardLayoutList (ULONG nBuff, HKL *pHklBuff)
 
BOOL APIENTRY NtUserGetKeyboardLayoutName (_Inout_ PUNICODE_STRING pustrName)
 
HKL NTAPI NtUserLoadKeyboardLayoutEx (_In_ HANDLE hFile, _In_ ULONG offTable, _In_opt_ PKBDTABLE_MULTI_EX pMulti, _In_opt_ HKL hOldKL, _In_ PUNICODE_STRING puszKLID, _In_ ULONG dwNewKL, _In_ UINT Flags)
 
HKL NTAPI NtUserActivateKeyboardLayout (HKL hKL, ULONG Flags)
 
BOOL APIENTRY NtUserUnloadKeyboardLayout (HKL hKl)
 

Variables

PKL gspklBaseLayout = NULL
 
PKBDFILE gpkfList = NULL
 
DWORD gSystemFS = 0
 
UINT gSystemCPCharSet = 0
 
HKL ghKLSentToShell = NULL
 

Macro Definition Documentation

◆ CP_ACP

#define CP_ACP   0

Definition at line 18 of file kbdlayout.c.

◆ KBD_MAX_DEADKEYS

#define KBD_MAX_DEADKEYS   0x10000

Definition at line 493 of file kbdlayout.c.

◆ KBD_MAX_DEADNAMES

#define KBD_MAX_DEADNAMES   0x400

Definition at line 492 of file kbdlayout.c.

◆ KBD_MAX_KEYNAMES

#define KBD_MAX_KEYNAMES   0x200

Definition at line 491 of file kbdlayout.c.

◆ KBD_MAX_LIGATURE_CCH

#define KBD_MAX_LIGATURE_CCH   5 /* the widest LIGATURE<n> */

Definition at line 499 of file kbdlayout.c.

◆ KBD_MAX_LIGATURES

#define KBD_MAX_LIGATURES   0x10000

Definition at line 495 of file kbdlayout.c.

◆ KBD_MAX_MOD_COLUMNS

#define KBD_MAX_MOD_COLUMNS   10 /* the widest VK_TO_WCHARS<n> */

Definition at line 498 of file kbdlayout.c.

◆ KBD_MAX_NAME_CCH

#define KBD_MAX_NAME_CCH   128 /* including the terminating NUL */

Definition at line 497 of file kbdlayout.c.

◆ KBD_MAX_NLS_ENTRIES

#define KBD_MAX_NLS_ENTRIES   0x100

Definition at line 496 of file kbdlayout.c.

◆ KBD_MAX_VKTOBIT

#define KBD_MAX_VKTOBIT   0x100

Definition at line 488 of file kbdlayout.c.

◆ KBD_MAX_VKTOWCH

#define KBD_MAX_VKTOWCH   0x200

Definition at line 490 of file kbdlayout.c.

◆ KBD_MAX_VSCVK

#define KBD_MAX_VSCVK   0x100

Definition at line 494 of file kbdlayout.c.

◆ KBD_MAX_WCHTABLES

#define KBD_MAX_WCHTABLES   0x20

Definition at line 489 of file kbdlayout.c.

◆ KBD_REBASE

#define KBD_REBASE (   pv,
  field,
  cbMin,
  cbAlign,
  bRequired 
)     KbdRebaseSlot((pv), (PVOID*)&(field), (cbMin), (cbAlign), (bRequired), #field)

Definition at line 599 of file kbdlayout.c.

◆ KBDIMG_MAXSIZE

#define KBDIMG_MAXSIZE   (1 * 1024 * 1024)

Definition at line 240 of file kbdlayout.c.

◆ KBDIMG_TAG

#define KBDIMG_TAG   'ibKU'

Definition at line 239 of file kbdlayout.c.

◆ RVA

#define RVA (   m,
  b 
)    ((PVOID)((ULONG_PTR)(b) + (ULONG_PTR)(m)))

Definition at line 243 of file kbdlayout.c.

Typedef Documentation

◆ KBDFILE_NAME

◆ KBDIMAGE_INFO

◆ KBDVALIDATE

◆ PKBDFILE_NAME

◆ PKBDIMAGE_INFO

◆ PKBDVALIDATE

Function Documentation

◆ co_IntActivateKeyboardLayout()

HKL APIENTRY co_IntActivateKeyboardLayout ( _Inout_ PWINSTATION_OBJECT  pWinSta,
_In_ HKL  hKL,
_In_ ULONG  uFlags,
_In_opt_ PWND  pWnd 
)

Definition at line 1640 of file kbdlayout.c.

1645{
1646 PKL pKL;
1648
1649 pKL = IntHKLtoPKL(pti, hKL);
1650 if (!pKL)
1651 {
1652 ERR("Invalid HKL %p!\n", hKL);
1653 return NULL;
1654 }
1655
1656 if (uFlags & KLF_REORDER)
1657 IntReorderKeyboardLayouts(pWinSta, pKL);
1658
1659 return co_UserActivateKeyboardLayout(pKL, uFlags, pWnd);
1660}
#define ERR(fmt,...)
Definition: precomp.h:57
#define NULL
Definition: types.h:112
UINT uFlags
Definition: api.c:59
PVOID NTAPI PsGetCurrentThreadWin32Thread(VOID)
Definition: thread.c:829
#define KLF_REORDER
Definition: winuser.h:114
Definition: input.h:18
VOID FASTCALL IntReorderKeyboardLayouts(_Inout_ PWINSTATION_OBJECT pWinSta, _Inout_ PKL pNewKL)
Definition: kbdlayout.c:1415
PKL FASTCALL IntHKLtoPKL(_Inout_ PTHREADINFO pti, _In_ HKL hKL)
Definition: kbdlayout.c:34
HKL APIENTRY co_UserActivateKeyboardLayout(_Inout_ PKL pKL, _In_ ULONG uFlags, _In_opt_ PWND pWnd)
Definition: kbdlayout.c:1525

Referenced by NtUserActivateKeyboardLayout().

◆ co_IntLoadKeyboardLayoutEx()

HKL APIENTRY co_IntLoadKeyboardLayoutEx ( _Inout_ PWINSTATION_OBJECT  pWinSta,
_In_ HANDLE  hKernelFile,
_In_opt_ HKL  hOldKL,
_In_ ULONG  offTable,
_In_ PUNICODE_STRING  puszSafeKLID,
_In_ HKL  hNewKL,
_In_ UINT  Flags 
)

Definition at line 1743 of file kbdlayout.c.

1751{
1752 PKL pOldKL, pNewKL;
1753
1754 if (hNewKL == NULL || (pWinSta->Flags & WSS_NOIO))
1755 return NULL;
1756
1757 /* If hOldKL is specified, unload it and load new layput as default */
1758 if (hOldKL && hOldKL != hNewKL)
1759 {
1760 pOldKL = UserHklToKbl(hOldKL);
1761 if (pOldKL)
1762 UserUnloadKbl(pOldKL);
1763 }
1764
1765 /* FIXME: It seems KLF_RESET is only supported for WINLOGON */
1766
1767 /* Let's see if layout was already loaded. */
1768 pNewKL = UserHklToKbl(hNewKL);
1769 if (!pNewKL)
1770 {
1771 /* It wasn't, so load it. */
1772 pNewKL = co_UserLoadKbdLayout(hKernelFile, offTable, puszSafeKLID, hNewKL);
1773 if (!pNewKL)
1774 return NULL;
1775
1776 if (gspklBaseLayout)
1777 {
1778 /* Find last not unloaded layout */
1779 PKL pLastKL = gspklBaseLayout->pklPrev;
1780 while (pLastKL != gspklBaseLayout && (pLastKL->dwKL_Flags & KL_UNLOAD))
1781 pLastKL = pLastKL->pklPrev;
1782
1783 /* Add new layout to the list */
1784 pNewKL->pklNext = pLastKL->pklNext;
1785 pNewKL->pklPrev = pLastKL;
1786 pNewKL->pklNext->pklPrev = pNewKL;
1787 pNewKL->pklPrev->pklNext = pNewKL;
1788 }
1789 else
1790 {
1791 /* This is the first layout */
1792 pNewKL->pklNext = pNewKL;
1793 pNewKL->pklPrev = pNewKL;
1794 gspklBaseLayout = pNewKL;
1795 }
1796
1797 pNewKL->piiex = co_UserImmLoadLayout(hNewKL);
1798 }
1799
1800 /* If this layout was prepared to unload, undo it */
1801 pNewKL->dwKL_Flags &= ~KL_UNLOAD;
1802
1803 /* Reorder if necessary */
1804 if (Flags & KLF_REORDER)
1805 IntReorderKeyboardLayouts(pWinSta, pNewKL);
1806
1807 /* Activate this layout in current thread */
1808 if (Flags & KLF_ACTIVATE)
1810
1811 /* Send shell message */
1812 if (!(Flags & KLF_NOTELLSHELL))
1813 co_IntShellHookNotify(HSHELL_LANGUAGE, 0, (LPARAM)hNewKL);
1814
1815 /* FIXME: KLF_REPLACELANG */
1816
1817 return hNewKL;
1818}
_In_ D3DDDI_VIDEO_PRESENT_TARGET_ID _In_ ULONG _In_ ULONG Flags
Definition: dispmprt.h:245
LONG_PTR LPARAM
Definition: minwindef.h:175
#define KLF_ACTIVATE
Definition: winuser.h:111
#define KLF_NOTELLSHELL
Definition: winuser.h:116
DWORD dwKL_Flags
Definition: input.h:22
PIMEINFOEX piiex
Definition: input.h:29
struct tagKL * pklNext
Definition: input.h:20
struct tagKL * pklPrev
Definition: input.h:21
#define KL_UNLOAD
Definition: undocuser.h:183
VOID co_IntShellHookNotify(WPARAM Message, WPARAM wParam, LPARAM lParam)
Definition: desktop.c:1712
PKL gspklBaseLayout
Definition: kbdlayout.c:22
PKL NTAPI UserHklToKbl(HKL hKl)
Definition: kbdlayout.c:1396
BOOL UserUnloadKbl(PKL pKl)
Definition: kbdlayout.c:1328
PIMEINFOEX FASTCALL co_UserImmLoadLayout(_In_ HKL hKL)
Invokes imm32!ImmLoadLayout and returns PIMEINFOEX.
Definition: kbdlayout.c:1722
static PKL co_UserLoadKbdLayout(_In_ HANDLE hKernelFile, _In_ ULONG offTable, _In_ const UNICODE_STRING *pustrKLID, _In_ HKL hKL)
Definition: kbdlayout.c:1221
#define WSS_NOIO
Definition: winsta.h:9

Referenced by NtUserLoadKeyboardLayoutEx().

◆ co_IntSetKeyboardLayoutForProcess()

static VOID co_IntSetKeyboardLayoutForProcess ( PPROCESSINFO  ppi,
PKL  pKL 
)
static

Definition at line 1490 of file kbdlayout.c.

1491{
1492 PTHREADINFO ptiNode, ptiNext;
1493 PCLIENTINFO pClientInfo;
1494 BOOL bImmMode = IS_IMM_MODE();
1495
1496 for (ptiNode = ppi->ptiList; ptiNode; ptiNode = ptiNext)
1497 {
1498 IntReferenceThreadInfo(ptiNode);
1499 ptiNext = ptiNode->ptiSibling;
1500
1501 /* Skip this thread if its keyboard layout is already the correct one, or if it's dying */
1502 if (ptiNode->KeyboardLayout == pKL || (ptiNode->TIF_flags & TIF_INCLEANUP))
1503 {
1504 IntDereferenceThreadInfo(ptiNode);
1505 continue;
1506 }
1507
1508 if (bImmMode)
1509 {
1510 IntImmActivateLayout(ptiNode, pKL);
1511 }
1512 else
1513 {
1514 UserAssignmentLock((PVOID*)&ptiNode->KeyboardLayout, pKL);
1515 pClientInfo = ptiNode->pClientInfo;
1516 pClientInfo->CodePage = pKL->CodePage;
1517 pClientInfo->hKL = pKL->hkl;
1518 }
1519
1520 IntDereferenceThreadInfo(ptiNode);
1521 }
1522}
unsigned int BOOL
Definition: ntddk_ex.h:94
#define TIF_INCLEANUP
Definition: ntuser.h:290
#define IS_IMM_MODE()
Definition: ntuser.h:1243
USHORT CodePage
Definition: ntuser.h:368
HKL hKL
Definition: ntuser.h:367
PTHREADINFO ptiList
Definition: win32.h:260
PTHREADINFO ptiSibling
Definition: win32.h:117
struct _CLIENTINFO * pClientInfo
Definition: win32.h:95
struct tagKL * KeyboardLayout
Definition: win32.h:91
FLONG TIF_flags
Definition: win32.h:96
USHORT CodePage
Definition: input.h:27
HKL hkl
Definition: input.h:23
#define IntDereferenceThreadInfo(pti)
Definition: win32.h:175
#define IntReferenceThreadInfo(pti)
Definition: win32.h:170
VOID APIENTRY IntImmActivateLayout(_Inout_ PTHREADINFO pti, _Inout_ PKL pKL)
Definition: kbdlayout.c:1453
PVOID FASTCALL UserAssignmentLock(PVOID *ppvObj, PVOID pvNew)
Definition: object.c:861

Referenced by co_UserActivateKeyboardLayout().

◆ co_IntUnloadKeyboardLayoutEx()

static BOOL APIENTRY co_IntUnloadKeyboardLayoutEx ( _Inout_ PWINSTATION_OBJECT  pWinSta,
_Inout_ PKL  pKL,
_In_ DWORD  dwFlags 
)
static

Definition at line 1663 of file kbdlayout.c.

1667{
1668 PKL pNextKL;
1669 USER_REFERENCE_ENTRY Ref1, Ref2;
1671
1672 if (pKL == gspklBaseLayout && !(dwFlags & UKL_NOACTIVATENEXT))
1673 return FALSE;
1674
1675 UserRefObjectCo(pKL, &Ref1); /* Add reference */
1676
1677 /* Regard as unloaded */
1679 pKL->dwKL_Flags |= KL_UNLOAD;
1680
1681 if (!(dwFlags & UKL_NOACTIVATENEXT) && pti->KeyboardLayout == pKL)
1682 {
1683 pNextKL = IntHKLtoPKL(pti, UlongToHandle(HKL_NEXT));
1684 if (pNextKL)
1685 {
1686 UserRefObjectCo(pNextKL, &Ref2); /* Add reference */
1688 UserDerefObjectCo(pNextKL); /* Release reference */
1689 }
1690 }
1691
1692 if (gspklBaseLayout == pKL && pKL != pKL->pklNext)
1693 {
1694 /* Set next layout as default (FIXME: Use UserAssignmentLock?) */
1695 gspklBaseLayout = pKL->pklNext;
1696 }
1697
1698 UserDerefObjectCo(pKL); /* Release reference */
1699
1700 if (ISITHOOKED(WH_SHELL))
1701 {
1702 co_IntShellHookNotify(HSHELL_LANGUAGE, 0, 0);
1704 }
1705
1706 return TRUE;
1707}
#define UlongToHandle(ul)
Definition: basetsd.h:91
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define ISITHOOKED(HookId)
Definition: hook.h:6
_In_ LPWSTR _In_ DWORD _In_ DWORD _In_ DWORD dwFlags
Definition: netsh.h:141
PTHREADINFO gptiCurrent
Definition: ntuser.c:15
static __inline VOID UserDerefObjectCo(PVOID obj)
Definition: object.h:44
static __inline VOID UserRefObjectCo(PVOID obj, PUSER_REFERENCE_ENTRY UserReferenceEntry)
Definition: object.h:28
#define WH_SHELL
Definition: winuser.h:40
#define HKL_NEXT
Definition: winuser.h:109
Definition: object.h:4
#define UKL_NOACTIVATENEXT
Definition: undocuser.h:186
HKL ghKLSentToShell
Definition: kbdlayout.c:26
BOOL FASTCALL UserMarkObjectDestroy(PVOID Object)
Definition: object.c:624

Referenced by IntUnloadKeyboardLayout().

◆ co_UserActivateKeyboardLayout()

HKL APIENTRY co_UserActivateKeyboardLayout ( _Inout_ PKL  pKL,
_In_ ULONG  uFlags,
_In_opt_ PWND  pWnd 
)

Definition at line 1525 of file kbdlayout.c.

1529{
1530 HKL hOldKL = NULL;
1531 PKL pOldKL;
1533 PWND pTargetWnd, pImeWnd;
1534 HWND hTargetWnd, hImeWnd;
1535 USER_REFERENCE_ENTRY Ref1, Ref2;
1537 BOOL bSetForProcess = !!(uFlags & KLF_SETFORPROCESS);
1538
1540 ClientInfo = pti->pClientInfo;
1541
1542 pOldKL = pti->KeyboardLayout;
1543 if (pOldKL)
1544 hOldKL = pOldKL->hkl;
1545
1546 if (uFlags & KLF_RESET)
1547 {
1548 FIXME("KLF_RESET\n");
1549 }
1550
1551 if (!bSetForProcess && pKL == pti->KeyboardLayout)
1552 {
1554 return hOldKL;
1555 }
1556
1557 pKL->wchDiacritic = UNICODE_NULL;
1558
1559 if (pOldKL)
1560 UserRefObjectCo(pOldKL, &Ref1);
1561
1562 if (pti->TIF_flags & TIF_CSRSSTHREAD)
1563 {
1565 ClientInfo->CodePage = pKL->CodePage;
1566 ClientInfo->hKL = pKL->hkl;
1567 }
1568 else if (bSetForProcess)
1569 {
1571 }
1572 else
1573 {
1574 if (IS_IMM_MODE())
1575 IntImmActivateLayout(pti, pKL);
1576 else
1578
1579 ClientInfo->CodePage = pKL->CodePage;
1580 ClientInfo->hKL = pKL->hkl;
1581 }
1582
1583 /* Send shell message if necessary */
1585 {
1586 /* Send the HKL if needed and remember it */
1587 if (ghKLSentToShell != pKL->hkl)
1588 {
1589 co_IntShellHookNotify(HSHELL_LANGUAGE, 0, (LPARAM)pKL->hkl);
1590 ghKLSentToShell = pKL->hkl;
1591 }
1592 }
1593
1594 if (pti->MessageQueue)
1595 {
1596 /* Determine the target window */
1597 pTargetWnd = pti->MessageQueue->spwndFocus;
1598 if (!pTargetWnd)
1599 {
1600 pTargetWnd = pti->MessageQueue->spwndActive;
1601 if (!pTargetWnd)
1602 pTargetWnd = pWnd;
1603 }
1604
1605 /* Send WM_INPUTLANGCHANGE message */
1606 if (pTargetWnd)
1607 {
1608 UserRefObjectCo(pTargetWnd, &Ref2);
1609 hTargetWnd = UserHMGetHandle(pTargetWnd);
1610 co_IntSendMessage(hTargetWnd, WM_INPUTLANGCHANGE, pKL->iBaseCharset, (LPARAM)pKL->hkl);
1611 UserDerefObjectCo(pTargetWnd);
1612 }
1613 }
1614
1615 // Refresh IME UI via WM_IME_SYSTEM:IMS_SENDNOTIFICATION messaging
1616 if (!(pti->TIF_flags & TIF_CSRSSTHREAD))
1617 {
1618 if (IS_IME_HKL(pKL->hkl) || (IS_CICERO_MODE() && !IS_16BIT_MODE()))
1619 {
1620 pImeWnd = pti->spwndDefaultIme;
1621 if (pImeWnd)
1622 {
1623 bSetForProcess &= !IS_16BIT_MODE();
1624 UserRefObjectCo(pImeWnd, &Ref2);
1625 hImeWnd = UserHMGetHandle(pImeWnd);
1626 co_IntSendMessage(hImeWnd, WM_IME_SYSTEM, IMS_SENDNOTIFICATION, bSetForProcess);
1627 UserDerefObjectCo(pImeWnd);
1628 }
1629 }
1630 }
1631
1632 if (pOldKL)
1633 UserDerefObjectCo(pOldKL);
1634
1636 return hOldKL;
1637}
#define FIXME(fmt,...)
Definition: precomp.h:53
PTHREADINFO gptiForeground
Definition: focus.c:15
#define IS_IME_HKL(hKL)
Definition: imm32_undoc.h:21
#define WM_IME_SYSTEM
Definition: imm32_undoc.h:32
#define IMS_SENDNOTIFICATION
Definition: imm32_undoc.h:66
#define TIF_CSRSSTHREAD
Definition: ntuser.h:293
#define UserHMGetHandle(obj)
Definition: ntuser.h:257
struct _THREADINFO * GetW32ThreadInfo(VOID)
Definition: misc.c:806
#define IS_16BIT_MODE()
Definition: ntuser.h:1245
#define IS_CICERO_MODE()
Definition: ntuser.h:1244
CLIENT_DATA ClientInfo
UINT_PTR HKL
Definition: msctf.idl:125
#define UNICODE_NULL
#define KLF_SETFORPROCESS
Definition: winuser.h:117
PPROCESSINFO ppi
Definition: win32.h:89
struct _WND * spwndDefaultIme
Definition: win32.h:132
struct _USER_MESSAGE_QUEUE * MessageQueue
Definition: win32.h:90
Definition: ntuser.h:722
static VOID co_IntSetKeyboardLayoutForProcess(PPROCESSINFO ppi, PKL pKL)
Definition: kbdlayout.c:1490
LRESULT FASTCALL co_IntSendMessage(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
Definition: message.c:1497

Referenced by co_IntActivateKeyboardLayout(), co_IntLoadKeyboardLayoutEx(), and co_IntUnloadKeyboardLayoutEx().

◆ co_UserImmLoadLayout()

PIMEINFOEX FASTCALL co_UserImmLoadLayout ( _In_ HKL  hKL)

Invokes imm32!ImmLoadLayout and returns PIMEINFOEX.

Definition at line 1722 of file kbdlayout.c.

1723{
1724 PIMEINFOEX piiex;
1725
1726 if (!IS_IME_HKL(hKL) && !IS_CICERO_MODE())
1727 return NULL;
1728
1730 if (!piiex)
1731 return NULL;
1732
1733 if (!co_ClientImmLoadLayout(hKL, piiex))
1734 {
1736 return NULL;
1737 }
1738
1739 return piiex;
1740}
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
#define PagedPool
Definition: env_spec_w32.h:308
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1109
BOOL APIENTRY co_ClientImmLoadLayout(_In_ HKL hKL, _Inout_ PIMEINFOEX pImeInfoEx)
Definition: callback.c:1276
#define USERTAG_IME
Definition: tags.h:240

Referenced by co_IntLoadKeyboardLayoutEx().

◆ co_UserLoadKbdLayout()

static PKL co_UserLoadKbdLayout ( _In_ HANDLE  hKernelFile,
_In_ ULONG  offTable,
_In_ const UNICODE_STRING *  pustrKLID,
_In_ HKL  hKL 
)
static

Definition at line 1221 of file kbdlayout.c.

1226{
1227 LCID lCid;
1229 PKL pKl;
1230
1231 if (!hKernelFile || !offTable)
1232 {
1233 ERR("hKernelFile %p, offTable 0x%08X\n", hKernelFile, offTable);
1234 return NULL;
1235 }
1236
1237 // Retrieve the language identifier.
1238 // NOTE: Do this before creating any object, so that a failure here leaks nothing.
1239 if (!NT_SUCCESS(RtlUnicodeStringToInteger(pustrKLID, 16, (PULONG)&lCid)))
1240 {
1241 ERR("RtlUnicodeStringToInteger failed for '%wZ'\n", pustrKLID);
1242 return NULL;
1243 }
1244
1245 TRACE("Language Identifiers %wZ LCID 0x%x\n", pustrKLID, lCid);
1246
1247 /* Create keyboard layout object */
1249 if (!pKl)
1250 {
1251 ERR("Failed to create object!\n");
1252 return NULL;
1253 }
1254
1255 pKl->hkl = hKL;
1256 pKl->spkf = UserLoadKbdFile(hKernelFile, offTable, pustrKLID);
1257
1258 /* Dereference keyboard layout */
1260
1261 /* If we failed, remove KL object */
1262 if (!pKl->spkf)
1263 {
1264 ERR("UserLoadKbdFile(%wZ) failed!\n", pustrKLID);
1266 return NULL;
1267 }
1268
1269 if (co_IntGetCharsetInfo(lCid, &cs))
1270 {
1271 pKl->iBaseCharset = cs.ciCharset;
1272 pKl->dwFontSigs = cs.fs.fsCsb[0];
1273 pKl->CodePage = (USHORT)cs.ciACP;
1274 TRACE("Charset %u Font Sig %lu CodePage %u\n",
1275 pKl->iBaseCharset, pKl->dwFontSigs, pKl->CodePage);
1276 }
1277 else
1278 {
1280 pKl->dwFontSigs = FS_LATIN1;
1281 pKl->CodePage = CP_ACP;
1282 }
1283
1284 // Set initial system character set and font signature.
1285 if (gSystemFS == 0)
1286 {
1288 gSystemFS = pKl->dwFontSigs;
1289 }
1290
1291 return pKl;
1292}
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
#define cs
Definition: i386-dis.c:442
@ TYPE_KBDLAYOUT
Definition: ntuser.h:58
NTSYSAPI NTSTATUS NTAPI RtlUnicodeStringToInteger(PUNICODE_STRING String, ULONG Base, PULONG Value)
unsigned short USHORT
Definition: pedump.c:61
DWORD LCID
Definition: nls.h:13
#define TRACE(s)
Definition: solgame.cpp:4
DWORD dwFontSigs
Definition: input.h:25
PKBDFILE spkf
Definition: input.h:24
UINT iBaseCharset
Definition: input.h:26
uint32_t * PULONG
Definition: typedefs.h:59
BOOL APIENTRY co_IntGetCharsetInfo(LCID Locale, PCHARSETINFO pCs)
Definition: callback.c:1040
DWORD gSystemFS
Definition: kbdlayout.c:24
UINT gSystemCPCharSet
Definition: kbdlayout.c:25
#define CP_ACP
Definition: kbdlayout.c:18
static PKBDFILE UserLoadKbdFile(_In_ HANDLE hKernelFile, _In_ ULONG offTable, _In_ PCUNICODE_STRING pustrKLID)
Definition: kbdlayout.c:1179
BOOL FASTCALL UserDereferenceObject(PVOID Object)
Definition: object.c:647
BOOL FASTCALL UserDeleteObject(HANDLE h, HANDLE_TYPE type)
Definition: object.c:740
PUSER_HANDLE_TABLE gHandleTable
Definition: object.c:13
PVOID FASTCALL UserCreateObject(PUSER_HANDLE_TABLE ht, PDESKTOP pDesktop, PTHREADINFO pti, HANDLE *h, HANDLE_TYPE type, ULONG size)
Definition: object.c:572
#define ANSI_CHARSET
Definition: wingdi.h:383
#define FS_LATIN1
Definition: wingdi.h:560

Referenced by co_IntLoadKeyboardLayoutEx().

◆ DBG_DEFAULT_CHANNEL()

DBG_DEFAULT_CHANNEL ( UserKbdLayout  )

◆ IntEqualDir()

static BOOL FASTCALL IntEqualDir ( _In_ PCUNICODE_STRING  pustrParent,
_In_ PCUNICODE_STRING  pustrDir 
)
static

Definition at line 1834 of file kbdlayout.c.

1837{
1841 HANDLE hDir;
1843
1844 /* Get the canonical name of the directory */
1850 if (!NT_SUCCESS(Status))
1851 {
1852 ERR("ZwOpenFile(%wZ) failed, Status 0x%08lx\n", pustrDir, Status);
1853 return FALSE;
1854 }
1855
1856 Status = IntQueryHandleName(hDir, &Name);
1857 ZwClose(hDir);
1858 if (!NT_SUCCESS(Status))
1859 {
1860 ERR("IntQueryHandleName(%p) failed, Status 0x%08lx\n", hDir, Status);
1861 return FALSE;
1862 }
1863
1864 if (!RtlEqualUnicodeString(pustrParent, &Name.Info.Name, TRUE))
1865 return FALSE;
1866
1867 return TRUE;
1868}
LONG NTSTATUS
Definition: precomp.h:26
#define FILE_DIRECTORY_FILE
Definition: constants.h:491
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:36
LPWSTR Name
Definition: desk.c:124
_In_ PIO_STATUS_BLOCK IoStatusBlock
Definition: dispmprt.h:608
#define FILE_SHARE_READ
Definition: compat.h:136
#define FILE_SYNCHRONOUS_IO_NONALERT
Definition: from_kernel.h:31
Status
Definition: gdiplustypes.h:24
ZwClose(Handle)
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:115
NTSYSAPI NTSTATUS NTAPI ZwOpenFile(_Out_ PHANDLE FileHandle, _In_ ACCESS_MASK DesiredAccess, _In_ POBJECT_ATTRIBUTES ObjectAttributes, _Out_ PIO_STATUS_BLOCK IoStatusBlock, _In_ ULONG ShareAccess, _In_ ULONG OpenOptions)
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
#define SYNCHRONIZE
Definition: nt_native.h:61
#define FILE_LIST_DIRECTORY
Definition: nt_native.h:629
NTSYSAPI BOOLEAN NTAPI RtlEqualUnicodeString(PUNICODE_STRING String1, PUNICODE_STRING String2, BOOLEAN CaseInSensitive)
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
static NTSTATUS IntQueryHandleName(HANDLE hObject, PKBDFILE_NAME pName)
Definition: kbdlayout.c:1827

Referenced by IntVerifyKeyboardFileHandle().

◆ IntGetKeyboardLayoutList()

static UINT APIENTRY IntGetKeyboardLayoutList ( _Inout_ PWINSTATION_OBJECT  pWinSta,
_In_ ULONG  nBuff,
_Out_ HKL *  pHklBuff 
)
static

Definition at line 94 of file kbdlayout.c.

98{
99 UINT ret = 0;
100 PKL pKL, pFirstKL;
101
102 pFirstKL = gspklBaseLayout; /* FIXME: Use pWinSta->spklList instead */
103 if (!pWinSta || !pFirstKL)
104 return 0;
105
106 pKL = pFirstKL;
107
108 if (nBuff == 0)
109 {
110 /* Count the effective PKLs */
111 do
112 {
113 if (!(pKL->dwKL_Flags & KL_UNLOAD))
114 ++ret;
115 pKL = pKL->pklNext;
116 } while (pKL != pFirstKL);
117 }
118 else
119 {
120 /* Copy the effective HKLs to pHklBuff */
121 do
122 {
123 if (!(pKL->dwKL_Flags & KL_UNLOAD))
124 {
125 *pHklBuff = pKL->hkl;
126 ++pHklBuff;
127 ++ret;
128 --nBuff;
129
130 if (nBuff == 0)
131 break;
132 }
133 pKL = pKL->pklNext;
134 } while (pKL != pFirstKL);
135 }
136
137 return ret;
138}
return ret
Definition: mutex.c:147
unsigned int UINT
Definition: sysinfo.c:13

Referenced by NtUserGetKeyboardLayoutList().

◆ IntHKLtoPKL()

PKL FASTCALL IntHKLtoPKL ( _Inout_ PTHREADINFO  pti,
_In_ HKL  hKL 
)

Definition at line 34 of file kbdlayout.c.

35{
36 PKL pFirstKL, pKL;
37
38 pFirstKL = pti->KeyboardLayout;
39 if (!pFirstKL)
40 return NULL;
41
42 pKL = pFirstKL;
43
44 /* hKL can have special value HKL_NEXT or HKL_PREV */
45 if (hKL == UlongToHandle(HKL_NEXT)) /* Looking forward */
46 {
47 do
48 {
49 pKL = pKL->pklNext;
50 if (!(pKL->dwKL_Flags & KL_UNLOAD))
51 return pKL;
52 } while (pKL != pFirstKL);
53 }
54 else if (hKL == UlongToHandle(HKL_PREV)) /* Looking backward */
55 {
56 do
57 {
58 pKL = pKL->pklPrev;
59 if (!(pKL->dwKL_Flags & KL_UNLOAD))
60 return pKL;
61 } while (pKL != pFirstKL);
62 }
63 else if (HIWORD(hKL)) /* hKL is a full input locale identifier */
64 {
65 /* No KL_UNLOAD check */
66 do
67 {
68 if (pKL->hkl == hKL)
69 return pKL;
70
71 pKL = pKL->pklNext;
72 } while (pKL != pFirstKL);
73 }
74 else /* Language only specified */
75 {
76 /* No KL_UNLOAD check */
77 do
78 {
79 if (LOWORD(pKL->hkl) == LOWORD(hKL)) /* Low word is language ID */
80 return pKL;
81
82 pKL = pKL->pklNext;
83 } while (pKL != pFirstKL);
84 }
85
86 return NULL;
87}
#define LOWORD(l)
Definition: pedump.c:82
#define HKL_PREV
Definition: winuser.h:110
#define HIWORD(l)
Definition: typedefs.h:247

Referenced by co_IntActivateKeyboardLayout(), co_IntUnloadKeyboardLayoutEx(), and IntUnloadKeyboardLayout().

◆ IntImmActivateLayout()

VOID APIENTRY IntImmActivateLayout ( _Inout_ PTHREADINFO  pti,
_Inout_ PKL  pKL 
)

Definition at line 1453 of file kbdlayout.c.

1456{
1457 PWND pImeWnd;
1458 HWND hImeWnd;
1460
1461 if (pti->KeyboardLayout == pKL)
1462 return;
1463
1464 pImeWnd = pti->spwndDefaultIme;
1465 if (pImeWnd)
1466 {
1467 UserRefObjectCo(pImeWnd, &Ref);
1468 hImeWnd = UserHMGetHandle(pImeWnd);
1470 UserDerefObjectCo(pImeWnd);
1471 }
1472 else
1473 {
1474 /* Remember old keyboard layout to switch back for Chinese IMEs */
1475 if (pti->KeyboardLayout)
1476 pti->hklPrev = pti->KeyboardLayout->hkl;
1477
1478 if (pti->spDefaultImc)
1479 {
1480 /* IME Activation is needed */
1481 pti->pClientInfo->CI_flags |= CI_IMMACTIVATE;
1482 }
1483 }
1484
1485 UserAssignmentLock((PVOID*)&(pti->KeyboardLayout), pKL);
1486 pti->pClientInfo->hKL = pKL->hkl;
1487 pti->pClientInfo->CodePage = pKL->CodePage;
1488}
#define IMS_ACTIVATELAYOUT
Definition: imm32_undoc.h:60
#define CI_IMMACTIVATE
Definition: ntuser.h:334

Referenced by co_IntSetKeyboardLayoutForProcess(), and co_UserActivateKeyboardLayout().

◆ IntQueryHandleName()

static NTSTATUS IntQueryHandleName ( HANDLE  hObject,
PKBDFILE_NAME  pName 
)
static

Definition at line 1827 of file kbdlayout.c.

1828{
1829 ULONG cbReturned;
1830 return ZwQueryObject(hObject, ObjectNameInformation, pName, sizeof(*pName), &cbReturned);
1831}
@ ObjectNameInformation
Definition: DriverTester.h:55
static LPSTR pName
Definition: security.c:86
uint32_t ULONG
Definition: typedefs.h:59

Referenced by IntEqualDir(), and IntVerifyKeyboardFileHandle().

◆ IntReorderKeyboardLayouts()

VOID FASTCALL IntReorderKeyboardLayouts ( _Inout_ PWINSTATION_OBJECT  pWinSta,
_Inout_ PKL  pNewKL 
)

Definition at line 1415 of file kbdlayout.c.

1418{
1419 PKL pOldKL = gspklBaseLayout;
1420
1421 if ((pWinSta->Flags & WSS_NOIO) || pNewKL == pOldKL)
1422 return;
1423
1424 pNewKL->pklPrev->pklNext = pNewKL->pklNext;
1425 pNewKL->pklNext->pklPrev = pNewKL->pklPrev;
1426 pNewKL->pklNext = pOldKL;
1427 pNewKL->pklPrev = pOldKL->pklPrev;
1428 pOldKL->pklPrev->pklNext = pNewKL;
1429 pOldKL->pklPrev = pNewKL;
1430 gspklBaseLayout = pNewKL; /* Should we use UserAssignmentLock? */
1431}

Referenced by co_IntActivateKeyboardLayout(), co_IntLoadKeyboardLayoutEx(), and UserSetDefaultInputLang().

◆ IntUnloadKeyboardLayout()

static BOOL APIENTRY IntUnloadKeyboardLayout ( _Inout_ PWINSTATION_OBJECT  pWinSta,
_In_ HKL  hKL 
)
static

Definition at line 1710 of file kbdlayout.c.

1711{
1712 PKL pKL = IntHKLtoPKL(gptiCurrent, hKL);
1713 if (!pKL)
1714 {
1715 ERR("Invalid HKL %p!\n", hKL);
1716 return FALSE;
1717 }
1718 return co_IntUnloadKeyboardLayoutEx(pWinSta, pKL, 0);
1719}
static BOOL APIENTRY co_IntUnloadKeyboardLayoutEx(_Inout_ PWINSTATION_OBJECT pWinSta, _Inout_ PKL pKL, _In_ DWORD dwFlags)
Definition: kbdlayout.c:1663

Referenced by NtUserUnloadKeyboardLayout().

◆ IntVerifyKeyboardFileHandle()

HANDLE FASTCALL IntVerifyKeyboardFileHandle ( HANDLE  hFile)

Validates that the given user-mode handle corresponds to an opened file in SystemRoot\system32.

If so, duplicates it into a kernel-mode handle.

Parameters
[in]hFileUser-mode handle to a file. Accept it only if the file is in system32.
Returns
On success, returns a NEW kernel handle (OBJ_KERNEL_HANDLE) to the same file object. The caller must release it with ZwClose and must use it, not hFile, for all further I/O: the user-mode handle value can be closed and reused by another thread right after the check. The caller's own handle is never closed here (user32 owns and closes it).
Note
Only synchronous file objects are accepted, because KbdLoadImage calls ZwReadFile without an event and relies on the read being complete when it returns.

Definition at line 1890 of file kbdlayout.c.

1891{
1892 static const UNICODE_STRING ustrDir = RTL_CONSTANT_STRING(L"\\SystemRoot\\system32");
1894 UNICODE_STRING ustrParent;
1895 PFILE_OBJECT pFileObject = NULL;
1896 HANDLE hKernelFile = NULL;
1898
1899 if (!hFile)
1900 return NULL;
1901
1902 /* Resolve the user handle exactly once and pin the file object */
1904 (PVOID*)&pFileObject, NULL);
1905 if (!NT_SUCCESS(Status))
1906 {
1907 ERR("ObReferenceObjectByHandle(%p) failed, Status 0x%08lx\n", hFile, Status);
1908 return NULL;
1909 }
1910
1911 if (!(pFileObject->Flags & FO_SYNCHRONOUS_IO))
1912 {
1913 ERR("Not a synchronous file: %p\n", hFile);
1914 ObDereferenceObject(pFileObject);
1915 return NULL;
1916 }
1917
1918 /* Duplicate the handle for kernel-mode, which user mode cannot close or swap */
1921 *IoFileObjectType, KernelMode, &hKernelFile);
1922 ObDereferenceObject(pFileObject);
1923 if (!NT_SUCCESS(Status))
1924 {
1925 ERR("ObOpenObjectByPointer(%p) failed, Status 0x%08lx\n", hFile, Status);
1926 return NULL;
1927 }
1928
1929 /* Get the canonical name of the file */
1930 Status = IntQueryHandleName(hKernelFile, &Name);
1931 if (!NT_SUCCESS(Status))
1932 {
1933 ERR("IntQueryHandleName(%p) failed, Status 0x%08lx\n", hFile, Status);
1934 goto Failure;
1935 }
1936
1937 /* Strip the filename of the file */
1938 ustrParent = Name.Info.Name;
1939 while (ustrParent.Length > 0)
1940 {
1941 ustrParent.Length -= sizeof(WCHAR);
1942 if (ustrParent.Buffer[ustrParent.Length / sizeof(WCHAR)] == L'\\')
1943 break;
1944 }
1945
1946 if (!IntEqualDir(&ustrParent, &ustrDir))
1947 {
1948 ERR("Not in system32: %wZ\n", &Name.Info.Name);
1949 goto Failure;
1950 }
1951
1952 return hKernelFile; /* Success */
1953
1954Failure:
1955 ZwClose(hKernelFile);
1956 return NULL;
1957}
#define RTL_CONSTANT_STRING(s)
Definition: combase.c:35
#define L(x)
Definition: resources.c:13
POBJECT_TYPE IoFileObjectType
Definition: iomgr.c:36
_In_ HANDLE hFile
Definition: mswsock.h:90
#define KernelMode
Definition: asm.h:38
#define UserMode
Definition: asm.h:39
#define FILE_READ_DATA
Definition: nt_native.h:628
#define FILE_READ_ATTRIBUTES
Definition: nt_native.h:647
NTSTATUS NTAPI ObOpenObjectByPointer(IN PVOID Object, IN ULONG HandleAttributes, IN PACCESS_STATE PassedAccessState, IN ACCESS_MASK DesiredAccess, IN POBJECT_TYPE ObjectType, IN KPROCESSOR_MODE AccessMode, OUT PHANDLE Handle)
Definition: obhandle.c:2769
NTSTATUS NTAPI ObReferenceObjectByHandle(IN HANDLE Handle, IN ACCESS_MASK DesiredAccess, IN POBJECT_TYPE ObjectType, IN KPROCESSOR_MODE AccessMode, OUT PVOID *Object, OUT POBJECT_HANDLE_INFORMATION HandleInformation OPTIONAL)
Definition: obref.c:493
short WCHAR
Definition: pedump.c:58
static BOOL FASTCALL IntEqualDir(_In_ PCUNICODE_STRING pustrParent, _In_ PCUNICODE_STRING pustrDir)
Definition: kbdlayout.c:1834
* PFILE_OBJECT
Definition: iotypes.h:1998
#define FO_SYNCHRONOUS_IO
Definition: iotypes.h:1776
#define ObDereferenceObject
Definition: obfuncs.h:203

Referenced by NtUserLoadKeyboardLayoutEx().

◆ KbdBuildImage()

static PVOID KbdBuildImage ( _In_ PVOID  pvRaw,
_In_ SIZE_T  cbRaw,
_Out_ PULONG  pcbImage,
_Out_ PKBDIMAGE_INFO  pInfo 
)
static

Build the image of the keyboard layout file without executing it, due to security reasons.

For a valid keyboard layout file, the tables (and any data they point to) are all stored in the .data section, so this is the only section we take. The headers and any other sections are neither considered nor retained (the returned buffer is exactly the size of the .data section), and neither is the base-relocation data: the pointers inside the tables are adjusted later by KbdRebaseTables(), using the RVA macro.

Note
Offset 0 of the returned buffer corresponds to RVA pInfo->offData.

Definition at line 271 of file kbdlayout.c.

276{
277 static const UCHAR s_DataName[IMAGE_SIZEOF_SHORT_NAME] = ".data";
278 PIMAGE_DOS_HEADER pDos = pvRaw;
281 PBYTE pbRaw = pvRaw;
282 _SEH2_VOLATILE PBYTE pbImage = NULL;
283 ULONG cbImage, cbVirt, cbCopy, i;
284 SIZE_T offSec;
285 ULONG_PTR imageBase;
286
287 *pcbImage = 0;
288 RtlZeroMemory(pInfo, sizeof(*pInfo));
289
291 {
292 if (cbRaw < sizeof(IMAGE_DOS_HEADER) || pDos->e_magic != IMAGE_DOS_SIGNATURE ||
293 pDos->e_lfanew <= 0 || cbRaw < sizeof(IMAGE_NT_HEADERS) ||
294 (SIZE_T)pDos->e_lfanew > cbRaw - sizeof(IMAGE_NT_HEADERS) || (pDos->e_lfanew & 3))
295 {
296 _SEH2_YIELD(return NULL);
297 }
298
299 pNt = (PIMAGE_NT_HEADERS)(pbRaw + pDos->e_lfanew);
300 if (pNt->Signature != IMAGE_NT_SIGNATURE)
301 _SEH2_YIELD(return NULL);
302
303#ifdef _M_AMD64
306 {
307 imageBase = (ULONG_PTR)((PIMAGE_NT_HEADERS64)pNt)->OptionalHeader.ImageBase;
308 }
309#elif defined(_M_ARM64)
312 {
313 imageBase = (ULONG_PTR)((PIMAGE_NT_HEADERS64)pNt)->OptionalHeader.ImageBase;
314 }
315#elif defined(_M_ARM)
318 {
319 imageBase = ((PIMAGE_NT_HEADERS32)pNt)->OptionalHeader.ImageBase;
320 }
321#elif defined(_M_IX86)
324 {
325 imageBase = ((PIMAGE_NT_HEADERS32)pNt)->OptionalHeader.ImageBase;
326 }
327#else
328 #error Unsupported architecture.
329#endif
330 else
331 {
332 _SEH2_YIELD(return NULL);
333 }
334
335 cbImage = pNt->OptionalHeader.SizeOfImage;
336 if (!cbImage || cbImage > KBDIMG_MAXSIZE)
337 _SEH2_YIELD(return NULL);
338
339 /* Keep pSec aligned: IMAGE_FIRST_SECTION depends on SizeOfOptionalHeader */
340 if (pNt->FileHeader.SizeOfOptionalHeader & 3)
341 _SEH2_YIELD(return NULL);
342
343 /* The section headers must lie within the file (compare offsets, not pointers) */
344 pSec = IMAGE_FIRST_SECTION(pNt);
345 offSec = (SIZE_T)((PBYTE)pSec - pbRaw);
346 if (offSec > cbRaw ||
347 (cbRaw - offSec) / sizeof(IMAGE_SECTION_HEADER) < pNt->FileHeader.NumberOfSections)
348 {
349 _SEH2_YIELD(return NULL);
350 }
351
352 /* Find the .data section. Any other section is ignored. */
353 for (i = 0; i < pNt->FileHeader.NumberOfSections; ++i)
354 {
355 if (RtlEqualMemory(pSec[i].Name, s_DataName, sizeof(s_DataName)))
356 {
357 pData = &pSec[i];
358 break;
359 }
360 }
361 if (!pData)
362 {
363 ERR("No .data section\n");
364 _SEH2_YIELD(return NULL);
365 }
366
367 /* The part beyond SizeOfRawData (if any) is zero-filled, as the loader does */
368 cbVirt = pData->Misc.VirtualSize ? pData->Misc.VirtualSize : pData->SizeOfRawData;
369 cbCopy = min(pData->SizeOfRawData, cbVirt);
370 if (!cbVirt || pData->VirtualAddress > cbImage ||
371 cbVirt > cbImage - pData->VirtualAddress || pData->PointerToRawData > cbRaw ||
372 cbCopy > cbRaw - pData->PointerToRawData)
373 {
374 ERR("Invalid .data section\n");
375 _SEH2_YIELD(return NULL);
376 }
377
378 /* Retain the .data section only. The buffer starts at the beginning of .data,
379 * so an RVA must be converted to an offset by subtracting offData. */
380 pbImage = ExAllocatePoolZero(PagedPool, cbVirt, KBDIMG_TAG);
381 if (!pbImage)
382 _SEH2_YIELD(return NULL);
383 RtlCopyMemory(pbImage, pbRaw + pData->PointerToRawData, cbCopy);
384
385 pInfo->ImageBase = imageBase;
386 pInfo->offData = pData->VirtualAddress;
387 pInfo->cbData = cbVirt;
388 *pcbImage = cbVirt;
389 _SEH2_YIELD(return pbImage);
390 }
392 {
393 ERR("Exception in KbdBuildImage! Status: 0x%lx\n", _SEH2_GetExceptionCode());
394 }
395 _SEH2_END;
396
397 if (pbImage)
399 return NULL;
400}
#define IMAGE_FILE_MACHINE_ARMNT
Definition: compat.h:127
#define IMAGE_FILE_MACHINE_ARM64
Definition: compat.h:129
#define ULONG_PTR
Definition: config.h:101
struct _IMAGE_NT_HEADERS * PIMAGE_NT_HEADERS32
PIMAGE_NT_HEADERS32 PIMAGE_NT_HEADERS
Definition: ntddk_ex.h:187
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:90
#define RtlEqualMemory(dst, src, len)
Definition: kdvm.h:18
static PVOID ExAllocatePoolZero(ULONG PoolType, SIZE_T NumberOfBytes, ULONG Tag)
Definition: precomp.h:45
#define min(a, b)
Definition: monoChain.cc:55
#define IMAGE_NT_OPTIONAL_HDR32_MAGIC
Definition: ntimage.h:377
#define IMAGE_NT_OPTIONAL_HDR64_MAGIC
Definition: ntimage.h:378
#define IMAGE_FIRST_SECTION(NtHeader)
Definition: ntimage.h:428
#define IMAGE_FILE_MACHINE_AMD64
Definition: ntimage.h:17
#define IMAGE_FILE_MACHINE_I386
Definition: pedump.c:174
BYTE * PBYTE
Definition: pedump.c:66
#define IMAGE_NT_SIGNATURE
Definition: pedump.c:93
#define IMAGE_SIZEOF_SHORT_NAME
Definition: pedump.c:277
#define IMAGE_DOS_SIGNATURE
Definition: pedump.c:89
struct _IMAGE_SECTION_HEADER IMAGE_SECTION_HEADER
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:204
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:104
#define _SEH2_VOLATILE
Definition: pseh2_64.h:208
#define _SEH2_END
Definition: pseh2_64.h:194
#define _SEH2_TRY
Definition: pseh2_64.h:93
#define _SEH2_YIELD(__stmt)
Definition: pseh2_64.h:207
WORD SizeOfOptionalHeader
Definition: ntddk_ex.h:127
IMAGE_OPTIONAL_HEADER32 OptionalHeader
Definition: ntddk_ex.h:184
IMAGE_FILE_HEADER FileHeader
Definition: ntddk_ex.h:183
TW_UINT32 TW_UINT16 TW_UINT16 TW_MEMREF pData
Definition: twain.h:1830
unsigned char UCHAR
Definition: typedefs.h:53
ULONG_PTR SIZE_T
Definition: typedefs.h:80
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
uint32_t ULONG_PTR
Definition: typedefs.h:65
#define KBDIMG_TAG
Definition: kbdlayout.c:239
#define KBDIMG_MAXSIZE
Definition: kbdlayout.c:240

Referenced by KbdLoadImage().

◆ KbdCheckDeadKeyNames()

static BOOL KbdCheckDeadKeyNames ( _In_ PKBDVALIDATE  pv,
_Inout_ PKBDTABLES  pTbl 
)
static

Verifies and rebases the dead key names (optional). This array of strings ends with NULL.

Parameters
[in]pvThe state of validation.
[in,out]pTblThe main tables to verify and rebase the dead key names.

Definition at line 850 of file kbdlayout.c.

853{
854 LPWSTR *ppwsz;
855 ULONG i, cNames;
856
857 if (!KBD_REBASE(pv, pTbl->pKeyNamesDead, sizeof(LPWSTR), TYPE_ALIGNMENT(LPWSTR), FALSE))
858 return FALSE;
859 if (!pTbl->pKeyNamesDead)
860 return TRUE;
861
862 if (!KbdMeasureTable(pv, pTbl->pKeyNamesDead, sizeof(LPWSTR), sizeof(LPWSTR),
863 TYPE_ALIGNMENT(LPWSTR), KBD_MAX_DEADNAMES, &cNames, "pKeyNamesDead"))
864 {
865 return FALSE;
866 }
867
868 ppwsz = pTbl->pKeyNamesDead;
869 for (i = 0; i < cNames; ++i)
870 {
871 if (!KbdRebaseSlot(pv, (PVOID*)&ppwsz[i], sizeof(WCHAR), TYPE_ALIGNMENT(WCHAR),
872 TRUE, "pKeyNamesDead[]") ||
873 !KbdCheckWideString(pv, ppwsz[i]))
874 {
875 ERR("pKeyNamesDead[%lu]: Invalid name\n", i);
876 return FALSE;
877 }
878 }
879 return TRUE;
880}
#define TYPE_ALIGNMENT(t)
Definition: ntbasedef.h:117
uint16_t * LPWSTR
Definition: typedefs.h:56
static BOOL KbdCheckWideString(_In_ const KBDVALIDATE *pv, _In_ const WCHAR *pwsz)
Verifies a wide string is terminated within the layout data (and is not unreasonably long).
Definition: kbdlayout.c:658
static BOOL KbdRebaseSlot(_In_ PKBDVALIDATE pv, _Inout_ PVOID *ppSlot, _In_ SIZE_T cbMin, _In_ SIZE_T cbAlign, _In_ BOOL bRequired, _In_ PCSTR pszName)
Converts a pointer slot of the layout tables from a virtual address (relative to ImageBase) to a poin...
Definition: kbdlayout.c:547
static BOOL KbdMeasureTable(_In_ const KBDVALIDATE *pv, _In_ const VOID *pTable, _In_ SIZE_T cbElem, _In_ SIZE_T cbKey, _In_ SIZE_T cbAlign, _In_ ULONG cMax, _Out_ PULONG pcElems, _In_ PCSTR pszName)
Measures a table that ends with a terminating element, verifying every element (the terminator includ...
Definition: kbdlayout.c:609
#define KBD_MAX_DEADNAMES
Definition: kbdlayout.c:492
#define KBD_REBASE(pv, field, cbMin, cbAlign, bRequired)
Definition: kbdlayout.c:599

Referenced by KbdRebaseTables().

◆ KbdCheckDeadKeys()

static BOOL KbdCheckDeadKeys ( _In_ PKBDVALIDATE  pv,
_Inout_ PKBDTABLES  pTbl 
)
static

Verifies and rebases the dead keys (optional). This table ends with an entry that has dwBoth == 0.

Parameters
[in]pvThe state of validation.
[in,out]pTblThe main tables to verify and rebase the dead keys.

Definition at line 914 of file kbdlayout.c.

917{
919
920 if (!KBD_REBASE(pv, pTbl->pDeadKey, sizeof(DEADKEY), TYPE_ALIGNMENT(DEADKEY), FALSE))
921 return FALSE;
922 if (!pTbl->pDeadKey)
923 return TRUE;
924
925 return KbdMeasureTable(pv, pTbl->pDeadKey, sizeof(DEADKEY), sizeof(DWORD),
927}
unsigned long DWORD
Definition: ntddk_ex.h:95
Definition: kbd.h:111
#define KBD_MAX_DEADKEYS
Definition: kbdlayout.c:493
_In_ UINT _In_ UINT cEntries
Definition: wingdi.h:4067

Referenced by KbdRebaseTables().

◆ KbdCheckKeyNames()

static BOOL KbdCheckKeyNames ( _In_ PKBDVALIDATE  pv,
_Inout_ VSC_LPWSTR **  ppNames,
_In_ PCSTR  pszName 
)
static

Verifies and rebases the key name. This table ends with an entry that has vsc == 0.

Parameters
[in]pvThe state of validation.
[in,out]ppNamesThe target VSC_LPWSTR table to verify and rebase the key name.
[in]pszNameThe target key name to verify.

Definition at line 812 of file kbdlayout.c.

816{
817 VSC_LPWSTR *pNames;
818 ULONG i, cNames;
819
820 if (!KbdRebaseSlot(pv, (PVOID*)ppNames, sizeof(VSC_LPWSTR), TYPE_ALIGNMENT(VSC_LPWSTR),
821 TRUE, pszName) ||
822 !KbdMeasureTable(pv, *ppNames, sizeof(VSC_LPWSTR), sizeof(BYTE),
823 TYPE_ALIGNMENT(VSC_LPWSTR), KBD_MAX_KEYNAMES, &cNames, pszName))
824 {
825 return FALSE;
826 }
827
828 pNames = *ppNames;
829 for (i = 0; i < cNames; ++i)
830 {
831 if (!KbdRebaseSlot(pv, (PVOID*)&pNames[i].pwsz, sizeof(WCHAR), TYPE_ALIGNMENT(WCHAR),
832 TRUE, pszName) ||
833 !KbdCheckWideString(pv, pNames[i].pwsz))
834 {
835 ERR("%s[%lu]: Invalid name\n", pszName, i);
836 return FALSE;
837 }
838 }
839 return TRUE;
840}
#define KBD_MAX_KEYNAMES
Definition: kbdlayout.c:491
unsigned char BYTE
Definition: xxhash.c:193

Referenced by KbdRebaseTables().

◆ KbdCheckLigatures()

static BOOL KbdCheckLigatures ( _In_ PKBDVALIDATE  pv,
_Inout_ PKBDTABLES  pTbl 
)
static

Verifies and rebases the ligatures (optional). This table ends with an entry that has VirtualKey == 0.

Parameters
[in]pvThe state of validation.
[in,out]pTblThe main tables to verify and rebase its ligatures.

Definition at line 938 of file kbdlayout.c.

941{
943 SIZE_T cbEntry;
944
945 if (!pTbl->pLigature) /* Not yet rebased, but we only test it for NULL here */
946 return KBD_REBASE(pv, pTbl->pLigature, 0, 1, FALSE);
947
948 cbEntry = FIELD_OFFSET(LIGATURE1, wch) + pTbl->nLgMaxd * sizeof(WCHAR);
949 if (pTbl->nLgMaxd < 1 || pTbl->nLgMaxd > KBD_MAX_LIGATURE_CCH || pTbl->cbLgEntry != cbEntry)
950 {
951 ERR("Invalid ligature stride: nLgMaxd %u, cbLgEntry %u\n",
952 pTbl->nLgMaxd, pTbl->cbLgEntry);
953 return FALSE;
954 }
955
956 return KBD_REBASE(pv, pTbl->pLigature, cbEntry, TYPE_ALIGNMENT(LIGATURE1), TRUE) &&
957 KbdMeasureTable(pv, pTbl->pLigature, cbEntry, sizeof(BYTE), TYPE_ALIGNMENT(LIGATURE1),
958 KBD_MAX_LIGATURES, &cEntries, "pLigature");
959}
#define FIELD_OFFSET(t, f)
Definition: typedefs.h:255
#define KBD_MAX_LIGATURES
Definition: kbdlayout.c:495
#define KBD_MAX_LIGATURE_CCH
Definition: kbdlayout.c:499

Referenced by KbdRebaseTables().

◆ KbdCheckModifiers()

static BOOL KbdCheckModifiers ( _In_ PKBDVALIDATE  pv,
_Inout_ PKBDTABLES  pTbl 
)
static

Verifies and rebases the modifiers in the main tables of the layout data.

Parameters
[in]pvThe state of validation.
[in,out]pTblThe main tables to verify and rebase the modifiers.

Definition at line 687 of file kbdlayout.c.

690{
691 PMODIFIERS pMod;
692 ULONG i, cVkToBit;
693 SIZE_T cbMod;
694
695 if (!KBD_REBASE(pv, pTbl->pCharModifiers, sizeof(MODIFIERS), TYPE_ALIGNMENT(MODIFIERS), TRUE))
696 return FALSE;
697 pMod = pTbl->pCharModifiers;
698
699 /* ModNumber[] is indexed by the modifier bits, up to wMaxModBits (inclusive) */
700 cbMod = FIELD_OFFSET(MODIFIERS, ModNumber) + ((SIZE_T)pMod->wMaxModBits + 1) * sizeof(BYTE);
701 if (pMod->wMaxModBits > 0xFF || !KbdIsBufInRange(pv, pMod, cbMod, TYPE_ALIGNMENT(MODIFIERS)))
702 {
703 ERR("Invalid wMaxModBits: 0x%X\n", pMod->wMaxModBits);
704 return FALSE;
705 }
706 for (i = 0; i <= pMod->wMaxModBits; ++i)
707 {
708 /* A column number must not exceed the widest VK_TO_WCHARS<n> */
709 if (pMod->ModNumber[i] != SHFT_INVALID && pMod->ModNumber[i] >= KBD_MAX_MOD_COLUMNS)
710 {
711 ERR("Invalid ModNumber[%lu]: 0x%X\n", i, pMod->ModNumber[i]);
712 return FALSE;
713 }
714 }
715
716 if (!KBD_REBASE(pv, pMod->pVkToBit, sizeof(VK_TO_BIT), TYPE_ALIGNMENT(VK_TO_BIT), TRUE) ||
717 !KbdMeasureTable(pv, pMod->pVkToBit, sizeof(VK_TO_BIT), sizeof(BYTE),
718 TYPE_ALIGNMENT(VK_TO_BIT), KBD_MAX_VKTOBIT, &cVkToBit, "pVkToBit"))
719 {
720 return FALSE;
721 }
722 return TRUE;
723}
#define SHFT_INVALID
Definition: kbd.h:74
Definition: kbd.h:81
WORD wMaxModBits
Definition: kbd.h:83
BYTE ModNumber[]
Definition: kbd.h:84
Definition: kbd.h:76
static BOOL KbdIsBufInRange(_In_ const KBDVALIDATE *pv, _In_ const VOID *p, _In_ SIZE_T cb, _In_ SIZE_T cbAlign)
Definition: kbdlayout.c:513
#define KBD_MAX_VKTOBIT
Definition: kbdlayout.c:488
#define KBD_MAX_MOD_COLUMNS
Definition: kbdlayout.c:498

Referenced by KbdRebaseTables().

◆ KbdCheckNls()

static BOOL KbdCheckNls ( _In_ PKBDVALIDATE  pv,
_Inout_ PKBDNLSTABLES  pNls 
)
static

Verifies and rebases the NLS (national language support; for Far-East) tables. These tables are counted, not terminated.

Parameters
[in]pvThe state of validation.
[in,ouy]pNls The NLS tables to verify and rebase.

Definition at line 970 of file kbdlayout.c.

973{
974 PVK_F pVkToF;
975 ULONG i, j;
976
977 if (pNls->NumOfVkToF > KBD_MAX_NLS_ENTRIES ||
978 pNls->NumOfMouseVKey < 0 || pNls->NumOfMouseVKey > KBD_MAX_NLS_ENTRIES)
979 {
980 ERR("Invalid NLS counts: %lu, %d\n", pNls->NumOfVkToF, pNls->NumOfMouseVKey);
981 return FALSE;
982 }
983
984 /* A pointer is required only if its count is not zero */
985 if (!KBD_REBASE(pv, pNls->pVkToF, pNls->NumOfVkToF * sizeof(VK_F), TYPE_ALIGNMENT(VK_F),
986 pNls->NumOfVkToF != 0) ||
987 !KBD_REBASE(pv, pNls->pusMouseVKey, pNls->NumOfMouseVKey * sizeof(USHORT),
988 TYPE_ALIGNMENT(USHORT), pNls->NumOfMouseVKey != 0))
989 {
990 return FALSE;
991 }
992
993 /* The indexes in these entries select the functions and the arrays of 8 elements */
994 pVkToF = pNls->pVkToF;
995 for (i = 0; i < pNls->NumOfVkToF; ++i)
996 {
997 if (pVkToF[i].NLSFEProcType > KBDNLS_TYPE_TOGGLE ||
998 pVkToF[i].NLSFEProcCurrent >= RTL_NUMBER_OF(pVkToF[i].NLSFEProc))
999 {
1000 ERR("Invalid VK_F[%lu]\n", i);
1001 return FALSE;
1002 }
1003 for (j = 0; j < RTL_NUMBER_OF(pVkToF[i].NLSFEProc); ++j)
1004 {
1005 if (pVkToF[i].NLSFEProc[j].NLSFEProcIndex > KBDNLS_CONV_OR_NONCONV ||
1006 pVkToF[i].NLSFEProcAlt[j].NLSFEProcIndex > KBDNLS_CONV_OR_NONCONV)
1007 {
1008 ERR("Invalid VK_F[%lu] procedure\n", i);
1009 return FALSE;
1010 }
1011 }
1012 }
1013 return TRUE;
1014}
#define RTL_NUMBER_OF(x)
Definition: RtlRegistry.c:12
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint GLint GLint j
Definition: glfuncs.h:250
#define KBDNLS_TYPE_TOGGLE
Definition: kbd.h:212
#define KBDNLS_CONV_OR_NONCONV
Definition: kbd.h:233
#define KBD_MAX_NLS_ENTRIES
Definition: kbdlayout.c:496

Referenced by KbdRebaseTables().

◆ KbdCheckVkToWchars()

static BOOL KbdCheckVkToWchars ( _In_ PKBDVALIDATE  pv,
_Inout_ PKBDTABLES  pTbl,
_Out_ PBOOL  pbLigature,
_Out_ PBOOL  pbDead 
)
static

Verifies and rebases the VK_TO_WCHAR_TABLE structure in the layout data.

Parameters
[in]pvThe state of validation.
[in,out]pTblThe main tables to verify and rebase the VK_TO_WCHAR_TABLE structure.
[out]pbLigatureThe pointer to a BOOL value that indicates existence of ligature.
[out]pbDeadThe pointer to a BOOL value that indicates existence of dead-key.

Definition at line 735 of file kbdlayout.c.

740{
741 PVK_TO_WCHAR_TABLE pWchTbl;
742 const WCHAR *pwch;
743 SIZE_T cbEntry;
744 ULONG iTbl, cTbls, iVk, cVks, iMod;
745
746 *pbLigature = *pbDead = FALSE;
747
748 /* This table ends with an entry that has NULL pVkToWchars */
749 if (!KBD_REBASE(pv, pTbl->pVkToWcharTable, sizeof(VK_TO_WCHAR_TABLE),
751 !KbdMeasureTable(pv, pTbl->pVkToWcharTable, sizeof(VK_TO_WCHAR_TABLE), sizeof(PVOID),
753 "pVkToWcharTable"))
754 {
755 return FALSE;
756 }
757 if (cTbls == 0)
758 {
759 ERR("pVkToWcharTable is empty\n");
760 return FALSE;
761 }
762
763 for (iTbl = 0; iTbl < cTbls; ++iTbl)
764 {
765 pWchTbl = &pTbl->pVkToWcharTable[iTbl];
766
767 /* The stride must match the number of columns, or we would walk (and later
768 * index) the entries wrongly */
769 cbEntry = FIELD_OFFSET(VK_TO_WCHARS1, wch) + pWchTbl->nModifications * sizeof(WCHAR);
770 if (pWchTbl->nModifications < 1 || pWchTbl->nModifications > KBD_MAX_MOD_COLUMNS ||
771 pWchTbl->cbSize != cbEntry)
772 {
773 ERR("Invalid table: nModifications %u, cbSize %u\n",
774 pWchTbl->nModifications, pWchTbl->cbSize);
775 return FALSE;
776 }
777
778 /* The entries end with an entry that has VirtualKey == 0 */
779 if (!KBD_REBASE(pv, pWchTbl->pVkToWchars, cbEntry, TYPE_ALIGNMENT(VK_TO_WCHARS1), TRUE) ||
780 !KbdMeasureTable(pv, pWchTbl->pVkToWchars, cbEntry, sizeof(BYTE),
781 TYPE_ALIGNMENT(VK_TO_WCHARS1), KBD_MAX_VKTOWCH, &cVks,
782 "pVkToWchars"))
783 {
784 return FALSE;
785 }
786
787 for (iVk = 0; iVk < cVks; ++iVk)
788 {
789 pwch = (const WCHAR *)((PBYTE)pWchTbl->pVkToWchars + iVk * cbEntry +
790 FIELD_OFFSET(VK_TO_WCHARS1, wch));
791 for (iMod = 0; iMod < pWchTbl->nModifications; ++iMod)
792 {
793 if (pwch[iMod] == WCH_LGTR)
794 *pbLigature = TRUE;
795 else if (pwch[iMod] == WCH_DEAD)
796 *pbDead = TRUE;
797 }
798 }
799 }
800 return TRUE;
801}
#define WCH_DEAD
Definition: kbd.h:170
#define WCH_LGTR
Definition: kbd.h:171
BYTE nModifications
Definition: kbd.h:107
#define KBD_MAX_WCHTABLES
Definition: kbdlayout.c:489
#define KBD_MAX_VKTOWCH
Definition: kbdlayout.c:490

Referenced by KbdRebaseTables().

◆ KbdCheckVscToVk()

static BOOL KbdCheckVscToVk ( _In_ PKBDVALIDATE  pv,
_Inout_ PVSC_VK *  ppVscVk,
_In_ PCSTR  pszName 
)
static

Verifies and rebases the VSC -> VK table for the E0/E1 prefixes. They end with an entry that has Vsc == 0.

Parameters
[in]pvThe state of validation.
[in,out]ppVscVkThe VSC_VK structure to verify and rebase the VSC -> VK table.
[in]pszNameThe target name.

Definition at line 892 of file kbdlayout.c.

896{
898
899 return KbdRebaseSlot(pv, (PVOID*)ppVscVk, sizeof(VSC_VK), TYPE_ALIGNMENT(VSC_VK), TRUE,
900 pszName) &&
901 KbdMeasureTable(pv, *ppVscVk, sizeof(VSC_VK), sizeof(BYTE), TYPE_ALIGNMENT(VSC_VK),
902 KBD_MAX_VSCVK, &cEntries, pszName);
903}
Definition: kbd.h:127
#define KBD_MAX_VSCVK
Definition: kbdlayout.c:494

Referenced by KbdRebaseTables().

◆ KbdCheckWideString()

static BOOL KbdCheckWideString ( _In_ const KBDVALIDATE *  pv,
_In_ const WCHAR *  pwsz 
)
static

Verifies a wide string is terminated within the layout data (and is not unreasonably long).

Parameters
[in]pvThe state of validation.
[in]pszThe target wide string to verify.

Definition at line 658 of file kbdlayout.c.

661{
662 SIZE_T i, cchMax;
663
664 if (!KbdIsBufInRange(pv, pwsz, sizeof(WCHAR), TYPE_ALIGNMENT(WCHAR)))
665 return FALSE;
666
667 cchMax = (pv->pbImage + pv->cbData - (const BYTE *)pwsz) / sizeof(WCHAR);
669 for (i = 0; i < cchMax; ++i)
670 {
671 if (!pwsz[i])
672 return TRUE;
673 }
674
675 ERR("Unterminated string at 0x%p\n", pwsz);
676 return FALSE;
677}
UINT cchMax
#define KBD_MAX_NAME_CCH
Definition: kbdlayout.c:497

Referenced by KbdCheckDeadKeyNames(), and KbdCheckKeyNames().

◆ KbdIsBufInRange()

static BOOL KbdIsBufInRange ( _In_ const KBDVALIDATE *  pv,
_In_ const VOID *  p,
_In_ SIZE_T  cb,
_In_ SIZE_T  cbAlign 
)
static

Definition at line 513 of file kbdlayout.c.

518{
519 ULONG_PTR off;
520
521 if ((ULONG_PTR)p < (ULONG_PTR)pv->pbImage)
522 return FALSE;
523
524 off = (ULONG_PTR)p - (ULONG_PTR)pv->pbImage;
525 if ((off & (cbAlign - 1)) || cb > pv->cbData || off > pv->cbData - cb)
526 return FALSE;
527
528 return TRUE;
529}
static MonoProfilerRuntimeShutdownBeginCallback cb
Definition: metahost.c:118
GLfloat GLfloat p
Definition: glext.h:8902

Referenced by KbdCheckModifiers(), KbdCheckWideString(), KbdMeasureTable(), KbdRebaseSlot(), and KbdRebaseTables().

◆ KbdIsRangeInData()

static __inline BOOL KbdIsRangeInData ( _In_ const KBDIMAGE_INFO *  pInfo,
_In_ ULONG_PTR  off,
_In_ SIZE_T  cb 
)
static

Definition at line 479 of file kbdlayout.c.

480{
481 return (off >= pInfo->offData) && (cb <= pInfo->cbData) &&
482 ((off - pInfo->offData) <= (pInfo->cbData - cb));
483}

Referenced by UserLoadKbdDll().

◆ KbdLoadImage()

static PVOID KbdLoadImage ( _In_ HANDLE  hKernelFile,
_Out_ PULONG  pcbImage,
_Out_ PKBDIMAGE_INFO  pInfo 
)
static

Definition at line 404 of file kbdlayout.c.

408{
413 PVOID pvRaw, pvImage;
414 ULONG cbRaw;
415
416 *pcbImage = 0;
417 RtlZeroMemory(pInfo, sizeof(*pInfo));
418
419 if (!hKernelFile)
420 return NULL;
421
422 /* Get the file size */
423 Status = ZwQueryInformationFile(hKernelFile, &IoStatusBlock, &FileInfo,
425 if (!NT_SUCCESS(Status))
426 {
427 ERR("ZwQueryInformationFile failed: 0x%08X\n", Status);
428 return NULL;
429 }
430 if (FileInfo.EndOfFile.HighPart != 0 || FileInfo.EndOfFile.LowPart > KBDIMG_MAXSIZE)
431 {
432 ERR("Invalid file size (%I64u bytes)\n", FileInfo.EndOfFile.QuadPart);
433 return NULL;
434 }
435 cbRaw = FileInfo.EndOfFile.LowPart;
436 if (cbRaw < sizeof(IMAGE_DOS_HEADER)) /* Too small to be a PE file? */
437 {
438 ERR("Invalid file size (%lu bytes)\n", cbRaw);
439 return NULL;
440 }
441
442 /* Allocate raw image buffer */
444 if (!pvRaw)
445 return NULL;
446
447 /* Load the file content */
448 Offset.QuadPart = 0;
449 Status = ZwReadFile(hKernelFile, NULL, NULL, NULL, &IoStatusBlock,
450 pvRaw, cbRaw, &Offset, NULL);
451 if (!NT_SUCCESS(Status) || IoStatusBlock.Information != cbRaw)
452 {
453 ERR("ZwReadFile failed: 0x%08X (read %Iu of %lu)\n",
454 Status, IoStatusBlock.Information, cbRaw);
456 return NULL;
457 }
458
459 /* Take the .data section only (no relocation; see KbdRebaseTables) */
460 pvImage = KbdBuildImage(pvRaw, cbRaw, pcbImage, pInfo);
461 if (!pvImage)
462 ERR("KbdBuildImage failed\n");
463
465 return pvImage;
466}
_In_ ULONG _In_ ULONG Offset
Definition: ntddpcm.h:101
#define FileStandardInformation
Definition: propsheet.cpp:61
static PVOID KbdBuildImage(_In_ PVOID pvRaw, _In_ SIZE_T cbRaw, _Out_ PULONG pcbImage, _Out_ PKBDIMAGE_INFO pInfo)
Build the image of the keyboard layout file without executing it, due to security reasons.
Definition: kbdlayout.c:271

Referenced by UserLoadKbdDll().

◆ KbdMeasureTable()

static BOOL KbdMeasureTable ( _In_ const KBDVALIDATE *  pv,
_In_ const VOID *  pTable,
_In_ SIZE_T  cbElem,
_In_ SIZE_T  cbKey,
_In_ SIZE_T  cbAlign,
_In_ ULONG  cMax,
_Out_ PULONG  pcElems,
_In_ PCSTR  pszName 
)
static

Measures a table that ends with a terminating element, verifying every element (the terminator included) in full, that is, with the stride of the element. The terminator is an element whose first cbKey bytes are all zero.

Definition at line 609 of file kbdlayout.c.

618{
619 const BYTE *pb;
620 SIZE_T k;
621 ULONG i;
622
623 *pcElems = 0;
624
625 for (i = 0; i < cMax; ++i)
626 {
627 pb = (const BYTE *)pTable + i * cbElem;
628 if (!KbdIsBufInRange(pv, pb, cbElem, cbAlign))
629 {
630 ERR("%s: Element %lu is out of range\n", pszName, i);
631 return FALSE;
632 }
633
634 for (k = 0; k < cbKey; ++k)
635 {
636 if (pb[k])
637 break;
638 }
639 if (k == cbKey) /* The terminator */
640 {
641 *pcElems = i;
642 return TRUE;
643 }
644 }
645
646 ERR("%s: No terminator within %lu elements\n", pszName, cMax);
647 return FALSE;
648}
static const uint32_t k[]
Definition: sha256.c:24
PCBYTE SIZE_T cbKey
static const EHCI_PERIOD pTable[]
Definition: usbehci.c:29

Referenced by KbdCheckDeadKeyNames(), KbdCheckDeadKeys(), KbdCheckKeyNames(), KbdCheckLigatures(), KbdCheckModifiers(), KbdCheckVkToWchars(), and KbdCheckVscToVk().

◆ KbdRebaseSlot()

static BOOL KbdRebaseSlot ( _In_ PKBDVALIDATE  pv,
_Inout_ PVOID *  ppSlot,
_In_ SIZE_T  cbMin,
_In_ SIZE_T  cbAlign,
_In_ BOOL  bRequired,
_In_ PCSTR  pszName 
)
static

Converts a pointer slot of the layout tables from a virtual address (relative to ImageBase) to a pointer into our buffer, and verifies that the target has room for cbMin bytes.

Every slot can be converted only once: if the tables overlap, the slot would hold an already-converted pointer (a kernel address), which must never be treated as a virtual address again.

Parameters
[in]ppSlotThe slot. It must lie within the buffer.
[in]cbMinThe number of bytes that must be available at the target.
[in]cbAlignThe alignment of the target.
[in]bRequiredWhether a NULL pointer is rejected (otherwise, it is left as NULL).
[in]pszNameThe name.

Definition at line 547 of file kbdlayout.c.

554{
555 ULONG_PTR va, rva, idx;
556
557 if (!KbdIsBufInRange(pv, ppSlot, sizeof(PVOID), sizeof(PVOID)))
558 {
559 ERR("Invalid slot of %s: 0x%p\n", pszName, ppSlot);
560 return FALSE;
561 }
562
563 idx = ((ULONG_PTR)ppSlot - (ULONG_PTR)pv->pbImage) / sizeof(PVOID);
564 if (pv->pbSlots[idx >> 3] & (1 << (idx & 7)))
565 {
566 ERR("%s overlaps another pointer\n", pszName);
567 return FALSE;
568 }
569 pv->pbSlots[idx >> 3] |= (1 << (idx & 7));
570
571 va = (ULONG_PTR)*ppSlot;
572 if (!va)
573 {
574 if (bRequired)
575 {
576 ERR("%s is NULL\n", pszName);
577 return FALSE;
578 }
579 return TRUE;
580 }
581
582 rva = va - pv->ImageBase;
583 if (va < pv->ImageBase || rva < pv->offData || (rva - pv->offData) >= pv->cbData)
584 {
585 ERR("Invalid %s: 0x%p\n", pszName, (PVOID)va);
586 return FALSE;
587 }
588
589 *ppSlot = pv->pbImage + (rva - pv->offData);
590 if (!KbdIsBufInRange(pv, *ppSlot, cbMin, cbAlign))
591 {
592 ERR("%s: No room for %Iu bytes at 0x%p\n", pszName, cbMin, (PVOID)va);
593 *ppSlot = NULL;
594 return FALSE;
595 }
596 return TRUE;
597}
unsigned int idx
Definition: utils.c:40

Referenced by KbdCheckDeadKeyNames(), KbdCheckKeyNames(), and KbdCheckVscToVk().

◆ KbdRebaseTables()

static BOOL KbdRebaseTables ( _In_ PBYTE  pbImage,
_In_ const KBDIMAGE_INFO *  pInfo,
_Inout_ PKBDTABLES  pTbl,
_Inout_opt_ PKBDNLSTABLES  pNls 
)
static

Adjusts the pointers inside the layout tables, and validates the whole layout.

In the file, these pointers are virtual addresses relative to ImageBase, so the RVA of the target is (VA - ImageBase), and the adjusted pointer is the offset (RVA - offData) applied to our buffer, which holds the .data section only. The tables are modified in place (in our own copy).

The layout comes from user mode, so nothing in it is trusted:

  • The pointers that the keyboard code uses unconditionally must not be NULL.
  • Every counted table, and every table that ends with a terminator, must lie in the .data section in full: each element with its stride (including the terminator element), and the string with its terminating NUL.
  • No pointer slot can be taken as a virtual address twice (overlapping tables).
Returns
Returns TRUE if the layout is valid, or FALSE if anything is invalid. The layout must not be used (or installed) unless this returns TRUE.

Definition at line 1037 of file kbdlayout.c.

1042{
1043 KBDVALIDATE v;
1044 BOOL bLigature, bDead, ret = FALSE;
1045
1046 v.pbImage = pbImage;
1047 v.cbData = pInfo->cbData;
1048 v.ImageBase = pInfo->ImageBase;
1049 v.offData = pInfo->offData;
1050 v.pbSlots = ExAllocatePoolZero(PagedPool, v.cbData / (sizeof(PVOID) * 8) + 1, KBDIMG_TAG);
1051 if (!v.pbSlots)
1052 return FALSE;
1053
1054 /* The root tables themselves */
1055 if (!KbdIsBufInRange(&v, pTbl, sizeof(*pTbl), TYPE_ALIGNMENT(KBDTABLES)) ||
1056 (pNls && !KbdIsBufInRange(&v, pNls, sizeof(*pNls), TYPE_ALIGNMENT(KBDNLSTABLES))))
1057 {
1058 ERR("Misplaced root tables\n");
1059 goto Quit;
1060 }
1061
1062 /* pusVSCtoVK is counted by bMaxVSCtoVK */
1063 if (pTbl->bMaxVSCtoVK == 0)
1064 {
1065 ERR("bMaxVSCtoVK is zero\n");
1066 goto Quit;
1067 }
1068
1069 if (!KbdCheckModifiers(&v, pTbl) ||
1070 !KbdCheckVkToWchars(&v, pTbl, &bLigature, &bDead) ||
1071 !KbdCheckKeyNames(&v, &pTbl->pKeyNames, "pKeyNames") ||
1072 !KbdCheckKeyNames(&v, &pTbl->pKeyNamesExt, "pKeyNamesExt") ||
1073 !KbdCheckDeadKeyNames(&v, pTbl) ||
1074 !KbdCheckDeadKeys(&v, pTbl) ||
1075 !KBD_REBASE(&v, pTbl->pusVSCtoVK, pTbl->bMaxVSCtoVK * sizeof(USHORT),
1077 !KbdCheckVscToVk(&v, &pTbl->pVSCtoVK_E0, "pVSCtoVK_E0") ||
1078 !KbdCheckVscToVk(&v, &pTbl->pVSCtoVK_E1, "pVSCtoVK_E1") ||
1079 !KbdCheckLigatures(&v, pTbl))
1080 {
1081 goto Quit;
1082 }
1083
1084 /* WCH_LGTR and WCH_DEAD refer to these tables */
1085 if ((bLigature && !pTbl->pLigature) || (bDead && !pTbl->pDeadKey))
1086 {
1087 ERR("The layout uses WCH_LGTR/WCH_DEAD without the table\n");
1088 goto Quit;
1089 }
1090
1091 if (pNls && !KbdCheckNls(&v, pNls))
1092 goto Quit;
1093
1094 ret = TRUE;
1095
1096Quit:
1097 ExFreePoolWithTag(v.pbSlots, KBDIMG_TAG);
1098 return ret;
1099}
const GLdouble * v
Definition: gl.h:2040
static BOOL KbdCheckKeyNames(_In_ PKBDVALIDATE pv, _Inout_ VSC_LPWSTR **ppNames, _In_ PCSTR pszName)
Verifies and rebases the key name. This table ends with an entry that has vsc == 0.
Definition: kbdlayout.c:812
static BOOL KbdCheckDeadKeys(_In_ PKBDVALIDATE pv, _Inout_ PKBDTABLES pTbl)
Verifies and rebases the dead keys (optional). This table ends with an entry that has dwBoth == 0.
Definition: kbdlayout.c:914
static BOOL KbdCheckNls(_In_ PKBDVALIDATE pv, _Inout_ PKBDNLSTABLES pNls)
Verifies and rebases the NLS (national language support; for Far-East) tables. These tables are count...
Definition: kbdlayout.c:970
static BOOL KbdCheckVkToWchars(_In_ PKBDVALIDATE pv, _Inout_ PKBDTABLES pTbl, _Out_ PBOOL pbLigature, _Out_ PBOOL pbDead)
Verifies and rebases the VK_TO_WCHAR_TABLE structure in the layout data.
Definition: kbdlayout.c:735
static BOOL KbdCheckDeadKeyNames(_In_ PKBDVALIDATE pv, _Inout_ PKBDTABLES pTbl)
Verifies and rebases the dead key names (optional). This array of strings ends with NULL.
Definition: kbdlayout.c:850
static BOOL KbdCheckLigatures(_In_ PKBDVALIDATE pv, _Inout_ PKBDTABLES pTbl)
Verifies and rebases the ligatures (optional). This table ends with an entry that has VirtualKey == 0...
Definition: kbdlayout.c:938
static BOOL KbdCheckVscToVk(_In_ PKBDVALIDATE pv, _Inout_ PVSC_VK *ppVscVk, _In_ PCSTR pszName)
Verifies and rebases the VSC -> VK table for the E0/E1 prefixes. They end with an entry that has Vsc ...
Definition: kbdlayout.c:892
static BOOL KbdCheckModifiers(_In_ PKBDVALIDATE pv, _Inout_ PKBDTABLES pTbl)
Verifies and rebases the modifiers in the main tables of the layout data.
Definition: kbdlayout.c:687

Referenced by UserLoadKbdDll().

◆ KbdUnloadImage()

static __inline VOID KbdUnloadImage ( _In_opt_ PVOID  pvImageBase)
static

Definition at line 469 of file kbdlayout.c.

471{
472 if (pvImageBase)
473 ExFreePoolWithTag(pvImageBase, KBDIMG_TAG);
474}

Referenced by UnloadKbdFile(), and UserLoadKbdDll().

◆ NtUserActivateKeyboardLayout()

HKL NTAPI NtUserActivateKeyboardLayout ( HKL  hKL,
ULONG  Flags 
)

Definition at line 2230 of file kbdlayout.c.

2233{
2234 PWINSTATION_OBJECT pWinSta;
2235 HKL hOldKL;
2236
2238
2239 /* FIXME */
2240
2242 hOldKL = co_IntActivateKeyboardLayout(pWinSta, hKL, Flags, NULL);
2243 UserLeave();
2244
2245 return hOldKL;
2246}
PWINSTATION_OBJECT FASTCALL IntGetProcessWindowStation(HWINSTA *phWinSta OPTIONAL)
Definition: winsta.c:422
VOID FASTCALL UserLeave(VOID)
Definition: ntuser.c:255
VOID FASTCALL UserEnterExclusive(VOID)
Definition: ntuser.c:247
HKL APIENTRY co_IntActivateKeyboardLayout(_Inout_ PWINSTATION_OBJECT pWinSta, _In_ HKL hKL, _In_ ULONG uFlags, _In_opt_ PWND pWnd)
Definition: kbdlayout.c:1640

Referenced by LoadKeyboardLayoutW(), and User32DefWindowProc().

◆ NtUserGetKeyboardLayoutList()

UINT APIENTRY NtUserGetKeyboardLayoutList ( ULONG  nBuff,
HKL *  pHklBuff 
)

Definition at line 2010 of file kbdlayout.c.

2013{
2014 UINT ret = 0;
2015 PWINSTATION_OBJECT pWinSta;
2016
2017 if (!pHklBuff)
2018 nBuff = 0;
2019
2021
2022 if (nBuff > MAXULONG / sizeof(HKL))
2023 {
2025 goto Quit;
2026 }
2027
2028 _SEH2_TRY
2029 {
2030 ProbeForWrite(pHklBuff, nBuff * sizeof(HKL), 1);
2031 }
2033 {
2035 _SEH2_YIELD(goto Quit);
2036 }
2037 _SEH2_END;
2038
2040
2041 _SEH2_TRY
2042 {
2043 ret = IntGetKeyboardLayoutList(pWinSta, nBuff, pHklBuff);
2044 }
2046 {
2048 _SEH2_YIELD(goto Quit);
2049 }
2050 _SEH2_END;
2051
2052Quit:
2053 UserLeave();
2054 return ret;
2055}
#define ERROR_INVALID_PARAMETER
Definition: compat.h:101
VOID NTAPI ProbeForWrite(IN PVOID Address, IN SIZE_T Length, IN ULONG Alignment)
Definition: exintrin.c:143
VOID FASTCALL UserEnterShared(VOID)
Definition: ntuser.c:241
#define MAXULONG
Definition: typedefs.h:251
VOID FASTCALL SetLastNtError(_In_ NTSTATUS Status)
Definition: error.c:30
static UINT APIENTRY IntGetKeyboardLayoutList(_Inout_ PWINSTATION_OBJECT pWinSta, _In_ ULONG nBuff, _Out_ HKL *pHklBuff)
Definition: kbdlayout.c:94
ENGAPI VOID APIENTRY EngSetLastError(_In_ ULONG iError)
Definition: error.c:21

Referenced by CliImmInitializeHotKeys().

◆ NtUserGetKeyboardLayoutName()

BOOL APIENTRY NtUserGetKeyboardLayoutName ( _Inout_ PUNICODE_STRING  pustrName)

Definition at line 2064 of file kbdlayout.c.

2066{
2067 BOOL bRet = FALSE;
2068 PKL pKl;
2069 PTHREADINFO pti;
2070 UNICODE_STRING ustrNameSafe;
2072
2074
2076 pKl = pti->KeyboardLayout;
2077
2078 if (!pKl)
2079 goto cleanup;
2080
2081 _SEH2_TRY
2082 {
2083 ProbeForWriteUnicodeString(pustrName);
2084 ustrNameSafe = *pustrName;
2085
2086 ProbeForWrite(ustrNameSafe.Buffer, ustrNameSafe.MaximumLength, 1);
2087
2088 if (IS_IME_HKL(pKl->hkl))
2089 {
2090 Status = RtlIntegerToUnicodeString(HandleToUlong(pKl->hkl), 16, &ustrNameSafe);
2091 }
2092 else
2093 {
2094 if (ustrNameSafe.MaximumLength < KL_NAMELENGTH * sizeof(WCHAR))
2095 {
2097 _SEH2_YIELD(goto cleanup);
2098 }
2099
2100 /* FIXME: Do not use awchKF */
2101 ustrNameSafe.Length = 0;
2102 Status = RtlAppendUnicodeToString(&ustrNameSafe, pKl->spkf->awchKF);
2103 }
2104
2105 if (NT_SUCCESS(Status))
2106 {
2107 *pustrName = ustrNameSafe;
2108 bRet = TRUE;
2109 }
2110 }
2112 {
2114 }
2115 _SEH2_END;
2116
2117cleanup:
2118 UserLeave();
2119 return bRet;
2120}
#define HandleToUlong(h)
Definition: basetsd.h:73
static void cleanup(void)
Definition: main.c:1335
NTSTATUS RtlAppendUnicodeToString(IN PUNICODE_STRING Str1, IN PWSTR Str2)
Definition: string_lib.cpp:62
NTSYSAPI NTSTATUS NTAPI RtlIntegerToUnicodeString(ULONG Value, ULONG Base, PUNICODE_STRING String)
#define KL_NAMELENGTH
Definition: winuser.h:122
#define ProbeForWriteUnicodeString(Ptr)
Definition: probe.h:48
USHORT MaximumLength
Definition: env_spec_w32.h:370
WCHAR awchKF[20]
Definition: input.h:10

Referenced by GetKeyboardLayoutNameA(), GetKeyboardLayoutNameW(), and START_TEST().

◆ NtUserLoadKeyboardLayoutEx()

HKL NTAPI NtUserLoadKeyboardLayoutEx ( _In_ HANDLE  hFile,
_In_ ULONG  offTable,
_In_opt_ PKBDTABLE_MULTI_EX  pMulti,
_In_opt_ HKL  hOldKL,
_In_ PUNICODE_STRING  puszKLID,
_In_ ULONG  dwNewKL,
_In_ UINT  Flags 
)

Definition at line 2129 of file kbdlayout.c.

2137{
2138 HKL hRetKL = NULL;
2140 UNICODE_STRING uszSafeKLID;
2141 PWINSTATION_OBJECT pWinSta;
2142 HANDLE hKernelFile = NULL; /* Local kernel handle duplicated from hFile */
2143 HANDLE ahKernelAuxFile[KBDTABLE_MULTI_MAX] = { NULL }; /* Ditto, for the aux files */
2144 KBDTABLE_MULTI_EX multi;
2145 ULONG iTable;
2146
2149 KLF_RESET|KLF_SHIFTLOCK))
2150 {
2151 ERR("Invalid flags: %x\n", Flags);
2153 return NULL;
2154 }
2155
2156 RtlInitEmptyUnicodeString(&uszSafeKLID, Buffer, sizeof(Buffer));
2157 RtlZeroMemory(&multi, sizeof(multi));
2158
2159 /* Read from paramters */
2160 _SEH2_TRY
2161 {
2162 ProbeForRead(puszKLID, sizeof(*puszKLID), 1);
2163 ProbeForRead(puszKLID->Buffer, sizeof(puszKLID->Length), 1); /* FIXME */
2164 RtlCopyUnicodeString(&uszSafeKLID, puszKLID);
2165 if (pMulti)
2166 {
2167 ProbeForRead(pMulti, sizeof(*pMulti), 1);
2168 multi = *pMulti;
2169 }
2170 }
2172 {
2174 _SEH2_YIELD(return NULL);
2175 }
2176 _SEH2_END;
2177
2179
2180 /* Validate data */
2181 if (!hFile || !offTable || multi.Multi.nTables > KBDTABLE_MULTI_MAX)
2182 {
2183 ERR("hFile:%p, nTables:%u, offTable:0x%08X\n", hFile, multi.Multi.nTables, offTable);
2185 goto Quit;
2186 }
2187
2188 /* Verify file handles. The handles passed in belong to user32, which closes them itself.
2189 * We only work with (and close) our own kernel handles made by the verification. */
2190 hKernelFile = IntVerifyKeyboardFileHandle(hFile);
2191 if (!hKernelFile)
2192 goto Quit;
2193 for (iTable = 0; iTable < multi.Multi.nTables; ++iTable) /* nTables < MAX is checked above */
2194 {
2195 HANDLE hAuxFile = multi.AuxEntries[iTable].hFile;
2196 /* user32 tolerates a failed aux file by storing NULL */
2197 if (!hAuxFile)
2198 continue;
2199
2200 ahKernelAuxFile[iTable] = IntVerifyKeyboardFileHandle(hAuxFile);
2201 if (!ahKernelAuxFile[iTable])
2202 goto Quit;
2203 }
2204
2205 /* Load it now */
2207 hRetKL = co_IntLoadKeyboardLayoutEx(pWinSta, hKernelFile, hOldKL, offTable, &uszSafeKLID,
2208 UlongToHandle(dwNewKL), Flags);
2209Quit:
2210 /* Close our kernel handles. Never use the (unvalidated) user-supplied nTables here. */
2211 if (hKernelFile)
2212 ZwClose(hKernelFile);
2213 for (iTable = 0; iTable < RTL_NUMBER_OF(ahKernelAuxFile); ++iTable)
2214 {
2215 if (ahKernelAuxFile[iTable])
2216 ZwClose(ahKernelAuxFile[iTable]);
2217 }
2218
2219 UserLeave();
2220 return hRetKL;
2221}
RtlInitEmptyUnicodeString(SymbolicLinkName, NULL, 0)
Definition: bufpool.h:45
VOID NTAPI ProbeForRead(IN CONST VOID *Address, IN SIZE_T Length, IN ULONG Alignment)
Definition: exintrin.c:102
#define KBDTABLE_MULTI_MAX
Definition: kbd.h:199
NTSYSAPI VOID NTAPI RtlCopyUnicodeString(PUNICODE_STRING DestinationString, PUNICODE_STRING SourceString)
#define KLF_REPLACELANG
Definition: winuser.h:115
#define KLF_SUBSTITUTE_OK
Definition: winuser.h:112
#define KLF_UNLOADPREVIOUS
Definition: winuser.h:113
HANDLE hFile
Definition: ntuser.h:114
KBDTABLE_MULTI_AUX_ENTRY AuxEntries[KBDTABLE_MULTI_MAX]
Definition: ntuser.h:124
KBDTABLE_MULTI Multi
Definition: ntuser.h:122
UINT nTables
Definition: kbd.h:203
HKL APIENTRY co_IntLoadKeyboardLayoutEx(_Inout_ PWINSTATION_OBJECT pWinSta, _In_ HANDLE hKernelFile, _In_opt_ HKL hOldKL, _In_ ULONG offTable, _In_ PUNICODE_STRING puszSafeKLID, _In_ HKL hNewKL, _In_ UINT Flags)
Definition: kbdlayout.c:1743
HANDLE FASTCALL IntVerifyKeyboardFileHandle(HANDLE hFile)
Validates that the given user-mode handle corresponds to an opened file in SystemRoot\system32.
Definition: kbdlayout.c:1890
#define ERROR_INVALID_FLAGS
Definition: winerror.h:907

Referenced by IntLoadKeyboardLayout().

◆ NtUserUnloadKeyboardLayout()

BOOL APIENTRY NtUserUnloadKeyboardLayout ( HKL  hKl)

Definition at line 2255 of file kbdlayout.c.

2257{
2258 BOOL ret;
2259 PWINSTATION_OBJECT pWinSta;
2260
2262
2264 ret = IntUnloadKeyboardLayout(pWinSta, hKl);
2265
2266 UserLeave();
2267 return ret;
2268}
static BOOL APIENTRY IntUnloadKeyboardLayout(_Inout_ PWINSTATION_OBJECT pWinSta, _In_ HKL hKL)
Definition: kbdlayout.c:1710

Referenced by UnloadKeyboardLayout().

◆ UnloadKbdFile()

static VOID UnloadKbdFile ( _In_ PKBDFILE  pkf)
static

Definition at line 1301 of file kbdlayout.c.

1302{
1303 PKBDFILE *ppkfLink = &gpkfList;
1304 NT_ASSERT(pkf != NULL);
1305
1306 /* Find previous object */
1307 while (*ppkfLink)
1308 {
1309 if (*ppkfLink == pkf)
1310 break;
1311
1312 ppkfLink = &(*ppkfLink)->pkfNext;
1313 }
1314
1315 if (*ppkfLink == pkf)
1316 *ppkfLink = pkf->pkfNext;
1317
1318 KbdUnloadImage(pkf->hBase);
1320}
@ TYPE_KBDFILE
Definition: ntuser.h:59
struct tagKBDFILE * pkfNext
Definition: input.h:9
PKBDFILE gpkfList
Definition: kbdlayout.c:23
static __inline VOID KbdUnloadImage(_In_opt_ PVOID pvImageBase)
Definition: kbdlayout.c:469
#define NT_ASSERT
Definition: rtlfuncs.h:3334

Referenced by UserUnloadKbl().

◆ UserGetKeyboardLayout()

HKL FASTCALL UserGetKeyboardLayout ( DWORD  dwThreadId)

Definition at line 1967 of file kbdlayout.c.

1969{
1970 PTHREADINFO pti;
1971 PLIST_ENTRY ListEntry;
1972 PKL pKl;
1973
1975
1976 if (!dwThreadId)
1977 {
1978 pKl = pti->KeyboardLayout;
1979 return pKl ? pKl->hkl : NULL;
1980 }
1981
1982 ListEntry = pti->rpdesk->PtiList.Flink;
1983
1984 //
1985 // Search the Desktop Thread list for related Desktop active Threads.
1986 //
1987 while(ListEntry != &pti->rpdesk->PtiList)
1988 {
1989 pti = CONTAINING_RECORD(ListEntry, THREADINFO, PtiLink);
1990
1991 if (PsGetThreadId(pti->pEThread) == UlongToHandle(dwThreadId))
1992 {
1993 pKl = pti->KeyboardLayout;
1994 return pKl ? pKl->hkl : NULL;
1995 }
1996
1997 ListEntry = ListEntry->Flink;
1998 }
1999
2000 return NULL;
2001}
DWORD dwThreadId
Definition: fdebug.c:31
HANDLE NTAPI PsGetThreadId(IN PETHREAD Thread)
Definition: thread.c:729
Definition: typedefs.h:120
struct _LIST_ENTRY * Flink
Definition: typedefs.h:121
struct _DESKTOP * rpdesk
Definition: win32.h:93
#define CONTAINING_RECORD(address, type, field)
Definition: typedefs.h:260

Referenced by IntGetActiveKeyboardLayout(), and NtUserCallOneParam().

◆ UserHklToKbl()

PKL NTAPI UserHklToKbl ( HKL  hKl)

Definition at line 1396 of file kbdlayout.c.

1397{
1398 PKL pKl = gspklBaseLayout;
1399
1400 if (!gspklBaseLayout)
1401 return NULL;
1402
1403 do
1404 {
1405 if (pKl->hkl == hKl)
1406 return pKl;
1407
1408 pKl = pKl->pklNext;
1409 } while (pKl != gspklBaseLayout);
1410
1411 return NULL;
1412}

Referenced by co_IntLoadKeyboardLayoutEx(), NtUserMapVirtualKeyEx(), NtUserSetThreadLayoutHandles(), NtUserToUnicodeEx(), NtUserVkKeyScanEx(), and UserSetDefaultInputLang().

◆ UserLoadKbdDll()

static BOOL UserLoadKbdDll ( _In_ HANDLE  hKernelFile,
_In_ ULONG  offTable,
_Out_ HANDLE *  phModule,
_Out_ PULONG  pcbImage,
_Out_ PKBDTABLES *  ppKbdTables,
_Out_ PKBDNLSTABLES *  ppKbdNlsTables 
)
static

Definition at line 1103 of file kbdlayout.c.

1110{
1111 PBYTE pbImage;
1112 ULONG cbImage, wMainOffset = LOWORD(offTable), wNlsOffset = HIWORD(offTable);
1115 PKBDTABLES pKbdTables;
1116 PKBDNLSTABLES pKbdNlsTables;
1117
1118 *phModule = NULL;
1119 *ppKbdTables = NULL;
1120 *ppKbdNlsTables = NULL;
1121
1122 if (!wMainOffset)
1123 return FALSE;
1124
1125 /* Load keyboard layout DLL */
1126 hModule = KbdLoadImage(hKernelFile, &cbImage, &info);
1127 if (!hModule)
1128 return FALSE;
1129
1130 /* offTable comes from user mode: the tables must lie entirely within the .data section */
1131 if (!KbdIsRangeInData(&info, wMainOffset, sizeof(**ppKbdTables)) ||
1132 (wNlsOffset && !KbdIsRangeInData(&info, wNlsOffset, sizeof(**ppKbdNlsTables))))
1133 {
1134 ERR("Table offset out of range: offTable 0x%08X, .data 0x%08lX+0x%08lX\n",
1135 offTable, info.offData, info.cbData);
1137 return FALSE;
1138 }
1139
1140 /* The buffer holds the .data section only: convert the RVAs to the offsets in the buffer */
1141 pbImage = (PBYTE)hModule;
1142 pKbdTables = (PKBDTABLES)RVA(wMainOffset - info.offData, pbImage);
1143 pKbdNlsTables = wNlsOffset ? (PKBDNLSTABLES)RVA(wNlsOffset - info.offData, pbImage) : NULL;
1144
1145 _SEH2_TRY
1146 {
1147 /* Adjust the pointers inside the tables using RVA (instead of the base relocations),
1148 and validate their targets */
1149 if (!KbdRebaseTables(pbImage, &info, pKbdTables, pKbdNlsTables))
1150 {
1151 ERR("Invalid pointer in the layout tables\n");
1153 _SEH2_YIELD(return FALSE);
1154 }
1155 }
1157 {
1158 ERR("Exception in UserLoadKbdDll! Status: 0x%lx\n", _SEH2_GetExceptionCode());
1160 _SEH2_YIELD(return FALSE);
1161 }
1162 _SEH2_END;
1163
1164 *phModule = hModule;
1165 *pcbImage = cbImage;
1166 *ppKbdTables = pKbdTables;
1167 *ppKbdNlsTables = pKbdNlsTables;
1168
1169#if 0 && DBG
1170 /* Dump keyboard layout */
1171 DumpKbdLayout(*ppKbdTables);
1172#endif
1173
1174 return TRUE;
1175}
HMODULE hModule
Definition: animate.c:44
struct _KBDTABLES * PKBDTABLES
struct tagKbdNlsLayer * PKBDNLSTABLES
#define RVA(m, b)
Definition: kbdlayout.c:243
static PVOID KbdLoadImage(_In_ HANDLE hKernelFile, _Out_ PULONG pcbImage, _Out_ PKBDIMAGE_INFO pInfo)
Definition: kbdlayout.c:404
static BOOL KbdRebaseTables(_In_ PBYTE pbImage, _In_ const KBDIMAGE_INFO *pInfo, _Inout_ PKBDTABLES pTbl, _Inout_opt_ PKBDNLSTABLES pNls)
Adjusts the pointers inside the layout tables, and validates the whole layout.
Definition: kbdlayout.c:1037
static __inline BOOL KbdIsRangeInData(_In_ const KBDIMAGE_INFO *pInfo, _In_ ULONG_PTR off, _In_ SIZE_T cb)
Definition: kbdlayout.c:479

Referenced by UserLoadKbdFile().

◆ UserLoadKbdFile()

static PKBDFILE UserLoadKbdFile ( _In_ HANDLE  hKernelFile,
_In_ ULONG  offTable,
_In_ PCUNICODE_STRING  pustrKLID 
)
static

Definition at line 1179 of file kbdlayout.c.

1183{
1184 PKBDFILE pkf, pRet = NULL;
1185
1186 /* Create keyboard layout file object */
1188 if (!pkf)
1189 {
1190 ERR("Failed to create object!\n");
1191 return NULL;
1192 }
1193
1194 _swprintf(pkf->awchKF, L"%wZ", pustrKLID); /* Set keyboard layout name */
1195
1196 /* Load keyboard file now */
1197 if (!UserLoadKbdDll(hKernelFile, offTable, &pkf->hBase, &pkf->Size,
1198 &pkf->pKbdTbl, &pkf->pKbdNlsTbl))
1199 {
1200 ERR("Failed to load layout file!\n");
1201 goto cleanup;
1202 }
1203
1204 /* Update next field */
1205 pkf->pkfNext = gpkfList;
1206 gpkfList = pkf;
1207
1208 /* Return keyboard file */
1209 pRet = pkf;
1210
1211cleanup:
1212 if (pkf)
1213 UserDereferenceObject(pkf); // we dont need ptr anymore
1214 if (!pRet && pkf) /* If we have failed, destroy the created object */
1216 return pRet;
1217}
#define _swprintf(buf, format,...)
Definition: sprintf.c:56
PKBDTABLES pKbdTbl
Definition: input.h:12
PKBDNLSTABLES pKbdNlsTbl
Definition: input.h:14
ULONG Size
Definition: input.h:13
HANDLE hBase
Definition: input.h:11
static BOOL UserLoadKbdDll(_In_ HANDLE hKernelFile, _In_ ULONG offTable, _Out_ HANDLE *phModule, _Out_ PULONG pcbImage, _Out_ PKBDTABLES *ppKbdTables, _Out_ PKBDNLSTABLES *ppKbdNlsTables)
Definition: kbdlayout.c:1103

Referenced by co_UserLoadKbdLayout().

◆ UserSetDefaultInputLang()

BOOL NTAPI UserSetDefaultInputLang ( HKL  hKl)

Definition at line 1440 of file kbdlayout.c.

1441{
1442 PKL pKl;
1443
1444 pKl = UserHklToKbl(hKl);
1445 if (!pKl)
1446 return FALSE;
1447
1449 return TRUE;
1450}

Referenced by SpiGetSet().

◆ UserUnloadKbl()

BOOL UserUnloadKbl ( PKL  pKl)

Definition at line 1328 of file kbdlayout.c.

1329{
1330 /* According to msdn, UnloadKeyboardLayout can fail
1331 if the keyboard layout identifier was preloaded. */
1332 if (pKl == gspklBaseLayout)
1333 {
1334 if (pKl->pklNext == pKl)
1335 {
1336 /* There is only one layout */
1337 return FALSE;
1338 }
1339
1340 /* Set next layout as default */
1341 gspklBaseLayout = pKl->pklNext;
1342 }
1343
1344 if (pKl->head.cLockObj > 1)
1345 {
1346 /* Layout is used by other threads */
1347 pKl->dwKL_Flags |= KL_UNLOAD;
1348 return FALSE;
1349 }
1350
1351 /* Unload the layout */
1352 pKl->pklPrev->pklNext = pKl->pklNext;
1353 pKl->pklNext->pklPrev = pKl->pklPrev;
1354 UnloadKbdFile(pKl->spkf);
1355 if (pKl->piiex)
1356 {
1358 }
1360 return TRUE;
1361}
DWORD cLockObj
Definition: ntuser.h:209
HEAD head
Definition: input.h:19
static VOID UnloadKbdFile(_In_ PKBDFILE pkf)
Definition: kbdlayout.c:1301

Referenced by co_IntLoadKeyboardLayoutEx().

◆ W32kGetDefaultKeyLayout()

PKL W32kGetDefaultKeyLayout ( VOID  )

Definition at line 1369 of file kbdlayout.c.

1370{
1371 PKL pKl = gspklBaseLayout;
1372
1373 if (!pKl)
1374 return NULL;
1375
1376 /* Return not unloaded layout */
1377 do
1378 {
1379 if (!(pKl->dwKL_Flags & KL_UNLOAD))
1380 return pKl;
1381
1382 pKl = pKl->pklPrev; /* Confirmed on Win2k */
1383 } while(pKl != gspklBaseLayout);
1384
1385 /* We have not found proper KL */
1386 return NULL;
1387}

Referenced by InitThreadCallback(), IntTranslateKbdMessage(), UserProcessKeyboardInput(), and UserSendKeyboardInput().

Variable Documentation

◆ ghKLSentToShell

HKL ghKLSentToShell = NULL

Definition at line 26 of file kbdlayout.c.

Referenced by co_IntUnloadKeyboardLayoutEx(), and co_UserActivateKeyboardLayout().

◆ gpkfList

PKBDFILE gpkfList = NULL

Definition at line 23 of file kbdlayout.c.

Referenced by UnloadKbdFile(), and UserLoadKbdFile().

◆ gspklBaseLayout

◆ gSystemCPCharSet

UINT gSystemCPCharSet = 0

Definition at line 25 of file kbdlayout.c.

Referenced by co_UserLoadKbdLayout().

◆ gSystemFS

DWORD gSystemFS = 0

Definition at line 24 of file kbdlayout.c.

Referenced by co_UserLoadKbdLayout(), IntImmProcessKey(), and IntLanguageToggle().