2020-07-15 09:41:59 -07:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Text;
|
2017-10-31 01:06:56 -07:00
|
|
|
|
|
2020-12-07 15:08:57 -08:00
|
|
|
|
using SabreTools.IO;
|
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
|
|
|
|
|
|
|
2020-07-15 09:41:59 -07:00
|
|
|
|
// Common header fields
|
|
|
|
|
|
protected char[] tag = new char[8]; // 'MComprHD'
|
|
|
|
|
|
protected uint length; // length of header (including tag and length fields)
|
|
|
|
|
|
protected uint version; // drive format version
|
2019-02-08 20:51:44 -08:00
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Constructors
|
|
|
|
|
|
|
2020-12-19 15:53:19 -08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Empty constructor
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public CHDFile()
|
|
|
|
|
|
{
|
|
|
|
|
|
Type = FileType.CHD;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-02-08 20:51:44 -08:00
|
|
|
|
/// <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>
|
|
|
|
|
|
public static CHDFile Create(string filename)
|
2019-02-08 20:51:44 -08:00
|
|
|
|
{
|
2020-12-14 16:01:28 -08:00
|
|
|
|
using FileStream fs = File.OpenRead(filename);
|
|
|
|
|
|
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>
|
2020-07-15 09:41:59 -07:00
|
|
|
|
/// <param name="chdstream">Stream representing the CHD file</param>
|
|
|
|
|
|
public static CHDFile Create(Stream chdstream)
|
2019-02-08 20:51:44 -08:00
|
|
|
|
{
|
2020-07-19 21:59:34 -07:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
// Read the standard CHD headers
|
|
|
|
|
|
(char[] tag, uint length, uint version) = GetHeaderValues(chdstream);
|
2020-08-28 01:13:55 -07:00
|
|
|
|
chdstream.SeekIfPossible(); // Seek back to start
|
2019-02-08 20:51:44 -08:00
|
|
|
|
|
2020-07-19 21:59:34 -07:00
|
|
|
|
// Validate that this is actually a valid CHD
|
|
|
|
|
|
uint validatedVersion = ValidateHeader(tag, length, version);
|
|
|
|
|
|
if (validatedVersion == 0)
|
|
|
|
|
|
return null;
|
2019-02-08 20:51:44 -08:00
|
|
|
|
|
2020-07-19 21:59:34 -07:00
|
|
|
|
// Read and retrun the current CHD
|
|
|
|
|
|
CHDFile generated = ReadAsVersion(chdstream, version);
|
|
|
|
|
|
if (generated != null)
|
|
|
|
|
|
generated.Type = FileType.CHD;
|
2019-02-08 20:51:44 -08:00
|
|
|
|
|
2020-07-19 21:59:34 -07:00
|
|
|
|
return generated;
|
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
2020-07-15 09:41:59 -07:00
|
|
|
|
#region Header Parsing
|
2019-02-08 20:51:44 -08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-07-15 09:41:59 -07:00
|
|
|
|
/// Get the generic header values of a CHD, if possible
|
2019-02-08 20:51:44 -08:00
|
|
|
|
/// </summary>
|
2020-07-15 09:41:59 -07:00
|
|
|
|
/// <param name="stream"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
private static (char[] tag, uint length, uint version) GetHeaderValues(Stream stream)
|
2019-02-08 20:51:44 -08:00
|
|
|
|
{
|
2020-07-15 09:41:59 -07:00
|
|
|
|
char[] parsedTag = new char[8];
|
|
|
|
|
|
uint parsedLength = 0;
|
|
|
|
|
|
uint parsedVersion = 0;
|
2019-02-08 20:51:44 -08:00
|
|
|
|
|
2023-04-19 16:39:58 -04:00
|
|
|
|
using (BinaryReader br = new(stream, Encoding.Default, true))
|
2019-02-08 20:51:44 -08:00
|
|
|
|
{
|
2020-07-19 21:59:34 -07:00
|
|
|
|
parsedTag = br.ReadChars(8);
|
2020-07-15 09:41:59 -07:00
|
|
|
|
parsedLength = br.ReadUInt32BigEndian();
|
|
|
|
|
|
parsedVersion = br.ReadUInt32BigEndian();
|
2019-02-08 20:51:44 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-15 09:41:59 -07:00
|
|
|
|
return (parsedTag, parsedLength, parsedVersion);
|
2019-02-08 20:51:44 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-07-15 09:41:59 -07:00
|
|
|
|
/// Validate the header values
|
2019-02-08 20:51:44 -08:00
|
|
|
|
/// </summary>
|
2020-07-15 09:41:59 -07:00
|
|
|
|
/// <returns>Matching version, 0 if none</returns>
|
|
|
|
|
|
private static uint ValidateHeader(char[] tag, uint length, uint version)
|
2019-02-08 20:51:44 -08:00
|
|
|
|
{
|
2020-07-15 09:41:59 -07:00
|
|
|
|
if (!string.Equals(new string(tag), "MComprHD", StringComparison.Ordinal))
|
|
|
|
|
|
return 0;
|
2019-02-08 20:51:44 -08:00
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-07-15 09:41:59 -07:00
|
|
|
|
/// Read a stream as a particular CHD version
|
2019-02-08 20:51:44 -08:00
|
|
|
|
/// </summary>
|
2020-07-15 09:41:59 -07:00
|
|
|
|
/// <param name="stream">CHD file as a stream</param>
|
|
|
|
|
|
/// <param name="version">CHD version to parse</param>
|
|
|
|
|
|
/// <returns>Populated CHD file, null on failure</returns>
|
|
|
|
|
|
private static CHDFile ReadAsVersion(Stream stream, uint version)
|
2019-02-08 20:51:44 -08:00
|
|
|
|
{
|
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 => CHDFileV1.Deserialize(stream),
|
|
|
|
|
|
2 => CHDFileV2.Deserialize(stream),
|
|
|
|
|
|
3 => CHDFileV3.Deserialize(stream),
|
|
|
|
|
|
4 => CHDFileV4.Deserialize(stream),
|
|
|
|
|
|
5 => CHDFileV5.Deserialize(stream),
|
|
|
|
|
|
_ => null,
|
|
|
|
|
|
};
|
2019-02-08 20:51:44 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
2017-10-31 01:06:56 -07:00
|
|
|
|
}
|