diff --git a/RombaSharp/RombaSharp.Helpers.cs b/RombaSharp/RombaSharp.Helpers.cs
index b806e419..053e30f8 100644
--- a/RombaSharp/RombaSharp.Helpers.cs
+++ b/RombaSharp/RombaSharp.Helpers.cs
@@ -129,7 +129,7 @@ namespace RombaSharp
if (lowerCaseDats.Contains(input.ToLowerInvariant()))
{
string fullpath = Path.GetFullPath(datRootDats[lowerCaseDats.IndexOf(input.ToLowerInvariant())]);
- string sha1 = FileTools.GetFileInfo(fullpath).SHA1;
+ string sha1 = ((Rom)FileTools.GetFileInfo(fullpath)).SHA1;
foundDats.Add(sha1, fullpath);
}
else
diff --git a/SabreTools.Library/Data/Constants.cs b/SabreTools.Library/Data/Constants.cs
index dc6e7171..e5b3220b 100644
--- a/SabreTools.Library/Data/Constants.cs
+++ b/SabreTools.Library/Data/Constants.cs
@@ -56,12 +56,37 @@ namespace SabreTools.Library.Data
#region CHD header values
- public readonly static byte[] CHDSignature = { 0x4d, 0x43, 0x6f, 0x6d, 0x70, 0x72, 0x48, 0x44 }; // "MComprHD"
+ // CHD signature - "MComprHD"
+ public readonly static byte[] CHDSignatureBytes = { 0x4d, 0x43, 0x6f, 0x6d, 0x70, 0x72, 0x48, 0x44 };
+ public const ulong CHDSignature = 0x4d436f6d70724844;
+
+ // Header versions and sizes
public const int CHD_HEADER_VERSION = 5;
public const int CHD_V3_HEADER_SIZE = 120;
public const int CHD_V4_HEADER_SIZE = 108;
public const int CHD_V5_HEADER_SIZE = 124;
+ // Key offsets within the header (V3)
+ public const long CHDv3MapOffsetOffset = 0; // offset of map offset field
+ public const long CHDv3MetaOffsetOffset = 36; // offset of metaoffset field
+ public const long CHDv3SHA1Offset = 80; // offset of SHA1 field
+ public const long CHDv3RawSHA1Offset = 0; // offset of raw SHA1 field
+ public const long CHDv3ParentSHA1Offset = 100; // offset of parent SHA1 field
+
+ // Key offsets within the header (V4)
+ public const long CHDv4MapOffsetOffset = 0; // offset of map offset field
+ public const long CHDv4MetaOffsetOffset = 36; // offset of metaoffset field
+ public const long CHDv4SHA1Offset = 48; // offset of SHA1 field
+ public const long CHDv4RawSHA1Offset = 88; // offset of raw SHA1 field
+ public const long CHDv4ParentSHA1Offset = 68; // offset of parent SHA1 field
+
+ // Key offsets within the header (V5)
+ public const long CHDv5MapOffsetOffset = 40; // offset of map offset field
+ public const long CHDv5MetaOffsetOffset = 48; // offset of metaoffset field
+ public const long CHDv5SHA1Offset = 84; // offset of SHA1 field
+ public const long CHDv5RawSHA1Offset = 64; // offset of raw SHA1 field
+ public const long CHDv5ParentSHA1Offset = 104; // offset of parent SHA1 field
+
#endregion
#region Database schema
diff --git a/SabreTools.Library/External/CHDFile.cs b/SabreTools.Library/External/CHDFile.cs
index efaf4dfe..0e7d05a3 100644
--- a/SabreTools.Library/External/CHDFile.cs
+++ b/SabreTools.Library/External/CHDFile.cs
@@ -17,9 +17,58 @@ namespace SabreTools.Library.External
///
/// This is code adapted from chd.h and chd.cpp in MAME
///
+ ///
+ /// ----------------------------------------------
+ /// Common CHD Header:
+ /// 0x00-0x07 - CHD signature
+ /// 0x08-0x0B - Header size
+ /// 0x0C-0x0F - CHD version
+ /// ----------------------------------------------
+ /// CHD v3 header layout:
+ /// 0x10-0x13 - Flags (1: Has parent SHA-1, 2: Disallow writes)
+ /// 0x14-0x17 - Compression
+ /// 0x18-0x1B - Hunk count
+ /// 0x1C-0x23 - Logical Bytes
+ /// 0x24-0x2C - Metadata Offset
+ /// ...
+ /// 0x4C-0x4F - Hunk Bytes
+ /// 0x50-0x63 - SHA-1
+ /// 0x64-0x77 - Parent SHA-1
+ /// 0x78-0x87 - Map
+ /// ----------------------------------------------
+ /// CHD v4 header layout:
+ /// 0x10-0x13 - Flags (1: Has parent SHA-1, 2: Disallow writes)
+ /// 0x14-0x17 - Compression
+ /// 0x18-0x1B - Hunk count
+ /// 0x1C-0x23 - Logical Bytes
+ /// 0x24-0x2C - Metadata Offset
+ /// ...
+ /// 0x2C-0x2F - Hunk Bytes
+ /// 0x30-0x43 - SHA-1
+ /// 0x44-0x57 - Parent SHA-1
+ /// 0x58-0x6b - Raw SHA-1
+ /// 0x6c-0x7b - Map
+ /// ----------------------------------------------
+ /// CHD v5 header layout:
+ /// 0x10-0x13 - Compression format 1
+ /// 0x14-0x17 - Compression format 2
+ /// 0x18-0x1B - Compression format 3
+ /// 0x1C-0x1F - Compression format 4
+ /// 0x20-0x27 - Logical Bytes
+ /// 0x28-0x2F - Map Offset
+ /// 0x30-0x37 - Metadata Offset
+ /// 0x38-0x3B - Hunk Bytes
+ /// 0x3C-0x3F - Unit Bytes
+ /// 0x40-0x53 - Raw SHA-1
+ /// 0x54-0x67 - SHA-1
+ /// 0x68-0x7b - Parent SHA-1
+ /// ----------------------------------------------
+ ///
public class CHDFile
{
- // core parameters from the header
+ // Core parameters from the header
+ private ulong m_signature; // signature
+ private uint m_headersize; // size of the header
private uint m_version; // version of the header
private ulong m_logicalbytes; // logical size of the raw CHD data in bytes
private ulong m_mapoffset; // offset of map
@@ -30,13 +79,6 @@ namespace SabreTools.Library.External
private ulong m_unitcount; // number of units represented
private CHDCodecType[] m_compression = new CHDCodecType[4]; // array of compression types used
- // key offsets within the header
- private long m_mapoffset_offset; // offset of map offset field
- private long m_metaoffset_offset;// offset of metaoffset field
- private long m_sha1_offset; // offset of SHA1 field
- private long m_rawsha1_offset; // offset of raw SHA1 field
- private long m_parentsha1_offset;// offset of paren SHA1 field
-
// map information
uint m_mapentrybytes; // length of each entry in a map
@@ -59,12 +101,12 @@ namespace SabreTools.Library.External
///
/// Get internal metadata from a CHD
///
- /// FileStreams of possible CHD
+ /// Stream of possible CHD
/// A Disk object with internal SHA-1 on success, null on error, empty Disk otherwise
///
/// Original code had a "writable" param. This is not required for metadata checking
///
- public DatItem GetCHDInfo(FileStream fs)
+ public DatItem GetCHDInfo(Stream fs)
{
// Create a blank Disk to populate and return
Disk datItem = new Disk();
@@ -73,97 +115,89 @@ namespace SabreTools.Library.External
BinaryReader br = new BinaryReader(fs);
// Read and verify the CHD signature
- // read the raw header
- byte[] signature = br.ReadBytes(8);
-
- // verify the signature
- bool correct = true;
- for (int i = 0; i < signature.Length; i++)
- {
- correct &= (signature[i] == Constants.CHDSignature[i]);
- }
- if (!correct)
+ m_signature = br.ReadUInt64();
+ if (m_signature != Constants.CHDSignature)
{
// throw CHDERR_INVALID_FILE;
return null;
}
- // only allow writes to the most recent version
- br.BaseStream.Seek(12, SeekOrigin.Begin);
+ // Get the header size and version
+ m_headersize = br.ReadUInt32();
m_version = br.ReadUInt32();
- // read the header if we support it
+ // Create a placeholder for the extracted SHA-1
byte[] sha1 = new byte[20];
- switch (m_version)
+
+ // If we have a CHD v3 file, parse it accordingly
+ if (m_headersize == Constants.CHD_V3_HEADER_SIZE && m_version == 3)
{
- case 3:
- ParseV3Header(br, out sha1);
- break;
- case 4:
- ParseV4Header(br, out sha1);
- break;
- case 5:
- ParseV5Header(br, out sha1);
- break;
- default:
- // throw CHDERR_UNSUPPORTED_VERSION;
- return null;
+ sha1 = ParseCHDv3Header(br);
+ }
+ // If we have a CHD v4 file, parse it accordingly
+ else if (m_headersize == Constants.CHD_V4_HEADER_SIZE && m_version == 4)
+ {
+ sha1 = ParseCHDv4Header(br);
+ }
+ // If we have a CHD v5 file, parse it accordingly
+ else if (m_headersize == Constants.CHD_V5_HEADER_SIZE && m_version == 5)
+ {
+ sha1 = 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;
}
///
- /// Parse a CHD v3 header, populate the fields, and return the SHA-1
+ /// Get if file is a valid CHD
///
- /// BinaryReader representing the file to read
- /// Out parameter representing the SHA-1
- /// True if the header was parsed properly, false otherwise
- ///
- /// CHD v3 header layout:
- /// 0x00-0x07 - CHD signature
- /// 0x08-0x0B - Header size
- /// 0x0C-0x0F - [UNUSED]
- /// 0x10-0x13 - Flags (1: Has parent SHA-1, 2: Disallow writes)
- /// 0x14-0x17 - Compression
- /// 0x18-0x1B - Hunk count
- /// 0x1C-0x23 - Logical Bytes
- /// 0x24-0x2C - Metadata Offset
- /// ...
- /// 0x4C-0x4F - Hunk Bytes
- /// 0x50-0x63 - SHA-1
- /// 0x64-0x77 - Parent SHA-1
- /// 0x78-0x87 - Map
- ///
- private bool ParseV3Header(BinaryReader br, out byte[] sha1)
+ /// Filename of possible CHD
+ /// True if a the file is a valid CHD, false otherwise
+ public bool IsValidCHD(string filename)
+ {
+ DatItem datItem = GetCHDInfo(filename);
+ return datItem != null
+ && datItem.Type == ItemType.Disk
+ && ((Disk)datItem).SHA1 != null;
+ }
+
+ ///
+ /// Get if stream is a valid CHD
+ ///
+ /// Stream of possible CHD
+ /// True if a the file is a valid CHD, false otherwise
+ public bool IsValidCHD(Stream fs)
+ {
+ DatItem datItem = GetCHDInfo(fs);
+ return datItem != null
+ && datItem.Type == ItemType.Disk
+ && ((Disk)datItem).SHA1 != null;
+ }
+
+ ///
+ /// Parse a CHD v3 header
+ ///
+ /// Binary reader representing the input stream
+ /// The extracted SHA-1 on success, null otherwise
+ private byte[] ParseCHDv3Header(BinaryReader br)
{
// Set the blank SHA-1 hash
- sha1 = null;
+ byte[] sha1 = new byte[20];
// Set offsets and defaults
- m_mapoffset_offset = 0;
- m_rawsha1_offset = 0;
m_mapoffset = 120;
- m_metaoffset_offset = 36;
- m_sha1_offset = 80;
- m_parentsha1_offset = 100;
m_mapentrybytes = 16;
- // Ensure the proper starting position
- br.BaseStream.Seek(8, SeekOrigin.Begin);
-
- // Verify header length
- if (br.ReadInt32() != Constants.CHD_V3_HEADER_SIZE)
- {
- // throw CHDERR_INVALID_FILE;
- return false;
- }
-
- // Skip over the 0x0C-0x0F block
- br.ReadUInt32();
-
// Read the CHD flags
uint flags = br.ReadUInt32();
@@ -174,7 +208,7 @@ namespace SabreTools.Library.External
case 1: m_compression[0] = CHDCodecType.CHD_CODEC_ZLIB; break;
case 2: m_compression[0] = CHDCodecType.CHD_CODEC_ZLIB; break;
case 3: m_compression[0] = CHDCodecType.CHD_CODEC_AVHUFF; break;
- default: /* throw CHDERR_UNKNOWN_COMPRESSION; */ return false;
+ default: /* throw CHDERR_UNKNOWN_COMPRESSION; */ return null;
}
m_compression[1] = m_compression[2] = m_compression[3] = CHDCodecType.CHD_CODEC_NONE;
@@ -186,63 +220,30 @@ namespace SabreTools.Library.External
br.BaseStream.Seek(76, SeekOrigin.Begin);
m_hunkbytes = br.ReadUInt32();
- br.BaseStream.Seek(m_sha1_offset, SeekOrigin.Begin);
+ br.BaseStream.Seek(Constants.CHDv3SHA1Offset, SeekOrigin.Begin);
sha1 = br.ReadBytes(20);
// guess at the units based on snooping the metadata
// m_unitbytes = guess_unitbytes();
m_unitcount = (m_logicalbytes + m_unitbytes - 1) / m_unitbytes;
- return true;
+ return sha1;
}
///
- /// Parse a CHD v4 header, populate the fields, and return the SHA-1
+ /// Parse a CHD v4 header
///
- /// BinaryReader representing the file to read
- /// Out parameter representing the SHA-1
- /// True if the header was parsed properly, false otherwise
- ///
- /// CHD v4 header layout:
- /// 0x00-0x07 - CHD signature
- /// 0x08-0x0B - Header size
- /// 0x0C-0x0F - [UNUSED]
- /// 0x10-0x13 - Flags (1: Has parent SHA-1, 2: Disallow writes)
- /// 0x14-0x17 - Compression
- /// 0x18-0x1B - Hunk count
- /// 0x1C-0x23 - Logical Bytes
- /// 0x24-0x2C - Metadata Offset
- /// ...
- /// 0x2C-0x2F - Hunk Bytes
- /// 0x30-0x43 - SHA-1
- /// 0x44-0x57 - Parent SHA-1
- /// 0x58-0x6b - Raw SHA-1
- /// 0x6c-0x7b - Map
- ///
- private bool ParseV4Header(BinaryReader br, out byte[] sha1)
+ /// Binary reader representing the input stream
+ /// The extracted SHA-1 on success, null otherwise
+ private byte[] ParseCHDv4Header(BinaryReader br)
{
// Set the blank SHA-1 hash
- sha1 = null;
+ byte[] sha1 = new byte[20];
// Set offsets and defaults
- m_mapoffset_offset = 0;
- m_metaoffset_offset = 36;
- m_sha1_offset = 48;
- m_parentsha1_offset = 68;
- m_rawsha1_offset = 88;
m_mapoffset = 108;
m_mapentrybytes = 16;
- // Ensure the proper starting position
- br.BaseStream.Seek(8, SeekOrigin.Begin);
-
- // Verify header length
- if (br.ReadUInt32() != Constants.CHD_V4_HEADER_SIZE)
- {
- // throw CHDERR_INVALID_FILE;
- return false;
- }
-
// Read the CHD flags
uint flags = br.ReadUInt32();
@@ -253,7 +254,7 @@ namespace SabreTools.Library.External
case 1: m_compression[0] = CHDCodecType.CHD_CODEC_ZLIB; break;
case 2: m_compression[0] = CHDCodecType.CHD_CODEC_ZLIB; break;
case 3: m_compression[0] = CHDCodecType.CHD_CODEC_AVHUFF; break;
- default: /* throw CHDERR_UNKNOWN_COMPRESSION; */ return false;
+ default: /* throw CHDERR_UNKNOWN_COMPRESSION; */ return null;
}
m_compression[1] = m_compression[2] = m_compression[3] = CHDCodecType.CHD_CODEC_NONE;
@@ -264,63 +265,25 @@ namespace SabreTools.Library.External
br.BaseStream.Seek(44, SeekOrigin.Begin);
m_hunkbytes = br.ReadUInt32();
-
- br.BaseStream.Seek(m_sha1_offset, SeekOrigin.Begin);
+
+ br.BaseStream.Seek(Constants.CHDv4SHA1Offset, SeekOrigin.Begin);
sha1 = br.ReadBytes(20);
// guess at the units based on snooping the metadata
// m_unitbytes = guess_unitbytes();
m_unitcount = (m_logicalbytes + m_unitbytes - 1) / m_unitbytes;
-
- return true;
+ return sha1;
}
///
- /// Parse a CHD v5 header, populate the fields, and return the SHA-1
+ /// Parse a CHD v5 header
///
- /// BinaryReader representing the file to read
- /// Out parameter representing the SHA-1
- /// True if the header was parsed properly, false otherwise
- ///
- /// CHD v5 header layout:
- /// 0x00-0x07 - CHD signature
- /// 0x08-0x0B - Header size
- /// 0x0C-0x0F - [UNUSED]
- /// 0x10-0x13 - Compression format 1
- /// 0x14-0x17 - Compression format 2
- /// 0x18-0x1B - Compression format 3
- /// 0x1C-0x1F - Compression format 4
- /// 0x18-0x1B - Hunk count
- /// 0x20-0x27 - Logical Bytes
- /// 0x28-0x2F - Map Offset
- /// 0x30-0x37 - Metadata Offset
- /// 0x38-0x3B - Hunk Bytes
- /// 0x3C-0x3F - Unit Bytes
- /// 0x40-0x53 - Raw SHA-1
- /// 0x54-0x67 - SHA-1
- /// 0x68-0x7b - Parent SHA-1
- ///
- private bool ParseV5Header(BinaryReader br, out byte[] sha1)
+ /// Binary reader representing the input stream
+ /// The extracted SHA-1 on success, null otherwise
+ private byte[] ParseCHDv5Header(BinaryReader br)
{
// Set the blank SHA-1 hash
- sha1 = null;
-
- // Set offsets and defaults
- m_mapoffset_offset = 40;
- m_metaoffset_offset = 48;
- m_rawsha1_offset = 64;
- m_sha1_offset = 84;
- m_parentsha1_offset = 104;
-
- // Ensure the proper starting position
- br.BaseStream.Seek(8, SeekOrigin.Begin);
-
- // Verify header length
- if (br.ReadUInt32() != Constants.CHD_V5_HEADER_SIZE)
- {
- // throw CHDERR_INVALID_FILE
- return false;
- }
+ byte[] sha1 = new byte[20];
// Determine compression
m_compression[0] = (CHDCodecType)br.ReadUInt32();
@@ -341,10 +304,9 @@ namespace SabreTools.Library.External
// determine properties of map entries
// m_mapentrybytes = compressed() ? 12 : 4;
- br.BaseStream.Seek(m_sha1_offset, SeekOrigin.Begin);
+ br.BaseStream.Seek(Constants.CHDv5SHA1Offset, SeekOrigin.Begin);
sha1 = br.ReadBytes(20);
-
- return true;
+ return sha1;
}
}
}