Use CHD models from library

This commit is contained in:
Matt Nadareski
2024-10-20 00:03:29 -04:00
parent 4d5ac92125
commit ab93ba406c
6 changed files with 158 additions and 394 deletions

View File

@@ -1,7 +1,7 @@
using System;
using System.IO;
using System.IO;
using System.Text;
using SabreTools.IO.Extensions;
using SabreTools.Models.CHD;
namespace SabreTools.FileTypes.CHD
{
@@ -10,80 +10,40 @@ namespace SabreTools.FileTypes.CHD
/// </summary>
public class CHDFileV2 : CHDFile
{
/// <summary>
/// CHD flags
/// </summary>
[Flags]
public enum Flags : uint
{
DriveHasParent = 0x00000001,
DriveAllowsWrites = 0x00000002,
}
/// <summary>
/// Compression being used in CHD
/// </summary>
public enum Compression : uint
{
CHDCOMPRESSION_NONE = 0,
CHDCOMPRESSION_ZLIB = 1,
}
/// <summary>
/// Map format
/// </summary>
public class Map
{
public ulong offset; // 44; starting offset within the file
public ulong length; // 20; length of data; if == hunksize, data is uncompressed
}
public const int HeaderSize = 80;
public const uint Version = 2;
// V2-specific header values
public Flags flags; // flags (see above)
public Compression compression; // compression type
public uint hunksize; // 512-byte sectors per hunk
public uint totalhunks; // total # of hunks represented
public uint cylinders; // number of cylinders on hard disk
public uint heads; // number of heads on hard disk
public uint sectors; // number of sectors on hard disk
public byte[] md5 = new byte[16]; // MD5 checksum of raw data
public byte[] parentmd5 = new byte[16]; // MD5 checksum of parent file
public uint seclen; // number of bytes per sector
/// <summary>
/// Parse and validate the header as if it's V2
/// </summary>
public static CHDFileV2 Deserialize(Stream stream)
public static CHDFileV2? Deserialize(Stream stream)
{
CHDFileV2 chd = new();
var header = new HeaderV2();
#if NET20 || NET35 || NET40
using (var br = new BinaryReader(stream, Encoding.Default))
#else
using (var br = new BinaryReader(stream, Encoding.Default, true))
#endif
{
chd.tag = br.ReadChars(8);
chd.length = br.ReadUInt32BigEndian();
chd.version = br.ReadUInt32BigEndian();
chd.flags = (Flags)br.ReadUInt32BigEndian();
chd.compression = (Compression)br.ReadUInt32BigEndian();
chd.hunksize = br.ReadUInt32BigEndian();
chd.totalhunks = br.ReadUInt32BigEndian();
chd.cylinders = br.ReadUInt32BigEndian();
chd.heads = br.ReadUInt32BigEndian();
chd.sectors = br.ReadUInt32BigEndian();
chd.md5 = br.ReadBytes(16);
chd.parentmd5 = br.ReadBytes(16);
chd.seclen = br.ReadUInt32BigEndian();
byte[] tagBytes = stream.ReadBytes(8);
header.Tag = Encoding.ASCII.GetString(tagBytes);
if (header.Tag != Signature)
return null;
chd.MD5 = chd.md5;
}
header.Length = stream.ReadUInt32BigEndian();
if (header.Length != HeaderSize)
return null;
return chd;
header.Version = stream.ReadUInt32BigEndian();
header.Flags = (Flags)stream.ReadUInt32BigEndian();
header.Compression = (CompressionType)stream.ReadUInt32BigEndian();
if (header.Compression > CompressionType.CHDCOMPRESSION_ZLIB)
return null;
header.HunkSize = stream.ReadUInt32BigEndian();
header.TotalHunks = stream.ReadUInt32BigEndian();
header.Cylinders = stream.ReadUInt32BigEndian();
header.Heads = stream.ReadUInt32BigEndian();
header.Sectors = stream.ReadUInt32BigEndian();
header.MD5 = stream.ReadBytes(16);
header.ParentMD5 = stream.ReadBytes(16);
header.BytesPerSector = stream.ReadUInt32BigEndian();
return new CHDFileV2 { _header = header, MD5 = header.MD5 };
}
/// <summary>
@@ -91,7 +51,7 @@ namespace SabreTools.FileTypes.CHD
/// </summary>
public override byte[] GetHash()
{
return md5;
return (_header as HeaderV2)?.MD5 ?? [];
}
}
}