mirror of
https://github.com/claunia/libexeinfo.git
synced 2025-12-16 19:14:24 +00:00
Add marshaler for little endian structures.
This commit is contained in:
@@ -48,6 +48,20 @@ namespace libexeinfo
|
||||
return SwapStructureMembersEndian(str);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Marshals a little endian structure from a byte array.
|
||||
/// </summary>
|
||||
/// <returns>The structure.</returns>
|
||||
/// <param name="bytes">Byte array.</param>
|
||||
/// <typeparam name="T">Structure type.</typeparam>
|
||||
public static T ByteArrayToStructureLittleEndian<T>(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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Swaps endian of structure members that correspond to numerical types.
|
||||
/// Does not traverse nested structures.
|
||||
|
||||
Reference in New Issue
Block a user