2019-02-27 08:49:42 +00:00
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:23 +00:00
|
|
|
// Aaru Data Preservation Suite
|
2019-02-27 08:49:42 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : Marshal.cs
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
//
|
|
|
|
|
// 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2022-02-18 10:02:41 +00:00
|
|
|
// Copyright © 2011-2022 Natalia Portillo
|
2019-02-27 08:49:42 +00:00
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
|
|
|
|
using System;
|
2021-08-17 16:27:42 +01:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2019-02-27 08:49:42 +00:00
|
|
|
using System.Reflection;
|
2019-03-11 19:24:06 +00:00
|
|
|
using System.Runtime.CompilerServices;
|
2019-02-27 08:49:42 +00:00
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
2022-11-15 15:58:41 +00:00
|
|
|
namespace Aaru.Helpers;
|
|
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
/// <summary>Provides methods to marshal binary data into C# structs</summary>
|
|
|
|
|
public static class Marshal
|
2019-02-27 08:49:42 +00:00
|
|
|
{
|
2022-03-06 13:29:37 +00:00
|
|
|
/// <summary>Returns the size of an unmanaged type in bytes.</summary>
|
|
|
|
|
/// <typeparam name="T">The type whose size is to be returned.</typeparam>
|
|
|
|
|
/// <returns>The size, in bytes, of the type that is specified by the <see cref="T" /> generic type parameter.</returns>
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static int SizeOf<T>() => System.Runtime.InteropServices.Marshal.SizeOf<T>();
|
|
|
|
|
|
|
|
|
|
/// <summary>Marshal little-endian binary data to a structure</summary>
|
|
|
|
|
/// <param name="bytes">Byte array containing the binary data</param>
|
|
|
|
|
/// <typeparam name="T">Type of the structure to marshal</typeparam>
|
|
|
|
|
/// <returns>The binary data marshalled in a structure with the specified type</returns>
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static T ByteArrayToStructureLittleEndian<T>(byte[] bytes) where T : struct
|
2019-02-27 08:49:42 +00:00
|
|
|
{
|
2022-03-06 13:29:37 +00:00
|
|
|
var ptr = GCHandle.Alloc(bytes, GCHandleType.Pinned);
|
2019-11-25 00:54:39 +00:00
|
|
|
|
2022-03-17 00:46:26 +00:00
|
|
|
var str = (T)(System.Runtime.InteropServices.Marshal.PtrToStructure(ptr.AddrOfPinnedObject(), typeof(T)) ??
|
|
|
|
|
default(T));
|
2019-11-25 00:54:39 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
ptr.Free();
|
2019-11-25 00:54:39 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
return str;
|
|
|
|
|
}
|
2019-02-27 08:49:42 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
/// <summary>Marshal little-endian binary data to a structure</summary>
|
|
|
|
|
/// <param name="bytes">Byte array containing the binary data</param>
|
|
|
|
|
/// <param name="start">Start on the array where the structure begins</param>
|
|
|
|
|
/// <param name="length">Length of the structure in bytes</param>
|
|
|
|
|
/// <typeparam name="T">Type of the structure to marshal</typeparam>
|
|
|
|
|
/// <returns>The binary data marshalled in a structure with the specified type</returns>
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static T ByteArrayToStructureLittleEndian<T>(byte[] bytes, int start, int length) where T : struct
|
|
|
|
|
{
|
|
|
|
|
Span<byte> span = bytes;
|
2019-11-25 00:54:39 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
return ByteArrayToStructureLittleEndian<T>(span.Slice(start, length).ToArray());
|
|
|
|
|
}
|
2019-02-28 10:35:40 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
/// <summary>Marshal big-endian binary data to a structure</summary>
|
|
|
|
|
/// <param name="bytes">Byte array containing the binary data</param>
|
|
|
|
|
/// <typeparam name="T">Type of the structure to marshal</typeparam>
|
|
|
|
|
/// <returns>The binary data marshalled in a structure with the specified type</returns>
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static T ByteArrayToStructureBigEndian<T>(byte[] bytes) where T : struct
|
|
|
|
|
{
|
|
|
|
|
var ptr = GCHandle.Alloc(bytes, GCHandleType.Pinned);
|
2019-11-25 00:54:39 +00:00
|
|
|
|
2022-03-17 00:46:26 +00:00
|
|
|
object str = (T)(System.Runtime.InteropServices.Marshal.PtrToStructure(ptr.AddrOfPinnedObject(), typeof(T)) ??
|
|
|
|
|
default(T));
|
2019-11-25 00:54:39 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
ptr.Free();
|
2019-11-25 00:54:39 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
return (T)SwapStructureMembersEndian(str);
|
|
|
|
|
}
|
2019-02-27 08:49:42 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
/// <summary>Marshal big-endian binary data to a structure</summary>
|
|
|
|
|
/// <param name="bytes">Byte array containing the binary data</param>
|
|
|
|
|
/// <param name="start">Start on the array where the structure begins</param>
|
|
|
|
|
/// <param name="length">Length of the structure in bytes</param>
|
|
|
|
|
/// <typeparam name="T">Type of the structure to marshal</typeparam>
|
|
|
|
|
/// <returns>The binary data marshalled in a structure with the specified type</returns>
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static T ByteArrayToStructureBigEndian<T>(byte[] bytes, int start, int length) where T : struct
|
|
|
|
|
{
|
|
|
|
|
Span<byte> span = bytes;
|
2019-11-25 00:54:39 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
return ByteArrayToStructureBigEndian<T>(span.Slice(start, length).ToArray());
|
|
|
|
|
}
|
2019-02-28 10:35:40 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
/// <summary>Marshal PDP-11 binary data to a structure</summary>
|
|
|
|
|
/// <param name="bytes">Byte array containing the binary data</param>
|
|
|
|
|
/// <typeparam name="T">Type of the structure to marshal</typeparam>
|
|
|
|
|
/// <returns>The binary data marshalled in a structure with the specified type</returns>
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static T ByteArrayToStructurePdpEndian<T>(byte[] bytes) where T : struct
|
|
|
|
|
{
|
2019-02-27 08:49:42 +00:00
|
|
|
{
|
2022-03-06 13:29:37 +00:00
|
|
|
var ptr = GCHandle.Alloc(bytes, GCHandleType.Pinned);
|
2019-11-25 00:54:39 +00:00
|
|
|
|
2022-03-17 00:46:26 +00:00
|
|
|
object str =
|
|
|
|
|
(T)(System.Runtime.InteropServices.Marshal.PtrToStructure(ptr.AddrOfPinnedObject(), typeof(T)) ??
|
|
|
|
|
default(T));
|
2019-11-25 00:54:39 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
ptr.Free();
|
2019-11-25 00:54:39 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
return (T)SwapStructureMembersEndianPdp(str);
|
2019-02-27 08:49:42 +00:00
|
|
|
}
|
2022-03-06 13:29:37 +00:00
|
|
|
}
|
2019-02-27 08:49:42 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
/// <summary>Marshal PDP-11 binary data to a structure</summary>
|
|
|
|
|
/// <param name="bytes">Byte array containing the binary data</param>
|
|
|
|
|
/// <param name="start">Start on the array where the structure begins</param>
|
|
|
|
|
/// <param name="length">Length of the structure in bytes</param>
|
|
|
|
|
/// <typeparam name="T">Type of the structure to marshal</typeparam>
|
|
|
|
|
/// <returns>The binary data marshalled in a structure with the specified type</returns>
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static T ByteArrayToStructurePdpEndian<T>(byte[] bytes, int start, int length) where T : struct
|
|
|
|
|
{
|
|
|
|
|
Span<byte> span = bytes;
|
2019-11-25 00:54:39 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
return ByteArrayToStructurePdpEndian<T>(span.Slice(start, length).ToArray());
|
|
|
|
|
}
|
2019-02-28 10:35:40 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Marshal little-endian binary data to a structure. If the structure type contains any non value type, this
|
|
|
|
|
/// method will crash.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="bytes">Byte array containing the binary data</param>
|
|
|
|
|
/// <typeparam name="T">Type of the structure to marshal</typeparam>
|
|
|
|
|
/// <returns>The binary data marshalled in a structure with the specified type</returns>
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static T SpanToStructureLittleEndian<T>(ReadOnlySpan<byte> bytes) where T : struct =>
|
|
|
|
|
MemoryMarshal.Read<T>(bytes);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Marshal little-endian binary data to a structure. If the structure type contains any non value type, this
|
|
|
|
|
/// method will crash.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="bytes">Byte span containing the binary data</param>
|
|
|
|
|
/// <param name="start">Start on the span where the structure begins</param>
|
|
|
|
|
/// <param name="length">Length of the structure in bytes</param>
|
|
|
|
|
/// <typeparam name="T">Type of the structure to marshal</typeparam>
|
|
|
|
|
/// <returns>The binary data marshalled in a structure with the specified type</returns>
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2022-03-07 07:36:42 +00:00
|
|
|
public static T SpanToStructureLittleEndian<T>(ReadOnlySpan<byte> bytes, int start, int length) where T : struct =>
|
|
|
|
|
MemoryMarshal.Read<T>(bytes.Slice(start, length));
|
2022-03-06 13:29:37 +00:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Marshal big-endian binary data to a structure. If the structure type contains any non value type, this method
|
|
|
|
|
/// will crash.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="bytes">Byte array containing the binary data</param>
|
|
|
|
|
/// <typeparam name="T">Type of the structure to marshal</typeparam>
|
|
|
|
|
/// <returns>The binary data marshalled in a structure with the specified type</returns>
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static T SpanToStructureBigEndian<T>(ReadOnlySpan<byte> bytes) where T : struct
|
|
|
|
|
{
|
|
|
|
|
T str = SpanToStructureLittleEndian<T>(bytes);
|
2019-11-25 00:54:39 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
return (T)SwapStructureMembersEndian(str);
|
|
|
|
|
}
|
2019-02-27 08:49:42 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Marshal big-endian binary data to a structure. If the structure type contains any non value type, this method
|
|
|
|
|
/// will crash.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="bytes">Byte span containing the binary data</param>
|
|
|
|
|
/// <param name="start">Start on the span where the structure begins</param>
|
|
|
|
|
/// <param name="length">Length of the structure in bytes</param>
|
|
|
|
|
/// <typeparam name="T">Type of the structure to marshal</typeparam>
|
|
|
|
|
/// <returns>The binary data marshalled in a structure with the specified type</returns>
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static T SpanToStructureBigEndian<T>(ReadOnlySpan<byte> bytes, int start, int length) where T : struct
|
|
|
|
|
{
|
|
|
|
|
T str = SpanToStructureLittleEndian<T>(bytes.Slice(start, length));
|
2019-11-25 00:54:39 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
return (T)SwapStructureMembersEndian(str);
|
|
|
|
|
}
|
2019-02-28 10:35:40 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Marshal PDP-11 binary data to a structure. If the structure type contains any non value type, this method will
|
|
|
|
|
/// crash.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="bytes">Byte array containing the binary data</param>
|
|
|
|
|
/// <typeparam name="T">Type of the structure to marshal</typeparam>
|
|
|
|
|
/// <returns>The binary data marshalled in a structure with the specified type</returns>
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static T SpanToStructurePdpEndian<T>(ReadOnlySpan<byte> bytes) where T : struct
|
|
|
|
|
{
|
|
|
|
|
object str = SpanToStructureLittleEndian<T>(bytes);
|
2019-11-25 00:54:39 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
return (T)SwapStructureMembersEndianPdp(str);
|
|
|
|
|
}
|
2019-02-27 08:49:42 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Marshal PDP-11 binary data to a structure. If the structure type contains any non value type, this method will
|
|
|
|
|
/// crash.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="bytes">Byte array containing the binary data</param>
|
|
|
|
|
/// <param name="start">Start on the span where the structure begins</param>
|
|
|
|
|
/// <param name="length">Length of the structure in bytes</param>
|
|
|
|
|
/// <typeparam name="T">Type of the structure to marshal</typeparam>
|
|
|
|
|
/// <returns>The binary data marshalled in a structure with the specified type</returns>
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static T SpanToStructurePdpEndian<T>(ReadOnlySpan<byte> bytes, int start, int length) where T : struct
|
|
|
|
|
{
|
|
|
|
|
object str = SpanToStructureLittleEndian<T>(bytes.Slice(start, length));
|
2019-11-25 00:54:39 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
return (T)SwapStructureMembersEndianPdp(str);
|
|
|
|
|
}
|
2019-02-28 10:35:40 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Marshal a structure depending on the decoration of <see cref="MarshallingPropertiesAttribute" />. If the
|
|
|
|
|
/// decoration is not present it will marshal as a reference type containing little endian structure.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="bytes">Byte array containing the binary data</param>
|
|
|
|
|
/// <typeparam name="T">Type of the structure to marshal</typeparam>
|
|
|
|
|
/// <returns>The binary data marshalled in a structure with the specified type</returns>
|
|
|
|
|
/// <exception cref="ArgumentOutOfRangeException">
|
|
|
|
|
/// The <see cref="MarshallingPropertiesAttribute" /> contains an unsupported
|
|
|
|
|
/// endian
|
|
|
|
|
/// </exception>
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static T MarshalStructure<T>(byte[] bytes) where T : struct
|
|
|
|
|
{
|
2022-11-14 01:20:28 +00:00
|
|
|
if(typeof(T).GetCustomAttribute(typeof(MarshallingPropertiesAttribute)) is not MarshallingPropertiesAttribute
|
|
|
|
|
properties)
|
2022-03-06 13:29:37 +00:00
|
|
|
return ByteArrayToStructureLittleEndian<T>(bytes);
|
|
|
|
|
|
2022-11-13 19:59:23 +00:00
|
|
|
return properties.Endian switch
|
2022-11-15 15:58:41 +00:00
|
|
|
{
|
|
|
|
|
BitEndian.Little => properties.HasReferences ? ByteArrayToStructureLittleEndian<T>(bytes)
|
|
|
|
|
: SpanToStructureLittleEndian<T>(bytes),
|
|
|
|
|
BitEndian.Big => properties.HasReferences ? ByteArrayToStructureBigEndian<T>(bytes)
|
|
|
|
|
: SpanToStructureBigEndian<T>(bytes),
|
|
|
|
|
BitEndian.Pdp => properties.HasReferences ? ByteArrayToStructurePdpEndian<T>(bytes)
|
|
|
|
|
: SpanToStructurePdpEndian<T>(bytes),
|
|
|
|
|
_ => throw new ArgumentOutOfRangeException()
|
|
|
|
|
};
|
2022-03-06 13:29:37 +00:00
|
|
|
}
|
2019-02-27 08:49:42 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
/// <summary>Swaps all members of a structure</summary>
|
|
|
|
|
/// <param name="str"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[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) ||
|
2022-11-15 15:58:41 +00:00
|
|
|
(fi.FieldType.IsEnum && fi.FieldType.GetEnumUnderlyingType() == typeof(short)))
|
2019-02-27 08:49:42 +00:00
|
|
|
{
|
2022-11-15 15:58:41 +00:00
|
|
|
short x = (short)(fi.GetValue(str) ?? default(short));
|
2022-03-06 13:29:37 +00:00
|
|
|
fi.SetValue(str, (short)((x << 8) | ((x >> 8) & 0xFF)));
|
2019-02-27 08:49:42 +00:00
|
|
|
}
|
2022-03-06 13:29:37 +00:00
|
|
|
else if(fi.FieldType == typeof(int) ||
|
2022-11-15 15:58:41 +00:00
|
|
|
(fi.FieldType.IsEnum && fi.FieldType.GetEnumUnderlyingType() == typeof(int)))
|
2022-03-06 13:29:37 +00:00
|
|
|
{
|
2022-11-15 15:58:41 +00:00
|
|
|
int x = (int)(fi.GetValue(str) ?? default(int));
|
2022-03-06 13:29:37 +00:00
|
|
|
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) ||
|
2022-11-15 15:58:41 +00:00
|
|
|
(fi.FieldType.IsEnum && fi.FieldType.GetEnumUnderlyingType() == typeof(long)))
|
2022-03-06 13:29:37 +00:00
|
|
|
{
|
2022-11-15 15:58:41 +00:00
|
|
|
long x = (long)(fi.GetValue(str) ?? default(long));
|
2022-03-06 13:29:37 +00:00
|
|
|
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);
|
2019-02-27 08:49:42 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
fi.SetValue(str, x);
|
|
|
|
|
}
|
|
|
|
|
else if(fi.FieldType == typeof(ushort) ||
|
2022-11-15 15:58:41 +00:00
|
|
|
(fi.FieldType.IsEnum && fi.FieldType.GetEnumUnderlyingType() == typeof(ushort)))
|
2022-03-06 13:29:37 +00:00
|
|
|
{
|
2022-11-15 15:58:41 +00:00
|
|
|
ushort x = (ushort)(fi.GetValue(str) ?? default(ushort));
|
2022-03-06 13:29:37 +00:00
|
|
|
fi.SetValue(str, (ushort)((x << 8) | (x >> 8)));
|
|
|
|
|
}
|
|
|
|
|
else if(fi.FieldType == typeof(uint) ||
|
2022-11-15 15:58:41 +00:00
|
|
|
(fi.FieldType.IsEnum && fi.FieldType.GetEnumUnderlyingType() == typeof(uint)))
|
2022-03-06 13:29:37 +00:00
|
|
|
{
|
2022-11-15 15:58:41 +00:00
|
|
|
uint x = (uint)(fi.GetValue(str) ?? default(uint));
|
2022-03-06 13:29:37 +00:00
|
|
|
x = ((x << 8) & 0xFF00FF00) | ((x >> 8) & 0xFF00FF);
|
|
|
|
|
fi.SetValue(str, (x << 16) | (x >> 16));
|
|
|
|
|
}
|
|
|
|
|
else if(fi.FieldType == typeof(ulong) ||
|
2022-11-15 15:58:41 +00:00
|
|
|
(fi.FieldType.IsEnum && fi.FieldType.GetEnumUnderlyingType() == typeof(ulong)))
|
2022-03-06 13:29:37 +00:00
|
|
|
{
|
2022-11-15 15:58:41 +00:00
|
|
|
ulong x = (ulong)(fi.GetValue(str) ?? default(ulong));
|
2022-03-06 13:29:37 +00:00
|
|
|
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))
|
|
|
|
|
{
|
2022-11-15 15:58:41 +00:00
|
|
|
float flt = (float)(fi.GetValue(str) ?? default(float));
|
2022-03-06 13:29:37 +00:00
|
|
|
byte[] flt_b = BitConverter.GetBytes(flt);
|
2019-11-25 00:54:39 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
fi.SetValue(str, BitConverter.ToSingle(new[]
|
2019-02-27 08:49:42 +00:00
|
|
|
{
|
2022-03-06 13:29:37 +00:00
|
|
|
flt_b[3], flt_b[2], flt_b[1], flt_b[0]
|
|
|
|
|
}, 0));
|
|
|
|
|
}
|
|
|
|
|
else if(fi.FieldType == typeof(double))
|
|
|
|
|
{
|
2022-11-15 15:58:41 +00:00
|
|
|
double dbl = (double)(fi.GetValue(str) ?? default(double));
|
2022-03-06 13:29:37 +00:00
|
|
|
byte[] dbl_b = BitConverter.GetBytes(dbl);
|
2019-11-25 00:54:39 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
fi.SetValue(str, BitConverter.ToDouble(new[]
|
2019-02-27 08:49:42 +00:00
|
|
|
{
|
2022-03-06 13:29:37 +00:00
|
|
|
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
|
|
|
|
|
}
|
2019-02-27 08:49:42 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
// TODO: Swap arrays
|
|
|
|
|
else if(fi.FieldType.IsValueType &&
|
2022-11-15 15:58:41 +00:00
|
|
|
fi.FieldType is { IsEnum: false, IsArray: false })
|
2022-03-06 13:29:37 +00:00
|
|
|
{
|
|
|
|
|
object obj = fi.GetValue(str);
|
|
|
|
|
object strc = SwapStructureMembersEndian(obj);
|
|
|
|
|
fi.SetValue(str, strc);
|
|
|
|
|
}
|
2019-02-27 08:49:42 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
return str;
|
|
|
|
|
}
|
2019-02-27 08:49:42 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
/// <summary>Swaps all fields in an structure considering them to follow PDP endian conventions</summary>
|
|
|
|
|
/// <param name="str">Source structure</param>
|
|
|
|
|
/// <returns>Resulting structure</returns>
|
|
|
|
|
[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) ||
|
2022-11-15 15:58:41 +00:00
|
|
|
(fi.FieldType.IsEnum && fi.FieldType.GetEnumUnderlyingType() == typeof(int)))
|
2022-03-06 13:29:37 +00:00
|
|
|
{
|
2022-11-15 15:58:41 +00:00
|
|
|
int x = (int)(fi.GetValue(str) ?? default(int));
|
2022-03-06 13:29:37 +00:00
|
|
|
fi.SetValue(str, ((x & 0xffffu) << 16) | ((x & 0xffff0000u) >> 16));
|
|
|
|
|
}
|
|
|
|
|
else if(fi.FieldType == typeof(uint) ||
|
2022-11-15 15:58:41 +00:00
|
|
|
(fi.FieldType.IsEnum && fi.FieldType.GetEnumUnderlyingType() == typeof(uint)))
|
2022-03-06 13:29:37 +00:00
|
|
|
{
|
2022-11-15 15:58:41 +00:00
|
|
|
uint x = (uint)(fi.GetValue(str) ?? default(uint));
|
2022-03-06 13:29:37 +00:00
|
|
|
fi.SetValue(str, ((x & 0xffffu) << 16) | ((x & 0xffff0000u) >> 16));
|
|
|
|
|
}
|
2019-10-12 22:13:33 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
// TODO: Swap arrays
|
|
|
|
|
else if(fi.FieldType.IsValueType &&
|
2022-11-15 15:58:41 +00:00
|
|
|
fi.FieldType is { IsEnum: false, IsArray: false })
|
2022-03-06 13:29:37 +00:00
|
|
|
{
|
|
|
|
|
object obj = fi.GetValue(str);
|
|
|
|
|
object strc = SwapStructureMembersEndianPdp(obj);
|
|
|
|
|
fi.SetValue(str, strc);
|
|
|
|
|
}
|
2019-11-25 00:54:39 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
return str;
|
|
|
|
|
}
|
2020-02-19 03:30:39 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
/// <summary>Marshal a structure to little-endian binary data</summary>
|
|
|
|
|
/// <param name="str">The structure you want to marshal to binary</param>
|
|
|
|
|
/// <typeparam name="T">Type of the structure to marshal</typeparam>
|
|
|
|
|
/// <returns>The byte array representing the given structure</returns>
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static byte[] StructureToByteArrayLittleEndian<T>(T str) where T : struct
|
|
|
|
|
{
|
2022-11-15 15:58:41 +00:00
|
|
|
byte[] buf = new byte[SizeOf<T>()];
|
|
|
|
|
var ptr = GCHandle.Alloc(buf, GCHandleType.Pinned);
|
2022-03-06 13:29:37 +00:00
|
|
|
System.Runtime.InteropServices.Marshal.StructureToPtr(str, ptr.AddrOfPinnedObject(), false);
|
|
|
|
|
ptr.Free();
|
2020-12-08 00:56:31 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
return buf;
|
|
|
|
|
}
|
2020-12-08 01:18:44 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
/// <summary>Marshal a structure to little-endian binary data</summary>
|
|
|
|
|
/// <param name="str">The structure you want to marshal to binary</param>
|
|
|
|
|
/// <typeparam name="T">Type of the structure to marshal</typeparam>
|
|
|
|
|
/// <returns>The byte array representing the given structure</returns>
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static byte[] StructureToByteArrayBigEndian<T>(T str) where T : struct =>
|
|
|
|
|
StructureToByteArrayLittleEndian((T)SwapStructureMembersEndian(str));
|
|
|
|
|
|
|
|
|
|
/// <summary>Converts a hexadecimal string into a byte array</summary>
|
|
|
|
|
/// <param name="hex">Hexadecimal string</param>
|
|
|
|
|
/// <param name="outBuf">Resulting byte array</param>
|
|
|
|
|
/// <returns>Number of output bytes processed</returns>
|
|
|
|
|
public static int ConvertFromHexAscii(string hex, out byte[] outBuf)
|
|
|
|
|
{
|
|
|
|
|
outBuf = null;
|
2020-12-08 00:56:31 +00:00
|
|
|
|
2022-03-16 11:47:00 +00:00
|
|
|
if(hex is null or "")
|
2022-03-06 13:29:37 +00:00
|
|
|
return -1;
|
2020-12-08 01:18:44 +00:00
|
|
|
|
2022-11-15 15:58:41 +00:00
|
|
|
int off = 0;
|
2020-12-08 01:18:44 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
if(hex[0] == '0' &&
|
|
|
|
|
(hex[1] == 'x' || hex[1] == 'X'))
|
|
|
|
|
off = 2;
|
2020-12-08 01:18:44 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
outBuf = new byte[(hex.Length - off) / 2];
|
2022-11-15 15:58:41 +00:00
|
|
|
int count = 0;
|
2020-12-08 01:18:44 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
for(int i = off; i < hex.Length; i += 2)
|
|
|
|
|
{
|
|
|
|
|
char c = hex[i];
|
2020-12-08 01:18:44 +00:00
|
|
|
|
2022-11-14 01:06:06 +00:00
|
|
|
if(c is < '0' or > '9' and < 'A' or > 'F' and < 'a' or > 'f')
|
2022-03-06 13:29:37 +00:00
|
|
|
break;
|
2020-12-08 01:18:44 +00:00
|
|
|
|
2022-11-14 01:06:06 +00:00
|
|
|
c -= c switch
|
2022-11-15 15:58:41 +00:00
|
|
|
{
|
|
|
|
|
>= 'a' and <= 'f' => '\u0057',
|
|
|
|
|
>= 'A' and <= 'F' => '\u0037',
|
|
|
|
|
_ => '\u0030'
|
|
|
|
|
};
|
2020-12-08 01:18:44 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
outBuf[(i - off) / 2] = (byte)(c << 4);
|
2020-12-08 01:18:44 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
c = hex[i + 1];
|
2020-12-08 01:18:44 +00:00
|
|
|
|
2022-11-14 01:06:06 +00:00
|
|
|
if(c is < '0' or > '9' and < 'A' or > 'F' and < 'a' or > 'f')
|
2022-03-06 13:29:37 +00:00
|
|
|
break;
|
2020-12-08 01:18:44 +00:00
|
|
|
|
2022-11-14 01:06:06 +00:00
|
|
|
c -= c switch
|
2022-11-15 15:58:41 +00:00
|
|
|
{
|
|
|
|
|
>= 'a' and <= 'f' => '\u0057',
|
|
|
|
|
>= 'A' and <= 'F' => '\u0037',
|
|
|
|
|
_ => '\u0030'
|
|
|
|
|
};
|
2022-03-06 13:29:37 +00:00
|
|
|
|
|
|
|
|
outBuf[(i - off) / 2] += (byte)c;
|
2020-12-08 00:56:31 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
count++;
|
2020-12-08 00:56:31 +00:00
|
|
|
}
|
2022-03-06 13:29:37 +00:00
|
|
|
|
|
|
|
|
return count;
|
2019-02-27 08:49:42 +00:00
|
|
|
}
|
|
|
|
|
}
|