2018-03-15 20:06:24 -04:00
|
|
|
using System;
|
2018-02-04 18:08:20 -05:00
|
|
|
using System.IO;
|
2018-03-15 20:06:24 -04:00
|
|
|
using System.Runtime.InteropServices;
|
2018-02-04 18:08:20 -05:00
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
namespace ChocolArm64.Memory
|
|
|
|
{
|
|
|
|
public static class AMemoryHelper
|
|
|
|
{
|
|
|
|
public static void FillWithZeros(AMemory Memory, long Position, int Size)
|
|
|
|
{
|
|
|
|
int Size8 = Size & ~(8 - 1);
|
|
|
|
|
|
|
|
for (int Offs = 0; Offs < Size8; Offs += 8)
|
|
|
|
{
|
|
|
|
Memory.WriteInt64(Position + Offs, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int Offs = Size8; Offs < (Size - Size8); Offs++)
|
|
|
|
{
|
|
|
|
Memory.WriteByte(Position + Offs, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-15 20:06:24 -04:00
|
|
|
public unsafe static T Read<T>(AMemory Memory, long Position) where T : struct
|
|
|
|
{
|
|
|
|
long Size = Marshal.SizeOf<T>();
|
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
Memory.EnsureRangeIsValid(Position, Size);
|
2018-03-15 20:06:24 -04:00
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
IntPtr Ptr = (IntPtr)Memory.Translate(Position);
|
2018-03-15 20:06:24 -04:00
|
|
|
|
|
|
|
return Marshal.PtrToStructure<T>(Ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
public unsafe static void Write<T>(AMemory Memory, long Position, T Value) where T : struct
|
|
|
|
{
|
|
|
|
long Size = Marshal.SizeOf<T>();
|
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
Memory.EnsureRangeIsValid(Position, Size);
|
2018-03-15 20:06:24 -04:00
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
IntPtr Ptr = (IntPtr)Memory.TranslateWrite(Position);
|
2018-03-15 20:06:24 -04:00
|
|
|
|
|
|
|
Marshal.StructureToPtr<T>(Value, Ptr, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static string ReadAsciiString(AMemory Memory, long Position, long MaxSize = -1)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
|
|
|
using (MemoryStream MS = new MemoryStream())
|
|
|
|
{
|
2018-03-15 20:06:24 -04:00
|
|
|
for (long Offs = 0; Offs < MaxSize || MaxSize == -1; Offs++)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
|
|
|
byte Value = (byte)Memory.ReadByte(Position + Offs);
|
|
|
|
|
|
|
|
if (Value == 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
MS.WriteByte(Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Encoding.ASCII.GetString(MS.ToArray());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|