// /*************************************************************************** // Aaru Data Preservation Suite // ---------------------------------------------------------------------------- // // Filename : Marshal.cs // Author(s) : Natalia Portillo // // Component : Helpers. // // --[ Description ] ---------------------------------------------------------- // // Provides marshalling for binary data. // // --[ License ] -------------------------------------------------------------- // // This library is free software; you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as // published by the Free Software Foundation; either version 2.1 of the // License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, see . // // ---------------------------------------------------------------------------- // Copyright © 2011-2022 Natalia Portillo // ****************************************************************************/ namespace Aaru.Helpers; using System; using System.Diagnostics.CodeAnalysis; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; /// Provides methods to marshal binary data into C# structs public static class Marshal { /// Returns the size of an unmanaged type in bytes. /// The type whose size is to be returned. /// The size, in bytes, of the type that is specified by the generic type parameter. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int SizeOf() => System.Runtime.InteropServices.Marshal.SizeOf(); /// Marshal little-endian binary data to a structure /// Byte array containing the binary data /// Type of the structure to marshal /// The binary data marshalled in a structure with the specified type [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T ByteArrayToStructureLittleEndian(byte[] bytes) where T : struct { var ptr = GCHandle.Alloc(bytes, GCHandleType.Pinned); var str = (T)(System.Runtime.InteropServices.Marshal.PtrToStructure(ptr.AddrOfPinnedObject(), typeof(T)) ?? default(T)); ptr.Free(); return str; } /// Marshal little-endian binary data to a structure /// Byte array containing the binary data /// Start on the array where the structure begins /// Length of the structure in bytes /// Type of the structure to marshal /// The binary data marshalled in a structure with the specified type [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T ByteArrayToStructureLittleEndian(byte[] bytes, int start, int length) where T : struct { Span span = bytes; return ByteArrayToStructureLittleEndian(span.Slice(start, length).ToArray()); } /// Marshal big-endian binary data to a structure /// Byte array containing the binary data /// Type of the structure to marshal /// The binary data marshalled in a structure with the specified type [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T ByteArrayToStructureBigEndian(byte[] bytes) where T : struct { var ptr = GCHandle.Alloc(bytes, GCHandleType.Pinned); object str = (T)(System.Runtime.InteropServices.Marshal.PtrToStructure(ptr.AddrOfPinnedObject(), typeof(T)) ?? default(T)); ptr.Free(); return (T)SwapStructureMembersEndian(str); } /// Marshal big-endian binary data to a structure /// Byte array containing the binary data /// Start on the array where the structure begins /// Length of the structure in bytes /// Type of the structure to marshal /// The binary data marshalled in a structure with the specified type [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T ByteArrayToStructureBigEndian(byte[] bytes, int start, int length) where T : struct { Span span = bytes; return ByteArrayToStructureBigEndian(span.Slice(start, length).ToArray()); } /// Marshal PDP-11 binary data to a structure /// Byte array containing the binary data /// Type of the structure to marshal /// The binary data marshalled in a structure with the specified type [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T ByteArrayToStructurePdpEndian(byte[] bytes) where T : struct { { var ptr = GCHandle.Alloc(bytes, GCHandleType.Pinned); object str = (T)(System.Runtime.InteropServices.Marshal.PtrToStructure(ptr.AddrOfPinnedObject(), typeof(T)) ?? default(T)); ptr.Free(); return (T)SwapStructureMembersEndianPdp(str); } } /// Marshal PDP-11 binary data to a structure /// Byte array containing the binary data /// Start on the array where the structure begins /// Length of the structure in bytes /// Type of the structure to marshal /// The binary data marshalled in a structure with the specified type [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T ByteArrayToStructurePdpEndian(byte[] bytes, int start, int length) where T : struct { Span span = bytes; return ByteArrayToStructurePdpEndian(span.Slice(start, length).ToArray()); } /// /// Marshal little-endian binary data to a structure. If the structure type contains any non value type, this /// method will crash. /// /// Byte array containing the binary data /// Type of the structure to marshal /// The binary data marshalled in a structure with the specified type [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T SpanToStructureLittleEndian(ReadOnlySpan bytes) where T : struct => MemoryMarshal.Read(bytes); /// /// Marshal little-endian binary data to a structure. If the structure type contains any non value type, this /// method will crash. /// /// Byte span containing the binary data /// Start on the span where the structure begins /// Length of the structure in bytes /// Type of the structure to marshal /// The binary data marshalled in a structure with the specified type [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T SpanToStructureLittleEndian(ReadOnlySpan bytes, int start, int length) where T : struct => MemoryMarshal.Read(bytes.Slice(start, length)); /// /// Marshal big-endian binary data to a structure. If the structure type contains any non value type, this method /// will crash. /// /// Byte array containing the binary data /// Type of the structure to marshal /// The binary data marshalled in a structure with the specified type [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T SpanToStructureBigEndian(ReadOnlySpan bytes) where T : struct { T str = SpanToStructureLittleEndian(bytes); return (T)SwapStructureMembersEndian(str); } /// /// Marshal big-endian binary data to a structure. If the structure type contains any non value type, this method /// will crash. /// /// Byte span containing the binary data /// Start on the span where the structure begins /// Length of the structure in bytes /// Type of the structure to marshal /// The binary data marshalled in a structure with the specified type [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T SpanToStructureBigEndian(ReadOnlySpan bytes, int start, int length) where T : struct { T str = SpanToStructureLittleEndian(bytes.Slice(start, length)); return (T)SwapStructureMembersEndian(str); } /// /// Marshal PDP-11 binary data to a structure. If the structure type contains any non value type, this method will /// crash. /// /// Byte array containing the binary data /// Type of the structure to marshal /// The binary data marshalled in a structure with the specified type [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T SpanToStructurePdpEndian(ReadOnlySpan bytes) where T : struct { object str = SpanToStructureLittleEndian(bytes); return (T)SwapStructureMembersEndianPdp(str); } /// /// Marshal PDP-11 binary data to a structure. If the structure type contains any non value type, this method will /// crash. /// /// Byte array containing the binary data /// Start on the span where the structure begins /// Length of the structure in bytes /// Type of the structure to marshal /// The binary data marshalled in a structure with the specified type [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T SpanToStructurePdpEndian(ReadOnlySpan bytes, int start, int length) where T : struct { object str = SpanToStructureLittleEndian(bytes.Slice(start, length)); return (T)SwapStructureMembersEndianPdp(str); } /// /// Marshal a structure depending on the decoration of . If the /// decoration is not present it will marshal as a reference type containing little endian structure. /// /// Byte array containing the binary data /// Type of the structure to marshal /// The binary data marshalled in a structure with the specified type /// /// The contains an unsupported /// endian /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T MarshalStructure(byte[] bytes) where T : struct { if(typeof(T).GetCustomAttribute(typeof(MarshallingPropertiesAttribute)) is not MarshallingPropertiesAttribute properties) return ByteArrayToStructureLittleEndian(bytes); return properties.Endian switch { BitEndian.Little => properties.HasReferences ? ByteArrayToStructureLittleEndian(bytes) : SpanToStructureLittleEndian(bytes), BitEndian.Big => properties.HasReferences ? ByteArrayToStructureBigEndian(bytes) : SpanToStructureBigEndian(bytes), BitEndian.Pdp => properties.HasReferences ? ByteArrayToStructurePdpEndian(bytes) : SpanToStructurePdpEndian(bytes), _ => throw new ArgumentOutOfRangeException() }; } /// Swaps all members of a structure /// /// [MethodImpl(MethodImplOptions.AggressiveInlining), SuppressMessage("ReSharper", "InconsistentNaming")] public static object SwapStructureMembersEndian(object str) { Type t = str.GetType(); FieldInfo[] fieldInfo = t.GetFields(); foreach(FieldInfo fi in fieldInfo) if(fi.FieldType == typeof(short) || fi.FieldType.IsEnum && fi.FieldType.GetEnumUnderlyingType() == typeof(short)) { var x = (short)(fi.GetValue(str) ?? default(short)); fi.SetValue(str, (short)((x << 8) | ((x >> 8) & 0xFF))); } else if(fi.FieldType == typeof(int) || fi.FieldType.IsEnum && fi.FieldType.GetEnumUnderlyingType() == typeof(int)) { var x = (int)(fi.GetValue(str) ?? default(int)); x = (int)(((x << 8) & 0xFF00FF00) | (((uint)x >> 8) & 0xFF00FF)); fi.SetValue(str, (int)(((uint)x << 16) | (((uint)x >> 16) & 0xFFFF))); } else if(fi.FieldType == typeof(long) || fi.FieldType.IsEnum && fi.FieldType.GetEnumUnderlyingType() == typeof(long)) { var x = (long)(fi.GetValue(str) ?? default(long)); x = ((x & 0x00000000FFFFFFFF) << 32) | (long)(((ulong)x & 0xFFFFFFFF00000000) >> 32); x = ((x & 0x0000FFFF0000FFFF) << 16) | (long)(((ulong)x & 0xFFFF0000FFFF0000) >> 16); x = ((x & 0x00FF00FF00FF00FF) << 8) | (long)(((ulong)x & 0xFF00FF00FF00FF00) >> 8); fi.SetValue(str, x); } else if(fi.FieldType == typeof(ushort) || fi.FieldType.IsEnum && fi.FieldType.GetEnumUnderlyingType() == typeof(ushort)) { var x = (ushort)(fi.GetValue(str) ?? default(ushort)); fi.SetValue(str, (ushort)((x << 8) | (x >> 8))); } else if(fi.FieldType == typeof(uint) || fi.FieldType.IsEnum && fi.FieldType.GetEnumUnderlyingType() == typeof(uint)) { var x = (uint)(fi.GetValue(str) ?? default(uint)); x = ((x << 8) & 0xFF00FF00) | ((x >> 8) & 0xFF00FF); fi.SetValue(str, (x << 16) | (x >> 16)); } else if(fi.FieldType == typeof(ulong) || fi.FieldType.IsEnum && fi.FieldType.GetEnumUnderlyingType() == typeof(ulong)) { var x = (ulong)(fi.GetValue(str) ?? default(ulong)); x = ((x & 0x00000000FFFFFFFF) << 32) | ((x & 0xFFFFFFFF00000000) >> 32); x = ((x & 0x0000FFFF0000FFFF) << 16) | ((x & 0xFFFF0000FFFF0000) >> 16); x = ((x & 0x00FF00FF00FF00FF) << 8) | ((x & 0xFF00FF00FF00FF00) >> 8); fi.SetValue(str, x); } else if(fi.FieldType == typeof(float)) { var flt = (float)(fi.GetValue(str) ?? default(float)); byte[] flt_b = BitConverter.GetBytes(flt); fi.SetValue(str, BitConverter.ToSingle(new[] { flt_b[3], flt_b[2], flt_b[1], flt_b[0] }, 0)); } else if(fi.FieldType == typeof(double)) { var dbl = (double)(fi.GetValue(str) ?? default(double)); byte[] dbl_b = BitConverter.GetBytes(dbl); fi.SetValue(str, BitConverter.ToDouble(new[] { dbl_b[7], dbl_b[6], dbl_b[5], dbl_b[4], dbl_b[3], dbl_b[2], dbl_b[1], dbl_b[0] }, 0)); } else if(fi.FieldType == typeof(byte) || fi.FieldType == typeof(sbyte)) { // Do nothing, can't byteswap them! } else if(fi.FieldType == typeof(Guid)) { // TODO: Swap GUID } // TODO: Swap arrays else if(fi.FieldType.IsValueType && !fi.FieldType.IsEnum && !fi.FieldType.IsArray) { object obj = fi.GetValue(str); object strc = SwapStructureMembersEndian(obj); fi.SetValue(str, strc); } return str; } /// Swaps all fields in an structure considering them to follow PDP endian conventions /// Source structure /// Resulting structure [MethodImpl(MethodImplOptions.AggressiveInlining)] public static object SwapStructureMembersEndianPdp(object str) { Type t = str.GetType(); FieldInfo[] fieldInfo = t.GetFields(); foreach(FieldInfo fi in fieldInfo) if(fi.FieldType == typeof(short) || fi.FieldType == typeof(long) || fi.FieldType == typeof(ushort) || fi.FieldType == typeof(ulong) || fi.FieldType == typeof(float) || fi.FieldType == typeof(double) || fi.FieldType == typeof(byte) || fi.FieldType == typeof(sbyte) || fi.FieldType == typeof(Guid)) { // Do nothing } else if(fi.FieldType == typeof(int) || fi.FieldType.IsEnum && fi.FieldType.GetEnumUnderlyingType() == typeof(int)) { var x = (int)(fi.GetValue(str) ?? default(int)); fi.SetValue(str, ((x & 0xffffu) << 16) | ((x & 0xffff0000u) >> 16)); } else if(fi.FieldType == typeof(uint) || fi.FieldType.IsEnum && fi.FieldType.GetEnumUnderlyingType() == typeof(uint)) { var x = (uint)(fi.GetValue(str) ?? default(uint)); fi.SetValue(str, ((x & 0xffffu) << 16) | ((x & 0xffff0000u) >> 16)); } // TODO: Swap arrays else if(fi.FieldType.IsValueType && !fi.FieldType.IsEnum && !fi.FieldType.IsArray) { object obj = fi.GetValue(str); object strc = SwapStructureMembersEndianPdp(obj); fi.SetValue(str, strc); } return str; } /// Marshal a structure to little-endian binary data /// The structure you want to marshal to binary /// Type of the structure to marshal /// The byte array representing the given structure [MethodImpl(MethodImplOptions.AggressiveInlining)] public static byte[] StructureToByteArrayLittleEndian(T str) where T : struct { var buf = new byte[SizeOf()]; var ptr = GCHandle.Alloc(buf, GCHandleType.Pinned); System.Runtime.InteropServices.Marshal.StructureToPtr(str, ptr.AddrOfPinnedObject(), false); ptr.Free(); return buf; } /// Marshal a structure to little-endian binary data /// The structure you want to marshal to binary /// Type of the structure to marshal /// The byte array representing the given structure [MethodImpl(MethodImplOptions.AggressiveInlining)] public static byte[] StructureToByteArrayBigEndian(T str) where T : struct => StructureToByteArrayLittleEndian((T)SwapStructureMembersEndian(str)); /// Converts a hexadecimal string into a byte array /// Hexadecimal string /// Resulting byte array /// Number of output bytes processed public static int ConvertFromHexAscii(string hex, out byte[] outBuf) { outBuf = null; if(hex is null or "") return -1; var off = 0; if(hex[0] == '0' && (hex[1] == 'x' || hex[1] == 'X')) off = 2; outBuf = new byte[(hex.Length - off) / 2]; var count = 0; for(int i = off; i < hex.Length; i += 2) { char c = hex[i]; if(c is < '0' or > '9' and < 'A' or > 'F' and < 'a' or > 'f') break; c -= c switch { >= 'a' and <= 'f' => '\u0057', >= 'A' and <= 'F' => '\u0037', _ => '\u0030' }; outBuf[(i - off) / 2] = (byte)(c << 4); c = hex[i + 1]; if(c is < '0' or > '9' and < 'A' or > 'F' and < 'a' or > 'f') break; c -= c switch { >= 'a' and <= 'f' => '\u0057', >= 'A' and <= 'F' => '\u0037', _ => '\u0030' }; outBuf[(i - off) / 2] += (byte)c; count++; } return count; } }