mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
57 lines
1.8 KiB
C#
57 lines
1.8 KiB
C#
using System.IO;
|
|
using System.Text;
|
|
using SabreTools.IO.Extensions;
|
|
using SabreTools.Models.CHD;
|
|
|
|
namespace SabreTools.FileTypes.CHD
|
|
{
|
|
/// <summary>
|
|
/// CHD V4 File
|
|
/// </summary>
|
|
public class CHDFileV4 : CHDFile
|
|
{
|
|
public const int HeaderSize = 108;
|
|
|
|
/// <summary>
|
|
/// Parse and validate the header as if it's V4
|
|
/// </summary>
|
|
public static CHDFileV4? Deserialize(Stream stream)
|
|
{
|
|
var header = new HeaderV4();
|
|
|
|
byte[] tagBytes = stream.ReadBytes(8);
|
|
header.Tag = Encoding.ASCII.GetString(tagBytes);
|
|
if (header.Tag != Signature)
|
|
return null;
|
|
|
|
header.Length = stream.ReadUInt32BigEndian();
|
|
if (header.Length != HeaderSize)
|
|
return null;
|
|
|
|
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>
|
|
/// Return internal SHA1 hash
|
|
/// </summary>
|
|
public override byte[] GetHash()
|
|
{
|
|
return (_header as HeaderV4)?.SHA1 ?? [];
|
|
}
|
|
}
|
|
}
|