diff --git a/Aaru.Helpers/Marshal.cs b/Aaru.Helpers/Marshal.cs index 2e0c04f44..7585852a5 100644 --- a/Aaru.Helpers/Marshal.cs +++ b/Aaru.Helpers/Marshal.cs @@ -97,6 +97,40 @@ public static class Marshal return (T)SwapStructureMembersEndian(str); } + /// + /// Marshal big-endian binary data to a structure using compile-time generated swap method. + /// This method is faster than as it avoids boxing and reflection. + /// + /// Byte array containing the binary data + /// Type of the structure to marshal (must be marked with [SwapEndian] attribute) + /// The binary data marshalled in a structure with the specified type + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static T ByteArrayToStructureBigEndianGenerated(byte[] bytes) where T : struct, ISwapEndian + { + T str = ByteArrayToStructureLittleEndian(bytes); + + return str.SwapEndian(); + } + + /// + /// Marshal big-endian binary data to a structure using compile-time generated swap method. + /// This method is faster than as it avoids boxing + /// and reflection. + /// + /// 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 (must be marked with [SwapEndian] attribute) + /// The binary data marshalled in a structure with the specified type + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static T ByteArrayToStructureBigEndianGenerated(byte[] bytes, int start, int length) + where T : struct, ISwapEndian + { + Span span = bytes; + + return ByteArrayToStructureBigEndianGenerated(span.Slice(start, length).ToArray()); + } + /// Marshal big-endian binary data to a structure /// Byte array containing the binary data /// Start on the array where the structure begins @@ -185,20 +219,40 @@ public static class Marshal } /// - /// Marshal big-endian binary data to a structure. If the structure type contains any non value type, this method - /// will crash. + /// Marshal big-endian binary data to a structure using compile-time generated swap method. + /// This method is faster than as it avoids boxing and + /// reflection. + /// 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 (must be marked with [SwapEndian] attribute) + /// The binary data marshalled in a structure with the specified type + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static T SpanToStructureBigEndianGenerated(ReadOnlySpan bytes) where T : struct, ISwapEndian + { + T str = SpanToStructureLittleEndian(bytes); + + return str.SwapEndian(); + } + + /// + /// Marshal big-endian binary data to a structure using compile-time generated swap method. + /// This method is faster than as it avoids + /// boxing and reflection. + /// 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 + /// Type of the structure to marshal (must be marked with [SwapEndian] attribute) /// 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 + public static T SpanToStructureBigEndianGenerated(ReadOnlySpan bytes, int start, int length) + where T : struct, ISwapEndian { T str = SpanToStructureLittleEndian(bytes.Slice(start, length)); - return (T)SwapStructureMembersEndian(str); + return str.SwapEndian(); } ///