From f73ff879220e8480af250a004d5adf179d4da35a Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Tue, 12 Nov 2024 12:31:23 -0500 Subject: [PATCH] Make CHD headers (mostly) serializable --- SabreTools.Models/CHD/Header.cs | 10 +++++++--- SabreTools.Models/CHD/HeaderV1.cs | 27 +++++++++++++++++---------- SabreTools.Models/CHD/HeaderV2.cs | 29 ++++++++++++++++++----------- SabreTools.Models/CHD/HeaderV3.cs | 31 ++++++++++++++++++++----------- SabreTools.Models/CHD/HeaderV4.cs | 28 ++++++++++++++++++---------- SabreTools.Models/CHD/HeaderV5.cs | 5 ++++- 6 files changed, 84 insertions(+), 46 deletions(-) diff --git a/SabreTools.Models/CHD/Header.cs b/SabreTools.Models/CHD/Header.cs index 630bf0f..2e97aca 100644 --- a/SabreTools.Models/CHD/Header.cs +++ b/SabreTools.Models/CHD/Header.cs @@ -1,21 +1,25 @@ +using System.Runtime.InteropServices; + namespace SabreTools.Models.CHD { /// + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public abstract class Header { /// /// 'MComprHD' /// - public string? Tag { get; set; } + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)] + public string? Tag; /// /// Length of header (including tag and length fields) /// - public uint Length { get; set; } + public uint Length; /// /// Drive format version /// - public uint Version { get; set; } + public uint Version; } } \ No newline at end of file diff --git a/SabreTools.Models/CHD/HeaderV1.cs b/SabreTools.Models/CHD/HeaderV1.cs index 6afaceb..ff73e89 100644 --- a/SabreTools.Models/CHD/HeaderV1.cs +++ b/SabreTools.Models/CHD/HeaderV1.cs @@ -1,51 +1,58 @@ -namespace SabreTools.Models.CHD +using System.Runtime.InteropServices; + +namespace SabreTools.Models.CHD { /// + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public class HeaderV1 : Header { /// /// Flags /// - public Flags Flags { get; set; } + [MarshalAs(UnmanagedType.U4)] + public Flags Flags; /// /// Compression type /// - public CompressionType Compression { get; set; } + [MarshalAs(UnmanagedType.U4)] + public CompressionType Compression; /// /// 512-byte sectors per hunk /// - public uint HunkSize { get; set; } + public uint HunkSize; /// /// Total # of hunks represented /// - public uint TotalHunks { get; set; } + public uint TotalHunks; /// /// Number of cylinders on hard disk /// - public uint Cylinders { get; set; } + public uint Cylinders; /// /// Number of heads on hard disk /// - public uint Heads { get; set; } + public uint Heads; /// /// Number of sectors on hard disk /// - public uint Sectors { get; set; } + public uint Sectors; /// /// MD5 checksum of raw data /// - public byte[]? MD5 { get; set; } = new byte[16]; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] + public byte[] MD5 = new byte[16]; /// /// MD5 checksum of parent file /// - public byte[]? ParentMD5 { get; set; } = new byte[16]; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] + public byte[] ParentMD5 = new byte[16]; } } diff --git a/SabreTools.Models/CHD/HeaderV2.cs b/SabreTools.Models/CHD/HeaderV2.cs index aac4134..873633c 100644 --- a/SabreTools.Models/CHD/HeaderV2.cs +++ b/SabreTools.Models/CHD/HeaderV2.cs @@ -1,56 +1,63 @@ -namespace SabreTools.Models.CHD +using System.Runtime.InteropServices; + +namespace SabreTools.Models.CHD { /// + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public class HeaderV2 : Header { /// /// Flags /// - public Flags Flags { get; set; } + [MarshalAs(UnmanagedType.U4)] + public Flags Flags; /// /// Compression type /// - public CompressionType Compression { get; set; } + [MarshalAs(UnmanagedType.U4)] + public CompressionType Compression; /// /// Seclen-byte sectors per hunk /// - public uint HunkSize { get; set; } + public uint HunkSize; /// /// Total # of hunks represented /// - public uint TotalHunks { get; set; } + public uint TotalHunks; /// /// Number of cylinders on hard disk /// - public uint Cylinders { get; set; } + public uint Cylinders; /// /// Number of heads on hard disk /// - public uint Heads { get; set; } + public uint Heads; /// /// Number of sectors on hard disk /// - public uint Sectors { get; set; } + public uint Sectors; /// /// MD5 checksum of raw data /// - public byte[]? MD5 { get; set; } = new byte[16]; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] + public byte[]? MD5 = new byte[16]; /// /// MD5 checksum of parent file /// - public byte[]? ParentMD5 { get; set; } = new byte[16]; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] + public byte[]? ParentMD5 = new byte[16]; /// /// Number of bytes per sector /// - public uint BytesPerSector { get; set; } + public uint BytesPerSector; } } diff --git a/SabreTools.Models/CHD/HeaderV3.cs b/SabreTools.Models/CHD/HeaderV3.cs index 92af8f7..9b235ab 100644 --- a/SabreTools.Models/CHD/HeaderV3.cs +++ b/SabreTools.Models/CHD/HeaderV3.cs @@ -1,56 +1,65 @@ -namespace SabreTools.Models.CHD +using System.Runtime.InteropServices; + +namespace SabreTools.Models.CHD { /// + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public class HeaderV3 : Header { /// /// Flags /// - public Flags Flags { get; set; } + [MarshalAs(UnmanagedType.U4)] + public Flags Flags; /// /// Compression type /// - public CompressionType Compression { get; set; } + [MarshalAs(UnmanagedType.U4)] + public CompressionType Compression; /// /// Total # of hunks represented /// - public uint TotalHunks { get; set; } + public uint TotalHunks; /// /// Logical size of the data (in bytes) /// - public ulong LogicalBytes { get; set; } + public ulong LogicalBytes; /// /// Offset to the first blob of metadata /// - public ulong MetaOffset { get; set; } + public ulong MetaOffset; /// /// MD5 checksum of raw data /// - public byte[]? MD5 { get; set; } = new byte[16]; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] + public byte[]? MD5 = new byte[16]; /// /// MD5 checksum of parent file /// - public byte[]? ParentMD5 { get; set; } = new byte[16]; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] + public byte[]? ParentMD5 = new byte[16]; /// /// Number of bytes per hunk /// - public uint HunkBytes { get; set; } + public uint HunkBytes; /// /// SHA1 checksum of raw data /// - public byte[]? SHA1 { get; set; } = new byte[20]; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)] + public byte[]? SHA1 = new byte[20]; /// /// SHA1 checksum of parent file /// - public byte[]? ParentSHA1 { get; set; } = new byte[20]; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)] + public byte[]? ParentSHA1 = new byte[20]; } } diff --git a/SabreTools.Models/CHD/HeaderV4.cs b/SabreTools.Models/CHD/HeaderV4.cs index 75041dc..069ea60 100644 --- a/SabreTools.Models/CHD/HeaderV4.cs +++ b/SabreTools.Models/CHD/HeaderV4.cs @@ -1,51 +1,59 @@ -namespace SabreTools.Models.CHD +using System.Runtime.InteropServices; + +namespace SabreTools.Models.CHD { /// + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public class HeaderV4 : Header { /// /// Flags /// - public Flags Flags { get; set; } + [MarshalAs(UnmanagedType.U4)] + public Flags Flags; /// /// Compression type /// - public CompressionType Compression { get; set; } + [MarshalAs(UnmanagedType.U4)] + public CompressionType Compression; /// /// Total # of hunks represented /// - public uint TotalHunks { get; set; } + public uint TotalHunks; /// /// Logical size of the data (in bytes) /// - public ulong LogicalBytes { get; set; } + public ulong LogicalBytes; /// /// Offset to the first blob of metadata /// - public ulong MetaOffset { get; set; } + public ulong MetaOffset; /// /// Number of bytes per hunk /// - public uint HunkBytes { get; set; } + public uint HunkBytes; /// /// Combined raw+meta SHA1 /// - public byte[]? SHA1 { get; set; } = new byte[20]; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)] + public byte[]? SHA1 = new byte[20]; /// /// Combined raw+meta SHA1 of parent /// - public byte[]? ParentSHA1 { get; set; } = new byte[20]; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)] + public byte[]? ParentSHA1 = new byte[20]; /// /// Raw data SHA1 /// - public byte[]? RawSHA1 { get; set; } = new byte[20]; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)] + public byte[]? RawSHA1 = new byte[20]; } } diff --git a/SabreTools.Models/CHD/HeaderV5.cs b/SabreTools.Models/CHD/HeaderV5.cs index dd8c819..c3bfd02 100644 --- a/SabreTools.Models/CHD/HeaderV5.cs +++ b/SabreTools.Models/CHD/HeaderV5.cs @@ -1,6 +1,9 @@ -namespace SabreTools.Models.CHD +using System.Runtime.InteropServices; + +namespace SabreTools.Models.CHD { /// + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public class HeaderV5 : Header { ///