Exemplo n.º 1
0
        public bool ReadAbsolute(UIntPtr address, byte[] buffer, EndiannessType endianness)
        {
            if (Endianness == endianness)
            {
                int numOfBytes = 0;
                return(ReadFunc(address, buffer));
            }
            else
            {
                // Read padded if misaligned address
                byte[]  swapBytes;
                UIntPtr alignedAddress = EndiannessUtilities.AlignedAddressFloor(address);
                swapBytes = new byte[(buffer.Length / 4) * 4 + 8];

                // Read memory
                if (!ReadFunc(address, swapBytes))
                {
                    return(false);
                }

                swapBytes = EndiannessUtilities.SwapByteEndianness(swapBytes);

                // Copy memory
                Buffer.BlockCopy(swapBytes, (int)(address.ToUInt64() - alignedAddress.ToUInt64()), buffer, 0, buffer.Length);

                return(true);
            }
        }
Exemplo n.º 2
0
        public bool ReadAbsolute(UIntPtr address, byte[] buffer, EndiannessType endianness)
        {
            if (Endianness == endianness)
            {
                return(ReadFunc(address, buffer));
            }
            else
            {
                // Not a between alignment, or aligned write
                if (buffer.Length >= 4 && (buffer.Length % 4 != 0))
                {
                    throw new Exception("Misaligned data");
                }

                address = EndiannessUtilities.SwapAddressEndianness(address, buffer.Length);
                byte[] readBytes = new byte[buffer.Length];
                if (!ReadFunc(address, readBytes))
                {
                    return(false);
                }

                readBytes = EndiannessUtilities.SwapByteEndianness(readBytes);
                Buffer.BlockCopy(readBytes, 0, buffer, 0, buffer.Length);

                return(true);
            }
        }
Exemplo n.º 3
0
        public static bool DataIsMisaligned(UIntPtr address, int dataSize, EndiannessType endianness)
        {
            // Get the number of bytes remaining to alignment for the address
            int bytesToAlignment = NumberOfBytesToAlignment(address);

            if (endianness == EndiannessType.Little) // Little endianess goes backwards in count, not forwards
            {
                bytesToAlignment = _fixAddress[bytesToAlignment];
            }

            // All datasize greater than 4 must already have an aligned address, and an aligned data size (multiple of 4)
            if (dataSize >= 4)
            {
                return(bytesToAlignment != 0 || dataSize % 4 != 0);
            }

            // If we are already aligned, we really have 4 bytes remaining
            if (bytesToAlignment == 0)
            {
                bytesToAlignment = 4;
            }

            // Be sure the bytes fit in the remaining section and do go past the 4-byte alignment
            return(bytesToAlignment < dataSize);
        }
Exemplo n.º 4
0
      public byte[] ReadRam(UIntPtr address, int length, EndiannessType endianness, bool absoluteAddress = false)
      {
          byte[] readBytes = new byte[length];

          // Get local address
          uint localAddress;

          if (absoluteAddress)
          {
              localAddress = _io?.GetRelativeAddress(address, length) ?? 0;
          }
          else
          {
              localAddress = address.ToUInt32();
          }
          localAddress &= ~0x80000000;

          if (EndiannessUtilities.DataIsMisaligned(address, length, EndiannessType.Big))
          {
              return(readBytes);
          }

          /// Fix endianness
          switch (endianness)
          {
          case EndiannessType.Little:
              // Address is not little endian, fix:
              localAddress = EndiannessUtilities.SwapAddressEndianness(localAddress, length);

              if (localAddress + length > _ram.Length)
              {
                  break;
              }

              Buffer.BlockCopy(_ram, (int)localAddress, readBytes, 0, length);
              break;

          case EndiannessType.Big:
              // Read padded if misaligned address
              byte[] swapBytes;
              uint   alignedAddress = EndiannessUtilities.AlignedAddressFloor(localAddress);

              int alignedReadByteCount = (readBytes.Length / 4) * 4 + 8;
              swapBytes = new byte[alignedReadByteCount];

              // Read memory
              Buffer.BlockCopy(_ram, (int)alignedAddress, swapBytes, 0, swapBytes.Length);
              swapBytes = EndiannessUtilities.SwapByteEndianness(swapBytes);

              // Copy memory
              Buffer.BlockCopy(swapBytes, (int)(localAddress - alignedAddress), readBytes, 0, readBytes.Length);

              break;
          }


          return(readBytes);
      }
Exemplo n.º 5
0
 /// <summary>
 /// ByteRunType class constructor
 /// </summary>
 public ByteRunType()
 {
     _hashes             = new List <HashType>();
     _length             = new IntegerObjectPropertyType();
     _image_Offset       = new IntegerObjectPropertyType();
     _file_System_Offset = new IntegerObjectPropertyType();
     _byte_Order         = new EndiannessType();
     _offset             = new IntegerObjectPropertyType();
 }
Exemplo n.º 6
0
 /// <summary>
 /// DataSegmentType class constructor
 /// </summary>
 public DataSegmentType()
 {
     _search_Within   = new IntegerObjectPropertyType();
     _search_Distance = new IntegerObjectPropertyType();
     _offset          = new IntegerObjectPropertyType();
     _data_Segment    = new StringObjectPropertyType();
     _byte_Order      = new EndiannessType();
     _data_Size       = new DataSizeType();
 }
