using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace Tools { namespace ByValData { namespace Fixed { namespace Special { public unsafe struct FixedSHA1Hash { public const int Size = 20; public fixed byte data[Size]; public FixedSHA1Hash(byte[] from) { fixed (byte* p = data) { Marshal.Copy(from, 0, (IntPtr)p, Math.Min(Size, from.Length)); } } public static implicit operator byte[](FixedSHA1Hash from) { return from.ToByteArrayUnmanaged(); } public static implicit operator FixedSHA1Hash(byte[] from) { return from.ToUnmanaged(); } } } public unsafe struct FixedByteArray1 { public const int Size = 1; public fixed byte data[Size]; public byte AsByte { get { fixed (byte* ptr = data) return *ptr; } set { fixed (byte* ptr = data) *ptr = value; } } public sbyte AsSByte { get { fixed (byte* ptr = data) return *(sbyte*)ptr; } set { fixed (byte* ptr = data) *(sbyte*)ptr = value; } } public FixedByteArray1(byte asByte) { AsByte = asByte; } public FixedByteArray1(sbyte asSbyte) { AsSByte = asSbyte; } public FixedByteArray1(byte[] from) { fixed (byte* p = data) { Marshal.Copy(from, 0, (IntPtr)p, Math.Min(Size, from.Length)); } } public static implicit operator byte[](FixedByteArray1 from) { return from.ToByteArrayUnmanaged(); } public static implicit operator FixedByteArray1(byte[] from) { return from.ToUnmanaged(); } } public unsafe struct FixedByteArray2 { public const int Size = 2; public fixed byte data[Size]; public short AsShort { get { fixed (byte* ptr = data) return *(short*)ptr; } set { fixed (byte* ptr = data) *(short*)ptr = value; } } public ushort AsUShort { get { fixed (byte* ptr = data) return *(ushort*)ptr; } set { fixed (byte* ptr = data) *(ushort*)ptr = value; } } public char AsChar { get { fixed (byte* ptr = data) return *(char*)ptr; } set { fixed (byte* ptr = data) *(char*)ptr = value; } } public FixedByteArray2(char fromChar) { AsChar = fromChar; } public FixedByteArray2(short fromShort) { AsShort = fromShort; } public FixedByteArray2(ushort fromUshort) { AsUShort = fromUshort; } public FixedByteArray2(byte[] from) { fixed (byte* p = data) { Marshal.Copy(from, 0, (IntPtr)p, Math.Min(Size, from.Length)); } } public static implicit operator byte[](FixedByteArray2 from) { return from.ToByteArrayUnmanaged(); } public static implicit operator FixedByteArray2(byte[] from) { return from.ToUnmanaged(); } } public unsafe struct FixedByteArray4 { public const int Size = 4; public fixed byte data[Size]; public int AsInt { get { fixed (byte* ptr = data) return *(int*)ptr; } set { fixed (byte* ptr = data) *(int*)ptr = value; } } public uint AsUInt { get { fixed (byte* ptr = data) return *(uint*)ptr; } set { fixed (byte* ptr = data) *(uint*)ptr = value; } } public bool AsBool { get { fixed (byte* ptr = data) return *(bool*)ptr; } set { fixed (byte* ptr = data) *(bool*)ptr = value; } } public float AsFloat { get { fixed (byte* ptr = data) return *(float*)ptr; } set { fixed (byte* ptr = data) *(float*)ptr = value; } } public FixedByteArray4(float fromFloat) { AsFloat = fromFloat; } public FixedByteArray4(bool fromBool) { AsBool = fromBool; } public FixedByteArray4(int fromInt) { AsInt = fromInt; } public FixedByteArray4(uint fromUint) { AsUInt = fromUint; } public FixedByteArray4(byte[] from) { fixed (byte* p = data) { Marshal.Copy(from, 0, (IntPtr)p, Math.Min(Size, from.Length)); } } public static implicit operator byte[](FixedByteArray4 from) { return from.ToByteArrayUnmanaged(); } public static implicit operator FixedByteArray4(byte[] from) { return from.ToUnmanaged(); } } public unsafe struct FixedByteArray8 { public const int Size = 8; public fixed byte data[Size]; public long AsLong { get { fixed (byte* ptr = data) return *(long*)ptr; } set { fixed (byte* ptr = data) *(long*)ptr = value; } } public ulong AsULong { get { fixed (byte* ptr = data) return *(ulong*)ptr; } set { fixed (byte* ptr = data) *(ulong*)ptr = value; } } public double AsDouble { get { fixed (byte* ptr = data) return *(double*)ptr; } set { fixed (byte* ptr = data) *(double*)ptr = value; } } public FixedByteArray8(double asDouble) { AsDouble = asDouble; } public FixedByteArray8(ulong asUlong) { AsULong = asUlong; } public FixedByteArray8(long asLong) { AsLong = asLong; } public FixedByteArray8(byte[] from) { fixed (byte* p = data) { Marshal.Copy(from, 0, (IntPtr)p, Math.Min(Size, from.Length)); } } public static implicit operator byte[](FixedByteArray8 from) { return from.ToByteArrayUnmanaged(); } public static implicit operator FixedByteArray8(byte[] from) { return from.ToUnmanaged(); } } public unsafe struct FixedByteArray16 { public const int Size = 16; public fixed byte data[Size]; public decimal AsDecimal { get { fixed (byte* ptr = data) return *(decimal*)ptr; } set { fixed (byte* ptr = data) *(decimal*)ptr = value; } } public FixedByteArray16(decimal from) { AsDecimal = from; } public FixedByteArray16(byte[] from) { fixed (byte* p = data) { Marshal.Copy(from, 0, (IntPtr)p, Math.Min(Size, from.Length)); } } public static implicit operator byte[](FixedByteArray16 from) { return from.ToByteArrayUnmanaged(); } public static implicit operator FixedByteArray16(byte[] from) { return from.ToUnmanaged(); } } public unsafe struct FixedByteArray32 { public const int Size = 32; public fixed byte data[Size]; public FixedByteArray32(byte[] from) { fixed (byte* p = data) { Marshal.Copy(from, 0, (IntPtr)p, Math.Min(Size, from.Length)); } } public static implicit operator byte[](FixedByteArray32 from) { return from.ToByteArrayUnmanaged(); } public static implicit operator FixedByteArray32(byte[] from) { return from.ToUnmanaged(); } } public unsafe struct FixedByteArray64 { public const int Size = 64; public fixed byte data[Size]; public FixedByteArray64(byte[] from) { fixed (byte* p = data) { Marshal.Copy(from, 0, (IntPtr)p, Math.Min(Size, from.Length)); } } public static implicit operator byte[](FixedByteArray64 from) { return from.ToByteArrayUnmanaged(); } public static implicit operator FixedByteArray64(byte[] from) { return from.ToUnmanaged(); } } public unsafe struct FixedByteArray128 { public const int Size = 128; public fixed byte data[Size]; public FixedByteArray128(byte[] from) { fixed (byte* p = data) { Marshal.Copy(from, 0, (IntPtr)p, Math.Min(Size, from.Length)); } } public static implicit operator byte[](FixedByteArray128 from) { return from.ToByteArrayUnmanaged(); } public static implicit operator FixedByteArray128(byte[] from) { return from.ToUnmanaged(); } } public unsafe struct FixedByteArray256 { public const int Size = 128; public fixed byte data[Size]; public FixedByteArray256(byte[] from) { fixed (byte* p = data) { Marshal.Copy(from, 0, (IntPtr)p, Math.Min(Size, from.Length)); } } public static implicit operator byte[](FixedByteArray256 from) { return from.ToByteArrayUnmanaged(); } public static implicit operator FixedByteArray256(byte[] from) { return from.ToUnmanaged(); } } public unsafe struct FixedByteArray512 { public const int Size = 512; public fixed byte data[Size]; public FixedByteArray512(byte[] from) { fixed (byte* p = data) { Marshal.Copy(from, 0, (IntPtr)p, Math.Min(Size, from.Length)); } } public static implicit operator byte[](FixedByteArray512 from) { return from.ToByteArrayUnmanaged(); } public static implicit operator FixedByteArray512(byte[] from) { return from.ToUnmanaged(); } } public unsafe struct FixedByteArray1024 { public const int Size = 1024; public fixed byte data[Size]; public FixedByteArray1024(byte[] from) { fixed (byte* p = data) { Marshal.Copy(from, 0, (IntPtr)p, Math.Min(Size, from.Length)); } } public static implicit operator byte[](FixedByteArray1024 from) { return from.ToByteArrayUnmanaged(); } public static implicit operator FixedByteArray1024(byte[] from) { return from.ToUnmanaged(); } } public unsafe struct FixedByteArray2048 { public const int Size = 2048; public fixed byte data[Size]; public FixedByteArray2048(byte[] from) { fixed (byte* p = data) { Marshal.Copy(from, 0, (IntPtr)p, Math.Min(Size, from.Length)); } } public static implicit operator byte[](FixedByteArray2048 from) { return from.ToByteArrayUnmanaged(); } public static implicit operator FixedByteArray2048(byte[] from) { return from.ToUnmanaged(); } } public unsafe struct FixedByteArray4096 { public const int Size = 4096; public fixed byte data[Size]; public FixedByteArray4096(byte[] from) { fixed (byte* p = data) { Marshal.Copy(from, 0, (IntPtr)p, Math.Min(Size, from.Length)); } } public static implicit operator byte[](FixedByteArray4096 from) { return from.ToByteArrayUnmanaged(); } public static implicit operator FixedByteArray4096(byte[] from) { return from.ToUnmanaged(); } } } } public static class ExBinTools { [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe byte[] ToByteArrayUnmanaged(this T t) where T : unmanaged { byte[] o = new byte[sizeof(T)]; fixed (byte* ptr = o) { *(T*)ptr = t; } return o; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe T ToUnmanaged(this byte[] bytes) where T : unmanaged { fixed (byte* ptr = bytes) { return *(T*)ptr; } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe ref T ZeroMemory(this ref T value) where T : unmanaged { byte[] buffer = new byte[sizeof(T)]; fixed (T* valuePtr = &value) { Marshal.Copy(buffer, 0, (IntPtr)valuePtr, sizeof(T)); } return ref value; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe ref T SetMemory(this ref T value, byte def=default) where T : unmanaged { byte[] buffer = new byte[sizeof(T)]; for (int i = 0; i < sizeof(T); i++) buffer[i] = def; fixed (T* valuePtr = &value) { Marshal.Copy(buffer, 0, (IntPtr)valuePtr, sizeof(T)); } return ref value; } } }