using System; using System.IO; using System.Text; using SabreTools.IO; namespace SabreTools.FileTypes.CHD { /// /// 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 /// /// Empty constructor /// public CHDFile() { Type = FileType.CHD; } /// /// 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; #if NET20 || NET35 || NET40 using (BinaryReader br = new(stream, Encoding.Default)) #else using (BinaryReader br = new(stream, Encoding.Default, true)) #endif { 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; return version switch { 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, }; } /// /// 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) { return version switch { 1 => CHDFileV1.Deserialize(stream), 2 => CHDFileV2.Deserialize(stream), 3 => CHDFileV3.Deserialize(stream), 4 => CHDFileV4.Deserialize(stream), 5 => CHDFileV5.Deserialize(stream), _ => null, }; } #endregion } }