2017-10-31 01:06:56 -07:00
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
|
|
using SabreTools.Library.Data;
|
|
|
|
|
|
using SabreTools.Library.Items;
|
|
|
|
|
|
using SabreTools.Library.Tools;
|
|
|
|
|
|
|
|
|
|
|
|
#if MONO
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
#else
|
|
|
|
|
|
using BinaryReader = System.IO.BinaryReader;
|
|
|
|
|
|
using FileStream = System.IO.FileStream;
|
|
|
|
|
|
using SeekOrigin = System.IO.SeekOrigin;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
namespace SabreTools.Library.External
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// This is code adapted from chd.h and chd.cpp in MAME
|
|
|
|
|
|
/// </summary>
|
2017-10-31 02:00:31 -07:00
|
|
|
|
/// <remrks>
|
|
|
|
|
|
/// ----------------------------------------------
|
|
|
|
|
|
/// 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
|
|
|
|
|
|
/// ----------------------------------------------
|
|
|
|
|
|
/// </remrks>
|
2017-10-31 01:06:56 -07:00
|
|
|
|
public class CHDFile
|
|
|
|
|
|
{
|
2017-10-31 02:00:31 -07:00
|
|
|
|
// Core parameters from the header
|
|
|
|
|
|
private ulong m_signature; // signature
|
|
|
|
|
|
private uint m_headersize; // size of the header
|
2017-10-31 01:06:56 -07:00
|
|
|
|
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
|
|
|
|
|
|
private ulong m_metaoffset; // offset to first metadata bit
|
|
|
|
|
|
private uint m_hunkbytes; // size of each raw hunk in bytes
|
|
|
|
|
|
private ulong m_hunkcount; // number of hunks represented
|
|
|
|
|
|
private uint m_unitbytes; // size of each unit in bytes
|
|
|
|
|
|
private ulong m_unitcount; // number of units represented
|
|
|
|
|
|
private CHDCodecType[] m_compression = new CHDCodecType[4]; // array of compression types used
|
|
|
|
|
|
|
|
|
|
|
|
// map information
|
|
|
|
|
|
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 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>
|
2017-10-31 02:00:31 -07:00
|
|
|
|
/// <param name="fs">Stream of possible CHD</param>
|
2017-10-31 01:06:56 -07:00
|
|
|
|
/// <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>
|
2017-10-31 02:00:31 -07:00
|
|
|
|
public DatItem GetCHDInfo(Stream fs)
|
2017-10-31 01:06:56 -07:00
|
|
|
|
{
|
|
|
|
|
|
// Create a blank Disk to populate and return
|
|
|
|
|
|
Disk datItem = new Disk();
|
|
|
|
|
|
|
|
|
|
|
|
// Get a binary reader to make life easier
|
|
|
|
|
|
BinaryReader br = new BinaryReader(fs);
|
|
|
|
|
|
|
|
|
|
|
|
// Read and verify the CHD signature
|
2017-10-31 02:00:31 -07:00
|
|
|
|
m_signature = br.ReadUInt64();
|
|
|
|
|
|
if (m_signature != Constants.CHDSignature)
|
2017-10-31 01:06:56 -07:00
|
|
|
|
{
|
|
|
|
|
|
// throw CHDERR_INVALID_FILE;
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-10-31 02:00:31 -07:00
|
|
|
|
// Get the header size and version
|
|
|
|
|
|
m_headersize = br.ReadUInt32();
|
2017-10-31 01:06:56 -07:00
|
|
|
|
m_version = br.ReadUInt32();
|
|
|
|
|
|
|
2017-10-31 02:00:31 -07:00
|
|
|
|
// Create a placeholder for the extracted SHA-1
|
2017-10-31 01:06:56 -07:00
|
|
|
|
byte[] sha1 = new byte[20];
|
2017-10-31 02:00:31 -07:00
|
|
|
|
|
|
|
|
|
|
// If we have a CHD v3 file, parse it accordingly
|
|
|
|
|
|
if (m_headersize == Constants.CHD_V3_HEADER_SIZE && m_version == 3)
|
|
|
|
|
|
{
|
|
|
|
|
|
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)
|
2017-10-31 01:06:56 -07:00
|
|
|
|
{
|
2017-10-31 02:00:31 -07:00
|
|
|
|
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;
|
2017-10-31 01:06:56 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-10-31 02:00:31 -07:00
|
|
|
|
// Set the SHA-1 of the Disk to return
|
2017-10-31 01:06:56 -07:00
|
|
|
|
datItem.SHA1 = BitConverter.ToString(sha1).Replace("-", string.Empty).ToLowerInvariant();
|
|
|
|
|
|
|
|
|
|
|
|
return datItem;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-10-31 02:00:31 -07:00
|
|
|
|
/// Get if file is a valid CHD
|
2017-10-31 01:06:56 -07:00
|
|
|
|
/// </summary>
|
2017-10-31 02:00:31 -07:00
|
|
|
|
/// <param name="filename">Filename of possible CHD</param>
|
|
|
|
|
|
/// <returns>True if a the file is a valid CHD, false otherwise</returns>
|
|
|
|
|
|
public 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 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>
|
|
|
|
|
|
/// <param name="br">Binary reader representing the input stream</param>
|
|
|
|
|
|
/// <returns>The extracted SHA-1 on success, null otherwise</returns>
|
|
|
|
|
|
private byte[] ParseCHDv3Header(BinaryReader br)
|
2017-10-31 01:06:56 -07:00
|
|
|
|
{
|
|
|
|
|
|
// Set the blank SHA-1 hash
|
2017-10-31 02:00:31 -07:00
|
|
|
|
byte[] sha1 = new byte[20];
|
2017-10-31 01:06:56 -07:00
|
|
|
|
|
|
|
|
|
|
// Set offsets and defaults
|
|
|
|
|
|
m_mapoffset = 120;
|
|
|
|
|
|
m_mapentrybytes = 16;
|
|
|
|
|
|
|
|
|
|
|
|
// Read the CHD flags
|
|
|
|
|
|
uint flags = br.ReadUInt32();
|
|
|
|
|
|
|
|
|
|
|
|
// Determine compression
|
|
|
|
|
|
switch (br.ReadUInt32())
|
|
|
|
|
|
{
|
|
|
|
|
|
case 0: m_compression[0] = CHDCodecType.CHD_CODEC_NONE; break;
|
|
|
|
|
|
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;
|
2017-10-31 02:00:31 -07:00
|
|
|
|
default: /* throw CHDERR_UNKNOWN_COMPRESSION; */ return null;
|
2017-10-31 01:06:56 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
m_compression[1] = m_compression[2] = m_compression[3] = CHDCodecType.CHD_CODEC_NONE;
|
|
|
|
|
|
|
|
|
|
|
|
m_hunkcount = br.ReadUInt32();
|
|
|
|
|
|
m_logicalbytes = br.ReadUInt64();
|
|
|
|
|
|
m_metaoffset = br.ReadUInt64();
|
|
|
|
|
|
|
|
|
|
|
|
br.BaseStream.Seek(76, SeekOrigin.Begin);
|
|
|
|
|
|
m_hunkbytes = br.ReadUInt32();
|
|
|
|
|
|
|
2017-10-31 02:00:31 -07:00
|
|
|
|
br.BaseStream.Seek(Constants.CHDv3SHA1Offset, SeekOrigin.Begin);
|
2017-10-31 01:06:56 -07:00
|
|
|
|
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;
|
|
|
|
|
|
|
2017-10-31 02:00:31 -07:00
|
|
|
|
return sha1;
|
2017-10-31 01:06:56 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-10-31 02:00:31 -07:00
|
|
|
|
/// Parse a CHD v4 header
|
2017-10-31 01:06:56 -07:00
|
|
|
|
/// </summary>
|
2017-10-31 02:00:31 -07:00
|
|
|
|
/// <param name="br">Binary reader representing the input stream</param>
|
|
|
|
|
|
/// <returns>The extracted SHA-1 on success, null otherwise</returns>
|
|
|
|
|
|
private byte[] ParseCHDv4Header(BinaryReader br)
|
2017-10-31 01:06:56 -07:00
|
|
|
|
{
|
|
|
|
|
|
// Set the blank SHA-1 hash
|
2017-10-31 02:00:31 -07:00
|
|
|
|
byte[] sha1 = new byte[20];
|
2017-10-31 01:06:56 -07:00
|
|
|
|
|
|
|
|
|
|
// Set offsets and defaults
|
|
|
|
|
|
m_mapoffset = 108;
|
|
|
|
|
|
m_mapentrybytes = 16;
|
|
|
|
|
|
|
|
|
|
|
|
// Read the CHD flags
|
|
|
|
|
|
uint flags = br.ReadUInt32();
|
|
|
|
|
|
|
|
|
|
|
|
// Determine compression
|
|
|
|
|
|
switch (br.ReadUInt32())
|
|
|
|
|
|
{
|
|
|
|
|
|
case 0: m_compression[0] = CHDCodecType.CHD_CODEC_NONE; break;
|
|
|
|
|
|
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;
|
2017-10-31 02:00:31 -07:00
|
|
|
|
default: /* throw CHDERR_UNKNOWN_COMPRESSION; */ return null;
|
2017-10-31 01:06:56 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
m_compression[1] = m_compression[2] = m_compression[3] = CHDCodecType.CHD_CODEC_NONE;
|
|
|
|
|
|
|
|
|
|
|
|
m_hunkcount = br.ReadUInt32();
|
|
|
|
|
|
m_logicalbytes = br.ReadUInt64();
|
|
|
|
|
|
m_metaoffset = br.ReadUInt64();
|
|
|
|
|
|
|
|
|
|
|
|
br.BaseStream.Seek(44, SeekOrigin.Begin);
|
|
|
|
|
|
m_hunkbytes = br.ReadUInt32();
|
2017-10-31 02:00:31 -07:00
|
|
|
|
|
|
|
|
|
|
br.BaseStream.Seek(Constants.CHDv4SHA1Offset, SeekOrigin.Begin);
|
2017-10-31 01:06:56 -07:00
|
|
|
|
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;
|
2017-10-31 02:00:31 -07:00
|
|
|
|
return sha1;
|
2017-10-31 01:06:56 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-10-31 02:00:31 -07:00
|
|
|
|
/// Parse a CHD v5 header
|
2017-10-31 01:06:56 -07:00
|
|
|
|
/// </summary>
|
2017-10-31 02:00:31 -07:00
|
|
|
|
/// <param name="br">Binary reader representing the input stream</param>
|
|
|
|
|
|
/// <returns>The extracted SHA-1 on success, null otherwise</returns>
|
|
|
|
|
|
private byte[] ParseCHDv5Header(BinaryReader br)
|
2017-10-31 01:06:56 -07:00
|
|
|
|
{
|
|
|
|
|
|
// Set the blank SHA-1 hash
|
2017-10-31 02:00:31 -07:00
|
|
|
|
byte[] sha1 = new byte[20];
|
2017-10-31 01:06:56 -07:00
|
|
|
|
|
|
|
|
|
|
// Determine compression
|
|
|
|
|
|
m_compression[0] = (CHDCodecType)br.ReadUInt32();
|
|
|
|
|
|
m_compression[1] = (CHDCodecType)br.ReadUInt32();
|
|
|
|
|
|
m_compression[2] = (CHDCodecType)br.ReadUInt32();
|
|
|
|
|
|
m_compression[3] = (CHDCodecType)br.ReadUInt32();
|
|
|
|
|
|
|
|
|
|
|
|
m_logicalbytes = br.ReadUInt64();
|
|
|
|
|
|
m_mapoffset = br.ReadUInt64();
|
|
|
|
|
|
m_metaoffset = br.ReadUInt64();
|
|
|
|
|
|
m_hunkbytes = br.ReadUInt32();
|
|
|
|
|
|
m_hunkcount = (m_logicalbytes + m_hunkbytes - 1) / m_hunkbytes;
|
|
|
|
|
|
m_unitbytes = br.ReadUInt32();
|
|
|
|
|
|
m_unitcount = (m_logicalbytes + m_unitbytes - 1) / m_unitbytes;
|
|
|
|
|
|
|
|
|
|
|
|
// m_allow_writes = !compressed();
|
|
|
|
|
|
|
|
|
|
|
|
// determine properties of map entries
|
|
|
|
|
|
// m_mapentrybytes = compressed() ? 12 : 4;
|
|
|
|
|
|
|
2017-10-31 02:00:31 -07:00
|
|
|
|
br.BaseStream.Seek(Constants.CHDv5SHA1Offset, SeekOrigin.Begin);
|
2017-10-31 01:06:56 -07:00
|
|
|
|
sha1 = br.ReadBytes(20);
|
2017-10-31 02:00:31 -07:00
|
|
|
|
return sha1;
|
2017-10-31 01:06:56 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|