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,83 +10,39 @@ namespace SabreTools.FileTypes.CHD
/// </summary>
public class CHDFileV4 : 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,
CHDCOMPRESSION_ZLIB_PLUS = 2,
CHDCOMPRESSION_AV = 3,
}
/// <summary>
/// Map format
/// </summary>
public class Map
{
public ulong offset; // starting offset within the file
public uint crc32; // 32-bit CRC of the uncompressed data
public ushort length_lo; // lower 16 bits of length
public byte length_hi; // upper 8 bits of length
public byte flags; // flags, indicating compression info
}
public const int HeaderSize = 108;
public const uint Version = 4;
// V4-specific header values
public Flags flags; // flags (see above)
public Compression compression; // compression type
public uint totalhunks; // total # of hunks represented
public ulong logicalbytes; // logical size of the data (in bytes)
public ulong metaoffset; // offset to the first blob of metadata
public uint hunkbytes; // number of bytes per hunk
public byte[] sha1 = new byte[20]; // combined raw+meta SHA1
public byte[] parentsha1 = new byte[20]; // combined raw+meta SHA1 of parent
public byte[] rawsha1 = new byte[20]; // raw data SHA1
/// <summary>
/// Parse and validate the header as if it's V4
/// </summary>
public static CHDFileV4 Deserialize(Stream stream)
public static CHDFileV4? Deserialize(Stream stream)
{
CHDFileV4 chd = new();
var header = new HeaderV4();
#if NET20 || NET35 || NET40
using (BinaryReader br = new(stream, Encoding.Default))
#else
using (BinaryReader br = new(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.totalhunks = br.ReadUInt32BigEndian();
chd.logicalbytes = br.ReadUInt64BigEndian();
chd.metaoffset = br.ReadUInt64BigEndian();
chd.hunkbytes = br.ReadUInt32BigEndian();
chd.sha1 = br.ReadBytes(20);
chd.parentsha1 = br.ReadBytes(20);
chd.rawsha1 = br.ReadBytes(20);
byte[] tagBytes = stream.ReadBytes(8);
header.Tag = Encoding.ASCII.GetString(tagBytes);
if (header.Tag != Signature)
return null;
chd.SHA1 = chd.sha1;
}
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_AV)
return null;
header.TotalHunks = stream.ReadUInt32BigEndian();
header.LogicalBytes = stream.ReadUInt64BigEndian();
header.MetaOffset = stream.ReadUInt64BigEndian();
header.HunkBytes = stream.ReadUInt32BigEndian();
header.SHA1 = stream.ReadBytes(20);
header.ParentSHA1 = stream.ReadBytes(20);
header.RawSHA1 = stream.ReadBytes(20);
return new CHDFileV4 { _header = header, SHA1 = header.SHA1 };
}
/// <summary>
@@ -94,7 +50,7 @@ namespace SabreTools.FileTypes.CHD
/// </summary>
public override byte[] GetHash()
{
return sha1;
return (_header as HeaderV4)?.SHA1 ?? [];
}
}
}