/* * Copyright (c) 2022 Winsider Seminars & Solutions, Inc. All rights reserved. * * This file is part of System Informer. * * Authors: * * wj32 2010-2016 * dmex 2017-2026 * */ #ifndef _PH_PHBASESUP_H #define _PH_PHBASESUP_H EXTERN_C_START PHLIBAPI NTSTATUS NTAPI PhBaseInitialization( VOID ); // // Threads // #ifdef DEBUG typedef struct _PH_AUTO_POOL *PPH_AUTO_POOL; typedef struct _PHP_BASE_THREAD_DBG { CLIENT_ID ClientId; LIST_ENTRY ListEntry; PVOID StartAddress; PVOID Parameter; PPH_AUTO_POOL CurrentAutoPool; } PHP_BASE_THREAD_DBG, *PPHP_BASE_THREAD_DBG; extern ULONG PhDbgThreadDbgTlsIndex; extern LIST_ENTRY PhDbgThreadListHead; extern PH_QUEUED_LOCK PhDbgThreadListLock; #endif PHLIBAPI NTSTATUS NTAPI PhCreateUserThread( _In_ HANDLE ProcessHandle, _In_opt_ PSECURITY_DESCRIPTOR ThreadSecurityDescriptor, _In_ ACCESS_MASK DesiredAccess, _In_opt_ ULONG CreateFlags, _In_opt_ SIZE_T ZeroBits, _In_opt_ SIZE_T StackSize, _In_opt_ SIZE_T MaximumStackSize, _In_ PUSER_THREAD_START_ROUTINE StartRoutine, _In_opt_ PVOID Argument, _Out_opt_ PHANDLE ThreadHandle, _Out_opt_ PCLIENT_ID ClientId ); PHLIBAPI HANDLE NTAPI PhCreateThread( _In_opt_ SIZE_T StackSize, _In_ PUSER_THREAD_START_ROUTINE StartAddress, _In_opt_ PVOID Parameter ); PHLIBAPI NTSTATUS NTAPI PhCreateThreadEx( _Out_ PHANDLE ThreadHandle, _In_ PUSER_THREAD_START_ROUTINE StartAddress, _In_opt_ PVOID Parameter ); PHLIBAPI NTSTATUS NTAPI PhCreateThread2( _In_ PUSER_THREAD_START_ROUTINE StartAddress, _In_opt_ PVOID Parameter ); PHLIBAPI NTSTATUS NTAPI PhQueueUserWorkItem( _In_ PUSER_THREAD_START_ROUTINE StartRoutine, _In_opt_ PVOID Argument ); // // Misc. system // PHLIBAPI ULONG64 NTAPI PhReadTimeStampCounter( VOID ); PHLIBAPI BOOLEAN NTAPI PhQueryPerformanceCounter( _Out_ PLARGE_INTEGER PerformanceCounter ); PHLIBAPI BOOLEAN NTAPI PhQueryPerformanceFrequency( _Out_ PLARGE_INTEGER PerformanceFrequency ); /** * Reads the current high-resolution performance counter value. * \return The current performance counter value. */ FORCEINLINE ULONGLONG NTAPI PhReadPerformanceCounter( VOID ) { LARGE_INTEGER counter; PhQueryPerformanceCounter(&counter); return (ULONGLONG)counter.QuadPart; } /** * Reads the high-resolution performance counter frequency. * \return The performance counter frequency in counts per second. */ FORCEINLINE ULONGLONG NTAPI PhReadPerformanceFrequency( VOID ) { LARGE_INTEGER counter; PhQueryPerformanceFrequency(&counter); return (ULONGLONG)counter.QuadPart; } PHLIBAPI VOID NTAPI PhQueryInterruptTime( _Out_ PLARGE_INTEGER InterruptTime ); PHLIBAPI VOID NTAPI PhQueryUnbiasedInterruptTime( _Out_ PLARGE_INTEGER UnbiasedInterruptTime ); /** * Reads the tick count used for deadlines governing a wait. * * \return The elapsed time, in 100-nanosecond units, in the same time * domain as the wait that it bounds. Scale millisecond bounds into this * domain with UInt32x32To64(Milliseconds, PH_TIMEOUT_MS). * * \remarks There are three distinct time domains, and they are not * interchangeable: * * - Biased time (NtGetTickCount64): boot-relative time that includes * time spent suspended. This is suitable for user-interface throttling. * - Unbiased time (QueryUnbiasedInterruptTime): time that the system * has spent in the working state. * - Wall-clock uptime (PhGetSystemUptime). * * This returns the native 100-nanosecond tick domain rather than * milliseconds. The underlying counters already report ticks, so a * millisecond-returning wrapper would have to divide by PH_TICKS_PER_MS on * every read, discarding the sub-millisecond remainder and leaving each * converted read up to one millisecond away from the true value. Returning * ticks avoids that conversion altogether: callers compare at the full * resolution the counter provides, and rounding occurs only where * milliseconds are actually required, in the argument handed to a wait * routine. * * Windows 7 and earlier (Biased Domain): Wait APIs (like NtWaitForSingleObject with relative timeouts) * evaluate deadlines against the standard interrupt time, which continues to tick while the system is suspended or sleeping. * Thus, PhQueryInterruptTime() (which tracks the biased domain) provides the correct baseline to prevent premature expiration upon resume. * * Windows 8 and later (Unbiased Domain): To support Connected Standby (Modern Standby), the kernel was updated and relative wait evaluations * are paused during sleep states. Wait timeouts now operate strictly in the unbiased domain. If a thread waits for 10 seconds and the machine * sleeps for 8, the thread still has 2 seconds remaining upon wake. * Thus, PhQueryUnbiasedInterruptTime() (which tracks the unbiased domain) provides the correct baseline to prevent premature expiration upon resume. * * Define PHNT_TICKWAIT to use NtGetTickCount64() on Windows 7 and earlier. It tracks the same biased * domain but only advances once per clock tick (~15.6 ms by default). */ FORCEINLINE ULONG64 NTAPI PhQueryWaitTime( VOID ) { LARGE_INTEGER interruptTime; if (WindowsVersion < WINDOWS_8) { #if defined(PHNT_TICKWAIT) return NtGetTickCount64() * PH_TICKS_PER_MS; #else PhQueryInterruptTime(&interruptTime); #endif } else { PhQueryUnbiasedInterruptTime(&interruptTime); } return (ULONG64)interruptTime.QuadPart; } PHLIBAPI VOID NTAPI PhQuerySystemTime( _Out_ PLARGE_INTEGER SystemTime ); PHLIBAPI NTSTATUS NTAPI PhQueryTimeZoneBias( _Out_ PLARGE_INTEGER TimeZoneBias ); PHLIBAPI NTSTATUS NTAPI PhSystemTimeToLocalTime( _In_ PLARGE_INTEGER SystemTime, _Out_ PLARGE_INTEGER LocalTime ); PHLIBAPI NTSTATUS NTAPI PhLocalTimeToSystemTime( _In_ PLARGE_INTEGER LocalTime, _Out_ PLARGE_INTEGER SystemTime ); PHLIBAPI BOOLEAN NTAPI PhTimeToSecondsSince1980( _In_ PLARGE_INTEGER Time, _Out_ PULONG ElapsedSeconds ); PHLIBAPI BOOLEAN NTAPI PhTimeToSecondsSince1970( _In_ PLARGE_INTEGER Time, _Out_ PULONG ElapsedSeconds ); PHLIBAPI VOID NTAPI PhSecondsSince1980ToTime( _In_ ULONG ElapsedSeconds, _Out_ PLARGE_INTEGER Time ); PHLIBAPI VOID NTAPI PhSecondsSince1970ToTime( _In_ ULONG ElapsedSeconds, _Out_ PLARGE_INTEGER Time ); // // Heap // _May_raise_ _Post_writable_byte_size_(Size) PHLIBAPI DECLSPEC_ALLOCATOR DECLSPEC_NOALIAS DECLSPEC_RESTRICT PVOID NTAPI PhAllocate( _In_ SIZE_T Size ); _Must_inspect_result_ _Ret_maybenull_ _When_(return != NULL, _Post_writable_byte_size_(Size)) _Success_(return != NULL) PHLIBAPI DECLSPEC_ALLOCATOR DECLSPEC_NOALIAS DECLSPEC_RESTRICT PVOID NTAPI PhAllocateSafe( _In_ SIZE_T Size ); _Must_inspect_result_ _Ret_maybenull_ _When_(return != NULL, _Post_writable_byte_size_(Size)) _Success_(return != NULL) PHLIBAPI DECLSPEC_ALLOCATOR DECLSPEC_NOALIAS DECLSPEC_RESTRICT PVOID NTAPI PhAllocateExSafe( _In_ SIZE_T Size, _In_ ULONG Flags ); PHLIBAPI VOID NTAPI PhFree( _In_opt_ _Frees_ptr_opt_ _Post_invalid_ PVOID Memory ); _Must_inspect_result_ PHLIBAPI NTSTATUS NTAPI PhAllocateHeap( _In_ SIZE_T Size, _Outptr_result_bytebuffer_(Size) PVOID* Buffer ); _May_raise_ _Ret_maybenull_ _When_(Size == 0, _Post_null_) _When_(return != NULL, _Post_writable_byte_size_(Size)) _Success_(return != NULL) PHLIBAPI DECLSPEC_ALLOCATOR DECLSPEC_NOALIAS DECLSPEC_RESTRICT PVOID NTAPI PhReAllocate( _In_opt_ _Frees_ptr_opt_ PVOID Memory, _In_ SIZE_T Size ); _Must_inspect_result_ _Ret_maybenull_ _When_(return != NULL, _Post_writable_byte_size_(Size)) _Success_(return != NULL) PHLIBAPI DECLSPEC_ALLOCATOR DECLSPEC_NOALIAS DECLSPEC_RESTRICT PVOID NTAPI PhReAllocateSafe( _In_opt_ _Frees_ptr_opt_ PVOID Memory, _In_ SIZE_T Size ); _Must_inspect_result_ _Ret_maybenull_ _When_(return != NULL, _Post_writable_byte_size_(Size)) _Success_(return != NULL) PHLIBAPI DECLSPEC_ALLOCATOR DECLSPEC_NOALIAS DECLSPEC_RESTRICT PVOID NTAPI PhReAllocateExSafe( _In_opt_ _Frees_ptr_opt_ PVOID Memory, _In_ SIZE_T Size, _In_ ULONG Flags ); PHLIBAPI SIZE_T NTAPI PhSizeHeap( _In_ PVOID Memory ); _Must_inspect_result_ _Ret_maybenull_ _When_(return != NULL, _Post_writable_byte_size_(Size)) _Success_(return != NULL) PHLIBAPI DECLSPEC_ALLOCATOR DECLSPEC_NOALIAS DECLSPEC_RESTRICT PVOID NTAPI PhAllocatePage( _In_ SIZE_T Size, _Out_opt_ PSIZE_T NewSize ); PHLIBAPI VOID NTAPI PhFreePage( _In_opt_ _Frees_ptr_opt_ _Post_invalid_ PVOID Memory ); FORCEINLINE PVOID PhAllocatePageZero( _In_ SIZE_T Size ) { PVOID buffer; if (buffer = PhAllocatePage(Size, NULL)) { memset(buffer, 0, Size); return buffer; } return NULL; } _Must_inspect_result_ _Ret_maybenull_ _When_(return != NULL, _Post_writable_byte_size_(Size)) _Success_(return != NULL) PHLIBAPI DECLSPEC_ALLOCATOR DECLSPEC_NOALIAS DECLSPEC_RESTRICT PVOID NTAPI PhAllocatePageAligned( _In_ SIZE_T Size, _In_ SIZE_T Alignment ); PHLIBAPI VOID NTAPI PhFreePageAligned( _In_opt_ _Frees_ptr_opt_ _Post_invalid_ PVOID Memory ); PHLIBAPI NTSTATUS NTAPI PhAllocateVirtualMemory( _In_ HANDLE ProcessHandle, _Outptr_result_bytebuffer_(AllocationSize) PVOID* BaseAddress, _In_ SIZE_T AllocationSize, _In_ ULONG AllocationType, _In_ ULONG Protection ); PHLIBAPI NTSTATUS NTAPI PhFreeVirtualMemory( _In_ HANDLE ProcessHandle, _In_ PVOID BaseAddress, _In_ ULONG FreeType ); PHLIBAPI NTSTATUS NTAPI PhProtectVirtualMemory( _In_ HANDLE ProcessHandle, _In_ PVOID BaseAddress, _In_ SIZE_T RegionSize, _In_ ULONG NewProtection, _Out_opt_ PULONG OldProtection ); PHLIBAPI NTSTATUS NTAPI PhReadVirtualMemory( _In_ HANDLE ProcessHandle, _In_opt_ PVOID BaseAddress, _Out_writes_bytes_(BufferSize) PVOID Buffer, _In_ SIZE_T BufferSize, _Out_opt_ PSIZE_T NumberOfBytesRead ); PHLIBAPI NTSTATUS NTAPI PhReadVirtualMemoryPrefix( _In_ HANDLE ProcessHandle, _In_opt_ PVOID BaseAddress, _Out_writes_bytes_(BufferSize) PVOID Buffer, _In_ SIZE_T BufferSize, _Out_opt_ PSIZE_T NumberOfBytesRead ); PHLIBAPI NTSTATUS NTAPI PhWriteVirtualMemory( _In_ HANDLE ProcessHandle, _In_opt_ PVOID BaseAddress, _In_reads_bytes_(NumberOfBytesToWrite) PVOID Buffer, _In_ SIZE_T NumberOfBytesToWrite, _Out_opt_ PSIZE_T NumberOfBytesWritten ); FORCEINLINE PVOID PhAllocateCopy( _In_ PVOID Data, _In_ SIZE_T Size ) { PVOID copy; copy = PhAllocate(Size); memcpy(copy, Data, Size); return copy; } FORCEINLINE PVOID PhAllocateZero( _In_ SIZE_T Size ) { return PhAllocateExSafe(Size, HEAP_GENERATE_EXCEPTIONS | HEAP_ZERO_MEMORY); } FORCEINLINE PVOID PhAllocateZeroSafe( _In_ SIZE_T Size ) { return PhAllocateExSafe(Size, HEAP_ZERO_MEMORY); } FORCEINLINE PVOID PhReAllocateZeroSafe( _In_opt_ PVOID Memory, _In_ SIZE_T Size ) { return PhReAllocateExSafe(Memory, Size, HEAP_ZERO_MEMORY); } #define PhAllocateStack(Size) _malloca(Size) #define PhFreeStack(Memory) _freea(Memory) // // Singly linked list // // rev from RtlInitializeSListHead (dmex) /** * The PhInitializeSListHead function initializes a sequenced singly linked listhead. * * \param ListHead A pointer to a sequenced singly linked listhead. */ FORCEINLINE VOID NTAPI PhInitializeSListHead( _Out_ PSLIST_HEADER ListHead ) { #if defined(PHNT_NATIVE_SLIST) RtlInitializeSListHead(ListHead); #else if (IS_ALIGNED(ListHead, MEMORY_ALLOCATION_ALIGNMENT)) memset(ListHead, 0, sizeof(SLIST_HEADER)); else PhRaiseStatus(STATUS_DATATYPE_MISALIGNMENT); #endif } // rev from RtlQueryDepthSList (dmex) /** * The PhQueryDepthSList function queries the current number of entries contained in a sequenced single linked list. * * \param ListHead A pointer to a sequenced singly linked listhead. * \return The current number of entries in the sequenced singly linked list is returned as the function value. */ FORCEINLINE USHORT NTAPI PhQueryDepthSList( _In_ PSLIST_HEADER ListHead ) { #if defined(PHNT_NATIVE_SLIST) return RtlQueryDepthSList(ListHead); #else #ifdef _M_X64 return (USHORT)ListHead->HeaderX64.Depth; #elif _M_ARM64 return (USHORT)ListHead->HeaderArm64.Depth; #else return ListHead->Depth; #endif #endif } // // Event // #define PH_EVENT_SET 0x1 #define PH_EVENT_SET_SHIFT 0 #define PH_EVENT_REFCOUNT_SHIFT 1 #define PH_EVENT_REFCOUNT_INC 0x2 #define PH_EVENT_REFCOUNT_MASK (((ULONG_PTR)1 << 15) - 1) /** * A fast event object. * * \remarks This event object does not use a kernel event object until necessary, and frees the * object automatically when it is no longer needed. */ typedef struct _PH_EVENT { union { ULONG_PTR Value; struct { USHORT Set : 1; USHORT RefCount : 15; UCHAR Reserved; UCHAR AvailableForUse; #ifdef _WIN64 ULONG Spare; #endif }; }; HANDLE EventHandle; } PH_EVENT, *PPH_EVENT; #define PH_EVENT_INIT { { PH_EVENT_REFCOUNT_INC }, NULL } PHLIBAPI VOID FASTCALL PhfInitializeEvent( _Out_ PPH_EVENT Event ); #define PhSetEvent PhfSetEvent PHLIBAPI VOID FASTCALL PhfSetEvent( _Inout_ PPH_EVENT Event ); PHLIBAPI BOOLEAN FASTCALL PhfWaitForEvent( _Inout_ PPH_EVENT Event, _In_opt_ PLARGE_INTEGER Timeout ); FORCEINLINE BOOLEAN PhWaitForEvent( _Inout_ PPH_EVENT Event, _In_opt_ PLARGE_INTEGER Timeout ) { if (ReadULongPtrAcquire(&Event->Value) & PH_EVENT_SET) return TRUE; return PhfWaitForEvent(Event, Timeout); } #define PhResetEvent PhfResetEvent PHLIBAPI VOID FASTCALL PhfResetEvent( _Inout_ PPH_EVENT Event ); FORCEINLINE VOID PhInitializeEvent( _Out_ PPH_EVENT Event ) { WriteULongPtrRelease(&Event->Value, PH_EVENT_REFCOUNT_INC); #pragma warning(push) #pragma warning(disable : 6387) WritePointerRelease(&Event->EventHandle, NULL); #pragma warning(pop) } /** * Determines whether an event object is set. * * \param Event A pointer to an event object. * * \return TRUE if the event object is set, otherwise FALSE. */ FORCEINLINE BOOLEAN PhTestEvent( _In_ PPH_EVENT Event ) { return !!(ReadULongPtrAcquire(&Event->Value) & PH_EVENT_SET); } // // Barrier // #define PH_BARRIER_COUNT_SHIFT 0 #define PH_BARRIER_COUNT_MASK (((LONG_PTR)1 << (sizeof(ULONG_PTR) * 8 / 2 - 1)) - 1) #define PH_BARRIER_COUNT_INC ((LONG_PTR)1 << PH_BARRIER_COUNT_SHIFT) #define PH_BARRIER_TARGET_SHIFT (sizeof(ULONG_PTR) * 8 / 2) #define PH_BARRIER_TARGET_MASK (((LONG_PTR)1 << (sizeof(ULONG_PTR) * 8 / 2 - 1)) - 1) #define PH_BARRIER_TARGET_INC ((LONG_PTR)1 << PH_BARRIER_TARGET_SHIFT) #define PH_BARRIER_WAKING ((LONG_PTR)1 << (sizeof(ULONG_PTR) * 8 - 1)) #define PH_BARRIER_MASTER 1 #define PH_BARRIER_SLAVE 2 #define PH_BARRIER_OBSERVER 3 typedef struct _PH_BARRIER { ULONG_PTR Value; PH_WAKE_EVENT WakeEvent; } PH_BARRIER, *PPH_BARRIER; #define PH_BARRIER_INIT(Target) { (ULONG_PTR)(Target) << PH_BARRIER_TARGET_SHIFT, PH_WAKE_EVENT_INIT } PHLIBAPI VOID FASTCALL PhfInitializeBarrier( _Out_ PPH_BARRIER Barrier, _In_ ULONG_PTR Target ); #define PhWaitForBarrier PhfWaitForBarrier PHLIBAPI BOOLEAN FASTCALL PhfWaitForBarrier( _Inout_ PPH_BARRIER Barrier, _In_ BOOLEAN Spin ); FORCEINLINE VOID PhInitializeBarrier( _Out_ PPH_BARRIER Barrier, _In_ ULONG_PTR Target ) { Barrier->Value = Target << PH_BARRIER_TARGET_SHIFT; PhInitializeQueuedLock(&Barrier->WakeEvent); } // // Rundown protection // #define PH_RUNDOWN_ACTIVE 0x1 #define PH_RUNDOWN_REF_SHIFT 1 #define PH_RUNDOWN_REF_INC 0x2 typedef struct _PH_RUNDOWN_PROTECT { ULONG_PTR Value; } PH_RUNDOWN_PROTECT, *PPH_RUNDOWN_PROTECT; #define PH_RUNDOWN_PROTECT_INIT { 0 } typedef struct _PH_RUNDOWN_WAIT_BLOCK { ULONG_PTR Count; PH_EVENT WakeEvent; } PH_RUNDOWN_WAIT_BLOCK, *PPH_RUNDOWN_WAIT_BLOCK; PHLIBAPI VOID FASTCALL PhfInitializeRundownProtection( _Out_ PPH_RUNDOWN_PROTECT Protection ); PHLIBAPI BOOLEAN FASTCALL PhfAcquireRundownProtection( _Inout_ PPH_RUNDOWN_PROTECT Protection ); PHLIBAPI VOID FASTCALL PhfReleaseRundownProtection( _Inout_ PPH_RUNDOWN_PROTECT Protection ); PHLIBAPI VOID FASTCALL PhfWaitForRundownProtection( _Inout_ PPH_RUNDOWN_PROTECT Protection ); /** * Initializes a rundown protection object. * * \param Protection A pointer to a rundown protection object. */ FORCEINLINE VOID PhInitializeRundownProtection( _Out_ PPH_RUNDOWN_PROTECT Protection ) { Protection->Value = 0; // PhfInitializeRundownProtection(Protection); } /** * Attempts to acquire rundown protection. * * \param Protection A rundown protection object. * \return TRUE if rundown protection was acquired, otherwise FALSE. */ FORCEINLINE BOOLEAN PhAcquireRundownProtection( _Inout_ PPH_RUNDOWN_PROTECT Protection ) { ULONG_PTR value; value = ReadULongPtrAcquire(&Protection->Value) & ~PH_RUNDOWN_ACTIVE; // fail fast path when rundown is active if ((ULONG_PTR)_InterlockedCompareExchangePointer( (PVOID *)&Protection->Value, (PVOID)(value + PH_RUNDOWN_REF_INC), (PVOID)value ) == value) { return TRUE; } else { return PhfAcquireRundownProtection(Protection); } } /** * Releases rundown protection. * * \param Protection A rundown protection object. */ FORCEINLINE VOID PhReleaseRundownProtection( _Inout_ PPH_RUNDOWN_PROTECT Protection ) { ULONG_PTR value; value = ReadULongPtrAcquire(&Protection->Value) & ~PH_RUNDOWN_ACTIVE; // Fail fast path when rundown is active if ((ULONG_PTR)_InterlockedCompareExchangePointer( (PVOID *)&Protection->Value, (PVOID)(value - PH_RUNDOWN_REF_INC), (PVOID)value ) != value) { PhfReleaseRundownProtection(Protection); } } /** * Starts rundown and waits for all protected users to finish. * * \param Protection A rundown protection object. */ FORCEINLINE VOID PhWaitForRundownProtection( _Inout_ PPH_RUNDOWN_PROTECT Protection ) { ULONG_PTR value; value = (ULONG_PTR)_InterlockedCompareExchangePointer( (PVOID *)&Protection->Value, (PVOID)PH_RUNDOWN_ACTIVE, (PVOID)0 ); if (value != 0 && value != PH_RUNDOWN_ACTIVE) PhfWaitForRundownProtection(Protection); } // // One-time initialization // #define PH_INITONCE_SHIFT 31 #define PH_INITONCE_INITIALIZING (0x1 << PH_INITONCE_SHIFT) #define PH_INITONCE_INITIALIZING_SHIFT PH_INITONCE_SHIFT typedef struct _PH_INITONCE { PH_EVENT Event; } PH_INITONCE, *PPH_INITONCE; C_ASSERT(PH_INITONCE_SHIFT >= FIELD_OFFSET(PH_EVENT, AvailableForUse) * 8); #define PH_INITONCE_INIT { PH_EVENT_INIT } #define PhInitializeInitOnce PhfInitializeInitOnce PHLIBAPI VOID FASTCALL PhfInitializeInitOnce( _Out_ PPH_INITONCE InitOnce ); PHLIBAPI BOOLEAN FASTCALL PhfBeginInitOnce( _Inout_ PPH_INITONCE InitOnce ); #define PhEndInitOnce PhfEndInitOnce PHLIBAPI VOID FASTCALL PhfEndInitOnce( _Inout_ PPH_INITONCE InitOnce ); /** * Begins one-time initialization. * * \param InitOnce An init-once object. * \return TRUE if the caller should perform initialization, otherwise FALSE. */ FORCEINLINE BOOLEAN PhBeginInitOnce( _Inout_ PPH_INITONCE InitOnce ) { if (PhTestEvent(&InitOnce->Event)) return FALSE; else return PhfBeginInitOnce(InitOnce); } FORCEINLINE BOOLEAN PhTestInitOnce( _In_ PPH_INITONCE InitOnce ) { return PhTestEvent(&InitOnce->Event); } // // String // PHLIBAPI SIZE_T NTAPI PhCountStringZ( _In_ PCWSTR PH_RESTRICT String ); /** * Computes the length of a null-terminated ANSI/UTF-8 string in bytes. * * \param String A pointer to a null-terminated ANSI string. * \return The length of the string in bytes, excluding the null terminator. */ FORCEINLINE SIZE_T PhCountBytesZ( _In_ PCSTR String ) { return (SIZE_T)strlen(String); } _Ret_notnull_ _Post_z_ PHLIBAPI PSTR NTAPI PhDuplicateBytesZ( _In_ PCSTR String ); _Ret_maybenull_ _Post_z_ PHLIBAPI PSTR NTAPI PhDuplicateBytesZSafe( _In_ PCSTR String ); _Ret_notnull_ _Post_z_ PHLIBAPI PWSTR NTAPI PhDuplicateStringZ( _In_ PCWSTR String ); PHLIBAPI NTSTATUS NTAPI PhCopyBytesZ( _In_ PCSTR PH_RESTRICT InputBuffer, _In_ SIZE_T InputCount, _Out_writes_opt_z_(OutputCount) PSTR PH_RESTRICT OutputBuffer, _In_ SIZE_T OutputCount, _Out_opt_ PSIZE_T ReturnCount ); PHLIBAPI NTSTATUS NTAPI PhCopyStringZ( _In_ PCWSTR PH_RESTRICT InputBuffer, _In_ SIZE_T InputCount, _Out_writes_opt_z_(OutputCount) PWSTR PH_RESTRICT OutputBuffer, _In_ SIZE_T OutputCount, _Out_opt_ PSIZE_T ReturnCount ); PHLIBAPI NTSTATUS NTAPI PhCopyStringZFromBytes( _In_ PCSTR InputBuffer, _In_ SIZE_T InputCount, _Out_writes_opt_z_(OutputCount) PWSTR OutputBuffer, _In_ SIZE_T OutputCount, _Out_opt_ PSIZE_T ReturnCount ); PHLIBAPI NTSTATUS NTAPI PhCopyStringZFromMultiByte( _In_ PCSTR InputBuffer, _In_ SIZE_T InputCount, _Out_writes_opt_z_(OutputCount) PWSTR OutputBuffer, _In_ SIZE_T OutputCount, _Out_opt_ PSIZE_T ReturnCount ); PHLIBAPI NTSTATUS NTAPI PhCopyStringZFromUtf8( _In_ PCSTR InputBuffer, _In_ SIZE_T InputCount, _Out_writes_opt_z_(OutputCount) PWSTR OutputBuffer, _In_ SIZE_T OutputCount, _Out_opt_ PSIZE_T ReturnCount ); FORCEINLINE BOOLEAN PhAreCharactersDifferent( _In_ WCHAR Char1, _In_ WCHAR Char2 ) { WCHAR d; d = Char1 ^ Char2; // We ignore bits beyond bit 5 because bit 6 is the case bit, and also we // don't support localization here. if (d & 0x1f) return TRUE; return FALSE; } FORCEINLINE BOOLEAN PhIsDigitCharacter( _In_ WCHAR Char ) { return (USHORT)(Char - '0') < 10; } FORCEINLINE WCHAR NTAPI_INLINE PhUpcaseUnicodeChar( _In_ WCHAR SourceCharacter ) { WCHAR c; //c = towupper(c); //c = __ascii_towupper(c); c = RtlUpcaseUnicodeChar(SourceCharacter); return c; } FORCEINLINE WCHAR NTAPI_INLINE PhDowncaseUnicodeChar( _In_ WCHAR SourceCharacter ) { WCHAR c; c = RtlDowncaseUnicodeChar(SourceCharacter); return c; } /** * Tests if a character is whitespace. * * \param c The character to test. * \return TRUE if the character is whitespace (space, tab, CR, LF, etc.); otherwise, FALSE. */ FORCEINLINE BOOLEAN PhIsWhiteSpaceUnicodeChar( _In_ WCHAR SourceCharacter ) { return ( SourceCharacter == L' ' || SourceCharacter == L'\t' || SourceCharacter == L'\r' || SourceCharacter == L'\n' || SourceCharacter == L'\v' || SourceCharacter == L'\f' ); } /** * Tests if a character is a unicode control or formatting character. * * Detects bidirectional overrides (RTLO, etc.), zero-width characters, and * control characters (including tab, CR, LF). * * \param c The character to test. * \return TRUE if the character is a control/formatting character; otherwise, FALSE. */ FORCEINLINE BOOLEAN PhIsControlOrFormattingUnicodeChar( _In_ WCHAR c ) { // C0 and C1 control characters if (c < 0x20 || (c >= 0x7F && c <= 0x9F)) // TAB, CR, LF return TRUE; // Bidirectional format characters if (c >= 0x202A && c <= 0x202E) return TRUE; // Direction mark characters if (c == 0x200E || c == 0x200F) return TRUE; // Zero-width and invisible characters if (c >= 0x200B && c <= 0x200D) // ZWSP, ZWNJ, ZWJ return TRUE; // Zero-width no-break space (BOM) if (c == 0xFEFF) return TRUE; return FALSE; } FORCEINLINE LONG PhCompareBytesZ( _In_ PCSTR String1, _In_ PCSTR String2, _In_ BOOLEAN IgnoreCase ) { if (IgnoreCase) return _stricmp(String1, String2); else return strcmp(String1, String2); } FORCEINLINE BOOLEAN PhEqualBytesZ( _In_ PCSTR String1, _In_ PCSTR String2, _In_ BOOLEAN IgnoreCase ) { if (IgnoreCase) return _stricmp(String1, String2) == 0; else return strcmp(String1, String2) == 0; } FORCEINLINE BOOLEAN PhEqualStringZ( _In_ PCWSTR String1, _In_ PCWSTR String2, _In_ BOOLEAN IgnoreCase ) { if (IgnoreCase) { // wcsicmp is very expensive, so we do a quick check for negatives first. if (PhAreCharactersDifferent(String1[0], String2[0])) return FALSE; return _wcsicmp(String1, String2) == 0; } else { return wcscmp(String1, String2) == 0; } } typedef struct _PH_STRINGREF { /** The length, in bytes, of the string. */ SIZE_T Length; /** The buffer containing the contents of the string. */ PWCH Buffer; } PH_STRINGREF, *PPH_STRINGREF; typedef const PH_STRINGREF* PCPH_STRINGREF; typedef struct _PH_BYTESREF { /** The length, in bytes, of the string. */ SIZE_T Length; /** The buffer containing the contents of the string. */ PCH Buffer; } PH_BYTESREF, *PPH_BYTESREF; typedef const PH_BYTESREF* PCPH_BYTESREF; typedef struct _PH_RELATIVE_BYTESREF { /** The length, in bytes, of the string. */ ULONG Length; /** A user-defined offset. */ ULONG Offset; } PH_RELATIVE_BYTESREF, *PPH_RELATIVE_BYTESREF, PH_RELATIVE_STRINGREF, *PPH_RELATIVE_STRINGREF; typedef const PH_RELATIVE_BYTESREF* PCPH_RELATIVE_BYTESREF, *PCPH_RELATIVE_STRINGREF; #define PH_STRINGREF_INIT(String) { sizeof(String) - sizeof(UNICODE_NULL), RTL_CONST_CAST(PWCH)(String) } #define PH_BYTESREF_INIT(String) { sizeof(String) - sizeof(ANSI_NULL), RTL_CONST_CAST(PCH)(String) } #define PH_RELATIVE_STRINGREF_INIT(String) { (ULONG)(sizeof(String) - sizeof(UNICODE_NULL)), (ULONG)((ULONG_PTR)(String) - (ULONG_PTR)NtCurrentImageBase()) } /** * Initializes a string reference from a null-terminated Unicode string. * * \param String A pointer to the string reference to initialize. * \param Buffer A null-terminated Unicode string. */ FORCEINLINE VOID PhInitializeStringRef( _Out_ PPH_STRINGREF String, _In_ PCWSTR Buffer ) { String->Length = wcslen(Buffer) * sizeof(WCHAR); String->Buffer = (PWCH)Buffer; } /** * Initializes a string reference using the long-string count helper. * * \param String A pointer to the string reference to initialize. * \param Buffer A null-terminated Unicode string. */ FORCEINLINE VOID PhInitializeStringRefLongHint( _Out_ PPH_STRINGREF String, _In_ PCWSTR Buffer ) { String->Length = PhCountStringZ(Buffer) * sizeof(WCHAR); String->Buffer = (PWCH)Buffer; } /** * Initializes a byte-string reference from a null-terminated ANSI string. * * \param Bytes A pointer to the byte-string reference to initialize. * \param Buffer A null-terminated ANSI string. */ FORCEINLINE VOID PhInitializeBytesRef( _Out_ PPH_BYTESREF Bytes, _In_ PCSTR Buffer ) { Bytes->Length = strlen(Buffer) * sizeof(CHAR); Bytes->Buffer = (PCH)Buffer; } /** * Initializes a string reference to an empty value. * * \param String A pointer to the string reference to initialize. */ FORCEINLINE VOID PhInitializeEmptyStringRef( _Out_ PPH_STRINGREF String ) { String->Length = 0; String->Buffer = NULL; } /** * Initializes a byte-string reference to an empty value. * * \param String A pointer to the byte-string reference to initialize. */ FORCEINLINE VOID PhInitializeEmptyBytesRef( _Out_ PPH_BYTESREF String ) { String->Length = 0; String->Buffer = NULL; } /** * Initializes a string reference from a caller-supplied buffer and length. * * \param String A pointer to the string reference to initialize. * \param Buffer The string buffer. * \param Length The length of the string, in bytes. */ FORCEINLINE VOID NTAPI PhInitializeBufferStringRef( _Out_ PPH_STRINGREF String, _Writable_bytes_(Length) _When_(Length != 0, _Notnull_) PWCH Buffer, _In_ SIZE_T Length ) { memset(String, 0, sizeof(PH_STRINGREF)); String->Length = Length; String->Buffer = Buffer; } /** * Initializes a byte-string reference from a caller-supplied buffer and length. * * \param String A pointer to the byte-string reference to initialize. * \param Buffer The byte buffer. * \param Length The length of the byte string, in bytes. */ FORCEINLINE VOID NTAPI PhInitializeBufferBytesRef( _Out_ PPH_BYTESREF String, _Writable_bytes_(Length) _When_(Length != 0, _Notnull_) PCH Buffer, _In_ SIZE_T Length ) { memset(String, 0, sizeof(PH_BYTESREF)); String->Length = Length; String->Buffer = Buffer; } /** * Initializes a string reference from an image-relative string reference. * * \param String A pointer to the string reference to initialize. * \param RelativeString An image-relative string reference initialized with PH_RELATIVE_STRINGREF_INIT. */ FORCEINLINE VOID PhInitializeStringRefFromRelative( _Out_ PPH_STRINGREF String, _In_ PCPH_RELATIVE_STRINGREF RelativeString ) { String->Length = RelativeString->Length; String->Buffer = (PWCH)PTR_ADD_OFFSET(NtCurrentImageBase(), RelativeString->Offset); } /** * Converts a string reference to a UNICODE_STRING view. * * \param String A pointer to the source string reference. * \param UnicodeString A pointer to the destination UNICODE_STRING. * \return TRUE if the source length fits in UNICODE_STRING length fields; otherwise, FALSE. */ FORCEINLINE BOOLEAN PhStringRefToUnicodeString( _In_ PCPH_STRINGREF String, _Out_ PUNICODE_STRING UnicodeString ) { memset(UnicodeString, 0, sizeof(UNICODE_STRING)); UnicodeString->Length = (USHORT)String->Length; UnicodeString->MaximumLength = (USHORT)String->Length + sizeof(UNICODE_NULL); UnicodeString->Buffer = String->Buffer; return String->Length <= UNICODE_STRING_MAX_BYTES; } /** * Initializes a string reference from a UNICODE_STRING. * * \param UnicodeString A pointer to the source UNICODE_STRING. * \param String A pointer to the destination string reference. */ FORCEINLINE VOID PhUnicodeStringToStringRef( _In_ PCUNICODE_STRING UnicodeString, _Out_ PPH_STRINGREF String ) { memset(String, 0, sizeof(PH_STRINGREF)); String->Length = UnicodeString->Length; String->Buffer = UnicodeString->Buffer; } FORCEINLINE LONG PhCompareStringZ( _In_ PCWSTR String1, _In_ PCWSTR String2, _In_ BOOLEAN IgnoreCase ) { if (IgnoreCase) return _wcsicmp(String1, String2); else return wcscmp(String1, String2); } PHLIBAPI LONG NTAPI PhCompareStringZNatural( _In_ PCWSTR A, _In_ PCWSTR B, _In_ BOOLEAN IgnoreCase ); PHLIBAPI LONG NTAPI PhCompareStringRef( _In_ PCPH_STRINGREF PH_RESTRICT String1, _In_ PCPH_STRINGREF PH_RESTRICT String2, _In_ BOOLEAN IgnoreCase ); PHLIBAPI BOOLEAN NTAPI PhEqualStringRef( _In_ PCPH_STRINGREF PH_RESTRICT String1, _In_ PCPH_STRINGREF PH_RESTRICT String2, _In_ BOOLEAN IgnoreCase ); PHLIBAPI ULONG_PTR NTAPI PhFindCharInStringRef( _In_ PCPH_STRINGREF String, _In_ WCHAR Character, _In_ BOOLEAN IgnoreCase ); PHLIBAPI SIZE_T NTAPI PhFindFirstOfCharsW( _In_reads_(Length) PCWCH Buffer, _In_ SIZE_T Length, _In_reads_(Count) PCWCH Chars, _In_ ULONG Count ); PHLIBAPI ULONG_PTR NTAPI PhFindLastCharInStringRef( _In_ PCPH_STRINGREF String, _In_ WCHAR Character, _In_ BOOLEAN IgnoreCase ); PHLIBAPI ULONG_PTR NTAPI PhFindStringInStringRef( _In_ PCPH_STRINGREF String, _In_ PCPH_STRINGREF SubString, _In_ BOOLEAN IgnoreCase ); FORCEINLINE ULONG_PTR PhFindStringInStringRefZ( _In_ PCPH_STRINGREF String, _In_ PCWSTR SubString, _In_ BOOLEAN IgnoreCase ) { PH_STRINGREF sr2; PhInitializeStringRef(&sr2, SubString); return PhFindStringInStringRef(String, &sr2, IgnoreCase); } PHLIBAPI BOOLEAN NTAPI PhSplitStringRefAtChar( _In_ PCPH_STRINGREF Input, _In_ WCHAR Separator, _Out_ PPH_STRINGREF FirstPart, _Out_ PPH_STRINGREF SecondPart ); PHLIBAPI BOOLEAN NTAPI PhSplitStringRefAtLastChar( _In_ PCPH_STRINGREF Input, _In_ WCHAR Separator, _Out_ PPH_STRINGREF FirstPart, _Out_ PPH_STRINGREF SecondPart ); PHLIBAPI BOOLEAN NTAPI PhSplitStringRefAtString( _In_ PCPH_STRINGREF Input, _In_ PCPH_STRINGREF Separator, _In_ BOOLEAN IgnoreCase, _Out_ PPH_STRINGREF FirstPart, _Out_ PPH_STRINGREF SecondPart ); #define PH_SPLIT_AT_CHAR_SET 0x0 // default #define PH_SPLIT_AT_STRING 0x1 #define PH_SPLIT_AT_RANGE 0x2 #define PH_SPLIT_CASE_INSENSITIVE 0x1000 #define PH_SPLIT_COMPLEMENT_CHAR_SET 0x2000 #define PH_SPLIT_START_AT_END 0x4000 #define PH_SPLIT_CHAR_SET_IS_UPPERCASE 0x8000 PHLIBAPI BOOLEAN NTAPI PhSplitStringRefEx( _In_ PCPH_STRINGREF Input, _In_ PCPH_STRINGREF Separator, _In_ ULONG Flags, _Out_ PPH_STRINGREF FirstPart, _Out_ PPH_STRINGREF SecondPart, _Out_opt_ PPH_STRINGREF SeparatorPart ); PHLIBAPI VOID NTAPI PhSplitStringRef( _In_ PCPH_STRINGREF Input, _In_ WCHAR Separator, _Out_ PVOID **Strings, _Out_ PULONG NumberOfStrings ); PHLIBAPI VOID NTAPI PhFreeStringArray( _In_ PVOID *Strings, _In_ ULONG NumberOfStrings ); #define PH_TRIM_START_ONLY 0x1 #define PH_TRIM_END_ONLY 0x2 PHLIBAPI VOID NTAPI PhTrimStringRef( _Inout_ PPH_STRINGREF String, _In_ PCPH_STRINGREF CharSet, _In_ ULONG Flags ); FORCEINLINE LONG PhCompareStringRef2( _In_ PCPH_STRINGREF String1, _In_ PCWSTR String2, _In_ BOOLEAN IgnoreCase ) { PH_STRINGREF sr2; PhInitializeStringRef(&sr2, String2); return PhCompareStringRef(String1, &sr2, IgnoreCase); } FORCEINLINE BOOLEAN PhEqualStringRef2( _In_ PCPH_STRINGREF String1, _In_ PCWSTR String2, _In_ BOOLEAN IgnoreCase ) { PH_STRINGREF sr2; PhInitializeStringRef(&sr2, String2); return PhEqualStringRef(String1, &sr2, IgnoreCase); } FORCEINLINE BOOLEAN PhStartsWithStringRef( _In_ PCPH_STRINGREF String, _In_ PCPH_STRINGREF Prefix, _In_ BOOLEAN IgnoreCase ) { PH_STRINGREF sr; sr.Buffer = String->Buffer; sr.Length = Prefix->Length; if (String->Length < sr.Length) return FALSE; return PhEqualStringRef(&sr, Prefix, IgnoreCase); } FORCEINLINE BOOLEAN PhStartsWithStringRef2( _In_ PCPH_STRINGREF String, _In_ PCWSTR Prefix, _In_ BOOLEAN IgnoreCase ) { PH_STRINGREF prefix; PhInitializeStringRef(&prefix, Prefix); return PhStartsWithStringRef(String, &prefix, IgnoreCase); } FORCEINLINE BOOLEAN PhEndsWithStringRef( _In_ PCPH_STRINGREF String, _In_ PCPH_STRINGREF Suffix, _In_ BOOLEAN IgnoreCase ) { PH_STRINGREF sr; if (Suffix->Length > String->Length) return FALSE; sr.Buffer = (PWCH)PTR_ADD_OFFSET(String->Buffer, String->Length - Suffix->Length); sr.Length = Suffix->Length; return PhEqualStringRef(&sr, Suffix, IgnoreCase); } FORCEINLINE BOOLEAN PhEndsWithStringRef2( _In_ PCPH_STRINGREF String, _In_ PCWSTR Suffix, _In_ BOOLEAN IgnoreCase ) { PH_STRINGREF suffix; PhInitializeStringRef(&suffix, Suffix); return PhEndsWithStringRef(String, &suffix, IgnoreCase); } FORCEINLINE VOID PhSkipStringRef( _Inout_ PPH_STRINGREF String, _In_ SIZE_T Length ) { String->Buffer = (PWCH)PTR_ADD_OFFSET(String->Buffer, Length); String->Length -= Length; } FORCEINLINE VOID PhReverseStringRef( _In_ PCPH_STRINGREF String ) { SIZE_T i; SIZE_T j; WCHAR t; for (i = 0, j = String->Length / sizeof(WCHAR) - 1; i <= j; i++, j--) { t = String->Buffer[i]; String->Buffer[i] = String->Buffer[j]; String->Buffer[j] = t; } } extern PPH_OBJECT_TYPE PhStringType; /** * A 16-bit string object, which supports UTF-16. * * \remarks The \a Length never includes the null terminator. Every string must have a null * terminator at the end, for compatibility reasons. The invariant is: * \code Buffer[Length / sizeof(WCHAR)] = 0 \endcode */ typedef struct _PH_STRING { // Header union { PH_STRINGREF sr; struct { /** The length, in bytes, of the string. */ SIZE_T Length; /** The buffer containing the contents of the string. */ PWCH Buffer; }; }; // Data union { WCHAR Data[1]; struct { /** Reserved. */ ULONG AllocationFlags; /** Reserved. */ PVOID Allocation; }; }; } PH_STRING, *PPH_STRING; extern PPH_STRING PhSharedEmptyString; _Ret_notnull_ PHLIBAPI PPH_STRING NTAPI PhCreateStringEx( _In_reads_bytes_opt_(Length) PCWCHAR Buffer, _In_ SIZE_T Length ); /** * Creates a string object from an existing null-terminated string. * * \param Buffer A null-terminated Unicode string. */ FORCEINLINE PPH_STRING NTAPI PhCreateString( _In_ PCWSTR Buffer ) { return PhCreateStringEx(Buffer, PhCountStringZ(Buffer) * sizeof(WCHAR)); } PHLIBAPI PPH_STRING NTAPI PhReferenceEmptyString( VOID ); FORCEINLINE PPH_STRING NTAPI_INLINE PhCreateString2( _In_ PCPH_STRINGREF String ) { if (String->Length == 0) return PhReferenceEmptyString(); return PhCreateStringEx(String->Buffer, String->Length); } /** * Creates a string object from a null-terminated string. * * \param String A null-terminated Unicode string. */ FORCEINLINE PPH_STRING NTAPI_INLINE PhCreateStringZ( _In_z_ PCWSTR String ) { PH_STRINGREF string; string.Length = wcslen(String) * sizeof(WCHAR); string.Buffer = (PWSTR)String; //PhInitializeStringRef(&string, (PWSTR)String); return PhCreateString2(&string); } /** * Creates a string object from a null-terminated string up to a maximum length. * * \param String A null-terminated Unicode string. * \param MaximumLength The maximum length, in bytes, of the string. */ FORCEINLINE PPH_STRING NTAPI_INLINE PhCreateStringZ2( _In_reads_or_z_(MaximumLength / sizeof(WCHAR)) PCWSTR String, _In_ SIZE_T MaximumLength ) { PH_STRINGREF string; string.Length = wcsnlen(String, MaximumLength / sizeof(WCHAR)) * sizeof(WCHAR); string.Buffer = (PWSTR)String; return PhCreateString2(&string); } #define PH_STRING_TRIM_START_ONLY PH_TRIM_START_ONLY #define PH_STRING_TRIM_END_ONLY PH_TRIM_END_ONLY #define PH_STRING_TRIM_MASK (PH_STRING_TRIM_START_ONLY | PH_STRING_TRIM_END_ONLY) #define PH_STRING_UPPER_CASE 0x4 #define PH_STRING_LOWER_CASE 0x8 #define PH_STRING_CASE_MASK (PH_STRING_UPPER_CASE | PH_STRING_LOWER_CASE) _Ret_notnull_ PHLIBAPI PPH_STRING NTAPI PhCreateString3( _In_ PCPH_STRINGREF String, _In_ ULONG Flags, _In_opt_ PCPH_STRINGREF TrimCharSet ); FORCEINLINE PPH_STRING NTAPI_INLINE PhTrimStringZ( _In_ PCPH_STRINGREF String, _In_ ULONG Flags, _In_ PCWSTR TrimCharSet ) { PH_STRINGREF string; PhInitializeStringRef(&string, TrimCharSet); return PhCreateString3(String, Flags, &string); } FORCEINLINE VOID NTAPI_INLINE PhUpperStringRefInto( _Inout_ PPH_STRINGREF Destination, _In_ PCPH_STRINGREF Source ) { assert(Destination->Length >= Source->Length); SIZE_T numberOfChars = Source->Length / sizeof(WCHAR); for (SIZE_T i = 0; i < numberOfChars; i++) Destination->Buffer[i] = PhUpcaseUnicodeChar(Source->Buffer[i]); Destination->Length = Source->Length; } FORCEINLINE VOID NTAPI_INLINE PhUpperStringRef( _Inout_ PPH_STRINGREF String ) { PhUpperStringRefInto(String, String); } FORCEINLINE VOID NTAPI_INLINE PhLowerStringRefInto( _Inout_ PPH_STRINGREF Destination, _In_ PCPH_STRINGREF Source ) { assert(Destination->Length >= Source->Length); SIZE_T numberOfChars = Source->Length / sizeof(WCHAR); for (SIZE_T i = 0; i < numberOfChars; i++) Destination->Buffer[i] = PhDowncaseUnicodeChar(Source->Buffer[i]); Destination->Length = Source->Length; } FORCEINLINE VOID NTAPI_INLINE PhLowerStringRef( _Inout_ PPH_STRINGREF String ) { PhLowerStringRefInto(String, String); } FORCEINLINE VOID NTAPI_INLINE PhLowerStringZ( _Inout_ PCWSTR String ) { PH_STRINGREF string; PhInitializeStringRef(&string, String); PhLowerStringRefInto(&string, &string); } FORCEINLINE VOID NTAPI_INLINE PhLowerStringMaxZ( _In_reads_or_z_(MaximumLength / sizeof(WCHAR)) PCWSTR String, _In_ SIZE_T MaximumLength ) { PH_STRINGREF string; string.Length = wcsnlen(String, MaximumLength / sizeof(WCHAR)) * sizeof(WCHAR); string.Buffer = (PWSTR)String; PhLowerStringRefInto(&string, &string); } FORCEINLINE PPH_STRING NTAPI_INLINE PhCreateStringFromUnicodeString( _In_ PCUNICODE_STRING UnicodeString ) { if (UnicodeString->Length == 0) return PhReferenceEmptyString(); return PhCreateStringEx(UnicodeString->Buffer, UnicodeString->Length); } _Ret_notnull_ PHLIBAPI PPH_STRING NTAPI PhConcatStrings( _In_ ULONG Count, ... ); #define PH_CONCAT_STRINGS_LENGTH_CACHE_SIZE 16 _Ret_notnull_ PHLIBAPI PPH_STRING NTAPI PhConcatStrings_V( _In_ ULONG Count, _In_ va_list ArgPtr ); _Ret_notnull_ PHLIBAPI PPH_STRING NTAPI PhConcatStrings2( _In_ PCWSTR String1, _In_ PCWSTR String2 ); _Ret_notnull_ PHLIBAPI PPH_STRING NTAPI PhConcatStringRef2( _In_ PCPH_STRINGREF String1, _In_ PCPH_STRINGREF String2 ); _Ret_notnull_ PHLIBAPI PPH_STRING NTAPI PhConcatStringRef3( _In_ PCPH_STRINGREF String1, _In_ PCPH_STRINGREF String2, _In_ PCPH_STRINGREF String3 ); _Ret_notnull_ PHLIBAPI PPH_STRING NTAPI PhConcatStringRef4( _In_ PCPH_STRINGREF String1, _In_ PCPH_STRINGREF String2, _In_ PCPH_STRINGREF String3, _In_ PCPH_STRINGREF String4 ); FORCEINLINE PPH_STRING PhConcatStringRefZ( _In_ PCPH_STRINGREF String1, _In_ PCWSTR String2 ) { PH_STRINGREF string; PhInitializeStringRef(&string, String2); return PhConcatStringRef2(String1, &string); } _Ret_maybenull_ PHLIBAPI PPH_STRING NTAPI PhFormatString( _In_ _Printf_format_string_ PCWSTR Format, ... ); _Ret_maybenull_ PHLIBAPI PPH_STRING NTAPI PhFormatString_V( _In_ _Printf_format_string_ PCWSTR Format, _In_ va_list ArgPtr ); /** * Retrieves a pointer to a string object's buffer or returns NULL. * * \param String A pointer to a string object. * * \return A pointer to the string object's buffer if the supplied pointer is non-NULL, otherwise * NULL. */ FORCEINLINE PWSTR PhGetString( _In_opt_ PPH_STRING String ) { if (String) return String->Buffer; else return NULL; } /** * Retrieves a string reference from a given string object. * * This function returns a `PH_STRINGREF` structure that references the string * contained in the provided `PPH_STRING` object. If the input string object is * `NULL`, it initializes and returns an empty `PH_STRINGREF`. * * \param String A pointer to a `PPH_STRING` object. This parameter is optional and can be `NULL`. * \return A `PH_STRINGREF` structure that references the string in the provided `PPH_STRING` object, * or an empty `PH_STRINGREF` if the input is `NULL`. */ FORCEINLINE PH_STRINGREF PhGetStringRef( _In_opt_ PPH_STRING String ) { PH_STRINGREF sr; if (String) sr = String->sr; else PhInitializeEmptyStringRef(&sr); return sr; } /** * Retrieves the buffer from a PPH_STRINGREF structure or returns an empty string if the input is NULL. * * This function checks if the provided PPH_STRINGREF structure is not NULL. If it is not NULL, it returns the buffer * contained within the structure. If the structure is NULL, it returns an empty string. * * \param String A pointer to a PPH_STRINGREF structure. This parameter is optional and can be NULL. * \return A pointer to the buffer contained within the PPH_STRINGREF structure if it is not NULL, otherwise an empty string. */ FORCEINLINE PCWSTR PhGetStringRefZ( _In_opt_ PCPH_STRINGREF String ) { if (String) return String->Buffer; else return L""; } /** * Retrieves a pointer to a string object's buffer or returns an empty string. * * \param String A pointer to a string object. * \return A pointer to the string object's buffer if the supplied pointer is non-NULL, otherwise an empty string. */ FORCEINLINE PCWSTR PhGetStringOrEmpty( _In_opt_ PPH_STRING String ) { if (String) return String->Buffer; else return L""; } /** * Retrieves a pointer to a string object's buffer or returns the specified alternative string. * * \param String A pointer to a string object. * \param DefaultString The alternative string. * * \return A pointer to the string object's buffer if the supplied pointer is non-NULL, otherwise * the specified alternative string. */ FORCEINLINE PCWSTR PhGetStringOrDefault( _In_opt_ PPH_STRING String, _In_ PCWSTR DefaultString ) { if (String) return String->Buffer; else return DefaultString; } /** * Determines whether a string is null or empty. * * \param String A pointer to a string object. */ _Success_(return == FALSE) _At_(String, _When_(return == FALSE, _Notnull_)) FORCEINLINE BOOLEAN PhIsNullOrEmptyString( _In_opt_ PPH_STRING String ) { if (String == NULL) return TRUE; return String->Length == 0; } /** * Determines whether a STRINGREF is null or empty. * * \param String A pointer to a string object. */ _Success_(return == FALSE) _At_(String, _When_(return == FALSE, _Notnull_)) FORCEINLINE BOOLEAN PhIsNullOrEmptyStringRef( _In_opt_ PCPH_STRINGREF String ) { return !(String && String->Length); } /** * Tests if a string is null, empty, or starts with whitespace. * * \param String The string to test. * \return TRUE if the string is null, empty, or starts with whitespace; otherwise, FALSE. */ #undef PhIsNullOrWhitespaceString #define PhIsNullOrWhitespaceString(string) \ ((!(string)) || ((string)->Length == 0) || PhIsWhiteSpaceUnicodeChar((string)->Buffer[0])) // FORCEINLINE // BOOLEAN // PhIsNullOrWhitespaceString( // _In_opt_ PPH_STRING String // ) // { // SIZE_T length; // // if (!String || String->Length == 0) // return TRUE; // // length = String->Length / sizeof(WCHAR); // // // Check first and last character (leading/trailing whitespace) // if (PhIsWhiteSpaceUnicodeChar(String->Buffer[0]) || // PhIsWhiteSpaceUnicodeChar(String->Buffer[length - 1])) // { // return TRUE; // } // // return FALSE; // } // The MSVC static analyzer can misinterpret the _In_opt_ SAL for String on PhIsNullOrEmptyString and raise C6387. // Provide a macro override during analysis to avoid the false positive while preserving semantics. #if (defined(_PREFAST_) || defined(_MSC_ANALYSIS)) #undef PhIsNullOrEmptyString #define PhIsNullOrEmptyString(string) \ ((!(string)) || ((string)->Length == 0)) #undef PhIsNullOrEmptyStringRef #define PhIsNullOrEmptyStringRef(string) \ ((!(string)) || ((string)->Length == 0)) #endif /** * Duplicates a string. * * \param String A string to duplicate. */ FORCEINLINE PPH_STRING PhDuplicateString( _In_ PPH_STRING String ) { return PhCreateStringEx(String->Buffer, String->Length); } /** * Compares two strings. * * \param String1 The first string. * \param String2 The second string. * \param IgnoreCase Whether to ignore character cases. */ FORCEINLINE LONG PhCompareString( _In_ PPH_STRING String1, _In_ PPH_STRING String2, _In_ BOOLEAN IgnoreCase ) { if (IgnoreCase) return PhCompareStringRef(&String1->sr, &String2->sr, IgnoreCase); // faster than wcsicmp else return wcscmp(String1->Buffer, String2->Buffer); } /** * Compares two strings. * * \param String1 The first string. * \param String2 The second string. * \param IgnoreCase Whether to ignore character cases. */ FORCEINLINE LONG PhCompareString2( _In_ PPH_STRING String1, _In_ PCWSTR String2, _In_ BOOLEAN IgnoreCase ) { if (IgnoreCase) return PhCompareStringRef2(&String1->sr, String2, IgnoreCase); else return wcscmp(String1->Buffer, String2); } /** * Compares two strings, handling NULL strings. * * \param String1 The first string. * \param String2 The second string. * \param IgnoreCase Whether to ignore character cases. */ FORCEINLINE LONG PhCompareStringWithNull( _In_opt_ PPH_STRING String1, _In_opt_ PPH_STRING String2, _In_ BOOLEAN IgnoreCase ) { if (String1 && String2) { return PhCompareString(String1, String2, IgnoreCase); } else if (!String1) { return !String2 ? 0 : 1; } else { return -1; } } /** * Compares two strings with support for null values and customizable sort order. * * This function compares two strings, taking into account the possibility of null values and allowing for customizable sort order. * If both `String1` and `String2` are not null, the function calls `PhCompareString` to perform the comparison. * If `String1` is null and `String2` is not null, the function returns 0 if `String2` is also null, or 1 or -1 based on the specified `Order` (AscendingSortOrder or DescendingSortOrder). * If `String1` is not null and `String2` is null, the function returns -1 or 1 based on the specified `Order`. * * \param String1 The first string to compare. * \param String2 The second string to compare. * \param Order The sort order to use for the comparison (AscendingSortOrder or DescendingSortOrder). * \param IgnoreCase Specifies whether the comparison should be case-insensitive (TRUE) or case-sensitive (FALSE). * \return Returns a negative value if `String1` is less than `String2`, a positive value if `String1` is greater than `String2`, or 0 if they are equal. */ FORCEINLINE LONG PhCompareStringWithNullSortOrder( _In_opt_ PPH_STRING String1, _In_opt_ PPH_STRING String2, _In_ PH_SORT_ORDER Order, _In_ BOOLEAN IgnoreCase ) { if (String1 && String2) { return PhCompareString(String1, String2, IgnoreCase); } else if (!String1) { return !String2 ? 0 : (Order == AscendingSortOrder ? 1 : -1); } else { return (Order == AscendingSortOrder ? -1 : 1); } } /** * Compares two strings with support for null values and customizable sort order. * * This function compares two strings, taking into account the possibility of null values and allowing for customizable sort order. * If both `String1` and `String2` are not null, the function calls `PhCompareString` to perform the comparison. * If `String1` is null and `String2` is not null, the function returns 0 if `String2` is also null, or 1 if `String2` is not null, depending on the specified sort order. * If `String1` is not null and `String2` is null, the function returns -1 or 1, depending on the specified sort order. * * \param String1 The first string to compare. Can be null. * \param String2 The second string to compare. Can be null. * \param Order The sort order to use for the comparison. Must be either `AscendingSortOrder` or `DescendingSortOrder`. * \param IgnoreCase Specifies whether the comparison should be case-insensitive (`true`) or case-sensitive (`false`). * \return The result of the comparison. Returns a negative value if `String1` is less than `String2`, 0 if they are equal, or a positive value if `String1` is greater than `String2`. */ FORCEINLINE LONG PhCompareStringRefWithNullSortOrder( _In_opt_ PCPH_STRINGREF String1, _In_opt_ PCPH_STRINGREF String2, _In_ PH_SORT_ORDER Order, _In_ BOOLEAN IgnoreCase ) { if (String1 && String2) { return PhCompareStringRef(String1, String2, IgnoreCase); } else if (!String1) { return !String2 ? 0 : (Order == AscendingSortOrder ? 1 : -1); } else { return (Order == AscendingSortOrder ? -1 : 1); } } /** * Determines whether two strings are equal. * * \param String1 The first string. * \param String2 The second string. * \param IgnoreCase Whether to ignore character cases. */ FORCEINLINE BOOLEAN PhEqualString( _In_ PPH_STRING String1, _In_ PPH_STRING String2, _In_ BOOLEAN IgnoreCase ) { return PhEqualStringRef(&String1->sr, &String2->sr, IgnoreCase); } /** * Determines whether two strings are equal. * * \param String1 The first string. * \param String2 The second string. * \param IgnoreCase Whether to ignore character cases. */ FORCEINLINE BOOLEAN PhEqualString2( _In_ PPH_STRING String1, _In_ PCWSTR String2, _In_ BOOLEAN IgnoreCase ) { if (IgnoreCase) return PhEqualStringRef2(&String1->sr, String2, IgnoreCase); else return wcscmp(String1->Buffer, String2) == 0; } /** * Determines whether a string starts with another. * * \param String The first string. * \param Prefix The second string. * \param IgnoreCase Whether to ignore character cases. * * \return TRUE if \a String starts with \a Prefix, otherwise FALSE. */ FORCEINLINE BOOLEAN PhStartsWithString( _In_ PPH_STRING String, _In_ PPH_STRING Prefix, _In_ BOOLEAN IgnoreCase ) { return PhStartsWithStringRef(&String->sr, &Prefix->sr, IgnoreCase); } /** * Determines whether a string starts with another. * * \param String The first string. * \param Prefix The second string. * \param IgnoreCase Whether to ignore character cases. * * \return TRUE if \a String starts with \a Prefix, otherwise FALSE. */ FORCEINLINE BOOLEAN PhStartsWithString2( _In_ PPH_STRING String, _In_ PCWSTR Prefix, _In_ BOOLEAN IgnoreCase ) { PH_STRINGREF prefix; PhInitializeStringRef(&prefix, Prefix); return PhStartsWithStringRef(&String->sr, &prefix, IgnoreCase); } /** * Determines whether a string ends with another. * * \param String The first string. * \param Suffix The second string. * \param IgnoreCase Whether to ignore character cases. * * \return TRUE if \a String ends with \a Suffix, otherwise FALSE. */ FORCEINLINE BOOLEAN PhEndsWithString( _In_ PPH_STRING String, _In_ PPH_STRING Suffix, _In_ BOOLEAN IgnoreCase ) { return PhEndsWithStringRef(&String->sr, &Suffix->sr, IgnoreCase); } /** * Determines whether a string ends with another. * * \param String The first string. * \param Suffix The second string. * \param IgnoreCase Whether to ignore character cases. * * \return TRUE if \a String ends with \a Suffix, otherwise FALSE. */ FORCEINLINE BOOLEAN PhEndsWithString2( _In_ PPH_STRING String, _In_ PCWSTR Suffix, _In_ BOOLEAN IgnoreCase ) { PH_STRINGREF suffix; PhInitializeStringRef(&suffix, Suffix); return PhEndsWithStringRef(&String->sr, &suffix, IgnoreCase); } /** * Locates a character in a string. * * \param String The string to search. * \param StartIndex The index, in characters, to start searching at. * \param Char The character to search for. * * \return The index, in characters, of the first occurrence of \a Char in \a String after * \a StartIndex. If \a Char was not found, -1 is returned. */ FORCEINLINE ULONG_PTR PhFindCharInString( _In_ PPH_STRING String, _In_ SIZE_T StartIndex, _In_ WCHAR Char ) { if (StartIndex != 0) { ULONG_PTR r; PH_STRINGREF sr; sr = String->sr; PhSkipStringRef(&sr, StartIndex * sizeof(WCHAR)); r = PhFindCharInStringRef(&sr, Char, FALSE); if (r != SIZE_MAX) return r + StartIndex; else return SIZE_MAX; } else { return PhFindCharInStringRef(&String->sr, Char, FALSE); } } /** * Locates a character in a string, backwards. * * \param String The string to search. * \param StartIndex The index, in characters, to start searching at. * \param Char The character to search for. * * \return The index, in characters, of the last occurrence of \a Char in \a String after * \a StartIndex. If \a Char was not found, -1 is returned. */ FORCEINLINE ULONG_PTR PhFindLastCharInString( _In_ PPH_STRING String, _In_ SIZE_T StartIndex, _In_ WCHAR Char ) { if (StartIndex != 0) { ULONG_PTR r; PH_STRINGREF sr; sr = String->sr; PhSkipStringRef(&sr, StartIndex * sizeof(WCHAR)); r = PhFindLastCharInStringRef(&sr, Char, FALSE); if (r != SIZE_MAX) return r + StartIndex; else return SIZE_MAX; } else { return PhFindLastCharInStringRef(&String->sr, Char, FALSE); } } /** * Locates a string in a string. * * \param String The string to search. * \param StartIndex The index, in characters, to start searching at. * \param SubString The string to search for. * * \return The index, in characters, of the first occurrence of \a SubString in \a String after * \a StartIndex. If \a SubString was not found, -1 is returned. */ FORCEINLINE ULONG_PTR PhFindStringInString( _In_ PPH_STRING String, _In_ SIZE_T StartIndex, _In_ PCWSTR SubString ) { PH_STRINGREF sr2; PhInitializeStringRef(&sr2, SubString); if (StartIndex != 0) { ULONG_PTR r; PH_STRINGREF sr1; sr1 = String->sr; PhSkipStringRef(&sr1, StartIndex * sizeof(WCHAR)); r = PhFindStringInStringRef(&sr1, &sr2, FALSE); if (r != SIZE_MAX) return r + StartIndex; else return SIZE_MAX; } else { return PhFindStringInStringRef(&String->sr, &sr2, FALSE); } } /** * Creates a substring of a string. * * \param String The original string. * \param StartIndex The start index, in characters. * \param Count The number of characters to use. */ FORCEINLINE PPH_STRING PhSubstring( _In_ PPH_STRING String, _In_ SIZE_T StartIndex, _In_ SIZE_T Count ) { return PhCreateStringEx(&String->Buffer[StartIndex], Count * sizeof(WCHAR)); } /** * Updates a string object's length with its true length as determined by an embedded null * terminator. * * \param String The string to modify. * * \remarks Use this function after modifying a string object's buffer manually. */ FORCEINLINE VOID PhTrimToNullTerminatorString( _Inout_ PPH_STRING String ) { String->Length = PhCountStringZ(String->Buffer) * sizeof(WCHAR); } // Byte string extern PPH_OBJECT_TYPE PhBytesType; /** * An 8-bit string object, which supports ASCII, UTF-8 and Windows multi-byte encodings, as well as * binary data. */ typedef struct _PH_BYTES { // Header union { PH_BYTESREF br; struct { /** The length, in bytes, of the string. */ SIZE_T Length; /** The buffer containing the contents of the string. */ PCH Buffer; }; }; // Data union { CHAR Data[1]; struct { /** Reserved. */ ULONG AllocationFlags; /** Reserved. */ PVOID Allocation; }; }; } PH_BYTES, *PPH_BYTES; PHLIBAPI PPH_BYTES NTAPI PhCreateBytes( _In_ PCSTR Buffer ); PHLIBAPI PPH_BYTES NTAPI PhCreateBytesEx( _In_opt_ PCCH Buffer, _In_ SIZE_T Length ); /** * Creates a bytes object from an existing null-terminated string of bytes. * * \param Buffer A null-terminated byte string. */ FORCEINLINE PPH_BYTES NTAPI PhCreateBytes( _In_ PCSTR Buffer ) { return PhCreateBytesEx(Buffer, strlen(Buffer) * sizeof(CHAR)); } FORCEINLINE PPH_BYTES NTAPI PhCreateBytes2( _In_ PPH_BYTESREF Bytes ) { return PhCreateBytesEx(Bytes->Buffer, Bytes->Length); } PPH_BYTES NTAPI PhFormatBytes_V( _In_ _Printf_format_string_ PCSTR Format, _In_ va_list ArgPtr ); PPH_BYTES NTAPI PhFormatBytes( _In_ _Printf_format_string_ PCSTR Format, ... ); // // Unicode // #define PH_UNICODE_BYTE_ORDER_MARK 0xfeff #define PH_UNICODE_MAX_CODE_POINT 0x10ffff #define PH_UNICODE_REPLACEMENT_CHARACTER 0xfffd #define PH_UNICODE_UTF16_TO_HIGH_SURROGATE(CodePoint) ((USHORT)((CodePoint) >> 10) + 0xd7c0) #define PH_UNICODE_UTF16_TO_LOW_SURROGATE(CodePoint) ((USHORT)((CodePoint) & 0x3ff) + 0xdc00) #define PH_UNICODE_UTF16_IS_HIGH_SURROGATE(CodeUnit) ((CodeUnit) >= 0xd800 && (CodeUnit) <= 0xdbff) #define PH_UNICODE_UTF16_IS_LOW_SURROGATE(CodeUnit) ((CodeUnit) >= 0xdc00 && (CodeUnit) <= 0xdfff) #define PH_UNICODE_UTF16_TO_CODE_POINT(HighSurrogate, LowSurrogate) (((ULONG)(HighSurrogate) << 10) + (ULONG)(LowSurrogate) - 0x35fdc00) #define PH_UNICODE_UTF8 0 #define PH_UNICODE_UTF16 1 #define PH_UNICODE_UTF32 2 typedef struct _PH_UNICODE_DECODER { UCHAR Encoding; // PH_UNICODE_* UCHAR State; UCHAR InputCount; UCHAR Reserved; union { UCHAR Utf8[4]; USHORT Utf16[2]; ULONG Utf32; } Input; union { struct { UCHAR Input[4]; UCHAR CodeUnit1; UCHAR CodeUnit2; UCHAR CodeUnit3; UCHAR CodeUnit4; } Utf8; struct { USHORT Input[2]; USHORT CodeUnit; } Utf16; struct { ULONG Input; } Utf32; } u; } PH_UNICODE_DECODER, *PPH_UNICODE_DECODER; FORCEINLINE VOID PhInitializeUnicodeDecoder( _Out_ PPH_UNICODE_DECODER Decoder, _In_ UCHAR Encoding ) { memset(Decoder, 0, sizeof(PH_UNICODE_DECODER)); Decoder->Encoding = Encoding; } PHLIBAPI BOOLEAN NTAPI PhWriteUnicodeDecoder( _Inout_ PPH_UNICODE_DECODER Decoder, _In_ ULONG CodeUnit ); _Success_(return) PHLIBAPI BOOLEAN NTAPI PhDecodeUnicodeDecoder( _Inout_ PPH_UNICODE_DECODER Decoder, _Out_ PULONG CodePoint ); _Success_(return) PHLIBAPI BOOLEAN NTAPI PhEncodeUnicode( _In_ UCHAR Encoding, _In_ ULONG CodePoint, _Out_opt_ PVOID CodeUnits, _Out_ PULONG NumberOfCodeUnits ); // // 8-bit to UTF-16 // PHLIBAPI VOID NTAPI PhZeroExtendToUtf16Buffer( _In_reads_bytes_(InputLength) PCCH Input, _In_ SIZE_T InputLength, _Out_writes_bytes_(InputLength * sizeof(WCHAR)) PWCH Output ); PHLIBAPI PPH_STRING NTAPI PhZeroExtendToUtf16Ex( _In_reads_bytes_(InputLength) PCCH Input, _In_ SIZE_T InputLength ); FORCEINLINE PPH_STRING NTAPI PhZeroExtendToUtf16( _In_ PCSTR Input ) { return PhZeroExtendToUtf16Ex(Input, strlen(Input)); } // // UTF-16 to ASCII // PHLIBAPI PPH_BYTES NTAPI PhConvertUtf16ToAsciiEx( _In_ PCWCH Buffer, _In_ SIZE_T Length, _In_ CHAR Replacement ); FORCEINLINE PPH_BYTES PhConvertUtf16ToAscii( _In_ PCWSTR Buffer, _In_ CHAR Replacement ) { return PhConvertUtf16ToAsciiEx(Buffer, PhCountStringZ(Buffer) * sizeof(WCHAR), Replacement); } // // Multi-byte to UTF-16 // In-place: RtlMultiByteToUnicodeN, RtlMultiByteToUnicodeSize // PHLIBAPI PPH_STRING NTAPI PhConvertMultiByteToUtf16( _In_ PCSTR Buffer ); PHLIBAPI PPH_STRING NTAPI PhConvertMultiByteToUtf16Ex( _In_ PCSTR Buffer, _In_ SIZE_T Length ); // // UTF-16 to multi-byte // In-place: RtlUnicodeToMultiByteN, RtlUnicodeToMultiByteSize // PHLIBAPI PPH_BYTES NTAPI PhConvertUtf16ToMultiByte( _In_ PCWSTR Buffer ); PHLIBAPI PPH_BYTES NTAPI PhConvertUtf16ToMultiByteEx( _In_ PCWCH Buffer, _In_ SIZE_T Length ); // // UTF-8 to UTF-16 // In-place: RtlUTF8ToUnicodeN // PHLIBAPI NTSTATUS NTAPI PhConvertUtf8ToUtf16Size( _Out_ PSIZE_T BytesInUtf16String, _In_reads_bytes_(BytesInUtf8String) PCCH Utf8String, _In_ SIZE_T BytesInUtf8String ); PHLIBAPI NTSTATUS NTAPI PhConvertUtf8ToUtf16Buffer( _Out_writes_bytes_to_(MaxBytesInUtf16String, *BytesInUtf16String) PWCH Utf16String, _In_ SIZE_T MaxBytesInUtf16String, _Out_opt_ PSIZE_T BytesInUtf16String, _In_reads_bytes_(BytesInUtf8String) PCCH Utf8String, _In_ SIZE_T BytesInUtf8String ); _Ret_maybenull_ PHLIBAPI PPH_STRING NTAPI PhConvertUtf8ToUtf16( _In_ PCSTR Buffer ); _Ret_maybenull_ PHLIBAPI PPH_STRING NTAPI PhConvertUtf8ToUtf16Ex( _In_reads_bytes_(Length) PCCH Buffer, _In_ SIZE_T Length ); // // UTF-16 to UTF-8 // In-place: RtlUnicodeToUTF8N // PHLIBAPI NTSTATUS NTAPI PhConvertUtf16ToUtf8Size( _Out_ PSIZE_T BytesInUtf8String, _In_reads_bytes_(BytesInUtf16String) PCWCH Utf16String, _In_ SIZE_T BytesInUtf16String ); PHLIBAPI NTSTATUS NTAPI PhConvertUtf16ToUtf8Buffer( _Out_writes_bytes_to_(MaxBytesInUtf8String, *BytesInUtf8String) PCH Utf8String, _In_ SIZE_T MaxBytesInUtf8String, _Out_opt_ PSIZE_T BytesInUtf8String, _In_reads_bytes_(BytesInUtf16String) PCWCH Utf16String, _In_ SIZE_T BytesInUtf16String ); _Ret_notnull_ PHLIBAPI PPH_BYTES NTAPI PhConvertUtf16ToUtf8( _In_ PCWSTR Buffer ); _Ret_notnull_ PHLIBAPI PPH_BYTES NTAPI PhConvertUtf16ToUtf8Ex( _In_reads_bytes_(Length) PCWCH Buffer, _In_ SIZE_T Length ); FORCEINLINE PPH_BYTES NTAPI PhConvertStringToUtf8( _In_ PPH_STRING String ) { return PhConvertUtf16ToUtf8Ex(String->Buffer, String->Length); } FORCEINLINE PPH_BYTES NTAPI PhConvertStringRefToUtf8( _In_ PCPH_STRINGREF String ) { return PhConvertUtf16ToUtf8Ex(String->Buffer, String->Length); } FORCEINLINE PPH_STRING NTAPI PhConvertBytesToUtf16( _In_ PPH_BYTES String ) { return PhConvertUtf8ToUtf16Ex(String->Buffer, String->Length); } // // String builder // /** * A string builder structure. * The string builder object allows you to easily construct complex strings without allocating * a great number of strings in the process. */ typedef struct _PH_STRING_BUILDER { /** Allocated length of the string, not including the null terminator. */ SIZE_T AllocatedLength; /** * The constructed string. * \a String will be allocated for \a AllocatedLength, we will modify the \a Length field to be * the correct length. */ PPH_STRING String; } PH_STRING_BUILDER, *PPH_STRING_BUILDER; PHLIBAPI VOID NTAPI PhInitializeStringBuilder( _Out_ PPH_STRING_BUILDER StringBuilder, _In_ SIZE_T InitialCapacity ); PHLIBAPI VOID NTAPI PhDeleteStringBuilder( _Inout_ PPH_STRING_BUILDER StringBuilder ); PHLIBAPI PPH_STRING NTAPI PhFinalStringBuilderString( _Inout_ PPH_STRING_BUILDER StringBuilder ); PHLIBAPI VOID NTAPI PhAppendStringBuilderEx( _Inout_ PPH_STRING_BUILDER StringBuilder, _In_opt_ PWCHAR String, _In_ SIZE_T Length ); /** * Appends a string to the end of a string builder string. * * \param StringBuilder A string builder object. * \param String The string to append. */ FORCEINLINE VOID NTAPI PhAppendStringBuilder( _Inout_ PPH_STRING_BUILDER StringBuilder, _In_ PCPH_STRINGREF String ) { PhAppendStringBuilderEx( StringBuilder, String->Buffer, String->Length ); } /** * Appends a string to the end of a string builder string. * * \param StringBuilder A string builder object. * \param String The string to append. */ FORCEINLINE VOID NTAPI PhAppendStringBuilder2( _Inout_ PPH_STRING_BUILDER StringBuilder, _In_ PCWSTR String ) { PH_STRINGREF string; PhInitializeStringRef(&string, String); PhAppendStringBuilder(StringBuilder, &string); } PHLIBAPI VOID NTAPI PhAppendCharStringBuilder( _Inout_ PPH_STRING_BUILDER StringBuilder, _In_ WCHAR Character ); PHLIBAPI VOID NTAPI PhAppendCharStringBuilder2( _Inout_ PPH_STRING_BUILDER StringBuilder, _In_ WCHAR Character, _In_ SIZE_T Count ); PHLIBAPI VOID NTAPI PhAppendFormatStringBuilder( _Inout_ PPH_STRING_BUILDER StringBuilder, _In_ _Printf_format_string_ PCWSTR Format, ... ); VOID NTAPI PhAppendFormatStringBuilder_V( _Inout_ PPH_STRING_BUILDER StringBuilder, _In_ _Printf_format_string_ PCWSTR Format, _In_ va_list ArgPtr ); PHLIBAPI VOID NTAPI PhInsertStringBuilder( _Inout_ PPH_STRING_BUILDER StringBuilder, _In_ SIZE_T Index, _In_ PCPH_STRINGREF String ); PHLIBAPI VOID NTAPI PhInsertStringBuilder2( _Inout_ PPH_STRING_BUILDER StringBuilder, _In_ SIZE_T Index, _In_ PCWSTR String ); PHLIBAPI VOID NTAPI PhInsertStringBuilderEx( _Inout_ PPH_STRING_BUILDER StringBuilder, _In_ SIZE_T Index, _In_opt_ PCWCHAR String, _In_ SIZE_T Length ); PHLIBAPI VOID NTAPI PhRemoveStringBuilder( _Inout_ PPH_STRING_BUILDER StringBuilder, _In_ SIZE_T StartIndex, _In_ SIZE_T Count ); FORCEINLINE VOID PhRemoveEndStringBuilder( _Inout_ PPH_STRING_BUILDER StringBuilder, _In_ SIZE_T Count ) { PhRemoveStringBuilder( StringBuilder, StringBuilder->String->Length / sizeof(WCHAR) - Count, Count ); } // // Byte string builder // /** * A byte string builder structure. * This is similar to string builder, but is based on PH_BYTES and is suitable for general binary * data. */ typedef struct _PH_BYTES_BUILDER { /** Allocated length of the byte string, not including the null terminator. */ SIZE_T AllocatedLength; /** * The constructed byte string. * \a Bytes will be allocated for \a AllocatedLength, we will modify the \a Length field to be * the correct length. */ PPH_BYTES Bytes; } PH_BYTES_BUILDER, *PPH_BYTES_BUILDER; PHLIBAPI VOID NTAPI PhInitializeBytesBuilder( _Out_ PPH_BYTES_BUILDER BytesBuilder, _In_ SIZE_T InitialCapacity ); PHLIBAPI VOID NTAPI PhDeleteBytesBuilder( _Inout_ PPH_BYTES_BUILDER BytesBuilder ); PHLIBAPI PPH_BYTES NTAPI PhFinalBytesBuilderBytes( _Inout_ PPH_BYTES_BUILDER BytesBuilder ); FORCEINLINE PVOID PhOffsetBytesBuilder( _In_ PPH_BYTES_BUILDER BytesBuilder, _In_ SIZE_T Offset ) { return BytesBuilder->Bytes->Buffer + Offset; } PHLIBAPI VOID NTAPI PhAppendBytesBuilder( _Inout_ PPH_BYTES_BUILDER BytesBuilder, _In_ PPH_BYTESREF Bytes ); /** * Appends a byte string to the end of a byte string builder string. * * \param BytesBuilder A byte string builder object. * \param Bytes The byte string to append. */ FORCEINLINE VOID NTAPI PhAppendBytesBuilder2( _Inout_ PPH_BYTES_BUILDER BytesBuilder, _In_ PCSTR Bytes ) { PH_BYTESREF string; PhInitializeBytesRef(&string, Bytes); PhAppendBytesBuilder(BytesBuilder, &string); } PHLIBAPI PVOID NTAPI PhAppendBytesBuilderEx( _Inout_ PPH_BYTES_BUILDER BytesBuilder, _In_opt_ PVOID Buffer, _In_ SIZE_T Length, _In_opt_ SIZE_T Alignment, _Out_opt_ PSIZE_T Offset ); PHLIBAPI VOID NTAPI PhAppendFormatBytesBuilder_V( _Inout_ PPH_BYTES_BUILDER BytesBuilder, _In_ _Printf_format_string_ PCSTR Format, _In_ va_list ArgPtr ); PHLIBAPI VOID NTAPI PhAppendFormatBytesBuilder( _Inout_ PPH_BYTES_BUILDER BytesBuilder, _In_ _Printf_format_string_ PCSTR Format, ... ); // // Array // /** An array structure. Storage is automatically allocated for new elements. */ typedef struct _PH_ARRAY { /** The number of items in the list. */ SIZE_T Count; /** The number of items for which storage is allocated. */ SIZE_T AllocatedCount; /** The size of each item, in bytes. */ SIZE_T ItemSize; /** The base address of the array. */ PVOID Items; } PH_ARRAY, *PPH_ARRAY; PHLIBAPI VOID NTAPI PhInitializeArray( _Out_ PPH_ARRAY Array, _In_ SIZE_T ItemSize, _In_ SIZE_T InitialCapacity ); PHLIBAPI VOID NTAPI PhDeleteArray( _Inout_ PPH_ARRAY Array ); FORCEINLINE PVOID PhItemArray( _In_ PPH_ARRAY Array, _In_ SIZE_T Index ) { return PTR_ADD_OFFSET(Array->Items, Index * Array->ItemSize); } FORCEINLINE PVOID PhFinalArrayItems( _Inout_ PPH_ARRAY Array ) { return Array->Items; } FORCEINLINE SIZE_T PhFinalArrayCount( _In_ PPH_ARRAY Array ) { return Array->Count; } PHLIBAPI VOID NTAPI PhResizeArray( _Inout_ PPH_ARRAY Array, _In_ SIZE_T NewCapacity ); PHLIBAPI VOID NTAPI PhAddItemArray( _Inout_ PPH_ARRAY Array, _In_ PVOID Item ); PHLIBAPI VOID NTAPI PhAddItemsArray( _Inout_ PPH_ARRAY Array, _In_ PVOID Items, _In_ SIZE_T Count ); PHLIBAPI VOID NTAPI PhClearArray( _Inout_ PPH_ARRAY Array ); PHLIBAPI VOID NTAPI PhRemoveItemArray( _Inout_ PPH_ARRAY Array, _In_ SIZE_T Index ); PHLIBAPI VOID NTAPI PhRemoveItemsArray( _Inout_ PPH_ARRAY Array, _In_ SIZE_T StartIndex, _In_ SIZE_T Count ); // // List // extern PPH_OBJECT_TYPE PhListType; /** A list structure. Storage is automatically allocated for new elements. */ typedef struct _PH_LIST { /** The number of items in the list. */ ULONG Count; /** The number of items for which storage is allocated. */ ULONG AllocatedCount; /** The array of list items. */ PVOID *Items; } PH_LIST, *PPH_LIST; FORCEINLINE PVOID PhItemList( _In_ PPH_LIST List, _In_ ULONG Index ) { return List->Items[Index]; } PHLIBAPI PPH_LIST NTAPI PhCreateList( _In_ ULONG InitialCapacity ); PHLIBAPI VOID NTAPI PhResizeList( _Inout_ PPH_LIST List, _In_ ULONG NewCapacity ); PHLIBAPI VOID NTAPI PhAddItemList( _Inout_ PPH_LIST List, _In_ PVOID Item ); PHLIBAPI VOID NTAPI PhAddItemsList( _Inout_ PPH_LIST List, _In_ PVOID *Items, _In_ ULONG Count ); PHLIBAPI VOID NTAPI PhClearList( _Inout_ PPH_LIST List ); _Success_(return != ULONG_MAX) PHLIBAPI ULONG NTAPI PhFindItemList( _In_ PPH_LIST List, _In_ PVOID Item ); PHLIBAPI VOID NTAPI PhInsertItemList( _Inout_ PPH_LIST List, _In_ ULONG Index, _In_ PVOID Item ); PHLIBAPI VOID NTAPI PhInsertItemsList( _Inout_ PPH_LIST List, _In_ ULONG Index, _In_ PVOID *Items, _In_ ULONG Count ); PHLIBAPI VOID NTAPI PhRemoveItemList( _Inout_ PPH_LIST List, _In_ ULONG Index ); PHLIBAPI VOID NTAPI PhRemoveItemsList( _Inout_ PPH_LIST List, _In_ ULONG StartIndex, _In_ ULONG Count ); /** * A comparison function. * * \param Item1 The first item. * \param Item2 The second item. * \param Context A user-defined value. * * \return * \li A positive value if \a Item1 > \a Item2, * \li A negative value if \a Item1 < \a Item2, and * \li 0 if \a Item1 = \a Item2. */ typedef _Function_class_(PH_COMPARE_FUNCTION) LONG NTAPI PH_COMPARE_FUNCTION( _In_ PVOID Item1, _In_ PVOID Item2, _In_opt_ PVOID Context ); typedef PH_COMPARE_FUNCTION* PPH_COMPARE_FUNCTION; // // Pointer list // extern PPH_OBJECT_TYPE PhPointerListType; /** * A pointer list structure. The pointer list is similar to the normal list structure, but both * insertions and deletions occur in constant time. The list is not ordered. */ typedef struct _PH_POINTER_LIST { /** The number of pointers in the list. */ ULONG Count; /** The number of pointers for which storage is allocated. */ ULONG AllocatedCount; /** Index into pointer array for free list. */ ULONG FreeEntry; /** Index of next usable index into pointer array. */ ULONG NextEntry; /** The array of pointers. */ PVOID *Items; } PH_POINTER_LIST, *PPH_POINTER_LIST; #define PH_IS_LIST_POINTER_VALID(Pointer) (!((ULONG_PTR)(Pointer) & 0x1)) PHLIBAPI PPH_POINTER_LIST NTAPI PhCreatePointerList( _In_ ULONG InitialCapacity ); PHLIBAPI HANDLE NTAPI PhAddItemPointerList( _Inout_ PPH_POINTER_LIST PointerList, _In_ PVOID Pointer ); _Success_(return) PHLIBAPI BOOLEAN NTAPI PhEnumPointerListEx( _In_ PPH_POINTER_LIST PointerList, _Inout_ PULONG EnumerationKey, _Out_ PVOID *Pointer, _Out_ PHANDLE PointerHandle ); PHLIBAPI HANDLE NTAPI PhFindItemPointerList( _In_ PPH_POINTER_LIST PointerList, _In_ PVOID Pointer ); PHLIBAPI PVOID NTAPI PhGetItemPointerList( _In_ PPH_POINTER_LIST PointerList, _In_ HANDLE PointerHandle ); PHLIBAPI VOID NTAPI PhRemoveItemPointerList( _Inout_ PPH_POINTER_LIST PointerList, _In_ HANDLE PointerHandle ); _Success_(return != FALSE) FORCEINLINE BOOLEAN PhEnumPointerList( _In_ PPH_POINTER_LIST PointerList, _Inout_ PULONG EnumerationKey, _Out_ PVOID *Pointer ) { while (*EnumerationKey < PointerList->NextEntry) { PVOID pointer = PointerList->Items[*EnumerationKey]; (*EnumerationKey)++; if (PH_IS_LIST_POINTER_VALID(pointer)) { *Pointer = pointer; return TRUE; } } return FALSE; } // // Hash // typedef struct _PH_HASH_ENTRY { struct _PH_HASH_ENTRY *Next; ULONG Hash; } PH_HASH_ENTRY, *PPH_HASH_ENTRY; #define PH_HASH_SET_INIT { 0 } #define PH_HASH_SET_SIZE(Buckets) (sizeof(Buckets) / sizeof(PPH_HASH_ENTRY)) /** * Initializes a hash set. * * \param Buckets The bucket array. * \param NumberOfBuckets The number of buckets. */ FORCEINLINE VOID PhInitializeHashSet( _Out_ PPH_HASH_ENTRY *Buckets, _In_ ULONG NumberOfBuckets ) { memset(Buckets, 0, sizeof(PPH_HASH_ENTRY) * NumberOfBuckets); } /** * Allocates and initializes a hash set. * * \param NumberOfBuckets The number of buckets. * * \return The allocated hash set. You must free it with PhFree() when you no longer need it. */ FORCEINLINE PPH_HASH_ENTRY * PhCreateHashSet( _In_ ULONG NumberOfBuckets ) { PPH_HASH_ENTRY *buckets; buckets = (PPH_HASH_ENTRY *)PhAllocate(sizeof(PPH_HASH_ENTRY) * NumberOfBuckets); PhInitializeHashSet(buckets, NumberOfBuckets); return buckets; } /** * Determines the number of entries in a hash set. * * \param Buckets The bucket array. * \param NumberOfBuckets The number of buckets. * * \return The number of entries in the hash set. */ FORCEINLINE ULONG PhCountHashSet( _In_ PPH_HASH_ENTRY *Buckets, _In_ ULONG NumberOfBuckets ) { ULONG i; PPH_HASH_ENTRY entry; ULONG count; count = 0; for (i = 0; i < NumberOfBuckets; i++) { for (entry = Buckets[i]; entry; entry = entry->Next) count++; } return count; } /** * Moves entries from one hash set to another. * * \param NewBuckets The new bucket array. * \param NumberOfNewBuckets The number of buckets in \a NewBuckets. * \param OldBuckets The old bucket array. * \param NumberOfOldBuckets The number of buckets in \a OldBuckets. * * \remarks \a NewBuckets and \a OldBuckets must be different. */ FORCEINLINE VOID PhDistributeHashSet( _Inout_ PPH_HASH_ENTRY *NewBuckets, _In_ ULONG NumberOfNewBuckets, _In_ CONST PPH_HASH_ENTRY *OldBuckets, _In_ ULONG NumberOfOldBuckets ) { ULONG i; PPH_HASH_ENTRY entry; PPH_HASH_ENTRY nextEntry; ULONG index; for (i = 0; i < NumberOfOldBuckets; i++) { entry = OldBuckets[i]; while (entry) { nextEntry = entry->Next; index = entry->Hash & (NumberOfNewBuckets - 1); entry->Next = NewBuckets[index]; NewBuckets[index] = entry; entry = nextEntry; } } } /** * Adds an entry to a hash set. * * \param Buckets The bucket array. * \param NumberOfBuckets The number of buckets. * \param Entry The entry. * \param Hash The hash for the entry. * * \remarks This function does not check for duplicates. */ FORCEINLINE VOID PhAddEntryHashSet( _Inout_ PPH_HASH_ENTRY *Buckets, _In_ ULONG NumberOfBuckets, _Out_ PPH_HASH_ENTRY Entry, _In_ ULONG Hash ) { ULONG index; index = Hash & (NumberOfBuckets - 1); Entry->Hash = Hash; Entry->Next = Buckets[index]; Buckets[index] = Entry; } /** * Begins the process of finding an entry in a hash set. * * \param Buckets The bucket array. * \param NumberOfBuckets The number of buckets. * \param Hash The hash for the entry. * * \return The first entry in the chain. * * \remarks If the function returns NULL, the entry does not exist in the hash set. */ FORCEINLINE PPH_HASH_ENTRY PhFindEntryHashSet( _In_ CONST PPH_HASH_ENTRY *Buckets, _In_ ULONG NumberOfBuckets, _In_ ULONG Hash ) { return Buckets[Hash & (NumberOfBuckets - 1)]; } /** * Removes an entry from a hash set. * * \param Buckets The bucket array. * \param NumberOfBuckets The number of buckets. * \param Entry An entry present in the hash set. */ FORCEINLINE VOID PhRemoveEntryHashSet( _Inout_ PPH_HASH_ENTRY *Buckets, _In_ ULONG NumberOfBuckets, _Inout_ PPH_HASH_ENTRY Entry ) { ULONG index; PPH_HASH_ENTRY entry; PPH_HASH_ENTRY previousEntry; index = Entry->Hash & (NumberOfBuckets - 1); previousEntry = NULL; entry = Buckets[index]; do { if (entry == Entry) { if (!previousEntry) Buckets[index] = entry->Next; else previousEntry->Next = entry->Next; return; } previousEntry = entry; entry = entry->Next; } while (entry); // Entry doesn't actually exist in the set. This is a fatal logic error. PhRaiseStatus(STATUS_INTERNAL_ERROR); } /** * Resizes a hash set. * * \param Buckets A pointer to the bucket array. On return the new bucket array is stored in this * variable. * \param NumberOfBuckets A pointer to the number of buckets. On return the new number of buckets is * stored in this variable. * \param NewNumberOfBuckets The new number of buckets. */ FORCEINLINE VOID PhResizeHashSet( _Inout_ PPH_HASH_ENTRY **Buckets, _Inout_ PULONG NumberOfBuckets, _In_ ULONG NewNumberOfBuckets ) { PPH_HASH_ENTRY *newBuckets; newBuckets = PhCreateHashSet(NewNumberOfBuckets); PhDistributeHashSet(newBuckets, NewNumberOfBuckets, *Buckets, *NumberOfBuckets); PhFree(*Buckets); *Buckets = newBuckets; *NumberOfBuckets = NewNumberOfBuckets; } // // Hashtable // extern PPH_OBJECT_TYPE PhHashtableType; typedef struct _PH_HASHTABLE_ENTRY { /** Hash code of the entry. -1 if entry is unused. */ ULONG HashCode; /** * Either the index of the next entry in the bucket, the index of the next free entry, or -1 for * invalid. */ ULONG Next; /** The beginning of user data. */ QUAD_PTR Body; } PH_HASHTABLE_ENTRY, *PPH_HASHTABLE_ENTRY; C_ASSERT((FIELD_OFFSET(PH_HASHTABLE_ENTRY, Body) % MEMORY_ALLOCATION_ALIGNMENT) == 0); /** * A comparison function used by a hashtable. * * \param Entry1 The first entry. * \param Entry2 The second entry. * * \return TRUE if the entries are equal, otherwise FALSE. */ typedef _Function_class_(PH_HASHTABLE_EQUAL_FUNCTION) BOOLEAN NTAPI PH_HASHTABLE_EQUAL_FUNCTION( _In_ PVOID Entry1, _In_ PVOID Entry2 ); typedef PH_HASHTABLE_EQUAL_FUNCTION* PPH_HASHTABLE_EQUAL_FUNCTION; /** * A hash function used by a hashtable. * * \param Entry The entry. * * \return A hash code for the entry. * * \remarks * \li Two entries which are considered to be equal by the comparison function must be given the * same hash code. * \li Two different entries do not have to be given different hash codes. */ typedef _Function_class_(PH_HASHTABLE_HASH_FUNCTION) ULONG NTAPI PH_HASHTABLE_HASH_FUNCTION( _In_ PVOID Entry ); typedef PH_HASHTABLE_HASH_FUNCTION* PPH_HASHTABLE_HASH_FUNCTION; // Use power-of-two sizes instead of primes #define PH_HASHTABLE_POWER_OF_TWO_SIZE // Enables 2^32-1 possible hash codes instead of only 2^31 //#define PH_HASHTABLE_FULL_HASH /** * A hashtable structure. */ typedef struct _PH_HASHTABLE { /** Size of user data in each entry. */ ULONG EntrySize; /** The comparison function. */ PPH_HASHTABLE_EQUAL_FUNCTION EqualFunction; /** The hash function. */ PPH_HASHTABLE_HASH_FUNCTION HashFunction; /** The number of allocated buckets. */ ULONG AllocatedBuckets; /** The bucket array. */ PULONG Buckets; /** The number of allocated entries. */ ULONG AllocatedEntries; /** The entry array. */ PVOID Entries; /** Number of entries in the hashtable. */ ULONG Count; /** Index into entry array for free list. */ ULONG FreeEntry; /** * Index of next usable index into entry array, a.k.a. the count of entries that were ever * allocated. */ ULONG NextEntry; } PH_HASHTABLE, *PPH_HASHTABLE; #define PH_HASHTABLE_ENTRY_SIZE(InnerSize) (UInt32Add32To64(UFIELD_OFFSET(PH_HASHTABLE_ENTRY, Body), (InnerSize))) #define PH_HASHTABLE_GET_ENTRY(Hashtable, Index) ((PPH_HASHTABLE_ENTRY)PTR_ADD_OFFSET((Hashtable)->Entries, UInt32x32To64(PH_HASHTABLE_ENTRY_SIZE((Hashtable)->EntrySize), (Index)))) #define PH_HASHTABLE_GET_ENTRY_INDEX(Hashtable, Entry) ((ULONG)(PTR_ADD_OFFSET(Entry, -(Hashtable)->Entries) / PH_HASHTABLE_ENTRY_SIZE((Hashtable)->EntrySize))) PHLIBAPI PPH_HASHTABLE NTAPI PhCreateHashtable( _In_ ULONG EntrySize, _In_ PPH_HASHTABLE_EQUAL_FUNCTION EqualFunction, _In_ PPH_HASHTABLE_HASH_FUNCTION HashFunction, _In_ ULONG InitialCapacity ); PHLIBAPI PVOID NTAPI PhAddEntryHashtable( _Inout_ PPH_HASHTABLE Hashtable, _In_ PVOID Entry ); PHLIBAPI PVOID NTAPI PhAddEntryHashtableEx( _Inout_ PPH_HASHTABLE Hashtable, _In_ PVOID Entry, _Out_opt_ PBOOLEAN Added ); PHLIBAPI VOID NTAPI PhClearHashtable( _Inout_ PPH_HASHTABLE Hashtable ); _Success_(return) PHLIBAPI BOOLEAN NTAPI PhEnumHashtable( _In_ PPH_HASHTABLE Hashtable, _Out_ PVOID *Entry, _Inout_ PULONG EnumerationKey ); PHLIBAPI PVOID NTAPI PhFindEntryHashtable( _In_ PPH_HASHTABLE Hashtable, _In_ PVOID Entry ); PHLIBAPI BOOLEAN NTAPI PhRemoveEntryHashtable( _Inout_ PPH_HASHTABLE Hashtable, _In_ PVOID Entry ); // New faster enumeration method typedef struct _PH_HASHTABLE_ENUM_CONTEXT { ULONG_PTR Current; ULONG_PTR End; ULONG_PTR Step; } PH_HASHTABLE_ENUM_CONTEXT, *PPH_HASHTABLE_ENUM_CONTEXT; FORCEINLINE VOID NTAPI_INLINE PhBeginEnumHashtable( _In_ PPH_HASHTABLE Hashtable, _Out_ PPH_HASHTABLE_ENUM_CONTEXT Context ) { Context->Current = (ULONG_PTR)Hashtable->Entries; Context->Step = PH_HASHTABLE_ENTRY_SIZE((ULONG_PTR)Hashtable->EntrySize); Context->End = Context->Current + (ULONG_PTR)Hashtable->NextEntry * Context->Step; } FORCEINLINE PVOID NTAPI_INLINE PhNextEnumHashtable( _Inout_ PPH_HASHTABLE_ENUM_CONTEXT Context ) { PPH_HASHTABLE_ENTRY entry; while (Context->Current != Context->End) { entry = (PPH_HASHTABLE_ENTRY)Context->Current; Context->Current += Context->Step; if (entry->HashCode != ULONG_MAX) return &entry->Body; } return NULL; } /* * 32 bit FNV-1 and FNV-1a non-zero initial basis */ #define FNV1_32_INIT (ULONG_C(0x811c9dc5)) /* * 32 bit magic FNV-1a prime */ #define FNV_32_PRIME (ULONG_C(0x01000193)) PHLIBAPI ULONG NTAPI PhHashBytes( _In_reads_(Length) PUCHAR Bytes, _In_ SIZE_T Length ); PHLIBAPI ULONG NTAPI PhHashStringRef( _In_ PCPH_STRINGREF String, _In_ BOOLEAN IgnoreCase ); typedef enum _PH_STRING_HASH { PH_STRING_HASH_DEFAULT, PH_STRING_HASH_FNV1A, PH_STRING_HASH_X65599, PH_STRING_HASH_XXH32, } PH_STRING_HASH; PHLIBAPI ULONG NTAPI PhHashStringRefEx( _In_ PCPH_STRINGREF String, _In_ BOOLEAN IgnoreCase, _In_ PH_STRING_HASH HashAlgorithm ); FORCEINLINE ULONG PhHashInt32( _In_ ULONG Value ) { // Java style. Value ^= (Value >> 20) ^ (Value >> 12); return Value ^ (Value >> 7) ^ (Value >> 4); } FORCEINLINE ULONG PhHashInt64( _In_ ULONG64 Value ) { // http://www.concentric.net/~Ttwang/tech/inthash.htm Value = ~Value + (Value << 18); Value ^= Value >> 31; Value *= 21; Value ^= Value >> 11; Value += Value << 6; Value ^= Value >> 22; return (ULONG)Value; } FORCEINLINE ULONG PhHashIntPtr( _In_ ULONG_PTR Value ) { #ifdef _WIN64 return PhHashInt64(Value); #else return PhHashInt32(Value); #endif } // // Simple hashtable // #define SIP(String, Integer) { (String), (PVOID)(Integer) } #define SREF(String) ((PVOID)&(PH_STRINGREF)PH_STRINGREF_INIT((String))) typedef struct _PH_KEY_VALUE_PAIR { PVOID Key; PVOID Value; } PH_KEY_VALUE_PAIR, *PPH_KEY_VALUE_PAIR; typedef CONST PH_KEY_VALUE_PAIR *PCPH_KEY_VALUE_PAIR; PHLIBAPI PPH_HASHTABLE NTAPI PhCreateSimpleHashtable( _In_ ULONG InitialCapacity ); PHLIBAPI PVOID NTAPI PhAddItemSimpleHashtable( _Inout_ PPH_HASHTABLE SimpleHashtable, _In_opt_ PVOID Key, _In_opt_ PVOID Value ); PHLIBAPI PVOID * NTAPI PhFindItemSimpleHashtable( _In_ PPH_HASHTABLE SimpleHashtable, _In_opt_ PVOID Key ); FORCEINLINE PVOID NTAPI_INLINE PhFindItemSimpleHashtable2( _In_ PPH_HASHTABLE SimpleHashtable, _In_opt_ PVOID Key ) { PVOID *item; item = PhFindItemSimpleHashtable(SimpleHashtable, Key); if (item) return *item; else return NULL; } PHLIBAPI BOOLEAN NTAPI PhRemoveItemSimpleHashtable( _Inout_ PPH_HASHTABLE SimpleHashtable, _In_opt_ PVOID Key ); // // Free list // typedef struct _PH_FREE_LIST { SLIST_HEADER ListHead; ULONG Count; ULONG MaximumCount; SIZE_T Size; } PH_FREE_LIST, *PPH_FREE_LIST; typedef struct _PH_FREE_LIST_ENTRY { SLIST_ENTRY ListEntry; QUAD_PTR Body; } PH_FREE_LIST_ENTRY, *PPH_FREE_LIST_ENTRY; C_ASSERT((FIELD_OFFSET(PH_FREE_LIST_ENTRY, Body) % MEMORY_ALLOCATION_ALIGNMENT) == 0); #ifdef _WIN64 C_ASSERT(FIELD_OFFSET(PH_FREE_LIST_ENTRY, ListEntry) == 0x0); C_ASSERT(FIELD_OFFSET(PH_FREE_LIST_ENTRY, Body) == 0x10); #else C_ASSERT(FIELD_OFFSET(PH_FREE_LIST_ENTRY, ListEntry) == 0x0); C_ASSERT(FIELD_OFFSET(PH_FREE_LIST_ENTRY, Body) == 0x8); #endif PHLIBAPI VOID NTAPI PhInitializeFreeList( _Out_ PPH_FREE_LIST FreeList, _In_ SIZE_T Size, _In_ ULONG MaximumCount ); PHLIBAPI VOID NTAPI PhDeleteFreeList( _Inout_ PPH_FREE_LIST FreeList ); PHLIBAPI PVOID NTAPI PhAllocateFromFreeList( _Inout_ PPH_FREE_LIST FreeList ); PHLIBAPI VOID NTAPI PhFreeToFreeList( _Inout_ PPH_FREE_LIST FreeList, _In_ _Post_invalid_ PVOID Memory ); // // Callback // /** * A callback function. * * \param Parameter A value given to all callback functions being notified. * \param Context A user-defined value passed to PhRegisterCallback(). */ typedef _Function_class_(PH_CALLBACK_FUNCTION) VOID NTAPI PH_CALLBACK_FUNCTION( _In_opt_ PVOID Parameter, _In_opt_ PVOID Context ); typedef PH_CALLBACK_FUNCTION *PPH_CALLBACK_FUNCTION; /** A callback registration structure. */ typedef struct _PH_CALLBACK_REGISTRATION { /** The list entry in the callbacks list. */ LIST_ENTRY ListEntry; /** The callback function. */ PPH_CALLBACK_FUNCTION Function; /** A user-defined value to be passed to the callback function. */ PVOID Context; /** A value indicating whether the registration structure is being used. */ LONG Busy; /** Whether the registration structure is being removed. */ BOOLEAN Unregistering; BOOLEAN Reserved; /** Flags controlling the callback. */ USHORT Flags; } PH_CALLBACK_REGISTRATION, *PPH_CALLBACK_REGISTRATION; /** * A callback structure. The callback object allows multiple callback functions to be registered and * notified in a thread-safe way. */ typedef struct _PH_CALLBACK { /** The list of registered callbacks. */ LIST_ENTRY ListHead; /** A lock protecting the callbacks list. */ PH_QUEUED_LOCK ListLock; /** A condition variable pulsed when the callback becomes free. */ PH_CONDITION BusyCondition; } PH_CALLBACK, *PPH_CALLBACK; #define PH_CALLBACK_DECLARE(Name) PH_CALLBACK Name = { &(Name).ListHead, &(Name).ListHead, PH_QUEUED_LOCK_INIT, PH_CONDITION_INIT } PHLIBAPI VOID NTAPI PhInitializeCallback( _Out_ PPH_CALLBACK Callback ); PHLIBAPI VOID NTAPI PhDeleteCallback( _Inout_ PPH_CALLBACK Callback ); PHLIBAPI VOID NTAPI PhRegisterCallback( _Inout_ PPH_CALLBACK Callback, _In_ PPH_CALLBACK_FUNCTION Function, _In_opt_ PVOID Context, _Out_ PPH_CALLBACK_REGISTRATION Registration ); PHLIBAPI VOID NTAPI PhRegisterCallbackEx( _Inout_ PPH_CALLBACK Callback, _In_ PPH_CALLBACK_FUNCTION Function, _In_opt_ PVOID Context, _In_ USHORT Flags, _Out_ PPH_CALLBACK_REGISTRATION Registration ); PHLIBAPI VOID NTAPI PhUnregisterCallback( _Inout_ PPH_CALLBACK Callback, _Inout_ PPH_CALLBACK_REGISTRATION Registration ); PHLIBAPI VOID NTAPI PhInvokeCallback( _In_ PPH_CALLBACK Callback, _In_opt_ PVOID Parameter ); // General PHLIBAPI ULONG NTAPI PhGetPrimeNumber( _In_ ULONG Minimum ); PHLIBAPI ULONG NTAPI PhRoundUpToPowerOfTwo( _In_ ULONG Number ); PHLIBAPI ULONG NTAPI PhExponentiate( _In_ ULONG Base, _In_ ULONG Exponent ); PHLIBAPI ULONG64 NTAPI PhExponentiate64( _In_ ULONG64 Base, _In_ ULONG Exponent ); PHLIBAPI BOOLEAN NTAPI PhHexStringToBuffer( _In_ PCPH_STRINGREF String, _Out_writes_bytes_(String->Length / sizeof(WCHAR) / 2) PUCHAR Buffer ); PHLIBAPI BOOLEAN NTAPI PhHexStringToBufferEx( _In_ PCPH_STRINGREF String, _In_ SIZE_T BufferLength, _Out_writes_bytes_(BufferLength) PVOID Buffer ); _Ret_notnull_ PHLIBAPI PPH_STRING NTAPI PhBufferToHexString( _In_reads_bytes_(Length) PUCHAR Buffer, _In_ SIZE_T Length ); _Ret_notnull_ PHLIBAPI PPH_STRING NTAPI PhBufferToHexStringEx( _In_reads_bytes_(Length) PUCHAR Buffer, _In_ SIZE_T Length, _In_ BOOLEAN UpperCase ); _Success_(return) PHLIBAPI BOOLEAN NTAPI PhBufferToHexStringBuffer( _In_reads_bytes_(InputLength) PUCHAR InputBuffer, _In_ SIZE_T InputLength, _In_ BOOLEAN UpperCase, _Out_writes_bytes_to_(OutputLength, *ReturnLength) PWSTR OutputBuffer, _In_ SIZE_T OutputLength, _Out_opt_ PSIZE_T ReturnLength ); _Success_(return) PHLIBAPI BOOLEAN NTAPI PhStringToInteger64( _In_ PCPH_STRINGREF String, _In_opt_ ULONG Base, _Out_opt_ PLONG64 Integer ); _Success_(return) PHLIBAPI BOOLEAN NTAPI PhStringToUInt64( _In_ PCPH_STRINGREF String, _In_opt_ ULONG Base, _Out_opt_ PULONG64 Integer ); PHLIBAPI BOOLEAN NTAPI PhStringToDouble( _In_ PCPH_STRINGREF String, _Reserved_ ULONG Base, _Out_opt_ DOUBLE *Double ); _Ret_notnull_ PHLIBAPI PPH_STRING NTAPI PhIntegerToString64( _In_ LONG64 Integer, _In_opt_ ULONG Base, _In_ BOOLEAN Signed ); #define PH_TIMESPAN_STR_LEN 30 #define PH_TIMESPAN_STR_LEN_1 (PH_TIMESPAN_STR_LEN + 1) #define PH_TIMESPAN_HMS 0 #define PH_TIMESPAN_HMSM 1 #define PH_TIMESPAN_DHMS 2 #define PH_TIMESPAN_DHMSM 3 PHLIBAPI VOID NTAPI PhPrintTimeSpan( _Out_writes_(PH_TIMESPAN_STR_LEN_1) PWSTR Destination, _In_ ULONG64 Ticks, _In_opt_ ULONG Mode ); PHLIBAPI BOOLEAN NTAPI PhPrintTimeSpanToBuffer( _In_ ULONG64 Ticks, _In_opt_ ULONG Mode, _Out_writes_bytes_(BufferLength) PWSTR Buffer, _In_ SIZE_T BufferLength, _Out_opt_ PSIZE_T ReturnLength ); PHLIBAPI BOOLEAN NTAPI PhCalculateEntropy( _In_ PBYTE Buffer, _In_ ULONG64 BufferLength, _Out_opt_ PFLOAT Entropy, _Out_opt_ PFLOAT Mean, _Out_opt_ PFLOAT Variance ); PHLIBAPI PPH_STRING NTAPI PhFormatEntropy( _In_ FLOAT Entropy, _In_ USHORT EntropyPrecision, _In_opt_ FLOAT Mean, _In_opt_ USHORT MeanPrecision, _In_opt_ FLOAT Variance, _In_opt_ USHORT VariancePrecision ); DECLSPEC_NOALIAS PHLIBAPI VOID NTAPI PhFillMemoryUlong( _Inout_updates_(Count) PULONG Memory, _In_ ULONG Value, _In_ SIZE_T Count ); DECLSPEC_NOALIAS PHLIBAPI VOID NTAPI PhDivideSinglesBySingle( _Inout_updates_(Count) PFLOAT A, _In_ FLOAT B, _In_ SIZE_T Count ); DECLSPEC_NOALIAS PHLIBAPI FLOAT NTAPI PhMaxMemorySingles( _In_ PFLOAT A, _In_ SIZE_T Count ); DECLSPEC_NOALIAS PHLIBAPI FLOAT NTAPI PhAddPlusMaxMemorySingles( _Inout_updates_(Count) PFLOAT A, _Inout_updates_(Count) PFLOAT B, _In_ SIZE_T Count ); DECLSPEC_NOALIAS PHLIBAPI VOID NTAPI PhConvertCopyMemoryUlong( _Inout_updates_(Count) PULONG From, _Inout_updates_(Count) PFLOAT To, _In_ SIZE_T Count ); PHLIBAPI VOID NTAPI PhAddMemoryUlong( _Inout_ PULONG A, _In_ PULONG B, _In_ ULONG Count ); DECLSPEC_NOALIAS PHLIBAPI VOID NTAPI PhConvertCopyMemoryUlong64( _Inout_updates_(Count) PULONG64 From, _Inout_updates_(Count) PFLOAT To, _In_ SIZE_T Count ); DECLSPEC_NOALIAS PHLIBAPI VOID NTAPI PhConvertCopyMemorySizeT( _Inout_updates_(Count) PSIZE_T From, _Inout_updates_(Count) PFLOAT To, _In_ SIZE_T Count ); PHLIBAPI VOID NTAPI PhAddMemoryUlong( _Inout_ PULONG A, _In_ PULONG B, _In_ ULONG Count ); DECLSPEC_NOALIAS PHLIBAPI VOID NTAPI PhConvertCopyMemorySingles( _Inout_updates_(Count) PFLOAT From, _Inout_updates_(Count) PULONG To, _In_ SIZE_T Count ); typedef struct _PH_CIRCULAR_BUFFER_ULONG* PPH_CIRCULAR_BUFFER_ULONG; typedef struct _PH_CIRCULAR_BUFFER_ULONG64* PPH_CIRCULAR_BUFFER_ULONG64; typedef struct _PH_CIRCULAR_BUFFER_SIZE_T* PPH_CIRCULAR_BUFFER_SIZE_T; DECLSPEC_NOALIAS PHLIBAPI VOID NTAPI PhCopyConvertCircularBufferULONG( _Inout_ PPH_CIRCULAR_BUFFER_ULONG Buffer, _Out_writes_(Count) FLOAT* Destination, _In_ ULONG Count ); DECLSPEC_NOALIAS PHLIBAPI VOID NTAPI PhCopyConvertCircularBufferULONG64( _Inout_ PPH_CIRCULAR_BUFFER_ULONG64 Buffer, _Out_writes_(Count) FLOAT* Destination, _In_ ULONG Count ); DECLSPEC_NOALIAS PHLIBAPI VOID NTAPI PhCopyConvertCircularBufferSizeT( _Inout_ PPH_CIRCULAR_BUFFER_SIZE_T Buffer, _Out_writes_(Count) FLOAT* Destination, _In_ ULONG Count ); PHLIBAPI ULONG NTAPI PhCountBits( _In_ ULONG Value ); PHLIBAPI ULONG NTAPI PhCountBitsUlongPtr( _In_ ULONG_PTR Value ); PHLIBAPI ULONG NTAPI PhCountBitsUlong64( _In_ ULONG64 Value ); PHLIBAPI BOOLEAN NTAPI PhAreBitsSet( _In_ PRTL_BITMAP BitMapHeader, _In_ ULONG StartingIndex, _In_ ULONG Length ); PHLIBAPI VOID NTAPI PhClearBits( _Inout_ PRTL_BITMAP BitMapHeader, _In_ ULONG StartingIndex, _In_ ULONG NumberToClear ); PHLIBAPI VOID NTAPI PhSetBits( _Inout_ PRTL_BITMAP BitMapHeader, _In_ ULONG StartingIndex, _In_ ULONG NumberToSet ); PHLIBAPI ULONG NTAPI PhNumberOfSetBits( _In_ PRTL_BITMAP BitMapHeader ); _Success_(return != ULONG_MAX) PHLIBAPI ULONG NTAPI PhFindClearBitsAndSet( _Inout_ PRTL_BITMAP BitMapHeader, _In_ ULONG NumberToFind, _In_ ULONG HintIndex ); // // Thread Local Storage (TLS) // PHLIBAPI ULONG NTAPI PhTlsAlloc( VOID ); PHLIBAPI NTSTATUS NTAPI PhTlsFree( _In_ ULONG Index ); PHLIBAPI PVOID NTAPI PhTlsGetValue( _In_ ULONG Index ); PHLIBAPI NTSTATUS NTAPI PhTlsGetValueEx( _In_ ULONG Index, _Out_ PVOID* Value ); PHLIBAPI NTSTATUS NTAPI PhTlsSetValue( _In_ ULONG Index, _In_opt_ PVOID Value ); // // Errors // _Post_equals_last_error_ PHLIBAPI ULONG NTAPI PhGetLastError( VOID ); PHLIBAPI VOID NTAPI PhSetLastError( _In_ ULONG ErrorValue ); // // Wildcards // PHLIBAPI BOOLEAN NTAPI PhDoesNameContainWildCards( _In_ PCPH_STRINGREF Expression ); PHLIBAPI BOOLEAN NTAPI PhIsNameInExpression( _In_ PCPH_STRINGREF Expression, _In_ PCPH_STRINGREF Name, _In_ BOOLEAN IgnoreCase ); PHLIBAPI BOOLEAN NTAPI PhStringFuzzyMatch( _In_ PCPH_STRINGREF Pattern, _In_ PCPH_STRINGREF Text, _In_ BOOLEAN IgnoreCase ); FORCEINLINE BOOLEAN PhIsLegacyPrefix( _In_ PCPH_STRINGREF Name ) { static const WCHAR prefix[] = { L'P',L'r',L'o',L'c',L'e',L's',L's',L'H',L'a',L'c',L'k',L'e',L'r',L'.' }; if (Name->Length < sizeof(prefix)) return FALSE; for (ULONG i = 0; i < RTL_NUMBER_OF(prefix); i++) { if (PhUpcaseUnicodeChar(Name->Buffer[i]) != PhUpcaseUnicodeChar(prefix[i])) return FALSE; } return TRUE; } // // Auto-dereference convenience functions // FORCEINLINE PPH_STRING NTAPI_INLINE PhaCreateString( _In_ PCWSTR Buffer ) { return PH_AUTO_T(PH_STRING, PhCreateString(Buffer)); } FORCEINLINE PPH_STRING NTAPI_INLINE PhaCreateStringEx( _In_opt_ PCWSTR Buffer, _In_ SIZE_T Length ) { return PH_AUTO_T(PH_STRING, PhCreateStringEx(Buffer, Length)); } FORCEINLINE PPH_STRING NTAPI_INLINE PhaDuplicateString( _In_ PPH_STRING String ) { return PH_AUTO_T(PH_STRING, PhDuplicateString(String)); } FORCEINLINE PPH_STRING NTAPI_INLINE PhaConcatStrings( _In_ ULONG Count, ... ) { PPH_STRING string; va_list argptr; va_start(argptr, Count); string = PH_AUTO_T(PH_STRING, PhConcatStrings_V(Count, argptr)); va_end(argptr); return string; } FORCEINLINE PPH_STRING NTAPI_INLINE PhaConcatStrings2( _In_ PCWSTR String1, _In_ PCWSTR String2 ) { return PH_AUTO_T(PH_STRING, PhConcatStrings2(String1, String2)); } FORCEINLINE PPH_STRING NTAPI_INLINE PhaFormatString( _In_ _Printf_format_string_ PCWSTR Format, ... ) { PPH_STRING string; va_list argptr; va_start(argptr, Format); string = PH_AUTO_T(PH_STRING, PhFormatString_V(Format, argptr)); va_end(argptr); return string; } FORCEINLINE PPH_STRING NTAPI_INLINE PhLowerString( _In_ PPH_STRING String ) { return PhCreateString3(&String->sr, PH_STRING_LOWER_CASE, NULL); } FORCEINLINE PPH_STRING NTAPI_INLINE PhaLowerString( _In_ PPH_STRING String ) { return PH_AUTO_T(PH_STRING, PhCreateString3(&String->sr, PH_STRING_LOWER_CASE, NULL)); } FORCEINLINE PPH_STRING NTAPI_INLINE PhUpperString( _In_ PPH_STRING String ) { return PhCreateString3(&String->sr, PH_STRING_UPPER_CASE, NULL); } FORCEINLINE PPH_STRING NTAPI_INLINE PhaUpperString( _In_ PPH_STRING String ) { return PH_AUTO_T(PH_STRING, PhCreateString3(&String->sr, PH_STRING_UPPER_CASE, NULL)); } FORCEINLINE PPH_STRING NTAPI_INLINE PhaSubstring( _In_ PPH_STRING String, _In_ SIZE_T StartIndex, _In_ SIZE_T Count ) { return PH_AUTO_T(PH_STRING, PhSubstring(String, StartIndex, Count)); } // // Format // typedef enum _PH_FORMAT_TYPE { CharFormatType, StringFormatType, StringZFormatType, MultiByteStringFormatType, MultiByteStringZFormatType, Int32FormatType, Int64FormatType, IntPtrFormatType, UInt32FormatType, UInt64FormatType, UIntPtrFormatType, SingleFormatType, DoubleFormatType, SizeFormatType, FormatTypeMask = 0x3f, /** If not specified, for floating-point 6 is assumed **/ FormatUsePrecision = 0x40, /** If not specified, ' ' is assumed */ FormatUsePad = 0x80, /** If not specified, 10 is assumed */ FormatUseRadix = 0x100, /** If not specified, the default value is assumed */ FormatUseParameter = 0x200, // // Floating-point flags // /** Use standard form instead of normal form */ FormatStandardForm = 0x1000, /** Use hexadecimal form instead of normal form */ FormatHexadecimalForm = 0x2000, /** Reserved */ FormatForceDecimalPoint = 0x4000, /** Trailing zeros and possibly the decimal point are trimmed */ FormatCropZeros = 0x8000, // // Floating-point and integer flags // /** Group digits (with floating-point, only works when in normal form) */ FormatGroupDigits = 0x10000, /** Always insert a prefix, '+' for positive and '-' for negative */ FormatPrefixSign = 0x20000, /** * Pad left with zeros, taking into consideration the sign. Width must be specified. * Format*Align cannot be used in conjunction with this flag. If FormatGroupDigits is specified, * this flag is ignored. */ FormatPadZeros = 0x40000, // // General flags // /** Make characters uppercase (only available for some types) */ FormatUpperCase = 0x20000000, /** Applies right alignment. Width must be specified. */ FormatRightAlign = 0x40000000, /** Applies left alignment. Width must be specified. */ FormatLeftAlign = 0x80000000, } PH_FORMAT_TYPE; /** Describes an element to be formatted to a string. */ typedef struct _PH_FORMAT { /** Specifies the type of the element and optional flags. */ ULONG Type; /** * The precision of the element. The meaning of this field depends on the element type. For * \a Double and \a Size, this field specifies the number of decimal points to include. */ USHORT Precision; /** * The width of the element. This field specifies the minimum number of characters to output. * The remaining space is padded with either spaces, zeros, or a custom character. */ USHORT Width; /** The pad character. */ WCHAR Pad; /** * The meaning of this field depends on the element type. For integer types, this field * specifies the base to convert the number into. For \a Size, this field specifies the maximum * size unit. */ UCHAR Radix; /** * The meaning of this field depends on the element type. For \a Size, this field specifies the * minimum size unit. */ UCHAR Parameter; union { WCHAR Char; PH_STRINGREF String; PWSTR StringZ; PH_BYTESREF MultiByteString; PSTR MultiByteStringZ; LONG Int32; LONG64 Int64; LONG_PTR IntPtr; ULONG UInt32; ULONG64 UInt64; ULONG_PTR UIntPtr; FLOAT Single; DOUBLE Double; ULONG64 Size; } u; } PH_FORMAT, *PPH_FORMAT; // // Convenience functions // FORCEINLINE VOID PhInitFormatC( _Out_ PPH_FORMAT Format, _In_ WCHAR Char ) { Format->Type = CharFormatType; Format->u.Char = Char; } FORCEINLINE VOID PhInitFormatS( _Out_ PPH_FORMAT Format, _In_ PCWSTR String ) { Format->Type = StringFormatType; PhInitializeStringRef(&Format->u.String, String); } FORCEINLINE VOID PhInitFormatSR( _Out_ PPH_FORMAT Format, _In_ PH_STRINGREF String ) { Format->Type = StringFormatType; Format->u.String = String; } FORCEINLINE VOID PhInitFormatUCS( _Out_ PPH_FORMAT Format, _In_ PCUNICODE_STRING String ) { Format->Type = StringFormatType; PhUnicodeStringToStringRef(String, &Format->u.String); } FORCEINLINE VOID PhInitFormatMultiByteS( _Out_ PPH_FORMAT Format, _In_ PCSTR String ) { Format->Type = MultiByteStringFormatType; PhInitializeBytesRef(&Format->u.MultiByteString, String); } FORCEINLINE VOID PhInitFormatD( _Out_ PPH_FORMAT Format, _In_ LONG Int32 ) { Format->Type = Int32FormatType; Format->u.Int32 = Int32; } FORCEINLINE VOID PhInitFormatU( _Out_ PPH_FORMAT Format, _In_ ULONG UInt32 ) { Format->Type = UInt32FormatType; Format->u.UInt32 = UInt32; } FORCEINLINE VOID PhInitFormatX( _Out_ PPH_FORMAT Format, _In_ ULONG UInt32 ) { Format->Type = UInt32FormatType | FormatUseRadix; Format->u.UInt32 = UInt32; Format->Radix = 16; } FORCEINLINE VOID PhInitFormatI64D( _Out_ PPH_FORMAT Format, _In_ LONG64 Int64 ) { Format->Type = Int64FormatType; Format->u.Int64 = Int64; } FORCEINLINE VOID PhInitFormatI64U( _Out_ PPH_FORMAT Format, _In_ ULONG64 UInt64 ) { Format->Type = UInt64FormatType; Format->u.UInt64 = UInt64; } FORCEINLINE VOID PhInitFormatI64UGroupDigits( _Out_ PPH_FORMAT Format, _In_ ULONG64 UInt64 ) { Format->Type = UInt64FormatType | FormatGroupDigits; Format->u.UInt64 = UInt64; } FORCEINLINE VOID PhInitFormatI64UWithWidth( _Out_ PPH_FORMAT Format, _In_ ULONG64 UInt64, _In_ USHORT Width ) { Format->Type = UInt64FormatType | FormatPadZeros; Format->u.UInt64 = UInt64; Format->Width = Width; } FORCEINLINE VOID PhInitFormatI64X( _Out_ PPH_FORMAT Format, _In_ ULONG64 UInt64 ) { Format->Type = UInt64FormatType | FormatUseRadix; Format->u.UInt64 = UInt64; Format->Radix = 16; } FORCEINLINE VOID PhInitFormatI64XWithWidth( _Out_ PPH_FORMAT Format, _In_ ULONG64 UInt64, _In_ USHORT Width ) { Format->Type = UInt64FormatType | FormatUseRadix | FormatPadZeros; Format->u.UInt64 = UInt64; Format->Radix = 16; Format->Width = Width; } FORCEINLINE VOID PhInitFormatI64XPadZeroes( _Out_ PPH_FORMAT Format, _In_ ULONG64 UInt64, _In_ USHORT Width ) { Format->Type = UInt64FormatType | FormatUseRadix | FormatPadZeros; Format->u.UInt64 = UInt64; Format->Radix = 16; Format->Width = Width; } FORCEINLINE VOID PhInitFormatIU( _Out_ PPH_FORMAT Format, _In_ ULONG_PTR UIntPtr ) { Format->Type = UIntPtrFormatType; Format->u.UIntPtr = UIntPtr; } FORCEINLINE VOID PhInitFormatIX( _Out_ PPH_FORMAT Format, _In_ ULONG_PTR UIntPtr ) { Format->Type = UIntPtrFormatType | FormatUseRadix; Format->u.UIntPtr = UIntPtr; Format->Radix = 16; } FORCEINLINE VOID PhInitFormatIXPadZeros( _Out_ PPH_FORMAT Format, _In_ ULONG_PTR UIntPtr ) { Format->Type = UIntPtrFormatType | FormatUseRadix | FormatPadZeros; Format->u.UIntPtr = UIntPtr; Format->Radix = 16; Format->Width = sizeof(ULONG_PTR) * 2; } FORCEINLINE VOID PhInitFormatF( _Out_ PPH_FORMAT Format, _In_ FLOAT Single, _In_ USHORT Precision ) { Format->Type = SingleFormatType | FormatUsePrecision; Format->u.Single = Single; Format->Precision = Precision; } FORCEINLINE VOID PhInitFormatFD( _Out_ PPH_FORMAT Format, _In_ DOUBLE Double, _In_ USHORT Precision ) { Format->Type = DoubleFormatType | FormatUsePrecision; Format->u.Double = Double; Format->Precision = Precision; } FORCEINLINE VOID PhInitFormatE( _Out_ PPH_FORMAT Format, _In_ DOUBLE Double, _In_ USHORT Precision ) { Format->Type = DoubleFormatType | FormatStandardForm | FormatUsePrecision; Format->u.Double = Double; Format->Precision = Precision; } FORCEINLINE VOID PhInitFormatA( _Out_ PPH_FORMAT Format, _In_ DOUBLE Double, _In_ USHORT Precision ) { Format->Type = DoubleFormatType | FormatHexadecimalForm | FormatUsePrecision; Format->u.Double = Double; Format->Precision = Precision; } FORCEINLINE VOID PhInitFormatSize( _Out_ PPH_FORMAT Format, _In_ ULONG64 Size ) { Format->Type = SizeFormatType; Format->u.Size = Size; } FORCEINLINE VOID PhInitFormatSizeWithPrecision( _Out_ PPH_FORMAT Format, _In_ ULONG64 Size, _In_ USHORT Precision ) { Format->Type = SizeFormatType | FormatUsePrecision; Format->u.Size = Size; Format->Precision = Precision; } PHLIBAPI PPH_STRING NTAPI PhFormat( _In_reads_(Count) PPH_FORMAT Format, _In_ ULONG Count, _In_opt_ SIZE_T InitialCapacity ); FORCEINLINE PPH_STRING PhaFormat( _In_reads_(Count) PPH_FORMAT Format, _In_ ULONG Count, _In_opt_ SIZE_T InitialCapacity ) { return PH_AUTO_T(PH_STRING, PhFormat(Format, Count, InitialCapacity)); } PHLIBAPI BOOLEAN NTAPI PhFormatToBuffer( _In_reads_(Count) PPH_FORMAT Format, _In_ ULONG Count, _Out_writes_bytes_opt_(BufferLength) PWSTR Buffer, _In_opt_ SIZE_T BufferLength, _Out_opt_ PSIZE_T ReturnLength ); PHLIBAPI NTSTATUS NTAPI PhFormatSingleToUtf8( _In_ FLOAT Value, _In_ ULONG Type, _In_ LONG Precision, _Out_writes_bytes_(BufferLength) PSTR Buffer, _In_opt_ SIZE_T BufferLength, _Out_opt_ PSIZE_T ReturnLength ); PHLIBAPI NTSTATUS NTAPI PhFormatDoubleToUtf8( _In_ DOUBLE Value, _In_ ULONG Type, _In_ LONG Precision, _Out_writes_bytes_(BufferLength) PSTR Buffer, _In_opt_ SIZE_T BufferLength, _Out_opt_ PSIZE_T ReturnLength ); PHLIBAPI NTSTATUS NTAPI PhIntegerToUtf8Buffer( _In_ LONG64 Integer, _In_opt_ ULONG Base, _In_ BOOLEAN Signed, _Out_writes_bytes_(BufferLength) PSTR Buffer, _In_ SIZE_T BufferLength, _Out_opt_ PSIZE_T ReturnLength ); PHLIBAPI NTSTATUS NTAPI PhStringToInteger64Utf8( _In_ PCSTR String, _In_opt_ ULONG Base, _Out_ PLONG64 Integer ); PHLIBAPI NTSTATUS NTAPI PhStringToInteger64Utf8Ex( _In_reads_bytes_(Length) PCSTR String, _In_ SIZE_T Length, _In_opt_ ULONG Base, _Out_ PLONG64 Integer ); PHLIBAPI NTSTATUS NTAPI PhStringToUInt64Utf8( _In_ PCSTR String, _In_opt_ ULONG Base, _Out_ PULONG64 Integer ); PHLIBAPI NTSTATUS NTAPI PhStringToUInt64Utf8Ex( _In_reads_bytes_(Length) PCSTR String, _In_ SIZE_T Length, _In_opt_ ULONG Base, _Out_ PULONG64 Integer ); PHLIBAPI NTSTATUS NTAPI PhStringToDoubleUtf8( _In_ PCSTR String, _Out_ DOUBLE* Double ); PHLIBAPI NTSTATUS NTAPI PhStringToDoubleUtf8Ex( _In_reads_bytes_(Length) PCSTR String, _In_ SIZE_T Length, _Out_ DOUBLE* Double ); // // Errors // #define HRESULT_CUSTOMER(hr) (((ULONG)(hr) >> 29) & 0x1) #define HRESULT_NTSTATUS(hr) (((ULONG)(hr) >> 28) & 0x1) PHLIBAPI ULONG NTAPI PhNtStatusToDosError( _In_ NTSTATUS Status ); PHLIBAPI ULONG NTAPI PhNtStatusToServiceStatus( _In_ NTSTATUS Status ); PHLIBAPI NTSTATUS NTAPI PhDosErrorToNtStatus( _In_ ULONG DosError ); PHLIBAPI BOOLEAN NTAPI PhNtStatusFileNotFound( _In_ NTSTATUS Status ); PHLIBAPI NTSTATUS NTAPI PhNtStatusFromHResult( _In_ HRESULT Result ); FORCEINLINE NTSTATUS NTAPI PhGetLastWin32ErrorAsNtStatus( VOID ) { return PhDosErrorToNtStatus(PhGetLastError()); } // // Generic tree definitions // typedef enum _PH_TREE_ENUMERATION_ORDER { TreeEnumerateInOrder, TreeEnumerateInReverseOrder } PH_TREE_ENUMERATION_ORDER; #define PhIsLeftChildElement(Links) ((Links)->Parent->Left == (Links)) #define PhIsRightChildElement(Links) ((Links)->Parent->Right == (Links)) // // avltree // typedef struct _PH_AVL_LINKS { struct _PH_AVL_LINKS *Parent; struct _PH_AVL_LINKS *Left; struct _PH_AVL_LINKS *Right; LONG Balance; } PH_AVL_LINKS, *PPH_AVL_LINKS; typedef _Function_class_(PH_AVL_TREE_COMPARE_FUNCTION) LONG NTAPI PH_AVL_TREE_COMPARE_FUNCTION( _In_ PPH_AVL_LINKS Links1, _In_ PPH_AVL_LINKS Links2 ); typedef PH_AVL_TREE_COMPARE_FUNCTION* PPH_AVL_TREE_COMPARE_FUNCTION; typedef struct _PH_AVL_TREE { PH_AVL_LINKS Root; // Right contains real root ULONG Count; PPH_AVL_TREE_COMPARE_FUNCTION CompareFunction; } PH_AVL_TREE, *PPH_AVL_TREE; #define PH_AVL_TREE_INIT(CompareFunction) { { NULL, NULL, NULL, 0 }, 0, CompareFunction } #define PhRootElementAvlTree(Tree) ((Tree)->Root.Right) PHLIBAPI VOID NTAPI PhInitializeAvlTree( _Out_ PPH_AVL_TREE Tree, _In_ PPH_AVL_TREE_COMPARE_FUNCTION CompareFunction ); PHLIBAPI PPH_AVL_LINKS NTAPI PhAddElementAvlTree( _Inout_ PPH_AVL_TREE Tree, _Out_ PPH_AVL_LINKS Element ); PHLIBAPI VOID NTAPI PhRemoveElementAvlTree( _Inout_ PPH_AVL_TREE Tree, _Inout_ PPH_AVL_LINKS Element ); PHLIBAPI PPH_AVL_LINKS NTAPI PhFindElementAvlTree( _In_ PPH_AVL_TREE Tree, _In_ PPH_AVL_LINKS Element ); PHLIBAPI PPH_AVL_LINKS NTAPI PhLowerBoundElementAvlTree( _In_ PPH_AVL_TREE Tree, _In_ PPH_AVL_LINKS Element ); PHLIBAPI PPH_AVL_LINKS NTAPI PhUpperBoundElementAvlTree( _In_ PPH_AVL_TREE Tree, _In_ PPH_AVL_LINKS Element ); PHLIBAPI PPH_AVL_LINKS NTAPI PhLowerDualBoundElementAvlTree( _In_ PPH_AVL_TREE Tree, _In_ PPH_AVL_LINKS Element ); PHLIBAPI PPH_AVL_LINKS NTAPI PhUpperDualBoundElementAvlTree( _In_ PPH_AVL_TREE Tree, _In_ PPH_AVL_LINKS Element ); PHLIBAPI PPH_AVL_LINKS NTAPI PhMinimumElementAvlTree( _In_ PPH_AVL_TREE Tree ); PHLIBAPI PPH_AVL_LINKS NTAPI PhMaximumElementAvlTree( _In_ PPH_AVL_TREE Tree ); PHLIBAPI PPH_AVL_LINKS NTAPI PhSuccessorElementAvlTree( _In_ PPH_AVL_LINKS Element ); PHLIBAPI PPH_AVL_LINKS NTAPI PhPredecessorElementAvlTree( _In_ PPH_AVL_LINKS Element ); typedef _Function_class_(PH_ENUM_AVL_TREE_CALLBACK) BOOLEAN NTAPI PH_ENUM_AVL_TREE_CALLBACK( _In_ PPH_AVL_TREE Tree, _In_ PPH_AVL_LINKS Element, _In_opt_ PVOID Context ); typedef PH_ENUM_AVL_TREE_CALLBACK *PPH_ENUM_AVL_TREE_CALLBACK; PHLIBAPI VOID NTAPI PhEnumAvlTree( _In_ PPH_AVL_TREE Tree, _In_ PH_TREE_ENUMERATION_ORDER Order, _In_ PPH_ENUM_AVL_TREE_CALLBACK Callback, _In_opt_ PVOID Context ); EXTERN_C_END #endif