102 Patches: Detours to the Rescue
C reference for DttR maintainers and modders.
Loading...
Searching...
No Matches
dttr_util_mem.h
Go to the documentation of this file.
1
7
8#ifndef DTTR_UTIL_MEM_H
9#define DTTR_UTIL_MEM_H
10
11#include <stdbool.h>
12#include <stddef.h>
13#include <stdint.h>
14#ifndef DTTR_SDK_ENABLE_UNSTABLE
15#error "Define DTTR_SDK_ENABLE_UNSTABLE before including dttr_util_mem.h"
16#endif
17
18#ifdef _WIN32
19#include <windows.h>
20#endif
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25
30static inline bool DTTR_Util_MemReadable(const void *ptr, size_t size) {
31 if (!ptr || size == 0) {
32 return false;
33 }
34
35#ifdef _WIN32
36 MEMORY_BASIC_INFORMATION mbi;
37 if (VirtualQuery(ptr, &mbi, sizeof(mbi)) == 0) {
38 return false;
39 }
40
41 const uintptr_t start = (uintptr_t)ptr;
42 const uintptr_t end = start + size;
43 const uintptr_t region_end = (uintptr_t)mbi.BaseAddress + mbi.RegionSize;
44 const DWORD bad = PAGE_NOACCESS | PAGE_GUARD;
45 return end >= start && end <= region_end && mbi.State == MEM_COMMIT
46 && (mbi.Protect & bad) == 0;
47#else
48 // On non-Windows builds this stub treats every range as readable.
49 return true;
50#endif
51}
52
53#ifdef __cplusplus
54}
55#endif
56
57#endif // DTTR_UTIL_MEM_H
const DWORD size
static bool DTTR_Util_MemReadable(const void *ptr, size_t size)