using System.Runtime.InteropServices; namespace XFEWarframe.Bridge; internal static partial class NativeMemoryRange { [StructLayout(LayoutKind.Sequential)] private struct Information { public nint BaseAddress, AllocationBase; public uint AllocationProtect; public ushort PartitionId; public nuint RegionSize; public uint State, Protect, Type; } [LibraryImport("kernel32.dll")] private static partial nuint VirtualQuery(nint address, out Information information, nuint length); internal static bool Readable(nint address, nuint length) { var current = (nuint)address; if (current < 0x10000 || length > 16 * 1024 * 1024 || current > nuint.MaxValue - length) return false; var end = current + length; while (current < end) { if (VirtualQuery((nint)current, out var region, (nuint)Marshal.SizeOf()) == 0 || region.State != 0x1000 || (region.Protect & 0x101) != 0) return false; var regionEnd = (nuint)region.BaseAddress + region.RegionSize; if (regionEnd <= current) return false; current = Math.Min(end, regionEnd); } return true; } }