diff --git a/SabreTools.IO.Test/Extensions/BinaryReaderExtensionsTests.cs b/SabreTools.IO.Test/Extensions/BinaryReaderExtensionsTests.cs index e9b0d74..b194e72 100644 --- a/SabreTools.IO.Test/Extensions/BinaryReaderExtensionsTests.cs +++ b/SabreTools.IO.Test/Extensions/BinaryReaderExtensionsTests.cs @@ -108,6 +108,50 @@ namespace SabreTools.IO.Test.Extensions Assert.Equal(expected, read); } +#if NET7_0_OR_GREATER + [Fact] + public void ReadInt128Test() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + var expected = new Int128(BitConverter.ToUInt64(_bytes, 0), BitConverter.ToUInt64(_bytes, 8)); + Int128 read = br.ReadInt128(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadInt128BigEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + var reversed = _bytes.Reverse().ToArray(); + var expected = new Int128(BitConverter.ToUInt64(reversed, 0), BitConverter.ToUInt64(reversed, 8)); + Int128 read = br.ReadInt128BigEndian(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadUInt128Test() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + var expected = new UInt128(BitConverter.ToUInt64(_bytes, 0), BitConverter.ToUInt64(_bytes, 8)); + UInt128 read = br.ReadUInt128(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadUInt128BigEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + var reversed = _bytes.Reverse().ToArray(); + var expected = new UInt128(BitConverter.ToUInt64(reversed, 0), BitConverter.ToUInt64(reversed, 8)); + UInt128 read = br.ReadUInt128BigEndian(); + Assert.Equal(expected, read); + } +#endif + // TODO: Add byte[], char[] tests // TODO: Add string reading tests } diff --git a/SabreTools.IO/Extensions/BinaryReaderExtensions.cs b/SabreTools.IO/Extensions/BinaryReaderExtensions.cs index 5d4c72f..19735be 100644 --- a/SabreTools.IO/Extensions/BinaryReaderExtensions.cs +++ b/SabreTools.IO/Extensions/BinaryReaderExtensions.cs @@ -7,7 +7,6 @@ namespace SabreTools.IO.Extensions /// Big endian reading overloads for BinaryReader /// /// TODO: Add U/Int24 and U/Int48 methods - /// TODO: Add U/Int128 methods public static class BinaryReaderExtensions { /// @@ -117,6 +116,21 @@ namespace SabreTools.IO.Extensions return BitConverter.ToDouble(retval, 0); } + /// + /// Reads in big-endian format + public static decimal ReadDecimalBigEndian(this BinaryReader reader) + { + byte[] retval = reader.ReadBytes(16); + Array.Reverse(retval); + + 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([i1, i2, i3, i4]); + } + /// /// Read a Guid from the underlying stream /// @@ -137,19 +151,47 @@ namespace SabreTools.IO.Extensions return new Guid(buffer); } - /// - /// Reads in big-endian format - public static decimal ReadDecimalBigEndian(this BinaryReader reader) + // TODO: Determine if the reverse reads are doing what are expected +#if NET7_0_OR_GREATER + /// + /// Read an Int128 from the underlying stream + /// + public static Int128 ReadInt128(this BinaryReader reader) { - byte[] retval = reader.ReadBytes(16); - Array.Reverse(retval); - - 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([i1, i2, i3, i4]); + byte[] buffer = reader.ReadBytes(16); + return new Int128(BitConverter.ToUInt64(buffer, 0), BitConverter.ToUInt64(buffer, 8)); } + + /// + /// Read an Int128 from the underlying stream + /// + /// Reads in big-endian format + public static Int128 ReadInt128BigEndian(this BinaryReader reader) + { + byte[] buffer = reader.ReadBytes(16); + Array.Reverse(buffer); + return new Int128(BitConverter.ToUInt64(buffer, 0), BitConverter.ToUInt64(buffer, 8)); + } + + /// + /// Read a UInt128 from the underlying stream + /// + public static UInt128 ReadUInt128(this BinaryReader reader) + { + byte[] buffer = reader.ReadBytes(16); + return new UInt128(BitConverter.ToUInt64(buffer, 0), BitConverter.ToUInt64(buffer, 8)); + } + + /// + /// Read a UInt128 from the underlying stream + /// + /// Reads in big-endian format + public static UInt128 ReadUInt128BigEndian(this BinaryReader reader) + { + byte[] buffer = reader.ReadBytes(16); + Array.Reverse(buffer); + return new UInt128(BitConverter.ToUInt64(buffer, 0), BitConverter.ToUInt64(buffer, 8)); + } +#endif } }