Exemplo n.º 7
0
        public bool WriteAbsolute(UIntPtr address, byte[] buffer, EndiannessType endianness)
        {
            int numOfBytes = 0;

            // Safety bounds check
            if (address.ToUInt64() < BaseOffset.ToUInt64() ||
                address.ToUInt64() + (uint)buffer.Length >= BaseOffset.ToUInt64() + _ramSize)
            {
                return(false);
            }

            if (Endianness == endianness)
            {
                return(WriteFunc(address, buffer));
            }
            else
            {
                bool success             = true;
                IEnumerable <byte> bytes = buffer;

                int numberToWrite = Math.Min(EndiannessUtilities.NumberOfBytesToAlignment(address), buffer.Length);
                if (numberToWrite > 0)
                {
                    byte[] toWrite = bytes.Take(numberToWrite).Reverse().ToArray();
                    success &= WriteFunc(EndiannessUtilities.AlignedAddressFloor(address), toWrite);

                    bytes    = bytes.Skip(toWrite.Length);
                    address += toWrite.Length;
                }

                numberToWrite = bytes.Count();
                if (bytes.Count() >= 4)
                {
                    byte[] toWrite = EndiannessUtilities.SwapByteEndianness(bytes.Take(bytes.Count() & ~0x03).ToArray());

                    success &= WriteFunc(address, toWrite);

                    bytes    = bytes.Skip(toWrite.Length);
                    address += toWrite.Length;
                }

                numberToWrite = bytes.Count();
                if (numberToWrite > 0)
                {
                    byte[] toWrite = bytes.Reverse().ToArray();
                    address = EndiannessUtilities.SwapAddressEndianness(address, toWrite.Length);

                    success &= WriteFunc(address, toWrite);
                }

                return(success);
            }
        }
Exemplo n.º 8
0
      public bool WriteRam(byte[] buffer, UIntPtr address, EndiannessType endianness, bool absoluteAddress = false,
                           int bufferStart = 0, int?length = null, bool safeWrite = true)
      {
          if (length == null)
          {
              length = buffer.Length - bufferStart;
          }

          if (CheckReadonlyOff())
          {
              return(false);
          }

          byte[] writeBytes = new byte[length.Value];
          Array.Copy(buffer, bufferStart, writeBytes, 0, length.Value);

          // Attempt to pause the game before writing
          bool preSuspended = _io?.IsSuspended ?? false;

          if (safeWrite)
          {
              _io?.Suspend();
          }

          if (EndiannessUtilities.DataIsMisaligned(address, length.Value, EndiannessType.Big))
          {
              throw new Exception("Misaligned data");
          }

          // Write memory to game/process
          bool result;

          if (absoluteAddress)
          {
              result = _io?.WriteAbsolute(address, writeBytes, endianness) ?? false;
          }
          else
          {
              result = _io?.WriteRelative(address.ToUInt32(), writeBytes, endianness) ?? false;
          }

          // Resume stream
          if (safeWrite && !preSuspended)
          {
              _io?.Resume();
          }

          //FocusOnEmulator();
          return(result);
      }
Exemplo n.º 9
0
        public bool WriteAbsolute(UIntPtr address, byte[] buffer, EndiannessType endianness)
        {
            // Safety bounds check
            if (address.ToUInt64() < BaseOffset.ToUInt64() ||
                address.ToUInt64() + (uint)buffer.Length >= BaseOffset.ToUInt64() + Config.RamSize)
            {
                return(false);
            }

            if (Endianness == endianness)
            {
                return(WriteFunc(address, buffer));
            }
            else
            {
                bool   success = true;
                byte[] toWrite = EndiannessUtilities.SwapByteEndianness(buffer);

                // Between alignment writes
                if (buffer.Length < 4)
                {
                    address  = EndiannessUtilities.SwapAddressEndianness(address, toWrite.Length);
                    success &= WriteFunc(address, toWrite);
                }
                else if (buffer.Length % 4 == 0) // Full alignment writes
                {
                    success &= WriteFunc(address, toWrite);
                }
                else
                {
                    throw new Exception("Misaligned data");
                }

                return(success);
            }
        }
Exemplo n.º 10
0
 public void SetEndianness(EndiannessType endianness)
 {
     _endianness = endianness;
 }
Exemplo n.º 11
0
 public bool WriteRam(byte[] buffer, uint address, EndiannessType endianness,
                      int bufferStart = 0, int?length = null, bool safeWrite = true)
 {
     return(WriteRam(buffer, (UIntPtr)address, endianness, false, bufferStart, length, safeWrite));
 }
Exemplo n.º 12
0
 public bool ReadProcessMemory(UIntPtr address, byte[] buffer, EndiannessType endianness)
 {
     return(_io?.ReadAbsolute(address, buffer, endianness) ?? false);
 }
Exemplo n.º 13
0
 public byte[] ReadRam(uint address, int length, EndiannessType endianness)
 {
     return(ReadRam((UIntPtr)address, length, endianness, false));
 }
Exemplo n.º 14
0
 public byte[] ReadRam(uint address, int length, EndiannessType endianness, bool absoluteAddress = false)
 {
     return(ReadRam((UIntPtr)address, length, endianness, absoluteAddress));
 }
Exemplo n.º 15
0
 public bool WriteRelative(uint address, byte[] buffer, EndiannessType endianness)
 {
     return(WriteAbsolute(GetAbsoluteAddress(address, buffer.Length), buffer, endianness));
 }
Exemplo n.º 16
0
 /// <summary>
 /// Instantiates new <see cref="UMemoryStream"/> type.
 /// </summary>
 /// <param name="length">Length of the underlying array.</param>
 /// <param name="endiannessType">Endianness order to Read/Write.</param>
 public UMemoryStream(int length, EndiannessType endiannessType)
     : base(length, endiannessType)
 {
     _encoding = Encoding.UTF8;
 }
Exemplo n.º 17
0
 public MockEmuIO(EndiannessType endianness)
 {
     _endianness = endianness;
     Clear();
 }