From 6419fcf8ed65f2858141a162216aa923f91cb03f Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Tue, 27 Feb 2018 20:22:56 +0000 Subject: [PATCH] Add marshaler for little endian structures. --- libexeinfo/BigEndianMarshal.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/libexeinfo/BigEndianMarshal.cs b/libexeinfo/BigEndianMarshal.cs index b60aca9..6c87dc8 100644 --- a/libexeinfo/BigEndianMarshal.cs +++ b/libexeinfo/BigEndianMarshal.cs @@ -48,6 +48,20 @@ namespace libexeinfo return SwapStructureMembersEndian(str); } + /// + /// Marshals a little endian structure from a byte array. + /// + /// The structure. + /// Byte array. + /// Structure type. + public static T ByteArrayToStructureLittleEndian(byte[] bytes) where T : struct + { + GCHandle ptr = GCHandle.Alloc(bytes, GCHandleType.Pinned); + T str = (T)Marshal.PtrToStructure(ptr.AddrOfPinnedObject(), typeof(T)); + ptr.Free(); + return str; + } + /// /// Swaps endian of structure members that correspond to numerical types. /// Does not traverse nested structures.