Skip to content

Instantly share code, notes, and snippets.

@Measter
Created October 11, 2013 08:08
Show Gist options
  • Save Measter/6931272 to your computer and use it in GitHub Desktop.
Save Measter/6931272 to your computer and use it in GitHub Desktop.
Abusing Operator Overloads.
using System;
namespace PointerTest
{
public struct Pointer
{
public static byte[] Memory = new byte[UInt16.MaxValue];
private readonly UInt16 m_address;
public override string ToString()
{
return string.Format( "Address: {0}", m_address );
}
private Pointer( UInt16 address )
{
m_address = address;
}
public static implicit operator Pointer( UInt16 value )
{
return new Pointer( value );
}
private static byte[] GetBytes( UInt16 addr, int size )
{
byte[] ret = new byte[size];
for( int i = 0; i < size; i++ )
ret[i] = Memory[addr + i];
return ret;
}
private static void SetBytes( UInt16 addr, byte[] bytes )
{
for( int i = 0; i < bytes.Length; i++ )
Memory[addr + i] = bytes[i];
}
#region Fetch
public static byte operator &( byte b, Pointer a )
{
return Memory[a.m_address];
}
public static Int16 operator &( Int16 b, Pointer a )
{
byte[] bytes = GetBytes( a.m_address, sizeof( Int16 ) );
return BitConverter.ToInt16( bytes, 0 );
}
public static UInt16 operator &( UInt16 b, Pointer a )
{
byte[] bytes = GetBytes( a.m_address, sizeof( UInt16 ) );
return BitConverter.ToUInt16( bytes, 0 );
}
public static Int32 operator &( Int32 b, Pointer a )
{
byte[] bytes = GetBytes( a.m_address, sizeof( Int32 ) );
return BitConverter.ToInt32( bytes, 0 );
}
public static UInt32 operator &( UInt32 b, Pointer a )
{
byte[] bytes = GetBytes( a.m_address, sizeof( UInt32 ) );
return BitConverter.ToUInt32( bytes, 0 );
}
public static Int64 operator &( Int64 b, Pointer a )
{
byte[] bytes = GetBytes( a.m_address, sizeof( Int64 ) );
return BitConverter.ToInt64( bytes, 0 );
}
public static UInt64 operator &( UInt64 b, Pointer a )
{
byte[] bytes = GetBytes( a.m_address, sizeof( UInt64 ) );
return BitConverter.ToUInt64( bytes, 0 );
}
public static Single operator &( Single b, Pointer a )
{
byte[] bytes = GetBytes( a.m_address, sizeof( Single ) );
return BitConverter.ToSingle( bytes, 0 );
}
public static Double operator &( Double b, Pointer a )
{
byte[] bytes = GetBytes( a.m_address, sizeof( Double ) );
return BitConverter.ToDouble( bytes, 0 );
}
public static Boolean operator &( Boolean b, Pointer a )
{
byte[] bytes = GetBytes( a.m_address, sizeof( Boolean ) );
return BitConverter.ToBoolean( bytes, 0 );
}
public static Char operator &( Char b, Pointer a )
{
byte[] bytes = GetBytes( a.m_address, sizeof( Char ) );
return BitConverter.ToChar( bytes, 0 );
}
#endregion
#region Set
public static Pointer operator |( Pointer a, byte b )
{
Memory[a.m_address] = b;
return a;
}
public static Pointer operator |( Pointer a, Int16 b )
{
SetBytes( a.m_address, BitConverter.GetBytes( b ) );
return a;
}
public static Pointer operator |( Pointer a, UInt16 b )
{
SetBytes( a.m_address, BitConverter.GetBytes( b ) );
return a;
}
public static Pointer operator |( Pointer a, Int32 b )
{
SetBytes( a.m_address, BitConverter.GetBytes( b ) );
return a;
}
public static Pointer operator |( Pointer a, UInt32 b )
{
SetBytes( a.m_address, BitConverter.GetBytes( b ) );
return a;
}
public static Pointer operator |( Pointer a, Int64 b )
{
SetBytes( a.m_address, BitConverter.GetBytes( b ) );
return a;
}
public static Pointer operator |( Pointer a, UInt64 b )
{
SetBytes( a.m_address, BitConverter.GetBytes( b ) );
return a;
}
public static Pointer operator |( Pointer a, Single b )
{
SetBytes( a.m_address, BitConverter.GetBytes( b ) );
return a;
}
public static Pointer operator |( Pointer a, Double b )
{
SetBytes( a.m_address, BitConverter.GetBytes( b ) );
return a;
}
public static Pointer operator |( Pointer a, Boolean b )
{
SetBytes( a.m_address, BitConverter.GetBytes( b ) );
return a;
}
public static Pointer operator |( Pointer a, Char b )
{
SetBytes( a.m_address, BitConverter.GetBytes( b ) );
return a;
}
#endregion
#region Arithmetic
public static Pointer operator +( Pointer a, sbyte b )
{
return new Pointer( (UInt16)( a.m_address + sizeof( sbyte ) * b ) );
}
public static Pointer operator +( Pointer a, byte b )
{
return new Pointer( (UInt16)( a.m_address + sizeof( byte ) * b ) );
}
public static Pointer operator +( Pointer a, Int16 b )
{
return new Pointer( (UInt16)( a.m_address + sizeof( Int16 ) * b ) );
}
public static Pointer operator +( Pointer a, UInt16 b )
{
return new Pointer( (UInt16)( a.m_address + sizeof( UInt16 ) * b ) );
}
public static Pointer operator +( Pointer a, Int32 b )
{
return new Pointer( (UInt16)( a.m_address + sizeof( Int32 ) * b ) );
}
public static Pointer operator +( Pointer a, UInt32 b )
{
return new Pointer( (UInt16)( a.m_address + sizeof( UInt32 ) * b ) );
}
public static Pointer operator +( Pointer a, Int64 b )
{
return new Pointer( (UInt16)( a.m_address + sizeof( Int64 ) * b ) );
}
public static Pointer operator +( Pointer a, UInt64 b )
{
return new Pointer( (UInt16)( a.m_address + sizeof( UInt64 ) * b ) );
}
public static Pointer operator +( Pointer a, Pointer b )
{
return new Pointer( (UInt16)( a.m_address + b.m_address ) );
}
public static Pointer operator -( Pointer a, sbyte b )
{
return new Pointer( (UInt16)( a.m_address - sizeof( sbyte ) * b ) );
}
public static Pointer operator -( Pointer a, byte b )
{
return new Pointer( (UInt16)( a.m_address - sizeof( byte ) * b ) );
}
public static Pointer operator -( Pointer a, Int16 b )
{
return new Pointer( (UInt16)( a.m_address - sizeof( Int16 ) * b ) );
}
public static Pointer operator -( Pointer a, UInt16 b )
{
return new Pointer( (UInt16)( a.m_address - sizeof( UInt16 ) * b ) );
}
public static Pointer operator -( Pointer a, Int32 b )
{
return new Pointer( (UInt16)( a.m_address - sizeof( Int32 ) * b ) );
}
public static Pointer operator -( Pointer a, UInt32 b )
{
return new Pointer( (UInt16)( a.m_address - sizeof( UInt32 ) * b ) );
}
public static Pointer operator -( Pointer a, Int64 b )
{
return new Pointer( (UInt16)( a.m_address - sizeof( Int64 ) * b ) );
}
public static Pointer operator -( Pointer a, UInt64 b )
{
return new Pointer( (UInt16)( a.m_address - sizeof( UInt64 ) * b ) );
}
public static Pointer operator -( Pointer a, Pointer b )
{
return new Pointer( (UInt16)( a.m_address - b.m_address ) );
}
public static Pointer operator *( Pointer a, sbyte b )
{
return new Pointer( (UInt16)( a.m_address * ( sizeof( sbyte ) * b ) ) );
}
public static Pointer operator *( Pointer a, byte b )
{
return new Pointer( (UInt16)( a.m_address * ( sizeof( byte ) * b ) ) );
}
public static Pointer operator *( Pointer a, Int16 b )
{
return new Pointer( (UInt16)( a.m_address * ( sizeof( Int16 ) * b ) ) );
}
public static Pointer operator *( Pointer a, UInt16 b )
{
return new Pointer( (UInt16)( a.m_address * ( sizeof( UInt16 ) * b ) ) );
}
public static Pointer operator *( Pointer a, Int32 b )
{
return new Pointer( (UInt16)( a.m_address * ( sizeof( Int32 ) * b ) ) );
}
public static Pointer operator *( Pointer a, UInt32 b )
{
return new Pointer( (UInt16)( a.m_address * ( sizeof( UInt32 ) * b ) ) );
}
public static Pointer operator *( Pointer a, Int64 b )
{
return new Pointer( (UInt16)( a.m_address * ( sizeof( Int64 ) * b ) ) );
}
public static Pointer operator *( Pointer a, UInt64 b )
{
return new Pointer( (UInt16)( a.m_address * ( sizeof( UInt64 ) * b ) ) );
}
public static Pointer operator *( Pointer a, Pointer b )
{
return new Pointer( (UInt16)( a.m_address * b.m_address ) );
}
public static Pointer operator /( Pointer a, sbyte b )
{
return new Pointer( (UInt16)( a.m_address / ( sizeof( sbyte ) * b ) ) );
}
public static Pointer operator /( Pointer a, byte b )
{
return new Pointer( (UInt16)( a.m_address / ( sizeof( byte ) * b ) ) );
}
public static Pointer operator /( Pointer a, Int16 b )
{
return new Pointer( (UInt16)( a.m_address / ( sizeof( Int16 ) * b ) ) );
}
public static Pointer operator /( Pointer a, UInt16 b )
{
return new Pointer( (UInt16)( a.m_address / ( sizeof( UInt16 ) * b ) ) );
}
public static Pointer operator /( Pointer a, Int32 b )
{
return new Pointer( (UInt16)( a.m_address / ( sizeof( Int32 ) * b ) ) );
}
public static Pointer operator /( Pointer a, UInt32 b )
{
return new Pointer( (UInt16)( a.m_address / ( sizeof( UInt32 ) * b ) ) );
}
public static Pointer operator /( Pointer a, Int64 b )
{
return new Pointer( (UInt16)( a.m_address / ( sizeof( Int64 ) * b ) ) );
}
public static Pointer operator /( Pointer a, UInt64 b )
{
return new Pointer( (UInt16)( a.m_address / ( sizeof( UInt64 ) * b ) ) );
}
public static Pointer operator /( Pointer a, Pointer b )
{
return new Pointer( (UInt16)( a.m_address / b.m_address ) );
}
public static Pointer operator %( Pointer a, sbyte b )
{
return new Pointer( (UInt16)( a.m_address % ( sizeof( sbyte ) * b ) ) );
}
public static Pointer operator %( Pointer a, byte b )
{
return new Pointer( (UInt16)( a.m_address % ( sizeof( byte ) * b ) ) );
}
public static Pointer operator %( Pointer a, Int16 b )
{
return new Pointer( (UInt16)( a.m_address % ( sizeof( Int16 ) * b ) ) );
}
public static Pointer operator %( Pointer a, UInt16 b )
{
return new Pointer( (UInt16)( a.m_address % ( sizeof( UInt16 ) * b ) ) );
}
public static Pointer operator %( Pointer a, Int32 b )
{
return new Pointer( (UInt16)( a.m_address % ( sizeof( Int32 ) * b ) ) );
}
public static Pointer operator %( Pointer a, UInt32 b )
{
return new Pointer( (UInt16)( a.m_address % ( sizeof( UInt32 ) * b ) ) );
}
public static Pointer operator %( Pointer a, Int64 b )
{
return new Pointer( (UInt16)( a.m_address % ( sizeof( Int64 ) * b ) ) );
}
public static Pointer operator %( Pointer a, UInt64 b )
{
return new Pointer( (UInt16)( a.m_address % ( sizeof( UInt64 ) * b ) ) );
}
public static Pointer operator %( Pointer a, Pointer b )
{
return new Pointer( (UInt16)( a.m_address % b.m_address ) );
}
#endregion
}
}
using System;
namespace PointerTest
{
class Program
{
static void Main( string[] args )
{
Pointer loc = 0;
Pointer loc2 = loc + (UInt32)1;
Pointer loc3 = loc2 * (byte)2;
// Set values in memory.
loc |= (UInt32)0x12345678;
loc2 |= (UInt16)0x9ABC;
loc3 |= -102467;
// Need to initialize to stop the compiler complaining.
UInt32 a = 0;
UInt16 b = 0;
Int32 c = 0;
// Retrieve values.
a &= loc;
b &= loc2;
c &= loc3;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment