mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
[CHDFile] Make parsing values from CHD easier
This commit is contained in:
70
SabreTools.Library/External/CHDFile.cs
vendored
70
SabreTools.Library/External/CHDFile.cs
vendored
@@ -132,10 +132,8 @@ namespace SabreTools.Library.External
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get the header size and version
|
// Get the header size and version
|
||||||
byte[] headersizebytes = m_br.ReadBytes(4).Reverse().ToArray();
|
m_headersize = ReadUInt32();
|
||||||
m_headersize = BitConverter.ToUInt32(headersizebytes, 0);
|
m_version = ReadUInt32();
|
||||||
byte[] versionbytes = m_br.ReadBytes(4).Reverse().ToArray();
|
|
||||||
m_version = BitConverter.ToUInt32(versionbytes, 0);
|
|
||||||
|
|
||||||
// If we have an invalid combination of size and version
|
// If we have an invalid combination of size and version
|
||||||
if ((m_version == 3 && m_headersize != Constants.CHD_V3_HEADER_SIZE)
|
if ((m_version == 3 && m_headersize != Constants.CHD_V3_HEADER_SIZE)
|
||||||
@@ -190,7 +188,7 @@ namespace SabreTools.Library.External
|
|||||||
private byte[] ParseCHDv3Header()
|
private byte[] ParseCHDv3Header()
|
||||||
{
|
{
|
||||||
// Seek to after the signature to make sure we're reading the correct bytes
|
// Seek to after the signature to make sure we're reading the correct bytes
|
||||||
m_br.BaseStream.Seek(8, SeekOrigin.Begin);
|
m_br.BaseStream.Seek(16, SeekOrigin.Begin);
|
||||||
|
|
||||||
// Set the blank SHA-1 hash
|
// Set the blank SHA-1 hash
|
||||||
byte[] sha1 = new byte[20];
|
byte[] sha1 = new byte[20];
|
||||||
@@ -200,7 +198,7 @@ namespace SabreTools.Library.External
|
|||||||
m_mapentrybytes = 16;
|
m_mapentrybytes = 16;
|
||||||
|
|
||||||
// Read the CHD flags
|
// Read the CHD flags
|
||||||
uint flags = m_br.ReadUInt32();
|
uint flags = ReadUInt32();
|
||||||
|
|
||||||
// Determine compression
|
// Determine compression
|
||||||
switch (m_br.ReadUInt32())
|
switch (m_br.ReadUInt32())
|
||||||
@@ -214,12 +212,12 @@ namespace SabreTools.Library.External
|
|||||||
|
|
||||||
m_compression[1] = m_compression[2] = m_compression[3] = CHDCodecType.CHD_CODEC_NONE;
|
m_compression[1] = m_compression[2] = m_compression[3] = CHDCodecType.CHD_CODEC_NONE;
|
||||||
|
|
||||||
m_hunkcount = m_br.ReadUInt32();
|
m_hunkcount = ReadUInt32();
|
||||||
m_logicalbytes = m_br.ReadUInt64();
|
m_logicalbytes = ReadUInt64();
|
||||||
m_metaoffset = m_br.ReadUInt64();
|
m_metaoffset = ReadUInt32();
|
||||||
|
|
||||||
m_br.BaseStream.Seek(76, SeekOrigin.Begin);
|
m_br.BaseStream.Seek(76, SeekOrigin.Begin);
|
||||||
m_hunkbytes = m_br.ReadUInt32();
|
m_hunkbytes = ReadUInt32();
|
||||||
|
|
||||||
m_br.BaseStream.Seek(Constants.CHDv3SHA1Offset, SeekOrigin.Begin);
|
m_br.BaseStream.Seek(Constants.CHDv3SHA1Offset, SeekOrigin.Begin);
|
||||||
sha1 = m_br.ReadBytes(20);
|
sha1 = m_br.ReadBytes(20);
|
||||||
@@ -238,7 +236,7 @@ namespace SabreTools.Library.External
|
|||||||
private byte[] ParseCHDv4Header()
|
private byte[] ParseCHDv4Header()
|
||||||
{
|
{
|
||||||
// Seek to after the signature to make sure we're reading the correct bytes
|
// Seek to after the signature to make sure we're reading the correct bytes
|
||||||
m_br.BaseStream.Seek(8, SeekOrigin.Begin);
|
m_br.BaseStream.Seek(16, SeekOrigin.Begin);
|
||||||
|
|
||||||
// Set the blank SHA-1 hash
|
// Set the blank SHA-1 hash
|
||||||
byte[] sha1 = new byte[20];
|
byte[] sha1 = new byte[20];
|
||||||
@@ -248,7 +246,7 @@ namespace SabreTools.Library.External
|
|||||||
m_mapentrybytes = 16;
|
m_mapentrybytes = 16;
|
||||||
|
|
||||||
// Read the CHD flags
|
// Read the CHD flags
|
||||||
uint flags = m_br.ReadUInt32();
|
uint flags = ReadUInt32();
|
||||||
|
|
||||||
// Determine compression
|
// Determine compression
|
||||||
switch (m_br.ReadUInt32())
|
switch (m_br.ReadUInt32())
|
||||||
@@ -262,12 +260,12 @@ namespace SabreTools.Library.External
|
|||||||
|
|
||||||
m_compression[1] = m_compression[2] = m_compression[3] = CHDCodecType.CHD_CODEC_NONE;
|
m_compression[1] = m_compression[2] = m_compression[3] = CHDCodecType.CHD_CODEC_NONE;
|
||||||
|
|
||||||
m_hunkcount = m_br.ReadUInt32();
|
m_hunkcount = ReadUInt32();
|
||||||
m_logicalbytes = m_br.ReadUInt64();
|
m_logicalbytes = ReadUInt64();
|
||||||
m_metaoffset = m_br.ReadUInt64();
|
m_metaoffset = ReadUInt32();
|
||||||
|
|
||||||
m_br.BaseStream.Seek(44, SeekOrigin.Begin);
|
m_br.BaseStream.Seek(44, SeekOrigin.Begin);
|
||||||
m_hunkbytes = m_br.ReadUInt32();
|
m_hunkbytes = ReadUInt32();
|
||||||
|
|
||||||
m_br.BaseStream.Seek(Constants.CHDv4SHA1Offset, SeekOrigin.Begin);
|
m_br.BaseStream.Seek(Constants.CHDv4SHA1Offset, SeekOrigin.Begin);
|
||||||
sha1 = m_br.ReadBytes(20);
|
sha1 = m_br.ReadBytes(20);
|
||||||
@@ -285,23 +283,23 @@ namespace SabreTools.Library.External
|
|||||||
private byte[] ParseCHDv5Header()
|
private byte[] ParseCHDv5Header()
|
||||||
{
|
{
|
||||||
// Seek to after the signature to make sure we're reading the correct bytes
|
// Seek to after the signature to make sure we're reading the correct bytes
|
||||||
m_br.BaseStream.Seek(8, SeekOrigin.Begin);
|
m_br.BaseStream.Seek(16, SeekOrigin.Begin);
|
||||||
|
|
||||||
// Set the blank SHA-1 hash
|
// Set the blank SHA-1 hash
|
||||||
byte[] sha1 = new byte[20];
|
byte[] sha1 = new byte[20];
|
||||||
|
|
||||||
// Determine compression
|
// Determine compression
|
||||||
m_compression[0] = (CHDCodecType)m_br.ReadUInt32();
|
m_compression[0] = (CHDCodecType)ReadUInt32();
|
||||||
m_compression[1] = (CHDCodecType)m_br.ReadUInt32();
|
m_compression[1] = (CHDCodecType)ReadUInt32();
|
||||||
m_compression[2] = (CHDCodecType)m_br.ReadUInt32();
|
m_compression[2] = (CHDCodecType)ReadUInt32();
|
||||||
m_compression[3] = (CHDCodecType)m_br.ReadUInt32();
|
m_compression[3] = (CHDCodecType)ReadUInt32();
|
||||||
|
|
||||||
m_logicalbytes = m_br.ReadUInt64();
|
m_logicalbytes = ReadUInt64();
|
||||||
m_mapoffset = m_br.ReadUInt64();
|
m_mapoffset = ReadUInt64();
|
||||||
m_metaoffset = m_br.ReadUInt64();
|
m_metaoffset = ReadUInt64();
|
||||||
m_hunkbytes = m_br.ReadUInt32();
|
m_hunkbytes = ReadUInt32();
|
||||||
m_hunkcount = (m_logicalbytes + m_hunkbytes - 1) / m_hunkbytes;
|
m_hunkcount = (m_logicalbytes + m_hunkbytes - 1) / m_hunkbytes;
|
||||||
m_unitbytes = m_br.ReadUInt32();
|
m_unitbytes = ReadUInt32();
|
||||||
m_unitcount = (m_logicalbytes + m_unitbytes - 1) / m_unitbytes;
|
m_unitcount = (m_logicalbytes + m_unitbytes - 1) / m_unitbytes;
|
||||||
|
|
||||||
// m_allow_writes = !compressed();
|
// m_allow_writes = !compressed();
|
||||||
@@ -315,5 +313,25 @@ namespace SabreTools.Library.External
|
|||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region Helpers
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Read a proper UInt32 from the stream
|
||||||
|
/// </summary>
|
||||||
|
private uint ReadUInt32()
|
||||||
|
{
|
||||||
|
return BitConverter.ToUInt32(m_br.ReadBytes(4).Reverse().ToArray(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Read a proper UInt64 from the stream
|
||||||
|
/// </summary>
|
||||||
|
private ulong ReadUInt64()
|
||||||
|
{
|
||||||
|
return BitConverter.ToUInt64(m_br.ReadBytes(8).Reverse().ToArray(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user