2020-07-15 09:41:59 -07:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Text;
|
2024-04-24 13:45:38 -04:00
|
|
|
|
using SabreTools.IO.Extensions;
|
2024-10-20 00:24:00 -04:00
|
|
|
|
using SabreTools.Models.CHD;
|
2017-10-31 01:06:56 -07:00
|
|
|
|
|
2020-12-10 22:31:23 -08:00
|
|
|
|
namespace SabreTools.FileTypes.CHD
|
2017-10-31 01:06:56 -07:00
|
|
|
|
{
|
2019-02-08 20:51:44 -08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// This is code adapted from chd.h and chd.cpp in MAME
|
|
|
|
|
|
/// Additional archival code from https://github.com/rtissera/libchdr/blob/master/src/chd.h
|
|
|
|
|
|
/// </summary>
|
2024-10-20 00:24:00 -04:00
|
|
|
|
public class CHDFile : BaseFile
|
2019-02-08 20:51:44 -08:00
|
|
|
|
{
|
|
|
|
|
|
#region Private instance variables
|
|
|
|
|
|
|
2024-10-20 00:03:29 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Model representing the correct CHD header
|
|
|
|
|
|
/// </summary>
|
2024-10-20 00:24:00 -04:00
|
|
|
|
protected Header? _header;
|
2019-02-08 20:51:44 -08:00
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Constructors
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Create a new CHDFile from an input file
|
|
|
|
|
|
/// </summary>
|
2020-07-15 09:41:59 -07:00
|
|
|
|
/// <param name="filename">Filename respresenting the CHD file</param>
|
2024-02-28 19:19:50 -05:00
|
|
|
|
public static CHDFile? Create(string filename)
|
2019-02-08 20:51:44 -08:00
|
|
|
|
{
|
2024-10-20 00:06:40 -04:00
|
|
|
|
using var fs = File.OpenRead(filename);
|
2020-12-14 16:01:28 -08:00
|
|
|
|
return Create(fs);
|
2019-02-08 20:51:44 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-07-15 09:41:59 -07:00
|
|
|
|
/// Create a new CHDFile from an input stream
|
2019-02-08 20:51:44 -08:00
|
|
|
|
/// </summary>
|
2024-10-20 00:06:40 -04:00
|
|
|
|
/// <param name="stream">Stream representing the CHD file</param>
|
|
|
|
|
|
public static CHDFile? Create(Stream stream)
|
2019-02-08 20:51:44 -08:00
|
|
|
|
{
|
2020-07-19 21:59:34 -07:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2024-10-20 00:06:40 -04:00
|
|
|
|
// Get the detected CHD version
|
|
|
|
|
|
uint version = GetVersion(stream);
|
2019-02-08 20:51:44 -08:00
|
|
|
|
|
2024-07-19 15:35:23 -04:00
|
|
|
|
// Read and return the current CHD
|
2024-10-20 00:06:40 -04:00
|
|
|
|
return version switch
|
|
|
|
|
|
{
|
2024-10-20 00:24:00 -04:00
|
|
|
|
1 => DeserializeV1(stream),
|
|
|
|
|
|
2 => DeserializeV2(stream),
|
|
|
|
|
|
3 => DeserializeV3(stream),
|
|
|
|
|
|
4 => DeserializeV4(stream),
|
|
|
|
|
|
5 => DeserializeV5(stream),
|
2024-10-20 00:06:40 -04:00
|
|
|
|
_ => null,
|
|
|
|
|
|
};
|
2020-07-19 21:59:34 -07:00
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
2020-07-15 09:41:59 -07:00
|
|
|
|
}
|
2019-02-08 20:51:44 -08:00
|
|
|
|
|
2020-07-15 09:41:59 -07:00
|
|
|
|
#endregion
|
2019-02-08 20:51:44 -08:00
|
|
|
|
|
2024-10-20 00:24:00 -04:00
|
|
|
|
#region Deserializers
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Parse and validate the header as if it's V1
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private static CHDFile? DeserializeV1(Stream stream)
|
|
|
|
|
|
{
|
|
|
|
|
|
var header = new HeaderV1();
|
|
|
|
|
|
|
|
|
|
|
|
byte[] tagBytes = stream.ReadBytes(8);
|
|
|
|
|
|
header.Tag = Encoding.ASCII.GetString(tagBytes);
|
|
|
|
|
|
if (header.Tag != Constants.SignatureString)
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
|
|
header.Length = stream.ReadUInt32BigEndian();
|
|
|
|
|
|
if (header.Length != Constants.HeaderV1Size)
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
return new CHDFile { _header = header, MD5 = header.MD5 };
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Parse and validate the header as if it's V2
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private static CHDFile? DeserializeV2(Stream stream)
|
|
|
|
|
|
{
|
|
|
|
|
|
var header = new HeaderV2();
|
|
|
|
|
|
|
|
|
|
|
|
byte[] tagBytes = stream.ReadBytes(8);
|
|
|
|
|
|
header.Tag = Encoding.ASCII.GetString(tagBytes);
|
|
|
|
|
|
if (header.Tag != Constants.SignatureString)
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
|
|
header.Length = stream.ReadUInt32BigEndian();
|
|
|
|
|
|
if (header.Length != Constants.HeaderV2Size)
|
|
|
|
|
|
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 CHDFile { _header = header, MD5 = header.MD5 };
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Parse and validate the header as if it's V2
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private static CHDFile? DeserializeV3(Stream stream)
|
|
|
|
|
|
{
|
|
|
|
|
|
var header = new HeaderV3();
|
|
|
|
|
|
|
|
|
|
|
|
byte[] tagBytes = stream.ReadBytes(8);
|
|
|
|
|
|
header.Tag = Encoding.ASCII.GetString(tagBytes);
|
|
|
|
|
|
if (header.Tag != Constants.SignatureString)
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
|
|
header.Length = stream.ReadUInt32BigEndian();
|
|
|
|
|
|
if (header.Length != Constants.HeaderV3Size)
|
|
|
|
|
|
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 CHDFile { _header = header, MD5 = header.MD5, SHA1 = header.SHA1 };
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Parse and validate the header as if it's V4
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private static CHDFile? DeserializeV4(Stream stream)
|
|
|
|
|
|
{
|
|
|
|
|
|
var header = new HeaderV4();
|
|
|
|
|
|
|
|
|
|
|
|
byte[] tagBytes = stream.ReadBytes(8);
|
|
|
|
|
|
header.Tag = Encoding.ASCII.GetString(tagBytes);
|
|
|
|
|
|
if (header.Tag != Constants.SignatureString)
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
|
|
header.Length = stream.ReadUInt32BigEndian();
|
|
|
|
|
|
if (header.Length != Constants.HeaderV4Size)
|
|
|
|
|
|
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 CHDFile { _header = header, SHA1 = header.SHA1 };
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Parse and validate the header as if it's V5
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private static CHDFile? DeserializeV5(Stream stream)
|
|
|
|
|
|
{
|
|
|
|
|
|
var header = new HeaderV5();
|
|
|
|
|
|
|
|
|
|
|
|
byte[] tagBytes = stream.ReadBytes(8);
|
|
|
|
|
|
header.Tag = Encoding.ASCII.GetString(tagBytes);
|
|
|
|
|
|
if (header.Tag != Constants.SignatureString)
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
|
|
header.Length = stream.ReadUInt32BigEndian();
|
|
|
|
|
|
if (header.Length != Constants.HeaderV5Size)
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
|
|
header.Version = stream.ReadUInt32BigEndian();
|
|
|
|
|
|
header.Compressors = new uint[4];
|
|
|
|
|
|
for (int i = 0; i < header.Compressors.Length; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
header.Compressors[i] = stream.ReadUInt32BigEndian();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
header.LogicalBytes = stream.ReadUInt64BigEndian();
|
|
|
|
|
|
header.MapOffset = stream.ReadUInt64BigEndian();
|
|
|
|
|
|
header.MetaOffset = stream.ReadUInt64BigEndian();
|
|
|
|
|
|
header.HunkBytes = stream.ReadUInt32BigEndian();
|
|
|
|
|
|
header.UnitBytes = stream.ReadUInt32BigEndian();
|
|
|
|
|
|
header.RawSHA1 = stream.ReadBytes(20);
|
|
|
|
|
|
header.SHA1 = stream.ReadBytes(20);
|
|
|
|
|
|
header.ParentSHA1 = stream.ReadBytes(20);
|
|
|
|
|
|
|
|
|
|
|
|
return new CHDFile { _header = header, SHA1 = header.SHA1 };
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
2024-10-20 00:06:40 -04:00
|
|
|
|
#region Helpers
|
2019-02-08 20:51:44 -08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-10-20 00:06:40 -04:00
|
|
|
|
/// Get the matching CHD version, if possible
|
2019-02-08 20:51:44 -08:00
|
|
|
|
/// </summary>
|
2020-07-15 09:41:59 -07:00
|
|
|
|
/// <returns>Matching version, 0 if none</returns>
|
2024-10-20 00:06:40 -04:00
|
|
|
|
private static uint GetVersion(Stream stream)
|
2019-02-08 20:51:44 -08:00
|
|
|
|
{
|
2024-10-20 00:03:29 -04:00
|
|
|
|
// Read the header values
|
|
|
|
|
|
byte[] tagBytes = stream.ReadBytes(8);
|
|
|
|
|
|
string tag = Encoding.ASCII.GetString(tagBytes);
|
|
|
|
|
|
uint length = stream.ReadUInt32BigEndian();
|
|
|
|
|
|
uint version = stream.ReadUInt32BigEndian();
|
|
|
|
|
|
|
|
|
|
|
|
// Seek back to start
|
|
|
|
|
|
stream.SeekIfPossible();
|
|
|
|
|
|
|
|
|
|
|
|
// Check the signature
|
2024-10-20 00:24:00 -04:00
|
|
|
|
if (!string.Equals(tag, Constants.SignatureString, StringComparison.Ordinal))
|
2020-07-15 09:41:59 -07:00
|
|
|
|
return 0;
|
2019-02-08 20:51:44 -08:00
|
|
|
|
|
2024-10-20 00:03:29 -04:00
|
|
|
|
// Match the version to header length
|
2024-10-20 00:24:00 -04:00
|
|
|
|
return (version, length) switch
|
2019-02-08 20:51:44 -08:00
|
|
|
|
{
|
2024-10-20 00:24:00 -04:00
|
|
|
|
(1, Constants.HeaderV1Size) => version,
|
|
|
|
|
|
(2, Constants.HeaderV2Size) => version,
|
|
|
|
|
|
(3, Constants.HeaderV3Size) => version,
|
|
|
|
|
|
(4, Constants.HeaderV4Size) => version,
|
|
|
|
|
|
(5, Constants.HeaderV5Size) => version,
|
2020-12-14 16:01:28 -08:00
|
|
|
|
_ => 0,
|
|
|
|
|
|
};
|
2019-02-08 20:51:44 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
2017-10-31 01:06:56 -07:00
|
|
|
|
}
|