13 #define PTR_ADD_OFFSET(Pointer, Offset) ((PVOID)((ULONG_PTR)(Pointer) + (ULONG_PTR)(Offset)))
14 #define PTR_SUB_OFFSET(Pointer, Offset) ((PVOID)((ULONG_PTR)(Pointer) - (ULONG_PTR)(Offset)))
15 #define ALIGN_UP_BY(Address, Align) (((ULONG_PTR)(Address) + (Align) - 1) & ~((Align) - 1))
16 #define ALIGN_UP_POINTER_BY(Pointer, Align) ((PVOID)ALIGN_UP_BY(Pointer, Align))
17 #define ALIGN_UP(Address, Type) ALIGN_UP_BY(Address, sizeof(Type))
18 #define ALIGN_UP_POINTER(Pointer, Type) ((PVOID)ALIGN_UP(Pointer, Type))
20 #define PAGE_SIZE 0x1000
22 #define PH_LARGE_BUFFER_SIZE (256 * 1024 * 1024)
26 #define PhRaiseStatus(Status) RtlRaiseStatus(Status)
28 #define SIMPLE_EXCEPTION_FILTER(Condition) \
29 ((Condition) ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
34 #define ASSUME_ASSERT(Expression) assert(Expression)
35 #define ASSUME_NO_DEFAULT assert(FALSE)
37 #define ASSUME_ASSERT(Expression) __assume(Expression)
38 #define ASSUME_NO_DEFAULT __assume(FALSE)
43 #define PH_TICKS_PER_NS ((LONG64)1 * 10)
44 #define PH_TICKS_PER_MS (PH_TICKS_PER_NS * 1000)
45 #define PH_TICKS_PER_SEC (PH_TICKS_PER_MS * 1000)
46 #define PH_TICKS_PER_MIN (PH_TICKS_PER_SEC * 60)
47 #define PH_TICKS_PER_HOUR (PH_TICKS_PER_MIN * 60)
48 #define PH_TICKS_PER_DAY (PH_TICKS_PER_HOUR * 24)
50 #define PH_TICKS_PARTIAL_MS(Ticks) (((ULONG64)(Ticks) / PH_TICKS_PER_MS) % 1000)
51 #define PH_TICKS_PARTIAL_SEC(Ticks) (((ULONG64)(Ticks) / PH_TICKS_PER_SEC) % 60)
52 #define PH_TICKS_PARTIAL_MIN(Ticks) (((ULONG64)(Ticks) / PH_TICKS_PER_MIN) % 60)
53 #define PH_TICKS_PARTIAL_HOURS(Ticks) (((ULONG64)(Ticks) / PH_TICKS_PER_HOUR) % 24)
54 #define PH_TICKS_PARTIAL_DAYS(Ticks) ((ULONG64)(Ticks) / PH_TICKS_PER_DAY)
56 #define PH_TIMEOUT_MS PH_TICKS_PER_MS
57 #define PH_TIMEOUT_SEC PH_TICKS_PER_SEC
74 #define _Assume_refs_(count)
92 #define _Needs_align_(align)
97 #define C_1uTo2(x) ((unsigned short)(unsigned char)(x))
98 #define C_1sTo2(x) ((unsigned short)(signed char)(x))
99 #define C_1uTo4(x) ((unsigned int)(unsigned char)(x))
100 #define C_1sTo4(x) ((unsigned int)(signed char)(x))
101 #define C_2uTo4(x) ((unsigned int)(unsigned short)(x))
102 #define C_2sTo4(x) ((unsigned int)(signed short)(x))
103 #define C_4uTo8(x) ((unsigned __int64)(unsigned int)(x))
104 #define C_4sTo8(x) ((unsigned __int64)(signed int)(x))
128 #define PH_BUILTIN_COMPARE(value1, value2) \
129 if (value1 > value2) \
131 else if (value1 < value2) \
137 _In_
signed char value1,
138 _In_
signed char value2
141 return C_1sTo4(value1 - value2);
145 _In_
unsigned char value1,
146 _In_
unsigned char value2
153 _In_
signed short value1,
154 _In_
signed short value2
157 return C_2sTo4(value1 - value2);
161 _In_
unsigned short value1,
162 _In_
unsigned short value2
173 return value1 - value2;
177 _In_
unsigned int value1,
178 _In_
unsigned int value2
193 _In_
unsigned __int64 value1,
194 _In_
unsigned __int64 value2
201 _In_ LONG_PTR value1,
209 _In_ ULONG_PTR value1,
210 _In_ ULONG_PTR value2
233 _In_opt_ PWSTR Value1,
234 _In_opt_ PWSTR Value2
237 if (Value1 && Value2)
238 return wcsicmp(Value1, Value2);
240 return !Value2 ? 0 : -1;
251 #ifndef _InterlockedCompareExchangePointer
253 void *
volatile *Destination,
259 #ifndef _InterlockedExchangePointer
261 void *
volatile *Destination,
265 return (PVOID)_InterlockedExchange(
266 (PLONG_PTR)Destination,
275 _Inout_ _Interlocked_operand_ LONG_PTR
volatile *Addend,
280 return (LONG_PTR)_InterlockedExchangeAdd64((PLONG64)Addend, (LONG64)Value);
282 return (LONG_PTR)_InterlockedExchangeAdd((PLONG)Addend, (
LONG)Value);
287 _Inout_ _Interlocked_operand_ LONG_PTR
volatile *Addend
291 return (LONG_PTR)_InterlockedIncrement64((PLONG64)Addend);
293 return (LONG_PTR)_InterlockedIncrement((PLONG)Addend);
298 _Inout_ _Interlocked_operand_ LONG_PTR
volatile *Addend
302 return (LONG_PTR)_InterlockedDecrement64((PLONG64)Addend);
304 return (LONG_PTR)_InterlockedDecrement((PLONG)Addend);
309 _Inout_ _Interlocked_operand_ LONG_PTR
volatile *Base,
314 return _interlockedbittestandreset64((PLONG64)Base, (LONG64)Bit);
316 return _interlockedbittestandreset((PLONG)Base, (
LONG)Bit);
321 _Inout_ _Interlocked_operand_ LONG_PTR
volatile *Base,
326 return _interlockedbittestandset64((PLONG64)Base, (LONG64)Bit);
328 return _interlockedbittestandset((PLONG)Base, (
LONG)Bit);
333 _Inout_ _Interlocked_operand_
LONG volatile *Addend
346 if ((newValue = _InterlockedCompareExchange(
361 #define PH_INT32_STR_LEN 12
362 #define PH_INT32_STR_LEN_1 (PH_INT32_STR_LEN + 1)
364 #define PH_INT64_STR_LEN 50
365 #define PH_INT64_STR_LEN_1 (PH_INT64_STR_LEN + 1)
367 #define PH_PTR_STR_LEN 24
368 #define PH_PTR_STR_LEN_1 (PH_PTR_STR_LEN + 1)
375 _ltow(Int32, Destination, 10);
383 _ultow(UInt32, Destination, 10);
391 _i64tow(Int64, Destination, 10);
399 _ui64tow(UInt64, Destination, 10);
407 Destination[0] =
'0';
408 Destination[1] =
'x';
410 _ui64tow((ULONG64)Pointer, &Destination[2], 16);
412 _ultow((ULONG)Pointer, &Destination[2], 16);
435 _In_ ULONG Multiplier
440 newValue = Value / Multiplier * Multiplier;
452 if (Value - newValue < Multiplier / 2)
455 return newValue + Multiplier;
459 _In_ PVOID UserAddress,
460 _In_ SIZE_T UserLength,
461 _In_ PVOID BufferAddress,
462 _In_ SIZE_T BufferLength,
468 if (((ULONG_PTR)UserAddress & (Alignment - 1)) != 0)
472 ((ULONG_PTR)UserAddress + UserLength < (ULONG_PTR)UserAddress) ||
473 ((ULONG_PTR)UserAddress < (ULONG_PTR)BufferAddress) ||
474 ((ULONG_PTR)UserAddress + UserLength > (ULONG_PTR)BufferAddress + BufferLength)
481 _Out_ PLARGE_INTEGER Timeout,
482 _In_ ULONG Milliseconds
485 if (Milliseconds == INFINITE)
488 Timeout->QuadPart = -(LONGLONG)UInt32x32To64(Milliseconds,
PH_TIMEOUT_MS);
498 win32Result = GetLastError();
500 return NTSTATUS_FROM_WIN32(win32Result);
504 _In_ PWSTR ModuleName,
510 module = GetModuleHandle(ModuleName);
513 return GetProcAddress(module, ProcName);