diff --git a/SabreTools.Library/FileTypes/CHDFile.cs b/SabreTools.Library/FileTypes/CHDFile.cs index b5c3eaf8..342d018a 100644 --- a/SabreTools.Library/FileTypes/CHDFile.cs +++ b/SabreTools.Library/FileTypes/CHDFile.cs @@ -2,6 +2,7 @@ using System.Linq; using SabreTools.Library.Data; +using SabreTools.Library.Tools; #if MONO using System.IO; @@ -132,8 +133,8 @@ namespace SabreTools.Library.FileTypes } // Get the header size and version - m_headersize = ReadUInt32(); - m_version = ReadUInt32(); + m_headersize = m_br.ReadUInt32Reverse(); + m_version = m_br.ReadUInt32Reverse(); // If we have an invalid combination of size and version if ((m_version == 3 && m_headersize != Constants.CHD_V3_HEADER_SIZE) @@ -198,7 +199,7 @@ namespace SabreTools.Library.FileTypes m_mapentrybytes = 16; // Read the CHD flags - uint flags = ReadUInt32(); + uint flags = m_br.ReadUInt32Reverse(); // Determine compression switch (m_br.ReadUInt32()) @@ -212,12 +213,12 @@ namespace SabreTools.Library.FileTypes m_compression[1] = m_compression[2] = m_compression[3] = CHDCodecType.CHD_CODEC_NONE; - m_hunkcount = ReadUInt32(); - m_logicalbytes = ReadUInt64(); - m_metaoffset = ReadUInt32(); + m_hunkcount = m_br.ReadUInt32Reverse(); + m_logicalbytes = m_br.ReadUInt64Reverse(); + m_metaoffset = m_br.ReadUInt32Reverse(); m_br.BaseStream.Seek(76, SeekOrigin.Begin); - m_hunkbytes = ReadUInt32(); + m_hunkbytes = m_br.ReadUInt32Reverse(); m_br.BaseStream.Seek(Constants.CHDv3SHA1Offset, SeekOrigin.Begin); sha1 = m_br.ReadBytes(20); @@ -246,7 +247,7 @@ namespace SabreTools.Library.FileTypes m_mapentrybytes = 16; // Read the CHD flags - uint flags = ReadUInt32(); + uint flags = m_br.ReadUInt32Reverse(); // Determine compression switch (m_br.ReadUInt32()) @@ -260,12 +261,12 @@ namespace SabreTools.Library.FileTypes m_compression[1] = m_compression[2] = m_compression[3] = CHDCodecType.CHD_CODEC_NONE; - m_hunkcount = ReadUInt32(); - m_logicalbytes = ReadUInt64(); - m_metaoffset = ReadUInt32(); + m_hunkcount = m_br.ReadUInt32Reverse(); + m_logicalbytes = m_br.ReadUInt64Reverse(); + m_metaoffset = m_br.ReadUInt32Reverse(); m_br.BaseStream.Seek(44, SeekOrigin.Begin); - m_hunkbytes = ReadUInt32(); + m_hunkbytes = m_br.ReadUInt32Reverse(); m_br.BaseStream.Seek(Constants.CHDv4SHA1Offset, SeekOrigin.Begin); sha1 = m_br.ReadBytes(20); @@ -289,17 +290,17 @@ namespace SabreTools.Library.FileTypes byte[] sha1 = new byte[20]; // Determine compression - m_compression[0] = (CHDCodecType)ReadUInt32(); - m_compression[1] = (CHDCodecType)ReadUInt32(); - m_compression[2] = (CHDCodecType)ReadUInt32(); - m_compression[3] = (CHDCodecType)ReadUInt32(); + m_compression[0] = (CHDCodecType)m_br.ReadUInt32Reverse(); + m_compression[1] = (CHDCodecType)m_br.ReadUInt32Reverse(); + m_compression[2] = (CHDCodecType)m_br.ReadUInt32Reverse(); + m_compression[3] = (CHDCodecType)m_br.ReadUInt32Reverse(); - m_logicalbytes = ReadUInt64(); - m_mapoffset = ReadUInt64(); - m_metaoffset = ReadUInt64(); - m_hunkbytes = ReadUInt32(); + m_logicalbytes = m_br.ReadUInt64Reverse(); + m_mapoffset = m_br.ReadUInt64Reverse(); + m_metaoffset = m_br.ReadUInt64Reverse(); + m_hunkbytes = m_br.ReadUInt32Reverse(); m_hunkcount = (m_logicalbytes + m_hunkbytes - 1) / m_hunkbytes; - m_unitbytes = ReadUInt32(); + m_unitbytes = m_br.ReadUInt32Reverse(); m_unitcount = (m_logicalbytes + m_unitbytes - 1) / m_unitbytes; // m_allow_writes = !compressed(); @@ -313,25 +314,5 @@ namespace SabreTools.Library.FileTypes } #endregion - - #region Helpers - - /// - /// Read a proper UInt32 from the stream - /// - private uint ReadUInt32() - { - return BitConverter.ToUInt32(m_br.ReadBytes(4).Reverse().ToArray(), 0); - } - - /// - /// Read a proper UInt64 from the stream - /// - private ulong ReadUInt64() - { - return BitConverter.ToUInt64(m_br.ReadBytes(8).Reverse().ToArray(), 0); - } - - #endregion } } diff --git a/SabreTools.Library/FileTypes/GZipArchive.cs b/SabreTools.Library/FileTypes/GZipArchive.cs index 59d59c2e..33c1d5f3 100644 --- a/SabreTools.Library/FileTypes/GZipArchive.cs +++ b/SabreTools.Library/FileTypes/GZipArchive.cs @@ -216,10 +216,9 @@ namespace SabreTools.Library.FileTypes Rom tempRom = new Rom(gamename, gamename, omitFromScan); BinaryReader br = new BinaryReader(FileTools.TryOpenRead(_filename)); br.BaseStream.Seek(-8, SeekOrigin.End); - byte[] headercrc = br.ReadBytes(4); - tempRom.CRC = BitConverter.ToString(headercrc.Reverse().ToArray()).Replace("-", string.Empty).ToLowerInvariant(); - byte[] headersize = br.ReadBytes(4); - tempRom.Size = BitConverter.ToInt32(headersize.Reverse().ToArray(), 0); + byte[] headercrc = br.ReadBytesReverse(4); + tempRom.CRC = Style.ByteArrayToString(headercrc); + tempRom.Size = br.ReadInt32Reverse(); br.Dispose(); found.Add(tempRom); @@ -387,8 +386,8 @@ namespace SabreTools.Library.FileTypes } // Now convert the data and get the right position - string gzmd5 = BitConverter.ToString(headermd5).Replace("-", string.Empty); - string gzcrc = BitConverter.ToString(headercrc).Replace("-", string.Empty); + string gzmd5 = Style.ByteArrayToString(headermd5); + string gzcrc = Style.ByteArrayToString(headercrc); long extractedsize = (long)headersz; Rom rom = new Rom diff --git a/SabreTools.Library/Items/Disk.cs b/SabreTools.Library/Items/Disk.cs index ff44b4d1..d59be1dd 100644 --- a/SabreTools.Library/Items/Disk.cs +++ b/SabreTools.Library/Items/Disk.cs @@ -1,6 +1,7 @@ -using System; +using System.Linq; using SabreTools.Library.Data; +using SabreTools.Library.Tools; namespace SabreTools.Library.Items { @@ -12,12 +13,12 @@ namespace SabreTools.Library.Items #region Private instance variables // Disk information - protected string _md5; - protected string _sha1; - protected string _sha256; - protected string _sha384; - protected string _sha512; - protected ItemStatus _itemStatus; + private byte[] _md5; // 16 bytes + private byte[] _sha1; // 20 bytes + private byte[] _sha256; // 32 bytes + private byte[] _sha384; // 48 bytes + private byte[] _sha512; // 64 bytes + private ItemStatus _itemStatus; #endregion @@ -26,28 +27,28 @@ namespace SabreTools.Library.Items // Disk information public string MD5 { - get { return _md5; } - set { _md5 = value; } + get { return _md5.IsNullOrEmpty() ? null : Style.ByteArrayToString(_md5); } + set { _md5 = Style.StringToByteArray(value); } } public string SHA1 { - get { return _sha1; } - set { _sha1 = value; } + get { return _sha1.IsNullOrEmpty() ? null : Style.ByteArrayToString(_sha1); } + set { _sha1 = Style.StringToByteArray(value); } } public string SHA256 { - get { return _sha256; } - set { _sha256 = value; } + get { return _sha256.IsNullOrEmpty() ? null : Style.ByteArrayToString(_sha256); } + set { _sha256 = Style.StringToByteArray(value); } } public string SHA384 { - get { return _sha384; } - set { _sha384 = value; } + get { return _sha384.IsNullOrEmpty() ? null : Style.ByteArrayToString(_sha384); } + set { _sha384 = Style.StringToByteArray(value); } } public string SHA512 { - get { return _sha512; } - set { _sha512 = value; } + get { return _sha512.IsNullOrEmpty() ? null : Style.ByteArrayToString(_sha512); } + set { _sha512 = Style.StringToByteArray(value); } } public ItemStatus ItemStatus { @@ -111,11 +112,11 @@ namespace SabreTools.Library.Items SourceID = this.SourceID, Source = this.Source, - MD5 = this.MD5, - SHA1 = this.SHA1, - SHA256 = this.SHA256, - SHA384 = this.SHA384, - SHA512 = this.SHA512, + _md5 = this._md5, + _sha1 = this._sha1, + _sha256 = this._sha256, + _sha384 = this._sha384, + _sha512 = this._sha512, ItemStatus = this.ItemStatus, }; } @@ -144,19 +145,19 @@ namespace SabreTools.Library.Items } // If we can determine that the disks have no non-empty hashes in common, we return false - if ((String.IsNullOrEmpty(_md5) || String.IsNullOrEmpty(newOther.MD5)) - && (String.IsNullOrEmpty(_sha1) || String.IsNullOrEmpty(newOther.SHA1)) - && (String.IsNullOrEmpty(_sha256) || String.IsNullOrEmpty(newOther.SHA256)) - && (String.IsNullOrEmpty(_sha384) || String.IsNullOrEmpty(newOther.SHA384)) - && (String.IsNullOrEmpty(_sha512) || String.IsNullOrEmpty(newOther.SHA512))) + if ((this._md5.IsNullOrEmpty() || newOther._md5.IsNullOrEmpty()) + && (this._sha1.IsNullOrEmpty() || newOther._sha1.IsNullOrEmpty()) + && (this._sha256.IsNullOrEmpty() || newOther._sha256.IsNullOrEmpty()) + && (this._sha384.IsNullOrEmpty() || newOther._sha384.IsNullOrEmpty()) + && (this._sha512.IsNullOrEmpty() || newOther._sha512.IsNullOrEmpty())) { dupefound = false; } - else if (((String.IsNullOrEmpty(_md5) || String.IsNullOrEmpty(newOther.MD5)) || this.MD5 == newOther.MD5) - && ((String.IsNullOrEmpty(this.SHA1) || String.IsNullOrEmpty(newOther.SHA1)) || this.SHA1 == newOther.SHA1) - && ((String.IsNullOrEmpty(this.SHA256) || String.IsNullOrEmpty(newOther.SHA256)) || this.SHA256 == newOther.SHA256) - && ((String.IsNullOrEmpty(this.SHA384) || String.IsNullOrEmpty(newOther.SHA384)) || this.SHA384 == newOther.SHA384) - && ((String.IsNullOrEmpty(this.SHA512) || String.IsNullOrEmpty(newOther.SHA512)) || this.SHA256 == newOther.SHA512)) + else if (((this._md5.IsNullOrEmpty() || newOther._md5.IsNullOrEmpty()) || Enumerable.SequenceEqual(this._md5, newOther._md5)) + && ((this._sha1.IsNullOrEmpty() || newOther._sha1.IsNullOrEmpty()) || Enumerable.SequenceEqual(this._sha1, newOther._sha1)) + && ((this._sha256.IsNullOrEmpty() || newOther._sha256.IsNullOrEmpty()) || Enumerable.SequenceEqual(this._sha256, newOther._sha256)) + && ((this._sha384.IsNullOrEmpty() || newOther._sha384.IsNullOrEmpty()) || Enumerable.SequenceEqual(this._sha384, newOther._sha384)) + && ((this._sha512.IsNullOrEmpty() || newOther._sha512.IsNullOrEmpty()) || Enumerable.SequenceEqual(this._sha512, newOther._sha512))) { dupefound = true; } diff --git a/SabreTools.Library/Items/Rom.cs b/SabreTools.Library/Items/Rom.cs index 503ccda8..bd3f5aed 100644 --- a/SabreTools.Library/Items/Rom.cs +++ b/SabreTools.Library/Items/Rom.cs @@ -1,6 +1,7 @@ -using System; +using System.Linq; using SabreTools.Library.Data; +using SabreTools.Library.Tools; namespace SabreTools.Library.Items { @@ -13,12 +14,12 @@ namespace SabreTools.Library.Items // Rom information private long _size; - private string _crc; - private string _md5; - private string _sha1; - private string _sha256; - private string _sha384; - private string _sha512; + private byte[] _crc; // 8 bytes + private byte[] _md5; // 16 bytes + private byte[] _sha1; // 20 bytes + private byte[] _sha256; // 32 bytes + private byte[] _sha384; // 48 bytes + private byte[] _sha512; // 64 bytes private string _date; private ItemStatus _itemStatus; @@ -34,33 +35,33 @@ namespace SabreTools.Library.Items } public string CRC { - get { return _crc; } - set { _crc = value; } + get { return _crc.IsNullOrEmpty() ? null : Style.ByteArrayToString(_crc); } + set { _crc = Style.StringToByteArray(value); } } public string MD5 { - get { return _md5; } - set { _md5 = value; } + get { return _md5.IsNullOrEmpty() ? null : Style.ByteArrayToString(_md5); } + set { _md5 = Style.StringToByteArray(value); } } public string SHA1 { - get { return _sha1; } - set { _sha1 = value; } + get { return _sha1.IsNullOrEmpty() ? null : Style.ByteArrayToString(_sha1); } + set { _sha1 = Style.StringToByteArray(value); } } public string SHA256 { - get { return _sha256; } - set { _sha256 = value; } + get { return _sha256.IsNullOrEmpty() ? null : Style.ByteArrayToString(_sha256); } + set { _sha256 = Style.StringToByteArray(value); } } public string SHA384 { - get { return _sha384; } - set { _sha384 = value; } + get { return _sha384.IsNullOrEmpty() ? null : Style.ByteArrayToString(_sha384); } + set { _sha384 = Style.StringToByteArray(value); } } public string SHA512 { - get { return _sha512; } - set { _sha512 = value; } + get { return _sha512.IsNullOrEmpty() ? null : Style.ByteArrayToString(_sha512); } + set { _sha512 = Style.StringToByteArray(value); } } public string Date { @@ -103,27 +104,27 @@ namespace SabreTools.Library.Items _size = -1; if ((omitFromScan & Hash.CRC) == 0) { - _crc = "null"; + _crc = null; } if ((omitFromScan & Hash.MD5) == 0) { - _md5 = "null"; + _md5 = null; } if ((omitFromScan & Hash.SHA1) == 0) { - _sha1 = "null"; + _sha1 = null; } if ((omitFromScan & Hash.SHA256) == 0) { - _sha256 = "null"; + _sha256 = null; } if ((omitFromScan & Hash.SHA384) == 0) { - _sha384 = "null"; + _sha384 = null; } if ((omitFromScan & Hash.SHA512) == 0) { - _sha512 = "null"; + _sha512 = null; } _itemStatus = ItemStatus.None; @@ -176,12 +177,12 @@ namespace SabreTools.Library.Items Source = this.Source, Size = this.Size, - CRC = this.CRC, - MD5 = this.MD5, - SHA1 = this.SHA1, - SHA256 = this.SHA256, - SHA384 = this.SHA384, - SHA512 = this.SHA512, + _crc = this._crc, + _md5 = this._md5, + _sha1 = this._sha1, + _sha256 = this._sha256, + _sha384 = this._sha384, + _sha512 = this._sha512, ItemStatus = this.ItemStatus, Date = this.Date, }; @@ -211,22 +212,22 @@ namespace SabreTools.Library.Items } // If we can determine that the roms have no non-empty hashes in common, we return false - if ((String.IsNullOrEmpty(_crc) || String.IsNullOrEmpty(newOther.CRC)) - && (String.IsNullOrEmpty(_md5) || String.IsNullOrEmpty(newOther.MD5)) - && (String.IsNullOrEmpty(_sha1) || String.IsNullOrEmpty(newOther.SHA1)) - && (String.IsNullOrEmpty(_sha256) || String.IsNullOrEmpty(newOther.SHA256)) - && (String.IsNullOrEmpty(_sha384) || String.IsNullOrEmpty(newOther.SHA384)) - && (String.IsNullOrEmpty(_sha512) || String.IsNullOrEmpty(newOther.SHA512))) + if ((_crc.IsNullOrEmpty() || newOther._crc.IsNullOrEmpty()) + && (this._md5.IsNullOrEmpty() || newOther._md5.IsNullOrEmpty()) + && (this._sha1.IsNullOrEmpty() || newOther._sha1.IsNullOrEmpty()) + && (this._sha256.IsNullOrEmpty() || newOther._sha256.IsNullOrEmpty()) + && (this._sha384.IsNullOrEmpty() || newOther._sha384.IsNullOrEmpty()) + && (this._sha512.IsNullOrEmpty() || newOther._sha512.IsNullOrEmpty())) { dupefound = false; } else if ((this.Size == newOther.Size) - && ((String.IsNullOrEmpty(_crc) || String.IsNullOrEmpty(newOther.CRC)) || _crc == newOther.CRC) - && ((String.IsNullOrEmpty(_md5) || String.IsNullOrEmpty(newOther.MD5)) || _md5 == newOther.MD5) - && ((String.IsNullOrEmpty(_sha1) || String.IsNullOrEmpty(newOther.SHA1)) || _sha1 == newOther.SHA1) - && ((String.IsNullOrEmpty(_sha256) || String.IsNullOrEmpty(newOther.SHA256)) || _sha256 == newOther.SHA256) - && ((String.IsNullOrEmpty(_sha384) || String.IsNullOrEmpty(newOther.SHA384)) || _sha384 == newOther.SHA384) - && ((String.IsNullOrEmpty(_sha512) || String.IsNullOrEmpty(newOther.SHA512)) || _sha512 == newOther.SHA512)) + && ((this._crc.IsNullOrEmpty() || newOther._crc.IsNullOrEmpty()) || Enumerable.SequenceEqual(this._crc, newOther._crc)) + && ((this._md5.IsNullOrEmpty() || newOther._md5.IsNullOrEmpty()) || Enumerable.SequenceEqual(this._md5, newOther._md5)) + && ((this._sha1.IsNullOrEmpty() || newOther._sha1.IsNullOrEmpty()) || Enumerable.SequenceEqual(this._sha1, newOther._sha1)) + && ((this._sha256.IsNullOrEmpty() || newOther._sha256.IsNullOrEmpty()) || Enumerable.SequenceEqual(this._sha256, newOther._sha256)) + && ((this._sha384.IsNullOrEmpty() || newOther._sha384.IsNullOrEmpty()) || Enumerable.SequenceEqual(this._sha384, newOther._sha384)) + && ((this._sha512.IsNullOrEmpty() || newOther._sha512.IsNullOrEmpty()) || Enumerable.SequenceEqual(this._sha512, newOther._sha512))) { dupefound = true; } diff --git a/SabreTools.Library/Tools/FileTools.cs b/SabreTools.Library/Tools/FileTools.cs index 2c1b56f9..729aeb2f 100644 --- a/SabreTools.Library/Tools/FileTools.cs +++ b/SabreTools.Library/Tools/FileTools.cs @@ -720,10 +720,7 @@ namespace SabreTools.Library.Tools // Extract the header as a string for the database byte[] hbin = br.ReadBytes((int)rule.StartOffset); - for (int i = 0; i < (int)rule.StartOffset; i++) - { - hstr += BitConverter.ToString(new byte[] { hbin[i] }); - } + hstr = Style.ByteArrayToString(hbin); br.Dispose(); // Apply the rule to the file @@ -1199,27 +1196,27 @@ namespace SabreTools.Library.Tools if ((omitFromScan & Hash.MD5) == 0) { md5.TransformFinalBlock(buffer, 0, 0); - rom.MD5 = BitConverter.ToString(md5.Hash).Replace("-", "").ToLowerInvariant(); + rom.MD5 = Style.ByteArrayToString(md5.Hash); } if ((omitFromScan & Hash.SHA1) == 0) { sha1.TransformFinalBlock(buffer, 0, 0); - rom.SHA1 = BitConverter.ToString(sha1.Hash).Replace("-", "").ToLowerInvariant(); + rom.SHA1 = Style.ByteArrayToString(sha1.Hash); } if ((omitFromScan & Hash.SHA256) == 0) { sha256.TransformFinalBlock(buffer, 0, 0); - rom.SHA256 = BitConverter.ToString(sha256.Hash).Replace("-", "").ToLowerInvariant(); + rom.SHA256 = Style.ByteArrayToString(sha256.Hash); } if ((omitFromScan & Hash.SHA384) == 0) { sha384.TransformFinalBlock(buffer, 0, 0); - rom.SHA384 = BitConverter.ToString(sha384.Hash).Replace("-", "").ToLowerInvariant(); + rom.SHA384 = Style.ByteArrayToString(sha384.Hash); } if ((omitFromScan & Hash.SHA512) == 0) { sha512.TransformFinalBlock(buffer, 0, 0); - rom.SHA512 = BitConverter.ToString(sha512.Hash).Replace("-", "").ToLowerInvariant(); + rom.SHA512 = Style.ByteArrayToString(sha512.Hash); } if ((omitFromScan & Hash.xxHash) == 0) { @@ -1283,7 +1280,7 @@ namespace SabreTools.Library.Tools byte[] sha1 = chd.GetSHA1FromHeader(); // Set the SHA-1 of the Disk to return - datItem.SHA1 = (sha1 == null ? null : BitConverter.ToString(sha1).Replace("-", string.Empty).ToLowerInvariant()); + datItem.SHA1 = (sha1 == null ? null : Style.ByteArrayToString(sha1)); return datItem; } diff --git a/SabreTools.Library/Tools/Style.cs b/SabreTools.Library/Tools/Style.cs index ab5aea83..221c7949 100644 --- a/SabreTools.Library/Tools/Style.cs +++ b/SabreTools.Library/Tools/Style.cs @@ -14,6 +14,7 @@ using System.IO; #else using Alphaleonis.Win32.Filesystem; +using BinaryReader = System.IO.BinaryReader; using FileStream = System.IO.FileStream; #endif @@ -399,6 +400,162 @@ namespace SabreTools.Library.Tools #endregion + #region Extensions + + /// + /// Reads the specified number of bytes from the stream, starting from a specified point in the byte array. + /// + /// The buffer to read data into. + /// The starting point in the buffer at which to begin reading into the buffer. + /// The number of bytes to read. + /// The number of bytes read into buffer. This might be less than the number of bytes requested if that many bytes are not available, or it might be zero if the end of the stream is reached. + public static int ReadReverse(this BinaryReader reader, byte[] buffer, int index, int count) + { + int retval = reader.Read(buffer, index, count); + buffer = buffer.Reverse().ToArray(); + return retval; + } + + /// + /// Reads the specified number of characters from the stream, starting from a specified point in the character array. + /// + /// The buffer to read data into. + /// The starting point in the buffer at which to begin reading into the buffer. + /// The number of characters to read. + /// The total number of characters read into the buffer. This might be less than the number of characters requested if that many characters are not currently available, or it might be zero if the end of the stream is reached. + public static int ReadReverse(this BinaryReader reader, char[] buffer, int index, int count) + + { + int retval = reader.Read(buffer, index, count); + buffer = buffer.Reverse().ToArray(); + return retval; + } + + /// + /// Reads the specified number of bytes from the current stream into a byte array and advances the current position by that number of bytes. + /// + /// The number of bytes to read. This value must be 0 or a non-negative number or an exception will occur. + /// A byte array containing data read from the underlying stream. This might be less than the number of bytes requested if the end of the stream is reached. + public static byte[] ReadBytesReverse(this BinaryReader reader, int count) + { + byte[] retval = reader.ReadBytes(count); + retval = retval.Reverse().ToArray(); + return retval; + } + + /// + /// Reads a decimal value from the current stream and advances the current position of the stream by sixteen bytes. + /// + /// A decimal value read from the current stream. + public static decimal ReadDecimalReverse(this BinaryReader reader) + { + byte[] retval = reader.ReadBytes(16); + retval = retval.Reverse().ToArray(); + + int i1 = BitConverter.ToInt32(retval, 0); + int i2 = BitConverter.ToInt32(retval, 4); + int i3 = BitConverter.ToInt32(retval, 8); + int i4 = BitConverter.ToInt32(retval, 12); + + return new decimal(new int[] { i1, i2, i3, i4 }); + } + + /// + /// eads an 8-byte floating point value from the current stream and advances the current position of the stream by eight bytes. + /// + /// An 8-byte floating point value read from the current stream. + public static double ReadDoubleReverse(this BinaryReader reader) + { + byte[] retval = reader.ReadBytes(8); + retval = retval.Reverse().ToArray(); + return BitConverter.ToDouble(retval, 0); + } + + /// + /// Reads a 2-byte signed integer from the current stream and advances the current position of the stream by two bytes. + /// + /// A 2-byte signed integer read from the current stream. + public static short ReadInt16Reverse(this BinaryReader reader) + { + byte[] retval = reader.ReadBytes(2); + retval = retval.Reverse().ToArray(); + return BitConverter.ToInt16(retval, 0); + } + + /// + /// Reads a 4-byte signed integer from the current stream and advances the current position of the stream by four bytes. + /// + /// A 4-byte signed integer read from the current stream. + public static int ReadInt32Reverse(this BinaryReader reader) + { + byte[] retval = reader.ReadBytes(4); + retval = retval.Reverse().ToArray(); + return BitConverter.ToInt32(retval, 0); + } + + /// + /// Reads an 8-byte signed integer from the current stream and advances the current position of the stream by eight bytes. + /// + /// An 8-byte signed integer read from the current stream. + public static long ReadInt64Reverse(this BinaryReader reader) + { + byte[] retval = reader.ReadBytes(8); + retval = retval.Reverse().ToArray(); + return BitConverter.ToInt64(retval, 0); + } + + /// + /// Reads a 4-byte floating point value from the current stream and advances the current position of the stream by four bytes. + /// + /// A 4-byte floating point value read from the current stream. + public static float ReadSingleReverse(this BinaryReader reader) + { + byte[] retval = reader.ReadBytes(4); + retval = retval.Reverse().ToArray(); + return BitConverter.ToSingle(retval, 0); + } + + /// + /// Reads a 2-byte unsigned integer from the current stream using little-endian encoding and advances the position of the stream by two bytes. + /// + /// This API is not CLS-compliant. + /// + /// A 2-byte unsigned integer read from this stream. + public static ushort ReadUInt16Reverse(this BinaryReader reader) + { + byte[] retval = reader.ReadBytes(2); + retval = retval.Reverse().ToArray(); + return BitConverter.ToUInt16(retval, 0); + } + + /// + /// Reads a 4-byte unsigned integer from the current stream and advances the position of the stream by four bytes. + /// + /// This API is not CLS-compliant. + /// + /// A 4-byte unsigned integer read from this stream. + public static uint ReadUInt32Reverse(this BinaryReader reader) + { + byte[] retval = reader.ReadBytes(4); + retval = retval.Reverse().ToArray(); + return BitConverter.ToUInt32(retval, 0); + } + + /// + /// Reads an 8-byte unsigned integer from the current stream and advances the position of the stream by eight bytes. + /// + /// This API is not CLS-compliant. + /// + /// An 8-byte unsigned integer read from this stream. + public static ulong ReadUInt64Reverse(this BinaryReader reader) + { + byte[] retval = reader.ReadBytes(8); + retval = retval.Reverse().ToArray(); + return BitConverter.ToUInt64(retval, 0); + } + + #endregion + #region String Manipulation /// @@ -935,16 +1092,39 @@ namespace SabreTools.Library.Tools return readable.ToString("0.### ") + suffix; } + /// + /// http://stackoverflow.com/questions/311165/how-do-you-convert-byte-array-to-hexadecimal-string-and-vice-versa + /// + public static string ByteArrayToString(byte[] bytes) + { + try + { + string hex = BitConverter.ToString(bytes); + return hex.Replace("-", string.Empty).ToLowerInvariant(); + } + catch + { + return null; + } + } + /// /// http://stackoverflow.com/questions/311165/how-do-you-convert-byte-array-to-hexadecimal-string-and-vice-versa /// public static byte[] StringToByteArray(string hex) { - int NumberChars = hex.Length; - byte[] bytes = new byte[NumberChars / 2]; - for (int i = 0; i < NumberChars; i += 2) - bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16); - return bytes; + try + { + int NumberChars = hex.Length; + byte[] bytes = new byte[NumberChars / 2]; + for (int i = 0; i < NumberChars; i += 2) + bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16); + return bytes; + } + catch + { + return null; + } } /// @@ -1061,6 +1241,17 @@ namespace SabreTools.Library.Tools return localTime; } + /// + /// Indicates whether the specified array is null or has a length of zero. + /// https://stackoverflow.com/questions/8560106/isnullorempty-equivalent-for-array-c-sharp + /// + /// The array to test. + /// true if the array parameter is null or has a length of zero; otherwise, false. + public static bool IsNullOrEmpty(this Array array) + { + return (array == null || array.Length == 0); + } + #endregion } } diff --git a/SabreTools/SabreTools.Inits.cs b/SabreTools/SabreTools.Inits.cs index 01b38cc4..7468bee6 100644 --- a/SabreTools/SabreTools.Inits.cs +++ b/SabreTools/SabreTools.Inits.cs @@ -338,7 +338,7 @@ namespace SabreTools SplitType splitType, bool chdsAsFiles) { // Get the archive scanning level - ArchiveScanLevel asl = ArchiveTools.GetArchiveScanLevelFromNumbers(sevenzip, gz, rar, zip); + ArchiveScanLevel asl = FileTools.GetArchiveScanLevelFromNumbers(sevenzip, gz, rar, zip); // Get a list of files from the input datfiles datfiles = FileTools.GetOnlyFilesFromInputs(datfiles); @@ -749,7 +749,7 @@ namespace SabreTools string headerToCheckAgainst, SplitType splitType, bool chdsAsFiles) { // Get the archive scanning level - ArchiveScanLevel asl = ArchiveTools.GetArchiveScanLevelFromNumbers(1, 1, 1, 1); + ArchiveScanLevel asl = FileTools.GetArchiveScanLevelFromNumbers(1, 1, 1, 1); // Get a list of files from the input datfiles datfiles = FileTools.GetOnlyFilesFromInputs(datfiles);