mirror of
https://github.com/aaru-dps/Aaru.git
synced 2025-12-16 19:24:25 +00:00
Add overrides to marshal with offset and length.
This commit is contained in:
@@ -39,8 +39,6 @@ namespace DiscImageChef.Helpers
|
|||||||
/// <summary>Provides methods to marshal binary data into C# structs</summary>
|
/// <summary>Provides methods to marshal binary data into C# structs</summary>
|
||||||
public static class Marshal
|
public static class Marshal
|
||||||
{
|
{
|
||||||
static int count;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Marshal little-endian binary data to a structure
|
/// Marshal little-endian binary data to a structure
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -56,6 +54,20 @@ namespace DiscImageChef.Helpers
|
|||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
public static T ByteArrayToStructureLittleEndian<T>(byte[] bytes, int start, int length) where T : struct
|
||||||
|
{
|
||||||
|
Span<byte> span = bytes;
|
||||||
|
return ByteArrayToStructureLittleEndian<T>(span.Slice(start, length).ToArray());
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Marshal big-endian binary data to a structure
|
/// Marshal big-endian binary data to a structure
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -71,6 +83,20 @@ namespace DiscImageChef.Helpers
|
|||||||
return (T)SwapStructureMembersEndian(str);
|
return (T)SwapStructureMembersEndian(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
public static T ByteArrayToStructureBigEndian<T>(byte[] bytes, int start, int length) where T : struct
|
||||||
|
{
|
||||||
|
Span<byte> span = bytes;
|
||||||
|
return ByteArrayToStructureBigEndian<T>(span.Slice(start, length).ToArray());
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Marshal PDP-11 binary data to a structure
|
/// Marshal PDP-11 binary data to a structure
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -88,6 +114,20 @@ namespace DiscImageChef.Helpers
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
public static T ByteArrayToStructurePdpEndian<T>(byte[] bytes, int start, int length) where T : struct
|
||||||
|
{
|
||||||
|
Span<byte> span = bytes;
|
||||||
|
return ByteArrayToStructurePdpEndian<T>(span.Slice(start, length).ToArray());
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Marshal little-endian binary data to a structure. If the structure type contains any non value type, this method
|
/// Marshal little-endian binary data to a structure. If the structure type contains any non value type, this method
|
||||||
/// will crash.
|
/// will crash.
|
||||||
@@ -98,6 +138,19 @@ namespace DiscImageChef.Helpers
|
|||||||
public static T SpanToStructureLittleEndian<T>(ReadOnlySpan<byte> bytes) where T : struct =>
|
public static T SpanToStructureLittleEndian<T>(ReadOnlySpan<byte> bytes) where T : struct =>
|
||||||
MemoryMarshal.Read<T>(bytes);
|
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>
|
||||||
|
public static T SpanToStructureLittleEndian<T>(ReadOnlySpan<byte> bytes, int start, int length)
|
||||||
|
where T : struct =>
|
||||||
|
MemoryMarshal.Read<T>(bytes.Slice(start, length));
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Marshal big-endian binary data to a structure. If the structure type contains any non value type, this method will
|
/// Marshal big-endian binary data to a structure. If the structure type contains any non value type, this method will
|
||||||
/// crash.
|
/// crash.
|
||||||
@@ -111,6 +164,21 @@ namespace DiscImageChef.Helpers
|
|||||||
return (T)SwapStructureMembersEndian(str);
|
return (T)SwapStructureMembersEndian(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
public static T SpanToStructureBigEndian<T>(ReadOnlySpan<byte> bytes, int start, int length) where T : struct
|
||||||
|
{
|
||||||
|
T str = SpanToStructureLittleEndian<T>(bytes.Slice(start, length));
|
||||||
|
return (T)SwapStructureMembersEndian(str);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Marshal PDP-11 binary data to a structure. If the structure type contains any non value type, this method will
|
/// Marshal PDP-11 binary data to a structure. If the structure type contains any non value type, this method will
|
||||||
/// crash.
|
/// crash.
|
||||||
@@ -124,6 +192,19 @@ namespace DiscImageChef.Helpers
|
|||||||
return (T)SwapStructureMembersEndianPdp(str);
|
return (T)SwapStructureMembersEndianPdp(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
public static T SpanToStructurePdpEndian<T>(ReadOnlySpan<byte> bytes, int start, int length) where T : struct
|
||||||
|
{
|
||||||
|
object str = SpanToStructureLittleEndian<T>(bytes.Slice(start, length));
|
||||||
|
return (T)SwapStructureMembersEndianPdp(str);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Marshal a structure depending on the decoration of <see cref="MarshallingPropertiesAttribute" />. If the decoration
|
/// 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.
|
/// 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
|
// TODO: Swap arrays and enums
|
||||||
else if(fi.FieldType.IsValueType && !fi.FieldType.IsEnum && !fi.FieldType.IsArray)
|
else if(fi.FieldType.IsValueType && !fi.FieldType.IsEnum && !fi.FieldType.IsArray)
|
||||||
{
|
{
|
||||||
System.Console.WriteLine("PDP {0}", count++);
|
|
||||||
|
|
||||||
object obj = fi.GetValue(str);
|
object obj = fi.GetValue(str);
|
||||||
object strc = SwapStructureMembersEndianPdp(obj);
|
object strc = SwapStructureMembersEndianPdp(obj);
|
||||||
fi.SetValue(str, strc);
|
fi.SetValue(str, strc);
|
||||||
|
|||||||
Reference in New Issue
Block a user