2024-10-20 00:03:29 -04:00
|
|
|
|
using System.IO;
|
2020-07-15 09:41:59 -07:00
|
|
|
|
using System.Text;
|
2024-04-24 13:45:38 -04:00
|
|
|
|
using SabreTools.IO.Extensions;
|
2024-10-20 00:03:29 -04:00
|
|
|
|
using SabreTools.Models.CHD;
|
2020-07-15 09:41:59 -07:00
|
|
|
|
|
2020-12-08 14:53:49 -08:00
|
|
|
|
namespace SabreTools.FileTypes.CHD
|
2020-07-15 09:41:59 -07:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// CHD V3 File
|
|
|
|
|
|
/// </summary>
|
2020-12-19 15:53:19 -08:00
|
|
|
|
public class CHDFileV3 : CHDFile
|
2020-07-15 09:41:59 -07:00
|
|
|
|
{
|
|
|
|
|
|
public const int HeaderSize = 120;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Parse and validate the header as if it's V3
|
|
|
|
|
|
/// </summary>
|
2024-10-20 00:03:29 -04:00
|
|
|
|
public static CHDFileV3? Deserialize(Stream stream)
|
2020-07-15 09:41:59 -07:00
|
|
|
|
{
|
2024-10-20 00:03:29 -04:00
|
|
|
|
var header = new HeaderV3();
|
|
|
|
|
|
|
|
|
|
|
|
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_ZLIB_PLUS)
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
|
|
header.TotalHunks = stream.ReadUInt32BigEndian();
|
|
|
|
|
|
header.LogicalBytes = stream.ReadUInt64BigEndian();
|
|
|
|
|
|
header.MetaOffset = stream.ReadUInt64BigEndian();
|
|
|
|
|
|
header.MD5 = stream.ReadBytes(16);
|
|
|
|
|
|
header.ParentMD5 = stream.ReadBytes(16);
|
|
|
|
|
|
header.HunkBytes = stream.ReadUInt32BigEndian();
|
|
|
|
|
|
header.SHA1 = stream.ReadBytes(20);
|
|
|
|
|
|
header.ParentSHA1 = stream.ReadBytes(20);
|
|
|
|
|
|
|
|
|
|
|
|
return new CHDFileV3 { _header = header, MD5 = header.MD5, SHA1 = header.SHA1 };
|
2020-07-15 09:41:59 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Return internal SHA1 hash
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public override byte[] GetHash()
|
|
|
|
|
|
{
|
2024-10-20 00:03:29 -04:00
|
|
|
|
return (_header as HeaderV3)?.SHA1 ?? [];
|
2020-07-15 09:41:59 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|