[CHDFile] Remove helper class

This commit is contained in:
Matt Nadareski
2017-10-31 02:11:23 -07:00
parent 3ddad2505e
commit 6d8cdc3c3b

View File

@@ -65,11 +65,6 @@ namespace SabreTools.Library.External
/// ----------------------------------------------
/// </remrks>
public class CHDFile
{
/// <summary>
/// Information regarding the CHD, mostly unused
/// </summary>
private class CHD
{
// Core parameters from the header
public ulong m_signature; // signature
@@ -87,6 +82,111 @@ namespace SabreTools.Library.External
// map information
public uint m_mapentrybytes; // length of each entry in a map
/// <summary>
/// Get internal metadata from a CHD
/// </summary>
/// <param name="filename">Filename of possible CHD</param>
/// <returns>A Disk object with internal SHA-1 on success, null on error, empty Disk otherwise</returns>
/// <remarks>
/// Original code had a "writable" param. This is not required for metadata checking
/// </remarks>
public static DatItem GetCHDInfo(string filename)
{
FileStream fs = FileTools.TryOpenRead(filename);
DatItem datItem = GetCHDInfo(fs);
fs.Dispose();
return datItem;
}
/// <summary>
/// Get internal metadata from a CHD
/// </summary>
/// <param name="fs">Stream of possible CHD</param>
/// <returns>A Disk object with internal SHA-1 on success, null on error, empty Disk otherwise</returns>
/// <remarks>
/// Original code had a "writable" param. This is not required for metadata checking
/// </remarks>
public static DatItem GetCHDInfo(Stream fs)
{
// Create a blank Disk to populate and return
Disk datItem = new Disk();
// Get a CHD object to store the data
CHDFile chd = new CHDFile();
// Get a binary reader to make life easier
BinaryReader br = new BinaryReader(fs);
// Read and verify the CHD signature
chd.m_signature = br.ReadUInt64();
if (chd.m_signature != Constants.CHDSignature)
{
// throw CHDERR_INVALID_FILE;
return null;
}
// Get the header size and version
chd.m_headersize = br.ReadUInt32();
chd.m_version = br.ReadUInt32();
// Create a placeholder for the extracted SHA-1
byte[] sha1 = new byte[20];
// If we have a CHD v3 file, parse it accordingly
if (chd.m_headersize == Constants.CHD_V3_HEADER_SIZE && chd.m_version == 3)
{
sha1 = chd.ParseCHDv3Header(br);
}
// If we have a CHD v4 file, parse it accordingly
else if (chd.m_headersize == Constants.CHD_V4_HEADER_SIZE && chd.m_version == 4)
{
sha1 = chd.ParseCHDv4Header(br);
}
// If we have a CHD v5 file, parse it accordingly
else if (chd.m_headersize == Constants.CHD_V5_HEADER_SIZE && chd.m_version == 5)
{
sha1 = chd.ParseCHDv5Header(br);
}
// If we don't have a valid combination, return null
else
{
// throw CHDERR_UNSUPPORTED_VERSION;
// throw CHDERR_INVALID_FILE;
return null;
}
// Set the SHA-1 of the Disk to return
datItem.SHA1 = BitConverter.ToString(sha1).Replace("-", string.Empty).ToLowerInvariant();
return datItem;
}
/// <summary>
/// Get if file is a valid CHD
/// </summary>
/// <param name="filename">Filename of possible CHD</param>
/// <returns>True if a the file is a valid CHD, false otherwise</returns>
public static bool IsValidCHD(string filename)
{
DatItem datItem = GetCHDInfo(filename);
return datItem != null
&& datItem.Type == ItemType.Disk
&& ((Disk)datItem).SHA1 != null;
}
/// <summary>
/// Get if stream is a valid CHD
/// </summary>
/// <param name="fs">Stream of possible CHD</param>
/// <returns>True if a the file is a valid CHD, false otherwise</returns>
public static bool IsValidCHD(Stream fs)
{
DatItem datItem = GetCHDInfo(fs);
return datItem != null
&& datItem.Type == ItemType.Disk
&& ((Disk)datItem).SHA1 != null;
}
/// <summary>
/// Parse a CHD v3 header
/// </summary>
@@ -212,110 +312,4 @@ namespace SabreTools.Library.External
return sha1;
}
}
/// <summary>
/// Get internal metadata from a CHD
/// </summary>
/// <param name="filename">Filename of possible CHD</param>
/// <returns>A Disk object with internal SHA-1 on success, null on error, empty Disk otherwise</returns>
/// <remarks>
/// Original code had a "writable" param. This is not required for metadata checking
/// </remarks>
public static DatItem GetCHDInfo(string filename)
{
FileStream fs = FileTools.TryOpenRead(filename);
DatItem datItem = GetCHDInfo(fs);
fs.Dispose();
return datItem;
}
/// <summary>
/// Get internal metadata from a CHD
/// </summary>
/// <param name="fs">Stream of possible CHD</param>
/// <returns>A Disk object with internal SHA-1 on success, null on error, empty Disk otherwise</returns>
/// <remarks>
/// Original code had a "writable" param. This is not required for metadata checking
/// </remarks>
public static DatItem GetCHDInfo(Stream fs)
{
// Create a blank Disk to populate and return
Disk datItem = new Disk();
// Get a CHD object to store the data
CHD chd = new CHD();
// Get a binary reader to make life easier
BinaryReader br = new BinaryReader(fs);
// Read and verify the CHD signature
chd.m_signature = br.ReadUInt64();
if (chd.m_signature != Constants.CHDSignature)
{
// throw CHDERR_INVALID_FILE;
return null;
}
// Get the header size and version
chd.m_headersize = br.ReadUInt32();
chd.m_version = br.ReadUInt32();
// Create a placeholder for the extracted SHA-1
byte[] sha1 = new byte[20];
// If we have a CHD v3 file, parse it accordingly
if (chd.m_headersize == Constants.CHD_V3_HEADER_SIZE && chd.m_version == 3)
{
sha1 = chd.ParseCHDv3Header(br);
}
// If we have a CHD v4 file, parse it accordingly
else if (chd.m_headersize == Constants.CHD_V4_HEADER_SIZE && chd.m_version == 4)
{
sha1 = chd.ParseCHDv4Header(br);
}
// If we have a CHD v5 file, parse it accordingly
else if (chd.m_headersize == Constants.CHD_V5_HEADER_SIZE && chd.m_version == 5)
{
sha1 = chd.ParseCHDv5Header(br);
}
// If we don't have a valid combination, return null
else
{
// throw CHDERR_UNSUPPORTED_VERSION;
// throw CHDERR_INVALID_FILE;
return null;
}
// Set the SHA-1 of the Disk to return
datItem.SHA1 = BitConverter.ToString(sha1).Replace("-", string.Empty).ToLowerInvariant();
return datItem;
}
/// <summary>
/// Get if file is a valid CHD
/// </summary>
/// <param name="filename">Filename of possible CHD</param>
/// <returns>True if a the file is a valid CHD, false otherwise</returns>
public static bool IsValidCHD(string filename)
{
DatItem datItem = GetCHDInfo(filename);
return datItem != null
&& datItem.Type == ItemType.Disk
&& ((Disk)datItem).SHA1 != null;
}
/// <summary>
/// Get if stream is a valid CHD
/// </summary>
/// <param name="fs">Stream of possible CHD</param>
/// <returns>True if a the file is a valid CHD, false otherwise</returns>
public static bool IsValidCHD(Stream fs)
{
DatItem datItem = GetCHDInfo(fs);
return datItem != null
&& datItem.Type == ItemType.Disk
&& ((Disk)datItem).SHA1 != null;
}
}
}