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 V2 File
|
|
|
|
|
|
/// </summary>
|
2020-12-19 15:53:19 -08:00
|
|
|
|
public class CHDFileV2 : CHDFile
|
2020-07-15 09:41:59 -07:00
|
|
|
|
{
|
2024-10-20 00:11:15 -04:00
|
|
|
|
internal const int HeaderSize = 80;
|
2020-07-15 09:41:59 -07:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Parse and validate the header as if it's V2
|
|
|
|
|
|
/// </summary>
|
2024-10-20 00:11:15 -04:00
|
|
|
|
internal static CHDFileV2? Deserialize(Stream stream)
|
2020-07-15 09:41:59 -07:00
|
|
|
|
{
|
2024-10-20 00:03:29 -04:00
|
|
|
|
var header = new HeaderV2();
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
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 };
|
2020-07-15 09:41:59 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|