Truly independent web browser
/*
* Copyright (c) 2024, stasoid <[email protected]>
* Copyright (c) 2025, ayeteadoe <[email protected]>
* Copyright (c) 2025, Ryszard Goc <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
// This header should be included only in cpp files.
// It should be included after all other files and should be separated from them by a non-#include line to prevent clang-format from changing header order.
#pragma once
#include <AK/Assertions.h>
#include <AK/Diagnostics.h>
#include <AK/Error.h>
#include <AK/Platform.h>
#include <AK/Vector.h>
#ifdef AK_OS_WINDOWS
# define timeval dummy_timeval
# include <ntstatus.h>
# include <winsock2.h>
# include <winternl.h>
extern "C" {
// NOTE: These are documented here: https://learn.microsoft.com/en-us/windows/win32/devnotes/-win32-misclowlevelclientsupport
// If the function signature changes, we should catch it with GetProcAddress failing in most cases.
// None of these are marked deprecated and they seem to be used internally in the kernel.
NTAPI NTSTATUS NtAssociateWaitCompletionPacket(
_In_ HANDLE WaitCompletionPacketHandle,
_In_ HANDLE IoCompletionHandle,
_In_ HANDLE TargetObjectHandle,
_In_opt_ PVOID KeyContext,
_In_opt_ PVOID ApcContext,
_In_ NTSTATUS IoStatus,
_In_ ULONG_PTR IoStatusInformation,
_Out_opt_ PBOOLEAN AlreadySignaled);
NTAPI NTSTATUS NtCancelWaitCompletionPacket(
_In_ HANDLE WaitCompletionPacketHandle,
_In_ BOOLEAN RemoveSignaledPacket);
NTAPI NTSTATUS NtCreateWaitCompletionPacket(
_Out_ PHANDLE WaitCompletionPacketHandle,
_In_ ACCESS_MASK DesiredAccess,
_In_opt_ POBJECT_ATTRIBUTES ObjectAttributes);
// https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/ntifs/nf-ntifs-ntqueryinformationfile
NTAPI NTSTATUS NtQueryInformationFile(
_In_ HANDLE FileHandle,
_Out_ PIO_STATUS_BLOCK IoStatusBlock,
_Out_ PVOID FileInformation,
_In_ ULONG Length,
// winternl.h's FILE_INFORMATION_CLASS lacks most of the classes, so the class is taken as its value.
_In_ ULONG FileInformationClass);
// https://learn.microsoft.com/en-us/windows/win32/seccng/processprng
BOOL WINAPI ProcessPrng(PBYTE pbData, SIZE_T cbData);
}
using PFN_NtCreateWaitCompletionPacket = decltype(&NtCreateWaitCompletionPacket);
using PFN_NtCancelWaitCompletionPacket = decltype(&NtCancelWaitCompletionPacket);
using PFN_NtAssociateWaitCompletionPacket = decltype(&NtAssociateWaitCompletionPacket);
using PFN_NtCreateFile = decltype(&NtCreateFile);
using PFN_NtQueryInformationFile = decltype(&NtQueryInformationFile);
using PFN_ProcessPrng = decltype(&ProcessPrng);
inline struct SystemApi {
PFN_NtAssociateWaitCompletionPacket NtAssociateWaitCompletionPacket = NULL;
PFN_NtCancelWaitCompletionPacket NtCancelWaitCompletionPacket = NULL;
PFN_NtCreateWaitCompletionPacket NtCreateWaitCompletionPacket = NULL;
PFN_NtCreateFile NtCreateFile = NULL;
PFN_NtQueryInformationFile NtQueryInformationFile = NULL;
PFN_ProcessPrng ProcessPrng = NULL;
SystemApi()
{
HMODULE hBcryptprimitives = LoadLibraryW(L"bcryptprimitives.dll");
HMODULE hNtdll = GetModuleHandleW(L"ntdll.dll");
VERIFY(hBcryptprimitives);
VERIFY(hNtdll);
AK_IGNORE_DIAGNOSTIC("-Wcast-function-type-mismatch",
NtAssociateWaitCompletionPacket = (PFN_NtAssociateWaitCompletionPacket)GetProcAddress(hNtdll, "NtAssociateWaitCompletionPacket");
NtCancelWaitCompletionPacket = (PFN_NtCancelWaitCompletionPacket)GetProcAddress(hNtdll, "NtCancelWaitCompletionPacket");
NtCreateWaitCompletionPacket = (PFN_NtCreateWaitCompletionPacket)GetProcAddress(hNtdll, "NtCreateWaitCompletionPacket");
NtCreateFile = (PFN_NtCreateFile)GetProcAddress(hNtdll, "NtCreateFile");
NtQueryInformationFile = (PFN_NtQueryInformationFile)GetProcAddress(hNtdll, "NtQueryInformationFile");
ProcessPrng = (PFN_ProcessPrng)GetProcAddress(hBcryptprimitives, "ProcessPrng");)
VERIFY(NtAssociateWaitCompletionPacket);
VERIFY(NtCancelWaitCompletionPacket);
VERIFY(NtCreateWaitCompletionPacket);
VERIFY(NtCreateFile);
VERIFY(NtQueryInformationFile);
VERIFY(ProcessPrng);
}
} g_system;
# undef timeval
# undef IN
# pragma comment(lib, "ws2_32.lib")
# include <io.h>
# include <stdlib.h>
// Converts UTF-8 text to a null-terminated UTF-16 string for use with wide-char Win32 APIs.
inline ErrorOr<Vector<wchar_t>> to_wide_string(StringView utf8)
{
int length = 0;
if (!utf8.is_empty()) {
length = MultiByteToWideChar(CP_UTF8, 0, utf8.characters_without_null_termination(), static_cast<int>(utf8.length()), nullptr, 0);
if (length == 0)
return Error::from_windows_error();
}
Vector<wchar_t> result;
TRY(result.try_resize(length + 1));
if (length > 0)
MultiByteToWideChar(CP_UTF8, 0, utf8.characters_without_null_termination(), static_cast<int>(utf8.length()), result.data(), length);
return result;
}
// Cached SYSTEM_INFO::dwAllocationGranularity — the required alignment for MapViewOfFile offsets.
inline size_t system_allocation_granularity()
{
static size_t granularity = [] {
SYSTEM_INFO system_info {};
GetSystemInfo(&system_info);
return static_cast<size_t>(system_info.dwAllocationGranularity);
}();
return granularity;
}
inline void initiate_wsa()
{
WSADATA wsa;
WORD version = MAKEWORD(2, 2);
int rc = WSAStartup(version, &wsa);
VERIFY(rc == 0 && wsa.wVersion == version);
}
inline void terminate_wsa()
{
int rc = WSACleanup();
VERIFY(rc == 0);
}
static void invalid_parameter_handler(wchar_t const*, wchar_t const*, wchar_t const*, unsigned int, uintptr_t)
{
}
inline void override_crt_invalid_parameter_handler()
{
// Make _get_osfhandle return -1 instead of crashing on invalid fd in release (debug still __debugbreak's)
_set_invalid_parameter_handler(invalid_parameter_handler);
}
inline UINT& saved_console_output_code_page()
{
static UINT code_page = 0;
return code_page;
}
inline void use_utf8_console_output()
{
// All Ladybird output is UTF-8; make consoles interpret it as such. The code page is a property of the console
// itself and outlives this process, so the original one is restored in windows_shutdown(). Save it only once:
// this runs both from AK/Format.cpp's static initializer and from windows_init(), and a later run would
// overwrite the saved value with the already-switched CP_UTF8.
if (saved_console_output_code_page() == 0)
saved_console_output_code_page() = GetConsoleOutputCP();
if (saved_console_output_code_page() != 0)
SetConsoleOutputCP(CP_UTF8);
}
inline void windows_init()
{
initiate_wsa();
override_crt_invalid_parameter_handler();
use_utf8_console_output();
}
inline void windows_shutdown()
{
if (saved_console_output_code_page() != 0 && saved_console_output_code_page() != CP_UTF8)
SetConsoleOutputCP(saved_console_output_code_page());
terminate_wsa();
}
#endif