diff --git a/DiscImageChef.Helpers/Marshal.cs b/DiscImageChef.Helpers/Marshal.cs index bc0863473..86f6987ed 100644 --- a/DiscImageChef.Helpers/Marshal.cs +++ b/DiscImageChef.Helpers/Marshal.cs @@ -39,8 +39,6 @@ namespace DiscImageChef.Helpers /// Provides methods to marshal binary data into C# structs public static class Marshal { - static int count; - /// /// Marshal little-endian binary data to a structure /// @@ -56,6 +54,20 @@ namespace DiscImageChef.Helpers 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 + 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 /// @@ -71,6 +83,20 @@ namespace DiscImageChef.Helpers 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 + 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 /// @@ -88,6 +114,20 @@ namespace DiscImageChef.Helpers } } + /// + /// 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 + 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. @@ -98,6 +138,19 @@ namespace DiscImageChef.Helpers 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 + 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. @@ -111,6 +164,21 @@ namespace DiscImageChef.Helpers 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 + 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. @@ -124,6 +192,19 @@ namespace DiscImageChef.Helpers 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 + /// Type of the structure to marshal + /// The binary data marshalled in a structure with the specified type + 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. @@ -270,8 +351,6 @@ namespace DiscImageChef.Helpers // TODO: Swap arrays and enums else if(fi.FieldType.IsValueType && !fi.FieldType.IsEnum && !fi.FieldType.IsArray) { - System.Console.WriteLine("PDP {0}", count++); - object obj = fi.GetValue(str); object strc = SwapStructureMembersEndianPdp(obj); fi.SetValue(str, strc);