From 42a9ed01318707894a719ef5858a675ccb0be4fb Mon Sep 17 00:00:00 2001 From: A2va <49582555+A2va@users.noreply.github.com> Date: Sat, 8 Feb 2025 17:08:08 +0100 Subject: [PATCH] Adds some NT functions (#1358) --- libc/isystem/windowsesque.h | 17 +++++++++++ libc/nt/advapi32/RegOpenKeyExA.S | 18 +++++++++++ libc/nt/enum/keyaccess.h | 10 +++++++ libc/nt/files.h | 2 ++ libc/nt/kernel32/GetACP.S | 19 ++++++++++++ libc/nt/kernel32/GetCPInfoExW.S | 18 +++++++++++ libc/nt/kernel32/GetLogicalDriveStringsA.S | 18 +++++++++++ libc/nt/kernel32/GetOEMCP.S | 19 ++++++++++++ libc/nt/kernel32/GetShortPathNameW.S | 18 +++++++++++ libc/nt/master.sh | 6 ++++ libc/nt/nls.h | 35 ++++++++++++++++++++++ libc/nt/registry.h | 2 ++ libc/nt/struct/cpinfoex.h | 13 ++++++++ 13 files changed, 195 insertions(+) create mode 100644 libc/nt/advapi32/RegOpenKeyExA.S create mode 100644 libc/nt/kernel32/GetACP.S create mode 100644 libc/nt/kernel32/GetCPInfoExW.S create mode 100644 libc/nt/kernel32/GetLogicalDriveStringsA.S create mode 100644 libc/nt/kernel32/GetOEMCP.S create mode 100644 libc/nt/kernel32/GetShortPathNameW.S create mode 100644 libc/nt/nls.h create mode 100644 libc/nt/struct/cpinfoex.h diff --git a/libc/isystem/windowsesque.h b/libc/isystem/windowsesque.h index f228173def1..4b27c516c59 100644 --- a/libc/isystem/windowsesque.h +++ b/libc/isystem/windowsesque.h @@ -12,6 +12,7 @@ #include "libc/nt/files.h" #include "libc/nt/ipc.h" #include "libc/nt/memory.h" +#include "libc/nt/nls.h" #include "libc/nt/paint.h" #include "libc/nt/process.h" #include "libc/nt/registry.h" @@ -1420,6 +1421,15 @@ #define HKEY_CURRENT_CONFIG kNtHkeyCurrentConfig #define HKEY_DYN_DATA kNtHkeyDynData #define HKEY_CURRENT_USER_LOCAL_SETTINGS kNtHkeyCurrentUserLocalSettings +#define KEY_QUERY_VALUE kNtKeyQueryValue +#define KEY_SET_VALUE kNtKeySetValue +#define KEY_CREATE_SUB_KEY kNtKeyCreateSubKey +#define KEY_ENUMERATE_SUB_KEYS kNtKeyEnumerateSubKeys +#define KEY_NOTIFY kNtKeyNotify +#define KEY_CREATE_LINK kNtKeyCreateLink +#define KEY_WOW64_32KEY kNtWow6432Key +#define KEY_WOW64_64KEY kNtWow6464Key +#define KEY_WOW64_RES kNtWow64Res #define KEY_READ kNtKeyRead #define KEY_WRITE kNtKeyWrite #define KEY_EXECUTE kNtKeyExecute @@ -4291,6 +4301,13 @@ #define MAKE_HRESULT(sev,fac,code) ((HRESULT) (((unsigned long)(sev)<<31) | ((unsigned long)(fac)<<16) | ((unsigned long)(code))) ) #define MAKE_SCODE(sev,fac,code) ((SCODE) (((unsigned long)(sev)<<31) | ((unsigned long)(fac)<<16) | ((unsigned long)(code))) ) +#define CP_ACP 0 +#define CP_OEMCP 1 +#define CP_MACCP 2 +#define CP_THREAD_ACP 3 +#define CP_SYMBOL 42 + +#define CP_UTF7 65000 #define CP_UTF8 65001 #endif /* COSMOPOLITAN_LIBC_COMPAT_INCLUDE_WINDOWS_H_ */ diff --git a/libc/nt/advapi32/RegOpenKeyExA.S b/libc/nt/advapi32/RegOpenKeyExA.S new file mode 100644 index 00000000000..31ee268483b --- /dev/null +++ b/libc/nt/advapi32/RegOpenKeyExA.S @@ -0,0 +1,18 @@ +#include "libc/nt/codegen.h" +.imp advapi32,__imp_RegOpenKeyExA,RegOpenKeyExA + + .text.windows + .ftrace1 +RegOpenKeyExA: + .ftrace2 +#ifdef __x86_64__ + push %rbp + mov %rsp,%rbp + mov __imp_RegOpenKeyExA(%rip),%rax + jmp __sysv2nt6 +#elif defined(__aarch64__) + mov x0,#0 + ret +#endif + .endfn RegOpenKeyExA,globl + .previous diff --git a/libc/nt/enum/keyaccess.h b/libc/nt/enum/keyaccess.h index 06709ad4251..1abb200a4b6 100644 --- a/libc/nt/enum/keyaccess.h +++ b/libc/nt/enum/keyaccess.h @@ -1,6 +1,16 @@ #ifndef COSMOPOLITAN_LIBC_NT_ENUM_KEYACCESS_H_ #define COSMOPOLITAN_LIBC_NT_ENUM_KEYACCESS_H_ +#define kNtKeyQueryValue 0x00000001 +#define kNtKeySetValue 0x00000002 +#define kNtKeyCreateSubKey 0x00000004 +#define kNtKeyEnumerateSubKeys 0x00000008 +#define kNtKeyNotify 0x00000010 +#define kNtKeyCreateLink 0x00000020 +#define kNtWow6432Key 0x00000200 +#define kNtWow6464Key 0x00000100 +#define kNtWow64Res 0x00000300 + #define kNtKeyRead 0x00020019 #define kNtKeyWrite 0x00020006 #define kNtKeyExecute 0x00020019 diff --git a/libc/nt/files.h b/libc/nt/files.h index 6959a0d1334..2b844c32ff7 100644 --- a/libc/nt/files.h +++ b/libc/nt/files.h @@ -49,6 +49,7 @@ COSMOPOLITAN_C_START_ intptr_t LoadResource(int64_t hModule, int64_t hResInfo); uint32_t SetHandleCount(uint32_t uNumber); uint32_t GetLogicalDrives(void); +uint32_t GetLogicalDriveStringsA(uint32_t nBufferLength, char *lpBuffer); bool32 FlushFileBuffers(int64_t hFile); int64_t ReOpenFile(int64_t hOriginalFile, uint32_t dwDesiredAccess, @@ -205,6 +206,7 @@ uint32_t GetFinalPathNameByHandle(int64_t hFile, char16_t *out_path, uint32_t GetFullPathName(const char16_t *lpFileName, uint32_t nBufferLength, char16_t *lpBuffer, char16_t **lpFilePart); +uint32_t GetShortPathName(const char16_t *lpszLongPath, char16_t *out_lpszShortPath, uint32_t cchBuffer); bool32 GetOverlappedResult(int64_t hFile, struct NtOverlapped *lpOverlapped, uint32_t *lpNumberOfBytesTransferred, bool32 bWait); diff --git a/libc/nt/kernel32/GetACP.S b/libc/nt/kernel32/GetACP.S new file mode 100644 index 00000000000..f0121f7e0a3 --- /dev/null +++ b/libc/nt/kernel32/GetACP.S @@ -0,0 +1,19 @@ +#include "libc/nt/codegen.h" +.imp kernel32,__imp_GetACP,GetACP + + .text.windows + .ftrace1 +GetACP: + .ftrace2 +#ifdef __x86_64__ + push %rbp + mov %rsp,%rbp + sub $32,%rsp + call *__imp_GetACP(%rip) + leave +#elif defined(__aarch64__) + mov x0,#0 +#endif + ret + .endfn GetACP,globl + .previous diff --git a/libc/nt/kernel32/GetCPInfoExW.S b/libc/nt/kernel32/GetCPInfoExW.S new file mode 100644 index 00000000000..a58310911c1 --- /dev/null +++ b/libc/nt/kernel32/GetCPInfoExW.S @@ -0,0 +1,18 @@ +#include "libc/nt/codegen.h" +.imp kernel32,__imp_GetCPInfoExW,GetCPInfoExW + + .text.windows + .ftrace1 +GetCPInfoEx: + .ftrace2 +#ifdef __x86_64__ + push %rbp + mov %rsp,%rbp + mov __imp_GetCPInfoExW(%rip),%rax + jmp __sysv2nt +#elif defined(__aarch64__) + mov x0,#0 + ret +#endif + .endfn GetCPInfoEx,globl + .previous diff --git a/libc/nt/kernel32/GetLogicalDriveStringsA.S b/libc/nt/kernel32/GetLogicalDriveStringsA.S new file mode 100644 index 00000000000..de327c7fc02 --- /dev/null +++ b/libc/nt/kernel32/GetLogicalDriveStringsA.S @@ -0,0 +1,18 @@ +#include "libc/nt/codegen.h" +.imp kernel32,__imp_GetLogicalDriveStringsA,GetLogicalDriveStringsA + + .text.windows + .ftrace1 +GetLogicalDriveStringsA: + .ftrace2 +#ifdef __x86_64__ + push %rbp + mov %rsp,%rbp + mov __imp_GetLogicalDriveStringsA(%rip),%rax + jmp __sysv2nt +#elif defined(__aarch64__) + mov x0,#0 + ret +#endif + .endfn GetLogicalDriveStringsA,globl + .previous diff --git a/libc/nt/kernel32/GetOEMCP.S b/libc/nt/kernel32/GetOEMCP.S new file mode 100644 index 00000000000..18227546fca --- /dev/null +++ b/libc/nt/kernel32/GetOEMCP.S @@ -0,0 +1,19 @@ +#include "libc/nt/codegen.h" +.imp kernel32,__imp_GetOEMCP,GetOEMCP + + .text.windows + .ftrace1 +GetOEMCP: + .ftrace2 +#ifdef __x86_64__ + push %rbp + mov %rsp,%rbp + sub $32,%rsp + call *__imp_GetOEMCP(%rip) + leave +#elif defined(__aarch64__) + mov x0,#0 +#endif + ret + .endfn GetOEMCP,globl + .previous diff --git a/libc/nt/kernel32/GetShortPathNameW.S b/libc/nt/kernel32/GetShortPathNameW.S new file mode 100644 index 00000000000..d0c28f2f667 --- /dev/null +++ b/libc/nt/kernel32/GetShortPathNameW.S @@ -0,0 +1,18 @@ +#include "libc/nt/codegen.h" +.imp kernel32,__imp_GetShortPathNameW,GetShortPathNameW + + .text.windows + .ftrace1 +GetShortPathName: + .ftrace2 +#ifdef __x86_64__ + push %rbp + mov %rsp,%rbp + mov __imp_GetShortPathNameW(%rip),%rax + jmp __sysv2nt +#elif defined(__aarch64__) + mov x0,#0 + ret +#endif + .endfn GetShortPathName,globl + .previous diff --git a/libc/nt/master.sh b/libc/nt/master.sh index 9d3ae3d3b78..570a77e7238 100755 --- a/libc/nt/master.sh +++ b/libc/nt/master.sh @@ -129,10 +129,12 @@ imp 'GetFileTime' GetFileTime kernel32 4 imp 'GetFileType' GetFileType kernel32 1 imp 'GetFinalPathNameByHandle' GetFinalPathNameByHandleW kernel32 4 imp 'GetFullPathName' GetFullPathNameW kernel32 4 +imp 'GetShortPathName' GetShortPathNameW kernel32 3 imp 'GetHandleInformation' GetHandleInformation kernel32 2 imp 'GetLargestConsoleWindowSize' GetLargestConsoleWindowSize kernel32 1 imp 'GetLastError' GetLastError kernel32 0 imp 'GetLogicalDrives' GetLogicalDrives kernel32 0 +imp 'GetLogicalDriveStringsA' GetLogicalDriveStringsA kernel32 2 imp 'GetMaximumProcessorCount' GetMaximumProcessorCount kernel32 1 # Windows 7+ imp 'GetModuleFileName' GetModuleFileNameW kernel32 3 imp 'GetModuleHandle' GetModuleHandleA kernel32 1 @@ -186,6 +188,9 @@ imp 'GetVolumeInformationByHandle' GetVolumeInformationByHandleW kernel32 imp 'GetVolumePathName' GetVolumePathNameW kernel32 3 imp 'GetWindowsDirectory' GetWindowsDirectoryW kernel32 2 imp 'GetWindowsDirectoryA' GetWindowsDirectoryA kernel32 2 +imp 'GetOEMCP' GetOEMCP kernel32 0 +imp 'GetACP' GetACP kernel32 0 +imp 'GetCPInfoEx' GetCPInfoExW kernel32 3 imp 'GlobalAlloc' GlobalAlloc kernel32 2 imp 'GlobalFree' GlobalFree kernel32 1 imp 'GlobalLock' GlobalLock kernel32 1 @@ -356,6 +361,7 @@ imp 'RegLoadKey' RegLoadKeyW advapi32 3 imp 'RegNotifyChangeKeyValue' RegNotifyChangeKeyValue advapi32 5 imp 'RegOpenCurrentUser' RegOpenCurrentUser advapi32 2 imp 'RegOpenKeyEx' RegOpenKeyExW advapi32 5 +imp 'RegOpenKeyExA' RegOpenKeyExA advapi32 5 imp 'RegOpenUserClassesRoot' RegOpenUserClassesRoot advapi32 4 imp 'RegOverridePredefKey' RegOverridePredefKey advapi32 2 imp 'RegQueryInfoKey' RegQueryInfoKeyW advapi32 12 diff --git a/libc/nt/nls.h b/libc/nt/nls.h new file mode 100644 index 00000000000..4e27615199b --- /dev/null +++ b/libc/nt/nls.h @@ -0,0 +1,35 @@ +#ifndef COSMOPOLITAN_LIBC_NT_NLS_H_ +#define COSMOPOLITAN_LIBC_NT_NLS_H_ +#include "libc/nt/struct/cpinfoex.h" +/* ░░░░ + ▒▒▒░░░▒▒▒▒▒▒▒▓▓▓░ + ▒▒▒▒░░░▒▒▒▒▒▒▓▓▓▓▓▓░ + ▒▒▒▒░░░▒▒▒▒▒▒▒▓▓▓▓▓▓ ▒▓░ + ▒▒▒░░░░▒▒▒▒▒▒▓▓▓▓▓▓ ▓▓▓▓▓▓▒ ▒▒▒▓▓█ + ▒▒▒▒░░░▒▒▒▒▒▒▒▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▓▓▓ + ░▒▒▒░░░░▒▒▒▒▒▒▓▓▓▓▓▓ █▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▓▓█ + ▒▒▒▒░░░▒▒▒▒▒▒▒▓▓▓▓▓░ ▓▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▓▓▓ + ▒▒▒▒░░░▒▒▒▒▒▒▒▓▓▓▓▓▓ ▒▓▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▓▓▒ + ▒▒▒▒▓▓ ▓▒▒▓▓▓▓ ▓▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▓▓█ + ▒▓ ▓▓▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▓▓ + ░░░░░░░░░░░▒▒▒▒ ▓▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▓▓█ + ▒▒░░░░░░░░░░▒▒▒▒▒▓▓▓ ▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▓▓▓ + ░▒░░░░░░░░░░░▒▒▒▒▒▓▓ ▓░ ░▓███▓ + ▒▒░░░░░░░░░░▒▒▒▒▒▓▓░ ▒▓▓▓▒▒▒ ░▒▒▒▓ ████████████ + ▒▒░░░░░░░░░░░▒▒▒▒▒▓▓ ▒▓▓▓▓▒▒▒▒▒▒▒▒░░░▒▒▒▒▒░ ░███ + ▒░░░░░░░░░░░▒▒▒▒▒▓▓ ▓▓▓▓▒▒▒▒▒▒▒▒░░░░▒▒▒▒▓ ███ + ▒▒░░░░░░░░░░▒▒▒▒▒▒▓▓ ▒▓▓▓▒▒▒▒▒▒▒▒░░░░▒▒▒▒▒ ▓██ + ▒░░░░░░░░░░░▒▒▒▒▒▓▓ ▓▓▓▓▒▒▒▒▒▒▒▒░░░▒▒▒▒▒▓ ▓██ + ▒▒░░░▒▒▒░░░▒▒░▒▒▒▓▓▒ ▒▓▓▓▒▒▒▒▒▒▒▒░░░░▒▒▒▒▒ ███ + ░▒▓ ░▓▓▓▓▒▒▒▒▒▒▒▒░░░░▒▒▒▒▓ ▓██ +╔────────────────────────────────────────────────────────────────▀▀▀─────────│─╗ +│ cosmopolitan § new technology » internationalization ─╬─│┼ +╚────────────────────────────────────────────────────────────────────────────│*/ +COSMOPOLITAN_C_START_ + +uint32_t GetOEMCP(); +uint32_t GetACP(); +bool32 GetCPInfoEx(uint32_t CodePage, uint32_t dwFlags, struct NtCpInfoEx *out_lpCPInfoEx) paramsnonnull((3)); + +COSMOPOLITAN_C_END_ +#endif /* COSMOPOLITAN_LIBC_NT_NLS_H_ */ \ No newline at end of file diff --git a/libc/nt/registry.h b/libc/nt/registry.h index d7f8abb9943..a03abfc5754 100644 --- a/libc/nt/registry.h +++ b/libc/nt/registry.h @@ -51,6 +51,8 @@ int RegOpenKey(int64_t hKey, const char16_t *opt_lpSubKey, int RegOpenKeyEx(int64_t hKey, const char16_t *opt_lpSubKey, uint32_t opt_ulOptions, int samDesired, int64_t *out_phkResult) paramsnonnull((5)); +int RegOpenKeyExA(int64_t hKey, const char *opt_lpSubKey, uint32_t opt_ulOptions, + int samDesired, int64_t *out_phkResult) paramsnonnull((5)); int RegCloseKey(int64_t hKey); int RegGetValue(int64_t hkey, const char16_t *opt_lpSubKey, diff --git a/libc/nt/struct/cpinfoex.h b/libc/nt/struct/cpinfoex.h new file mode 100644 index 00000000000..754501bb5a7 --- /dev/null +++ b/libc/nt/struct/cpinfoex.h @@ -0,0 +1,13 @@ +#ifndef COSMOPOLITAN_LIBC_NT_STRUCT_CPINFOEX_H_ +#define COSMOPOLITAN_LIBC_NT_STRUCT_CPINFOEX_H_ + +struct NtCpInfoEx { + uint32_t MaxCharSize; + uint8_t DefaultChar[2]; + uint8_t LeadByte[12]; + char16_t UnicodeDefaultChar; + uint32_t CodePage; + char16_t CodePageName[260]; +}; + +#endif /* COSMOPOLITAN_LIBC_NT_STRUCT_CPINFOEX_H_ */