using System;
using System.IO;
using System.Text;
using SabreTools.IO;
using SabreTools.FileTypes.CHD;
namespace SabreTools.FileTypes
{
///
/// 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
///
public abstract class CHDFile : BaseFile
{
#region Private instance variables
// 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
#endregion
#region Constructors
///
/// Create a new CHDFile from an input file
///
/// Filename respresenting the CHD file
public static CHDFile Create(string filename)
{
using (FileStream fs = File.OpenRead(filename))
{
return Create(fs);
}
}
///
/// Create a new CHDFile from an input stream
///
/// Stream representing the CHD file
public static CHDFile Create(Stream chdstream)
{
try
{
// Read the standard CHD headers
(char[] tag, uint length, uint version) = GetHeaderValues(chdstream);
chdstream.SeekIfPossible(); // Seek back to start
// Validate that this is actually a valid CHD
uint validatedVersion = ValidateHeader(tag, length, version);
if (validatedVersion == 0)
return null;
// Read and retrun the current CHD
CHDFile generated = ReadAsVersion(chdstream, version);
if (generated != null)
generated.Type = FileType.CHD;
return generated;
}
catch
{
return null;
}
}
#endregion
#region Abstract functionality
///
/// Return the best-available hash for a particular CHD version
///
public abstract byte[] GetHash();
#endregion
#region Header Parsing
///
/// Get the generic header values of a CHD, if possible
///
///
///
private static (char[] tag, uint length, uint version) GetHeaderValues(Stream stream)
{
char[] parsedTag = new char[8];
uint parsedLength = 0;
uint parsedVersion = 0;
using (BinaryReader br = new BinaryReader(stream, Encoding.Default, true))
{
parsedTag = br.ReadChars(8);
parsedLength = br.ReadUInt32BigEndian();
parsedVersion = br.ReadUInt32BigEndian();
}
return (parsedTag, parsedLength, parsedVersion);
}
///
/// Validate the header values
///
/// Matching version, 0 if none
private static uint ValidateHeader(char[] tag, uint length, uint version)
{
if (!string.Equals(new string(tag), "MComprHD", StringComparison.Ordinal))
return 0;
switch (version)
{
case 1:
return length == CHDFileV1.HeaderSize ? version : 0;
case 2:
return length == CHDFileV2.HeaderSize ? version : 0;
case 3:
return length == CHDFileV3.HeaderSize ? version : 0;
case 4:
return length == CHDFileV4.HeaderSize ? version : 0;
case 5:
return length == CHDFileV5.HeaderSize ? version : 0;
default:
return 0;
}
}
///
/// Read a stream as a particular CHD version
///
/// CHD file as a stream
/// CHD version to parse
/// Populated CHD file, null on failure
private static CHDFile ReadAsVersion(Stream stream, uint version)
{
switch (version)
{
case 1:
return CHDFileV1.Deserialize(stream);
case 2:
return CHDFileV2.Deserialize(stream);
case 3:
return CHDFileV3.Deserialize(stream);
case 4:
return CHDFileV4.Deserialize(stream);
case 5:
return CHDFileV5.Deserialize(stream);
default:
return null;
}
}
#endregion
}
}