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;
|
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>
|
2020-07-15 09:41:59 -07:00
|
|
|
|
public abstract class CHDFile : BaseFile
|
2019-02-08 20:51:44 -08:00
|
|
|
|
{
|
|
|
|
|
|
#region Private instance variables
|
|
|
|
|
|
|
2024-10-20 00:03:29 -04:00
|
|
|
|
protected const string Signature = "MComprHD";
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Model representing the correct CHD header
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
protected Models.CHD.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
|
|
|
|
|
|
{
|
|
|
|
|
|
1 => CHDFileV1.Deserialize(stream),
|
|
|
|
|
|
2 => CHDFileV2.Deserialize(stream),
|
|
|
|
|
|
3 => CHDFileV3.Deserialize(stream),
|
|
|
|
|
|
4 => CHDFileV4.Deserialize(stream),
|
|
|
|
|
|
5 => CHDFileV5.Deserialize(stream),
|
|
|
|
|
|
_ => 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
|
|
|
|
|
2020-07-15 09:41:59 -07:00
|
|
|
|
#region Abstract functionality
|
2019-02-08 20:51:44 -08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-07-15 09:41:59 -07:00
|
|
|
|
/// Return the best-available hash for a particular CHD version
|
2019-02-08 20:51:44 -08:00
|
|
|
|
/// </summary>
|
2020-07-15 09:41:59 -07:00
|
|
|
|
public abstract byte[] GetHash();
|
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: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
|
|
|
|
|
|
if (!string.Equals(tag, Signature, 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
|
2020-12-14 16:01:28 -08:00
|
|
|
|
return version switch
|
2019-02-08 20:51:44 -08:00
|
|
|
|
{
|
2020-12-14 16:01:28 -08:00
|
|
|
|
1 => length == CHDFileV1.HeaderSize ? version : 0,
|
|
|
|
|
|
2 => length == CHDFileV2.HeaderSize ? version : 0,
|
|
|
|
|
|
3 => length == CHDFileV3.HeaderSize ? version : 0,
|
|
|
|
|
|
4 => length == CHDFileV4.HeaderSize ? version : 0,
|
|
|
|
|
|
5 => length == CHDFileV5.HeaderSize ? version : 0,
|
|
|
|
|
|
_ => 0,
|
|
|
|
|
|
};
|
2019-02-08 20:51:44 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
2017-10-31 01:06:56 -07:00
|
|
|
|
}
|