From 6330b6826e46453dd547fee1fa7f11a6c26d35e4 Mon Sep 17 00:00:00 2001 From: A2va <49582555+A2va@users.noreply.github.com> Date: Wed, 26 Mar 2025 13:57:10 +0100 Subject: [PATCH] Add win32 apis --- libc/isystem/windowsesque.h | 6 ++++ libc/nt/base.h | 39 ++++++++++++++++++++++++ libc/nt/files.h | 1 + libc/nt/kernel32/GetDriveTypeA.S | 20 ++++++++++++ libc/nt/kernel32/ReadDirectoryChangesW.S | 18 +++++++++++ libc/nt/master.sh | 2 ++ libc/nt/windows.h | 1 + 7 files changed, 87 insertions(+) create mode 100644 libc/nt/base.h create mode 100644 libc/nt/kernel32/GetDriveTypeA.S create mode 100644 libc/nt/kernel32/ReadDirectoryChangesW.S diff --git a/libc/isystem/windowsesque.h b/libc/isystem/windowsesque.h index 4b27c516c..1e955c709 100644 --- a/libc/isystem/windowsesque.h +++ b/libc/isystem/windowsesque.h @@ -381,6 +381,9 @@ #define _FILE_STREAM_INFORMATION NtFileStreamInformation #define FILE_STREAM_INFORMATION struct NtFileStreamInformation #define PFILE_STREAM_INFORMATION struct NtFileStreamInformation* +#define _FILE_NOTIFY_INFORMATION NtFileNotifyInformation +#define FILE_NOTIFY_INFORMATION struct NtFileNotifyInformation +#define PFILE_NOTIFY_INFORMATION struct NtFileNotifyInformation* #define _KERNEL_USER_TIMES NtKernelUserTimes #define KERNEL_USER_TIMES struct NtKernelUserTimes #define PKERNEL_USER_TIMES struct NtKernelUserTimes* @@ -1361,6 +1364,9 @@ #define WSAEVENT HANDLE #define GROUP uint32_t +#define _OVERLAPPED NtOverlapped +#define OVERLAPPED struct NtOverlapped +#define LOVERLAPPED struct NtOverlapped* #define WSAOVERLAPPED OVERLAPPED #define INVALID_SOCKET -1ULL #define SOCKET_ERROR -1 diff --git a/libc/nt/base.h b/libc/nt/base.h new file mode 100644 index 000000000..bb00d987a --- /dev/null +++ b/libc/nt/base.h @@ -0,0 +1,39 @@ +#ifndef COSMOPOLITAN_LIBC_NT_BASE_H_ +#define COSMOPOLITAN_LIBC_NT_BASE_H_ +#include "struct/overlapped.h" +/* ░░░░ + ▒▒▒░░░▒▒▒▒▒▒▒▓▓▓░ + ▒▒▒▒░░░▒▒▒▒▒▒▓▓▓▓▓▓░ + ▒▒▒▒░░░▒▒▒▒▒▒▒▓▓▓▓▓▓ ▒▓░ + ▒▒▒░░░░▒▒▒▒▒▒▓▓▓▓▓▓ ▓▓▓▓▓▓▒ ▒▒▒▓▓█ + ▒▒▒▒░░░▒▒▒▒▒▒▒▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▓▓▓ + ░▒▒▒░░░░▒▒▒▒▒▒▓▓▓▓▓▓ █▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▓▓█ + ▒▒▒▒░░░▒▒▒▒▒▒▒▓▓▓▓▓░ ▓▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▓▓▓ + ▒▒▒▒░░░▒▒▒▒▒▒▒▓▓▓▓▓▓ ▒▓▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▓▓▒ + ▒▒▒▒▓▓ ▓▒▒▓▓▓▓ ▓▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▓▓█ + ▒▓ ▓▓▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▓▓ + ░░░░░░░░░░░▒▒▒▒ ▓▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▓▓█ + ▒▒░░░░░░░░░░▒▒▒▒▒▓▓▓ ▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▓▓▓ + ░▒░░░░░░░░░░░▒▒▒▒▒▓▓ ▓░ ░▓███▓ + ▒▒░░░░░░░░░░▒▒▒▒▒▓▓░ ▒▓▓▓▒▒▒ ░▒▒▒▓ ████████████ + ▒▒░░░░░░░░░░░▒▒▒▒▒▓▓ ▒▓▓▓▓▒▒▒▒▒▒▒▒░░░▒▒▒▒▒░ ░███ + ▒░░░░░░░░░░░▒▒▒▒▒▓▓ ▓▓▓▓▒▒▒▒▒▒▒▒░░░░▒▒▒▒▓ ███ + ▒▒░░░░░░░░░░▒▒▒▒▒▒▓▓ ▒▓▓▓▒▒▒▒▒▒▒▒░░░░▒▒▒▒▒ ▓██ + ▒░░░░░░░░░░░▒▒▒▒▒▓▓ ▓▓▓▓▒▒▒▒▒▒▒▒░░░▒▒▒▒▒▓ ▓██ + ▒▒░░░▒▒▒░░░▒▒░▒▒▒▓▓▒ ▒▓▓▓▒▒▒▒▒▒▒▒░░░░▒▒▒▒▒ ███ + ░▒▓ ░▓▓▓▓▒▒▒▒▒▒▒▒░░░░▒▒▒▒▓ ▓██ +╔────────────────────────────────────────────────────────────────▀▀▀─────────│─╗ +│ cosmopolitan § new technology » string ─╬─│┼ +╚────────────────────────────────────────────────────────────────────────────│*/ +COSMOPOLITAN_C_START_ + +typedef void (*NtOverlappedCompletionRoutine)( + uint32_t dwErrorCode, uint32_t dwNumberOfBytesTransfered, + struct NtOverlapped *lpOverlapped); + + +uint32_t FormatMessageW(uint32_t dwFlags, const void* lpSource, uint32_t dwMessageId, uint32_t dwLanguageId, char16_t *lpBuffer, uint32_t nSize, va_list *Arguments); +bool32 ReadDirectoryChangesW(int64_t hDirectory, void *lpBuffer, uint32_t nBufferLength, bool32 bWatchSubtree, uint32_t dwNotifyFilter, uint32_t *lpBytesReturned, struct NtOverlapped *lpOverlapped, NtOverlappedCompletionRoutine lpCompletionRoutine); + +COSMOPOLITAN_C_END_ +#endif /* COSMOPOLITAN_LIBC_NT_BASE_H_ */ \ No newline at end of file diff --git a/libc/nt/files.h b/libc/nt/files.h index 2b844c32f..44c848883 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); +unsigned int GetDriveTypeA(const char *lpRootPathName); uint32_t GetLogicalDriveStringsA(uint32_t nBufferLength, char *lpBuffer); bool32 FlushFileBuffers(int64_t hFile); diff --git a/libc/nt/kernel32/GetDriveTypeA.S b/libc/nt/kernel32/GetDriveTypeA.S new file mode 100644 index 000000000..8b33c2dd5 --- /dev/null +++ b/libc/nt/kernel32/GetDriveTypeA.S @@ -0,0 +1,20 @@ +#include "libc/nt/codegen.h" +.imp kernel32,__imp_GetDriveTypeA,GetDriveTypeA + + .text.windows + .ftrace1 +GetDriveTypeA: + .ftrace2 +#ifdef __x86_64__ + push %rbp + mov %rsp,%rbp + mov %rdi,%rcx + sub $32,%rsp + call *__imp_GetDriveTypeA(%rip) + leave +#elif defined(__aarch64__) + mov x0,#0 +#endif + ret + .endfn GetDriveTypeA,globl + .previous diff --git a/libc/nt/kernel32/ReadDirectoryChangesW.S b/libc/nt/kernel32/ReadDirectoryChangesW.S new file mode 100644 index 000000000..cae680a3e --- /dev/null +++ b/libc/nt/kernel32/ReadDirectoryChangesW.S @@ -0,0 +1,18 @@ +#include "libc/nt/codegen.h" +.imp kernel32,__imp_ReadDirectoryChangesW,ReadDirectoryChangesW + + .text.windows + .ftrace1 +ReadDirectoryChanges: + .ftrace2 +#ifdef __x86_64__ + push %rbp + mov %rsp,%rbp + mov __imp_ReadDirectoryChangesW(%rip),%rax + jmp __sysv2nt8 +#elif defined(__aarch64__) + mov x0,#0 + ret +#endif + .endfn ReadDirectoryChanges,globl + .previous diff --git a/libc/nt/master.sh b/libc/nt/master.sh index 570a77e72..0c67da930 100755 --- a/libc/nt/master.sh +++ b/libc/nt/master.sh @@ -117,6 +117,7 @@ imp 'GetCurrentProcessorNumberEx' GetCurrentProcessorNumberEx kernel32 1 imp 'GetCurrentThread' GetCurrentThread kernel32 0 imp 'GetCurrentThreadId' GetCurrentThreadId kernel32 0 imp 'GetDynamicTimeZoneInformation' GetDynamicTimeZoneInformation kernel32 1 +imp 'GetDriveTypeA' GetDriveTypeA kernel32 1 imp 'GetEnvironmentStrings' GetEnvironmentStringsW kernel32 1 imp 'GetEnvironmentVariable' GetEnvironmentVariableW kernel32 3 imp 'GetExitCodeThread' GetExitCodeThread kernel32 2 @@ -239,6 +240,7 @@ imp 'ReadConsoleOutputCharacter' ReadConsoleOutputCharacterW kernel32 5 imp 'ReadFile' ReadFile kernel32 5 imp 'ReadFileEx' ReadFileEx kernel32 5 imp 'ReadFileScatter' ReadFileScatter kernel32 5 +imp 'ReadDirectoryChanges' ReadDirectoryChangesW kernel32 8 imp 'RegisterWaitForSingleObject' RegisterWaitForSingleObject kernel32 6 imp 'ReleaseMutex' ReleaseMutex kernel32 1 imp 'ReleaseSRWLockExclusive' ReleaseSRWLockExclusive kernel32 1 diff --git a/libc/nt/windows.h b/libc/nt/windows.h index e8c6de3b9..eab21b0d3 100644 --- a/libc/nt/windows.h +++ b/libc/nt/windows.h @@ -94,6 +94,7 @@ int WideCharToMultiByte(unsigned int CodePage, uint32_t dwFlags, uint16_t *lpWideCharStr, int cchWideChar, char *lpMultiByteStr, int cbMultiByte, uint16_t *lpDefaultChar, int *lpUsedDefaultChar); +int MutliByteToWideChar(unsigned int CodePage, uint32_t dwFlags, const char *lpMultiByteStr, int cbMultiByte, uint16_t *lpWideCharStr, int cchWideChar); COSMOPOLITAN_C_END_ #endif /* COSMOPOLITAN_LIBC_NT_WINDOWS_H_ */