Skip to content

Commit

Permalink
check for valid theCode var before read/write methods occur
Browse files Browse the repository at this point in the history
  • Loading branch information
erfg12 committed Jan 25, 2022
1 parent 771ff6f commit 036274b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 20 deletions.
60 changes: 40 additions & 20 deletions Memory/Methods/Read.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public byte[] ReadBytes(string code, long length, string file = "")
{
byte[] memory = new byte[length];
UIntPtr theCode = GetCode(code, file);
if (theCode == null || theCode == UIntPtr.Zero || theCode.ToUInt64() < 0x10000)
return null;

if (!ReadProcessMemory(mProc.Handle, theCode, memory, (UIntPtr)length, IntPtr.Zero))
return null;
Expand All @@ -61,8 +63,10 @@ public float ReadFloat(string code, string file = "", bool round = true)
{
byte[] memory = new byte[4];

UIntPtr theCode;
theCode = GetCode(code, file);
UIntPtr theCode = GetCode(code, file);
if (theCode == null || theCode == UIntPtr.Zero || theCode.ToUInt64() < 0x10000)
return 0;

try
{
if (ReadProcessMemory(mProc.Handle, theCode, memory, (UIntPtr)4, IntPtr.Zero))
Expand Down Expand Up @@ -97,8 +101,9 @@ public string ReadString(string code, string file = "", int length = 32, bool ze
stringEncoding = System.Text.Encoding.UTF8;

byte[] memoryNormal = new byte[length];
UIntPtr theCode;
theCode = GetCode(code, file);
UIntPtr theCode = GetCode(code, file);
if (theCode == null || theCode == UIntPtr.Zero || theCode.ToUInt64() < 0x10000)
return "";

if (ReadProcessMemory(mProc.Handle, theCode, memoryNormal, (UIntPtr)length, IntPtr.Zero))
return (zeroTerminated) ? stringEncoding.GetString(memoryNormal).Split('\0')[0] : stringEncoding.GetString(memoryNormal);
Expand All @@ -117,8 +122,10 @@ public double ReadDouble(string code, string file = "", bool round = true)
{
byte[] memory = new byte[8];

UIntPtr theCode;
theCode = GetCode(code, file);
UIntPtr theCode = GetCode(code, file);
if (theCode == null || theCode == UIntPtr.Zero || theCode.ToUInt64() < 0x10000)
return 0;

try
{
if (ReadProcessMemory(mProc.Handle, theCode, memory, (UIntPtr)8, IntPtr.Zero))
Expand Down Expand Up @@ -156,8 +163,10 @@ public int ReadUIntPtr(UIntPtr code)
public int ReadInt(string code, string file = "")
{
byte[] memory = new byte[4];
UIntPtr theCode;
theCode = GetCode(code, file);
UIntPtr theCode = GetCode(code, file);
if (theCode == null || theCode == UIntPtr.Zero || theCode.ToUInt64() < 0x10000)
return 0;

if (ReadProcessMemory(mProc.Handle, theCode, memory, (UIntPtr)4, IntPtr.Zero))
return BitConverter.ToInt32(memory, 0);
else
Expand All @@ -173,9 +182,10 @@ public int ReadInt(string code, string file = "")
public long ReadLong(string code, string file = "")
{
byte[] memory = new byte[16];
UIntPtr theCode;
UIntPtr theCode = GetCode(code, file);

theCode = GetCode(code, file);
if (theCode == null || theCode == UIntPtr.Zero || theCode.ToUInt64() < 0x10000)
return 0;

if (ReadProcessMemory(mProc.Handle, theCode, memory, (UIntPtr)8, IntPtr.Zero))
return BitConverter.ToInt64(memory, 0);
Expand All @@ -192,8 +202,9 @@ public long ReadLong(string code, string file = "")
public UInt32 ReadUInt(string code, string file = "")
{
byte[] memory = new byte[4];
UIntPtr theCode;
theCode = GetCode(code, file);
UIntPtr theCode = GetCode(code, file);
if (theCode == null || theCode == UIntPtr.Zero || theCode.ToUInt64() < 0x10000)
return 0;

if (ReadProcessMemory(mProc.Handle, theCode, memory, (UIntPtr)4, IntPtr.Zero))
return BitConverter.ToUInt32(memory, 0);
Expand All @@ -211,8 +222,9 @@ public UInt32 ReadUInt(string code, string file = "")
public int Read2ByteMove(string code, int moveQty, string file = "")
{
byte[] memory = new byte[4];
UIntPtr theCode;
theCode = GetCode(code, file);
UIntPtr theCode = GetCode(code, file);
if (theCode == null || theCode == UIntPtr.Zero || theCode.ToUInt64() < 0x10000)
return 0;

UIntPtr newCode = UIntPtr.Add(theCode, moveQty);

Expand All @@ -232,8 +244,9 @@ public int Read2ByteMove(string code, int moveQty, string file = "")
public int ReadIntMove(string code, int moveQty, string file = "")
{
byte[] memory = new byte[4];
UIntPtr theCode;
theCode = GetCode(code, file);
UIntPtr theCode = GetCode(code, file);
if (theCode == null || theCode == UIntPtr.Zero || theCode.ToUInt64() < 0x10000)
return 0;

UIntPtr newCode = UIntPtr.Add(theCode, moveQty);

Expand All @@ -253,8 +266,9 @@ public int ReadIntMove(string code, int moveQty, string file = "")
public ulong ReadUIntMove(string code, int moveQty, string file = "")
{
byte[] memory = new byte[8];
UIntPtr theCode;
theCode = GetCode(code, file, 8);
UIntPtr theCode = GetCode(code, file, 8);
if (theCode == null || theCode == UIntPtr.Zero || theCode.ToUInt64() < 0x10000)
return 0;

UIntPtr newCode = UIntPtr.Add(theCode, moveQty);

Expand All @@ -274,8 +288,9 @@ public int Read2Byte(string code, string file = "")
{
byte[] memoryTiny = new byte[4];

UIntPtr theCode;
theCode = GetCode(code, file);
UIntPtr theCode = GetCode(code, file);
if (theCode == null || theCode == UIntPtr.Zero || theCode.ToUInt64() < 0x10000)
return 0;

if (ReadProcessMemory(mProc.Handle, theCode, memoryTiny, (UIntPtr)2, IntPtr.Zero))
return BitConverter.ToInt32(memoryTiny, 0);
Expand All @@ -294,6 +309,8 @@ public int ReadByte(string code, string file = "")
byte[] memoryTiny = new byte[1];

UIntPtr theCode = GetCode(code, file);
if (theCode == null || theCode == UIntPtr.Zero || theCode.ToUInt64() < 0x10000)
return 0;

if (ReadProcessMemory(mProc.Handle, theCode, memoryTiny, (UIntPtr)1, IntPtr.Zero))
return memoryTiny[0];
Expand All @@ -315,6 +332,9 @@ public bool[] ReadBits(string code, string file = "")

bool[] ret = new bool[8];

if (theCode == null || theCode == UIntPtr.Zero || theCode.ToUInt64() < 0x10000)
return ret;

if (!ReadProcessMemory(mProc.Handle, theCode, buf, (UIntPtr)1, IntPtr.Zero))
return ret;

Expand Down
3 changes: 3 additions & 0 deletions Memory/Methods/Write.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ public bool WriteMemory(string code, string type, string write, string file = ""
UIntPtr theCode;
theCode = GetCode(code, file);

if (theCode == null || theCode == UIntPtr.Zero || theCode.ToUInt64() < 0x10000)
return false;

if (type.ToLower() == "float")
{
write = Convert.ToString(float.Parse(write, CultureInfo.InvariantCulture));
Expand Down

0 comments on commit 036274b

Please sign in to comment.